1 /* $NetBSD: irmce.c,v 1.1 2011/07/19 12:23:04 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 Jared D. McNeill <jmcneill@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 2011/07/19 12:23:04 jmcneill 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 usbd_device_handle sc_udev; 66 usbd_interface_handle sc_iface; 67 68 int sc_bulkin_ep; 69 uint16_t sc_bulkin_maxpktsize; 70 usbd_pipe_handle sc_bulkin_pipe; 71 usbd_xfer_handle sc_bulkin_xfer; 72 uint8_t * sc_bulkin_buffer; 73 74 int sc_bulkout_ep; 75 uint16_t sc_bulkout_maxpktsize; 76 usbd_pipe_handle sc_bulkout_pipe; 77 usbd_xfer_handle 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_NO_COPY|USBD_FORCE_SHORT_XFER, 309 USBD_DEFAULT_TIMEOUT, sc->sc_bulkout_buffer, &wlen, 310 "irmcereset"); 311 if (err != USBD_NORMAL_COMPLETION) { 312 if (err == USBD_INTERRUPTED) 313 return EINTR; 314 else if (err == USBD_TIMEOUT) 315 return ETIMEDOUT; 316 else 317 return EIO; 318 } 319 320 return 0; 321 } 322 323 static int 324 irmce_open(void *priv, int flag, int mode, struct proc *p) 325 { 326 struct irmce_softc *sc = priv; 327 usbd_status err; 328 329 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_ep, 330 USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe); 331 if (err) { 332 aprint_error_dev(sc->sc_dev, 333 "couldn't open bulk-in pipe: %s\n", usbd_errstr(err)); 334 return ENXIO; 335 } 336 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_ep, 337 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe); 338 if (err) { 339 aprint_error_dev(sc->sc_dev, 340 "couldn't open bulk-out pipe: %s\n", usbd_errstr(err)); 341 usbd_close_pipe(sc->sc_bulkin_pipe); 342 sc->sc_bulkin_pipe = NULL; 343 return ENXIO; 344 } 345 346 err = irmce_reset(sc); 347 if (err) { 348 aprint_error_dev(sc->sc_dev, 349 "couldn't reset device: %s\n", usbd_errstr(err)); 350 usbd_close_pipe(sc->sc_bulkin_pipe); 351 sc->sc_bulkin_pipe = NULL; 352 usbd_close_pipe(sc->sc_bulkout_pipe); 353 sc->sc_bulkout_pipe = NULL; 354 } 355 sc->sc_ir_state = IRMCE_STATE_HEADER; 356 sc->sc_rc6_nhb = 0; 357 358 return 0; 359 } 360 361 static int 362 irmce_close(void *priv, int flag, int mode, struct proc *p) 363 { 364 struct irmce_softc *sc = priv; 365 366 if (sc->sc_bulkin_pipe) { 367 usbd_abort_pipe(sc->sc_bulkin_pipe); 368 usbd_close_pipe(sc->sc_bulkin_pipe); 369 sc->sc_bulkin_pipe = NULL; 370 } 371 if (sc->sc_bulkout_pipe) { 372 usbd_abort_pipe(sc->sc_bulkout_pipe); 373 usbd_close_pipe(sc->sc_bulkout_pipe); 374 sc->sc_bulkout_pipe = NULL; 375 } 376 377 return 0; 378 } 379 380 static int 381 irmce_rc6_decode(struct irmce_softc *sc, uint8_t *buf, size_t buflen, 382 struct uio *uio) 383 { 384 bool *hb = &sc->sc_rc6_hb[0]; 385 unsigned int n; 386 int state, pulse; 387 uint32_t data; 388 uint8_t mode; 389 bool idle = false; 390 391 for (n = 0; n < buflen; n++) { 392 state = (buf[n] & 0x80) ? 1 : 0; 393 pulse = (buf[n] & 0x7f) * 50; 394 395 if (pulse >= 300 && pulse <= 600) { 396 hb[sc->sc_rc6_nhb++] = state; 397 } else if (pulse >= 680 && pulse <= 1080) { 398 hb[sc->sc_rc6_nhb++] = state; 399 hb[sc->sc_rc6_nhb++] = state; 400 } else if (pulse >= 1150 && pulse <= 1450) { 401 hb[sc->sc_rc6_nhb++] = state; 402 hb[sc->sc_rc6_nhb++] = state; 403 hb[sc->sc_rc6_nhb++] = state; 404 } else if (pulse >= 2400 && pulse <= 2800) { 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 hb[sc->sc_rc6_nhb++] = state; 411 } else if (pulse > 3000) { 412 if (sc->sc_rc6_nhb & 1) 413 hb[sc->sc_rc6_nhb++] = state; 414 idle = true; 415 break; 416 } else { 417 aprint_debug_dev(sc->sc_dev, 418 "error parsing RC6 stream (pulse=%d)\n", pulse); 419 return EIO; 420 } 421 } 422 423 if (!idle) 424 return 0; 425 426 if (sc->sc_rc6_nhb < 20) { 427 aprint_debug_dev(sc->sc_dev, "not enough RC6 data\n"); 428 return EIO; 429 } 430 431 /* RC6 leader 11111100 */ 432 if (!hb[0] || !hb[1] || !hb[2] || !hb[3] || !hb[4] || !hb[5] || 433 hb[6] || hb[7]) { 434 aprint_debug_dev(sc->sc_dev, "bad RC6 leader\n"); 435 return EIO; 436 } 437 438 /* start bit 10 */ 439 if (!hb[8] || hb[9]) { 440 aprint_debug_dev(sc->sc_dev, "missing RC6 start bit\n"); 441 return EIO; 442 } 443 444 /* mode info */ 445 mode = 0x00; 446 for (n = 10; n < 15; n += 2) { 447 if (hb[n] && !hb[n + 1]) 448 mode = (mode << 1) | 1; 449 else if (!hb[n] && hb[n + 1]) 450 mode = (mode << 1) | 0; 451 else { 452 aprint_debug_dev(sc->sc_dev, "bad RC6 mode bits\n"); 453 return EIO; 454 } 455 } 456 457 data = 0; 458 for (n = 20; n < sc->sc_rc6_nhb; n += 2) { 459 if (hb[n] && !hb[n + 1]) 460 data = (data << 1) | 1; 461 else if (!hb[n] && hb[n + 1]) 462 data = (data << 1) | 0; 463 else { 464 aprint_debug_dev(sc->sc_dev, "bad RC6 data bits\n"); 465 return EIO; 466 } 467 } 468 469 sc->sc_rc6_nhb = 0; 470 471 return uiomove(&data, sizeof(data), uio); 472 } 473 474 static int 475 irmce_process(struct irmce_softc *sc, uint8_t *buf, size_t buflen, 476 struct uio *uio) 477 { 478 uint8_t *p = buf; 479 uint8_t data, cmd; 480 int error; 481 482 while (p - buf < (ssize_t)buflen) { 483 switch (sc->sc_ir_state) { 484 case IRMCE_STATE_HEADER: 485 sc->sc_ir_header = data = *p++; 486 if ((data & 0xe0) == 0x80 && (data & 0x1f) != 0x1f) { 487 sc->sc_ir_bufused = 0; 488 sc->sc_ir_resid = data & 0x1f; 489 sc->sc_ir_state = IRMCE_STATE_IRDATA; 490 if (sc->sc_ir_resid > sizeof(sc->sc_ir_buf)) 491 return EIO; 492 if (sc->sc_ir_resid == 0) 493 sc->sc_ir_state = IRMCE_STATE_HEADER; 494 } else { 495 sc->sc_ir_state = IRMCE_STATE_CMDHEADER; 496 } 497 break; 498 case IRMCE_STATE_CMDHEADER: 499 cmd = *p++; 500 data = sc->sc_ir_header; 501 if (data == 0x00 && cmd == 0x9f) 502 sc->sc_ir_resid = 1; 503 else if (data == 0xff && cmd == 0x0b) 504 sc->sc_ir_resid = 2; 505 else if (data == 0x9f) { 506 if (cmd == 0x04 || cmd == 0x06 || 507 cmd == 0x0c || cmd == 0x15) { 508 sc->sc_ir_resid = 2; 509 } else if (cmd == 0x01 || cmd == 0x08 || 510 cmd == 0x14) { 511 sc->sc_ir_resid = 1; 512 } 513 } 514 if (sc->sc_ir_resid > 0) 515 sc->sc_ir_state = IRMCE_STATE_CMDDATA; 516 else 517 sc->sc_ir_state = IRMCE_STATE_HEADER; 518 break; 519 case IRMCE_STATE_IRDATA: 520 sc->sc_ir_resid--; 521 sc->sc_ir_buf[sc->sc_ir_bufused++] = *p; 522 p++; 523 if (sc->sc_ir_resid == 0) { 524 sc->sc_ir_state = IRMCE_STATE_HEADER; 525 error = irmce_rc6_decode(sc, 526 sc->sc_ir_buf, sc->sc_ir_bufused, uio); 527 if (error) 528 sc->sc_rc6_nhb = 0; 529 } 530 break; 531 case IRMCE_STATE_CMDDATA: 532 p++; 533 sc->sc_ir_resid--; 534 if (sc->sc_ir_resid == 0) 535 sc->sc_ir_state = IRMCE_STATE_HEADER; 536 break; 537 } 538 539 } 540 541 return 0; 542 } 543 544 static int 545 irmce_read(void *priv, struct uio *uio, int flag) 546 { 547 struct irmce_softc *sc = priv; 548 usbd_status err; 549 uint32_t rlen; 550 int error = 0; 551 552 while (uio->uio_resid > 0) { 553 rlen = sc->sc_bulkin_maxpktsize; 554 err = usbd_bulk_transfer(sc->sc_bulkin_xfer, 555 sc->sc_bulkin_pipe, USBD_NO_COPY|USBD_SHORT_XFER_OK, 556 USBD_DEFAULT_TIMEOUT, sc->sc_bulkin_buffer, &rlen, 557 "irmcerd"); 558 if (err != USBD_NORMAL_COMPLETION) { 559 if (err == USBD_INTERRUPTED) 560 return EINTR; 561 else if (err == USBD_TIMEOUT) 562 continue; 563 else 564 return EIO; 565 } 566 567 if (sc->sc_raw) { 568 error = uiomove(sc->sc_bulkin_buffer, rlen, uio); 569 break; 570 } else { 571 error = irmce_process(sc, sc->sc_bulkin_buffer, 572 rlen, uio); 573 if (error) 574 break; 575 } 576 } 577 578 return error; 579 } 580 581 static int 582 irmce_write(void *priv, struct uio *uio, int flag) 583 { 584 return EIO; 585 } 586 587 static int 588 irmce_setparams(void *priv, struct cir_params *params) 589 { 590 struct irmce_softc *sc = priv; 591 592 if (params->raw > 1) 593 return EINVAL; 594 sc->sc_raw = params->raw; 595 596 return 0; 597 } 598 599 MODULE(MODULE_CLASS_DRIVER, irmce, NULL); 600 601 #ifdef _MODULE 602 #include "ioconf.c" 603 #endif 604 605 static int 606 irmce_modcmd(modcmd_t cmd, void *opaque) 607 { 608 switch (cmd) { 609 case MODULE_CMD_INIT: 610 #ifdef _MODULE 611 return config_init_component(cfdriver_ioconf_irmce, 612 cfattach_ioconf_irmce, cfdata_ioconf_irmce); 613 #else 614 return 0; 615 #endif 616 case MODULE_CMD_FINI: 617 #ifdef _MODULE 618 return config_fini_component(cfdriver_ioconf_irmce, 619 cfattach_ioconf_irmce, cfdata_ioconf_irmce); 620 #else 621 return 0; 622 #endif 623 default: 624 return ENOTTY; 625 } 626 } 627