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 | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 127 | struct redrat3_header { |
| 128 | __be16 length; |
| 129 | __be16 transfer_type; |
| 130 | } __packed; |
| 131 | |
| 132 | /* sending and receiving irdata */ |
| 133 | struct redrat3_irdata { |
| 134 | struct redrat3_header header; |
| 135 | __be32 pause; |
| 136 | __be16 mod_freq_count; |
| 137 | __be16 num_periods; |
| 138 | __u8 max_lengths; |
| 139 | __u8 no_lengths; |
| 140 | __be16 max_sig_size; |
| 141 | __be16 sig_size; |
| 142 | __u8 no_repeats; |
| 143 | __be16 lens[RR3_DRIVER_MAXLENS]; /* not aligned */ |
| 144 | __u8 sigdata[RR3_MAX_SIG_SIZE]; |
| 145 | } __packed; |
| 146 | |
| 147 | /* firmware errors */ |
| 148 | struct redrat3_error { |
| 149 | struct redrat3_header header; |
| 150 | __be16 fw_error; |
| 151 | } __packed; |
| 152 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 153 | /* table of devices that work with this driver */ |
| 154 | static struct usb_device_id redrat3_dev_table[] = { |
| 155 | /* Original version of the RedRat3 */ |
| 156 | {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)}, |
| 157 | /* Second Version/release of the RedRat3 - RetRat3-II */ |
| 158 | {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)}, |
| 159 | {} /* Terminating entry */ |
| 160 | }; |
| 161 | |
| 162 | /* Structure to hold all of our device specific stuff */ |
| 163 | struct redrat3_dev { |
| 164 | /* core device bits */ |
| 165 | struct rc_dev *rc; |
| 166 | struct device *dev; |
| 167 | |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 168 | /* led control */ |
| 169 | struct led_classdev led; |
| 170 | atomic_t flash; |
| 171 | struct usb_ctrlrequest flash_control; |
| 172 | struct urb *flash_urb; |
| 173 | u8 flash_in_buf; |
| 174 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 175 | /* save off the usb device pointer */ |
| 176 | struct usb_device *udev; |
| 177 | |
| 178 | /* the receive endpoint */ |
| 179 | struct usb_endpoint_descriptor *ep_in; |
| 180 | /* the buffer to receive data */ |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 181 | void *bulk_in_buf; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 182 | /* urb used to read ir data */ |
| 183 | struct urb *read_urb; |
| 184 | |
| 185 | /* the send endpoint */ |
| 186 | struct usb_endpoint_descriptor *ep_out; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 187 | |
| 188 | /* usb dma */ |
| 189 | dma_addr_t dma_in; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 190 | |
Sean Young | 25da661 | 2016-07-10 13:34:37 -0300 | [diff] [blame^] | 191 | /* rx signal timeout */ |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 192 | u32 hw_timeout; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 193 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 194 | /* Is the device currently transmitting?*/ |
| 195 | bool transmitting; |
| 196 | |
| 197 | /* store for current packet */ |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 198 | struct redrat3_irdata irdata; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 199 | u16 bytes_read; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 200 | |
| 201 | u32 carrier; |
| 202 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 203 | char name[64]; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 204 | char phys[64]; |
| 205 | }; |
| 206 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 207 | /* |
| 208 | * redrat3_issue_async |
| 209 | * |
| 210 | * Issues an async read to the ir data in port.. |
| 211 | * sets the callback to be redrat3_handle_async |
| 212 | */ |
| 213 | static void redrat3_issue_async(struct redrat3_dev *rr3) |
| 214 | { |
| 215 | int res; |
| 216 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 217 | res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC); |
| 218 | if (res) |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 219 | dev_dbg(rr3->dev, |
| 220 | "%s: receive request FAILED! (res %d, len %d)\n", |
| 221 | __func__, res, rr3->read_urb->transfer_buffer_length); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code) |
| 225 | { |
| 226 | if (!rr3->transmitting && (code != 0x40)) |
| 227 | dev_info(rr3->dev, "fw error code 0x%02x: ", code); |
| 228 | |
| 229 | switch (code) { |
| 230 | case 0x00: |
| 231 | pr_cont("No Error\n"); |
| 232 | break; |
| 233 | |
| 234 | /* Codes 0x20 through 0x2f are IR Firmware Errors */ |
| 235 | case 0x20: |
| 236 | pr_cont("Initial signal pulse not long enough " |
| 237 | "to measure carrier frequency\n"); |
| 238 | break; |
| 239 | case 0x21: |
| 240 | pr_cont("Not enough length values allocated for signal\n"); |
| 241 | break; |
| 242 | case 0x22: |
| 243 | pr_cont("Not enough memory allocated for signal data\n"); |
| 244 | break; |
| 245 | case 0x23: |
| 246 | pr_cont("Too many signal repeats\n"); |
| 247 | break; |
| 248 | case 0x28: |
| 249 | pr_cont("Insufficient memory available for IR signal " |
| 250 | "data memory allocation\n"); |
| 251 | break; |
| 252 | case 0x29: |
| 253 | pr_cont("Insufficient memory available " |
| 254 | "for IrDa signal data memory allocation\n"); |
| 255 | break; |
| 256 | |
| 257 | /* Codes 0x30 through 0x3f are USB Firmware Errors */ |
| 258 | case 0x30: |
| 259 | pr_cont("Insufficient memory available for bulk " |
| 260 | "transfer structure\n"); |
| 261 | break; |
| 262 | |
| 263 | /* |
| 264 | * Other error codes... These are primarily errors that can occur in |
| 265 | * the control messages sent to the redrat |
| 266 | */ |
| 267 | case 0x40: |
| 268 | if (!rr3->transmitting) |
| 269 | pr_cont("Signal capture has been terminated\n"); |
| 270 | break; |
| 271 | case 0x41: |
| 272 | pr_cont("Attempt to set/get and unknown signal I/O " |
| 273 | "algorithm parameter\n"); |
| 274 | break; |
| 275 | case 0x42: |
| 276 | pr_cont("Signal capture already started\n"); |
| 277 | break; |
| 278 | |
| 279 | default: |
| 280 | pr_cont("Unknown Error\n"); |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 285 | static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 286 | { |
| 287 | u32 mod_freq = 0; |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 288 | u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 289 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 290 | if (mod_freq_count != 0) |
| 291 | mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) / |
| 292 | (mod_freq_count * RR3_CLK_PER_COUNT); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 293 | |
| 294 | return mod_freq; |
| 295 | } |
| 296 | |
| 297 | /* this function scales down the figures for the same result... */ |
| 298 | static u32 redrat3_len_to_us(u32 length) |
| 299 | { |
| 300 | u32 biglen = length * 1000; |
| 301 | u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000; |
| 302 | u32 result = (u32) (biglen / divisor); |
| 303 | |
| 304 | /* don't allow zero lengths to go back, breaks lirc */ |
| 305 | return result ? result : 1; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * convert us back into redrat3 lengths |
| 310 | * |
| 311 | * length * 1000 length * 1000000 |
| 312 | * ------------- = ---------------- = micro |
| 313 | * rr3clk / 1000 rr3clk |
| 314 | |
| 315 | * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000 |
| 316 | * ----- = 4 ----- = 6 -------------- = len --------------------- |
| 317 | * 3 2 1000000 1000 |
| 318 | */ |
| 319 | static u32 redrat3_us_to_len(u32 microsec) |
| 320 | { |
| 321 | u32 result; |
| 322 | u32 divisor; |
| 323 | |
Mauro Carvalho Chehab | 95cf60a | 2015-06-05 10:25:24 -0300 | [diff] [blame] | 324 | microsec = (microsec > IR_MAX_DURATION) ? IR_MAX_DURATION : microsec; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 325 | divisor = (RR3_CLK_CONV_FACTOR / 1000); |
| 326 | result = (u32)(microsec * divisor) / 1000; |
| 327 | |
| 328 | /* don't allow zero lengths to go back, breaks lirc */ |
| 329 | return result ? result : 1; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 330 | } |
| 331 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 332 | static void redrat3_process_ir_data(struct redrat3_dev *rr3) |
| 333 | { |
| 334 | DEFINE_IR_RAW_EVENT(rawir); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 335 | struct device *dev; |
Sean Young | 25da661 | 2016-07-10 13:34:37 -0300 | [diff] [blame^] | 336 | unsigned int i, sig_size, single_len, offset, val; |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 337 | u32 mod_freq; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 338 | |
| 339 | if (!rr3) { |
| 340 | pr_err("%s called with no context!\n", __func__); |
| 341 | return; |
| 342 | } |
| 343 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 344 | dev = rr3->dev; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 345 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 346 | mod_freq = redrat3_val_to_mod_freq(&rr3->irdata); |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 347 | dev_dbg(dev, "Got mod_freq of %u\n", mod_freq); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 348 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 349 | /* process each rr3 encoded byte into an int */ |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 350 | sig_size = be16_to_cpu(rr3->irdata.sig_size); |
| 351 | for (i = 0; i < sig_size; i++) { |
| 352 | offset = rr3->irdata.sigdata[i]; |
| 353 | val = get_unaligned_be16(&rr3->irdata.lens[offset]); |
| 354 | single_len = redrat3_len_to_us(val); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 355 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 356 | /* we should always get pulse/space/pulse/space samples */ |
| 357 | if (i % 2) |
| 358 | rawir.pulse = false; |
| 359 | else |
| 360 | rawir.pulse = true; |
| 361 | |
| 362 | rawir.duration = US_TO_NS(single_len); |
Jarod Wilson | 2c594ff | 2011-07-13 18:26:06 -0300 | [diff] [blame] | 363 | /* cap the value to IR_MAX_DURATION */ |
Mauro Carvalho Chehab | 95cf60a | 2015-06-05 10:25:24 -0300 | [diff] [blame] | 364 | rawir.duration = (rawir.duration > IR_MAX_DURATION) ? |
| 365 | IR_MAX_DURATION : rawir.duration; |
Jarod Wilson | 2c594ff | 2011-07-13 18:26:06 -0300 | [diff] [blame] | 366 | |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 367 | dev_dbg(dev, "storing %s with duration %d (i: %d)\n", |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 368 | rawir.pulse ? "pulse" : "space", rawir.duration, i); |
| 369 | ir_raw_event_store_with_filter(rr3->rc, &rawir); |
| 370 | } |
| 371 | |
Sean Young | 25da661 | 2016-07-10 13:34:37 -0300 | [diff] [blame^] | 372 | /* add a trailing space */ |
| 373 | rawir.pulse = false; |
| 374 | rawir.timeout = true; |
| 375 | rawir.duration = US_TO_NS(rr3->hw_timeout); |
| 376 | dev_dbg(dev, "storing trailing timeout with duration %d\n", |
| 377 | rawir.duration); |
| 378 | ir_raw_event_store_with_filter(rr3->rc, &rawir); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 379 | |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 380 | dev_dbg(dev, "calling ir_raw_event_handle\n"); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 381 | ir_raw_event_handle(rr3->rc); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* Util fn to send rr3 cmds */ |
Mauro Carvalho Chehab | 6948524 | 2015-04-29 15:00:30 -0300 | [diff] [blame] | 385 | static int redrat3_send_cmd(int cmd, struct redrat3_dev *rr3) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 386 | { |
| 387 | struct usb_device *udev; |
| 388 | u8 *data; |
| 389 | int res; |
| 390 | |
| 391 | data = kzalloc(sizeof(u8), GFP_KERNEL); |
| 392 | if (!data) |
| 393 | return -ENOMEM; |
| 394 | |
| 395 | udev = rr3->udev; |
| 396 | res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd, |
| 397 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 398 | 0x0000, 0x0000, data, sizeof(u8), HZ * 10); |
| 399 | |
| 400 | if (res < 0) { |
| 401 | dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d", |
| 402 | __func__, res, *data); |
| 403 | res = -EIO; |
| 404 | } else |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 405 | res = data[0]; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 406 | |
| 407 | kfree(data); |
| 408 | |
| 409 | return res; |
| 410 | } |
| 411 | |
| 412 | /* Enables the long range detector and starts async receive */ |
| 413 | static int redrat3_enable_detector(struct redrat3_dev *rr3) |
| 414 | { |
| 415 | struct device *dev = rr3->dev; |
| 416 | u8 ret; |
| 417 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 418 | ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3); |
| 419 | if (ret != 0) |
| 420 | dev_dbg(dev, "%s: unexpected ret of %d\n", |
| 421 | __func__, ret); |
| 422 | |
| 423 | ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3); |
| 424 | if (ret != 1) { |
| 425 | dev_err(dev, "%s: detector status: %d, should be 1\n", |
| 426 | __func__, ret); |
| 427 | return -EIO; |
| 428 | } |
| 429 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 430 | redrat3_issue_async(rr3); |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 435 | static inline void redrat3_delete(struct redrat3_dev *rr3, |
| 436 | struct usb_device *udev) |
| 437 | { |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 438 | usb_kill_urb(rr3->read_urb); |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 439 | usb_kill_urb(rr3->flash_urb); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 440 | usb_free_urb(rr3->read_urb); |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 441 | usb_free_urb(rr3->flash_urb); |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 442 | usb_free_coherent(udev, le16_to_cpu(rr3->ep_in->wMaxPacketSize), |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 443 | rr3->bulk_in_buf, rr3->dma_in); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 444 | |
| 445 | kfree(rr3); |
| 446 | } |
| 447 | |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 448 | static u32 redrat3_get_timeout(struct redrat3_dev *rr3) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 449 | { |
Sean Young | 801b69f | 2013-02-16 17:25:43 -0300 | [diff] [blame] | 450 | __be32 *tmp; |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 451 | 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] | 452 | int len, ret, pipe; |
| 453 | |
| 454 | len = sizeof(*tmp); |
| 455 | tmp = kzalloc(len, GFP_KERNEL); |
| 456 | if (!tmp) { |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 457 | dev_warn(rr3->dev, "Memory allocation faillure\n"); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 458 | return timeout; |
| 459 | } |
| 460 | |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 461 | pipe = usb_rcvctrlpipe(rr3->udev, 0); |
| 462 | ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM, |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 463 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 464 | RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5); |
Sean Young | 801b69f | 2013-02-16 17:25:43 -0300 | [diff] [blame] | 465 | if (ret != len) |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 466 | dev_warn(rr3->dev, "Failed to read timeout from hardware\n"); |
Sean Young | 801b69f | 2013-02-16 17:25:43 -0300 | [diff] [blame] | 467 | else { |
| 468 | timeout = redrat3_len_to_us(be32_to_cpup(tmp)); |
| 469 | |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 470 | dev_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 471 | } |
| 472 | |
Sean Young | 801b69f | 2013-02-16 17:25:43 -0300 | [diff] [blame] | 473 | kfree(tmp); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 474 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 475 | return timeout; |
| 476 | } |
| 477 | |
| 478 | static void redrat3_reset(struct redrat3_dev *rr3) |
| 479 | { |
| 480 | struct usb_device *udev = rr3->udev; |
| 481 | struct device *dev = rr3->dev; |
| 482 | int rc, rxpipe, txpipe; |
| 483 | u8 *val; |
| 484 | int len = sizeof(u8); |
| 485 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 486 | rxpipe = usb_rcvctrlpipe(udev, 0); |
| 487 | txpipe = usb_sndctrlpipe(udev, 0); |
| 488 | |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 489 | val = kmalloc(len, GFP_KERNEL); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 490 | if (!val) { |
| 491 | dev_err(dev, "Memory allocation failure\n"); |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | *val = 0x01; |
| 496 | rc = usb_control_msg(udev, rxpipe, RR3_RESET, |
| 497 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 498 | RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25); |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 499 | dev_dbg(dev, "reset returned 0x%02x\n", rc); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 500 | |
| 501 | *val = 5; |
| 502 | rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, |
| 503 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 504 | RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25); |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 505 | 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] | 506 | |
| 507 | *val = RR3_DRIVER_MAXLENS; |
| 508 | rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, |
| 509 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 510 | RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25); |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 511 | 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] | 512 | |
| 513 | kfree(val); |
| 514 | } |
| 515 | |
| 516 | static void redrat3_get_firmware_rev(struct redrat3_dev *rr3) |
| 517 | { |
| 518 | int rc = 0; |
| 519 | char *buffer; |
| 520 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 521 | buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL); |
| 522 | if (!buffer) { |
| 523 | dev_err(rr3->dev, "Memory allocation failure\n"); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0), |
| 528 | RR3_FW_VERSION, |
| 529 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 530 | 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5); |
| 531 | |
| 532 | if (rc >= 0) |
| 533 | dev_info(rr3->dev, "Firmware rev: %s", buffer); |
| 534 | else |
| 535 | dev_err(rr3->dev, "Problem fetching firmware ID\n"); |
| 536 | |
| 537 | kfree(buffer); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 538 | } |
| 539 | |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 540 | static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 541 | { |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 542 | struct redrat3_header *header = rr3->bulk_in_buf; |
| 543 | unsigned pktlen, pkttype; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 544 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 545 | /* grab the Length and type of transfer */ |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 546 | pktlen = be16_to_cpu(header->length); |
| 547 | pkttype = be16_to_cpu(header->transfer_type); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 548 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 549 | if (pktlen > sizeof(rr3->irdata)) { |
| 550 | dev_warn(rr3->dev, "packet length %u too large\n", pktlen); |
| 551 | return; |
| 552 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 553 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 554 | switch (pkttype) { |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 555 | case RR3_ERROR: |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 556 | if (len >= sizeof(struct redrat3_error)) { |
| 557 | struct redrat3_error *error = rr3->bulk_in_buf; |
| 558 | unsigned fw_error = be16_to_cpu(error->fw_error); |
| 559 | redrat3_dump_fw_error(rr3, fw_error); |
| 560 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 561 | break; |
| 562 | |
| 563 | case RR3_MOD_SIGNAL_IN: |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 564 | memcpy(&rr3->irdata, rr3->bulk_in_buf, len); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 565 | rr3->bytes_read = len; |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 566 | dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 567 | rr3->bytes_read, pktlen); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 568 | break; |
| 569 | |
| 570 | default: |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 571 | 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] | 572 | pkttype, len, pktlen); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 573 | break; |
| 574 | } |
| 575 | } |
| 576 | |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 577 | static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 578 | { |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 579 | void *irdata = &rr3->irdata; |
| 580 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 581 | if (len + rr3->bytes_read > sizeof(rr3->irdata)) { |
| 582 | dev_warn(rr3->dev, "too much data for packet\n"); |
| 583 | rr3->bytes_read = 0; |
| 584 | return; |
| 585 | } |
| 586 | |
| 587 | memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 588 | |
| 589 | rr3->bytes_read += len; |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 590 | 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] | 591 | be16_to_cpu(rr3->irdata.header.length)); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* gather IR data from incoming urb, process it when we have enough */ |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 595 | static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 596 | { |
| 597 | struct device *dev = rr3->dev; |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 598 | unsigned pkttype; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 599 | int ret = 0; |
| 600 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 601 | if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) { |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 602 | redrat3_read_packet_start(rr3, len); |
| 603 | } else if (rr3->bytes_read != 0) { |
| 604 | redrat3_read_packet_continue(rr3, len); |
| 605 | } else if (rr3->bytes_read == 0) { |
| 606 | dev_err(dev, "error: no packet data read\n"); |
| 607 | ret = -ENODATA; |
| 608 | goto out; |
| 609 | } |
| 610 | |
Sean Young | 38e35a8 | 2013-07-30 19:00:00 -0300 | [diff] [blame] | 611 | if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length) + |
| 612 | sizeof(struct redrat3_header)) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 613 | /* we're still accumulating data */ |
| 614 | return 0; |
| 615 | |
| 616 | /* if we get here, we've got IR data to decode */ |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 617 | pkttype = be16_to_cpu(rr3->irdata.header.transfer_type); |
| 618 | if (pkttype == RR3_MOD_SIGNAL_IN) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 619 | redrat3_process_ir_data(rr3); |
| 620 | else |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 621 | dev_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n", |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 622 | pkttype); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 623 | |
| 624 | out: |
| 625 | rr3->bytes_read = 0; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 626 | return ret; |
| 627 | } |
| 628 | |
| 629 | /* callback function from USB when async USB request has completed */ |
Sean Young | 801b69f | 2013-02-16 17:25:43 -0300 | [diff] [blame] | 630 | static void redrat3_handle_async(struct urb *urb) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 631 | { |
| 632 | struct redrat3_dev *rr3; |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 633 | int ret; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 634 | |
| 635 | if (!urb) |
| 636 | return; |
| 637 | |
| 638 | rr3 = urb->context; |
| 639 | if (!rr3) { |
| 640 | pr_err("%s called with invalid context!\n", __func__); |
| 641 | usb_unlink_urb(urb); |
| 642 | return; |
| 643 | } |
| 644 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 645 | switch (urb->status) { |
| 646 | case 0: |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 647 | ret = redrat3_get_ir_data(rr3, urb->actual_length); |
| 648 | if (!ret) { |
| 649 | /* no error, prepare to read more */ |
| 650 | redrat3_issue_async(rr3); |
| 651 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 652 | break; |
| 653 | |
| 654 | case -ECONNRESET: |
| 655 | case -ENOENT: |
| 656 | case -ESHUTDOWN: |
| 657 | usb_unlink_urb(urb); |
| 658 | return; |
| 659 | |
| 660 | case -EPIPE: |
| 661 | default: |
| 662 | dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status); |
| 663 | rr3->bytes_read = 0; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 664 | break; |
| 665 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 666 | } |
| 667 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 668 | static u16 mod_freq_to_val(unsigned int mod_freq) |
| 669 | { |
| 670 | int mult = 6000000; |
| 671 | |
| 672 | /* Clk used in mod. freq. generation is CLK24/4. */ |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 673 | return 65536 - (mult / mod_freq); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 674 | } |
| 675 | |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 676 | static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 677 | { |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 678 | struct redrat3_dev *rr3 = rcdev->priv; |
| 679 | struct device *dev = rr3->dev; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 680 | |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 681 | dev_dbg(dev, "Setting modulation frequency to %u", carrier); |
Dan Carpenter | 48cafec | 2012-09-11 07:11:24 -0300 | [diff] [blame] | 682 | if (carrier == 0) |
| 683 | return -EINVAL; |
| 684 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 685 | rr3->carrier = carrier; |
| 686 | |
Sean Young | 14d8188 | 2016-07-10 13:34:33 -0300 | [diff] [blame] | 687 | return 0; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 688 | } |
| 689 | |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 690 | static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf, |
| 691 | unsigned count) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 692 | { |
| 693 | struct redrat3_dev *rr3 = rcdev->priv; |
| 694 | struct device *dev = rr3->dev; |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 695 | struct redrat3_irdata *irdata = NULL; |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 696 | int ret, ret_len; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 697 | int lencheck, cur_sample_len, pipe; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 698 | int *sample_lens = NULL; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 699 | u8 curlencheck = 0; |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 700 | unsigned i, sendbuf_len; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 701 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 702 | if (rr3->transmitting) { |
| 703 | dev_warn(dev, "%s: transmitter already in use\n", __func__); |
| 704 | return -EAGAIN; |
| 705 | } |
| 706 | |
Sean Young | 671ea67 | 2013-07-08 17:33:09 -0300 | [diff] [blame] | 707 | if (count > RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN) |
| 708 | return -EINVAL; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 709 | |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 710 | /* rr3 will disable rc detector on transmit */ |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 711 | rr3->transmitting = true; |
| 712 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 713 | sample_lens = kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS, GFP_KERNEL); |
| 714 | if (!sample_lens) { |
| 715 | ret = -ENOMEM; |
| 716 | goto out; |
| 717 | } |
| 718 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 719 | irdata = kzalloc(sizeof(*irdata), GFP_KERNEL); |
| 720 | if (!irdata) { |
Sean Young | 801b69f | 2013-02-16 17:25:43 -0300 | [diff] [blame] | 721 | ret = -ENOMEM; |
| 722 | goto out; |
| 723 | } |
| 724 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 725 | for (i = 0; i < count; i++) { |
Sean Young | 06eae25 | 2013-01-29 08:19:31 -0300 | [diff] [blame] | 726 | cur_sample_len = redrat3_us_to_len(txbuf[i]); |
Sean Young | 801b69f | 2013-02-16 17:25:43 -0300 | [diff] [blame] | 727 | if (cur_sample_len > 0xffff) { |
| 728 | dev_warn(dev, "transmit period of %uus truncated to %uus\n", |
| 729 | txbuf[i], redrat3_len_to_us(0xffff)); |
| 730 | cur_sample_len = 0xffff; |
| 731 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 732 | for (lencheck = 0; lencheck < curlencheck; lencheck++) { |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 733 | if (sample_lens[lencheck] == cur_sample_len) |
| 734 | break; |
| 735 | } |
| 736 | if (lencheck == curlencheck) { |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 737 | dev_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n", |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 738 | i, txbuf[i], curlencheck, cur_sample_len); |
Sean Young | 06eae25 | 2013-01-29 08:19:31 -0300 | [diff] [blame] | 739 | if (curlencheck < RR3_DRIVER_MAXLENS) { |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 740 | /* now convert the value to a proper |
| 741 | * rr3 value.. */ |
| 742 | sample_lens[curlencheck] = cur_sample_len; |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 743 | put_unaligned_be16(cur_sample_len, |
| 744 | &irdata->lens[curlencheck]); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 745 | curlencheck++; |
| 746 | } else { |
Sean Young | 671ea67 | 2013-07-08 17:33:09 -0300 | [diff] [blame] | 747 | ret = -EINVAL; |
| 748 | goto out; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 749 | } |
| 750 | } |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 751 | irdata->sigdata[i] = lencheck; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 752 | } |
| 753 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 754 | irdata->sigdata[count] = RR3_END_OF_SIGNAL; |
| 755 | irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 756 | |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 757 | sendbuf_len = offsetof(struct redrat3_irdata, |
| 758 | sigdata[count + RR3_TX_TRAILER_LEN]); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 759 | /* fill in our packet header */ |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 760 | irdata->header.length = cpu_to_be16(sendbuf_len - |
| 761 | sizeof(struct redrat3_header)); |
| 762 | irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT); |
| 763 | irdata->pause = cpu_to_be32(redrat3_len_to_us(100)); |
| 764 | irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier)); |
| 765 | irdata->no_lengths = curlencheck; |
| 766 | irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 767 | |
| 768 | pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress); |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 769 | ret = usb_bulk_msg(rr3->udev, pipe, irdata, |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 770 | sendbuf_len, &ret_len, 10 * HZ); |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 771 | dev_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 772 | |
| 773 | /* now tell the hardware to transmit what we sent it */ |
| 774 | pipe = usb_rcvctrlpipe(rr3->udev, 0); |
| 775 | ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL, |
| 776 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 777 | 0, 0, irdata, 2, HZ * 10); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 778 | |
| 779 | if (ret < 0) |
| 780 | dev_err(dev, "Error: control msg send failed, rc %d\n", ret); |
| 781 | else |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 782 | ret = count; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 783 | |
| 784 | out: |
| 785 | kfree(sample_lens); |
Sean Young | 4c055a5 | 2013-02-16 17:25:44 -0300 | [diff] [blame] | 786 | kfree(irdata); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 787 | |
| 788 | rr3->transmitting = false; |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 789 | /* rr3 re-enables rc detector because it was enabled before */ |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 790 | |
| 791 | return ret; |
| 792 | } |
| 793 | |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 794 | static void redrat3_brightness_set(struct led_classdev *led_dev, enum |
| 795 | led_brightness brightness) |
| 796 | { |
| 797 | struct redrat3_dev *rr3 = container_of(led_dev, struct redrat3_dev, |
| 798 | led); |
| 799 | |
| 800 | if (brightness != LED_OFF && atomic_cmpxchg(&rr3->flash, 0, 1) == 0) { |
| 801 | int ret = usb_submit_urb(rr3->flash_urb, GFP_ATOMIC); |
| 802 | if (ret != 0) { |
| 803 | dev_dbg(rr3->dev, "%s: unexpected ret of %d\n", |
| 804 | __func__, ret); |
| 805 | atomic_set(&rr3->flash, 0); |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | static void redrat3_led_complete(struct urb *urb) |
| 811 | { |
| 812 | struct redrat3_dev *rr3 = urb->context; |
| 813 | |
| 814 | switch (urb->status) { |
| 815 | case 0: |
| 816 | break; |
| 817 | case -ECONNRESET: |
| 818 | case -ENOENT: |
| 819 | case -ESHUTDOWN: |
| 820 | usb_unlink_urb(urb); |
| 821 | return; |
| 822 | case -EPIPE: |
| 823 | default: |
| 824 | dev_dbg(rr3->dev, "Error: urb status = %d\n", urb->status); |
| 825 | break; |
| 826 | } |
| 827 | |
| 828 | rr3->led.brightness = LED_OFF; |
| 829 | atomic_dec(&rr3->flash); |
| 830 | } |
| 831 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 832 | static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3) |
| 833 | { |
| 834 | struct device *dev = rr3->dev; |
| 835 | struct rc_dev *rc; |
| 836 | int ret = -ENODEV; |
| 837 | u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct); |
| 838 | |
| 839 | rc = rc_allocate_device(); |
| 840 | if (!rc) { |
| 841 | dev_err(dev, "remote input dev allocation failed\n"); |
| 842 | goto out; |
| 843 | } |
| 844 | |
| 845 | snprintf(rr3->name, sizeof(rr3->name), "RedRat3%s " |
| 846 | "Infrared Remote Transceiver (%04x:%04x)", |
| 847 | prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "", |
| 848 | le16_to_cpu(rr3->udev->descriptor.idVendor), prod); |
| 849 | |
| 850 | usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys)); |
| 851 | |
| 852 | rc->input_name = rr3->name; |
| 853 | rc->input_phys = rr3->phys; |
| 854 | usb_to_input_id(rr3->udev, &rc->input_id); |
| 855 | rc->dev.parent = dev; |
| 856 | rc->priv = rr3; |
| 857 | rc->driver_type = RC_DRIVER_IR_RAW; |
David Härdeman | c5540fb | 2014-04-03 20:32:21 -0300 | [diff] [blame] | 858 | rc->allowed_protocols = RC_BIT_ALL; |
Sean Young | 25da661 | 2016-07-10 13:34:37 -0300 | [diff] [blame^] | 859 | rc->timeout = US_TO_NS(rr3->hw_timeout); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 860 | rc->tx_ir = redrat3_transmit_ir; |
| 861 | rc->s_tx_carrier = redrat3_set_tx_carrier; |
| 862 | rc->driver_name = DRIVER_NAME; |
Sean Young | 06eae25 | 2013-01-29 08:19:31 -0300 | [diff] [blame] | 863 | rc->rx_resolution = US_TO_NS(2); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 864 | rc->map_name = RC_MAP_HAUPPAUGE; |
| 865 | |
| 866 | ret = rc_register_device(rc); |
| 867 | if (ret < 0) { |
| 868 | dev_err(dev, "remote dev registration failed\n"); |
| 869 | goto out; |
| 870 | } |
| 871 | |
| 872 | return rc; |
| 873 | |
| 874 | out: |
| 875 | rc_free_device(rc); |
| 876 | return NULL; |
| 877 | } |
| 878 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 879 | static int redrat3_dev_probe(struct usb_interface *intf, |
| 880 | const struct usb_device_id *id) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 881 | { |
| 882 | struct usb_device *udev = interface_to_usbdev(intf); |
| 883 | struct device *dev = &intf->dev; |
| 884 | struct usb_host_interface *uhi; |
| 885 | struct redrat3_dev *rr3; |
| 886 | struct usb_endpoint_descriptor *ep; |
| 887 | struct usb_endpoint_descriptor *ep_in = NULL; |
| 888 | struct usb_endpoint_descriptor *ep_out = NULL; |
| 889 | u8 addr, attrs; |
| 890 | int pipe, i; |
| 891 | int retval = -ENOMEM; |
| 892 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 893 | uhi = intf->cur_altsetting; |
| 894 | |
| 895 | /* find our bulk-in and bulk-out endpoints */ |
| 896 | for (i = 0; i < uhi->desc.bNumEndpoints; ++i) { |
| 897 | ep = &uhi->endpoint[i].desc; |
| 898 | addr = ep->bEndpointAddress; |
| 899 | attrs = ep->bmAttributes; |
| 900 | |
| 901 | if ((ep_in == NULL) && |
| 902 | ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) && |
| 903 | ((attrs & USB_ENDPOINT_XFERTYPE_MASK) == |
| 904 | USB_ENDPOINT_XFER_BULK)) { |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 905 | dev_dbg(dev, "found bulk-in endpoint at 0x%02x\n", |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 906 | ep->bEndpointAddress); |
| 907 | /* data comes in on 0x82, 0x81 is for other data... */ |
| 908 | if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR) |
| 909 | ep_in = ep; |
| 910 | } |
| 911 | |
| 912 | if ((ep_out == NULL) && |
| 913 | ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) && |
| 914 | ((attrs & USB_ENDPOINT_XFERTYPE_MASK) == |
| 915 | USB_ENDPOINT_XFER_BULK)) { |
Greg Kroah-Hartman | 3228bf8 | 2014-05-28 11:34:01 -0700 | [diff] [blame] | 916 | dev_dbg(dev, "found bulk-out endpoint at 0x%02x\n", |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 917 | ep->bEndpointAddress); |
| 918 | ep_out = ep; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | if (!ep_in || !ep_out) { |
| 923 | dev_err(dev, "Couldn't find both in and out endpoints\n"); |
| 924 | retval = -ENODEV; |
| 925 | goto no_endpoints; |
| 926 | } |
| 927 | |
| 928 | /* allocate memory for our device state and initialize it */ |
| 929 | rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL); |
| 930 | if (rr3 == NULL) { |
| 931 | dev_err(dev, "Memory allocation failure\n"); |
Dan Carpenter | 7eb7571 | 2011-05-26 05:55:08 -0300 | [diff] [blame] | 932 | goto no_endpoints; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | rr3->dev = &intf->dev; |
| 936 | |
| 937 | /* set up bulk-in endpoint */ |
| 938 | rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 939 | if (!rr3->read_urb) { |
| 940 | dev_err(dev, "Read urb allocation failure\n"); |
| 941 | goto error; |
| 942 | } |
| 943 | |
| 944 | rr3->ep_in = ep_in; |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 945 | rr3->bulk_in_buf = usb_alloc_coherent(udev, |
Sean Young | d0a0a65 | 2014-12-01 05:48:16 -0300 | [diff] [blame] | 946 | le16_to_cpu(ep_in->wMaxPacketSize), GFP_KERNEL, &rr3->dma_in); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 947 | if (!rr3->bulk_in_buf) { |
| 948 | dev_err(dev, "Read buffer allocation failure\n"); |
| 949 | goto error; |
| 950 | } |
| 951 | |
| 952 | pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress); |
Sean Young | fa7b9ac | 2013-02-16 17:25:45 -0300 | [diff] [blame] | 953 | usb_fill_bulk_urb(rr3->read_urb, udev, pipe, rr3->bulk_in_buf, |
| 954 | le16_to_cpu(ep_in->wMaxPacketSize), redrat3_handle_async, rr3); |
Sean Young | d0a0a65 | 2014-12-01 05:48:16 -0300 | [diff] [blame] | 955 | rr3->read_urb->transfer_dma = rr3->dma_in; |
| 956 | rr3->read_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 957 | |
| 958 | rr3->ep_out = ep_out; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 959 | rr3->udev = udev; |
| 960 | |
| 961 | redrat3_reset(rr3); |
| 962 | redrat3_get_firmware_rev(rr3); |
| 963 | |
| 964 | /* might be all we need to do? */ |
| 965 | retval = redrat3_enable_detector(rr3); |
| 966 | if (retval < 0) |
| 967 | goto error; |
| 968 | |
Sean Young | 25da661 | 2016-07-10 13:34:37 -0300 | [diff] [blame^] | 969 | /* store current hardware timeout, in µs */ |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 970 | rr3->hw_timeout = redrat3_get_timeout(rr3); |
| 971 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 972 | /* default.. will get overridden by any sends with a freq defined */ |
| 973 | rr3->carrier = 38000; |
| 974 | |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 975 | /* led control */ |
| 976 | rr3->led.name = "redrat3:red:feedback"; |
| 977 | rr3->led.default_trigger = "rc-feedback"; |
| 978 | rr3->led.brightness_set = redrat3_brightness_set; |
| 979 | retval = led_classdev_register(&intf->dev, &rr3->led); |
| 980 | if (retval) |
| 981 | goto error; |
| 982 | |
| 983 | atomic_set(&rr3->flash, 0); |
| 984 | rr3->flash_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 985 | if (!rr3->flash_urb) { |
| 986 | retval = -ENOMEM; |
| 987 | goto led_free_error; |
| 988 | } |
| 989 | |
| 990 | /* setup packet is 'c0 b9 0000 0000 0001' */ |
| 991 | rr3->flash_control.bRequestType = 0xc0; |
| 992 | rr3->flash_control.bRequest = RR3_BLINK_LED; |
| 993 | rr3->flash_control.wLength = cpu_to_le16(1); |
| 994 | |
| 995 | usb_fill_control_urb(rr3->flash_urb, udev, usb_rcvctrlpipe(udev, 0), |
| 996 | (unsigned char *)&rr3->flash_control, |
| 997 | &rr3->flash_in_buf, sizeof(rr3->flash_in_buf), |
| 998 | redrat3_led_complete, rr3); |
| 999 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1000 | rr3->rc = redrat3_init_rc_dev(rr3); |
Peter Senna Tschudin | 6ea7cf7 | 2012-09-04 08:05:05 -0300 | [diff] [blame] | 1001 | if (!rr3->rc) { |
| 1002 | retval = -ENOMEM; |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 1003 | goto led_free_error; |
Peter Senna Tschudin | 6ea7cf7 | 2012-09-04 08:05:05 -0300 | [diff] [blame] | 1004 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1005 | |
| 1006 | /* we can register the device now, as it is ready */ |
| 1007 | usb_set_intfdata(intf, rr3); |
| 1008 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1009 | return 0; |
| 1010 | |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 1011 | led_free_error: |
| 1012 | led_classdev_unregister(&rr3->led); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1013 | error: |
| 1014 | redrat3_delete(rr3, rr3->udev); |
| 1015 | |
| 1016 | no_endpoints: |
| 1017 | dev_err(dev, "%s: retval = %x", __func__, retval); |
| 1018 | |
| 1019 | return retval; |
| 1020 | } |
| 1021 | |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 1022 | static void redrat3_dev_disconnect(struct usb_interface *intf) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1023 | { |
| 1024 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1025 | struct redrat3_dev *rr3 = usb_get_intfdata(intf); |
| 1026 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1027 | if (!rr3) |
| 1028 | return; |
| 1029 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1030 | usb_set_intfdata(intf, NULL); |
| 1031 | rc_unregister_device(rr3->rc); |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 1032 | led_classdev_unregister(&rr3->led); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1033 | redrat3_delete(rr3, udev); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message) |
| 1037 | { |
| 1038 | struct redrat3_dev *rr3 = usb_get_intfdata(intf); |
Greg Kroah-Hartman | a36d5b6 | 2014-05-28 11:34:00 -0700 | [diff] [blame] | 1039 | |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 1040 | led_classdev_suspend(&rr3->led); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1041 | usb_kill_urb(rr3->read_urb); |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 1042 | usb_kill_urb(rr3->flash_urb); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | static int redrat3_dev_resume(struct usb_interface *intf) |
| 1047 | { |
| 1048 | struct redrat3_dev *rr3 = usb_get_intfdata(intf); |
Greg Kroah-Hartman | a36d5b6 | 2014-05-28 11:34:00 -0700 | [diff] [blame] | 1049 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1050 | if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC)) |
| 1051 | return -EIO; |
Sean Young | bf13972 | 2013-07-30 19:00:04 -0300 | [diff] [blame] | 1052 | led_classdev_resume(&rr3->led); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1053 | return 0; |
| 1054 | } |
| 1055 | |
| 1056 | static struct usb_driver redrat3_dev_driver = { |
| 1057 | .name = DRIVER_NAME, |
| 1058 | .probe = redrat3_dev_probe, |
Greg Kroah-Hartman | 4c62e97 | 2012-12-21 13:17:53 -0800 | [diff] [blame] | 1059 | .disconnect = redrat3_dev_disconnect, |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1060 | .suspend = redrat3_dev_suspend, |
| 1061 | .resume = redrat3_dev_resume, |
| 1062 | .reset_resume = redrat3_dev_resume, |
| 1063 | .id_table = redrat3_dev_table |
| 1064 | }; |
| 1065 | |
Greg Kroah-Hartman | ecb3b2b | 2011-11-18 09:46:12 -0800 | [diff] [blame] | 1066 | module_usb_driver(redrat3_dev_driver); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1067 | |
| 1068 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 1069 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1070 | MODULE_AUTHOR(DRIVER_AUTHOR2); |
| 1071 | MODULE_LICENSE("GPL"); |
| 1072 | MODULE_DEVICE_TABLE(usb, redrat3_dev_table); |