xref: /freebsd/share/examples/kld/cdev/module/cdev.c (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
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$
69  */
70 #include <sys/param.h>
71 #include <sys/uio.h>
72 #include <sys/proc.h>
73 #include <sys/systm.h>
74 #include <sys/ioccom.h>
75 #include <sys/conf.h>
76 
77 #include "cdev.h"
78 
79 /*
80  * This is the actual code for the system call... it can't be static because
81  * it is exported to another part of the module... the only place it needs
82  * to be referenced is the sysent we are interested in.
83  *
84  * To write your own system call using this as a template, you could strip
85  * out this code and use the rest as a prototype module, changing only the
86  * function names and the number of arguments to the call in the module
87  * specific "sysent".
88  *
89  * You would have to use the "-R" option of "ld" to ensure a linkable file
90  * if you were to do this, since you would need to combine multiple ".o"
91  * files into a single ".o" file for use by "modload".
92  */
93 
94 #define CDEV_IOCTL1         _IOR('C', 1, u_int)
95 
96 /* Stores string recv'd by _write() */
97 static char buf[512+1];
98 static int len;
99 
100 int
101 mydev_open(dev_t dev, int flag, int otyp, struct thread *td)
102 {
103     struct proc *procp = td->td_proc;
104 
105     printf("mydev_open: dev_t=%d, flag=%x, otyp=%x, procp=%p\n",
106 	   dev2udev(dev), flag, otyp, procp);
107     memset(&buf, '\0', 513);
108     len = 0;
109     return (0);
110 }
111 
112 int
113 mydev_close(dev_t dev, int flag, int otyp, struct thread *td)
114 {
115     struct proc *procp = td->td_proc;
116 
117     printf("mydev_close: dev_t=%d, flag=%x, otyp=%x, procp=%p\n",
118 	      dev2udev(dev), flag, otyp, procp);
119     return (0);
120 }
121 
122 int
123 mydev_ioctl(dev_t dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
124 {
125     int error = 0;
126     struct proc *procp = td->td_proc;
127 
128     printf("mydev_ioctl: dev_t=%d, cmd=%lx, arg=%p, mode=%x procp=%p\n",
129 	   dev2udev(dev), cmd, arg, mode, procp);
130 
131     switch(cmd) {
132     case CDEV_IOCTL1:
133 	printf("you called mydev_ioctl CDEV_IOCTL1\n");
134 	break;
135     default:
136 	printf("No such ioctl for me!\n");
137 	error = EINVAL;
138 	break;
139     }
140     return (error);
141 }
142 
143 /*
144  * mydev_write takes in a character string and saves it
145  * to buf for later accessing.
146  */
147 int
148 mydev_write(dev_t dev, struct uio *uio, int ioflag)
149 {
150     int err = 0;
151 
152     printf("mydev_write: dev_t=%d, uio=%p, ioflag=%d\n",
153 	dev2udev(dev), uio, ioflag);
154 
155     err = copyinstr(uio->uio_iov->iov_base, &buf, 512, &len);
156     if (err != 0) {
157 	printf("Write to \"cdev\" failed.\n");
158     }
159     return(err);
160 }
161 
162 /*
163  * The mydev_read function just takes the buf that was saved
164  * via mydev_write() and returns it to userland for
165  * accessing.
166  */
167 int
168 mydev_read(dev_t dev, struct uio *uio, int ioflag)
169 {
170     int err = 0;
171 
172     printf("mydev_read: dev_t=%d, uio=%p, ioflag=%d\n",
173 	dev2udev(dev), uio, ioflag);
174 
175     if (len <= 0) {
176 	err = -1;
177     } else {	/* copy buf to userland */
178 	copystr(&buf, uio->uio_iov->iov_base, 513, &len);
179     }
180     return(err);
181 }
182