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 | |
| 48 | #include <linux/device.h> |
| 49 | #include <linux/module.h> |
| 50 | #include <linux/slab.h> |
| 51 | #include <linux/usb.h> |
| 52 | #include <linux/usb/input.h> |
| 53 | #include <media/rc-core.h> |
| 54 | |
| 55 | /* Driver Information */ |
| 56 | #define DRIVER_VERSION "0.70" |
| 57 | #define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>" |
| 58 | #define DRIVER_AUTHOR2 "The Dweller, Stephen Cox" |
| 59 | #define DRIVER_DESC "RedRat3 USB IR Transceiver Driver" |
| 60 | #define DRIVER_NAME "redrat3" |
| 61 | |
| 62 | /* module parameters */ |
| 63 | #ifdef CONFIG_USB_DEBUG |
| 64 | static int debug = 1; |
| 65 | #else |
| 66 | static int debug; |
| 67 | #endif |
| 68 | |
| 69 | #define RR3_DEBUG_STANDARD 0x1 |
| 70 | #define RR3_DEBUG_FUNCTION_TRACE 0x2 |
| 71 | |
| 72 | #define rr3_dbg(dev, fmt, ...) \ |
| 73 | do { \ |
| 74 | if (debug & RR3_DEBUG_STANDARD) \ |
| 75 | dev_info(dev, fmt, ## __VA_ARGS__); \ |
| 76 | } while (0) |
| 77 | |
| 78 | #define rr3_ftr(dev, fmt, ...) \ |
| 79 | do { \ |
| 80 | if (debug & RR3_DEBUG_FUNCTION_TRACE) \ |
| 81 | dev_info(dev, fmt, ## __VA_ARGS__); \ |
| 82 | } while (0) |
| 83 | |
| 84 | /* bulk data transfer types */ |
| 85 | #define RR3_ERROR 0x01 |
| 86 | #define RR3_MOD_SIGNAL_IN 0x20 |
| 87 | #define RR3_MOD_SIGNAL_OUT 0x21 |
| 88 | |
| 89 | /* Get the RR firmware version */ |
| 90 | #define RR3_FW_VERSION 0xb1 |
| 91 | #define RR3_FW_VERSION_LEN 64 |
| 92 | /* Send encoded signal bulk-sent earlier*/ |
| 93 | #define RR3_TX_SEND_SIGNAL 0xb3 |
| 94 | #define RR3_SET_IR_PARAM 0xb7 |
| 95 | #define RR3_GET_IR_PARAM 0xb8 |
| 96 | /* Blink the red LED on the device */ |
| 97 | #define RR3_BLINK_LED 0xb9 |
| 98 | /* Read serial number of device */ |
| 99 | #define RR3_READ_SER_NO 0xba |
| 100 | #define RR3_SER_NO_LEN 4 |
| 101 | /* Start capture with the RC receiver */ |
| 102 | #define RR3_RC_DET_ENABLE 0xbb |
| 103 | /* Stop capture with the RC receiver */ |
| 104 | #define RR3_RC_DET_DISABLE 0xbc |
| 105 | /* Return the status of RC detector capture */ |
| 106 | #define RR3_RC_DET_STATUS 0xbd |
| 107 | /* Reset redrat */ |
| 108 | #define RR3_RESET 0xa0 |
| 109 | |
| 110 | /* Max number of lengths in the signal. */ |
| 111 | #define RR3_IR_IO_MAX_LENGTHS 0x01 |
| 112 | /* Periods to measure mod. freq. */ |
| 113 | #define RR3_IR_IO_PERIODS_MF 0x02 |
| 114 | /* Size of memory for main signal data */ |
| 115 | #define RR3_IR_IO_SIG_MEM_SIZE 0x03 |
| 116 | /* Delta value when measuring lengths */ |
| 117 | #define RR3_IR_IO_LENGTH_FUZZ 0x04 |
| 118 | /* Timeout for end of signal detection */ |
| 119 | #define RR3_IR_IO_SIG_TIMEOUT 0x05 |
| 120 | /* Minumum value for pause recognition. */ |
| 121 | #define RR3_IR_IO_MIN_PAUSE 0x06 |
| 122 | |
| 123 | /* Clock freq. of EZ-USB chip */ |
| 124 | #define RR3_CLK 24000000 |
| 125 | /* Clock periods per timer count */ |
| 126 | #define RR3_CLK_PER_COUNT 12 |
| 127 | /* (RR3_CLK / RR3_CLK_PER_COUNT) */ |
| 128 | #define RR3_CLK_CONV_FACTOR 2000000 |
| 129 | /* USB bulk-in IR data endpoint address */ |
| 130 | #define RR3_BULK_IN_EP_ADDR 0x82 |
| 131 | |
| 132 | /* Raw Modulated signal data value offsets */ |
| 133 | #define RR3_PAUSE_OFFSET 0 |
| 134 | #define RR3_FREQ_COUNT_OFFSET 4 |
| 135 | #define RR3_NUM_PERIOD_OFFSET 6 |
| 136 | #define RR3_MAX_LENGTHS_OFFSET 8 |
| 137 | #define RR3_NUM_LENGTHS_OFFSET 9 |
| 138 | #define RR3_MAX_SIGS_OFFSET 10 |
| 139 | #define RR3_NUM_SIGS_OFFSET 12 |
| 140 | #define RR3_REPEATS_OFFSET 14 |
| 141 | |
| 142 | /* Size of the fixed-length portion of the signal */ |
| 143 | #define RR3_HEADER_LENGTH 15 |
| 144 | #define RR3_DRIVER_MAXLENS 128 |
| 145 | #define RR3_MAX_SIG_SIZE 512 |
| 146 | #define RR3_MAX_BUF_SIZE \ |
| 147 | ((2 * RR3_HEADER_LENGTH) + RR3_DRIVER_MAXLENS + RR3_MAX_SIG_SIZE) |
| 148 | #define RR3_TIME_UNIT 50 |
| 149 | #define RR3_END_OF_SIGNAL 0x7f |
| 150 | #define RR3_TX_HEADER_OFFSET 4 |
| 151 | #define RR3_TX_TRAILER_LEN 2 |
| 152 | #define RR3_RX_MIN_TIMEOUT 5 |
| 153 | #define RR3_RX_MAX_TIMEOUT 2000 |
| 154 | |
| 155 | /* The 8051's CPUCS Register address */ |
| 156 | #define RR3_CPUCS_REG_ADDR 0x7f92 |
| 157 | |
| 158 | #define USB_RR3USB_VENDOR_ID 0x112a |
| 159 | #define USB_RR3USB_PRODUCT_ID 0x0001 |
| 160 | #define USB_RR3IIUSB_PRODUCT_ID 0x0005 |
| 161 | |
| 162 | /* table of devices that work with this driver */ |
| 163 | static struct usb_device_id redrat3_dev_table[] = { |
| 164 | /* Original version of the RedRat3 */ |
| 165 | {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)}, |
| 166 | /* Second Version/release of the RedRat3 - RetRat3-II */ |
| 167 | {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)}, |
| 168 | {} /* Terminating entry */ |
| 169 | }; |
| 170 | |
| 171 | /* Structure to hold all of our device specific stuff */ |
| 172 | struct redrat3_dev { |
| 173 | /* core device bits */ |
| 174 | struct rc_dev *rc; |
| 175 | struct device *dev; |
| 176 | |
| 177 | /* save off the usb device pointer */ |
| 178 | struct usb_device *udev; |
| 179 | |
| 180 | /* the receive endpoint */ |
| 181 | struct usb_endpoint_descriptor *ep_in; |
| 182 | /* the buffer to receive data */ |
| 183 | unsigned char *bulk_in_buf; |
| 184 | /* urb used to read ir data */ |
| 185 | struct urb *read_urb; |
| 186 | |
| 187 | /* the send endpoint */ |
| 188 | struct usb_endpoint_descriptor *ep_out; |
| 189 | /* the buffer to send data */ |
| 190 | unsigned char *bulk_out_buf; |
| 191 | /* the urb used to send data */ |
| 192 | struct urb *write_urb; |
| 193 | |
| 194 | /* usb dma */ |
| 195 | dma_addr_t dma_in; |
| 196 | dma_addr_t dma_out; |
| 197 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 198 | /* locks this structure */ |
| 199 | struct mutex lock; |
| 200 | |
| 201 | /* rx signal timeout timer */ |
| 202 | struct timer_list rx_timeout; |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 203 | u32 hw_timeout; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 204 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 205 | /* is the detector enabled*/ |
| 206 | bool det_enabled; |
| 207 | /* Is the device currently transmitting?*/ |
| 208 | bool transmitting; |
| 209 | |
| 210 | /* store for current packet */ |
| 211 | char pbuf[RR3_MAX_BUF_SIZE]; |
| 212 | u16 pktlen; |
| 213 | u16 pkttype; |
| 214 | u16 bytes_read; |
| 215 | /* indicate whether we are going to reprocess |
| 216 | * the USB callback with a bigger buffer */ |
| 217 | int buftoosmall; |
| 218 | char *datap; |
| 219 | |
| 220 | u32 carrier; |
| 221 | |
| 222 | char name[128]; |
| 223 | char phys[64]; |
| 224 | }; |
| 225 | |
| 226 | /* All incoming data buffers adhere to a very specific data format */ |
| 227 | struct redrat3_signal_header { |
| 228 | u16 length; /* Length of data being transferred */ |
| 229 | u16 transfer_type; /* Type of data transferred */ |
| 230 | u32 pause; /* Pause between main and repeat signals */ |
| 231 | u16 mod_freq_count; /* Value of timer on mod. freq. measurement */ |
| 232 | u16 no_periods; /* No. of periods over which mod. freq. is measured */ |
| 233 | u8 max_lengths; /* Max no. of lengths (i.e. size of array) */ |
| 234 | u8 no_lengths; /* Actual no. of elements in lengths array */ |
| 235 | u16 max_sig_size; /* Max no. of values in signal data array */ |
| 236 | u16 sig_size; /* Acuto no. of values in signal data array */ |
| 237 | u8 no_repeats; /* No. of repeats of repeat signal section */ |
| 238 | /* Here forward is the lengths and signal data */ |
| 239 | }; |
| 240 | |
| 241 | static void redrat3_dump_signal_header(struct redrat3_signal_header *header) |
| 242 | { |
| 243 | pr_info("%s:\n", __func__); |
| 244 | pr_info(" * length: %u, transfer_type: 0x%02x\n", |
| 245 | header->length, header->transfer_type); |
| 246 | pr_info(" * pause: %u, freq_count: %u, no_periods: %u\n", |
| 247 | header->pause, header->mod_freq_count, header->no_periods); |
| 248 | pr_info(" * lengths: %u (max: %u)\n", |
| 249 | header->no_lengths, header->max_lengths); |
| 250 | pr_info(" * sig_size: %u (max: %u)\n", |
| 251 | header->sig_size, header->max_sig_size); |
| 252 | pr_info(" * repeats: %u\n", header->no_repeats); |
| 253 | } |
| 254 | |
| 255 | static void redrat3_dump_signal_data(char *buffer, u16 len) |
| 256 | { |
| 257 | int offset, i; |
| 258 | char *data_vals; |
| 259 | |
| 260 | pr_info("%s:", __func__); |
| 261 | |
| 262 | offset = RR3_TX_HEADER_OFFSET + RR3_HEADER_LENGTH |
| 263 | + (RR3_DRIVER_MAXLENS * sizeof(u16)); |
| 264 | |
| 265 | /* read RR3_DRIVER_MAXLENS from ctrl msg */ |
| 266 | data_vals = buffer + offset; |
| 267 | |
| 268 | for (i = 0; i < len; i++) { |
| 269 | if (i % 10 == 0) |
| 270 | pr_cont("\n * "); |
| 271 | pr_cont("%02x ", *data_vals++); |
| 272 | } |
| 273 | |
| 274 | pr_cont("\n"); |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * redrat3_issue_async |
| 279 | * |
| 280 | * Issues an async read to the ir data in port.. |
| 281 | * sets the callback to be redrat3_handle_async |
| 282 | */ |
| 283 | static void redrat3_issue_async(struct redrat3_dev *rr3) |
| 284 | { |
| 285 | int res; |
| 286 | |
| 287 | rr3_ftr(rr3->dev, "Entering %s\n", __func__); |
| 288 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 289 | memset(rr3->bulk_in_buf, 0, rr3->ep_in->wMaxPacketSize); |
| 290 | res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC); |
| 291 | if (res) |
| 292 | rr3_dbg(rr3->dev, "%s: receive request FAILED! " |
| 293 | "(res %d, len %d)\n", __func__, res, |
| 294 | rr3->read_urb->transfer_buffer_length); |
| 295 | } |
| 296 | |
| 297 | static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code) |
| 298 | { |
| 299 | if (!rr3->transmitting && (code != 0x40)) |
| 300 | dev_info(rr3->dev, "fw error code 0x%02x: ", code); |
| 301 | |
| 302 | switch (code) { |
| 303 | case 0x00: |
| 304 | pr_cont("No Error\n"); |
| 305 | break; |
| 306 | |
| 307 | /* Codes 0x20 through 0x2f are IR Firmware Errors */ |
| 308 | case 0x20: |
| 309 | pr_cont("Initial signal pulse not long enough " |
| 310 | "to measure carrier frequency\n"); |
| 311 | break; |
| 312 | case 0x21: |
| 313 | pr_cont("Not enough length values allocated for signal\n"); |
| 314 | break; |
| 315 | case 0x22: |
| 316 | pr_cont("Not enough memory allocated for signal data\n"); |
| 317 | break; |
| 318 | case 0x23: |
| 319 | pr_cont("Too many signal repeats\n"); |
| 320 | break; |
| 321 | case 0x28: |
| 322 | pr_cont("Insufficient memory available for IR signal " |
| 323 | "data memory allocation\n"); |
| 324 | break; |
| 325 | case 0x29: |
| 326 | pr_cont("Insufficient memory available " |
| 327 | "for IrDa signal data memory allocation\n"); |
| 328 | break; |
| 329 | |
| 330 | /* Codes 0x30 through 0x3f are USB Firmware Errors */ |
| 331 | case 0x30: |
| 332 | pr_cont("Insufficient memory available for bulk " |
| 333 | "transfer structure\n"); |
| 334 | break; |
| 335 | |
| 336 | /* |
| 337 | * Other error codes... These are primarily errors that can occur in |
| 338 | * the control messages sent to the redrat |
| 339 | */ |
| 340 | case 0x40: |
| 341 | if (!rr3->transmitting) |
| 342 | pr_cont("Signal capture has been terminated\n"); |
| 343 | break; |
| 344 | case 0x41: |
| 345 | pr_cont("Attempt to set/get and unknown signal I/O " |
| 346 | "algorithm parameter\n"); |
| 347 | break; |
| 348 | case 0x42: |
| 349 | pr_cont("Signal capture already started\n"); |
| 350 | break; |
| 351 | |
| 352 | default: |
| 353 | pr_cont("Unknown Error\n"); |
| 354 | break; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | static u32 redrat3_val_to_mod_freq(struct redrat3_signal_header *ph) |
| 359 | { |
| 360 | u32 mod_freq = 0; |
| 361 | |
| 362 | if (ph->mod_freq_count != 0) |
| 363 | mod_freq = (RR3_CLK * ph->no_periods) / |
| 364 | (ph->mod_freq_count * RR3_CLK_PER_COUNT); |
| 365 | |
| 366 | return mod_freq; |
| 367 | } |
| 368 | |
| 369 | /* this function scales down the figures for the same result... */ |
| 370 | static u32 redrat3_len_to_us(u32 length) |
| 371 | { |
| 372 | u32 biglen = length * 1000; |
| 373 | u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000; |
| 374 | u32 result = (u32) (biglen / divisor); |
| 375 | |
| 376 | /* don't allow zero lengths to go back, breaks lirc */ |
| 377 | return result ? result : 1; |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * convert us back into redrat3 lengths |
| 382 | * |
| 383 | * length * 1000 length * 1000000 |
| 384 | * ------------- = ---------------- = micro |
| 385 | * rr3clk / 1000 rr3clk |
| 386 | |
| 387 | * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000 |
| 388 | * ----- = 4 ----- = 6 -------------- = len --------------------- |
| 389 | * 3 2 1000000 1000 |
| 390 | */ |
| 391 | static u32 redrat3_us_to_len(u32 microsec) |
| 392 | { |
| 393 | u32 result; |
| 394 | u32 divisor; |
| 395 | |
| 396 | microsec &= IR_MAX_DURATION; |
| 397 | divisor = (RR3_CLK_CONV_FACTOR / 1000); |
| 398 | result = (u32)(microsec * divisor) / 1000; |
| 399 | |
| 400 | /* don't allow zero lengths to go back, breaks lirc */ |
| 401 | return result ? result : 1; |
| 402 | |
| 403 | } |
| 404 | |
Jarod Wilson | 68b2a69 | 2011-07-13 18:26:05 -0300 | [diff] [blame] | 405 | /* timer callback to send reset event */ |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 406 | static void redrat3_rx_timeout(unsigned long data) |
| 407 | { |
| 408 | struct redrat3_dev *rr3 = (struct redrat3_dev *)data; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 409 | |
| 410 | rr3_dbg(rr3->dev, "calling ir_raw_event_reset\n"); |
| 411 | ir_raw_event_reset(rr3->rc); |
| 412 | } |
| 413 | |
| 414 | static void redrat3_process_ir_data(struct redrat3_dev *rr3) |
| 415 | { |
| 416 | DEFINE_IR_RAW_EVENT(rawir); |
| 417 | struct redrat3_signal_header header; |
| 418 | struct device *dev; |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 419 | int i, trailer = 0; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 420 | unsigned long delay; |
| 421 | u32 mod_freq, single_len; |
| 422 | u16 *len_vals; |
| 423 | u8 *data_vals; |
| 424 | u32 tmp32; |
| 425 | u16 tmp16; |
| 426 | char *sig_data; |
| 427 | |
| 428 | if (!rr3) { |
| 429 | pr_err("%s called with no context!\n", __func__); |
| 430 | return; |
| 431 | } |
| 432 | |
| 433 | rr3_ftr(rr3->dev, "Entered %s\n", __func__); |
| 434 | |
| 435 | dev = rr3->dev; |
| 436 | sig_data = rr3->pbuf; |
| 437 | |
| 438 | header.length = rr3->pktlen; |
| 439 | header.transfer_type = rr3->pkttype; |
| 440 | |
| 441 | /* Sanity check */ |
| 442 | if (!(header.length >= RR3_HEADER_LENGTH)) |
| 443 | dev_warn(dev, "read returned less than rr3 header len\n"); |
| 444 | |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 445 | /* Make sure we reset the IR kfifo after a bit of inactivity */ |
| 446 | delay = usecs_to_jiffies(rr3->hw_timeout); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 447 | mod_timer(&rr3->rx_timeout, jiffies + delay); |
| 448 | |
| 449 | memcpy(&tmp32, sig_data + RR3_PAUSE_OFFSET, sizeof(tmp32)); |
| 450 | header.pause = be32_to_cpu(tmp32); |
| 451 | |
| 452 | memcpy(&tmp16, sig_data + RR3_FREQ_COUNT_OFFSET, sizeof(tmp16)); |
| 453 | header.mod_freq_count = be16_to_cpu(tmp16); |
| 454 | |
| 455 | memcpy(&tmp16, sig_data + RR3_NUM_PERIOD_OFFSET, sizeof(tmp16)); |
| 456 | header.no_periods = be16_to_cpu(tmp16); |
| 457 | |
| 458 | header.max_lengths = sig_data[RR3_MAX_LENGTHS_OFFSET]; |
| 459 | header.no_lengths = sig_data[RR3_NUM_LENGTHS_OFFSET]; |
| 460 | |
| 461 | memcpy(&tmp16, sig_data + RR3_MAX_SIGS_OFFSET, sizeof(tmp16)); |
| 462 | header.max_sig_size = be16_to_cpu(tmp16); |
| 463 | |
| 464 | memcpy(&tmp16, sig_data + RR3_NUM_SIGS_OFFSET, sizeof(tmp16)); |
| 465 | header.sig_size = be16_to_cpu(tmp16); |
| 466 | |
| 467 | header.no_repeats= sig_data[RR3_REPEATS_OFFSET]; |
| 468 | |
| 469 | if (debug) { |
| 470 | redrat3_dump_signal_header(&header); |
| 471 | redrat3_dump_signal_data(sig_data, header.sig_size); |
| 472 | } |
| 473 | |
| 474 | mod_freq = redrat3_val_to_mod_freq(&header); |
| 475 | rr3_dbg(dev, "Got mod_freq of %u\n", mod_freq); |
| 476 | |
| 477 | /* Here we pull out the 'length' values from the signal */ |
| 478 | len_vals = (u16 *)(sig_data + RR3_HEADER_LENGTH); |
| 479 | |
| 480 | data_vals = sig_data + RR3_HEADER_LENGTH + |
| 481 | (header.max_lengths * sizeof(u16)); |
| 482 | |
| 483 | /* process each rr3 encoded byte into an int */ |
| 484 | for (i = 0; i < header.sig_size; i++) { |
| 485 | u16 val = len_vals[data_vals[i]]; |
| 486 | single_len = redrat3_len_to_us((u32)be16_to_cpu(val)); |
| 487 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 488 | /* we should always get pulse/space/pulse/space samples */ |
| 489 | if (i % 2) |
| 490 | rawir.pulse = false; |
| 491 | else |
| 492 | rawir.pulse = true; |
| 493 | |
| 494 | rawir.duration = US_TO_NS(single_len); |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 495 | /* Save initial pulse length to fudge trailer */ |
| 496 | if (i == 0) |
| 497 | trailer = rawir.duration; |
Jarod Wilson | 2c594ff | 2011-07-13 18:26:06 -0300 | [diff] [blame] | 498 | /* cap the value to IR_MAX_DURATION */ |
| 499 | rawir.duration &= IR_MAX_DURATION; |
| 500 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 501 | rr3_dbg(dev, "storing %s with duration %d (i: %d)\n", |
| 502 | rawir.pulse ? "pulse" : "space", rawir.duration, i); |
| 503 | ir_raw_event_store_with_filter(rr3->rc, &rawir); |
| 504 | } |
| 505 | |
| 506 | /* add a trailing space, if need be */ |
| 507 | if (i % 2) { |
| 508 | rawir.pulse = false; |
| 509 | /* this duration is made up, and may not be ideal... */ |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 510 | if (trailer < US_TO_NS(1000)) |
| 511 | rawir.duration = US_TO_NS(2800); |
| 512 | else |
| 513 | rawir.duration = trailer; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 514 | rr3_dbg(dev, "storing trailing space with duration %d\n", |
| 515 | rawir.duration); |
| 516 | ir_raw_event_store_with_filter(rr3->rc, &rawir); |
| 517 | } |
| 518 | |
| 519 | rr3_dbg(dev, "calling ir_raw_event_handle\n"); |
| 520 | ir_raw_event_handle(rr3->rc); |
| 521 | |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | /* Util fn to send rr3 cmds */ |
| 526 | static u8 redrat3_send_cmd(int cmd, struct redrat3_dev *rr3) |
| 527 | { |
| 528 | struct usb_device *udev; |
| 529 | u8 *data; |
| 530 | int res; |
| 531 | |
| 532 | data = kzalloc(sizeof(u8), GFP_KERNEL); |
| 533 | if (!data) |
| 534 | return -ENOMEM; |
| 535 | |
| 536 | udev = rr3->udev; |
| 537 | res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd, |
| 538 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 539 | 0x0000, 0x0000, data, sizeof(u8), HZ * 10); |
| 540 | |
| 541 | if (res < 0) { |
| 542 | dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d", |
| 543 | __func__, res, *data); |
| 544 | res = -EIO; |
| 545 | } else |
| 546 | res = (u8)data[0]; |
| 547 | |
| 548 | kfree(data); |
| 549 | |
| 550 | return res; |
| 551 | } |
| 552 | |
| 553 | /* Enables the long range detector and starts async receive */ |
| 554 | static int redrat3_enable_detector(struct redrat3_dev *rr3) |
| 555 | { |
| 556 | struct device *dev = rr3->dev; |
| 557 | u8 ret; |
| 558 | |
| 559 | rr3_ftr(dev, "Entering %s\n", __func__); |
| 560 | |
| 561 | ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3); |
| 562 | if (ret != 0) |
| 563 | dev_dbg(dev, "%s: unexpected ret of %d\n", |
| 564 | __func__, ret); |
| 565 | |
| 566 | ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3); |
| 567 | if (ret != 1) { |
| 568 | dev_err(dev, "%s: detector status: %d, should be 1\n", |
| 569 | __func__, ret); |
| 570 | return -EIO; |
| 571 | } |
| 572 | |
| 573 | rr3->det_enabled = true; |
| 574 | redrat3_issue_async(rr3); |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | /* Disables the rr3 long range detector */ |
| 580 | static void redrat3_disable_detector(struct redrat3_dev *rr3) |
| 581 | { |
| 582 | struct device *dev = rr3->dev; |
| 583 | u8 ret; |
| 584 | |
| 585 | rr3_ftr(dev, "Entering %s\n", __func__); |
| 586 | |
| 587 | ret = redrat3_send_cmd(RR3_RC_DET_DISABLE, rr3); |
| 588 | if (ret != 0) |
| 589 | dev_err(dev, "%s: failure!\n", __func__); |
| 590 | |
| 591 | ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3); |
| 592 | if (ret != 0) |
| 593 | dev_warn(dev, "%s: detector status: %d, should be 0\n", |
| 594 | __func__, ret); |
| 595 | |
| 596 | rr3->det_enabled = false; |
| 597 | } |
| 598 | |
| 599 | static inline void redrat3_delete(struct redrat3_dev *rr3, |
| 600 | struct usb_device *udev) |
| 601 | { |
| 602 | rr3_ftr(rr3->dev, "%s cleaning up\n", __func__); |
| 603 | usb_kill_urb(rr3->read_urb); |
| 604 | usb_kill_urb(rr3->write_urb); |
| 605 | |
| 606 | usb_free_urb(rr3->read_urb); |
| 607 | usb_free_urb(rr3->write_urb); |
| 608 | |
| 609 | usb_free_coherent(udev, rr3->ep_in->wMaxPacketSize, |
| 610 | rr3->bulk_in_buf, rr3->dma_in); |
| 611 | usb_free_coherent(udev, rr3->ep_out->wMaxPacketSize, |
| 612 | rr3->bulk_out_buf, rr3->dma_out); |
| 613 | |
| 614 | kfree(rr3); |
| 615 | } |
| 616 | |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 617 | static u32 redrat3_get_timeout(struct redrat3_dev *rr3) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 618 | { |
| 619 | u32 *tmp; |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 620 | 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] | 621 | int len, ret, pipe; |
| 622 | |
| 623 | len = sizeof(*tmp); |
| 624 | tmp = kzalloc(len, GFP_KERNEL); |
| 625 | if (!tmp) { |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 626 | dev_warn(rr3->dev, "Memory allocation faillure\n"); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 627 | return timeout; |
| 628 | } |
| 629 | |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 630 | pipe = usb_rcvctrlpipe(rr3->udev, 0); |
| 631 | ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM, |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 632 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 633 | RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5); |
| 634 | if (ret != len) { |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 635 | dev_warn(rr3->dev, "Failed to read timeout from hardware\n"); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 636 | return timeout; |
| 637 | } |
| 638 | |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 639 | timeout = redrat3_len_to_us(be32_to_cpu(*tmp)); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 640 | |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 641 | rr3_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 642 | return timeout; |
| 643 | } |
| 644 | |
| 645 | static void redrat3_reset(struct redrat3_dev *rr3) |
| 646 | { |
| 647 | struct usb_device *udev = rr3->udev; |
| 648 | struct device *dev = rr3->dev; |
| 649 | int rc, rxpipe, txpipe; |
| 650 | u8 *val; |
| 651 | int len = sizeof(u8); |
| 652 | |
| 653 | rr3_ftr(dev, "Entering %s\n", __func__); |
| 654 | |
| 655 | rxpipe = usb_rcvctrlpipe(udev, 0); |
| 656 | txpipe = usb_sndctrlpipe(udev, 0); |
| 657 | |
| 658 | val = kzalloc(len, GFP_KERNEL); |
| 659 | if (!val) { |
| 660 | dev_err(dev, "Memory allocation failure\n"); |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | *val = 0x01; |
| 665 | rc = usb_control_msg(udev, rxpipe, RR3_RESET, |
| 666 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 667 | RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25); |
| 668 | rr3_dbg(dev, "reset returned 0x%02x\n", rc); |
| 669 | |
| 670 | *val = 5; |
| 671 | rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, |
| 672 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 673 | RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25); |
| 674 | rr3_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc); |
| 675 | |
| 676 | *val = RR3_DRIVER_MAXLENS; |
| 677 | rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM, |
| 678 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 679 | RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25); |
| 680 | rr3_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc); |
| 681 | |
| 682 | kfree(val); |
| 683 | } |
| 684 | |
| 685 | static void redrat3_get_firmware_rev(struct redrat3_dev *rr3) |
| 686 | { |
| 687 | int rc = 0; |
| 688 | char *buffer; |
| 689 | |
| 690 | rr3_ftr(rr3->dev, "Entering %s\n", __func__); |
| 691 | |
| 692 | buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL); |
| 693 | if (!buffer) { |
| 694 | dev_err(rr3->dev, "Memory allocation failure\n"); |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0), |
| 699 | RR3_FW_VERSION, |
| 700 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 701 | 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5); |
| 702 | |
| 703 | if (rc >= 0) |
| 704 | dev_info(rr3->dev, "Firmware rev: %s", buffer); |
| 705 | else |
| 706 | dev_err(rr3->dev, "Problem fetching firmware ID\n"); |
| 707 | |
| 708 | kfree(buffer); |
| 709 | rr3_ftr(rr3->dev, "Exiting %s\n", __func__); |
| 710 | } |
| 711 | |
| 712 | static void redrat3_read_packet_start(struct redrat3_dev *rr3, int len) |
| 713 | { |
| 714 | u16 tx_error; |
| 715 | u16 hdrlen; |
| 716 | |
| 717 | rr3_ftr(rr3->dev, "Entering %s\n", __func__); |
| 718 | |
| 719 | /* grab the Length and type of transfer */ |
| 720 | memcpy(&(rr3->pktlen), (unsigned char *) rr3->bulk_in_buf, |
| 721 | sizeof(rr3->pktlen)); |
| 722 | memcpy(&(rr3->pkttype), ((unsigned char *) rr3->bulk_in_buf + |
| 723 | sizeof(rr3->pktlen)), |
| 724 | sizeof(rr3->pkttype)); |
| 725 | |
| 726 | /*data needs conversion to know what its real values are*/ |
| 727 | rr3->pktlen = be16_to_cpu(rr3->pktlen); |
| 728 | rr3->pkttype = be16_to_cpu(rr3->pkttype); |
| 729 | |
| 730 | switch (rr3->pkttype) { |
| 731 | case RR3_ERROR: |
| 732 | memcpy(&tx_error, ((unsigned char *)rr3->bulk_in_buf |
| 733 | + (sizeof(rr3->pktlen) + sizeof(rr3->pkttype))), |
| 734 | sizeof(tx_error)); |
| 735 | tx_error = be16_to_cpu(tx_error); |
| 736 | redrat3_dump_fw_error(rr3, tx_error); |
| 737 | break; |
| 738 | |
| 739 | case RR3_MOD_SIGNAL_IN: |
| 740 | hdrlen = sizeof(rr3->pktlen) + sizeof(rr3->pkttype); |
| 741 | rr3->bytes_read = len; |
| 742 | rr3->bytes_read -= hdrlen; |
| 743 | rr3->datap = &(rr3->pbuf[0]); |
| 744 | |
| 745 | memcpy(rr3->datap, ((unsigned char *)rr3->bulk_in_buf + hdrlen), |
| 746 | rr3->bytes_read); |
| 747 | rr3->datap += rr3->bytes_read; |
| 748 | rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", |
| 749 | rr3->bytes_read, rr3->pktlen); |
| 750 | break; |
| 751 | |
| 752 | default: |
| 753 | rr3_dbg(rr3->dev, "ignoring packet with type 0x%02x, " |
| 754 | "len of %d, 0x%02x\n", rr3->pkttype, len, rr3->pktlen); |
| 755 | break; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | static void redrat3_read_packet_continue(struct redrat3_dev *rr3, int len) |
| 760 | { |
| 761 | |
| 762 | rr3_ftr(rr3->dev, "Entering %s\n", __func__); |
| 763 | |
| 764 | memcpy(rr3->datap, (unsigned char *)rr3->bulk_in_buf, len); |
| 765 | rr3->datap += len; |
| 766 | |
| 767 | rr3->bytes_read += len; |
| 768 | rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", |
| 769 | rr3->bytes_read, rr3->pktlen); |
| 770 | } |
| 771 | |
| 772 | /* gather IR data from incoming urb, process it when we have enough */ |
| 773 | static int redrat3_get_ir_data(struct redrat3_dev *rr3, int len) |
| 774 | { |
| 775 | struct device *dev = rr3->dev; |
| 776 | int ret = 0; |
| 777 | |
| 778 | rr3_ftr(dev, "Entering %s\n", __func__); |
| 779 | |
| 780 | if (rr3->pktlen > RR3_MAX_BUF_SIZE) { |
| 781 | dev_err(rr3->dev, "error: packet larger than buffer\n"); |
| 782 | ret = -EINVAL; |
| 783 | goto out; |
| 784 | } |
| 785 | |
| 786 | if ((rr3->bytes_read == 0) && |
| 787 | (len >= (sizeof(rr3->pkttype) + sizeof(rr3->pktlen)))) { |
| 788 | redrat3_read_packet_start(rr3, len); |
| 789 | } else if (rr3->bytes_read != 0) { |
| 790 | redrat3_read_packet_continue(rr3, len); |
| 791 | } else if (rr3->bytes_read == 0) { |
| 792 | dev_err(dev, "error: no packet data read\n"); |
| 793 | ret = -ENODATA; |
| 794 | goto out; |
| 795 | } |
| 796 | |
| 797 | if (rr3->bytes_read > rr3->pktlen) { |
| 798 | dev_err(dev, "bytes_read (%d) greater than pktlen (%d)\n", |
| 799 | rr3->bytes_read, rr3->pktlen); |
| 800 | ret = -EINVAL; |
| 801 | goto out; |
| 802 | } else if (rr3->bytes_read < rr3->pktlen) |
| 803 | /* we're still accumulating data */ |
| 804 | return 0; |
| 805 | |
| 806 | /* if we get here, we've got IR data to decode */ |
| 807 | if (rr3->pkttype == RR3_MOD_SIGNAL_IN) |
| 808 | redrat3_process_ir_data(rr3); |
| 809 | else |
| 810 | rr3_dbg(dev, "discarding non-signal data packet " |
| 811 | "(type 0x%02x)\n", rr3->pkttype); |
| 812 | |
| 813 | out: |
| 814 | rr3->bytes_read = 0; |
| 815 | rr3->pktlen = 0; |
| 816 | rr3->pkttype = 0; |
| 817 | return ret; |
| 818 | } |
| 819 | |
| 820 | /* callback function from USB when async USB request has completed */ |
| 821 | static void redrat3_handle_async(struct urb *urb, struct pt_regs *regs) |
| 822 | { |
| 823 | struct redrat3_dev *rr3; |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 824 | int ret; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 825 | |
| 826 | if (!urb) |
| 827 | return; |
| 828 | |
| 829 | rr3 = urb->context; |
| 830 | if (!rr3) { |
| 831 | pr_err("%s called with invalid context!\n", __func__); |
| 832 | usb_unlink_urb(urb); |
| 833 | return; |
| 834 | } |
| 835 | |
| 836 | rr3_ftr(rr3->dev, "Entering %s\n", __func__); |
| 837 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 838 | switch (urb->status) { |
| 839 | case 0: |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 840 | ret = redrat3_get_ir_data(rr3, urb->actual_length); |
| 841 | if (!ret) { |
| 842 | /* no error, prepare to read more */ |
| 843 | redrat3_issue_async(rr3); |
| 844 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 845 | break; |
| 846 | |
| 847 | case -ECONNRESET: |
| 848 | case -ENOENT: |
| 849 | case -ESHUTDOWN: |
| 850 | usb_unlink_urb(urb); |
| 851 | return; |
| 852 | |
| 853 | case -EPIPE: |
| 854 | default: |
| 855 | dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status); |
| 856 | rr3->bytes_read = 0; |
| 857 | rr3->pktlen = 0; |
| 858 | rr3->pkttype = 0; |
| 859 | break; |
| 860 | } |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | static void redrat3_write_bulk_callback(struct urb *urb, struct pt_regs *regs) |
| 864 | { |
| 865 | struct redrat3_dev *rr3; |
| 866 | int len; |
| 867 | |
| 868 | if (!urb) |
| 869 | return; |
| 870 | |
| 871 | rr3 = urb->context; |
| 872 | if (rr3) { |
| 873 | len = urb->actual_length; |
| 874 | rr3_ftr(rr3->dev, "%s: called (status=%d len=%d)\n", |
| 875 | __func__, urb->status, len); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | static u16 mod_freq_to_val(unsigned int mod_freq) |
| 880 | { |
| 881 | int mult = 6000000; |
| 882 | |
| 883 | /* Clk used in mod. freq. generation is CLK24/4. */ |
| 884 | return (u16)(65536 - (mult / mod_freq)); |
| 885 | } |
| 886 | |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 887 | static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 888 | { |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 889 | struct redrat3_dev *rr3 = rcdev->priv; |
| 890 | struct device *dev = rr3->dev; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 891 | |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 892 | rr3_dbg(dev, "Setting modulation frequency to %u", carrier); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 893 | rr3->carrier = carrier; |
| 894 | |
| 895 | return carrier; |
| 896 | } |
| 897 | |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 898 | static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf, |
| 899 | unsigned count) |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 900 | { |
| 901 | struct redrat3_dev *rr3 = rcdev->priv; |
| 902 | struct device *dev = rr3->dev; |
| 903 | struct redrat3_signal_header header; |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 904 | int i, j, ret, ret_len, offset; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 905 | int lencheck, cur_sample_len, pipe; |
| 906 | char *buffer = NULL, *sigdata = NULL; |
| 907 | int *sample_lens = NULL; |
| 908 | u32 tmpi; |
| 909 | u16 tmps; |
| 910 | u8 *datap; |
| 911 | u8 curlencheck = 0; |
| 912 | u16 *lengths_ptr; |
| 913 | int sendbuf_len; |
| 914 | |
| 915 | rr3_ftr(dev, "Entering %s\n", __func__); |
| 916 | |
| 917 | if (rr3->transmitting) { |
| 918 | dev_warn(dev, "%s: transmitter already in use\n", __func__); |
| 919 | return -EAGAIN; |
| 920 | } |
| 921 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 922 | if (count > (RR3_DRIVER_MAXLENS * 2)) |
| 923 | return -EINVAL; |
| 924 | |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 925 | /* rr3 will disable rc detector on transmit */ |
| 926 | rr3->det_enabled = false; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 927 | rr3->transmitting = true; |
| 928 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 929 | sample_lens = kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS, GFP_KERNEL); |
| 930 | if (!sample_lens) { |
| 931 | ret = -ENOMEM; |
| 932 | goto out; |
| 933 | } |
| 934 | |
| 935 | for (i = 0; i < count; i++) { |
| 936 | for (lencheck = 0; lencheck < curlencheck; lencheck++) { |
| 937 | cur_sample_len = redrat3_us_to_len(txbuf[i]); |
| 938 | if (sample_lens[lencheck] == cur_sample_len) |
| 939 | break; |
| 940 | } |
| 941 | if (lencheck == curlencheck) { |
| 942 | cur_sample_len = redrat3_us_to_len(txbuf[i]); |
| 943 | rr3_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n", |
| 944 | i, txbuf[i], curlencheck, cur_sample_len); |
| 945 | if (curlencheck < 255) { |
| 946 | /* now convert the value to a proper |
| 947 | * rr3 value.. */ |
| 948 | sample_lens[curlencheck] = cur_sample_len; |
| 949 | curlencheck++; |
| 950 | } else { |
| 951 | dev_err(dev, "signal too long\n"); |
| 952 | ret = -EINVAL; |
| 953 | goto out; |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | sigdata = kzalloc((count + RR3_TX_TRAILER_LEN), GFP_KERNEL); |
| 959 | if (!sigdata) { |
| 960 | ret = -ENOMEM; |
| 961 | goto out; |
| 962 | } |
| 963 | |
| 964 | sigdata[count] = RR3_END_OF_SIGNAL; |
| 965 | sigdata[count + 1] = RR3_END_OF_SIGNAL; |
| 966 | for (i = 0; i < count; i++) { |
| 967 | for (j = 0; j < curlencheck; j++) { |
| 968 | if (sample_lens[j] == redrat3_us_to_len(txbuf[i])) |
| 969 | sigdata[i] = j; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | offset = RR3_TX_HEADER_OFFSET; |
| 974 | sendbuf_len = RR3_HEADER_LENGTH + (sizeof(u16) * RR3_DRIVER_MAXLENS) |
| 975 | + count + RR3_TX_TRAILER_LEN + offset; |
| 976 | |
| 977 | buffer = kzalloc(sendbuf_len, GFP_KERNEL); |
| 978 | if (!buffer) { |
| 979 | ret = -ENOMEM; |
| 980 | goto out; |
| 981 | } |
| 982 | |
| 983 | /* fill in our packet header */ |
| 984 | header.length = sendbuf_len - offset; |
| 985 | header.transfer_type = RR3_MOD_SIGNAL_OUT; |
| 986 | header.pause = redrat3_len_to_us(100); |
| 987 | header.mod_freq_count = mod_freq_to_val(rr3->carrier); |
| 988 | header.no_periods = 0; /* n/a to transmit */ |
| 989 | header.max_lengths = RR3_DRIVER_MAXLENS; |
| 990 | header.no_lengths = curlencheck; |
| 991 | header.max_sig_size = RR3_MAX_SIG_SIZE; |
| 992 | header.sig_size = count + RR3_TX_TRAILER_LEN; |
| 993 | /* we currently rely on repeat handling in the IR encoding source */ |
| 994 | header.no_repeats = 0; |
| 995 | |
| 996 | tmps = cpu_to_be16(header.length); |
| 997 | memcpy(buffer, &tmps, 2); |
| 998 | |
| 999 | tmps = cpu_to_be16(header.transfer_type); |
| 1000 | memcpy(buffer + 2, &tmps, 2); |
| 1001 | |
| 1002 | tmpi = cpu_to_be32(header.pause); |
| 1003 | memcpy(buffer + offset, &tmpi, sizeof(tmpi)); |
| 1004 | |
| 1005 | tmps = cpu_to_be16(header.mod_freq_count); |
| 1006 | memcpy(buffer + offset + RR3_FREQ_COUNT_OFFSET, &tmps, 2); |
| 1007 | |
| 1008 | buffer[offset + RR3_NUM_LENGTHS_OFFSET] = header.no_lengths; |
| 1009 | |
| 1010 | tmps = cpu_to_be16(header.sig_size); |
| 1011 | memcpy(buffer + offset + RR3_NUM_SIGS_OFFSET, &tmps, 2); |
| 1012 | |
| 1013 | buffer[offset + RR3_REPEATS_OFFSET] = header.no_repeats; |
| 1014 | |
| 1015 | lengths_ptr = (u16 *)(buffer + offset + RR3_HEADER_LENGTH); |
| 1016 | for (i = 0; i < curlencheck; ++i) |
| 1017 | lengths_ptr[i] = cpu_to_be16(sample_lens[i]); |
| 1018 | |
| 1019 | datap = (u8 *)(buffer + offset + RR3_HEADER_LENGTH + |
| 1020 | (sizeof(u16) * RR3_DRIVER_MAXLENS)); |
| 1021 | memcpy(datap, sigdata, (count + RR3_TX_TRAILER_LEN)); |
| 1022 | |
| 1023 | if (debug) { |
| 1024 | redrat3_dump_signal_header(&header); |
| 1025 | redrat3_dump_signal_data(buffer, header.sig_size); |
| 1026 | } |
| 1027 | |
| 1028 | pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress); |
| 1029 | tmps = usb_bulk_msg(rr3->udev, pipe, buffer, |
| 1030 | sendbuf_len, &ret_len, 10 * HZ); |
| 1031 | rr3_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, tmps); |
| 1032 | |
| 1033 | /* now tell the hardware to transmit what we sent it */ |
| 1034 | pipe = usb_rcvctrlpipe(rr3->udev, 0); |
| 1035 | ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL, |
| 1036 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 1037 | 0, 0, buffer, 2, HZ * 10); |
| 1038 | |
| 1039 | if (ret < 0) |
| 1040 | dev_err(dev, "Error: control msg send failed, rc %d\n", ret); |
| 1041 | else |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 1042 | ret = count; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1043 | |
| 1044 | out: |
| 1045 | kfree(sample_lens); |
| 1046 | kfree(buffer); |
| 1047 | kfree(sigdata); |
| 1048 | |
| 1049 | rr3->transmitting = false; |
Andrew Vincer | dbea188 | 2011-11-08 12:43:45 -0300 | [diff] [blame] | 1050 | /* rr3 re-enables rc detector because it was enabled before */ |
| 1051 | rr3->det_enabled = true; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1052 | |
| 1053 | return ret; |
| 1054 | } |
| 1055 | |
| 1056 | static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3) |
| 1057 | { |
| 1058 | struct device *dev = rr3->dev; |
| 1059 | struct rc_dev *rc; |
| 1060 | int ret = -ENODEV; |
| 1061 | u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct); |
| 1062 | |
| 1063 | rc = rc_allocate_device(); |
| 1064 | if (!rc) { |
| 1065 | dev_err(dev, "remote input dev allocation failed\n"); |
| 1066 | goto out; |
| 1067 | } |
| 1068 | |
| 1069 | snprintf(rr3->name, sizeof(rr3->name), "RedRat3%s " |
| 1070 | "Infrared Remote Transceiver (%04x:%04x)", |
| 1071 | prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "", |
| 1072 | le16_to_cpu(rr3->udev->descriptor.idVendor), prod); |
| 1073 | |
| 1074 | usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys)); |
| 1075 | |
| 1076 | rc->input_name = rr3->name; |
| 1077 | rc->input_phys = rr3->phys; |
| 1078 | usb_to_input_id(rr3->udev, &rc->input_id); |
| 1079 | rc->dev.parent = dev; |
| 1080 | rc->priv = rr3; |
| 1081 | rc->driver_type = RC_DRIVER_IR_RAW; |
| 1082 | rc->allowed_protos = RC_TYPE_ALL; |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 1083 | rc->timeout = US_TO_NS(2750); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1084 | rc->tx_ir = redrat3_transmit_ir; |
| 1085 | rc->s_tx_carrier = redrat3_set_tx_carrier; |
| 1086 | rc->driver_name = DRIVER_NAME; |
| 1087 | rc->map_name = RC_MAP_HAUPPAUGE; |
| 1088 | |
| 1089 | ret = rc_register_device(rc); |
| 1090 | if (ret < 0) { |
| 1091 | dev_err(dev, "remote dev registration failed\n"); |
| 1092 | goto out; |
| 1093 | } |
| 1094 | |
| 1095 | return rc; |
| 1096 | |
| 1097 | out: |
| 1098 | rc_free_device(rc); |
| 1099 | return NULL; |
| 1100 | } |
| 1101 | |
| 1102 | static int __devinit redrat3_dev_probe(struct usb_interface *intf, |
| 1103 | const struct usb_device_id *id) |
| 1104 | { |
| 1105 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1106 | struct device *dev = &intf->dev; |
| 1107 | struct usb_host_interface *uhi; |
| 1108 | struct redrat3_dev *rr3; |
| 1109 | struct usb_endpoint_descriptor *ep; |
| 1110 | struct usb_endpoint_descriptor *ep_in = NULL; |
| 1111 | struct usb_endpoint_descriptor *ep_out = NULL; |
| 1112 | u8 addr, attrs; |
| 1113 | int pipe, i; |
| 1114 | int retval = -ENOMEM; |
| 1115 | |
| 1116 | rr3_ftr(dev, "%s called\n", __func__); |
| 1117 | |
| 1118 | uhi = intf->cur_altsetting; |
| 1119 | |
| 1120 | /* find our bulk-in and bulk-out endpoints */ |
| 1121 | for (i = 0; i < uhi->desc.bNumEndpoints; ++i) { |
| 1122 | ep = &uhi->endpoint[i].desc; |
| 1123 | addr = ep->bEndpointAddress; |
| 1124 | attrs = ep->bmAttributes; |
| 1125 | |
| 1126 | if ((ep_in == NULL) && |
| 1127 | ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) && |
| 1128 | ((attrs & USB_ENDPOINT_XFERTYPE_MASK) == |
| 1129 | USB_ENDPOINT_XFER_BULK)) { |
| 1130 | rr3_dbg(dev, "found bulk-in endpoint at 0x%02x\n", |
| 1131 | ep->bEndpointAddress); |
| 1132 | /* data comes in on 0x82, 0x81 is for other data... */ |
| 1133 | if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR) |
| 1134 | ep_in = ep; |
| 1135 | } |
| 1136 | |
| 1137 | if ((ep_out == NULL) && |
| 1138 | ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) && |
| 1139 | ((attrs & USB_ENDPOINT_XFERTYPE_MASK) == |
| 1140 | USB_ENDPOINT_XFER_BULK)) { |
| 1141 | rr3_dbg(dev, "found bulk-out endpoint at 0x%02x\n", |
| 1142 | ep->bEndpointAddress); |
| 1143 | ep_out = ep; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | if (!ep_in || !ep_out) { |
| 1148 | dev_err(dev, "Couldn't find both in and out endpoints\n"); |
| 1149 | retval = -ENODEV; |
| 1150 | goto no_endpoints; |
| 1151 | } |
| 1152 | |
| 1153 | /* allocate memory for our device state and initialize it */ |
| 1154 | rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL); |
| 1155 | if (rr3 == NULL) { |
| 1156 | dev_err(dev, "Memory allocation failure\n"); |
Dan Carpenter | 7eb7571 | 2011-05-26 05:55:08 -0300 | [diff] [blame] | 1157 | goto no_endpoints; |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | rr3->dev = &intf->dev; |
| 1161 | |
| 1162 | /* set up bulk-in endpoint */ |
| 1163 | rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1164 | if (!rr3->read_urb) { |
| 1165 | dev_err(dev, "Read urb allocation failure\n"); |
| 1166 | goto error; |
| 1167 | } |
| 1168 | |
| 1169 | rr3->ep_in = ep_in; |
| 1170 | rr3->bulk_in_buf = usb_alloc_coherent(udev, ep_in->wMaxPacketSize, |
| 1171 | GFP_ATOMIC, &rr3->dma_in); |
| 1172 | if (!rr3->bulk_in_buf) { |
| 1173 | dev_err(dev, "Read buffer allocation failure\n"); |
| 1174 | goto error; |
| 1175 | } |
| 1176 | |
| 1177 | pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress); |
| 1178 | usb_fill_bulk_urb(rr3->read_urb, udev, pipe, |
| 1179 | rr3->bulk_in_buf, ep_in->wMaxPacketSize, |
| 1180 | (usb_complete_t)redrat3_handle_async, rr3); |
| 1181 | |
| 1182 | /* set up bulk-out endpoint*/ |
| 1183 | rr3->write_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1184 | if (!rr3->write_urb) { |
| 1185 | dev_err(dev, "Write urb allocation failure\n"); |
| 1186 | goto error; |
| 1187 | } |
| 1188 | |
| 1189 | rr3->ep_out = ep_out; |
| 1190 | rr3->bulk_out_buf = usb_alloc_coherent(udev, ep_out->wMaxPacketSize, |
| 1191 | GFP_ATOMIC, &rr3->dma_out); |
| 1192 | if (!rr3->bulk_out_buf) { |
| 1193 | dev_err(dev, "Write buffer allocation failure\n"); |
| 1194 | goto error; |
| 1195 | } |
| 1196 | |
| 1197 | pipe = usb_sndbulkpipe(udev, ep_out->bEndpointAddress); |
| 1198 | usb_fill_bulk_urb(rr3->write_urb, udev, pipe, |
| 1199 | rr3->bulk_out_buf, ep_out->wMaxPacketSize, |
| 1200 | (usb_complete_t)redrat3_write_bulk_callback, rr3); |
| 1201 | |
| 1202 | mutex_init(&rr3->lock); |
| 1203 | rr3->udev = udev; |
| 1204 | |
| 1205 | redrat3_reset(rr3); |
| 1206 | redrat3_get_firmware_rev(rr3); |
| 1207 | |
| 1208 | /* might be all we need to do? */ |
| 1209 | retval = redrat3_enable_detector(rr3); |
| 1210 | if (retval < 0) |
| 1211 | goto error; |
| 1212 | |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 1213 | /* store current hardware timeout, in us, will use for kfifo resets */ |
| 1214 | rr3->hw_timeout = redrat3_get_timeout(rr3); |
| 1215 | |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1216 | /* default.. will get overridden by any sends with a freq defined */ |
| 1217 | rr3->carrier = 38000; |
| 1218 | |
| 1219 | rr3->rc = redrat3_init_rc_dev(rr3); |
| 1220 | if (!rr3->rc) |
| 1221 | goto error; |
| 1222 | |
| 1223 | setup_timer(&rr3->rx_timeout, redrat3_rx_timeout, (unsigned long)rr3); |
| 1224 | |
| 1225 | /* we can register the device now, as it is ready */ |
| 1226 | usb_set_intfdata(intf, rr3); |
| 1227 | |
| 1228 | rr3_ftr(dev, "Exiting %s\n", __func__); |
| 1229 | return 0; |
| 1230 | |
| 1231 | error: |
| 1232 | redrat3_delete(rr3, rr3->udev); |
| 1233 | |
| 1234 | no_endpoints: |
| 1235 | dev_err(dev, "%s: retval = %x", __func__, retval); |
| 1236 | |
| 1237 | return retval; |
| 1238 | } |
| 1239 | |
| 1240 | static void __devexit redrat3_dev_disconnect(struct usb_interface *intf) |
| 1241 | { |
| 1242 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1243 | struct redrat3_dev *rr3 = usb_get_intfdata(intf); |
| 1244 | |
| 1245 | rr3_ftr(&intf->dev, "Entering %s\n", __func__); |
| 1246 | |
| 1247 | if (!rr3) |
| 1248 | return; |
| 1249 | |
| 1250 | redrat3_disable_detector(rr3); |
| 1251 | |
| 1252 | usb_set_intfdata(intf, NULL); |
| 1253 | rc_unregister_device(rr3->rc); |
Jarod Wilson | c53f9f0 | 2011-07-13 18:26:07 -0300 | [diff] [blame] | 1254 | del_timer_sync(&rr3->rx_timeout); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1255 | redrat3_delete(rr3, udev); |
| 1256 | |
| 1257 | rr3_ftr(&intf->dev, "RedRat3 IR Transceiver now disconnected\n"); |
| 1258 | } |
| 1259 | |
| 1260 | static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message) |
| 1261 | { |
| 1262 | struct redrat3_dev *rr3 = usb_get_intfdata(intf); |
| 1263 | rr3_ftr(rr3->dev, "suspend\n"); |
| 1264 | usb_kill_urb(rr3->read_urb); |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | static int redrat3_dev_resume(struct usb_interface *intf) |
| 1269 | { |
| 1270 | struct redrat3_dev *rr3 = usb_get_intfdata(intf); |
| 1271 | rr3_ftr(rr3->dev, "resume\n"); |
| 1272 | if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC)) |
| 1273 | return -EIO; |
| 1274 | return 0; |
| 1275 | } |
| 1276 | |
| 1277 | static struct usb_driver redrat3_dev_driver = { |
| 1278 | .name = DRIVER_NAME, |
| 1279 | .probe = redrat3_dev_probe, |
| 1280 | .disconnect = redrat3_dev_disconnect, |
| 1281 | .suspend = redrat3_dev_suspend, |
| 1282 | .resume = redrat3_dev_resume, |
| 1283 | .reset_resume = redrat3_dev_resume, |
| 1284 | .id_table = redrat3_dev_table |
| 1285 | }; |
| 1286 | |
Greg Kroah-Hartman | ecb3b2b | 2011-11-18 09:46:12 -0800 | [diff] [blame] | 1287 | module_usb_driver(redrat3_dev_driver); |
Jarod Wilson | 2154be6 | 2011-05-04 14:02:42 -0300 | [diff] [blame] | 1288 | |
| 1289 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 1290 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1291 | MODULE_AUTHOR(DRIVER_AUTHOR2); |
| 1292 | MODULE_LICENSE("GPL"); |
| 1293 | MODULE_DEVICE_TABLE(usb, redrat3_dev_table); |
| 1294 | |
| 1295 | module_param(debug, int, S_IRUGO | S_IWUSR); |
| 1296 | MODULE_PARM_DESC(debug, "Enable module debug spew. 0 = no debugging (default) " |
| 1297 | "0x1 = standard debug messages, 0x2 = function tracing debug. " |
| 1298 | "Flag bits are addative (i.e., 0x3 for both debug types)."); |