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