irframe.c revision 1.42 1 /* $NetBSD: irframe.c,v 1.42 2008/06/10 22:53:08 cegger 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: irframe.c,v 1.42 2008/06/10 22:53:08 cegger Exp $");
34
35 #include "irframe.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/ioctl.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/conf.h>
43 #include <sys/malloc.h>
44 #include <sys/poll.h>
45 #include <sys/select.h>
46 #include <sys/vnode.h>
47
48 #include <dev/ir/ir.h>
49 #include <dev/ir/irdaio.h>
50 #include <dev/ir/irframevar.h>
51
52 #ifdef IRFRAME_DEBUG
53 #define DPRINTF(x) if (irframedebug) printf x
54 #define Static
55 int irframedebug = 0;
56 #else
57 #define DPRINTF(x)
58 #define Static static
59 #endif
60
61 dev_type_open(irframeopen);
62 dev_type_close(irframeclose);
63 dev_type_read(irframeread);
64 dev_type_write(irframewrite);
65 dev_type_ioctl(irframeioctl);
66 dev_type_poll(irframepoll);
67 dev_type_kqfilter(irframekqfilter);
68
69 const struct cdevsw irframe_cdevsw = {
70 irframeopen, irframeclose, irframeread, irframewrite, irframeioctl,
71 nostop, notty, irframepoll, nommap, irframekqfilter, D_OTHER,
72 };
73
74 int irframe_match(device_t parent, cfdata_t match, void *aux);
75 int irframe_activate(device_t self, enum devact act);
76
77 Static int irf_set_params(struct irframe_softc *sc, struct irda_params *p);
78 Static int irf_reset_params(struct irframe_softc *sc);
79
80 #if NIRFRAME == 0
81 /* In case we just have tty attachment. */
82 CFDRIVER_DECL(irframe, DV_DULL, NULL);
83 #endif
84
85 CFATTACH_DECL_NEW(irframe, sizeof(struct irframe_softc),
86 irframe_match, irframe_attach, irframe_detach, irframe_activate);
87
88 extern struct cfdriver irframe_cd;
89
90 #define IRFRAMEUNIT(dev) (minor(dev))
91
92 int
93 irframe_match(device_t parent, cfdata_t match, void *aux)
94 {
95 struct ir_attach_args *ia = aux;
96
97 return (ia->ia_type == IR_TYPE_IRFRAME);
98 }
99
100 void
101 irframe_attach(device_t parent, device_t self, void *aux)
102 {
103 struct irframe_softc *sc = device_private(self);
104 struct ir_attach_args *ia = aux;
105 const char *delim;
106 int speeds = 0;
107
108 sc->sc_dev = self;
109 sc->sc_methods = ia->ia_methods;
110 sc->sc_handle = ia->ia_handle;
111
112 #ifdef DIAGNOSTIC
113 if (sc->sc_methods->im_read == NULL ||
114 sc->sc_methods->im_write == NULL ||
115 sc->sc_methods->im_poll == NULL ||
116 sc->sc_methods->im_kqfilter == NULL ||
117 sc->sc_methods->im_set_params == NULL ||
118 sc->sc_methods->im_get_speeds == NULL ||
119 sc->sc_methods->im_get_turnarounds == NULL)
120 panic("%s: missing methods", device_xname(self));
121 #endif
122
123 (void)sc->sc_methods->im_get_speeds(sc->sc_handle, &speeds);
124 sc->sc_speedmask = speeds;
125 delim = ":";
126 if (speeds & IRDA_SPEEDS_SIR) {
127 printf("%s SIR", delim);
128 delim = ",";
129 }
130 if (speeds & IRDA_SPEEDS_MIR) {
131 printf("%s MIR", delim);
132 delim = ",";
133 }
134 if (speeds & IRDA_SPEEDS_FIR) {
135 printf("%s FIR", delim);
136 delim = ",";
137 }
138 if (speeds & IRDA_SPEEDS_VFIR) {
139 printf("%s VFIR", delim);
140 delim = ",";
141 }
142 printf("\n");
143 }
144
145 int
146 irframe_activate(device_t self, enum devact act)
147 {
148 /*struct irframe_softc *sc = device_private(self);*/
149
150 switch (act) {
151 case DVACT_ACTIVATE:
152 return (EOPNOTSUPP);
153
154 case DVACT_DEACTIVATE:
155 break;
156 }
157 return (0);
158 }
159
160 int
161 irframe_detach(device_t self, int flags)
162 {
163 /*struct irframe_softc *sc = device_private(self);*/
164 int maj, mn;
165
166 /* XXX needs reference count */
167
168 /* locate the major number */
169 maj = cdevsw_lookup_major(&irframe_cdevsw);
170
171 /* Nuke the vnodes for any open instances (calls close). */
172 mn = device_unit(self);
173 vdevgone(maj, mn, mn, VCHR);
174
175 return (0);
176 }
177
178 int
179 irframeopen(dev_t dev, int flag, int mode, struct lwp *l)
180 {
181 struct irframe_softc *sc;
182 int error;
183
184 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
185 if (sc == NULL)
186 return (ENXIO);
187 if (!device_is_active(sc->sc_dev))
188 return (EIO);
189 if (sc->sc_open)
190 return (EBUSY);
191 if (sc->sc_methods->im_open != NULL) {
192 error = sc->sc_methods->im_open(sc->sc_handle, flag, mode, l);
193 if (error)
194 return (error);
195 }
196 sc->sc_open = 1;
197 #ifdef DIAGNOSTIC
198 sc->sc_speed = IRDA_DEFAULT_SPEED;
199 #endif
200 (void)irf_reset_params(sc);
201 return (0);
202 }
203
204 int
205 irframeclose(dev_t dev, int flag, int mode, struct lwp *l)
206 {
207 struct irframe_softc *sc;
208 int error;
209
210 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
211 if (sc == NULL)
212 return (ENXIO);
213 sc->sc_open = 0;
214 if (sc->sc_methods->im_close != NULL)
215 error = sc->sc_methods->im_close(sc->sc_handle, flag, mode, l);
216 else
217 error = 0;
218 return (error);
219 }
220
221 int
222 irframeread(dev_t dev, struct uio *uio, int flag)
223 {
224 struct irframe_softc *sc;
225
226 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
227 if (sc == NULL)
228 return (ENXIO);
229 if (!device_is_active(sc->sc_dev) || !sc->sc_open)
230 return (EIO);
231 if (uio->uio_resid < sc->sc_params.maxsize) {
232 #ifdef DIAGNOSTIC
233 printf("irframeread: short read %ld < %d\n",
234 (long)uio->uio_resid, sc->sc_params.maxsize);
235 #endif
236 return (EINVAL);
237 }
238 return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
239 }
240
241 int
242 irframewrite(dev_t dev, struct uio *uio, int flag)
243 {
244 struct irframe_softc *sc;
245
246 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
247 if (sc == NULL)
248 return (ENXIO);
249 if (!device_is_active(sc->sc_dev) || !sc->sc_open)
250 return (EIO);
251 if (uio->uio_resid > sc->sc_params.maxsize) {
252 #ifdef DIAGNOSTIC
253 printf("irframeread: long write %ld > %d\n",
254 (long)uio->uio_resid, sc->sc_params.maxsize);
255 #endif
256 return (EINVAL);
257 }
258 return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
259 }
260
261 int
262 irf_set_params(struct irframe_softc *sc, struct irda_params *p)
263 {
264 int error;
265
266 DPRINTF(("irf_set_params: set params speed=%u ebofs=%u maxsize=%u "
267 "speedmask=0x%x\n", p->speed, p->ebofs, p->maxsize,
268 sc->sc_speedmask));
269
270 if (p->maxsize > IRDA_MAX_FRAME_SIZE) {
271 #ifdef IRFRAME_DEBUG
272 printf("irf_set_params: bad maxsize=%u\n", p->maxsize);
273 #endif
274 return (EINVAL);
275 }
276
277 if (p->ebofs > IRDA_MAX_EBOFS) {
278 #ifdef IRFRAME_DEBUG
279 printf("irf_set_params: bad maxsize=%u\n", p->maxsize);
280 #endif
281 return (EINVAL);
282 }
283
284 #define CONC(x,y) x##y
285 #define CASE(s) case s: if (!(sc->sc_speedmask & CONC(IRDA_SPEED_,s))) return (EINVAL); break
286 switch (p->speed) {
287 CASE(2400);
288 CASE(9600);
289 CASE(19200);
290 CASE(38400);
291 CASE(57600);
292 CASE(115200);
293 CASE(576000);
294 CASE(1152000);
295 CASE(4000000);
296 CASE(16000000);
297 default: return (EINVAL);
298 }
299 #undef CONC
300 #undef CASE
301
302 error = sc->sc_methods->im_set_params(sc->sc_handle, p);
303 if (!error) {
304 sc->sc_params = *p;
305 DPRINTF(("irf_set_params: ok\n"));
306 #ifdef DIAGNOSTIC
307 if (p->speed != sc->sc_speed) {
308 sc->sc_speed = p->speed;
309 aprint_verbose_dev(sc->sc_dev, "set speed %u\n",
310 sc->sc_speed);
311 }
312 #endif
313 } else {
314 #ifdef IRFRAME_DEBUG
315 printf("irf_set_params: error=%d\n", error);
316 #endif
317 }
318 return (error);
319 }
320
321 int
322 irf_reset_params(struct irframe_softc *sc)
323 {
324 struct irda_params params;
325
326 params.speed = IRDA_DEFAULT_SPEED;
327 params.ebofs = IRDA_DEFAULT_EBOFS;
328 params.maxsize = IRDA_DEFAULT_SIZE;
329 return (irf_set_params(sc, ¶ms));
330 }
331
332 int
333 irframeioctl(dev_t dev, u_long cmd, void *addr, int flag,
334 struct lwp *l)
335 {
336 struct irframe_softc *sc;
337 void *vaddr = addr;
338 int error;
339
340 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
341 if (sc == NULL)
342 return (ENXIO);
343 if (!device_is_active(sc->sc_dev) || !sc->sc_open)
344 return (EIO);
345
346 switch (cmd) {
347 case FIONBIO:
348 /* All handled in the upper FS layer. */
349 error = 0;
350 break;
351
352 case IRDA_SET_PARAMS:
353 error = irf_set_params(sc, vaddr);
354 break;
355
356 case IRDA_RESET_PARAMS:
357 error = irf_reset_params(sc);
358 break;
359
360 case IRDA_GET_SPEEDMASK:
361 error = sc->sc_methods->im_get_speeds(sc->sc_handle, vaddr);
362 break;
363
364 case IRDA_GET_TURNAROUNDMASK:
365 error = sc->sc_methods->im_get_turnarounds(sc->sc_handle,vaddr);
366 break;
367
368 default:
369 error = EINVAL;
370 break;
371 }
372 return (error);
373 }
374
375 int
376 irframepoll(dev_t dev, int events, struct lwp *l)
377 {
378 struct irframe_softc *sc;
379
380 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
381 if (sc == NULL)
382 return (POLLHUP);
383 if (!device_is_active(sc->sc_dev) || !sc->sc_open)
384 return (POLLHUP);
385
386 return (sc->sc_methods->im_poll(sc->sc_handle, events, l));
387 }
388
389 int
390 irframekqfilter(dev_t dev, struct knote *kn)
391 {
392 struct irframe_softc *sc;
393
394 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
395 if (!device_is_active(sc->sc_dev) || !sc->sc_open)
396 return (1);
397
398 return (sc->sc_methods->im_kqfilter(sc->sc_handle, kn));
399 }
400