2 # This writes a skeleton driver and puts it into the kernel tree for you
3 #arg1 is lowercase "foo"
5 # Trust me, RUN THIS SCRIPT :)
7 #-------cut here------------------
12 echo "Hey , how about some help here.. give me a device name!"
16 UPPER=`echo ${1} |tr "[:lower:]" "[:upper:]"`
17 cat >files.${UPPER} <<DONE
18 dev/${1}.c optional ${1} device-driver
22 # Configuration file for kernel type: ${UPPER}
26 grep -v X86_64_GENERIC < X86_64_GENERIC >>${UPPER}
29 # trust me, you'll need this
31 pseudo-device ${1} 4 # might as well allow 4 of them
34 cat >../../dev/${1}.c <<DONE
42 #include "${1}.h" /* generated file.. defines N${UPPER} */
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h> /* SYSINIT stuff */
46 #include <sys/conf.h> /* cdevsw stuff */
47 #include <sys/malloc.h> /* malloc region definitions */
48 #include <machine/clock.h> /* DELAY() */
49 #include <sys/${1}io.h> /* ${1} IOCTL definitions */
53 /* Function prototypes (these should all be static) */
54 static d_open_t ${1}open;
55 static d_close_t ${1}close;
56 static d_read_t ${1}read;
57 static d_write_t ${1}write;
58 static d_ioctl_t ${1}ioctl;
59 static d_mmap_t ${1}mmap;
60 static d_poll_t ${1}poll;
63 static struct cdevsw ${1}_cdevsw = {
80 * device specific Misc defines
82 #define BUFFERSIZE 1024
83 #define UNIT(dev) minor(dev) /* assume one minor number per unit */
86 * One of these per allocated device
89 struct isa_device *dev;
90 char buffer[BUFFERSIZE];
93 typedef struct ${1}_softc *sc_p;
95 static sc_p sca[N${UPPER}];
98 * Macro to check that the unit number is valid
99 * Often this isn't needed as once the open() is performed,
100 * the unit number is pretty much safe.. The exception would be if we
101 * implemented devices that could "go away". in which case all these routines
102 * would be wise to check the number, DIAGNOSTIC or not.
104 #define CHECKUNIT(RETVAL) \
105 do { /* the do-while is a safe way to do this grouping */ \
106 if (unit > N${UPPER}) { \
107 printf("%s: bad unit %d\n", __func__, unit); \
111 printf("%s: unit %d not attached\n", __func__, unit);\
116 #define CHECKUNIT_DIAG(RETVAL) CHECKUNIT(RETVAL)
117 #else /* DIAGNOSTIC */
118 #define CHECKUNIT_DIAG(RETVAL)
119 #endif /* DIAGNOSTIC */
121 int ${1}ioctl (dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
123 int unit = UNIT (dev);
124 sc_p scp = sca[unit];
126 CHECKUNIT_DIAG(ENXIO);
130 /* whatever resets it */
131 outb(scp->dev->id_iobase, 0xff);
139 * You also need read, write, open, close routines.
140 * This should get you started
143 ${1}open(dev_t dev, int oflags, int devtype, struct proc *p)
145 int unit = UNIT (dev);
146 sc_p scp = sca[unit];
157 ${1}close(dev_t dev, int fflag, int devtype, struct proc *p)
159 int unit = UNIT (dev);
160 sc_p scp = sca[unit];
162 CHECKUNIT_DIAG(ENXIO);
171 ${1}read(dev_t dev, struct uio *uio, int ioflag)
173 int unit = UNIT (dev);
174 sc_p scp = sca[unit];
178 CHECKUNIT_DIAG(ENXIO);
184 toread = (min(uio->uio_resid, sizeof(scp->buffer)));
185 return(uiomove(scp->buffer, toread, uio));
189 ${1}write(dev_t dev, struct uio *uio, int ioflag)
191 int unit = UNIT (dev);
192 sc_p scp = sca[unit];
195 CHECKUNIT_DIAG(ENXIO);
201 towrite = (min(uio->uio_resid, sizeof(scp->buffer)));
202 return(uiomove(scp->buffer, towrite, uio));
206 ${1}mmap(dev_t dev, int offset, int nprot)
208 int unit = UNIT (dev);
209 sc_p scp = sca[unit];
216 #if 0 /* if we had a frame buffer or whatever.. do this */
217 if (offset > FRAMEBUFFERSIZE - PAGE_SIZE) {
220 return i386_btop((FRAMEBASE + offset));
227 ${1}poll(dev_t dev, int which, struct proc *p)
229 int unit = UNIT (dev);
230 sc_p scp = sca[unit];
232 CHECKUNIT_DIAG(ENXIO);
237 return (0); /* this is the wrong value I'm sure */
241 * Now for some driver initialisation.
242 * Occurs ONCE during boot (very early).
245 ${1}_drvinit(void *unused)
249 sc_p scp = sca[unit];
251 dev = makedev(CDEV_MAJOR, 0);
252 cdevsw_add(&dev, &${1}_cdevsw, NULL);
253 for (unit = 0; unit < N${UPPER}; unit++) {
255 * Allocate storage for this instance .
257 scp = kmalloc(sizeof(*scp), M_DEVBUF, M_NOWAIT);
259 printf("${1}%d failed to allocate strorage\n", unit);
262 bzero(scp, sizeof(*scp));
267 SYSINIT(${1}dev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR,
273 cat >../../sys/${1}io.h <<DONE
275 * Definitions needed to access the ${1} device (ioctls etc)
276 * see mtio.h , ioctl.h as examples
282 #include <sys/types.h>
284 #include <sys/ioccom.h>
287 * define an ioctl here
289 #define DHIOCRESET _IO('D', 0) /* reset the ${1} device */
294 cd ../../compile/${UPPER}
300 #--------------end of script---------------
302 #you also need to add an entry into the cdevsw[]
303 #array in conf.c, but it's too hard to do in a script..
305 #edit to your taste..