Path: blob/main/share/examples/kld/cdev/module/cdev.c
39553 views
/* 08 Nov 1998*/1/*-2* cdev.c3*4* 08 Nov 1998 Rajesh Vaidheeswarran5*6* SPDX-License-Identifier: BSD-4-Clause7*8* Copyright (c) 1998 Rajesh Vaidheeswarran9* All rights reserved.10*11* Redistribution and use in source and binary forms, with or without12* modification, are permitted provided that the following conditions13* are met:14* 1. Redistributions of source code must retain the above copyright15* notice, this list of conditions and the following disclaimer.16* 2. Redistributions in binary form must reproduce the above copyright17* notice, this list of conditions and the following disclaimer in the18* documentation and/or other materials provided with the distribution.19* 3. All advertising materials mentioning features or use of this software20* must display the following acknowledgement:21* This product includes software developed by Rajesh Vaidheeswarran.22* 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote23* products derived from this software without specific prior written24* permission.25*26* THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``AS IS'' AND ANY27* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE28* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE29* ARE DISCLAIMED. IN NO EVENT SHALL THE RAJESH VAIDHEESWARRAN BE LIABLE30* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL31* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS32* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)33* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT34* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY35* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF36* SUCH DAMAGE.37*38* Copyright (c) 1993 Terrence R. Lambert.39* All rights reserved.40*41* Redistribution and use in source and binary forms, with or without42* modification, are permitted provided that the following conditions43* are met:44* 1. Redistributions of source code must retain the above copyright45* notice, this list of conditions and the following disclaimer.46* 2. Redistributions in binary form must reproduce the above copyright47* notice, this list of conditions and the following disclaimer in the48* documentation and/or other materials provided with the distribution.49* 3. All advertising materials mentioning features or use of this software50* must display the following acknowledgement:51* This product includes software developed by Terrence R. Lambert.52* 4. The name Terrence R. Lambert may not be used to endorse or promote53* products derived from this software without specific prior written54* permission.55*56* THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY57* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE58* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE59* ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE60* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL61* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS62* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)63* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT64* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY65* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF66* SUCH DAMAGE.67*68*/69#include <sys/param.h>70#include <sys/uio.h>71#include <sys/proc.h>72#include <sys/systm.h>73#include <sys/ioccom.h>74#include <sys/conf.h>7576#include "cdev.h"7778/*79* This is the actual code for the system call... it can't be static because80* it is exported to another part of the module... the only place it needs81* to be referenced is the sysent we are interested in.82*83* To write your own system call using this as a template, you could strip84* out this code and use the rest as a prototype module, changing only the85* function names and the number of arguments to the call in the module86* specific "sysent".87*88* You would have to use the "-R" option of "ld" to ensure a linkable file89* if you were to do this, since you would need to combine multiple ".o"90* files into a single ".o" file for use by "modload".91*/9293#define CDEV_IOCTL1 _IOR('C', 1, u_int)9495/* Stores string recv'd by _write() */96static char buf[512+1];97static size_t len;9899int100mydev_open(struct cdev *dev, int flag, int otyp, struct thread *td)101{102struct proc *procp = td->td_proc;103104printf("mydev_open: dev_t=%lu, flag=%x, otyp=%x, procp=%p\n",105dev2udev(dev), flag, otyp, procp);106memset(&buf, '\0', 513);107len = 0;108return (0);109}110111int112mydev_close(struct cdev *dev, int flag, int otyp, struct thread *td)113{114struct proc *procp = td->td_proc;115116printf("mydev_close: dev_t=%lu, flag=%x, otyp=%x, procp=%p\n",117dev2udev(dev), flag, otyp, procp);118return (0);119}120121int122mydev_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode,123struct thread *td)124{125int error = 0;126struct proc *procp = td->td_proc;127128printf("mydev_ioctl: dev_t=%lu, cmd=%lx, arg=%p, mode=%x procp=%p\n",129dev2udev(dev), cmd, arg, mode, procp);130131switch(cmd) {132case CDEV_IOCTL1:133printf("you called mydev_ioctl CDEV_IOCTL1\n");134break;135default:136printf("No such ioctl for me!\n");137error = EINVAL;138break;139}140return (error);141}142143/*144* mydev_write takes in a character string and saves it145* to buf for later accessing.146*/147int148mydev_write(struct cdev *dev, struct uio *uio, int ioflag)149{150int err = 0;151152printf("mydev_write: dev_t=%lu, uio=%p, ioflag=%d\n",153dev2udev(dev), uio, ioflag);154155err = copyinstr(uio->uio_iov->iov_base, &buf, 512, &len);156if (err != 0) {157printf("Write to \"cdev\" failed.\n");158}159return(err);160}161162/*163* The mydev_read function just takes the buf that was saved164* via mydev_write() and returns it to userland for165* accessing.166*/167int168mydev_read(struct cdev *dev, struct uio *uio, int ioflag)169{170int err = 0;171172printf("mydev_read: dev_t=%lu, uio=%p, ioflag=%d\n",173dev2udev(dev), uio, ioflag);174175if (len <= 0) {176err = -1;177} else { /* copy buf to userland */178copystr(&buf, uio->uio_iov->iov_base, 513, &len);179}180return(err);181}182183184