diff options
author | sobomax <sobomax@FreeBSD.org> | 2000-10-02 14:14:07 +0000 |
---|---|---|
committer | sobomax <sobomax@FreeBSD.org> | 2000-10-02 14:14:07 +0000 |
commit | ca9093b327311b17a6c77820abb0c66abf6a94f4 (patch) | |
tree | b7b5dd86df52d6d28fcd6420e78651ff948cf748 /share/examples/kld/cdev/module/cdev.c | |
parent | f35f4e93b21d23ac9951fb2a706ba5bb5c2bd00a (diff) | |
download | FreeBSD-src-ca9093b327311b17a6c77820abb0c66abf6a94f4.zip FreeBSD-src-ca9093b327311b17a6c77820abb0c66abf6a94f4.tar.gz |
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
Diffstat (limited to 'share/examples/kld/cdev/module/cdev.c')
-rw-r--r-- | share/examples/kld/cdev/module/cdev.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/share/examples/kld/cdev/module/cdev.c b/share/examples/kld/cdev/module/cdev.c index 4cbbc07..ba6fa54 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); +} |