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 <linux/device.h>
49 #include <linux/module.h>
50 #include <linux/slab.h>
51 #include <linux/usb.h>
52 #include <linux/usb/input.h>
53 #include <media/rc-core.h>
55 /* Driver Information */
56 #define DRIVER_VERSION "0.70"
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 /* Raw Modulated signal data value offsets */
133 #define RR3_PAUSE_OFFSET 0
134 #define RR3_FREQ_COUNT_OFFSET 4
135 #define RR3_NUM_PERIOD_OFFSET 6
136 #define RR3_MAX_LENGTHS_OFFSET 8
137 #define RR3_NUM_LENGTHS_OFFSET 9
138 #define RR3_MAX_SIGS_OFFSET 10
139 #define RR3_NUM_SIGS_OFFSET 12
140 #define RR3_REPEATS_OFFSET 14
142 /* Size of the fixed-length portion of the signal */
143 #define RR3_HEADER_LENGTH 15
144 #define RR3_DRIVER_MAXLENS 128
145 #define RR3_MAX_SIG_SIZE 512
146 #define RR3_MAX_BUF_SIZE \
147 ((2 * RR3_HEADER_LENGTH) + RR3_DRIVER_MAXLENS + RR3_MAX_SIG_SIZE)
148 #define RR3_TIME_UNIT 50
149 #define RR3_END_OF_SIGNAL 0x7f
150 #define RR3_TX_HEADER_OFFSET 4
151 #define RR3_TX_TRAILER_LEN 2
152 #define RR3_RX_MIN_TIMEOUT 5
153 #define RR3_RX_MAX_TIMEOUT 2000
155 /* The 8051's CPUCS Register address */
156 #define RR3_CPUCS_REG_ADDR 0x7f92
158 #define USB_RR3USB_VENDOR_ID 0x112a
159 #define USB_RR3USB_PRODUCT_ID 0x0001
160 #define USB_RR3IIUSB_PRODUCT_ID 0x0005
162 /* table of devices that work with this driver */
163 static struct usb_device_id redrat3_dev_table
[] = {
164 /* Original version of the RedRat3 */
165 {USB_DEVICE(USB_RR3USB_VENDOR_ID
, USB_RR3USB_PRODUCT_ID
)},
166 /* Second Version/release of the RedRat3 - RetRat3-II */
167 {USB_DEVICE(USB_RR3USB_VENDOR_ID
, USB_RR3IIUSB_PRODUCT_ID
)},
168 {} /* Terminating entry */
171 /* Structure to hold all of our device specific stuff */
173 /* core device bits */
177 /* save off the usb device pointer */
178 struct usb_device
*udev
;
180 /* the receive endpoint */
181 struct usb_endpoint_descriptor
*ep_in
;
182 /* the buffer to receive data */
183 unsigned char *bulk_in_buf
;
184 /* urb used to read ir data */
185 struct urb
*read_urb
;
187 /* the send endpoint */
188 struct usb_endpoint_descriptor
*ep_out
;
189 /* the buffer to send data */
190 unsigned char *bulk_out_buf
;
191 /* the urb used to send data */
192 struct urb
*write_urb
;
198 /* true if write urb is busy */
200 /* wait for the write to finish */
201 struct completion write_finished
;
203 /* locks this structure */
206 /* rx signal timeout timer */
207 struct timer_list rx_timeout
;
210 /* Is the device currently receiving? */
211 bool recv_in_progress
;
212 /* is the detector enabled*/
214 /* Is the device currently transmitting?*/
217 /* store for current packet */
218 char pbuf
[RR3_MAX_BUF_SIZE
];
222 /* indicate whether we are going to reprocess
223 * the USB callback with a bigger buffer */
233 /* All incoming data buffers adhere to a very specific data format */
234 struct redrat3_signal_header
{
235 u16 length
; /* Length of data being transferred */
236 u16 transfer_type
; /* Type of data transferred */
237 u32 pause
; /* Pause between main and repeat signals */
238 u16 mod_freq_count
; /* Value of timer on mod. freq. measurement */
239 u16 no_periods
; /* No. of periods over which mod. freq. is measured */
240 u8 max_lengths
; /* Max no. of lengths (i.e. size of array) */
241 u8 no_lengths
; /* Actual no. of elements in lengths array */
242 u16 max_sig_size
; /* Max no. of values in signal data array */
243 u16 sig_size
; /* Acuto no. of values in signal data array */
244 u8 no_repeats
; /* No. of repeats of repeat signal section */
245 /* Here forward is the lengths and signal data */
248 static void redrat3_dump_signal_header(struct redrat3_signal_header
*header
)
250 pr_info("%s:\n", __func__
);
251 pr_info(" * length: %u, transfer_type: 0x%02x\n",
252 header
->length
, header
->transfer_type
);
253 pr_info(" * pause: %u, freq_count: %u, no_periods: %u\n",
254 header
->pause
, header
->mod_freq_count
, header
->no_periods
);
255 pr_info(" * lengths: %u (max: %u)\n",
256 header
->no_lengths
, header
->max_lengths
);
257 pr_info(" * sig_size: %u (max: %u)\n",
258 header
->sig_size
, header
->max_sig_size
);
259 pr_info(" * repeats: %u\n", header
->no_repeats
);
262 static void redrat3_dump_signal_data(char *buffer
, u16 len
)
267 pr_info("%s:", __func__
);
269 offset
= RR3_TX_HEADER_OFFSET
+ RR3_HEADER_LENGTH
270 + (RR3_DRIVER_MAXLENS
* sizeof(u16
));
272 /* read RR3_DRIVER_MAXLENS from ctrl msg */
273 data_vals
= buffer
+ offset
;
275 for (i
= 0; i
< len
; i
++) {
278 pr_cont("%02x ", *data_vals
++);
285 * redrat3_issue_async
287 * Issues an async read to the ir data in port..
288 * sets the callback to be redrat3_handle_async
290 static void redrat3_issue_async(struct redrat3_dev
*rr3
)
294 rr3_ftr(rr3
->dev
, "Entering %s\n", __func__
);
296 if (!rr3
->det_enabled
) {
297 dev_warn(rr3
->dev
, "not issuing async read, "
298 "detector not enabled\n");
302 memset(rr3
->bulk_in_buf
, 0, rr3
->ep_in
->wMaxPacketSize
);
303 res
= usb_submit_urb(rr3
->read_urb
, GFP_ATOMIC
);
305 rr3_dbg(rr3
->dev
, "%s: receive request FAILED! "
306 "(res %d, len %d)\n", __func__
, res
,
307 rr3
->read_urb
->transfer_buffer_length
);
310 static void redrat3_dump_fw_error(struct redrat3_dev
*rr3
, int code
)
312 if (!rr3
->transmitting
&& (code
!= 0x40))
313 dev_info(rr3
->dev
, "fw error code 0x%02x: ", code
);
317 pr_cont("No Error\n");
320 /* Codes 0x20 through 0x2f are IR Firmware Errors */
322 pr_cont("Initial signal pulse not long enough "
323 "to measure carrier frequency\n");
326 pr_cont("Not enough length values allocated for signal\n");
329 pr_cont("Not enough memory allocated for signal data\n");
332 pr_cont("Too many signal repeats\n");
335 pr_cont("Insufficient memory available for IR signal "
336 "data memory allocation\n");
339 pr_cont("Insufficient memory available "
340 "for IrDa signal data memory allocation\n");
343 /* Codes 0x30 through 0x3f are USB Firmware Errors */
345 pr_cont("Insufficient memory available for bulk "
346 "transfer structure\n");
350 * Other error codes... These are primarily errors that can occur in
351 * the control messages sent to the redrat
354 if (!rr3
->transmitting
)
355 pr_cont("Signal capture has been terminated\n");
358 pr_cont("Attempt to set/get and unknown signal I/O "
359 "algorithm parameter\n");
362 pr_cont("Signal capture already started\n");
366 pr_cont("Unknown Error\n");
371 static u32
redrat3_val_to_mod_freq(struct redrat3_signal_header
*ph
)
375 if (ph
->mod_freq_count
!= 0)
376 mod_freq
= (RR3_CLK
* ph
->no_periods
) /
377 (ph
->mod_freq_count
* RR3_CLK_PER_COUNT
);
382 /* this function scales down the figures for the same result... */
383 static u32
redrat3_len_to_us(u32 length
)
385 u32 biglen
= length
* 1000;
386 u32 divisor
= (RR3_CLK_CONV_FACTOR
) / 1000;
387 u32 result
= (u32
) (biglen
/ divisor
);
389 /* don't allow zero lengths to go back, breaks lirc */
390 return result
? result
: 1;
394 * convert us back into redrat3 lengths
396 * length * 1000 length * 1000000
397 * ------------- = ---------------- = micro
398 * rr3clk / 1000 rr3clk
400 * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
401 * ----- = 4 ----- = 6 -------------- = len ---------------------
404 static u32
redrat3_us_to_len(u32 microsec
)
409 microsec
&= IR_MAX_DURATION
;
410 divisor
= (RR3_CLK_CONV_FACTOR
/ 1000);
411 result
= (u32
)(microsec
* divisor
) / 1000;
413 /* don't allow zero lengths to go back, breaks lirc */
414 return result
? result
: 1;
418 /* timer callback to send reset event */
419 static void redrat3_rx_timeout(unsigned long data
)
421 struct redrat3_dev
*rr3
= (struct redrat3_dev
*)data
;
423 rr3_dbg(rr3
->dev
, "calling ir_raw_event_reset\n");
424 ir_raw_event_reset(rr3
->rc
);
427 static void redrat3_process_ir_data(struct redrat3_dev
*rr3
)
429 DEFINE_IR_RAW_EVENT(rawir
);
430 struct redrat3_signal_header header
;
434 u32 mod_freq
, single_len
;
442 pr_err("%s called with no context!\n", __func__
);
446 rr3_ftr(rr3
->dev
, "Entered %s\n", __func__
);
449 sig_data
= rr3
->pbuf
;
451 header
.length
= rr3
->pktlen
;
452 header
.transfer_type
= rr3
->pkttype
;
455 if (!(header
.length
>= RR3_HEADER_LENGTH
))
456 dev_warn(dev
, "read returned less than rr3 header len\n");
458 /* Make sure we reset the IR kfifo after a bit of inactivity */
459 delay
= usecs_to_jiffies(rr3
->hw_timeout
);
460 mod_timer(&rr3
->rx_timeout
, jiffies
+ delay
);
462 memcpy(&tmp32
, sig_data
+ RR3_PAUSE_OFFSET
, sizeof(tmp32
));
463 header
.pause
= be32_to_cpu(tmp32
);
465 memcpy(&tmp16
, sig_data
+ RR3_FREQ_COUNT_OFFSET
, sizeof(tmp16
));
466 header
.mod_freq_count
= be16_to_cpu(tmp16
);
468 memcpy(&tmp16
, sig_data
+ RR3_NUM_PERIOD_OFFSET
, sizeof(tmp16
));
469 header
.no_periods
= be16_to_cpu(tmp16
);
471 header
.max_lengths
= sig_data
[RR3_MAX_LENGTHS_OFFSET
];
472 header
.no_lengths
= sig_data
[RR3_NUM_LENGTHS_OFFSET
];
474 memcpy(&tmp16
, sig_data
+ RR3_MAX_SIGS_OFFSET
, sizeof(tmp16
));
475 header
.max_sig_size
= be16_to_cpu(tmp16
);
477 memcpy(&tmp16
, sig_data
+ RR3_NUM_SIGS_OFFSET
, sizeof(tmp16
));
478 header
.sig_size
= be16_to_cpu(tmp16
);
480 header
.no_repeats
= sig_data
[RR3_REPEATS_OFFSET
];
483 redrat3_dump_signal_header(&header
);
484 redrat3_dump_signal_data(sig_data
, header
.sig_size
);
487 mod_freq
= redrat3_val_to_mod_freq(&header
);
488 rr3_dbg(dev
, "Got mod_freq of %u\n", mod_freq
);
490 /* Here we pull out the 'length' values from the signal */
491 len_vals
= (u16
*)(sig_data
+ RR3_HEADER_LENGTH
);
493 data_vals
= sig_data
+ RR3_HEADER_LENGTH
+
494 (header
.max_lengths
* sizeof(u16
));
496 /* process each rr3 encoded byte into an int */
497 for (i
= 0; i
< header
.sig_size
; i
++) {
498 u16 val
= len_vals
[data_vals
[i
]];
499 single_len
= redrat3_len_to_us((u32
)be16_to_cpu(val
));
501 /* we should always get pulse/space/pulse/space samples */
507 rawir
.duration
= US_TO_NS(single_len
);
508 /* Save initial pulse length to fudge trailer */
510 trailer
= rawir
.duration
;
511 /* cap the value to IR_MAX_DURATION */
512 rawir
.duration
&= IR_MAX_DURATION
;
514 rr3_dbg(dev
, "storing %s with duration %d (i: %d)\n",
515 rawir
.pulse
? "pulse" : "space", rawir
.duration
, i
);
516 ir_raw_event_store_with_filter(rr3
->rc
, &rawir
);
519 /* add a trailing space, if need be */
522 /* this duration is made up, and may not be ideal... */
523 if (trailer
< US_TO_NS(1000))
524 rawir
.duration
= US_TO_NS(2800);
526 rawir
.duration
= trailer
;
527 rr3_dbg(dev
, "storing trailing space with duration %d\n",
529 ir_raw_event_store_with_filter(rr3
->rc
, &rawir
);
532 rr3_dbg(dev
, "calling ir_raw_event_handle\n");
533 ir_raw_event_handle(rr3
->rc
);
538 /* Util fn to send rr3 cmds */
539 static u8
redrat3_send_cmd(int cmd
, struct redrat3_dev
*rr3
)
541 struct usb_device
*udev
;
545 data
= kzalloc(sizeof(u8
), GFP_KERNEL
);
550 res
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0), cmd
,
551 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
552 0x0000, 0x0000, data
, sizeof(u8
), HZ
* 10);
555 dev_err(rr3
->dev
, "%s: Error sending rr3 cmd res %d, data %d",
556 __func__
, res
, *data
);
566 /* Enables the long range detector and starts async receive */
567 static int redrat3_enable_detector(struct redrat3_dev
*rr3
)
569 struct device
*dev
= rr3
->dev
;
572 rr3_ftr(dev
, "Entering %s\n", __func__
);
574 ret
= redrat3_send_cmd(RR3_RC_DET_ENABLE
, rr3
);
576 dev_dbg(dev
, "%s: unexpected ret of %d\n",
579 ret
= redrat3_send_cmd(RR3_RC_DET_STATUS
, rr3
);
581 dev_err(dev
, "%s: detector status: %d, should be 1\n",
586 rr3
->det_enabled
= true;
587 redrat3_issue_async(rr3
);
592 /* Disables the rr3 long range detector */
593 static void redrat3_disable_detector(struct redrat3_dev
*rr3
)
595 struct device
*dev
= rr3
->dev
;
598 rr3_ftr(dev
, "Entering %s\n", __func__
);
600 ret
= redrat3_send_cmd(RR3_RC_DET_DISABLE
, rr3
);
602 dev_err(dev
, "%s: failure!\n", __func__
);
604 ret
= redrat3_send_cmd(RR3_RC_DET_STATUS
, rr3
);
606 dev_warn(dev
, "%s: detector status: %d, should be 0\n",
609 rr3
->det_enabled
= false;
612 static inline void redrat3_delete(struct redrat3_dev
*rr3
,
613 struct usb_device
*udev
)
615 rr3_ftr(rr3
->dev
, "%s cleaning up\n", __func__
);
616 usb_kill_urb(rr3
->read_urb
);
617 usb_kill_urb(rr3
->write_urb
);
619 usb_free_urb(rr3
->read_urb
);
620 usb_free_urb(rr3
->write_urb
);
622 usb_free_coherent(udev
, rr3
->ep_in
->wMaxPacketSize
,
623 rr3
->bulk_in_buf
, rr3
->dma_in
);
624 usb_free_coherent(udev
, rr3
->ep_out
->wMaxPacketSize
,
625 rr3
->bulk_out_buf
, rr3
->dma_out
);
630 static u32
redrat3_get_timeout(struct redrat3_dev
*rr3
)
633 u32 timeout
= MS_TO_US(150); /* a sane default, if things go haywire */
637 tmp
= kzalloc(len
, GFP_KERNEL
);
639 dev_warn(rr3
->dev
, "Memory allocation faillure\n");
643 pipe
= usb_rcvctrlpipe(rr3
->udev
, 0);
644 ret
= usb_control_msg(rr3
->udev
, pipe
, RR3_GET_IR_PARAM
,
645 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
646 RR3_IR_IO_SIG_TIMEOUT
, 0, tmp
, len
, HZ
* 5);
648 dev_warn(rr3
->dev
, "Failed to read timeout from hardware\n");
652 timeout
= redrat3_len_to_us(be32_to_cpu(*tmp
));
654 rr3_dbg(rr3
->dev
, "Got timeout of %d ms\n", timeout
/ 1000);
658 static void redrat3_reset(struct redrat3_dev
*rr3
)
660 struct usb_device
*udev
= rr3
->udev
;
661 struct device
*dev
= rr3
->dev
;
662 int rc
, rxpipe
, txpipe
;
664 int len
= sizeof(u8
);
666 rr3_ftr(dev
, "Entering %s\n", __func__
);
668 rxpipe
= usb_rcvctrlpipe(udev
, 0);
669 txpipe
= usb_sndctrlpipe(udev
, 0);
671 val
= kzalloc(len
, GFP_KERNEL
);
673 dev_err(dev
, "Memory allocation failure\n");
678 rc
= usb_control_msg(udev
, rxpipe
, RR3_RESET
,
679 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
680 RR3_CPUCS_REG_ADDR
, 0, val
, len
, HZ
* 25);
681 rr3_dbg(dev
, "reset returned 0x%02x\n", rc
);
684 rc
= usb_control_msg(udev
, txpipe
, RR3_SET_IR_PARAM
,
685 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
686 RR3_IR_IO_LENGTH_FUZZ
, 0, val
, len
, HZ
* 25);
687 rr3_dbg(dev
, "set ir parm len fuzz %d rc 0x%02x\n", *val
, rc
);
689 *val
= RR3_DRIVER_MAXLENS
;
690 rc
= usb_control_msg(udev
, txpipe
, RR3_SET_IR_PARAM
,
691 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
,
692 RR3_IR_IO_MAX_LENGTHS
, 0, val
, len
, HZ
* 25);
693 rr3_dbg(dev
, "set ir parm max lens %d rc 0x%02x\n", *val
, rc
);
698 static void redrat3_get_firmware_rev(struct redrat3_dev
*rr3
)
703 rr3_ftr(rr3
->dev
, "Entering %s\n", __func__
);
705 buffer
= kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN
+ 1), GFP_KERNEL
);
707 dev_err(rr3
->dev
, "Memory allocation failure\n");
711 rc
= usb_control_msg(rr3
->udev
, usb_rcvctrlpipe(rr3
->udev
, 0),
713 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
714 0, 0, buffer
, RR3_FW_VERSION_LEN
, HZ
* 5);
717 dev_info(rr3
->dev
, "Firmware rev: %s", buffer
);
719 dev_err(rr3
->dev
, "Problem fetching firmware ID\n");
722 rr3_ftr(rr3
->dev
, "Exiting %s\n", __func__
);
725 static void redrat3_read_packet_start(struct redrat3_dev
*rr3
, int len
)
730 rr3_ftr(rr3
->dev
, "Entering %s\n", __func__
);
732 /* grab the Length and type of transfer */
733 memcpy(&(rr3
->pktlen
), (unsigned char *) rr3
->bulk_in_buf
,
734 sizeof(rr3
->pktlen
));
735 memcpy(&(rr3
->pkttype
), ((unsigned char *) rr3
->bulk_in_buf
+
736 sizeof(rr3
->pktlen
)),
737 sizeof(rr3
->pkttype
));
739 /*data needs conversion to know what its real values are*/
740 rr3
->pktlen
= be16_to_cpu(rr3
->pktlen
);
741 rr3
->pkttype
= be16_to_cpu(rr3
->pkttype
);
743 switch (rr3
->pkttype
) {
745 memcpy(&tx_error
, ((unsigned char *)rr3
->bulk_in_buf
746 + (sizeof(rr3
->pktlen
) + sizeof(rr3
->pkttype
))),
748 tx_error
= be16_to_cpu(tx_error
);
749 redrat3_dump_fw_error(rr3
, tx_error
);
752 case RR3_MOD_SIGNAL_IN
:
753 hdrlen
= sizeof(rr3
->pktlen
) + sizeof(rr3
->pkttype
);
754 rr3
->bytes_read
= len
;
755 rr3
->bytes_read
-= hdrlen
;
756 rr3
->datap
= &(rr3
->pbuf
[0]);
758 memcpy(rr3
->datap
, ((unsigned char *)rr3
->bulk_in_buf
+ hdrlen
),
760 rr3
->datap
+= rr3
->bytes_read
;
761 rr3_dbg(rr3
->dev
, "bytes_read %d, pktlen %d\n",
762 rr3
->bytes_read
, rr3
->pktlen
);
766 rr3_dbg(rr3
->dev
, "ignoring packet with type 0x%02x, "
767 "len of %d, 0x%02x\n", rr3
->pkttype
, len
, rr3
->pktlen
);
772 static void redrat3_read_packet_continue(struct redrat3_dev
*rr3
, int len
)
775 rr3_ftr(rr3
->dev
, "Entering %s\n", __func__
);
777 memcpy(rr3
->datap
, (unsigned char *)rr3
->bulk_in_buf
, len
);
780 rr3
->bytes_read
+= len
;
781 rr3_dbg(rr3
->dev
, "bytes_read %d, pktlen %d\n",
782 rr3
->bytes_read
, rr3
->pktlen
);
785 /* gather IR data from incoming urb, process it when we have enough */
786 static int redrat3_get_ir_data(struct redrat3_dev
*rr3
, int len
)
788 struct device
*dev
= rr3
->dev
;
791 rr3_ftr(dev
, "Entering %s\n", __func__
);
793 if (rr3
->pktlen
> RR3_MAX_BUF_SIZE
) {
794 dev_err(rr3
->dev
, "error: packet larger than buffer\n");
799 if ((rr3
->bytes_read
== 0) &&
800 (len
>= (sizeof(rr3
->pkttype
) + sizeof(rr3
->pktlen
)))) {
801 redrat3_read_packet_start(rr3
, len
);
802 } else if (rr3
->bytes_read
!= 0) {
803 redrat3_read_packet_continue(rr3
, len
);
804 } else if (rr3
->bytes_read
== 0) {
805 dev_err(dev
, "error: no packet data read\n");
810 if (rr3
->bytes_read
> rr3
->pktlen
) {
811 dev_err(dev
, "bytes_read (%d) greater than pktlen (%d)\n",
812 rr3
->bytes_read
, rr3
->pktlen
);
815 } else if (rr3
->bytes_read
< rr3
->pktlen
)
816 /* we're still accumulating data */
819 /* if we get here, we've got IR data to decode */
820 if (rr3
->pkttype
== RR3_MOD_SIGNAL_IN
)
821 redrat3_process_ir_data(rr3
);
823 rr3_dbg(dev
, "discarding non-signal data packet "
824 "(type 0x%02x)\n", rr3
->pkttype
);
833 /* callback function from USB when async USB request has completed */
834 static void redrat3_handle_async(struct urb
*urb
, struct pt_regs
*regs
)
836 struct redrat3_dev
*rr3
;
843 pr_err("%s called with invalid context!\n", __func__
);
848 rr3_ftr(rr3
->dev
, "Entering %s\n", __func__
);
850 if (!rr3
->det_enabled
) {
851 rr3_dbg(rr3
->dev
, "received a read callback but detector "
852 "disabled - ignoring\n");
856 switch (urb
->status
) {
858 redrat3_get_ir_data(rr3
, urb
->actual_length
);
869 dev_warn(rr3
->dev
, "Error: urb status = %d\n", urb
->status
);
876 if (!rr3
->transmitting
)
877 redrat3_issue_async(rr3
);
879 rr3_dbg(rr3
->dev
, "IR transmit in progress\n");
882 static void redrat3_write_bulk_callback(struct urb
*urb
, struct pt_regs
*regs
)
884 struct redrat3_dev
*rr3
;
892 len
= urb
->actual_length
;
893 rr3_ftr(rr3
->dev
, "%s: called (status=%d len=%d)\n",
894 __func__
, urb
->status
, len
);
898 static u16
mod_freq_to_val(unsigned int mod_freq
)
902 /* Clk used in mod. freq. generation is CLK24/4. */
903 return (u16
)(65536 - (mult
/ mod_freq
));
906 static int redrat3_set_tx_carrier(struct rc_dev
*dev
, u32 carrier
)
908 struct redrat3_dev
*rr3
= dev
->priv
;
910 rr3
->carrier
= carrier
;
915 static int redrat3_transmit_ir(struct rc_dev
*rcdev
, int *txbuf
, u32 n
)
917 struct redrat3_dev
*rr3
= rcdev
->priv
;
918 struct device
*dev
= rr3
->dev
;
919 struct redrat3_signal_header header
;
920 int i
, j
, count
, ret
, ret_len
, offset
;
921 int lencheck
, cur_sample_len
, pipe
;
922 char *buffer
= NULL
, *sigdata
= NULL
;
923 int *sample_lens
= NULL
;
931 rr3_ftr(dev
, "Entering %s\n", __func__
);
933 if (rr3
->transmitting
) {
934 dev_warn(dev
, "%s: transmitter already in use\n", __func__
);
938 count
= n
/ sizeof(int);
939 if (count
> (RR3_DRIVER_MAXLENS
* 2))
942 rr3
->transmitting
= true;
944 redrat3_disable_detector(rr3
);
946 if (rr3
->det_enabled
) {
947 dev_err(dev
, "%s: cannot tx while rx is enabled\n", __func__
);
952 sample_lens
= kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS
, GFP_KERNEL
);
958 for (i
= 0; i
< count
; i
++) {
959 for (lencheck
= 0; lencheck
< curlencheck
; lencheck
++) {
960 cur_sample_len
= redrat3_us_to_len(txbuf
[i
]);
961 if (sample_lens
[lencheck
] == cur_sample_len
)
964 if (lencheck
== curlencheck
) {
965 cur_sample_len
= redrat3_us_to_len(txbuf
[i
]);
966 rr3_dbg(dev
, "txbuf[%d]=%u, pos %d, enc %u\n",
967 i
, txbuf
[i
], curlencheck
, cur_sample_len
);
968 if (curlencheck
< 255) {
969 /* now convert the value to a proper
971 sample_lens
[curlencheck
] = cur_sample_len
;
974 dev_err(dev
, "signal too long\n");
981 sigdata
= kzalloc((count
+ RR3_TX_TRAILER_LEN
), GFP_KERNEL
);
987 sigdata
[count
] = RR3_END_OF_SIGNAL
;
988 sigdata
[count
+ 1] = RR3_END_OF_SIGNAL
;
989 for (i
= 0; i
< count
; i
++) {
990 for (j
= 0; j
< curlencheck
; j
++) {
991 if (sample_lens
[j
] == redrat3_us_to_len(txbuf
[i
]))
996 offset
= RR3_TX_HEADER_OFFSET
;
997 sendbuf_len
= RR3_HEADER_LENGTH
+ (sizeof(u16
) * RR3_DRIVER_MAXLENS
)
998 + count
+ RR3_TX_TRAILER_LEN
+ offset
;
1000 buffer
= kzalloc(sendbuf_len
, GFP_KERNEL
);
1006 /* fill in our packet header */
1007 header
.length
= sendbuf_len
- offset
;
1008 header
.transfer_type
= RR3_MOD_SIGNAL_OUT
;
1009 header
.pause
= redrat3_len_to_us(100);
1010 header
.mod_freq_count
= mod_freq_to_val(rr3
->carrier
);
1011 header
.no_periods
= 0; /* n/a to transmit */
1012 header
.max_lengths
= RR3_DRIVER_MAXLENS
;
1013 header
.no_lengths
= curlencheck
;
1014 header
.max_sig_size
= RR3_MAX_SIG_SIZE
;
1015 header
.sig_size
= count
+ RR3_TX_TRAILER_LEN
;
1016 /* we currently rely on repeat handling in the IR encoding source */
1017 header
.no_repeats
= 0;
1019 tmps
= cpu_to_be16(header
.length
);
1020 memcpy(buffer
, &tmps
, 2);
1022 tmps
= cpu_to_be16(header
.transfer_type
);
1023 memcpy(buffer
+ 2, &tmps
, 2);
1025 tmpi
= cpu_to_be32(header
.pause
);
1026 memcpy(buffer
+ offset
, &tmpi
, sizeof(tmpi
));
1028 tmps
= cpu_to_be16(header
.mod_freq_count
);
1029 memcpy(buffer
+ offset
+ RR3_FREQ_COUNT_OFFSET
, &tmps
, 2);
1031 buffer
[offset
+ RR3_NUM_LENGTHS_OFFSET
] = header
.no_lengths
;
1033 tmps
= cpu_to_be16(header
.sig_size
);
1034 memcpy(buffer
+ offset
+ RR3_NUM_SIGS_OFFSET
, &tmps
, 2);
1036 buffer
[offset
+ RR3_REPEATS_OFFSET
] = header
.no_repeats
;
1038 lengths_ptr
= (u16
*)(buffer
+ offset
+ RR3_HEADER_LENGTH
);
1039 for (i
= 0; i
< curlencheck
; ++i
)
1040 lengths_ptr
[i
] = cpu_to_be16(sample_lens
[i
]);
1042 datap
= (u8
*)(buffer
+ offset
+ RR3_HEADER_LENGTH
+
1043 (sizeof(u16
) * RR3_DRIVER_MAXLENS
));
1044 memcpy(datap
, sigdata
, (count
+ RR3_TX_TRAILER_LEN
));
1047 redrat3_dump_signal_header(&header
);
1048 redrat3_dump_signal_data(buffer
, header
.sig_size
);
1051 pipe
= usb_sndbulkpipe(rr3
->udev
, rr3
->ep_out
->bEndpointAddress
);
1052 tmps
= usb_bulk_msg(rr3
->udev
, pipe
, buffer
,
1053 sendbuf_len
, &ret_len
, 10 * HZ
);
1054 rr3_dbg(dev
, "sent %d bytes, (ret %d)\n", ret_len
, tmps
);
1056 /* now tell the hardware to transmit what we sent it */
1057 pipe
= usb_rcvctrlpipe(rr3
->udev
, 0);
1058 ret
= usb_control_msg(rr3
->udev
, pipe
, RR3_TX_SEND_SIGNAL
,
1059 USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
,
1060 0, 0, buffer
, 2, HZ
* 10);
1063 dev_err(dev
, "Error: control msg send failed, rc %d\n", ret
);
1072 rr3
->transmitting
= false;
1074 redrat3_enable_detector(rr3
);
1079 static struct rc_dev
*redrat3_init_rc_dev(struct redrat3_dev
*rr3
)
1081 struct device
*dev
= rr3
->dev
;
1084 u16 prod
= le16_to_cpu(rr3
->udev
->descriptor
.idProduct
);
1086 rc
= rc_allocate_device();
1088 dev_err(dev
, "remote input dev allocation failed\n");
1092 snprintf(rr3
->name
, sizeof(rr3
->name
), "RedRat3%s "
1093 "Infrared Remote Transceiver (%04x:%04x)",
1094 prod
== USB_RR3IIUSB_PRODUCT_ID
? "-II" : "",
1095 le16_to_cpu(rr3
->udev
->descriptor
.idVendor
), prod
);
1097 usb_make_path(rr3
->udev
, rr3
->phys
, sizeof(rr3
->phys
));
1099 rc
->input_name
= rr3
->name
;
1100 rc
->input_phys
= rr3
->phys
;
1101 usb_to_input_id(rr3
->udev
, &rc
->input_id
);
1102 rc
->dev
.parent
= dev
;
1104 rc
->driver_type
= RC_DRIVER_IR_RAW
;
1105 rc
->allowed_protos
= RC_TYPE_ALL
;
1106 rc
->timeout
= US_TO_NS(2750);
1107 rc
->tx_ir
= redrat3_transmit_ir
;
1108 rc
->s_tx_carrier
= redrat3_set_tx_carrier
;
1109 rc
->driver_name
= DRIVER_NAME
;
1110 rc
->map_name
= RC_MAP_HAUPPAUGE
;
1112 ret
= rc_register_device(rc
);
1114 dev_err(dev
, "remote dev registration failed\n");
1125 static int __devinit
redrat3_dev_probe(struct usb_interface
*intf
,
1126 const struct usb_device_id
*id
)
1128 struct usb_device
*udev
= interface_to_usbdev(intf
);
1129 struct device
*dev
= &intf
->dev
;
1130 struct usb_host_interface
*uhi
;
1131 struct redrat3_dev
*rr3
;
1132 struct usb_endpoint_descriptor
*ep
;
1133 struct usb_endpoint_descriptor
*ep_in
= NULL
;
1134 struct usb_endpoint_descriptor
*ep_out
= NULL
;
1137 int retval
= -ENOMEM
;
1139 rr3_ftr(dev
, "%s called\n", __func__
);
1141 uhi
= intf
->cur_altsetting
;
1143 /* find our bulk-in and bulk-out endpoints */
1144 for (i
= 0; i
< uhi
->desc
.bNumEndpoints
; ++i
) {
1145 ep
= &uhi
->endpoint
[i
].desc
;
1146 addr
= ep
->bEndpointAddress
;
1147 attrs
= ep
->bmAttributes
;
1149 if ((ep_in
== NULL
) &&
1150 ((addr
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_IN
) &&
1151 ((attrs
& USB_ENDPOINT_XFERTYPE_MASK
) ==
1152 USB_ENDPOINT_XFER_BULK
)) {
1153 rr3_dbg(dev
, "found bulk-in endpoint at 0x%02x\n",
1154 ep
->bEndpointAddress
);
1155 /* data comes in on 0x82, 0x81 is for other data... */
1156 if (ep
->bEndpointAddress
== RR3_BULK_IN_EP_ADDR
)
1160 if ((ep_out
== NULL
) &&
1161 ((addr
& USB_ENDPOINT_DIR_MASK
) == USB_DIR_OUT
) &&
1162 ((attrs
& USB_ENDPOINT_XFERTYPE_MASK
) ==
1163 USB_ENDPOINT_XFER_BULK
)) {
1164 rr3_dbg(dev
, "found bulk-out endpoint at 0x%02x\n",
1165 ep
->bEndpointAddress
);
1170 if (!ep_in
|| !ep_out
) {
1171 dev_err(dev
, "Couldn't find both in and out endpoints\n");
1176 /* allocate memory for our device state and initialize it */
1177 rr3
= kzalloc(sizeof(*rr3
), GFP_KERNEL
);
1179 dev_err(dev
, "Memory allocation failure\n");
1183 rr3
->dev
= &intf
->dev
;
1185 /* set up bulk-in endpoint */
1186 rr3
->read_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1187 if (!rr3
->read_urb
) {
1188 dev_err(dev
, "Read urb allocation failure\n");
1193 rr3
->bulk_in_buf
= usb_alloc_coherent(udev
, ep_in
->wMaxPacketSize
,
1194 GFP_ATOMIC
, &rr3
->dma_in
);
1195 if (!rr3
->bulk_in_buf
) {
1196 dev_err(dev
, "Read buffer allocation failure\n");
1200 pipe
= usb_rcvbulkpipe(udev
, ep_in
->bEndpointAddress
);
1201 usb_fill_bulk_urb(rr3
->read_urb
, udev
, pipe
,
1202 rr3
->bulk_in_buf
, ep_in
->wMaxPacketSize
,
1203 (usb_complete_t
)redrat3_handle_async
, rr3
);
1205 /* set up bulk-out endpoint*/
1206 rr3
->write_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1207 if (!rr3
->write_urb
) {
1208 dev_err(dev
, "Write urb allocation failure\n");
1212 rr3
->ep_out
= ep_out
;
1213 rr3
->bulk_out_buf
= usb_alloc_coherent(udev
, ep_out
->wMaxPacketSize
,
1214 GFP_ATOMIC
, &rr3
->dma_out
);
1215 if (!rr3
->bulk_out_buf
) {
1216 dev_err(dev
, "Write buffer allocation failure\n");
1220 pipe
= usb_sndbulkpipe(udev
, ep_out
->bEndpointAddress
);
1221 usb_fill_bulk_urb(rr3
->write_urb
, udev
, pipe
,
1222 rr3
->bulk_out_buf
, ep_out
->wMaxPacketSize
,
1223 (usb_complete_t
)redrat3_write_bulk_callback
, rr3
);
1225 mutex_init(&rr3
->lock
);
1229 redrat3_get_firmware_rev(rr3
);
1231 /* might be all we need to do? */
1232 retval
= redrat3_enable_detector(rr3
);
1236 /* store current hardware timeout, in us, will use for kfifo resets */
1237 rr3
->hw_timeout
= redrat3_get_timeout(rr3
);
1239 /* default.. will get overridden by any sends with a freq defined */
1240 rr3
->carrier
= 38000;
1242 rr3
->rc
= redrat3_init_rc_dev(rr3
);
1246 setup_timer(&rr3
->rx_timeout
, redrat3_rx_timeout
, (unsigned long)rr3
);
1248 /* we can register the device now, as it is ready */
1249 usb_set_intfdata(intf
, rr3
);
1251 rr3_ftr(dev
, "Exiting %s\n", __func__
);
1255 redrat3_delete(rr3
, rr3
->udev
);
1258 dev_err(dev
, "%s: retval = %x", __func__
, retval
);
1263 static void __devexit
redrat3_dev_disconnect(struct usb_interface
*intf
)
1265 struct usb_device
*udev
= interface_to_usbdev(intf
);
1266 struct redrat3_dev
*rr3
= usb_get_intfdata(intf
);
1268 rr3_ftr(&intf
->dev
, "Entering %s\n", __func__
);
1273 redrat3_disable_detector(rr3
);
1275 usb_set_intfdata(intf
, NULL
);
1276 rc_unregister_device(rr3
->rc
);
1277 del_timer_sync(&rr3
->rx_timeout
);
1278 redrat3_delete(rr3
, udev
);
1280 rr3_ftr(&intf
->dev
, "RedRat3 IR Transceiver now disconnected\n");
1283 static int redrat3_dev_suspend(struct usb_interface
*intf
, pm_message_t message
)
1285 struct redrat3_dev
*rr3
= usb_get_intfdata(intf
);
1286 rr3_ftr(rr3
->dev
, "suspend\n");
1287 usb_kill_urb(rr3
->read_urb
);
1291 static int redrat3_dev_resume(struct usb_interface
*intf
)
1293 struct redrat3_dev
*rr3
= usb_get_intfdata(intf
);
1294 rr3_ftr(rr3
->dev
, "resume\n");
1295 if (usb_submit_urb(rr3
->read_urb
, GFP_ATOMIC
))
1300 static struct usb_driver redrat3_dev_driver
= {
1301 .name
= DRIVER_NAME
,
1302 .probe
= redrat3_dev_probe
,
1303 .disconnect
= redrat3_dev_disconnect
,
1304 .suspend
= redrat3_dev_suspend
,
1305 .resume
= redrat3_dev_resume
,
1306 .reset_resume
= redrat3_dev_resume
,
1307 .id_table
= redrat3_dev_table
1310 static int __init
redrat3_dev_init(void)
1314 ret
= usb_register(&redrat3_dev_driver
);
1317 ": usb register failed, result = %d\n", ret
);
1322 static void __exit
redrat3_dev_exit(void)
1324 usb_deregister(&redrat3_dev_driver
);
1327 module_init(redrat3_dev_init
);
1328 module_exit(redrat3_dev_exit
);
1330 MODULE_DESCRIPTION(DRIVER_DESC
);
1331 MODULE_AUTHOR(DRIVER_AUTHOR
);
1332 MODULE_AUTHOR(DRIVER_AUTHOR2
);
1333 MODULE_LICENSE("GPL");
1334 MODULE_DEVICE_TABLE(usb
, redrat3_dev_table
);
1336 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
1337 MODULE_PARM_DESC(debug
, "Enable module debug spew. 0 = no debugging (default) "
1338 "0x1 = standard debug messages, 0x2 = function tracing debug. "
1339 "Flag bits are addative (i.e., 0x3 for both debug types).");