Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1 | /* |
| 2 | * USB RedRat3 IR Transceiver rc-core driver |
| 3 | * |
| 4 | * Copyright (c) 2011 by Jarod Wilson <jarod@redhat.com> |
| 5 | * based heavily on the work of Stephen Cox, with additional |
| 6 | * help from RedRat Ltd. |
| 7 | * |
| 8 | * This driver began life based an an old version of the first-generation |
| 9 | * lirc_mceusb driver from the lirc 0.7.2 distribution. It was then |
| 10 | * significantly rewritten by Stephen Cox with the aid of RedRat Ltd's |
| 11 | * Chris Dodge. |
| 12 | * |
| 13 | * The driver was then ported to rc-core and significantly rewritten again, |
| 14 | * by Jarod, using the in-kernel mceusb driver as a guide, after an initial |
| 15 | * port effort was started by Stephen. |
| 16 | * |
| 17 | * TODO LIST: |
| 18 | * - fix lirc not showing repeats properly |
| 19 | * -- |
| 20 | * |
| 21 | * The RedRat3 is a USB transceiver with both send & receive, |
| 22 | * with 2 separate sensors available for receive to enable |
| 23 | * both good long range reception for general use, and good |
| 24 | * short range reception when required for learning a signal. |
| 25 | * |
| 26 | * http://www.redrat.co.uk/ |
| 27 | * |
| 28 | * It uses its own little protocol to communicate, the required |
| 29 | * parts of which are embedded within this driver. |
| 30 | * -- |
| 31 | * |
| 32 | * This program is free software; you can redistribute it and/or modify |
| 33 | * it under the terms of the GNU General Public License as published by |
| 34 | * the Free Software Foundation; either version 2 of the License, or |
| 35 | * (at your option) any later version. |
| 36 | * |
| 37 | * This program is distributed in the hope that it will be useful, |
| 38 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 39 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 40 | * GNU General Public License for more details. |
| 41 | * |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 42 | */ |
| 43 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 44 | #include <asm/unaligned.h> |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 45 | #include <linux/device.h> |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 46 | #include <linux/leds.h> |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 47 | #include <linux/module.h> |
| 48 | #include <linux/slab.h> |
| 49 | #include <linux/usb.h> |
| 50 | #include <linux/usb/input.h> |
| 51 | #include <media/rc-core.h> |
| 52 | |
| 53 | /* Driver Information */ |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 54 | #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>" |
| 55 | #define DRIVER_AUTHOR2 "The Dweller, Stephen Cox" |
| 56 | #define DRIVER_DESC "RedRat3 USB IR Transceiver Driver" |
| 57 | #define DRIVER_NAME "redrat3" |
| 58 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 59 | /* bulk data transfer types */ |
| 60 | #define RR3_ERROR 0x01 |
| 61 | #define RR3_MOD_SIGNAL_IN 0x20 |
| 62 | #define RR3_MOD_SIGNAL_OUT 0x21 |
| 63 | |
| 64 | /* Get the RR firmware version */ |
| 65 | #define RR3_FW_VERSION 0xb1 |
| 66 | #define RR3_FW_VERSION_LEN 64 |
| 67 | /* Send encoded signal bulk-sent earlier*/ |
| 68 | #define RR3_TX_SEND_SIGNAL 0xb3 |
| 69 | #define RR3_SET_IR_PARAM 0xb7 |
| 70 | #define RR3_GET_IR_PARAM 0xb8 |
| 71 | /* Blink the red LED on the device */ |
| 72 | #define RR3_BLINK_LED 0xb9 |
| 73 | /* Read serial number of device */ |
| 74 | #define RR3_READ_SER_NO 0xba |
| 75 | #define RR3_SER_NO_LEN 4 |
| 76 | /* Start capture with the RC receiver */ |
| 77 | #define RR3_RC_DET_ENABLE 0xbb |
| 78 | /* Stop capture with the RC receiver */ |
| 79 | #define RR3_RC_DET_DISABLE 0xbc |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 80 | /* Start capture with the wideband receiver */ |
| 81 | #define RR3_MODSIG_CAPTURE 0xb2 |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 82 | /* Return the status of RC detector capture */ |
| 83 | #define RR3_RC_DET_STATUS 0xbd |
| 84 | /* Reset redrat */ |
| 85 | #define RR3_RESET 0xa0 |
| 86 | |
| 87 | /* Max number of lengths in the signal. */ |
| 88 | #define RR3_IR_IO_MAX_LENGTHS 0x01 |
| 89 | /* Periods to measure mod. freq. */ |
| 90 | #define RR3_IR_IO_PERIODS_MF 0x02 |
| 91 | /* Size of memory for main signal data */ |
| 92 | #define RR3_IR_IO_SIG_MEM_SIZE 0x03 |
| 93 | /* Delta value when measuring lengths */ |
| 94 | #define RR3_IR_IO_LENGTH_FUZZ 0x04 |
| 95 | /* Timeout for end of signal detection */ |
| 96 | #define RR3_IR_IO_SIG_TIMEOUT 0x05 |
Jonathan McCrohan | 39c1cb2 | 2013-10-20 21:34:01 -0300 | [diff] [blame] | 97 | /* Minimum value for pause recognition. */ |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 98 | #define RR3_IR_IO_MIN_PAUSE 0x06 |
| 99 | |
| 100 | /* Clock freq. of EZ-USB chip */ |
| 101 | #define RR3_CLK 24000000 |
| 102 | /* Clock periods per timer count */ |
| 103 | #define RR3_CLK_PER_COUNT 12 |
| 104 | /* (RR3_CLK / RR3_CLK_PER_COUNT) */ |
| 105 | #define RR3_CLK_CONV_FACTOR 2000000 |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 106 | /* USB bulk-in wideband IR data endpoint address */ |
| 107 | #define RR3_WIDE_IN_EP_ADDR 0x81 |
| 108 | /* USB bulk-in narrowband IR data endpoint address */ |
| 109 | #define RR3_NARROW_IN_EP_ADDR 0x82 |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 110 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 111 | /* Size of the fixed-length portion of the signal */ |
Sean Young | a48c4bb | 2016-10-31 15:52:24 -0200 | [diff] [blame] | 112 | #define RR3_DRIVER_MAXLENS 255 |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 113 | #define RR3_MAX_SIG_SIZE 512 |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 114 | #define RR3_TIME_UNIT 50 |
| 115 | #define RR3_END_OF_SIGNAL 0x7f |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 116 | #define RR3_TX_TRAILER_LEN 2 |
| 117 | #define RR3_RX_MIN_TIMEOUT 5 |
| 118 | #define RR3_RX_MAX_TIMEOUT 2000 |
| 119 | |
| 120 | /* The 8051's CPUCS Register address */ |
| 121 | #define RR3_CPUCS_REG_ADDR 0x7f92 |
| 122 | |
| 123 | #define USB_RR3USB_VENDOR_ID 0x112a |
| 124 | #define USB_RR3USB_PRODUCT_ID 0x0001 |
| 125 | #define USB_RR3IIUSB_PRODUCT_ID 0x0005 |
| 126 | |
Sean Young | d6ae162 | 2016-07-20 14:03:50 -0300 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * The redrat3 encodes an IR signal as set of different lengths and a set |
| 130 | * of indices into those lengths. This sets how much two lengths must |
| 131 | * differ before they are considered distinct, the value is specified |
| 132 | * in microseconds. |
| 133 | * Default 5, value 0 to 127. |
| 134 | */ |
| 135 | static int length_fuzz = 5; |
| 136 | module_param(length_fuzz, uint, 0644); |
| 137 | MODULE_PARM_DESC(length_fuzz, "Length Fuzz (0-127)"); |
| 138 | |
| 139 | /* |
| 140 | * When receiving a continuous ir stream (for example when a user is |
| 141 | * holding a button down on a remote), this specifies the minimum size |
| 142 | * of a space when the redrat3 sends a irdata packet to the host. Specified |
| 143 | * in miliseconds. Default value 18ms. |
| 144 | * The value can be between 2 and 30 inclusive. |
| 145 | */ |
| 146 | static int minimum_pause = 18; |
| 147 | module_param(minimum_pause, uint, 0644); |
| 148 | MODULE_PARM_DESC(minimum_pause, "Minimum Pause in ms (2-30)"); |
| 149 | |
| 150 | /* |
| 151 | * The carrier frequency is measured during the first pulse of the IR |
| 152 | * signal. The larger the number of periods used To measure, the more |
| 153 | * accurate the result is likely to be, however some signals have short |
| 154 | * initial pulses, so in some case it may be necessary to reduce this value. |
| 155 | * Default 8, value 1 to 255. |
| 156 | */ |
| 157 | static int periods_measure_carrier = 8; |
| 158 | module_param(periods_measure_carrier, uint, 0644); |
| 159 | MODULE_PARM_DESC(periods_measure_carrier, "Number of Periods to Measure Carrier (1-255)"); |
| 160 | |
| 161 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 162 | struct redrat3_header { |
| 163 | __be16 length; |
| 164 | __be16 transfer_type; |
| 165 | } __packed; |
| 166 | |
| 167 | /* sending and receiving irdata */ |
| 168 | struct redrat3_irdata { |
| 169 | struct redrat3_header header; |
| 170 | __be32 pause; |
| 171 | __be16 mod_freq_count; |
| 172 | __be16 num_periods; |
| 173 | __u8 max_lengths; |
| 174 | __u8 no_lengths; |
| 175 | __be16 max_sig_size; |
| 176 | __be16 sig_size; |
| 177 | __u8 no_repeats; |
| 178 | __be16 lens[RR3_DRIVER_MAXLENS]; /* not aligned */ |
| 179 | __u8 sigdata[RR3_MAX_SIG_SIZE]; |
| 180 | } __packed; |
| 181 | |
| 182 | /* firmware errors */ |
| 183 | struct redrat3_error { |
| 184 | struct redrat3_header header; |
| 185 | __be16 fw_error; |
| 186 | } __packed; |
| 187 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 188 | /* table of devices that work with this driver */ |
| 189 | static struct usb_device_id redrat3_dev_table[] = { |
| 190 | /* Original version of the RedRat3 */ |
| 191 | {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)}, |
| 192 | /* Second Version/release of the RedRat3 - RetRat3-II */ |
| 193 | {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)}, |
| 194 | {} /* Terminating entry */ |
| 195 | }; |
| 196 | |
| 197 | /* Structure to hold all of our device specific stuff */ |
| 198 | struct redrat3_dev { |
| 199 | /* core device bits */ |
| 200 | struct rc_dev *rc; |
| 201 | struct device *dev; |
| 202 | |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 203 | /* led control */ |
| 204 | struct led_classdev led; |
| 205 | atomic_t flash; |
| 206 | struct usb_ctrlrequest flash_control; |
| 207 | struct urb *flash_urb; |
| 208 | u8 flash_in_buf; |
| 209 | |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 210 | /* learning */ |
| 211 | bool wideband; |
| 212 | struct usb_ctrlrequest learn_control; |
| 213 | struct urb *learn_urb; |
| 214 | u8 learn_buf; |
| 215 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 216 | /* save off the usb device pointer */ |
| 217 | struct usb_device *udev; |
| 218 | |
| 219 | /* the receive endpoint */ |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 220 | struct usb_endpoint_descriptor *ep_narrow; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 221 | /* the buffer to receive data */ |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 222 | void *bulk_in_buf; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 223 | /* urb used to read ir data */ |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 224 | struct urb *narrow_urb; |
| 225 | struct urb *wide_urb; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 226 | |
| 227 | /* the send endpoint */ |
| 228 | struct usb_endpoint_descriptor *ep_out; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 229 | |
| 230 | /* usb dma */ |
| 231 | dma_addr_t dma_in; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 232 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 233 | /* Is the device currently transmitting?*/ |
| 234 | bool transmitting; |
| 235 | |
| 236 | /* store for current packet */ |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 237 | struct redrat3_irdata irdata; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 238 | u16 bytes_read; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 239 | |
| 240 | u32 carrier; |
| 241 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 242 | char name[64]; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 243 | char phys[64]; |
| 244 | }; |
| 245 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 246 | static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code) |
| 247 | { |
| 248 | if (!rr3->transmitting && (code != 0x40)) |
| 249 | dev_info(rr3->dev, "fw error code 0x%02x: ", code); |
| 250 | |
| 251 | switch (code) { |
| 252 | case 0x00: |
| 253 | pr_cont("No Error\n"); |
| 254 | break; |
| 255 | |
| 256 | /* Codes 0x20 through 0x2f are IR Firmware Errors */ |
| 257 | case 0x20: |
Mauro Carvalho Chehab | 25ec587 | 2016-10-18 17:44:25 -0200 | [diff] [blame] | 258 | pr_cont("Initial signal pulse not long enough to measure carrier frequency\n"); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 259 | break; |
| 260 | case 0x21: |
| 261 | pr_cont("Not enough length values allocated for signal\n"); |
| 262 | break; |
| 263 | case 0x22: |
| 264 | pr_cont("Not enough memory allocated for signal data\n"); |
| 265 | break; |
| 266 | case 0x23: |
| 267 | pr_cont("Too many signal repeats\n"); |
| 268 | break; |
| 269 | case 0x28: |
Mauro Carvalho Chehab | 25ec587 | 2016-10-18 17:44:25 -0200 | [diff] [blame] | 270 | pr_cont("Insufficient memory available for IR signal data memory allocation\n"); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 271 | break; |
| 272 | case 0x29: |
Mauro Carvalho Chehab | 25ec587 | 2016-10-18 17:44:25 -0200 | [diff] [blame] | 273 | pr_cont("Insufficient memory available for IrDa signal data memory allocation\n"); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 274 | break; |
| 275 | |
| 276 | /* Codes 0x30 through 0x3f are USB Firmware Errors */ |
| 277 | case 0x30: |
Mauro Carvalho Chehab | 25ec587 | 2016-10-18 17:44:25 -0200 | [diff] [blame] | 278 | pr_cont("Insufficient memory available for bulk transfer structure\n"); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 279 | break; |
| 280 | |
| 281 | /* |
| 282 | * Other error codes... These are primarily errors that can occur in |
| 283 | * the control messages sent to the redrat |
| 284 | */ |
| 285 | case 0x40: |
| 286 | if (!rr3->transmitting) |
| 287 | pr_cont("Signal capture has been terminated\n"); |
| 288 | break; |
| 289 | case 0x41: |
Mauro Carvalho Chehab | 25ec587 | 2016-10-18 17:44:25 -0200 | [diff] [blame] | 290 | pr_cont("Attempt to set/get and unknown signal I/O algorithm parameter\n"); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 291 | break; |
| 292 | case 0x42: |
| 293 | pr_cont("Signal capture already started\n"); |
| 294 | break; |
| 295 | |
| 296 | default: |
| 297 | pr_cont("Unknown Error\n"); |
| 298 | break; |
| 299 | } |
| 300 | } |
| 301 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 302 | static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 303 | { |
| 304 | u32 mod_freq = 0; |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 305 | u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 306 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 307 | if (mod_freq_count != 0) |
| 308 | mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) / |
| 309 | (mod_freq_count * RR3_CLK_PER_COUNT); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 310 | |
| 311 | return mod_freq; |
| 312 | } |
| 313 | |
| 314 | /* this function scales down the figures for the same result... */ |
| 315 | static u32 redrat3_len_to_us(u32 length) |
| 316 | { |
| 317 | u32 biglen = length * 1000; |
| 318 | u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000; |
| 319 | u32 result = (u32) (biglen / divisor); |
| 320 | |
| 321 | /* don't allow zero lengths to go back, breaks lirc */ |
| 322 | return result ? result : 1; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * convert us back into redrat3 lengths |
| 327 | * |
| 328 | * length * 1000 length * 1000000 |
| 329 | * ------------- = ---------------- = micro |
| 330 | * rr3clk / 1000 rr3clk |
| 331 | |
| 332 | * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000 |
| 333 | * ----- = 4 ----- = 6 -------------- = len --------------------- |
| 334 | * 3 2 1000000 1000 |
| 335 | */ |
| 336 | static u32 redrat3_us_to_len(u32 microsec) |
| 337 | { |
| 338 | u32 result; |
| 339 | u32 divisor; |
| 340 | |
Mauro Carvalho Chehab | 95cf60a | 2015-06-05 10:25:24 -0300 | [diff] [blame] | 341 | microsec = (microsec > IR_MAX_DURATION) ? IR_MAX_DURATION : microsec; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 342 | divisor = (RR3_CLK_CONV_FACTOR / 1000); |
| 343 | result = (u32)(microsec * divisor) / 1000; |
| 344 | |
| 345 | /* don't allow zero lengths to go back, breaks lirc */ |
| 346 | return result ? result : 1; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 347 | } |
| 348 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 349 | static void redrat3_process_ir_data(struct redrat3_dev *rr3) |
| 350 | { |
| 351 | DEFINE_IR_RAW_EVENT(rawir); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 352 | struct device *dev; |
Sean Young | 25da661 | 2016-07-10 13:34:37 -0300 | [diff] [blame] | 353 | unsigned int i, sig_size, single_len, offset, val; |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 354 | u32 mod_freq; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 355 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 356 | dev = rr3->dev; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 357 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 358 | mod_freq = redrat3_val_to_mod_freq(&rr3->irdata); |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 359 | dev_dbg(dev, "Got mod_freq of %u\n", mod_freq); |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 360 | if (mod_freq && rr3->wideband) { |
| 361 | DEFINE_IR_RAW_EVENT(ev); |
| 362 | |
| 363 | ev.carrier_report = 1; |
| 364 | ev.carrier = mod_freq; |
| 365 | |
| 366 | ir_raw_event_store(rr3->rc, &ev); |
| 367 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 368 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 369 | /* process each rr3 encoded byte into an int */ |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 370 | sig_size = be16_to_cpu(rr3->irdata.sig_size); |
| 371 | for (i = 0; i < sig_size; i++) { |
| 372 | offset = rr3->irdata.sigdata[i]; |
| 373 | val = get_unaligned_be16(&rr3->irdata.lens[offset]); |
| 374 | single_len = redrat3_len_to_us(val); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 375 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 376 | /* we should always get pulse/space/pulse/space samples */ |
| 377 | if (i % 2) |
| 378 | rawir.pulse = false; |
| 379 | else |
| 380 | rawir.pulse = true; |
| 381 | |
| 382 | rawir.duration = US_TO_NS(single_len); |
Jarod Wilson | 2c594ff | 2011-07-13 18:26:06 -0300 | [diff] [blame] | 383 | /* cap the value to IR_MAX_DURATION */ |
Mauro Carvalho Chehab | 95cf60a | 2015-06-05 10:25:24 -0300 | [diff] [blame] | 384 | rawir.duration = (rawir.duration > IR_MAX_DURATION) ? |
| 385 | IR_MAX_DURATION : rawir.duration; |
Jarod Wilson | 2c594ff | 2011-07-13 18:26:06 -0300 | [diff] [blame] | 386 | |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 387 | dev_dbg(dev, "storing %s with duration %d (i: %d)\n", |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 388 | rawir.pulse ? "pulse" : "space", rawir.duration, i); |
| 389 | ir_raw_event_store_with_filter(rr3->rc, &rawir); |
| 390 | } |
| 391 | |
Sean Young | 25da661 | 2016-07-10 13:34:37 -0300 | [diff] [blame] | 392 | /* add a trailing space */ |
| 393 | rawir.pulse = false; |
| 394 | rawir.timeout = true; |
Sean Young | c7470ff | 2016-07-20 14:03:49 -0300 | [diff] [blame] | 395 | rawir.duration = rr3->rc->timeout; |
Sean Young | 25da661 | 2016-07-10 13:34:37 -0300 | [diff] [blame] | 396 | dev_dbg(dev, "storing trailing timeout with duration %d\n", |
| 397 | rawir.duration); |
| 398 | ir_raw_event_store_with_filter(rr3->rc, &rawir); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 399 | |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 400 | dev_dbg(dev, "calling ir_raw_event_handle\n"); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 401 | ir_raw_event_handle(rr3->rc); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /* Util fn to send rr3 cmds */ |
Mauro Carvalho Chehab | 6948524 | 2015-04-29 15:00:30 -0300 | [diff] [blame] | 405 | static int redrat3_send_cmd(int cmd, struct redrat3_dev *rr3) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 406 | { |
| 407 | struct usb_device *udev; |
| 408 | u8 *data; |
| 409 | int res; |
| 410 | |
| 411 | data = kzalloc(sizeof(u8), GFP_KERNEL); |
| 412 | if (!data) |
| 413 | return -ENOMEM; |
| 414 | |
| 415 | udev = rr3->udev; |
| 416 | res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd, |
| 417 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 418 | 0x0000, 0x0000, data, sizeof(u8), HZ * 10); |
| 419 | |
| 420 | if (res < 0) { |
| 421 | dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d", |
| 422 | __func__, res, *data); |
| 423 | res = -EIO; |
| 424 | } else |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 425 | res = data[0]; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 426 | |
| 427 | kfree(data); |
| 428 | |
| 429 | return res; |
| 430 | } |
| 431 | |
| 432 | /* Enables the long range detector and starts async receive */ |
| 433 | static int redrat3_enable_detector(struct redrat3_dev *rr3) |
| 434 | { |
| 435 | struct device *dev = rr3->dev; |
| 436 | u8 ret; |
| 437 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 438 | ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3); |
| 439 | if (ret != 0) |
| 440 | dev_dbg(dev, "%s: unexpected ret of %d\n", |
| 441 | __func__, ret); |
| 442 | |
| 443 | ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3); |
| 444 | if (ret != 1) { |
| 445 | dev_err(dev, "%s: detector status: %d, should be 1\n", |
| 446 | __func__, ret); |
| 447 | return -EIO; |
| 448 | } |
| 449 | |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 450 | ret = usb_submit_urb(rr3->narrow_urb, GFP_KERNEL); |
| 451 | if (ret) { |
| 452 | dev_err(rr3->dev, "narrow band urb failed: %d", ret); |
| 453 | return ret; |
| 454 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 455 | |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 456 | ret = usb_submit_urb(rr3->wide_urb, GFP_KERNEL); |
| 457 | if (ret) |
| 458 | dev_err(rr3->dev, "wide band urb failed: %d", ret); |
| 459 | |
| 460 | return ret; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 461 | } |
| 462 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 463 | static inline void redrat3_delete(struct redrat3_dev *rr3, |
| 464 | struct usb_device *udev) |
| 465 | { |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 466 | usb_kill_urb(rr3->narrow_urb); |
| 467 | usb_kill_urb(rr3->wide_urb); |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 468 | usb_kill_urb(rr3->flash_urb); |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 469 | usb_kill_urb(rr3->learn_urb); |
| 470 | usb_free_urb(rr3->narrow_urb); |
| 471 | usb_free_urb(rr3->wide_urb); |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 472 | usb_free_urb(rr3->flash_urb); |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 473 | usb_free_urb(rr3->learn_urb); |
| 474 | usb_free_coherent(udev, le16_to_cpu(rr3->ep_narrow->wMaxPacketSize), |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 475 | rr3->bulk_in_buf, rr3->dma_in); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 476 | |
| 477 | kfree(rr3); |
| 478 | } |
| 479 | |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 480 | static u32 redrat3_get_timeout(struct redrat3_dev *rr3) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 481 | { |
Sean Young | 801b69f | 2013-02-16 17:25:43 -0300 | [diff] [blame] | 482 | __be32 *tmp; |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 483 | u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */ |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 484 | int len, ret, pipe; |
| 485 | |
| 486 | len = sizeof(*tmp); |
| 487 | tmp = kzalloc(len, GFP_KERNEL); |
Markus Elfring | 559f64d | 2016-10-13 08:20:19 -0300 | [diff] [blame] | 488 | if (!tmp) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 489 | return timeout; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 490 | |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 491 | pipe = usb_rcvctrlpipe(rr3->udev, 0); |
| 492 | ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM, |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 493 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 494 | RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5); |
Sean Young | 801b69f | 2013-02-16 17:25:43 -0300 | [diff] [blame] | 495 | if (ret != len) |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 496 | dev_warn(rr3->dev, "Failed to read timeout from hardware\n"); |
Sean Young | 801b69f | 2013-02-16 17:25:43 -0300 | [diff] [blame] | 497 | else { |
| 498 | timeout = redrat3_len_to_us(be32_to_cpup(tmp)); |
| 499 | |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 500 | dev_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 501 | } |
| 502 | |
Sean Young | 801b69f | 2013-02-16 17:25:43 -0300 | [diff] [blame] | 503 | kfree(tmp); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 504 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 505 | return timeout; |
| 506 | } |
| 507 | |
Sean Young | 4f253ce | 2016-07-10 13:34:38 -0300 | [diff] [blame] | 508 | static int redrat3_set_timeout(struct rc_dev *rc_dev, unsigned int timeoutns) |
| 509 | { |
| 510 | struct redrat3_dev *rr3 = rc_dev->priv; |
| 511 | struct usb_device *udev = rr3->udev; |
| 512 | struct device *dev = rr3->dev; |
Hans Verkuil | b1cb50b | 2016-08-23 03:48:37 -0300 | [diff] [blame] | 513 | __be32 *timeout; |
Sean Young | 4f253ce | 2016-07-10 13:34:38 -0300 | [diff] [blame] | 514 | int ret; |
| 515 | |
| 516 | timeout = kmalloc(sizeof(*timeout), GFP_KERNEL); |
| 517 | if (!timeout) |
| 518 | return -ENOMEM; |
| 519 | |
| 520 | *timeout = cpu_to_be32(redrat3_us_to_len(timeoutns / 1000)); |
| 521 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), RR3_SET_IR_PARAM, |
| 522 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 523 | RR3_IR_IO_SIG_TIMEOUT, 0, timeout, sizeof(*timeout), |
| 524 | HZ * 25); |
| 525 | dev_dbg(dev, "set ir parm timeout %d ret 0x%02x\n", |
| 526 | be32_to_cpu(*timeout), ret); |
| 527 | |
Sean Young | c7470ff | 2016-07-20 14:03:49 -0300 | [diff] [blame] | 528 | if (ret == sizeof(*timeout)) |
Sean Young | 4f253ce | 2016-07-10 13:34:38 -0300 | [diff] [blame] | 529 | ret = 0; |
Sean Young | c7470ff | 2016-07-20 14:03:49 -0300 | [diff] [blame] | 530 | else if (ret >= 0) |
Sean Young | 4f253ce | 2016-07-10 13:34:38 -0300 | [diff] [blame] | 531 | ret = -EIO; |
| 532 | |
| 533 | kfree(timeout); |
| 534 | |
| 535 | return ret; |
| 536 | } |
| 537 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 538 | static void redrat3_reset(struct redrat3_dev *rr3) |
| 539 | { |
| 540 | struct usb_device *udev = rr3->udev; |
| 541 | struct device *dev = rr3->dev; |
| 542 | int rc, rxpipe, txpipe; |
| 543 | u8 *val; |
Markus Elfring | 576df63 | 2016-10-13 08:23:22 -0300 | [diff] [blame] | 544 | size_t const len = sizeof(*val); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 545 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 546 | rxpipe = usb_rcvctrlpipe(udev, 0); |
| 547 | txpipe = usb_sndctrlpipe(udev, 0); |
| 548 | |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 549 | val = kmalloc(len, GFP_KERNEL); |
Markus Elfring | 559f64d | 2016-10-13 08:20:19 -0300 | [diff] [blame] | 550 | if (!val) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 551 | return; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 552 | |
| 553 | *val = 0x01; |
| 554 | rc = usb_control_msg(udev, rxpipe, RR3_RESET, |
| 555 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 556 | RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25); |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 557 | dev_dbg(dev, "reset returned 0x%02x\n", rc); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 558 | |
Sean Young | d6ae162 | 2016-07-20 14:03:50 -0300 | [diff] [blame] | 559 | *val = length_fuzz; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 560 | rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, |
| 561 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 562 | RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25); |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 563 | dev_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 564 | |
Sean Young | d6ae162 | 2016-07-20 14:03:50 -0300 | [diff] [blame] | 565 | *val = (65536 - (minimum_pause * 2000)) / 256; |
| 566 | rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, |
| 567 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 568 | RR3_IR_IO_MIN_PAUSE, 0, val, len, HZ * 25); |
| 569 | dev_dbg(dev, "set ir parm min pause %d rc 0x%02x\n", *val, rc); |
| 570 | |
| 571 | *val = periods_measure_carrier; |
| 572 | rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, |
| 573 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 574 | RR3_IR_IO_PERIODS_MF, 0, val, len, HZ * 25); |
| 575 | dev_dbg(dev, "set ir parm periods measure carrier %d rc 0x%02x", *val, |
| 576 | rc); |
| 577 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 578 | *val = RR3_DRIVER_MAXLENS; |
| 579 | rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, |
| 580 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 581 | RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25); |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 582 | dev_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 583 | |
| 584 | kfree(val); |
| 585 | } |
| 586 | |
| 587 | static void redrat3_get_firmware_rev(struct redrat3_dev *rr3) |
| 588 | { |
Markus Elfring | 7aa20af | 2016-10-13 08:21:55 -0300 | [diff] [blame] | 589 | int rc; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 590 | char *buffer; |
| 591 | |
Markus Elfring | 0bebaa5 | 2016-10-13 03:35:57 -0300 | [diff] [blame] | 592 | buffer = kcalloc(RR3_FW_VERSION_LEN + 1, sizeof(*buffer), GFP_KERNEL); |
Markus Elfring | 559f64d | 2016-10-13 08:20:19 -0300 | [diff] [blame] | 593 | if (!buffer) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 594 | return; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 595 | |
| 596 | rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0), |
| 597 | RR3_FW_VERSION, |
| 598 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 599 | 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5); |
| 600 | |
| 601 | if (rc >= 0) |
| 602 | dev_info(rr3->dev, "Firmware rev: %s", buffer); |
| 603 | else |
| 604 | dev_err(rr3->dev, "Problem fetching firmware ID\n"); |
| 605 | |
| 606 | kfree(buffer); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 607 | } |
| 608 | |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 609 | static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 610 | { |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 611 | struct redrat3_header *header = rr3->bulk_in_buf; |
| 612 | unsigned pktlen, pkttype; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 613 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 614 | /* grab the Length and type of transfer */ |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 615 | pktlen = be16_to_cpu(header->length); |
| 616 | pkttype = be16_to_cpu(header->transfer_type); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 617 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 618 | if (pktlen > sizeof(rr3->irdata)) { |
| 619 | dev_warn(rr3->dev, "packet length %u too large\n", pktlen); |
| 620 | return; |
| 621 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 622 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 623 | switch (pkttype) { |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 624 | case RR3_ERROR: |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 625 | if (len >= sizeof(struct redrat3_error)) { |
| 626 | struct redrat3_error *error = rr3->bulk_in_buf; |
| 627 | unsigned fw_error = be16_to_cpu(error->fw_error); |
| 628 | redrat3_dump_fw_error(rr3, fw_error); |
| 629 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 630 | break; |
| 631 | |
| 632 | case RR3_MOD_SIGNAL_IN: |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 633 | memcpy(&rr3->irdata, rr3->bulk_in_buf, len); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 634 | rr3->bytes_read = len; |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 635 | dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 636 | rr3->bytes_read, pktlen); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 637 | break; |
| 638 | |
| 639 | default: |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 640 | dev_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n", |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 641 | pkttype, len, pktlen); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 642 | break; |
| 643 | } |
| 644 | } |
| 645 | |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 646 | static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 647 | { |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 648 | void *irdata = &rr3->irdata; |
| 649 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 650 | if (len + rr3->bytes_read > sizeof(rr3->irdata)) { |
| 651 | dev_warn(rr3->dev, "too much data for packet\n"); |
| 652 | rr3->bytes_read = 0; |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 657 | |
| 658 | rr3->bytes_read += len; |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 659 | dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read, |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 660 | be16_to_cpu(rr3->irdata.header.length)); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /* gather IR data from incoming urb, process it when we have enough */ |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 664 | static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 665 | { |
| 666 | struct device *dev = rr3->dev; |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 667 | unsigned pkttype; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 668 | int ret = 0; |
| 669 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 670 | if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) { |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 671 | redrat3_read_packet_start(rr3, len); |
| 672 | } else if (rr3->bytes_read != 0) { |
| 673 | redrat3_read_packet_continue(rr3, len); |
| 674 | } else if (rr3->bytes_read == 0) { |
| 675 | dev_err(dev, "error: no packet data read\n"); |
| 676 | ret = -ENODATA; |
| 677 | goto out; |
| 678 | } |
| 679 | |
Sean Young | 38e35a8 | 2013-07-30 19:00:00 -0300 | [diff] [blame] | 680 | if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length) + |
| 681 | sizeof(struct redrat3_header)) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 682 | /* we're still accumulating data */ |
| 683 | return 0; |
| 684 | |
| 685 | /* if we get here, we've got IR data to decode */ |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 686 | pkttype = be16_to_cpu(rr3->irdata.header.transfer_type); |
| 687 | if (pkttype == RR3_MOD_SIGNAL_IN) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 688 | redrat3_process_ir_data(rr3); |
| 689 | else |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 690 | dev_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n", |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 691 | pkttype); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 692 | |
| 693 | out: |
| 694 | rr3->bytes_read = 0; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 695 | return ret; |
| 696 | } |
| 697 | |
| 698 | /* callback function from USB when async USB request has completed */ |
Sean Young | 801b69f | 2013-02-16 17:25:43 -0300 | [diff] [blame] | 699 | static void redrat3_handle_async(struct urb *urb) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 700 | { |
Sean Young | d6aca6e | 2016-10-31 15:52:21 -0200 | [diff] [blame] | 701 | struct redrat3_dev *rr3 = urb->context; |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 702 | int ret; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 703 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 704 | switch (urb->status) { |
| 705 | case 0: |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 706 | ret = redrat3_get_ir_data(rr3, urb->actual_length); |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 707 | if (!ret && rr3->wideband && !rr3->learn_urb->hcpriv) { |
| 708 | ret = usb_submit_urb(rr3->learn_urb, GFP_ATOMIC); |
| 709 | if (ret) |
| 710 | dev_err(rr3->dev, "Failed to submit learning urb: %d", |
| 711 | ret); |
| 712 | } |
| 713 | |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 714 | if (!ret) { |
| 715 | /* no error, prepare to read more */ |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 716 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
| 717 | if (ret) |
| 718 | dev_err(rr3->dev, "Failed to resubmit urb: %d", |
| 719 | ret); |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 720 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 721 | break; |
| 722 | |
| 723 | case -ECONNRESET: |
| 724 | case -ENOENT: |
| 725 | case -ESHUTDOWN: |
| 726 | usb_unlink_urb(urb); |
| 727 | return; |
| 728 | |
| 729 | case -EPIPE: |
| 730 | default: |
| 731 | dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status); |
| 732 | rr3->bytes_read = 0; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 733 | break; |
| 734 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 735 | } |
| 736 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 737 | static u16 mod_freq_to_val(unsigned int mod_freq) |
| 738 | { |
| 739 | int mult = 6000000; |
| 740 | |
| 741 | /* Clk used in mod. freq. generation is CLK24/4. */ |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 742 | return 65536 - (mult / mod_freq); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 743 | } |
| 744 | |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 745 | static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 746 | { |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 747 | struct redrat3_dev *rr3 = rcdev->priv; |
| 748 | struct device *dev = rr3->dev; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 749 | |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 750 | dev_dbg(dev, "Setting modulation frequency to %u", carrier); |
Dan Carpenter | 48cafec | 2012-09-11 07:11:24 -0300 | [diff] [blame] | 751 | if (carrier == 0) |
| 752 | return -EINVAL; |
| 753 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 754 | rr3->carrier = carrier; |
| 755 | |
Sean Young | 14d8188 | 2016-07-10 13:34:33 -0300 | [diff] [blame] | 756 | return 0; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 757 | } |
| 758 | |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 759 | static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf, |
| 760 | unsigned count) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 761 | { |
| 762 | struct redrat3_dev *rr3 = rcdev->priv; |
| 763 | struct device *dev = rr3->dev; |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 764 | struct redrat3_irdata *irdata = NULL; |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 765 | int ret, ret_len; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 766 | int lencheck, cur_sample_len, pipe; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 767 | int *sample_lens = NULL; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 768 | u8 curlencheck = 0; |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 769 | unsigned i, sendbuf_len; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 770 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 771 | if (rr3->transmitting) { |
| 772 | dev_warn(dev, "%s: transmitter already in use\n", __func__); |
| 773 | return -EAGAIN; |
| 774 | } |
| 775 | |
Sean Young | 671ea67 | 2013-07-08 17:33:09 -0300 | [diff] [blame] | 776 | if (count > RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN) |
| 777 | return -EINVAL; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 778 | |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 779 | /* rr3 will disable rc detector on transmit */ |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 780 | rr3->transmitting = true; |
| 781 | |
Markus Elfring | 0bebaa5 | 2016-10-13 03:35:57 -0300 | [diff] [blame] | 782 | sample_lens = kcalloc(RR3_DRIVER_MAXLENS, |
| 783 | sizeof(*sample_lens), |
| 784 | GFP_KERNEL); |
Markus Elfring | fac5913 | 2016-10-13 05:34:29 -0300 | [diff] [blame] | 785 | if (!sample_lens) |
| 786 | return -ENOMEM; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 787 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 788 | irdata = kzalloc(sizeof(*irdata), GFP_KERNEL); |
| 789 | if (!irdata) { |
Sean Young | 801b69f | 2013-02-16 17:25:43 -0300 | [diff] [blame] | 790 | ret = -ENOMEM; |
| 791 | goto out; |
| 792 | } |
| 793 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 794 | for (i = 0; i < count; i++) { |
Sean Young | 06eae25 | 2013-01-29 08:19:31 -0300 | [diff] [blame] | 795 | cur_sample_len = redrat3_us_to_len(txbuf[i]); |
Sean Young | 801b69f | 2013-02-16 17:25:43 -0300 | [diff] [blame] | 796 | if (cur_sample_len > 0xffff) { |
| 797 | dev_warn(dev, "transmit period of %uus truncated to %uus\n", |
| 798 | txbuf[i], redrat3_len_to_us(0xffff)); |
| 799 | cur_sample_len = 0xffff; |
| 800 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 801 | for (lencheck = 0; lencheck < curlencheck; lencheck++) { |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 802 | if (sample_lens[lencheck] == cur_sample_len) |
| 803 | break; |
| 804 | } |
| 805 | if (lencheck == curlencheck) { |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 806 | dev_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n", |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 807 | i, txbuf[i], curlencheck, cur_sample_len); |
Sean Young | 06eae25 | 2013-01-29 08:19:31 -0300 | [diff] [blame] | 808 | if (curlencheck < RR3_DRIVER_MAXLENS) { |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 809 | /* now convert the value to a proper |
| 810 | * rr3 value.. */ |
| 811 | sample_lens[curlencheck] = cur_sample_len; |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 812 | put_unaligned_be16(cur_sample_len, |
| 813 | &irdata->lens[curlencheck]); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 814 | curlencheck++; |
| 815 | } else { |
Sean Young | 671ea67 | 2013-07-08 17:33:09 -0300 | [diff] [blame] | 816 | ret = -EINVAL; |
| 817 | goto out; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 818 | } |
| 819 | } |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 820 | irdata->sigdata[i] = lencheck; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 821 | } |
| 822 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 823 | irdata->sigdata[count] = RR3_END_OF_SIGNAL; |
| 824 | irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 825 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 826 | sendbuf_len = offsetof(struct redrat3_irdata, |
| 827 | sigdata[count + RR3_TX_TRAILER_LEN]); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 828 | /* fill in our packet header */ |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 829 | irdata->header.length = cpu_to_be16(sendbuf_len - |
| 830 | sizeof(struct redrat3_header)); |
| 831 | irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT); |
| 832 | irdata->pause = cpu_to_be32(redrat3_len_to_us(100)); |
| 833 | irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier)); |
| 834 | irdata->no_lengths = curlencheck; |
| 835 | irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 836 | |
| 837 | pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress); |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 838 | ret = usb_bulk_msg(rr3->udev, pipe, irdata, |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 839 | sendbuf_len, &ret_len, 10 * HZ); |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 840 | dev_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 841 | |
| 842 | /* now tell the hardware to transmit what we sent it */ |
| 843 | pipe = usb_rcvctrlpipe(rr3->udev, 0); |
| 844 | ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL, |
| 845 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 846 | 0, 0, irdata, 2, HZ * 10); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 847 | |
| 848 | if (ret < 0) |
| 849 | dev_err(dev, "Error: control msg send failed, rc %d\n", ret); |
| 850 | else |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 851 | ret = count; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 852 | |
| 853 | out: |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 854 | kfree(irdata); |
Markus Elfring | fac5913 | 2016-10-13 05:34:29 -0300 | [diff] [blame] | 855 | kfree(sample_lens); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 856 | |
| 857 | rr3->transmitting = false; |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 858 | /* rr3 re-enables rc detector because it was enabled before */ |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 859 | |
| 860 | return ret; |
| 861 | } |
| 862 | |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 863 | static void redrat3_brightness_set(struct led_classdev *led_dev, enum |
| 864 | led_brightness brightness) |
| 865 | { |
| 866 | struct redrat3_dev *rr3 = container_of(led_dev, struct redrat3_dev, |
| 867 | led); |
| 868 | |
| 869 | if (brightness != LED_OFF && atomic_cmpxchg(&rr3->flash, 0, 1) == 0) { |
| 870 | int ret = usb_submit_urb(rr3->flash_urb, GFP_ATOMIC); |
| 871 | if (ret != 0) { |
| 872 | dev_dbg(rr3->dev, "%s: unexpected ret of %d\n", |
| 873 | __func__, ret); |
| 874 | atomic_set(&rr3->flash, 0); |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 879 | static int redrat3_wideband_receiver(struct rc_dev *rcdev, int enable) |
| 880 | { |
| 881 | struct redrat3_dev *rr3 = rcdev->priv; |
| 882 | int ret = 0; |
| 883 | |
| 884 | rr3->wideband = enable != 0; |
| 885 | |
| 886 | if (enable) { |
| 887 | ret = usb_submit_urb(rr3->learn_urb, GFP_KERNEL); |
| 888 | if (ret) |
| 889 | dev_err(rr3->dev, "Failed to submit learning urb: %d", |
| 890 | ret); |
| 891 | } |
| 892 | |
| 893 | return ret; |
| 894 | } |
| 895 | |
| 896 | static void redrat3_learn_complete(struct urb *urb) |
| 897 | { |
| 898 | struct redrat3_dev *rr3 = urb->context; |
| 899 | |
| 900 | switch (urb->status) { |
| 901 | case 0: |
| 902 | break; |
| 903 | case -ECONNRESET: |
| 904 | case -ENOENT: |
| 905 | case -ESHUTDOWN: |
| 906 | usb_unlink_urb(urb); |
| 907 | return; |
| 908 | case -EPIPE: |
| 909 | default: |
| 910 | dev_err(rr3->dev, "Error: learn urb status = %d", urb->status); |
| 911 | break; |
| 912 | } |
| 913 | } |
| 914 | |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 915 | static void redrat3_led_complete(struct urb *urb) |
| 916 | { |
| 917 | struct redrat3_dev *rr3 = urb->context; |
| 918 | |
| 919 | switch (urb->status) { |
| 920 | case 0: |
| 921 | break; |
| 922 | case -ECONNRESET: |
| 923 | case -ENOENT: |
| 924 | case -ESHUTDOWN: |
| 925 | usb_unlink_urb(urb); |
| 926 | return; |
| 927 | case -EPIPE: |
| 928 | default: |
| 929 | dev_dbg(rr3->dev, "Error: urb status = %d\n", urb->status); |
| 930 | break; |
| 931 | } |
| 932 | |
| 933 | rr3->led.brightness = LED_OFF; |
| 934 | atomic_dec(&rr3->flash); |
| 935 | } |
| 936 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 937 | static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3) |
| 938 | { |
| 939 | struct device *dev = rr3->dev; |
| 940 | struct rc_dev *rc; |
Markus Elfring | 9d45d5f | 2016-10-13 09:17:43 -0300 | [diff] [blame] | 941 | int ret; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 942 | u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct); |
| 943 | |
| 944 | rc = rc_allocate_device(); |
Markus Elfring | 559f64d | 2016-10-13 08:20:19 -0300 | [diff] [blame] | 945 | if (!rc) |
Markus Elfring | 3f81678 | 2016-10-13 09:54:46 -0300 | [diff] [blame] | 946 | return NULL; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 947 | |
Sean Young | da50003 | 2016-10-31 15:52:20 -0200 | [diff] [blame] | 948 | snprintf(rr3->name, sizeof(rr3->name), |
| 949 | "RedRat3%s Infrared Remote Transceiver", |
| 950 | prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : ""); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 951 | |
| 952 | usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys)); |
| 953 | |
| 954 | rc->input_name = rr3->name; |
| 955 | rc->input_phys = rr3->phys; |
| 956 | usb_to_input_id(rr3->udev, &rc->input_id); |
| 957 | rc->dev.parent = dev; |
| 958 | rc->priv = rr3; |
| 959 | rc->driver_type = RC_DRIVER_IR_RAW; |
David Härdeman | c5540fb | 2014-04-03 20:32:21 -0300 | [diff] [blame] | 960 | rc->allowed_protocols = RC_BIT_ALL; |
Sean Young | 4f253ce | 2016-07-10 13:34:38 -0300 | [diff] [blame] | 961 | rc->min_timeout = MS_TO_NS(RR3_RX_MIN_TIMEOUT); |
| 962 | rc->max_timeout = MS_TO_NS(RR3_RX_MAX_TIMEOUT); |
Sean Young | c7470ff | 2016-07-20 14:03:49 -0300 | [diff] [blame] | 963 | rc->timeout = US_TO_NS(redrat3_get_timeout(rr3)); |
Sean Young | 4f253ce | 2016-07-10 13:34:38 -0300 | [diff] [blame] | 964 | rc->s_timeout = redrat3_set_timeout; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 965 | rc->tx_ir = redrat3_transmit_ir; |
| 966 | rc->s_tx_carrier = redrat3_set_tx_carrier; |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 967 | rc->s_carrier_report = redrat3_wideband_receiver; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 968 | rc->driver_name = DRIVER_NAME; |
Sean Young | 06eae25 | 2013-01-29 08:19:31 -0300 | [diff] [blame] | 969 | rc->rx_resolution = US_TO_NS(2); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 970 | rc->map_name = RC_MAP_HAUPPAUGE; |
| 971 | |
| 972 | ret = rc_register_device(rc); |
| 973 | if (ret < 0) { |
| 974 | dev_err(dev, "remote dev registration failed\n"); |
| 975 | goto out; |
| 976 | } |
| 977 | |
| 978 | return rc; |
| 979 | |
| 980 | out: |
| 981 | rc_free_device(rc); |
| 982 | return NULL; |
| 983 | } |
| 984 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 985 | static int redrat3_dev_probe(struct usb_interface *intf, |
| 986 | const struct usb_device_id *id) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 987 | { |
| 988 | struct usb_device *udev = interface_to_usbdev(intf); |
| 989 | struct device *dev = &intf->dev; |
| 990 | struct usb_host_interface *uhi; |
| 991 | struct redrat3_dev *rr3; |
| 992 | struct usb_endpoint_descriptor *ep; |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 993 | struct usb_endpoint_descriptor *ep_narrow = NULL; |
| 994 | struct usb_endpoint_descriptor *ep_wide = NULL; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 995 | struct usb_endpoint_descriptor *ep_out = NULL; |
| 996 | u8 addr, attrs; |
| 997 | int pipe, i; |
| 998 | int retval = -ENOMEM; |
| 999 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1000 | uhi = intf->cur_altsetting; |
| 1001 | |
| 1002 | /* find our bulk-in and bulk-out endpoints */ |
| 1003 | for (i = 0; i < uhi->desc.bNumEndpoints; ++i) { |
| 1004 | ep = &uhi->endpoint[i].desc; |
| 1005 | addr = ep->bEndpointAddress; |
| 1006 | attrs = ep->bmAttributes; |
| 1007 | |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 1008 | if (((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) && |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1009 | ((attrs & USB_ENDPOINT_XFERTYPE_MASK) == |
| 1010 | USB_ENDPOINT_XFER_BULK)) { |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 1011 | dev_dbg(dev, "found bulk-in endpoint at 0x%02x\n", |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1012 | ep->bEndpointAddress); |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 1013 | /* data comes in on 0x82, 0x81 is for learning */ |
| 1014 | if (ep->bEndpointAddress == RR3_NARROW_IN_EP_ADDR) |
| 1015 | ep_narrow = ep; |
| 1016 | if (ep->bEndpointAddress == RR3_WIDE_IN_EP_ADDR) |
| 1017 | ep_wide = ep; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | if ((ep_out == NULL) && |
| 1021 | ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) && |
| 1022 | ((attrs & USB_ENDPOINT_XFERTYPE_MASK) == |
| 1023 | USB_ENDPOINT_XFER_BULK)) { |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 1024 | dev_dbg(dev, "found bulk-out endpoint at 0x%02x\n", |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1025 | ep->bEndpointAddress); |
| 1026 | ep_out = ep; |
| 1027 | } |
| 1028 | } |
| 1029 | |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 1030 | if (!ep_narrow || !ep_out || !ep_wide) { |
| 1031 | dev_err(dev, "Couldn't find all endpoints\n"); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1032 | retval = -ENODEV; |
| 1033 | goto no_endpoints; |
| 1034 | } |
| 1035 | |
| 1036 | /* allocate memory for our device state and initialize it */ |
| 1037 | rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL); |
Markus Elfring | 559f64d | 2016-10-13 08:20:19 -0300 | [diff] [blame] | 1038 | if (!rr3) |
Dan Carpenter | 7eb7571 | 2011-05-26 05:55:08 -0300 | [diff] [blame] | 1039 | goto no_endpoints; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1040 | |
| 1041 | rr3->dev = &intf->dev; |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 1042 | rr3->ep_narrow = ep_narrow; |
Sean Young | 8a21ec9 | 2016-10-31 15:52:22 -0200 | [diff] [blame] | 1043 | rr3->ep_out = ep_out; |
| 1044 | rr3->udev = udev; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1045 | |
| 1046 | /* set up bulk-in endpoint */ |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 1047 | rr3->narrow_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1048 | if (!rr3->narrow_urb) |
| 1049 | goto redrat_free; |
| 1050 | |
| 1051 | rr3->wide_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1052 | if (!rr3->wide_urb) |
Sean Young | 8a21ec9 | 2016-10-31 15:52:22 -0200 | [diff] [blame] | 1053 | goto redrat_free; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1054 | |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 1055 | rr3->bulk_in_buf = usb_alloc_coherent(udev, |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 1056 | le16_to_cpu(ep_narrow->wMaxPacketSize), |
| 1057 | GFP_KERNEL, &rr3->dma_in); |
Markus Elfring | 559f64d | 2016-10-13 08:20:19 -0300 | [diff] [blame] | 1058 | if (!rr3->bulk_in_buf) |
Sean Young | 8a21ec9 | 2016-10-31 15:52:22 -0200 | [diff] [blame] | 1059 | goto redrat_free; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1060 | |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 1061 | pipe = usb_rcvbulkpipe(udev, ep_narrow->bEndpointAddress); |
| 1062 | usb_fill_bulk_urb(rr3->narrow_urb, udev, pipe, rr3->bulk_in_buf, |
| 1063 | le16_to_cpu(ep_narrow->wMaxPacketSize), |
| 1064 | redrat3_handle_async, rr3); |
| 1065 | rr3->narrow_urb->transfer_dma = rr3->dma_in; |
| 1066 | rr3->narrow_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 1067 | |
| 1068 | pipe = usb_rcvbulkpipe(udev, ep_wide->bEndpointAddress); |
| 1069 | usb_fill_bulk_urb(rr3->wide_urb, udev, pipe, rr3->bulk_in_buf, |
| 1070 | le16_to_cpu(ep_narrow->wMaxPacketSize), |
| 1071 | redrat3_handle_async, rr3); |
| 1072 | rr3->wide_urb->transfer_dma = rr3->dma_in; |
| 1073 | rr3->wide_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1074 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1075 | redrat3_reset(rr3); |
| 1076 | redrat3_get_firmware_rev(rr3); |
| 1077 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1078 | /* default.. will get overridden by any sends with a freq defined */ |
| 1079 | rr3->carrier = 38000; |
| 1080 | |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 1081 | atomic_set(&rr3->flash, 0); |
| 1082 | rr3->flash_urb = usb_alloc_urb(0, GFP_KERNEL); |
Sean Young | 8a21ec9 | 2016-10-31 15:52:22 -0200 | [diff] [blame] | 1083 | if (!rr3->flash_urb) |
| 1084 | goto redrat_free; |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 1085 | |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 1086 | /* learn urb */ |
| 1087 | rr3->learn_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1088 | if (!rr3->learn_urb) |
| 1089 | goto redrat_free; |
| 1090 | |
| 1091 | /* setup packet is 'c0 b2 0000 0000 0001' */ |
| 1092 | rr3->learn_control.bRequestType = 0xc0; |
| 1093 | rr3->learn_control.bRequest = RR3_MODSIG_CAPTURE; |
| 1094 | rr3->learn_control.wLength = cpu_to_le16(1); |
| 1095 | |
| 1096 | usb_fill_control_urb(rr3->learn_urb, udev, usb_rcvctrlpipe(udev, 0), |
| 1097 | (unsigned char *)&rr3->learn_control, |
| 1098 | &rr3->learn_buf, sizeof(rr3->learn_buf), |
| 1099 | redrat3_learn_complete, rr3); |
| 1100 | |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 1101 | /* setup packet is 'c0 b9 0000 0000 0001' */ |
| 1102 | rr3->flash_control.bRequestType = 0xc0; |
| 1103 | rr3->flash_control.bRequest = RR3_BLINK_LED; |
| 1104 | rr3->flash_control.wLength = cpu_to_le16(1); |
| 1105 | |
| 1106 | usb_fill_control_urb(rr3->flash_urb, udev, usb_rcvctrlpipe(udev, 0), |
| 1107 | (unsigned char *)&rr3->flash_control, |
| 1108 | &rr3->flash_in_buf, sizeof(rr3->flash_in_buf), |
| 1109 | redrat3_led_complete, rr3); |
| 1110 | |
Sean Young | 8a21ec9 | 2016-10-31 15:52:22 -0200 | [diff] [blame] | 1111 | /* led control */ |
| 1112 | rr3->led.name = "redrat3:red:feedback"; |
| 1113 | rr3->led.default_trigger = "rc-feedback"; |
| 1114 | rr3->led.brightness_set = redrat3_brightness_set; |
| 1115 | retval = led_classdev_register(&intf->dev, &rr3->led); |
| 1116 | if (retval) |
| 1117 | goto redrat_free; |
| 1118 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1119 | rr3->rc = redrat3_init_rc_dev(rr3); |
Peter Senna Tschudin | 6ea7cf7 | 2012-09-04 08:05:05 -0300 | [diff] [blame] | 1120 | if (!rr3->rc) { |
| 1121 | retval = -ENOMEM; |
Sean Young | 8a21ec9 | 2016-10-31 15:52:22 -0200 | [diff] [blame] | 1122 | goto led_free; |
Peter Senna Tschudin | 6ea7cf7 | 2012-09-04 08:05:05 -0300 | [diff] [blame] | 1123 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1124 | |
Sean Young | 8a21ec9 | 2016-10-31 15:52:22 -0200 | [diff] [blame] | 1125 | /* might be all we need to do? */ |
| 1126 | retval = redrat3_enable_detector(rr3); |
| 1127 | if (retval < 0) |
| 1128 | goto led_free; |
| 1129 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1130 | /* we can register the device now, as it is ready */ |
| 1131 | usb_set_intfdata(intf, rr3); |
| 1132 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1133 | return 0; |
| 1134 | |
Sean Young | 8a21ec9 | 2016-10-31 15:52:22 -0200 | [diff] [blame] | 1135 | led_free: |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 1136 | led_classdev_unregister(&rr3->led); |
Sean Young | 8a21ec9 | 2016-10-31 15:52:22 -0200 | [diff] [blame] | 1137 | redrat_free: |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1138 | redrat3_delete(rr3, rr3->udev); |
| 1139 | |
| 1140 | no_endpoints: |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1141 | return retval; |
| 1142 | } |
| 1143 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 1144 | static void redrat3_dev_disconnect(struct usb_interface *intf) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1145 | { |
| 1146 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1147 | struct redrat3_dev *rr3 = usb_get_intfdata(intf); |
| 1148 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1149 | usb_set_intfdata(intf, NULL); |
| 1150 | rc_unregister_device(rr3->rc); |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 1151 | led_classdev_unregister(&rr3->led); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1152 | redrat3_delete(rr3, udev); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message) |
| 1156 | { |
| 1157 | struct redrat3_dev *rr3 = usb_get_intfdata(intf); |
Greg Kroah-Hartman | a36d5b6 | 2014-05-28 11:34:00 -0700 | [diff] [blame] | 1158 | |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 1159 | led_classdev_suspend(&rr3->led); |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 1160 | usb_kill_urb(rr3->narrow_urb); |
| 1161 | usb_kill_urb(rr3->wide_urb); |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 1162 | usb_kill_urb(rr3->flash_urb); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1163 | return 0; |
| 1164 | } |
| 1165 | |
| 1166 | static int redrat3_dev_resume(struct usb_interface *intf) |
| 1167 | { |
| 1168 | struct redrat3_dev *rr3 = usb_get_intfdata(intf); |
Greg Kroah-Hartman | a36d5b6 | 2014-05-28 11:34:00 -0700 | [diff] [blame] | 1169 | |
Sean Young | c49fcdd | 2016-10-31 19:13:11 -0200 | [diff] [blame] | 1170 | if (usb_submit_urb(rr3->narrow_urb, GFP_ATOMIC)) |
| 1171 | return -EIO; |
| 1172 | if (usb_submit_urb(rr3->wide_urb, GFP_ATOMIC)) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1173 | return -EIO; |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 1174 | led_classdev_resume(&rr3->led); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | static struct usb_driver redrat3_dev_driver = { |
| 1179 | .name = DRIVER_NAME, |
| 1180 | .probe = redrat3_dev_probe, |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 1181 | .disconnect = redrat3_dev_disconnect, |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1182 | .suspend = redrat3_dev_suspend, |
| 1183 | .resume = redrat3_dev_resume, |
| 1184 | .reset_resume = redrat3_dev_resume, |
| 1185 | .id_table = redrat3_dev_table |
| 1186 | }; |
| 1187 | |
Greg Kroah-Hartman | ecb3b2b | 2011-11-18 09:46:12 -0800 | [diff] [blame] | 1188 | module_usb_driver(redrat3_dev_driver); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1189 | |
| 1190 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 1191 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1192 | MODULE_AUTHOR(DRIVER_AUTHOR2); |
| 1193 | MODULE_LICENSE("GPL"); |
| 1194 | MODULE_DEVICE_TABLE(usb, redrat3_dev_table); |