1 /* 2 * USB RedRat3 IR Transceiver rc-core driver 3 * 4 * Copyright (c) 2011 by Jarod Wilson <jarod@redhat.com> 5 * based heavily on the work of Stephen Cox, with additional 6 * help from RedRat Ltd. 7 * 8 * This driver began life based an an old version of the first-generation 9 * lirc_mceusb driver from the lirc 0.7.2 distribution. It was then 10 * significantly rewritten by Stephen Cox with the aid of RedRat Ltd's 11 * Chris Dodge. 12 * 13 * The driver was then ported to rc-core and significantly rewritten again, 14 * by Jarod, using the in-kernel mceusb driver as a guide, after an initial 15 * port effort was started by Stephen. 16 * 17 * TODO LIST: 18 * - fix lirc not showing repeats properly 19 * -- 20 * 21 * The RedRat3 is a USB transceiver with both send & receive, 22 * with 2 separate sensors available for receive to enable 23 * both good long range reception for general use, and good 24 * short range reception when required for learning a signal. 25 * 26 * http://www.redrat.co.uk/ 27 * 28 * It uses its own little protocol to communicate, the required 29 * parts of which are embedded within this driver. 30 * -- 31 * 32 * This program is free software; you can redistribute it and/or modify 33 * it under the terms of the GNU General Public License as published by 34 * the Free Software Foundation; either version 2 of the License, or 35 * (at your option) any later version. 36 * 37 * This program is distributed in the hope that it will be useful, 38 * but WITHOUT ANY WARRANTY; without even the implied warranty of 39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 40 * GNU General Public License for more details. 41 * 42 * You should have received a copy of the GNU General Public License 43 * along with this program; if not, write to the Free Software 44 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 45 * 46 */ 47 48 #include <asm/unaligned.h> 49 #include <linux/device.h> 50 #include <linux/module.h> 51 #include <linux/slab.h> 52 #include <linux/usb.h> 53 #include <linux/usb/input.h> 54 #include <media/rc-core.h> 55 56 /* Driver Information */ 57 #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>" 58 #define DRIVER_AUTHOR2 "The Dweller, Stephen Cox" 59 #define DRIVER_DESC "RedRat3 USB IR Transceiver Driver" 60 #define DRIVER_NAME "redrat3" 61 62 /* module parameters */ 63 #ifdef CONFIG_USB_DEBUG 64 static int debug = 1; 65 #else 66 static int debug; 67 #endif 68 69 #define RR3_DEBUG_STANDARD 0x1 70 #define RR3_DEBUG_FUNCTION_TRACE 0x2 71 72 #define rr3_dbg(dev, fmt, ...) \ 73 do { \ 74 if (debug & RR3_DEBUG_STANDARD) \ 75 dev_info(dev, fmt, ## __VA_ARGS__); \ 76 } while (0) 77 78 #define rr3_ftr(dev, fmt, ...) \ 79 do { \ 80 if (debug & RR3_DEBUG_FUNCTION_TRACE) \ 81 dev_info(dev, fmt, ## __VA_ARGS__); \ 82 } while (0) 83 84 /* bulk data transfer types */ 85 #define RR3_ERROR 0x01 86 #define RR3_MOD_SIGNAL_IN 0x20 87 #define RR3_MOD_SIGNAL_OUT 0x21 88 89 /* Get the RR firmware version */ 90 #define RR3_FW_VERSION 0xb1 91 #define RR3_FW_VERSION_LEN 64 92 /* Send encoded signal bulk-sent earlier*/ 93 #define RR3_TX_SEND_SIGNAL 0xb3 94 #define RR3_SET_IR_PARAM 0xb7 95 #define RR3_GET_IR_PARAM 0xb8 96 /* Blink the red LED on the device */ 97 #define RR3_BLINK_LED 0xb9 98 /* Read serial number of device */ 99 #define RR3_READ_SER_NO 0xba 100 #define RR3_SER_NO_LEN 4 101 /* Start capture with the RC receiver */ 102 #define RR3_RC_DET_ENABLE 0xbb 103 /* Stop capture with the RC receiver */ 104 #define RR3_RC_DET_DISABLE 0xbc 105 /* Return the status of RC detector capture */ 106 #define RR3_RC_DET_STATUS 0xbd 107 /* Reset redrat */ 108 #define RR3_RESET 0xa0 109 110 /* Max number of lengths in the signal. */ 111 #define RR3_IR_IO_MAX_LENGTHS 0x01 112 /* Periods to measure mod. freq. */ 113 #define RR3_IR_IO_PERIODS_MF 0x02 114 /* Size of memory for main signal data */ 115 #define RR3_IR_IO_SIG_MEM_SIZE 0x03 116 /* Delta value when measuring lengths */ 117 #define RR3_IR_IO_LENGTH_FUZZ 0x04 118 /* Timeout for end of signal detection */ 119 #define RR3_IR_IO_SIG_TIMEOUT 0x05 120 /* Minumum value for pause recognition. */ 121 #define RR3_IR_IO_MIN_PAUSE 0x06 122 123 /* Clock freq. of EZ-USB chip */ 124 #define RR3_CLK 24000000 125 /* Clock periods per timer count */ 126 #define RR3_CLK_PER_COUNT 12 127 /* (RR3_CLK / RR3_CLK_PER_COUNT) */ 128 #define RR3_CLK_CONV_FACTOR 2000000 129 /* USB bulk-in IR data endpoint address */ 130 #define RR3_BULK_IN_EP_ADDR 0x82 131 132 /* Size of the fixed-length portion of the signal */ 133 #define RR3_DRIVER_MAXLENS 128 134 #define RR3_MAX_SIG_SIZE 512 135 #define RR3_TIME_UNIT 50 136 #define RR3_END_OF_SIGNAL 0x7f 137 #define RR3_TX_TRAILER_LEN 2 138 #define RR3_RX_MIN_TIMEOUT 5 139 #define RR3_RX_MAX_TIMEOUT 2000 140 141 /* The 8051's CPUCS Register address */ 142 #define RR3_CPUCS_REG_ADDR 0x7f92 143 144 #define USB_RR3USB_VENDOR_ID 0x112a 145 #define USB_RR3USB_PRODUCT_ID 0x0001 146 #define USB_RR3IIUSB_PRODUCT_ID 0x0005 147 148 struct redrat3_header { 149 __be16 length; 150 __be16 transfer_type; 151 } __packed; 152 153 /* sending and receiving irdata */ 154 struct redrat3_irdata { 155 struct redrat3_header header; 156 __be32 pause; 157 __be16 mod_freq_count; 158 __be16 num_periods; 159 __u8 max_lengths; 160 __u8 no_lengths; 161 __be16 max_sig_size; 162 __be16 sig_size; 163 __u8 no_repeats; 164 __be16 lens[RR3_DRIVER_MAXLENS]; /* not aligned */ 165 __u8 sigdata[RR3_MAX_SIG_SIZE]; 166 } __packed; 167 168 /* firmware errors */ 169 struct redrat3_error { 170 struct redrat3_header header; 171 __be16 fw_error; 172 } __packed; 173 174 /* table of devices that work with this driver */ 175 static struct usb_device_id redrat3_dev_table[] = { 176 /* Original version of the RedRat3 */ 177 {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)}, 178 /* Second Version/release of the RedRat3 - RetRat3-II */ 179 {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)}, 180 {} /* Terminating entry */ 181 }; 182 183 /* Structure to hold all of our device specific stuff */ 184 struct redrat3_dev { 185 /* core device bits */ 186 struct rc_dev *rc; 187 struct device *dev; 188 189 /* save off the usb device pointer */ 190 struct usb_device *udev; 191 192 /* the receive endpoint */ 193 struct usb_endpoint_descriptor *ep_in; 194 /* the buffer to receive data */ 195 void *bulk_in_buf; 196 /* urb used to read ir data */ 197 struct urb *read_urb; 198 199 /* the send endpoint */ 200 struct usb_endpoint_descriptor *ep_out; 201 202 /* usb dma */ 203 dma_addr_t dma_in; 204 205 /* rx signal timeout timer */ 206 struct timer_list rx_timeout; 207 u32 hw_timeout; 208 209 /* is the detector enabled*/ 210 bool det_enabled; 211 /* Is the device currently transmitting?*/ 212 bool transmitting; 213 214 /* store for current packet */ 215 struct redrat3_irdata irdata; 216 u16 bytes_read; 217 218 u32 carrier; 219 220 char name[64]; 221 char phys[64]; 222 }; 223 224 /* 225 * redrat3_issue_async 226 * 227 * Issues an async read to the ir data in port.. 228 * sets the callback to be redrat3_handle_async 229 */ 230 static void redrat3_issue_async(struct redrat3_dev *rr3) 231 { 232 int res; 233 234 rr3_ftr(rr3->dev, "Entering %s\n", __func__); 235 236 res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC); 237 if (res) 238 rr3_dbg(rr3->dev, "%s: receive request FAILED! " 239 "(res %d, len %d)\n", __func__, res, 240 rr3->read_urb->transfer_buffer_length); 241 } 242 243 static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code) 244 { 245 if (!rr3->transmitting && (code != 0x40)) 246 dev_info(rr3->dev, "fw error code 0x%02x: ", code); 247 248 switch (code) { 249 case 0x00: 250 pr_cont("No Error\n"); 251 break; 252 253 /* Codes 0x20 through 0x2f are IR Firmware Errors */ 254 case 0x20: 255 pr_cont("Initial signal pulse not long enough " 256 "to measure carrier frequency\n"); 257 break; 258 case 0x21: 259 pr_cont("Not enough length values allocated for signal\n"); 260 break; 261 case 0x22: 262 pr_cont("Not enough memory allocated for signal data\n"); 263 break; 264 case 0x23: 265 pr_cont("Too many signal repeats\n"); 266 break; 267 case 0x28: 268 pr_cont("Insufficient memory available for IR signal " 269 "data memory allocation\n"); 270 break; 271 case 0x29: 272 pr_cont("Insufficient memory available " 273 "for IrDa signal data memory allocation\n"); 274 break; 275 276 /* Codes 0x30 through 0x3f are USB Firmware Errors */ 277 case 0x30: 278 pr_cont("Insufficient memory available for bulk " 279 "transfer structure\n"); 280 break; 281 282 /* 283 * Other error codes... These are primarily errors that can occur in 284 * the control messages sent to the redrat 285 */ 286 case 0x40: 287 if (!rr3->transmitting) 288 pr_cont("Signal capture has been terminated\n"); 289 break; 290 case 0x41: 291 pr_cont("Attempt to set/get and unknown signal I/O " 292 "algorithm parameter\n"); 293 break; 294 case 0x42: 295 pr_cont("Signal capture already started\n"); 296 break; 297 298 default: 299 pr_cont("Unknown Error\n"); 300 break; 301 } 302 } 303 304 static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata) 305 { 306 u32 mod_freq = 0; 307 u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count); 308 309 if (mod_freq_count != 0) 310 mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) / 311 (mod_freq_count * RR3_CLK_PER_COUNT); 312 313 return mod_freq; 314 } 315 316 /* this function scales down the figures for the same result... */ 317 static u32 redrat3_len_to_us(u32 length) 318 { 319 u32 biglen = length * 1000; 320 u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000; 321 u32 result = (u32) (biglen / divisor); 322 323 /* don't allow zero lengths to go back, breaks lirc */ 324 return result ? result : 1; 325 } 326 327 /* 328 * convert us back into redrat3 lengths 329 * 330 * length * 1000 length * 1000000 331 * ------------- = ---------------- = micro 332 * rr3clk / 1000 rr3clk 333 334 * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000 335 * ----- = 4 ----- = 6 -------------- = len --------------------- 336 * 3 2 1000000 1000 337 */ 338 static u32 redrat3_us_to_len(u32 microsec) 339 { 340 u32 result; 341 u32 divisor; 342 343 microsec &= IR_MAX_DURATION; 344 divisor = (RR3_CLK_CONV_FACTOR / 1000); 345 result = (u32)(microsec * divisor) / 1000; 346 347 /* don't allow zero lengths to go back, breaks lirc */ 348 return result ? result : 1; 349 } 350 351 /* timer callback to send reset event */ 352 static void redrat3_rx_timeout(unsigned long data) 353 { 354 struct redrat3_dev *rr3 = (struct redrat3_dev *)data; 355 356 rr3_dbg(rr3->dev, "calling ir_raw_event_reset\n"); 357 ir_raw_event_reset(rr3->rc); 358 } 359 360 static void redrat3_process_ir_data(struct redrat3_dev *rr3) 361 { 362 DEFINE_IR_RAW_EVENT(rawir); 363 struct device *dev; 364 unsigned i, trailer = 0; 365 unsigned sig_size, single_len, offset, val; 366 unsigned long delay; 367 u32 mod_freq; 368 369 if (!rr3) { 370 pr_err("%s called with no context!\n", __func__); 371 return; 372 } 373 374 rr3_ftr(rr3->dev, "Entered %s\n", __func__); 375 376 dev = rr3->dev; 377 378 /* Make sure we reset the IR kfifo after a bit of inactivity */ 379 delay = usecs_to_jiffies(rr3->hw_timeout); 380 mod_timer(&rr3->rx_timeout, jiffies + delay); 381 382 mod_freq = redrat3_val_to_mod_freq(&rr3->irdata); 383 rr3_dbg(dev, "Got mod_freq of %u\n", mod_freq); 384 385 /* process each rr3 encoded byte into an int */ 386 sig_size = be16_to_cpu(rr3->irdata.sig_size); 387 for (i = 0; i < sig_size; i++) { 388 offset = rr3->irdata.sigdata[i]; 389 val = get_unaligned_be16(&rr3->irdata.lens[offset]); 390 single_len = redrat3_len_to_us(val); 391 392 /* we should always get pulse/space/pulse/space samples */ 393 if (i % 2) 394 rawir.pulse = false; 395 else 396 rawir.pulse = true; 397 398 rawir.duration = US_TO_NS(single_len); 399 /* Save initial pulse length to fudge trailer */ 400 if (i == 0) 401 trailer = rawir.duration; 402 /* cap the value to IR_MAX_DURATION */ 403 rawir.duration &= IR_MAX_DURATION; 404 405 rr3_dbg(dev, "storing %s with duration %d (i: %d)\n", 406 rawir.pulse ? "pulse" : "space", rawir.duration, i); 407 ir_raw_event_store_with_filter(rr3->rc, &rawir); 408 } 409 410 /* add a trailing space, if need be */ 411 if (i % 2) { 412 rawir.pulse = false; 413 /* this duration is made up, and may not be ideal... */ 414 if (trailer < US_TO_NS(1000)) 415 rawir.duration = US_TO_NS(2800); 416 else 417 rawir.duration = trailer; 418 rr3_dbg(dev, "storing trailing space with duration %d\n", 419 rawir.duration); 420 ir_raw_event_store_with_filter(rr3->rc, &rawir); 421 } 422 423 rr3_dbg(dev, "calling ir_raw_event_handle\n"); 424 ir_raw_event_handle(rr3->rc); 425 } 426 427 /* Util fn to send rr3 cmds */ 428 static u8 redrat3_send_cmd(int cmd, struct redrat3_dev *rr3) 429 { 430 struct usb_device *udev; 431 u8 *data; 432 int res; 433 434 data = kzalloc(sizeof(u8), GFP_KERNEL); 435 if (!data) 436 return -ENOMEM; 437 438 udev = rr3->udev; 439 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd, 440 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 441 0x0000, 0x0000, data, sizeof(u8), HZ * 10); 442 443 if (res < 0) { 444 dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d", 445 __func__, res, *data); 446 res = -EIO; 447 } else 448 res = data[0]; 449 450 kfree(data); 451 452 return res; 453 } 454 455 /* Enables the long range detector and starts async receive */ 456 static int redrat3_enable_detector(struct redrat3_dev *rr3) 457 { 458 struct device *dev = rr3->dev; 459 u8 ret; 460 461 rr3_ftr(dev, "Entering %s\n", __func__); 462 463 ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3); 464 if (ret != 0) 465 dev_dbg(dev, "%s: unexpected ret of %d\n", 466 __func__, ret); 467 468 ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3); 469 if (ret != 1) { 470 dev_err(dev, "%s: detector status: %d, should be 1\n", 471 __func__, ret); 472 return -EIO; 473 } 474 475 rr3->det_enabled = true; 476 redrat3_issue_async(rr3); 477 478 return 0; 479 } 480 481 /* Disables the rr3 long range detector */ 482 static void redrat3_disable_detector(struct redrat3_dev *rr3) 483 { 484 struct device *dev = rr3->dev; 485 u8 ret; 486 487 rr3_ftr(dev, "Entering %s\n", __func__); 488 489 ret = redrat3_send_cmd(RR3_RC_DET_DISABLE, rr3); 490 if (ret != 0) 491 dev_err(dev, "%s: failure!\n", __func__); 492 493 ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3); 494 if (ret != 0) 495 dev_warn(dev, "%s: detector status: %d, should be 0\n", 496 __func__, ret); 497 498 rr3->det_enabled = false; 499 } 500 501 static inline void redrat3_delete(struct redrat3_dev *rr3, 502 struct usb_device *udev) 503 { 504 rr3_ftr(rr3->dev, "%s cleaning up\n", __func__); 505 usb_kill_urb(rr3->read_urb); 506 507 usb_free_urb(rr3->read_urb); 508 509 usb_free_coherent(udev, le16_to_cpu(rr3->ep_in->wMaxPacketSize), 510 rr3->bulk_in_buf, rr3->dma_in); 511 512 kfree(rr3); 513 } 514 515 static u32 redrat3_get_timeout(struct redrat3_dev *rr3) 516 { 517 __be32 *tmp; 518 u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */ 519 int len, ret, pipe; 520 521 len = sizeof(*tmp); 522 tmp = kzalloc(len, GFP_KERNEL); 523 if (!tmp) { 524 dev_warn(rr3->dev, "Memory allocation faillure\n"); 525 return timeout; 526 } 527 528 pipe = usb_rcvctrlpipe(rr3->udev, 0); 529 ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM, 530 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 531 RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5); 532 if (ret != len) 533 dev_warn(rr3->dev, "Failed to read timeout from hardware\n"); 534 else { 535 timeout = redrat3_len_to_us(be32_to_cpup(tmp)); 536 537 rr3_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000); 538 } 539 540 kfree(tmp); 541 542 return timeout; 543 } 544 545 static void redrat3_reset(struct redrat3_dev *rr3) 546 { 547 struct usb_device *udev = rr3->udev; 548 struct device *dev = rr3->dev; 549 int rc, rxpipe, txpipe; 550 u8 *val; 551 int len = sizeof(u8); 552 553 rr3_ftr(dev, "Entering %s\n", __func__); 554 555 rxpipe = usb_rcvctrlpipe(udev, 0); 556 txpipe = usb_sndctrlpipe(udev, 0); 557 558 val = kmalloc(len, GFP_KERNEL); 559 if (!val) { 560 dev_err(dev, "Memory allocation failure\n"); 561 return; 562 } 563 564 *val = 0x01; 565 rc = usb_control_msg(udev, rxpipe, RR3_RESET, 566 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 567 RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25); 568 rr3_dbg(dev, "reset returned 0x%02x\n", rc); 569 570 *val = 5; 571 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, 572 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, 573 RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25); 574 rr3_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc); 575 576 *val = RR3_DRIVER_MAXLENS; 577 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, 578 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, 579 RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25); 580 rr3_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc); 581 582 kfree(val); 583 } 584 585 static void redrat3_get_firmware_rev(struct redrat3_dev *rr3) 586 { 587 int rc = 0; 588 char *buffer; 589 590 rr3_ftr(rr3->dev, "Entering %s\n", __func__); 591 592 buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL); 593 if (!buffer) { 594 dev_err(rr3->dev, "Memory allocation failure\n"); 595 return; 596 } 597 598 rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0), 599 RR3_FW_VERSION, 600 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 601 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5); 602 603 if (rc >= 0) 604 dev_info(rr3->dev, "Firmware rev: %s", buffer); 605 else 606 dev_err(rr3->dev, "Problem fetching firmware ID\n"); 607 608 kfree(buffer); 609 rr3_ftr(rr3->dev, "Exiting %s\n", __func__); 610 } 611 612 static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len) 613 { 614 struct redrat3_header *header = rr3->bulk_in_buf; 615 unsigned pktlen, pkttype; 616 617 rr3_ftr(rr3->dev, "Entering %s\n", __func__); 618 619 /* grab the Length and type of transfer */ 620 pktlen = be16_to_cpu(header->length); 621 pkttype = be16_to_cpu(header->transfer_type); 622 623 if (pktlen > sizeof(rr3->irdata)) { 624 dev_warn(rr3->dev, "packet length %u too large\n", pktlen); 625 return; 626 } 627 628 switch (pkttype) { 629 case RR3_ERROR: 630 if (len >= sizeof(struct redrat3_error)) { 631 struct redrat3_error *error = rr3->bulk_in_buf; 632 unsigned fw_error = be16_to_cpu(error->fw_error); 633 redrat3_dump_fw_error(rr3, fw_error); 634 } 635 break; 636 637 case RR3_MOD_SIGNAL_IN: 638 memcpy(&rr3->irdata, rr3->bulk_in_buf, len); 639 rr3->bytes_read = len; 640 rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", 641 rr3->bytes_read, pktlen); 642 break; 643 644 default: 645 rr3_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n", 646 pkttype, len, pktlen); 647 break; 648 } 649 } 650 651 static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len) 652 { 653 void *irdata = &rr3->irdata; 654 655 rr3_ftr(rr3->dev, "Entering %s\n", __func__); 656 657 if (len + rr3->bytes_read > sizeof(rr3->irdata)) { 658 dev_warn(rr3->dev, "too much data for packet\n"); 659 rr3->bytes_read = 0; 660 return; 661 } 662 663 memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len); 664 665 rr3->bytes_read += len; 666 rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read, 667 be16_to_cpu(rr3->irdata.header.length)); 668 } 669 670 /* gather IR data from incoming urb, process it when we have enough */ 671 static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len) 672 { 673 struct device *dev = rr3->dev; 674 unsigned pkttype; 675 int ret = 0; 676 677 rr3_ftr(dev, "Entering %s\n", __func__); 678 679 if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) { 680 redrat3_read_packet_start(rr3, len); 681 } else if (rr3->bytes_read != 0) { 682 redrat3_read_packet_continue(rr3, len); 683 } else if (rr3->bytes_read == 0) { 684 dev_err(dev, "error: no packet data read\n"); 685 ret = -ENODATA; 686 goto out; 687 } 688 689 if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length)) 690 /* we're still accumulating data */ 691 return 0; 692 693 /* if we get here, we've got IR data to decode */ 694 pkttype = be16_to_cpu(rr3->irdata.header.transfer_type); 695 if (pkttype == RR3_MOD_SIGNAL_IN) 696 redrat3_process_ir_data(rr3); 697 else 698 rr3_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n", 699 pkttype); 700 701 out: 702 rr3->bytes_read = 0; 703 return ret; 704 } 705 706 /* callback function from USB when async USB request has completed */ 707 static void redrat3_handle_async(struct urb *urb) 708 { 709 struct redrat3_dev *rr3; 710 int ret; 711 712 if (!urb) 713 return; 714 715 rr3 = urb->context; 716 if (!rr3) { 717 pr_err("%s called with invalid context!\n", __func__); 718 usb_unlink_urb(urb); 719 return; 720 } 721 722 rr3_ftr(rr3->dev, "Entering %s\n", __func__); 723 724 switch (urb->status) { 725 case 0: 726 ret = redrat3_get_ir_data(rr3, urb->actual_length); 727 if (!ret) { 728 /* no error, prepare to read more */ 729 redrat3_issue_async(rr3); 730 } 731 break; 732 733 case -ECONNRESET: 734 case -ENOENT: 735 case -ESHUTDOWN: 736 usb_unlink_urb(urb); 737 return; 738 739 case -EPIPE: 740 default: 741 dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status); 742 rr3->bytes_read = 0; 743 break; 744 } 745 } 746 747 static u16 mod_freq_to_val(unsigned int mod_freq) 748 { 749 int mult = 6000000; 750 751 /* Clk used in mod. freq. generation is CLK24/4. */ 752 return 65536 - (mult / mod_freq); 753 } 754 755 static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier) 756 { 757 struct redrat3_dev *rr3 = rcdev->priv; 758 struct device *dev = rr3->dev; 759 760 rr3_dbg(dev, "Setting modulation frequency to %u", carrier); 761 if (carrier == 0) 762 return -EINVAL; 763 764 rr3->carrier = carrier; 765 766 return carrier; 767 } 768 769 static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf, 770 unsigned count) 771 { 772 struct redrat3_dev *rr3 = rcdev->priv; 773 struct device *dev = rr3->dev; 774 struct redrat3_irdata *irdata = NULL; 775 int ret, ret_len; 776 int lencheck, cur_sample_len, pipe; 777 int *sample_lens = NULL; 778 u8 curlencheck = 0; 779 unsigned i, sendbuf_len; 780 781 rr3_ftr(dev, "Entering %s\n", __func__); 782 783 if (rr3->transmitting) { 784 dev_warn(dev, "%s: transmitter already in use\n", __func__); 785 return -EAGAIN; 786 } 787 788 count = min_t(unsigned, count, RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN); 789 790 /* rr3 will disable rc detector on transmit */ 791 rr3->det_enabled = false; 792 rr3->transmitting = true; 793 794 sample_lens = kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS, GFP_KERNEL); 795 if (!sample_lens) { 796 ret = -ENOMEM; 797 goto out; 798 } 799 800 irdata = kzalloc(sizeof(*irdata), GFP_KERNEL); 801 if (!irdata) { 802 ret = -ENOMEM; 803 goto out; 804 } 805 806 for (i = 0; i < count; i++) { 807 cur_sample_len = redrat3_us_to_len(txbuf[i]); 808 if (cur_sample_len > 0xffff) { 809 dev_warn(dev, "transmit period of %uus truncated to %uus\n", 810 txbuf[i], redrat3_len_to_us(0xffff)); 811 cur_sample_len = 0xffff; 812 } 813 for (lencheck = 0; lencheck < curlencheck; lencheck++) { 814 if (sample_lens[lencheck] == cur_sample_len) 815 break; 816 } 817 if (lencheck == curlencheck) { 818 rr3_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n", 819 i, txbuf[i], curlencheck, cur_sample_len); 820 if (curlencheck < RR3_DRIVER_MAXLENS) { 821 /* now convert the value to a proper 822 * rr3 value.. */ 823 sample_lens[curlencheck] = cur_sample_len; 824 put_unaligned_be16(cur_sample_len, 825 &irdata->lens[curlencheck]); 826 curlencheck++; 827 } else { 828 count = i - 1; 829 break; 830 } 831 } 832 irdata->sigdata[i] = lencheck; 833 } 834 835 irdata->sigdata[count] = RR3_END_OF_SIGNAL; 836 irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL; 837 838 sendbuf_len = offsetof(struct redrat3_irdata, 839 sigdata[count + RR3_TX_TRAILER_LEN]); 840 /* fill in our packet header */ 841 irdata->header.length = cpu_to_be16(sendbuf_len - 842 sizeof(struct redrat3_header)); 843 irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT); 844 irdata->pause = cpu_to_be32(redrat3_len_to_us(100)); 845 irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier)); 846 irdata->no_lengths = curlencheck; 847 irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN); 848 849 pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress); 850 ret = usb_bulk_msg(rr3->udev, pipe, irdata, 851 sendbuf_len, &ret_len, 10 * HZ); 852 rr3_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret); 853 854 /* now tell the hardware to transmit what we sent it */ 855 pipe = usb_rcvctrlpipe(rr3->udev, 0); 856 ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL, 857 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 858 0, 0, irdata, 2, HZ * 10); 859 860 if (ret < 0) 861 dev_err(dev, "Error: control msg send failed, rc %d\n", ret); 862 else 863 ret = count; 864 865 out: 866 kfree(sample_lens); 867 kfree(irdata); 868 869 rr3->transmitting = false; 870 /* rr3 re-enables rc detector because it was enabled before */ 871 rr3->det_enabled = true; 872 873 return ret; 874 } 875 876 static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3) 877 { 878 struct device *dev = rr3->dev; 879 struct rc_dev *rc; 880 int ret = -ENODEV; 881 u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct); 882 883 rc = rc_allocate_device(); 884 if (!rc) { 885 dev_err(dev, "remote input dev allocation failed\n"); 886 goto out; 887 } 888 889 snprintf(rr3->name, sizeof(rr3->name), "RedRat3%s " 890 "Infrared Remote Transceiver (%04x:%04x)", 891 prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "", 892 le16_to_cpu(rr3->udev->descriptor.idVendor), prod); 893 894 usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys)); 895 896 rc->input_name = rr3->name; 897 rc->input_phys = rr3->phys; 898 usb_to_input_id(rr3->udev, &rc->input_id); 899 rc->dev.parent = dev; 900 rc->priv = rr3; 901 rc->driver_type = RC_DRIVER_IR_RAW; 902 rc->allowed_protos = RC_BIT_ALL; 903 rc->timeout = US_TO_NS(2750); 904 rc->tx_ir = redrat3_transmit_ir; 905 rc->s_tx_carrier = redrat3_set_tx_carrier; 906 rc->driver_name = DRIVER_NAME; 907 rc->rx_resolution = US_TO_NS(2); 908 rc->map_name = RC_MAP_HAUPPAUGE; 909 910 ret = rc_register_device(rc); 911 if (ret < 0) { 912 dev_err(dev, "remote dev registration failed\n"); 913 goto out; 914 } 915 916 return rc; 917 918 out: 919 rc_free_device(rc); 920 return NULL; 921 } 922 923 static int redrat3_dev_probe(struct usb_interface *intf, 924 const struct usb_device_id *id) 925 { 926 struct usb_device *udev = interface_to_usbdev(intf); 927 struct device *dev = &intf->dev; 928 struct usb_host_interface *uhi; 929 struct redrat3_dev *rr3; 930 struct usb_endpoint_descriptor *ep; 931 struct usb_endpoint_descriptor *ep_in = NULL; 932 struct usb_endpoint_descriptor *ep_out = NULL; 933 u8 addr, attrs; 934 int pipe, i; 935 int retval = -ENOMEM; 936 937 rr3_ftr(dev, "%s called\n", __func__); 938 939 uhi = intf->cur_altsetting; 940 941 /* find our bulk-in and bulk-out endpoints */ 942 for (i = 0; i < uhi->desc.bNumEndpoints; ++i) { 943 ep = &uhi->endpoint[i].desc; 944 addr = ep->bEndpointAddress; 945 attrs = ep->bmAttributes; 946 947 if ((ep_in == NULL) && 948 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) && 949 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) == 950 USB_ENDPOINT_XFER_BULK)) { 951 rr3_dbg(dev, "found bulk-in endpoint at 0x%02x\n", 952 ep->bEndpointAddress); 953 /* data comes in on 0x82, 0x81 is for other data... */ 954 if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR) 955 ep_in = ep; 956 } 957 958 if ((ep_out == NULL) && 959 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) && 960 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) == 961 USB_ENDPOINT_XFER_BULK)) { 962 rr3_dbg(dev, "found bulk-out endpoint at 0x%02x\n", 963 ep->bEndpointAddress); 964 ep_out = ep; 965 } 966 } 967 968 if (!ep_in || !ep_out) { 969 dev_err(dev, "Couldn't find both in and out endpoints\n"); 970 retval = -ENODEV; 971 goto no_endpoints; 972 } 973 974 /* allocate memory for our device state and initialize it */ 975 rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL); 976 if (rr3 == NULL) { 977 dev_err(dev, "Memory allocation failure\n"); 978 goto no_endpoints; 979 } 980 981 rr3->dev = &intf->dev; 982 983 /* set up bulk-in endpoint */ 984 rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL); 985 if (!rr3->read_urb) { 986 dev_err(dev, "Read urb allocation failure\n"); 987 goto error; 988 } 989 990 rr3->ep_in = ep_in; 991 rr3->bulk_in_buf = usb_alloc_coherent(udev, 992 le16_to_cpu(ep_in->wMaxPacketSize), GFP_ATOMIC, &rr3->dma_in); 993 if (!rr3->bulk_in_buf) { 994 dev_err(dev, "Read buffer allocation failure\n"); 995 goto error; 996 } 997 998 pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress); 999 usb_fill_bulk_urb(rr3->read_urb, udev, pipe, rr3->bulk_in_buf, 1000 le16_to_cpu(ep_in->wMaxPacketSize), redrat3_handle_async, rr3); 1001 1002 rr3->ep_out = ep_out; 1003 rr3->udev = udev; 1004 1005 redrat3_reset(rr3); 1006 redrat3_get_firmware_rev(rr3); 1007 1008 /* might be all we need to do? */ 1009 retval = redrat3_enable_detector(rr3); 1010 if (retval < 0) 1011 goto error; 1012 1013 /* store current hardware timeout, in us, will use for kfifo resets */ 1014 rr3->hw_timeout = redrat3_get_timeout(rr3); 1015 1016 /* default.. will get overridden by any sends with a freq defined */ 1017 rr3->carrier = 38000; 1018 1019 rr3->rc = redrat3_init_rc_dev(rr3); 1020 if (!rr3->rc) { 1021 retval = -ENOMEM; 1022 goto error; 1023 } 1024 setup_timer(&rr3->rx_timeout, redrat3_rx_timeout, (unsigned long)rr3); 1025 1026 /* we can register the device now, as it is ready */ 1027 usb_set_intfdata(intf, rr3); 1028 1029 rr3_ftr(dev, "Exiting %s\n", __func__); 1030 return 0; 1031 1032 error: 1033 redrat3_delete(rr3, rr3->udev); 1034 1035 no_endpoints: 1036 dev_err(dev, "%s: retval = %x", __func__, retval); 1037 1038 return retval; 1039 } 1040 1041 static void redrat3_dev_disconnect(struct usb_interface *intf) 1042 { 1043 struct usb_device *udev = interface_to_usbdev(intf); 1044 struct redrat3_dev *rr3 = usb_get_intfdata(intf); 1045 1046 rr3_ftr(&intf->dev, "Entering %s\n", __func__); 1047 1048 if (!rr3) 1049 return; 1050 1051 redrat3_disable_detector(rr3); 1052 1053 usb_set_intfdata(intf, NULL); 1054 rc_unregister_device(rr3->rc); 1055 del_timer_sync(&rr3->rx_timeout); 1056 redrat3_delete(rr3, udev); 1057 1058 rr3_ftr(&intf->dev, "RedRat3 IR Transceiver now disconnected\n"); 1059 } 1060 1061 static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message) 1062 { 1063 struct redrat3_dev *rr3 = usb_get_intfdata(intf); 1064 rr3_ftr(rr3->dev, "suspend\n"); 1065 usb_kill_urb(rr3->read_urb); 1066 return 0; 1067 } 1068 1069 static int redrat3_dev_resume(struct usb_interface *intf) 1070 { 1071 struct redrat3_dev *rr3 = usb_get_intfdata(intf); 1072 rr3_ftr(rr3->dev, "resume\n"); 1073 if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC)) 1074 return -EIO; 1075 return 0; 1076 } 1077 1078 static struct usb_driver redrat3_dev_driver = { 1079 .name = DRIVER_NAME, 1080 .probe = redrat3_dev_probe, 1081 .disconnect = redrat3_dev_disconnect, 1082 .suspend = redrat3_dev_suspend, 1083 .resume = redrat3_dev_resume, 1084 .reset_resume = redrat3_dev_resume, 1085 .id_table = redrat3_dev_table 1086 }; 1087 1088 module_usb_driver(redrat3_dev_driver); 1089 1090 MODULE_DESCRIPTION(DRIVER_DESC); 1091 MODULE_AUTHOR(DRIVER_AUTHOR); 1092 MODULE_AUTHOR(DRIVER_AUTHOR2); 1093 MODULE_LICENSE("GPL"); 1094 MODULE_DEVICE_TABLE(usb, redrat3_dev_table); 1095 1096 module_param(debug, int, S_IRUGO | S_IWUSR); 1097 MODULE_PARM_DESC(debug, "Enable module debug spew. 0 = no debugging (default) " 1098 "0x1 = standard debug messages, 0x2 = function tracing debug. " 1099 "Flag bits are addative (i.e., 0x3 for both debug types)."); 1100