xref: /netbsd-src/sys/dev/ir/irframe.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: irframe.c,v 1.43 2009/01/11 14:21:48 mlelstv 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  *
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.43 2009/01/11 14:21:48 mlelstv 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 	if (!pmf_device_register(self, NULL, NULL))
145 		aprint_error_dev(self, "couldn't establish power handler\n");
146 }
147 
148 int
149 irframe_activate(device_t self, enum devact act)
150 {
151 	/*struct irframe_softc *sc = device_private(self);*/
152 
153 	switch (act) {
154 	case DVACT_ACTIVATE:
155 		return (EOPNOTSUPP);
156 
157 	case DVACT_DEACTIVATE:
158 		break;
159 	}
160 	return (0);
161 }
162 
163 int
164 irframe_detach(device_t self, int flags)
165 {
166 	/*struct irframe_softc *sc = device_private(self);*/
167 	int maj, mn;
168 
169 	pmf_device_deregister(self);
170 
171 	/* XXX needs reference count */
172 
173 	/* locate the major number */
174 	maj = cdevsw_lookup_major(&irframe_cdevsw);
175 
176 	/* Nuke the vnodes for any open instances (calls close). */
177 	mn = device_unit(self);
178 	vdevgone(maj, mn, mn, VCHR);
179 
180 	return (0);
181 }
182 
183 int
184 irframeopen(dev_t dev, int flag, int mode, struct lwp *l)
185 {
186 	struct irframe_softc *sc;
187 	int error;
188 
189 	sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
190 	if (sc == NULL)
191 		return (ENXIO);
192 	if (!device_is_active(sc->sc_dev))
193 		return (EIO);
194 	if (sc->sc_open)
195 		return (EBUSY);
196 	if (sc->sc_methods->im_open != NULL) {
197 		error = sc->sc_methods->im_open(sc->sc_handle, flag, mode, l);
198 		if (error)
199 			return (error);
200 	}
201 	sc->sc_open = 1;
202 #ifdef DIAGNOSTIC
203 	sc->sc_speed = IRDA_DEFAULT_SPEED;
204 #endif
205 	(void)irf_reset_params(sc);
206 	return (0);
207 }
208 
209 int
210 irframeclose(dev_t dev, int flag, int mode, struct lwp *l)
211 {
212 	struct irframe_softc *sc;
213 	int error;
214 
215 	sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
216 	if (sc == NULL)
217 		return (ENXIO);
218 	sc->sc_open = 0;
219 	if (sc->sc_methods->im_close != NULL)
220 		error = sc->sc_methods->im_close(sc->sc_handle, flag, mode, l);
221 	else
222 		error = 0;
223 	return (error);
224 }
225 
226 int
227 irframeread(dev_t dev, struct uio *uio, int flag)
228 {
229 	struct irframe_softc *sc;
230 
231 	sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
232 	if (sc == NULL)
233 		return (ENXIO);
234 	if (!device_is_active(sc->sc_dev) || !sc->sc_open)
235 		return (EIO);
236 	if (uio->uio_resid < sc->sc_params.maxsize) {
237 #ifdef DIAGNOSTIC
238 		printf("irframeread: short read %ld < %d\n",
239 		       (long)uio->uio_resid, sc->sc_params.maxsize);
240 #endif
241 		return (EINVAL);
242 	}
243 	return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
244 }
245 
246 int
247 irframewrite(dev_t dev, struct uio *uio, int flag)
248 {
249 	struct irframe_softc *sc;
250 
251 	sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
252 	if (sc == NULL)
253 		return (ENXIO);
254 	if (!device_is_active(sc->sc_dev) || !sc->sc_open)
255 		return (EIO);
256 	if (uio->uio_resid > sc->sc_params.maxsize) {
257 #ifdef DIAGNOSTIC
258 		printf("irframeread: long write %ld > %d\n",
259 		       (long)uio->uio_resid, sc->sc_params.maxsize);
260 #endif
261 		return (EINVAL);
262 	}
263 	return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
264 }
265 
266 int
267 irf_set_params(struct irframe_softc *sc, struct irda_params *p)
268 {
269 	int error;
270 
271 	DPRINTF(("irf_set_params: set params speed=%u ebofs=%u maxsize=%u "
272 		 "speedmask=0x%x\n", p->speed, p->ebofs, p->maxsize,
273 		 sc->sc_speedmask));
274 
275 	if (p->maxsize > IRDA_MAX_FRAME_SIZE) {
276 #ifdef IRFRAME_DEBUG
277 		printf("irf_set_params: bad maxsize=%u\n", p->maxsize);
278 #endif
279 		return (EINVAL);
280 	}
281 
282 	if (p->ebofs > IRDA_MAX_EBOFS) {
283 #ifdef IRFRAME_DEBUG
284 		printf("irf_set_params: bad maxsize=%u\n", p->maxsize);
285 #endif
286 		return (EINVAL);
287 	}
288 
289 #define CONC(x,y) x##y
290 #define CASE(s) case s: if (!(sc->sc_speedmask & CONC(IRDA_SPEED_,s))) return (EINVAL); break
291 	switch (p->speed) {
292 	CASE(2400);
293 	CASE(9600);
294 	CASE(19200);
295 	CASE(38400);
296 	CASE(57600);
297 	CASE(115200);
298 	CASE(576000);
299 	CASE(1152000);
300 	CASE(4000000);
301 	CASE(16000000);
302 	default: return (EINVAL);
303 	}
304 #undef CONC
305 #undef CASE
306 
307 	error = sc->sc_methods->im_set_params(sc->sc_handle, p);
308 	if (!error) {
309 		sc->sc_params = *p;
310 		DPRINTF(("irf_set_params: ok\n"));
311 #ifdef DIAGNOSTIC
312 		if (p->speed != sc->sc_speed) {
313 			sc->sc_speed = p->speed;
314 			aprint_verbose_dev(sc->sc_dev, "set speed %u\n",
315 			       sc->sc_speed);
316 		}
317 #endif
318 	} else {
319 #ifdef IRFRAME_DEBUG
320 		printf("irf_set_params: error=%d\n", error);
321 #endif
322 	}
323 	return (error);
324 }
325 
326 int
327 irf_reset_params(struct irframe_softc *sc)
328 {
329 	struct irda_params params;
330 
331 	params.speed = IRDA_DEFAULT_SPEED;
332 	params.ebofs = IRDA_DEFAULT_EBOFS;
333 	params.maxsize = IRDA_DEFAULT_SIZE;
334 	return (irf_set_params(sc, &params));
335 }
336 
337 int
338 irframeioctl(dev_t dev, u_long cmd, void *addr, int flag,
339     struct lwp *l)
340 {
341 	struct irframe_softc *sc;
342 	void *vaddr = addr;
343 	int error;
344 
345 	sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
346 	if (sc == NULL)
347 		return (ENXIO);
348 	if (!device_is_active(sc->sc_dev) || !sc->sc_open)
349 		return (EIO);
350 
351 	switch (cmd) {
352 	case FIONBIO:
353 		/* All handled in the upper FS layer. */
354 		error = 0;
355 		break;
356 
357 	case IRDA_SET_PARAMS:
358 		error = irf_set_params(sc, vaddr);
359 		break;
360 
361 	case IRDA_RESET_PARAMS:
362 		error = irf_reset_params(sc);
363 		break;
364 
365 	case IRDA_GET_SPEEDMASK:
366 		error = sc->sc_methods->im_get_speeds(sc->sc_handle, vaddr);
367 		break;
368 
369 	case IRDA_GET_TURNAROUNDMASK:
370 		error = sc->sc_methods->im_get_turnarounds(sc->sc_handle,vaddr);
371 		break;
372 
373 	default:
374 		error = EINVAL;
375 		break;
376 	}
377 	return (error);
378 }
379 
380 int
381 irframepoll(dev_t dev, int events, struct lwp *l)
382 {
383 	struct irframe_softc *sc;
384 
385 	sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
386 	if (sc == NULL)
387 		return (POLLHUP);
388 	if (!device_is_active(sc->sc_dev) || !sc->sc_open)
389 		return (POLLHUP);
390 
391 	return (sc->sc_methods->im_poll(sc->sc_handle, events, l));
392 }
393 
394 int
395 irframekqfilter(dev_t dev, struct knote *kn)
396 {
397 	struct irframe_softc *sc;
398 
399 	sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
400 	if (!device_is_active(sc->sc_dev) || !sc->sc_open)
401 		return (1);
402 
403 	return (sc->sc_methods->im_kqfilter(sc->sc_handle, kn));
404 }
405