1 | /* $NetBSD: cir.c,v 1.32 2016/07/14 10:19:06 msaitoh Exp $ */ |
2 | |
3 | /* |
4 | * Copyright (c) 2001 The NetBSD Foundation, Inc. |
5 | * All rights reserved. |
6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Lennart Augustsson (lennart@augustsson.net). |
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 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
31 | |
32 | #include <sys/cdefs.h> |
33 | __KERNEL_RCSID(0, "$NetBSD: cir.c,v 1.32 2016/07/14 10:19:06 msaitoh Exp $" ); |
34 | |
35 | #include <sys/param.h> |
36 | #include <sys/systm.h> |
37 | #include <sys/ioctl.h> |
38 | #include <sys/kernel.h> |
39 | #include <sys/device.h> |
40 | #include <sys/conf.h> |
41 | #include <sys/poll.h> |
42 | #include <sys/select.h> |
43 | #include <sys/vnode.h> |
44 | #include <sys/module.h> |
45 | |
46 | #include <dev/ir/ir.h> |
47 | #include <dev/ir/cirio.h> |
48 | #include <dev/ir/cirvar.h> |
49 | |
50 | dev_type_open(ciropen); |
51 | dev_type_close(circlose); |
52 | dev_type_read(cirread); |
53 | dev_type_write(cirwrite); |
54 | dev_type_ioctl(cirioctl); |
55 | dev_type_poll(cirpoll); |
56 | |
57 | const struct cdevsw cir_cdevsw = { |
58 | .d_open = ciropen, |
59 | .d_close = circlose, |
60 | .d_read = cirread, |
61 | .d_write = cirwrite, |
62 | .d_ioctl = cirioctl, |
63 | .d_stop = nostop, |
64 | .d_tty = notty, |
65 | .d_poll = cirpoll, |
66 | .d_mmap = nommap, |
67 | .d_kqfilter = nokqfilter, |
68 | .d_discard = nodiscard, |
69 | .d_flag = D_OTHER |
70 | }; |
71 | |
72 | int cir_match(device_t parent, cfdata_t match, void *aux); |
73 | void cir_attach(device_t parent, device_t self, void *aux); |
74 | int cir_detach(device_t self, int flags); |
75 | |
76 | CFATTACH_DECL_NEW(cir, sizeof(struct cir_softc), |
77 | cir_match, cir_attach, cir_detach, NULL); |
78 | |
79 | extern struct cfdriver cir_cd; |
80 | |
81 | #define CIRUNIT(dev) (minor(dev)) |
82 | |
83 | int |
84 | cir_match(device_t parent, cfdata_t match, void *aux) |
85 | { |
86 | struct ir_attach_args *ia = aux; |
87 | |
88 | return (ia->ia_type == IR_TYPE_CIR); |
89 | } |
90 | |
91 | void |
92 | cir_attach(device_t parent, device_t self, void *aux) |
93 | { |
94 | struct cir_softc *sc = device_private(self); |
95 | struct ir_attach_args *ia = aux; |
96 | |
97 | sc->sc_dev = self; |
98 | |
99 | selinit(&sc->sc_rdsel); |
100 | sc->sc_methods = ia->ia_methods; |
101 | sc->sc_handle = ia->ia_handle; |
102 | |
103 | #ifdef DIAGNOSTIC |
104 | if (sc->sc_methods->im_read == NULL || |
105 | sc->sc_methods->im_write == NULL || |
106 | sc->sc_methods->im_setparams == NULL) |
107 | panic("%s: missing methods" , device_xname(sc->sc_dev)); |
108 | #endif |
109 | aprint_naive("\n" ); |
110 | aprint_normal("\n" ); |
111 | } |
112 | |
113 | int |
114 | cir_detach(device_t self, int flags) |
115 | { |
116 | struct cir_softc *sc = device_private(self); |
117 | int maj, mn; |
118 | |
119 | /* locate the major number */ |
120 | maj = cdevsw_lookup_major(&cir_cdevsw); |
121 | |
122 | /* Nuke the vnodes for any open instances (calls close). */ |
123 | mn = device_unit(self); |
124 | vdevgone(maj, mn, mn, VCHR); |
125 | |
126 | seldestroy(&sc->sc_rdsel); |
127 | |
128 | return (0); |
129 | } |
130 | |
131 | int |
132 | ciropen(dev_t dev, int flag, int mode, struct lwp *l) |
133 | { |
134 | struct cir_softc *sc; |
135 | int error; |
136 | |
137 | sc = device_lookup_private(&cir_cd, CIRUNIT(dev)); |
138 | if (sc == NULL) |
139 | return (ENXIO); |
140 | if (!device_is_active(sc->sc_dev)) |
141 | return (EIO); |
142 | if (sc->sc_open) |
143 | return (EBUSY); |
144 | |
145 | sc->sc_rdframes = 0; |
146 | if (sc->sc_methods->im_open != NULL) { |
147 | error = sc->sc_methods->im_open(sc->sc_handle, flag, mode, |
148 | l->l_proc); |
149 | if (error) |
150 | return (error); |
151 | } |
152 | sc->sc_open = 1; |
153 | return (0); |
154 | } |
155 | |
156 | int |
157 | circlose(dev_t dev, int flag, int mode, struct lwp *l) |
158 | { |
159 | struct cir_softc *sc; |
160 | int error; |
161 | |
162 | sc = device_lookup_private(&cir_cd, CIRUNIT(dev)); |
163 | if (sc == NULL) |
164 | return (ENXIO); |
165 | if (sc->sc_methods->im_close != NULL) |
166 | error = sc->sc_methods->im_close(sc->sc_handle, flag, mode, |
167 | l->l_proc); |
168 | else |
169 | error = 0; |
170 | sc->sc_open = 0; |
171 | return (error); |
172 | } |
173 | |
174 | int |
175 | cirread(dev_t dev, struct uio *uio, int flag) |
176 | { |
177 | struct cir_softc *sc; |
178 | |
179 | sc = device_lookup_private(&cir_cd, CIRUNIT(dev)); |
180 | if (sc == NULL) |
181 | return (ENXIO); |
182 | if (!device_is_active(sc->sc_dev)) |
183 | return (EIO); |
184 | return (sc->sc_methods->im_read(sc->sc_handle, uio, flag)); |
185 | } |
186 | |
187 | int |
188 | cirwrite(dev_t dev, struct uio *uio, int flag) |
189 | { |
190 | struct cir_softc *sc; |
191 | |
192 | sc = device_lookup_private(&cir_cd, CIRUNIT(dev)); |
193 | if (sc == NULL) |
194 | return (ENXIO); |
195 | if (!device_is_active(sc->sc_dev)) |
196 | return (EIO); |
197 | return (sc->sc_methods->im_write(sc->sc_handle, uio, flag)); |
198 | } |
199 | |
200 | int |
201 | cirioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l) |
202 | { |
203 | struct cir_softc *sc; |
204 | int error; |
205 | |
206 | sc = device_lookup_private(&cir_cd, CIRUNIT(dev)); |
207 | if (sc == NULL) |
208 | return (ENXIO); |
209 | if (!device_is_active(sc->sc_dev)) |
210 | return (EIO); |
211 | |
212 | switch (cmd) { |
213 | case FIONBIO: |
214 | /* All handled in the upper FS layer. */ |
215 | error = 0; |
216 | break; |
217 | case CIR_GET_PARAMS: |
218 | *(struct cir_params *)addr = sc->sc_params; |
219 | error = 0; |
220 | break; |
221 | case CIR_SET_PARAMS: |
222 | error = sc->sc_methods->im_setparams(sc->sc_handle, |
223 | (struct cir_params *)addr); |
224 | if (!error) |
225 | sc->sc_params = *(struct cir_params *)addr; |
226 | break; |
227 | default: |
228 | error = EINVAL; |
229 | break; |
230 | } |
231 | return (error); |
232 | } |
233 | |
234 | int |
235 | cirpoll(dev_t dev, int events, struct lwp *l) |
236 | { |
237 | struct cir_softc *sc; |
238 | int revents; |
239 | int s; |
240 | |
241 | sc = device_lookup_private(&cir_cd, CIRUNIT(dev)); |
242 | if (sc == NULL) |
243 | return (POLLERR); |
244 | if (!device_is_active(sc->sc_dev)) |
245 | return (POLLERR); |
246 | |
247 | revents = 0; |
248 | s = splir(); |
249 | if (events & (POLLIN | POLLRDNORM)) |
250 | if (sc->sc_rdframes > 0) |
251 | revents |= events & (POLLIN | POLLRDNORM); |
252 | |
253 | #if 0 |
254 | /* How about write? */ |
255 | if (events & (POLLOUT | POLLWRNORM)) |
256 | if (/* ??? */) |
257 | revents |= events & (POLLOUT | POLLWRNORM); |
258 | #endif |
259 | |
260 | if (revents == 0) { |
261 | if (events & (POLLIN | POLLRDNORM)) |
262 | selrecord(l, &sc->sc_rdsel); |
263 | |
264 | #if 0 |
265 | if (events & (POLLOUT | POLLWRNORM)) |
266 | selrecord(p, &sc->sc_wrsel); |
267 | #endif |
268 | } |
269 | |
270 | splx(s); |
271 | return (revents); |
272 | } |
273 | |
274 | MODULE(MODULE_CLASS_DRIVER, cir, "ir" ); |
275 | |
276 | #ifdef _MODULE |
277 | #include "ioconf.c" |
278 | #endif |
279 | |
280 | static int |
281 | cir_modcmd(modcmd_t cmd, void *opaque) |
282 | { |
283 | int error = 0; |
284 | #ifdef _MODULE |
285 | int bmaj = -1, cmaj = -1; |
286 | #endif |
287 | |
288 | switch (cmd) { |
289 | case MODULE_CMD_INIT: |
290 | #ifdef _MODULE |
291 | error = config_init_component(cfdriver_ioconf_cir, |
292 | cfattach_ioconf_cir, cfdata_ioconf_cir); |
293 | if (error) |
294 | return error; |
295 | error = devsw_attach("cir" , NULL, &bmaj, &cir_cdevsw, &cmaj); |
296 | if (error) |
297 | config_fini_component(cfdriver_ioconf_cir, |
298 | cfattach_ioconf_cir, cfdata_ioconf_cir); |
299 | #endif |
300 | return error; |
301 | case MODULE_CMD_FINI: |
302 | #ifdef _MODULE |
303 | devsw_detach(NULL, &cir_cdevsw); |
304 | return config_fini_component(cfdriver_ioconf_cir, |
305 | cfattach_ioconf_cir, cfdata_ioconf_cir); |
306 | #endif |
307 | return error; |
308 | default: |
309 | return ENOTTY; |
310 | } |
311 | } |
312 | |