irframe.c revision 1.46 1 1.46 dholland /* $NetBSD: irframe.c,v 1.46 2014/07/25 08:10:37 dholland Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.1 augustss * by Lennart Augustsson (lennart (at) augustsson.net).
9 1.1 augustss *
10 1.1 augustss * Redistribution and use in source and binary forms, with or without
11 1.1 augustss * modification, are permitted provided that the following conditions
12 1.1 augustss * are met:
13 1.1 augustss * 1. Redistributions of source code must retain the above copyright
14 1.1 augustss * notice, this list of conditions and the following disclaimer.
15 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer in the
17 1.1 augustss * documentation and/or other materials provided with the distribution.
18 1.1 augustss *
19 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
30 1.1 augustss */
31 1.27 lukem
32 1.27 lukem #include <sys/cdefs.h>
33 1.46 dholland __KERNEL_RCSID(0, "$NetBSD: irframe.c,v 1.46 2014/07/25 08:10:37 dholland Exp $");
34 1.1 augustss
35 1.4 augustss #include "irframe.h"
36 1.4 augustss
37 1.1 augustss #include <sys/param.h>
38 1.1 augustss #include <sys/systm.h>
39 1.1 augustss #include <sys/ioctl.h>
40 1.1 augustss #include <sys/kernel.h>
41 1.1 augustss #include <sys/device.h>
42 1.1 augustss #include <sys/conf.h>
43 1.1 augustss #include <sys/malloc.h>
44 1.1 augustss #include <sys/poll.h>
45 1.1 augustss #include <sys/select.h>
46 1.1 augustss #include <sys/vnode.h>
47 1.1 augustss
48 1.1 augustss #include <dev/ir/ir.h>
49 1.1 augustss #include <dev/ir/irdaio.h>
50 1.1 augustss #include <dev/ir/irframevar.h>
51 1.1 augustss
52 1.12 augustss #ifdef IRFRAME_DEBUG
53 1.12 augustss #define DPRINTF(x) if (irframedebug) printf x
54 1.12 augustss #define Static
55 1.12 augustss int irframedebug = 0;
56 1.12 augustss #else
57 1.12 augustss #define DPRINTF(x)
58 1.12 augustss #define Static static
59 1.12 augustss #endif
60 1.12 augustss
61 1.17 gehenna dev_type_open(irframeopen);
62 1.17 gehenna dev_type_close(irframeclose);
63 1.17 gehenna dev_type_read(irframeread);
64 1.17 gehenna dev_type_write(irframewrite);
65 1.17 gehenna dev_type_ioctl(irframeioctl);
66 1.17 gehenna dev_type_poll(irframepoll);
67 1.23 jdolecek dev_type_kqfilter(irframekqfilter);
68 1.17 gehenna
69 1.17 gehenna const struct cdevsw irframe_cdevsw = {
70 1.45 dholland .d_open = irframeopen,
71 1.45 dholland .d_close = irframeclose,
72 1.45 dholland .d_read = irframeread,
73 1.45 dholland .d_write = irframewrite,
74 1.45 dholland .d_ioctl = irframeioctl,
75 1.45 dholland .d_stop = nostop,
76 1.45 dholland .d_tty = notty,
77 1.45 dholland .d_poll = irframepoll,
78 1.45 dholland .d_mmap = nommap,
79 1.45 dholland .d_kqfilter = irframekqfilter,
80 1.46 dholland .d_discard = nodiscard,
81 1.45 dholland .d_flag = D_OTHER
82 1.17 gehenna };
83 1.1 augustss
84 1.40 cube int irframe_match(device_t parent, cfdata_t match, void *aux);
85 1.1 augustss
86 1.13 augustss Static int irf_set_params(struct irframe_softc *sc, struct irda_params *p);
87 1.12 augustss Static int irf_reset_params(struct irframe_softc *sc);
88 1.6 augustss
89 1.4 augustss #if NIRFRAME == 0
90 1.4 augustss /* In case we just have tty attachment. */
91 1.22 thorpej CFDRIVER_DECL(irframe, DV_DULL, NULL);
92 1.4 augustss #endif
93 1.4 augustss
94 1.40 cube CFATTACH_DECL_NEW(irframe, sizeof(struct irframe_softc),
95 1.44 dyoung irframe_match, irframe_attach, irframe_detach, NULL);
96 1.1 augustss
97 1.1 augustss extern struct cfdriver irframe_cd;
98 1.1 augustss
99 1.1 augustss #define IRFRAMEUNIT(dev) (minor(dev))
100 1.1 augustss
101 1.1 augustss int
102 1.40 cube irframe_match(device_t parent, cfdata_t match, void *aux)
103 1.1 augustss {
104 1.1 augustss struct ir_attach_args *ia = aux;
105 1.1 augustss
106 1.1 augustss return (ia->ia_type == IR_TYPE_IRFRAME);
107 1.1 augustss }
108 1.1 augustss
109 1.1 augustss void
110 1.40 cube irframe_attach(device_t parent, device_t self, void *aux)
111 1.1 augustss {
112 1.33 thorpej struct irframe_softc *sc = device_private(self);
113 1.1 augustss struct ir_attach_args *ia = aux;
114 1.1 augustss const char *delim;
115 1.1 augustss int speeds = 0;
116 1.1 augustss
117 1.40 cube sc->sc_dev = self;
118 1.1 augustss sc->sc_methods = ia->ia_methods;
119 1.1 augustss sc->sc_handle = ia->ia_handle;
120 1.1 augustss
121 1.1 augustss #ifdef DIAGNOSTIC
122 1.1 augustss if (sc->sc_methods->im_read == NULL ||
123 1.1 augustss sc->sc_methods->im_write == NULL ||
124 1.4 augustss sc->sc_methods->im_poll == NULL ||
125 1.23 jdolecek sc->sc_methods->im_kqfilter == NULL ||
126 1.1 augustss sc->sc_methods->im_set_params == NULL ||
127 1.1 augustss sc->sc_methods->im_get_speeds == NULL ||
128 1.1 augustss sc->sc_methods->im_get_turnarounds == NULL)
129 1.40 cube panic("%s: missing methods", device_xname(self));
130 1.1 augustss #endif
131 1.1 augustss
132 1.1 augustss (void)sc->sc_methods->im_get_speeds(sc->sc_handle, &speeds);
133 1.11 augustss sc->sc_speedmask = speeds;
134 1.1 augustss delim = ":";
135 1.1 augustss if (speeds & IRDA_SPEEDS_SIR) {
136 1.1 augustss printf("%s SIR", delim);
137 1.1 augustss delim = ",";
138 1.1 augustss }
139 1.1 augustss if (speeds & IRDA_SPEEDS_MIR) {
140 1.1 augustss printf("%s MIR", delim);
141 1.1 augustss delim = ",";
142 1.1 augustss }
143 1.1 augustss if (speeds & IRDA_SPEEDS_FIR) {
144 1.1 augustss printf("%s FIR", delim);
145 1.1 augustss delim = ",";
146 1.1 augustss }
147 1.1 augustss if (speeds & IRDA_SPEEDS_VFIR) {
148 1.1 augustss printf("%s VFIR", delim);
149 1.1 augustss delim = ",";
150 1.1 augustss }
151 1.1 augustss printf("\n");
152 1.43 mlelstv
153 1.43 mlelstv if (!pmf_device_register(self, NULL, NULL))
154 1.43 mlelstv aprint_error_dev(self, "couldn't establish power handler\n");
155 1.1 augustss }
156 1.1 augustss
157 1.1 augustss int
158 1.40 cube irframe_detach(device_t self, int flags)
159 1.1 augustss {
160 1.33 thorpej /*struct irframe_softc *sc = device_private(self);*/
161 1.1 augustss int maj, mn;
162 1.15 augustss
163 1.43 mlelstv pmf_device_deregister(self);
164 1.43 mlelstv
165 1.15 augustss /* XXX needs reference count */
166 1.1 augustss
167 1.1 augustss /* locate the major number */
168 1.17 gehenna maj = cdevsw_lookup_major(&irframe_cdevsw);
169 1.1 augustss
170 1.1 augustss /* Nuke the vnodes for any open instances (calls close). */
171 1.32 thorpej mn = device_unit(self);
172 1.1 augustss vdevgone(maj, mn, mn, VCHR);
173 1.1 augustss
174 1.1 augustss return (0);
175 1.1 augustss }
176 1.1 augustss
177 1.1 augustss int
178 1.30 christos irframeopen(dev_t dev, int flag, int mode, struct lwp *l)
179 1.1 augustss {
180 1.1 augustss struct irframe_softc *sc;
181 1.1 augustss int error;
182 1.1 augustss
183 1.42 cegger sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
184 1.1 augustss if (sc == NULL)
185 1.1 augustss return (ENXIO);
186 1.40 cube if (!device_is_active(sc->sc_dev))
187 1.1 augustss return (EIO);
188 1.1 augustss if (sc->sc_open)
189 1.1 augustss return (EBUSY);
190 1.1 augustss if (sc->sc_methods->im_open != NULL) {
191 1.30 christos error = sc->sc_methods->im_open(sc->sc_handle, flag, mode, l);
192 1.1 augustss if (error)
193 1.1 augustss return (error);
194 1.1 augustss }
195 1.1 augustss sc->sc_open = 1;
196 1.11 augustss #ifdef DIAGNOSTIC
197 1.11 augustss sc->sc_speed = IRDA_DEFAULT_SPEED;
198 1.11 augustss #endif
199 1.8 augustss (void)irf_reset_params(sc);
200 1.1 augustss return (0);
201 1.1 augustss }
202 1.1 augustss
203 1.1 augustss int
204 1.30 christos irframeclose(dev_t dev, int flag, int mode, struct lwp *l)
205 1.1 augustss {
206 1.1 augustss struct irframe_softc *sc;
207 1.1 augustss int error;
208 1.1 augustss
209 1.42 cegger sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
210 1.1 augustss if (sc == NULL)
211 1.1 augustss return (ENXIO);
212 1.14 augustss sc->sc_open = 0;
213 1.1 augustss if (sc->sc_methods->im_close != NULL)
214 1.30 christos error = sc->sc_methods->im_close(sc->sc_handle, flag, mode, l);
215 1.1 augustss else
216 1.1 augustss error = 0;
217 1.1 augustss return (error);
218 1.1 augustss }
219 1.1 augustss
220 1.1 augustss int
221 1.1 augustss irframeread(dev_t dev, struct uio *uio, int flag)
222 1.1 augustss {
223 1.1 augustss struct irframe_softc *sc;
224 1.1 augustss
225 1.42 cegger sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
226 1.1 augustss if (sc == NULL)
227 1.1 augustss return (ENXIO);
228 1.40 cube if (!device_is_active(sc->sc_dev) || !sc->sc_open)
229 1.1 augustss return (EIO);
230 1.12 augustss if (uio->uio_resid < sc->sc_params.maxsize) {
231 1.12 augustss #ifdef DIAGNOSTIC
232 1.24 fvdl printf("irframeread: short read %ld < %d\n",
233 1.24 fvdl (long)uio->uio_resid, sc->sc_params.maxsize);
234 1.12 augustss #endif
235 1.11 augustss return (EINVAL);
236 1.12 augustss }
237 1.1 augustss return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
238 1.1 augustss }
239 1.1 augustss
240 1.1 augustss int
241 1.1 augustss irframewrite(dev_t dev, struct uio *uio, int flag)
242 1.1 augustss {
243 1.1 augustss struct irframe_softc *sc;
244 1.1 augustss
245 1.42 cegger sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
246 1.1 augustss if (sc == NULL)
247 1.1 augustss return (ENXIO);
248 1.40 cube if (!device_is_active(sc->sc_dev) || !sc->sc_open)
249 1.1 augustss return (EIO);
250 1.12 augustss if (uio->uio_resid > sc->sc_params.maxsize) {
251 1.12 augustss #ifdef DIAGNOSTIC
252 1.24 fvdl printf("irframeread: long write %ld > %d\n",
253 1.24 fvdl (long)uio->uio_resid, sc->sc_params.maxsize);
254 1.12 augustss #endif
255 1.11 augustss return (EINVAL);
256 1.12 augustss }
257 1.2 augustss return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
258 1.1 augustss }
259 1.1 augustss
260 1.1 augustss int
261 1.13 augustss irf_set_params(struct irframe_softc *sc, struct irda_params *p)
262 1.13 augustss {
263 1.13 augustss int error;
264 1.13 augustss
265 1.13 augustss DPRINTF(("irf_set_params: set params speed=%u ebofs=%u maxsize=%u "
266 1.13 augustss "speedmask=0x%x\n", p->speed, p->ebofs, p->maxsize,
267 1.13 augustss sc->sc_speedmask));
268 1.13 augustss
269 1.13 augustss if (p->maxsize > IRDA_MAX_FRAME_SIZE) {
270 1.13 augustss #ifdef IRFRAME_DEBUG
271 1.13 augustss printf("irf_set_params: bad maxsize=%u\n", p->maxsize);
272 1.13 augustss #endif
273 1.13 augustss return (EINVAL);
274 1.13 augustss }
275 1.13 augustss
276 1.13 augustss if (p->ebofs > IRDA_MAX_EBOFS) {
277 1.13 augustss #ifdef IRFRAME_DEBUG
278 1.13 augustss printf("irf_set_params: bad maxsize=%u\n", p->maxsize);
279 1.13 augustss #endif
280 1.13 augustss return (EINVAL);
281 1.13 augustss }
282 1.13 augustss
283 1.13 augustss #define CONC(x,y) x##y
284 1.13 augustss #define CASE(s) case s: if (!(sc->sc_speedmask & CONC(IRDA_SPEED_,s))) return (EINVAL); break
285 1.13 augustss switch (p->speed) {
286 1.13 augustss CASE(2400);
287 1.13 augustss CASE(9600);
288 1.13 augustss CASE(19200);
289 1.13 augustss CASE(38400);
290 1.13 augustss CASE(57600);
291 1.13 augustss CASE(115200);
292 1.13 augustss CASE(576000);
293 1.13 augustss CASE(1152000);
294 1.13 augustss CASE(4000000);
295 1.13 augustss CASE(16000000);
296 1.13 augustss default: return (EINVAL);
297 1.13 augustss }
298 1.13 augustss #undef CONC
299 1.13 augustss #undef CASE
300 1.13 augustss
301 1.13 augustss error = sc->sc_methods->im_set_params(sc->sc_handle, p);
302 1.13 augustss if (!error) {
303 1.13 augustss sc->sc_params = *p;
304 1.13 augustss DPRINTF(("irf_set_params: ok\n"));
305 1.13 augustss #ifdef DIAGNOSTIC
306 1.13 augustss if (p->speed != sc->sc_speed) {
307 1.13 augustss sc->sc_speed = p->speed;
308 1.40 cube aprint_verbose_dev(sc->sc_dev, "set speed %u\n",
309 1.13 augustss sc->sc_speed);
310 1.13 augustss }
311 1.13 augustss #endif
312 1.13 augustss } else {
313 1.13 augustss #ifdef IRFRAME_DEBUG
314 1.13 augustss printf("irf_set_params: error=%d\n", error);
315 1.13 augustss #endif
316 1.13 augustss }
317 1.13 augustss return (error);
318 1.13 augustss }
319 1.13 augustss
320 1.13 augustss int
321 1.6 augustss irf_reset_params(struct irframe_softc *sc)
322 1.6 augustss {
323 1.6 augustss struct irda_params params;
324 1.6 augustss
325 1.6 augustss params.speed = IRDA_DEFAULT_SPEED;
326 1.6 augustss params.ebofs = IRDA_DEFAULT_EBOFS;
327 1.6 augustss params.maxsize = IRDA_DEFAULT_SIZE;
328 1.13 augustss return (irf_set_params(sc, ¶ms));
329 1.6 augustss }
330 1.6 augustss
331 1.6 augustss int
332 1.37 christos irframeioctl(dev_t dev, u_long cmd, void *addr, int flag,
333 1.36 christos struct lwp *l)
334 1.1 augustss {
335 1.1 augustss struct irframe_softc *sc;
336 1.2 augustss void *vaddr = addr;
337 1.1 augustss int error;
338 1.1 augustss
339 1.42 cegger sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
340 1.1 augustss if (sc == NULL)
341 1.1 augustss return (ENXIO);
342 1.40 cube if (!device_is_active(sc->sc_dev) || !sc->sc_open)
343 1.1 augustss return (EIO);
344 1.1 augustss
345 1.1 augustss switch (cmd) {
346 1.1 augustss case FIONBIO:
347 1.1 augustss /* All handled in the upper FS layer. */
348 1.1 augustss error = 0;
349 1.1 augustss break;
350 1.1 augustss
351 1.1 augustss case IRDA_SET_PARAMS:
352 1.13 augustss error = irf_set_params(sc, vaddr);
353 1.1 augustss break;
354 1.1 augustss
355 1.1 augustss case IRDA_RESET_PARAMS:
356 1.7 augustss error = irf_reset_params(sc);
357 1.1 augustss break;
358 1.1 augustss
359 1.1 augustss case IRDA_GET_SPEEDMASK:
360 1.2 augustss error = sc->sc_methods->im_get_speeds(sc->sc_handle, vaddr);
361 1.1 augustss break;
362 1.1 augustss
363 1.1 augustss case IRDA_GET_TURNAROUNDMASK:
364 1.2 augustss error = sc->sc_methods->im_get_turnarounds(sc->sc_handle,vaddr);
365 1.1 augustss break;
366 1.1 augustss
367 1.1 augustss default:
368 1.1 augustss error = EINVAL;
369 1.1 augustss break;
370 1.1 augustss }
371 1.1 augustss return (error);
372 1.1 augustss }
373 1.1 augustss
374 1.1 augustss int
375 1.30 christos irframepoll(dev_t dev, int events, struct lwp *l)
376 1.1 augustss {
377 1.1 augustss struct irframe_softc *sc;
378 1.1 augustss
379 1.42 cegger sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
380 1.1 augustss if (sc == NULL)
381 1.29 ws return (POLLHUP);
382 1.40 cube if (!device_is_active(sc->sc_dev) || !sc->sc_open)
383 1.29 ws return (POLLHUP);
384 1.1 augustss
385 1.30 christos return (sc->sc_methods->im_poll(sc->sc_handle, events, l));
386 1.1 augustss }
387 1.23 jdolecek
388 1.23 jdolecek int
389 1.23 jdolecek irframekqfilter(dev_t dev, struct knote *kn)
390 1.23 jdolecek {
391 1.23 jdolecek struct irframe_softc *sc;
392 1.23 jdolecek
393 1.42 cegger sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
394 1.40 cube if (!device_is_active(sc->sc_dev) || !sc->sc_open)
395 1.23 jdolecek return (1);
396 1.23 jdolecek
397 1.23 jdolecek return (sc->sc_methods->im_kqfilter(sc->sc_handle, kn));
398 1.23 jdolecek }
399