diff options
Diffstat (limited to 'share/examples/drivers/make_pseudo_driver.sh')
-rw-r--r-- | share/examples/drivers/make_pseudo_driver.sh | 147 |
1 files changed, 72 insertions, 75 deletions
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} <<DONE dev/${1}.c optional ${1} device-driver DONE @@ -21,15 +23,15 @@ DONE cat >${UPPER} <<DONE # Configuration file for kernel type: ${UPPER} ident ${UPPER} -# \$FreeBSD$" +# \$FreeBSD\$ DONE grep -v GENERIC < GENERIC >>${UPPER} cat >>${UPPER} <<DONE # trust me, you'll need this -options DDB -device ${1} 4 # might as well allow 4 of them +options DDB +device ${1} 4 # might as well allow 4 of them DONE cat >../../dev/${1}.c <<DONE @@ -37,24 +39,23 @@ cat >../../dev/${1}.c <<DONE * Copyright ME * * ${1} driver - * \$FreeBSD$ */ +#include <sys/cdefs.h> +__FBSDID("\$FreeBSD\$"); -#include "${1}.h" /* generated file.. defines N${UPPER} */ #include <sys/param.h> #include <sys/systm.h> #include <sys/kernel.h> /* SYSINIT stuff */ #include <sys/conf.h> /* cdevsw stuff */ +#include <sys/devfsext.h> /* DEVFS definitions */ #include <sys/malloc.h> /* malloc region definitions */ #include <sys/proc.h> -#include <machine/clock.h> /* DELAY() */ #include <sys/${1}io.h> /* ${1} IOCTL definitions */ -#ifdef DEVFS -#include <sys/devfsext.h> /* DEVFS defintitions */ -#endif /* DEVFS */ +#include <machine/clock.h> /* 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 <<DONE @@ -316,6 +315,4 @@ exit # #edit to your taste.. # -# - - +# |