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