Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / ir / irframe.c
blob223f925f57e491b570a1abcdb4bb2da48bd26da5
1 /* $NetBSD: irframe.c,v 1.43 2009/01/11 14:21:48 mlelstv Exp $ */
3 /*
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net).
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: irframe.c,v 1.43 2009/01/11 14:21:48 mlelstv Exp $");
35 #include "irframe.h"
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>
48 #include <dev/ir/ir.h>
49 #include <dev/ir/irdaio.h>
50 #include <dev/ir/irframevar.h>
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
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);
69 const struct cdevsw irframe_cdevsw = {
70 irframeopen, irframeclose, irframeread, irframewrite, irframeioctl,
71 nostop, notty, irframepoll, nommap, irframekqfilter, D_OTHER,
74 int irframe_match(device_t parent, cfdata_t match, void *aux);
76 Static int irf_set_params(struct irframe_softc *sc, struct irda_params *p);
77 Static int irf_reset_params(struct irframe_softc *sc);
79 #if NIRFRAME == 0
80 /* In case we just have tty attachment. */
81 CFDRIVER_DECL(irframe, DV_DULL, NULL);
82 #endif
84 CFATTACH_DECL_NEW(irframe, sizeof(struct irframe_softc),
85 irframe_match, irframe_attach, irframe_detach, NULL);
87 extern struct cfdriver irframe_cd;
89 #define IRFRAMEUNIT(dev) (minor(dev))
91 int
92 irframe_match(device_t parent, cfdata_t match, void *aux)
94 struct ir_attach_args *ia = aux;
96 return (ia->ia_type == IR_TYPE_IRFRAME);
99 void
100 irframe_attach(device_t parent, device_t self, void *aux)
102 struct irframe_softc *sc = device_private(self);
103 struct ir_attach_args *ia = aux;
104 const char *delim;
105 int speeds = 0;
107 sc->sc_dev = self;
108 sc->sc_methods = ia->ia_methods;
109 sc->sc_handle = ia->ia_handle;
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_kqfilter == NULL ||
116 sc->sc_methods->im_set_params == NULL ||
117 sc->sc_methods->im_get_speeds == NULL ||
118 sc->sc_methods->im_get_turnarounds == NULL)
119 panic("%s: missing methods", device_xname(self));
120 #endif
122 (void)sc->sc_methods->im_get_speeds(sc->sc_handle, &speeds);
123 sc->sc_speedmask = speeds;
124 delim = ":";
125 if (speeds & IRDA_SPEEDS_SIR) {
126 printf("%s SIR", delim);
127 delim = ",";
129 if (speeds & IRDA_SPEEDS_MIR) {
130 printf("%s MIR", delim);
131 delim = ",";
133 if (speeds & IRDA_SPEEDS_FIR) {
134 printf("%s FIR", delim);
135 delim = ",";
137 if (speeds & IRDA_SPEEDS_VFIR) {
138 printf("%s VFIR", delim);
139 delim = ",";
141 printf("\n");
143 if (!pmf_device_register(self, NULL, NULL))
144 aprint_error_dev(self, "couldn't establish power handler\n");
148 irframe_detach(device_t self, int flags)
150 /*struct irframe_softc *sc = device_private(self);*/
151 int maj, mn;
153 pmf_device_deregister(self);
155 /* XXX needs reference count */
157 /* locate the major number */
158 maj = cdevsw_lookup_major(&irframe_cdevsw);
160 /* Nuke the vnodes for any open instances (calls close). */
161 mn = device_unit(self);
162 vdevgone(maj, mn, mn, VCHR);
164 return (0);
168 irframeopen(dev_t dev, int flag, int mode, struct lwp *l)
170 struct irframe_softc *sc;
171 int error;
173 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
174 if (sc == NULL)
175 return (ENXIO);
176 if (!device_is_active(sc->sc_dev))
177 return (EIO);
178 if (sc->sc_open)
179 return (EBUSY);
180 if (sc->sc_methods->im_open != NULL) {
181 error = sc->sc_methods->im_open(sc->sc_handle, flag, mode, l);
182 if (error)
183 return (error);
185 sc->sc_open = 1;
186 #ifdef DIAGNOSTIC
187 sc->sc_speed = IRDA_DEFAULT_SPEED;
188 #endif
189 (void)irf_reset_params(sc);
190 return (0);
194 irframeclose(dev_t dev, int flag, int mode, struct lwp *l)
196 struct irframe_softc *sc;
197 int error;
199 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
200 if (sc == NULL)
201 return (ENXIO);
202 sc->sc_open = 0;
203 if (sc->sc_methods->im_close != NULL)
204 error = sc->sc_methods->im_close(sc->sc_handle, flag, mode, l);
205 else
206 error = 0;
207 return (error);
211 irframeread(dev_t dev, struct uio *uio, int flag)
213 struct irframe_softc *sc;
215 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
216 if (sc == NULL)
217 return (ENXIO);
218 if (!device_is_active(sc->sc_dev) || !sc->sc_open)
219 return (EIO);
220 if (uio->uio_resid < sc->sc_params.maxsize) {
221 #ifdef DIAGNOSTIC
222 printf("irframeread: short read %ld < %d\n",
223 (long)uio->uio_resid, sc->sc_params.maxsize);
224 #endif
225 return (EINVAL);
227 return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
231 irframewrite(dev_t dev, struct uio *uio, int flag)
233 struct irframe_softc *sc;
235 sc = device_lookup_private(&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: long write %ld > %d\n",
243 (long)uio->uio_resid, sc->sc_params.maxsize);
244 #endif
245 return (EINVAL);
247 return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
251 irf_set_params(struct irframe_softc *sc, struct irda_params *p)
253 int error;
255 DPRINTF(("irf_set_params: set params speed=%u ebofs=%u maxsize=%u "
256 "speedmask=0x%x\n", p->speed, p->ebofs, p->maxsize,
257 sc->sc_speedmask));
259 if (p->maxsize > IRDA_MAX_FRAME_SIZE) {
260 #ifdef IRFRAME_DEBUG
261 printf("irf_set_params: bad maxsize=%u\n", p->maxsize);
262 #endif
263 return (EINVAL);
266 if (p->ebofs > IRDA_MAX_EBOFS) {
267 #ifdef IRFRAME_DEBUG
268 printf("irf_set_params: bad maxsize=%u\n", p->maxsize);
269 #endif
270 return (EINVAL);
273 #define CONC(x,y) x##y
274 #define CASE(s) case s: if (!(sc->sc_speedmask & CONC(IRDA_SPEED_,s))) return (EINVAL); break
275 switch (p->speed) {
276 CASE(2400);
277 CASE(9600);
278 CASE(19200);
279 CASE(38400);
280 CASE(57600);
281 CASE(115200);
282 CASE(576000);
283 CASE(1152000);
284 CASE(4000000);
285 CASE(16000000);
286 default: return (EINVAL);
288 #undef CONC
289 #undef CASE
291 error = sc->sc_methods->im_set_params(sc->sc_handle, p);
292 if (!error) {
293 sc->sc_params = *p;
294 DPRINTF(("irf_set_params: ok\n"));
295 #ifdef DIAGNOSTIC
296 if (p->speed != sc->sc_speed) {
297 sc->sc_speed = p->speed;
298 aprint_verbose_dev(sc->sc_dev, "set speed %u\n",
299 sc->sc_speed);
301 #endif
302 } else {
303 #ifdef IRFRAME_DEBUG
304 printf("irf_set_params: error=%d\n", error);
305 #endif
307 return (error);
311 irf_reset_params(struct irframe_softc *sc)
313 struct irda_params params;
315 params.speed = IRDA_DEFAULT_SPEED;
316 params.ebofs = IRDA_DEFAULT_EBOFS;
317 params.maxsize = IRDA_DEFAULT_SIZE;
318 return (irf_set_params(sc, &params));
322 irframeioctl(dev_t dev, u_long cmd, void *addr, int flag,
323 struct lwp *l)
325 struct irframe_softc *sc;
326 void *vaddr = addr;
327 int error;
329 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
330 if (sc == NULL)
331 return (ENXIO);
332 if (!device_is_active(sc->sc_dev) || !sc->sc_open)
333 return (EIO);
335 switch (cmd) {
336 case FIONBIO:
337 /* All handled in the upper FS layer. */
338 error = 0;
339 break;
341 case IRDA_SET_PARAMS:
342 error = irf_set_params(sc, vaddr);
343 break;
345 case IRDA_RESET_PARAMS:
346 error = irf_reset_params(sc);
347 break;
349 case IRDA_GET_SPEEDMASK:
350 error = sc->sc_methods->im_get_speeds(sc->sc_handle, vaddr);
351 break;
353 case IRDA_GET_TURNAROUNDMASK:
354 error = sc->sc_methods->im_get_turnarounds(sc->sc_handle,vaddr);
355 break;
357 default:
358 error = EINVAL;
359 break;
361 return (error);
365 irframepoll(dev_t dev, int events, struct lwp *l)
367 struct irframe_softc *sc;
369 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
370 if (sc == NULL)
371 return (POLLHUP);
372 if (!device_is_active(sc->sc_dev) || !sc->sc_open)
373 return (POLLHUP);
375 return (sc->sc_methods->im_poll(sc->sc_handle, events, l));
379 irframekqfilter(dev_t dev, struct knote *kn)
381 struct irframe_softc *sc;
383 sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
384 if (!device_is_active(sc->sc_dev) || !sc->sc_open)
385 return (1);
387 return (sc->sc_methods->im_kqfilter(sc->sc_handle, kn));