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