Merge remote-tracking branch 'kspp/for-next/kspp'
[deliverable/linux.git] / drivers / media / rc / redrat3.c
CommitLineData
2154be65
JW
1/*
2 * USB RedRat3 IR Transceiver rc-core driver
3 *
4 * Copyright (c) 2011 by Jarod Wilson <jarod@redhat.com>
5 * based heavily on the work of Stephen Cox, with additional
6 * help from RedRat Ltd.
7 *
8 * This driver began life based an an old version of the first-generation
9 * lirc_mceusb driver from the lirc 0.7.2 distribution. It was then
10 * significantly rewritten by Stephen Cox with the aid of RedRat Ltd's
11 * Chris Dodge.
12 *
13 * The driver was then ported to rc-core and significantly rewritten again,
14 * by Jarod, using the in-kernel mceusb driver as a guide, after an initial
15 * port effort was started by Stephen.
16 *
17 * TODO LIST:
18 * - fix lirc not showing repeats properly
19 * --
20 *
21 * The RedRat3 is a USB transceiver with both send & receive,
22 * with 2 separate sensors available for receive to enable
23 * both good long range reception for general use, and good
24 * short range reception when required for learning a signal.
25 *
26 * http://www.redrat.co.uk/
27 *
28 * It uses its own little protocol to communicate, the required
29 * parts of which are embedded within this driver.
30 * --
31 *
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
36 *
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
41 *
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
45 *
46 */
47
4c055a5a 48#include <asm/unaligned.h>
2154be65 49#include <linux/device.h>
bf139726 50#include <linux/leds.h>
2154be65
JW
51#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 */
2154be65
JW
58#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
2154be65
JW
63/* 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
39c1cb2b 99/* Minimum value for pause recognition. */
2154be65
JW
100#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
2154be65 111/* Size of the fixed-length portion of the signal */
2154be65
JW
112#define RR3_DRIVER_MAXLENS 128
113#define RR3_MAX_SIG_SIZE 512
2154be65
JW
114#define RR3_TIME_UNIT 50
115#define RR3_END_OF_SIGNAL 0x7f
2154be65
JW
116#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
4c055a5a
SY
127struct 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
2154be65
JW
153/* 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
bf139726
SY
168 /* 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
2154be65
JW
175 /* 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 */
4c055a5a 181 void *bulk_in_buf;
2154be65
JW
182 /* urb used to read ir data */
183 struct urb *read_urb;
184
185 /* the send endpoint */
186 struct usb_endpoint_descriptor *ep_out;
2154be65
JW
187
188 /* usb dma */
189 dma_addr_t dma_in;
2154be65 190
25da661a 191 /* rx signal timeout */
c53f9f00 192 u32 hw_timeout;
2154be65 193
2154be65
JW
194 /* Is the device currently transmitting?*/
195 bool transmitting;
196
197 /* store for current packet */
4c055a5a 198 struct redrat3_irdata irdata;
2154be65 199 u16 bytes_read;
2154be65
JW
200
201 u32 carrier;
202
4c055a5a 203 char name[64];
2154be65
JW
204 char phys[64];
205};
206
2154be65
JW
207/*
208 * redrat3_issue_async
209 *
210 * Issues an async read to the ir data in port..
211 * sets the callback to be redrat3_handle_async
212 */
213static void redrat3_issue_async(struct redrat3_dev *rr3)
214{
215 int res;
216
2154be65
JW
217 res = usb_submit_urb(rr3->read_urb, GFP_ATOMIC);
218 if (res)
3228bf81
GKH
219 dev_dbg(rr3->dev,
220 "%s: receive request FAILED! (res %d, len %d)\n",
221 __func__, res, rr3->read_urb->transfer_buffer_length);
2154be65
JW
222}
223
224static void redrat3_dump_fw_error(struct redrat3_dev *rr3, int code)
225{
226 if (!rr3->transmitting && (code != 0x40))
227 dev_info(rr3->dev, "fw error code 0x%02x: ", code);
228
229 switch (code) {
230 case 0x00:
231 pr_cont("No Error\n");
232 break;
233
234 /* Codes 0x20 through 0x2f are IR Firmware Errors */
235 case 0x20:
236 pr_cont("Initial signal pulse not long enough "
237 "to measure carrier frequency\n");
238 break;
239 case 0x21:
240 pr_cont("Not enough length values allocated for signal\n");
241 break;
242 case 0x22:
243 pr_cont("Not enough memory allocated for signal data\n");
244 break;
245 case 0x23:
246 pr_cont("Too many signal repeats\n");
247 break;
248 case 0x28:
249 pr_cont("Insufficient memory available for IR signal "
250 "data memory allocation\n");
251 break;
252 case 0x29:
253 pr_cont("Insufficient memory available "
254 "for IrDa signal data memory allocation\n");
255 break;
256
257 /* Codes 0x30 through 0x3f are USB Firmware Errors */
258 case 0x30:
259 pr_cont("Insufficient memory available for bulk "
260 "transfer structure\n");
261 break;
262
263 /*
264 * Other error codes... These are primarily errors that can occur in
265 * the control messages sent to the redrat
266 */
267 case 0x40:
268 if (!rr3->transmitting)
269 pr_cont("Signal capture has been terminated\n");
270 break;
271 case 0x41:
272 pr_cont("Attempt to set/get and unknown signal I/O "
273 "algorithm parameter\n");
274 break;
275 case 0x42:
276 pr_cont("Signal capture already started\n");
277 break;
278
279 default:
280 pr_cont("Unknown Error\n");
281 break;
282 }
283}
284
4c055a5a 285static u32 redrat3_val_to_mod_freq(struct redrat3_irdata *irdata)
2154be65
JW
286{
287 u32 mod_freq = 0;
4c055a5a 288 u16 mod_freq_count = be16_to_cpu(irdata->mod_freq_count);
2154be65 289
4c055a5a
SY
290 if (mod_freq_count != 0)
291 mod_freq = (RR3_CLK * be16_to_cpu(irdata->num_periods)) /
292 (mod_freq_count * RR3_CLK_PER_COUNT);
2154be65
JW
293
294 return mod_freq;
295}
296
297/* this function scales down the figures for the same result... */
298static u32 redrat3_len_to_us(u32 length)
299{
300 u32 biglen = length * 1000;
301 u32 divisor = (RR3_CLK_CONV_FACTOR) / 1000;
302 u32 result = (u32) (biglen / divisor);
303
304 /* don't allow zero lengths to go back, breaks lirc */
305 return result ? result : 1;
306}
307
308/*
309 * convert us back into redrat3 lengths
310 *
311 * length * 1000 length * 1000000
312 * ------------- = ---------------- = micro
313 * rr3clk / 1000 rr3clk
314
315 * 6 * 2 4 * 3 micro * rr3clk micro * rr3clk / 1000
316 * ----- = 4 ----- = 6 -------------- = len ---------------------
317 * 3 2 1000000 1000
318 */
319static u32 redrat3_us_to_len(u32 microsec)
320{
321 u32 result;
322 u32 divisor;
323
95cf60aa 324 microsec = (microsec > IR_MAX_DURATION) ? IR_MAX_DURATION : microsec;
2154be65
JW
325 divisor = (RR3_CLK_CONV_FACTOR / 1000);
326 result = (u32)(microsec * divisor) / 1000;
327
328 /* don't allow zero lengths to go back, breaks lirc */
329 return result ? result : 1;
2154be65
JW
330}
331
2154be65
JW
332static void redrat3_process_ir_data(struct redrat3_dev *rr3)
333{
334 DEFINE_IR_RAW_EVENT(rawir);
2154be65 335 struct device *dev;
25da661a 336 unsigned int i, sig_size, single_len, offset, val;
4c055a5a 337 u32 mod_freq;
2154be65
JW
338
339 if (!rr3) {
340 pr_err("%s called with no context!\n", __func__);
341 return;
342 }
343
2154be65 344 dev = rr3->dev;
2154be65 345
4c055a5a 346 mod_freq = redrat3_val_to_mod_freq(&rr3->irdata);
3228bf81 347 dev_dbg(dev, "Got mod_freq of %u\n", mod_freq);
2154be65 348
2154be65 349 /* process each rr3 encoded byte into an int */
4c055a5a
SY
350 sig_size = be16_to_cpu(rr3->irdata.sig_size);
351 for (i = 0; i < sig_size; i++) {
352 offset = rr3->irdata.sigdata[i];
353 val = get_unaligned_be16(&rr3->irdata.lens[offset]);
354 single_len = redrat3_len_to_us(val);
2154be65 355
2154be65
JW
356 /* we should always get pulse/space/pulse/space samples */
357 if (i % 2)
358 rawir.pulse = false;
359 else
360 rawir.pulse = true;
361
362 rawir.duration = US_TO_NS(single_len);
2c594ffa 363 /* cap the value to IR_MAX_DURATION */
95cf60aa
MCC
364 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
365 IR_MAX_DURATION : rawir.duration;
2c594ffa 366
3228bf81 367 dev_dbg(dev, "storing %s with duration %d (i: %d)\n",
2154be65
JW
368 rawir.pulse ? "pulse" : "space", rawir.duration, i);
369 ir_raw_event_store_with_filter(rr3->rc, &rawir);
370 }
371
25da661a
SY
372 /* add a trailing space */
373 rawir.pulse = false;
374 rawir.timeout = true;
375 rawir.duration = US_TO_NS(rr3->hw_timeout);
376 dev_dbg(dev, "storing trailing timeout with duration %d\n",
377 rawir.duration);
378 ir_raw_event_store_with_filter(rr3->rc, &rawir);
2154be65 379
3228bf81 380 dev_dbg(dev, "calling ir_raw_event_handle\n");
2154be65 381 ir_raw_event_handle(rr3->rc);
2154be65
JW
382}
383
384/* Util fn to send rr3 cmds */
6948524d 385static int redrat3_send_cmd(int cmd, struct redrat3_dev *rr3)
2154be65
JW
386{
387 struct usb_device *udev;
388 u8 *data;
389 int res;
390
391 data = kzalloc(sizeof(u8), GFP_KERNEL);
392 if (!data)
393 return -ENOMEM;
394
395 udev = rr3->udev;
396 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), cmd,
397 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
398 0x0000, 0x0000, data, sizeof(u8), HZ * 10);
399
400 if (res < 0) {
401 dev_err(rr3->dev, "%s: Error sending rr3 cmd res %d, data %d",
402 __func__, res, *data);
403 res = -EIO;
404 } else
4c055a5a 405 res = data[0];
2154be65
JW
406
407 kfree(data);
408
409 return res;
410}
411
412/* Enables the long range detector and starts async receive */
413static int redrat3_enable_detector(struct redrat3_dev *rr3)
414{
415 struct device *dev = rr3->dev;
416 u8 ret;
417
2154be65
JW
418 ret = redrat3_send_cmd(RR3_RC_DET_ENABLE, rr3);
419 if (ret != 0)
420 dev_dbg(dev, "%s: unexpected ret of %d\n",
421 __func__, ret);
422
423 ret = redrat3_send_cmd(RR3_RC_DET_STATUS, rr3);
424 if (ret != 1) {
425 dev_err(dev, "%s: detector status: %d, should be 1\n",
426 __func__, ret);
427 return -EIO;
428 }
429
2154be65
JW
430 redrat3_issue_async(rr3);
431
432 return 0;
433}
434
2154be65
JW
435static inline void redrat3_delete(struct redrat3_dev *rr3,
436 struct usb_device *udev)
437{
2154be65 438 usb_kill_urb(rr3->read_urb);
bf139726 439 usb_kill_urb(rr3->flash_urb);
2154be65 440 usb_free_urb(rr3->read_urb);
bf139726 441 usb_free_urb(rr3->flash_urb);
fa7b9ac2 442 usb_free_coherent(udev, le16_to_cpu(rr3->ep_in->wMaxPacketSize),
2154be65 443 rr3->bulk_in_buf, rr3->dma_in);
2154be65
JW
444
445 kfree(rr3);
446}
447
c53f9f00 448static u32 redrat3_get_timeout(struct redrat3_dev *rr3)
2154be65 449{
801b69f2 450 __be32 *tmp;
c53f9f00 451 u32 timeout = MS_TO_US(150); /* a sane default, if things go haywire */
2154be65
JW
452 int len, ret, pipe;
453
454 len = sizeof(*tmp);
455 tmp = kzalloc(len, GFP_KERNEL);
456 if (!tmp) {
c53f9f00 457 dev_warn(rr3->dev, "Memory allocation faillure\n");
2154be65
JW
458 return timeout;
459 }
460
c53f9f00
JW
461 pipe = usb_rcvctrlpipe(rr3->udev, 0);
462 ret = usb_control_msg(rr3->udev, pipe, RR3_GET_IR_PARAM,
2154be65
JW
463 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
464 RR3_IR_IO_SIG_TIMEOUT, 0, tmp, len, HZ * 5);
801b69f2 465 if (ret != len)
c53f9f00 466 dev_warn(rr3->dev, "Failed to read timeout from hardware\n");
801b69f2
SY
467 else {
468 timeout = redrat3_len_to_us(be32_to_cpup(tmp));
469
3228bf81 470 dev_dbg(rr3->dev, "Got timeout of %d ms\n", timeout / 1000);
2154be65
JW
471 }
472
801b69f2 473 kfree(tmp);
2154be65 474
2154be65
JW
475 return timeout;
476}
477
4f253cec
SY
478static int redrat3_set_timeout(struct rc_dev *rc_dev, unsigned int timeoutns)
479{
480 struct redrat3_dev *rr3 = rc_dev->priv;
481 struct usb_device *udev = rr3->udev;
482 struct device *dev = rr3->dev;
b1cb50be 483 __be32 *timeout;
4f253cec
SY
484 int ret;
485
486 timeout = kmalloc(sizeof(*timeout), GFP_KERNEL);
487 if (!timeout)
488 return -ENOMEM;
489
490 *timeout = cpu_to_be32(redrat3_us_to_len(timeoutns / 1000));
491 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), RR3_SET_IR_PARAM,
492 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
493 RR3_IR_IO_SIG_TIMEOUT, 0, timeout, sizeof(*timeout),
494 HZ * 25);
495 dev_dbg(dev, "set ir parm timeout %d ret 0x%02x\n",
496 be32_to_cpu(*timeout), ret);
497
498 if (ret == sizeof(*timeout)) {
499 rr3->hw_timeout = timeoutns / 1000;
500 ret = 0;
501 } else if (ret >= 0)
502 ret = -EIO;
503
504 kfree(timeout);
505
506 return ret;
507}
508
2154be65
JW
509static void redrat3_reset(struct redrat3_dev *rr3)
510{
511 struct usb_device *udev = rr3->udev;
512 struct device *dev = rr3->dev;
513 int rc, rxpipe, txpipe;
514 u8 *val;
515 int len = sizeof(u8);
516
2154be65
JW
517 rxpipe = usb_rcvctrlpipe(udev, 0);
518 txpipe = usb_sndctrlpipe(udev, 0);
519
fa7b9ac2 520 val = kmalloc(len, GFP_KERNEL);
2154be65
JW
521 if (!val) {
522 dev_err(dev, "Memory allocation failure\n");
523 return;
524 }
525
526 *val = 0x01;
527 rc = usb_control_msg(udev, rxpipe, RR3_RESET,
528 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
529 RR3_CPUCS_REG_ADDR, 0, val, len, HZ * 25);
3228bf81 530 dev_dbg(dev, "reset returned 0x%02x\n", rc);
2154be65
JW
531
532 *val = 5;
533 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
534 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
535 RR3_IR_IO_LENGTH_FUZZ, 0, val, len, HZ * 25);
3228bf81 536 dev_dbg(dev, "set ir parm len fuzz %d rc 0x%02x\n", *val, rc);
2154be65
JW
537
538 *val = RR3_DRIVER_MAXLENS;
539 rc = usb_control_msg(udev, txpipe, RR3_SET_IR_PARAM,
540 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
541 RR3_IR_IO_MAX_LENGTHS, 0, val, len, HZ * 25);
3228bf81 542 dev_dbg(dev, "set ir parm max lens %d rc 0x%02x\n", *val, rc);
2154be65
JW
543
544 kfree(val);
545}
546
547static void redrat3_get_firmware_rev(struct redrat3_dev *rr3)
548{
549 int rc = 0;
550 char *buffer;
551
2154be65
JW
552 buffer = kzalloc(sizeof(char) * (RR3_FW_VERSION_LEN + 1), GFP_KERNEL);
553 if (!buffer) {
554 dev_err(rr3->dev, "Memory allocation failure\n");
555 return;
556 }
557
558 rc = usb_control_msg(rr3->udev, usb_rcvctrlpipe(rr3->udev, 0),
559 RR3_FW_VERSION,
560 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
561 0, 0, buffer, RR3_FW_VERSION_LEN, HZ * 5);
562
563 if (rc >= 0)
564 dev_info(rr3->dev, "Firmware rev: %s", buffer);
565 else
566 dev_err(rr3->dev, "Problem fetching firmware ID\n");
567
568 kfree(buffer);
2154be65
JW
569}
570
fa7b9ac2 571static void redrat3_read_packet_start(struct redrat3_dev *rr3, unsigned len)
2154be65 572{
4c055a5a
SY
573 struct redrat3_header *header = rr3->bulk_in_buf;
574 unsigned pktlen, pkttype;
2154be65 575
2154be65 576 /* grab the Length and type of transfer */
4c055a5a
SY
577 pktlen = be16_to_cpu(header->length);
578 pkttype = be16_to_cpu(header->transfer_type);
2154be65 579
4c055a5a
SY
580 if (pktlen > sizeof(rr3->irdata)) {
581 dev_warn(rr3->dev, "packet length %u too large\n", pktlen);
582 return;
583 }
2154be65 584
4c055a5a 585 switch (pkttype) {
2154be65 586 case RR3_ERROR:
4c055a5a
SY
587 if (len >= sizeof(struct redrat3_error)) {
588 struct redrat3_error *error = rr3->bulk_in_buf;
589 unsigned fw_error = be16_to_cpu(error->fw_error);
590 redrat3_dump_fw_error(rr3, fw_error);
591 }
2154be65
JW
592 break;
593
594 case RR3_MOD_SIGNAL_IN:
4c055a5a 595 memcpy(&rr3->irdata, rr3->bulk_in_buf, len);
2154be65 596 rr3->bytes_read = len;
3228bf81 597 dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n",
4c055a5a 598 rr3->bytes_read, pktlen);
2154be65
JW
599 break;
600
601 default:
3228bf81 602 dev_dbg(rr3->dev, "ignoring packet with type 0x%02x, len of %d, 0x%02x\n",
4c055a5a 603 pkttype, len, pktlen);
2154be65
JW
604 break;
605 }
606}
607
fa7b9ac2 608static void redrat3_read_packet_continue(struct redrat3_dev *rr3, unsigned len)
2154be65 609{
4c055a5a
SY
610 void *irdata = &rr3->irdata;
611
4c055a5a
SY
612 if (len + rr3->bytes_read > sizeof(rr3->irdata)) {
613 dev_warn(rr3->dev, "too much data for packet\n");
614 rr3->bytes_read = 0;
615 return;
616 }
617
618 memcpy(irdata + rr3->bytes_read, rr3->bulk_in_buf, len);
2154be65
JW
619
620 rr3->bytes_read += len;
3228bf81 621 dev_dbg(rr3->dev, "bytes_read %d, pktlen %d\n", rr3->bytes_read,
4c055a5a 622 be16_to_cpu(rr3->irdata.header.length));
2154be65
JW
623}
624
625/* gather IR data from incoming urb, process it when we have enough */
fa7b9ac2 626static int redrat3_get_ir_data(struct redrat3_dev *rr3, unsigned len)
2154be65
JW
627{
628 struct device *dev = rr3->dev;
4c055a5a 629 unsigned pkttype;
2154be65
JW
630 int ret = 0;
631
4c055a5a 632 if (rr3->bytes_read == 0 && len >= sizeof(struct redrat3_header)) {
2154be65
JW
633 redrat3_read_packet_start(rr3, len);
634 } else if (rr3->bytes_read != 0) {
635 redrat3_read_packet_continue(rr3, len);
636 } else if (rr3->bytes_read == 0) {
637 dev_err(dev, "error: no packet data read\n");
638 ret = -ENODATA;
639 goto out;
640 }
641
38e35a85
SY
642 if (rr3->bytes_read < be16_to_cpu(rr3->irdata.header.length) +
643 sizeof(struct redrat3_header))
2154be65
JW
644 /* we're still accumulating data */
645 return 0;
646
647 /* if we get here, we've got IR data to decode */
4c055a5a
SY
648 pkttype = be16_to_cpu(rr3->irdata.header.transfer_type);
649 if (pkttype == RR3_MOD_SIGNAL_IN)
2154be65
JW
650 redrat3_process_ir_data(rr3);
651 else
3228bf81 652 dev_dbg(dev, "discarding non-signal data packet (type 0x%02x)\n",
4c055a5a 653 pkttype);
2154be65
JW
654
655out:
656 rr3->bytes_read = 0;
2154be65
JW
657 return ret;
658}
659
660/* callback function from USB when async USB request has completed */
801b69f2 661static void redrat3_handle_async(struct urb *urb)
2154be65
JW
662{
663 struct redrat3_dev *rr3;
dbea1880 664 int ret;
2154be65
JW
665
666 if (!urb)
667 return;
668
669 rr3 = urb->context;
670 if (!rr3) {
671 pr_err("%s called with invalid context!\n", __func__);
672 usb_unlink_urb(urb);
673 return;
674 }
675
2154be65
JW
676 switch (urb->status) {
677 case 0:
dbea1880
AV
678 ret = redrat3_get_ir_data(rr3, urb->actual_length);
679 if (!ret) {
680 /* no error, prepare to read more */
681 redrat3_issue_async(rr3);
682 }
2154be65
JW
683 break;
684
685 case -ECONNRESET:
686 case -ENOENT:
687 case -ESHUTDOWN:
688 usb_unlink_urb(urb);
689 return;
690
691 case -EPIPE:
692 default:
693 dev_warn(rr3->dev, "Error: urb status = %d\n", urb->status);
694 rr3->bytes_read = 0;
2154be65
JW
695 break;
696 }
2154be65
JW
697}
698
2154be65
JW
699static u16 mod_freq_to_val(unsigned int mod_freq)
700{
701 int mult = 6000000;
702
703 /* Clk used in mod. freq. generation is CLK24/4. */
4c055a5a 704 return 65536 - (mult / mod_freq);
2154be65
JW
705}
706
dbea1880 707static int redrat3_set_tx_carrier(struct rc_dev *rcdev, u32 carrier)
2154be65 708{
dbea1880
AV
709 struct redrat3_dev *rr3 = rcdev->priv;
710 struct device *dev = rr3->dev;
2154be65 711
3228bf81 712 dev_dbg(dev, "Setting modulation frequency to %u", carrier);
48cafec9
DC
713 if (carrier == 0)
714 return -EINVAL;
715
2154be65
JW
716 rr3->carrier = carrier;
717
14d8188a 718 return 0;
2154be65
JW
719}
720
dbea1880
AV
721static int redrat3_transmit_ir(struct rc_dev *rcdev, unsigned *txbuf,
722 unsigned count)
2154be65
JW
723{
724 struct redrat3_dev *rr3 = rcdev->priv;
725 struct device *dev = rr3->dev;
4c055a5a 726 struct redrat3_irdata *irdata = NULL;
fa7b9ac2 727 int ret, ret_len;
2154be65 728 int lencheck, cur_sample_len, pipe;
2154be65 729 int *sample_lens = NULL;
2154be65 730 u8 curlencheck = 0;
fa7b9ac2 731 unsigned i, sendbuf_len;
2154be65 732
2154be65
JW
733 if (rr3->transmitting) {
734 dev_warn(dev, "%s: transmitter already in use\n", __func__);
735 return -EAGAIN;
736 }
737
671ea670
SY
738 if (count > RR3_MAX_SIG_SIZE - RR3_TX_TRAILER_LEN)
739 return -EINVAL;
2154be65 740
dbea1880 741 /* rr3 will disable rc detector on transmit */
2154be65
JW
742 rr3->transmitting = true;
743
2154be65
JW
744 sample_lens = kzalloc(sizeof(int) * RR3_DRIVER_MAXLENS, GFP_KERNEL);
745 if (!sample_lens) {
746 ret = -ENOMEM;
747 goto out;
748 }
749
4c055a5a
SY
750 irdata = kzalloc(sizeof(*irdata), GFP_KERNEL);
751 if (!irdata) {
801b69f2
SY
752 ret = -ENOMEM;
753 goto out;
754 }
755
2154be65 756 for (i = 0; i < count; i++) {
06eae25f 757 cur_sample_len = redrat3_us_to_len(txbuf[i]);
801b69f2
SY
758 if (cur_sample_len > 0xffff) {
759 dev_warn(dev, "transmit period of %uus truncated to %uus\n",
760 txbuf[i], redrat3_len_to_us(0xffff));
761 cur_sample_len = 0xffff;
762 }
2154be65 763 for (lencheck = 0; lencheck < curlencheck; lencheck++) {
2154be65
JW
764 if (sample_lens[lencheck] == cur_sample_len)
765 break;
766 }
767 if (lencheck == curlencheck) {
3228bf81 768 dev_dbg(dev, "txbuf[%d]=%u, pos %d, enc %u\n",
2154be65 769 i, txbuf[i], curlencheck, cur_sample_len);
06eae25f 770 if (curlencheck < RR3_DRIVER_MAXLENS) {
2154be65
JW
771 /* now convert the value to a proper
772 * rr3 value.. */
773 sample_lens[curlencheck] = cur_sample_len;
4c055a5a
SY
774 put_unaligned_be16(cur_sample_len,
775 &irdata->lens[curlencheck]);
2154be65
JW
776 curlencheck++;
777 } else {
671ea670
SY
778 ret = -EINVAL;
779 goto out;
2154be65
JW
780 }
781 }
4c055a5a 782 irdata->sigdata[i] = lencheck;
2154be65
JW
783 }
784
4c055a5a
SY
785 irdata->sigdata[count] = RR3_END_OF_SIGNAL;
786 irdata->sigdata[count + 1] = RR3_END_OF_SIGNAL;
2154be65 787
4c055a5a
SY
788 sendbuf_len = offsetof(struct redrat3_irdata,
789 sigdata[count + RR3_TX_TRAILER_LEN]);
2154be65 790 /* fill in our packet header */
4c055a5a
SY
791 irdata->header.length = cpu_to_be16(sendbuf_len -
792 sizeof(struct redrat3_header));
793 irdata->header.transfer_type = cpu_to_be16(RR3_MOD_SIGNAL_OUT);
794 irdata->pause = cpu_to_be32(redrat3_len_to_us(100));
795 irdata->mod_freq_count = cpu_to_be16(mod_freq_to_val(rr3->carrier));
796 irdata->no_lengths = curlencheck;
797 irdata->sig_size = cpu_to_be16(count + RR3_TX_TRAILER_LEN);
2154be65
JW
798
799 pipe = usb_sndbulkpipe(rr3->udev, rr3->ep_out->bEndpointAddress);
4c055a5a 800 ret = usb_bulk_msg(rr3->udev, pipe, irdata,
2154be65 801 sendbuf_len, &ret_len, 10 * HZ);
3228bf81 802 dev_dbg(dev, "sent %d bytes, (ret %d)\n", ret_len, ret);
2154be65
JW
803
804 /* now tell the hardware to transmit what we sent it */
805 pipe = usb_rcvctrlpipe(rr3->udev, 0);
806 ret = usb_control_msg(rr3->udev, pipe, RR3_TX_SEND_SIGNAL,
807 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
4c055a5a 808 0, 0, irdata, 2, HZ * 10);
2154be65
JW
809
810 if (ret < 0)
811 dev_err(dev, "Error: control msg send failed, rc %d\n", ret);
812 else
dbea1880 813 ret = count;
2154be65
JW
814
815out:
816 kfree(sample_lens);
4c055a5a 817 kfree(irdata);
2154be65
JW
818
819 rr3->transmitting = false;
dbea1880 820 /* rr3 re-enables rc detector because it was enabled before */
2154be65
JW
821
822 return ret;
823}
824
bf139726
SY
825static void redrat3_brightness_set(struct led_classdev *led_dev, enum
826 led_brightness brightness)
827{
828 struct redrat3_dev *rr3 = container_of(led_dev, struct redrat3_dev,
829 led);
830
831 if (brightness != LED_OFF && atomic_cmpxchg(&rr3->flash, 0, 1) == 0) {
832 int ret = usb_submit_urb(rr3->flash_urb, GFP_ATOMIC);
833 if (ret != 0) {
834 dev_dbg(rr3->dev, "%s: unexpected ret of %d\n",
835 __func__, ret);
836 atomic_set(&rr3->flash, 0);
837 }
838 }
839}
840
841static void redrat3_led_complete(struct urb *urb)
842{
843 struct redrat3_dev *rr3 = urb->context;
844
845 switch (urb->status) {
846 case 0:
847 break;
848 case -ECONNRESET:
849 case -ENOENT:
850 case -ESHUTDOWN:
851 usb_unlink_urb(urb);
852 return;
853 case -EPIPE:
854 default:
855 dev_dbg(rr3->dev, "Error: urb status = %d\n", urb->status);
856 break;
857 }
858
859 rr3->led.brightness = LED_OFF;
860 atomic_dec(&rr3->flash);
861}
862
2154be65
JW
863static struct rc_dev *redrat3_init_rc_dev(struct redrat3_dev *rr3)
864{
865 struct device *dev = rr3->dev;
866 struct rc_dev *rc;
867 int ret = -ENODEV;
868 u16 prod = le16_to_cpu(rr3->udev->descriptor.idProduct);
869
870 rc = rc_allocate_device();
871 if (!rc) {
872 dev_err(dev, "remote input dev allocation failed\n");
873 goto out;
874 }
875
876 snprintf(rr3->name, sizeof(rr3->name), "RedRat3%s "
877 "Infrared Remote Transceiver (%04x:%04x)",
878 prod == USB_RR3IIUSB_PRODUCT_ID ? "-II" : "",
879 le16_to_cpu(rr3->udev->descriptor.idVendor), prod);
880
881 usb_make_path(rr3->udev, rr3->phys, sizeof(rr3->phys));
882
883 rc->input_name = rr3->name;
884 rc->input_phys = rr3->phys;
885 usb_to_input_id(rr3->udev, &rc->input_id);
886 rc->dev.parent = dev;
887 rc->priv = rr3;
888 rc->driver_type = RC_DRIVER_IR_RAW;
c5540fbb 889 rc->allowed_protocols = RC_BIT_ALL;
4f253cec
SY
890 rc->min_timeout = MS_TO_NS(RR3_RX_MIN_TIMEOUT);
891 rc->max_timeout = MS_TO_NS(RR3_RX_MAX_TIMEOUT);
25da661a 892 rc->timeout = US_TO_NS(rr3->hw_timeout);
4f253cec 893 rc->s_timeout = redrat3_set_timeout;
2154be65
JW
894 rc->tx_ir = redrat3_transmit_ir;
895 rc->s_tx_carrier = redrat3_set_tx_carrier;
896 rc->driver_name = DRIVER_NAME;
06eae25f 897 rc->rx_resolution = US_TO_NS(2);
2154be65
JW
898 rc->map_name = RC_MAP_HAUPPAUGE;
899
900 ret = rc_register_device(rc);
901 if (ret < 0) {
902 dev_err(dev, "remote dev registration failed\n");
903 goto out;
904 }
905
906 return rc;
907
908out:
909 rc_free_device(rc);
910 return NULL;
911}
912
4c62e976
GKH
913static int redrat3_dev_probe(struct usb_interface *intf,
914 const struct usb_device_id *id)
2154be65
JW
915{
916 struct usb_device *udev = interface_to_usbdev(intf);
917 struct device *dev = &intf->dev;
918 struct usb_host_interface *uhi;
919 struct redrat3_dev *rr3;
920 struct usb_endpoint_descriptor *ep;
921 struct usb_endpoint_descriptor *ep_in = NULL;
922 struct usb_endpoint_descriptor *ep_out = NULL;
923 u8 addr, attrs;
924 int pipe, i;
925 int retval = -ENOMEM;
926
2154be65
JW
927 uhi = intf->cur_altsetting;
928
929 /* find our bulk-in and bulk-out endpoints */
930 for (i = 0; i < uhi->desc.bNumEndpoints; ++i) {
931 ep = &uhi->endpoint[i].desc;
932 addr = ep->bEndpointAddress;
933 attrs = ep->bmAttributes;
934
935 if ((ep_in == NULL) &&
936 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
937 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
938 USB_ENDPOINT_XFER_BULK)) {
3228bf81 939 dev_dbg(dev, "found bulk-in endpoint at 0x%02x\n",
2154be65
JW
940 ep->bEndpointAddress);
941 /* data comes in on 0x82, 0x81 is for other data... */
942 if (ep->bEndpointAddress == RR3_BULK_IN_EP_ADDR)
943 ep_in = ep;
944 }
945
946 if ((ep_out == NULL) &&
947 ((addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
948 ((attrs & USB_ENDPOINT_XFERTYPE_MASK) ==
949 USB_ENDPOINT_XFER_BULK)) {
3228bf81 950 dev_dbg(dev, "found bulk-out endpoint at 0x%02x\n",
2154be65
JW
951 ep->bEndpointAddress);
952 ep_out = ep;
953 }
954 }
955
956 if (!ep_in || !ep_out) {
957 dev_err(dev, "Couldn't find both in and out endpoints\n");
958 retval = -ENODEV;
959 goto no_endpoints;
960 }
961
962 /* allocate memory for our device state and initialize it */
963 rr3 = kzalloc(sizeof(*rr3), GFP_KERNEL);
964 if (rr3 == NULL) {
965 dev_err(dev, "Memory allocation failure\n");
7eb75715 966 goto no_endpoints;
2154be65
JW
967 }
968
969 rr3->dev = &intf->dev;
970
971 /* set up bulk-in endpoint */
972 rr3->read_urb = usb_alloc_urb(0, GFP_KERNEL);
cd859c19 973 if (!rr3->read_urb)
2154be65 974 goto error;
2154be65
JW
975
976 rr3->ep_in = ep_in;
fa7b9ac2 977 rr3->bulk_in_buf = usb_alloc_coherent(udev,
d0a0a65e 978 le16_to_cpu(ep_in->wMaxPacketSize), GFP_KERNEL, &rr3->dma_in);
2154be65
JW
979 if (!rr3->bulk_in_buf) {
980 dev_err(dev, "Read buffer allocation failure\n");
981 goto error;
982 }
983
984 pipe = usb_rcvbulkpipe(udev, ep_in->bEndpointAddress);
fa7b9ac2
SY
985 usb_fill_bulk_urb(rr3->read_urb, udev, pipe, rr3->bulk_in_buf,
986 le16_to_cpu(ep_in->wMaxPacketSize), redrat3_handle_async, rr3);
d0a0a65e
SY
987 rr3->read_urb->transfer_dma = rr3->dma_in;
988 rr3->read_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2154be65
JW
989
990 rr3->ep_out = ep_out;
2154be65
JW
991 rr3->udev = udev;
992
993 redrat3_reset(rr3);
994 redrat3_get_firmware_rev(rr3);
995
996 /* might be all we need to do? */
997 retval = redrat3_enable_detector(rr3);
998 if (retval < 0)
999 goto error;
1000
25da661a 1001 /* store current hardware timeout, in µs */
c53f9f00
JW
1002 rr3->hw_timeout = redrat3_get_timeout(rr3);
1003
2154be65
JW
1004 /* default.. will get overridden by any sends with a freq defined */
1005 rr3->carrier = 38000;
1006
bf139726
SY
1007 /* led control */
1008 rr3->led.name = "redrat3:red:feedback";
1009 rr3->led.default_trigger = "rc-feedback";
1010 rr3->led.brightness_set = redrat3_brightness_set;
1011 retval = led_classdev_register(&intf->dev, &rr3->led);
1012 if (retval)
1013 goto error;
1014
1015 atomic_set(&rr3->flash, 0);
1016 rr3->flash_urb = usb_alloc_urb(0, GFP_KERNEL);
1017 if (!rr3->flash_urb) {
1018 retval = -ENOMEM;
1019 goto led_free_error;
1020 }
1021
1022 /* setup packet is 'c0 b9 0000 0000 0001' */
1023 rr3->flash_control.bRequestType = 0xc0;
1024 rr3->flash_control.bRequest = RR3_BLINK_LED;
1025 rr3->flash_control.wLength = cpu_to_le16(1);
1026
1027 usb_fill_control_urb(rr3->flash_urb, udev, usb_rcvctrlpipe(udev, 0),
1028 (unsigned char *)&rr3->flash_control,
1029 &rr3->flash_in_buf, sizeof(rr3->flash_in_buf),
1030 redrat3_led_complete, rr3);
1031
2154be65 1032 rr3->rc = redrat3_init_rc_dev(rr3);
6ea7cf76
PST
1033 if (!rr3->rc) {
1034 retval = -ENOMEM;
bf139726 1035 goto led_free_error;
6ea7cf76 1036 }
2154be65
JW
1037
1038 /* we can register the device now, as it is ready */
1039 usb_set_intfdata(intf, rr3);
1040
2154be65
JW
1041 return 0;
1042
bf139726
SY
1043led_free_error:
1044 led_classdev_unregister(&rr3->led);
2154be65
JW
1045error:
1046 redrat3_delete(rr3, rr3->udev);
1047
1048no_endpoints:
1049 dev_err(dev, "%s: retval = %x", __func__, retval);
1050
1051 return retval;
1052}
1053
4c62e976 1054static void redrat3_dev_disconnect(struct usb_interface *intf)
2154be65
JW
1055{
1056 struct usb_device *udev = interface_to_usbdev(intf);
1057 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
1058
2154be65
JW
1059 if (!rr3)
1060 return;
1061
2154be65
JW
1062 usb_set_intfdata(intf, NULL);
1063 rc_unregister_device(rr3->rc);
bf139726 1064 led_classdev_unregister(&rr3->led);
2154be65 1065 redrat3_delete(rr3, udev);
2154be65
JW
1066}
1067
1068static int redrat3_dev_suspend(struct usb_interface *intf, pm_message_t message)
1069{
1070 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
a36d5b61 1071
bf139726 1072 led_classdev_suspend(&rr3->led);
2154be65 1073 usb_kill_urb(rr3->read_urb);
bf139726 1074 usb_kill_urb(rr3->flash_urb);
2154be65
JW
1075 return 0;
1076}
1077
1078static int redrat3_dev_resume(struct usb_interface *intf)
1079{
1080 struct redrat3_dev *rr3 = usb_get_intfdata(intf);
a36d5b61 1081
2154be65
JW
1082 if (usb_submit_urb(rr3->read_urb, GFP_ATOMIC))
1083 return -EIO;
bf139726 1084 led_classdev_resume(&rr3->led);
2154be65
JW
1085 return 0;
1086}
1087
1088static struct usb_driver redrat3_dev_driver = {
1089 .name = DRIVER_NAME,
1090 .probe = redrat3_dev_probe,
4c62e976 1091 .disconnect = redrat3_dev_disconnect,
2154be65
JW
1092 .suspend = redrat3_dev_suspend,
1093 .resume = redrat3_dev_resume,
1094 .reset_resume = redrat3_dev_resume,
1095 .id_table = redrat3_dev_table
1096};
1097
ecb3b2b3 1098module_usb_driver(redrat3_dev_driver);
2154be65
JW
1099
1100MODULE_DESCRIPTION(DRIVER_DESC);
1101MODULE_AUTHOR(DRIVER_AUTHOR);
1102MODULE_AUTHOR(DRIVER_AUTHOR2);
1103MODULE_LICENSE("GPL");
1104MODULE_DEVICE_TABLE(usb, redrat3_dev_table);
This page took 0.659329 seconds and 5 git commands to generate.