Entry Points
d_open() takes several arguments, the formal list looks something like:
d_close() takes the same argument list as d_open():
d_read() and d_write take the following argument lists:
It's argument list is as follows:
There are four different types of ioctl's which can be implemented.
<sys/ioccom.h> contains convenience macros for defining these ioctls.
d_stop()
d_poll()'s argument list is as follows:
d_poll() is used to find out if a device is ready for IO. An
example is waiting for data to become available from the network, or
waiting for the user to press a key. This correspond to the poll()
call in userland.
The d_poll() call should check for the events specified in the
event mask. If none of the requested events are active, but they may
become active later, it should record this for the kernel's later
persual. d_poll() does this by calling selrecord() with a selinfo
structure for this device. The sum of all these activities look
something like this:
d_select() is used in 2.2 and previous versions of FreeBSD. Instead
of 'events' it take a single int 'rw', which can be FREAD for reads
(as in POLLIN above), FWRITE for write (as in POLLOUT above), and 0
for 'exception' - something exceptional happened, like a card being
inserted or removed for the pccard driver.
For select, the above code fragment would look like this:
d_strategy()'s argument list is as follows:
d_strategy() is used for devices which use some form of scatter-gather
io. It is most common in a block device. This is significantly different
than the System V model, where only the block driver performs scatter-gather
io. Under BSD, character devices are sometimes requested to perform
scatter-gather io via the readv() and writev() system calls.
Entry Points
Described in the Character device section.
Described in the Character device section.
Described in the Character device section.
Described in the Character device section.
Entry Points
Entry Points
probe() takes a attach()
attach() also takes a Header Files
Entry Points
Described in the ISA device section.
Described in the ISA device section.
Entry Points
Entry Points
Entry Points
In FreeBSD, support for the ISA and EISA busses is i386 specific.
While FreeBSD itself is presently available on the i386 platform,
some effort has been made to make the PCI, PCCARD, and SCSI code
portable. The ISA and EISA specific code resides in
/usr/src/sys/i386/isa and /usr/src/sys/i386/eisa respectively.
The machine independent PCI, PCCARD, and SCSI code reside in
/usr/src/sys/{pci,pccard,scsi}. The i386 specific code for these
reside in /usr/src/sys/i386/{pci,pccard,scsi}.
In FreeBSD, a device driver can be either binary or source. There is
no ``official'' place for binary drivers to reside. BSD/OS uses
something like sys/i386/OBJ. Since most drivers are distributed
in source, the following discussion refers to a source driver.
Binary only drivers are sometimes provided by hardware vendors
who wish to maintain the source as proprietary.
A typical driver has the source code in one c-file, say dev.c. The
driver also can have some include files; devreg.h typically contains
public device register declarations, macros, and other driver
specific declarations. Some drivers call this devvar.h instead.
Some drivers, such as the dgb (for the Digiboard PC/Xe),
require microcode to be loaded onto the board. For the dgb driver
the microcode is compiled and dumped into a header file ala
If the driver has data structures and ioctl's which are specific to
the driver/device, and need to be accessible from user-space, they
should be put in a separate include file which will reside in
/usr/include/machine/ (some of these reside in /usr/include/sys/).
These are typically named something like ioctl_dev.h or devio.h.
If a driver is being written which, from user space is
identical to a device which already exists, care should be taken to
use the same ioctl interface and data structures. For example, from
user space, a SCSI CDROM drive should be identical to an IDE cdrom
drive; or a serial line on an intelligent multiport card (Digiboard,
Cyclades, ...) should be identical to the sio devices. These devices
have a fairly well defined interface which should be used.
There are two methods for linking a driver into the kernel, static and
the LKM model. The first method is fairly standard across the
*BSD family. The other method was originally developed by Sun
(I believe), and has been implemented into BSD using the Sun model.
I don't believe that the current implementation uses any Sun code.
The steps required to add your driver to the standard FreeBSD kernel are
The standard model for adding a device driver to the Berkeley kernel
is to add your driver to the list of known devices. This list is
dependent on the CPU architecture. If the device is not i386 specific
(PCCARD, PCI, SCSI), the file is in ``/usr/src/sys/conf/files''.
If the device is i386 specific, use ``/usr/src/sys/i386/conf/files.i386''.
A typical line looks like:
Now you must edit ``/usr/src/sys/i386/i386/conf.c'' to make an entry
for your driver. Somewhere near the top, you need to declare your
entry points. The entry for the joystick driver is:
This is simply adding a line describing your device.
The joystick description line is:
Now with our config file in hand, we can create a kernel compile directory.
This is done by simply typing:
On FreeBSD, you are responsible for making your own device nodes. The
major number of your device is determined by the slot number in the
device switch. Minor number is driver dependent, of course. You can
either run the mknod's from the command line, or add a section to
/dev/MAKEDEV.local, or even /dev/MAKEDEV to do the work. I sometimes
create a MAKEDEV.dev script that can be run stand-alone or pasted
into /dev/MAKEDEV.local
This is the easy part. There are a number of ways to do this, reboot,
fastboot, shutdown -r, cycle the power, etc. Upon bootup you should
see your XXprobe() called, and if all is successful, your XXattach()
too.
There are really no defined procedures for writing an LKM driver. The
following is my own conception after experimenting with the LKM device
interface and looking at the standard device driver model, this is one
way of adding an LKM interface to an existing driver without touching
the original driver source (or binary). It is recommended though,
that if you plan to release source to your driver, the LKM specific
parts should be part of the driver itself, conditionally compiled
on the LKM macro (i.e. #ifdef LKM).
This section will focus on writing the LKM specific part of the driver. We
will assume that we have written a driver which will drop into the standard
device driver model, which we would now like to implement as an LKM. We will
use the pcaudio driver as a sample driver, and develop an LKM front-end. The
source and makefile for the pcaudio LKM, ``pcaudio_lkm.c'' and ``Makefile'',
should be placed in /usr/src/lkm/pcaudio. What follows is a breakdown of
pcaudio_lkm.c.
Lines 17 - 26
-- This includes the file ``pca.h'' and conditionally compiles the rest
of the LKM on whether or not we have a pcaudio device defined. This
mimics the behavior of config. In a standard device driver,
Note: this has gone through
FreeBSD Kernel Sources http://www.freebsd.org
NetBSD Kernel Sources http://www.netbsd.org
Writing Device Drivers: Tutorial and Reference;
Tim Burke, Mark A. Parenti, Al, Wojtas;
Digital Press, ISBN 1-55558-141-2.
Writing A Unix Device Driver;
Janet I. Egan, Thomas J. Teixeira;
John Wiley & Sons, ISBN 0-471-62859-X.
Writing Device Drivers for SCO Unix;
Peter Kettle;
int
d_open(dev_t dev, int flag, int mode, struct proc *p)
d_open() is called on
The
The
manual page. It is recommended that you check these for access modes
in <sys/fcntl.h> and do what is required. For example if
The d_close()
int
d_close(dev_t dev , int flag , int mode , struct proc *p)
d_close() is only called on the last close of your device (per minor
device). For example in the following code fragment, d_open() is called
3 times, but d_close() is called only once.
...
fd1=open("/dev/mydev", O_RDONLY);
fd2=open("/dev/mydev", O_RDONLY);
fd3=open("/dev/mydev", O_RDONLY);
...
The arguments are similar to those described above for
d_open().
int
d_read(dev_t dev, struct uio *uio, int flat)
int
d_write(dev_t dev, struct uio *uio, int flat)
The d_read() and d_write() entry points are called when
int
d_ioctl(dev_t dev, int cmd, caddr_t arg, int flag, struct proc *p)
d_ioctl() is a catch-all for operations which don't make sense in
a read/write paradigm. Probably the most famous of all ioctl's is on
tty devices, through
void
d_poll(dev_t dev, int events, struct proc *p)
static struct my_softc {
struct queue rx_queue; /* As example only - not required */
struct queue tx_queue; /* As example only - not required */
struct selinfo selp; /* Required */
} my_softc[NMYDEV];
...
static int
mydevpoll(dev_t dev, int events, struct proc *p)
{
int revents = 0; /* Events we found */
int s;
struct my_softc *sc = &my_softc[dev];
/* We can only check for IN and OUT */
if ((events & (POLLIN|POLLOUT)) == 0)
return(POLLNVAL);
s = splhigh();
/* Writes are if the transmit queue can take them */
if ((events & POLLOUT) &&
!IF_QFULL(sc->tx_queue))
revents |= POLLOUT;
/* ... while reads are OK if we have any data */
if ((events & POLLIN) &&
!IF_QEMPTY(sc->rx_queue))
revents |= POLLIN;
if (revents == 0)
selrecord(p, &sc->selp);
splx(s);
return revents;
}
static int
mydevselect(dev_t dev, int rw, struct proc *p)
{
int ret = 0;
int s;
struct my_softc *sc = &my_softc[dev];
s = splhigh();
switch (rw) {
case FWRITE:
/* Writes are if the transmit queue can take them */
if (!IF_QFULL(sc->tx_queue))
ret = 1;
break;
case FREAD:
/* ... while reads are OK if we have any data */
if (!IF_QEMPTY(sc->rx_queue))
ret = 1;
break;
case 0:
/* This driver never get any exceptions */
break;
+ }
if(ret == 0)
selrecord(p, &sc->selp);
splx(s);
return(revents);
}
void
d_strategy(struct buf *bp)
struct isa_device {
int id_id; /* device id */
struct isa_driver *id_driver;
int id_iobase; /* base i/o address */
u_short id_irq; /* interrupt request */
short id_drq; /* DMA request */
caddr_t id_maddr; /* physical i/o memory address on bus (if any)*/
int id_msize; /* size of i/o memory */
inthand2_t *id_intr; /* interrupt interface routine */
int id_unit; /* unit number */
int id_flags; /* flags */
int id_scsiid; /* scsi id if needed */
int id_alive; /* device is present */
#define RI_FAST 1 /* fast interrupt handler */
u_int id_ri_flags; /* flags for register_intr() */
int id_reconfig; /* hot eject device support (such as PCMCIA) */
int id_enabled; /* is device enabled */
int id_conflicts; /* we're allowed to conflict with things */
struct isa_device *id_next; /* used in isa_devlist in userconfig() */
};
struct isa_driver {
int (*probe) __P((struct isa_device *idp));
/* test whether device is present */
int (*attach) __P((struct isa_device *idp));
/* setup driver for a device */
char *name; /* device name */
int sensitive_hw; /* true if other probes confuse us */
};
This is the structure used by the probe/attach code to detect and
initialize your device. The ). The
struct isa_driver mcddriver = { mcd_probe, mcd_attach, "mcd" };
i386/isa/joy.c optional joy device-driver
#include "joy.h"
#if NJOY > 0
d_open_t joyopen;
d_close_t joyclose;
d_rdwr_t joyread;
d_ioctl_t joyioctl;
#else
#define joyopen nxopen
#define joyclose nxclose
#define joyread nxread
#define joyioctl nxioctl
#endif
This either defines your entry points, or null entry points which
will return ENXIO when called (the #else clause).
The include file ``joy.h'' is automatically generated by
#define NJOY 1
or
#define NJOY 0
which defines the number of your devices in your kernel.
You must additionally add a slot to either cdevsw[&rsqb, or to
bdevsw[&rsqb, depending on whether it is a character device or
a block device, or both if it is a block device with a raw interface.
The entry for the joystick driver is:
/* open, close, read, write, ioctl, stop, reset, ttys, select, mmap, strat */
struct cdevsw cdevsw[] =
{
...
{ joyopen, joyclose, joyread, nowrite, /*51*/
joyioctl, nostop, nullreset, nodevtotty,/*joystick */
seltrue, nommap, NULL},
...
}
Order is what determines the major number of your device. Which is why
there will always be an entry for your driver, either null entry
points, or actual entry points. It is probably worth noting that this
is significantly different from SCO and other system V derivatives,
where any device can (in theory) have any major number. This is
largely a convenience on FreeBSD, due to the way device nodes are
created. More on this later.
17 /*
18 * figure out how many devices we have..
19 */
20
21 #include "pca.h"
22
23 /*
24 * if we have at least one ...
25 */
26 #if NPCA > 0
Lines 27 - 37
-- Includes required files from various include directories.
27 #include
Lines 38 - 51
-- Declares the device driver entry points as external.
38 /*
39 * declare your entry points as externs
40 */
41
42 extern int pcaprobe(struct isa_device *);
43 extern int pcaattach(struct isa_device *);
44 extern int pcaopen(dev_t, int, int, struct proc *);
45 extern int pcaclose(dev_t, int, int, struct proc *);
46 extern int pcawrite(dev_t, struct uio *, int);
47 extern int pcaioctl(dev_t, int, caddr_t);
48 extern int pcaselect(dev_t, int, struct proc *);
49 extern void pcaintr(struct clockframe *);
50 extern struct isa_driver pcadriver;
51
Lines 52 - 70
-- This is creates the device switch entry table for your driver.
This table gets swapped wholesale into the system device switch at
the location specified by your major number. In the standard model,
these are in /usr/src/sys/i386/i386/conf.c. NOTE: you cannot pick a
device major number higher than what exists in conf.c, for example at
present, conf.c rev 1.85, there are 67 slots for character devices,
you cannot use a (character) major device number 67 or greater,
without first reserving space in conf.c.
52 /*
53 * build your device switch entry table
54 */
55
56 static struct cdevsw pcacdevsw = {
57 (d_open_t *) pcaopen, /* open */
58 (d_close_t *) pcaclose, /* close */
59 (d_rdwr_t *) enodev, /* read */
60 (d_rdwr_t *) pcawrite, /* write */
61 (d_ioctl_t *) pcaioctl, /* ioctl */
62 (d_stop_t *) enodev, /* stop?? */
63 (d_reset_t *) enodev, /* reset */
64 (d_ttycv_t *) enodev, /* ttys */
65 (d_select_t *) pcaselect, /* select */
66 (d_mmap_t *) enodev, /* mmap */
67 (d_strategy_t *) enodev /* strategy */
68 };
69
70
Lines 71 - 131
-- This section is analogous to the config file declaration of your
device. The members of the isa_device structure are filled in by what
is known about your device, I/O port, shared memory segment, etc. We
will probably never have a need for two pcaudio devices in the kernel,
but this example shows how multiple devices can be supported.
71 /*
72 * this lkm arbitrarily supports two
73 * instantiations of the pc-audio device.
74 *
75 * this is for illustration purposes
76 * only, it doesn't make much sense
77 * to have two of these beasts...
78 */
79
80
81 /*
82 * these have a direct correlation to the
83 * config file entries...
84 */
85 struct isa_device pcadev[NPCA] = {
86 {
87 11, /* device id */
88 &pcadriver, /* driver pointer */
89 IO_TIMER1, /* base io address */
90 -1, /* interrupt */
91 -1, /* dma channel */
92 (caddr_t)-1, /* physical io memory */
93 0, /* size of io memory */
94 pcaintr , /* interrupt interface */
95 0, /* unit number */
96 0, /* flags */
97 0, /* scsi id */
98 0, /* is alive */
99 0, /* flags for register_intr */
100 0, /* hot eject device support */
101 1 /* is device enabled */
102 },
103 #if NPCA >1
104 {
105
106 /*
107 * these are all zeros, because it doesn't make
108 * much sense to be here
109 * but it may make sense for your device
110 */
111
112 0, /* device id */
113 &pcadriver, /* driver pointer */
114 0, /* base io address */
115 -1, /* interrupt */
116 -1, /* dma channel */
117 -1, /* physical io memory */
118 0, /* size of io memory */
119 NULL, /* interrupt interface */
120 1, /* unit number */
121 0, /* flags */
122 0, /* scsi id */
123 0, /* is alive */
124 0, /* flags for register_intr */
125 0, /* hot eject device support */
126 1 /* is device enabled */
127 },
128 #endif
129
130 };
131
Lines 132 - 139
-- This calls the C-preprocessor macro MOD_DEV, which sets up an LKM device
driver, as opposed to an LKM filesystem, or an LKM system call.
132 /*
133 * this macro maps to a function which
134 * sets the LKM up for a driver
135 * as opposed to a filesystem, system call, or misc
136 * LKM.
137 */
138 MOD_DEV("pcaudio_mod", LM_DT_CHAR, 24, &pcacdevsw);
139
Lines 140 - 168
-- This is the function which will be called when the driver is
loaded. This function tries to work like sys/i386/isa/isa.c
which does the probe/attach calls for a driver at boot time. The
biggest trick here is that it maps the physical address of the shared
memory segment, which is specified in the isa_device structure to a
kernel virtual address. Normally the physical address is put in the
config file which builds the isa_device structures in
/usr/src/sys/compile/KERNEL/ioconf.c. The probe/attach sequence of
/usr/src/sys/isa/isa.c translates the physical address to a virtual
one so that in your probe/attach routines you can do things like
140 /*
141 * this function is called when the module is
142 * loaded; it tries to mimic the behavior
143 * of the standard probe/attach stuff from
144 * isa.c
145 */
146 int
147 pcaload(){
148 int i;
149 uprintf("PC Audio Driver Loaded\n");
150 for (i=0; i
169 /*
170 * this function is called
171 * when the module is unloaded
172 */
173
174 int
175 pcaunload(){
176 uprintf("PC Audio Driver Unloaded\n");
177 return 0;
178 }
179
Lines 180 - 190
-- This is the entry point which is specified on the command line of the
modload. By convention it is named <dev>_mod. This is how it is
defined in bsd.lkm.mk, the makefile which builds the LKM. If you name your
module following this convention, you can do ``make load'' and ``make
unload'' from /usr/src/lkm/pcaudio.
180 /*
181 * this is the entry point specified
182 * on the modload command line
183 */
184
185 int
186 pcaudio_mod(struct lkm_table *lkmtp, int cmd, int ver)
187 {
188 DISPATCH(lkmtp, cmd, ver, pcaload, pcaunload, nosys);
189 }
190
191 #endif /* NICP > 0 */
struct devconf {
char dc_name[MAXDEVNAME]; /* name */
char dc_descr[MAXDEVDESCR]; /* description */
int dc_unit; /* unit number */
int dc_number; /* unique id */
char dc_pname[MAXDEVNAME]; /* name of the parent device */
int dc_punit; /* unit number of the parent */
int dc_pnumber; /* unique id of the parent */
struct machdep_devconf dc_md; /* machine-dependent stuff */
enum dc_state dc_state; /* state of the device (see above) */
enum dc_class dc_class; /* type of device (see above) */
size_t dc_datalen; /* length of data */
char dc_data[1]; /* variable-length data */
};
/*
* Description of a process.
*
* This structure contains the information needed to manage a thread of
* control, known in UN*X as a process; it has references to substructures
* containing descriptions of things that the process uses, but may share
* with related processes. The process structure and the substructures
* are always addressable except for those marked "(PROC ONLY)" below,
* which might be addressable only on a processor on which the process
* is running.
*/
struct proc {
struct proc *p_forw; /* Doubly-linked run/sleep queue. */
struct proc *p_back;
struct proc *p_next; /* Linked list of active procs */
struct proc **p_prev; /* and zombies. */
/* substructures: */
struct pcred *p_cred; /* Process owner's identity. */
struct filedesc *p_fd; /* Ptr to open files structure. */
struct pstats *p_stats; /* Accounting/statistics (PROC ONLY). */ struct plimit *p_limit; /* Process limits. */
struct vmspace *p_vmspace; /* Address space. */
struct sigacts *p_sigacts; /* Signal actions, state (PROC ONLY). */
#define p_ucred p_cred->pc_ucred
#define p_rlimit p_limit->pl_rlimit
int p_flag; /* P_* flags. */
char p_stat; /* S* process status. */
char p_pad1[3];
pid_t p_pid; /* Process identifier. */
struct proc *p_hash; /* Hashed based on p_pid for kill+exit+... */
struct proc *p_pgrpnxt; /* Pointer to next process in process group. */
struct proc *p_pptr; /* Pointer to process structure of parent. */
struct proc *p_osptr; /* Pointer to older sibling processes. */
/* The following fields are all zeroed upon creation in fork. */
#define p_startzero p_ysptr
struct proc *p_ysptr; /* Pointer to younger siblings. */
struct proc *p_cptr; /* Pointer to youngest living child. */
pid_t p_oppid; /* Save parent pid during ptrace. XXX */
int p_dupfd; /* Sideways return value from fdopen. XXX */
/* scheduling */
u_int p_estcpu; /* Time averaged value of p_cpticks. */
int p_cpticks; /* Ticks of cpu time. */
fixpt_t p_pctcpu; /* %cpu for this process during p_swtime */
void *p_wchan; /* Sleep address. */
char *p_wmesg; /* Reason for sleep. */
u_int p_swtime; /* Time swapped in or out. */
u_int p_slptime; /* Time since last blocked. */
struct itimerval p_realtimer; /* Alarm timer. */
struct timeval p_rtime; /* Real time. */
u_quad_t p_uticks; /* Statclock hits in user mode. */
u_quad_t p_sticks; /* Statclock hits in system mode. */
u_quad_t p_iticks; /* Statclock hits processing intr. */
int p_traceflag; /* Kernel trace points. */
struct vnode *p_tracep; /* Trace to vnode. */
int p_siglist; /* Signals arrived but not delivered. */
struct vnode *p_textvp; /* Vnode of executable. */
char p_lock; /* Process lock (prevent swap) count. */
char p_pad2[3]; /* alignment */
/* End area that is zeroed on creation. */
#define p_endzero p_startcopy
/* The following fields are all copied upon creation in fork. */
#define p_startcopy p_sigmask
sigset_t p_sigmask; /* Current signal mask. */
sigset_t p_sigignore; /* Signals being ignored. */
sigset_t p_sigcatch; /* Signals being caught by user. */
u_char p_priority; /* Process priority. */
u_char p_usrpri; /* User-priority based on p_cpu and p_nice. */
char p_nice; /* Process "nice" value. */
char p_comm[MAXCOMLEN+1];
struct pgrp *p_pgrp; /* Pointer to process group. */
struct sysentvec *p_sysent; /* System call dispatch information. */
struct rtprio p_rtprio; /* Realtime priority. */
/* End area that is copied on creation. */
#define p_endcopy p_addr
struct user *p_addr; /* Kernel virtual addr of u-area (PROC ONLY). */
struct mdproc p_md; /* Any machine-dependent fields. */
u_short p_xstat; /* Exit status for wait; also stop signal. */
u_short p_acflag; /* Accounting flags. */
struct rusage *p_ru; /* Exit information. XXX */
};
struct uio {
struct iovec *uio_iov;
int uio_iovcnt;
off_t uio_offset;
int uio_resid;
enum uio_seg uio_segflg;
enum uio_rw uio_rw;
struct proc *uio_procp;
};