From 45c154579aa8762a6e2ef19bae8b1ea8a9e8d01d Mon Sep 17 00:00:00 2001 From: markm Date: Fri, 2 Aug 2002 10:37:22 +0000 Subject: Modernise and tidy up. --- share/examples/drivers/make_device_driver.sh | 146 +++++++++++--------------- share/examples/drivers/make_pseudo_driver.sh | 147 +++++++++++++-------------- 2 files changed, 131 insertions(+), 162 deletions(-) (limited to 'share') diff --git a/share/examples/drivers/make_device_driver.sh b/share/examples/drivers/make_device_driver.sh index 360679f..92c9e53 100755 --- a/share/examples/drivers/make_device_driver.sh +++ b/share/examples/drivers/make_device_driver.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/sh # This writes a skeleton driver and puts it into the kernel tree for you. # It also adds FOO and files.FOO configuration files so you can compile # a kernel with your FOO driver linked in. @@ -11,7 +11,7 @@ # To do so: # cd /sys/modules/foo; make depend; make; make install; kldload foo # -# arg1 to this script is expected to be lowercase "foo" +# arg1 to this script is expected to be lowercase "foo" # # Trust me, RUN THIS SCRIPT :) # @@ -22,12 +22,12 @@ # $FreeBSD$" # # -if [ "X${1}" = "X" ] +if [ "X${1}" = "X" ] then echo "Hey , how about some help here.. give me a device name!" exit 1 fi -UPPER=`echo ${1} |tr "[:lower:]" "[:upper:]"` +UPPER=`echo ${1} |tr "[:lower:]" "[:upper:]"` HERE=`pwd` cd /sys @@ -44,7 +44,7 @@ then then VAL=YES fi - case ${VAL} in + case ${VAL} in [yY]*) echo "Cleaning up from prior runs" rm -rf ${TOP}/dev/${1} @@ -69,7 +69,6 @@ echo ${TOP}/sys/${1}io.h echo ${TOP}/modules/${1} echo ${TOP}/modules/${1}/Makefile - mkdir ${TOP}/modules/${1} ####################################################################### @@ -93,7 +92,7 @@ DONE cat >${TOP}/i386/conf/${UPPER} <>${TOP}/i386/conf/${UPPER} @@ -108,9 +107,6 @@ then mkdir -p ${TOP}/dev/${1} fi - - - cat >${TOP}/dev/${1}/${1}.c <${TOP}/dev/${1}/${1}.c <${TOP}/dev/${1}/${1}.c < +__FBSDID("\$${RCS_KEYWORD}$"); + #include #include #include /* cdevsw stuff */ @@ -156,19 +151,21 @@ cat >${TOP}/dev/${1}/${1}.c < #include #include +#include +#include +#include /* ${1} IOCTL definitions */ + #include #include #include #include -#include -#include #include #include #include + #include "isa_if.h" -#include /* ${1} IOCTL definitions */ /* XXX These should be defined in terms of bus-space ops */ #define ${UPPER}_INB(port) inb(port_start) @@ -183,8 +180,8 @@ cat >${TOP}/dev/${1}/${1}.c <si_drv1) #define DEVICE2SOFTC(dev) ((struct ${1}_softc *) device_get_softc(dev)) -/* - * device specific Misc defines +/* + * device specific Misc defines */ #define BUFFERSIZE 1024 #define NUMPORTS 4 @@ -209,7 +206,7 @@ struct ${1}_softc { void *intr_cookie; void *vaddr; /* Virtual address of mem resource */ char buffer[BUFFERSIZE]; /* if we needed to buffer something */ -} ; +}; /* Function prototypes (these should all be static) */ static int ${1}_deallocate_resources(device_t device); @@ -225,7 +222,7 @@ static d_ioctl_t ${1}ioctl; static d_mmap_t ${1}mmap; static d_poll_t ${1}poll; static void ${1}intr(void *arg); - + #define CDEV_MAJOR 20 static struct cdevsw ${1}_cdevsw = { /* open */ ${1}open, @@ -245,7 +242,7 @@ static struct cdevsw ${1}_cdevsw = { }; static devclass_t ${1}_devclass; - + /* ***************************************** * ISA Attachment structures and functions @@ -276,7 +273,6 @@ static driver_t ${1}_isa_driver = { sizeof (struct ${1}_softc) }; - DRIVER_MODULE(${1}, isa, ${1}_isa_driver, ${1}_devclass, 0, 0); /* @@ -319,7 +315,7 @@ static struct localhints { * For NON-PNP "Smart" devices: * If the device has a NON-PNP way of being detected and setting/sensing * the card, then do that here and add a child for each set of - * hardware found. + * hardware found. * * For PNP devices: * If the device is always PNP capable then this function can be removed. @@ -340,7 +336,6 @@ ${1}_isa_identify (driver_t *driver, device_t parent) device_t child; int i; - /* * If we've already got ${UPPER} attached somehow, don't try again. * Maybe it was in the hints file. or it was loaded before. @@ -355,9 +350,8 @@ ${1}_isa_identify (driver_t *driver, device_t parent) ioport = res[i].ioport; irq = res[i].irq; - if ((ioport == 0) && (irq == 0)) { + if ((ioport == 0) && (irq == 0)) return; /* we've added all our local hints */ - } child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "${1}", -1); bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, NUMPORTS); @@ -365,7 +359,6 @@ ${1}_isa_identify (driver_t *driver, device_t parent) bus_set_resource(child, SYS_RES_DRQ, 0, res[i].drq, 1); bus_set_resource(child, SYS_RES_MEMORY, 0, res[i].mem, MEMSIZE); - #if 0 /* * If we wanted to pretend PNP found it @@ -390,7 +383,7 @@ ${1}_isa_identify (driver_t *driver, device_t parent) /* * The ISA code calls this for each device it knows about, * whether via the PNP code or via the hints etc. - * If the device nas no PNP capabilities, remove all the + * If the device nas no PNP capabilities, remove all the * PNP entries, but keep the call to ISA_PNP_PROBE() * As it will guard against accidentally recognising * foreign hardware. This is because we will be called to check against @@ -404,7 +397,6 @@ ${1}_isa_probe (device_t device) struct ${1}_softc *scp = DEVICE2SOFTC(device); u_long port_start, port_count; - bzero(scp, sizeof(*scp)); scp->device = device; @@ -437,7 +429,7 @@ ${1}_isa_probe (device_t device) * Well it didn't show up in the PNP tables * so look directly at known ports (if we have any) * in case we are looking for an old pre-PNP card. - * + * * Hopefully the 'identify' routine will have picked these * up for us first if they use some proprietary detection * method. @@ -453,15 +445,15 @@ ${1}_isa_probe (device_t device) */ /* * find out the values of any resources we - * need for our dumb probe. Also check we have enough ports - * in the request. (could be hints based). + * need for our dumb probe. Also check we have enough ports + * in the request. (could be hints based). * Should probably do the same for memory regions too. */ error = bus_get_resource(device, SYS_RES_IOPORT, 0, - &port_start, &port_count); + &port_start, &port_count); if (port_count != NUMPORTS) { bus_set_resource(device, SYS_RES_IOPORT, 0, - port_start, NUMPORTS); + port_start, NUMPORTS); } /* @@ -477,8 +469,8 @@ ${1}_isa_probe (device_t device) } /* dummy heuristic type probe */ - if ( inb(port_start) != EXPECTED_VALUE) { - /* + if (inb(port_start) != EXPECTED_VALUE) { + /* * It isn't what we hoped, so quit looking for it. */ error = ENXIO; @@ -539,14 +531,12 @@ ${1}_isa_attach (device_t device) struct ${1}_softc *scp = DEVICE2SOFTC(device); error = ${1}_attach(device, scp); - if (error) { + if (error) ${1}_isa_detach(device); - } return (error); - } -/* +/* * detach the driver (e.g. module unload) * call the bus independent version * and undo anything we did in the ISA attach routine. @@ -567,9 +557,9 @@ ${1}_isa_detach (device_t device) *************************************** */ -static int ${1}_pci_probe __P((device_t)); -static int ${1}_pci_attach __P((device_t)); -static int ${1}_pci_detach __P((device_t)); +static int ${1}_pci_probe(device_t); +static int ${1}_pci_attach(device_t); +static int ${1}_pci_detach(device_t); static device_method_t ${1}_pci_methods[] = { /* Device interface */ @@ -585,7 +575,6 @@ static driver_t ${1}_pci_driver = { sizeof(struct ${1}_softc), }; - DRIVER_MODULE(${1}, pci, ${1}_pci_driver, ${1}_devclass, 0, 0); /* * Cardbus is a pci bus plus extra, so use the pci driver unless special @@ -624,9 +613,8 @@ ${1}_pci_probe (device_t device) if (ep->desc) { device_set_desc(device, ep->desc); return 0; /* If there might be a better driver, return -2 */ - } else { + } else return ENXIO; - } } static int @@ -636,9 +624,8 @@ ${1}_pci_attach(device_t device) struct ${1}_softc *scp = DEVICE2SOFTC(device); error = ${1}_attach(device, scp); - if (error) { + if (error) ${1}_pci_detach(device); - } return (error); } @@ -652,7 +639,6 @@ ${1}_pci_detach (device_t device) return (error); } - /* **************************************** * Common Attachment sub-functions @@ -668,9 +654,8 @@ ${1}_attach(device_t device, struct ${1}_softc * scp) UID_ROOT, GID_OPERATOR, 0600, "${1}%d", unit); scp->dev->si_drv1 = scp; - if (${1}_allocate_resources(device)) { + if (${1}_allocate_resources(device)) goto errexit; - } scp->bt = rman_get_bustag(scp->res_ioport); scp->bh = rman_get_bushandle(scp->res_ioport); @@ -693,9 +678,8 @@ ${1}_attach(device_t device, struct ${1}_softc * scp) if (BUS_SETUP_INTR(parent, device, scp->res_irq, INTR_TYPE_TTY, ${1}intr, scp, &scp->intr_cookie) == 0) { /* do something if successful */ - } else { + } else goto errexit; - } } /* @@ -724,8 +708,8 @@ ${1}_detach(device_t device, struct ${1}_softc *scp) /* * At this point stick a strong piece of wood into the device - * to make sure it is stopped safely. The alternative is to - * simply REFUSE to detach if it's busy. What you do depends on + * to make sure it is stopped safely. The alternative is to + * simply REFUSE to detach if it's busy. What you do depends on * your specific situation. * * Sometimes the parent bus will detach you anyway, even if you @@ -741,9 +725,8 @@ ${1}_detach(device_t device, struct ${1}_softc *scp) */ if (scp->intr_cookie != NULL) { if (BUS_TEARDOWN_INTR(parent, device, - scp->res_irq, scp->intr_cookie) != 0) { + scp->res_irq, scp->intr_cookie) != 0) printf("intr teardown failed.. continuing\n"); - } scp->intr_cookie = NULL; } @@ -764,27 +747,23 @@ ${1}_allocate_resources(device_t device) scp->res_ioport = bus_alloc_resource(device, SYS_RES_IOPORT, &scp->rid_ioport, 0ul, ~0ul, size, RF_ACTIVE); - if (scp->res_ioport == NULL) { + if (scp->res_ioport == NULL) goto errexit; - } scp->res_irq = bus_alloc_resource(device, SYS_RES_IRQ, &scp->rid_irq, 0ul, ~0ul, 1, RF_SHAREABLE|RF_ACTIVE); - if (scp->res_irq == NULL) { + if (scp->res_irq == NULL) goto errexit; - } scp->res_drq = bus_alloc_resource(device, SYS_RES_DRQ, &scp->rid_drq, 0ul, ~0ul, 1, RF_ACTIVE); - if (scp->res_drq == NULL) { + if (scp->res_drq == NULL) goto errexit; - } scp->res_memory = bus_alloc_resource(device, SYS_RES_MEMORY, &scp->rid_memory, 0ul, ~0ul, MSIZE, RF_ACTIVE); - if (scp->res_memory == NULL) { + if (scp->res_memory == NULL) goto errexit; - } return (0); errexit: @@ -827,9 +806,8 @@ ${1}_deallocate_resources(device_t device) scp->rid_drq, scp->res_drq); scp->res_drq = 0; } - if (scp->dev) { + if (scp->dev) destroy_dev(scp->dev); - } return (0); } @@ -838,7 +816,7 @@ ${1}intr(void *arg) { struct ${1}_softc *scp = (struct ${1}_softc *) arg; - /* + /* * well we got an interrupt, now what? * * Make sure that the interrupt routine will always terminate, @@ -856,7 +834,7 @@ ${1}ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td) case DHIOCRESET: /* whatever resets it */ #if 0 - ${UPPER}_OUTB(SOME_PORT, 0xff) ; + ${UPPER}_OUTB(SOME_PORT, 0xff); #endif break; default: @@ -873,7 +851,7 @@ ${1}open(dev_t dev, int oflags, int devtype, struct thread *td) { struct ${1}_softc *scp = DEV2SOFTC(dev); - /* + /* * Do processing */ return (0); @@ -884,7 +862,7 @@ ${1}close(dev_t dev, int fflag, int devtype, struct thread *td) { struct ${1}_softc *scp = DEV2SOFTC(dev); - /* + /* * Do processing */ return (0); @@ -896,8 +874,7 @@ ${1}read(dev_t dev, struct uio *uio, int ioflag) struct ${1}_softc *scp = DEV2SOFTC(dev); int toread; - - /* + /* * Do processing * read from buffer */ @@ -911,7 +888,7 @@ ${1}write(dev_t dev, struct uio *uio, int ioflag) struct ${1}_softc *scp = DEV2SOFTC(dev); int towrite; - /* + /* * Do processing * write to buffer */ @@ -924,14 +901,13 @@ ${1}mmap(dev_t dev, vm_offset_t offset, int nprot) { struct ${1}_softc *scp = DEV2SOFTC(dev); - /* + /* * Given a byte offset into your device, return the PHYSICAL * page number that it would map to. */ #if 0 /* if we had a frame buffer or whatever.. do this */ - if (offset > FRAMEBUFFERSIZE - PAGE_SIZE) { + if (offset > FRAMEBUFFERSIZE - PAGE_SIZE) return (-1); - } return i386_btop((FRAMEBASE + offset)); #else return (-1); @@ -943,7 +919,7 @@ ${1}poll(dev_t dev, int which, struct thread *td) { struct ${1}_softc *scp = DEV2SOFTC(dev); - /* + /* * Do processing */ return (0); /* this is the wrong value I'm sure */ @@ -980,16 +956,16 @@ cat >${TOP}/modules/${1}/Makefile < opt_inet.h - + .include DONE @@ -1007,8 +983,4 @@ exit # #edit to your taste.. # -# - - - - +# diff --git a/share/examples/drivers/make_pseudo_driver.sh b/share/examples/drivers/make_pseudo_driver.sh index 00cd519..1a3641f 100644 --- a/share/examples/drivers/make_pseudo_driver.sh +++ b/share/examples/drivers/make_pseudo_driver.sh @@ -1,19 +1,21 @@ -#!/bin/sh +#!/bin/sh # This writes a skeleton driver and puts it into the kernel tree for you -#arg1 is lowercase "foo" +#arg1 is lowercase "foo" # # Trust me, RUN THIS SCRIPT :) # +# $FreeBSD$ +# #-------cut here------------------ cd /sys/i386/conf -if [ "${1}X" = "X" ] +if [ "${1}X" = "X" ] then echo "Hey , how about some help here.. give me a device name!" exit 1 fi -UPPER=`echo ${1} |tr "[:lower:]" "[:upper:]"` +UPPER=`echo ${1} |tr "[:lower:]" "[:upper:]"` cat >files.${UPPER} <${UPPER} <>${UPPER} cat >>${UPPER} <../../dev/${1}.c <../../dev/${1}.c < +__FBSDID("\$FreeBSD\$"); -#include "${1}.h" /* generated file.. defines N${UPPER} */ #include #include #include /* SYSINIT stuff */ #include /* cdevsw stuff */ +#include /* DEVFS definitions */ #include /* malloc region definitions */ #include -#include /* DELAY() */ #include /* ${1} IOCTL definitions */ -#ifdef DEVFS -#include /* DEVFS defintitions */ -#endif /* DEVFS */ +#include /* DELAY() */ +#include "${1}.h" /* generated file.. defines N${UPPER} */ /* Function prototypes (these should all be static) */ static d_open_t ${1}open; @@ -64,26 +65,27 @@ static d_write_t ${1}write; static d_ioctl_t ${1}ioctl; static d_mmap_t ${1}mmap; static d_poll_t ${1}poll; - + #define CDEV_MAJOR 20 static struct cdevsw ${1}_cdevsw = { ${1}open, ${1}close, ${1}read, - ${1}write, + ${1}write, ${1}ioctl, nullstop, nullreset, - nodevtotty, + nodevtotty, ${1}poll, ${1}mmap, NULL, "${1}", NULL, - -1 }; - -/* - * device specific Misc defines + -1 +}; + +/* + * device specific Misc defines */ #define BUFFERSIZE 1024 #define UNIT(dev) minor(dev) /* assume one minor number per unit */ @@ -94,46 +96,46 @@ static struct cdevsw ${1}_cdevsw = { struct ${1}_softc { struct isa_device *dev; char buffer[BUFFERSIZE]; -#ifdef DEVFS static void *devfs_token; -#endif -} ; +}; typedef struct ${1}_softc *sc_p; static sc_p sca[N${UPPER}]; -/* +/* * Macro to check that the unit number is valid * Often this isn't needed as once the open() is performed, * the unit number is pretty much safe.. The exception would be if we * implemented devices that could "go away". in which case all these routines * would be wise to check the number, DIAGNOSTIC or not. */ -#define CHECKUNIT(RETVAL) \ -do { /* the do-while is a safe way to do this grouping */ \ - if (unit > N${UPPER}) { \ - printf(__FUNCTION__ ":bad unit %d\n", unit); \ - return (RETVAL); \ - } \ - if (scp == NULL) { \ - printf( __FUNCTION__ ": unit %d not attached\n", unit);\ - return (RETVAL); \ - } \ -} while (0) +#define CHECKUNIT(RETVAL) \ +do { /* the do-while is a safe way to do this grouping */ \ + if (unit > N${UPPER}) { \ + printf(__FUNCTION__ ":bad unit %d\n", unit); \ + return (RETVAL); \ + } \ + if (scp == NULL) { \ + printf( __FUNCTION__ ": unit %d not attached\n", unit); \ + return (RETVAL); \ + } \ +} while (0) + #ifdef DIAGNOSTIC #define CHECKUNIT_DIAG(RETVAL) CHECKUNIT(RETVAL) #else /* DIAGNOSTIC */ #define CHECKUNIT_DIAG(RETVAL) #endif /* DIAGNOSTIC */ -int ${1}ioctl (dev_t dev, int cmd, caddr_t data, int flag, struct thread *td) +static int +${1}ioctl (dev_t dev, int cmd, caddr_t data, int flag, struct thread *td) { - int unit = UNIT (dev); + int unit = UNIT(dev); sc_p scp = sca[unit]; - + CHECKUNIT_DIAG(ENXIO); - + switch (cmd) { case DHIOCRESET: /* whatever resets it */ @@ -143,50 +145,51 @@ int ${1}ioctl (dev_t dev, int cmd, caddr_t data, int flag, struct thread *td) return ENXIO; } return (0); -} +} + /* * You also need read, write, open, close routines. * This should get you started */ -static int +static int ${1}open(dev_t dev, int oflags, int devtype, struct thread *td) { - int unit = UNIT (dev); + int unit = UNIT(dev); sc_p scp = sca[unit]; - + CHECKUNIT(ENXIO); - /* + /* * Do processing */ return (0); } -static int +static int ${1}close(dev_t dev, int fflag, int devtype, struct thread *td) { - int unit = UNIT (dev); + int unit = UNIT(dev); sc_p scp = sca[unit]; - + CHECKUNIT_DIAG(ENXIO); - /* + /* * Do processing */ return (0); } -static int +static int ${1}read(dev_t dev, struct uio *uio, int ioflag) { - int unit = UNIT (dev); + int unit = UNIT(dev); sc_p scp = sca[unit]; int toread; - - + + CHECKUNIT_DIAG(ENXIO); - /* + /* * Do processing * read from buffer */ @@ -194,16 +197,16 @@ ${1}read(dev_t dev, struct uio *uio, int ioflag) return(uiomove(scp->buffer, toread, uio)); } -static int +static int ${1}write(dev_t dev, struct uio *uio, int ioflag) { - int unit = UNIT (dev); + int unit = UNIT(dev); sc_p scp = sca[unit]; int towrite; - + CHECKUNIT_DIAG(ENXIO); - /* + /* * Do processing * write to buffer */ @@ -211,15 +214,15 @@ ${1}write(dev_t dev, struct uio *uio, int ioflag) return(uiomove(scp->buffer, towrite, uio)); } -static int +static int ${1}mmap(dev_t dev, int offset, int nprot) { - int unit = UNIT (dev); + int unit = UNIT(dev); sc_p scp = sca[unit]; - + CHECKUNIT_DIAG(-1); - /* + /* * Do processing */ #if 0 /* if we had a frame buffer or whatever.. do this */ @@ -232,15 +235,15 @@ ${1}mmap(dev_t dev, int offset, int nprot) #endif } -static int +static int ${1}poll(dev_t dev, int which, struct thread *td) { - int unit = UNIT (dev); + int unit = UNIT(dev); sc_p scp = sca[unit]; - + CHECKUNIT_DIAG(ENXIO); - /* + /* * Do processing */ return (0); /* this is the wrong value I'm sure */ @@ -250,7 +253,7 @@ ${1}poll(dev_t dev, int which, struct thread *td) * Now for some driver initialisation. * Occurs ONCE during boot (very early). */ -static void +static void ${1}_drvinit(void *unused) { dev_t dev; @@ -260,26 +263,22 @@ ${1}_drvinit(void *unused) dev = makedev(CDEV_MAJOR, 0); cdevsw_add(&dev, &${1}_cdevsw, NULL); for (unit = 0; unit < N${UPPER}; unit++) { - /* + /* * Allocate storage for this instance . */ scp = malloc(sizeof(*scp), M_DEVBUF, M_NOWAIT | M_ZERO); if( scp == NULL) { printf("${1}%d failed to allocate strorage\n", unit); - return ; + return; } sca[unit] = scp; -#if DEVFS scp->devfs_token = devfs_add_devswf(&${1}_cdevsw, unit, DV_CHR, UID_ROOT, GID_KMEM, 0640, "${1}%d", unit); -#endif } } SYSINIT(${1}dev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR, ${1}_drvinit, NULL) - - DONE cat >../../sys/${1}io.h <