irqchip/s3c24xx: Mark init_eint as __maybe_unused
[linux/fpc-iii.git] / drivers / media / rc / redrat3.c
blobec74244a38530a937d07280a304debdf7a5988b7
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 /* Return the status of RC detector capture */
85 #define RR3_RC_DET_STATUS 0xbd
86 /* Reset redrat */
87 #define RR3_RESET 0xa0
89 /* Max number of lengths in the signal. */
90 #define RR3_IR_IO_MAX_LENGTHS 0x01
91 /* Periods to measure mod. freq. */
92 #define RR3_IR_IO_PERIODS_MF 0x02
93 /* Size of memory for main signal data */
94 #define RR3_IR_IO_SIG_MEM_SIZE 0x03
95 /* Delta value when measuring lengths */
96 #define RR3_IR_IO_LENGTH_FUZZ 0x04
97 /* Timeout for end of signal detection */
98 #define RR3_IR_IO_SIG_TIMEOUT 0x05
99 /* Minimum value for pause recognition. */
100 #define RR3_IR_IO_MIN_PAUSE 0x06
102 /* Clock freq. of EZ-USB chip */
103 #define RR3_CLK 24000000
104 /* Clock periods per timer count */
105 #define RR3_CLK_PER_COUNT 12
106 /* (RR3_CLK / RR3_CLK_PER_COUNT) */
107 #define RR3_CLK_CONV_FACTOR 2000000
108 /* USB bulk-in IR data endpoint address */
109 #define RR3_BULK_IN_EP_ADDR 0x82
111 /* Size of the fixed-length portion of the signal */
112 #define RR3_DRIVER_MAXLENS 128
113 #define RR3_MAX_SIG_SIZE 512
114 #define RR3_TIME_UNIT 50
115 #define RR3_END_OF_SIGNAL 0x7f
116 #define RR3_TX_TRAILER_LEN 2
117 #define RR3_RX_MIN_TIMEOUT 5
118 #define RR3_RX_MAX_TIMEOUT 2000
120 /* The 8051's CPUCS Register address */
121 #define RR3_CPUCS_REG_ADDR 0x7f92
123 #define USB_RR3USB_VENDOR_ID 0x112a
124 #define USB_RR3USB_PRODUCT_ID 0x0001
125 #define USB_RR3IIUSB_PRODUCT_ID 0x0005
127 struct redrat3_header {
128 __be16 length;
129 __be16 transfer_type;
130 } __packed;
132 /* sending and receiving irdata */
133 struct redrat3_irdata {
134 struct redrat3_header header;
135 __be32 pause;
136 __be16 mod_freq_count;
137 __be16 num_periods;
138 __u8 max_lengths;
139 __u8 no_lengths;
140 __be16 max_sig_size;
141 __be16 sig_size;
142 __u8 no_repeats;
143 __be16 lens[RR3_DRIVER_MAXLENS]; /* not aligned */
144 __u8 sigdata[RR3_MAX_SIG_SIZE];
145 } __packed;
147 /* firmware errors */
148 struct redrat3_error {
149 struct redrat3_header header;
150 __be16 fw_error;
151 } __packed;
153 /* table of devices that work with this driver */
154 static struct usb_device_id redrat3_dev_table[] = {
155 /* Original version of the RedRat3 */
156 {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)},
157 /* Second Version/release of the RedRat3 - RetRat3-II */
158 {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)},
159 {} /* Terminating entry */
162 /* Structure to hold all of our device specific stuff */
163 struct redrat3_dev {
164 /* core device bits */
165 struct rc_dev *rc;
166 struct device *dev;
168 /* led control */
169 struct led_classdev led;
170 atomic_t flash;
171 struct usb_ctrlrequest flash_control;
172 struct urb *flash_urb;
173 u8 flash_in_buf;
175 /* save off the usb device pointer */
176 struct usb_device *udev;
178 /* the receive endpoint */
179 struct usb_endpoint_descriptor *ep_in;
180 /* the buffer to receive data */
181 void *bulk_in_buf;
182 /* urb used to read ir data */
183 struct urb *read_urb;
185 /* the send endpoint */
186 struct usb_endpoint_descriptor *ep_out;
188 /* usb dma */
189 dma_addr_t dma_in;
191 /* rx signal timeout timer */
192 struct timer_list rx_timeout;
193 u32 hw_timeout;
195 /* Is the device currently transmitting?*/
196 bool transmitting;
198 /* store for current packet */
199 struct redrat3_irdata irdata;
200 u16 bytes_read;
202 u32 carrier;
204 char name[64];
205 char phys[64];
209 * redrat3_issue_async
211 * Issues an async read to the ir data in port..
212 * sets the callback to be redrat3_handle_async
214 static void redrat3_issue_async(struct redrat3_dev *rr3)
216 int res;
218 res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC);
219 if (res)
220 dev_dbg(rr3->dev,
221 "%s: receive request FAILED! (res %d, len %d)\n",
222 __func__, res, rr3->read_urb->transfer_buffer_length);
225 static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
227 if (!rr3->transmitting && (code != 0x40))
228 dev_info(rr3->dev, "fw error code 0x%02x: ", code);
230 switch (code) {
231 case 0x00:
232 pr_cont("No Error\n");
233 break;
235 /* Codes 0x20 through 0x2f are IR Firmware Errors */
236 case 0x20:
237 pr_cont("Initial signal pulse not long enough "
238 "to measure carrier frequency\n");
239 break;
240 case 0x21:
241 pr_cont("Not enough length values allocated for signal\n");
242 break;
243 case 0x22:
244 pr_cont("Not enough memory allocated for signal data\n");
245 break;
246 case 0x23:
247 pr_cont("Too many signal repeats\n");
248 break;
249 case 0x28:
250 pr_cont("Insufficient memory available for IR signal "
251 "data memory allocation\n");
252 break;
253 case 0x29:
254 pr_cont("Insufficient memory available "
255 "for IrDa signal data memory allocation\n");
256 break;
258 /* Codes 0x30 through 0x3f are USB Firmware Errors */
259 case 0x30:
260 pr_cont("Insufficient memory available for bulk "
261 "transfer structure\n");
262 break;
265 * Other error codes... These are primarily errors that can occur in
266 * the control messages sent to the redrat
268 case 0x40:
269 if (!rr3->transmitting)
270 pr_cont("Signal capture has been terminated\n");
271 break;
272 case 0x41:
273 pr_cont("Attempt to set/get and unknown signal I/O "
274 "algorithm parameter\n");
275 break;
276 case 0x42:
277 pr_cont("Signal capture already started\n");
278 break;
280 default:
281 pr_cont("Unknown Error\n");
282 break;
286 static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata)
288 u32 mod_freq = 0;
289 u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count);
291 if (mod_freq_count != 0)
292 mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) /
293 (mod_freq_count * RR3_CLK_PER_COUNT);
295 return mod_freq;
298 /* this function scales down the figures for the same result... */
299 static u32 redrat3_len_to_us(u32 length)
301 u32 biglen = length * 1000;
302 u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000;
303 u32 result = (u32) (biglen / divisor);
305 /* don't allow zero lengths to go back, breaks lirc */
306 return result ? result : 1;
310 * convert us back into redrat3 lengths
312 * length * 1000 length * 1000000
313 * ------------- = ---------------- = micro
314 * rr3clk / 1000 rr3clk
316 * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
317 * ----- = 4 ----- = 6 -------------- = len ---------------------
318 * 3 2 1000000 1000
320 static u32 redrat3_us_to_len(u32 microsec)
322 u32 result;
323 u32 divisor;
325 microsec = (microsec > IR_MAX_DURATION) ? IR_MAX_DURATION : microsec;
326 divisor = (RR3_CLK_CONV_FACTOR / 1000);
327 result = (u32)(microsec * divisor) / 1000;
329 /* don't allow zero lengths to go back, breaks lirc */
330 return result ? result : 1;
333 /* timer callback to send reset event */
334 static void redrat3_rx_timeout(unsigned long data)
336 struct redrat3_dev *rr3 = (struct redrat3_dev *)data;
338 dev_dbg(rr3->dev, "calling ir_raw_event_reset\n");
339 ir_raw_event_reset(rr3->rc);
342 static void redrat3_process_ir_data(struct redrat3_dev *rr3)
344 DEFINE_IR_RAW_EVENT(rawir);
345 struct device *dev;
346 unsigned i, trailer = 0;
347 unsigned sig_size, single_len, offset, val;
348 unsigned long delay;
349 u32 mod_freq;
351 if (!rr3) {
352 pr_err("%s called with no context!\n", __func__);
353 return;
356 dev = rr3->dev;
358 /* Make sure we reset the IR kfifo after a bit of inactivity */
359 delay = usecs_to_jiffies(rr3->hw_timeout);
360 mod_timer(&rr3->rx_timeout, jiffies + delay);
362 mod_freq = redrat3_val_to_mod_freq(&rr3->irdata);
363 dev_dbg(dev, "Got mod_freq of %u\n", mod_freq);
365 /* process each rr3 encoded byte into an int */
366 sig_size = be16_to_cpu(rr3->irdata.sig_size);
367 for (i = 0; i < sig_size; i++) {
368 offset = rr3->irdata.sigdata[i];
369 val = get_unaligned_be16(&rr3->irdata.lens[offset]);
370 single_len = redrat3_len_to_us(val);
372 /* we should always get pulse/space/pulse/space samples */
373 if (i % 2)
374 rawir.pulse = false;
375 else
376 rawir.pulse = true;
378 rawir.duration = US_TO_NS(single_len);
379 /* Save initial pulse length to fudge trailer */
380 if (i == 0)
381 trailer = rawir.duration;
382 /* cap the value to IR_MAX_DURATION */
383 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
384 IR_MAX_DURATION : rawir.duration;
386 dev_dbg(dev, "storing %s with duration %d (i: %d)\n",
387 rawir.pulse ? "pulse" : "space", rawir.duration, i);
388 ir_raw_event_store_with_filter(rr3->rc, &rawir);
391 /* add a trailing space, if need be */
392 if (i % 2) {
393 rawir.pulse = false;
394 /* this duration is made up, and may not be ideal... */
395 if (trailer < US_TO_NS(1000))
396 rawir.duration = US_TO_NS(2800);
397 else
398 rawir.duration = trailer;
399 dev_dbg(dev, "storing trailing space with duration %d\n",
400 rawir.duration);
401 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 redrat3_issue_async(rr3);
456 return 0;
459 static inline void redrat3_delete(struct redrat3_dev *rr3,
460 struct usb_device *udev)
462 usb_kill_urb(rr3->read_urb);
463 usb_kill_urb(rr3->flash_urb);
464 usb_free_urb(rr3->read_urb);
465 usb_free_urb(rr3->flash_urb);
466 usb_free_coherent(udev, le16_to_cpu(rr3->ep_in->wMaxPacketSize),
467 rr3->bulk_in_buf, rr3->dma_in);
469 kfree(rr3);
472 static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
474 __be32 *tmp;
475 u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
476 int len, ret, pipe;
478 len = sizeof(*tmp);
479 tmp = kzalloc(len, GFP_KERNEL);
480 if (!tmp) {
481 dev_warn(rr3->dev, "Memory allocation faillure\n");
482 return timeout;
485 pipe = usb_rcvctrlpipe(rr3->udev, 0);
486 ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
487 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
488 RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5);
489 if (ret != len)
490 dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
491 else {
492 timeout = redrat3_len_to_us(be32_to_cpup(tmp));
494 dev_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
497 kfree(tmp);
499 return timeout;
502 static void redrat3_reset(struct redrat3_dev *rr3)
504 struct usb_device *udev = rr3->udev;
505 struct device *dev = rr3->dev;
506 int rc, rxpipe, txpipe;
507 u8 *val;
508 int len = sizeof(u8);
510 rxpipe = usb_rcvctrlpipe(udev, 0);
511 txpipe = usb_sndctrlpipe(udev, 0);
513 val = kmalloc(len, GFP_KERNEL);
514 if (!val) {
515 dev_err(dev, "Memory allocation failure\n");
516 return;
519 *val = 0x01;
520 rc = usb_control_msg(udev, rxpipe, RR3_RESET,
521 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
522 RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25);
523 dev_dbg(dev, "reset returned 0x%02x\n", rc);
525 *val = 5;
526 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
527 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
528 RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25);
529 dev_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
531 *val = RR3_DRIVER_MAXLENS;
532 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
533 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
534 RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25);
535 dev_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
537 kfree(val);
540 static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
542 int rc = 0;
543 char *buffer;
545 buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL);
546 if (!buffer) {
547 dev_err(rr3->dev, "Memory allocation failure\n");
548 return;
551 rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
552 RR3_FW_VERSION,
553 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
554 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5);
556 if (rc >= 0)
557 dev_info(rr3->dev, "Firmware rev: %s", buffer);
558 else
559 dev_err(rr3->dev, "Problem fetching firmware ID\n");
561 kfree(buffer);
564 static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
566 struct redrat3_header *header = rr3->bulk_in_buf;
567 unsigned pktlen, pkttype;
569 /* grab the Length and type of transfer */
570 pktlen = be16_to_cpu(header->length);
571 pkttype = be16_to_cpu(header->transfer_type);
573 if (pktlen > sizeof(rr3->irdata)) {
574 dev_warn(rr3->dev, "packet length %u too large\n", pktlen);
575 return;
578 switch (pkttype) {
579 case RR3_ERROR:
580 if (len >= sizeof(struct redrat3_error)) {
581 struct redrat3_error *error = rr3->bulk_in_buf;
582 unsigned fw_error = be16_to_cpu(error->fw_error);
583 redrat3_dump_fw_error(rr3, fw_error);
585 break;
587 case RR3_MOD_SIGNAL_IN:
588 memcpy(&rr3->irdata, rr3->bulk_in_buf, len);
589 rr3->bytes_read = len;
590 dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
591 rr3->bytes_read, pktlen);
592 break;
594 default:
595 dev_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
596 pkttype, len, pktlen);
597 break;
601 static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len)
603 void *irdata = &rr3->irdata;
605 if (len + rr3->bytes_read > sizeof(rr3->irdata)) {
606 dev_warn(rr3->dev, "too much data for packet\n");
607 rr3->bytes_read = 0;
608 return;
611 memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len);
613 rr3->bytes_read += len;
614 dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read,
615 be16_to_cpu(rr3->irdata.header.length));
618 /* gather IR data from incoming urb, process it when we have enough */
619 static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len)
621 struct device *dev = rr3->dev;
622 unsigned pkttype;
623 int ret = 0;
625 if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) {
626 redrat3_read_packet_start(rr3, len);
627 } else if (rr3->bytes_read != 0) {
628 redrat3_read_packet_continue(rr3, len);
629 } else if (rr3->bytes_read == 0) {
630 dev_err(dev, "error: no packet data read\n");
631 ret = -ENODATA;
632 goto out;
635 if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length) +
636 sizeof(struct redrat3_header))
637 /* we're still accumulating data */
638 return 0;
640 /* if we get here, we've got IR data to decode */
641 pkttype = be16_to_cpu(rr3->irdata.header.transfer_type);
642 if (pkttype == RR3_MOD_SIGNAL_IN)
643 redrat3_process_ir_data(rr3);
644 else
645 dev_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n",
646 pkttype);
648 out:
649 rr3->bytes_read = 0;
650 return ret;
653 /* callback function from USB when async USB request has completed */
654 static void redrat3_handle_async(struct urb *urb)
656 struct redrat3_dev *rr3;
657 int ret;
659 if (!urb)
660 return;
662 rr3 = urb->context;
663 if (!rr3) {
664 pr_err("%s called with invalid context!\n", __func__);
665 usb_unlink_urb(urb);
666 return;
669 switch (urb->status) {
670 case 0:
671 ret = redrat3_get_ir_data(rr3, urb->actual_length);
672 if (!ret) {
673 /* no error, prepare to read more */
674 redrat3_issue_async(rr3);
676 break;
678 case -ECONNRESET:
679 case -ENOENT:
680 case -ESHUTDOWN:
681 usb_unlink_urb(urb);
682 return;
684 case -EPIPE:
685 default:
686 dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
687 rr3->bytes_read = 0;
688 break;
692 static u16 mod_freq_to_val(unsigned int mod_freq)
694 int mult = 6000000;
696 /* Clk used in mod. freq. generation is CLK24/4. */
697 return 65536 - (mult / mod_freq);
700 static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
702 struct redrat3_dev *rr3 = rcdev->priv;
703 struct device *dev = rr3->dev;
705 dev_dbg(dev, "Setting modulation frequency to %u", carrier);
706 if (carrier == 0)
707 return -EINVAL;
709 rr3->carrier = carrier;
711 return carrier;
714 static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
715 unsigned count)
717 struct redrat3_dev *rr3 = rcdev->priv;
718 struct device *dev = rr3->dev;
719 struct redrat3_irdata *irdata = NULL;
720 int ret, ret_len;
721 int lencheck, cur_sample_len, pipe;
722 int *sample_lens = NULL;
723 u8 curlencheck = 0;
724 unsigned i, sendbuf_len;
726 if (rr3->transmitting) {
727 dev_warn(dev, "%s: transmitter already in use\n", __func__);
728 return -EAGAIN;
731 if (count > RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN)
732 return -EINVAL;
734 /* rr3 will disable rc detector on transmit */
735 rr3->transmitting = true;
737 sample_lens = kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS, GFP_KERNEL);
738 if (!sample_lens) {
739 ret = -ENOMEM;
740 goto out;
743 irdata = kzalloc(sizeof(*irdata), GFP_KERNEL);
744 if (!irdata) {
745 ret = -ENOMEM;
746 goto out;
749 for (i = 0; i < count; i++) {
750 cur_sample_len = redrat3_us_to_len(txbuf[i]);
751 if (cur_sample_len > 0xffff) {
752 dev_warn(dev, "transmit period of %uus truncated to %uus\n",
753 txbuf[i], redrat3_len_to_us(0xffff));
754 cur_sample_len = 0xffff;
756 for (lencheck = 0; lencheck < curlencheck; lencheck++) {
757 if (sample_lens[lencheck] == cur_sample_len)
758 break;
760 if (lencheck == curlencheck) {
761 dev_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
762 i, txbuf[i], curlencheck, cur_sample_len);
763 if (curlencheck < RR3_DRIVER_MAXLENS) {
764 /* now convert the value to a proper
765 * rr3 value.. */
766 sample_lens[curlencheck] = cur_sample_len;
767 put_unaligned_be16(cur_sample_len,
768 &irdata->lens[curlencheck]);
769 curlencheck++;
770 } else {
771 ret = -EINVAL;
772 goto out;
775 irdata->sigdata[i] = lencheck;
778 irdata->sigdata[count] = RR3_END_OF_SIGNAL;
779 irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL;
781 sendbuf_len = offsetof(struct redrat3_irdata,
782 sigdata[count + RR3_TX_TRAILER_LEN]);
783 /* fill in our packet header */
784 irdata->header.length = cpu_to_be16(sendbuf_len -
785 sizeof(struct redrat3_header));
786 irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT);
787 irdata->pause = cpu_to_be32(redrat3_len_to_us(100));
788 irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier));
789 irdata->no_lengths = curlencheck;
790 irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN);
792 pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
793 ret = usb_bulk_msg(rr3->udev, pipe, irdata,
794 sendbuf_len, &ret_len, 10 * HZ);
795 dev_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret);
797 /* now tell the hardware to transmit what we sent it */
798 pipe = usb_rcvctrlpipe(rr3->udev, 0);
799 ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
800 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
801 0, 0, irdata, 2, HZ * 10);
803 if (ret < 0)
804 dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
805 else
806 ret = count;
808 out:
809 kfree(sample_lens);
810 kfree(irdata);
812 rr3->transmitting = false;
813 /* rr3 re-enables rc detector because it was enabled before */
815 return ret;
818 static void redrat3_brightness_set(struct led_classdev *led_dev, enum
819 led_brightness brightness)
821 struct redrat3_dev *rr3 = container_of(led_dev, struct redrat3_dev,
822 led);
824 if (brightness != LED_OFF && atomic_cmpxchg(&rr3->flash, 0, 1) == 0) {
825 int ret = usb_submit_urb(rr3->flash_urb, GFP_ATOMIC);
826 if (ret != 0) {
827 dev_dbg(rr3->dev, "%s: unexpected ret of %d\n",
828 __func__, ret);
829 atomic_set(&rr3->flash, 0);
834 static void redrat3_led_complete(struct urb *urb)
836 struct redrat3_dev *rr3 = urb->context;
838 switch (urb->status) {
839 case 0:
840 break;
841 case -ECONNRESET:
842 case -ENOENT:
843 case -ESHUTDOWN:
844 usb_unlink_urb(urb);
845 return;
846 case -EPIPE:
847 default:
848 dev_dbg(rr3->dev, "Error: urb status = %d\n", urb->status);
849 break;
852 rr3->led.brightness = LED_OFF;
853 atomic_dec(&rr3->flash);
856 static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
858 struct device *dev = rr3->dev;
859 struct rc_dev *rc;
860 int ret = -ENODEV;
861 u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
863 rc = rc_allocate_device();
864 if (!rc) {
865 dev_err(dev, "remote input dev allocation failed\n");
866 goto out;
869 snprintf(rr3->name, sizeof(rr3->name), "RedRat3%s "
870 "Infrared Remote Transceiver (%04x:%04x)",
871 prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "",
872 le16_to_cpu(rr3->udev->descriptor.idVendor), prod);
874 usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
876 rc->input_name = rr3->name;
877 rc->input_phys = rr3->phys;
878 usb_to_input_id(rr3->udev, &rc->input_id);
879 rc->dev.parent = dev;
880 rc->priv = rr3;
881 rc->driver_type = RC_DRIVER_IR_RAW;
882 rc->allowed_protocols = RC_BIT_ALL;
883 rc->timeout = US_TO_NS(2750);
884 rc->tx_ir = redrat3_transmit_ir;
885 rc->s_tx_carrier = redrat3_set_tx_carrier;
886 rc->driver_name = DRIVER_NAME;
887 rc->rx_resolution = US_TO_NS(2);
888 rc->map_name = RC_MAP_HAUPPAUGE;
890 ret = rc_register_device(rc);
891 if (ret < 0) {
892 dev_err(dev, "remote dev registration failed\n");
893 goto out;
896 return rc;
898 out:
899 rc_free_device(rc);
900 return NULL;
903 static int redrat3_dev_probe(struct usb_interface *intf,
904 const struct usb_device_id *id)
906 struct usb_device *udev = interface_to_usbdev(intf);
907 struct device *dev = &intf->dev;
908 struct usb_host_interface *uhi;
909 struct redrat3_dev *rr3;
910 struct usb_endpoint_descriptor *ep;
911 struct usb_endpoint_descriptor *ep_in = NULL;
912 struct usb_endpoint_descriptor *ep_out = NULL;
913 u8 addr, attrs;
914 int pipe, i;
915 int retval = -ENOMEM;
917 uhi = intf->cur_altsetting;
919 /* find our bulk-in and bulk-out endpoints */
920 for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
921 ep = &uhi->endpoint[i].desc;
922 addr = ep->bEndpointAddress;
923 attrs = ep->bmAttributes;
925 if ((ep_in == NULL) &&
926 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
927 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
928 USB_ENDPOINT_XFER_BULK)) {
929 dev_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
930 ep->bEndpointAddress);
931 /* data comes in on 0x82, 0x81 is for other data... */
932 if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR)
933 ep_in = ep;
936 if ((ep_out == NULL) &&
937 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
938 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
939 USB_ENDPOINT_XFER_BULK)) {
940 dev_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
941 ep->bEndpointAddress);
942 ep_out = ep;
946 if (!ep_in || !ep_out) {
947 dev_err(dev, "Couldn't find both in and out endpoints\n");
948 retval = -ENODEV;
949 goto no_endpoints;
952 /* allocate memory for our device state and initialize it */
953 rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
954 if (rr3 == NULL) {
955 dev_err(dev, "Memory allocation failure\n");
956 goto no_endpoints;
959 rr3->dev = &intf->dev;
961 /* set up bulk-in endpoint */
962 rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL);
963 if (!rr3->read_urb) {
964 dev_err(dev, "Read urb allocation failure\n");
965 goto error;
968 rr3->ep_in = ep_in;
969 rr3->bulk_in_buf = usb_alloc_coherent(udev,
970 le16_to_cpu(ep_in->wMaxPacketSize), GFP_KERNEL, &rr3->dma_in);
971 if (!rr3->bulk_in_buf) {
972 dev_err(dev, "Read buffer allocation failure\n");
973 goto error;
976 pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress);
977 usb_fill_bulk_urb(rr3->read_urb, udev, pipe, rr3->bulk_in_buf,
978 le16_to_cpu(ep_in->wMaxPacketSize), redrat3_handle_async, rr3);
979 rr3->read_urb->transfer_dma = rr3->dma_in;
980 rr3->read_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
982 rr3->ep_out = ep_out;
983 rr3->udev = udev;
985 redrat3_reset(rr3);
986 redrat3_get_firmware_rev(rr3);
988 /* might be all we need to do? */
989 retval = redrat3_enable_detector(rr3);
990 if (retval < 0)
991 goto error;
993 /* store current hardware timeout, in us, will use for kfifo resets */
994 rr3->hw_timeout = redrat3_get_timeout(rr3);
996 /* default.. will get overridden by any sends with a freq defined */
997 rr3->carrier = 38000;
999 /* led control */
1000 rr3->led.name = "redrat3:red:feedback";
1001 rr3->led.default_trigger = "rc-feedback";
1002 rr3->led.brightness_set = redrat3_brightness_set;
1003 retval = led_classdev_register(&intf->dev, &rr3->led);
1004 if (retval)
1005 goto error;
1007 atomic_set(&rr3->flash, 0);
1008 rr3->flash_urb = usb_alloc_urb(0, GFP_KERNEL);
1009 if (!rr3->flash_urb) {
1010 retval = -ENOMEM;
1011 goto led_free_error;
1014 /* setup packet is 'c0 b9 0000 0000 0001' */
1015 rr3->flash_control.bRequestType = 0xc0;
1016 rr3->flash_control.bRequest = RR3_BLINK_LED;
1017 rr3->flash_control.wLength = cpu_to_le16(1);
1019 usb_fill_control_urb(rr3->flash_urb, udev, usb_rcvctrlpipe(udev, 0),
1020 (unsigned char *)&rr3->flash_control,
1021 &rr3->flash_in_buf, sizeof(rr3->flash_in_buf),
1022 redrat3_led_complete, rr3);
1024 rr3->rc = redrat3_init_rc_dev(rr3);
1025 if (!rr3->rc) {
1026 retval = -ENOMEM;
1027 goto led_free_error;
1029 setup_timer(&rr3->rx_timeout, redrat3_rx_timeout, (unsigned long)rr3);
1031 /* we can register the device now, as it is ready */
1032 usb_set_intfdata(intf, rr3);
1034 return 0;
1036 led_free_error:
1037 led_classdev_unregister(&rr3->led);
1038 error:
1039 redrat3_delete(rr3, rr3->udev);
1041 no_endpoints:
1042 dev_err(dev, "%s: retval = %x", __func__, retval);
1044 return retval;
1047 static void redrat3_dev_disconnect(struct usb_interface *intf)
1049 struct usb_device *udev = interface_to_usbdev(intf);
1050 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1052 if (!rr3)
1053 return;
1055 usb_set_intfdata(intf, NULL);
1056 rc_unregister_device(rr3->rc);
1057 led_classdev_unregister(&rr3->led);
1058 del_timer_sync(&rr3->rx_timeout);
1059 redrat3_delete(rr3, udev);
1062 static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
1064 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1066 led_classdev_suspend(&rr3->led);
1067 usb_kill_urb(rr3->read_urb);
1068 usb_kill_urb(rr3->flash_urb);
1069 return 0;
1072 static int redrat3_dev_resume(struct usb_interface *intf)
1074 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1076 if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC))
1077 return -EIO;
1078 led_classdev_resume(&rr3->led);
1079 return 0;
1082 static struct usb_driver redrat3_dev_driver = {
1083 .name = DRIVER_NAME,
1084 .probe = redrat3_dev_probe,
1085 .disconnect = redrat3_dev_disconnect,
1086 .suspend = redrat3_dev_suspend,
1087 .resume = redrat3_dev_resume,
1088 .reset_resume = redrat3_dev_resume,
1089 .id_table = redrat3_dev_table
1092 module_usb_driver(redrat3_dev_driver);
1094 MODULE_DESCRIPTION(DRIVER_DESC);
1095 MODULE_AUTHOR(DRIVER_AUTHOR);
1096 MODULE_AUTHOR(DRIVER_AUTHOR2);
1097 MODULE_LICENSE("GPL");
1098 MODULE_DEVICE_TABLE(usb, redrat3_dev_table);