blob: 12167a6b5472d02e242b7b686a068f90e68d3797 [file] [log] [blame]
Jarod Wilson2154be62011-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
Sean Young4c055a52013-02-16 17:25:44 -030048#include <asm/unaligned.h>
Jarod Wilson2154be62011-05-04 14:02:42 -030049#include <linux/device.h>
50#include <linux/module.h>
51#include <linux/slab.h>
52#include <linux/usb.h>
53#include <linux/usb/input.h>
54#include <media/rc-core.h>
55
56/* Driver Information */
Jarod Wilson2154be62011-05-04 14:02:42 -030057#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
Jarod Wilson2154be62011-05-04 14:02:42 -0300132/* Size of the fixed-length portion of the signal */
Jarod Wilson2154be62011-05-04 14:02:42 -0300133#define RR3_DRIVER_MAXLENS 128
134#define RR3_MAX_SIG_SIZE 512
Jarod Wilson2154be62011-05-04 14:02:42 -0300135#define RR3_TIME_UNIT 50
136#define RR3_END_OF_SIGNAL 0x7f
Jarod Wilson2154be62011-05-04 14:02:42 -0300137#define RR3_TX_TRAILER_LEN 2
138#define RR3_RX_MIN_TIMEOUT 5
139#define RR3_RX_MAX_TIMEOUT 2000
140
141/* The 8051's CPUCS Register address */
142#define RR3_CPUCS_REG_ADDR 0x7f92
143
144#define USB_RR3USB_VENDOR_ID 0x112a
145#define USB_RR3USB_PRODUCT_ID 0x0001
146#define USB_RR3IIUSB_PRODUCT_ID 0x0005
147
Sean Young4c055a52013-02-16 17:25:44 -0300148struct redrat3_header {
149 __be16 length;
150 __be16 transfer_type;
151} __packed;
152
153/* sending and receiving irdata */
154struct redrat3_irdata {
155 struct redrat3_header header;
156 __be32 pause;
157 __be16 mod_freq_count;
158 __be16 num_periods;
159 __u8 max_lengths;
160 __u8 no_lengths;
161 __be16 max_sig_size;
162 __be16 sig_size;
163 __u8 no_repeats;
164 __be16 lens[RR3_DRIVER_MAXLENS]; /* not aligned */
165 __u8 sigdata[RR3_MAX_SIG_SIZE];
166} __packed;
167
168/* firmware errors */
169struct redrat3_error {
170 struct redrat3_header header;
171 __be16 fw_error;
172} __packed;
173
Jarod Wilson2154be62011-05-04 14:02:42 -0300174/* table of devices that work with this driver */
175static struct usb_device_id redrat3_dev_table[] = {
176 /* Original version of the RedRat3 */
177 {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)},
178 /* Second Version/release of the RedRat3 - RetRat3-II */
179 {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)},
180 {} /* Terminating entry */
181};
182
183/* Structure to hold all of our device specific stuff */
184struct redrat3_dev {
185 /* core device bits */
186 struct rc_dev *rc;
187 struct device *dev;
188
189 /* save off the usb device pointer */
190 struct usb_device *udev;
191
192 /* the receive endpoint */
193 struct usb_endpoint_descriptor *ep_in;
194 /* the buffer to receive data */
Sean Young4c055a52013-02-16 17:25:44 -0300195 void *bulk_in_buf;
Jarod Wilson2154be62011-05-04 14:02:42 -0300196 /* urb used to read ir data */
197 struct urb *read_urb;
198
199 /* the send endpoint */
200 struct usb_endpoint_descriptor *ep_out;
Jarod Wilson2154be62011-05-04 14:02:42 -0300201
202 /* usb dma */
203 dma_addr_t dma_in;
Jarod Wilson2154be62011-05-04 14:02:42 -0300204
Jarod Wilson2154be62011-05-04 14:02:42 -0300205 /* rx signal timeout timer */
206 struct timer_list rx_timeout;
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300207 u32 hw_timeout;
Jarod Wilson2154be62011-05-04 14:02:42 -0300208
Jarod Wilson2154be62011-05-04 14:02:42 -0300209 /* is the detector enabled*/
210 bool det_enabled;
211 /* Is the device currently transmitting?*/
212 bool transmitting;
213
214 /* store for current packet */
Sean Young4c055a52013-02-16 17:25:44 -0300215 struct redrat3_irdata irdata;
Jarod Wilson2154be62011-05-04 14:02:42 -0300216 u16 bytes_read;
Jarod Wilson2154be62011-05-04 14:02:42 -0300217
218 u32 carrier;
219
Sean Young4c055a52013-02-16 17:25:44 -0300220 char name[64];
Jarod Wilson2154be62011-05-04 14:02:42 -0300221 char phys[64];
222};
223
Jarod Wilson2154be62011-05-04 14:02:42 -0300224/*
225 * redrat3_issue_async
226 *
227 * Issues an async read to the ir data in port..
228 * sets the callback to be redrat3_handle_async
229 */
230static void redrat3_issue_async(struct redrat3_dev *rr3)
231{
232 int res;
233
234 rr3_ftr(rr3->dev, "Entering %s\n", __func__);
235
Jarod Wilson2154be62011-05-04 14:02:42 -0300236 res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC);
237 if (res)
238 rr3_dbg(rr3->dev, "%s: receive request FAILED! "
239 "(res %d, len %d)\n", __func__, res,
240 rr3->read_urb->transfer_buffer_length);
241}
242
243static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
244{
245 if (!rr3->transmitting && (code != 0x40))
246 dev_info(rr3->dev, "fw error code 0x%02x: ", code);
247
248 switch (code) {
249 case 0x00:
250 pr_cont("No Error\n");
251 break;
252
253 /* Codes 0x20 through 0x2f are IR Firmware Errors */
254 case 0x20:
255 pr_cont("Initial signal pulse not long enough "
256 "to measure carrier frequency\n");
257 break;
258 case 0x21:
259 pr_cont("Not enough length values allocated for signal\n");
260 break;
261 case 0x22:
262 pr_cont("Not enough memory allocated for signal data\n");
263 break;
264 case 0x23:
265 pr_cont("Too many signal repeats\n");
266 break;
267 case 0x28:
268 pr_cont("Insufficient memory available for IR signal "
269 "data memory allocation\n");
270 break;
271 case 0x29:
272 pr_cont("Insufficient memory available "
273 "for IrDa signal data memory allocation\n");
274 break;
275
276 /* Codes 0x30 through 0x3f are USB Firmware Errors */
277 case 0x30:
278 pr_cont("Insufficient memory available for bulk "
279 "transfer structure\n");
280 break;
281
282 /*
283 * Other error codes... These are primarily errors that can occur in
284 * the control messages sent to the redrat
285 */
286 case 0x40:
287 if (!rr3->transmitting)
288 pr_cont("Signal capture has been terminated\n");
289 break;
290 case 0x41:
291 pr_cont("Attempt to set/get and unknown signal I/O "
292 "algorithm parameter\n");
293 break;
294 case 0x42:
295 pr_cont("Signal capture already started\n");
296 break;
297
298 default:
299 pr_cont("Unknown Error\n");
300 break;
301 }
302}
303
Sean Young4c055a52013-02-16 17:25:44 -0300304static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata)
Jarod Wilson2154be62011-05-04 14:02:42 -0300305{
306 u32 mod_freq = 0;
Sean Young4c055a52013-02-16 17:25:44 -0300307 u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count);
Jarod Wilson2154be62011-05-04 14:02:42 -0300308
Sean Young4c055a52013-02-16 17:25:44 -0300309 if (mod_freq_count != 0)
310 mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) /
311 (mod_freq_count * RR3_CLK_PER_COUNT);
Jarod Wilson2154be62011-05-04 14:02:42 -0300312
313 return mod_freq;
314}
315
316/* this function scales down the figures for the same result... */
317static u32 redrat3_len_to_us(u32 length)
318{
319 u32 biglen = length * 1000;
320 u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000;
321 u32 result = (u32) (biglen / divisor);
322
323 /* don't allow zero lengths to go back, breaks lirc */
324 return result ? result : 1;
325}
326
327/*
328 * convert us back into redrat3 lengths
329 *
330 * length * 1000 length * 1000000
331 * ------------- = ---------------- = micro
332 * rr3clk / 1000 rr3clk
333
334 * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
335 * ----- = 4 ----- = 6 -------------- = len ---------------------
336 * 3 2 1000000 1000
337 */
338static u32 redrat3_us_to_len(u32 microsec)
339{
340 u32 result;
341 u32 divisor;
342
343 microsec &= IR_MAX_DURATION;
344 divisor = (RR3_CLK_CONV_FACTOR / 1000);
345 result = (u32)(microsec * divisor) / 1000;
346
347 /* don't allow zero lengths to go back, breaks lirc */
348 return result ? result : 1;
Jarod Wilson2154be62011-05-04 14:02:42 -0300349}
350
Jarod Wilson68b2a692011-07-13 18:26:05 -0300351/* timer callback to send reset event */
Jarod Wilson2154be62011-05-04 14:02:42 -0300352static void redrat3_rx_timeout(unsigned long data)
353{
354 struct redrat3_dev *rr3 = (struct redrat3_dev *)data;
Jarod Wilson2154be62011-05-04 14:02:42 -0300355
356 rr3_dbg(rr3->dev, "calling ir_raw_event_reset\n");
357 ir_raw_event_reset(rr3->rc);
358}
359
360static void redrat3_process_ir_data(struct redrat3_dev *rr3)
361{
362 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson2154be62011-05-04 14:02:42 -0300363 struct device *dev;
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300364 unsigned i, trailer = 0;
Sean Young4c055a52013-02-16 17:25:44 -0300365 unsigned sig_size, single_len, offset, val;
Jarod Wilson2154be62011-05-04 14:02:42 -0300366 unsigned long delay;
Sean Young4c055a52013-02-16 17:25:44 -0300367 u32 mod_freq;
Jarod Wilson2154be62011-05-04 14:02:42 -0300368
369 if (!rr3) {
370 pr_err("%s called with no context!\n", __func__);
371 return;
372 }
373
374 rr3_ftr(rr3->dev, "Entered %s\n", __func__);
375
376 dev = rr3->dev;
Jarod Wilson2154be62011-05-04 14:02:42 -0300377
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300378 /* Make sure we reset the IR kfifo after a bit of inactivity */
379 delay = usecs_to_jiffies(rr3->hw_timeout);
Jarod Wilson2154be62011-05-04 14:02:42 -0300380 mod_timer(&rr3->rx_timeout, jiffies + delay);
381
Sean Young4c055a52013-02-16 17:25:44 -0300382 mod_freq = redrat3_val_to_mod_freq(&rr3->irdata);
Jarod Wilson2154be62011-05-04 14:02:42 -0300383 rr3_dbg(dev, "Got mod_freq of %u\n", mod_freq);
384
Jarod Wilson2154be62011-05-04 14:02:42 -0300385 /* process each rr3 encoded byte into an int */
Sean Young4c055a52013-02-16 17:25:44 -0300386 sig_size = be16_to_cpu(rr3->irdata.sig_size);
387 for (i = 0; i < sig_size; i++) {
388 offset = rr3->irdata.sigdata[i];
389 val = get_unaligned_be16(&rr3->irdata.lens[offset]);
390 single_len = redrat3_len_to_us(val);
Jarod Wilson2154be62011-05-04 14:02:42 -0300391
Jarod Wilson2154be62011-05-04 14:02:42 -0300392 /* we should always get pulse/space/pulse/space samples */
393 if (i % 2)
394 rawir.pulse = false;
395 else
396 rawir.pulse = true;
397
398 rawir.duration = US_TO_NS(single_len);
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300399 /* Save initial pulse length to fudge trailer */
400 if (i == 0)
401 trailer = rawir.duration;
Jarod Wilson2c594ff2011-07-13 18:26:06 -0300402 /* cap the value to IR_MAX_DURATION */
403 rawir.duration &= IR_MAX_DURATION;
404
Jarod Wilson2154be62011-05-04 14:02:42 -0300405 rr3_dbg(dev, "storing %s with duration %d (i: %d)\n",
406 rawir.pulse ? "pulse" : "space", rawir.duration, i);
407 ir_raw_event_store_with_filter(rr3->rc, &rawir);
408 }
409
410 /* add a trailing space, if need be */
411 if (i % 2) {
412 rawir.pulse = false;
413 /* this duration is made up, and may not be ideal... */
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300414 if (trailer < US_TO_NS(1000))
415 rawir.duration = US_TO_NS(2800);
416 else
417 rawir.duration = trailer;
Jarod Wilson2154be62011-05-04 14:02:42 -0300418 rr3_dbg(dev, "storing trailing space with duration %d\n",
419 rawir.duration);
420 ir_raw_event_store_with_filter(rr3->rc, &rawir);
421 }
422
423 rr3_dbg(dev, "calling ir_raw_event_handle\n");
424 ir_raw_event_handle(rr3->rc);
Jarod Wilson2154be62011-05-04 14:02:42 -0300425}
426
427/* Util fn to send rr3 cmds */
428static u8 redrat3_send_cmd(int cmd, struct redrat3_dev *rr3)
429{
430 struct usb_device *udev;
431 u8 *data;
432 int res;
433
434 data = kzalloc(sizeof(u8), GFP_KERNEL);
435 if (!data)
436 return -ENOMEM;
437
438 udev = rr3->udev;
439 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd,
440 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
441 0x0000, 0x0000, data, sizeof(u8), HZ * 10);
442
443 if (res < 0) {
444 dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d",
445 __func__, res, *data);
446 res = -EIO;
447 } else
Sean Young4c055a52013-02-16 17:25:44 -0300448 res = data[0];
Jarod Wilson2154be62011-05-04 14:02:42 -0300449
450 kfree(data);
451
452 return res;
453}
454
455/* Enables the long range detector and starts async receive */
456static int redrat3_enable_detector(struct redrat3_dev *rr3)
457{
458 struct device *dev = rr3->dev;
459 u8 ret;
460
461 rr3_ftr(dev, "Entering %s\n", __func__);
462
463 ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
464 if (ret != 0)
465 dev_dbg(dev, "%s: unexpected ret of %d\n",
466 __func__, ret);
467
468 ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
469 if (ret != 1) {
470 dev_err(dev, "%s: detector status: %d, should be 1\n",
471 __func__, ret);
472 return -EIO;
473 }
474
475 rr3->det_enabled = true;
476 redrat3_issue_async(rr3);
477
478 return 0;
479}
480
481/* Disables the rr3 long range detector */
482static void redrat3_disable_detector(struct redrat3_dev *rr3)
483{
484 struct device *dev = rr3->dev;
485 u8 ret;
486
487 rr3_ftr(dev, "Entering %s\n", __func__);
488
489 ret = redrat3_send_cmd(RR3_RC_DET_DISABLE, rr3);
490 if (ret != 0)
491 dev_err(dev, "%s: failure!\n", __func__);
492
493 ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
494 if (ret != 0)
495 dev_warn(dev, "%s: detector status: %d, should be 0\n",
496 __func__, ret);
497
498 rr3->det_enabled = false;
499}
500
501static inline void redrat3_delete(struct redrat3_dev *rr3,
502 struct usb_device *udev)
503{
504 rr3_ftr(rr3->dev, "%s cleaning up\n", __func__);
505 usb_kill_urb(rr3->read_urb);
Jarod Wilson2154be62011-05-04 14:02:42 -0300506
507 usb_free_urb(rr3->read_urb);
Jarod Wilson2154be62011-05-04 14:02:42 -0300508
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300509 usb_free_coherent(udev, le16_to_cpu(rr3->ep_in->wMaxPacketSize),
Jarod Wilson2154be62011-05-04 14:02:42 -0300510 rr3->bulk_in_buf, rr3->dma_in);
Jarod Wilson2154be62011-05-04 14:02:42 -0300511
512 kfree(rr3);
513}
514
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300515static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
Jarod Wilson2154be62011-05-04 14:02:42 -0300516{
Sean Young801b69f2013-02-16 17:25:43 -0300517 __be32 *tmp;
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300518 u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
Jarod Wilson2154be62011-05-04 14:02:42 -0300519 int len, ret, pipe;
520
521 len = sizeof(*tmp);
522 tmp = kzalloc(len, GFP_KERNEL);
523 if (!tmp) {
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300524 dev_warn(rr3->dev, "Memory allocation faillure\n");
Jarod Wilson2154be62011-05-04 14:02:42 -0300525 return timeout;
526 }
527
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300528 pipe = usb_rcvctrlpipe(rr3->udev, 0);
529 ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
Jarod Wilson2154be62011-05-04 14:02:42 -0300530 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
531 RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5);
Sean Young801b69f2013-02-16 17:25:43 -0300532 if (ret != len)
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300533 dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
Sean Young801b69f2013-02-16 17:25:43 -0300534 else {
535 timeout = redrat3_len_to_us(be32_to_cpup(tmp));
536
537 rr3_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
Jarod Wilson2154be62011-05-04 14:02:42 -0300538 }
539
Sean Young801b69f2013-02-16 17:25:43 -0300540 kfree(tmp);
Jarod Wilson2154be62011-05-04 14:02:42 -0300541
Jarod Wilson2154be62011-05-04 14:02:42 -0300542 return timeout;
543}
544
545static void redrat3_reset(struct redrat3_dev *rr3)
546{
547 struct usb_device *udev = rr3->udev;
548 struct device *dev = rr3->dev;
549 int rc, rxpipe, txpipe;
550 u8 *val;
551 int len = sizeof(u8);
552
553 rr3_ftr(dev, "Entering %s\n", __func__);
554
555 rxpipe = usb_rcvctrlpipe(udev, 0);
556 txpipe = usb_sndctrlpipe(udev, 0);
557
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300558 val = kmalloc(len, GFP_KERNEL);
Jarod Wilson2154be62011-05-04 14:02:42 -0300559 if (!val) {
560 dev_err(dev, "Memory allocation failure\n");
561 return;
562 }
563
564 *val = 0x01;
565 rc = usb_control_msg(udev, rxpipe, RR3_RESET,
566 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
567 RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25);
568 rr3_dbg(dev, "reset returned 0x%02x\n", rc);
569
570 *val = 5;
571 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
572 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
573 RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25);
574 rr3_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
575
576 *val = RR3_DRIVER_MAXLENS;
577 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
578 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
579 RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25);
580 rr3_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
581
582 kfree(val);
583}
584
585static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
586{
587 int rc = 0;
588 char *buffer;
589
590 rr3_ftr(rr3->dev, "Entering %s\n", __func__);
591
592 buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL);
593 if (!buffer) {
594 dev_err(rr3->dev, "Memory allocation failure\n");
595 return;
596 }
597
598 rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
599 RR3_FW_VERSION,
600 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
601 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5);
602
603 if (rc >= 0)
604 dev_info(rr3->dev, "Firmware rev: %s", buffer);
605 else
606 dev_err(rr3->dev, "Problem fetching firmware ID\n");
607
608 kfree(buffer);
609 rr3_ftr(rr3->dev, "Exiting %s\n", __func__);
610}
611
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300612static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
Jarod Wilson2154be62011-05-04 14:02:42 -0300613{
Sean Young4c055a52013-02-16 17:25:44 -0300614 struct redrat3_header *header = rr3->bulk_in_buf;
615 unsigned pktlen, pkttype;
Jarod Wilson2154be62011-05-04 14:02:42 -0300616
617 rr3_ftr(rr3->dev, "Entering %s\n", __func__);
618
619 /* grab the Length and type of transfer */
Sean Young4c055a52013-02-16 17:25:44 -0300620 pktlen = be16_to_cpu(header->length);
621 pkttype = be16_to_cpu(header->transfer_type);
Jarod Wilson2154be62011-05-04 14:02:42 -0300622
Sean Young4c055a52013-02-16 17:25:44 -0300623 if (pktlen > sizeof(rr3->irdata)) {
624 dev_warn(rr3->dev, "packet length %u too large\n", pktlen);
625 return;
626 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300627
Sean Young4c055a52013-02-16 17:25:44 -0300628 switch (pkttype) {
Jarod Wilson2154be62011-05-04 14:02:42 -0300629 case RR3_ERROR:
Sean Young4c055a52013-02-16 17:25:44 -0300630 if (len >= sizeof(struct redrat3_error)) {
631 struct redrat3_error *error = rr3->bulk_in_buf;
632 unsigned fw_error = be16_to_cpu(error->fw_error);
633 redrat3_dump_fw_error(rr3, fw_error);
634 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300635 break;
636
637 case RR3_MOD_SIGNAL_IN:
Sean Young4c055a52013-02-16 17:25:44 -0300638 memcpy(&rr3->irdata, rr3->bulk_in_buf, len);
Jarod Wilson2154be62011-05-04 14:02:42 -0300639 rr3->bytes_read = len;
Jarod Wilson2154be62011-05-04 14:02:42 -0300640 rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
Sean Young4c055a52013-02-16 17:25:44 -0300641 rr3->bytes_read, pktlen);
Jarod Wilson2154be62011-05-04 14:02:42 -0300642 break;
643
644 default:
Sean Young4c055a52013-02-16 17:25:44 -0300645 rr3_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
646 pkttype, len, pktlen);
Jarod Wilson2154be62011-05-04 14:02:42 -0300647 break;
648 }
649}
650
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300651static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len)
Jarod Wilson2154be62011-05-04 14:02:42 -0300652{
Sean Young4c055a52013-02-16 17:25:44 -0300653 void *irdata = &rr3->irdata;
654
Jarod Wilson2154be62011-05-04 14:02:42 -0300655 rr3_ftr(rr3->dev, "Entering %s\n", __func__);
656
Sean Young4c055a52013-02-16 17:25:44 -0300657 if (len + rr3->bytes_read > sizeof(rr3->irdata)) {
658 dev_warn(rr3->dev, "too much data for packet\n");
659 rr3->bytes_read = 0;
660 return;
661 }
662
663 memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len);
Jarod Wilson2154be62011-05-04 14:02:42 -0300664
665 rr3->bytes_read += len;
Sean Young4c055a52013-02-16 17:25:44 -0300666 rr3_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read,
667 be16_to_cpu(rr3->irdata.header.length));
Jarod Wilson2154be62011-05-04 14:02:42 -0300668}
669
670/* gather IR data from incoming urb, process it when we have enough */
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300671static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len)
Jarod Wilson2154be62011-05-04 14:02:42 -0300672{
673 struct device *dev = rr3->dev;
Sean Young4c055a52013-02-16 17:25:44 -0300674 unsigned pkttype;
Jarod Wilson2154be62011-05-04 14:02:42 -0300675 int ret = 0;
676
677 rr3_ftr(dev, "Entering %s\n", __func__);
678
Sean Young4c055a52013-02-16 17:25:44 -0300679 if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) {
Jarod Wilson2154be62011-05-04 14:02:42 -0300680 redrat3_read_packet_start(rr3, len);
681 } else if (rr3->bytes_read != 0) {
682 redrat3_read_packet_continue(rr3, len);
683 } else if (rr3->bytes_read == 0) {
684 dev_err(dev, "error: no packet data read\n");
685 ret = -ENODATA;
686 goto out;
687 }
688
Sean Young4c055a52013-02-16 17:25:44 -0300689 if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length))
Jarod Wilson2154be62011-05-04 14:02:42 -0300690 /* we're still accumulating data */
691 return 0;
692
693 /* if we get here, we've got IR data to decode */
Sean Young4c055a52013-02-16 17:25:44 -0300694 pkttype = be16_to_cpu(rr3->irdata.header.transfer_type);
695 if (pkttype == RR3_MOD_SIGNAL_IN)
Jarod Wilson2154be62011-05-04 14:02:42 -0300696 redrat3_process_ir_data(rr3);
697 else
Sean Young4c055a52013-02-16 17:25:44 -0300698 rr3_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n",
699 pkttype);
Jarod Wilson2154be62011-05-04 14:02:42 -0300700
701out:
702 rr3->bytes_read = 0;
Jarod Wilson2154be62011-05-04 14:02:42 -0300703 return ret;
704}
705
706/* callback function from USB when async USB request has completed */
Sean Young801b69f2013-02-16 17:25:43 -0300707static void redrat3_handle_async(struct urb *urb)
Jarod Wilson2154be62011-05-04 14:02:42 -0300708{
709 struct redrat3_dev *rr3;
Andrew Vincerdbea1882011-11-08 12:43:45 -0300710 int ret;
Jarod Wilson2154be62011-05-04 14:02:42 -0300711
712 if (!urb)
713 return;
714
715 rr3 = urb->context;
716 if (!rr3) {
717 pr_err("%s called with invalid context!\n", __func__);
718 usb_unlink_urb(urb);
719 return;
720 }
721
722 rr3_ftr(rr3->dev, "Entering %s\n", __func__);
723
Jarod Wilson2154be62011-05-04 14:02:42 -0300724 switch (urb->status) {
725 case 0:
Andrew Vincerdbea1882011-11-08 12:43:45 -0300726 ret = redrat3_get_ir_data(rr3, urb->actual_length);
727 if (!ret) {
728 /* no error, prepare to read more */
729 redrat3_issue_async(rr3);
730 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300731 break;
732
733 case -ECONNRESET:
734 case -ENOENT:
735 case -ESHUTDOWN:
736 usb_unlink_urb(urb);
737 return;
738
739 case -EPIPE:
740 default:
741 dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
742 rr3->bytes_read = 0;
Jarod Wilson2154be62011-05-04 14:02:42 -0300743 break;
744 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300745}
746
Jarod Wilson2154be62011-05-04 14:02:42 -0300747static u16 mod_freq_to_val(unsigned int mod_freq)
748{
749 int mult = 6000000;
750
751 /* Clk used in mod. freq. generation is CLK24/4. */
Sean Young4c055a52013-02-16 17:25:44 -0300752 return 65536 - (mult / mod_freq);
Jarod Wilson2154be62011-05-04 14:02:42 -0300753}
754
Andrew Vincerdbea1882011-11-08 12:43:45 -0300755static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
Jarod Wilson2154be62011-05-04 14:02:42 -0300756{
Andrew Vincerdbea1882011-11-08 12:43:45 -0300757 struct redrat3_dev *rr3 = rcdev->priv;
758 struct device *dev = rr3->dev;
Jarod Wilson2154be62011-05-04 14:02:42 -0300759
Andrew Vincerdbea1882011-11-08 12:43:45 -0300760 rr3_dbg(dev, "Setting modulation frequency to %u", carrier);
Dan Carpenter48cafec2012-09-11 07:11:24 -0300761 if (carrier == 0)
762 return -EINVAL;
763
Jarod Wilson2154be62011-05-04 14:02:42 -0300764 rr3->carrier = carrier;
765
766 return carrier;
767}
768
Andrew Vincerdbea1882011-11-08 12:43:45 -0300769static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
770 unsigned count)
Jarod Wilson2154be62011-05-04 14:02:42 -0300771{
772 struct redrat3_dev *rr3 = rcdev->priv;
773 struct device *dev = rr3->dev;
Sean Young4c055a52013-02-16 17:25:44 -0300774 struct redrat3_irdata *irdata = NULL;
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300775 int ret, ret_len;
Jarod Wilson2154be62011-05-04 14:02:42 -0300776 int lencheck, cur_sample_len, pipe;
Jarod Wilson2154be62011-05-04 14:02:42 -0300777 int *sample_lens = NULL;
Jarod Wilson2154be62011-05-04 14:02:42 -0300778 u8 curlencheck = 0;
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300779 unsigned i, sendbuf_len;
Jarod Wilson2154be62011-05-04 14:02:42 -0300780
781 rr3_ftr(dev, "Entering %s\n", __func__);
782
783 if (rr3->transmitting) {
784 dev_warn(dev, "%s: transmitter already in use\n", __func__);
785 return -EAGAIN;
786 }
787
Sean Young06eae252013-01-29 08:19:31 -0300788 count = min_t(unsigned, count, RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN);
Jarod Wilson2154be62011-05-04 14:02:42 -0300789
Andrew Vincerdbea1882011-11-08 12:43:45 -0300790 /* rr3 will disable rc detector on transmit */
791 rr3->det_enabled = false;
Jarod Wilson2154be62011-05-04 14:02:42 -0300792 rr3->transmitting = true;
793
Jarod Wilson2154be62011-05-04 14:02:42 -0300794 sample_lens = kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS, GFP_KERNEL);
795 if (!sample_lens) {
796 ret = -ENOMEM;
797 goto out;
798 }
799
Sean Young4c055a52013-02-16 17:25:44 -0300800 irdata = kzalloc(sizeof(*irdata), GFP_KERNEL);
801 if (!irdata) {
Sean Young801b69f2013-02-16 17:25:43 -0300802 ret = -ENOMEM;
803 goto out;
804 }
805
Jarod Wilson2154be62011-05-04 14:02:42 -0300806 for (i = 0; i < count; i++) {
Sean Young06eae252013-01-29 08:19:31 -0300807 cur_sample_len = redrat3_us_to_len(txbuf[i]);
Sean Young801b69f2013-02-16 17:25:43 -0300808 if (cur_sample_len > 0xffff) {
809 dev_warn(dev, "transmit period of %uus truncated to %uus\n",
810 txbuf[i], redrat3_len_to_us(0xffff));
811 cur_sample_len = 0xffff;
812 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300813 for (lencheck = 0; lencheck < curlencheck; lencheck++) {
Jarod Wilson2154be62011-05-04 14:02:42 -0300814 if (sample_lens[lencheck] == cur_sample_len)
815 break;
816 }
817 if (lencheck == curlencheck) {
Jarod Wilson2154be62011-05-04 14:02:42 -0300818 rr3_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
819 i, txbuf[i], curlencheck, cur_sample_len);
Sean Young06eae252013-01-29 08:19:31 -0300820 if (curlencheck < RR3_DRIVER_MAXLENS) {
Jarod Wilson2154be62011-05-04 14:02:42 -0300821 /* now convert the value to a proper
822 * rr3 value.. */
823 sample_lens[curlencheck] = cur_sample_len;
Sean Young4c055a52013-02-16 17:25:44 -0300824 put_unaligned_be16(cur_sample_len,
825 &irdata->lens[curlencheck]);
Jarod Wilson2154be62011-05-04 14:02:42 -0300826 curlencheck++;
827 } else {
Sean Young06eae252013-01-29 08:19:31 -0300828 count = i - 1;
829 break;
Jarod Wilson2154be62011-05-04 14:02:42 -0300830 }
831 }
Sean Young4c055a52013-02-16 17:25:44 -0300832 irdata->sigdata[i] = lencheck;
Jarod Wilson2154be62011-05-04 14:02:42 -0300833 }
834
Sean Young4c055a52013-02-16 17:25:44 -0300835 irdata->sigdata[count] = RR3_END_OF_SIGNAL;
836 irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL;
Jarod Wilson2154be62011-05-04 14:02:42 -0300837
Sean Young4c055a52013-02-16 17:25:44 -0300838 sendbuf_len = offsetof(struct redrat3_irdata,
839 sigdata[count + RR3_TX_TRAILER_LEN]);
Jarod Wilson2154be62011-05-04 14:02:42 -0300840 /* fill in our packet header */
Sean Young4c055a52013-02-16 17:25:44 -0300841 irdata->header.length = cpu_to_be16(sendbuf_len -
842 sizeof(struct redrat3_header));
843 irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT);
844 irdata->pause = cpu_to_be32(redrat3_len_to_us(100));
845 irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier));
846 irdata->no_lengths = curlencheck;
847 irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN);
Jarod Wilson2154be62011-05-04 14:02:42 -0300848
849 pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
Sean Young4c055a52013-02-16 17:25:44 -0300850 ret = usb_bulk_msg(rr3->udev, pipe, irdata,
Jarod Wilson2154be62011-05-04 14:02:42 -0300851 sendbuf_len, &ret_len, 10 * HZ);
Sean Young4c055a52013-02-16 17:25:44 -0300852 rr3_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret);
Jarod Wilson2154be62011-05-04 14:02:42 -0300853
854 /* now tell the hardware to transmit what we sent it */
855 pipe = usb_rcvctrlpipe(rr3->udev, 0);
856 ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
857 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Sean Young4c055a52013-02-16 17:25:44 -0300858 0, 0, irdata, 2, HZ * 10);
Jarod Wilson2154be62011-05-04 14:02:42 -0300859
860 if (ret < 0)
861 dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
862 else
Andrew Vincerdbea1882011-11-08 12:43:45 -0300863 ret = count;
Jarod Wilson2154be62011-05-04 14:02:42 -0300864
865out:
866 kfree(sample_lens);
Sean Young4c055a52013-02-16 17:25:44 -0300867 kfree(irdata);
Jarod Wilson2154be62011-05-04 14:02:42 -0300868
869 rr3->transmitting = false;
Andrew Vincerdbea1882011-11-08 12:43:45 -0300870 /* rr3 re-enables rc detector because it was enabled before */
871 rr3->det_enabled = true;
Jarod Wilson2154be62011-05-04 14:02:42 -0300872
873 return ret;
874}
875
876static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
877{
878 struct device *dev = rr3->dev;
879 struct rc_dev *rc;
880 int ret = -ENODEV;
881 u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
882
883 rc = rc_allocate_device();
884 if (!rc) {
885 dev_err(dev, "remote input dev allocation failed\n");
886 goto out;
887 }
888
889 snprintf(rr3->name, sizeof(rr3->name), "RedRat3%s "
890 "Infrared Remote Transceiver (%04x:%04x)",
891 prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "",
892 le16_to_cpu(rr3->udev->descriptor.idVendor), prod);
893
894 usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
895
896 rc->input_name = rr3->name;
897 rc->input_phys = rr3->phys;
898 usb_to_input_id(rr3->udev, &rc->input_id);
899 rc->dev.parent = dev;
900 rc->priv = rr3;
901 rc->driver_type = RC_DRIVER_IR_RAW;
David Härdemanc003ab12012-10-11 19:11:54 -0300902 rc->allowed_protos = RC_BIT_ALL;
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300903 rc->timeout = US_TO_NS(2750);
Jarod Wilson2154be62011-05-04 14:02:42 -0300904 rc->tx_ir = redrat3_transmit_ir;
905 rc->s_tx_carrier = redrat3_set_tx_carrier;
906 rc->driver_name = DRIVER_NAME;
Sean Young06eae252013-01-29 08:19:31 -0300907 rc->rx_resolution = US_TO_NS(2);
Jarod Wilson2154be62011-05-04 14:02:42 -0300908 rc->map_name = RC_MAP_HAUPPAUGE;
909
910 ret = rc_register_device(rc);
911 if (ret < 0) {
912 dev_err(dev, "remote dev registration failed\n");
913 goto out;
914 }
915
916 return rc;
917
918out:
919 rc_free_device(rc);
920 return NULL;
921}
922
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800923static int redrat3_dev_probe(struct usb_interface *intf,
924 const struct usb_device_id *id)
Jarod Wilson2154be62011-05-04 14:02:42 -0300925{
926 struct usb_device *udev = interface_to_usbdev(intf);
927 struct device *dev = &intf->dev;
928 struct usb_host_interface *uhi;
929 struct redrat3_dev *rr3;
930 struct usb_endpoint_descriptor *ep;
931 struct usb_endpoint_descriptor *ep_in = NULL;
932 struct usb_endpoint_descriptor *ep_out = NULL;
933 u8 addr, attrs;
934 int pipe, i;
935 int retval = -ENOMEM;
936
937 rr3_ftr(dev, "%s called\n", __func__);
938
939 uhi = intf->cur_altsetting;
940
941 /* find our bulk-in and bulk-out endpoints */
942 for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
943 ep = &uhi->endpoint[i].desc;
944 addr = ep->bEndpointAddress;
945 attrs = ep->bmAttributes;
946
947 if ((ep_in == NULL) &&
948 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
949 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
950 USB_ENDPOINT_XFER_BULK)) {
951 rr3_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
952 ep->bEndpointAddress);
953 /* data comes in on 0x82, 0x81 is for other data... */
954 if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR)
955 ep_in = ep;
956 }
957
958 if ((ep_out == NULL) &&
959 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
960 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
961 USB_ENDPOINT_XFER_BULK)) {
962 rr3_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
963 ep->bEndpointAddress);
964 ep_out = ep;
965 }
966 }
967
968 if (!ep_in || !ep_out) {
969 dev_err(dev, "Couldn't find both in and out endpoints\n");
970 retval = -ENODEV;
971 goto no_endpoints;
972 }
973
974 /* allocate memory for our device state and initialize it */
975 rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
976 if (rr3 == NULL) {
977 dev_err(dev, "Memory allocation failure\n");
Dan Carpenter7eb75712011-05-26 05:55:08 -0300978 goto no_endpoints;
Jarod Wilson2154be62011-05-04 14:02:42 -0300979 }
980
981 rr3->dev = &intf->dev;
982
983 /* set up bulk-in endpoint */
984 rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL);
985 if (!rr3->read_urb) {
986 dev_err(dev, "Read urb allocation failure\n");
987 goto error;
988 }
989
990 rr3->ep_in = ep_in;
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300991 rr3->bulk_in_buf = usb_alloc_coherent(udev,
992 le16_to_cpu(ep_in->wMaxPacketSize), GFP_ATOMIC, &rr3->dma_in);
Jarod Wilson2154be62011-05-04 14:02:42 -0300993 if (!rr3->bulk_in_buf) {
994 dev_err(dev, "Read buffer allocation failure\n");
995 goto error;
996 }
997
998 pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress);
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300999 usb_fill_bulk_urb(rr3->read_urb, udev, pipe, rr3->bulk_in_buf,
1000 le16_to_cpu(ep_in->wMaxPacketSize), redrat3_handle_async, rr3);
Jarod Wilson2154be62011-05-04 14:02:42 -03001001
1002 rr3->ep_out = ep_out;
Jarod Wilson2154be62011-05-04 14:02:42 -03001003 rr3->udev = udev;
1004
1005 redrat3_reset(rr3);
1006 redrat3_get_firmware_rev(rr3);
1007
1008 /* might be all we need to do? */
1009 retval = redrat3_enable_detector(rr3);
1010 if (retval < 0)
1011 goto error;
1012
Jarod Wilsonc53f9f02011-07-13 18:26:07 -03001013 /* store current hardware timeout, in us, will use for kfifo resets */
1014 rr3->hw_timeout = redrat3_get_timeout(rr3);
1015
Jarod Wilson2154be62011-05-04 14:02:42 -03001016 /* default.. will get overridden by any sends with a freq defined */
1017 rr3->carrier = 38000;
1018
1019 rr3->rc = redrat3_init_rc_dev(rr3);
Peter Senna Tschudin6ea7cf72012-09-04 08:05:05 -03001020 if (!rr3->rc) {
1021 retval = -ENOMEM;
Jarod Wilson2154be62011-05-04 14:02:42 -03001022 goto error;
Peter Senna Tschudin6ea7cf72012-09-04 08:05:05 -03001023 }
Jarod Wilson2154be62011-05-04 14:02:42 -03001024 setup_timer(&rr3->rx_timeout, redrat3_rx_timeout, (unsigned long)rr3);
1025
1026 /* we can register the device now, as it is ready */
1027 usb_set_intfdata(intf, rr3);
1028
1029 rr3_ftr(dev, "Exiting %s\n", __func__);
1030 return 0;
1031
1032error:
1033 redrat3_delete(rr3, rr3->udev);
1034
1035no_endpoints:
1036 dev_err(dev, "%s: retval = %x", __func__, retval);
1037
1038 return retval;
1039}
1040
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001041static void redrat3_dev_disconnect(struct usb_interface *intf)
Jarod Wilson2154be62011-05-04 14:02:42 -03001042{
1043 struct usb_device *udev = interface_to_usbdev(intf);
1044 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1045
1046 rr3_ftr(&intf->dev, "Entering %s\n", __func__);
1047
1048 if (!rr3)
1049 return;
1050
1051 redrat3_disable_detector(rr3);
1052
1053 usb_set_intfdata(intf, NULL);
1054 rc_unregister_device(rr3->rc);
Jarod Wilsonc53f9f02011-07-13 18:26:07 -03001055 del_timer_sync(&rr3->rx_timeout);
Jarod Wilson2154be62011-05-04 14:02:42 -03001056 redrat3_delete(rr3, udev);
1057
1058 rr3_ftr(&intf->dev, "RedRat3 IR Transceiver now disconnected\n");
1059}
1060
1061static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
1062{
1063 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1064 rr3_ftr(rr3->dev, "suspend\n");
1065 usb_kill_urb(rr3->read_urb);
1066 return 0;
1067}
1068
1069static int redrat3_dev_resume(struct usb_interface *intf)
1070{
1071 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1072 rr3_ftr(rr3->dev, "resume\n");
1073 if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC))
1074 return -EIO;
1075 return 0;
1076}
1077
1078static struct usb_driver redrat3_dev_driver = {
1079 .name = DRIVER_NAME,
1080 .probe = redrat3_dev_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001081 .disconnect = redrat3_dev_disconnect,
Jarod Wilson2154be62011-05-04 14:02:42 -03001082 .suspend = redrat3_dev_suspend,
1083 .resume = redrat3_dev_resume,
1084 .reset_resume = redrat3_dev_resume,
1085 .id_table = redrat3_dev_table
1086};
1087
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08001088module_usb_driver(redrat3_dev_driver);
Jarod Wilson2154be62011-05-04 14:02:42 -03001089
1090MODULE_DESCRIPTION(DRIVER_DESC);
1091MODULE_AUTHOR(DRIVER_AUTHOR);
1092MODULE_AUTHOR(DRIVER_AUTHOR2);
1093MODULE_LICENSE("GPL");
1094MODULE_DEVICE_TABLE(usb, redrat3_dev_table);
1095
1096module_param(debug, int, S_IRUGO | S_IWUSR);
1097MODULE_PARM_DESC(debug, "Enable module debug spew. 0 = no debugging (default) "
1098 "0x1 = standard debug messages, 0x2 = function tracing debug. "
1099 "Flag bits are addative (i.e., 0x3 for both debug types).");