sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / rc / redrat3.c
blob2784f5dae398ee9483a36cdeec5217a9a1f6fcc7
1 /*
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
11 * Chris Dodge.
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.
17 * TODO LIST:
18 * - fix lirc not showing repeats properly
19 * --
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.
30 * --
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 /* Start capture with the wideband receiver */
85 #define RR3_MODSIG_CAPTURE 0xb2
86 /* Return the status of RC detector capture */
87 #define RR3_RC_DET_STATUS 0xbd
88 /* Reset redrat */
89 #define RR3_RESET 0xa0
91 /* Max number of lengths in the signal. */
92 #define RR3_IR_IO_MAX_LENGTHS 0x01
93 /* Periods to measure mod. freq. */
94 #define RR3_IR_IO_PERIODS_MF 0x02
95 /* Size of memory for main signal data */
96 #define RR3_IR_IO_SIG_MEM_SIZE 0x03
97 /* Delta value when measuring lengths */
98 #define RR3_IR_IO_LENGTH_FUZZ 0x04
99 /* Timeout for end of signal detection */
100 #define RR3_IR_IO_SIG_TIMEOUT 0x05
101 /* Minimum value for pause recognition. */
102 #define RR3_IR_IO_MIN_PAUSE 0x06
104 /* Clock freq. of EZ-USB chip */
105 #define RR3_CLK 24000000
106 /* Clock periods per timer count */
107 #define RR3_CLK_PER_COUNT 12
108 /* (RR3_CLK / RR3_CLK_PER_COUNT) */
109 #define RR3_CLK_CONV_FACTOR 2000000
110 /* USB bulk-in wideband IR data endpoint address */
111 #define RR3_WIDE_IN_EP_ADDR 0x81
112 /* USB bulk-in narrowband IR data endpoint address */
113 #define RR3_NARROW_IN_EP_ADDR 0x82
115 /* Size of the fixed-length portion of the signal */
116 #define RR3_DRIVER_MAXLENS 255
117 #define RR3_MAX_SIG_SIZE 512
118 #define RR3_TIME_UNIT 50
119 #define RR3_END_OF_SIGNAL 0x7f
120 #define RR3_TX_TRAILER_LEN 2
121 #define RR3_RX_MIN_TIMEOUT 5
122 #define RR3_RX_MAX_TIMEOUT 2000
124 /* The 8051's CPUCS Register address */
125 #define RR3_CPUCS_REG_ADDR 0x7f92
127 #define USB_RR3USB_VENDOR_ID 0x112a
128 #define USB_RR3USB_PRODUCT_ID 0x0001
129 #define USB_RR3IIUSB_PRODUCT_ID 0x0005
133 * The redrat3 encodes an IR signal as set of different lengths and a set
134 * of indices into those lengths. This sets how much two lengths must
135 * differ before they are considered distinct, the value is specified
136 * in microseconds.
137 * Default 5, value 0 to 127.
139 static int length_fuzz = 5;
140 module_param(length_fuzz, uint, 0644);
141 MODULE_PARM_DESC(length_fuzz, "Length Fuzz (0-127)");
144 * When receiving a continuous ir stream (for example when a user is
145 * holding a button down on a remote), this specifies the minimum size
146 * of a space when the redrat3 sends a irdata packet to the host. Specified
147 * in miliseconds. Default value 18ms.
148 * The value can be between 2 and 30 inclusive.
150 static int minimum_pause = 18;
151 module_param(minimum_pause, uint, 0644);
152 MODULE_PARM_DESC(minimum_pause, "Minimum Pause in ms (2-30)");
155 * The carrier frequency is measured during the first pulse of the IR
156 * signal. The larger the number of periods used To measure, the more
157 * accurate the result is likely to be, however some signals have short
158 * initial pulses, so in some case it may be necessary to reduce this value.
159 * Default 8, value 1 to 255.
161 static int periods_measure_carrier = 8;
162 module_param(periods_measure_carrier, uint, 0644);
163 MODULE_PARM_DESC(periods_measure_carrier, "Number of Periods to Measure Carrier (1-255)");
166 struct redrat3_header {
167 __be16 length;
168 __be16 transfer_type;
169 } __packed;
171 /* sending and receiving irdata */
172 struct redrat3_irdata {
173 struct redrat3_header header;
174 __be32 pause;
175 __be16 mod_freq_count;
176 __be16 num_periods;
177 __u8 max_lengths;
178 __u8 no_lengths;
179 __be16 max_sig_size;
180 __be16 sig_size;
181 __u8 no_repeats;
182 __be16 lens[RR3_DRIVER_MAXLENS]; /* not aligned */
183 __u8 sigdata[RR3_MAX_SIG_SIZE];
184 } __packed;
186 /* firmware errors */
187 struct redrat3_error {
188 struct redrat3_header header;
189 __be16 fw_error;
190 } __packed;
192 /* table of devices that work with this driver */
193 static struct usb_device_id redrat3_dev_table[] = {
194 /* Original version of the RedRat3 */
195 {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)},
196 /* Second Version/release of the RedRat3 - RetRat3-II */
197 {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)},
198 {} /* Terminating entry */
201 /* Structure to hold all of our device specific stuff */
202 struct redrat3_dev {
203 /* core device bits */
204 struct rc_dev *rc;
205 struct device *dev;
207 /* led control */
208 struct led_classdev led;
209 atomic_t flash;
210 struct usb_ctrlrequest flash_control;
211 struct urb *flash_urb;
212 u8 flash_in_buf;
214 /* learning */
215 bool wideband;
216 struct usb_ctrlrequest learn_control;
217 struct urb *learn_urb;
218 u8 learn_buf;
220 /* save off the usb device pointer */
221 struct usb_device *udev;
223 /* the receive endpoint */
224 struct usb_endpoint_descriptor *ep_narrow;
225 /* the buffer to receive data */
226 void *bulk_in_buf;
227 /* urb used to read ir data */
228 struct urb *narrow_urb;
229 struct urb *wide_urb;
231 /* the send endpoint */
232 struct usb_endpoint_descriptor *ep_out;
234 /* usb dma */
235 dma_addr_t dma_in;
237 /* Is the device currently transmitting?*/
238 bool transmitting;
240 /* store for current packet */
241 struct redrat3_irdata irdata;
242 u16 bytes_read;
244 u32 carrier;
246 char name[64];
247 char phys[64];
250 static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
252 if (!rr3->transmitting && (code != 0x40))
253 dev_info(rr3->dev, "fw error code 0x%02x: ", code);
255 switch (code) {
256 case 0x00:
257 pr_cont("No Error\n");
258 break;
260 /* Codes 0x20 through 0x2f are IR Firmware Errors */
261 case 0x20:
262 pr_cont("Initial signal pulse not long enough to measure carrier frequency\n");
263 break;
264 case 0x21:
265 pr_cont("Not enough length values allocated for signal\n");
266 break;
267 case 0x22:
268 pr_cont("Not enough memory allocated for signal data\n");
269 break;
270 case 0x23:
271 pr_cont("Too many signal repeats\n");
272 break;
273 case 0x28:
274 pr_cont("Insufficient memory available for IR signal data memory allocation\n");
275 break;
276 case 0x29:
277 pr_cont("Insufficient memory available for IrDa signal data memory allocation\n");
278 break;
280 /* Codes 0x30 through 0x3f are USB Firmware Errors */
281 case 0x30:
282 pr_cont("Insufficient memory available for bulk transfer structure\n");
283 break;
286 * Other error codes... These are primarily errors that can occur in
287 * the control messages sent to the redrat
289 case 0x40:
290 if (!rr3->transmitting)
291 pr_cont("Signal capture has been terminated\n");
292 break;
293 case 0x41:
294 pr_cont("Attempt to set/get and unknown signal I/O algorithm parameter\n");
295 break;
296 case 0x42:
297 pr_cont("Signal capture already started\n");
298 break;
300 default:
301 pr_cont("Unknown Error\n");
302 break;
306 static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata)
308 u32 mod_freq = 0;
309 u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count);
311 if (mod_freq_count != 0)
312 mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) /
313 (mod_freq_count * RR3_CLK_PER_COUNT);
315 return mod_freq;
318 /* this function scales down the figures for the same result... */
319 static u32 redrat3_len_to_us(u32 length)
321 u32 biglen = length * 1000;
322 u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000;
323 u32 result = (u32) (biglen / divisor);
325 /* don't allow zero lengths to go back, breaks lirc */
326 return result ? result : 1;
330 * convert us back into redrat3 lengths
332 * length * 1000 length * 1000000
333 * ------------- = ---------------- = micro
334 * rr3clk / 1000 rr3clk
336 * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
337 * ----- = 4 ----- = 6 -------------- = len ---------------------
338 * 3 2 1000000 1000
340 static u32 redrat3_us_to_len(u32 microsec)
342 u32 result;
343 u32 divisor;
345 microsec = (microsec > IR_MAX_DURATION) ? IR_MAX_DURATION : microsec;
346 divisor = (RR3_CLK_CONV_FACTOR / 1000);
347 result = (u32)(microsec * divisor) / 1000;
349 /* don't allow zero lengths to go back, breaks lirc */
350 return result ? result : 1;
353 static void redrat3_process_ir_data(struct redrat3_dev *rr3)
355 DEFINE_IR_RAW_EVENT(rawir);
356 struct device *dev;
357 unsigned int i, sig_size, single_len, offset, val;
358 u32 mod_freq;
360 dev = rr3->dev;
362 mod_freq = redrat3_val_to_mod_freq(&rr3->irdata);
363 dev_dbg(dev, "Got mod_freq of %u\n", mod_freq);
364 if (mod_freq && rr3->wideband) {
365 DEFINE_IR_RAW_EVENT(ev);
367 ev.carrier_report = 1;
368 ev.carrier = mod_freq;
370 ir_raw_event_store(rr3->rc, &ev);
373 /* process each rr3 encoded byte into an int */
374 sig_size = be16_to_cpu(rr3->irdata.sig_size);
375 for (i = 0; i < sig_size; i++) {
376 offset = rr3->irdata.sigdata[i];
377 val = get_unaligned_be16(&rr3->irdata.lens[offset]);
378 single_len = redrat3_len_to_us(val);
380 /* we should always get pulse/space/pulse/space samples */
381 if (i % 2)
382 rawir.pulse = false;
383 else
384 rawir.pulse = true;
386 rawir.duration = US_TO_NS(single_len);
387 /* cap the value to IR_MAX_DURATION */
388 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
389 IR_MAX_DURATION : rawir.duration;
391 dev_dbg(dev, "storing %s with duration %d (i: %d)\n",
392 rawir.pulse ? "pulse" : "space", rawir.duration, i);
393 ir_raw_event_store_with_filter(rr3->rc, &rawir);
396 /* add a trailing space */
397 rawir.pulse = false;
398 rawir.timeout = true;
399 rawir.duration = rr3->rc->timeout;
400 dev_dbg(dev, "storing trailing timeout with duration %d\n",
401 rawir.duration);
402 ir_raw_event_store_with_filter(rr3->rc, &rawir);
404 dev_dbg(dev, "calling ir_raw_event_handle\n");
405 ir_raw_event_handle(rr3->rc);
408 /* Util fn to send rr3 cmds */
409 static int redrat3_send_cmd(int cmd, struct redrat3_dev *rr3)
411 struct usb_device *udev;
412 u8 *data;
413 int res;
415 data = kzalloc(sizeof(u8), GFP_KERNEL);
416 if (!data)
417 return -ENOMEM;
419 udev = rr3->udev;
420 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd,
421 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
422 0x0000, 0x0000, data, sizeof(u8), HZ * 10);
424 if (res < 0) {
425 dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d",
426 __func__, res, *data);
427 res = -EIO;
428 } else
429 res = data[0];
431 kfree(data);
433 return res;
436 /* Enables the long range detector and starts async receive */
437 static int redrat3_enable_detector(struct redrat3_dev *rr3)
439 struct device *dev = rr3->dev;
440 u8 ret;
442 ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
443 if (ret != 0)
444 dev_dbg(dev, "%s: unexpected ret of %d\n",
445 __func__, ret);
447 ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
448 if (ret != 1) {
449 dev_err(dev, "%s: detector status: %d, should be 1\n",
450 __func__, ret);
451 return -EIO;
454 ret = usb_submit_urb(rr3->narrow_urb, GFP_KERNEL);
455 if (ret) {
456 dev_err(rr3->dev, "narrow band urb failed: %d", ret);
457 return ret;
460 ret = usb_submit_urb(rr3->wide_urb, GFP_KERNEL);
461 if (ret)
462 dev_err(rr3->dev, "wide band urb failed: %d", ret);
464 return ret;
467 static inline void redrat3_delete(struct redrat3_dev *rr3,
468 struct usb_device *udev)
470 usb_kill_urb(rr3->narrow_urb);
471 usb_kill_urb(rr3->wide_urb);
472 usb_kill_urb(rr3->flash_urb);
473 usb_kill_urb(rr3->learn_urb);
474 usb_free_urb(rr3->narrow_urb);
475 usb_free_urb(rr3->wide_urb);
476 usb_free_urb(rr3->flash_urb);
477 usb_free_urb(rr3->learn_urb);
478 usb_free_coherent(udev, le16_to_cpu(rr3->ep_narrow->wMaxPacketSize),
479 rr3->bulk_in_buf, rr3->dma_in);
481 kfree(rr3);
484 static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
486 __be32 *tmp;
487 u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
488 int len, ret, pipe;
490 len = sizeof(*tmp);
491 tmp = kzalloc(len, GFP_KERNEL);
492 if (!tmp)
493 return timeout;
495 pipe = usb_rcvctrlpipe(rr3->udev, 0);
496 ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
497 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
498 RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5);
499 if (ret != len)
500 dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
501 else {
502 timeout = redrat3_len_to_us(be32_to_cpup(tmp));
504 dev_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
507 kfree(tmp);
509 return timeout;
512 static int redrat3_set_timeout(struct rc_dev *rc_dev, unsigned int timeoutns)
514 struct redrat3_dev *rr3 = rc_dev->priv;
515 struct usb_device *udev = rr3->udev;
516 struct device *dev = rr3->dev;
517 __be32 *timeout;
518 int ret;
520 timeout = kmalloc(sizeof(*timeout), GFP_KERNEL);
521 if (!timeout)
522 return -ENOMEM;
524 *timeout = cpu_to_be32(redrat3_us_to_len(timeoutns / 1000));
525 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), RR3_SET_IR_PARAM,
526 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
527 RR3_IR_IO_SIG_TIMEOUT, 0, timeout, sizeof(*timeout),
528 HZ * 25);
529 dev_dbg(dev, "set ir parm timeout %d ret 0x%02x\n",
530 be32_to_cpu(*timeout), ret);
532 if (ret == sizeof(*timeout))
533 ret = 0;
534 else if (ret >= 0)
535 ret = -EIO;
537 kfree(timeout);
539 return ret;
542 static void redrat3_reset(struct redrat3_dev *rr3)
544 struct usb_device *udev = rr3->udev;
545 struct device *dev = rr3->dev;
546 int rc, rxpipe, txpipe;
547 u8 *val;
548 size_t const len = sizeof(*val);
550 rxpipe = usb_rcvctrlpipe(udev, 0);
551 txpipe = usb_sndctrlpipe(udev, 0);
553 val = kmalloc(len, GFP_KERNEL);
554 if (!val)
555 return;
557 *val = 0x01;
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);
563 *val = length_fuzz;
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,
580 rc);
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);
588 kfree(val);
591 static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
593 int rc;
594 char *buffer;
596 buffer = kcalloc(RR3_FW_VERSION_LEN + 1, sizeof(*buffer), GFP_KERNEL);
597 if (!buffer)
598 return;
600 rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
601 RR3_FW_VERSION,
602 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
603 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5);
605 if (rc >= 0)
606 dev_info(rr3->dev, "Firmware rev: %s", buffer);
607 else
608 dev_err(rr3->dev, "Problem fetching firmware ID\n");
610 kfree(buffer);
613 static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
615 struct redrat3_header *header = rr3->bulk_in_buf;
616 unsigned pktlen, pkttype;
618 /* grab the Length and type of transfer */
619 pktlen = be16_to_cpu(header->length);
620 pkttype = be16_to_cpu(header->transfer_type);
622 if (pktlen > sizeof(rr3->irdata)) {
623 dev_warn(rr3->dev, "packet length %u too large\n", pktlen);
624 return;
627 switch (pkttype) {
628 case RR3_ERROR:
629 if (len >= sizeof(struct redrat3_error)) {
630 struct redrat3_error *error = rr3->bulk_in_buf;
631 unsigned fw_error = be16_to_cpu(error->fw_error);
632 redrat3_dump_fw_error(rr3, fw_error);
634 break;
636 case RR3_MOD_SIGNAL_IN:
637 memcpy(&rr3->irdata, rr3->bulk_in_buf, len);
638 rr3->bytes_read = len;
639 dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
640 rr3->bytes_read, pktlen);
641 break;
643 default:
644 dev_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
645 pkttype, len, pktlen);
646 break;
650 static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len)
652 void *irdata = &rr3->irdata;
654 if (len + rr3->bytes_read > sizeof(rr3->irdata)) {
655 dev_warn(rr3->dev, "too much data for packet\n");
656 rr3->bytes_read = 0;
657 return;
660 memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len);
662 rr3->bytes_read += len;
663 dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read,
664 be16_to_cpu(rr3->irdata.header.length));
667 /* gather IR data from incoming urb, process it when we have enough */
668 static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len)
670 struct device *dev = rr3->dev;
671 unsigned pkttype;
672 int ret = 0;
674 if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) {
675 redrat3_read_packet_start(rr3, len);
676 } else if (rr3->bytes_read != 0) {
677 redrat3_read_packet_continue(rr3, len);
678 } else if (rr3->bytes_read == 0) {
679 dev_err(dev, "error: no packet data read\n");
680 ret = -ENODATA;
681 goto out;
684 if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length) +
685 sizeof(struct redrat3_header))
686 /* we're still accumulating data */
687 return 0;
689 /* if we get here, we've got IR data to decode */
690 pkttype = be16_to_cpu(rr3->irdata.header.transfer_type);
691 if (pkttype == RR3_MOD_SIGNAL_IN)
692 redrat3_process_ir_data(rr3);
693 else
694 dev_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n",
695 pkttype);
697 out:
698 rr3->bytes_read = 0;
699 return ret;
702 /* callback function from USB when async USB request has completed */
703 static void redrat3_handle_async(struct urb *urb)
705 struct redrat3_dev *rr3 = urb->context;
706 int ret;
708 switch (urb->status) {
709 case 0:
710 ret = redrat3_get_ir_data(rr3, urb->actual_length);
711 if (!ret && rr3->wideband && !rr3->learn_urb->hcpriv) {
712 ret = usb_submit_urb(rr3->learn_urb, GFP_ATOMIC);
713 if (ret)
714 dev_err(rr3->dev, "Failed to submit learning urb: %d",
715 ret);
718 if (!ret) {
719 /* no error, prepare to read more */
720 ret = usb_submit_urb(urb, GFP_ATOMIC);
721 if (ret)
722 dev_err(rr3->dev, "Failed to resubmit urb: %d",
723 ret);
725 break;
727 case -ECONNRESET:
728 case -ENOENT:
729 case -ESHUTDOWN:
730 usb_unlink_urb(urb);
731 return;
733 case -EPIPE:
734 default:
735 dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
736 rr3->bytes_read = 0;
737 break;
741 static u16 mod_freq_to_val(unsigned int mod_freq)
743 int mult = 6000000;
745 /* Clk used in mod. freq. generation is CLK24/4. */
746 return 65536 - (mult / mod_freq);
749 static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
751 struct redrat3_dev *rr3 = rcdev->priv;
752 struct device *dev = rr3->dev;
754 dev_dbg(dev, "Setting modulation frequency to %u", carrier);
755 if (carrier == 0)
756 return -EINVAL;
758 rr3->carrier = carrier;
760 return 0;
763 static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
764 unsigned count)
766 struct redrat3_dev *rr3 = rcdev->priv;
767 struct device *dev = rr3->dev;
768 struct redrat3_irdata *irdata = NULL;
769 int ret, ret_len;
770 int lencheck, cur_sample_len, pipe;
771 int *sample_lens = NULL;
772 u8 curlencheck = 0;
773 unsigned i, sendbuf_len;
775 if (rr3->transmitting) {
776 dev_warn(dev, "%s: transmitter already in use\n", __func__);
777 return -EAGAIN;
780 if (count > RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN)
781 return -EINVAL;
783 /* rr3 will disable rc detector on transmit */
784 rr3->transmitting = true;
786 sample_lens = kcalloc(RR3_DRIVER_MAXLENS,
787 sizeof(*sample_lens),
788 GFP_KERNEL);
789 if (!sample_lens)
790 return -ENOMEM;
792 irdata = kzalloc(sizeof(*irdata), GFP_KERNEL);
793 if (!irdata) {
794 ret = -ENOMEM;
795 goto out;
798 for (i = 0; i < count; i++) {
799 cur_sample_len = redrat3_us_to_len(txbuf[i]);
800 if (cur_sample_len > 0xffff) {
801 dev_warn(dev, "transmit period of %uus truncated to %uus\n",
802 txbuf[i], redrat3_len_to_us(0xffff));
803 cur_sample_len = 0xffff;
805 for (lencheck = 0; lencheck < curlencheck; lencheck++) {
806 if (sample_lens[lencheck] == cur_sample_len)
807 break;
809 if (lencheck == curlencheck) {
810 dev_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
811 i, txbuf[i], curlencheck, cur_sample_len);
812 if (curlencheck < RR3_DRIVER_MAXLENS) {
813 /* now convert the value to a proper
814 * rr3 value.. */
815 sample_lens[curlencheck] = cur_sample_len;
816 put_unaligned_be16(cur_sample_len,
817 &irdata->lens[curlencheck]);
818 curlencheck++;
819 } else {
820 ret = -EINVAL;
821 goto out;
824 irdata->sigdata[i] = lencheck;
827 irdata->sigdata[count] = RR3_END_OF_SIGNAL;
828 irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL;
830 sendbuf_len = offsetof(struct redrat3_irdata,
831 sigdata[count + RR3_TX_TRAILER_LEN]);
832 /* fill in our packet header */
833 irdata->header.length = cpu_to_be16(sendbuf_len -
834 sizeof(struct redrat3_header));
835 irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT);
836 irdata->pause = cpu_to_be32(redrat3_len_to_us(100));
837 irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier));
838 irdata->no_lengths = curlencheck;
839 irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN);
841 pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
842 ret = usb_bulk_msg(rr3->udev, pipe, irdata,
843 sendbuf_len, &ret_len, 10 * HZ);
844 dev_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret);
846 /* now tell the hardware to transmit what we sent it */
847 pipe = usb_rcvctrlpipe(rr3->udev, 0);
848 ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
849 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
850 0, 0, irdata, 2, HZ * 10);
852 if (ret < 0)
853 dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
854 else
855 ret = count;
857 out:
858 kfree(irdata);
859 kfree(sample_lens);
861 rr3->transmitting = false;
862 /* rr3 re-enables rc detector because it was enabled before */
864 return ret;
867 static void redrat3_brightness_set(struct led_classdev *led_dev, enum
868 led_brightness brightness)
870 struct redrat3_dev *rr3 = container_of(led_dev, struct redrat3_dev,
871 led);
873 if (brightness != LED_OFF && atomic_cmpxchg(&rr3->flash, 0, 1) == 0) {
874 int ret = usb_submit_urb(rr3->flash_urb, GFP_ATOMIC);
875 if (ret != 0) {
876 dev_dbg(rr3->dev, "%s: unexpected ret of %d\n",
877 __func__, ret);
878 atomic_set(&rr3->flash, 0);
883 static int redrat3_wideband_receiver(struct rc_dev *rcdev, int enable)
885 struct redrat3_dev *rr3 = rcdev->priv;
886 int ret = 0;
888 rr3->wideband = enable != 0;
890 if (enable) {
891 ret = usb_submit_urb(rr3->learn_urb, GFP_KERNEL);
892 if (ret)
893 dev_err(rr3->dev, "Failed to submit learning urb: %d",
894 ret);
897 return ret;
900 static void redrat3_learn_complete(struct urb *urb)
902 struct redrat3_dev *rr3 = urb->context;
904 switch (urb->status) {
905 case 0:
906 break;
907 case -ECONNRESET:
908 case -ENOENT:
909 case -ESHUTDOWN:
910 usb_unlink_urb(urb);
911 return;
912 case -EPIPE:
913 default:
914 dev_err(rr3->dev, "Error: learn urb status = %d", urb->status);
915 break;
919 static void redrat3_led_complete(struct urb *urb)
921 struct redrat3_dev *rr3 = urb->context;
923 switch (urb->status) {
924 case 0:
925 break;
926 case -ECONNRESET:
927 case -ENOENT:
928 case -ESHUTDOWN:
929 usb_unlink_urb(urb);
930 return;
931 case -EPIPE:
932 default:
933 dev_dbg(rr3->dev, "Error: urb status = %d\n", urb->status);
934 break;
937 rr3->led.brightness = LED_OFF;
938 atomic_dec(&rr3->flash);
941 static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
943 struct device *dev = rr3->dev;
944 struct rc_dev *rc;
945 int ret;
946 u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
948 rc = rc_allocate_device();
949 if (!rc)
950 return NULL;
952 snprintf(rr3->name, sizeof(rr3->name),
953 "RedRat3%s Infrared Remote Transceiver",
954 prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "");
956 usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
958 rc->input_name = rr3->name;
959 rc->input_phys = rr3->phys;
960 usb_to_input_id(rr3->udev, &rc->input_id);
961 rc->dev.parent = dev;
962 rc->priv = rr3;
963 rc->driver_type = RC_DRIVER_IR_RAW;
964 rc->allowed_protocols = RC_BIT_ALL;
965 rc->min_timeout = MS_TO_NS(RR3_RX_MIN_TIMEOUT);
966 rc->max_timeout = MS_TO_NS(RR3_RX_MAX_TIMEOUT);
967 rc->timeout = US_TO_NS(redrat3_get_timeout(rr3));
968 rc->s_timeout = redrat3_set_timeout;
969 rc->tx_ir = redrat3_transmit_ir;
970 rc->s_tx_carrier = redrat3_set_tx_carrier;
971 rc->s_carrier_report = redrat3_wideband_receiver;
972 rc->driver_name = DRIVER_NAME;
973 rc->rx_resolution = US_TO_NS(2);
974 rc->map_name = RC_MAP_HAUPPAUGE;
976 ret = rc_register_device(rc);
977 if (ret < 0) {
978 dev_err(dev, "remote dev registration failed\n");
979 goto out;
982 return rc;
984 out:
985 rc_free_device(rc);
986 return NULL;
989 static int redrat3_dev_probe(struct usb_interface *intf,
990 const struct usb_device_id *id)
992 struct usb_device *udev = interface_to_usbdev(intf);
993 struct device *dev = &intf->dev;
994 struct usb_host_interface *uhi;
995 struct redrat3_dev *rr3;
996 struct usb_endpoint_descriptor *ep;
997 struct usb_endpoint_descriptor *ep_narrow = NULL;
998 struct usb_endpoint_descriptor *ep_wide = NULL;
999 struct usb_endpoint_descriptor *ep_out = NULL;
1000 u8 addr, attrs;
1001 int pipe, i;
1002 int retval = -ENOMEM;
1004 uhi = intf->cur_altsetting;
1006 /* find our bulk-in and bulk-out endpoints */
1007 for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
1008 ep = &uhi->endpoint[i].desc;
1009 addr = ep->bEndpointAddress;
1010 attrs = ep->bmAttributes;
1012 if (((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
1013 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
1014 USB_ENDPOINT_XFER_BULK)) {
1015 dev_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
1016 ep->bEndpointAddress);
1017 /* data comes in on 0x82, 0x81 is for learning */
1018 if (ep->bEndpointAddress == RR3_NARROW_IN_EP_ADDR)
1019 ep_narrow = ep;
1020 if (ep->bEndpointAddress == RR3_WIDE_IN_EP_ADDR)
1021 ep_wide = ep;
1024 if ((ep_out == NULL) &&
1025 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
1026 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
1027 USB_ENDPOINT_XFER_BULK)) {
1028 dev_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
1029 ep->bEndpointAddress);
1030 ep_out = ep;
1034 if (!ep_narrow || !ep_out || !ep_wide) {
1035 dev_err(dev, "Couldn't find all endpoints\n");
1036 retval = -ENODEV;
1037 goto no_endpoints;
1040 /* allocate memory for our device state and initialize it */
1041 rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
1042 if (!rr3)
1043 goto no_endpoints;
1045 rr3->dev = &intf->dev;
1046 rr3->ep_narrow = ep_narrow;
1047 rr3->ep_out = ep_out;
1048 rr3->udev = udev;
1050 /* set up bulk-in endpoint */
1051 rr3->narrow_urb = usb_alloc_urb(0, GFP_KERNEL);
1052 if (!rr3->narrow_urb)
1053 goto redrat_free;
1055 rr3->wide_urb = usb_alloc_urb(0, GFP_KERNEL);
1056 if (!rr3->wide_urb)
1057 goto redrat_free;
1059 rr3->bulk_in_buf = usb_alloc_coherent(udev,
1060 le16_to_cpu(ep_narrow->wMaxPacketSize),
1061 GFP_KERNEL, &rr3->dma_in);
1062 if (!rr3->bulk_in_buf)
1063 goto redrat_free;
1065 pipe = usb_rcvbulkpipe(udev, ep_narrow->bEndpointAddress);
1066 usb_fill_bulk_urb(rr3->narrow_urb, udev, pipe, rr3->bulk_in_buf,
1067 le16_to_cpu(ep_narrow->wMaxPacketSize),
1068 redrat3_handle_async, rr3);
1069 rr3->narrow_urb->transfer_dma = rr3->dma_in;
1070 rr3->narrow_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1072 pipe = usb_rcvbulkpipe(udev, ep_wide->bEndpointAddress);
1073 usb_fill_bulk_urb(rr3->wide_urb, udev, pipe, rr3->bulk_in_buf,
1074 le16_to_cpu(ep_narrow->wMaxPacketSize),
1075 redrat3_handle_async, rr3);
1076 rr3->wide_urb->transfer_dma = rr3->dma_in;
1077 rr3->wide_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1079 redrat3_reset(rr3);
1080 redrat3_get_firmware_rev(rr3);
1082 /* default.. will get overridden by any sends with a freq defined */
1083 rr3->carrier = 38000;
1085 atomic_set(&rr3->flash, 0);
1086 rr3->flash_urb = usb_alloc_urb(0, GFP_KERNEL);
1087 if (!rr3->flash_urb)
1088 goto redrat_free;
1090 /* learn urb */
1091 rr3->learn_urb = usb_alloc_urb(0, GFP_KERNEL);
1092 if (!rr3->learn_urb)
1093 goto redrat_free;
1095 /* setup packet is 'c0 b2 0000 0000 0001' */
1096 rr3->learn_control.bRequestType = 0xc0;
1097 rr3->learn_control.bRequest = RR3_MODSIG_CAPTURE;
1098 rr3->learn_control.wLength = cpu_to_le16(1);
1100 usb_fill_control_urb(rr3->learn_urb, udev, usb_rcvctrlpipe(udev, 0),
1101 (unsigned char *)&rr3->learn_control,
1102 &rr3->learn_buf, sizeof(rr3->learn_buf),
1103 redrat3_learn_complete, rr3);
1105 /* setup packet is 'c0 b9 0000 0000 0001' */
1106 rr3->flash_control.bRequestType = 0xc0;
1107 rr3->flash_control.bRequest = RR3_BLINK_LED;
1108 rr3->flash_control.wLength = cpu_to_le16(1);
1110 usb_fill_control_urb(rr3->flash_urb, udev, usb_rcvctrlpipe(udev, 0),
1111 (unsigned char *)&rr3->flash_control,
1112 &rr3->flash_in_buf, sizeof(rr3->flash_in_buf),
1113 redrat3_led_complete, rr3);
1115 /* led control */
1116 rr3->led.name = "redrat3:red:feedback";
1117 rr3->led.default_trigger = "rc-feedback";
1118 rr3->led.brightness_set = redrat3_brightness_set;
1119 retval = led_classdev_register(&intf->dev, &rr3->led);
1120 if (retval)
1121 goto redrat_free;
1123 rr3->rc = redrat3_init_rc_dev(rr3);
1124 if (!rr3->rc) {
1125 retval = -ENOMEM;
1126 goto led_free;
1129 /* might be all we need to do? */
1130 retval = redrat3_enable_detector(rr3);
1131 if (retval < 0)
1132 goto led_free;
1134 /* we can register the device now, as it is ready */
1135 usb_set_intfdata(intf, rr3);
1137 return 0;
1139 led_free:
1140 led_classdev_unregister(&rr3->led);
1141 redrat_free:
1142 redrat3_delete(rr3, rr3->udev);
1144 no_endpoints:
1145 return retval;
1148 static void redrat3_dev_disconnect(struct usb_interface *intf)
1150 struct usb_device *udev = interface_to_usbdev(intf);
1151 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1153 usb_set_intfdata(intf, NULL);
1154 rc_unregister_device(rr3->rc);
1155 led_classdev_unregister(&rr3->led);
1156 redrat3_delete(rr3, udev);
1159 static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
1161 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1163 led_classdev_suspend(&rr3->led);
1164 usb_kill_urb(rr3->narrow_urb);
1165 usb_kill_urb(rr3->wide_urb);
1166 usb_kill_urb(rr3->flash_urb);
1167 return 0;
1170 static int redrat3_dev_resume(struct usb_interface *intf)
1172 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1174 if (usb_submit_urb(rr3->narrow_urb, GFP_ATOMIC))
1175 return -EIO;
1176 if (usb_submit_urb(rr3->wide_urb, GFP_ATOMIC))
1177 return -EIO;
1178 led_classdev_resume(&rr3->led);
1179 return 0;
1182 static struct usb_driver redrat3_dev_driver = {
1183 .name = DRIVER_NAME,
1184 .probe = redrat3_dev_probe,
1185 .disconnect = redrat3_dev_disconnect,
1186 .suspend = redrat3_dev_suspend,
1187 .resume = redrat3_dev_resume,
1188 .reset_resume = redrat3_dev_resume,
1189 .id_table = redrat3_dev_table
1192 module_usb_driver(redrat3_dev_driver);
1194 MODULE_DESCRIPTION(DRIVER_DESC);
1195 MODULE_AUTHOR(DRIVER_AUTHOR);
1196 MODULE_AUTHOR(DRIVER_AUTHOR2);
1197 MODULE_LICENSE("GPL");
1198 MODULE_DEVICE_TABLE(usb, redrat3_dev_table);