diff options
author | Maxim Sobolev <sobomax@FreeBSD.org> | 2000-10-02 14:14:07 +0000 |
---|---|---|
committer | Maxim Sobolev <sobomax@FreeBSD.org> | 2000-10-02 14:14:07 +0000 |
commit | 7320fd3a3003e601d784b9b0c3780ae5875bc45a (patch) | |
tree | b5184959884769a8211d25c133ed53baa7de7b9c /share/examples/kld/cdev/module | |
parent | c7f95f537217b2412d9f2b196a9fa0612ffb34c2 (diff) |
Fix cdev kld example after it has been broken for year or so. Also extend list
of supported operations by example read() and write() operations.
Inspired by: http://www.daemonnews.org/200010/blueprints.html
PR: 16173
Submitted by: sobomax
Notes
Notes:
svn path=/head/; revision=66549
Diffstat (limited to 'share/examples/kld/cdev/module')
-rw-r--r-- | share/examples/kld/cdev/module/Makefile | 4 | ||||
-rw-r--r-- | share/examples/kld/cdev/module/cdev.c | 50 | ||||
-rw-r--r-- | share/examples/kld/cdev/module/cdev.h | 10 | ||||
-rw-r--r-- | share/examples/kld/cdev/module/cdevmod.c | 22 |
4 files changed, 73 insertions, 13 deletions
diff --git a/share/examples/kld/cdev/module/Makefile b/share/examples/kld/cdev/module/Makefile index 8fedcfc14264..50047cc13f04 100644 --- a/share/examples/kld/cdev/module/Makefile +++ b/share/examples/kld/cdev/module/Makefile @@ -78,8 +78,8 @@ KLDUNLOAD = /sbin/kldunload CLEANFILES+= ${KMOD} -load: /dev/cdev - ${KLDLOAD} -v ./${KMOD} +load: /dev/cdev ${KMOD}.ko + ${KLDLOAD} -v ./${KMOD}.ko unload: rm -f /dev/cdev diff --git a/share/examples/kld/cdev/module/cdev.c b/share/examples/kld/cdev/module/cdev.c index 4cbbc0769b4a..ba6fa544fd5a 100644 --- a/share/examples/kld/cdev/module/cdev.c +++ b/share/examples/kld/cdev/module/cdev.c @@ -64,7 +64,11 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * + * + * $FreeBSD$ */ +#include <sys/types.h> +#include <sys/uio.h> #include <sys/param.h> #include <sys/systm.h> #include <sys/ioccom.h> @@ -90,11 +94,17 @@ #define CDEV_IOCTL1 _IOR('C', 1, u_int) +/* Stores string recv'd by _write() */ +static char buf[512+1]; +static int len; + int mydev_open(dev_t dev, int flag, int otyp, struct proc *procp) { printf("mydev_open: dev_t=%d, flag=%x, otyp=%x, procp=%p\n", dev2udev(dev), flag, otyp, procp); + memset(&buf, '\0', 513); + len = 0; return (0); } @@ -125,3 +135,43 @@ mydev_ioctl(dev_t dev, u_long cmd, caddr_t arg, int mode, struct proc *procp) } return error; } + +/* + * mydev_write takes in a character string and saves it + * to buf for later accessing. + */ +int +mydev_write(dev_t dev, struct uio *uio, int ioflag) +{ + int err = 0; + + printf("mydev_write: dev_t=%d, uio=%p, ioflag=%d\n", + dev2udev(dev), uio, ioflag); + + err = copyinstr(uio->uio_iov->iov_base, &buf, 512, &len); + if (err != 0) { + printf("Write to \"cdev\" failed.\n"); + } + return(err); +} + +/* + * The mydev_read function just takes the buf that was saved + * via mydev_write() and returns it to userland for + * accessing. + */ +int +mydev_read(dev_t dev, struct uio *uio, int ioflag) +{ + int err = 0; + + printf("mydev_read: dev_t=%d, uio=%p, ioflag=%d\n", + dev2udev(dev), uio, ioflag); + + if (len <= 0) { + err = -1; + } else { /* copy buf to userland */ + copystr(&buf, uio->uio_iov->iov_base, 513, &len); + } + return(err); +} diff --git a/share/examples/kld/cdev/module/cdev.h b/share/examples/kld/cdev/module/cdev.h index 88b3cb908896..b09e53ae3dba 100644 --- a/share/examples/kld/cdev/module/cdev.h +++ b/share/examples/kld/cdev/module/cdev.h @@ -65,13 +65,17 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * + * + * $FreeBSD$ */ #ifndef __CDEV_H_ #define __CDEV_H_ -d_open_t mydev_open; -d_close_t mydev_close; -d_ioctl_t mydev_ioctl; +d_open_t mydev_open; +d_close_t mydev_close; +d_ioctl_t mydev_ioctl; +d_read_t mydev_read; +d_write_t mydev_write; #endif diff --git a/share/examples/kld/cdev/module/cdevmod.c b/share/examples/kld/cdev/module/cdevmod.c index 5c9ef47e47d7..cb80a72277c0 100644 --- a/share/examples/kld/cdev/module/cdevmod.c +++ b/share/examples/kld/cdev/module/cdevmod.c @@ -64,6 +64,8 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * + * + * $FreeBSD$ */ #include <sys/param.h> #include <sys/systm.h> @@ -78,25 +80,27 @@ static struct cdevsw my_devsw = { /* open */ mydev_open, /* close */ mydev_close, - /* read */ noread, - /* write */ nowrite, + /* read */ mydev_read, + /* write */ mydev_write, /* ioctl */ mydev_ioctl, - /* stop */ nostop, - /* reset */ noreset, - /* devtotty */ nodevtotty, /* poll */ nopoll, /* mmap */ nommap, /* strategy */ nostrategy, /* name */ "cdev", - /* parms */ noparms, /* maj */ CDEV_MAJOR, /* dump */ nodump, /* psize */ nopsize, /* flags */ D_TTY, - /* maxio */ 0, /* bmaj */ -1 }; +/* + * Used as the variable that is the reference to our device + * in devfs... we must keep this variable sane until we + * call kldunload. + */ +static dev_t sdev; + /* * This function is called each time the module is loaded or unloaded. * Since we are a miscellaneous module, we have to provide whatever @@ -123,10 +127,12 @@ cdev_load(module_t mod, int cmd, void *arg) printf("Copyright (c) 1998\n"); printf("Rajesh Vaidheeswarran\n"); printf("All rights reserved\n"); + sdev = make_dev(&my_devsw, 0, UID_ROOT, GID_WHEEL, 0600, "cdev"); break; /* Success*/ case MOD_UNLOAD: printf("Unloaded kld character device driver\n"); + destroy_dev(sdev); break; /* Success*/ default: /* we only understand load/unload*/ @@ -139,4 +145,4 @@ cdev_load(module_t mod, int cmd, void *arg) /* Now declare the module to the system */ -DEV_MODULE(cdev, CDEV_MAJOR, -1, my_devsw, cdev_load, 0); +DEV_MODULE(cdev, cdev_load, NULL); |