141681Sdfr/* 08 Nov 1998*/ 241681Sdfr/* 341681Sdfr * cdev.c 441681Sdfr * 541681Sdfr * 08 Nov 1998 Rajesh Vaidheeswarran 641681Sdfr * 741681Sdfr * Copyright (c) 1998 Rajesh Vaidheeswarran 841681Sdfr * All rights reserved. 941681Sdfr * 1041681Sdfr * Redistribution and use in source and binary forms, with or without 1141681Sdfr * modification, are permitted provided that the following conditions 1241681Sdfr * are met: 1341681Sdfr * 1. Redistributions of source code must retain the above copyright 1441681Sdfr * notice, this list of conditions and the following disclaimer. 1541681Sdfr * 2. Redistributions in binary form must reproduce the above copyright 1641681Sdfr * notice, this list of conditions and the following disclaimer in the 1741681Sdfr * documentation and/or other materials provided with the distribution. 1841681Sdfr * 3. All advertising materials mentioning features or use of this software 1941681Sdfr * must display the following acknowledgement: 2041681Sdfr * This product includes software developed by Rajesh Vaidheeswarran. 2141681Sdfr * 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote 2241681Sdfr * products derived from this software without specific prior written 2341681Sdfr * permission. 2441681Sdfr * 2541681Sdfr * THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``AS IS'' AND ANY 2641681Sdfr * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2741681Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2841681Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE RAJESH VAIDHEESWARRAN BE LIABLE 2941681Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3041681Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3141681Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3241681Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3341681Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3441681Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3541681Sdfr * SUCH DAMAGE. 3641681Sdfr * 3741681Sdfr * Copyright (c) 1993 Terrence R. Lambert. 3841681Sdfr * All rights reserved. 3941681Sdfr * 4041681Sdfr * Redistribution and use in source and binary forms, with or without 4141681Sdfr * modification, are permitted provided that the following conditions 4241681Sdfr * are met: 4341681Sdfr * 1. Redistributions of source code must retain the above copyright 4441681Sdfr * notice, this list of conditions and the following disclaimer. 4541681Sdfr * 2. Redistributions in binary form must reproduce the above copyright 4641681Sdfr * notice, this list of conditions and the following disclaimer in the 4741681Sdfr * documentation and/or other materials provided with the distribution. 4841681Sdfr * 3. All advertising materials mentioning features or use of this software 4941681Sdfr * must display the following acknowledgement: 5041681Sdfr * This product includes software developed by Terrence R. Lambert. 5141681Sdfr * 4. The name Terrence R. Lambert may not be used to endorse or promote 5241681Sdfr * products derived from this software without specific prior written 5341681Sdfr * permission. 5441681Sdfr * 5541681Sdfr * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY 5641681Sdfr * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5741681Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5841681Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE 5941681Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 6041681Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 6141681Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 6241681Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 6341681Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 6441681Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 6541681Sdfr * SUCH DAMAGE. 6641681Sdfr * 6766549Ssobomax * 6866549Ssobomax * $FreeBSD: releng/10.3/share/examples/kld/cdev/module/cdev.c 132247 2004-07-16 06:55:34Z tjr $ 6941681Sdfr */ 70130276Smaxim#include <sys/param.h> 7166549Ssobomax#include <sys/uio.h> 7286316Sarr#include <sys/proc.h> 7341681Sdfr#include <sys/systm.h> 7441681Sdfr#include <sys/ioccom.h> 7548275Sdfr#include <sys/conf.h> 7648275Sdfr 7741681Sdfr#include "cdev.h" 7841681Sdfr 7941681Sdfr/* 8041681Sdfr * This is the actual code for the system call... it can't be static because 8141681Sdfr * it is exported to another part of the module... the only place it needs 8241681Sdfr * to be referenced is the sysent we are interested in. 8341681Sdfr * 8441681Sdfr * To write your own system call using this as a template, you could strip 8541681Sdfr * out this code and use the rest as a prototype module, changing only the 8641681Sdfr * function names and the number of arguments to the call in the module 8741681Sdfr * specific "sysent". 8841681Sdfr * 8941681Sdfr * You would have to use the "-R" option of "ld" to ensure a linkable file 9041681Sdfr * if you were to do this, since you would need to combine multiple ".o" 9141681Sdfr * files into a single ".o" file for use by "modload". 9241681Sdfr */ 9341681Sdfr 9441681Sdfr#define CDEV_IOCTL1 _IOR('C', 1, u_int) 9541681Sdfr 9666549Ssobomax/* Stores string recv'd by _write() */ 9766549Ssobomaxstatic char buf[512+1]; 98132247Stjrstatic size_t len; 9966549Ssobomax 10041681Sdfrint 101132247Stjrmydev_open(struct cdev *dev, int flag, int otyp, struct thread *td) 10241681Sdfr{ 10386316Sarr struct proc *procp = td->td_proc; 10486316Sarr 10541681Sdfr printf("mydev_open: dev_t=%d, flag=%x, otyp=%x, procp=%p\n", 10648212Sdfr dev2udev(dev), flag, otyp, procp); 10766549Ssobomax memset(&buf, '\0', 513); 10866549Ssobomax len = 0; 10941681Sdfr return (0); 11041681Sdfr} 11141681Sdfr 11241681Sdfrint 113132247Stjrmydev_close(struct cdev *dev, int flag, int otyp, struct thread *td) 11441681Sdfr{ 11586316Sarr struct proc *procp = td->td_proc; 11686316Sarr 11741681Sdfr printf("mydev_close: dev_t=%d, flag=%x, otyp=%x, procp=%p\n", 11848212Sdfr dev2udev(dev), flag, otyp, procp); 11941681Sdfr return (0); 12041681Sdfr} 12141681Sdfr 12241681Sdfrint 123132247Stjrmydev_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode, 124132247Stjr struct thread *td) 12541681Sdfr{ 12641681Sdfr int error = 0; 12786316Sarr struct proc *procp = td->td_proc; 12841681Sdfr 12941681Sdfr printf("mydev_ioctl: dev_t=%d, cmd=%lx, arg=%p, mode=%x procp=%p\n", 13048275Sdfr dev2udev(dev), cmd, arg, mode, procp); 13141681Sdfr 13241681Sdfr switch(cmd) { 13341681Sdfr case CDEV_IOCTL1: 13441681Sdfr printf("you called mydev_ioctl CDEV_IOCTL1\n"); 13541681Sdfr break; 13641681Sdfr default: 13741681Sdfr printf("No such ioctl for me!\n"); 13841681Sdfr error = EINVAL; 13941681Sdfr break; 14041681Sdfr } 141130276Smaxim return (error); 14241681Sdfr} 14366549Ssobomax 14466549Ssobomax/* 14566549Ssobomax * mydev_write takes in a character string and saves it 14666549Ssobomax * to buf for later accessing. 14766549Ssobomax */ 14866549Ssobomaxint 149132247Stjrmydev_write(struct cdev *dev, struct uio *uio, int ioflag) 15066549Ssobomax{ 15166549Ssobomax int err = 0; 15266549Ssobomax 15366549Ssobomax printf("mydev_write: dev_t=%d, uio=%p, ioflag=%d\n", 15466549Ssobomax dev2udev(dev), uio, ioflag); 15566549Ssobomax 15666549Ssobomax err = copyinstr(uio->uio_iov->iov_base, &buf, 512, &len); 15766549Ssobomax if (err != 0) { 15866549Ssobomax printf("Write to \"cdev\" failed.\n"); 15966549Ssobomax } 16066549Ssobomax return(err); 16166549Ssobomax} 16266549Ssobomax 16366549Ssobomax/* 164130276Smaxim * The mydev_read function just takes the buf that was saved 16566549Ssobomax * via mydev_write() and returns it to userland for 16666549Ssobomax * accessing. 16766549Ssobomax */ 16866549Ssobomaxint 169132247Stjrmydev_read(struct cdev *dev, struct uio *uio, int ioflag) 17066549Ssobomax{ 17166549Ssobomax int err = 0; 17266549Ssobomax 17366549Ssobomax printf("mydev_read: dev_t=%d, uio=%p, ioflag=%d\n", 17466549Ssobomax dev2udev(dev), uio, ioflag); 17566549Ssobomax 17666549Ssobomax if (len <= 0) { 177130276Smaxim err = -1; 17866549Ssobomax } else { /* copy buf to userland */ 17966549Ssobomax copystr(&buf, uio->uio_iov->iov_base, 513, &len); 18066549Ssobomax } 18166549Ssobomax return(err); 18266549Ssobomax} 183