blob: c4def66f9aa2cac019a6abd9bc2070644fe8a42a [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>
Sean Youngbf139722013-07-30 19:00:04 -030050#include <linux/leds.h>
Jarod Wilson2154be62011-05-04 14:02:42 -030051#include <linux/module.h>
52#include <linux/slab.h>
53#include <linux/usb.h>
54#include <linux/usb/input.h>
55#include <media/rc-core.h>
56
57/* Driver Information */
Jarod Wilson2154be62011-05-04 14:02:42 -030058#define DRIVER_AUTHOR "Jarod Wilson <jarod@redhat.com>"
59#define DRIVER_AUTHOR2 "The Dweller, Stephen Cox"
60#define DRIVER_DESC "RedRat3 USB IR Transceiver Driver"
61#define DRIVER_NAME "redrat3"
62
Jarod Wilson2154be62011-05-04 14:02:42 -030063/* bulk data transfer types */
64#define RR3_ERROR 0x01
65#define RR3_MOD_SIGNAL_IN 0x20
66#define RR3_MOD_SIGNAL_OUT 0x21
67
68/* Get the RR firmware version */
69#define RR3_FW_VERSION 0xb1
70#define RR3_FW_VERSION_LEN 64
71/* Send encoded signal bulk-sent earlier*/
72#define RR3_TX_SEND_SIGNAL 0xb3
73#define RR3_SET_IR_PARAM 0xb7
74#define RR3_GET_IR_PARAM 0xb8
75/* Blink the red LED on the device */
76#define RR3_BLINK_LED 0xb9
77/* Read serial number of device */
78#define RR3_READ_SER_NO 0xba
79#define RR3_SER_NO_LEN 4
80/* Start capture with the RC receiver */
81#define RR3_RC_DET_ENABLE 0xbb
82/* Stop capture with the RC receiver */
83#define RR3_RC_DET_DISABLE 0xbc
84/* Return the status of RC detector capture */
85#define RR3_RC_DET_STATUS 0xbd
86/* Reset redrat */
87#define RR3_RESET 0xa0
88
89/* Max number of lengths in the signal. */
90#define RR3_IR_IO_MAX_LENGTHS 0x01
91/* Periods to measure mod. freq. */
92#define RR3_IR_IO_PERIODS_MF 0x02
93/* Size of memory for main signal data */
94#define RR3_IR_IO_SIG_MEM_SIZE 0x03
95/* Delta value when measuring lengths */
96#define RR3_IR_IO_LENGTH_FUZZ 0x04
97/* Timeout for end of signal detection */
98#define RR3_IR_IO_SIG_TIMEOUT 0x05
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -030099/* Minimum value for pause recognition. */
Jarod Wilson2154be62011-05-04 14:02:42 -0300100#define RR3_IR_IO_MIN_PAUSE 0x06
101
102/* Clock freq. of EZ-USB chip */
103#define RR3_CLK 24000000
104/* Clock periods per timer count */
105#define RR3_CLK_PER_COUNT 12
106/* (RR3_CLK / RR3_CLK_PER_COUNT) */
107#define RR3_CLK_CONV_FACTOR 2000000
108/* USB bulk-in IR data endpoint address */
109#define RR3_BULK_IN_EP_ADDR 0x82
110
Jarod Wilson2154be62011-05-04 14:02:42 -0300111/* Size of the fixed-length portion of the signal */
Jarod Wilson2154be62011-05-04 14:02:42 -0300112#define RR3_DRIVER_MAXLENS 128
113#define RR3_MAX_SIG_SIZE 512
Jarod Wilson2154be62011-05-04 14:02:42 -0300114#define RR3_TIME_UNIT 50
115#define RR3_END_OF_SIGNAL 0x7f
Jarod Wilson2154be62011-05-04 14:02:42 -0300116#define RR3_TX_TRAILER_LEN 2
117#define RR3_RX_MIN_TIMEOUT 5
118#define RR3_RX_MAX_TIMEOUT 2000
119
120/* The 8051's CPUCS Register address */
121#define RR3_CPUCS_REG_ADDR 0x7f92
122
123#define USB_RR3USB_VENDOR_ID 0x112a
124#define USB_RR3USB_PRODUCT_ID 0x0001
125#define USB_RR3IIUSB_PRODUCT_ID 0x0005
126
Sean Young4c055a52013-02-16 17:25:44 -0300127struct redrat3_header {
128 __be16 length;
129 __be16 transfer_type;
130} __packed;
131
132/* sending and receiving irdata */
133struct redrat3_irdata {
134 struct redrat3_header header;
135 __be32 pause;
136 __be16 mod_freq_count;
137 __be16 num_periods;
138 __u8 max_lengths;
139 __u8 no_lengths;
140 __be16 max_sig_size;
141 __be16 sig_size;
142 __u8 no_repeats;
143 __be16 lens[RR3_DRIVER_MAXLENS]; /* not aligned */
144 __u8 sigdata[RR3_MAX_SIG_SIZE];
145} __packed;
146
147/* firmware errors */
148struct redrat3_error {
149 struct redrat3_header header;
150 __be16 fw_error;
151} __packed;
152
Jarod Wilson2154be62011-05-04 14:02:42 -0300153/* table of devices that work with this driver */
154static struct usb_device_id redrat3_dev_table[] = {
155 /* Original version of the RedRat3 */
156 {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)},
157 /* Second Version/release of the RedRat3 - RetRat3-II */
158 {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)},
159 {} /* Terminating entry */
160};
161
162/* Structure to hold all of our device specific stuff */
163struct redrat3_dev {
164 /* core device bits */
165 struct rc_dev *rc;
166 struct device *dev;
167
Sean Youngbf139722013-07-30 19:00:04 -0300168 /* led control */
169 struct led_classdev led;
170 atomic_t flash;
171 struct usb_ctrlrequest flash_control;
172 struct urb *flash_urb;
173 u8 flash_in_buf;
174
Jarod Wilson2154be62011-05-04 14:02:42 -0300175 /* save off the usb device pointer */
176 struct usb_device *udev;
177
178 /* the receive endpoint */
179 struct usb_endpoint_descriptor *ep_in;
180 /* the buffer to receive data */
Sean Young4c055a52013-02-16 17:25:44 -0300181 void *bulk_in_buf;
Jarod Wilson2154be62011-05-04 14:02:42 -0300182 /* urb used to read ir data */
183 struct urb *read_urb;
184
185 /* the send endpoint */
186 struct usb_endpoint_descriptor *ep_out;
Jarod Wilson2154be62011-05-04 14:02:42 -0300187
188 /* usb dma */
189 dma_addr_t dma_in;
Jarod Wilson2154be62011-05-04 14:02:42 -0300190
Jarod Wilson2154be62011-05-04 14:02:42 -0300191 /* rx signal timeout timer */
192 struct timer_list rx_timeout;
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300193 u32 hw_timeout;
Jarod Wilson2154be62011-05-04 14:02:42 -0300194
Jarod Wilson2154be62011-05-04 14:02:42 -0300195 /* Is the device currently transmitting?*/
196 bool transmitting;
197
198 /* store for current packet */
Sean Young4c055a52013-02-16 17:25:44 -0300199 struct redrat3_irdata irdata;
Jarod Wilson2154be62011-05-04 14:02:42 -0300200 u16 bytes_read;
Jarod Wilson2154be62011-05-04 14:02:42 -0300201
202 u32 carrier;
203
Sean Young4c055a52013-02-16 17:25:44 -0300204 char name[64];
Jarod Wilson2154be62011-05-04 14:02:42 -0300205 char phys[64];
206};
207
Jarod Wilson2154be62011-05-04 14:02:42 -0300208/*
209 * redrat3_issue_async
210 *
211 * Issues an async read to the ir data in port..
212 * sets the callback to be redrat3_handle_async
213 */
214static void redrat3_issue_async(struct redrat3_dev *rr3)
215{
216 int res;
217
Jarod Wilson2154be62011-05-04 14:02:42 -0300218 res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC);
219 if (res)
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700220 dev_dbg(rr3->dev,
221 "%s: receive request FAILED! (res %d, len %d)\n",
222 __func__, res, rr3->read_urb->transfer_buffer_length);
Jarod Wilson2154be62011-05-04 14:02:42 -0300223}
224
225static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
226{
227 if (!rr3->transmitting && (code != 0x40))
228 dev_info(rr3->dev, "fw error code 0x%02x: ", code);
229
230 switch (code) {
231 case 0x00:
232 pr_cont("No Error\n");
233 break;
234
235 /* Codes 0x20 through 0x2f are IR Firmware Errors */
236 case 0x20:
237 pr_cont("Initial signal pulse not long enough "
238 "to measure carrier frequency\n");
239 break;
240 case 0x21:
241 pr_cont("Not enough length values allocated for signal\n");
242 break;
243 case 0x22:
244 pr_cont("Not enough memory allocated for signal data\n");
245 break;
246 case 0x23:
247 pr_cont("Too many signal repeats\n");
248 break;
249 case 0x28:
250 pr_cont("Insufficient memory available for IR signal "
251 "data memory allocation\n");
252 break;
253 case 0x29:
254 pr_cont("Insufficient memory available "
255 "for IrDa signal data memory allocation\n");
256 break;
257
258 /* Codes 0x30 through 0x3f are USB Firmware Errors */
259 case 0x30:
260 pr_cont("Insufficient memory available for bulk "
261 "transfer structure\n");
262 break;
263
264 /*
265 * Other error codes... These are primarily errors that can occur in
266 * the control messages sent to the redrat
267 */
268 case 0x40:
269 if (!rr3->transmitting)
270 pr_cont("Signal capture has been terminated\n");
271 break;
272 case 0x41:
273 pr_cont("Attempt to set/get and unknown signal I/O "
274 "algorithm parameter\n");
275 break;
276 case 0x42:
277 pr_cont("Signal capture already started\n");
278 break;
279
280 default:
281 pr_cont("Unknown Error\n");
282 break;
283 }
284}
285
Sean Young4c055a52013-02-16 17:25:44 -0300286static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata)
Jarod Wilson2154be62011-05-04 14:02:42 -0300287{
288 u32 mod_freq = 0;
Sean Young4c055a52013-02-16 17:25:44 -0300289 u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count);
Jarod Wilson2154be62011-05-04 14:02:42 -0300290
Sean Young4c055a52013-02-16 17:25:44 -0300291 if (mod_freq_count != 0)
292 mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) /
293 (mod_freq_count * RR3_CLK_PER_COUNT);
Jarod Wilson2154be62011-05-04 14:02:42 -0300294
295 return mod_freq;
296}
297
298/* this function scales down the figures for the same result... */
299static u32 redrat3_len_to_us(u32 length)
300{
301 u32 biglen = length * 1000;
302 u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000;
303 u32 result = (u32) (biglen / divisor);
304
305 /* don't allow zero lengths to go back, breaks lirc */
306 return result ? result : 1;
307}
308
309/*
310 * convert us back into redrat3 lengths
311 *
312 * length * 1000 length * 1000000
313 * ------------- = ---------------- = micro
314 * rr3clk / 1000 rr3clk
315
316 * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
317 * ----- = 4 ----- = 6 -------------- = len ---------------------
318 * 3 2 1000000 1000
319 */
320static u32 redrat3_us_to_len(u32 microsec)
321{
322 u32 result;
323 u32 divisor;
324
325 microsec &= IR_MAX_DURATION;
326 divisor = (RR3_CLK_CONV_FACTOR / 1000);
327 result = (u32)(microsec * divisor) / 1000;
328
329 /* don't allow zero lengths to go back, breaks lirc */
330 return result ? result : 1;
Jarod Wilson2154be62011-05-04 14:02:42 -0300331}
332
Jarod Wilson68b2a692011-07-13 18:26:05 -0300333/* timer callback to send reset event */
Jarod Wilson2154be62011-05-04 14:02:42 -0300334static void redrat3_rx_timeout(unsigned long data)
335{
336 struct redrat3_dev *rr3 = (struct redrat3_dev *)data;
Jarod Wilson2154be62011-05-04 14:02:42 -0300337
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700338 dev_dbg(rr3->dev, "calling ir_raw_event_reset\n");
Jarod Wilson2154be62011-05-04 14:02:42 -0300339 ir_raw_event_reset(rr3->rc);
340}
341
342static void redrat3_process_ir_data(struct redrat3_dev *rr3)
343{
344 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson2154be62011-05-04 14:02:42 -0300345 struct device *dev;
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300346 unsigned i, trailer = 0;
Sean Young4c055a52013-02-16 17:25:44 -0300347 unsigned sig_size, single_len, offset, val;
Jarod Wilson2154be62011-05-04 14:02:42 -0300348 unsigned long delay;
Sean Young4c055a52013-02-16 17:25:44 -0300349 u32 mod_freq;
Jarod Wilson2154be62011-05-04 14:02:42 -0300350
351 if (!rr3) {
352 pr_err("%s called with no context!\n", __func__);
353 return;
354 }
355
Jarod Wilson2154be62011-05-04 14:02:42 -0300356 dev = rr3->dev;
Jarod Wilson2154be62011-05-04 14:02:42 -0300357
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300358 /* Make sure we reset the IR kfifo after a bit of inactivity */
359 delay = usecs_to_jiffies(rr3->hw_timeout);
Jarod Wilson2154be62011-05-04 14:02:42 -0300360 mod_timer(&rr3->rx_timeout, jiffies + delay);
361
Sean Young4c055a52013-02-16 17:25:44 -0300362 mod_freq = redrat3_val_to_mod_freq(&rr3->irdata);
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700363 dev_dbg(dev, "Got mod_freq of %u\n", mod_freq);
Jarod Wilson2154be62011-05-04 14:02:42 -0300364
Jarod Wilson2154be62011-05-04 14:02:42 -0300365 /* process each rr3 encoded byte into an int */
Sean Young4c055a52013-02-16 17:25:44 -0300366 sig_size = be16_to_cpu(rr3->irdata.sig_size);
367 for (i = 0; i < sig_size; i++) {
368 offset = rr3->irdata.sigdata[i];
369 val = get_unaligned_be16(&rr3->irdata.lens[offset]);
370 single_len = redrat3_len_to_us(val);
Jarod Wilson2154be62011-05-04 14:02:42 -0300371
Jarod Wilson2154be62011-05-04 14:02:42 -0300372 /* we should always get pulse/space/pulse/space samples */
373 if (i % 2)
374 rawir.pulse = false;
375 else
376 rawir.pulse = true;
377
378 rawir.duration = US_TO_NS(single_len);
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300379 /* Save initial pulse length to fudge trailer */
380 if (i == 0)
381 trailer = rawir.duration;
Jarod Wilson2c594ff2011-07-13 18:26:06 -0300382 /* cap the value to IR_MAX_DURATION */
383 rawir.duration &= IR_MAX_DURATION;
384
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700385 dev_dbg(dev, "storing %s with duration %d (i: %d)\n",
Jarod Wilson2154be62011-05-04 14:02:42 -0300386 rawir.pulse ? "pulse" : "space", rawir.duration, i);
387 ir_raw_event_store_with_filter(rr3->rc, &rawir);
388 }
389
390 /* add a trailing space, if need be */
391 if (i % 2) {
392 rawir.pulse = false;
393 /* this duration is made up, and may not be ideal... */
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300394 if (trailer < US_TO_NS(1000))
395 rawir.duration = US_TO_NS(2800);
396 else
397 rawir.duration = trailer;
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700398 dev_dbg(dev, "storing trailing space with duration %d\n",
Jarod Wilson2154be62011-05-04 14:02:42 -0300399 rawir.duration);
400 ir_raw_event_store_with_filter(rr3->rc, &rawir);
401 }
402
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700403 dev_dbg(dev, "calling ir_raw_event_handle\n");
Jarod Wilson2154be62011-05-04 14:02:42 -0300404 ir_raw_event_handle(rr3->rc);
Jarod Wilson2154be62011-05-04 14:02:42 -0300405}
406
407/* Util fn to send rr3 cmds */
408static u8 redrat3_send_cmd(int cmd, struct redrat3_dev *rr3)
409{
410 struct usb_device *udev;
411 u8 *data;
412 int res;
413
414 data = kzalloc(sizeof(u8), GFP_KERNEL);
415 if (!data)
416 return -ENOMEM;
417
418 udev = rr3->udev;
419 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd,
420 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
421 0x0000, 0x0000, data, sizeof(u8), HZ * 10);
422
423 if (res < 0) {
424 dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d",
425 __func__, res, *data);
426 res = -EIO;
427 } else
Sean Young4c055a52013-02-16 17:25:44 -0300428 res = data[0];
Jarod Wilson2154be62011-05-04 14:02:42 -0300429
430 kfree(data);
431
432 return res;
433}
434
435/* Enables the long range detector and starts async receive */
436static int redrat3_enable_detector(struct redrat3_dev *rr3)
437{
438 struct device *dev = rr3->dev;
439 u8 ret;
440
Jarod Wilson2154be62011-05-04 14:02:42 -0300441 ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
442 if (ret != 0)
443 dev_dbg(dev, "%s: unexpected ret of %d\n",
444 __func__, ret);
445
446 ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
447 if (ret != 1) {
448 dev_err(dev, "%s: detector status: %d, should be 1\n",
449 __func__, ret);
450 return -EIO;
451 }
452
Jarod Wilson2154be62011-05-04 14:02:42 -0300453 redrat3_issue_async(rr3);
454
455 return 0;
456}
457
Jarod Wilson2154be62011-05-04 14:02:42 -0300458static inline void redrat3_delete(struct redrat3_dev *rr3,
459 struct usb_device *udev)
460{
Jarod Wilson2154be62011-05-04 14:02:42 -0300461 usb_kill_urb(rr3->read_urb);
Sean Youngbf139722013-07-30 19:00:04 -0300462 usb_kill_urb(rr3->flash_urb);
Jarod Wilson2154be62011-05-04 14:02:42 -0300463 usb_free_urb(rr3->read_urb);
Sean Youngbf139722013-07-30 19:00:04 -0300464 usb_free_urb(rr3->flash_urb);
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300465 usb_free_coherent(udev, le16_to_cpu(rr3->ep_in->wMaxPacketSize),
Jarod Wilson2154be62011-05-04 14:02:42 -0300466 rr3->bulk_in_buf, rr3->dma_in);
Jarod Wilson2154be62011-05-04 14:02:42 -0300467
468 kfree(rr3);
469}
470
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300471static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
Jarod Wilson2154be62011-05-04 14:02:42 -0300472{
Sean Young801b69f2013-02-16 17:25:43 -0300473 __be32 *tmp;
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300474 u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
Jarod Wilson2154be62011-05-04 14:02:42 -0300475 int len, ret, pipe;
476
477 len = sizeof(*tmp);
478 tmp = kzalloc(len, GFP_KERNEL);
479 if (!tmp) {
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300480 dev_warn(rr3->dev, "Memory allocation faillure\n");
Jarod Wilson2154be62011-05-04 14:02:42 -0300481 return timeout;
482 }
483
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300484 pipe = usb_rcvctrlpipe(rr3->udev, 0);
485 ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
Jarod Wilson2154be62011-05-04 14:02:42 -0300486 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
487 RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5);
Sean Young801b69f2013-02-16 17:25:43 -0300488 if (ret != len)
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300489 dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
Sean Young801b69f2013-02-16 17:25:43 -0300490 else {
491 timeout = redrat3_len_to_us(be32_to_cpup(tmp));
492
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700493 dev_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
Jarod Wilson2154be62011-05-04 14:02:42 -0300494 }
495
Sean Young801b69f2013-02-16 17:25:43 -0300496 kfree(tmp);
Jarod Wilson2154be62011-05-04 14:02:42 -0300497
Jarod Wilson2154be62011-05-04 14:02:42 -0300498 return timeout;
499}
500
501static void redrat3_reset(struct redrat3_dev *rr3)
502{
503 struct usb_device *udev = rr3->udev;
504 struct device *dev = rr3->dev;
505 int rc, rxpipe, txpipe;
506 u8 *val;
507 int len = sizeof(u8);
508
Jarod Wilson2154be62011-05-04 14:02:42 -0300509 rxpipe = usb_rcvctrlpipe(udev, 0);
510 txpipe = usb_sndctrlpipe(udev, 0);
511
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300512 val = kmalloc(len, GFP_KERNEL);
Jarod Wilson2154be62011-05-04 14:02:42 -0300513 if (!val) {
514 dev_err(dev, "Memory allocation failure\n");
515 return;
516 }
517
518 *val = 0x01;
519 rc = usb_control_msg(udev, rxpipe, RR3_RESET,
520 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
521 RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25);
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700522 dev_dbg(dev, "reset returned 0x%02x\n", rc);
Jarod Wilson2154be62011-05-04 14:02:42 -0300523
524 *val = 5;
525 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
526 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
527 RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25);
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700528 dev_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
Jarod Wilson2154be62011-05-04 14:02:42 -0300529
530 *val = RR3_DRIVER_MAXLENS;
531 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
532 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
533 RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25);
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700534 dev_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
Jarod Wilson2154be62011-05-04 14:02:42 -0300535
536 kfree(val);
537}
538
539static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
540{
541 int rc = 0;
542 char *buffer;
543
Jarod Wilson2154be62011-05-04 14:02:42 -0300544 buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL);
545 if (!buffer) {
546 dev_err(rr3->dev, "Memory allocation failure\n");
547 return;
548 }
549
550 rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
551 RR3_FW_VERSION,
552 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
553 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5);
554
555 if (rc >= 0)
556 dev_info(rr3->dev, "Firmware rev: %s", buffer);
557 else
558 dev_err(rr3->dev, "Problem fetching firmware ID\n");
559
560 kfree(buffer);
Jarod Wilson2154be62011-05-04 14:02:42 -0300561}
562
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300563static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
Jarod Wilson2154be62011-05-04 14:02:42 -0300564{
Sean Young4c055a52013-02-16 17:25:44 -0300565 struct redrat3_header *header = rr3->bulk_in_buf;
566 unsigned pktlen, pkttype;
Jarod Wilson2154be62011-05-04 14:02:42 -0300567
Jarod Wilson2154be62011-05-04 14:02:42 -0300568 /* grab the Length and type of transfer */
Sean Young4c055a52013-02-16 17:25:44 -0300569 pktlen = be16_to_cpu(header->length);
570 pkttype = be16_to_cpu(header->transfer_type);
Jarod Wilson2154be62011-05-04 14:02:42 -0300571
Sean Young4c055a52013-02-16 17:25:44 -0300572 if (pktlen > sizeof(rr3->irdata)) {
573 dev_warn(rr3->dev, "packet length %u too large\n", pktlen);
574 return;
575 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300576
Sean Young4c055a52013-02-16 17:25:44 -0300577 switch (pkttype) {
Jarod Wilson2154be62011-05-04 14:02:42 -0300578 case RR3_ERROR:
Sean Young4c055a52013-02-16 17:25:44 -0300579 if (len >= sizeof(struct redrat3_error)) {
580 struct redrat3_error *error = rr3->bulk_in_buf;
581 unsigned fw_error = be16_to_cpu(error->fw_error);
582 redrat3_dump_fw_error(rr3, fw_error);
583 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300584 break;
585
586 case RR3_MOD_SIGNAL_IN:
Sean Young4c055a52013-02-16 17:25:44 -0300587 memcpy(&rr3->irdata, rr3->bulk_in_buf, len);
Jarod Wilson2154be62011-05-04 14:02:42 -0300588 rr3->bytes_read = len;
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700589 dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
Sean Young4c055a52013-02-16 17:25:44 -0300590 rr3->bytes_read, pktlen);
Jarod Wilson2154be62011-05-04 14:02:42 -0300591 break;
592
593 default:
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700594 dev_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
Sean Young4c055a52013-02-16 17:25:44 -0300595 pkttype, len, pktlen);
Jarod Wilson2154be62011-05-04 14:02:42 -0300596 break;
597 }
598}
599
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300600static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len)
Jarod Wilson2154be62011-05-04 14:02:42 -0300601{
Sean Young4c055a52013-02-16 17:25:44 -0300602 void *irdata = &rr3->irdata;
603
Sean Young4c055a52013-02-16 17:25:44 -0300604 if (len + rr3->bytes_read > sizeof(rr3->irdata)) {
605 dev_warn(rr3->dev, "too much data for packet\n");
606 rr3->bytes_read = 0;
607 return;
608 }
609
610 memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len);
Jarod Wilson2154be62011-05-04 14:02:42 -0300611
612 rr3->bytes_read += len;
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700613 dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read,
Sean Young4c055a52013-02-16 17:25:44 -0300614 be16_to_cpu(rr3->irdata.header.length));
Jarod Wilson2154be62011-05-04 14:02:42 -0300615}
616
617/* gather IR data from incoming urb, process it when we have enough */
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300618static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len)
Jarod Wilson2154be62011-05-04 14:02:42 -0300619{
620 struct device *dev = rr3->dev;
Sean Young4c055a52013-02-16 17:25:44 -0300621 unsigned pkttype;
Jarod Wilson2154be62011-05-04 14:02:42 -0300622 int ret = 0;
623
Sean Young4c055a52013-02-16 17:25:44 -0300624 if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) {
Jarod Wilson2154be62011-05-04 14:02:42 -0300625 redrat3_read_packet_start(rr3, len);
626 } else if (rr3->bytes_read != 0) {
627 redrat3_read_packet_continue(rr3, len);
628 } else if (rr3->bytes_read == 0) {
629 dev_err(dev, "error: no packet data read\n");
630 ret = -ENODATA;
631 goto out;
632 }
633
Sean Young38e35a82013-07-30 19:00:00 -0300634 if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length) +
635 sizeof(struct redrat3_header))
Jarod Wilson2154be62011-05-04 14:02:42 -0300636 /* we're still accumulating data */
637 return 0;
638
639 /* if we get here, we've got IR data to decode */
Sean Young4c055a52013-02-16 17:25:44 -0300640 pkttype = be16_to_cpu(rr3->irdata.header.transfer_type);
641 if (pkttype == RR3_MOD_SIGNAL_IN)
Jarod Wilson2154be62011-05-04 14:02:42 -0300642 redrat3_process_ir_data(rr3);
643 else
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700644 dev_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n",
Sean Young4c055a52013-02-16 17:25:44 -0300645 pkttype);
Jarod Wilson2154be62011-05-04 14:02:42 -0300646
647out:
648 rr3->bytes_read = 0;
Jarod Wilson2154be62011-05-04 14:02:42 -0300649 return ret;
650}
651
652/* callback function from USB when async USB request has completed */
Sean Young801b69f2013-02-16 17:25:43 -0300653static void redrat3_handle_async(struct urb *urb)
Jarod Wilson2154be62011-05-04 14:02:42 -0300654{
655 struct redrat3_dev *rr3;
Andrew Vincerdbea1882011-11-08 12:43:45 -0300656 int ret;
Jarod Wilson2154be62011-05-04 14:02:42 -0300657
658 if (!urb)
659 return;
660
661 rr3 = urb->context;
662 if (!rr3) {
663 pr_err("%s called with invalid context!\n", __func__);
664 usb_unlink_urb(urb);
665 return;
666 }
667
Jarod Wilson2154be62011-05-04 14:02:42 -0300668 switch (urb->status) {
669 case 0:
Andrew Vincerdbea1882011-11-08 12:43:45 -0300670 ret = redrat3_get_ir_data(rr3, urb->actual_length);
671 if (!ret) {
672 /* no error, prepare to read more */
673 redrat3_issue_async(rr3);
674 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300675 break;
676
677 case -ECONNRESET:
678 case -ENOENT:
679 case -ESHUTDOWN:
680 usb_unlink_urb(urb);
681 return;
682
683 case -EPIPE:
684 default:
685 dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
686 rr3->bytes_read = 0;
Jarod Wilson2154be62011-05-04 14:02:42 -0300687 break;
688 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300689}
690
Jarod Wilson2154be62011-05-04 14:02:42 -0300691static u16 mod_freq_to_val(unsigned int mod_freq)
692{
693 int mult = 6000000;
694
695 /* Clk used in mod. freq. generation is CLK24/4. */
Sean Young4c055a52013-02-16 17:25:44 -0300696 return 65536 - (mult / mod_freq);
Jarod Wilson2154be62011-05-04 14:02:42 -0300697}
698
Andrew Vincerdbea1882011-11-08 12:43:45 -0300699static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
Jarod Wilson2154be62011-05-04 14:02:42 -0300700{
Andrew Vincerdbea1882011-11-08 12:43:45 -0300701 struct redrat3_dev *rr3 = rcdev->priv;
702 struct device *dev = rr3->dev;
Jarod Wilson2154be62011-05-04 14:02:42 -0300703
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700704 dev_dbg(dev, "Setting modulation frequency to %u", carrier);
Dan Carpenter48cafec2012-09-11 07:11:24 -0300705 if (carrier == 0)
706 return -EINVAL;
707
Jarod Wilson2154be62011-05-04 14:02:42 -0300708 rr3->carrier = carrier;
709
710 return carrier;
711}
712
Andrew Vincerdbea1882011-11-08 12:43:45 -0300713static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
714 unsigned count)
Jarod Wilson2154be62011-05-04 14:02:42 -0300715{
716 struct redrat3_dev *rr3 = rcdev->priv;
717 struct device *dev = rr3->dev;
Sean Young4c055a52013-02-16 17:25:44 -0300718 struct redrat3_irdata *irdata = NULL;
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300719 int ret, ret_len;
Jarod Wilson2154be62011-05-04 14:02:42 -0300720 int lencheck, cur_sample_len, pipe;
Jarod Wilson2154be62011-05-04 14:02:42 -0300721 int *sample_lens = NULL;
Jarod Wilson2154be62011-05-04 14:02:42 -0300722 u8 curlencheck = 0;
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300723 unsigned i, sendbuf_len;
Jarod Wilson2154be62011-05-04 14:02:42 -0300724
Jarod Wilson2154be62011-05-04 14:02:42 -0300725 if (rr3->transmitting) {
726 dev_warn(dev, "%s: transmitter already in use\n", __func__);
727 return -EAGAIN;
728 }
729
Sean Young671ea672013-07-08 17:33:09 -0300730 if (count > RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN)
731 return -EINVAL;
Jarod Wilson2154be62011-05-04 14:02:42 -0300732
Andrew Vincerdbea1882011-11-08 12:43:45 -0300733 /* rr3 will disable rc detector on transmit */
Jarod Wilson2154be62011-05-04 14:02:42 -0300734 rr3->transmitting = true;
735
Jarod Wilson2154be62011-05-04 14:02:42 -0300736 sample_lens = kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS, GFP_KERNEL);
737 if (!sample_lens) {
738 ret = -ENOMEM;
739 goto out;
740 }
741
Sean Young4c055a52013-02-16 17:25:44 -0300742 irdata = kzalloc(sizeof(*irdata), GFP_KERNEL);
743 if (!irdata) {
Sean Young801b69f2013-02-16 17:25:43 -0300744 ret = -ENOMEM;
745 goto out;
746 }
747
Jarod Wilson2154be62011-05-04 14:02:42 -0300748 for (i = 0; i < count; i++) {
Sean Young06eae252013-01-29 08:19:31 -0300749 cur_sample_len = redrat3_us_to_len(txbuf[i]);
Sean Young801b69f2013-02-16 17:25:43 -0300750 if (cur_sample_len > 0xffff) {
751 dev_warn(dev, "transmit period of %uus truncated to %uus\n",
752 txbuf[i], redrat3_len_to_us(0xffff));
753 cur_sample_len = 0xffff;
754 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300755 for (lencheck = 0; lencheck < curlencheck; lencheck++) {
Jarod Wilson2154be62011-05-04 14:02:42 -0300756 if (sample_lens[lencheck] == cur_sample_len)
757 break;
758 }
759 if (lencheck == curlencheck) {
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700760 dev_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
Jarod Wilson2154be62011-05-04 14:02:42 -0300761 i, txbuf[i], curlencheck, cur_sample_len);
Sean Young06eae252013-01-29 08:19:31 -0300762 if (curlencheck < RR3_DRIVER_MAXLENS) {
Jarod Wilson2154be62011-05-04 14:02:42 -0300763 /* now convert the value to a proper
764 * rr3 value.. */
765 sample_lens[curlencheck] = cur_sample_len;
Sean Young4c055a52013-02-16 17:25:44 -0300766 put_unaligned_be16(cur_sample_len,
767 &irdata->lens[curlencheck]);
Jarod Wilson2154be62011-05-04 14:02:42 -0300768 curlencheck++;
769 } else {
Sean Young671ea672013-07-08 17:33:09 -0300770 ret = -EINVAL;
771 goto out;
Jarod Wilson2154be62011-05-04 14:02:42 -0300772 }
773 }
Sean Young4c055a52013-02-16 17:25:44 -0300774 irdata->sigdata[i] = lencheck;
Jarod Wilson2154be62011-05-04 14:02:42 -0300775 }
776
Sean Young4c055a52013-02-16 17:25:44 -0300777 irdata->sigdata[count] = RR3_END_OF_SIGNAL;
778 irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL;
Jarod Wilson2154be62011-05-04 14:02:42 -0300779
Sean Young4c055a52013-02-16 17:25:44 -0300780 sendbuf_len = offsetof(struct redrat3_irdata,
781 sigdata[count + RR3_TX_TRAILER_LEN]);
Jarod Wilson2154be62011-05-04 14:02:42 -0300782 /* fill in our packet header */
Sean Young4c055a52013-02-16 17:25:44 -0300783 irdata->header.length = cpu_to_be16(sendbuf_len -
784 sizeof(struct redrat3_header));
785 irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT);
786 irdata->pause = cpu_to_be32(redrat3_len_to_us(100));
787 irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier));
788 irdata->no_lengths = curlencheck;
789 irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN);
Jarod Wilson2154be62011-05-04 14:02:42 -0300790
791 pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
Sean Young4c055a52013-02-16 17:25:44 -0300792 ret = usb_bulk_msg(rr3->udev, pipe, irdata,
Jarod Wilson2154be62011-05-04 14:02:42 -0300793 sendbuf_len, &ret_len, 10 * HZ);
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700794 dev_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret);
Jarod Wilson2154be62011-05-04 14:02:42 -0300795
796 /* now tell the hardware to transmit what we sent it */
797 pipe = usb_rcvctrlpipe(rr3->udev, 0);
798 ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
799 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Sean Young4c055a52013-02-16 17:25:44 -0300800 0, 0, irdata, 2, HZ * 10);
Jarod Wilson2154be62011-05-04 14:02:42 -0300801
802 if (ret < 0)
803 dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
804 else
Andrew Vincerdbea1882011-11-08 12:43:45 -0300805 ret = count;
Jarod Wilson2154be62011-05-04 14:02:42 -0300806
807out:
808 kfree(sample_lens);
Sean Young4c055a52013-02-16 17:25:44 -0300809 kfree(irdata);
Jarod Wilson2154be62011-05-04 14:02:42 -0300810
811 rr3->transmitting = false;
Andrew Vincerdbea1882011-11-08 12:43:45 -0300812 /* rr3 re-enables rc detector because it was enabled before */
Jarod Wilson2154be62011-05-04 14:02:42 -0300813
814 return ret;
815}
816
Sean Youngbf139722013-07-30 19:00:04 -0300817static void redrat3_brightness_set(struct led_classdev *led_dev, enum
818 led_brightness brightness)
819{
820 struct redrat3_dev *rr3 = container_of(led_dev, struct redrat3_dev,
821 led);
822
823 if (brightness != LED_OFF && atomic_cmpxchg(&rr3->flash, 0, 1) == 0) {
824 int ret = usb_submit_urb(rr3->flash_urb, GFP_ATOMIC);
825 if (ret != 0) {
826 dev_dbg(rr3->dev, "%s: unexpected ret of %d\n",
827 __func__, ret);
828 atomic_set(&rr3->flash, 0);
829 }
830 }
831}
832
833static void redrat3_led_complete(struct urb *urb)
834{
835 struct redrat3_dev *rr3 = urb->context;
836
837 switch (urb->status) {
838 case 0:
839 break;
840 case -ECONNRESET:
841 case -ENOENT:
842 case -ESHUTDOWN:
843 usb_unlink_urb(urb);
844 return;
845 case -EPIPE:
846 default:
847 dev_dbg(rr3->dev, "Error: urb status = %d\n", urb->status);
848 break;
849 }
850
851 rr3->led.brightness = LED_OFF;
852 atomic_dec(&rr3->flash);
853}
854
Jarod Wilson2154be62011-05-04 14:02:42 -0300855static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
856{
857 struct device *dev = rr3->dev;
858 struct rc_dev *rc;
859 int ret = -ENODEV;
860 u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
861
862 rc = rc_allocate_device();
863 if (!rc) {
864 dev_err(dev, "remote input dev allocation failed\n");
865 goto out;
866 }
867
868 snprintf(rr3->name, sizeof(rr3->name), "RedRat3%s "
869 "Infrared Remote Transceiver (%04x:%04x)",
870 prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "",
871 le16_to_cpu(rr3->udev->descriptor.idVendor), prod);
872
873 usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
874
875 rc->input_name = rr3->name;
876 rc->input_phys = rr3->phys;
877 usb_to_input_id(rr3->udev, &rc->input_id);
878 rc->dev.parent = dev;
879 rc->priv = rr3;
880 rc->driver_type = RC_DRIVER_IR_RAW;
David Härdemanc5540fb2014-04-03 20:32:21 -0300881 rc->allowed_protocols = RC_BIT_ALL;
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300882 rc->timeout = US_TO_NS(2750);
Jarod Wilson2154be62011-05-04 14:02:42 -0300883 rc->tx_ir = redrat3_transmit_ir;
884 rc->s_tx_carrier = redrat3_set_tx_carrier;
885 rc->driver_name = DRIVER_NAME;
Sean Young06eae252013-01-29 08:19:31 -0300886 rc->rx_resolution = US_TO_NS(2);
Jarod Wilson2154be62011-05-04 14:02:42 -0300887 rc->map_name = RC_MAP_HAUPPAUGE;
888
889 ret = rc_register_device(rc);
890 if (ret < 0) {
891 dev_err(dev, "remote dev registration failed\n");
892 goto out;
893 }
894
895 return rc;
896
897out:
898 rc_free_device(rc);
899 return NULL;
900}
901
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800902static int redrat3_dev_probe(struct usb_interface *intf,
903 const struct usb_device_id *id)
Jarod Wilson2154be62011-05-04 14:02:42 -0300904{
905 struct usb_device *udev = interface_to_usbdev(intf);
906 struct device *dev = &intf->dev;
907 struct usb_host_interface *uhi;
908 struct redrat3_dev *rr3;
909 struct usb_endpoint_descriptor *ep;
910 struct usb_endpoint_descriptor *ep_in = NULL;
911 struct usb_endpoint_descriptor *ep_out = NULL;
912 u8 addr, attrs;
913 int pipe, i;
914 int retval = -ENOMEM;
915
Jarod Wilson2154be62011-05-04 14:02:42 -0300916 uhi = intf->cur_altsetting;
917
918 /* find our bulk-in and bulk-out endpoints */
919 for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
920 ep = &uhi->endpoint[i].desc;
921 addr = ep->bEndpointAddress;
922 attrs = ep->bmAttributes;
923
924 if ((ep_in == NULL) &&
925 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
926 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
927 USB_ENDPOINT_XFER_BULK)) {
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700928 dev_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
Jarod Wilson2154be62011-05-04 14:02:42 -0300929 ep->bEndpointAddress);
930 /* data comes in on 0x82, 0x81 is for other data... */
931 if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR)
932 ep_in = ep;
933 }
934
935 if ((ep_out == NULL) &&
936 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
937 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
938 USB_ENDPOINT_XFER_BULK)) {
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700939 dev_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
Jarod Wilson2154be62011-05-04 14:02:42 -0300940 ep->bEndpointAddress);
941 ep_out = ep;
942 }
943 }
944
945 if (!ep_in || !ep_out) {
946 dev_err(dev, "Couldn't find both in and out endpoints\n");
947 retval = -ENODEV;
948 goto no_endpoints;
949 }
950
951 /* allocate memory for our device state and initialize it */
952 rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
953 if (rr3 == NULL) {
954 dev_err(dev, "Memory allocation failure\n");
Dan Carpenter7eb75712011-05-26 05:55:08 -0300955 goto no_endpoints;
Jarod Wilson2154be62011-05-04 14:02:42 -0300956 }
957
958 rr3->dev = &intf->dev;
959
960 /* set up bulk-in endpoint */
961 rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL);
962 if (!rr3->read_urb) {
963 dev_err(dev, "Read urb allocation failure\n");
964 goto error;
965 }
966
967 rr3->ep_in = ep_in;
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300968 rr3->bulk_in_buf = usb_alloc_coherent(udev,
Sean Youngd0a0a652014-12-01 05:48:16 -0300969 le16_to_cpu(ep_in->wMaxPacketSize), GFP_KERNEL, &rr3->dma_in);
Jarod Wilson2154be62011-05-04 14:02:42 -0300970 if (!rr3->bulk_in_buf) {
971 dev_err(dev, "Read buffer allocation failure\n");
972 goto error;
973 }
974
975 pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress);
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300976 usb_fill_bulk_urb(rr3->read_urb, udev, pipe, rr3->bulk_in_buf,
977 le16_to_cpu(ep_in->wMaxPacketSize), redrat3_handle_async, rr3);
Sean Youngd0a0a652014-12-01 05:48:16 -0300978 rr3->read_urb->transfer_dma = rr3->dma_in;
979 rr3->read_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Jarod Wilson2154be62011-05-04 14:02:42 -0300980
981 rr3->ep_out = ep_out;
Jarod Wilson2154be62011-05-04 14:02:42 -0300982 rr3->udev = udev;
983
984 redrat3_reset(rr3);
985 redrat3_get_firmware_rev(rr3);
986
987 /* might be all we need to do? */
988 retval = redrat3_enable_detector(rr3);
989 if (retval < 0)
990 goto error;
991
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300992 /* store current hardware timeout, in us, will use for kfifo resets */
993 rr3->hw_timeout = redrat3_get_timeout(rr3);
994
Jarod Wilson2154be62011-05-04 14:02:42 -0300995 /* default.. will get overridden by any sends with a freq defined */
996 rr3->carrier = 38000;
997
Sean Youngbf139722013-07-30 19:00:04 -0300998 /* led control */
999 rr3->led.name = "redrat3:red:feedback";
1000 rr3->led.default_trigger = "rc-feedback";
1001 rr3->led.brightness_set = redrat3_brightness_set;
1002 retval = led_classdev_register(&intf->dev, &rr3->led);
1003 if (retval)
1004 goto error;
1005
1006 atomic_set(&rr3->flash, 0);
1007 rr3->flash_urb = usb_alloc_urb(0, GFP_KERNEL);
1008 if (!rr3->flash_urb) {
1009 retval = -ENOMEM;
1010 goto led_free_error;
1011 }
1012
1013 /* setup packet is 'c0 b9 0000 0000 0001' */
1014 rr3->flash_control.bRequestType = 0xc0;
1015 rr3->flash_control.bRequest = RR3_BLINK_LED;
1016 rr3->flash_control.wLength = cpu_to_le16(1);
1017
1018 usb_fill_control_urb(rr3->flash_urb, udev, usb_rcvctrlpipe(udev, 0),
1019 (unsigned char *)&rr3->flash_control,
1020 &rr3->flash_in_buf, sizeof(rr3->flash_in_buf),
1021 redrat3_led_complete, rr3);
1022
Jarod Wilson2154be62011-05-04 14:02:42 -03001023 rr3->rc = redrat3_init_rc_dev(rr3);
Peter Senna Tschudin6ea7cf72012-09-04 08:05:05 -03001024 if (!rr3->rc) {
1025 retval = -ENOMEM;
Sean Youngbf139722013-07-30 19:00:04 -03001026 goto led_free_error;
Peter Senna Tschudin6ea7cf72012-09-04 08:05:05 -03001027 }
Jarod Wilson2154be62011-05-04 14:02:42 -03001028 setup_timer(&rr3->rx_timeout, redrat3_rx_timeout, (unsigned long)rr3);
1029
1030 /* we can register the device now, as it is ready */
1031 usb_set_intfdata(intf, rr3);
1032
Jarod Wilson2154be62011-05-04 14:02:42 -03001033 return 0;
1034
Sean Youngbf139722013-07-30 19:00:04 -03001035led_free_error:
1036 led_classdev_unregister(&rr3->led);
Jarod Wilson2154be62011-05-04 14:02:42 -03001037error:
1038 redrat3_delete(rr3, rr3->udev);
1039
1040no_endpoints:
1041 dev_err(dev, "%s: retval = %x", __func__, retval);
1042
1043 return retval;
1044}
1045
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001046static void redrat3_dev_disconnect(struct usb_interface *intf)
Jarod Wilson2154be62011-05-04 14:02:42 -03001047{
1048 struct usb_device *udev = interface_to_usbdev(intf);
1049 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1050
Jarod Wilson2154be62011-05-04 14:02:42 -03001051 if (!rr3)
1052 return;
1053
Jarod Wilson2154be62011-05-04 14:02:42 -03001054 usb_set_intfdata(intf, NULL);
1055 rc_unregister_device(rr3->rc);
Sean Youngbf139722013-07-30 19:00:04 -03001056 led_classdev_unregister(&rr3->led);
Jarod Wilsonc53f9f02011-07-13 18:26:07 -03001057 del_timer_sync(&rr3->rx_timeout);
Jarod Wilson2154be62011-05-04 14:02:42 -03001058 redrat3_delete(rr3, udev);
Jarod Wilson2154be62011-05-04 14:02:42 -03001059}
1060
1061static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
1062{
1063 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
Greg Kroah-Hartmana36d5b62014-05-28 11:34:00 -07001064
Sean Youngbf139722013-07-30 19:00:04 -03001065 led_classdev_suspend(&rr3->led);
Jarod Wilson2154be62011-05-04 14:02:42 -03001066 usb_kill_urb(rr3->read_urb);
Sean Youngbf139722013-07-30 19:00:04 -03001067 usb_kill_urb(rr3->flash_urb);
Jarod Wilson2154be62011-05-04 14:02:42 -03001068 return 0;
1069}
1070
1071static int redrat3_dev_resume(struct usb_interface *intf)
1072{
1073 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
Greg Kroah-Hartmana36d5b62014-05-28 11:34:00 -07001074
Jarod Wilson2154be62011-05-04 14:02:42 -03001075 if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC))
1076 return -EIO;
Sean Youngbf139722013-07-30 19:00:04 -03001077 led_classdev_resume(&rr3->led);
Jarod Wilson2154be62011-05-04 14:02:42 -03001078 return 0;
1079}
1080
1081static struct usb_driver redrat3_dev_driver = {
1082 .name = DRIVER_NAME,
1083 .probe = redrat3_dev_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001084 .disconnect = redrat3_dev_disconnect,
Jarod Wilson2154be62011-05-04 14:02:42 -03001085 .suspend = redrat3_dev_suspend,
1086 .resume = redrat3_dev_resume,
1087 .reset_resume = redrat3_dev_resume,
1088 .id_table = redrat3_dev_table
1089};
1090
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08001091module_usb_driver(redrat3_dev_driver);
Jarod Wilson2154be62011-05-04 14:02:42 -03001092
1093MODULE_DESCRIPTION(DRIVER_DESC);
1094MODULE_AUTHOR(DRIVER_AUTHOR);
1095MODULE_AUTHOR(DRIVER_AUTHOR2);
1096MODULE_LICENSE("GPL");
1097MODULE_DEVICE_TABLE(usb, redrat3_dev_table);