irframe.c revision 1.17 1 1.17 gehenna /* $NetBSD: irframe.c,v 1.17 2002/09/06 13:18:43 gehenna Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.1 augustss * by Lennart Augustsson (lennart (at) augustsson.net).
9 1.1 augustss *
10 1.1 augustss * Redistribution and use in source and binary forms, with or without
11 1.1 augustss * modification, are permitted provided that the following conditions
12 1.1 augustss * are met:
13 1.1 augustss * 1. Redistributions of source code must retain the above copyright
14 1.1 augustss * notice, this list of conditions and the following disclaimer.
15 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer in the
17 1.1 augustss * documentation and/or other materials provided with the distribution.
18 1.1 augustss * 3. All advertising materials mentioning features or use of this software
19 1.1 augustss * must display the following acknowledgement:
20 1.1 augustss * This product includes software developed by the NetBSD
21 1.1 augustss * Foundation, Inc. and its contributors.
22 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 augustss * contributors may be used to endorse or promote products derived
24 1.1 augustss * from this software without specific prior written permission.
25 1.1 augustss *
26 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
37 1.1 augustss */
38 1.1 augustss
39 1.4 augustss #include "irframe.h"
40 1.4 augustss
41 1.1 augustss #include <sys/param.h>
42 1.1 augustss #include <sys/systm.h>
43 1.1 augustss #include <sys/ioctl.h>
44 1.1 augustss #include <sys/kernel.h>
45 1.1 augustss #include <sys/device.h>
46 1.1 augustss #include <sys/conf.h>
47 1.1 augustss #include <sys/malloc.h>
48 1.1 augustss #include <sys/poll.h>
49 1.1 augustss #include <sys/select.h>
50 1.1 augustss #include <sys/vnode.h>
51 1.1 augustss
52 1.1 augustss #include <dev/ir/ir.h>
53 1.1 augustss #include <dev/ir/irdaio.h>
54 1.1 augustss #include <dev/ir/irframevar.h>
55 1.1 augustss
56 1.12 augustss #ifdef IRFRAME_DEBUG
57 1.12 augustss #define DPRINTF(x) if (irframedebug) printf x
58 1.12 augustss #define Static
59 1.12 augustss int irframedebug = 0;
60 1.12 augustss #else
61 1.12 augustss #define DPRINTF(x)
62 1.12 augustss #define Static static
63 1.12 augustss #endif
64 1.12 augustss
65 1.17 gehenna dev_type_open(irframeopen);
66 1.17 gehenna dev_type_close(irframeclose);
67 1.17 gehenna dev_type_read(irframeread);
68 1.17 gehenna dev_type_write(irframewrite);
69 1.17 gehenna dev_type_ioctl(irframeioctl);
70 1.17 gehenna dev_type_poll(irframepoll);
71 1.17 gehenna
72 1.17 gehenna const struct cdevsw irframe_cdevsw = {
73 1.17 gehenna irframeopen, irframeclose, irframeread, irframewrite, irframeioctl,
74 1.17 gehenna nostop, notty, irframepoll, nommap,
75 1.17 gehenna };
76 1.1 augustss
77 1.1 augustss int irframe_match(struct device *parent, struct cfdata *match, void *aux);
78 1.1 augustss void irframe_attach(struct device *parent, struct device *self, void *aux);
79 1.1 augustss int irframe_activate(struct device *self, enum devact act);
80 1.1 augustss int irframe_detach(struct device *self, int flags);
81 1.1 augustss
82 1.13 augustss Static int irf_set_params(struct irframe_softc *sc, struct irda_params *p);
83 1.12 augustss Static int irf_reset_params(struct irframe_softc *sc);
84 1.6 augustss
85 1.4 augustss #if NIRFRAME == 0
86 1.4 augustss /* In case we just have tty attachment. */
87 1.4 augustss struct cfdriver irframe_cd = {
88 1.4 augustss NULL, "irframe", DV_DULL
89 1.4 augustss };
90 1.4 augustss #endif
91 1.4 augustss
92 1.1 augustss struct cfattach irframe_ca = {
93 1.1 augustss sizeof(struct irframe_softc), irframe_match, irframe_attach,
94 1.1 augustss irframe_detach, irframe_activate
95 1.1 augustss };
96 1.1 augustss
97 1.1 augustss extern struct cfattach irframe_ca;
98 1.1 augustss extern struct cfdriver irframe_cd;
99 1.1 augustss
100 1.1 augustss #define IRFRAMEUNIT(dev) (minor(dev))
101 1.1 augustss
102 1.1 augustss int
103 1.1 augustss irframe_match(struct device *parent, struct cfdata *match, void *aux)
104 1.1 augustss {
105 1.1 augustss struct ir_attach_args *ia = aux;
106 1.1 augustss
107 1.1 augustss return (ia->ia_type == IR_TYPE_IRFRAME);
108 1.1 augustss }
109 1.1 augustss
110 1.1 augustss void
111 1.1 augustss irframe_attach(struct device *parent, struct device *self, void *aux)
112 1.1 augustss {
113 1.1 augustss struct irframe_softc *sc = (struct irframe_softc *)self;
114 1.1 augustss struct ir_attach_args *ia = aux;
115 1.1 augustss const char *delim;
116 1.1 augustss int speeds = 0;
117 1.1 augustss
118 1.1 augustss sc->sc_methods = ia->ia_methods;
119 1.1 augustss sc->sc_handle = ia->ia_handle;
120 1.1 augustss
121 1.1 augustss #ifdef DIAGNOSTIC
122 1.1 augustss if (sc->sc_methods->im_read == NULL ||
123 1.1 augustss sc->sc_methods->im_write == NULL ||
124 1.4 augustss sc->sc_methods->im_poll == NULL ||
125 1.1 augustss sc->sc_methods->im_set_params == NULL ||
126 1.1 augustss sc->sc_methods->im_get_speeds == NULL ||
127 1.1 augustss sc->sc_methods->im_get_turnarounds == NULL)
128 1.1 augustss panic("%s: missing methods\n", sc->sc_dev.dv_xname);
129 1.1 augustss #endif
130 1.1 augustss
131 1.1 augustss (void)sc->sc_methods->im_get_speeds(sc->sc_handle, &speeds);
132 1.11 augustss sc->sc_speedmask = speeds;
133 1.1 augustss delim = ":";
134 1.1 augustss if (speeds & IRDA_SPEEDS_SIR) {
135 1.1 augustss printf("%s SIR", delim);
136 1.1 augustss delim = ",";
137 1.1 augustss }
138 1.1 augustss if (speeds & IRDA_SPEEDS_MIR) {
139 1.1 augustss printf("%s MIR", delim);
140 1.1 augustss delim = ",";
141 1.1 augustss }
142 1.1 augustss if (speeds & IRDA_SPEEDS_FIR) {
143 1.1 augustss printf("%s FIR", delim);
144 1.1 augustss delim = ",";
145 1.1 augustss }
146 1.1 augustss if (speeds & IRDA_SPEEDS_VFIR) {
147 1.1 augustss printf("%s VFIR", delim);
148 1.1 augustss delim = ",";
149 1.1 augustss }
150 1.1 augustss printf("\n");
151 1.1 augustss }
152 1.1 augustss
153 1.1 augustss int
154 1.1 augustss irframe_activate(struct device *self, enum devact act)
155 1.1 augustss {
156 1.1 augustss /*struct irframe_softc *sc = (struct irframe_softc *)self;*/
157 1.1 augustss
158 1.1 augustss switch (act) {
159 1.1 augustss case DVACT_ACTIVATE:
160 1.1 augustss return (EOPNOTSUPP);
161 1.1 augustss break;
162 1.1 augustss
163 1.1 augustss case DVACT_DEACTIVATE:
164 1.1 augustss break;
165 1.1 augustss }
166 1.1 augustss return (0);
167 1.1 augustss }
168 1.1 augustss
169 1.1 augustss int
170 1.1 augustss irframe_detach(struct device *self, int flags)
171 1.1 augustss {
172 1.1 augustss /*struct irframe_softc *sc = (struct irframe_softc *)self;*/
173 1.1 augustss int maj, mn;
174 1.15 augustss
175 1.15 augustss /* XXX needs reference count */
176 1.1 augustss
177 1.1 augustss /* locate the major number */
178 1.17 gehenna maj = cdevsw_lookup_major(&irframe_cdevsw);
179 1.1 augustss
180 1.1 augustss /* Nuke the vnodes for any open instances (calls close). */
181 1.1 augustss mn = self->dv_unit;
182 1.1 augustss vdevgone(maj, mn, mn, VCHR);
183 1.1 augustss
184 1.1 augustss return (0);
185 1.1 augustss }
186 1.1 augustss
187 1.1 augustss int
188 1.1 augustss irframeopen(dev_t dev, int flag, int mode, struct proc *p)
189 1.1 augustss {
190 1.1 augustss struct irframe_softc *sc;
191 1.1 augustss int error;
192 1.1 augustss
193 1.1 augustss sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
194 1.1 augustss if (sc == NULL)
195 1.1 augustss return (ENXIO);
196 1.1 augustss if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
197 1.1 augustss return (EIO);
198 1.1 augustss if (sc->sc_open)
199 1.1 augustss return (EBUSY);
200 1.1 augustss if (sc->sc_methods->im_open != NULL) {
201 1.1 augustss error = sc->sc_methods->im_open(sc->sc_handle, flag, mode, p);
202 1.1 augustss if (error)
203 1.1 augustss return (error);
204 1.1 augustss }
205 1.1 augustss sc->sc_open = 1;
206 1.11 augustss #ifdef DIAGNOSTIC
207 1.11 augustss sc->sc_speed = IRDA_DEFAULT_SPEED;
208 1.11 augustss #endif
209 1.8 augustss (void)irf_reset_params(sc);
210 1.1 augustss return (0);
211 1.1 augustss }
212 1.1 augustss
213 1.1 augustss int
214 1.1 augustss irframeclose(dev_t dev, int flag, int mode, struct proc *p)
215 1.1 augustss {
216 1.1 augustss struct irframe_softc *sc;
217 1.1 augustss int error;
218 1.1 augustss
219 1.1 augustss sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
220 1.1 augustss if (sc == NULL)
221 1.1 augustss return (ENXIO);
222 1.14 augustss sc->sc_open = 0;
223 1.1 augustss if (sc->sc_methods->im_close != NULL)
224 1.1 augustss error = sc->sc_methods->im_close(sc->sc_handle, flag, mode, p);
225 1.1 augustss else
226 1.1 augustss error = 0;
227 1.1 augustss return (error);
228 1.1 augustss }
229 1.1 augustss
230 1.1 augustss int
231 1.1 augustss irframeread(dev_t dev, struct uio *uio, int flag)
232 1.1 augustss {
233 1.1 augustss struct irframe_softc *sc;
234 1.1 augustss
235 1.1 augustss sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
236 1.1 augustss if (sc == NULL)
237 1.1 augustss return (ENXIO);
238 1.14 augustss if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || !sc->sc_open)
239 1.1 augustss return (EIO);
240 1.12 augustss if (uio->uio_resid < sc->sc_params.maxsize) {
241 1.12 augustss #ifdef DIAGNOSTIC
242 1.12 augustss printf("irframeread: short read %d < %d\n", uio->uio_resid,
243 1.12 augustss sc->sc_params.maxsize);
244 1.12 augustss #endif
245 1.11 augustss return (EINVAL);
246 1.12 augustss }
247 1.1 augustss return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
248 1.1 augustss }
249 1.1 augustss
250 1.1 augustss int
251 1.1 augustss irframewrite(dev_t dev, struct uio *uio, int flag)
252 1.1 augustss {
253 1.1 augustss struct irframe_softc *sc;
254 1.1 augustss
255 1.1 augustss sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
256 1.1 augustss if (sc == NULL)
257 1.1 augustss return (ENXIO);
258 1.14 augustss if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || !sc->sc_open)
259 1.1 augustss return (EIO);
260 1.12 augustss if (uio->uio_resid > sc->sc_params.maxsize) {
261 1.12 augustss #ifdef DIAGNOSTIC
262 1.12 augustss printf("irframeread: long write %d > %d\n", uio->uio_resid,
263 1.12 augustss sc->sc_params.maxsize);
264 1.12 augustss #endif
265 1.11 augustss return (EINVAL);
266 1.12 augustss }
267 1.2 augustss return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
268 1.1 augustss }
269 1.1 augustss
270 1.1 augustss int
271 1.13 augustss irf_set_params(struct irframe_softc *sc, struct irda_params *p)
272 1.13 augustss {
273 1.13 augustss int error;
274 1.13 augustss
275 1.13 augustss DPRINTF(("irf_set_params: set params speed=%u ebofs=%u maxsize=%u "
276 1.13 augustss "speedmask=0x%x\n", p->speed, p->ebofs, p->maxsize,
277 1.13 augustss sc->sc_speedmask));
278 1.13 augustss
279 1.13 augustss if (p->maxsize > IRDA_MAX_FRAME_SIZE) {
280 1.13 augustss #ifdef IRFRAME_DEBUG
281 1.13 augustss printf("irf_set_params: bad maxsize=%u\n", p->maxsize);
282 1.13 augustss #endif
283 1.13 augustss return (EINVAL);
284 1.13 augustss }
285 1.13 augustss
286 1.13 augustss if (p->ebofs > IRDA_MAX_EBOFS) {
287 1.13 augustss #ifdef IRFRAME_DEBUG
288 1.13 augustss printf("irf_set_params: bad maxsize=%u\n", p->maxsize);
289 1.13 augustss #endif
290 1.13 augustss return (EINVAL);
291 1.13 augustss }
292 1.13 augustss
293 1.13 augustss #define CONC(x,y) x##y
294 1.13 augustss #define CASE(s) case s: if (!(sc->sc_speedmask & CONC(IRDA_SPEED_,s))) return (EINVAL); break
295 1.13 augustss switch (p->speed) {
296 1.13 augustss CASE(2400);
297 1.13 augustss CASE(9600);
298 1.13 augustss CASE(19200);
299 1.13 augustss CASE(38400);
300 1.13 augustss CASE(57600);
301 1.13 augustss CASE(115200);
302 1.13 augustss CASE(576000);
303 1.13 augustss CASE(1152000);
304 1.13 augustss CASE(4000000);
305 1.13 augustss CASE(16000000);
306 1.13 augustss default: return (EINVAL);
307 1.13 augustss }
308 1.13 augustss #undef CONC
309 1.13 augustss #undef CASE
310 1.13 augustss
311 1.13 augustss error = sc->sc_methods->im_set_params(sc->sc_handle, p);
312 1.13 augustss if (!error) {
313 1.13 augustss sc->sc_params = *p;
314 1.13 augustss DPRINTF(("irf_set_params: ok\n"));
315 1.13 augustss #ifdef DIAGNOSTIC
316 1.13 augustss if (p->speed != sc->sc_speed) {
317 1.13 augustss sc->sc_speed = p->speed;
318 1.13 augustss printf("%s: set speed %u\n", sc->sc_dev.dv_xname,
319 1.13 augustss sc->sc_speed);
320 1.13 augustss }
321 1.13 augustss #endif
322 1.13 augustss } else {
323 1.13 augustss #ifdef IRFRAME_DEBUG
324 1.13 augustss printf("irf_set_params: error=%d\n", error);
325 1.13 augustss #endif
326 1.13 augustss }
327 1.13 augustss return (error);
328 1.13 augustss }
329 1.13 augustss
330 1.13 augustss int
331 1.6 augustss irf_reset_params(struct irframe_softc *sc)
332 1.6 augustss {
333 1.6 augustss struct irda_params params;
334 1.6 augustss
335 1.6 augustss params.speed = IRDA_DEFAULT_SPEED;
336 1.6 augustss params.ebofs = IRDA_DEFAULT_EBOFS;
337 1.6 augustss params.maxsize = IRDA_DEFAULT_SIZE;
338 1.13 augustss return (irf_set_params(sc, ¶ms));
339 1.6 augustss }
340 1.6 augustss
341 1.6 augustss int
342 1.1 augustss irframeioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
343 1.1 augustss {
344 1.1 augustss struct irframe_softc *sc;
345 1.2 augustss void *vaddr = addr;
346 1.1 augustss int error;
347 1.1 augustss
348 1.1 augustss sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
349 1.1 augustss if (sc == NULL)
350 1.1 augustss return (ENXIO);
351 1.14 augustss if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || !sc->sc_open)
352 1.1 augustss return (EIO);
353 1.1 augustss
354 1.1 augustss switch (cmd) {
355 1.1 augustss case FIONBIO:
356 1.1 augustss /* All handled in the upper FS layer. */
357 1.1 augustss error = 0;
358 1.1 augustss break;
359 1.1 augustss
360 1.1 augustss case IRDA_SET_PARAMS:
361 1.13 augustss error = irf_set_params(sc, vaddr);
362 1.1 augustss break;
363 1.1 augustss
364 1.1 augustss case IRDA_RESET_PARAMS:
365 1.7 augustss error = irf_reset_params(sc);
366 1.1 augustss break;
367 1.1 augustss
368 1.1 augustss case IRDA_GET_SPEEDMASK:
369 1.2 augustss error = sc->sc_methods->im_get_speeds(sc->sc_handle, vaddr);
370 1.1 augustss break;
371 1.1 augustss
372 1.1 augustss case IRDA_GET_TURNAROUNDMASK:
373 1.2 augustss error = sc->sc_methods->im_get_turnarounds(sc->sc_handle,vaddr);
374 1.1 augustss break;
375 1.1 augustss
376 1.1 augustss default:
377 1.1 augustss error = EINVAL;
378 1.1 augustss break;
379 1.1 augustss }
380 1.1 augustss return (error);
381 1.1 augustss }
382 1.1 augustss
383 1.1 augustss int
384 1.1 augustss irframepoll(dev_t dev, int events, struct proc *p)
385 1.1 augustss {
386 1.1 augustss struct irframe_softc *sc;
387 1.1 augustss
388 1.1 augustss sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
389 1.1 augustss if (sc == NULL)
390 1.1 augustss return (ENXIO);
391 1.14 augustss if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0 || !sc->sc_open)
392 1.1 augustss return (EIO);
393 1.1 augustss
394 1.4 augustss return (sc->sc_methods->im_poll(sc->sc_handle, events, p));
395 1.1 augustss }
396 1.1 augustss
397 1.1 augustss /*********/
398 1.1 augustss
399 1.1 augustss
400 1.1 augustss struct device *
401 1.9 augustss irframe_alloc(size_t size, const struct irframe_methods *m, void *h)
402 1.1 augustss {
403 1.1 augustss struct cfdriver *cd = &irframe_cd;
404 1.1 augustss struct device *dev;
405 1.4 augustss struct ir_attach_args ia;
406 1.1 augustss int unit;
407 1.1 augustss
408 1.1 augustss for (unit = 0; unit < cd->cd_ndevs; unit++)
409 1.1 augustss if (cd->cd_devs[unit] == NULL)
410 1.1 augustss break;
411 1.16 tsutsui dev = malloc(size, M_DEVBUF, M_WAITOK|M_ZERO);
412 1.1 augustss snprintf(dev->dv_xname, sizeof dev->dv_xname, "irframe%d", unit);
413 1.1 augustss dev->dv_unit = unit;
414 1.1 augustss dev->dv_flags = DVF_ACTIVE; /* always initially active */
415 1.1 augustss
416 1.1 augustss config_makeroom(unit, cd);
417 1.1 augustss cd->cd_devs[unit] = dev;
418 1.4 augustss
419 1.4 augustss ia.ia_methods = m;
420 1.4 augustss ia.ia_handle = h;
421 1.6 augustss printf("%s", dev->dv_xname);
422 1.4 augustss irframe_attach(NULL, dev, &ia);
423 1.1 augustss
424 1.1 augustss return (dev);
425 1.1 augustss }
426 1.1 augustss
427 1.1 augustss void
428 1.1 augustss irframe_dealloc(struct device *dev)
429 1.1 augustss {
430 1.1 augustss struct cfdriver *cd = &irframe_cd;
431 1.1 augustss int unit;
432 1.1 augustss
433 1.1 augustss for (unit = 0; unit < cd->cd_ndevs; unit++) {
434 1.1 augustss if (cd->cd_devs[unit] == dev) {
435 1.1 augustss cd->cd_devs[unit] = NULL;
436 1.1 augustss free(dev, M_DEVBUF);
437 1.1 augustss return;
438 1.1 augustss }
439 1.1 augustss }
440 1.1 augustss panic("irframe_dealloc: device not found\n");
441 1.1 augustss }
442