48 #include <linux/device.h>
49 #include <linux/module.h>
50 #include <linux/slab.h>
56 #define DRIVER_VERSION "0.70"
58 #define DRIVER_AUTHOR2 "The Dweller, Stephen Cox"
59 #define DRIVER_DESC "RedRat3 USB IR Transceiver Driver"
60 #define DRIVER_NAME "redrat3"
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__); \
85 #define RR3_ERROR 0x01
86 #define RR3_MOD_SIGNAL_IN 0x20
87 #define RR3_MOD_SIGNAL_OUT 0x21
90 #define RR3_FW_VERSION 0xb1
91 #define RR3_FW_VERSION_LEN 64
93 #define RR3_TX_SEND_SIGNAL 0xb3
94 #define RR3_SET_IR_PARAM 0xb7
95 #define RR3_GET_IR_PARAM 0xb8
97 #define RR3_BLINK_LED 0xb9
99 #define RR3_READ_SER_NO 0xba
100 #define RR3_SER_NO_LEN 4
102 #define RR3_RC_DET_ENABLE 0xbb
104 #define RR3_RC_DET_DISABLE 0xbc
106 #define RR3_RC_DET_STATUS 0xbd
108 #define RR3_RESET 0xa0
111 #define RR3_IR_IO_MAX_LENGTHS 0x01
113 #define RR3_IR_IO_PERIODS_MF 0x02
115 #define RR3_IR_IO_SIG_MEM_SIZE 0x03
117 #define RR3_IR_IO_LENGTH_FUZZ 0x04
119 #define RR3_IR_IO_SIG_TIMEOUT 0x05
121 #define RR3_IR_IO_MIN_PAUSE 0x06
124 #define RR3_CLK 24000000
126 #define RR3_CLK_PER_COUNT 12
128 #define RR3_CLK_CONV_FACTOR 2000000
130 #define RR3_BULK_IN_EP_ADDR 0x82
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
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
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
244 pr_info(
" * length: %u, transfer_type: 0x%02x\n",
246 pr_info(
" * pause: %u, freq_count: %u, no_periods: %u\n",
248 pr_info(
" * lengths: %u (max: %u)\n",
250 pr_info(
" * sig_size: %u (max: %u)\n",
255 static void redrat3_dump_signal_data(
char *
buffer,
u16 len)
268 for (
i = 0;
i <
len;
i++) {
271 pr_cont(
"%02x ", *data_vals++);
283 static void redrat3_issue_async(
struct redrat3_dev *rr3)
292 rr3_dbg(rr3->
dev,
"%s: receive request FAILED! "
293 "(res %d, len %d)\n", __func__, res,
294 rr3->
read_urb->transfer_buffer_length);
300 dev_info(rr3->
dev,
"fw error code 0x%02x: ", code);
309 pr_cont(
"Initial signal pulse not long enough "
310 "to measure carrier frequency\n");
313 pr_cont(
"Not enough length values allocated for signal\n");
316 pr_cont(
"Not enough memory allocated for signal data\n");
319 pr_cont(
"Too many signal repeats\n");
322 pr_cont(
"Insufficient memory available for IR signal "
323 "data memory allocation\n");
326 pr_cont(
"Insufficient memory available "
327 "for IrDa signal data memory allocation\n");
332 pr_cont(
"Insufficient memory available for bulk "
333 "transfer structure\n");
342 pr_cont(
"Signal capture has been terminated\n");
345 pr_cont(
"Attempt to set/get and unknown signal I/O "
346 "algorithm parameter\n");
349 pr_cont(
"Signal capture already started\n");
372 u32 biglen = length * 1000;
377 return result ? result : 1;
391 static u32 redrat3_us_to_len(
u32 microsec)
398 result = (
u32)(microsec * divisor) / 1000;
401 return result ? result : 1;
406 static void redrat3_rx_timeout(
unsigned long data)
410 rr3_dbg(rr3->
dev,
"calling ir_raw_event_reset\n");
411 ir_raw_event_reset(rr3->
rc);
414 static void redrat3_process_ir_data(
struct redrat3_dev *rr3)
421 u32 mod_freq, single_len;
429 pr_err(
"%s called with no context!\n", __func__);
436 sig_data = rr3->
pbuf;
443 dev_warn(dev,
"read returned less than rr3 header len\n");
470 redrat3_dump_signal_header(&
header);
471 redrat3_dump_signal_data(sig_data,
header.sig_size);
474 mod_freq = redrat3_val_to_mod_freq(&
header);
475 rr3_dbg(dev,
"Got mod_freq of %u\n", mod_freq);
484 for (i = 0; i <
header.sig_size; i++) {
485 u16 val = len_vals[data_vals[
i]];
494 rawir.duration =
US_TO_NS(single_len);
497 trailer = rawir.duration;
501 rr3_dbg(dev,
"storing %s with duration %d (i: %d)\n",
502 rawir.pulse ?
"pulse" :
"space", rawir.duration, i);
513 rawir.duration = trailer;
514 rr3_dbg(dev,
"storing trailing space with duration %d\n",
519 rr3_dbg(dev,
"calling ir_raw_event_handle\n");
528 struct usb_device *
udev;
539 0x0000, 0x0000, data,
sizeof(
u8),
HZ * 10);
542 dev_err(rr3->
dev,
"%s: Error sending rr3 cmd res %d, data %d",
543 __func__, res, *data);
554 static int redrat3_enable_detector(
struct redrat3_dev *rr3)
559 rr3_ftr(dev,
"Entering %s\n", __func__);
563 dev_dbg(dev,
"%s: unexpected ret of %d\n",
568 dev_err(dev,
"%s: detector status: %d, should be 1\n",
574 redrat3_issue_async(rr3);
580 static void redrat3_disable_detector(
struct redrat3_dev *rr3)
585 rr3_ftr(dev,
"Entering %s\n", __func__);
589 dev_err(dev,
"%s: failure!\n", __func__);
593 dev_warn(dev,
"%s: detector status: %d, should be 0\n",
599 static inline void redrat3_delete(
struct redrat3_dev *rr3,
600 struct usb_device *udev)
602 rr3_ftr(rr3->
dev,
"%s cleaning up\n", __func__);
626 dev_warn(rr3->
dev,
"Memory allocation faillure\n");
630 pipe = usb_rcvctrlpipe(rr3->
udev, 0);
635 dev_warn(rr3->
dev,
"Failed to read timeout from hardware\n");
641 rr3_dbg(rr3->
dev,
"Got timeout of %d ms\n", timeout / 1000);
647 struct usb_device *udev = rr3->
udev;
649 int rc, rxpipe, txpipe;
651 int len =
sizeof(
u8);
653 rr3_ftr(dev,
"Entering %s\n", __func__);
655 rxpipe = usb_rcvctrlpipe(udev, 0);
656 txpipe = usb_sndctrlpipe(udev, 0);
660 dev_err(dev,
"Memory allocation failure\n");
668 rr3_dbg(dev,
"reset returned 0x%02x\n", rc);
674 rr3_dbg(dev,
"set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
680 rr3_dbg(dev,
"set ir parm max lens %d rc 0x%02x\n", *val, rc);
685 static void redrat3_get_firmware_rev(
struct redrat3_dev *rr3)
694 dev_err(rr3->
dev,
"Memory allocation failure\n");
706 dev_err(rr3->
dev,
"Problem fetching firmware ID\n");
712 static void redrat3_read_packet_start(
struct redrat3_dev *rr3,
int len)
736 redrat3_dump_fw_error(rr3, tx_error);
748 rr3_dbg(rr3->
dev,
"bytes_read %d, pktlen %d\n",
753 rr3_dbg(rr3->
dev,
"ignoring packet with type 0x%02x, "
759 static void redrat3_read_packet_continue(
struct redrat3_dev *rr3,
int len)
768 rr3_dbg(rr3->
dev,
"bytes_read %d, pktlen %d\n",
773 static int redrat3_get_ir_data(
struct redrat3_dev *rr3,
int len)
778 rr3_ftr(dev,
"Entering %s\n", __func__);
781 dev_err(rr3->
dev,
"error: packet larger than buffer\n");
788 redrat3_read_packet_start(rr3, len);
790 redrat3_read_packet_continue(rr3, len);
792 dev_err(dev,
"error: no packet data read\n");
798 dev_err(dev,
"bytes_read (%d) greater than pktlen (%d)\n",
808 redrat3_process_ir_data(rr3);
810 rr3_dbg(dev,
"discarding non-signal data packet "
811 "(type 0x%02x)\n", rr3->
pkttype);
831 pr_err(
"%s called with invalid context!\n", __func__);
838 switch (urb->status) {
840 ret = redrat3_get_ir_data(rr3, urb->actual_length);
843 redrat3_issue_async(rr3);
855 dev_warn(rr3->
dev,
"Error: urb status = %d\n", urb->status);
863 static void redrat3_write_bulk_callback(
struct urb *urb,
struct pt_regs *regs)
873 len = urb->actual_length;
874 rr3_ftr(rr3->
dev,
"%s: called (status=%d len=%d)\n",
875 __func__, urb->status, len);
879 static u16 mod_freq_to_val(
unsigned int mod_freq)
884 return (
u16)(65536 - (mult / mod_freq));
892 rr3_dbg(dev,
"Setting modulation frequency to %u", carrier);
901 static int redrat3_transmit_ir(
struct rc_dev *rcdev,
unsigned *txbuf,
908 int lencheck, cur_sample_len,
pipe;
909 char *buffer =
NULL, *sigdata =
NULL;
910 int *sample_lens =
NULL;
918 rr3_ftr(dev,
"Entering %s\n", __func__);
921 dev_warn(dev,
"%s: transmitter already in use\n", __func__);
938 for (i = 0; i <
count; i++) {
939 for (lencheck = 0; lencheck < curlencheck; lencheck++) {
940 cur_sample_len = redrat3_us_to_len(txbuf[i]);
941 if (sample_lens[lencheck] == cur_sample_len)
944 if (lencheck == curlencheck) {
945 cur_sample_len = redrat3_us_to_len(txbuf[i]);
946 rr3_dbg(dev,
"txbuf[%d]=%u, pos %d, enc %u\n",
947 i, txbuf[i], curlencheck, cur_sample_len);
948 if (curlencheck < 255) {
951 sample_lens[curlencheck] = cur_sample_len;
954 dev_err(dev,
"signal too long\n");
969 for (i = 0; i <
count; i++) {
970 for (j = 0; j < curlencheck; j++) {
971 if (sample_lens[j] == redrat3_us_to_len(txbuf[i]))
989 header.pause = redrat3_len_to_us(100);
993 header.no_lengths = curlencheck;
1000 memcpy(buffer, &tmps, 2);
1003 memcpy(buffer + 2, &tmps, 2);
1006 memcpy(buffer + offset, &tmpi,
sizeof(tmpi));
1019 for (i = 0; i < curlencheck; ++
i)
1024 memcpy(datap, sigdata, (count + RR3_TX_TRAILER_LEN));
1027 redrat3_dump_signal_header(&
header);
1028 redrat3_dump_signal_data(buffer,
header.sig_size);
1031 pipe = usb_sndbulkpipe(rr3->
udev, rr3->
ep_out->bEndpointAddress);
1033 sendbuf_len, &ret_len, 10 *
HZ);
1034 rr3_dbg(dev,
"sent %d bytes, (ret %d)\n", ret_len, tmps);
1037 pipe = usb_rcvctrlpipe(rr3->
udev, 0);
1040 0, 0, buffer, 2,
HZ * 10);
1043 dev_err(dev,
"Error: control msg send failed, rc %d\n", ret);
1068 dev_err(dev,
"remote input dev allocation failed\n");
1073 "Infrared Remote Transceiver (%04x:%04x)",
1077 usb_make_path(rr3->
udev, rr3->
phys,
sizeof(rr3->
phys));
1087 rc->
tx_ir = redrat3_transmit_ir;
1094 dev_err(dev,
"remote dev registration failed\n");
1108 struct usb_device *udev = interface_to_usbdev(intf);
1109 struct device *dev = &intf->dev;
1110 struct usb_host_interface *uhi;
1119 rr3_ftr(dev,
"%s called\n", __func__);
1121 uhi = intf->cur_altsetting;
1124 for (i = 0; i < uhi->desc.bNumEndpoints; ++
i) {
1125 ep = &uhi->endpoint[
i].desc;
1129 if ((ep_in ==
NULL) &&
1133 rr3_dbg(dev,
"found bulk-in endpoint at 0x%02x\n",
1140 if ((ep_out ==
NULL) &&
1141 ((addr & USB_ENDPOINT_DIR_MASK) ==
USB_DIR_OUT) &&
1142 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
1144 rr3_dbg(dev,
"found bulk-out endpoint at 0x%02x\n",
1150 if (!ep_in || !ep_out) {
1151 dev_err(dev,
"Couldn't find both in and out endpoints\n");
1159 dev_err(dev,
"Memory allocation failure\n");
1163 rr3->
dev = &intf->dev;
1168 dev_err(dev,
"Read urb allocation failure\n");
1176 dev_err(dev,
"Read buffer allocation failure\n");
1181 usb_fill_bulk_urb(rr3->
read_urb, udev, pipe,
1183 (usb_complete_t)redrat3_handle_async, rr3);
1188 dev_err(dev,
"Write urb allocation failure\n");
1196 dev_err(dev,
"Write buffer allocation failure\n");
1201 usb_fill_bulk_urb(rr3->
write_urb, udev, pipe,
1203 (usb_complete_t)redrat3_write_bulk_callback, rr3);
1209 redrat3_get_firmware_rev(rr3);
1212 retval = redrat3_enable_detector(rr3);
1222 rr3->
rc = redrat3_init_rc_dev(rr3);
1230 usb_set_intfdata(intf, rr3);
1232 rr3_ftr(dev,
"Exiting %s\n", __func__);
1236 redrat3_delete(rr3, rr3->
udev);
1239 dev_err(dev,
"%s: retval = %x", __func__, retval);
1246 struct usb_device *udev = interface_to_usbdev(intf);
1249 rr3_ftr(&intf->dev,
"Entering %s\n", __func__);
1254 redrat3_disable_detector(rr3);
1256 usb_set_intfdata(intf,
NULL);
1259 redrat3_delete(rr3, udev);
1261 rr3_ftr(&intf->dev,
"RedRat3 IR Transceiver now disconnected\n");
1281 static struct usb_driver redrat3_dev_driver = {
1283 .probe = redrat3_dev_probe,
1284 .disconnect =
__devexit_p(redrat3_dev_disconnect),
1285 .suspend = redrat3_dev_suspend,
1286 .resume = redrat3_dev_resume,
1287 .reset_resume = redrat3_dev_resume,
1288 .id_table = redrat3_dev_table
1301 "0x1 = standard debug messages, 0x2 = function tracing debug. "
1302 "Flag bits are addative (i.e., 0x3 for both debug types).");