2 # This writes a skeleton driver and puts it into the kernel tree for you
4 # arg1 is lowercase "foo"
5 # arg2 path to the kernel sources, "/sys" if omitted
7 # Trust me, RUN THIS SCRIPT :)
11 #-------cut here------------------
15 echo "Hey , how about some help here.. give me a device name!"
18 if [ "X${2}" = "X" ]; then
20 echo "Using ${TOP} as the path to the kernel sources!"
25 for i in "" "conf" "i386" "i386/conf" "dev" "sys" "modules"
31 echo "${TOP}/${i}: no such directory."
32 echo "Please, correct the error and try again."
36 UPPER=`echo ${1} |tr "[:lower:]" "[:upper:]"`
38 if [ -d ${TOP}/modules/${1} ]; then
39 echo "There appears to already be a module called ${1}"
40 echo -n "Should it be overwritten? [Y]"
42 if [ "-z" "$VAL" ]; then
47 echo "Cleaning up from prior runs"
48 rm -rf ${TOP}/dev/${1}
49 rm -rf ${TOP}/modules/${1}
50 rm ${TOP}/conf/files.${UPPER}
51 rm ${TOP}/i386/conf/${UPPER}
52 rm ${TOP}/sys/${1}io.h
60 echo "The following files will be created:"
61 echo ${TOP}/modules/${1}
62 echo ${TOP}/conf/files.${UPPER}
63 echo ${TOP}/i386/conf/${UPPER}
65 echo ${TOP}/dev/${1}/${1}.c
66 echo ${TOP}/sys/${1}io.h
67 echo ${TOP}/modules/${1}
68 echo ${TOP}/modules/${1}/Makefile
70 mkdir ${TOP}/modules/${1}
72 cat >${TOP}/conf/files.${UPPER} <<DONE
73 dev/${1}/${1}.c optional ${1}
76 cat >${TOP}/i386/conf/${UPPER} <<DONE
77 # Configuration file for kernel type: ${UPPER}
80 files "${TOP}/conf/files.${UPPER}"
86 # trust me, you'll need this
92 if [ ! -d ${TOP}/dev/${1} ]; then
93 mkdir -p ${TOP}/dev/${1}
96 cat >${TOP}/dev/${1}/${1}.c <<DONE
98 * Copyright (c) [year] [your name]
100 * Redistribution and use in source and binary forms, with or without
101 * modification, are permitted provided that the following conditions
103 * 1. Redistributions of source code must retain the above copyright
104 * notice, this list of conditions and the following disclaimer.
105 * 2. Redistributions in binary form must reproduce the above copyright
106 * notice, this list of conditions and the following disclaimer in the
107 * documentation and/or other materials provided with the distribution.
109 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
110 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
111 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
112 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
113 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
114 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
115 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
116 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
117 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
118 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
124 #include <sys/cdefs.h>
125 __FBSDID("\$FreeBSD\$");
127 #include <sys/param.h>
128 #include <sys/systm.h>
129 #include <sys/kernel.h> /* SYSINIT stuff */
130 #include <sys/uio.h> /* SYSINIT stuff */
131 #include <sys/conf.h> /* cdevsw stuff */
132 #include <sys/malloc.h> /* malloc region definitions */
133 #include <sys/proc.h>
134 #include <sys/${1}io.h> /* ${1} IOCTL definitions */
136 #include <machine/clock.h> /* DELAY() */
138 #define N${UPPER} 3 /* defines number of instances */
140 /* XXX These should be defined in terms of bus-space ops. */
141 #define ${UPPER}_INB(port) inb(port)
142 #define ${UPPER}_OUTB(port, val) (port, (val))
144 /* Function prototypes (these should all be static) */
145 static d_open_t ${1}open;
146 static d_close_t ${1}close;
147 static d_read_t ${1}read;
148 static d_write_t ${1}write;
149 static d_ioctl_t ${1}ioctl;
150 static d_mmap_t ${1}mmap;
151 static d_poll_t ${1}poll;
153 #define CDEV_MAJOR 20
154 static struct cdevsw ${1}_cdevsw = {
155 .d_version = D_VERSION,
157 .d_close = ${1}close,
159 .d_write = ${1}write,
160 .d_ioctl = ${1}ioctl,
167 * device specific Misc defines
169 #define BUFFERSIZE 1024
170 #define UNIT(dev) dev2unit(dev) /* assume one minor number per unit */
173 * One of these per allocated device
177 char buffer[BUFFERSIZE];
181 typedef struct ${1}_softc *sc_p;
183 static sc_p sca[N${UPPER}];
186 * Macro to check that the unit number is valid
187 * Often this isn't needed as once the open() is performed,
188 * the unit number is pretty much safe.. The exception would be if we
189 * implemented devices that could "go away". in which case all these routines
190 * would be wise to check the number, DIAGNOSTIC or not.
192 #define CHECKUNIT(RETVAL) \
193 do { /* the do-while is a safe way to do this grouping */ \
194 if (unit > N${UPPER}) { \
195 printf("%s: bad unit %d\n", __func__, unit); \
199 printf("%s: unit %d not attached\n", __func__, unit); \
205 #define CHECKUNIT_DIAG(RETVAL) CHECKUNIT(RETVAL)
206 #else /* DIAGNOSTIC */
207 #define CHECKUNIT_DIAG(RETVAL)
208 #endif /* DIAGNOSTIC */
211 ${1}ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
213 int unit = UNIT(dev);
214 sc_p scp = sca[unit];
216 CHECKUNIT_DIAG(ENXIO);
220 /* whatever resets it */
221 (void)scp; /* Delete this line after using scp. */
223 ${UPPER}_OUTB(scp->iobase, 0xff);
233 * You also need read, write, open, close routines.
234 * This should get you started
237 ${1}open(struct cdev *dev, int oflags, int devtype, struct thread *td)
239 int unit = UNIT(dev);
240 sc_p scp = sca[unit];
244 (void)scp; /* Delete this line after using scp. */
252 ${1}close(struct cdev *dev, int fflag, int devtype, struct thread *td)
254 int unit = UNIT(dev);
255 sc_p scp = sca[unit];
257 CHECKUNIT_DIAG(ENXIO);
259 (void)scp; /* Delete this line after using scp. */
267 ${1}read(struct cdev *dev, struct uio *uio, int ioflag)
269 int unit = UNIT(dev);
270 sc_p scp = sca[unit];
274 CHECKUNIT_DIAG(ENXIO);
280 toread = (min(uio->uio_resid, sizeof(scp->buffer)));
281 return(uiomove(scp->buffer, toread, uio));
285 ${1}write(struct cdev *dev, struct uio *uio, int ioflag)
287 int unit = UNIT(dev);
288 sc_p scp = sca[unit];
291 CHECKUNIT_DIAG(ENXIO);
297 towrite = (min(uio->uio_resid, sizeof(scp->buffer)));
298 return(uiomove(scp->buffer, towrite, uio));
302 ${1}mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
304 int unit = UNIT(dev);
305 sc_p scp = sca[unit];
309 (void)scp; /* Delete this line after using scp. */
313 #if 0 /* if we had a frame buffer or whatever.. do this */
314 if (offset > FRAMEBUFFERSIZE - PAGE_SIZE) {
317 return i386_btop((FRAMEBASE + offset));
324 ${1}poll(struct cdev *dev, int which, struct thread *td)
326 int unit = UNIT(dev);
327 sc_p scp = sca[unit];
329 CHECKUNIT_DIAG(ENXIO);
331 (void)scp; /* Delete this line after using scp. */
335 return (0); /* this is the wrong value I'm sure */
339 * Now for some driver initialisation.
340 * Occurs ONCE during boot (very early).
343 ${1}_drvinit(void *unused)
348 for (unit = 0; unit < N${UPPER}; unit++) {
350 * Allocate storage for this instance .
352 scp = malloc(sizeof(*scp), M_DEVBUF, M_NOWAIT | M_ZERO);
354 printf("${1}%d failed to allocate strorage\n", unit);
358 scp->dev = make_dev(&${1}_cdevsw, unit,
359 UID_ROOT, GID_KMEM, 0640, "${1}%d", unit);
363 SYSINIT(${1}dev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR,
367 cat >${TOP}/sys/${1}io.h <<DONE
369 * Definitions needed to access the ${1} device (ioctls etc)
370 * see mtio.h , ioctl.h as examples
376 #include <sys/types.h>
378 #include <sys/ioccom.h>
381 * define an ioctl here
383 #define DHIOCRESET _IO('D', 0) /* reset the ${1} device */
387 if [ ! -d ${TOP}/modules/${1} ]; then
388 mkdir -p ${TOP}/modules/${1}
391 cat >${TOP}/modules/${1}/Makefile <<DONE
392 # ${UPPER} Loadable Kernel Module
396 .PATH: \${.CURDIR}/../../dev/${1}
400 .include <bsd.kmod.mk>
403 echo -n "Do you want to build the '${1}' module? [Y]"
405 if [ "-z" "$VAL" ]; then
410 (cd ${TOP}/modules/${1}; make depend; make )
418 echo -n "Do you want to build the '${UPPER}' kernel? [Y]"
420 if [ "-z" "$VAL" ]; then
426 cd ${TOP}/i386/conf; \
428 cd ${TOP}/i386/compile/${UPPER}; \
438 #--------------end of script---------------
440 #edit to your taste..