irmce.c revision 1.1.32.5 1 /* $NetBSD: irmce.c,v 1.1.32.5 2015/10/06 21:32:15 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2011 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * IR receiver/transceiver for Windows Media Center
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: irmce.c,v 1.1.32.5 2015/10/06 21:32:15 skrll Exp $");
35
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/conf.h>
41 #include <sys/bus.h>
42 #include <sys/select.h>
43 #include <sys/module.h>
44
45 #include <dev/usb/usb.h>
46 #include <dev/usb/usbdi.h>
47 #include <dev/usb/usbdi_util.h>
48 #include <dev/usb/usbdevs.h>
49
50 #include <dev/ir/ir.h>
51 #include <dev/ir/cirio.h>
52 #include <dev/ir/cirvar.h>
53
54 enum irmce_state {
55 IRMCE_STATE_HEADER,
56 IRMCE_STATE_IRDATA,
57 IRMCE_STATE_CMDHEADER,
58 IRMCE_STATE_CMDDATA,
59 };
60
61 struct irmce_softc {
62 device_t sc_dev;
63 device_t sc_cirdev;
64
65 struct usbd_device * sc_udev;
66 struct usbd_interface * sc_iface;
67
68 int sc_bulkin_ep;
69 uint16_t sc_bulkin_maxpktsize;
70 struct usbd_pipe * sc_bulkin_pipe;
71 struct usbd_xfer * sc_bulkin_xfer;
72 uint8_t * sc_bulkin_buffer;
73
74 int sc_bulkout_ep;
75 uint16_t sc_bulkout_maxpktsize;
76 struct usbd_pipe * sc_bulkout_pipe;
77 struct usbd_xfer * sc_bulkout_xfer;
78 uint8_t * sc_bulkout_buffer;
79
80 bool sc_raw;
81
82 uint8_t sc_ir_buf[16];
83 size_t sc_ir_bufused;
84 size_t sc_ir_resid;
85 enum irmce_state sc_ir_state;
86 uint8_t sc_ir_header;
87
88 bool sc_rc6_hb[256];
89 size_t sc_rc6_nhb;
90 };
91
92 static int irmce_match(device_t, cfdata_t, void *);
93 static void irmce_attach(device_t, device_t, void *);
94 static int irmce_detach(device_t, int);
95 static void irmce_childdet(device_t, device_t);
96 static int irmce_activate(device_t, enum devact);
97 static int irmce_rescan(device_t, const char *, const int *);
98
99 static int irmce_print(void *, const char *);
100
101 static int irmce_reset(struct irmce_softc *);
102
103 static int irmce_open(void *, int, int, struct proc *);
104 static int irmce_close(void *, int, int, struct proc *);
105 static int irmce_read(void *, struct uio *, int);
106 static int irmce_write(void *, struct uio *, int);
107 static int irmce_setparams(void *, struct cir_params *);
108
109 static const struct cir_methods irmce_cir_methods = {
110 .im_open = irmce_open,
111 .im_close = irmce_close,
112 .im_read = irmce_read,
113 .im_write = irmce_write,
114 .im_setparams = irmce_setparams,
115 };
116
117 static const struct {
118 uint16_t vendor;
119 uint16_t product;
120 } irmce_devices[] = {
121 { USB_VENDOR_SMK, USB_PRODUCT_SMK_MCE_IR },
122 };
123
124 CFATTACH_DECL2_NEW(irmce, sizeof(struct irmce_softc),
125 irmce_match, irmce_attach, irmce_detach, irmce_activate,
126 irmce_rescan, irmce_childdet);
127
128 static int
129 irmce_match(device_t parent, cfdata_t match, void *opaque)
130 {
131 struct usbif_attach_arg *uiaa = opaque;
132 unsigned int i;
133
134 for (i = 0; i < __arraycount(irmce_devices); i++) {
135 if (irmce_devices[i].vendor == uiaa->uiaa_vendor &&
136 irmce_devices[i].product == uiaa->uiaa_product)
137 return UMATCH_VENDOR_PRODUCT;
138 }
139
140 return UMATCH_NONE;
141 }
142
143 static void
144 irmce_attach(device_t parent, device_t self, void *opaque)
145 {
146 struct irmce_softc *sc = device_private(self);
147 struct usbif_attach_arg *uiaa = opaque;
148 usb_endpoint_descriptor_t *ed;
149 char *devinfop;
150 unsigned int i;
151 uint8_t nep;
152
153 pmf_device_register(self, NULL, NULL);
154
155 aprint_naive("\n");
156
157 devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
158 aprint_normal(": %s\n", devinfop);
159 usbd_devinfo_free(devinfop);
160
161 sc->sc_dev = self;
162 sc->sc_udev = uiaa->uiaa_device;
163 sc->sc_iface = uiaa->uiaa_iface;
164
165 nep = 0;
166 usbd_endpoint_count(sc->sc_iface, &nep);
167 sc->sc_bulkin_ep = sc->sc_bulkout_ep = -1;
168 for (i = 0; i < nep; i++) {
169 int dir, type;
170
171 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
172 if (ed == NULL) {
173 aprint_error_dev(self,
174 "couldn't read endpoint descriptor %d\n", i);
175 continue;
176 }
177
178 dir = UE_GET_DIR(ed->bEndpointAddress);
179 type = UE_GET_XFERTYPE(ed->bmAttributes);
180
181 if (type != UE_BULK)
182 continue;
183
184 if (dir == UE_DIR_IN && sc->sc_bulkin_ep == -1) {
185 sc->sc_bulkin_ep = ed->bEndpointAddress;
186 sc->sc_bulkin_maxpktsize =
187 UE_GET_SIZE(UGETW(ed->wMaxPacketSize)) *
188 (UE_GET_TRANS(UGETW(ed->wMaxPacketSize)) + 1);
189 }
190 if (dir == UE_DIR_OUT && sc->sc_bulkout_ep == -1) {
191 sc->sc_bulkout_ep = ed->bEndpointAddress;
192 sc->sc_bulkout_maxpktsize =
193 UE_GET_SIZE(UGETW(ed->wMaxPacketSize)) *
194 (UE_GET_TRANS(UGETW(ed->wMaxPacketSize)) + 1);
195 }
196 }
197
198 aprint_debug_dev(self, "in 0x%02x/%d out 0x%02x/%d\n",
199 sc->sc_bulkin_ep, sc->sc_bulkin_maxpktsize,
200 sc->sc_bulkout_ep, sc->sc_bulkout_maxpktsize);
201
202 if (sc->sc_bulkin_maxpktsize < 16 || sc->sc_bulkout_maxpktsize < 16) {
203 aprint_error_dev(self, "bad maxpktsize\n");
204 return;
205 }
206 usbd_status err;
207
208 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_ep,
209 USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
210 if (err) {
211 aprint_error_dev(sc->sc_dev,
212 "couldn't open bulk-in pipe: %s\n", usbd_errstr(err));
213 return;
214 }
215 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_ep,
216 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
217 if (err) {
218 aprint_error_dev(sc->sc_dev,
219 "couldn't open bulk-out pipe: %s\n", usbd_errstr(err));
220 usbd_close_pipe(sc->sc_bulkin_pipe);
221 sc->sc_bulkin_pipe = NULL;
222 return;
223 }
224
225 int error;
226 error = usbd_create_xfer(sc->sc_bulkin_pipe, sc->sc_bulkin_maxpktsize,
227 USBD_SHORT_XFER_OK, 0, &sc->sc_bulkin_xfer);
228 if (error) {
229 goto fail;
230 }
231
232 error = usbd_create_xfer(sc->sc_bulkout_pipe,
233 sc->sc_bulkout_maxpktsize, USBD_FORCE_SHORT_XFER, 0,
234 &sc->sc_bulkout_xfer);
235 if (error) {
236 goto fail;
237 }
238 sc->sc_bulkin_buffer = usbd_get_buffer(sc->sc_bulkin_xfer);
239 sc->sc_bulkout_buffer = usbd_get_buffer(sc->sc_bulkout_xfer);
240
241 irmce_rescan(self, NULL, NULL);
242 return;
243
244 fail:
245 if (sc->sc_bulkin_xfer)
246 usbd_destroy_xfer(sc->sc_bulkin_xfer);
247 if (sc->sc_bulkout_xfer)
248 usbd_destroy_xfer(sc->sc_bulkout_xfer);
249 }
250
251 static int
252 irmce_detach(device_t self, int flags)
253 {
254 struct irmce_softc *sc = device_private(self);
255 int error;
256
257 if (sc->sc_cirdev) {
258 error = config_detach(sc->sc_cirdev, flags);
259 if (error)
260 return error;
261 }
262
263 if (sc->sc_bulkin_xfer) {
264 usbd_destroy_xfer(sc->sc_bulkin_xfer);
265 sc->sc_bulkin_buffer = NULL;
266 sc->sc_bulkin_xfer = NULL;
267 }
268 if (sc->sc_bulkout_xfer) {
269 usbd_destroy_xfer(sc->sc_bulkout_xfer);
270 sc->sc_bulkout_buffer = NULL;
271 sc->sc_bulkout_xfer = NULL;
272 }
273
274 pmf_device_deregister(self);
275
276 return 0;
277 }
278
279 static int
280 irmce_activate(device_t self, enum devact act)
281 {
282 return 0;
283 }
284
285 static int
286 irmce_rescan(device_t self, const char *ifattr, const int *locators)
287 {
288 struct irmce_softc *sc = device_private(self);
289 struct ir_attach_args iaa;
290
291 if (sc->sc_cirdev == NULL) {
292 iaa.ia_type = IR_TYPE_CIR;
293 iaa.ia_methods = &irmce_cir_methods;
294 iaa.ia_handle = sc;
295 sc->sc_cirdev = config_found_ia(self, "irbus",
296 &iaa, irmce_print);
297 }
298
299 return 0;
300 }
301
302 static int
303 irmce_print(void *priv, const char *pnp)
304 {
305 if (pnp)
306 aprint_normal("cir at %s", pnp);
307
308 return UNCONF;
309 }
310
311 static void
312 irmce_childdet(device_t self, device_t child)
313 {
314 struct irmce_softc *sc = device_private(self);
315
316 if (sc->sc_cirdev == child)
317 sc->sc_cirdev = NULL;
318 }
319
320 static int
321 irmce_reset(struct irmce_softc *sc)
322 {
323 static const uint8_t reset_cmd[] = { 0x00, 0xff, 0xaa };
324 uint8_t *p = sc->sc_bulkout_buffer;
325 usbd_status err;
326 uint32_t wlen;
327 unsigned int n;
328
329 for (n = 0; n < __arraycount(reset_cmd); n++)
330 *p++ = reset_cmd[n];
331
332 wlen = sizeof(reset_cmd);
333 err = usbd_bulk_transfer(sc->sc_bulkout_xfer, sc->sc_bulkout_pipe,
334 USBD_FORCE_SHORT_XFER, USBD_DEFAULT_TIMEOUT,
335 sc->sc_bulkout_buffer, &wlen);
336 if (err != USBD_NORMAL_COMPLETION) {
337 if (err == USBD_INTERRUPTED)
338 return EINTR;
339 else if (err == USBD_TIMEOUT)
340 return ETIMEDOUT;
341 else
342 return EIO;
343 }
344
345 return 0;
346 }
347
348 static int
349 irmce_open(void *priv, int flag, int mode, struct proc *p)
350 {
351 struct irmce_softc *sc = priv;
352 int err = irmce_reset(sc);
353 if (err) {
354 aprint_error_dev(sc->sc_dev,
355 "couldn't reset device: %s\n", usbd_errstr(err));
356 usbd_close_pipe(sc->sc_bulkin_pipe);
357 sc->sc_bulkin_pipe = NULL;
358 usbd_close_pipe(sc->sc_bulkout_pipe);
359 sc->sc_bulkout_pipe = NULL;
360 }
361 sc->sc_ir_state = IRMCE_STATE_HEADER;
362 sc->sc_rc6_nhb = 0;
363
364 return 0;
365 }
366
367 static int
368 irmce_close(void *priv, int flag, int mode, struct proc *p)
369 {
370 struct irmce_softc *sc = priv;
371
372 if (sc->sc_bulkin_pipe) {
373 usbd_abort_pipe(sc->sc_bulkin_pipe);
374 usbd_close_pipe(sc->sc_bulkin_pipe);
375 sc->sc_bulkin_pipe = NULL;
376 }
377 if (sc->sc_bulkout_pipe) {
378 usbd_abort_pipe(sc->sc_bulkout_pipe);
379 usbd_close_pipe(sc->sc_bulkout_pipe);
380 sc->sc_bulkout_pipe = NULL;
381 }
382
383 return 0;
384 }
385
386 static int
387 irmce_rc6_decode(struct irmce_softc *sc, uint8_t *buf, size_t buflen,
388 struct uio *uio)
389 {
390 bool *hb = &sc->sc_rc6_hb[0];
391 unsigned int n;
392 int state, pulse;
393 uint32_t data;
394 uint8_t mode;
395 bool idle = false;
396
397 for (n = 0; n < buflen; n++) {
398 state = (buf[n] & 0x80) ? 1 : 0;
399 pulse = (buf[n] & 0x7f) * 50;
400
401 if (pulse >= 300 && pulse <= 600) {
402 hb[sc->sc_rc6_nhb++] = state;
403 } else if (pulse >= 680 && pulse <= 1080) {
404 hb[sc->sc_rc6_nhb++] = state;
405 hb[sc->sc_rc6_nhb++] = state;
406 } else if (pulse >= 1150 && pulse <= 1450) {
407 hb[sc->sc_rc6_nhb++] = state;
408 hb[sc->sc_rc6_nhb++] = state;
409 hb[sc->sc_rc6_nhb++] = state;
410 } else if (pulse >= 2400 && pulse <= 2800) {
411 hb[sc->sc_rc6_nhb++] = state;
412 hb[sc->sc_rc6_nhb++] = state;
413 hb[sc->sc_rc6_nhb++] = state;
414 hb[sc->sc_rc6_nhb++] = state;
415 hb[sc->sc_rc6_nhb++] = state;
416 hb[sc->sc_rc6_nhb++] = state;
417 } else if (pulse > 3000) {
418 if (sc->sc_rc6_nhb & 1)
419 hb[sc->sc_rc6_nhb++] = state;
420 idle = true;
421 break;
422 } else {
423 aprint_debug_dev(sc->sc_dev,
424 "error parsing RC6 stream (pulse=%d)\n", pulse);
425 return EIO;
426 }
427 }
428
429 if (!idle)
430 return 0;
431
432 if (sc->sc_rc6_nhb < 20) {
433 aprint_debug_dev(sc->sc_dev, "not enough RC6 data\n");
434 return EIO;
435 }
436
437 /* RC6 leader 11111100 */
438 if (!hb[0] || !hb[1] || !hb[2] || !hb[3] || !hb[4] || !hb[5] ||
439 hb[6] || hb[7]) {
440 aprint_debug_dev(sc->sc_dev, "bad RC6 leader\n");
441 return EIO;
442 }
443
444 /* start bit 10 */
445 if (!hb[8] || hb[9]) {
446 aprint_debug_dev(sc->sc_dev, "missing RC6 start bit\n");
447 return EIO;
448 }
449
450 /* mode info */
451 mode = 0x00;
452 for (n = 10; n < 15; n += 2) {
453 if (hb[n] && !hb[n + 1])
454 mode = (mode << 1) | 1;
455 else if (!hb[n] && hb[n + 1])
456 mode = (mode << 1) | 0;
457 else {
458 aprint_debug_dev(sc->sc_dev, "bad RC6 mode bits\n");
459 return EIO;
460 }
461 }
462
463 data = 0;
464 for (n = 20; n < sc->sc_rc6_nhb; n += 2) {
465 if (hb[n] && !hb[n + 1])
466 data = (data << 1) | 1;
467 else if (!hb[n] && hb[n + 1])
468 data = (data << 1) | 0;
469 else {
470 aprint_debug_dev(sc->sc_dev, "bad RC6 data bits\n");
471 return EIO;
472 }
473 }
474
475 sc->sc_rc6_nhb = 0;
476
477 return uiomove(&data, sizeof(data), uio);
478 }
479
480 static int
481 irmce_process(struct irmce_softc *sc, uint8_t *buf, size_t buflen,
482 struct uio *uio)
483 {
484 uint8_t *p = buf;
485 uint8_t data, cmd;
486 int error;
487
488 while (p - buf < (ssize_t)buflen) {
489 switch (sc->sc_ir_state) {
490 case IRMCE_STATE_HEADER:
491 sc->sc_ir_header = data = *p++;
492 if ((data & 0xe0) == 0x80 && (data & 0x1f) != 0x1f) {
493 sc->sc_ir_bufused = 0;
494 sc->sc_ir_resid = data & 0x1f;
495 sc->sc_ir_state = IRMCE_STATE_IRDATA;
496 if (sc->sc_ir_resid > sizeof(sc->sc_ir_buf))
497 return EIO;
498 if (sc->sc_ir_resid == 0)
499 sc->sc_ir_state = IRMCE_STATE_HEADER;
500 } else {
501 sc->sc_ir_state = IRMCE_STATE_CMDHEADER;
502 }
503 break;
504 case IRMCE_STATE_CMDHEADER:
505 cmd = *p++;
506 data = sc->sc_ir_header;
507 if (data == 0x00 && cmd == 0x9f)
508 sc->sc_ir_resid = 1;
509 else if (data == 0xff && cmd == 0x0b)
510 sc->sc_ir_resid = 2;
511 else if (data == 0x9f) {
512 if (cmd == 0x04 || cmd == 0x06 ||
513 cmd == 0x0c || cmd == 0x15) {
514 sc->sc_ir_resid = 2;
515 } else if (cmd == 0x01 || cmd == 0x08 ||
516 cmd == 0x14) {
517 sc->sc_ir_resid = 1;
518 }
519 }
520 if (sc->sc_ir_resid > 0)
521 sc->sc_ir_state = IRMCE_STATE_CMDDATA;
522 else
523 sc->sc_ir_state = IRMCE_STATE_HEADER;
524 break;
525 case IRMCE_STATE_IRDATA:
526 sc->sc_ir_resid--;
527 sc->sc_ir_buf[sc->sc_ir_bufused++] = *p;
528 p++;
529 if (sc->sc_ir_resid == 0) {
530 sc->sc_ir_state = IRMCE_STATE_HEADER;
531 error = irmce_rc6_decode(sc,
532 sc->sc_ir_buf, sc->sc_ir_bufused, uio);
533 if (error)
534 sc->sc_rc6_nhb = 0;
535 }
536 break;
537 case IRMCE_STATE_CMDDATA:
538 p++;
539 sc->sc_ir_resid--;
540 if (sc->sc_ir_resid == 0)
541 sc->sc_ir_state = IRMCE_STATE_HEADER;
542 break;
543 }
544
545 }
546
547 return 0;
548 }
549
550 static int
551 irmce_read(void *priv, struct uio *uio, int flag)
552 {
553 struct irmce_softc *sc = priv;
554 usbd_status err;
555 uint32_t rlen;
556 int error = 0;
557
558 while (uio->uio_resid > 0) {
559 rlen = sc->sc_bulkin_maxpktsize;
560 err = usbd_bulk_transfer(sc->sc_bulkin_xfer,
561 sc->sc_bulkin_pipe, USBD_SHORT_XFER_OK,
562 USBD_DEFAULT_TIMEOUT, sc->sc_bulkin_buffer, &rlen);
563 if (err != USBD_NORMAL_COMPLETION) {
564 if (err == USBD_INTERRUPTED)
565 return EINTR;
566 else if (err == USBD_TIMEOUT)
567 continue;
568 else
569 return EIO;
570 }
571
572 if (sc->sc_raw) {
573 error = uiomove(sc->sc_bulkin_buffer, rlen, uio);
574 break;
575 } else {
576 error = irmce_process(sc, sc->sc_bulkin_buffer,
577 rlen, uio);
578 if (error)
579 break;
580 }
581 }
582
583 return error;
584 }
585
586 static int
587 irmce_write(void *priv, struct uio *uio, int flag)
588 {
589 return EIO;
590 }
591
592 static int
593 irmce_setparams(void *priv, struct cir_params *params)
594 {
595 struct irmce_softc *sc = priv;
596
597 if (params->raw > 1)
598 return EINVAL;
599 sc->sc_raw = params->raw;
600
601 return 0;
602 }
603
604 MODULE(MODULE_CLASS_DRIVER, irmce, NULL);
605
606 #ifdef _MODULE
607 #include "ioconf.c"
608 #endif
609
610 static int
611 irmce_modcmd(modcmd_t cmd, void *opaque)
612 {
613 switch (cmd) {
614 case MODULE_CMD_INIT:
615 #ifdef _MODULE
616 return config_init_component(cfdriver_ioconf_irmce,
617 cfattach_ioconf_irmce, cfdata_ioconf_irmce);
618 #else
619 return 0;
620 #endif
621 case MODULE_CMD_FINI:
622 #ifdef _MODULE
623 return config_fini_component(cfdriver_ioconf_irmce,
624 cfattach_ioconf_irmce, cfdata_ioconf_irmce);
625 #else
626 return 0;
627 #endif
628 default:
629 return ENOTTY;
630 }
631 }
632