irmce.c revision 1.1.32.3 1 /* $NetBSD: irmce.c,v 1.1.32.3 2015/03/19 17:26:43 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.3 2015/03/19 17:26:43 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->vendor &&
136 irmce_devices[i].product == 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->device, 0);
158 aprint_normal(": %s\n", devinfop);
159 usbd_devinfo_free(devinfop);
160
161 sc->sc_dev = self;
162 sc->sc_udev = uiaa->device;
163 sc->sc_iface = 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
207 sc->sc_bulkin_xfer = usbd_alloc_xfer(sc->sc_udev);
208 sc->sc_bulkout_xfer = usbd_alloc_xfer(sc->sc_udev);
209 if (sc->sc_bulkin_xfer == NULL || sc->sc_bulkout_xfer == NULL) {
210 aprint_error_dev(self, "couldn't alloc xfer\n");
211 return;
212 }
213 sc->sc_bulkin_buffer = usbd_alloc_buffer(sc->sc_bulkin_xfer,
214 sc->sc_bulkin_maxpktsize);
215 sc->sc_bulkout_buffer = usbd_alloc_buffer(sc->sc_bulkout_xfer,
216 sc->sc_bulkout_maxpktsize);
217 if (sc->sc_bulkin_buffer == NULL || sc->sc_bulkout_buffer == NULL) {
218 aprint_error_dev(self, "couldn't alloc xfer buffer\n");
219 return;
220 }
221
222 irmce_rescan(self, NULL, NULL);
223 }
224
225 static int
226 irmce_detach(device_t self, int flags)
227 {
228 struct irmce_softc *sc = device_private(self);
229 int error;
230
231 if (sc->sc_cirdev) {
232 error = config_detach(sc->sc_cirdev, flags);
233 if (error)
234 return error;
235 }
236
237 if (sc->sc_bulkin_xfer) {
238 usbd_free_xfer(sc->sc_bulkin_xfer);
239 sc->sc_bulkin_buffer = NULL;
240 sc->sc_bulkin_xfer = NULL;
241 }
242 if (sc->sc_bulkout_xfer) {
243 usbd_free_xfer(sc->sc_bulkout_xfer);
244 sc->sc_bulkout_buffer = NULL;
245 sc->sc_bulkout_xfer = NULL;
246 }
247
248 pmf_device_deregister(self);
249
250 return 0;
251 }
252
253 static int
254 irmce_activate(device_t self, enum devact act)
255 {
256 return 0;
257 }
258
259 static int
260 irmce_rescan(device_t self, const char *ifattr, const int *locators)
261 {
262 struct irmce_softc *sc = device_private(self);
263 struct ir_attach_args iaa;
264
265 if (sc->sc_cirdev == NULL) {
266 iaa.ia_type = IR_TYPE_CIR;
267 iaa.ia_methods = &irmce_cir_methods;
268 iaa.ia_handle = sc;
269 sc->sc_cirdev = config_found_ia(self, "irbus",
270 &iaa, irmce_print);
271 }
272
273 return 0;
274 }
275
276 static int
277 irmce_print(void *priv, const char *pnp)
278 {
279 if (pnp)
280 aprint_normal("cir at %s", pnp);
281
282 return UNCONF;
283 }
284
285 static void
286 irmce_childdet(device_t self, device_t child)
287 {
288 struct irmce_softc *sc = device_private(self);
289
290 if (sc->sc_cirdev == child)
291 sc->sc_cirdev = NULL;
292 }
293
294 static int
295 irmce_reset(struct irmce_softc *sc)
296 {
297 static const uint8_t reset_cmd[] = { 0x00, 0xff, 0xaa };
298 uint8_t *p = sc->sc_bulkout_buffer;
299 usbd_status err;
300 uint32_t wlen;
301 unsigned int n;
302
303 for (n = 0; n < __arraycount(reset_cmd); n++)
304 *p++ = reset_cmd[n];
305
306 wlen = sizeof(reset_cmd);
307 err = usbd_bulk_transfer(sc->sc_bulkin_xfer,
308 sc->sc_bulkout_pipe, USBD_FORCE_SHORT_XFER,
309 USBD_DEFAULT_TIMEOUT, sc->sc_bulkout_buffer, &wlen);
310 if (err != USBD_NORMAL_COMPLETION) {
311 if (err == USBD_INTERRUPTED)
312 return EINTR;
313 else if (err == USBD_TIMEOUT)
314 return ETIMEDOUT;
315 else
316 return EIO;
317 }
318
319 return 0;
320 }
321
322 static int
323 irmce_open(void *priv, int flag, int mode, struct proc *p)
324 {
325 struct irmce_softc *sc = priv;
326 usbd_status err;
327
328 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_ep,
329 USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
330 if (err) {
331 aprint_error_dev(sc->sc_dev,
332 "couldn't open bulk-in pipe: %s\n", usbd_errstr(err));
333 return ENXIO;
334 }
335 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_ep,
336 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
337 if (err) {
338 aprint_error_dev(sc->sc_dev,
339 "couldn't open bulk-out pipe: %s\n", usbd_errstr(err));
340 usbd_close_pipe(sc->sc_bulkin_pipe);
341 sc->sc_bulkin_pipe = NULL;
342 return ENXIO;
343 }
344
345 err = irmce_reset(sc);
346 if (err) {
347 aprint_error_dev(sc->sc_dev,
348 "couldn't reset device: %s\n", usbd_errstr(err));
349 usbd_close_pipe(sc->sc_bulkin_pipe);
350 sc->sc_bulkin_pipe = NULL;
351 usbd_close_pipe(sc->sc_bulkout_pipe);
352 sc->sc_bulkout_pipe = NULL;
353 }
354 sc->sc_ir_state = IRMCE_STATE_HEADER;
355 sc->sc_rc6_nhb = 0;
356
357 return 0;
358 }
359
360 static int
361 irmce_close(void *priv, int flag, int mode, struct proc *p)
362 {
363 struct irmce_softc *sc = priv;
364
365 if (sc->sc_bulkin_pipe) {
366 usbd_abort_pipe(sc->sc_bulkin_pipe);
367 usbd_close_pipe(sc->sc_bulkin_pipe);
368 sc->sc_bulkin_pipe = NULL;
369 }
370 if (sc->sc_bulkout_pipe) {
371 usbd_abort_pipe(sc->sc_bulkout_pipe);
372 usbd_close_pipe(sc->sc_bulkout_pipe);
373 sc->sc_bulkout_pipe = NULL;
374 }
375
376 return 0;
377 }
378
379 static int
380 irmce_rc6_decode(struct irmce_softc *sc, uint8_t *buf, size_t buflen,
381 struct uio *uio)
382 {
383 bool *hb = &sc->sc_rc6_hb[0];
384 unsigned int n;
385 int state, pulse;
386 uint32_t data;
387 uint8_t mode;
388 bool idle = false;
389
390 for (n = 0; n < buflen; n++) {
391 state = (buf[n] & 0x80) ? 1 : 0;
392 pulse = (buf[n] & 0x7f) * 50;
393
394 if (pulse >= 300 && pulse <= 600) {
395 hb[sc->sc_rc6_nhb++] = state;
396 } else if (pulse >= 680 && pulse <= 1080) {
397 hb[sc->sc_rc6_nhb++] = state;
398 hb[sc->sc_rc6_nhb++] = state;
399 } else if (pulse >= 1150 && pulse <= 1450) {
400 hb[sc->sc_rc6_nhb++] = state;
401 hb[sc->sc_rc6_nhb++] = state;
402 hb[sc->sc_rc6_nhb++] = state;
403 } else if (pulse >= 2400 && pulse <= 2800) {
404 hb[sc->sc_rc6_nhb++] = state;
405 hb[sc->sc_rc6_nhb++] = state;
406 hb[sc->sc_rc6_nhb++] = state;
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 > 3000) {
411 if (sc->sc_rc6_nhb & 1)
412 hb[sc->sc_rc6_nhb++] = state;
413 idle = true;
414 break;
415 } else {
416 aprint_debug_dev(sc->sc_dev,
417 "error parsing RC6 stream (pulse=%d)\n", pulse);
418 return EIO;
419 }
420 }
421
422 if (!idle)
423 return 0;
424
425 if (sc->sc_rc6_nhb < 20) {
426 aprint_debug_dev(sc->sc_dev, "not enough RC6 data\n");
427 return EIO;
428 }
429
430 /* RC6 leader 11111100 */
431 if (!hb[0] || !hb[1] || !hb[2] || !hb[3] || !hb[4] || !hb[5] ||
432 hb[6] || hb[7]) {
433 aprint_debug_dev(sc->sc_dev, "bad RC6 leader\n");
434 return EIO;
435 }
436
437 /* start bit 10 */
438 if (!hb[8] || hb[9]) {
439 aprint_debug_dev(sc->sc_dev, "missing RC6 start bit\n");
440 return EIO;
441 }
442
443 /* mode info */
444 mode = 0x00;
445 for (n = 10; n < 15; n += 2) {
446 if (hb[n] && !hb[n + 1])
447 mode = (mode << 1) | 1;
448 else if (!hb[n] && hb[n + 1])
449 mode = (mode << 1) | 0;
450 else {
451 aprint_debug_dev(sc->sc_dev, "bad RC6 mode bits\n");
452 return EIO;
453 }
454 }
455
456 data = 0;
457 for (n = 20; n < sc->sc_rc6_nhb; n += 2) {
458 if (hb[n] && !hb[n + 1])
459 data = (data << 1) | 1;
460 else if (!hb[n] && hb[n + 1])
461 data = (data << 1) | 0;
462 else {
463 aprint_debug_dev(sc->sc_dev, "bad RC6 data bits\n");
464 return EIO;
465 }
466 }
467
468 sc->sc_rc6_nhb = 0;
469
470 return uiomove(&data, sizeof(data), uio);
471 }
472
473 static int
474 irmce_process(struct irmce_softc *sc, uint8_t *buf, size_t buflen,
475 struct uio *uio)
476 {
477 uint8_t *p = buf;
478 uint8_t data, cmd;
479 int error;
480
481 while (p - buf < (ssize_t)buflen) {
482 switch (sc->sc_ir_state) {
483 case IRMCE_STATE_HEADER:
484 sc->sc_ir_header = data = *p++;
485 if ((data & 0xe0) == 0x80 && (data & 0x1f) != 0x1f) {
486 sc->sc_ir_bufused = 0;
487 sc->sc_ir_resid = data & 0x1f;
488 sc->sc_ir_state = IRMCE_STATE_IRDATA;
489 if (sc->sc_ir_resid > sizeof(sc->sc_ir_buf))
490 return EIO;
491 if (sc->sc_ir_resid == 0)
492 sc->sc_ir_state = IRMCE_STATE_HEADER;
493 } else {
494 sc->sc_ir_state = IRMCE_STATE_CMDHEADER;
495 }
496 break;
497 case IRMCE_STATE_CMDHEADER:
498 cmd = *p++;
499 data = sc->sc_ir_header;
500 if (data == 0x00 && cmd == 0x9f)
501 sc->sc_ir_resid = 1;
502 else if (data == 0xff && cmd == 0x0b)
503 sc->sc_ir_resid = 2;
504 else if (data == 0x9f) {
505 if (cmd == 0x04 || cmd == 0x06 ||
506 cmd == 0x0c || cmd == 0x15) {
507 sc->sc_ir_resid = 2;
508 } else if (cmd == 0x01 || cmd == 0x08 ||
509 cmd == 0x14) {
510 sc->sc_ir_resid = 1;
511 }
512 }
513 if (sc->sc_ir_resid > 0)
514 sc->sc_ir_state = IRMCE_STATE_CMDDATA;
515 else
516 sc->sc_ir_state = IRMCE_STATE_HEADER;
517 break;
518 case IRMCE_STATE_IRDATA:
519 sc->sc_ir_resid--;
520 sc->sc_ir_buf[sc->sc_ir_bufused++] = *p;
521 p++;
522 if (sc->sc_ir_resid == 0) {
523 sc->sc_ir_state = IRMCE_STATE_HEADER;
524 error = irmce_rc6_decode(sc,
525 sc->sc_ir_buf, sc->sc_ir_bufused, uio);
526 if (error)
527 sc->sc_rc6_nhb = 0;
528 }
529 break;
530 case IRMCE_STATE_CMDDATA:
531 p++;
532 sc->sc_ir_resid--;
533 if (sc->sc_ir_resid == 0)
534 sc->sc_ir_state = IRMCE_STATE_HEADER;
535 break;
536 }
537
538 }
539
540 return 0;
541 }
542
543 static int
544 irmce_read(void *priv, struct uio *uio, int flag)
545 {
546 struct irmce_softc *sc = priv;
547 usbd_status err;
548 uint32_t rlen;
549 int error = 0;
550
551 while (uio->uio_resid > 0) {
552 rlen = sc->sc_bulkin_maxpktsize;
553 err = usbd_bulk_transfer(sc->sc_bulkin_xfer,
554 sc->sc_bulkin_pipe, USBD_SHORT_XFER_OK,
555 USBD_DEFAULT_TIMEOUT, sc->sc_bulkin_buffer, &rlen);
556 if (err != USBD_NORMAL_COMPLETION) {
557 if (err == USBD_INTERRUPTED)
558 return EINTR;
559 else if (err == USBD_TIMEOUT)
560 continue;
561 else
562 return EIO;
563 }
564
565 if (sc->sc_raw) {
566 error = uiomove(sc->sc_bulkin_buffer, rlen, uio);
567 break;
568 } else {
569 error = irmce_process(sc, sc->sc_bulkin_buffer,
570 rlen, uio);
571 if (error)
572 break;
573 }
574 }
575
576 return error;
577 }
578
579 static int
580 irmce_write(void *priv, struct uio *uio, int flag)
581 {
582 return EIO;
583 }
584
585 static int
586 irmce_setparams(void *priv, struct cir_params *params)
587 {
588 struct irmce_softc *sc = priv;
589
590 if (params->raw > 1)
591 return EINVAL;
592 sc->sc_raw = params->raw;
593
594 return 0;
595 }
596
597 MODULE(MODULE_CLASS_DRIVER, irmce, NULL);
598
599 #ifdef _MODULE
600 #include "ioconf.c"
601 #endif
602
603 static int
604 irmce_modcmd(modcmd_t cmd, void *opaque)
605 {
606 switch (cmd) {
607 case MODULE_CMD_INIT:
608 #ifdef _MODULE
609 return config_init_component(cfdriver_ioconf_irmce,
610 cfattach_ioconf_irmce, cfdata_ioconf_irmce);
611 #else
612 return 0;
613 #endif
614 case MODULE_CMD_FINI:
615 #ifdef _MODULE
616 return config_fini_component(cfdriver_ioconf_irmce,
617 cfattach_ioconf_irmce, cfdata_ioconf_irmce);
618 #else
619 return 0;
620 #endif
621 default:
622 return ENOTTY;
623 }
624 }
625