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