cir.c revision 1.30 1 /* $NetBSD: cir.c,v 1.30 2014/03/16 05:20:28 dholland 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 (at) 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.30 2014/03/16 05:20:28 dholland 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_flag = D_OTHER
69 };
70
71 int cir_match(device_t parent, cfdata_t match, void *aux);
72 void cir_attach(device_t parent, device_t self, void *aux);
73 int cir_detach(device_t self, int flags);
74
75 CFATTACH_DECL_NEW(cir, sizeof(struct cir_softc),
76 cir_match, cir_attach, cir_detach, NULL);
77
78 extern struct cfdriver cir_cd;
79
80 #define CIRUNIT(dev) (minor(dev))
81
82 int
83 cir_match(device_t parent, cfdata_t match, void *aux)
84 {
85 struct ir_attach_args *ia = aux;
86
87 return (ia->ia_type == IR_TYPE_CIR);
88 }
89
90 void
91 cir_attach(device_t parent, device_t self, void *aux)
92 {
93 struct cir_softc *sc = device_private(self);
94 struct ir_attach_args *ia = aux;
95
96 sc->sc_dev = self;
97
98 selinit(&sc->sc_rdsel);
99 sc->sc_methods = ia->ia_methods;
100 sc->sc_handle = ia->ia_handle;
101
102 #ifdef DIAGNOSTIC
103 if (sc->sc_methods->im_read == NULL ||
104 sc->sc_methods->im_write == NULL ||
105 sc->sc_methods->im_setparams == NULL)
106 panic("%s: missing methods", device_xname(sc->sc_dev));
107 #endif
108 printf("\n");
109 }
110
111 int
112 cir_detach(device_t self, int flags)
113 {
114 struct cir_softc *sc = device_private(self);
115 int maj, mn;
116
117 /* locate the major number */
118 maj = cdevsw_lookup_major(&cir_cdevsw);
119
120 /* Nuke the vnodes for any open instances (calls close). */
121 mn = device_unit(self);
122 vdevgone(maj, mn, mn, VCHR);
123
124 seldestroy(&sc->sc_rdsel);
125
126 return (0);
127 }
128
129 int
130 ciropen(dev_t dev, int flag, int mode, struct lwp *l)
131 {
132 struct cir_softc *sc;
133 int error;
134
135 sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
136 if (sc == NULL)
137 return (ENXIO);
138 if (!device_is_active(sc->sc_dev))
139 return (EIO);
140 if (sc->sc_open)
141 return (EBUSY);
142
143 sc->sc_rdframes = 0;
144 if (sc->sc_methods->im_open != NULL) {
145 error = sc->sc_methods->im_open(sc->sc_handle, flag, mode,
146 l->l_proc);
147 if (error)
148 return (error);
149 }
150 sc->sc_open = 1;
151 return (0);
152 }
153
154 int
155 circlose(dev_t dev, int flag, int mode, struct lwp *l)
156 {
157 struct cir_softc *sc;
158 int error;
159
160 sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
161 if (sc == NULL)
162 return (ENXIO);
163 if (sc->sc_methods->im_close != NULL)
164 error = sc->sc_methods->im_close(sc->sc_handle, flag, mode,
165 l->l_proc);
166 else
167 error = 0;
168 sc->sc_open = 0;
169 return (error);
170 }
171
172 int
173 cirread(dev_t dev, struct uio *uio, int flag)
174 {
175 struct cir_softc *sc;
176
177 sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
178 if (sc == NULL)
179 return (ENXIO);
180 if (!device_is_active(sc->sc_dev))
181 return (EIO);
182 return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
183 }
184
185 int
186 cirwrite(dev_t dev, struct uio *uio, int flag)
187 {
188 struct cir_softc *sc;
189
190 sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
191 if (sc == NULL)
192 return (ENXIO);
193 if (!device_is_active(sc->sc_dev))
194 return (EIO);
195 return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
196 }
197
198 int
199 cirioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
200 {
201 struct cir_softc *sc;
202 int error;
203
204 sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
205 if (sc == NULL)
206 return (ENXIO);
207 if (!device_is_active(sc->sc_dev))
208 return (EIO);
209
210 switch (cmd) {
211 case FIONBIO:
212 /* All handled in the upper FS layer. */
213 error = 0;
214 break;
215 case CIR_GET_PARAMS:
216 *(struct cir_params *)addr = sc->sc_params;
217 error = 0;
218 break;
219 case CIR_SET_PARAMS:
220 error = sc->sc_methods->im_setparams(sc->sc_handle,
221 (struct cir_params *)addr);
222 if (!error)
223 sc->sc_params = *(struct cir_params *)addr;
224 break;
225 default:
226 error = EINVAL;
227 break;
228 }
229 return (error);
230 }
231
232 int
233 cirpoll(dev_t dev, int events, struct lwp *l)
234 {
235 struct cir_softc *sc;
236 int revents;
237 int s;
238
239 sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
240 if (sc == NULL)
241 return (POLLERR);
242 if (!device_is_active(sc->sc_dev))
243 return (POLLERR);
244
245 revents = 0;
246 s = splir();
247 if (events & (POLLIN | POLLRDNORM))
248 if (sc->sc_rdframes > 0)
249 revents |= events & (POLLIN | POLLRDNORM);
250
251 #if 0
252 /* How about write? */
253 if (events & (POLLOUT | POLLWRNORM))
254 if (/* ??? */)
255 revents |= events & (POLLOUT | POLLWRNORM);
256 #endif
257
258 if (revents == 0) {
259 if (events & (POLLIN | POLLRDNORM))
260 selrecord(l, &sc->sc_rdsel);
261
262 #if 0
263 if (events & (POLLOUT | POLLWRNORM))
264 selrecord(p, &sc->sc_wrsel);
265 #endif
266 }
267
268 splx(s);
269 return (revents);
270 }
271
272 MODULE(MODULE_CLASS_DRIVER, cir, "ir");
273
274 #ifdef _MODULE
275 #include "ioconf.c"
276 #endif
277
278 static int
279 cir_modcmd(modcmd_t cmd, void *opaque)
280 {
281 int error = 0;
282 #ifdef _MODULE
283 int bmaj = -1, cmaj = -1;
284 #endif
285
286 switch (cmd) {
287 case MODULE_CMD_INIT:
288 #ifdef _MODULE
289 error = config_init_component(cfdriver_ioconf_cir,
290 cfattach_ioconf_cir, cfdata_ioconf_cir);
291 if (error)
292 return error;
293 error = devsw_attach("cir", NULL, &bmaj, &cir_cdevsw, &cmaj);
294 if (error)
295 config_fini_component(cfdriver_ioconf_cir,
296 cfattach_ioconf_cir, cfdata_ioconf_cir);
297 #endif
298 return error;
299 case MODULE_CMD_FINI:
300 #ifdef _MODULE
301 devsw_detach(NULL, &cir_cdevsw);
302 return config_fini_component(cfdriver_ioconf_cir,
303 cfattach_ioconf_cir, cfdata_ioconf_cir);
304 #endif
305 return error;
306 default:
307 return ENOTTY;
308 }
309 }
310