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