12154be65SJarod Wilson /* 22154be65SJarod Wilson * USB RedRat3 IR Transceiver rc-core driver 32154be65SJarod Wilson * 42154be65SJarod Wilson * Copyright (c) 2011 by Jarod Wilson <jarod@redhat.com> 52154be65SJarod Wilson * based heavily on the work of Stephen Cox, with additional 62154be65SJarod Wilson * help from RedRat Ltd. 72154be65SJarod Wilson * 82154be65SJarod Wilson * This driver began life based an an old version of the first-generation 92154be65SJarod Wilson * lirc_mceusb driver from the lirc 0.7.2 distribution. It was then 102154be65SJarod Wilson * significantly rewritten by Stephen Cox with the aid of RedRat Ltd's 112154be65SJarod Wilson * Chris Dodge. 122154be65SJarod Wilson * 132154be65SJarod Wilson * The driver was then ported to rc-core and significantly rewritten again, 142154be65SJarod Wilson * by Jarod, using the in-kernel mceusb driver as a guide, after an initial 152154be65SJarod Wilson * port effort was started by Stephen. 162154be65SJarod Wilson * 172154be65SJarod Wilson * TODO LIST: 182154be65SJarod Wilson * - fix lirc not showing repeats properly 192154be65SJarod Wilson * -- 202154be65SJarod Wilson * 212154be65SJarod Wilson * The RedRat3 is a USB transceiver with both send & receive, 222154be65SJarod Wilson * with 2 separate sensors available for receive to enable 232154be65SJarod Wilson * both good long range reception for general use, and good 242154be65SJarod Wilson * short range reception when required for learning a signal. 252154be65SJarod Wilson * 262154be65SJarod Wilson * http://www.redrat.co.uk/ 272154be65SJarod Wilson * 282154be65SJarod Wilson * It uses its own little protocol to communicate, the required 292154be65SJarod Wilson * parts of which are embedded within this driver. 302154be65SJarod Wilson * -- 312154be65SJarod Wilson * 322154be65SJarod Wilson * This program is free software; you can redistribute it and/or modify 332154be65SJarod Wilson * it under the terms of the GNU General Public License as published by 342154be65SJarod Wilson * the Free Software Foundation; either version 2 of the License, or 352154be65SJarod Wilson * (at your option) any later version. 362154be65SJarod Wilson * 372154be65SJarod Wilson * This program is distributed in the hope that it will be useful, 382154be65SJarod Wilson * but WITHOUT ANY WARRANTY; without even the implied warranty of 392154be65SJarod Wilson * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 402154be65SJarod Wilson * GNU General Public License for more details. 412154be65SJarod Wilson * 422154be65SJarod Wilson * You should have received a copy of the GNU General Public License 432154be65SJarod Wilson * along with this program; if not, write to the Free Software 442154be65SJarod Wilson * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 452154be65SJarod Wilson * 462154be65SJarod Wilson */ 472154be65SJarod Wilson 484c055a5aSSean Young #include <asm/unaligned.h> 492154be65SJarod Wilson #include <linux/device.h> 50bf139726SSean Young #include <linux/leds.h> 512154be65SJarod Wilson #include <linux/module.h> 522154be65SJarod Wilson #include <linux/slab.h> 532154be65SJarod Wilson #include <linux/usb.h> 542154be65SJarod Wilson #include <linux/usb/input.h> 552154be65SJarod Wilson #include <media/rc-core.h> 562154be65SJarod Wilson 572154be65SJarod Wilson /* Driver Information */ 582154be65SJarod Wilson #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>" 592154be65SJarod Wilson #define DRIVER_AUTHOR2 "The Dweller, Stephen Cox" 602154be65SJarod Wilson #define DRIVER_DESC "RedRat3 USB IR Transceiver Driver" 612154be65SJarod Wilson #define DRIVER_NAME "redrat3" 622154be65SJarod Wilson 632154be65SJarod Wilson /* bulk data transfer types */ 642154be65SJarod Wilson #define RR3_ERROR 0x01 652154be65SJarod Wilson #define RR3_MOD_SIGNAL_IN 0x20 662154be65SJarod Wilson #define RR3_MOD_SIGNAL_OUT 0x21 672154be65SJarod Wilson 682154be65SJarod Wilson /* Get the RR firmware version */ 692154be65SJarod Wilson #define RR3_FW_VERSION 0xb1 702154be65SJarod Wilson #define RR3_FW_VERSION_LEN 64 712154be65SJarod Wilson /* Send encoded signal bulk-sent earlier*/ 722154be65SJarod Wilson #define RR3_TX_SEND_SIGNAL 0xb3 732154be65SJarod Wilson #define RR3_SET_IR_PARAM 0xb7 742154be65SJarod Wilson #define RR3_GET_IR_PARAM 0xb8 752154be65SJarod Wilson /* Blink the red LED on the device */ 762154be65SJarod Wilson #define RR3_BLINK_LED 0xb9 772154be65SJarod Wilson /* Read serial number of device */ 782154be65SJarod Wilson #define RR3_READ_SER_NO 0xba 792154be65SJarod Wilson #define RR3_SER_NO_LEN 4 802154be65SJarod Wilson /* Start capture with the RC receiver */ 812154be65SJarod Wilson #define RR3_RC_DET_ENABLE 0xbb 822154be65SJarod Wilson /* Stop capture with the RC receiver */ 832154be65SJarod Wilson #define RR3_RC_DET_DISABLE 0xbc 842154be65SJarod Wilson /* Return the status of RC detector capture */ 852154be65SJarod Wilson #define RR3_RC_DET_STATUS 0xbd 862154be65SJarod Wilson /* Reset redrat */ 872154be65SJarod Wilson #define RR3_RESET 0xa0 882154be65SJarod Wilson 892154be65SJarod Wilson /* Max number of lengths in the signal. */ 902154be65SJarod Wilson #define RR3_IR_IO_MAX_LENGTHS 0x01 912154be65SJarod Wilson /* Periods to measure mod. freq. */ 922154be65SJarod Wilson #define RR3_IR_IO_PERIODS_MF 0x02 932154be65SJarod Wilson /* Size of memory for main signal data */ 942154be65SJarod Wilson #define RR3_IR_IO_SIG_MEM_SIZE 0x03 952154be65SJarod Wilson /* Delta value when measuring lengths */ 962154be65SJarod Wilson #define RR3_IR_IO_LENGTH_FUZZ 0x04 972154be65SJarod Wilson /* Timeout for end of signal detection */ 982154be65SJarod Wilson #define RR3_IR_IO_SIG_TIMEOUT 0x05 9939c1cb2bSJonathan McCrohan /* Minimum value for pause recognition. */ 1002154be65SJarod Wilson #define RR3_IR_IO_MIN_PAUSE 0x06 1012154be65SJarod Wilson 1022154be65SJarod Wilson /* Clock freq. of EZ-USB chip */ 1032154be65SJarod Wilson #define RR3_CLK 24000000 1042154be65SJarod Wilson /* Clock periods per timer count */ 1052154be65SJarod Wilson #define RR3_CLK_PER_COUNT 12 1062154be65SJarod Wilson /* (RR3_CLK / RR3_CLK_PER_COUNT) */ 1072154be65SJarod Wilson #define RR3_CLK_CONV_FACTOR 2000000 1082154be65SJarod Wilson /* USB bulk-in IR data endpoint address */ 1092154be65SJarod Wilson #define RR3_BULK_IN_EP_ADDR 0x82 1102154be65SJarod Wilson 1112154be65SJarod Wilson /* Size of the fixed-length portion of the signal */ 1122154be65SJarod Wilson #define RR3_DRIVER_MAXLENS 128 1132154be65SJarod Wilson #define RR3_MAX_SIG_SIZE 512 1142154be65SJarod Wilson #define RR3_TIME_UNIT 50 1152154be65SJarod Wilson #define RR3_END_OF_SIGNAL 0x7f 1162154be65SJarod Wilson #define RR3_TX_TRAILER_LEN 2 1172154be65SJarod Wilson #define RR3_RX_MIN_TIMEOUT 5 1182154be65SJarod Wilson #define RR3_RX_MAX_TIMEOUT 2000 1192154be65SJarod Wilson 1202154be65SJarod Wilson /* The 8051's CPUCS Register address */ 1212154be65SJarod Wilson #define RR3_CPUCS_REG_ADDR 0x7f92 1222154be65SJarod Wilson 1232154be65SJarod Wilson #define USB_RR3USB_VENDOR_ID 0x112a 1242154be65SJarod Wilson #define USB_RR3USB_PRODUCT_ID 0x0001 1252154be65SJarod Wilson #define USB_RR3IIUSB_PRODUCT_ID 0x0005 1262154be65SJarod Wilson 127d6ae162bSSean Young 128d6ae162bSSean Young /* 129d6ae162bSSean Young * The redrat3 encodes an IR signal as set of different lengths and a set 130d6ae162bSSean Young * of indices into those lengths. This sets how much two lengths must 131d6ae162bSSean Young * differ before they are considered distinct, the value is specified 132d6ae162bSSean Young * in microseconds. 133d6ae162bSSean Young * Default 5, value 0 to 127. 134d6ae162bSSean Young */ 135d6ae162bSSean Young static int length_fuzz = 5; 136d6ae162bSSean Young module_param(length_fuzz, uint, 0644); 137d6ae162bSSean Young MODULE_PARM_DESC(length_fuzz, "Length Fuzz (0-127)"); 138d6ae162bSSean Young 139d6ae162bSSean Young /* 140d6ae162bSSean Young * When receiving a continuous ir stream (for example when a user is 141d6ae162bSSean Young * holding a button down on a remote), this specifies the minimum size 142d6ae162bSSean Young * of a space when the redrat3 sends a irdata packet to the host. Specified 143d6ae162bSSean Young * in miliseconds. Default value 18ms. 144d6ae162bSSean Young * The value can be between 2 and 30 inclusive. 145d6ae162bSSean Young */ 146d6ae162bSSean Young static int minimum_pause = 18; 147d6ae162bSSean Young module_param(minimum_pause, uint, 0644); 148d6ae162bSSean Young MODULE_PARM_DESC(minimum_pause, "Minimum Pause in ms (2-30)"); 149d6ae162bSSean Young 150d6ae162bSSean Young /* 151d6ae162bSSean Young * The carrier frequency is measured during the first pulse of the IR 152d6ae162bSSean Young * signal. The larger the number of periods used To measure, the more 153d6ae162bSSean Young * accurate the result is likely to be, however some signals have short 154d6ae162bSSean Young * initial pulses, so in some case it may be necessary to reduce this value. 155d6ae162bSSean Young * Default 8, value 1 to 255. 156d6ae162bSSean Young */ 157d6ae162bSSean Young static int periods_measure_carrier = 8; 158d6ae162bSSean Young module_param(periods_measure_carrier, uint, 0644); 159d6ae162bSSean Young MODULE_PARM_DESC(periods_measure_carrier, "Number of Periods to Measure Carrier (1-255)"); 160d6ae162bSSean Young 161d6ae162bSSean Young 1624c055a5aSSean Young struct redrat3_header { 1634c055a5aSSean Young __be16 length; 1644c055a5aSSean Young __be16 transfer_type; 1654c055a5aSSean Young } __packed; 1664c055a5aSSean Young 1674c055a5aSSean Young /* sending and receiving irdata */ 1684c055a5aSSean Young struct redrat3_irdata { 1694c055a5aSSean Young struct redrat3_header header; 1704c055a5aSSean Young __be32 pause; 1714c055a5aSSean Young __be16 mod_freq_count; 1724c055a5aSSean Young __be16 num_periods; 1734c055a5aSSean Young __u8 max_lengths; 1744c055a5aSSean Young __u8 no_lengths; 1754c055a5aSSean Young __be16 max_sig_size; 1764c055a5aSSean Young __be16 sig_size; 1774c055a5aSSean Young __u8 no_repeats; 1784c055a5aSSean Young __be16 lens[RR3_DRIVER_MAXLENS]; /* not aligned */ 1794c055a5aSSean Young __u8 sigdata[RR3_MAX_SIG_SIZE]; 1804c055a5aSSean Young } __packed; 1814c055a5aSSean Young 1824c055a5aSSean Young /* firmware errors */ 1834c055a5aSSean Young struct redrat3_error { 1844c055a5aSSean Young struct redrat3_header header; 1854c055a5aSSean Young __be16 fw_error; 1864c055a5aSSean Young } __packed; 1874c055a5aSSean Young 1882154be65SJarod Wilson /* table of devices that work with this driver */ 1892154be65SJarod Wilson static struct usb_device_id redrat3_dev_table[] = { 1902154be65SJarod Wilson /* Original version of the RedRat3 */ 1912154be65SJarod Wilson {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)}, 1922154be65SJarod Wilson /* Second Version/release of the RedRat3 - RetRat3-II */ 1932154be65SJarod Wilson {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)}, 1942154be65SJarod Wilson {} /* Terminating entry */ 1952154be65SJarod Wilson }; 1962154be65SJarod Wilson 1972154be65SJarod Wilson /* Structure to hold all of our device specific stuff */ 1982154be65SJarod Wilson struct redrat3_dev { 1992154be65SJarod Wilson /* core device bits */ 2002154be65SJarod Wilson struct rc_dev *rc; 2012154be65SJarod Wilson struct device *dev; 2022154be65SJarod Wilson 203bf139726SSean Young /* led control */ 204bf139726SSean Young struct led_classdev led; 205bf139726SSean Young atomic_t flash; 206bf139726SSean Young struct usb_ctrlrequest flash_control; 207bf139726SSean Young struct urb *flash_urb; 208bf139726SSean Young u8 flash_in_buf; 209bf139726SSean Young 2102154be65SJarod Wilson /* save off the usb device pointer */ 2112154be65SJarod Wilson struct usb_device *udev; 2122154be65SJarod Wilson 2132154be65SJarod Wilson /* the receive endpoint */ 2142154be65SJarod Wilson struct usb_endpoint_descriptor *ep_in; 2152154be65SJarod Wilson /* the buffer to receive data */ 2164c055a5aSSean Young void *bulk_in_buf; 2172154be65SJarod Wilson /* urb used to read ir data */ 2182154be65SJarod Wilson struct urb *read_urb; 2192154be65SJarod Wilson 2202154be65SJarod Wilson /* the send endpoint */ 2212154be65SJarod Wilson struct usb_endpoint_descriptor *ep_out; 2222154be65SJarod Wilson 2232154be65SJarod Wilson /* usb dma */ 2242154be65SJarod Wilson dma_addr_t dma_in; 2252154be65SJarod Wilson 2262154be65SJarod Wilson /* Is the device currently transmitting?*/ 2272154be65SJarod Wilson bool transmitting; 2282154be65SJarod Wilson 2292154be65SJarod Wilson /* store for current packet */ 2304c055a5aSSean Young struct redrat3_irdata irdata; 2312154be65SJarod Wilson u16 bytes_read; 2322154be65SJarod Wilson 2332154be65SJarod Wilson u32 carrier; 2342154be65SJarod Wilson 2354c055a5aSSean Young char name[64]; 2362154be65SJarod Wilson char phys[64]; 2372154be65SJarod Wilson }; 2382154be65SJarod Wilson 2392154be65SJarod Wilson /* 2402154be65SJarod Wilson * redrat3_issue_async 2412154be65SJarod Wilson * 2422154be65SJarod Wilson * Issues an async read to the ir data in port.. 2432154be65SJarod Wilson * sets the callback to be redrat3_handle_async 2442154be65SJarod Wilson */ 2452154be65SJarod Wilson static void redrat3_issue_async(struct redrat3_dev *rr3) 2462154be65SJarod Wilson { 2472154be65SJarod Wilson int res; 2482154be65SJarod Wilson 2492154be65SJarod Wilson res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC); 2502154be65SJarod Wilson if (res) 2513228bf81SGreg Kroah-Hartman dev_dbg(rr3->dev, 2523228bf81SGreg Kroah-Hartman "%s: receive request FAILED! (res %d, len %d)\n", 2533228bf81SGreg Kroah-Hartman __func__, res, rr3->read_urb->transfer_buffer_length); 2542154be65SJarod Wilson } 2552154be65SJarod Wilson 2562154be65SJarod Wilson static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code) 2572154be65SJarod Wilson { 2582154be65SJarod Wilson if (!rr3->transmitting && (code != 0x40)) 2592154be65SJarod Wilson dev_info(rr3->dev, "fw error code 0x%02x: ", code); 2602154be65SJarod Wilson 2612154be65SJarod Wilson switch (code) { 2622154be65SJarod Wilson case 0x00: 2632154be65SJarod Wilson pr_cont("No Error\n"); 2642154be65SJarod Wilson break; 2652154be65SJarod Wilson 2662154be65SJarod Wilson /* Codes 0x20 through 0x2f are IR Firmware Errors */ 2672154be65SJarod Wilson case 0x20: 26825ec587cSMauro Carvalho Chehab pr_cont("Initial signal pulse not long enough to measure carrier frequency\n"); 2692154be65SJarod Wilson break; 2702154be65SJarod Wilson case 0x21: 2712154be65SJarod Wilson pr_cont("Not enough length values allocated for signal\n"); 2722154be65SJarod Wilson break; 2732154be65SJarod Wilson case 0x22: 2742154be65SJarod Wilson pr_cont("Not enough memory allocated for signal data\n"); 2752154be65SJarod Wilson break; 2762154be65SJarod Wilson case 0x23: 2772154be65SJarod Wilson pr_cont("Too many signal repeats\n"); 2782154be65SJarod Wilson break; 2792154be65SJarod Wilson case 0x28: 28025ec587cSMauro Carvalho Chehab pr_cont("Insufficient memory available for IR signal data memory allocation\n"); 2812154be65SJarod Wilson break; 2822154be65SJarod Wilson case 0x29: 28325ec587cSMauro Carvalho Chehab pr_cont("Insufficient memory available for IrDa signal data memory allocation\n"); 2842154be65SJarod Wilson break; 2852154be65SJarod Wilson 2862154be65SJarod Wilson /* Codes 0x30 through 0x3f are USB Firmware Errors */ 2872154be65SJarod Wilson case 0x30: 28825ec587cSMauro Carvalho Chehab pr_cont("Insufficient memory available for bulk transfer structure\n"); 2892154be65SJarod Wilson break; 2902154be65SJarod Wilson 2912154be65SJarod Wilson /* 2922154be65SJarod Wilson * Other error codes... These are primarily errors that can occur in 2932154be65SJarod Wilson * the control messages sent to the redrat 2942154be65SJarod Wilson */ 2952154be65SJarod Wilson case 0x40: 2962154be65SJarod Wilson if (!rr3->transmitting) 2972154be65SJarod Wilson pr_cont("Signal capture has been terminated\n"); 2982154be65SJarod Wilson break; 2992154be65SJarod Wilson case 0x41: 30025ec587cSMauro Carvalho Chehab pr_cont("Attempt to set/get and unknown signal I/O algorithm parameter\n"); 3012154be65SJarod Wilson break; 3022154be65SJarod Wilson case 0x42: 3032154be65SJarod Wilson pr_cont("Signal capture already started\n"); 3042154be65SJarod Wilson break; 3052154be65SJarod Wilson 3062154be65SJarod Wilson default: 3072154be65SJarod Wilson pr_cont("Unknown Error\n"); 3082154be65SJarod Wilson break; 3092154be65SJarod Wilson } 3102154be65SJarod Wilson } 3112154be65SJarod Wilson 3124c055a5aSSean Young static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata) 3132154be65SJarod Wilson { 3142154be65SJarod Wilson u32 mod_freq = 0; 3154c055a5aSSean Young u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count); 3162154be65SJarod Wilson 3174c055a5aSSean Young if (mod_freq_count != 0) 3184c055a5aSSean Young mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) / 3194c055a5aSSean Young (mod_freq_count * RR3_CLK_PER_COUNT); 3202154be65SJarod Wilson 3212154be65SJarod Wilson return mod_freq; 3222154be65SJarod Wilson } 3232154be65SJarod Wilson 3242154be65SJarod Wilson /* this function scales down the figures for the same result... */ 3252154be65SJarod Wilson static u32 redrat3_len_to_us(u32 length) 3262154be65SJarod Wilson { 3272154be65SJarod Wilson u32 biglen = length * 1000; 3282154be65SJarod Wilson u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000; 3292154be65SJarod Wilson u32 result = (u32) (biglen / divisor); 3302154be65SJarod Wilson 3312154be65SJarod Wilson /* don't allow zero lengths to go back, breaks lirc */ 3322154be65SJarod Wilson return result ? result : 1; 3332154be65SJarod Wilson } 3342154be65SJarod Wilson 3352154be65SJarod Wilson /* 3362154be65SJarod Wilson * convert us back into redrat3 lengths 3372154be65SJarod Wilson * 3382154be65SJarod Wilson * length * 1000 length * 1000000 3392154be65SJarod Wilson * ------------- = ---------------- = micro 3402154be65SJarod Wilson * rr3clk / 1000 rr3clk 3412154be65SJarod Wilson 3422154be65SJarod Wilson * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000 3432154be65SJarod Wilson * ----- = 4 ----- = 6 -------------- = len --------------------- 3442154be65SJarod Wilson * 3 2 1000000 1000 3452154be65SJarod Wilson */ 3462154be65SJarod Wilson static u32 redrat3_us_to_len(u32 microsec) 3472154be65SJarod Wilson { 3482154be65SJarod Wilson u32 result; 3492154be65SJarod Wilson u32 divisor; 3502154be65SJarod Wilson 35195cf60aaSMauro Carvalho Chehab microsec = (microsec > IR_MAX_DURATION) ? IR_MAX_DURATION : microsec; 3522154be65SJarod Wilson divisor = (RR3_CLK_CONV_FACTOR / 1000); 3532154be65SJarod Wilson result = (u32)(microsec * divisor) / 1000; 3542154be65SJarod Wilson 3552154be65SJarod Wilson /* don't allow zero lengths to go back, breaks lirc */ 3562154be65SJarod Wilson return result ? result : 1; 3572154be65SJarod Wilson } 3582154be65SJarod Wilson 3592154be65SJarod Wilson static void redrat3_process_ir_data(struct redrat3_dev *rr3) 3602154be65SJarod Wilson { 3612154be65SJarod Wilson DEFINE_IR_RAW_EVENT(rawir); 3622154be65SJarod Wilson struct device *dev; 36325da661aSSean Young unsigned int i, sig_size, single_len, offset, val; 3644c055a5aSSean Young u32 mod_freq; 3652154be65SJarod Wilson 3662154be65SJarod Wilson if (!rr3) { 3672154be65SJarod Wilson pr_err("%s called with no context!\n", __func__); 3682154be65SJarod Wilson return; 3692154be65SJarod Wilson } 3702154be65SJarod Wilson 3712154be65SJarod Wilson dev = rr3->dev; 3722154be65SJarod Wilson 3734c055a5aSSean Young mod_freq = redrat3_val_to_mod_freq(&rr3->irdata); 3743228bf81SGreg Kroah-Hartman dev_dbg(dev, "Got mod_freq of %u\n", mod_freq); 3752154be65SJarod Wilson 3762154be65SJarod Wilson /* process each rr3 encoded byte into an int */ 3774c055a5aSSean Young sig_size = be16_to_cpu(rr3->irdata.sig_size); 3784c055a5aSSean Young for (i = 0; i < sig_size; i++) { 3794c055a5aSSean Young offset = rr3->irdata.sigdata[i]; 3804c055a5aSSean Young val = get_unaligned_be16(&rr3->irdata.lens[offset]); 3814c055a5aSSean Young single_len = redrat3_len_to_us(val); 3822154be65SJarod Wilson 3832154be65SJarod Wilson /* we should always get pulse/space/pulse/space samples */ 3842154be65SJarod Wilson if (i % 2) 3852154be65SJarod Wilson rawir.pulse = false; 3862154be65SJarod Wilson else 3872154be65SJarod Wilson rawir.pulse = true; 3882154be65SJarod Wilson 3892154be65SJarod Wilson rawir.duration = US_TO_NS(single_len); 3902c594ffaSJarod Wilson /* cap the value to IR_MAX_DURATION */ 39195cf60aaSMauro Carvalho Chehab rawir.duration = (rawir.duration > IR_MAX_DURATION) ? 39295cf60aaSMauro Carvalho Chehab IR_MAX_DURATION : rawir.duration; 3932c594ffaSJarod Wilson 3943228bf81SGreg Kroah-Hartman dev_dbg(dev, "storing %s with duration %d (i: %d)\n", 3952154be65SJarod Wilson rawir.pulse ? "pulse" : "space", rawir.duration, i); 3962154be65SJarod Wilson ir_raw_event_store_with_filter(rr3->rc, &rawir); 3972154be65SJarod Wilson } 3982154be65SJarod Wilson 39925da661aSSean Young /* add a trailing space */ 4002154be65SJarod Wilson rawir.pulse = false; 40125da661aSSean Young rawir.timeout = true; 402c7470ff2SSean Young rawir.duration = rr3->rc->timeout; 40325da661aSSean Young dev_dbg(dev, "storing trailing timeout with duration %d\n", 4042154be65SJarod Wilson rawir.duration); 4052154be65SJarod Wilson ir_raw_event_store_with_filter(rr3->rc, &rawir); 4062154be65SJarod Wilson 4073228bf81SGreg Kroah-Hartman dev_dbg(dev, "calling ir_raw_event_handle\n"); 4082154be65SJarod Wilson ir_raw_event_handle(rr3->rc); 4092154be65SJarod Wilson } 4102154be65SJarod Wilson 4112154be65SJarod Wilson /* Util fn to send rr3 cmds */ 4126948524dSMauro Carvalho Chehab static int redrat3_send_cmd(int cmd, struct redrat3_dev *rr3) 4132154be65SJarod Wilson { 4142154be65SJarod Wilson struct usb_device *udev; 4152154be65SJarod Wilson u8 *data; 4162154be65SJarod Wilson int res; 4172154be65SJarod Wilson 4182154be65SJarod Wilson data = kzalloc(sizeof(u8), GFP_KERNEL); 4192154be65SJarod Wilson if (!data) 4202154be65SJarod Wilson return -ENOMEM; 4212154be65SJarod Wilson 4222154be65SJarod Wilson udev = rr3->udev; 4232154be65SJarod Wilson res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd, 4242154be65SJarod Wilson USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 4252154be65SJarod Wilson 0x0000, 0x0000, data, sizeof(u8), HZ * 10); 4262154be65SJarod Wilson 4272154be65SJarod Wilson if (res < 0) { 4282154be65SJarod Wilson dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d", 4292154be65SJarod Wilson __func__, res, *data); 4302154be65SJarod Wilson res = -EIO; 4312154be65SJarod Wilson } else 4324c055a5aSSean Young res = data[0]; 4332154be65SJarod Wilson 4342154be65SJarod Wilson kfree(data); 4352154be65SJarod Wilson 4362154be65SJarod Wilson return res; 4372154be65SJarod Wilson } 4382154be65SJarod Wilson 4392154be65SJarod Wilson /* Enables the long range detector and starts async receive */ 4402154be65SJarod Wilson static int redrat3_enable_detector(struct redrat3_dev *rr3) 4412154be65SJarod Wilson { 4422154be65SJarod Wilson struct device *dev = rr3->dev; 4432154be65SJarod Wilson u8 ret; 4442154be65SJarod Wilson 4452154be65SJarod Wilson ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3); 4462154be65SJarod Wilson if (ret != 0) 4472154be65SJarod Wilson dev_dbg(dev, "%s: unexpected ret of %d\n", 4482154be65SJarod Wilson __func__, ret); 4492154be65SJarod Wilson 4502154be65SJarod Wilson ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3); 4512154be65SJarod Wilson if (ret != 1) { 4522154be65SJarod Wilson dev_err(dev, "%s: detector status: %d, should be 1\n", 4532154be65SJarod Wilson __func__, ret); 4542154be65SJarod Wilson return -EIO; 4552154be65SJarod Wilson } 4562154be65SJarod Wilson 4572154be65SJarod Wilson redrat3_issue_async(rr3); 4582154be65SJarod Wilson 4592154be65SJarod Wilson return 0; 4602154be65SJarod Wilson } 4612154be65SJarod Wilson 4622154be65SJarod Wilson static inline void redrat3_delete(struct redrat3_dev *rr3, 4632154be65SJarod Wilson struct usb_device *udev) 4642154be65SJarod Wilson { 4652154be65SJarod Wilson usb_kill_urb(rr3->read_urb); 466bf139726SSean Young usb_kill_urb(rr3->flash_urb); 4672154be65SJarod Wilson usb_free_urb(rr3->read_urb); 468bf139726SSean Young usb_free_urb(rr3->flash_urb); 469fa7b9ac2SSean Young usb_free_coherent(udev, le16_to_cpu(rr3->ep_in->wMaxPacketSize), 4702154be65SJarod Wilson rr3->bulk_in_buf, rr3->dma_in); 4712154be65SJarod Wilson 4722154be65SJarod Wilson kfree(rr3); 4732154be65SJarod Wilson } 4742154be65SJarod Wilson 475c53f9f00SJarod Wilson static u32 redrat3_get_timeout(struct redrat3_dev *rr3) 4762154be65SJarod Wilson { 477801b69f2SSean Young __be32 *tmp; 478c53f9f00SJarod Wilson u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */ 4792154be65SJarod Wilson int len, ret, pipe; 4802154be65SJarod Wilson 4812154be65SJarod Wilson len = sizeof(*tmp); 4822154be65SJarod Wilson tmp = kzalloc(len, GFP_KERNEL); 4832154be65SJarod Wilson if (!tmp) { 484c53f9f00SJarod Wilson dev_warn(rr3->dev, "Memory allocation faillure\n"); 4852154be65SJarod Wilson return timeout; 4862154be65SJarod Wilson } 4872154be65SJarod Wilson 488c53f9f00SJarod Wilson pipe = usb_rcvctrlpipe(rr3->udev, 0); 489c53f9f00SJarod Wilson ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM, 4902154be65SJarod Wilson USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 4912154be65SJarod Wilson RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5); 492801b69f2SSean Young if (ret != len) 493c53f9f00SJarod Wilson dev_warn(rr3->dev, "Failed to read timeout from hardware\n"); 494801b69f2SSean Young else { 495801b69f2SSean Young timeout = redrat3_len_to_us(be32_to_cpup(tmp)); 4962154be65SJarod Wilson 4973228bf81SGreg Kroah-Hartman dev_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000); 498801b69f2SSean Young } 499801b69f2SSean Young 500801b69f2SSean Young kfree(tmp); 501801b69f2SSean Young 5022154be65SJarod Wilson return timeout; 5032154be65SJarod Wilson } 5042154be65SJarod Wilson 5054f253cecSSean Young static int redrat3_set_timeout(struct rc_dev *rc_dev, unsigned int timeoutns) 5064f253cecSSean Young { 5074f253cecSSean Young struct redrat3_dev *rr3 = rc_dev->priv; 5084f253cecSSean Young struct usb_device *udev = rr3->udev; 5094f253cecSSean Young struct device *dev = rr3->dev; 510b1cb50beSHans Verkuil __be32 *timeout; 5114f253cecSSean Young int ret; 5124f253cecSSean Young 5134f253cecSSean Young timeout = kmalloc(sizeof(*timeout), GFP_KERNEL); 5144f253cecSSean Young if (!timeout) 5154f253cecSSean Young return -ENOMEM; 5164f253cecSSean Young 5174f253cecSSean Young *timeout = cpu_to_be32(redrat3_us_to_len(timeoutns / 1000)); 5184f253cecSSean Young ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), RR3_SET_IR_PARAM, 5194f253cecSSean Young USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, 5204f253cecSSean Young RR3_IR_IO_SIG_TIMEOUT, 0, timeout, sizeof(*timeout), 5214f253cecSSean Young HZ * 25); 5224f253cecSSean Young dev_dbg(dev, "set ir parm timeout %d ret 0x%02x\n", 5234f253cecSSean Young be32_to_cpu(*timeout), ret); 5244f253cecSSean Young 525c7470ff2SSean Young if (ret == sizeof(*timeout)) 5264f253cecSSean Young ret = 0; 527c7470ff2SSean Young else if (ret >= 0) 5284f253cecSSean Young ret = -EIO; 5294f253cecSSean Young 5304f253cecSSean Young kfree(timeout); 5314f253cecSSean Young 5324f253cecSSean Young return ret; 5334f253cecSSean Young } 5344f253cecSSean Young 5352154be65SJarod Wilson static void redrat3_reset(struct redrat3_dev *rr3) 5362154be65SJarod Wilson { 5372154be65SJarod Wilson struct usb_device *udev = rr3->udev; 5382154be65SJarod Wilson struct device *dev = rr3->dev; 5392154be65SJarod Wilson int rc, rxpipe, txpipe; 5402154be65SJarod Wilson u8 *val; 5412154be65SJarod Wilson int len = sizeof(u8); 5422154be65SJarod Wilson 5432154be65SJarod Wilson rxpipe = usb_rcvctrlpipe(udev, 0); 5442154be65SJarod Wilson txpipe = usb_sndctrlpipe(udev, 0); 5452154be65SJarod Wilson 546fa7b9ac2SSean Young val = kmalloc(len, GFP_KERNEL); 5472154be65SJarod Wilson if (!val) { 5482154be65SJarod Wilson dev_err(dev, "Memory allocation failure\n"); 5492154be65SJarod Wilson return; 5502154be65SJarod Wilson } 5512154be65SJarod Wilson 5522154be65SJarod Wilson *val = 0x01; 5532154be65SJarod Wilson rc = usb_control_msg(udev, rxpipe, RR3_RESET, 5542154be65SJarod Wilson USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 5552154be65SJarod Wilson RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25); 5563228bf81SGreg Kroah-Hartman dev_dbg(dev, "reset returned 0x%02x\n", rc); 5572154be65SJarod Wilson 558d6ae162bSSean Young *val = length_fuzz; 5592154be65SJarod Wilson rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, 5602154be65SJarod Wilson USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, 5612154be65SJarod Wilson RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25); 5623228bf81SGreg Kroah-Hartman dev_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc); 5632154be65SJarod Wilson 564d6ae162bSSean Young *val = (65536 - (minimum_pause * 2000)) / 256; 565d6ae162bSSean Young rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, 566d6ae162bSSean Young USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, 567d6ae162bSSean Young RR3_IR_IO_MIN_PAUSE, 0, val, len, HZ * 25); 568d6ae162bSSean Young dev_dbg(dev, "set ir parm min pause %d rc 0x%02x\n", *val, rc); 569d6ae162bSSean Young 570d6ae162bSSean Young *val = periods_measure_carrier; 571d6ae162bSSean Young rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, 572d6ae162bSSean Young USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, 573d6ae162bSSean Young RR3_IR_IO_PERIODS_MF, 0, val, len, HZ * 25); 574d6ae162bSSean Young dev_dbg(dev, "set ir parm periods measure carrier %d rc 0x%02x", *val, 575d6ae162bSSean Young rc); 576d6ae162bSSean Young 5772154be65SJarod Wilson *val = RR3_DRIVER_MAXLENS; 5782154be65SJarod Wilson rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, 5792154be65SJarod Wilson USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, 5802154be65SJarod Wilson RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25); 5813228bf81SGreg Kroah-Hartman dev_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc); 5822154be65SJarod Wilson 5832154be65SJarod Wilson kfree(val); 5842154be65SJarod Wilson } 5852154be65SJarod Wilson 5862154be65SJarod Wilson static void redrat3_get_firmware_rev(struct redrat3_dev *rr3) 5872154be65SJarod Wilson { 5882154be65SJarod Wilson int rc = 0; 5892154be65SJarod Wilson char *buffer; 5902154be65SJarod Wilson 591*0bebaa5fSMarkus Elfring buffer = kcalloc(RR3_FW_VERSION_LEN + 1, sizeof(*buffer), GFP_KERNEL); 5922154be65SJarod Wilson if (!buffer) { 5932154be65SJarod Wilson dev_err(rr3->dev, "Memory allocation failure\n"); 5942154be65SJarod Wilson return; 5952154be65SJarod Wilson } 5962154be65SJarod Wilson 5972154be65SJarod Wilson rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0), 5982154be65SJarod Wilson RR3_FW_VERSION, 5992154be65SJarod Wilson USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 6002154be65SJarod Wilson 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5); 6012154be65SJarod Wilson 6022154be65SJarod Wilson if (rc >= 0) 6032154be65SJarod Wilson dev_info(rr3->dev, "Firmware rev: %s", buffer); 6042154be65SJarod Wilson else 6052154be65SJarod Wilson dev_err(rr3->dev, "Problem fetching firmware ID\n"); 6062154be65SJarod Wilson 6072154be65SJarod Wilson kfree(buffer); 6082154be65SJarod Wilson } 6092154be65SJarod Wilson 610fa7b9ac2SSean Young static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len) 6112154be65SJarod Wilson { 6124c055a5aSSean Young struct redrat3_header *header = rr3->bulk_in_buf; 6134c055a5aSSean Young unsigned pktlen, pkttype; 6142154be65SJarod Wilson 6152154be65SJarod Wilson /* grab the Length and type of transfer */ 6164c055a5aSSean Young pktlen = be16_to_cpu(header->length); 6174c055a5aSSean Young pkttype = be16_to_cpu(header->transfer_type); 6182154be65SJarod Wilson 6194c055a5aSSean Young if (pktlen > sizeof(rr3->irdata)) { 6204c055a5aSSean Young dev_warn(rr3->dev, "packet length %u too large\n", pktlen); 6214c055a5aSSean Young return; 6224c055a5aSSean Young } 6232154be65SJarod Wilson 6244c055a5aSSean Young switch (pkttype) { 6252154be65SJarod Wilson case RR3_ERROR: 6264c055a5aSSean Young if (len >= sizeof(struct redrat3_error)) { 6274c055a5aSSean Young struct redrat3_error *error = rr3->bulk_in_buf; 6284c055a5aSSean Young unsigned fw_error = be16_to_cpu(error->fw_error); 6294c055a5aSSean Young redrat3_dump_fw_error(rr3, fw_error); 6304c055a5aSSean Young } 6312154be65SJarod Wilson break; 6322154be65SJarod Wilson 6332154be65SJarod Wilson case RR3_MOD_SIGNAL_IN: 6344c055a5aSSean Young memcpy(&rr3->irdata, rr3->bulk_in_buf, len); 6352154be65SJarod Wilson rr3->bytes_read = len; 6363228bf81SGreg Kroah-Hartman dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", 6374c055a5aSSean Young rr3->bytes_read, pktlen); 6382154be65SJarod Wilson break; 6392154be65SJarod Wilson 6402154be65SJarod Wilson default: 6413228bf81SGreg Kroah-Hartman dev_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n", 6424c055a5aSSean Young pkttype, len, pktlen); 6432154be65SJarod Wilson break; 6442154be65SJarod Wilson } 6452154be65SJarod Wilson } 6462154be65SJarod Wilson 647fa7b9ac2SSean Young static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len) 6482154be65SJarod Wilson { 6494c055a5aSSean Young void *irdata = &rr3->irdata; 6504c055a5aSSean Young 6514c055a5aSSean Young if (len + rr3->bytes_read > sizeof(rr3->irdata)) { 6524c055a5aSSean Young dev_warn(rr3->dev, "too much data for packet\n"); 6534c055a5aSSean Young rr3->bytes_read = 0; 6544c055a5aSSean Young return; 6554c055a5aSSean Young } 6564c055a5aSSean Young 6574c055a5aSSean Young memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len); 6582154be65SJarod Wilson 6592154be65SJarod Wilson rr3->bytes_read += len; 6603228bf81SGreg Kroah-Hartman dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read, 6614c055a5aSSean Young be16_to_cpu(rr3->irdata.header.length)); 6622154be65SJarod Wilson } 6632154be65SJarod Wilson 6642154be65SJarod Wilson /* gather IR data from incoming urb, process it when we have enough */ 665fa7b9ac2SSean Young static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len) 6662154be65SJarod Wilson { 6672154be65SJarod Wilson struct device *dev = rr3->dev; 6684c055a5aSSean Young unsigned pkttype; 6692154be65SJarod Wilson int ret = 0; 6702154be65SJarod Wilson 6714c055a5aSSean Young if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) { 6722154be65SJarod Wilson redrat3_read_packet_start(rr3, len); 6732154be65SJarod Wilson } else if (rr3->bytes_read != 0) { 6742154be65SJarod Wilson redrat3_read_packet_continue(rr3, len); 6752154be65SJarod Wilson } else if (rr3->bytes_read == 0) { 6762154be65SJarod Wilson dev_err(dev, "error: no packet data read\n"); 6772154be65SJarod Wilson ret = -ENODATA; 6782154be65SJarod Wilson goto out; 6792154be65SJarod Wilson } 6802154be65SJarod Wilson 68138e35a85SSean Young if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length) + 68238e35a85SSean Young sizeof(struct redrat3_header)) 6832154be65SJarod Wilson /* we're still accumulating data */ 6842154be65SJarod Wilson return 0; 6852154be65SJarod Wilson 6862154be65SJarod Wilson /* if we get here, we've got IR data to decode */ 6874c055a5aSSean Young pkttype = be16_to_cpu(rr3->irdata.header.transfer_type); 6884c055a5aSSean Young if (pkttype == RR3_MOD_SIGNAL_IN) 6892154be65SJarod Wilson redrat3_process_ir_data(rr3); 6902154be65SJarod Wilson else 6913228bf81SGreg Kroah-Hartman dev_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n", 6924c055a5aSSean Young pkttype); 6932154be65SJarod Wilson 6942154be65SJarod Wilson out: 6952154be65SJarod Wilson rr3->bytes_read = 0; 6962154be65SJarod Wilson return ret; 6972154be65SJarod Wilson } 6982154be65SJarod Wilson 6992154be65SJarod Wilson /* callback function from USB when async USB request has completed */ 700801b69f2SSean Young static void redrat3_handle_async(struct urb *urb) 7012154be65SJarod Wilson { 7022154be65SJarod Wilson struct redrat3_dev *rr3; 703dbea1880SAndrew Vincer int ret; 7042154be65SJarod Wilson 7052154be65SJarod Wilson if (!urb) 7062154be65SJarod Wilson return; 7072154be65SJarod Wilson 7082154be65SJarod Wilson rr3 = urb->context; 7092154be65SJarod Wilson if (!rr3) { 7102154be65SJarod Wilson pr_err("%s called with invalid context!\n", __func__); 7112154be65SJarod Wilson usb_unlink_urb(urb); 7122154be65SJarod Wilson return; 7132154be65SJarod Wilson } 7142154be65SJarod Wilson 7152154be65SJarod Wilson switch (urb->status) { 7162154be65SJarod Wilson case 0: 717dbea1880SAndrew Vincer ret = redrat3_get_ir_data(rr3, urb->actual_length); 718dbea1880SAndrew Vincer if (!ret) { 719dbea1880SAndrew Vincer /* no error, prepare to read more */ 720dbea1880SAndrew Vincer redrat3_issue_async(rr3); 721dbea1880SAndrew Vincer } 7222154be65SJarod Wilson break; 7232154be65SJarod Wilson 7242154be65SJarod Wilson case -ECONNRESET: 7252154be65SJarod Wilson case -ENOENT: 7262154be65SJarod Wilson case -ESHUTDOWN: 7272154be65SJarod Wilson usb_unlink_urb(urb); 7282154be65SJarod Wilson return; 7292154be65SJarod Wilson 7302154be65SJarod Wilson case -EPIPE: 7312154be65SJarod Wilson default: 7322154be65SJarod Wilson dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status); 7332154be65SJarod Wilson rr3->bytes_read = 0; 7342154be65SJarod Wilson break; 7352154be65SJarod Wilson } 7362154be65SJarod Wilson } 7372154be65SJarod Wilson 7382154be65SJarod Wilson static u16 mod_freq_to_val(unsigned int mod_freq) 7392154be65SJarod Wilson { 7402154be65SJarod Wilson int mult = 6000000; 7412154be65SJarod Wilson 7422154be65SJarod Wilson /* Clk used in mod. freq. generation is CLK24/4. */ 7434c055a5aSSean Young return 65536 - (mult / mod_freq); 7442154be65SJarod Wilson } 7452154be65SJarod Wilson 746dbea1880SAndrew Vincer static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier) 7472154be65SJarod Wilson { 748dbea1880SAndrew Vincer struct redrat3_dev *rr3 = rcdev->priv; 749dbea1880SAndrew Vincer struct device *dev = rr3->dev; 7502154be65SJarod Wilson 7513228bf81SGreg Kroah-Hartman dev_dbg(dev, "Setting modulation frequency to %u", carrier); 75248cafec9SDan Carpenter if (carrier == 0) 75348cafec9SDan Carpenter return -EINVAL; 75448cafec9SDan Carpenter 7552154be65SJarod Wilson rr3->carrier = carrier; 7562154be65SJarod Wilson 75714d8188aSSean Young return 0; 7582154be65SJarod Wilson } 7592154be65SJarod Wilson 760dbea1880SAndrew Vincer static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf, 761dbea1880SAndrew Vincer unsigned count) 7622154be65SJarod Wilson { 7632154be65SJarod Wilson struct redrat3_dev *rr3 = rcdev->priv; 7642154be65SJarod Wilson struct device *dev = rr3->dev; 7654c055a5aSSean Young struct redrat3_irdata *irdata = NULL; 766fa7b9ac2SSean Young int ret, ret_len; 7672154be65SJarod Wilson int lencheck, cur_sample_len, pipe; 7682154be65SJarod Wilson int *sample_lens = NULL; 7692154be65SJarod Wilson u8 curlencheck = 0; 770fa7b9ac2SSean Young unsigned i, sendbuf_len; 7712154be65SJarod Wilson 7722154be65SJarod Wilson if (rr3->transmitting) { 7732154be65SJarod Wilson dev_warn(dev, "%s: transmitter already in use\n", __func__); 7742154be65SJarod Wilson return -EAGAIN; 7752154be65SJarod Wilson } 7762154be65SJarod Wilson 777671ea670SSean Young if (count > RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN) 778671ea670SSean Young return -EINVAL; 7792154be65SJarod Wilson 780dbea1880SAndrew Vincer /* rr3 will disable rc detector on transmit */ 7812154be65SJarod Wilson rr3->transmitting = true; 7822154be65SJarod Wilson 783*0bebaa5fSMarkus Elfring sample_lens = kcalloc(RR3_DRIVER_MAXLENS, 784*0bebaa5fSMarkus Elfring sizeof(*sample_lens), 785*0bebaa5fSMarkus Elfring GFP_KERNEL); 7862154be65SJarod Wilson if (!sample_lens) { 7872154be65SJarod Wilson ret = -ENOMEM; 7882154be65SJarod Wilson goto out; 7892154be65SJarod Wilson } 7902154be65SJarod Wilson 7914c055a5aSSean Young irdata = kzalloc(sizeof(*irdata), GFP_KERNEL); 7924c055a5aSSean Young if (!irdata) { 793801b69f2SSean Young ret = -ENOMEM; 794801b69f2SSean Young goto out; 795801b69f2SSean Young } 796801b69f2SSean Young 7972154be65SJarod Wilson for (i = 0; i < count; i++) { 7982154be65SJarod Wilson cur_sample_len = redrat3_us_to_len(txbuf[i]); 799801b69f2SSean Young if (cur_sample_len > 0xffff) { 800801b69f2SSean Young dev_warn(dev, "transmit period of %uus truncated to %uus\n", 801801b69f2SSean Young txbuf[i], redrat3_len_to_us(0xffff)); 802801b69f2SSean Young cur_sample_len = 0xffff; 803801b69f2SSean Young } 80406eae25fSSean Young for (lencheck = 0; lencheck < curlencheck; lencheck++) { 8052154be65SJarod Wilson if (sample_lens[lencheck] == cur_sample_len) 8062154be65SJarod Wilson break; 8072154be65SJarod Wilson } 8082154be65SJarod Wilson if (lencheck == curlencheck) { 8093228bf81SGreg Kroah-Hartman dev_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n", 8102154be65SJarod Wilson i, txbuf[i], curlencheck, cur_sample_len); 81106eae25fSSean Young if (curlencheck < RR3_DRIVER_MAXLENS) { 8122154be65SJarod Wilson /* now convert the value to a proper 8132154be65SJarod Wilson * rr3 value.. */ 8142154be65SJarod Wilson sample_lens[curlencheck] = cur_sample_len; 8154c055a5aSSean Young put_unaligned_be16(cur_sample_len, 8164c055a5aSSean Young &irdata->lens[curlencheck]); 8172154be65SJarod Wilson curlencheck++; 8182154be65SJarod Wilson } else { 819671ea670SSean Young ret = -EINVAL; 820671ea670SSean Young goto out; 8212154be65SJarod Wilson } 8222154be65SJarod Wilson } 8234c055a5aSSean Young irdata->sigdata[i] = lencheck; 8242154be65SJarod Wilson } 8252154be65SJarod Wilson 8264c055a5aSSean Young irdata->sigdata[count] = RR3_END_OF_SIGNAL; 8274c055a5aSSean Young irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL; 8282154be65SJarod Wilson 8294c055a5aSSean Young sendbuf_len = offsetof(struct redrat3_irdata, 8304c055a5aSSean Young sigdata[count + RR3_TX_TRAILER_LEN]); 8312154be65SJarod Wilson /* fill in our packet header */ 8324c055a5aSSean Young irdata->header.length = cpu_to_be16(sendbuf_len - 8334c055a5aSSean Young sizeof(struct redrat3_header)); 8344c055a5aSSean Young irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT); 8354c055a5aSSean Young irdata->pause = cpu_to_be32(redrat3_len_to_us(100)); 8364c055a5aSSean Young irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier)); 8374c055a5aSSean Young irdata->no_lengths = curlencheck; 8384c055a5aSSean Young irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN); 8392154be65SJarod Wilson 8402154be65SJarod Wilson pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress); 8414c055a5aSSean Young ret = usb_bulk_msg(rr3->udev, pipe, irdata, 8422154be65SJarod Wilson sendbuf_len, &ret_len, 10 * HZ); 8433228bf81SGreg Kroah-Hartman dev_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret); 8442154be65SJarod Wilson 8452154be65SJarod Wilson /* now tell the hardware to transmit what we sent it */ 8462154be65SJarod Wilson pipe = usb_rcvctrlpipe(rr3->udev, 0); 8472154be65SJarod Wilson ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL, 8482154be65SJarod Wilson USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, 8494c055a5aSSean Young 0, 0, irdata, 2, HZ * 10); 8502154be65SJarod Wilson 8512154be65SJarod Wilson if (ret < 0) 8522154be65SJarod Wilson dev_err(dev, "Error: control msg send failed, rc %d\n", ret); 8532154be65SJarod Wilson else 854dbea1880SAndrew Vincer ret = count; 8552154be65SJarod Wilson 8562154be65SJarod Wilson out: 8572154be65SJarod Wilson kfree(sample_lens); 8584c055a5aSSean Young kfree(irdata); 8592154be65SJarod Wilson 8602154be65SJarod Wilson rr3->transmitting = false; 861dbea1880SAndrew Vincer /* rr3 re-enables rc detector because it was enabled before */ 8622154be65SJarod Wilson 8632154be65SJarod Wilson return ret; 8642154be65SJarod Wilson } 8652154be65SJarod Wilson 866bf139726SSean Young static void redrat3_brightness_set(struct led_classdev *led_dev, enum 867bf139726SSean Young led_brightness brightness) 868bf139726SSean Young { 869bf139726SSean Young struct redrat3_dev *rr3 = container_of(led_dev, struct redrat3_dev, 870bf139726SSean Young led); 871bf139726SSean Young 872bf139726SSean Young if (brightness != LED_OFF && atomic_cmpxchg(&rr3->flash, 0, 1) == 0) { 873bf139726SSean Young int ret = usb_submit_urb(rr3->flash_urb, GFP_ATOMIC); 874bf139726SSean Young if (ret != 0) { 875bf139726SSean Young dev_dbg(rr3->dev, "%s: unexpected ret of %d\n", 876bf139726SSean Young __func__, ret); 877bf139726SSean Young atomic_set(&rr3->flash, 0); 878bf139726SSean Young } 879bf139726SSean Young } 880bf139726SSean Young } 881bf139726SSean Young 882bf139726SSean Young static void redrat3_led_complete(struct urb *urb) 883bf139726SSean Young { 884bf139726SSean Young struct redrat3_dev *rr3 = urb->context; 885bf139726SSean Young 886bf139726SSean Young switch (urb->status) { 887bf139726SSean Young case 0: 888bf139726SSean Young break; 889bf139726SSean Young case -ECONNRESET: 890bf139726SSean Young case -ENOENT: 891bf139726SSean Young case -ESHUTDOWN: 892bf139726SSean Young usb_unlink_urb(urb); 893bf139726SSean Young return; 894bf139726SSean Young case -EPIPE: 895bf139726SSean Young default: 896bf139726SSean Young dev_dbg(rr3->dev, "Error: urb status = %d\n", urb->status); 897bf139726SSean Young break; 898bf139726SSean Young } 899bf139726SSean Young 900bf139726SSean Young rr3->led.brightness = LED_OFF; 901bf139726SSean Young atomic_dec(&rr3->flash); 902bf139726SSean Young } 903bf139726SSean Young 9042154be65SJarod Wilson static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3) 9052154be65SJarod Wilson { 9062154be65SJarod Wilson struct device *dev = rr3->dev; 9072154be65SJarod Wilson struct rc_dev *rc; 9082154be65SJarod Wilson int ret = -ENODEV; 9092154be65SJarod Wilson u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct); 9102154be65SJarod Wilson 9112154be65SJarod Wilson rc = rc_allocate_device(); 9122154be65SJarod Wilson if (!rc) { 9132154be65SJarod Wilson dev_err(dev, "remote input dev allocation failed\n"); 9142154be65SJarod Wilson goto out; 9152154be65SJarod Wilson } 9162154be65SJarod Wilson 91725ec587cSMauro Carvalho Chehab snprintf(rr3->name, sizeof(rr3->name), "RedRat3%s Infrared Remote Transceiver (%04x:%04x)", 9182154be65SJarod Wilson prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "", 9192154be65SJarod Wilson le16_to_cpu(rr3->udev->descriptor.idVendor), prod); 9202154be65SJarod Wilson 9212154be65SJarod Wilson usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys)); 9222154be65SJarod Wilson 9232154be65SJarod Wilson rc->input_name = rr3->name; 9242154be65SJarod Wilson rc->input_phys = rr3->phys; 9252154be65SJarod Wilson usb_to_input_id(rr3->udev, &rc->input_id); 9262154be65SJarod Wilson rc->dev.parent = dev; 9272154be65SJarod Wilson rc->priv = rr3; 9282154be65SJarod Wilson rc->driver_type = RC_DRIVER_IR_RAW; 929c5540fbbSDavid Härdeman rc->allowed_protocols = RC_BIT_ALL; 9304f253cecSSean Young rc->min_timeout = MS_TO_NS(RR3_RX_MIN_TIMEOUT); 9314f253cecSSean Young rc->max_timeout = MS_TO_NS(RR3_RX_MAX_TIMEOUT); 932c7470ff2SSean Young rc->timeout = US_TO_NS(redrat3_get_timeout(rr3)); 9334f253cecSSean Young rc->s_timeout = redrat3_set_timeout; 9342154be65SJarod Wilson rc->tx_ir = redrat3_transmit_ir; 9352154be65SJarod Wilson rc->s_tx_carrier = redrat3_set_tx_carrier; 9362154be65SJarod Wilson rc->driver_name = DRIVER_NAME; 93706eae25fSSean Young rc->rx_resolution = US_TO_NS(2); 9382154be65SJarod Wilson rc->map_name = RC_MAP_HAUPPAUGE; 9392154be65SJarod Wilson 9402154be65SJarod Wilson ret = rc_register_device(rc); 9412154be65SJarod Wilson if (ret < 0) { 9422154be65SJarod Wilson dev_err(dev, "remote dev registration failed\n"); 9432154be65SJarod Wilson goto out; 9442154be65SJarod Wilson } 9452154be65SJarod Wilson 9462154be65SJarod Wilson return rc; 9472154be65SJarod Wilson 9482154be65SJarod Wilson out: 9492154be65SJarod Wilson rc_free_device(rc); 9502154be65SJarod Wilson return NULL; 9512154be65SJarod Wilson } 9522154be65SJarod Wilson 9534c62e976SGreg Kroah-Hartman static int redrat3_dev_probe(struct usb_interface *intf, 9542154be65SJarod Wilson const struct usb_device_id *id) 9552154be65SJarod Wilson { 9562154be65SJarod Wilson struct usb_device *udev = interface_to_usbdev(intf); 9572154be65SJarod Wilson struct device *dev = &intf->dev; 9582154be65SJarod Wilson struct usb_host_interface *uhi; 9592154be65SJarod Wilson struct redrat3_dev *rr3; 9602154be65SJarod Wilson struct usb_endpoint_descriptor *ep; 9612154be65SJarod Wilson struct usb_endpoint_descriptor *ep_in = NULL; 9622154be65SJarod Wilson struct usb_endpoint_descriptor *ep_out = NULL; 9632154be65SJarod Wilson u8 addr, attrs; 9642154be65SJarod Wilson int pipe, i; 9652154be65SJarod Wilson int retval = -ENOMEM; 9662154be65SJarod Wilson 9672154be65SJarod Wilson uhi = intf->cur_altsetting; 9682154be65SJarod Wilson 9692154be65SJarod Wilson /* find our bulk-in and bulk-out endpoints */ 9702154be65SJarod Wilson for (i = 0; i < uhi->desc.bNumEndpoints; ++i) { 9712154be65SJarod Wilson ep = &uhi->endpoint[i].desc; 9722154be65SJarod Wilson addr = ep->bEndpointAddress; 9732154be65SJarod Wilson attrs = ep->bmAttributes; 9742154be65SJarod Wilson 9752154be65SJarod Wilson if ((ep_in == NULL) && 9762154be65SJarod Wilson ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) && 9772154be65SJarod Wilson ((attrs & USB_ENDPOINT_XFERTYPE_MASK) == 9782154be65SJarod Wilson USB_ENDPOINT_XFER_BULK)) { 9793228bf81SGreg Kroah-Hartman dev_dbg(dev, "found bulk-in endpoint at 0x%02x\n", 9802154be65SJarod Wilson ep->bEndpointAddress); 9812154be65SJarod Wilson /* data comes in on 0x82, 0x81 is for other data... */ 9822154be65SJarod Wilson if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR) 9832154be65SJarod Wilson ep_in = ep; 9842154be65SJarod Wilson } 9852154be65SJarod Wilson 9862154be65SJarod Wilson if ((ep_out == NULL) && 9872154be65SJarod Wilson ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) && 9882154be65SJarod Wilson ((attrs & USB_ENDPOINT_XFERTYPE_MASK) == 9892154be65SJarod Wilson USB_ENDPOINT_XFER_BULK)) { 9903228bf81SGreg Kroah-Hartman dev_dbg(dev, "found bulk-out endpoint at 0x%02x\n", 9912154be65SJarod Wilson ep->bEndpointAddress); 9922154be65SJarod Wilson ep_out = ep; 9932154be65SJarod Wilson } 9942154be65SJarod Wilson } 9952154be65SJarod Wilson 9962154be65SJarod Wilson if (!ep_in || !ep_out) { 9972154be65SJarod Wilson dev_err(dev, "Couldn't find both in and out endpoints\n"); 9982154be65SJarod Wilson retval = -ENODEV; 9992154be65SJarod Wilson goto no_endpoints; 10002154be65SJarod Wilson } 10012154be65SJarod Wilson 10022154be65SJarod Wilson /* allocate memory for our device state and initialize it */ 10032154be65SJarod Wilson rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL); 10042154be65SJarod Wilson if (rr3 == NULL) { 10052154be65SJarod Wilson dev_err(dev, "Memory allocation failure\n"); 10067eb75715SDan Carpenter goto no_endpoints; 10072154be65SJarod Wilson } 10082154be65SJarod Wilson 10092154be65SJarod Wilson rr3->dev = &intf->dev; 10102154be65SJarod Wilson 10112154be65SJarod Wilson /* set up bulk-in endpoint */ 10122154be65SJarod Wilson rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL); 1013cd859c19SWolfram Sang if (!rr3->read_urb) 10142154be65SJarod Wilson goto error; 10152154be65SJarod Wilson 10162154be65SJarod Wilson rr3->ep_in = ep_in; 1017fa7b9ac2SSean Young rr3->bulk_in_buf = usb_alloc_coherent(udev, 1018d0a0a65eSSean Young le16_to_cpu(ep_in->wMaxPacketSize), GFP_KERNEL, &rr3->dma_in); 10192154be65SJarod Wilson if (!rr3->bulk_in_buf) { 10202154be65SJarod Wilson dev_err(dev, "Read buffer allocation failure\n"); 10212154be65SJarod Wilson goto error; 10222154be65SJarod Wilson } 10232154be65SJarod Wilson 10242154be65SJarod Wilson pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress); 1025fa7b9ac2SSean Young usb_fill_bulk_urb(rr3->read_urb, udev, pipe, rr3->bulk_in_buf, 1026fa7b9ac2SSean Young le16_to_cpu(ep_in->wMaxPacketSize), redrat3_handle_async, rr3); 1027d0a0a65eSSean Young rr3->read_urb->transfer_dma = rr3->dma_in; 1028d0a0a65eSSean Young rr3->read_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 10292154be65SJarod Wilson 10302154be65SJarod Wilson rr3->ep_out = ep_out; 10312154be65SJarod Wilson rr3->udev = udev; 10322154be65SJarod Wilson 10332154be65SJarod Wilson redrat3_reset(rr3); 10342154be65SJarod Wilson redrat3_get_firmware_rev(rr3); 10352154be65SJarod Wilson 10362154be65SJarod Wilson /* might be all we need to do? */ 10372154be65SJarod Wilson retval = redrat3_enable_detector(rr3); 10382154be65SJarod Wilson if (retval < 0) 10392154be65SJarod Wilson goto error; 10402154be65SJarod Wilson 10412154be65SJarod Wilson /* default.. will get overridden by any sends with a freq defined */ 10422154be65SJarod Wilson rr3->carrier = 38000; 10432154be65SJarod Wilson 1044bf139726SSean Young /* led control */ 1045bf139726SSean Young rr3->led.name = "redrat3:red:feedback"; 1046bf139726SSean Young rr3->led.default_trigger = "rc-feedback"; 1047bf139726SSean Young rr3->led.brightness_set = redrat3_brightness_set; 1048bf139726SSean Young retval = led_classdev_register(&intf->dev, &rr3->led); 1049bf139726SSean Young if (retval) 1050bf139726SSean Young goto error; 1051bf139726SSean Young 1052bf139726SSean Young atomic_set(&rr3->flash, 0); 1053bf139726SSean Young rr3->flash_urb = usb_alloc_urb(0, GFP_KERNEL); 1054bf139726SSean Young if (!rr3->flash_urb) { 1055bf139726SSean Young retval = -ENOMEM; 1056bf139726SSean Young goto led_free_error; 1057bf139726SSean Young } 1058bf139726SSean Young 1059bf139726SSean Young /* setup packet is 'c0 b9 0000 0000 0001' */ 1060bf139726SSean Young rr3->flash_control.bRequestType = 0xc0; 1061bf139726SSean Young rr3->flash_control.bRequest = RR3_BLINK_LED; 1062bf139726SSean Young rr3->flash_control.wLength = cpu_to_le16(1); 1063bf139726SSean Young 1064bf139726SSean Young usb_fill_control_urb(rr3->flash_urb, udev, usb_rcvctrlpipe(udev, 0), 1065bf139726SSean Young (unsigned char *)&rr3->flash_control, 1066bf139726SSean Young &rr3->flash_in_buf, sizeof(rr3->flash_in_buf), 1067bf139726SSean Young redrat3_led_complete, rr3); 1068bf139726SSean Young 10692154be65SJarod Wilson rr3->rc = redrat3_init_rc_dev(rr3); 10706ea7cf76SPeter Senna Tschudin if (!rr3->rc) { 10716ea7cf76SPeter Senna Tschudin retval = -ENOMEM; 1072bf139726SSean Young goto led_free_error; 10736ea7cf76SPeter Senna Tschudin } 10742154be65SJarod Wilson 10752154be65SJarod Wilson /* we can register the device now, as it is ready */ 10762154be65SJarod Wilson usb_set_intfdata(intf, rr3); 10772154be65SJarod Wilson 10782154be65SJarod Wilson return 0; 10792154be65SJarod Wilson 1080bf139726SSean Young led_free_error: 1081bf139726SSean Young led_classdev_unregister(&rr3->led); 10822154be65SJarod Wilson error: 10832154be65SJarod Wilson redrat3_delete(rr3, rr3->udev); 10842154be65SJarod Wilson 10852154be65SJarod Wilson no_endpoints: 10862154be65SJarod Wilson dev_err(dev, "%s: retval = %x", __func__, retval); 10872154be65SJarod Wilson 10882154be65SJarod Wilson return retval; 10892154be65SJarod Wilson } 10902154be65SJarod Wilson 10914c62e976SGreg Kroah-Hartman static void redrat3_dev_disconnect(struct usb_interface *intf) 10922154be65SJarod Wilson { 10932154be65SJarod Wilson struct usb_device *udev = interface_to_usbdev(intf); 10942154be65SJarod Wilson struct redrat3_dev *rr3 = usb_get_intfdata(intf); 10952154be65SJarod Wilson 10962154be65SJarod Wilson if (!rr3) 10972154be65SJarod Wilson return; 10982154be65SJarod Wilson 10992154be65SJarod Wilson usb_set_intfdata(intf, NULL); 11002154be65SJarod Wilson rc_unregister_device(rr3->rc); 1101bf139726SSean Young led_classdev_unregister(&rr3->led); 11022154be65SJarod Wilson redrat3_delete(rr3, udev); 11032154be65SJarod Wilson } 11042154be65SJarod Wilson 11052154be65SJarod Wilson static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message) 11062154be65SJarod Wilson { 11072154be65SJarod Wilson struct redrat3_dev *rr3 = usb_get_intfdata(intf); 1108a36d5b61SGreg Kroah-Hartman 1109bf139726SSean Young led_classdev_suspend(&rr3->led); 11102154be65SJarod Wilson usb_kill_urb(rr3->read_urb); 1111bf139726SSean Young usb_kill_urb(rr3->flash_urb); 11122154be65SJarod Wilson return 0; 11132154be65SJarod Wilson } 11142154be65SJarod Wilson 11152154be65SJarod Wilson static int redrat3_dev_resume(struct usb_interface *intf) 11162154be65SJarod Wilson { 11172154be65SJarod Wilson struct redrat3_dev *rr3 = usb_get_intfdata(intf); 1118a36d5b61SGreg Kroah-Hartman 11192154be65SJarod Wilson if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC)) 11202154be65SJarod Wilson return -EIO; 1121bf139726SSean Young led_classdev_resume(&rr3->led); 11222154be65SJarod Wilson return 0; 11232154be65SJarod Wilson } 11242154be65SJarod Wilson 11252154be65SJarod Wilson static struct usb_driver redrat3_dev_driver = { 11262154be65SJarod Wilson .name = DRIVER_NAME, 11272154be65SJarod Wilson .probe = redrat3_dev_probe, 11284c62e976SGreg Kroah-Hartman .disconnect = redrat3_dev_disconnect, 11292154be65SJarod Wilson .suspend = redrat3_dev_suspend, 11302154be65SJarod Wilson .resume = redrat3_dev_resume, 11312154be65SJarod Wilson .reset_resume = redrat3_dev_resume, 11322154be65SJarod Wilson .id_table = redrat3_dev_table 11332154be65SJarod Wilson }; 11342154be65SJarod Wilson 1135ecb3b2b3SGreg Kroah-Hartman module_usb_driver(redrat3_dev_driver); 11362154be65SJarod Wilson 11372154be65SJarod Wilson MODULE_DESCRIPTION(DRIVER_DESC); 11382154be65SJarod Wilson MODULE_AUTHOR(DRIVER_AUTHOR); 11392154be65SJarod Wilson MODULE_AUTHOR(DRIVER_AUTHOR2); 11402154be65SJarod Wilson MODULE_LICENSE("GPL"); 11412154be65SJarod Wilson MODULE_DEVICE_TABLE(usb, redrat3_dev_table); 1142