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/module.h>
51 #include <linux/slab.h>
52 #include <linux/usb.h>
53 #include <linux/usb/input.h>
54 #include <media/rc-core.h>
56 /* Driver Information */
57 #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
58 #define DRIVER_AUTHOR2 "The Dweller, Stephen Cox"
59 #define DRIVER_DESC "RedRat3 USB IR Transceiver Driver"
60 #define DRIVER_NAME "redrat3"
62 /* module parameters */
63 #ifdef CONFIG_USB_DEBUG
69 #define RR3_DEBUG_STANDARD 0x1
70 #define RR3_DEBUG_FUNCTION_TRACE 0x2
72 #define rr3_dbg(dev, fmt, ...) \
74 if (debug & RR3_DEBUG_STANDARD) \
75 dev_info(dev, fmt, ## __VA_ARGS__); \
78 #define rr3_ftr(dev, fmt, ...) \
80 if (debug & RR3_DEBUG_FUNCTION_TRACE) \
81 dev_info(dev, fmt, ## __VA_ARGS__); \
84 /* bulk data transfer types */
85 #define RR3_ERROR 0x01
86 #define RR3_MOD_SIGNAL_IN 0x20
87 #define RR3_MOD_SIGNAL_OUT 0x21
89 /* Get the RR firmware version */
90 #define RR3_FW_VERSION 0xb1
91 #define RR3_FW_VERSION_LEN 64
92 /* Send encoded signal bulk-sent earlier*/
93 #define RR3_TX_SEND_SIGNAL 0xb3
94 #define RR3_SET_IR_PARAM 0xb7
95 #define RR3_GET_IR_PARAM 0xb8
96 /* Blink the red LED on the device */
97 #define RR3_BLINK_LED 0xb9
98 /* Read serial number of device */
99 #define RR3_READ_SER_NO 0xba
100 #define RR3_SER_NO_LEN 4
101 /* Start capture with the RC receiver */
102 #define RR3_RC_DET_ENABLE 0xbb
103 /* Stop capture with the RC receiver */
104 #define RR3_RC_DET_DISABLE 0xbc
105 /* Return the status of RC detector capture */
106 #define RR3_RC_DET_STATUS 0xbd
108 #define RR3_RESET 0xa0
110 /* Max number of lengths in the signal. */
111 #define RR3_IR_IO_MAX_LENGTHS 0x01
112 /* Periods to measure mod. freq. */
113 #define RR3_IR_IO_PERIODS_MF 0x02
114 /* Size of memory for main signal data */
115 #define RR3_IR_IO_SIG_MEM_SIZE 0x03
116 /* Delta value when measuring lengths */
117 #define RR3_IR_IO_LENGTH_FUZZ 0x04
118 /* Timeout for end of signal detection */
119 #define RR3_IR_IO_SIG_TIMEOUT 0x05
120 /* Minumum value for pause recognition. */
121 #define RR3_IR_IO_MIN_PAUSE 0x06
123 /* Clock freq. of EZ-USB chip */
124 #define RR3_CLK 24000000
125 /* Clock periods per timer count */
126 #define RR3_CLK_PER_COUNT 12
127 /* (RR3_CLK / RR3_CLK_PER_COUNT) */
128 #define RR3_CLK_CONV_FACTOR 2000000
129 /* USB bulk-in IR data endpoint address */
130 #define RR3_BULK_IN_EP_ADDR 0x82
132 /* Size of the fixed-length portion of the signal */
133 #define RR3_DRIVER_MAXLENS 128
134 #define RR3_MAX_SIG_SIZE 512
135 #define RR3_TIME_UNIT 50
136 #define RR3_END_OF_SIGNAL 0x7f
137 #define RR3_TX_TRAILER_LEN 2
138 #define RR3_RX_MIN_TIMEOUT 5
139 #define RR3_RX_MAX_TIMEOUT 2000
141 /* The 8051's CPUCS Register address */
142 #define RR3_CPUCS_REG_ADDR 0x7f92
144 #define USB_RR3USB_VENDOR_ID 0x112a
145 #define USB_RR3USB_PRODUCT_ID 0x0001
146 #define USB_RR3IIUSB_PRODUCT_ID 0x0005
148 struct redrat3_header
{
150 __be16 transfer_type
;
153 /* sending and receiving irdata */
154 struct redrat3_irdata
{
155 struct redrat3_header header
;
157 __be16 mod_freq_count
;
164 __be16 lens
[RR3_DRIVER_MAXLENS
]; /* not aligned */
165 __u8 sigdata
[RR3_MAX_SIG_SIZE
];
168 /* firmware errors */
169 struct redrat3_error
{
170 struct redrat3_header header
;
174 /* table of devices that work with this driver */
175 static struct usb_device_id redrat3_dev_table
[] = {
176 /* Original version of the RedRat3 */
177 {USB_DEVICE(USB_RR3USB_VENDOR_ID
, USB_RR3USB_PRODUCT_ID
)},
178 /* Second Version/release of the RedRat3 - RetRat3-II */
179 {USB_DEVICE(USB_RR3USB_VENDOR_ID
, USB_RR3IIUSB_PRODUCT_ID
)},
180 {} /* Terminating entry */
183 /* Structure to hold all of our device specific stuff */
185 /* core device bits */
189 /* save off the usb device pointer */
190 struct usb_device
*udev
;
192 /* the receive endpoint */
193 struct usb_endpoint_descriptor
*ep_in
;
194 /* the buffer to receive data */
196 /* urb used to read ir data */
197 struct urb
*read_urb
;
199 /* the send endpoint */
200 struct usb_endpoint_descriptor
*ep_out
;
205 /* rx signal timeout timer */
206 struct timer_list rx_timeout
;
209 /* is the detector enabled*/
211 /* Is the device currently transmitting?*/
214 /* store for current packet */
215 struct redrat3_irdata irdata
;
225 * redrat3_issue_async
227 * Issues an async read to the ir data in port..
228 * sets the callback to be redrat3_handle_async
230 static void redrat3_issue_async(struct redrat3_dev
*rr3
)
234 rr3_ftr(rr3
->dev
, "Entering %s\n", __func__
);
236 res
= usb_submit_urb(rr3
->read_urb
, GFP_ATOMIC
);
238 rr3_dbg(rr3
->dev
, "%s: receive request FAILED! "
239 "(res %d, len %d)\n", __func__
, res
,
240 rr3
->read_urb
->transfer_buffer_length
);
243 static void redrat3_dump_fw_error(struct redrat3_dev
*rr3
, int code
)
245 if (!rr3
->transmitting
&& (code
!= 0x40))
246 dev_info(rr3
->dev
, "fw error code 0x%02x: ", code
);
250 pr_cont("No Error\n");
253 /* Codes 0x20 through 0x2f are IR Firmware Errors */
255 pr_cont("Initial signal pulse not long enough "
256 "to measure carrier frequency\n");
259 pr_cont("Not enough length values allocated for signal\n");
262 pr_cont("Not enough memory allocated for signal data\n");
265 pr_cont("Too many signal repeats\n");
268 pr_cont("Insufficient memory available for IR signal "
269 "data memory allocation\n");
272 pr_cont("Insufficient memory available "
273 "for IrDa signal data memory allocation\n");
276 /* Codes 0x30 through 0x3f are USB Firmware Errors */
278 pr_cont("Insufficient memory available for bulk "
279 "transfer structure\n");
283 * Other error codes... These are primarily errors that can occur in
284 * the control messages sent to the redrat
287 if (!rr3
->transmitting
)
288 pr_cont("Signal capture has been terminated\n");
291 pr_cont("Attempt to set/get and unknown signal I/O "
292 "algorithm parameter\n");
295 pr_cont("Signal capture already started\n");
299 pr_cont("Unknown Error\n");
304 static u32
redrat3_val_to_mod_freq(struct redrat3_irdata
*irdata
)
307 u16 mod_freq_count
= be16_to_cpu(irdata
->mod_freq_count
);
309 if (mod_freq_count
!= 0)
310 mod_freq
= (RR3_CLK
* be16_to_cpu(irdata
->num_periods
)) /
311 (mod_freq_count
* RR3_CLK_PER_COUNT
);
316 /* this function scales down the figures for the same result... */
317 static u32
redrat3_len_to_us(u32 length
)
319 u32 biglen
= length
* 1000;
320 u32 divisor
= (RR3_CLK_CONV_FACTOR
) / 1000;
321 u32 result
= (u32
) (biglen
/ divisor
);
323 /* don't allow zero lengths to go back, breaks lirc */
324 return result
? result
: 1;
328 * convert us back into redrat3 lengths
330 * length * 1000 length * 1000000
331 * ------------- = ---------------- = micro
332 * rr3clk / 1000 rr3clk
334 * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
335 * ----- = 4 ----- = 6 -------------- = len ---------------------
338 static u32
redrat3_us_to_len(u32 microsec
)
343 microsec
&= IR_MAX_DURATION
;
344 divisor
= (RR3_CLK_CONV_FACTOR
/ 1000);
345 result
= (u32
)(microsec
* divisor
) / 1000;
347 /* don't allow zero lengths to go back, breaks lirc */
348 return result
? result
: 1;
351 /* timer callback to send reset event */
352 static void redrat3_rx_timeout(unsigned long data
)
354 struct redrat3_dev
*rr3
= (struct redrat3_dev
*)data
;
356 rr3_dbg(rr3
->dev
, "calling ir_raw_event_reset\n");
357 ir_raw_event_reset(rr3
->rc
);
360 static void redrat3_process_ir_data(struct redrat3_dev
*rr3
)
362 DEFINE_IR_RAW_EVENT(rawir
);
364 unsigned i
, trailer
= 0;
365 unsigned sig_size
, single_len
, offset
, val
;
370 pr_err("%s called with no context!\n", __func__
);
374 rr3_ftr(rr3
->dev
, "Entered %s\n", __func__
);
378 /* Make sure we reset the IR kfifo after a bit of inactivity */
379 delay
= usecs_to_jiffies(rr3
->hw_timeout
);
380 mod_timer(&rr3
->rx_timeout
, jiffies
+ delay
);
382 mod_freq
= redrat3_val_to_mod_freq(&rr3
->irdata
);
383 rr3_dbg(dev
, "Got mod_freq of %u\n", mod_freq
);
385 /* process each rr3 encoded byte into an int */
386 sig_size
= be16_to_cpu(rr3
->irdata
.sig_size
);
387 for (i
= 0; i
< sig_size
; i
++) {
388 offset
= rr3
->irdata
.sigdata
[i
];
389 val
= get_unaligned_be16(&rr3
->irdata
.lens
[offset
]);
390 single_len
= redrat3_len_to_us(val
);
392 /* we should always get pulse/space/pulse/space samples */
398 rawir
.duration
= US_TO_NS(single_len
);
399 /* Save initial pulse length to fudge trailer */
401 trailer
= rawir
.duration
;
402 /* cap the value to IR_MAX_DURATION */
403 rawir
.duration
&= IR_MAX_DURATION
;
405 rr3_dbg(dev
, "storing %s with duration %d (i: %d)\n",
406 rawir
.pulse
? "pulse" : "space", rawir
.duration
, i
);
407 ir_raw_event_store_with_filter(rr3
->rc
, &rawir
);
410 /* add a trailing space, if need be */
413 /* this duration is made up, and may not be ideal... */
414 if (trailer
< US_TO_NS(1000))
415 rawir
.duration
= US_TO_NS(2800);
417 rawir
.duration
= trailer
;
418 rr3_dbg(dev
, "storing trailing space with duration %d\n",
420 ir_raw_event_store_with_filter(rr3
->rc
, &rawir
);
423 rr3_dbg(dev
, "calling ir_raw_event_handle\n");
424 ir_raw_event_handle(rr3
->rc
);
427 /* Util fn to send rr3 cmds */
428 static u8
redrat3_send_cmd(int cmd
, struct redrat3_dev
*rr3
)
430 struct usb_device
*udev
;
434 data
= kzalloc(sizeof(u8
), GFP_KERNEL
);
439 res
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0), cmd
,
440 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
441 0x0000, 0x0000, data
, sizeof(u8
), HZ
* 10);
444 dev_err(rr3
->dev
, "%s: Error sending rr3 cmd res %d, data %d",
445 __func__
, res
, *data
);
455 /* Enables the long range detector and starts async receive */
456 static int redrat3_enable_detector(struct redrat3_dev
*rr3
)
458 struct device
*dev
= rr3
->dev
;
461 rr3_ftr(dev
, "Entering %s\n", __func__
);
463 ret
= redrat3_send_cmd(RR3_RC_DET_ENABLE
, rr3
);
465 dev_dbg(dev
, "%s: unexpected ret of %d\n",
468 ret
= redrat3_send_cmd(RR3_RC_DET_STATUS
, rr3
);
470 dev_err(dev
, "%s: detector status: %d, should be 1\n",
475 rr3
->det_enabled
= true;
476 redrat3_issue_async(rr3
);
481 /* Disables the rr3 long range detector */
482 static void redrat3_disable_detector(struct redrat3_dev
*rr3
)
484 struct device
*dev
= rr3
->dev
;
487 rr3_ftr(dev
, "Entering %s\n", __func__
);
489 ret
= redrat3_send_cmd(RR3_RC_DET_DISABLE
, rr3
);
491 dev_err(dev
, "%s: failure!\n", __func__
);
493 ret
= redrat3_send_cmd(RR3_RC_DET_STATUS
, rr3
);
495 dev_warn(dev
, "%s: detector status: %d, should be 0\n",
498 rr3
->det_enabled
= false;
501 static inline void redrat3_delete(struct redrat3_dev
*rr3
,
502 struct usb_device
*udev
)
504 rr3_ftr(rr3
->dev
, "%s cleaning up\n", __func__
);
505 usb_kill_urb(rr3
->read_urb
);
507 usb_free_urb(rr3
->read_urb
);
509 usb_free_coherent(udev
, le16_to_cpu(rr3
->ep_in
->wMaxPacketSize
),
510 rr3
->bulk_in_buf
, rr3
->dma_in
);
515 static u32
redrat3_get_timeout(struct redrat3_dev
*rr3
)
518 u32 timeout
= MS_TO_US(150); /* a sane default, if things go haywire */
522 tmp
= kzalloc(len
, GFP_KERNEL
);
524 dev_warn(rr3
->dev
, "Memory allocation faillure\n");
528 pipe
= usb_rcvctrlpipe(rr3
->udev
, 0);
529 ret
= usb_control_msg(rr3
->udev
, pipe
, RR3_GET_IR_PARAM
,
530 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
531 RR3_IR_IO_SIG_TIMEOUT
, 0, tmp
, len
, HZ
* 5);
533 dev_warn(rr3
->dev
, "Failed to read timeout from hardware\n");
535 timeout
= redrat3_len_to_us(be32_to_cpup(tmp
));
537 rr3_dbg(rr3
->dev
, "Got timeout of %d ms\n", timeout
/ 1000);
545 static void redrat3_reset(struct redrat3_dev
*rr3
)
547 struct usb_device
*udev
= rr3
->udev
;
548 struct device
*dev
= rr3
->dev
;
549 int rc
, rxpipe
, txpipe
;
551 int len
= sizeof(u8
);
553 rr3_ftr(dev
, "Entering %s\n", __func__
);
555 rxpipe
= usb_rcvctrlpipe(udev
, 0);
556 txpipe
= usb_sndctrlpipe(udev
, 0);
558 val
= kmalloc(len
, GFP_KERNEL
);
560 dev_err(dev
, "Memory allocation failure\n");
565 rc
= usb_control_msg(udev
, rxpipe
, RR3_RESET
,
566 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
567 RR3_CPUCS_REG_ADDR
, 0, val
, len
, HZ
* 25);
568 rr3_dbg(dev
, "reset returned 0x%02x\n", rc
);
571 rc
= usb_control_msg(udev
, txpipe
, RR3_SET_IR_PARAM
,
572 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
573 RR3_IR_IO_LENGTH_FUZZ
, 0, val
, len
, HZ
* 25);
574 rr3_dbg(dev
, "set ir parm len fuzz %d rc 0x%02x\n", *val
, rc
);
576 *val
= RR3_DRIVER_MAXLENS
;
577 rc
= usb_control_msg(udev
, txpipe
, RR3_SET_IR_PARAM
,
578 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
579 RR3_IR_IO_MAX_LENGTHS
, 0, val
, len
, HZ
* 25);
580 rr3_dbg(dev
, "set ir parm max lens %d rc 0x%02x\n", *val
, rc
);
585 static void redrat3_get_firmware_rev(struct redrat3_dev
*rr3
)
590 rr3_ftr(rr3
->dev
, "Entering %s\n", __func__
);
592 buffer
= kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN
+ 1), GFP_KERNEL
);
594 dev_err(rr3
->dev
, "Memory allocation failure\n");
598 rc
= usb_control_msg(rr3
->udev
, usb_rcvctrlpipe(rr3
->udev
, 0),
600 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
601 0, 0, buffer
, RR3_FW_VERSION_LEN
, HZ
* 5);
604 dev_info(rr3
->dev
, "Firmware rev: %s", buffer
);
606 dev_err(rr3
->dev
, "Problem fetching firmware ID\n");
609 rr3_ftr(rr3
->dev
, "Exiting %s\n", __func__
);
612 static void redrat3_read_packet_start(struct redrat3_dev
*rr3
, unsigned len
)
614 struct redrat3_header
*header
= rr3
->bulk_in_buf
;
615 unsigned pktlen
, pkttype
;
617 rr3_ftr(rr3
->dev
, "Entering %s\n", __func__
);
619 /* grab the Length and type of transfer */
620 pktlen
= be16_to_cpu(header
->length
);
621 pkttype
= be16_to_cpu(header
->transfer_type
);
623 if (pktlen
> sizeof(rr3
->irdata
)) {
624 dev_warn(rr3
->dev
, "packet length %u too large\n", pktlen
);
630 if (len
>= sizeof(struct redrat3_error
)) {
631 struct redrat3_error
*error
= rr3
->bulk_in_buf
;
632 unsigned fw_error
= be16_to_cpu(error
->fw_error
);
633 redrat3_dump_fw_error(rr3
, fw_error
);
637 case RR3_MOD_SIGNAL_IN
:
638 memcpy(&rr3
->irdata
, rr3
->bulk_in_buf
, len
);
639 rr3
->bytes_read
= len
;
640 rr3_dbg(rr3
->dev
, "bytes_read %d, pktlen %d\n",
641 rr3
->bytes_read
, pktlen
);
645 rr3_dbg(rr3
->dev
, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
646 pkttype
, len
, pktlen
);
651 static void redrat3_read_packet_continue(struct redrat3_dev
*rr3
, unsigned len
)
653 void *irdata
= &rr3
->irdata
;
655 rr3_ftr(rr3
->dev
, "Entering %s\n", __func__
);
657 if (len
+ rr3
->bytes_read
> sizeof(rr3
->irdata
)) {
658 dev_warn(rr3
->dev
, "too much data for packet\n");
663 memcpy(irdata
+ rr3
->bytes_read
, rr3
->bulk_in_buf
, len
);
665 rr3
->bytes_read
+= len
;
666 rr3_dbg(rr3
->dev
, "bytes_read %d, pktlen %d\n", rr3
->bytes_read
,
667 be16_to_cpu(rr3
->irdata
.header
.length
));
670 /* gather IR data from incoming urb, process it when we have enough */
671 static int redrat3_get_ir_data(struct redrat3_dev
*rr3
, unsigned len
)
673 struct device
*dev
= rr3
->dev
;
677 rr3_ftr(dev
, "Entering %s\n", __func__
);
679 if (rr3
->bytes_read
== 0 && len
>= sizeof(struct redrat3_header
)) {
680 redrat3_read_packet_start(rr3
, len
);
681 } else if (rr3
->bytes_read
!= 0) {
682 redrat3_read_packet_continue(rr3
, len
);
683 } else if (rr3
->bytes_read
== 0) {
684 dev_err(dev
, "error: no packet data read\n");
689 if (rr3
->bytes_read
< be16_to_cpu(rr3
->irdata
.header
.length
))
690 /* we're still accumulating data */
693 /* if we get here, we've got IR data to decode */
694 pkttype
= be16_to_cpu(rr3
->irdata
.header
.transfer_type
);
695 if (pkttype
== RR3_MOD_SIGNAL_IN
)
696 redrat3_process_ir_data(rr3
);
698 rr3_dbg(dev
, "discarding non-signal data packet (type 0x%02x)\n",
706 /* callback function from USB when async USB request has completed */
707 static void redrat3_handle_async(struct urb
*urb
)
709 struct redrat3_dev
*rr3
;
717 pr_err("%s called with invalid context!\n", __func__
);
722 rr3_ftr(rr3
->dev
, "Entering %s\n", __func__
);
724 switch (urb
->status
) {
726 ret
= redrat3_get_ir_data(rr3
, urb
->actual_length
);
728 /* no error, prepare to read more */
729 redrat3_issue_async(rr3
);
741 dev_warn(rr3
->dev
, "Error: urb status = %d\n", urb
->status
);
747 static u16
mod_freq_to_val(unsigned int mod_freq
)
751 /* Clk used in mod. freq. generation is CLK24/4. */
752 return 65536 - (mult
/ mod_freq
);
755 static int redrat3_set_tx_carrier(struct rc_dev
*rcdev
, u32 carrier
)
757 struct redrat3_dev
*rr3
= rcdev
->priv
;
758 struct device
*dev
= rr3
->dev
;
760 rr3_dbg(dev
, "Setting modulation frequency to %u", carrier
);
764 rr3
->carrier
= carrier
;
769 static int redrat3_transmit_ir(struct rc_dev
*rcdev
, unsigned *txbuf
,
772 struct redrat3_dev
*rr3
= rcdev
->priv
;
773 struct device
*dev
= rr3
->dev
;
774 struct redrat3_irdata
*irdata
= NULL
;
776 int lencheck
, cur_sample_len
, pipe
;
777 int *sample_lens
= NULL
;
779 unsigned i
, sendbuf_len
;
781 rr3_ftr(dev
, "Entering %s\n", __func__
);
783 if (rr3
->transmitting
) {
784 dev_warn(dev
, "%s: transmitter already in use\n", __func__
);
788 count
= min_t(unsigned, count
, RR3_MAX_SIG_SIZE
- RR3_TX_TRAILER_LEN
);
790 /* rr3 will disable rc detector on transmit */
791 rr3
->det_enabled
= false;
792 rr3
->transmitting
= true;
794 sample_lens
= kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS
, GFP_KERNEL
);
800 irdata
= kzalloc(sizeof(*irdata
), GFP_KERNEL
);
806 for (i
= 0; i
< count
; i
++) {
807 cur_sample_len
= redrat3_us_to_len(txbuf
[i
]);
808 if (cur_sample_len
> 0xffff) {
809 dev_warn(dev
, "transmit period of %uus truncated to %uus\n",
810 txbuf
[i
], redrat3_len_to_us(0xffff));
811 cur_sample_len
= 0xffff;
813 for (lencheck
= 0; lencheck
< curlencheck
; lencheck
++) {
814 if (sample_lens
[lencheck
] == cur_sample_len
)
817 if (lencheck
== curlencheck
) {
818 rr3_dbg(dev
, "txbuf[%d]=%u, pos %d, enc %u\n",
819 i
, txbuf
[i
], curlencheck
, cur_sample_len
);
820 if (curlencheck
< RR3_DRIVER_MAXLENS
) {
821 /* now convert the value to a proper
823 sample_lens
[curlencheck
] = cur_sample_len
;
824 put_unaligned_be16(cur_sample_len
,
825 &irdata
->lens
[curlencheck
]);
832 irdata
->sigdata
[i
] = lencheck
;
835 irdata
->sigdata
[count
] = RR3_END_OF_SIGNAL
;
836 irdata
->sigdata
[count
+ 1] = RR3_END_OF_SIGNAL
;
838 sendbuf_len
= offsetof(struct redrat3_irdata
,
839 sigdata
[count
+ RR3_TX_TRAILER_LEN
]);
840 /* fill in our packet header */
841 irdata
->header
.length
= cpu_to_be16(sendbuf_len
-
842 sizeof(struct redrat3_header
));
843 irdata
->header
.transfer_type
= cpu_to_be16(RR3_MOD_SIGNAL_OUT
);
844 irdata
->pause
= cpu_to_be32(redrat3_len_to_us(100));
845 irdata
->mod_freq_count
= cpu_to_be16(mod_freq_to_val(rr3
->carrier
));
846 irdata
->no_lengths
= curlencheck
;
847 irdata
->sig_size
= cpu_to_be16(count
+ RR3_TX_TRAILER_LEN
);
849 pipe
= usb_sndbulkpipe(rr3
->udev
, rr3
->ep_out
->bEndpointAddress
);
850 ret
= usb_bulk_msg(rr3
->udev
, pipe
, irdata
,
851 sendbuf_len
, &ret_len
, 10 * HZ
);
852 rr3_dbg(dev
, "sent %d bytes, (ret %d)\n", ret_len
, ret
);
854 /* now tell the hardware to transmit what we sent it */
855 pipe
= usb_rcvctrlpipe(rr3
->udev
, 0);
856 ret
= usb_control_msg(rr3
->udev
, pipe
, RR3_TX_SEND_SIGNAL
,
857 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
858 0, 0, irdata
, 2, HZ
* 10);
861 dev_err(dev
, "Error: control msg send failed, rc %d\n", ret
);
869 rr3
->transmitting
= false;
870 /* rr3 re-enables rc detector because it was enabled before */
871 rr3
->det_enabled
= true;
876 static struct rc_dev
*redrat3_init_rc_dev(struct redrat3_dev
*rr3
)
878 struct device
*dev
= rr3
->dev
;
881 u16 prod
= le16_to_cpu(rr3
->udev
->descriptor
.idProduct
);
883 rc
= rc_allocate_device();
885 dev_err(dev
, "remote input dev allocation failed\n");
889 snprintf(rr3
->name
, sizeof(rr3
->name
), "RedRat3%s "
890 "Infrared Remote Transceiver (%04x:%04x)",
891 prod
== USB_RR3IIUSB_PRODUCT_ID
? "-II" : "",
892 le16_to_cpu(rr3
->udev
->descriptor
.idVendor
), prod
);
894 usb_make_path(rr3
->udev
, rr3
->phys
, sizeof(rr3
->phys
));
896 rc
->input_name
= rr3
->name
;
897 rc
->input_phys
= rr3
->phys
;
898 usb_to_input_id(rr3
->udev
, &rc
->input_id
);
899 rc
->dev
.parent
= dev
;
901 rc
->driver_type
= RC_DRIVER_IR_RAW
;
902 rc
->allowed_protos
= RC_BIT_ALL
;
903 rc
->timeout
= US_TO_NS(2750);
904 rc
->tx_ir
= redrat3_transmit_ir
;
905 rc
->s_tx_carrier
= redrat3_set_tx_carrier
;
906 rc
->driver_name
= DRIVER_NAME
;
907 rc
->rx_resolution
= US_TO_NS(2);
908 rc
->map_name
= RC_MAP_HAUPPAUGE
;
910 ret
= rc_register_device(rc
);
912 dev_err(dev
, "remote dev registration failed\n");
923 static int redrat3_dev_probe(struct usb_interface
*intf
,
924 const struct usb_device_id
*id
)
926 struct usb_device
*udev
= interface_to_usbdev(intf
);
927 struct device
*dev
= &intf
->dev
;
928 struct usb_host_interface
*uhi
;
929 struct redrat3_dev
*rr3
;
930 struct usb_endpoint_descriptor
*ep
;
931 struct usb_endpoint_descriptor
*ep_in
= NULL
;
932 struct usb_endpoint_descriptor
*ep_out
= NULL
;
935 int retval
= -ENOMEM
;
937 rr3_ftr(dev
, "%s called\n", __func__
);
939 uhi
= intf
->cur_altsetting
;
941 /* find our bulk-in and bulk-out endpoints */
942 for (i
= 0; i
< uhi
->desc
.bNumEndpoints
; ++i
) {
943 ep
= &uhi
->endpoint
[i
].desc
;
944 addr
= ep
->bEndpointAddress
;
945 attrs
= ep
->bmAttributes
;
947 if ((ep_in
== NULL
) &&
948 ((addr
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
) &&
949 ((attrs
& USB_ENDPOINT_XFERTYPE_MASK
) ==
950 USB_ENDPOINT_XFER_BULK
)) {
951 rr3_dbg(dev
, "found bulk-in endpoint at 0x%02x\n",
952 ep
->bEndpointAddress
);
953 /* data comes in on 0x82, 0x81 is for other data... */
954 if (ep
->bEndpointAddress
== RR3_BULK_IN_EP_ADDR
)
958 if ((ep_out
== NULL
) &&
959 ((addr
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) &&
960 ((attrs
& USB_ENDPOINT_XFERTYPE_MASK
) ==
961 USB_ENDPOINT_XFER_BULK
)) {
962 rr3_dbg(dev
, "found bulk-out endpoint at 0x%02x\n",
963 ep
->bEndpointAddress
);
968 if (!ep_in
|| !ep_out
) {
969 dev_err(dev
, "Couldn't find both in and out endpoints\n");
974 /* allocate memory for our device state and initialize it */
975 rr3
= kzalloc(sizeof(*rr3
), GFP_KERNEL
);
977 dev_err(dev
, "Memory allocation failure\n");
981 rr3
->dev
= &intf
->dev
;
983 /* set up bulk-in endpoint */
984 rr3
->read_urb
= usb_alloc_urb(0, GFP_KERNEL
);
985 if (!rr3
->read_urb
) {
986 dev_err(dev
, "Read urb allocation failure\n");
991 rr3
->bulk_in_buf
= usb_alloc_coherent(udev
,
992 le16_to_cpu(ep_in
->wMaxPacketSize
), GFP_ATOMIC
, &rr3
->dma_in
);
993 if (!rr3
->bulk_in_buf
) {
994 dev_err(dev
, "Read buffer allocation failure\n");
998 pipe
= usb_rcvbulkpipe(udev
, ep_in
->bEndpointAddress
);
999 usb_fill_bulk_urb(rr3
->read_urb
, udev
, pipe
, rr3
->bulk_in_buf
,
1000 le16_to_cpu(ep_in
->wMaxPacketSize
), redrat3_handle_async
, rr3
);
1002 rr3
->ep_out
= ep_out
;
1006 redrat3_get_firmware_rev(rr3
);
1008 /* might be all we need to do? */
1009 retval
= redrat3_enable_detector(rr3
);
1013 /* store current hardware timeout, in us, will use for kfifo resets */
1014 rr3
->hw_timeout
= redrat3_get_timeout(rr3
);
1016 /* default.. will get overridden by any sends with a freq defined */
1017 rr3
->carrier
= 38000;
1019 rr3
->rc
= redrat3_init_rc_dev(rr3
);
1024 setup_timer(&rr3
->rx_timeout
, redrat3_rx_timeout
, (unsigned long)rr3
);
1026 /* we can register the device now, as it is ready */
1027 usb_set_intfdata(intf
, rr3
);
1029 rr3_ftr(dev
, "Exiting %s\n", __func__
);
1033 redrat3_delete(rr3
, rr3
->udev
);
1036 dev_err(dev
, "%s: retval = %x", __func__
, retval
);
1041 static void redrat3_dev_disconnect(struct usb_interface
*intf
)
1043 struct usb_device
*udev
= interface_to_usbdev(intf
);
1044 struct redrat3_dev
*rr3
= usb_get_intfdata(intf
);
1046 rr3_ftr(&intf
->dev
, "Entering %s\n", __func__
);
1051 redrat3_disable_detector(rr3
);
1053 usb_set_intfdata(intf
, NULL
);
1054 rc_unregister_device(rr3
->rc
);
1055 del_timer_sync(&rr3
->rx_timeout
);
1056 redrat3_delete(rr3
, udev
);
1058 rr3_ftr(&intf
->dev
, "RedRat3 IR Transceiver now disconnected\n");
1061 static int redrat3_dev_suspend(struct usb_interface
*intf
, pm_message_t message
)
1063 struct redrat3_dev
*rr3
= usb_get_intfdata(intf
);
1064 rr3_ftr(rr3
->dev
, "suspend\n");
1065 usb_kill_urb(rr3
->read_urb
);
1069 static int redrat3_dev_resume(struct usb_interface
*intf
)
1071 struct redrat3_dev
*rr3
= usb_get_intfdata(intf
);
1072 rr3_ftr(rr3
->dev
, "resume\n");
1073 if (usb_submit_urb(rr3
->read_urb
, GFP_ATOMIC
))
1078 static struct usb_driver redrat3_dev_driver
= {
1079 .name
= DRIVER_NAME
,
1080 .probe
= redrat3_dev_probe
,
1081 .disconnect
= redrat3_dev_disconnect
,
1082 .suspend
= redrat3_dev_suspend
,
1083 .resume
= redrat3_dev_resume
,
1084 .reset_resume
= redrat3_dev_resume
,
1085 .id_table
= redrat3_dev_table
1088 module_usb_driver(redrat3_dev_driver
);
1090 MODULE_DESCRIPTION(DRIVER_DESC
);
1091 MODULE_AUTHOR(DRIVER_AUTHOR
);
1092 MODULE_AUTHOR(DRIVER_AUTHOR2
);
1093 MODULE_LICENSE("GPL");
1094 MODULE_DEVICE_TABLE(usb
, redrat3_dev_table
);
1096 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
1097 MODULE_PARM_DESC(debug
, "Enable module debug spew. 0 = no debugging (default) "
1098 "0x1 = standard debug messages, 0x2 = function tracing debug. "
1099 "Flag bits are addative (i.e., 0x3 for both debug types).");