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