1 /* 08 Nov 1998*/
2 /*
3 * cdev.c
4 *
5 * 08 Nov 1998 Rajesh Vaidheeswarran
6 *
7 * Copyright (c) 1998 Rajesh Vaidheeswarran
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Rajesh Vaidheeswarran.
21 * 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote
22 * products derived from this software without specific prior written
23 * permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``AS IS'' AND ANY
26 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE RAJESH VAIDHEESWARRAN BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * Copyright (c) 1993 Terrence R. Lambert.
38 * All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by Terrence R. Lambert.
51 * 4. The name Terrence R. Lambert may not be used to endorse or promote
52 * products derived from this software without specific prior written
53 * permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY
56 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 *
68 * $FreeBSD: src/share/examples/kld/cdev/module/cdev.c,v 1.3.2.1 2000/10/25 09:02:34 sobomax Exp $
69 */
70 #include <sys/types.h>
71 #include <sys/uio.h>
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/ioccom.h>
75 #include <sys/systm.h>
76 #include <sys/conf.h>
77 #include <sys/device.h>
78
79 #include "cdev.h"
80
81 /*
82 * This is the actual code for the system call... it can't be static because
83 * it is exported to another part of the module... the only place it needs
84 * to be referenced is the sysent we are interested in.
85 *
86 * To write your own system call using this as a template, you could strip
87 * out this code and use the rest as a prototype module, changing only the
88 * function names and the number of arguments to the call in the module
89 * specific "sysent".
90 *
91 * You would have to use the "-R" option of "ld" to ensure a linkable file
92 * if you were to do this, since you would need to combine multiple ".o"
93 * files into a single ".o" file for use by "modload".
94 */
95
96 #define CDEV_IOCTL1 _IOR('C', 1, u_int)
97
98 /* Stores string recv'd by _write() */
99 static char buf[512+1];
100 static size_t len;
101
102 int
mydev_open(struct dev_open_args * args)103 mydev_open(struct dev_open_args *args)
104 {
105 kprintf("mydev_open: dev_t=%d, flags=%x, type=%x\n",
106 devid_from_dev(args->a_head.a_dev), args->a_oflags,
107 args->a_devtype);
108 memset(&buf, '\0', 513);
109 len = 0;
110
111 return (0);
112 }
113
114 int
mydev_close(struct dev_close_args * args)115 mydev_close(struct dev_close_args *args)
116 {
117 kprintf("mydev_close: dev_t=%d, flags=%x, type=%x\n",
118 devid_from_dev(args->a_head.a_dev), args->a_fflag,
119 args->a_devtype);
120
121 return (0);
122 }
123
124 int
mydev_ioctl(struct dev_ioctl_args * args)125 mydev_ioctl(struct dev_ioctl_args *args)
126 {
127 int error = 0;
128
129 kprintf("mydev_ioctl: dev_t=%d, cmd=%lx, arg=%p, mode=%x\n",
130 devid_from_dev(args->a_head.a_dev), args->a_cmd, args->a_data,
131 args->a_fflag);
132
133 switch(args->a_cmd) {
134 case CDEV_IOCTL1:
135 kprintf("you called mydev_ioctl CDEV_IOCTL1\n");
136 break;
137 default:
138 kprintf("No such ioctl for me!\n");
139 error = EINVAL;
140 break;
141 }
142
143 return (error);
144 }
145
146 /*
147 * mydev_write takes in a character string and saves it
148 * to buf for later accessing.
149 */
150 int
mydev_write(struct dev_write_args * args)151 mydev_write(struct dev_write_args *args)
152 {
153 int err = 0;
154
155 kprintf("mydev_write: dev_t=%d, uio=%p, ioflag=%d\n",
156 devid_from_dev(args->a_head.a_dev), args->a_uio, args->a_ioflag);
157
158 err = copyinstr(args->a_uio->uio_iov->iov_base, &buf, 512, &len);
159 if (err != 0) {
160 kprintf("Write to \"cdev\" failed.\n");
161 }
162
163 return (err);
164 }
165
166 /*
167 * The mydev_read function just takes the buf that was saved
168 * via mydev_write() and returns it to userland for
169 * accessing.
170 */
171 int
mydev_read(struct dev_read_args * args)172 mydev_read(struct dev_read_args *args)
173 {
174 int err = 0;
175
176 kprintf("mydev_read: dev_t=%d, uio=%p, ioflag=%d\n",
177 devid_from_dev(args->a_head.a_dev), args->a_uio, args->a_ioflag);
178
179 if (len <= 0) {
180 err = -1;
181 }
182 else {
183 /* copy buf to userland */
184 copystr(&buf, args->a_uio->uio_iov->iov_base, 513, &len);
185 }
186
187 return (err);
188 }
189