1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
22154be65SJarod Wilson /*
32154be65SJarod Wilson * USB RedRat3 IR Transceiver rc-core driver
42154be65SJarod Wilson *
52154be65SJarod Wilson * Copyright (c) 2011 by Jarod Wilson <jarod@redhat.com>
62154be65SJarod Wilson * based heavily on the work of Stephen Cox, with additional
72154be65SJarod Wilson * help from RedRat Ltd.
82154be65SJarod Wilson *
9adb08a09Slijian * This driver began life based on an old version of the first-generation
102154be65SJarod Wilson * lirc_mceusb driver from the lirc 0.7.2 distribution. It was then
112154be65SJarod Wilson * significantly rewritten by Stephen Cox with the aid of RedRat Ltd's
122154be65SJarod Wilson * Chris Dodge.
132154be65SJarod Wilson *
142154be65SJarod Wilson * The driver was then ported to rc-core and significantly rewritten again,
152154be65SJarod Wilson * by Jarod, using the in-kernel mceusb driver as a guide, after an initial
162154be65SJarod Wilson * port effort was started by Stephen.
172154be65SJarod Wilson *
182154be65SJarod Wilson * TODO LIST:
192154be65SJarod Wilson * - fix lirc not showing repeats properly
202154be65SJarod Wilson * --
212154be65SJarod Wilson *
222154be65SJarod Wilson * The RedRat3 is a USB transceiver with both send & receive,
232154be65SJarod Wilson * with 2 separate sensors available for receive to enable
242154be65SJarod Wilson * both good long range reception for general use, and good
252154be65SJarod Wilson * short range reception when required for learning a signal.
262154be65SJarod Wilson *
272154be65SJarod Wilson * http://www.redrat.co.uk/
282154be65SJarod Wilson *
292154be65SJarod Wilson * It uses its own little protocol to communicate, the required
302154be65SJarod Wilson * parts of which are embedded within this driver.
312154be65SJarod Wilson * --
322154be65SJarod Wilson */
332154be65SJarod Wilson
344c055a5aSSean Young #include <asm/unaligned.h>
352154be65SJarod Wilson #include <linux/device.h>
36bf139726SSean Young #include <linux/leds.h>
372154be65SJarod Wilson #include <linux/module.h>
382154be65SJarod Wilson #include <linux/slab.h>
392154be65SJarod Wilson #include <linux/usb.h>
402154be65SJarod Wilson #include <linux/usb/input.h>
412154be65SJarod Wilson #include <media/rc-core.h>
422154be65SJarod Wilson
432154be65SJarod Wilson /* Driver Information */
442154be65SJarod Wilson #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
452154be65SJarod Wilson #define DRIVER_AUTHOR2 "The Dweller, Stephen Cox"
462154be65SJarod Wilson #define DRIVER_DESC "RedRat3 USB IR Transceiver Driver"
472154be65SJarod Wilson #define DRIVER_NAME "redrat3"
482154be65SJarod Wilson
492154be65SJarod Wilson /* bulk data transfer types */
502154be65SJarod Wilson #define RR3_ERROR 0x01
512154be65SJarod Wilson #define RR3_MOD_SIGNAL_IN 0x20
522154be65SJarod Wilson #define RR3_MOD_SIGNAL_OUT 0x21
532154be65SJarod Wilson
542154be65SJarod Wilson /* Get the RR firmware version */
552154be65SJarod Wilson #define RR3_FW_VERSION 0xb1
562154be65SJarod Wilson #define RR3_FW_VERSION_LEN 64
572154be65SJarod Wilson /* Send encoded signal bulk-sent earlier*/
582154be65SJarod Wilson #define RR3_TX_SEND_SIGNAL 0xb3
592154be65SJarod Wilson #define RR3_SET_IR_PARAM 0xb7
602154be65SJarod Wilson #define RR3_GET_IR_PARAM 0xb8
612154be65SJarod Wilson /* Blink the red LED on the device */
622154be65SJarod Wilson #define RR3_BLINK_LED 0xb9
632154be65SJarod Wilson /* Read serial number of device */
642154be65SJarod Wilson #define RR3_READ_SER_NO 0xba
652154be65SJarod Wilson #define RR3_SER_NO_LEN 4
662154be65SJarod Wilson /* Start capture with the RC receiver */
672154be65SJarod Wilson #define RR3_RC_DET_ENABLE 0xbb
682154be65SJarod Wilson /* Stop capture with the RC receiver */
692154be65SJarod Wilson #define RR3_RC_DET_DISABLE 0xbc
70c49fcddeSSean Young /* Start capture with the wideband receiver */
71c49fcddeSSean Young #define RR3_MODSIG_CAPTURE 0xb2
722154be65SJarod Wilson /* Return the status of RC detector capture */
732154be65SJarod Wilson #define RR3_RC_DET_STATUS 0xbd
742154be65SJarod Wilson /* Reset redrat */
752154be65SJarod Wilson #define RR3_RESET 0xa0
762154be65SJarod Wilson
772154be65SJarod Wilson /* Max number of lengths in the signal. */
782154be65SJarod Wilson #define RR3_IR_IO_MAX_LENGTHS 0x01
792154be65SJarod Wilson /* Periods to measure mod. freq. */
802154be65SJarod Wilson #define RR3_IR_IO_PERIODS_MF 0x02
812154be65SJarod Wilson /* Size of memory for main signal data */
822154be65SJarod Wilson #define RR3_IR_IO_SIG_MEM_SIZE 0x03
832154be65SJarod Wilson /* Delta value when measuring lengths */
842154be65SJarod Wilson #define RR3_IR_IO_LENGTH_FUZZ 0x04
852154be65SJarod Wilson /* Timeout for end of signal detection */
862154be65SJarod Wilson #define RR3_IR_IO_SIG_TIMEOUT 0x05
8739c1cb2bSJonathan McCrohan /* Minimum value for pause recognition. */
882154be65SJarod Wilson #define RR3_IR_IO_MIN_PAUSE 0x06
892154be65SJarod Wilson
902154be65SJarod Wilson /* Clock freq. of EZ-USB chip */
912154be65SJarod Wilson #define RR3_CLK 24000000
922154be65SJarod Wilson /* Clock periods per timer count */
932154be65SJarod Wilson #define RR3_CLK_PER_COUNT 12
942154be65SJarod Wilson /* (RR3_CLK / RR3_CLK_PER_COUNT) */
952154be65SJarod Wilson #define RR3_CLK_CONV_FACTOR 2000000
96c49fcddeSSean Young /* USB bulk-in wideband IR data endpoint address */
97c49fcddeSSean Young #define RR3_WIDE_IN_EP_ADDR 0x81
98c49fcddeSSean Young /* USB bulk-in narrowband IR data endpoint address */
99c49fcddeSSean Young #define RR3_NARROW_IN_EP_ADDR 0x82
1002154be65SJarod Wilson
1012154be65SJarod Wilson /* Size of the fixed-length portion of the signal */
102a48c4bbdSSean Young #define RR3_DRIVER_MAXLENS 255
1032154be65SJarod Wilson #define RR3_MAX_SIG_SIZE 512
1042154be65SJarod Wilson #define RR3_TIME_UNIT 50
1052154be65SJarod Wilson #define RR3_END_OF_SIGNAL 0x7f
1062154be65SJarod Wilson #define RR3_TX_TRAILER_LEN 2
1072154be65SJarod Wilson #define RR3_RX_MIN_TIMEOUT 5
1082154be65SJarod Wilson #define RR3_RX_MAX_TIMEOUT 2000
1092154be65SJarod Wilson
1102154be65SJarod Wilson /* The 8051's CPUCS Register address */
1112154be65SJarod Wilson #define RR3_CPUCS_REG_ADDR 0x7f92
1122154be65SJarod Wilson
1132154be65SJarod Wilson #define USB_RR3USB_VENDOR_ID 0x112a
1142154be65SJarod Wilson #define USB_RR3USB_PRODUCT_ID 0x0001
1152154be65SJarod Wilson #define USB_RR3IIUSB_PRODUCT_ID 0x0005
1162154be65SJarod Wilson
117d6ae162bSSean Young
118d6ae162bSSean Young /*
119d6ae162bSSean Young * The redrat3 encodes an IR signal as set of different lengths and a set
120d6ae162bSSean Young * of indices into those lengths. This sets how much two lengths must
121d6ae162bSSean Young * differ before they are considered distinct, the value is specified
122d6ae162bSSean Young * in microseconds.
123d6ae162bSSean Young * Default 5, value 0 to 127.
124d6ae162bSSean Young */
125d6ae162bSSean Young static int length_fuzz = 5;
126d6ae162bSSean Young module_param(length_fuzz, uint, 0644);
127d6ae162bSSean Young MODULE_PARM_DESC(length_fuzz, "Length Fuzz (0-127)");
128d6ae162bSSean Young
129d6ae162bSSean Young /*
130d6ae162bSSean Young * When receiving a continuous ir stream (for example when a user is
131d6ae162bSSean Young * holding a button down on a remote), this specifies the minimum size
132d6ae162bSSean Young * of a space when the redrat3 sends a irdata packet to the host. Specified
13304ad3011SMauro Carvalho Chehab * in milliseconds. Default value 18ms.
134d6ae162bSSean Young * The value can be between 2 and 30 inclusive.
135d6ae162bSSean Young */
136d6ae162bSSean Young static int minimum_pause = 18;
137d6ae162bSSean Young module_param(minimum_pause, uint, 0644);
138d6ae162bSSean Young MODULE_PARM_DESC(minimum_pause, "Minimum Pause in ms (2-30)");
139d6ae162bSSean Young
140d6ae162bSSean Young /*
141d6ae162bSSean Young * The carrier frequency is measured during the first pulse of the IR
142d6ae162bSSean Young * signal. The larger the number of periods used To measure, the more
143d6ae162bSSean Young * accurate the result is likely to be, however some signals have short
144d6ae162bSSean Young * initial pulses, so in some case it may be necessary to reduce this value.
145d6ae162bSSean Young * Default 8, value 1 to 255.
146d6ae162bSSean Young */
147d6ae162bSSean Young static int periods_measure_carrier = 8;
148d6ae162bSSean Young module_param(periods_measure_carrier, uint, 0644);
149d6ae162bSSean Young MODULE_PARM_DESC(periods_measure_carrier, "Number of Periods to Measure Carrier (1-255)");
150d6ae162bSSean Young
151d6ae162bSSean Young
1524c055a5aSSean Young struct redrat3_header {
1534c055a5aSSean Young __be16 length;
1544c055a5aSSean Young __be16 transfer_type;
1554c055a5aSSean Young } __packed;
1564c055a5aSSean Young
1574c055a5aSSean Young /* sending and receiving irdata */
1584c055a5aSSean Young struct redrat3_irdata {
1594c055a5aSSean Young struct redrat3_header header;
1604c055a5aSSean Young __be32 pause;
1614c055a5aSSean Young __be16 mod_freq_count;
1624c055a5aSSean Young __be16 num_periods;
1634c055a5aSSean Young __u8 max_lengths;
1644c055a5aSSean Young __u8 no_lengths;
1654c055a5aSSean Young __be16 max_sig_size;
1664c055a5aSSean Young __be16 sig_size;
1674c055a5aSSean Young __u8 no_repeats;
1684c055a5aSSean Young __be16 lens[RR3_DRIVER_MAXLENS]; /* not aligned */
1694c055a5aSSean Young __u8 sigdata[RR3_MAX_SIG_SIZE];
1704c055a5aSSean Young } __packed;
1714c055a5aSSean Young
1724c055a5aSSean Young /* firmware errors */
1734c055a5aSSean Young struct redrat3_error {
1744c055a5aSSean Young struct redrat3_header header;
1754c055a5aSSean Young __be16 fw_error;
1764c055a5aSSean Young } __packed;
1774c055a5aSSean Young
1782154be65SJarod Wilson /* table of devices that work with this driver */
1795fad16b5SArvind Yadav static const struct usb_device_id redrat3_dev_table[] = {
1802154be65SJarod Wilson /* Original version of the RedRat3 */
1812154be65SJarod Wilson {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)},
1822154be65SJarod Wilson /* Second Version/release of the RedRat3 - RetRat3-II */
1832154be65SJarod Wilson {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)},
1842154be65SJarod Wilson {} /* Terminating entry */
1852154be65SJarod Wilson };
1862154be65SJarod Wilson
1872154be65SJarod Wilson /* Structure to hold all of our device specific stuff */
1882154be65SJarod Wilson struct redrat3_dev {
1892154be65SJarod Wilson /* core device bits */
1902154be65SJarod Wilson struct rc_dev *rc;
1912154be65SJarod Wilson struct device *dev;
1922154be65SJarod Wilson
193bf139726SSean Young /* led control */
194bf139726SSean Young struct led_classdev led;
195bf139726SSean Young atomic_t flash;
196bf139726SSean Young struct usb_ctrlrequest flash_control;
197bf139726SSean Young struct urb *flash_urb;
198bf139726SSean Young u8 flash_in_buf;
199bf139726SSean Young
200c49fcddeSSean Young /* learning */
201c49fcddeSSean Young bool wideband;
202c49fcddeSSean Young struct usb_ctrlrequest learn_control;
203c49fcddeSSean Young struct urb *learn_urb;
204c49fcddeSSean Young u8 learn_buf;
205c49fcddeSSean Young
2062154be65SJarod Wilson /* save off the usb device pointer */
2072154be65SJarod Wilson struct usb_device *udev;
2082154be65SJarod Wilson
2092154be65SJarod Wilson /* the receive endpoint */
210c49fcddeSSean Young struct usb_endpoint_descriptor *ep_narrow;
2112154be65SJarod Wilson /* the buffer to receive data */
2124c055a5aSSean Young void *bulk_in_buf;
2132154be65SJarod Wilson /* urb used to read ir data */
214c49fcddeSSean Young struct urb *narrow_urb;
215c49fcddeSSean Young struct urb *wide_urb;
2162154be65SJarod Wilson
2172154be65SJarod Wilson /* the send endpoint */
2182154be65SJarod Wilson struct usb_endpoint_descriptor *ep_out;
2192154be65SJarod Wilson
2202154be65SJarod Wilson /* usb dma */
2212154be65SJarod Wilson dma_addr_t dma_in;
2222154be65SJarod Wilson
2232154be65SJarod Wilson /* Is the device currently transmitting?*/
2242154be65SJarod Wilson bool transmitting;
2252154be65SJarod Wilson
2262154be65SJarod Wilson /* store for current packet */
2274c055a5aSSean Young struct redrat3_irdata irdata;
2282154be65SJarod Wilson u16 bytes_read;
2292154be65SJarod Wilson
2302154be65SJarod Wilson u32 carrier;
2312154be65SJarod Wilson
2324c055a5aSSean Young char name[64];
2332154be65SJarod Wilson char phys[64];
2342154be65SJarod Wilson };
2352154be65SJarod Wilson
redrat3_dump_fw_error(struct redrat3_dev * rr3,int code)2362154be65SJarod Wilson static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
2372154be65SJarod Wilson {
2382154be65SJarod Wilson if (!rr3->transmitting && (code != 0x40))
2392154be65SJarod Wilson dev_info(rr3->dev, "fw error code 0x%02x: ", code);
2402154be65SJarod Wilson
2412154be65SJarod Wilson switch (code) {
2422154be65SJarod Wilson case 0x00:
2432154be65SJarod Wilson pr_cont("No Error\n");
2442154be65SJarod Wilson break;
2452154be65SJarod Wilson
2462154be65SJarod Wilson /* Codes 0x20 through 0x2f are IR Firmware Errors */
2472154be65SJarod Wilson case 0x20:
24825ec587cSMauro Carvalho Chehab pr_cont("Initial signal pulse not long enough to measure carrier frequency\n");
2492154be65SJarod Wilson break;
2502154be65SJarod Wilson case 0x21:
2512154be65SJarod Wilson pr_cont("Not enough length values allocated for signal\n");
2522154be65SJarod Wilson break;
2532154be65SJarod Wilson case 0x22:
2542154be65SJarod Wilson pr_cont("Not enough memory allocated for signal data\n");
2552154be65SJarod Wilson break;
2562154be65SJarod Wilson case 0x23:
2572154be65SJarod Wilson pr_cont("Too many signal repeats\n");
2582154be65SJarod Wilson break;
2592154be65SJarod Wilson case 0x28:
26025ec587cSMauro Carvalho Chehab pr_cont("Insufficient memory available for IR signal data memory allocation\n");
2612154be65SJarod Wilson break;
2622154be65SJarod Wilson case 0x29:
26325ec587cSMauro Carvalho Chehab pr_cont("Insufficient memory available for IrDa signal data memory allocation\n");
2642154be65SJarod Wilson break;
2652154be65SJarod Wilson
2662154be65SJarod Wilson /* Codes 0x30 through 0x3f are USB Firmware Errors */
2672154be65SJarod Wilson case 0x30:
26825ec587cSMauro Carvalho Chehab pr_cont("Insufficient memory available for bulk transfer structure\n");
2692154be65SJarod Wilson break;
2702154be65SJarod Wilson
2712154be65SJarod Wilson /*
2722154be65SJarod Wilson * Other error codes... These are primarily errors that can occur in
2732154be65SJarod Wilson * the control messages sent to the redrat
2742154be65SJarod Wilson */
2752154be65SJarod Wilson case 0x40:
2762154be65SJarod Wilson if (!rr3->transmitting)
2772154be65SJarod Wilson pr_cont("Signal capture has been terminated\n");
2782154be65SJarod Wilson break;
2792154be65SJarod Wilson case 0x41:
28025ec587cSMauro Carvalho Chehab pr_cont("Attempt to set/get and unknown signal I/O algorithm parameter\n");
2812154be65SJarod Wilson break;
2822154be65SJarod Wilson case 0x42:
2832154be65SJarod Wilson pr_cont("Signal capture already started\n");
2842154be65SJarod Wilson break;
2852154be65SJarod Wilson
2862154be65SJarod Wilson default:
2872154be65SJarod Wilson pr_cont("Unknown Error\n");
2882154be65SJarod Wilson break;
2892154be65SJarod Wilson }
2902154be65SJarod Wilson }
2912154be65SJarod Wilson
redrat3_val_to_mod_freq(struct redrat3_irdata * irdata)2924c055a5aSSean Young static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata)
2932154be65SJarod Wilson {
2942154be65SJarod Wilson u32 mod_freq = 0;
2954c055a5aSSean Young u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count);
2962154be65SJarod Wilson
2974c055a5aSSean Young if (mod_freq_count != 0)
2984c055a5aSSean Young mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) /
2994c055a5aSSean Young (mod_freq_count * RR3_CLK_PER_COUNT);
3002154be65SJarod Wilson
3012154be65SJarod Wilson return mod_freq;
3022154be65SJarod Wilson }
3032154be65SJarod Wilson
3042154be65SJarod Wilson /* this function scales down the figures for the same result... */
redrat3_len_to_us(u32 length)3052154be65SJarod Wilson static u32 redrat3_len_to_us(u32 length)
3062154be65SJarod Wilson {
3072154be65SJarod Wilson u32 biglen = length * 1000;
3082154be65SJarod Wilson u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000;
3092154be65SJarod Wilson u32 result = (u32) (biglen / divisor);
3102154be65SJarod Wilson
3112154be65SJarod Wilson /* don't allow zero lengths to go back, breaks lirc */
3122154be65SJarod Wilson return result ? result : 1;
3132154be65SJarod Wilson }
3142154be65SJarod Wilson
3152154be65SJarod Wilson /*
3162154be65SJarod Wilson * convert us back into redrat3 lengths
3172154be65SJarod Wilson *
3182154be65SJarod Wilson * length * 1000 length * 1000000
3192154be65SJarod Wilson * ------------- = ---------------- = micro
3202154be65SJarod Wilson * rr3clk / 1000 rr3clk
3212154be65SJarod Wilson
3222154be65SJarod Wilson * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
3232154be65SJarod Wilson * ----- = 4 ----- = 6 -------------- = len ---------------------
3242154be65SJarod Wilson * 3 2 1000000 1000
3252154be65SJarod Wilson */
redrat3_us_to_len(u32 microsec)3262154be65SJarod Wilson static u32 redrat3_us_to_len(u32 microsec)
3272154be65SJarod Wilson {
3282154be65SJarod Wilson u32 result;
3292154be65SJarod Wilson u32 divisor;
3302154be65SJarod Wilson
33195cf60aaSMauro Carvalho Chehab microsec = (microsec > IR_MAX_DURATION) ? IR_MAX_DURATION : microsec;
3322154be65SJarod Wilson divisor = (RR3_CLK_CONV_FACTOR / 1000);
3332154be65SJarod Wilson result = (u32)(microsec * divisor) / 1000;
3342154be65SJarod Wilson
3352154be65SJarod Wilson /* don't allow zero lengths to go back, breaks lirc */
3362154be65SJarod Wilson return result ? result : 1;
3372154be65SJarod Wilson }
3382154be65SJarod Wilson
redrat3_process_ir_data(struct redrat3_dev * rr3)3392154be65SJarod Wilson static void redrat3_process_ir_data(struct redrat3_dev *rr3)
3402154be65SJarod Wilson {
341183e19f5SSean Young struct ir_raw_event rawir = {};
3422154be65SJarod Wilson struct device *dev;
343528222d8SSean Young unsigned int i, sig_size, offset, val;
3444c055a5aSSean Young u32 mod_freq;
3452154be65SJarod Wilson
3462154be65SJarod Wilson dev = rr3->dev;
3472154be65SJarod Wilson
3484c055a5aSSean Young mod_freq = redrat3_val_to_mod_freq(&rr3->irdata);
3493228bf81SGreg Kroah-Hartman dev_dbg(dev, "Got mod_freq of %u\n", mod_freq);
350c49fcddeSSean Young if (mod_freq && rr3->wideband) {
351183e19f5SSean Young struct ir_raw_event ev = {
352183e19f5SSean Young .carrier_report = 1,
353183e19f5SSean Young .carrier = mod_freq
354183e19f5SSean Young };
355c49fcddeSSean Young
356c49fcddeSSean Young ir_raw_event_store(rr3->rc, &ev);
357c49fcddeSSean Young }
3582154be65SJarod Wilson
3592154be65SJarod Wilson /* process each rr3 encoded byte into an int */
3604c055a5aSSean Young sig_size = be16_to_cpu(rr3->irdata.sig_size);
3614c055a5aSSean Young for (i = 0; i < sig_size; i++) {
3624c055a5aSSean Young offset = rr3->irdata.sigdata[i];
3634c055a5aSSean Young val = get_unaligned_be16(&rr3->irdata.lens[offset]);
3642154be65SJarod Wilson
3652154be65SJarod Wilson /* we should always get pulse/space/pulse/space samples */
3662154be65SJarod Wilson if (i % 2)
3672154be65SJarod Wilson rawir.pulse = false;
3682154be65SJarod Wilson else
3692154be65SJarod Wilson rawir.pulse = true;
3702154be65SJarod Wilson
371528222d8SSean Young rawir.duration = redrat3_len_to_us(val);
3722c594ffaSJarod Wilson /* cap the value to IR_MAX_DURATION */
37395cf60aaSMauro Carvalho Chehab rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
37495cf60aaSMauro Carvalho Chehab IR_MAX_DURATION : rawir.duration;
3752c594ffaSJarod Wilson
3763228bf81SGreg Kroah-Hartman dev_dbg(dev, "storing %s with duration %d (i: %d)\n",
3772154be65SJarod Wilson rawir.pulse ? "pulse" : "space", rawir.duration, i);
3782154be65SJarod Wilson ir_raw_event_store_with_filter(rr3->rc, &rawir);
3792154be65SJarod Wilson }
3802154be65SJarod Wilson
38125da661aSSean Young /* add a trailing space */
3822154be65SJarod Wilson rawir.pulse = false;
38325da661aSSean Young rawir.timeout = true;
384c7470ff2SSean Young rawir.duration = rr3->rc->timeout;
38525da661aSSean Young dev_dbg(dev, "storing trailing timeout with duration %d\n",
3862154be65SJarod Wilson rawir.duration);
3872154be65SJarod Wilson ir_raw_event_store_with_filter(rr3->rc, &rawir);
3882154be65SJarod Wilson
3893228bf81SGreg Kroah-Hartman dev_dbg(dev, "calling ir_raw_event_handle\n");
3902154be65SJarod Wilson ir_raw_event_handle(rr3->rc);
3912154be65SJarod Wilson }
3922154be65SJarod Wilson
3932154be65SJarod Wilson /* Util fn to send rr3 cmds */
redrat3_send_cmd(int cmd,struct redrat3_dev * rr3)3946948524dSMauro Carvalho Chehab static int redrat3_send_cmd(int cmd, struct redrat3_dev *rr3)
3952154be65SJarod Wilson {
3962154be65SJarod Wilson struct usb_device *udev;
3972154be65SJarod Wilson u8 *data;
3982154be65SJarod Wilson int res;
3992154be65SJarod Wilson
4002154be65SJarod Wilson data = kzalloc(sizeof(u8), GFP_KERNEL);
4012154be65SJarod Wilson if (!data)
4022154be65SJarod Wilson return -ENOMEM;
4032154be65SJarod Wilson
4042154be65SJarod Wilson udev = rr3->udev;
4052154be65SJarod Wilson res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd,
4062154be65SJarod Wilson USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
4072adc965cSJohan Hovold 0x0000, 0x0000, data, sizeof(u8), 10000);
4082154be65SJarod Wilson
4092154be65SJarod Wilson if (res < 0) {
4102154be65SJarod Wilson dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d",
4112154be65SJarod Wilson __func__, res, *data);
4122154be65SJarod Wilson res = -EIO;
4132154be65SJarod Wilson } else
4144c055a5aSSean Young res = data[0];
4152154be65SJarod Wilson
4162154be65SJarod Wilson kfree(data);
4172154be65SJarod Wilson
4182154be65SJarod Wilson return res;
4192154be65SJarod Wilson }
4202154be65SJarod Wilson
4212154be65SJarod Wilson /* Enables the long range detector and starts async receive */
redrat3_enable_detector(struct redrat3_dev * rr3)4222154be65SJarod Wilson static int redrat3_enable_detector(struct redrat3_dev *rr3)
4232154be65SJarod Wilson {
4242154be65SJarod Wilson struct device *dev = rr3->dev;
4252154be65SJarod Wilson u8 ret;
4262154be65SJarod Wilson
4272154be65SJarod Wilson ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
4282154be65SJarod Wilson if (ret != 0)
4292154be65SJarod Wilson dev_dbg(dev, "%s: unexpected ret of %d\n",
4302154be65SJarod Wilson __func__, ret);
4312154be65SJarod Wilson
4322154be65SJarod Wilson ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
4332154be65SJarod Wilson if (ret != 1) {
4342154be65SJarod Wilson dev_err(dev, "%s: detector status: %d, should be 1\n",
4352154be65SJarod Wilson __func__, ret);
4362154be65SJarod Wilson return -EIO;
4372154be65SJarod Wilson }
4382154be65SJarod Wilson
439c49fcddeSSean Young ret = usb_submit_urb(rr3->narrow_urb, GFP_KERNEL);
440c49fcddeSSean Young if (ret) {
441c49fcddeSSean Young dev_err(rr3->dev, "narrow band urb failed: %d", ret);
442c49fcddeSSean Young return ret;
443c49fcddeSSean Young }
4442154be65SJarod Wilson
445c49fcddeSSean Young ret = usb_submit_urb(rr3->wide_urb, GFP_KERNEL);
446c49fcddeSSean Young if (ret)
447c49fcddeSSean Young dev_err(rr3->dev, "wide band urb failed: %d", ret);
448c49fcddeSSean Young
449c49fcddeSSean Young return ret;
4502154be65SJarod Wilson }
4512154be65SJarod Wilson
redrat3_delete(struct redrat3_dev * rr3,struct usb_device * udev)4522154be65SJarod Wilson static inline void redrat3_delete(struct redrat3_dev *rr3,
4532154be65SJarod Wilson struct usb_device *udev)
4542154be65SJarod Wilson {
455c49fcddeSSean Young usb_kill_urb(rr3->narrow_urb);
456c49fcddeSSean Young usb_kill_urb(rr3->wide_urb);
457bf139726SSean Young usb_kill_urb(rr3->flash_urb);
458c49fcddeSSean Young usb_kill_urb(rr3->learn_urb);
459c49fcddeSSean Young usb_free_urb(rr3->narrow_urb);
460c49fcddeSSean Young usb_free_urb(rr3->wide_urb);
461bf139726SSean Young usb_free_urb(rr3->flash_urb);
462c49fcddeSSean Young usb_free_urb(rr3->learn_urb);
463c49fcddeSSean Young usb_free_coherent(udev, le16_to_cpu(rr3->ep_narrow->wMaxPacketSize),
4642154be65SJarod Wilson rr3->bulk_in_buf, rr3->dma_in);
4652154be65SJarod Wilson
4662154be65SJarod Wilson kfree(rr3);
4672154be65SJarod Wilson }
4682154be65SJarod Wilson
redrat3_get_timeout(struct redrat3_dev * rr3)469c53f9f00SJarod Wilson static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
4702154be65SJarod Wilson {
471801b69f2SSean Young __be32 *tmp;
472c53f9f00SJarod Wilson u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
4732154be65SJarod Wilson int len, ret, pipe;
4742154be65SJarod Wilson
4752154be65SJarod Wilson len = sizeof(*tmp);
4762154be65SJarod Wilson tmp = kzalloc(len, GFP_KERNEL);
477559f64d4SMarkus Elfring if (!tmp)
4782154be65SJarod Wilson return timeout;
4792154be65SJarod Wilson
480c53f9f00SJarod Wilson pipe = usb_rcvctrlpipe(rr3->udev, 0);
481c53f9f00SJarod Wilson ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
4822154be65SJarod Wilson USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
4832adc965cSJohan Hovold RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, 5000);
484801b69f2SSean Young if (ret != len)
485c53f9f00SJarod Wilson dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
486801b69f2SSean Young else {
487801b69f2SSean Young timeout = redrat3_len_to_us(be32_to_cpup(tmp));
4882154be65SJarod Wilson
4893228bf81SGreg Kroah-Hartman dev_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
490801b69f2SSean Young }
491801b69f2SSean Young
492801b69f2SSean Young kfree(tmp);
493801b69f2SSean Young
4942154be65SJarod Wilson return timeout;
4952154be65SJarod Wilson }
4962154be65SJarod Wilson
redrat3_set_timeout(struct rc_dev * rc_dev,unsigned int timeoutus)497528222d8SSean Young static int redrat3_set_timeout(struct rc_dev *rc_dev, unsigned int timeoutus)
4984f253cecSSean Young {
4994f253cecSSean Young struct redrat3_dev *rr3 = rc_dev->priv;
5004f253cecSSean Young struct usb_device *udev = rr3->udev;
5014f253cecSSean Young struct device *dev = rr3->dev;
502b1cb50beSHans Verkuil __be32 *timeout;
5034f253cecSSean Young int ret;
5044f253cecSSean Young
5054f253cecSSean Young timeout = kmalloc(sizeof(*timeout), GFP_KERNEL);
5064f253cecSSean Young if (!timeout)
5074f253cecSSean Young return -ENOMEM;
5084f253cecSSean Young
509528222d8SSean Young *timeout = cpu_to_be32(redrat3_us_to_len(timeoutus));
5104f253cecSSean Young ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), RR3_SET_IR_PARAM,
5114f253cecSSean Young USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
5124f253cecSSean Young RR3_IR_IO_SIG_TIMEOUT, 0, timeout, sizeof(*timeout),
5132adc965cSJohan Hovold 25000);
5144f253cecSSean Young dev_dbg(dev, "set ir parm timeout %d ret 0x%02x\n",
5154f253cecSSean Young be32_to_cpu(*timeout), ret);
5164f253cecSSean Young
517c7470ff2SSean Young if (ret == sizeof(*timeout))
5184f253cecSSean Young ret = 0;
519c7470ff2SSean Young else if (ret >= 0)
5204f253cecSSean Young ret = -EIO;
5214f253cecSSean Young
5224f253cecSSean Young kfree(timeout);
5234f253cecSSean Young
5244f253cecSSean Young return ret;
5254f253cecSSean Young }
5264f253cecSSean Young
redrat3_reset(struct redrat3_dev * rr3)5272154be65SJarod Wilson static void redrat3_reset(struct redrat3_dev *rr3)
5282154be65SJarod Wilson {
5292154be65SJarod Wilson struct usb_device *udev = rr3->udev;
5302154be65SJarod Wilson struct device *dev = rr3->dev;
5312154be65SJarod Wilson int rc, rxpipe, txpipe;
5322154be65SJarod Wilson u8 *val;
533576df632SMarkus Elfring size_t const len = sizeof(*val);
5342154be65SJarod Wilson
5352154be65SJarod Wilson rxpipe = usb_rcvctrlpipe(udev, 0);
5362154be65SJarod Wilson txpipe = usb_sndctrlpipe(udev, 0);
5372154be65SJarod Wilson
538fa7b9ac2SSean Young val = kmalloc(len, GFP_KERNEL);
539559f64d4SMarkus Elfring if (!val)
5402154be65SJarod Wilson return;
5412154be65SJarod Wilson
5422154be65SJarod Wilson *val = 0x01;
5432154be65SJarod Wilson rc = usb_control_msg(udev, rxpipe, RR3_RESET,
5442154be65SJarod Wilson USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
5452adc965cSJohan Hovold RR3_CPUCS_REG_ADDR, 0, val, len, 25000);
5463228bf81SGreg Kroah-Hartman dev_dbg(dev, "reset returned 0x%02x\n", rc);
5472154be65SJarod Wilson
548d6ae162bSSean Young *val = length_fuzz;
5492154be65SJarod Wilson rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
5502154be65SJarod Wilson USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
5512adc965cSJohan Hovold RR3_IR_IO_LENGTH_FUZZ, 0, val, len, 25000);
5523228bf81SGreg Kroah-Hartman dev_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
5532154be65SJarod Wilson
554d6ae162bSSean Young *val = (65536 - (minimum_pause * 2000)) / 256;
555d6ae162bSSean Young rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
556d6ae162bSSean Young USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
5572adc965cSJohan Hovold RR3_IR_IO_MIN_PAUSE, 0, val, len, 25000);
558d6ae162bSSean Young dev_dbg(dev, "set ir parm min pause %d rc 0x%02x\n", *val, rc);
559d6ae162bSSean Young
560d6ae162bSSean Young *val = periods_measure_carrier;
561d6ae162bSSean Young rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
562d6ae162bSSean Young USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
5632adc965cSJohan Hovold RR3_IR_IO_PERIODS_MF, 0, val, len, 25000);
564d6ae162bSSean Young dev_dbg(dev, "set ir parm periods measure carrier %d rc 0x%02x", *val,
565d6ae162bSSean Young rc);
566d6ae162bSSean Young
5672154be65SJarod Wilson *val = RR3_DRIVER_MAXLENS;
5682154be65SJarod Wilson rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
5692154be65SJarod Wilson USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
5702adc965cSJohan Hovold RR3_IR_IO_MAX_LENGTHS, 0, val, len, 25000);
5713228bf81SGreg Kroah-Hartman dev_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
5722154be65SJarod Wilson
5732154be65SJarod Wilson kfree(val);
5742154be65SJarod Wilson }
5752154be65SJarod Wilson
redrat3_get_firmware_rev(struct redrat3_dev * rr3)5762154be65SJarod Wilson static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
5772154be65SJarod Wilson {
5787aa20afeSMarkus Elfring int rc;
5792154be65SJarod Wilson char *buffer;
5802154be65SJarod Wilson
5810bebaa5fSMarkus Elfring buffer = kcalloc(RR3_FW_VERSION_LEN + 1, sizeof(*buffer), GFP_KERNEL);
582559f64d4SMarkus Elfring if (!buffer)
5832154be65SJarod Wilson return;
5842154be65SJarod Wilson
5852154be65SJarod Wilson rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
5862154be65SJarod Wilson RR3_FW_VERSION,
5872154be65SJarod Wilson USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
5882adc965cSJohan Hovold 0, 0, buffer, RR3_FW_VERSION_LEN, 5000);
5892154be65SJarod Wilson
5902154be65SJarod Wilson if (rc >= 0)
5912154be65SJarod Wilson dev_info(rr3->dev, "Firmware rev: %s", buffer);
5922154be65SJarod Wilson else
5932154be65SJarod Wilson dev_err(rr3->dev, "Problem fetching firmware ID\n");
5942154be65SJarod Wilson
5952154be65SJarod Wilson kfree(buffer);
5962154be65SJarod Wilson }
5972154be65SJarod Wilson
redrat3_read_packet_start(struct redrat3_dev * rr3,unsigned len)598fa7b9ac2SSean Young static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
5992154be65SJarod Wilson {
6004c055a5aSSean Young struct redrat3_header *header = rr3->bulk_in_buf;
6014c055a5aSSean Young unsigned pktlen, pkttype;
6022154be65SJarod Wilson
6032154be65SJarod Wilson /* grab the Length and type of transfer */
6044c055a5aSSean Young pktlen = be16_to_cpu(header->length);
6054c055a5aSSean Young pkttype = be16_to_cpu(header->transfer_type);
6062154be65SJarod Wilson
6074c055a5aSSean Young if (pktlen > sizeof(rr3->irdata)) {
6084c055a5aSSean Young dev_warn(rr3->dev, "packet length %u too large\n", pktlen);
6094c055a5aSSean Young return;
6104c055a5aSSean Young }
6112154be65SJarod Wilson
6124c055a5aSSean Young switch (pkttype) {
6132154be65SJarod Wilson case RR3_ERROR:
6144c055a5aSSean Young if (len >= sizeof(struct redrat3_error)) {
6154c055a5aSSean Young struct redrat3_error *error = rr3->bulk_in_buf;
6164c055a5aSSean Young unsigned fw_error = be16_to_cpu(error->fw_error);
6174c055a5aSSean Young redrat3_dump_fw_error(rr3, fw_error);
6184c055a5aSSean Young }
6192154be65SJarod Wilson break;
6202154be65SJarod Wilson
6212154be65SJarod Wilson case RR3_MOD_SIGNAL_IN:
6224c055a5aSSean Young memcpy(&rr3->irdata, rr3->bulk_in_buf, len);
6232154be65SJarod Wilson rr3->bytes_read = len;
6243228bf81SGreg Kroah-Hartman dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
6254c055a5aSSean Young rr3->bytes_read, pktlen);
6262154be65SJarod Wilson break;
6272154be65SJarod Wilson
6282154be65SJarod Wilson default:
6293228bf81SGreg Kroah-Hartman dev_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
6304c055a5aSSean Young pkttype, len, pktlen);
6312154be65SJarod Wilson break;
6322154be65SJarod Wilson }
6332154be65SJarod Wilson }
6342154be65SJarod Wilson
redrat3_read_packet_continue(struct redrat3_dev * rr3,unsigned len)635fa7b9ac2SSean Young static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len)
6362154be65SJarod Wilson {
6374c055a5aSSean Young void *irdata = &rr3->irdata;
6384c055a5aSSean Young
6394c055a5aSSean Young if (len + rr3->bytes_read > sizeof(rr3->irdata)) {
6404c055a5aSSean Young dev_warn(rr3->dev, "too much data for packet\n");
6414c055a5aSSean Young rr3->bytes_read = 0;
6424c055a5aSSean Young return;
6434c055a5aSSean Young }
6444c055a5aSSean Young
6454c055a5aSSean Young memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len);
6462154be65SJarod Wilson
6472154be65SJarod Wilson rr3->bytes_read += len;
6483228bf81SGreg Kroah-Hartman dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read,
6494c055a5aSSean Young be16_to_cpu(rr3->irdata.header.length));
6502154be65SJarod Wilson }
6512154be65SJarod Wilson
6522154be65SJarod Wilson /* gather IR data from incoming urb, process it when we have enough */
redrat3_get_ir_data(struct redrat3_dev * rr3,unsigned len)653fa7b9ac2SSean Young static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len)
6542154be65SJarod Wilson {
6552154be65SJarod Wilson struct device *dev = rr3->dev;
6564c055a5aSSean Young unsigned pkttype;
6572154be65SJarod Wilson int ret = 0;
6582154be65SJarod Wilson
6594c055a5aSSean Young if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) {
6602154be65SJarod Wilson redrat3_read_packet_start(rr3, len);
6612154be65SJarod Wilson } else if (rr3->bytes_read != 0) {
6622154be65SJarod Wilson redrat3_read_packet_continue(rr3, len);
6632154be65SJarod Wilson } else if (rr3->bytes_read == 0) {
6642154be65SJarod Wilson dev_err(dev, "error: no packet data read\n");
6652154be65SJarod Wilson ret = -ENODATA;
6662154be65SJarod Wilson goto out;
6672154be65SJarod Wilson }
6682154be65SJarod Wilson
66938e35a85SSean Young if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length) +
67038e35a85SSean Young sizeof(struct redrat3_header))
6712154be65SJarod Wilson /* we're still accumulating data */
6722154be65SJarod Wilson return 0;
6732154be65SJarod Wilson
6742154be65SJarod Wilson /* if we get here, we've got IR data to decode */
6754c055a5aSSean Young pkttype = be16_to_cpu(rr3->irdata.header.transfer_type);
6764c055a5aSSean Young if (pkttype == RR3_MOD_SIGNAL_IN)
6772154be65SJarod Wilson redrat3_process_ir_data(rr3);
6782154be65SJarod Wilson else
6793228bf81SGreg Kroah-Hartman dev_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n",
6804c055a5aSSean Young pkttype);
6812154be65SJarod Wilson
6822154be65SJarod Wilson out:
6832154be65SJarod Wilson rr3->bytes_read = 0;
6842154be65SJarod Wilson return ret;
6852154be65SJarod Wilson }
6862154be65SJarod Wilson
6872154be65SJarod Wilson /* callback function from USB when async USB request has completed */
redrat3_handle_async(struct urb * urb)688801b69f2SSean Young static void redrat3_handle_async(struct urb *urb)
6892154be65SJarod Wilson {
690d6aca6eaSSean Young struct redrat3_dev *rr3 = urb->context;
691dbea1880SAndrew Vincer int ret;
6922154be65SJarod Wilson
6932154be65SJarod Wilson switch (urb->status) {
6942154be65SJarod Wilson case 0:
695dbea1880SAndrew Vincer ret = redrat3_get_ir_data(rr3, urb->actual_length);
696c49fcddeSSean Young if (!ret && rr3->wideband && !rr3->learn_urb->hcpriv) {
697c49fcddeSSean Young ret = usb_submit_urb(rr3->learn_urb, GFP_ATOMIC);
698c49fcddeSSean Young if (ret)
699c49fcddeSSean Young dev_err(rr3->dev, "Failed to submit learning urb: %d",
700c49fcddeSSean Young ret);
701c49fcddeSSean Young }
702c49fcddeSSean Young
703dbea1880SAndrew Vincer if (!ret) {
704dbea1880SAndrew Vincer /* no error, prepare to read more */
705c49fcddeSSean Young ret = usb_submit_urb(urb, GFP_ATOMIC);
706c49fcddeSSean Young if (ret)
707c49fcddeSSean Young dev_err(rr3->dev, "Failed to resubmit urb: %d",
708c49fcddeSSean Young ret);
709dbea1880SAndrew Vincer }
7102154be65SJarod Wilson break;
7112154be65SJarod Wilson
7122154be65SJarod Wilson case -ECONNRESET:
7132154be65SJarod Wilson case -ENOENT:
7142154be65SJarod Wilson case -ESHUTDOWN:
7152154be65SJarod Wilson usb_unlink_urb(urb);
7162154be65SJarod Wilson return;
7172154be65SJarod Wilson
7182154be65SJarod Wilson case -EPIPE:
7192154be65SJarod Wilson default:
7202154be65SJarod Wilson dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
7212154be65SJarod Wilson rr3->bytes_read = 0;
7222154be65SJarod Wilson break;
7232154be65SJarod Wilson }
7242154be65SJarod Wilson }
7252154be65SJarod Wilson
mod_freq_to_val(unsigned int mod_freq)7262154be65SJarod Wilson static u16 mod_freq_to_val(unsigned int mod_freq)
7272154be65SJarod Wilson {
7282154be65SJarod Wilson int mult = 6000000;
7292154be65SJarod Wilson
7302154be65SJarod Wilson /* Clk used in mod. freq. generation is CLK24/4. */
7314c055a5aSSean Young return 65536 - (mult / mod_freq);
7322154be65SJarod Wilson }
7332154be65SJarod Wilson
redrat3_set_tx_carrier(struct rc_dev * rcdev,u32 carrier)734dbea1880SAndrew Vincer static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
7352154be65SJarod Wilson {
736dbea1880SAndrew Vincer struct redrat3_dev *rr3 = rcdev->priv;
737dbea1880SAndrew Vincer struct device *dev = rr3->dev;
7382154be65SJarod Wilson
7393228bf81SGreg Kroah-Hartman dev_dbg(dev, "Setting modulation frequency to %u", carrier);
74048cafec9SDan Carpenter if (carrier == 0)
74148cafec9SDan Carpenter return -EINVAL;
74248cafec9SDan Carpenter
7432154be65SJarod Wilson rr3->carrier = carrier;
7442154be65SJarod Wilson
74514d8188aSSean Young return 0;
7462154be65SJarod Wilson }
7472154be65SJarod Wilson
redrat3_transmit_ir(struct rc_dev * rcdev,unsigned * txbuf,unsigned count)748dbea1880SAndrew Vincer static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
749dbea1880SAndrew Vincer unsigned count)
7502154be65SJarod Wilson {
7512154be65SJarod Wilson struct redrat3_dev *rr3 = rcdev->priv;
7522154be65SJarod Wilson struct device *dev = rr3->dev;
7534c055a5aSSean Young struct redrat3_irdata *irdata = NULL;
754fa7b9ac2SSean Young int ret, ret_len;
7552154be65SJarod Wilson int lencheck, cur_sample_len, pipe;
7562154be65SJarod Wilson int *sample_lens = NULL;
7572154be65SJarod Wilson u8 curlencheck = 0;
758fa7b9ac2SSean Young unsigned i, sendbuf_len;
7592154be65SJarod Wilson
7602154be65SJarod Wilson if (rr3->transmitting) {
7612154be65SJarod Wilson dev_warn(dev, "%s: transmitter already in use\n", __func__);
7622154be65SJarod Wilson return -EAGAIN;
7632154be65SJarod Wilson }
7642154be65SJarod Wilson
765671ea670SSean Young if (count > RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN)
766671ea670SSean Young return -EINVAL;
7672154be65SJarod Wilson
768dbea1880SAndrew Vincer /* rr3 will disable rc detector on transmit */
7692154be65SJarod Wilson rr3->transmitting = true;
7702154be65SJarod Wilson
7710bebaa5fSMarkus Elfring sample_lens = kcalloc(RR3_DRIVER_MAXLENS,
7720bebaa5fSMarkus Elfring sizeof(*sample_lens),
7730bebaa5fSMarkus Elfring GFP_KERNEL);
774fac59136SMarkus Elfring if (!sample_lens)
775fac59136SMarkus Elfring return -ENOMEM;
7762154be65SJarod Wilson
7774c055a5aSSean Young irdata = kzalloc(sizeof(*irdata), GFP_KERNEL);
7784c055a5aSSean Young if (!irdata) {
779801b69f2SSean Young ret = -ENOMEM;
780801b69f2SSean Young goto out;
781801b69f2SSean Young }
782801b69f2SSean Young
7832154be65SJarod Wilson for (i = 0; i < count; i++) {
7842154be65SJarod Wilson cur_sample_len = redrat3_us_to_len(txbuf[i]);
785801b69f2SSean Young if (cur_sample_len > 0xffff) {
786801b69f2SSean Young dev_warn(dev, "transmit period of %uus truncated to %uus\n",
787801b69f2SSean Young txbuf[i], redrat3_len_to_us(0xffff));
788801b69f2SSean Young cur_sample_len = 0xffff;
789801b69f2SSean Young }
79006eae25fSSean Young for (lencheck = 0; lencheck < curlencheck; lencheck++) {
7912154be65SJarod Wilson if (sample_lens[lencheck] == cur_sample_len)
7922154be65SJarod Wilson break;
7932154be65SJarod Wilson }
7942154be65SJarod Wilson if (lencheck == curlencheck) {
7953228bf81SGreg Kroah-Hartman dev_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
7962154be65SJarod Wilson i, txbuf[i], curlencheck, cur_sample_len);
79706eae25fSSean Young if (curlencheck < RR3_DRIVER_MAXLENS) {
7982154be65SJarod Wilson /* now convert the value to a proper
7992154be65SJarod Wilson * rr3 value.. */
8002154be65SJarod Wilson sample_lens[curlencheck] = cur_sample_len;
8014c055a5aSSean Young put_unaligned_be16(cur_sample_len,
8024c055a5aSSean Young &irdata->lens[curlencheck]);
8032154be65SJarod Wilson curlencheck++;
8042154be65SJarod Wilson } else {
805671ea670SSean Young ret = -EINVAL;
806671ea670SSean Young goto out;
8072154be65SJarod Wilson }
8082154be65SJarod Wilson }
8094c055a5aSSean Young irdata->sigdata[i] = lencheck;
8102154be65SJarod Wilson }
8112154be65SJarod Wilson
8124c055a5aSSean Young irdata->sigdata[count] = RR3_END_OF_SIGNAL;
8134c055a5aSSean Young irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL;
8142154be65SJarod Wilson
8154c055a5aSSean Young sendbuf_len = offsetof(struct redrat3_irdata,
8164c055a5aSSean Young sigdata[count + RR3_TX_TRAILER_LEN]);
8172154be65SJarod Wilson /* fill in our packet header */
8184c055a5aSSean Young irdata->header.length = cpu_to_be16(sendbuf_len -
8194c055a5aSSean Young sizeof(struct redrat3_header));
8204c055a5aSSean Young irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT);
8214c055a5aSSean Young irdata->pause = cpu_to_be32(redrat3_len_to_us(100));
8224c055a5aSSean Young irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier));
8234c055a5aSSean Young irdata->no_lengths = curlencheck;
8244c055a5aSSean Young irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN);
8252154be65SJarod Wilson
8262154be65SJarod Wilson pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
8274c055a5aSSean Young ret = usb_bulk_msg(rr3->udev, pipe, irdata,
8282adc965cSJohan Hovold sendbuf_len, &ret_len, 10000);
8293228bf81SGreg Kroah-Hartman dev_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret);
8302154be65SJarod Wilson
8312154be65SJarod Wilson /* now tell the hardware to transmit what we sent it */
8322154be65SJarod Wilson pipe = usb_rcvctrlpipe(rr3->udev, 0);
8332154be65SJarod Wilson ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
8342154be65SJarod Wilson USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
8352adc965cSJohan Hovold 0, 0, irdata, 2, 10000);
8362154be65SJarod Wilson
8372154be65SJarod Wilson if (ret < 0)
8382154be65SJarod Wilson dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
8392154be65SJarod Wilson else
840dbea1880SAndrew Vincer ret = count;
8412154be65SJarod Wilson
8422154be65SJarod Wilson out:
8434c055a5aSSean Young kfree(irdata);
844fac59136SMarkus Elfring kfree(sample_lens);
8452154be65SJarod Wilson
8462154be65SJarod Wilson rr3->transmitting = false;
847dbea1880SAndrew Vincer /* rr3 re-enables rc detector because it was enabled before */
8482154be65SJarod Wilson
8492154be65SJarod Wilson return ret;
8502154be65SJarod Wilson }
8512154be65SJarod Wilson
redrat3_brightness_set(struct led_classdev * led_dev,enum led_brightness brightness)852bf139726SSean Young static void redrat3_brightness_set(struct led_classdev *led_dev, enum
853bf139726SSean Young led_brightness brightness)
854bf139726SSean Young {
855bf139726SSean Young struct redrat3_dev *rr3 = container_of(led_dev, struct redrat3_dev,
856bf139726SSean Young led);
857bf139726SSean Young
858bf139726SSean Young if (brightness != LED_OFF && atomic_cmpxchg(&rr3->flash, 0, 1) == 0) {
859bf139726SSean Young int ret = usb_submit_urb(rr3->flash_urb, GFP_ATOMIC);
860bf139726SSean Young if (ret != 0) {
861bf139726SSean Young dev_dbg(rr3->dev, "%s: unexpected ret of %d\n",
862bf139726SSean Young __func__, ret);
863bf139726SSean Young atomic_set(&rr3->flash, 0);
864bf139726SSean Young }
865bf139726SSean Young }
866bf139726SSean Young }
867bf139726SSean Young
redrat3_wideband_receiver(struct rc_dev * rcdev,int enable)868c49fcddeSSean Young static int redrat3_wideband_receiver(struct rc_dev *rcdev, int enable)
869c49fcddeSSean Young {
870c49fcddeSSean Young struct redrat3_dev *rr3 = rcdev->priv;
871c49fcddeSSean Young int ret = 0;
872c49fcddeSSean Young
873c49fcddeSSean Young rr3->wideband = enable != 0;
874c49fcddeSSean Young
875c49fcddeSSean Young if (enable) {
876c49fcddeSSean Young ret = usb_submit_urb(rr3->learn_urb, GFP_KERNEL);
877c49fcddeSSean Young if (ret)
878c49fcddeSSean Young dev_err(rr3->dev, "Failed to submit learning urb: %d",
879c49fcddeSSean Young ret);
880c49fcddeSSean Young }
881c49fcddeSSean Young
882c49fcddeSSean Young return ret;
883c49fcddeSSean Young }
884c49fcddeSSean Young
redrat3_learn_complete(struct urb * urb)885c49fcddeSSean Young static void redrat3_learn_complete(struct urb *urb)
886c49fcddeSSean Young {
887c49fcddeSSean Young struct redrat3_dev *rr3 = urb->context;
888c49fcddeSSean Young
889c49fcddeSSean Young switch (urb->status) {
890c49fcddeSSean Young case 0:
891c49fcddeSSean Young break;
892c49fcddeSSean Young case -ECONNRESET:
893c49fcddeSSean Young case -ENOENT:
894c49fcddeSSean Young case -ESHUTDOWN:
895c49fcddeSSean Young usb_unlink_urb(urb);
896c49fcddeSSean Young return;
897c49fcddeSSean Young case -EPIPE:
898c49fcddeSSean Young default:
899c49fcddeSSean Young dev_err(rr3->dev, "Error: learn urb status = %d", urb->status);
900c49fcddeSSean Young break;
901c49fcddeSSean Young }
902c49fcddeSSean Young }
903c49fcddeSSean Young
redrat3_led_complete(struct urb * urb)904bf139726SSean Young static void redrat3_led_complete(struct urb *urb)
905bf139726SSean Young {
906bf139726SSean Young struct redrat3_dev *rr3 = urb->context;
907bf139726SSean Young
908bf139726SSean Young switch (urb->status) {
909bf139726SSean Young case 0:
910bf139726SSean Young break;
911bf139726SSean Young case -ECONNRESET:
912bf139726SSean Young case -ENOENT:
913bf139726SSean Young case -ESHUTDOWN:
914bf139726SSean Young usb_unlink_urb(urb);
915bf139726SSean Young return;
916bf139726SSean Young case -EPIPE:
917bf139726SSean Young default:
918bf139726SSean Young dev_dbg(rr3->dev, "Error: urb status = %d\n", urb->status);
919bf139726SSean Young break;
920bf139726SSean Young }
921bf139726SSean Young
922bf139726SSean Young rr3->led.brightness = LED_OFF;
923bf139726SSean Young atomic_dec(&rr3->flash);
924bf139726SSean Young }
925bf139726SSean Young
redrat3_init_rc_dev(struct redrat3_dev * rr3)9262154be65SJarod Wilson static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
9272154be65SJarod Wilson {
9282154be65SJarod Wilson struct device *dev = rr3->dev;
9292154be65SJarod Wilson struct rc_dev *rc;
9309d45d5fbSMarkus Elfring int ret;
9312154be65SJarod Wilson u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
9322154be65SJarod Wilson
9330f7499fdSAndi Shyti rc = rc_allocate_device(RC_DRIVER_IR_RAW);
934559f64d4SMarkus Elfring if (!rc)
9353f81678dSMarkus Elfring return NULL;
9362154be65SJarod Wilson
937da500033SSean Young snprintf(rr3->name, sizeof(rr3->name),
938da500033SSean Young "RedRat3%s Infrared Remote Transceiver",
939da500033SSean Young prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "");
9402154be65SJarod Wilson
9412154be65SJarod Wilson usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
9422154be65SJarod Wilson
943518f4b26SSean Young rc->device_name = rr3->name;
9442154be65SJarod Wilson rc->input_phys = rr3->phys;
9452154be65SJarod Wilson usb_to_input_id(rr3->udev, &rc->input_id);
9462154be65SJarod Wilson rc->dev.parent = dev;
9472154be65SJarod Wilson rc->priv = rr3;
9486d741bfeSSean Young rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
949528222d8SSean Young rc->min_timeout = MS_TO_US(RR3_RX_MIN_TIMEOUT);
950528222d8SSean Young rc->max_timeout = MS_TO_US(RR3_RX_MAX_TIMEOUT);
951528222d8SSean Young rc->timeout = redrat3_get_timeout(rr3);
9524f253cecSSean Young rc->s_timeout = redrat3_set_timeout;
9532154be65SJarod Wilson rc->tx_ir = redrat3_transmit_ir;
9542154be65SJarod Wilson rc->s_tx_carrier = redrat3_set_tx_carrier;
955c49fcddeSSean Young rc->s_carrier_report = redrat3_wideband_receiver;
9562154be65SJarod Wilson rc->driver_name = DRIVER_NAME;
957528222d8SSean Young rc->rx_resolution = 2;
9582154be65SJarod Wilson rc->map_name = RC_MAP_HAUPPAUGE;
9592154be65SJarod Wilson
9602154be65SJarod Wilson ret = rc_register_device(rc);
9612154be65SJarod Wilson if (ret < 0) {
9622154be65SJarod Wilson dev_err(dev, "remote dev registration failed\n");
9632154be65SJarod Wilson goto out;
9642154be65SJarod Wilson }
9652154be65SJarod Wilson
9662154be65SJarod Wilson return rc;
9672154be65SJarod Wilson
9682154be65SJarod Wilson out:
9692154be65SJarod Wilson rc_free_device(rc);
9702154be65SJarod Wilson return NULL;
9712154be65SJarod Wilson }
9722154be65SJarod Wilson
redrat3_dev_probe(struct usb_interface * intf,const struct usb_device_id * id)9734c62e976SGreg Kroah-Hartman static int redrat3_dev_probe(struct usb_interface *intf,
9742154be65SJarod Wilson const struct usb_device_id *id)
9752154be65SJarod Wilson {
9762154be65SJarod Wilson struct usb_device *udev = interface_to_usbdev(intf);
9772154be65SJarod Wilson struct device *dev = &intf->dev;
9782154be65SJarod Wilson struct usb_host_interface *uhi;
9792154be65SJarod Wilson struct redrat3_dev *rr3;
9802154be65SJarod Wilson struct usb_endpoint_descriptor *ep;
981c49fcddeSSean Young struct usb_endpoint_descriptor *ep_narrow = NULL;
982c49fcddeSSean Young struct usb_endpoint_descriptor *ep_wide = NULL;
9832154be65SJarod Wilson struct usb_endpoint_descriptor *ep_out = NULL;
9842154be65SJarod Wilson u8 addr, attrs;
9852154be65SJarod Wilson int pipe, i;
9862154be65SJarod Wilson int retval = -ENOMEM;
9872154be65SJarod Wilson
9882154be65SJarod Wilson uhi = intf->cur_altsetting;
9892154be65SJarod Wilson
9902154be65SJarod Wilson /* find our bulk-in and bulk-out endpoints */
9912154be65SJarod Wilson for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
9922154be65SJarod Wilson ep = &uhi->endpoint[i].desc;
9932154be65SJarod Wilson addr = ep->bEndpointAddress;
9942154be65SJarod Wilson attrs = ep->bmAttributes;
9952154be65SJarod Wilson
996c49fcddeSSean Young if (((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
9972154be65SJarod Wilson ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
9982154be65SJarod Wilson USB_ENDPOINT_XFER_BULK)) {
9993228bf81SGreg Kroah-Hartman dev_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
10002154be65SJarod Wilson ep->bEndpointAddress);
1001c49fcddeSSean Young /* data comes in on 0x82, 0x81 is for learning */
1002c49fcddeSSean Young if (ep->bEndpointAddress == RR3_NARROW_IN_EP_ADDR)
1003c49fcddeSSean Young ep_narrow = ep;
1004c49fcddeSSean Young if (ep->bEndpointAddress == RR3_WIDE_IN_EP_ADDR)
1005c49fcddeSSean Young ep_wide = ep;
10062154be65SJarod Wilson }
10072154be65SJarod Wilson
10082154be65SJarod Wilson if ((ep_out == NULL) &&
10092154be65SJarod Wilson ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
10102154be65SJarod Wilson ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
10112154be65SJarod Wilson USB_ENDPOINT_XFER_BULK)) {
10123228bf81SGreg Kroah-Hartman dev_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
10132154be65SJarod Wilson ep->bEndpointAddress);
10142154be65SJarod Wilson ep_out = ep;
10152154be65SJarod Wilson }
10162154be65SJarod Wilson }
10172154be65SJarod Wilson
1018c49fcddeSSean Young if (!ep_narrow || !ep_out || !ep_wide) {
1019c49fcddeSSean Young dev_err(dev, "Couldn't find all endpoints\n");
10202154be65SJarod Wilson retval = -ENODEV;
10212154be65SJarod Wilson goto no_endpoints;
10222154be65SJarod Wilson }
10232154be65SJarod Wilson
10242154be65SJarod Wilson /* allocate memory for our device state and initialize it */
10252154be65SJarod Wilson rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
1026559f64d4SMarkus Elfring if (!rr3)
10277eb75715SDan Carpenter goto no_endpoints;
10282154be65SJarod Wilson
10292154be65SJarod Wilson rr3->dev = &intf->dev;
1030c49fcddeSSean Young rr3->ep_narrow = ep_narrow;
10318a21ec9bSSean Young rr3->ep_out = ep_out;
10328a21ec9bSSean Young rr3->udev = udev;
10332154be65SJarod Wilson
10342154be65SJarod Wilson /* set up bulk-in endpoint */
1035c49fcddeSSean Young rr3->narrow_urb = usb_alloc_urb(0, GFP_KERNEL);
1036c49fcddeSSean Young if (!rr3->narrow_urb)
1037c49fcddeSSean Young goto redrat_free;
1038c49fcddeSSean Young
1039c49fcddeSSean Young rr3->wide_urb = usb_alloc_urb(0, GFP_KERNEL);
1040c49fcddeSSean Young if (!rr3->wide_urb)
10418a21ec9bSSean Young goto redrat_free;
10422154be65SJarod Wilson
1043fa7b9ac2SSean Young rr3->bulk_in_buf = usb_alloc_coherent(udev,
1044c49fcddeSSean Young le16_to_cpu(ep_narrow->wMaxPacketSize),
1045c49fcddeSSean Young GFP_KERNEL, &rr3->dma_in);
1046559f64d4SMarkus Elfring if (!rr3->bulk_in_buf)
10478a21ec9bSSean Young goto redrat_free;
10482154be65SJarod Wilson
1049c49fcddeSSean Young pipe = usb_rcvbulkpipe(udev, ep_narrow->bEndpointAddress);
1050c49fcddeSSean Young usb_fill_bulk_urb(rr3->narrow_urb, udev, pipe, rr3->bulk_in_buf,
1051c49fcddeSSean Young le16_to_cpu(ep_narrow->wMaxPacketSize),
1052c49fcddeSSean Young redrat3_handle_async, rr3);
1053c49fcddeSSean Young rr3->narrow_urb->transfer_dma = rr3->dma_in;
1054c49fcddeSSean Young rr3->narrow_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1055c49fcddeSSean Young
1056c49fcddeSSean Young pipe = usb_rcvbulkpipe(udev, ep_wide->bEndpointAddress);
1057c49fcddeSSean Young usb_fill_bulk_urb(rr3->wide_urb, udev, pipe, rr3->bulk_in_buf,
1058c49fcddeSSean Young le16_to_cpu(ep_narrow->wMaxPacketSize),
1059c49fcddeSSean Young redrat3_handle_async, rr3);
1060c49fcddeSSean Young rr3->wide_urb->transfer_dma = rr3->dma_in;
1061c49fcddeSSean Young rr3->wide_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
10622154be65SJarod Wilson
10632154be65SJarod Wilson redrat3_reset(rr3);
10642154be65SJarod Wilson redrat3_get_firmware_rev(rr3);
10652154be65SJarod Wilson
10662154be65SJarod Wilson /* default.. will get overridden by any sends with a freq defined */
10672154be65SJarod Wilson rr3->carrier = 38000;
10682154be65SJarod Wilson
1069bf139726SSean Young atomic_set(&rr3->flash, 0);
1070bf139726SSean Young rr3->flash_urb = usb_alloc_urb(0, GFP_KERNEL);
10718a21ec9bSSean Young if (!rr3->flash_urb)
10728a21ec9bSSean Young goto redrat_free;
1073bf139726SSean Young
1074c49fcddeSSean Young /* learn urb */
1075c49fcddeSSean Young rr3->learn_urb = usb_alloc_urb(0, GFP_KERNEL);
1076c49fcddeSSean Young if (!rr3->learn_urb)
1077c49fcddeSSean Young goto redrat_free;
1078c49fcddeSSean Young
1079c49fcddeSSean Young /* setup packet is 'c0 b2 0000 0000 0001' */
1080c49fcddeSSean Young rr3->learn_control.bRequestType = 0xc0;
1081c49fcddeSSean Young rr3->learn_control.bRequest = RR3_MODSIG_CAPTURE;
1082c49fcddeSSean Young rr3->learn_control.wLength = cpu_to_le16(1);
1083c49fcddeSSean Young
1084c49fcddeSSean Young usb_fill_control_urb(rr3->learn_urb, udev, usb_rcvctrlpipe(udev, 0),
1085c49fcddeSSean Young (unsigned char *)&rr3->learn_control,
1086c49fcddeSSean Young &rr3->learn_buf, sizeof(rr3->learn_buf),
1087c49fcddeSSean Young redrat3_learn_complete, rr3);
1088c49fcddeSSean Young
1089bf139726SSean Young /* setup packet is 'c0 b9 0000 0000 0001' */
1090bf139726SSean Young rr3->flash_control.bRequestType = 0xc0;
1091bf139726SSean Young rr3->flash_control.bRequest = RR3_BLINK_LED;
1092bf139726SSean Young rr3->flash_control.wLength = cpu_to_le16(1);
1093bf139726SSean Young
1094bf139726SSean Young usb_fill_control_urb(rr3->flash_urb, udev, usb_rcvctrlpipe(udev, 0),
1095bf139726SSean Young (unsigned char *)&rr3->flash_control,
1096bf139726SSean Young &rr3->flash_in_buf, sizeof(rr3->flash_in_buf),
1097bf139726SSean Young redrat3_led_complete, rr3);
1098bf139726SSean Young
10998a21ec9bSSean Young /* led control */
11008a21ec9bSSean Young rr3->led.name = "redrat3:red:feedback";
11018a21ec9bSSean Young rr3->led.default_trigger = "rc-feedback";
11028a21ec9bSSean Young rr3->led.brightness_set = redrat3_brightness_set;
11038a21ec9bSSean Young retval = led_classdev_register(&intf->dev, &rr3->led);
11048a21ec9bSSean Young if (retval)
11058a21ec9bSSean Young goto redrat_free;
11068a21ec9bSSean Young
11072154be65SJarod Wilson rr3->rc = redrat3_init_rc_dev(rr3);
11086ea7cf76SPeter Senna Tschudin if (!rr3->rc) {
11096ea7cf76SPeter Senna Tschudin retval = -ENOMEM;
11108a21ec9bSSean Young goto led_free;
11116ea7cf76SPeter Senna Tschudin }
11122154be65SJarod Wilson
11138a21ec9bSSean Young /* might be all we need to do? */
11148a21ec9bSSean Young retval = redrat3_enable_detector(rr3);
11158a21ec9bSSean Young if (retval < 0)
11168a21ec9bSSean Young goto led_free;
11178a21ec9bSSean Young
11182154be65SJarod Wilson /* we can register the device now, as it is ready */
11192154be65SJarod Wilson usb_set_intfdata(intf, rr3);
11202154be65SJarod Wilson
11212154be65SJarod Wilson return 0;
11222154be65SJarod Wilson
11238a21ec9bSSean Young led_free:
1124bf139726SSean Young led_classdev_unregister(&rr3->led);
11258a21ec9bSSean Young redrat_free:
11262154be65SJarod Wilson redrat3_delete(rr3, rr3->udev);
11272154be65SJarod Wilson
11282154be65SJarod Wilson no_endpoints:
11292154be65SJarod Wilson return retval;
11302154be65SJarod Wilson }
11312154be65SJarod Wilson
redrat3_dev_disconnect(struct usb_interface * intf)11324c62e976SGreg Kroah-Hartman static void redrat3_dev_disconnect(struct usb_interface *intf)
11332154be65SJarod Wilson {
11342154be65SJarod Wilson struct usb_device *udev = interface_to_usbdev(intf);
11352154be65SJarod Wilson struct redrat3_dev *rr3 = usb_get_intfdata(intf);
11362154be65SJarod Wilson
11372154be65SJarod Wilson usb_set_intfdata(intf, NULL);
11382154be65SJarod Wilson rc_unregister_device(rr3->rc);
1139bf139726SSean Young led_classdev_unregister(&rr3->led);
11402154be65SJarod Wilson redrat3_delete(rr3, udev);
11412154be65SJarod Wilson }
11422154be65SJarod Wilson
redrat3_dev_suspend(struct usb_interface * intf,pm_message_t message)11432154be65SJarod Wilson static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
11442154be65SJarod Wilson {
11452154be65SJarod Wilson struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1146a36d5b61SGreg Kroah-Hartman
1147bf139726SSean Young led_classdev_suspend(&rr3->led);
1148c49fcddeSSean Young usb_kill_urb(rr3->narrow_urb);
1149c49fcddeSSean Young usb_kill_urb(rr3->wide_urb);
1150bf139726SSean Young usb_kill_urb(rr3->flash_urb);
11512154be65SJarod Wilson return 0;
11522154be65SJarod Wilson }
11532154be65SJarod Wilson
redrat3_dev_resume(struct usb_interface * intf)11542154be65SJarod Wilson static int redrat3_dev_resume(struct usb_interface *intf)
11552154be65SJarod Wilson {
11562154be65SJarod Wilson struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1157a36d5b61SGreg Kroah-Hartman
1158*407cba97SOliver Neukum if (usb_submit_urb(rr3->narrow_urb, GFP_NOIO))
1159c49fcddeSSean Young return -EIO;
1160*407cba97SOliver Neukum if (usb_submit_urb(rr3->wide_urb, GFP_NOIO))
11612154be65SJarod Wilson return -EIO;
1162bf139726SSean Young led_classdev_resume(&rr3->led);
11632154be65SJarod Wilson return 0;
11642154be65SJarod Wilson }
11652154be65SJarod Wilson
11662154be65SJarod Wilson static struct usb_driver redrat3_dev_driver = {
11672154be65SJarod Wilson .name = DRIVER_NAME,
11682154be65SJarod Wilson .probe = redrat3_dev_probe,
11694c62e976SGreg Kroah-Hartman .disconnect = redrat3_dev_disconnect,
11702154be65SJarod Wilson .suspend = redrat3_dev_suspend,
11712154be65SJarod Wilson .resume = redrat3_dev_resume,
11722154be65SJarod Wilson .reset_resume = redrat3_dev_resume,
11732154be65SJarod Wilson .id_table = redrat3_dev_table
11742154be65SJarod Wilson };
11752154be65SJarod Wilson
1176ecb3b2b3SGreg Kroah-Hartman module_usb_driver(redrat3_dev_driver);
11772154be65SJarod Wilson
11782154be65SJarod Wilson MODULE_DESCRIPTION(DRIVER_DESC);
11792154be65SJarod Wilson MODULE_AUTHOR(DRIVER_AUTHOR);
11802154be65SJarod Wilson MODULE_AUTHOR(DRIVER_AUTHOR2);
11812154be65SJarod Wilson MODULE_LICENSE("GPL");
11822154be65SJarod Wilson MODULE_DEVICE_TABLE(usb, redrat3_dev_table);
1183