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.
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
48 #include <asm/unaligned.h>
49 #include <linux/device.h>
50 #include <linux/leds.h>
51 #include <linux/module.h>
52 #include <linux/slab.h>
53 #include <linux/usb.h>
54 #include <linux/usb/input.h>
55 #include <media/rc-core.h>
57 /* Driver Information */
58 #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
59 #define DRIVER_AUTHOR2 "The Dweller, Stephen Cox"
60 #define DRIVER_DESC "RedRat3 USB IR Transceiver Driver"
61 #define DRIVER_NAME "redrat3"
63 /* bulk data transfer types */
64 #define RR3_ERROR 0x01
65 #define RR3_MOD_SIGNAL_IN 0x20
66 #define RR3_MOD_SIGNAL_OUT 0x21
68 /* Get the RR firmware version */
69 #define RR3_FW_VERSION 0xb1
70 #define RR3_FW_VERSION_LEN 64
71 /* Send encoded signal bulk-sent earlier*/
72 #define RR3_TX_SEND_SIGNAL 0xb3
73 #define RR3_SET_IR_PARAM 0xb7
74 #define RR3_GET_IR_PARAM 0xb8
75 /* Blink the red LED on the device */
76 #define RR3_BLINK_LED 0xb9
77 /* Read serial number of device */
78 #define RR3_READ_SER_NO 0xba
79 #define RR3_SER_NO_LEN 4
80 /* Start capture with the RC receiver */
81 #define RR3_RC_DET_ENABLE 0xbb
82 /* Stop capture with the RC receiver */
83 #define RR3_RC_DET_DISABLE 0xbc
84 /* Return the status of RC detector capture */
85 #define RR3_RC_DET_STATUS 0xbd
87 #define RR3_RESET 0xa0
89 /* Max number of lengths in the signal. */
90 #define RR3_IR_IO_MAX_LENGTHS 0x01
91 /* Periods to measure mod. freq. */
92 #define RR3_IR_IO_PERIODS_MF 0x02
93 /* Size of memory for main signal data */
94 #define RR3_IR_IO_SIG_MEM_SIZE 0x03
95 /* Delta value when measuring lengths */
96 #define RR3_IR_IO_LENGTH_FUZZ 0x04
97 /* Timeout for end of signal detection */
98 #define RR3_IR_IO_SIG_TIMEOUT 0x05
99 /* Minimum value for pause recognition. */
100 #define RR3_IR_IO_MIN_PAUSE 0x06
102 /* Clock freq. of EZ-USB chip */
103 #define RR3_CLK 24000000
104 /* Clock periods per timer count */
105 #define RR3_CLK_PER_COUNT 12
106 /* (RR3_CLK / RR3_CLK_PER_COUNT) */
107 #define RR3_CLK_CONV_FACTOR 2000000
108 /* USB bulk-in IR data endpoint address */
109 #define RR3_BULK_IN_EP_ADDR 0x82
111 /* Size of the fixed-length portion of the signal */
112 #define RR3_DRIVER_MAXLENS 128
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
;
210 /* save off the usb device pointer */
211 struct usb_device
*udev
;
213 /* the receive endpoint */
214 struct usb_endpoint_descriptor
*ep_in
;
215 /* the buffer to receive data */
217 /* urb used to read ir data */
218 struct urb
*read_urb
;
220 /* the send endpoint */
221 struct usb_endpoint_descriptor
*ep_out
;
226 /* Is the device currently transmitting?*/
229 /* store for current packet */
230 struct redrat3_irdata irdata
;
240 * redrat3_issue_async
242 * Issues an async read to the ir data in port..
243 * sets the callback to be redrat3_handle_async
245 static void redrat3_issue_async(struct redrat3_dev
*rr3
)
249 res
= usb_submit_urb(rr3
->read_urb
, GFP_ATOMIC
);
252 "%s: receive request FAILED! (res %d, len %d)\n",
253 __func__
, res
, rr3
->read_urb
->transfer_buffer_length
);
256 static void redrat3_dump_fw_error(struct redrat3_dev
*rr3
, int code
)
258 if (!rr3
->transmitting
&& (code
!= 0x40))
259 dev_info(rr3
->dev
, "fw error code 0x%02x: ", code
);
263 pr_cont("No Error\n");
266 /* Codes 0x20 through 0x2f are IR Firmware Errors */
268 pr_cont("Initial signal pulse not long enough "
269 "to measure carrier frequency\n");
272 pr_cont("Not enough length values allocated for signal\n");
275 pr_cont("Not enough memory allocated for signal data\n");
278 pr_cont("Too many signal repeats\n");
281 pr_cont("Insufficient memory available for IR signal "
282 "data memory allocation\n");
285 pr_cont("Insufficient memory available "
286 "for IrDa signal data memory allocation\n");
289 /* Codes 0x30 through 0x3f are USB Firmware Errors */
291 pr_cont("Insufficient memory available for bulk "
292 "transfer structure\n");
296 * Other error codes... These are primarily errors that can occur in
297 * the control messages sent to the redrat
300 if (!rr3
->transmitting
)
301 pr_cont("Signal capture has been terminated\n");
304 pr_cont("Attempt to set/get and unknown signal I/O "
305 "algorithm parameter\n");
308 pr_cont("Signal capture already started\n");
312 pr_cont("Unknown Error\n");
317 static u32
redrat3_val_to_mod_freq(struct redrat3_irdata
*irdata
)
320 u16 mod_freq_count
= be16_to_cpu(irdata
->mod_freq_count
);
322 if (mod_freq_count
!= 0)
323 mod_freq
= (RR3_CLK
* be16_to_cpu(irdata
->num_periods
)) /
324 (mod_freq_count
* RR3_CLK_PER_COUNT
);
329 /* this function scales down the figures for the same result... */
330 static u32
redrat3_len_to_us(u32 length
)
332 u32 biglen
= length
* 1000;
333 u32 divisor
= (RR3_CLK_CONV_FACTOR
) / 1000;
334 u32 result
= (u32
) (biglen
/ divisor
);
336 /* don't allow zero lengths to go back, breaks lirc */
337 return result
? result
: 1;
341 * convert us back into redrat3 lengths
343 * length * 1000 length * 1000000
344 * ------------- = ---------------- = micro
345 * rr3clk / 1000 rr3clk
347 * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
348 * ----- = 4 ----- = 6 -------------- = len ---------------------
351 static u32
redrat3_us_to_len(u32 microsec
)
356 microsec
= (microsec
> IR_MAX_DURATION
) ? IR_MAX_DURATION
: microsec
;
357 divisor
= (RR3_CLK_CONV_FACTOR
/ 1000);
358 result
= (u32
)(microsec
* divisor
) / 1000;
360 /* don't allow zero lengths to go back, breaks lirc */
361 return result
? result
: 1;
364 static void redrat3_process_ir_data(struct redrat3_dev
*rr3
)
366 DEFINE_IR_RAW_EVENT(rawir
);
368 unsigned int i
, sig_size
, single_len
, offset
, val
;
372 pr_err("%s called with no context!\n", __func__
);
378 mod_freq
= redrat3_val_to_mod_freq(&rr3
->irdata
);
379 dev_dbg(dev
, "Got mod_freq of %u\n", mod_freq
);
381 /* process each rr3 encoded byte into an int */
382 sig_size
= be16_to_cpu(rr3
->irdata
.sig_size
);
383 for (i
= 0; i
< sig_size
; i
++) {
384 offset
= rr3
->irdata
.sigdata
[i
];
385 val
= get_unaligned_be16(&rr3
->irdata
.lens
[offset
]);
386 single_len
= redrat3_len_to_us(val
);
388 /* we should always get pulse/space/pulse/space samples */
394 rawir
.duration
= US_TO_NS(single_len
);
395 /* cap the value to IR_MAX_DURATION */
396 rawir
.duration
= (rawir
.duration
> IR_MAX_DURATION
) ?
397 IR_MAX_DURATION
: rawir
.duration
;
399 dev_dbg(dev
, "storing %s with duration %d (i: %d)\n",
400 rawir
.pulse
? "pulse" : "space", rawir
.duration
, i
);
401 ir_raw_event_store_with_filter(rr3
->rc
, &rawir
);
404 /* add a trailing space */
406 rawir
.timeout
= true;
407 rawir
.duration
= rr3
->rc
->timeout
;
408 dev_dbg(dev
, "storing trailing timeout with duration %d\n",
410 ir_raw_event_store_with_filter(rr3
->rc
, &rawir
);
412 dev_dbg(dev
, "calling ir_raw_event_handle\n");
413 ir_raw_event_handle(rr3
->rc
);
416 /* Util fn to send rr3 cmds */
417 static int redrat3_send_cmd(int cmd
, struct redrat3_dev
*rr3
)
419 struct usb_device
*udev
;
423 data
= kzalloc(sizeof(u8
), GFP_KERNEL
);
428 res
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0), cmd
,
429 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
430 0x0000, 0x0000, data
, sizeof(u8
), HZ
* 10);
433 dev_err(rr3
->dev
, "%s: Error sending rr3 cmd res %d, data %d",
434 __func__
, res
, *data
);
444 /* Enables the long range detector and starts async receive */
445 static int redrat3_enable_detector(struct redrat3_dev
*rr3
)
447 struct device
*dev
= rr3
->dev
;
450 ret
= redrat3_send_cmd(RR3_RC_DET_ENABLE
, rr3
);
452 dev_dbg(dev
, "%s: unexpected ret of %d\n",
455 ret
= redrat3_send_cmd(RR3_RC_DET_STATUS
, rr3
);
457 dev_err(dev
, "%s: detector status: %d, should be 1\n",
462 redrat3_issue_async(rr3
);
467 static inline void redrat3_delete(struct redrat3_dev
*rr3
,
468 struct usb_device
*udev
)
470 usb_kill_urb(rr3
->read_urb
);
471 usb_kill_urb(rr3
->flash_urb
);
472 usb_free_urb(rr3
->read_urb
);
473 usb_free_urb(rr3
->flash_urb
);
474 usb_free_coherent(udev
, le16_to_cpu(rr3
->ep_in
->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
);
489 dev_warn(rr3
->dev
, "Memory allocation faillure\n");
493 pipe
= usb_rcvctrlpipe(rr3
->udev
, 0);
494 ret
= usb_control_msg(rr3
->udev
, pipe
, RR3_GET_IR_PARAM
,
495 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
496 RR3_IR_IO_SIG_TIMEOUT
, 0, tmp
, len
, HZ
* 5);
498 dev_warn(rr3
->dev
, "Failed to read timeout from hardware\n");
500 timeout
= redrat3_len_to_us(be32_to_cpup(tmp
));
502 dev_dbg(rr3
->dev
, "Got timeout of %d ms\n", timeout
/ 1000);
510 static int redrat3_set_timeout(struct rc_dev
*rc_dev
, unsigned int timeoutns
)
512 struct redrat3_dev
*rr3
= rc_dev
->priv
;
513 struct usb_device
*udev
= rr3
->udev
;
514 struct device
*dev
= rr3
->dev
;
518 timeout
= kmalloc(sizeof(*timeout
), GFP_KERNEL
);
522 *timeout
= cpu_to_be32(redrat3_us_to_len(timeoutns
/ 1000));
523 ret
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0), RR3_SET_IR_PARAM
,
524 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
525 RR3_IR_IO_SIG_TIMEOUT
, 0, timeout
, sizeof(*timeout
),
527 dev_dbg(dev
, "set ir parm timeout %d ret 0x%02x\n",
528 be32_to_cpu(*timeout
), ret
);
530 if (ret
== sizeof(*timeout
))
540 static void redrat3_reset(struct redrat3_dev
*rr3
)
542 struct usb_device
*udev
= rr3
->udev
;
543 struct device
*dev
= rr3
->dev
;
544 int rc
, rxpipe
, txpipe
;
546 int len
= sizeof(u8
);
548 rxpipe
= usb_rcvctrlpipe(udev
, 0);
549 txpipe
= usb_sndctrlpipe(udev
, 0);
551 val
= kmalloc(len
, GFP_KERNEL
);
553 dev_err(dev
, "Memory allocation failure\n");
558 rc
= usb_control_msg(udev
, rxpipe
, RR3_RESET
,
559 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
560 RR3_CPUCS_REG_ADDR
, 0, val
, len
, HZ
* 25);
561 dev_dbg(dev
, "reset returned 0x%02x\n", rc
);
564 rc
= usb_control_msg(udev
, txpipe
, RR3_SET_IR_PARAM
,
565 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
566 RR3_IR_IO_LENGTH_FUZZ
, 0, val
, len
, HZ
* 25);
567 dev_dbg(dev
, "set ir parm len fuzz %d rc 0x%02x\n", *val
, rc
);
569 *val
= (65536 - (minimum_pause
* 2000)) / 256;
570 rc
= usb_control_msg(udev
, txpipe
, RR3_SET_IR_PARAM
,
571 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
572 RR3_IR_IO_MIN_PAUSE
, 0, val
, len
, HZ
* 25);
573 dev_dbg(dev
, "set ir parm min pause %d rc 0x%02x\n", *val
, rc
);
575 *val
= periods_measure_carrier
;
576 rc
= usb_control_msg(udev
, txpipe
, RR3_SET_IR_PARAM
,
577 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
578 RR3_IR_IO_PERIODS_MF
, 0, val
, len
, HZ
* 25);
579 dev_dbg(dev
, "set ir parm periods measure carrier %d rc 0x%02x", *val
,
582 *val
= RR3_DRIVER_MAXLENS
;
583 rc
= usb_control_msg(udev
, txpipe
, RR3_SET_IR_PARAM
,
584 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
585 RR3_IR_IO_MAX_LENGTHS
, 0, val
, len
, HZ
* 25);
586 dev_dbg(dev
, "set ir parm max lens %d rc 0x%02x\n", *val
, rc
);
591 static void redrat3_get_firmware_rev(struct redrat3_dev
*rr3
)
596 buffer
= kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN
+ 1), GFP_KERNEL
);
598 dev_err(rr3
->dev
, "Memory allocation failure\n");
602 rc
= usb_control_msg(rr3
->udev
, usb_rcvctrlpipe(rr3
->udev
, 0),
604 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
605 0, 0, buffer
, RR3_FW_VERSION_LEN
, HZ
* 5);
608 dev_info(rr3
->dev
, "Firmware rev: %s", buffer
);
610 dev_err(rr3
->dev
, "Problem fetching firmware ID\n");
615 static void redrat3_read_packet_start(struct redrat3_dev
*rr3
, unsigned len
)
617 struct redrat3_header
*header
= rr3
->bulk_in_buf
;
618 unsigned pktlen
, pkttype
;
620 /* grab the Length and type of transfer */
621 pktlen
= be16_to_cpu(header
->length
);
622 pkttype
= be16_to_cpu(header
->transfer_type
);
624 if (pktlen
> sizeof(rr3
->irdata
)) {
625 dev_warn(rr3
->dev
, "packet length %u too large\n", pktlen
);
631 if (len
>= sizeof(struct redrat3_error
)) {
632 struct redrat3_error
*error
= rr3
->bulk_in_buf
;
633 unsigned fw_error
= be16_to_cpu(error
->fw_error
);
634 redrat3_dump_fw_error(rr3
, fw_error
);
638 case RR3_MOD_SIGNAL_IN
:
639 memcpy(&rr3
->irdata
, rr3
->bulk_in_buf
, len
);
640 rr3
->bytes_read
= len
;
641 dev_dbg(rr3
->dev
, "bytes_read %d, pktlen %d\n",
642 rr3
->bytes_read
, pktlen
);
646 dev_dbg(rr3
->dev
, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
647 pkttype
, len
, pktlen
);
652 static void redrat3_read_packet_continue(struct redrat3_dev
*rr3
, unsigned len
)
654 void *irdata
= &rr3
->irdata
;
656 if (len
+ rr3
->bytes_read
> sizeof(rr3
->irdata
)) {
657 dev_warn(rr3
->dev
, "too much data for packet\n");
662 memcpy(irdata
+ rr3
->bytes_read
, rr3
->bulk_in_buf
, len
);
664 rr3
->bytes_read
+= len
;
665 dev_dbg(rr3
->dev
, "bytes_read %d, pktlen %d\n", rr3
->bytes_read
,
666 be16_to_cpu(rr3
->irdata
.header
.length
));
669 /* gather IR data from incoming urb, process it when we have enough */
670 static int redrat3_get_ir_data(struct redrat3_dev
*rr3
, unsigned len
)
672 struct device
*dev
= rr3
->dev
;
676 if (rr3
->bytes_read
== 0 && len
>= sizeof(struct redrat3_header
)) {
677 redrat3_read_packet_start(rr3
, len
);
678 } else if (rr3
->bytes_read
!= 0) {
679 redrat3_read_packet_continue(rr3
, len
);
680 } else if (rr3
->bytes_read
== 0) {
681 dev_err(dev
, "error: no packet data read\n");
686 if (rr3
->bytes_read
< be16_to_cpu(rr3
->irdata
.header
.length
) +
687 sizeof(struct redrat3_header
))
688 /* we're still accumulating data */
691 /* if we get here, we've got IR data to decode */
692 pkttype
= be16_to_cpu(rr3
->irdata
.header
.transfer_type
);
693 if (pkttype
== RR3_MOD_SIGNAL_IN
)
694 redrat3_process_ir_data(rr3
);
696 dev_dbg(dev
, "discarding non-signal data packet (type 0x%02x)\n",
704 /* callback function from USB when async USB request has completed */
705 static void redrat3_handle_async(struct urb
*urb
)
707 struct redrat3_dev
*rr3
;
715 pr_err("%s called with invalid context!\n", __func__
);
720 switch (urb
->status
) {
722 ret
= redrat3_get_ir_data(rr3
, urb
->actual_length
);
724 /* no error, prepare to read more */
725 redrat3_issue_async(rr3
);
737 dev_warn(rr3
->dev
, "Error: urb status = %d\n", urb
->status
);
743 static u16
mod_freq_to_val(unsigned int mod_freq
)
747 /* Clk used in mod. freq. generation is CLK24/4. */
748 return 65536 - (mult
/ mod_freq
);
751 static int redrat3_set_tx_carrier(struct rc_dev
*rcdev
, u32 carrier
)
753 struct redrat3_dev
*rr3
= rcdev
->priv
;
754 struct device
*dev
= rr3
->dev
;
756 dev_dbg(dev
, "Setting modulation frequency to %u", carrier
);
760 rr3
->carrier
= carrier
;
765 static int redrat3_transmit_ir(struct rc_dev
*rcdev
, unsigned *txbuf
,
768 struct redrat3_dev
*rr3
= rcdev
->priv
;
769 struct device
*dev
= rr3
->dev
;
770 struct redrat3_irdata
*irdata
= NULL
;
772 int lencheck
, cur_sample_len
, pipe
;
773 int *sample_lens
= NULL
;
775 unsigned i
, sendbuf_len
;
777 if (rr3
->transmitting
) {
778 dev_warn(dev
, "%s: transmitter already in use\n", __func__
);
782 if (count
> RR3_MAX_SIG_SIZE
- RR3_TX_TRAILER_LEN
)
785 /* rr3 will disable rc detector on transmit */
786 rr3
->transmitting
= true;
788 sample_lens
= kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS
, GFP_KERNEL
);
794 irdata
= kzalloc(sizeof(*irdata
), GFP_KERNEL
);
800 for (i
= 0; i
< count
; i
++) {
801 cur_sample_len
= redrat3_us_to_len(txbuf
[i
]);
802 if (cur_sample_len
> 0xffff) {
803 dev_warn(dev
, "transmit period of %uus truncated to %uus\n",
804 txbuf
[i
], redrat3_len_to_us(0xffff));
805 cur_sample_len
= 0xffff;
807 for (lencheck
= 0; lencheck
< curlencheck
; lencheck
++) {
808 if (sample_lens
[lencheck
] == cur_sample_len
)
811 if (lencheck
== curlencheck
) {
812 dev_dbg(dev
, "txbuf[%d]=%u, pos %d, enc %u\n",
813 i
, txbuf
[i
], curlencheck
, cur_sample_len
);
814 if (curlencheck
< RR3_DRIVER_MAXLENS
) {
815 /* now convert the value to a proper
817 sample_lens
[curlencheck
] = cur_sample_len
;
818 put_unaligned_be16(cur_sample_len
,
819 &irdata
->lens
[curlencheck
]);
826 irdata
->sigdata
[i
] = lencheck
;
829 irdata
->sigdata
[count
] = RR3_END_OF_SIGNAL
;
830 irdata
->sigdata
[count
+ 1] = RR3_END_OF_SIGNAL
;
832 sendbuf_len
= offsetof(struct redrat3_irdata
,
833 sigdata
[count
+ RR3_TX_TRAILER_LEN
]);
834 /* fill in our packet header */
835 irdata
->header
.length
= cpu_to_be16(sendbuf_len
-
836 sizeof(struct redrat3_header
));
837 irdata
->header
.transfer_type
= cpu_to_be16(RR3_MOD_SIGNAL_OUT
);
838 irdata
->pause
= cpu_to_be32(redrat3_len_to_us(100));
839 irdata
->mod_freq_count
= cpu_to_be16(mod_freq_to_val(rr3
->carrier
));
840 irdata
->no_lengths
= curlencheck
;
841 irdata
->sig_size
= cpu_to_be16(count
+ RR3_TX_TRAILER_LEN
);
843 pipe
= usb_sndbulkpipe(rr3
->udev
, rr3
->ep_out
->bEndpointAddress
);
844 ret
= usb_bulk_msg(rr3
->udev
, pipe
, irdata
,
845 sendbuf_len
, &ret_len
, 10 * HZ
);
846 dev_dbg(dev
, "sent %d bytes, (ret %d)\n", ret_len
, ret
);
848 /* now tell the hardware to transmit what we sent it */
849 pipe
= usb_rcvctrlpipe(rr3
->udev
, 0);
850 ret
= usb_control_msg(rr3
->udev
, pipe
, RR3_TX_SEND_SIGNAL
,
851 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
852 0, 0, irdata
, 2, HZ
* 10);
855 dev_err(dev
, "Error: control msg send failed, rc %d\n", ret
);
863 rr3
->transmitting
= false;
864 /* rr3 re-enables rc detector because it was enabled before */
869 static void redrat3_brightness_set(struct led_classdev
*led_dev
, enum
870 led_brightness brightness
)
872 struct redrat3_dev
*rr3
= container_of(led_dev
, struct redrat3_dev
,
875 if (brightness
!= LED_OFF
&& atomic_cmpxchg(&rr3
->flash
, 0, 1) == 0) {
876 int ret
= usb_submit_urb(rr3
->flash_urb
, GFP_ATOMIC
);
878 dev_dbg(rr3
->dev
, "%s: unexpected ret of %d\n",
880 atomic_set(&rr3
->flash
, 0);
885 static void redrat3_led_complete(struct urb
*urb
)
887 struct redrat3_dev
*rr3
= urb
->context
;
889 switch (urb
->status
) {
899 dev_dbg(rr3
->dev
, "Error: urb status = %d\n", urb
->status
);
903 rr3
->led
.brightness
= LED_OFF
;
904 atomic_dec(&rr3
->flash
);
907 static struct rc_dev
*redrat3_init_rc_dev(struct redrat3_dev
*rr3
)
909 struct device
*dev
= rr3
->dev
;
912 u16 prod
= le16_to_cpu(rr3
->udev
->descriptor
.idProduct
);
914 rc
= rc_allocate_device();
916 dev_err(dev
, "remote input dev allocation failed\n");
920 snprintf(rr3
->name
, sizeof(rr3
->name
), "RedRat3%s "
921 "Infrared Remote Transceiver (%04x:%04x)",
922 prod
== USB_RR3IIUSB_PRODUCT_ID
? "-II" : "",
923 le16_to_cpu(rr3
->udev
->descriptor
.idVendor
), prod
);
925 usb_make_path(rr3
->udev
, rr3
->phys
, sizeof(rr3
->phys
));
927 rc
->input_name
= rr3
->name
;
928 rc
->input_phys
= rr3
->phys
;
929 usb_to_input_id(rr3
->udev
, &rc
->input_id
);
930 rc
->dev
.parent
= dev
;
932 rc
->driver_type
= RC_DRIVER_IR_RAW
;
933 rc
->allowed_protocols
= RC_BIT_ALL
;
934 rc
->min_timeout
= MS_TO_NS(RR3_RX_MIN_TIMEOUT
);
935 rc
->max_timeout
= MS_TO_NS(RR3_RX_MAX_TIMEOUT
);
936 rc
->timeout
= US_TO_NS(redrat3_get_timeout(rr3
));
937 rc
->s_timeout
= redrat3_set_timeout
;
938 rc
->tx_ir
= redrat3_transmit_ir
;
939 rc
->s_tx_carrier
= redrat3_set_tx_carrier
;
940 rc
->driver_name
= DRIVER_NAME
;
941 rc
->rx_resolution
= US_TO_NS(2);
942 rc
->map_name
= RC_MAP_HAUPPAUGE
;
944 ret
= rc_register_device(rc
);
946 dev_err(dev
, "remote dev registration failed\n");
957 static int redrat3_dev_probe(struct usb_interface
*intf
,
958 const struct usb_device_id
*id
)
960 struct usb_device
*udev
= interface_to_usbdev(intf
);
961 struct device
*dev
= &intf
->dev
;
962 struct usb_host_interface
*uhi
;
963 struct redrat3_dev
*rr3
;
964 struct usb_endpoint_descriptor
*ep
;
965 struct usb_endpoint_descriptor
*ep_in
= NULL
;
966 struct usb_endpoint_descriptor
*ep_out
= NULL
;
969 int retval
= -ENOMEM
;
971 uhi
= intf
->cur_altsetting
;
973 /* find our bulk-in and bulk-out endpoints */
974 for (i
= 0; i
< uhi
->desc
.bNumEndpoints
; ++i
) {
975 ep
= &uhi
->endpoint
[i
].desc
;
976 addr
= ep
->bEndpointAddress
;
977 attrs
= ep
->bmAttributes
;
979 if ((ep_in
== NULL
) &&
980 ((addr
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
) &&
981 ((attrs
& USB_ENDPOINT_XFERTYPE_MASK
) ==
982 USB_ENDPOINT_XFER_BULK
)) {
983 dev_dbg(dev
, "found bulk-in endpoint at 0x%02x\n",
984 ep
->bEndpointAddress
);
985 /* data comes in on 0x82, 0x81 is for other data... */
986 if (ep
->bEndpointAddress
== RR3_BULK_IN_EP_ADDR
)
990 if ((ep_out
== NULL
) &&
991 ((addr
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) &&
992 ((attrs
& USB_ENDPOINT_XFERTYPE_MASK
) ==
993 USB_ENDPOINT_XFER_BULK
)) {
994 dev_dbg(dev
, "found bulk-out endpoint at 0x%02x\n",
995 ep
->bEndpointAddress
);
1000 if (!ep_in
|| !ep_out
) {
1001 dev_err(dev
, "Couldn't find both in and out endpoints\n");
1006 /* allocate memory for our device state and initialize it */
1007 rr3
= kzalloc(sizeof(*rr3
), GFP_KERNEL
);
1009 dev_err(dev
, "Memory allocation failure\n");
1013 rr3
->dev
= &intf
->dev
;
1015 /* set up bulk-in endpoint */
1016 rr3
->read_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1021 rr3
->bulk_in_buf
= usb_alloc_coherent(udev
,
1022 le16_to_cpu(ep_in
->wMaxPacketSize
), GFP_KERNEL
, &rr3
->dma_in
);
1023 if (!rr3
->bulk_in_buf
) {
1024 dev_err(dev
, "Read buffer allocation failure\n");
1028 pipe
= usb_rcvbulkpipe(udev
, ep_in
->bEndpointAddress
);
1029 usb_fill_bulk_urb(rr3
->read_urb
, udev
, pipe
, rr3
->bulk_in_buf
,
1030 le16_to_cpu(ep_in
->wMaxPacketSize
), redrat3_handle_async
, rr3
);
1031 rr3
->read_urb
->transfer_dma
= rr3
->dma_in
;
1032 rr3
->read_urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
1034 rr3
->ep_out
= ep_out
;
1038 redrat3_get_firmware_rev(rr3
);
1040 /* might be all we need to do? */
1041 retval
= redrat3_enable_detector(rr3
);
1045 /* default.. will get overridden by any sends with a freq defined */
1046 rr3
->carrier
= 38000;
1049 rr3
->led
.name
= "redrat3:red:feedback";
1050 rr3
->led
.default_trigger
= "rc-feedback";
1051 rr3
->led
.brightness_set
= redrat3_brightness_set
;
1052 retval
= led_classdev_register(&intf
->dev
, &rr3
->led
);
1056 atomic_set(&rr3
->flash
, 0);
1057 rr3
->flash_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1058 if (!rr3
->flash_urb
) {
1060 goto led_free_error
;
1063 /* setup packet is 'c0 b9 0000 0000 0001' */
1064 rr3
->flash_control
.bRequestType
= 0xc0;
1065 rr3
->flash_control
.bRequest
= RR3_BLINK_LED
;
1066 rr3
->flash_control
.wLength
= cpu_to_le16(1);
1068 usb_fill_control_urb(rr3
->flash_urb
, udev
, usb_rcvctrlpipe(udev
, 0),
1069 (unsigned char *)&rr3
->flash_control
,
1070 &rr3
->flash_in_buf
, sizeof(rr3
->flash_in_buf
),
1071 redrat3_led_complete
, rr3
);
1073 rr3
->rc
= redrat3_init_rc_dev(rr3
);
1076 goto led_free_error
;
1079 /* we can register the device now, as it is ready */
1080 usb_set_intfdata(intf
, rr3
);
1085 led_classdev_unregister(&rr3
->led
);
1087 redrat3_delete(rr3
, rr3
->udev
);
1090 dev_err(dev
, "%s: retval = %x", __func__
, retval
);
1095 static void redrat3_dev_disconnect(struct usb_interface
*intf
)
1097 struct usb_device
*udev
= interface_to_usbdev(intf
);
1098 struct redrat3_dev
*rr3
= usb_get_intfdata(intf
);
1103 usb_set_intfdata(intf
, NULL
);
1104 rc_unregister_device(rr3
->rc
);
1105 led_classdev_unregister(&rr3
->led
);
1106 redrat3_delete(rr3
, udev
);
1109 static int redrat3_dev_suspend(struct usb_interface
*intf
, pm_message_t message
)
1111 struct redrat3_dev
*rr3
= usb_get_intfdata(intf
);
1113 led_classdev_suspend(&rr3
->led
);
1114 usb_kill_urb(rr3
->read_urb
);
1115 usb_kill_urb(rr3
->flash_urb
);
1119 static int redrat3_dev_resume(struct usb_interface
*intf
)
1121 struct redrat3_dev
*rr3
= usb_get_intfdata(intf
);
1123 if (usb_submit_urb(rr3
->read_urb
, GFP_ATOMIC
))
1125 led_classdev_resume(&rr3
->led
);
1129 static struct usb_driver redrat3_dev_driver
= {
1130 .name
= DRIVER_NAME
,
1131 .probe
= redrat3_dev_probe
,
1132 .disconnect
= redrat3_dev_disconnect
,
1133 .suspend
= redrat3_dev_suspend
,
1134 .resume
= redrat3_dev_resume
,
1135 .reset_resume
= redrat3_dev_resume
,
1136 .id_table
= redrat3_dev_table
1139 module_usb_driver(redrat3_dev_driver
);
1141 MODULE_DESCRIPTION(DRIVER_DESC
);
1142 MODULE_AUTHOR(DRIVER_AUTHOR
);
1143 MODULE_AUTHOR(DRIVER_AUTHOR2
);
1144 MODULE_LICENSE("GPL");
1145 MODULE_DEVICE_TABLE(usb
, redrat3_dev_table
);