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