2 * USB RedRat3 IR Transceiver rc-core driver
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.
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
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.
18 * - fix lirc not showing repeats properly
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.
26 * http://www.redrat.co.uk/
28 * It uses its own little protocol to communicate, the required
29 * parts of which are embedded within this driver.
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.
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.
44 #include <asm/unaligned.h>
45 #include <linux/device.h>
46 #include <linux/leds.h>
47 #include <linux/module.h>
48 #include <linux/slab.h>
49 #include <linux/usb.h>
50 #include <linux/usb/input.h>
51 #include <media/rc-core.h>
53 /* Driver Information */
54 #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
55 #define DRIVER_AUTHOR2 "The Dweller, Stephen Cox"
56 #define DRIVER_DESC "RedRat3 USB IR Transceiver Driver"
57 #define DRIVER_NAME "redrat3"
59 /* bulk data transfer types */
60 #define RR3_ERROR 0x01
61 #define RR3_MOD_SIGNAL_IN 0x20
62 #define RR3_MOD_SIGNAL_OUT 0x21
64 /* Get the RR firmware version */
65 #define RR3_FW_VERSION 0xb1
66 #define RR3_FW_VERSION_LEN 64
67 /* Send encoded signal bulk-sent earlier*/
68 #define RR3_TX_SEND_SIGNAL 0xb3
69 #define RR3_SET_IR_PARAM 0xb7
70 #define RR3_GET_IR_PARAM 0xb8
71 /* Blink the red LED on the device */
72 #define RR3_BLINK_LED 0xb9
73 /* Read serial number of device */
74 #define RR3_READ_SER_NO 0xba
75 #define RR3_SER_NO_LEN 4
76 /* Start capture with the RC receiver */
77 #define RR3_RC_DET_ENABLE 0xbb
78 /* Stop capture with the RC receiver */
79 #define RR3_RC_DET_DISABLE 0xbc
80 /* Start capture with the wideband receiver */
81 #define RR3_MODSIG_CAPTURE 0xb2
82 /* Return the status of RC detector capture */
83 #define RR3_RC_DET_STATUS 0xbd
85 #define RR3_RESET 0xa0
87 /* Max number of lengths in the signal. */
88 #define RR3_IR_IO_MAX_LENGTHS 0x01
89 /* Periods to measure mod. freq. */
90 #define RR3_IR_IO_PERIODS_MF 0x02
91 /* Size of memory for main signal data */
92 #define RR3_IR_IO_SIG_MEM_SIZE 0x03
93 /* Delta value when measuring lengths */
94 #define RR3_IR_IO_LENGTH_FUZZ 0x04
95 /* Timeout for end of signal detection */
96 #define RR3_IR_IO_SIG_TIMEOUT 0x05
97 /* Minimum value for pause recognition. */
98 #define RR3_IR_IO_MIN_PAUSE 0x06
100 /* Clock freq. of EZ-USB chip */
101 #define RR3_CLK 24000000
102 /* Clock periods per timer count */
103 #define RR3_CLK_PER_COUNT 12
104 /* (RR3_CLK / RR3_CLK_PER_COUNT) */
105 #define RR3_CLK_CONV_FACTOR 2000000
106 /* USB bulk-in wideband IR data endpoint address */
107 #define RR3_WIDE_IN_EP_ADDR 0x81
108 /* USB bulk-in narrowband IR data endpoint address */
109 #define RR3_NARROW_IN_EP_ADDR 0x82
111 /* Size of the fixed-length portion of the signal */
112 #define RR3_DRIVER_MAXLENS 255
113 #define RR3_MAX_SIG_SIZE 512
114 #define RR3_TIME_UNIT 50
115 #define RR3_END_OF_SIGNAL 0x7f
116 #define RR3_TX_TRAILER_LEN 2
117 #define RR3_RX_MIN_TIMEOUT 5
118 #define RR3_RX_MAX_TIMEOUT 2000
120 /* The 8051's CPUCS Register address */
121 #define RR3_CPUCS_REG_ADDR 0x7f92
123 #define USB_RR3USB_VENDOR_ID 0x112a
124 #define USB_RR3USB_PRODUCT_ID 0x0001
125 #define USB_RR3IIUSB_PRODUCT_ID 0x0005
129 * The redrat3 encodes an IR signal as set of different lengths and a set
130 * of indices into those lengths. This sets how much two lengths must
131 * differ before they are considered distinct, the value is specified
133 * Default 5, value 0 to 127.
135 static int length_fuzz
= 5;
136 module_param(length_fuzz
, uint
, 0644);
137 MODULE_PARM_DESC(length_fuzz
, "Length Fuzz (0-127)");
140 * When receiving a continuous ir stream (for example when a user is
141 * holding a button down on a remote), this specifies the minimum size
142 * of a space when the redrat3 sends a irdata packet to the host. Specified
143 * in miliseconds. Default value 18ms.
144 * The value can be between 2 and 30 inclusive.
146 static int minimum_pause
= 18;
147 module_param(minimum_pause
, uint
, 0644);
148 MODULE_PARM_DESC(minimum_pause
, "Minimum Pause in ms (2-30)");
151 * The carrier frequency is measured during the first pulse of the IR
152 * signal. The larger the number of periods used To measure, the more
153 * accurate the result is likely to be, however some signals have short
154 * initial pulses, so in some case it may be necessary to reduce this value.
155 * Default 8, value 1 to 255.
157 static int periods_measure_carrier
= 8;
158 module_param(periods_measure_carrier
, uint
, 0644);
159 MODULE_PARM_DESC(periods_measure_carrier
, "Number of Periods to Measure Carrier (1-255)");
162 struct redrat3_header
{
164 __be16 transfer_type
;
167 /* sending and receiving irdata */
168 struct redrat3_irdata
{
169 struct redrat3_header header
;
171 __be16 mod_freq_count
;
178 __be16 lens
[RR3_DRIVER_MAXLENS
]; /* not aligned */
179 __u8 sigdata
[RR3_MAX_SIG_SIZE
];
182 /* firmware errors */
183 struct redrat3_error
{
184 struct redrat3_header header
;
188 /* table of devices that work with this driver */
189 static struct usb_device_id redrat3_dev_table
[] = {
190 /* Original version of the RedRat3 */
191 {USB_DEVICE(USB_RR3USB_VENDOR_ID
, USB_RR3USB_PRODUCT_ID
)},
192 /* Second Version/release of the RedRat3 - RetRat3-II */
193 {USB_DEVICE(USB_RR3USB_VENDOR_ID
, USB_RR3IIUSB_PRODUCT_ID
)},
194 {} /* Terminating entry */
197 /* Structure to hold all of our device specific stuff */
199 /* core device bits */
204 struct led_classdev led
;
206 struct usb_ctrlrequest flash_control
;
207 struct urb
*flash_urb
;
212 struct usb_ctrlrequest learn_control
;
213 struct urb
*learn_urb
;
216 /* save off the usb device pointer */
217 struct usb_device
*udev
;
219 /* the receive endpoint */
220 struct usb_endpoint_descriptor
*ep_narrow
;
221 /* the buffer to receive data */
223 /* urb used to read ir data */
224 struct urb
*narrow_urb
;
225 struct urb
*wide_urb
;
227 /* the send endpoint */
228 struct usb_endpoint_descriptor
*ep_out
;
233 /* Is the device currently transmitting?*/
236 /* store for current packet */
237 struct redrat3_irdata irdata
;
246 static void redrat3_dump_fw_error(struct redrat3_dev
*rr3
, int code
)
248 if (!rr3
->transmitting
&& (code
!= 0x40))
249 dev_info(rr3
->dev
, "fw error code 0x%02x: ", code
);
253 pr_cont("No Error\n");
256 /* Codes 0x20 through 0x2f are IR Firmware Errors */
258 pr_cont("Initial signal pulse not long enough to measure carrier frequency\n");
261 pr_cont("Not enough length values allocated for signal\n");
264 pr_cont("Not enough memory allocated for signal data\n");
267 pr_cont("Too many signal repeats\n");
270 pr_cont("Insufficient memory available for IR signal data memory allocation\n");
273 pr_cont("Insufficient memory available for IrDa signal data memory allocation\n");
276 /* Codes 0x30 through 0x3f are USB Firmware Errors */
278 pr_cont("Insufficient memory available for bulk transfer structure\n");
282 * Other error codes... These are primarily errors that can occur in
283 * the control messages sent to the redrat
286 if (!rr3
->transmitting
)
287 pr_cont("Signal capture has been terminated\n");
290 pr_cont("Attempt to set/get and unknown signal I/O algorithm parameter\n");
293 pr_cont("Signal capture already started\n");
297 pr_cont("Unknown Error\n");
302 static u32
redrat3_val_to_mod_freq(struct redrat3_irdata
*irdata
)
305 u16 mod_freq_count
= be16_to_cpu(irdata
->mod_freq_count
);
307 if (mod_freq_count
!= 0)
308 mod_freq
= (RR3_CLK
* be16_to_cpu(irdata
->num_periods
)) /
309 (mod_freq_count
* RR3_CLK_PER_COUNT
);
314 /* this function scales down the figures for the same result... */
315 static u32
redrat3_len_to_us(u32 length
)
317 u32 biglen
= length
* 1000;
318 u32 divisor
= (RR3_CLK_CONV_FACTOR
) / 1000;
319 u32 result
= (u32
) (biglen
/ divisor
);
321 /* don't allow zero lengths to go back, breaks lirc */
322 return result
? result
: 1;
326 * convert us back into redrat3 lengths
328 * length * 1000 length * 1000000
329 * ------------- = ---------------- = micro
330 * rr3clk / 1000 rr3clk
332 * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
333 * ----- = 4 ----- = 6 -------------- = len ---------------------
336 static u32
redrat3_us_to_len(u32 microsec
)
341 microsec
= (microsec
> IR_MAX_DURATION
) ? IR_MAX_DURATION
: microsec
;
342 divisor
= (RR3_CLK_CONV_FACTOR
/ 1000);
343 result
= (u32
)(microsec
* divisor
) / 1000;
345 /* don't allow zero lengths to go back, breaks lirc */
346 return result
? result
: 1;
349 static void redrat3_process_ir_data(struct redrat3_dev
*rr3
)
351 DEFINE_IR_RAW_EVENT(rawir
);
353 unsigned int i
, sig_size
, single_len
, offset
, val
;
358 mod_freq
= redrat3_val_to_mod_freq(&rr3
->irdata
);
359 dev_dbg(dev
, "Got mod_freq of %u\n", mod_freq
);
360 if (mod_freq
&& rr3
->wideband
) {
361 DEFINE_IR_RAW_EVENT(ev
);
363 ev
.carrier_report
= 1;
364 ev
.carrier
= mod_freq
;
366 ir_raw_event_store(rr3
->rc
, &ev
);
369 /* process each rr3 encoded byte into an int */
370 sig_size
= be16_to_cpu(rr3
->irdata
.sig_size
);
371 for (i
= 0; i
< sig_size
; i
++) {
372 offset
= rr3
->irdata
.sigdata
[i
];
373 val
= get_unaligned_be16(&rr3
->irdata
.lens
[offset
]);
374 single_len
= redrat3_len_to_us(val
);
376 /* we should always get pulse/space/pulse/space samples */
382 rawir
.duration
= US_TO_NS(single_len
);
383 /* cap the value to IR_MAX_DURATION */
384 rawir
.duration
= (rawir
.duration
> IR_MAX_DURATION
) ?
385 IR_MAX_DURATION
: rawir
.duration
;
387 dev_dbg(dev
, "storing %s with duration %d (i: %d)\n",
388 rawir
.pulse
? "pulse" : "space", rawir
.duration
, i
);
389 ir_raw_event_store_with_filter(rr3
->rc
, &rawir
);
392 /* add a trailing space */
394 rawir
.timeout
= true;
395 rawir
.duration
= rr3
->rc
->timeout
;
396 dev_dbg(dev
, "storing trailing timeout with duration %d\n",
398 ir_raw_event_store_with_filter(rr3
->rc
, &rawir
);
400 dev_dbg(dev
, "calling ir_raw_event_handle\n");
401 ir_raw_event_handle(rr3
->rc
);
404 /* Util fn to send rr3 cmds */
405 static int redrat3_send_cmd(int cmd
, struct redrat3_dev
*rr3
)
407 struct usb_device
*udev
;
411 data
= kzalloc(sizeof(u8
), GFP_KERNEL
);
416 res
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0), cmd
,
417 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
418 0x0000, 0x0000, data
, sizeof(u8
), HZ
* 10);
421 dev_err(rr3
->dev
, "%s: Error sending rr3 cmd res %d, data %d",
422 __func__
, res
, *data
);
432 /* Enables the long range detector and starts async receive */
433 static int redrat3_enable_detector(struct redrat3_dev
*rr3
)
435 struct device
*dev
= rr3
->dev
;
438 ret
= redrat3_send_cmd(RR3_RC_DET_ENABLE
, rr3
);
440 dev_dbg(dev
, "%s: unexpected ret of %d\n",
443 ret
= redrat3_send_cmd(RR3_RC_DET_STATUS
, rr3
);
445 dev_err(dev
, "%s: detector status: %d, should be 1\n",
450 ret
= usb_submit_urb(rr3
->narrow_urb
, GFP_KERNEL
);
452 dev_err(rr3
->dev
, "narrow band urb failed: %d", ret
);
456 ret
= usb_submit_urb(rr3
->wide_urb
, GFP_KERNEL
);
458 dev_err(rr3
->dev
, "wide band urb failed: %d", ret
);
463 static inline void redrat3_delete(struct redrat3_dev
*rr3
,
464 struct usb_device
*udev
)
466 usb_kill_urb(rr3
->narrow_urb
);
467 usb_kill_urb(rr3
->wide_urb
);
468 usb_kill_urb(rr3
->flash_urb
);
469 usb_kill_urb(rr3
->learn_urb
);
470 usb_free_urb(rr3
->narrow_urb
);
471 usb_free_urb(rr3
->wide_urb
);
472 usb_free_urb(rr3
->flash_urb
);
473 usb_free_urb(rr3
->learn_urb
);
474 usb_free_coherent(udev
, le16_to_cpu(rr3
->ep_narrow
->wMaxPacketSize
),
475 rr3
->bulk_in_buf
, rr3
->dma_in
);
480 static u32
redrat3_get_timeout(struct redrat3_dev
*rr3
)
483 u32 timeout
= MS_TO_US(150); /* a sane default, if things go haywire */
487 tmp
= kzalloc(len
, GFP_KERNEL
);
491 pipe
= usb_rcvctrlpipe(rr3
->udev
, 0);
492 ret
= usb_control_msg(rr3
->udev
, pipe
, RR3_GET_IR_PARAM
,
493 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
494 RR3_IR_IO_SIG_TIMEOUT
, 0, tmp
, len
, HZ
* 5);
496 dev_warn(rr3
->dev
, "Failed to read timeout from hardware\n");
498 timeout
= redrat3_len_to_us(be32_to_cpup(tmp
));
500 dev_dbg(rr3
->dev
, "Got timeout of %d ms\n", timeout
/ 1000);
508 static int redrat3_set_timeout(struct rc_dev
*rc_dev
, unsigned int timeoutns
)
510 struct redrat3_dev
*rr3
= rc_dev
->priv
;
511 struct usb_device
*udev
= rr3
->udev
;
512 struct device
*dev
= rr3
->dev
;
516 timeout
= kmalloc(sizeof(*timeout
), GFP_KERNEL
);
520 *timeout
= cpu_to_be32(redrat3_us_to_len(timeoutns
/ 1000));
521 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), RR3_SET_IR_PARAM
,
522 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
523 RR3_IR_IO_SIG_TIMEOUT
, 0, timeout
, sizeof(*timeout
),
525 dev_dbg(dev
, "set ir parm timeout %d ret 0x%02x\n",
526 be32_to_cpu(*timeout
), ret
);
528 if (ret
== sizeof(*timeout
))
538 static void redrat3_reset(struct redrat3_dev
*rr3
)
540 struct usb_device
*udev
= rr3
->udev
;
541 struct device
*dev
= rr3
->dev
;
542 int rc
, rxpipe
, txpipe
;
544 size_t const len
= sizeof(*val
);
546 rxpipe
= usb_rcvctrlpipe(udev
, 0);
547 txpipe
= usb_sndctrlpipe(udev
, 0);
549 val
= kmalloc(len
, GFP_KERNEL
);
554 rc
= usb_control_msg(udev
, rxpipe
, RR3_RESET
,
555 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
556 RR3_CPUCS_REG_ADDR
, 0, val
, len
, HZ
* 25);
557 dev_dbg(dev
, "reset returned 0x%02x\n", rc
);
560 rc
= usb_control_msg(udev
, txpipe
, RR3_SET_IR_PARAM
,
561 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
562 RR3_IR_IO_LENGTH_FUZZ
, 0, val
, len
, HZ
* 25);
563 dev_dbg(dev
, "set ir parm len fuzz %d rc 0x%02x\n", *val
, rc
);
565 *val
= (65536 - (minimum_pause
* 2000)) / 256;
566 rc
= usb_control_msg(udev
, txpipe
, RR3_SET_IR_PARAM
,
567 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
568 RR3_IR_IO_MIN_PAUSE
, 0, val
, len
, HZ
* 25);
569 dev_dbg(dev
, "set ir parm min pause %d rc 0x%02x\n", *val
, rc
);
571 *val
= periods_measure_carrier
;
572 rc
= usb_control_msg(udev
, txpipe
, RR3_SET_IR_PARAM
,
573 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
574 RR3_IR_IO_PERIODS_MF
, 0, val
, len
, HZ
* 25);
575 dev_dbg(dev
, "set ir parm periods measure carrier %d rc 0x%02x", *val
,
578 *val
= RR3_DRIVER_MAXLENS
;
579 rc
= usb_control_msg(udev
, txpipe
, RR3_SET_IR_PARAM
,
580 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
581 RR3_IR_IO_MAX_LENGTHS
, 0, val
, len
, HZ
* 25);
582 dev_dbg(dev
, "set ir parm max lens %d rc 0x%02x\n", *val
, rc
);
587 static void redrat3_get_firmware_rev(struct redrat3_dev
*rr3
)
592 buffer
= kcalloc(RR3_FW_VERSION_LEN
+ 1, sizeof(*buffer
), GFP_KERNEL
);
596 rc
= usb_control_msg(rr3
->udev
, usb_rcvctrlpipe(rr3
->udev
, 0),
598 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
599 0, 0, buffer
, RR3_FW_VERSION_LEN
, HZ
* 5);
602 dev_info(rr3
->dev
, "Firmware rev: %s", buffer
);
604 dev_err(rr3
->dev
, "Problem fetching firmware ID\n");
609 static void redrat3_read_packet_start(struct redrat3_dev
*rr3
, unsigned len
)
611 struct redrat3_header
*header
= rr3
->bulk_in_buf
;
612 unsigned pktlen
, pkttype
;
614 /* grab the Length and type of transfer */
615 pktlen
= be16_to_cpu(header
->length
);
616 pkttype
= be16_to_cpu(header
->transfer_type
);
618 if (pktlen
> sizeof(rr3
->irdata
)) {
619 dev_warn(rr3
->dev
, "packet length %u too large\n", pktlen
);
625 if (len
>= sizeof(struct redrat3_error
)) {
626 struct redrat3_error
*error
= rr3
->bulk_in_buf
;
627 unsigned fw_error
= be16_to_cpu(error
->fw_error
);
628 redrat3_dump_fw_error(rr3
, fw_error
);
632 case RR3_MOD_SIGNAL_IN
:
633 memcpy(&rr3
->irdata
, rr3
->bulk_in_buf
, len
);
634 rr3
->bytes_read
= len
;
635 dev_dbg(rr3
->dev
, "bytes_read %d, pktlen %d\n",
636 rr3
->bytes_read
, pktlen
);
640 dev_dbg(rr3
->dev
, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
641 pkttype
, len
, pktlen
);
646 static void redrat3_read_packet_continue(struct redrat3_dev
*rr3
, unsigned len
)
648 void *irdata
= &rr3
->irdata
;
650 if (len
+ rr3
->bytes_read
> sizeof(rr3
->irdata
)) {
651 dev_warn(rr3
->dev
, "too much data for packet\n");
656 memcpy(irdata
+ rr3
->bytes_read
, rr3
->bulk_in_buf
, len
);
658 rr3
->bytes_read
+= len
;
659 dev_dbg(rr3
->dev
, "bytes_read %d, pktlen %d\n", rr3
->bytes_read
,
660 be16_to_cpu(rr3
->irdata
.header
.length
));
663 /* gather IR data from incoming urb, process it when we have enough */
664 static int redrat3_get_ir_data(struct redrat3_dev
*rr3
, unsigned len
)
666 struct device
*dev
= rr3
->dev
;
670 if (rr3
->bytes_read
== 0 && len
>= sizeof(struct redrat3_header
)) {
671 redrat3_read_packet_start(rr3
, len
);
672 } else if (rr3
->bytes_read
!= 0) {
673 redrat3_read_packet_continue(rr3
, len
);
674 } else if (rr3
->bytes_read
== 0) {
675 dev_err(dev
, "error: no packet data read\n");
680 if (rr3
->bytes_read
< be16_to_cpu(rr3
->irdata
.header
.length
) +
681 sizeof(struct redrat3_header
))
682 /* we're still accumulating data */
685 /* if we get here, we've got IR data to decode */
686 pkttype
= be16_to_cpu(rr3
->irdata
.header
.transfer_type
);
687 if (pkttype
== RR3_MOD_SIGNAL_IN
)
688 redrat3_process_ir_data(rr3
);
690 dev_dbg(dev
, "discarding non-signal data packet (type 0x%02x)\n",
698 /* callback function from USB when async USB request has completed */
699 static void redrat3_handle_async(struct urb
*urb
)
701 struct redrat3_dev
*rr3
= urb
->context
;
704 switch (urb
->status
) {
706 ret
= redrat3_get_ir_data(rr3
, urb
->actual_length
);
707 if (!ret
&& rr3
->wideband
&& !rr3
->learn_urb
->hcpriv
) {
708 ret
= usb_submit_urb(rr3
->learn_urb
, GFP_ATOMIC
);
710 dev_err(rr3
->dev
, "Failed to submit learning urb: %d",
715 /* no error, prepare to read more */
716 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
718 dev_err(rr3
->dev
, "Failed to resubmit urb: %d",
731 dev_warn(rr3
->dev
, "Error: urb status = %d\n", urb
->status
);
737 static u16
mod_freq_to_val(unsigned int mod_freq
)
741 /* Clk used in mod. freq. generation is CLK24/4. */
742 return 65536 - (mult
/ mod_freq
);
745 static int redrat3_set_tx_carrier(struct rc_dev
*rcdev
, u32 carrier
)
747 struct redrat3_dev
*rr3
= rcdev
->priv
;
748 struct device
*dev
= rr3
->dev
;
750 dev_dbg(dev
, "Setting modulation frequency to %u", carrier
);
754 rr3
->carrier
= carrier
;
759 static int redrat3_transmit_ir(struct rc_dev
*rcdev
, unsigned *txbuf
,
762 struct redrat3_dev
*rr3
= rcdev
->priv
;
763 struct device
*dev
= rr3
->dev
;
764 struct redrat3_irdata
*irdata
= NULL
;
766 int lencheck
, cur_sample_len
, pipe
;
767 int *sample_lens
= NULL
;
769 unsigned i
, sendbuf_len
;
771 if (rr3
->transmitting
) {
772 dev_warn(dev
, "%s: transmitter already in use\n", __func__
);
776 if (count
> RR3_MAX_SIG_SIZE
- RR3_TX_TRAILER_LEN
)
779 /* rr3 will disable rc detector on transmit */
780 rr3
->transmitting
= true;
782 sample_lens
= kcalloc(RR3_DRIVER_MAXLENS
,
783 sizeof(*sample_lens
),
788 irdata
= kzalloc(sizeof(*irdata
), GFP_KERNEL
);
794 for (i
= 0; i
< count
; i
++) {
795 cur_sample_len
= redrat3_us_to_len(txbuf
[i
]);
796 if (cur_sample_len
> 0xffff) {
797 dev_warn(dev
, "transmit period of %uus truncated to %uus\n",
798 txbuf
[i
], redrat3_len_to_us(0xffff));
799 cur_sample_len
= 0xffff;
801 for (lencheck
= 0; lencheck
< curlencheck
; lencheck
++) {
802 if (sample_lens
[lencheck
] == cur_sample_len
)
805 if (lencheck
== curlencheck
) {
806 dev_dbg(dev
, "txbuf[%d]=%u, pos %d, enc %u\n",
807 i
, txbuf
[i
], curlencheck
, cur_sample_len
);
808 if (curlencheck
< RR3_DRIVER_MAXLENS
) {
809 /* now convert the value to a proper
811 sample_lens
[curlencheck
] = cur_sample_len
;
812 put_unaligned_be16(cur_sample_len
,
813 &irdata
->lens
[curlencheck
]);
820 irdata
->sigdata
[i
] = lencheck
;
823 irdata
->sigdata
[count
] = RR3_END_OF_SIGNAL
;
824 irdata
->sigdata
[count
+ 1] = RR3_END_OF_SIGNAL
;
826 sendbuf_len
= offsetof(struct redrat3_irdata
,
827 sigdata
[count
+ RR3_TX_TRAILER_LEN
]);
828 /* fill in our packet header */
829 irdata
->header
.length
= cpu_to_be16(sendbuf_len
-
830 sizeof(struct redrat3_header
));
831 irdata
->header
.transfer_type
= cpu_to_be16(RR3_MOD_SIGNAL_OUT
);
832 irdata
->pause
= cpu_to_be32(redrat3_len_to_us(100));
833 irdata
->mod_freq_count
= cpu_to_be16(mod_freq_to_val(rr3
->carrier
));
834 irdata
->no_lengths
= curlencheck
;
835 irdata
->sig_size
= cpu_to_be16(count
+ RR3_TX_TRAILER_LEN
);
837 pipe
= usb_sndbulkpipe(rr3
->udev
, rr3
->ep_out
->bEndpointAddress
);
838 ret
= usb_bulk_msg(rr3
->udev
, pipe
, irdata
,
839 sendbuf_len
, &ret_len
, 10 * HZ
);
840 dev_dbg(dev
, "sent %d bytes, (ret %d)\n", ret_len
, ret
);
842 /* now tell the hardware to transmit what we sent it */
843 pipe
= usb_rcvctrlpipe(rr3
->udev
, 0);
844 ret
= usb_control_msg(rr3
->udev
, pipe
, RR3_TX_SEND_SIGNAL
,
845 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
846 0, 0, irdata
, 2, HZ
* 10);
849 dev_err(dev
, "Error: control msg send failed, rc %d\n", ret
);
857 rr3
->transmitting
= false;
858 /* rr3 re-enables rc detector because it was enabled before */
863 static void redrat3_brightness_set(struct led_classdev
*led_dev
, enum
864 led_brightness brightness
)
866 struct redrat3_dev
*rr3
= container_of(led_dev
, struct redrat3_dev
,
869 if (brightness
!= LED_OFF
&& atomic_cmpxchg(&rr3
->flash
, 0, 1) == 0) {
870 int ret
= usb_submit_urb(rr3
->flash_urb
, GFP_ATOMIC
);
872 dev_dbg(rr3
->dev
, "%s: unexpected ret of %d\n",
874 atomic_set(&rr3
->flash
, 0);
879 static int redrat3_wideband_receiver(struct rc_dev
*rcdev
, int enable
)
881 struct redrat3_dev
*rr3
= rcdev
->priv
;
884 rr3
->wideband
= enable
!= 0;
887 ret
= usb_submit_urb(rr3
->learn_urb
, GFP_KERNEL
);
889 dev_err(rr3
->dev
, "Failed to submit learning urb: %d",
896 static void redrat3_learn_complete(struct urb
*urb
)
898 struct redrat3_dev
*rr3
= urb
->context
;
900 switch (urb
->status
) {
910 dev_err(rr3
->dev
, "Error: learn urb status = %d", urb
->status
);
915 static void redrat3_led_complete(struct urb
*urb
)
917 struct redrat3_dev
*rr3
= urb
->context
;
919 switch (urb
->status
) {
929 dev_dbg(rr3
->dev
, "Error: urb status = %d\n", urb
->status
);
933 rr3
->led
.brightness
= LED_OFF
;
934 atomic_dec(&rr3
->flash
);
937 static struct rc_dev
*redrat3_init_rc_dev(struct redrat3_dev
*rr3
)
939 struct device
*dev
= rr3
->dev
;
942 u16 prod
= le16_to_cpu(rr3
->udev
->descriptor
.idProduct
);
944 rc
= rc_allocate_device(RC_DRIVER_IR_RAW
);
948 snprintf(rr3
->name
, sizeof(rr3
->name
),
949 "RedRat3%s Infrared Remote Transceiver",
950 prod
== USB_RR3IIUSB_PRODUCT_ID
? "-II" : "");
952 usb_make_path(rr3
->udev
, rr3
->phys
, sizeof(rr3
->phys
));
954 rc
->input_name
= rr3
->name
;
955 rc
->input_phys
= rr3
->phys
;
956 usb_to_input_id(rr3
->udev
, &rc
->input_id
);
957 rc
->dev
.parent
= dev
;
959 rc
->allowed_protocols
= RC_BIT_ALL_IR_DECODER
;
960 rc
->min_timeout
= MS_TO_NS(RR3_RX_MIN_TIMEOUT
);
961 rc
->max_timeout
= MS_TO_NS(RR3_RX_MAX_TIMEOUT
);
962 rc
->timeout
= US_TO_NS(redrat3_get_timeout(rr3
));
963 rc
->s_timeout
= redrat3_set_timeout
;
964 rc
->tx_ir
= redrat3_transmit_ir
;
965 rc
->s_tx_carrier
= redrat3_set_tx_carrier
;
966 rc
->s_carrier_report
= redrat3_wideband_receiver
;
967 rc
->driver_name
= DRIVER_NAME
;
968 rc
->rx_resolution
= US_TO_NS(2);
969 rc
->map_name
= RC_MAP_HAUPPAUGE
;
971 ret
= rc_register_device(rc
);
973 dev_err(dev
, "remote dev registration failed\n");
984 static int redrat3_dev_probe(struct usb_interface
*intf
,
985 const struct usb_device_id
*id
)
987 struct usb_device
*udev
= interface_to_usbdev(intf
);
988 struct device
*dev
= &intf
->dev
;
989 struct usb_host_interface
*uhi
;
990 struct redrat3_dev
*rr3
;
991 struct usb_endpoint_descriptor
*ep
;
992 struct usb_endpoint_descriptor
*ep_narrow
= NULL
;
993 struct usb_endpoint_descriptor
*ep_wide
= NULL
;
994 struct usb_endpoint_descriptor
*ep_out
= NULL
;
997 int retval
= -ENOMEM
;
999 uhi
= intf
->cur_altsetting
;
1001 /* find our bulk-in and bulk-out endpoints */
1002 for (i
= 0; i
< uhi
->desc
.bNumEndpoints
; ++i
) {
1003 ep
= &uhi
->endpoint
[i
].desc
;
1004 addr
= ep
->bEndpointAddress
;
1005 attrs
= ep
->bmAttributes
;
1007 if (((addr
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
) &&
1008 ((attrs
& USB_ENDPOINT_XFERTYPE_MASK
) ==
1009 USB_ENDPOINT_XFER_BULK
)) {
1010 dev_dbg(dev
, "found bulk-in endpoint at 0x%02x\n",
1011 ep
->bEndpointAddress
);
1012 /* data comes in on 0x82, 0x81 is for learning */
1013 if (ep
->bEndpointAddress
== RR3_NARROW_IN_EP_ADDR
)
1015 if (ep
->bEndpointAddress
== RR3_WIDE_IN_EP_ADDR
)
1019 if ((ep_out
== NULL
) &&
1020 ((addr
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) &&
1021 ((attrs
& USB_ENDPOINT_XFERTYPE_MASK
) ==
1022 USB_ENDPOINT_XFER_BULK
)) {
1023 dev_dbg(dev
, "found bulk-out endpoint at 0x%02x\n",
1024 ep
->bEndpointAddress
);
1029 if (!ep_narrow
|| !ep_out
|| !ep_wide
) {
1030 dev_err(dev
, "Couldn't find all endpoints\n");
1035 /* allocate memory for our device state and initialize it */
1036 rr3
= kzalloc(sizeof(*rr3
), GFP_KERNEL
);
1040 rr3
->dev
= &intf
->dev
;
1041 rr3
->ep_narrow
= ep_narrow
;
1042 rr3
->ep_out
= ep_out
;
1045 /* set up bulk-in endpoint */
1046 rr3
->narrow_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1047 if (!rr3
->narrow_urb
)
1050 rr3
->wide_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1054 rr3
->bulk_in_buf
= usb_alloc_coherent(udev
,
1055 le16_to_cpu(ep_narrow
->wMaxPacketSize
),
1056 GFP_KERNEL
, &rr3
->dma_in
);
1057 if (!rr3
->bulk_in_buf
)
1060 pipe
= usb_rcvbulkpipe(udev
, ep_narrow
->bEndpointAddress
);
1061 usb_fill_bulk_urb(rr3
->narrow_urb
, udev
, pipe
, rr3
->bulk_in_buf
,
1062 le16_to_cpu(ep_narrow
->wMaxPacketSize
),
1063 redrat3_handle_async
, rr3
);
1064 rr3
->narrow_urb
->transfer_dma
= rr3
->dma_in
;
1065 rr3
->narrow_urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1067 pipe
= usb_rcvbulkpipe(udev
, ep_wide
->bEndpointAddress
);
1068 usb_fill_bulk_urb(rr3
->wide_urb
, udev
, pipe
, rr3
->bulk_in_buf
,
1069 le16_to_cpu(ep_narrow
->wMaxPacketSize
),
1070 redrat3_handle_async
, rr3
);
1071 rr3
->wide_urb
->transfer_dma
= rr3
->dma_in
;
1072 rr3
->wide_urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1075 redrat3_get_firmware_rev(rr3
);
1077 /* default.. will get overridden by any sends with a freq defined */
1078 rr3
->carrier
= 38000;
1080 atomic_set(&rr3
->flash
, 0);
1081 rr3
->flash_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1082 if (!rr3
->flash_urb
)
1086 rr3
->learn_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1087 if (!rr3
->learn_urb
)
1090 /* setup packet is 'c0 b2 0000 0000 0001' */
1091 rr3
->learn_control
.bRequestType
= 0xc0;
1092 rr3
->learn_control
.bRequest
= RR3_MODSIG_CAPTURE
;
1093 rr3
->learn_control
.wLength
= cpu_to_le16(1);
1095 usb_fill_control_urb(rr3
->learn_urb
, udev
, usb_rcvctrlpipe(udev
, 0),
1096 (unsigned char *)&rr3
->learn_control
,
1097 &rr3
->learn_buf
, sizeof(rr3
->learn_buf
),
1098 redrat3_learn_complete
, rr3
);
1100 /* setup packet is 'c0 b9 0000 0000 0001' */
1101 rr3
->flash_control
.bRequestType
= 0xc0;
1102 rr3
->flash_control
.bRequest
= RR3_BLINK_LED
;
1103 rr3
->flash_control
.wLength
= cpu_to_le16(1);
1105 usb_fill_control_urb(rr3
->flash_urb
, udev
, usb_rcvctrlpipe(udev
, 0),
1106 (unsigned char *)&rr3
->flash_control
,
1107 &rr3
->flash_in_buf
, sizeof(rr3
->flash_in_buf
),
1108 redrat3_led_complete
, rr3
);
1111 rr3
->led
.name
= "redrat3:red:feedback";
1112 rr3
->led
.default_trigger
= "rc-feedback";
1113 rr3
->led
.brightness_set
= redrat3_brightness_set
;
1114 retval
= led_classdev_register(&intf
->dev
, &rr3
->led
);
1118 rr3
->rc
= redrat3_init_rc_dev(rr3
);
1124 /* might be all we need to do? */
1125 retval
= redrat3_enable_detector(rr3
);
1129 /* we can register the device now, as it is ready */
1130 usb_set_intfdata(intf
, rr3
);
1135 led_classdev_unregister(&rr3
->led
);
1137 redrat3_delete(rr3
, rr3
->udev
);
1143 static void redrat3_dev_disconnect(struct usb_interface
*intf
)
1145 struct usb_device
*udev
= interface_to_usbdev(intf
);
1146 struct redrat3_dev
*rr3
= usb_get_intfdata(intf
);
1148 usb_set_intfdata(intf
, NULL
);
1149 rc_unregister_device(rr3
->rc
);
1150 led_classdev_unregister(&rr3
->led
);
1151 redrat3_delete(rr3
, udev
);
1154 static int redrat3_dev_suspend(struct usb_interface
*intf
, pm_message_t message
)
1156 struct redrat3_dev
*rr3
= usb_get_intfdata(intf
);
1158 led_classdev_suspend(&rr3
->led
);
1159 usb_kill_urb(rr3
->narrow_urb
);
1160 usb_kill_urb(rr3
->wide_urb
);
1161 usb_kill_urb(rr3
->flash_urb
);
1165 static int redrat3_dev_resume(struct usb_interface
*intf
)
1167 struct redrat3_dev
*rr3
= usb_get_intfdata(intf
);
1169 if (usb_submit_urb(rr3
->narrow_urb
, GFP_ATOMIC
))
1171 if (usb_submit_urb(rr3
->wide_urb
, GFP_ATOMIC
))
1173 led_classdev_resume(&rr3
->led
);
1177 static struct usb_driver redrat3_dev_driver
= {
1178 .name
= DRIVER_NAME
,
1179 .probe
= redrat3_dev_probe
,
1180 .disconnect
= redrat3_dev_disconnect
,
1181 .suspend
= redrat3_dev_suspend
,
1182 .resume
= redrat3_dev_resume
,
1183 .reset_resume
= redrat3_dev_resume
,
1184 .id_table
= redrat3_dev_table
1187 module_usb_driver(redrat3_dev_driver
);
1189 MODULE_DESCRIPTION(DRIVER_DESC
);
1190 MODULE_AUTHOR(DRIVER_AUTHOR
);
1191 MODULE_AUTHOR(DRIVER_AUTHOR2
);
1192 MODULE_LICENSE("GPL");
1193 MODULE_DEVICE_TABLE(usb
, redrat3_dev_table
);