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