1 /* 2 * IguanaWorks USB IR Transceiver support 3 * 4 * Copyright (C) 2012 Sean Young <sean@mess.org> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 20 21 #include <linux/device.h> 22 #include <linux/kernel.h> 23 #include <linux/module.h> 24 #include <linux/usb.h> 25 #include <linux/usb/input.h> 26 #include <linux/slab.h> 27 #include <linux/completion.h> 28 #include <media/rc-core.h> 29 30 #define DRIVER_NAME "iguanair" 31 32 struct iguanair { 33 struct rc_dev *rc; 34 35 struct device *dev; 36 struct usb_device *udev; 37 38 int pipe_out; 39 uint16_t version; 40 uint8_t bufsize; 41 42 struct mutex lock; 43 44 /* receiver support */ 45 bool receiver_on; 46 dma_addr_t dma_in; 47 uint8_t *buf_in; 48 struct urb *urb_in; 49 struct completion completion; 50 51 /* transmit support */ 52 bool tx_overflow; 53 uint32_t carrier; 54 uint8_t cycle_overhead; 55 uint8_t channels; 56 uint8_t busy4; 57 uint8_t busy7; 58 59 char name[64]; 60 char phys[64]; 61 }; 62 63 #define CMD_GET_VERSION 0x01 64 #define CMD_GET_BUFSIZE 0x11 65 #define CMD_GET_FEATURES 0x10 66 #define CMD_SEND 0x15 67 #define CMD_EXECUTE 0x1f 68 #define CMD_RX_OVERFLOW 0x31 69 #define CMD_TX_OVERFLOW 0x32 70 #define CMD_RECEIVER_ON 0x12 71 #define CMD_RECEIVER_OFF 0x14 72 73 #define DIR_IN 0xdc 74 #define DIR_OUT 0xcd 75 76 #define MAX_PACKET_SIZE 8u 77 #define TIMEOUT 1000 78 #define RX_RESOLUTION 21333 79 80 struct packet { 81 uint16_t start; 82 uint8_t direction; 83 uint8_t cmd; 84 }; 85 86 struct send_packet { 87 struct packet header; 88 uint8_t length; 89 uint8_t channels; 90 uint8_t busy7; 91 uint8_t busy4; 92 uint8_t payload[0]; 93 }; 94 95 static void process_ir_data(struct iguanair *ir, unsigned len) 96 { 97 if (len >= 4 && ir->buf_in[0] == 0 && ir->buf_in[1] == 0) { 98 switch (ir->buf_in[3]) { 99 case CMD_GET_VERSION: 100 if (len == 6) { 101 ir->version = (ir->buf_in[5] << 8) | 102 ir->buf_in[4]; 103 complete(&ir->completion); 104 } 105 break; 106 case CMD_GET_BUFSIZE: 107 if (len >= 5) { 108 ir->bufsize = ir->buf_in[4]; 109 complete(&ir->completion); 110 } 111 break; 112 case CMD_GET_FEATURES: 113 if (len > 5) { 114 ir->cycle_overhead = ir->buf_in[5]; 115 complete(&ir->completion); 116 } 117 break; 118 case CMD_TX_OVERFLOW: 119 ir->tx_overflow = true; 120 case CMD_RECEIVER_OFF: 121 case CMD_RECEIVER_ON: 122 case CMD_SEND: 123 complete(&ir->completion); 124 break; 125 case CMD_RX_OVERFLOW: 126 dev_warn(ir->dev, "receive overflow\n"); 127 ir_raw_event_reset(ir->rc); 128 break; 129 default: 130 dev_warn(ir->dev, "control code %02x received\n", 131 ir->buf_in[3]); 132 break; 133 } 134 } else if (len >= 7) { 135 DEFINE_IR_RAW_EVENT(rawir); 136 unsigned i; 137 bool event = false; 138 139 init_ir_raw_event(&rawir); 140 141 for (i = 0; i < 7; i++) { 142 if (ir->buf_in[i] == 0x80) { 143 rawir.pulse = false; 144 rawir.duration = US_TO_NS(21845); 145 } else { 146 rawir.pulse = (ir->buf_in[i] & 0x80) == 0; 147 rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) * 148 RX_RESOLUTION; 149 } 150 151 if (ir_raw_event_store_with_filter(ir->rc, &rawir)) 152 event = true; 153 } 154 155 if (event) 156 ir_raw_event_handle(ir->rc); 157 } 158 } 159 160 static void iguanair_rx(struct urb *urb) 161 { 162 struct iguanair *ir; 163 int rc; 164 165 if (!urb) 166 return; 167 168 ir = urb->context; 169 if (!ir) { 170 usb_unlink_urb(urb); 171 return; 172 } 173 174 switch (urb->status) { 175 case 0: 176 process_ir_data(ir, urb->actual_length); 177 break; 178 case -ECONNRESET: 179 case -ENOENT: 180 case -ESHUTDOWN: 181 usb_unlink_urb(urb); 182 return; 183 case -EPIPE: 184 default: 185 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status); 186 break; 187 } 188 189 rc = usb_submit_urb(urb, GFP_ATOMIC); 190 if (rc && rc != -ENODEV) 191 dev_warn(ir->dev, "failed to resubmit urb: %d\n", rc); 192 } 193 194 static int iguanair_send(struct iguanair *ir, void *data, unsigned size) 195 { 196 int rc, transferred; 197 198 INIT_COMPLETION(ir->completion); 199 200 rc = usb_interrupt_msg(ir->udev, ir->pipe_out, data, size, 201 &transferred, TIMEOUT); 202 if (rc) 203 return rc; 204 205 if (transferred != size) 206 return -EIO; 207 208 if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0) 209 return -ETIMEDOUT; 210 211 return rc; 212 } 213 214 static int iguanair_get_features(struct iguanair *ir) 215 { 216 struct packet packet; 217 int rc; 218 219 packet.start = 0; 220 packet.direction = DIR_OUT; 221 packet.cmd = CMD_GET_VERSION; 222 223 rc = iguanair_send(ir, &packet, sizeof(packet)); 224 if (rc) { 225 dev_info(ir->dev, "failed to get version\n"); 226 goto out; 227 } 228 229 if (ir->version < 0x205) { 230 dev_err(ir->dev, "firmware 0x%04x is too old\n", ir->version); 231 rc = -ENODEV; 232 goto out; 233 } 234 235 ir->bufsize = 150; 236 ir->cycle_overhead = 65; 237 238 packet.cmd = CMD_GET_BUFSIZE; 239 240 rc = iguanair_send(ir, &packet, sizeof(packet)); 241 if (rc) { 242 dev_info(ir->dev, "failed to get buffer size\n"); 243 goto out; 244 } 245 246 packet.cmd = CMD_GET_FEATURES; 247 248 rc = iguanair_send(ir, &packet, sizeof(packet)); 249 if (rc) { 250 dev_info(ir->dev, "failed to get features\n"); 251 goto out; 252 } 253 254 out: 255 return rc; 256 } 257 258 static int iguanair_receiver(struct iguanair *ir, bool enable) 259 { 260 struct packet packet = { 0, DIR_OUT, enable ? 261 CMD_RECEIVER_ON : CMD_RECEIVER_OFF }; 262 263 if (enable) 264 ir_raw_event_reset(ir->rc); 265 266 return iguanair_send(ir, &packet, sizeof(packet)); 267 } 268 269 /* 270 * The iguana ir creates the carrier by busy spinning after each pulse or 271 * space. This is counted in CPU cycles, with the CPU running at 24MHz. It is 272 * broken down into 7-cycles and 4-cyles delays, with a preference for 273 * 4-cycle delays. 274 */ 275 static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier) 276 { 277 struct iguanair *ir = dev->priv; 278 279 if (carrier < 25000 || carrier > 150000) 280 return -EINVAL; 281 282 mutex_lock(&ir->lock); 283 284 if (carrier != ir->carrier) { 285 uint32_t cycles, fours, sevens; 286 287 ir->carrier = carrier; 288 289 cycles = DIV_ROUND_CLOSEST(24000000, carrier * 2) - 290 ir->cycle_overhead; 291 292 /* make up the the remainer of 4-cycle blocks */ 293 switch (cycles & 3) { 294 case 0: 295 sevens = 0; 296 break; 297 case 1: 298 sevens = 3; 299 break; 300 case 2: 301 sevens = 2; 302 break; 303 case 3: 304 sevens = 1; 305 break; 306 } 307 308 fours = (cycles - sevens * 7) / 4; 309 310 /* magic happens here */ 311 ir->busy7 = (4 - sevens) * 2; 312 ir->busy4 = 110 - fours; 313 } 314 315 mutex_unlock(&ir->lock); 316 317 return carrier; 318 } 319 320 static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask) 321 { 322 struct iguanair *ir = dev->priv; 323 324 if (mask > 15) 325 return 4; 326 327 mutex_lock(&ir->lock); 328 ir->channels = mask; 329 mutex_unlock(&ir->lock); 330 331 return 0; 332 } 333 334 static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count) 335 { 336 struct iguanair *ir = dev->priv; 337 uint8_t space, *payload; 338 unsigned i, size, rc, bytes; 339 struct send_packet *packet; 340 341 mutex_lock(&ir->lock); 342 343 /* convert from us to carrier periods */ 344 for (i = size = 0; i < count; i++) { 345 txbuf[i] = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000); 346 bytes = (txbuf[i] + 126) / 127; 347 if (size + bytes > ir->bufsize) { 348 count = i; 349 break; 350 } 351 size += bytes; 352 } 353 354 if (count == 0) { 355 rc = -EINVAL; 356 goto out; 357 } 358 359 packet = kmalloc(sizeof(*packet) + size, GFP_KERNEL); 360 if (!packet) { 361 rc = -ENOMEM; 362 goto out; 363 } 364 365 packet->header.start = 0; 366 packet->header.direction = DIR_OUT; 367 packet->header.cmd = CMD_SEND; 368 packet->length = size; 369 packet->channels = ir->channels << 4; 370 packet->busy7 = ir->busy7; 371 packet->busy4 = ir->busy4; 372 373 space = 0; 374 payload = packet->payload; 375 376 for (i = 0; i < count; i++) { 377 unsigned periods = txbuf[i]; 378 379 while (periods > 127) { 380 *payload++ = 127 | space; 381 periods -= 127; 382 } 383 384 *payload++ = periods | space; 385 space ^= 0x80; 386 } 387 388 if (ir->receiver_on) { 389 rc = iguanair_receiver(ir, false); 390 if (rc) { 391 dev_warn(ir->dev, "disable receiver before transmit failed\n"); 392 goto out_kfree; 393 } 394 } 395 396 ir->tx_overflow = false; 397 398 rc = iguanair_send(ir, packet, size + 8); 399 400 if (rc == 0 && ir->tx_overflow) 401 rc = -EOVERFLOW; 402 403 if (ir->receiver_on) { 404 if (iguanair_receiver(ir, true)) 405 dev_warn(ir->dev, "re-enable receiver after transmit failed\n"); 406 } 407 408 out_kfree: 409 kfree(packet); 410 out: 411 mutex_unlock(&ir->lock); 412 413 return rc ? rc : count; 414 } 415 416 static int iguanair_open(struct rc_dev *rdev) 417 { 418 struct iguanair *ir = rdev->priv; 419 int rc; 420 421 mutex_lock(&ir->lock); 422 423 BUG_ON(ir->receiver_on); 424 425 rc = iguanair_receiver(ir, true); 426 if (rc == 0) 427 ir->receiver_on = true; 428 429 mutex_unlock(&ir->lock); 430 431 return rc; 432 } 433 434 static void iguanair_close(struct rc_dev *rdev) 435 { 436 struct iguanair *ir = rdev->priv; 437 int rc; 438 439 mutex_lock(&ir->lock); 440 441 rc = iguanair_receiver(ir, false); 442 ir->receiver_on = false; 443 if (rc && rc != -ENODEV) 444 dev_warn(ir->dev, "failed to disable receiver: %d\n", rc); 445 446 mutex_unlock(&ir->lock); 447 } 448 449 static int __devinit iguanair_probe(struct usb_interface *intf, 450 const struct usb_device_id *id) 451 { 452 struct usb_device *udev = interface_to_usbdev(intf); 453 struct iguanair *ir; 454 struct rc_dev *rc; 455 int ret, pipein; 456 struct usb_host_interface *idesc; 457 458 ir = kzalloc(sizeof(*ir), GFP_KERNEL); 459 rc = rc_allocate_device(); 460 if (!ir || !rc) { 461 ret = -ENOMEM; 462 goto out; 463 } 464 465 ir->buf_in = usb_alloc_coherent(udev, MAX_PACKET_SIZE, GFP_KERNEL, 466 &ir->dma_in); 467 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL); 468 469 if (!ir->buf_in || !ir->urb_in) { 470 ret = -ENOMEM; 471 goto out; 472 } 473 474 idesc = intf->altsetting; 475 476 if (idesc->desc.bNumEndpoints < 2) { 477 ret = -ENODEV; 478 goto out; 479 } 480 481 ir->rc = rc; 482 ir->dev = &intf->dev; 483 ir->udev = udev; 484 ir->pipe_out = usb_sndintpipe(udev, 485 idesc->endpoint[1].desc.bEndpointAddress); 486 mutex_init(&ir->lock); 487 init_completion(&ir->completion); 488 489 pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress); 490 usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in, MAX_PACKET_SIZE, 491 iguanair_rx, ir, 1); 492 ir->urb_in->transfer_dma = ir->dma_in; 493 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 494 495 ret = usb_submit_urb(ir->urb_in, GFP_KERNEL); 496 if (ret) { 497 dev_warn(&intf->dev, "failed to submit urb: %d\n", ret); 498 goto out; 499 } 500 501 ret = iguanair_get_features(ir); 502 if (ret) 503 goto out2; 504 505 snprintf(ir->name, sizeof(ir->name), 506 "IguanaWorks USB IR Transceiver version 0x%04x", ir->version); 507 508 usb_make_path(ir->udev, ir->phys, sizeof(ir->phys)); 509 510 rc->input_name = ir->name; 511 rc->input_phys = ir->phys; 512 usb_to_input_id(ir->udev, &rc->input_id); 513 rc->dev.parent = &intf->dev; 514 rc->driver_type = RC_DRIVER_IR_RAW; 515 rc->allowed_protos = RC_TYPE_ALL; 516 rc->priv = ir; 517 rc->open = iguanair_open; 518 rc->close = iguanair_close; 519 rc->s_tx_mask = iguanair_set_tx_mask; 520 rc->s_tx_carrier = iguanair_set_tx_carrier; 521 rc->tx_ir = iguanair_tx; 522 rc->driver_name = DRIVER_NAME; 523 rc->map_name = RC_MAP_RC6_MCE; 524 rc->timeout = MS_TO_NS(100); 525 rc->rx_resolution = RX_RESOLUTION; 526 527 iguanair_set_tx_carrier(rc, 38000); 528 529 ret = rc_register_device(rc); 530 if (ret < 0) { 531 dev_err(&intf->dev, "failed to register rc device %d", ret); 532 goto out2; 533 } 534 535 usb_set_intfdata(intf, ir); 536 537 return 0; 538 out2: 539 usb_kill_urb(ir->urb_in); 540 out: 541 if (ir) { 542 usb_free_urb(ir->urb_in); 543 usb_free_coherent(udev, MAX_PACKET_SIZE, ir->buf_in, 544 ir->dma_in); 545 } 546 rc_free_device(rc); 547 kfree(ir); 548 return ret; 549 } 550 551 static void __devexit iguanair_disconnect(struct usb_interface *intf) 552 { 553 struct iguanair *ir = usb_get_intfdata(intf); 554 555 rc_unregister_device(ir->rc); 556 usb_set_intfdata(intf, NULL); 557 usb_kill_urb(ir->urb_in); 558 usb_free_urb(ir->urb_in); 559 usb_free_coherent(ir->udev, MAX_PACKET_SIZE, ir->buf_in, ir->dma_in); 560 kfree(ir); 561 } 562 563 static int iguanair_suspend(struct usb_interface *intf, pm_message_t message) 564 { 565 struct iguanair *ir = usb_get_intfdata(intf); 566 int rc = 0; 567 568 mutex_lock(&ir->lock); 569 570 if (ir->receiver_on) { 571 rc = iguanair_receiver(ir, false); 572 if (rc) 573 dev_warn(ir->dev, "failed to disable receiver for suspend\n"); 574 } 575 576 usb_kill_urb(ir->urb_in); 577 578 mutex_unlock(&ir->lock); 579 580 return rc; 581 } 582 583 static int iguanair_resume(struct usb_interface *intf) 584 { 585 struct iguanair *ir = usb_get_intfdata(intf); 586 int rc = 0; 587 588 mutex_lock(&ir->lock); 589 590 rc = usb_submit_urb(ir->urb_in, GFP_KERNEL); 591 if (rc) 592 dev_warn(&intf->dev, "failed to submit urb: %d\n", rc); 593 594 if (ir->receiver_on) { 595 rc = iguanair_receiver(ir, true); 596 if (rc) 597 dev_warn(ir->dev, "failed to enable receiver after resume\n"); 598 } 599 600 mutex_unlock(&ir->lock); 601 602 return rc; 603 } 604 605 static const struct usb_device_id iguanair_table[] = { 606 { USB_DEVICE(0x1781, 0x0938) }, 607 { } 608 }; 609 610 static struct usb_driver iguanair_driver = { 611 .name = DRIVER_NAME, 612 .probe = iguanair_probe, 613 .disconnect = __devexit_p(iguanair_disconnect), 614 .suspend = iguanair_suspend, 615 .resume = iguanair_resume, 616 .reset_resume = iguanair_resume, 617 .id_table = iguanair_table, 618 .soft_unbind = 1 /* we want to disable receiver on unbind */ 619 }; 620 621 module_usb_driver(iguanair_driver); 622 623 MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver"); 624 MODULE_AUTHOR("Sean Young <sean@mess.org>"); 625 MODULE_LICENSE("GPL"); 626 MODULE_DEVICE_TABLE(usb, iguanair_table); 627 628