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