blob: 09e34a73d6639dfc7273d60c51acc83fb9dfee32 [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 Youngd6ae1622016-07-20 14:03:50 -0300127
128/*
129 * The redrat3 encodes an IR signal as set of different lengths and a set
130 * of indices into those lengths. This sets how much two lengths must
131 * differ before they are considered distinct, the value is specified
132 * in microseconds.
133 * Default 5, value 0 to 127.
134 */
135static int length_fuzz = 5;
136module_param(length_fuzz, uint, 0644);
137MODULE_PARM_DESC(length_fuzz, "Length Fuzz (0-127)");
138
139/*
140 * When receiving a continuous ir stream (for example when a user is
141 * holding a button down on a remote), this specifies the minimum size
142 * of a space when the redrat3 sends a irdata packet to the host. Specified
143 * in miliseconds. Default value 18ms.
144 * The value can be between 2 and 30 inclusive.
145 */
146static int minimum_pause = 18;
147module_param(minimum_pause, uint, 0644);
148MODULE_PARM_DESC(minimum_pause, "Minimum Pause in ms (2-30)");
149
150/*
151 * The carrier frequency is measured during the first pulse of the IR
152 * signal. The larger the number of periods used To measure, the more
153 * accurate the result is likely to be, however some signals have short
154 * initial pulses, so in some case it may be necessary to reduce this value.
155 * Default 8, value 1 to 255.
156 */
157static int periods_measure_carrier = 8;
158module_param(periods_measure_carrier, uint, 0644);
159MODULE_PARM_DESC(periods_measure_carrier, "Number of Periods to Measure Carrier (1-255)");
160
161
Sean Young4c055a52013-02-16 17:25:44 -0300162struct redrat3_header {
163 __be16 length;
164 __be16 transfer_type;
165} __packed;
166
167/* sending and receiving irdata */
168struct redrat3_irdata {
169 struct redrat3_header header;
170 __be32 pause;
171 __be16 mod_freq_count;
172 __be16 num_periods;
173 __u8 max_lengths;
174 __u8 no_lengths;
175 __be16 max_sig_size;
176 __be16 sig_size;
177 __u8 no_repeats;
178 __be16 lens[RR3_DRIVER_MAXLENS]; /* not aligned */
179 __u8 sigdata[RR3_MAX_SIG_SIZE];
180} __packed;
181
182/* firmware errors */
183struct redrat3_error {
184 struct redrat3_header header;
185 __be16 fw_error;
186} __packed;
187
Jarod Wilson2154be62011-05-04 14:02:42 -0300188/* table of devices that work with this driver */
189static struct usb_device_id redrat3_dev_table[] = {
190 /* Original version of the RedRat3 */
191 {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3USB_PRODUCT_ID)},
192 /* Second Version/release of the RedRat3 - RetRat3-II */
193 {USB_DEVICE(USB_RR3USB_VENDOR_ID, USB_RR3IIUSB_PRODUCT_ID)},
194 {} /* Terminating entry */
195};
196
197/* Structure to hold all of our device specific stuff */
198struct redrat3_dev {
199 /* core device bits */
200 struct rc_dev *rc;
201 struct device *dev;
202
Sean Youngbf139722013-07-30 19:00:04 -0300203 /* led control */
204 struct led_classdev led;
205 atomic_t flash;
206 struct usb_ctrlrequest flash_control;
207 struct urb *flash_urb;
208 u8 flash_in_buf;
209
Jarod Wilson2154be62011-05-04 14:02:42 -0300210 /* save off the usb device pointer */
211 struct usb_device *udev;
212
213 /* the receive endpoint */
214 struct usb_endpoint_descriptor *ep_in;
215 /* the buffer to receive data */
Sean Young4c055a52013-02-16 17:25:44 -0300216 void *bulk_in_buf;
Jarod Wilson2154be62011-05-04 14:02:42 -0300217 /* urb used to read ir data */
218 struct urb *read_urb;
219
220 /* the send endpoint */
221 struct usb_endpoint_descriptor *ep_out;
Jarod Wilson2154be62011-05-04 14:02:42 -0300222
223 /* usb dma */
224 dma_addr_t dma_in;
Jarod Wilson2154be62011-05-04 14:02:42 -0300225
Jarod Wilson2154be62011-05-04 14:02:42 -0300226 /* Is the device currently transmitting?*/
227 bool transmitting;
228
229 /* store for current packet */
Sean Young4c055a52013-02-16 17:25:44 -0300230 struct redrat3_irdata irdata;
Jarod Wilson2154be62011-05-04 14:02:42 -0300231 u16 bytes_read;
Jarod Wilson2154be62011-05-04 14:02:42 -0300232
233 u32 carrier;
234
Sean Young4c055a52013-02-16 17:25:44 -0300235 char name[64];
Jarod Wilson2154be62011-05-04 14:02:42 -0300236 char phys[64];
237};
238
Jarod Wilson2154be62011-05-04 14:02:42 -0300239/*
240 * redrat3_issue_async
241 *
242 * Issues an async read to the ir data in port..
243 * sets the callback to be redrat3_handle_async
244 */
245static void redrat3_issue_async(struct redrat3_dev *rr3)
246{
247 int res;
248
Jarod Wilson2154be62011-05-04 14:02:42 -0300249 res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC);
250 if (res)
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700251 dev_dbg(rr3->dev,
252 "%s: receive request FAILED! (res %d, len %d)\n",
253 __func__, res, rr3->read_urb->transfer_buffer_length);
Jarod Wilson2154be62011-05-04 14:02:42 -0300254}
255
256static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
257{
258 if (!rr3->transmitting && (code != 0x40))
259 dev_info(rr3->dev, "fw error code 0x%02x: ", code);
260
261 switch (code) {
262 case 0x00:
263 pr_cont("No Error\n");
264 break;
265
266 /* Codes 0x20 through 0x2f are IR Firmware Errors */
267 case 0x20:
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200268 pr_cont("Initial signal pulse not long enough to measure carrier frequency\n");
Jarod Wilson2154be62011-05-04 14:02:42 -0300269 break;
270 case 0x21:
271 pr_cont("Not enough length values allocated for signal\n");
272 break;
273 case 0x22:
274 pr_cont("Not enough memory allocated for signal data\n");
275 break;
276 case 0x23:
277 pr_cont("Too many signal repeats\n");
278 break;
279 case 0x28:
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200280 pr_cont("Insufficient memory available for IR signal data memory allocation\n");
Jarod Wilson2154be62011-05-04 14:02:42 -0300281 break;
282 case 0x29:
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200283 pr_cont("Insufficient memory available for IrDa signal data memory allocation\n");
Jarod Wilson2154be62011-05-04 14:02:42 -0300284 break;
285
286 /* Codes 0x30 through 0x3f are USB Firmware Errors */
287 case 0x30:
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200288 pr_cont("Insufficient memory available for bulk transfer structure\n");
Jarod Wilson2154be62011-05-04 14:02:42 -0300289 break;
290
291 /*
292 * Other error codes... These are primarily errors that can occur in
293 * the control messages sent to the redrat
294 */
295 case 0x40:
296 if (!rr3->transmitting)
297 pr_cont("Signal capture has been terminated\n");
298 break;
299 case 0x41:
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200300 pr_cont("Attempt to set/get and unknown signal I/O algorithm parameter\n");
Jarod Wilson2154be62011-05-04 14:02:42 -0300301 break;
302 case 0x42:
303 pr_cont("Signal capture already started\n");
304 break;
305
306 default:
307 pr_cont("Unknown Error\n");
308 break;
309 }
310}
311
Sean Young4c055a52013-02-16 17:25:44 -0300312static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata)
Jarod Wilson2154be62011-05-04 14:02:42 -0300313{
314 u32 mod_freq = 0;
Sean Young4c055a52013-02-16 17:25:44 -0300315 u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count);
Jarod Wilson2154be62011-05-04 14:02:42 -0300316
Sean Young4c055a52013-02-16 17:25:44 -0300317 if (mod_freq_count != 0)
318 mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) /
319 (mod_freq_count * RR3_CLK_PER_COUNT);
Jarod Wilson2154be62011-05-04 14:02:42 -0300320
321 return mod_freq;
322}
323
324/* this function scales down the figures for the same result... */
325static u32 redrat3_len_to_us(u32 length)
326{
327 u32 biglen = length * 1000;
328 u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000;
329 u32 result = (u32) (biglen / divisor);
330
331 /* don't allow zero lengths to go back, breaks lirc */
332 return result ? result : 1;
333}
334
335/*
336 * convert us back into redrat3 lengths
337 *
338 * length * 1000 length * 1000000
339 * ------------- = ---------------- = micro
340 * rr3clk / 1000 rr3clk
341
342 * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
343 * ----- = 4 ----- = 6 -------------- = len ---------------------
344 * 3 2 1000000 1000
345 */
346static u32 redrat3_us_to_len(u32 microsec)
347{
348 u32 result;
349 u32 divisor;
350
Mauro Carvalho Chehab95cf60a2015-06-05 10:25:24 -0300351 microsec = (microsec > IR_MAX_DURATION) ? IR_MAX_DURATION : microsec;
Jarod Wilson2154be62011-05-04 14:02:42 -0300352 divisor = (RR3_CLK_CONV_FACTOR / 1000);
353 result = (u32)(microsec * divisor) / 1000;
354
355 /* don't allow zero lengths to go back, breaks lirc */
356 return result ? result : 1;
Jarod Wilson2154be62011-05-04 14:02:42 -0300357}
358
Jarod Wilson2154be62011-05-04 14:02:42 -0300359static void redrat3_process_ir_data(struct redrat3_dev *rr3)
360{
361 DEFINE_IR_RAW_EVENT(rawir);
Jarod Wilson2154be62011-05-04 14:02:42 -0300362 struct device *dev;
Sean Young25da6612016-07-10 13:34:37 -0300363 unsigned int i, sig_size, single_len, offset, val;
Sean Young4c055a52013-02-16 17:25:44 -0300364 u32 mod_freq;
Jarod Wilson2154be62011-05-04 14:02:42 -0300365
Jarod Wilson2154be62011-05-04 14:02:42 -0300366 dev = rr3->dev;
Jarod Wilson2154be62011-05-04 14:02:42 -0300367
Sean Young4c055a52013-02-16 17:25:44 -0300368 mod_freq = redrat3_val_to_mod_freq(&rr3->irdata);
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700369 dev_dbg(dev, "Got mod_freq of %u\n", mod_freq);
Jarod Wilson2154be62011-05-04 14:02:42 -0300370
Jarod Wilson2154be62011-05-04 14:02:42 -0300371 /* process each rr3 encoded byte into an int */
Sean Young4c055a52013-02-16 17:25:44 -0300372 sig_size = be16_to_cpu(rr3->irdata.sig_size);
373 for (i = 0; i < sig_size; i++) {
374 offset = rr3->irdata.sigdata[i];
375 val = get_unaligned_be16(&rr3->irdata.lens[offset]);
376 single_len = redrat3_len_to_us(val);
Jarod Wilson2154be62011-05-04 14:02:42 -0300377
Jarod Wilson2154be62011-05-04 14:02:42 -0300378 /* we should always get pulse/space/pulse/space samples */
379 if (i % 2)
380 rawir.pulse = false;
381 else
382 rawir.pulse = true;
383
384 rawir.duration = US_TO_NS(single_len);
Jarod Wilson2c594ff2011-07-13 18:26:06 -0300385 /* cap the value to IR_MAX_DURATION */
Mauro Carvalho Chehab95cf60a2015-06-05 10:25:24 -0300386 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
387 IR_MAX_DURATION : rawir.duration;
Jarod Wilson2c594ff2011-07-13 18:26:06 -0300388
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700389 dev_dbg(dev, "storing %s with duration %d (i: %d)\n",
Jarod Wilson2154be62011-05-04 14:02:42 -0300390 rawir.pulse ? "pulse" : "space", rawir.duration, i);
391 ir_raw_event_store_with_filter(rr3->rc, &rawir);
392 }
393
Sean Young25da6612016-07-10 13:34:37 -0300394 /* add a trailing space */
395 rawir.pulse = false;
396 rawir.timeout = true;
Sean Youngc7470ff2016-07-20 14:03:49 -0300397 rawir.duration = rr3->rc->timeout;
Sean Young25da6612016-07-10 13:34:37 -0300398 dev_dbg(dev, "storing trailing timeout with duration %d\n",
399 rawir.duration);
400 ir_raw_event_store_with_filter(rr3->rc, &rawir);
Jarod Wilson2154be62011-05-04 14:02:42 -0300401
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700402 dev_dbg(dev, "calling ir_raw_event_handle\n");
Jarod Wilson2154be62011-05-04 14:02:42 -0300403 ir_raw_event_handle(rr3->rc);
Jarod Wilson2154be62011-05-04 14:02:42 -0300404}
405
406/* Util fn to send rr3 cmds */
Mauro Carvalho Chehab6948524d52015-04-29 15:00:30 -0300407static int redrat3_send_cmd(int cmd, struct redrat3_dev *rr3)
Jarod Wilson2154be62011-05-04 14:02:42 -0300408{
409 struct usb_device *udev;
410 u8 *data;
411 int res;
412
413 data = kzalloc(sizeof(u8), GFP_KERNEL);
414 if (!data)
415 return -ENOMEM;
416
417 udev = rr3->udev;
418 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd,
419 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
420 0x0000, 0x0000, data, sizeof(u8), HZ * 10);
421
422 if (res < 0) {
423 dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d",
424 __func__, res, *data);
425 res = -EIO;
426 } else
Sean Young4c055a52013-02-16 17:25:44 -0300427 res = data[0];
Jarod Wilson2154be62011-05-04 14:02:42 -0300428
429 kfree(data);
430
431 return res;
432}
433
434/* Enables the long range detector and starts async receive */
435static int redrat3_enable_detector(struct redrat3_dev *rr3)
436{
437 struct device *dev = rr3->dev;
438 u8 ret;
439
Jarod Wilson2154be62011-05-04 14:02:42 -0300440 ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
441 if (ret != 0)
442 dev_dbg(dev, "%s: unexpected ret of %d\n",
443 __func__, ret);
444
445 ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
446 if (ret != 1) {
447 dev_err(dev, "%s: detector status: %d, should be 1\n",
448 __func__, ret);
449 return -EIO;
450 }
451
Jarod Wilson2154be62011-05-04 14:02:42 -0300452 redrat3_issue_async(rr3);
453
454 return 0;
455}
456
Jarod Wilson2154be62011-05-04 14:02:42 -0300457static inline void redrat3_delete(struct redrat3_dev *rr3,
458 struct usb_device *udev)
459{
Jarod Wilson2154be62011-05-04 14:02:42 -0300460 usb_kill_urb(rr3->read_urb);
Sean Youngbf139722013-07-30 19:00:04 -0300461 usb_kill_urb(rr3->flash_urb);
Jarod Wilson2154be62011-05-04 14:02:42 -0300462 usb_free_urb(rr3->read_urb);
Sean Youngbf139722013-07-30 19:00:04 -0300463 usb_free_urb(rr3->flash_urb);
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300464 usb_free_coherent(udev, le16_to_cpu(rr3->ep_in->wMaxPacketSize),
Jarod Wilson2154be62011-05-04 14:02:42 -0300465 rr3->bulk_in_buf, rr3->dma_in);
Jarod Wilson2154be62011-05-04 14:02:42 -0300466
467 kfree(rr3);
468}
469
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300470static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
Jarod Wilson2154be62011-05-04 14:02:42 -0300471{
Sean Young801b69f2013-02-16 17:25:43 -0300472 __be32 *tmp;
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300473 u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
Jarod Wilson2154be62011-05-04 14:02:42 -0300474 int len, ret, pipe;
475
476 len = sizeof(*tmp);
477 tmp = kzalloc(len, GFP_KERNEL);
Markus Elfring559f64d2016-10-13 08:20:19 -0300478 if (!tmp)
Jarod Wilson2154be62011-05-04 14:02:42 -0300479 return timeout;
Jarod Wilson2154be62011-05-04 14:02:42 -0300480
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300481 pipe = usb_rcvctrlpipe(rr3->udev, 0);
482 ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
Jarod Wilson2154be62011-05-04 14:02:42 -0300483 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
484 RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5);
Sean Young801b69f2013-02-16 17:25:43 -0300485 if (ret != len)
Jarod Wilsonc53f9f02011-07-13 18:26:07 -0300486 dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
Sean Young801b69f2013-02-16 17:25:43 -0300487 else {
488 timeout = redrat3_len_to_us(be32_to_cpup(tmp));
489
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700490 dev_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
Jarod Wilson2154be62011-05-04 14:02:42 -0300491 }
492
Sean Young801b69f2013-02-16 17:25:43 -0300493 kfree(tmp);
Jarod Wilson2154be62011-05-04 14:02:42 -0300494
Jarod Wilson2154be62011-05-04 14:02:42 -0300495 return timeout;
496}
497
Sean Young4f253ce2016-07-10 13:34:38 -0300498static int redrat3_set_timeout(struct rc_dev *rc_dev, unsigned int timeoutns)
499{
500 struct redrat3_dev *rr3 = rc_dev->priv;
501 struct usb_device *udev = rr3->udev;
502 struct device *dev = rr3->dev;
Hans Verkuilb1cb50b2016-08-23 03:48:37 -0300503 __be32 *timeout;
Sean Young4f253ce2016-07-10 13:34:38 -0300504 int ret;
505
506 timeout = kmalloc(sizeof(*timeout), GFP_KERNEL);
507 if (!timeout)
508 return -ENOMEM;
509
510 *timeout = cpu_to_be32(redrat3_us_to_len(timeoutns / 1000));
511 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), RR3_SET_IR_PARAM,
512 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
513 RR3_IR_IO_SIG_TIMEOUT, 0, timeout, sizeof(*timeout),
514 HZ * 25);
515 dev_dbg(dev, "set ir parm timeout %d ret 0x%02x\n",
516 be32_to_cpu(*timeout), ret);
517
Sean Youngc7470ff2016-07-20 14:03:49 -0300518 if (ret == sizeof(*timeout))
Sean Young4f253ce2016-07-10 13:34:38 -0300519 ret = 0;
Sean Youngc7470ff2016-07-20 14:03:49 -0300520 else if (ret >= 0)
Sean Young4f253ce2016-07-10 13:34:38 -0300521 ret = -EIO;
522
523 kfree(timeout);
524
525 return ret;
526}
527
Jarod Wilson2154be62011-05-04 14:02:42 -0300528static void redrat3_reset(struct redrat3_dev *rr3)
529{
530 struct usb_device *udev = rr3->udev;
531 struct device *dev = rr3->dev;
532 int rc, rxpipe, txpipe;
533 u8 *val;
Markus Elfring576df632016-10-13 08:23:22 -0300534 size_t const len = sizeof(*val);
Jarod Wilson2154be62011-05-04 14:02:42 -0300535
Jarod Wilson2154be62011-05-04 14:02:42 -0300536 rxpipe = usb_rcvctrlpipe(udev, 0);
537 txpipe = usb_sndctrlpipe(udev, 0);
538
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300539 val = kmalloc(len, GFP_KERNEL);
Markus Elfring559f64d2016-10-13 08:20:19 -0300540 if (!val)
Jarod Wilson2154be62011-05-04 14:02:42 -0300541 return;
Jarod Wilson2154be62011-05-04 14:02:42 -0300542
543 *val = 0x01;
544 rc = usb_control_msg(udev, rxpipe, RR3_RESET,
545 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
546 RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25);
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700547 dev_dbg(dev, "reset returned 0x%02x\n", rc);
Jarod Wilson2154be62011-05-04 14:02:42 -0300548
Sean Youngd6ae1622016-07-20 14:03:50 -0300549 *val = length_fuzz;
Jarod Wilson2154be62011-05-04 14:02:42 -0300550 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
551 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
552 RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25);
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700553 dev_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
Jarod Wilson2154be62011-05-04 14:02:42 -0300554
Sean Youngd6ae1622016-07-20 14:03:50 -0300555 *val = (65536 - (minimum_pause * 2000)) / 256;
556 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
557 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
558 RR3_IR_IO_MIN_PAUSE, 0, val, len, HZ * 25);
559 dev_dbg(dev, "set ir parm min pause %d rc 0x%02x\n", *val, rc);
560
561 *val = periods_measure_carrier;
562 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
563 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
564 RR3_IR_IO_PERIODS_MF, 0, val, len, HZ * 25);
565 dev_dbg(dev, "set ir parm periods measure carrier %d rc 0x%02x", *val,
566 rc);
567
Jarod Wilson2154be62011-05-04 14:02:42 -0300568 *val = RR3_DRIVER_MAXLENS;
569 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
570 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
571 RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25);
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700572 dev_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
Jarod Wilson2154be62011-05-04 14:02:42 -0300573
574 kfree(val);
575}
576
577static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
578{
Markus Elfring7aa20af2016-10-13 08:21:55 -0300579 int rc;
Jarod Wilson2154be62011-05-04 14:02:42 -0300580 char *buffer;
581
Markus Elfring0bebaa52016-10-13 03:35:57 -0300582 buffer = kcalloc(RR3_FW_VERSION_LEN + 1, sizeof(*buffer), GFP_KERNEL);
Markus Elfring559f64d2016-10-13 08:20:19 -0300583 if (!buffer)
Jarod Wilson2154be62011-05-04 14:02:42 -0300584 return;
Jarod Wilson2154be62011-05-04 14:02:42 -0300585
586 rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
587 RR3_FW_VERSION,
588 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
589 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5);
590
591 if (rc >= 0)
592 dev_info(rr3->dev, "Firmware rev: %s", buffer);
593 else
594 dev_err(rr3->dev, "Problem fetching firmware ID\n");
595
596 kfree(buffer);
Jarod Wilson2154be62011-05-04 14:02:42 -0300597}
598
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300599static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
Jarod Wilson2154be62011-05-04 14:02:42 -0300600{
Sean Young4c055a52013-02-16 17:25:44 -0300601 struct redrat3_header *header = rr3->bulk_in_buf;
602 unsigned pktlen, pkttype;
Jarod Wilson2154be62011-05-04 14:02:42 -0300603
Jarod Wilson2154be62011-05-04 14:02:42 -0300604 /* grab the Length and type of transfer */
Sean Young4c055a52013-02-16 17:25:44 -0300605 pktlen = be16_to_cpu(header->length);
606 pkttype = be16_to_cpu(header->transfer_type);
Jarod Wilson2154be62011-05-04 14:02:42 -0300607
Sean Young4c055a52013-02-16 17:25:44 -0300608 if (pktlen > sizeof(rr3->irdata)) {
609 dev_warn(rr3->dev, "packet length %u too large\n", pktlen);
610 return;
611 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300612
Sean Young4c055a52013-02-16 17:25:44 -0300613 switch (pkttype) {
Jarod Wilson2154be62011-05-04 14:02:42 -0300614 case RR3_ERROR:
Sean Young4c055a52013-02-16 17:25:44 -0300615 if (len >= sizeof(struct redrat3_error)) {
616 struct redrat3_error *error = rr3->bulk_in_buf;
617 unsigned fw_error = be16_to_cpu(error->fw_error);
618 redrat3_dump_fw_error(rr3, fw_error);
619 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300620 break;
621
622 case RR3_MOD_SIGNAL_IN:
Sean Young4c055a52013-02-16 17:25:44 -0300623 memcpy(&rr3->irdata, rr3->bulk_in_buf, len);
Jarod Wilson2154be62011-05-04 14:02:42 -0300624 rr3->bytes_read = len;
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700625 dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
Sean Young4c055a52013-02-16 17:25:44 -0300626 rr3->bytes_read, pktlen);
Jarod Wilson2154be62011-05-04 14:02:42 -0300627 break;
628
629 default:
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700630 dev_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
Sean Young4c055a52013-02-16 17:25:44 -0300631 pkttype, len, pktlen);
Jarod Wilson2154be62011-05-04 14:02:42 -0300632 break;
633 }
634}
635
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300636static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len)
Jarod Wilson2154be62011-05-04 14:02:42 -0300637{
Sean Young4c055a52013-02-16 17:25:44 -0300638 void *irdata = &rr3->irdata;
639
Sean Young4c055a52013-02-16 17:25:44 -0300640 if (len + rr3->bytes_read > sizeof(rr3->irdata)) {
641 dev_warn(rr3->dev, "too much data for packet\n");
642 rr3->bytes_read = 0;
643 return;
644 }
645
646 memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len);
Jarod Wilson2154be62011-05-04 14:02:42 -0300647
648 rr3->bytes_read += len;
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700649 dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read,
Sean Young4c055a52013-02-16 17:25:44 -0300650 be16_to_cpu(rr3->irdata.header.length));
Jarod Wilson2154be62011-05-04 14:02:42 -0300651}
652
653/* gather IR data from incoming urb, process it when we have enough */
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300654static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len)
Jarod Wilson2154be62011-05-04 14:02:42 -0300655{
656 struct device *dev = rr3->dev;
Sean Young4c055a52013-02-16 17:25:44 -0300657 unsigned pkttype;
Jarod Wilson2154be62011-05-04 14:02:42 -0300658 int ret = 0;
659
Sean Young4c055a52013-02-16 17:25:44 -0300660 if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) {
Jarod Wilson2154be62011-05-04 14:02:42 -0300661 redrat3_read_packet_start(rr3, len);
662 } else if (rr3->bytes_read != 0) {
663 redrat3_read_packet_continue(rr3, len);
664 } else if (rr3->bytes_read == 0) {
665 dev_err(dev, "error: no packet data read\n");
666 ret = -ENODATA;
667 goto out;
668 }
669
Sean Young38e35a82013-07-30 19:00:00 -0300670 if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length) +
671 sizeof(struct redrat3_header))
Jarod Wilson2154be62011-05-04 14:02:42 -0300672 /* we're still accumulating data */
673 return 0;
674
675 /* if we get here, we've got IR data to decode */
Sean Young4c055a52013-02-16 17:25:44 -0300676 pkttype = be16_to_cpu(rr3->irdata.header.transfer_type);
677 if (pkttype == RR3_MOD_SIGNAL_IN)
Jarod Wilson2154be62011-05-04 14:02:42 -0300678 redrat3_process_ir_data(rr3);
679 else
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700680 dev_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n",
Sean Young4c055a52013-02-16 17:25:44 -0300681 pkttype);
Jarod Wilson2154be62011-05-04 14:02:42 -0300682
683out:
684 rr3->bytes_read = 0;
Jarod Wilson2154be62011-05-04 14:02:42 -0300685 return ret;
686}
687
688/* callback function from USB when async USB request has completed */
Sean Young801b69f2013-02-16 17:25:43 -0300689static void redrat3_handle_async(struct urb *urb)
Jarod Wilson2154be62011-05-04 14:02:42 -0300690{
Sean Youngd6aca6e2016-10-31 15:52:21 -0200691 struct redrat3_dev *rr3 = urb->context;
Andrew Vincerdbea1882011-11-08 12:43:45 -0300692 int ret;
Jarod Wilson2154be62011-05-04 14:02:42 -0300693
Jarod Wilson2154be62011-05-04 14:02:42 -0300694 switch (urb->status) {
695 case 0:
Andrew Vincerdbea1882011-11-08 12:43:45 -0300696 ret = redrat3_get_ir_data(rr3, urb->actual_length);
697 if (!ret) {
698 /* no error, prepare to read more */
699 redrat3_issue_async(rr3);
700 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300701 break;
702
703 case -ECONNRESET:
704 case -ENOENT:
705 case -ESHUTDOWN:
706 usb_unlink_urb(urb);
707 return;
708
709 case -EPIPE:
710 default:
711 dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
712 rr3->bytes_read = 0;
Jarod Wilson2154be62011-05-04 14:02:42 -0300713 break;
714 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300715}
716
Jarod Wilson2154be62011-05-04 14:02:42 -0300717static u16 mod_freq_to_val(unsigned int mod_freq)
718{
719 int mult = 6000000;
720
721 /* Clk used in mod. freq. generation is CLK24/4. */
Sean Young4c055a52013-02-16 17:25:44 -0300722 return 65536 - (mult / mod_freq);
Jarod Wilson2154be62011-05-04 14:02:42 -0300723}
724
Andrew Vincerdbea1882011-11-08 12:43:45 -0300725static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
Jarod Wilson2154be62011-05-04 14:02:42 -0300726{
Andrew Vincerdbea1882011-11-08 12:43:45 -0300727 struct redrat3_dev *rr3 = rcdev->priv;
728 struct device *dev = rr3->dev;
Jarod Wilson2154be62011-05-04 14:02:42 -0300729
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700730 dev_dbg(dev, "Setting modulation frequency to %u", carrier);
Dan Carpenter48cafec2012-09-11 07:11:24 -0300731 if (carrier == 0)
732 return -EINVAL;
733
Jarod Wilson2154be62011-05-04 14:02:42 -0300734 rr3->carrier = carrier;
735
Sean Young14d81882016-07-10 13:34:33 -0300736 return 0;
Jarod Wilson2154be62011-05-04 14:02:42 -0300737}
738
Andrew Vincerdbea1882011-11-08 12:43:45 -0300739static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
740 unsigned count)
Jarod Wilson2154be62011-05-04 14:02:42 -0300741{
742 struct redrat3_dev *rr3 = rcdev->priv;
743 struct device *dev = rr3->dev;
Sean Young4c055a52013-02-16 17:25:44 -0300744 struct redrat3_irdata *irdata = NULL;
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300745 int ret, ret_len;
Jarod Wilson2154be62011-05-04 14:02:42 -0300746 int lencheck, cur_sample_len, pipe;
Jarod Wilson2154be62011-05-04 14:02:42 -0300747 int *sample_lens = NULL;
Jarod Wilson2154be62011-05-04 14:02:42 -0300748 u8 curlencheck = 0;
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300749 unsigned i, sendbuf_len;
Jarod Wilson2154be62011-05-04 14:02:42 -0300750
Jarod Wilson2154be62011-05-04 14:02:42 -0300751 if (rr3->transmitting) {
752 dev_warn(dev, "%s: transmitter already in use\n", __func__);
753 return -EAGAIN;
754 }
755
Sean Young671ea672013-07-08 17:33:09 -0300756 if (count > RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN)
757 return -EINVAL;
Jarod Wilson2154be62011-05-04 14:02:42 -0300758
Andrew Vincerdbea1882011-11-08 12:43:45 -0300759 /* rr3 will disable rc detector on transmit */
Jarod Wilson2154be62011-05-04 14:02:42 -0300760 rr3->transmitting = true;
761
Markus Elfring0bebaa52016-10-13 03:35:57 -0300762 sample_lens = kcalloc(RR3_DRIVER_MAXLENS,
763 sizeof(*sample_lens),
764 GFP_KERNEL);
Markus Elfringfac59132016-10-13 05:34:29 -0300765 if (!sample_lens)
766 return -ENOMEM;
Jarod Wilson2154be62011-05-04 14:02:42 -0300767
Sean Young4c055a52013-02-16 17:25:44 -0300768 irdata = kzalloc(sizeof(*irdata), GFP_KERNEL);
769 if (!irdata) {
Sean Young801b69f2013-02-16 17:25:43 -0300770 ret = -ENOMEM;
771 goto out;
772 }
773
Jarod Wilson2154be62011-05-04 14:02:42 -0300774 for (i = 0; i < count; i++) {
Sean Young06eae252013-01-29 08:19:31 -0300775 cur_sample_len = redrat3_us_to_len(txbuf[i]);
Sean Young801b69f2013-02-16 17:25:43 -0300776 if (cur_sample_len > 0xffff) {
777 dev_warn(dev, "transmit period of %uus truncated to %uus\n",
778 txbuf[i], redrat3_len_to_us(0xffff));
779 cur_sample_len = 0xffff;
780 }
Jarod Wilson2154be62011-05-04 14:02:42 -0300781 for (lencheck = 0; lencheck < curlencheck; lencheck++) {
Jarod Wilson2154be62011-05-04 14:02:42 -0300782 if (sample_lens[lencheck] == cur_sample_len)
783 break;
784 }
785 if (lencheck == curlencheck) {
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700786 dev_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
Jarod Wilson2154be62011-05-04 14:02:42 -0300787 i, txbuf[i], curlencheck, cur_sample_len);
Sean Young06eae252013-01-29 08:19:31 -0300788 if (curlencheck < RR3_DRIVER_MAXLENS) {
Jarod Wilson2154be62011-05-04 14:02:42 -0300789 /* now convert the value to a proper
790 * rr3 value.. */
791 sample_lens[curlencheck] = cur_sample_len;
Sean Young4c055a52013-02-16 17:25:44 -0300792 put_unaligned_be16(cur_sample_len,
793 &irdata->lens[curlencheck]);
Jarod Wilson2154be62011-05-04 14:02:42 -0300794 curlencheck++;
795 } else {
Sean Young671ea672013-07-08 17:33:09 -0300796 ret = -EINVAL;
797 goto out;
Jarod Wilson2154be62011-05-04 14:02:42 -0300798 }
799 }
Sean Young4c055a52013-02-16 17:25:44 -0300800 irdata->sigdata[i] = lencheck;
Jarod Wilson2154be62011-05-04 14:02:42 -0300801 }
802
Sean Young4c055a52013-02-16 17:25:44 -0300803 irdata->sigdata[count] = RR3_END_OF_SIGNAL;
804 irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL;
Jarod Wilson2154be62011-05-04 14:02:42 -0300805
Sean Young4c055a52013-02-16 17:25:44 -0300806 sendbuf_len = offsetof(struct redrat3_irdata,
807 sigdata[count + RR3_TX_TRAILER_LEN]);
Jarod Wilson2154be62011-05-04 14:02:42 -0300808 /* fill in our packet header */
Sean Young4c055a52013-02-16 17:25:44 -0300809 irdata->header.length = cpu_to_be16(sendbuf_len -
810 sizeof(struct redrat3_header));
811 irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT);
812 irdata->pause = cpu_to_be32(redrat3_len_to_us(100));
813 irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier));
814 irdata->no_lengths = curlencheck;
815 irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN);
Jarod Wilson2154be62011-05-04 14:02:42 -0300816
817 pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
Sean Young4c055a52013-02-16 17:25:44 -0300818 ret = usb_bulk_msg(rr3->udev, pipe, irdata,
Jarod Wilson2154be62011-05-04 14:02:42 -0300819 sendbuf_len, &ret_len, 10 * HZ);
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700820 dev_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret);
Jarod Wilson2154be62011-05-04 14:02:42 -0300821
822 /* now tell the hardware to transmit what we sent it */
823 pipe = usb_rcvctrlpipe(rr3->udev, 0);
824 ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
825 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
Sean Young4c055a52013-02-16 17:25:44 -0300826 0, 0, irdata, 2, HZ * 10);
Jarod Wilson2154be62011-05-04 14:02:42 -0300827
828 if (ret < 0)
829 dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
830 else
Andrew Vincerdbea1882011-11-08 12:43:45 -0300831 ret = count;
Jarod Wilson2154be62011-05-04 14:02:42 -0300832
833out:
Sean Young4c055a52013-02-16 17:25:44 -0300834 kfree(irdata);
Markus Elfringfac59132016-10-13 05:34:29 -0300835 kfree(sample_lens);
Jarod Wilson2154be62011-05-04 14:02:42 -0300836
837 rr3->transmitting = false;
Andrew Vincerdbea1882011-11-08 12:43:45 -0300838 /* rr3 re-enables rc detector because it was enabled before */
Jarod Wilson2154be62011-05-04 14:02:42 -0300839
840 return ret;
841}
842
Sean Youngbf139722013-07-30 19:00:04 -0300843static void redrat3_brightness_set(struct led_classdev *led_dev, enum
844 led_brightness brightness)
845{
846 struct redrat3_dev *rr3 = container_of(led_dev, struct redrat3_dev,
847 led);
848
849 if (brightness != LED_OFF && atomic_cmpxchg(&rr3->flash, 0, 1) == 0) {
850 int ret = usb_submit_urb(rr3->flash_urb, GFP_ATOMIC);
851 if (ret != 0) {
852 dev_dbg(rr3->dev, "%s: unexpected ret of %d\n",
853 __func__, ret);
854 atomic_set(&rr3->flash, 0);
855 }
856 }
857}
858
859static void redrat3_led_complete(struct urb *urb)
860{
861 struct redrat3_dev *rr3 = urb->context;
862
863 switch (urb->status) {
864 case 0:
865 break;
866 case -ECONNRESET:
867 case -ENOENT:
868 case -ESHUTDOWN:
869 usb_unlink_urb(urb);
870 return;
871 case -EPIPE:
872 default:
873 dev_dbg(rr3->dev, "Error: urb status = %d\n", urb->status);
874 break;
875 }
876
877 rr3->led.brightness = LED_OFF;
878 atomic_dec(&rr3->flash);
879}
880
Jarod Wilson2154be62011-05-04 14:02:42 -0300881static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
882{
883 struct device *dev = rr3->dev;
884 struct rc_dev *rc;
Markus Elfring9d45d5f2016-10-13 09:17:43 -0300885 int ret;
Jarod Wilson2154be62011-05-04 14:02:42 -0300886 u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
887
888 rc = rc_allocate_device();
Markus Elfring559f64d2016-10-13 08:20:19 -0300889 if (!rc)
Markus Elfring3f816782016-10-13 09:54:46 -0300890 return NULL;
Jarod Wilson2154be62011-05-04 14:02:42 -0300891
Sean Youngda500032016-10-31 15:52:20 -0200892 snprintf(rr3->name, sizeof(rr3->name),
893 "RedRat3%s Infrared Remote Transceiver",
894 prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "");
Jarod Wilson2154be62011-05-04 14:02:42 -0300895
896 usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
897
898 rc->input_name = rr3->name;
899 rc->input_phys = rr3->phys;
900 usb_to_input_id(rr3->udev, &rc->input_id);
901 rc->dev.parent = dev;
902 rc->priv = rr3;
903 rc->driver_type = RC_DRIVER_IR_RAW;
David Härdemanc5540fb2014-04-03 20:32:21 -0300904 rc->allowed_protocols = RC_BIT_ALL;
Sean Young4f253ce2016-07-10 13:34:38 -0300905 rc->min_timeout = MS_TO_NS(RR3_RX_MIN_TIMEOUT);
906 rc->max_timeout = MS_TO_NS(RR3_RX_MAX_TIMEOUT);
Sean Youngc7470ff2016-07-20 14:03:49 -0300907 rc->timeout = US_TO_NS(redrat3_get_timeout(rr3));
Sean Young4f253ce2016-07-10 13:34:38 -0300908 rc->s_timeout = redrat3_set_timeout;
Jarod Wilson2154be62011-05-04 14:02:42 -0300909 rc->tx_ir = redrat3_transmit_ir;
910 rc->s_tx_carrier = redrat3_set_tx_carrier;
911 rc->driver_name = DRIVER_NAME;
Sean Young06eae252013-01-29 08:19:31 -0300912 rc->rx_resolution = US_TO_NS(2);
Jarod Wilson2154be62011-05-04 14:02:42 -0300913 rc->map_name = RC_MAP_HAUPPAUGE;
914
915 ret = rc_register_device(rc);
916 if (ret < 0) {
917 dev_err(dev, "remote dev registration failed\n");
918 goto out;
919 }
920
921 return rc;
922
923out:
924 rc_free_device(rc);
925 return NULL;
926}
927
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800928static int redrat3_dev_probe(struct usb_interface *intf,
929 const struct usb_device_id *id)
Jarod Wilson2154be62011-05-04 14:02:42 -0300930{
931 struct usb_device *udev = interface_to_usbdev(intf);
932 struct device *dev = &intf->dev;
933 struct usb_host_interface *uhi;
934 struct redrat3_dev *rr3;
935 struct usb_endpoint_descriptor *ep;
936 struct usb_endpoint_descriptor *ep_in = NULL;
937 struct usb_endpoint_descriptor *ep_out = NULL;
938 u8 addr, attrs;
939 int pipe, i;
940 int retval = -ENOMEM;
941
Jarod Wilson2154be62011-05-04 14:02:42 -0300942 uhi = intf->cur_altsetting;
943
944 /* find our bulk-in and bulk-out endpoints */
945 for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
946 ep = &uhi->endpoint[i].desc;
947 addr = ep->bEndpointAddress;
948 attrs = ep->bmAttributes;
949
950 if ((ep_in == NULL) &&
951 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
952 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
953 USB_ENDPOINT_XFER_BULK)) {
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700954 dev_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
Jarod Wilson2154be62011-05-04 14:02:42 -0300955 ep->bEndpointAddress);
956 /* data comes in on 0x82, 0x81 is for other data... */
957 if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR)
958 ep_in = ep;
959 }
960
961 if ((ep_out == NULL) &&
962 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
963 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
964 USB_ENDPOINT_XFER_BULK)) {
Greg Kroah-Hartman3228bf82014-05-28 11:34:01 -0700965 dev_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
Jarod Wilson2154be62011-05-04 14:02:42 -0300966 ep->bEndpointAddress);
967 ep_out = ep;
968 }
969 }
970
971 if (!ep_in || !ep_out) {
972 dev_err(dev, "Couldn't find both in and out endpoints\n");
973 retval = -ENODEV;
974 goto no_endpoints;
975 }
976
977 /* allocate memory for our device state and initialize it */
978 rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
Markus Elfring559f64d2016-10-13 08:20:19 -0300979 if (!rr3)
Dan Carpenter7eb75712011-05-26 05:55:08 -0300980 goto no_endpoints;
Jarod Wilson2154be62011-05-04 14:02:42 -0300981
982 rr3->dev = &intf->dev;
983
984 /* set up bulk-in endpoint */
985 rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sangcd859c12016-08-11 18:03:40 -0300986 if (!rr3->read_urb)
Jarod Wilson2154be62011-05-04 14:02:42 -0300987 goto error;
Jarod Wilson2154be62011-05-04 14:02:42 -0300988
989 rr3->ep_in = ep_in;
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300990 rr3->bulk_in_buf = usb_alloc_coherent(udev,
Sean Youngd0a0a652014-12-01 05:48:16 -0300991 le16_to_cpu(ep_in->wMaxPacketSize), GFP_KERNEL, &rr3->dma_in);
Markus Elfring559f64d2016-10-13 08:20:19 -0300992 if (!rr3->bulk_in_buf)
Jarod Wilson2154be62011-05-04 14:02:42 -0300993 goto error;
Jarod Wilson2154be62011-05-04 14:02:42 -0300994
995 pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress);
Sean Youngfa7b9ac2013-02-16 17:25:45 -0300996 usb_fill_bulk_urb(rr3->read_urb, udev, pipe, rr3->bulk_in_buf,
997 le16_to_cpu(ep_in->wMaxPacketSize), redrat3_handle_async, rr3);
Sean Youngd0a0a652014-12-01 05:48:16 -0300998 rr3->read_urb->transfer_dma = rr3->dma_in;
999 rr3->read_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Jarod Wilson2154be62011-05-04 14:02:42 -03001000
1001 rr3->ep_out = ep_out;
Jarod Wilson2154be62011-05-04 14:02:42 -03001002 rr3->udev = udev;
1003
1004 redrat3_reset(rr3);
1005 redrat3_get_firmware_rev(rr3);
1006
1007 /* might be all we need to do? */
1008 retval = redrat3_enable_detector(rr3);
1009 if (retval < 0)
1010 goto error;
1011
1012 /* default.. will get overridden by any sends with a freq defined */
1013 rr3->carrier = 38000;
1014
Sean Youngbf139722013-07-30 19:00:04 -03001015 /* led control */
1016 rr3->led.name = "redrat3:red:feedback";
1017 rr3->led.default_trigger = "rc-feedback";
1018 rr3->led.brightness_set = redrat3_brightness_set;
1019 retval = led_classdev_register(&intf->dev, &rr3->led);
1020 if (retval)
1021 goto error;
1022
1023 atomic_set(&rr3->flash, 0);
1024 rr3->flash_urb = usb_alloc_urb(0, GFP_KERNEL);
1025 if (!rr3->flash_urb) {
1026 retval = -ENOMEM;
1027 goto led_free_error;
1028 }
1029
1030 /* setup packet is 'c0 b9 0000 0000 0001' */
1031 rr3->flash_control.bRequestType = 0xc0;
1032 rr3->flash_control.bRequest = RR3_BLINK_LED;
1033 rr3->flash_control.wLength = cpu_to_le16(1);
1034
1035 usb_fill_control_urb(rr3->flash_urb, udev, usb_rcvctrlpipe(udev, 0),
1036 (unsigned char *)&rr3->flash_control,
1037 &rr3->flash_in_buf, sizeof(rr3->flash_in_buf),
1038 redrat3_led_complete, rr3);
1039
Jarod Wilson2154be62011-05-04 14:02:42 -03001040 rr3->rc = redrat3_init_rc_dev(rr3);
Peter Senna Tschudin6ea7cf72012-09-04 08:05:05 -03001041 if (!rr3->rc) {
1042 retval = -ENOMEM;
Sean Youngbf139722013-07-30 19:00:04 -03001043 goto led_free_error;
Peter Senna Tschudin6ea7cf72012-09-04 08:05:05 -03001044 }
Jarod Wilson2154be62011-05-04 14:02:42 -03001045
1046 /* we can register the device now, as it is ready */
1047 usb_set_intfdata(intf, rr3);
1048
Jarod Wilson2154be62011-05-04 14:02:42 -03001049 return 0;
1050
Sean Youngbf139722013-07-30 19:00:04 -03001051led_free_error:
1052 led_classdev_unregister(&rr3->led);
Jarod Wilson2154be62011-05-04 14:02:42 -03001053error:
1054 redrat3_delete(rr3, rr3->udev);
1055
1056no_endpoints:
Jarod Wilson2154be62011-05-04 14:02:42 -03001057 return retval;
1058}
1059
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001060static void redrat3_dev_disconnect(struct usb_interface *intf)
Jarod Wilson2154be62011-05-04 14:02:42 -03001061{
1062 struct usb_device *udev = interface_to_usbdev(intf);
1063 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1064
Jarod Wilson2154be62011-05-04 14:02:42 -03001065 usb_set_intfdata(intf, NULL);
1066 rc_unregister_device(rr3->rc);
Sean Youngbf139722013-07-30 19:00:04 -03001067 led_classdev_unregister(&rr3->led);
Jarod Wilson2154be62011-05-04 14:02:42 -03001068 redrat3_delete(rr3, udev);
Jarod Wilson2154be62011-05-04 14:02:42 -03001069}
1070
1071static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
1072{
1073 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
Greg Kroah-Hartmana36d5b612014-05-28 11:34:00 -07001074
Sean Youngbf139722013-07-30 19:00:04 -03001075 led_classdev_suspend(&rr3->led);
Jarod Wilson2154be62011-05-04 14:02:42 -03001076 usb_kill_urb(rr3->read_urb);
Sean Youngbf139722013-07-30 19:00:04 -03001077 usb_kill_urb(rr3->flash_urb);
Jarod Wilson2154be62011-05-04 14:02:42 -03001078 return 0;
1079}
1080
1081static int redrat3_dev_resume(struct usb_interface *intf)
1082{
1083 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
Greg Kroah-Hartmana36d5b612014-05-28 11:34:00 -07001084
Jarod Wilson2154be62011-05-04 14:02:42 -03001085 if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC))
1086 return -EIO;
Sean Youngbf139722013-07-30 19:00:04 -03001087 led_classdev_resume(&rr3->led);
Jarod Wilson2154be62011-05-04 14:02:42 -03001088 return 0;
1089}
1090
1091static struct usb_driver redrat3_dev_driver = {
1092 .name = DRIVER_NAME,
1093 .probe = redrat3_dev_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001094 .disconnect = redrat3_dev_disconnect,
Jarod Wilson2154be62011-05-04 14:02:42 -03001095 .suspend = redrat3_dev_suspend,
1096 .resume = redrat3_dev_resume,
1097 .reset_resume = redrat3_dev_resume,
1098 .id_table = redrat3_dev_table
1099};
1100
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -08001101module_usb_driver(redrat3_dev_driver);
Jarod Wilson2154be62011-05-04 14:02:42 -03001102
1103MODULE_DESCRIPTION(DRIVER_DESC);
1104MODULE_AUTHOR(DRIVER_AUTHOR);
1105MODULE_AUTHOR(DRIVER_AUTHOR2);
1106MODULE_LICENSE("GPL");
1107MODULE_DEVICE_TABLE(usb, redrat3_dev_table);