xref: /freebsd/share/examples/kld/cdev/module/cdev.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /* 08 Nov 1998*/
2 /*-
3  * cdev.c
4  *
5  * 08 Nov 1998	Rajesh Vaidheeswarran
6  *
7  * SPDX-License-Identifier: BSD-4-Clause
8  *
9  * Copyright (c) 1998 Rajesh Vaidheeswarran
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by Rajesh Vaidheeswarran.
23  * 4. The name Rajesh Vaidheeswarran may not be used to endorse or promote
24  *    products derived from this software without specific prior written
25  *    permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY RAJESH VAIDHEESWARRAN ``AS IS'' AND ANY
28  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE RAJESH VAIDHEESWARRAN BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39   * Copyright (c) 1993 Terrence R. Lambert.
40  * All rights reserved.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  * 3. All advertising materials mentioning features or use of this software
51  *    must display the following acknowledgement:
52  *      This product includes software developed by Terrence R. Lambert.
53  * 4. The name Terrence R. Lambert may not be used to endorse or promote
54  *    products derived from this software without specific prior written
55  *    permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY
58  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  *
69  *
70  * $FreeBSD$
71  */
72 #include <sys/param.h>
73 #include <sys/uio.h>
74 #include <sys/proc.h>
75 #include <sys/systm.h>
76 #include <sys/ioccom.h>
77 #include <sys/conf.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
103 mydev_open(struct cdev *dev, int flag, int otyp, struct thread *td)
104 {
105     struct proc *procp = td->td_proc;
106 
107     printf("mydev_open: dev_t=%lu, flag=%x, otyp=%x, procp=%p\n",
108 	   dev2udev(dev), flag, otyp, procp);
109     memset(&buf, '\0', 513);
110     len = 0;
111     return (0);
112 }
113 
114 int
115 mydev_close(struct cdev *dev, int flag, int otyp, struct thread *td)
116 {
117     struct proc *procp = td->td_proc;
118 
119     printf("mydev_close: dev_t=%lu, flag=%x, otyp=%x, procp=%p\n",
120 	      dev2udev(dev), flag, otyp, procp);
121     return (0);
122 }
123 
124 int
125 mydev_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
126     struct thread *td)
127 {
128     int error = 0;
129     struct proc *procp = td->td_proc;
130 
131     printf("mydev_ioctl: dev_t=%lu, cmd=%lx, arg=%p, mode=%x procp=%p\n",
132 	   dev2udev(dev), cmd, arg, mode, procp);
133 
134     switch(cmd) {
135     case CDEV_IOCTL1:
136 	printf("you called mydev_ioctl CDEV_IOCTL1\n");
137 	break;
138     default:
139 	printf("No such ioctl for me!\n");
140 	error = EINVAL;
141 	break;
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
151 mydev_write(struct cdev *dev, struct uio *uio, int ioflag)
152 {
153     int err = 0;
154 
155     printf("mydev_write: dev_t=%lu, uio=%p, ioflag=%d\n",
156 	dev2udev(dev), uio, ioflag);
157 
158     err = copyinstr(uio->uio_iov->iov_base, &buf, 512, &len);
159     if (err != 0) {
160 	printf("Write to \"cdev\" failed.\n");
161     }
162     return(err);
163 }
164 
165 /*
166  * The mydev_read function just takes the buf that was saved
167  * via mydev_write() and returns it to userland for
168  * accessing.
169  */
170 int
171 mydev_read(struct cdev *dev, struct uio *uio, int ioflag)
172 {
173     int err = 0;
174 
175     printf("mydev_read: dev_t=%lu, uio=%p, ioflag=%d\n",
176 	dev2udev(dev), uio, ioflag);
177 
178     if (len <= 0) {
179 	err = -1;
180     } else {	/* copy buf to userland */
181 	copystr(&buf, uio->uio_iov->iov_base, 513, &len);
182     }
183     return(err);
184 }
185