1abbcaa0aSDoug Rabson /* 08 Nov 1998*/
2f0cfa1b1SPedro F. Giffuni /*-
3abbcaa0aSDoug Rabson * cdev.c
4abbcaa0aSDoug Rabson *
5abbcaa0aSDoug Rabson * 08 Nov 1998 Rajesh Vaidheeswarran
6abbcaa0aSDoug Rabson *
7f0cfa1b1SPedro F. Giffuni * SPDX-License-Identifier: BSD-4-Clause
8f0cfa1b1SPedro F. Giffuni *
9abbcaa0aSDoug Rabson * Copyright (c) 1998 Rajesh Vaidheeswarran
10abbcaa0aSDoug Rabson * All rights reserved.
11abbcaa0aSDoug Rabson *
12abbcaa0aSDoug Rabson * Redistribution and use in source and binary forms, with or without
13abbcaa0aSDoug Rabson * modification, are permitted provided that the following conditions
14abbcaa0aSDoug Rabson * are met:
15abbcaa0aSDoug Rabson * 1. Redistributions of source code must retain the above copyright
16abbcaa0aSDoug Rabson * notice, this list of conditions and the following disclaimer.
17abbcaa0aSDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright
18abbcaa0aSDoug Rabson * notice, this list of conditions and the following disclaimer in the
19abbcaa0aSDoug Rabson * documentation and/or other materials provided with the distribution.
20abbcaa0aSDoug Rabson * 3. All advertising materials mentioning features or use of this software
21abbcaa0aSDoug Rabson * must display the following acknowledgement:
22abbcaa0aSDoug Rabson * This product includes software developed by Rajesh Vaidheeswarran.
23abbcaa0aSDoug Rabson * 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote
24abbcaa0aSDoug Rabson * products derived from this software without specific prior written
25abbcaa0aSDoug Rabson * permission.
26abbcaa0aSDoug Rabson *
27abbcaa0aSDoug Rabson * THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``AS IS'' AND ANY
28abbcaa0aSDoug Rabson * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29abbcaa0aSDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30abbcaa0aSDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE RAJESH VAIDHEESWARRAN BE LIABLE
31abbcaa0aSDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32abbcaa0aSDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33abbcaa0aSDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34abbcaa0aSDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35abbcaa0aSDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36abbcaa0aSDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37abbcaa0aSDoug Rabson * SUCH DAMAGE.
38abbcaa0aSDoug Rabson *
39abbcaa0aSDoug Rabson * Copyright (c) 1993 Terrence R. Lambert.
40abbcaa0aSDoug Rabson * All rights reserved.
41abbcaa0aSDoug Rabson *
42abbcaa0aSDoug Rabson * Redistribution and use in source and binary forms, with or without
43abbcaa0aSDoug Rabson * modification, are permitted provided that the following conditions
44abbcaa0aSDoug Rabson * are met:
45abbcaa0aSDoug Rabson * 1. Redistributions of source code must retain the above copyright
46abbcaa0aSDoug Rabson * notice, this list of conditions and the following disclaimer.
47abbcaa0aSDoug Rabson * 2. Redistributions in binary form must reproduce the above copyright
48abbcaa0aSDoug Rabson * notice, this list of conditions and the following disclaimer in the
49abbcaa0aSDoug Rabson * documentation and/or other materials provided with the distribution.
50abbcaa0aSDoug Rabson * 3. All advertising materials mentioning features or use of this software
51abbcaa0aSDoug Rabson * must display the following acknowledgement:
52abbcaa0aSDoug Rabson * This product includes software developed by Terrence R. Lambert.
53abbcaa0aSDoug Rabson * 4. The name Terrence R. Lambert may not be used to endorse or promote
54abbcaa0aSDoug Rabson * products derived from this software without specific prior written
55abbcaa0aSDoug Rabson * permission.
56abbcaa0aSDoug Rabson *
57abbcaa0aSDoug Rabson * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY
58abbcaa0aSDoug Rabson * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59abbcaa0aSDoug Rabson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60abbcaa0aSDoug Rabson * ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
61abbcaa0aSDoug Rabson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62abbcaa0aSDoug Rabson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63abbcaa0aSDoug Rabson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64abbcaa0aSDoug Rabson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65abbcaa0aSDoug Rabson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66abbcaa0aSDoug Rabson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67abbcaa0aSDoug Rabson * SUCH DAMAGE.
68abbcaa0aSDoug Rabson *
69abbcaa0aSDoug Rabson */
70abbcaa0aSDoug Rabson #include <sys/param.h>
71ef92aa11SMaxim Konovalov #include <sys/uio.h>
72a220d00eSAndrew R. Reiter #include <sys/proc.h>
73abbcaa0aSDoug Rabson #include <sys/systm.h>
74abbcaa0aSDoug Rabson #include <sys/ioccom.h>
750f2a20dfSDoug Rabson #include <sys/conf.h>
760f2a20dfSDoug Rabson
77abbcaa0aSDoug Rabson #include "cdev.h"
78abbcaa0aSDoug Rabson
79abbcaa0aSDoug Rabson /*
80abbcaa0aSDoug Rabson * This is the actual code for the system call... it can't be static because
81abbcaa0aSDoug Rabson * it is exported to another part of the module... the only place it needs
82abbcaa0aSDoug Rabson * to be referenced is the sysent we are interested in.
83abbcaa0aSDoug Rabson *
84abbcaa0aSDoug Rabson * To write your own system call using this as a template, you could strip
85abbcaa0aSDoug Rabson * out this code and use the rest as a prototype module, changing only the
86abbcaa0aSDoug Rabson * function names and the number of arguments to the call in the module
87abbcaa0aSDoug Rabson * specific "sysent".
88abbcaa0aSDoug Rabson *
89abbcaa0aSDoug Rabson * You would have to use the "-R" option of "ld" to ensure a linkable file
90abbcaa0aSDoug Rabson * if you were to do this, since you would need to combine multiple ".o"
91abbcaa0aSDoug Rabson * files into a single ".o" file for use by "modload".
92abbcaa0aSDoug Rabson */
93abbcaa0aSDoug Rabson
94abbcaa0aSDoug Rabson #define CDEV_IOCTL1 _IOR('C', 1, u_int)
95abbcaa0aSDoug Rabson
967320fd3aSMaxim Sobolev /* Stores string recv'd by _write() */
977320fd3aSMaxim Sobolev static char buf[512+1];
985db700e0STim J. Robbins static size_t len;
997320fd3aSMaxim Sobolev
100abbcaa0aSDoug Rabson int
mydev_open(struct cdev * dev,int flag,int otyp,struct thread * td)1015db700e0STim J. Robbins mydev_open(struct cdev *dev, int flag, int otyp, struct thread *td)
102abbcaa0aSDoug Rabson {
103a220d00eSAndrew R. Reiter struct proc *procp = td->td_proc;
104a220d00eSAndrew R. Reiter
105*6058f720SEitan Adler printf("mydev_open: dev_t=%lu, flag=%x, otyp=%x, procp=%p\n",
106e523e96fSDoug Rabson dev2udev(dev), flag, otyp, procp);
1077320fd3aSMaxim Sobolev memset(&buf, '\0', 513);
1087320fd3aSMaxim Sobolev len = 0;
109abbcaa0aSDoug Rabson return (0);
110abbcaa0aSDoug Rabson }
111abbcaa0aSDoug Rabson
112abbcaa0aSDoug Rabson int
mydev_close(struct cdev * dev,int flag,int otyp,struct thread * td)1135db700e0STim J. Robbins mydev_close(struct cdev *dev, int flag, int otyp, struct thread *td)
114abbcaa0aSDoug Rabson {
115a220d00eSAndrew R. Reiter struct proc *procp = td->td_proc;
116a220d00eSAndrew R. Reiter
117*6058f720SEitan Adler printf("mydev_close: dev_t=%lu, flag=%x, otyp=%x, procp=%p\n",
118e523e96fSDoug Rabson dev2udev(dev), flag, otyp, procp);
119abbcaa0aSDoug Rabson return (0);
120abbcaa0aSDoug Rabson }
121abbcaa0aSDoug Rabson
122abbcaa0aSDoug Rabson int
mydev_ioctl(struct cdev * dev,u_long cmd,caddr_t arg,int mode,struct thread * td)1235db700e0STim J. Robbins mydev_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
1245db700e0STim J. Robbins struct thread *td)
125abbcaa0aSDoug Rabson {
126abbcaa0aSDoug Rabson int error = 0;
127a220d00eSAndrew R. Reiter struct proc *procp = td->td_proc;
128abbcaa0aSDoug Rabson
129*6058f720SEitan Adler printf("mydev_ioctl: dev_t=%lu, cmd=%lx, arg=%p, mode=%x procp=%p\n",
1300f2a20dfSDoug Rabson dev2udev(dev), cmd, arg, mode, procp);
131abbcaa0aSDoug Rabson
132abbcaa0aSDoug Rabson switch(cmd) {
133abbcaa0aSDoug Rabson case CDEV_IOCTL1:
134abbcaa0aSDoug Rabson printf("you called mydev_ioctl CDEV_IOCTL1\n");
135abbcaa0aSDoug Rabson break;
136abbcaa0aSDoug Rabson default:
137abbcaa0aSDoug Rabson printf("No such ioctl for me!\n");
138abbcaa0aSDoug Rabson error = EINVAL;
139abbcaa0aSDoug Rabson break;
140abbcaa0aSDoug Rabson }
141ef92aa11SMaxim Konovalov return (error);
142abbcaa0aSDoug Rabson }
1437320fd3aSMaxim Sobolev
1447320fd3aSMaxim Sobolev /*
1457320fd3aSMaxim Sobolev * mydev_write takes in a character string and saves it
1467320fd3aSMaxim Sobolev * to buf for later accessing.
1477320fd3aSMaxim Sobolev */
1487320fd3aSMaxim Sobolev int
mydev_write(struct cdev * dev,struct uio * uio,int ioflag)1495db700e0STim J. Robbins mydev_write(struct cdev *dev, struct uio *uio, int ioflag)
1507320fd3aSMaxim Sobolev {
1517320fd3aSMaxim Sobolev int err = 0;
1527320fd3aSMaxim Sobolev
153*6058f720SEitan Adler printf("mydev_write: dev_t=%lu, uio=%p, ioflag=%d\n",
1547320fd3aSMaxim Sobolev dev2udev(dev), uio, ioflag);
1557320fd3aSMaxim Sobolev
1567320fd3aSMaxim Sobolev err = copyinstr(uio->uio_iov->iov_base, &buf, 512, &len);
1577320fd3aSMaxim Sobolev if (err != 0) {
1587320fd3aSMaxim Sobolev printf("Write to \"cdev\" failed.\n");
1597320fd3aSMaxim Sobolev }
1607320fd3aSMaxim Sobolev return(err);
1617320fd3aSMaxim Sobolev }
1627320fd3aSMaxim Sobolev
1637320fd3aSMaxim Sobolev /*
1647320fd3aSMaxim Sobolev * The mydev_read function just takes the buf that was saved
1657320fd3aSMaxim Sobolev * via mydev_write() and returns it to userland for
1667320fd3aSMaxim Sobolev * accessing.
1677320fd3aSMaxim Sobolev */
1687320fd3aSMaxim Sobolev int
mydev_read(struct cdev * dev,struct uio * uio,int ioflag)1695db700e0STim J. Robbins mydev_read(struct cdev *dev, struct uio *uio, int ioflag)
1707320fd3aSMaxim Sobolev {
1717320fd3aSMaxim Sobolev int err = 0;
1727320fd3aSMaxim Sobolev
173*6058f720SEitan Adler printf("mydev_read: dev_t=%lu, uio=%p, ioflag=%d\n",
1747320fd3aSMaxim Sobolev dev2udev(dev), uio, ioflag);
1757320fd3aSMaxim Sobolev
1767320fd3aSMaxim Sobolev if (len <= 0) {
1777320fd3aSMaxim Sobolev err = -1;
1787320fd3aSMaxim Sobolev } else { /* copy buf to userland */
1797320fd3aSMaxim Sobolev copystr(&buf, uio->uio_iov->iov_base, 513, &len);
1807320fd3aSMaxim Sobolev }
1817320fd3aSMaxim Sobolev return(err);
1827320fd3aSMaxim Sobolev }
183