blob: f1c5846b35f4e107b2c3beaa9a4cacc23226bf46 [file] [log] [blame]
Sean Young26ff6312012-07-15 13:31:00 -03001/*
2 * IguanaWorks USB IR Transceiver support
3 *
4 * Copyright (C) 2012 Sean Young <sean@mess.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/device.h>
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/usb.h>
25#include <linux/usb/input.h>
26#include <linux/slab.h>
27#include <linux/completion.h>
28#include <media/rc-core.h>
29
30#define DRIVER_NAME "iguanair"
31
32struct iguanair {
33 struct rc_dev *rc;
34
35 struct device *dev;
36 struct usb_device *udev;
37
Sean Younge99a7cf2012-08-13 08:59:39 -030038 int pipe_out;
Sean Young0797b482012-08-13 08:59:40 -030039 uint16_t version;
Sean Young26ff6312012-07-15 13:31:00 -030040 uint8_t bufsize;
Sean Young26ff6312012-07-15 13:31:00 -030041
42 struct mutex lock;
43
44 /* receiver support */
45 bool receiver_on;
46 dma_addr_t dma_in;
47 uint8_t *buf_in;
48 struct urb *urb_in;
49 struct completion completion;
50
51 /* transmit support */
52 bool tx_overflow;
53 uint32_t carrier;
54 uint8_t cycle_overhead;
55 uint8_t channels;
56 uint8_t busy4;
57 uint8_t busy7;
58
59 char name[64];
60 char phys[64];
61};
62
63#define CMD_GET_VERSION 0x01
64#define CMD_GET_BUFSIZE 0x11
65#define CMD_GET_FEATURES 0x10
66#define CMD_SEND 0x15
67#define CMD_EXECUTE 0x1f
68#define CMD_RX_OVERFLOW 0x31
69#define CMD_TX_OVERFLOW 0x32
70#define CMD_RECEIVER_ON 0x12
71#define CMD_RECEIVER_OFF 0x14
72
73#define DIR_IN 0xdc
74#define DIR_OUT 0xcd
75
76#define MAX_PACKET_SIZE 8u
77#define TIMEOUT 1000
78
79struct packet {
80 uint16_t start;
81 uint8_t direction;
82 uint8_t cmd;
83};
84
Sean Young26ff6312012-07-15 13:31:00 -030085struct send_packet {
86 struct packet header;
87 uint8_t length;
88 uint8_t channels;
89 uint8_t busy7;
90 uint8_t busy4;
91 uint8_t payload[0];
92};
93
94static void process_ir_data(struct iguanair *ir, unsigned len)
95{
96 if (len >= 4 && ir->buf_in[0] == 0 && ir->buf_in[1] == 0) {
97 switch (ir->buf_in[3]) {
Sean Younge99a7cf2012-08-13 08:59:39 -030098 case CMD_GET_VERSION:
99 if (len == 6) {
Sean Young0797b482012-08-13 08:59:40 -0300100 ir->version = (ir->buf_in[5] << 8) |
101 ir->buf_in[4];
Sean Younge99a7cf2012-08-13 08:59:39 -0300102 complete(&ir->completion);
103 }
104 break;
105 case CMD_GET_BUFSIZE:
106 if (len >= 5) {
107 ir->bufsize = ir->buf_in[4];
108 complete(&ir->completion);
109 }
110 break;
111 case CMD_GET_FEATURES:
112 if (len > 5) {
Sean Young0797b482012-08-13 08:59:40 -0300113 ir->cycle_overhead = ir->buf_in[5];
Sean Younge99a7cf2012-08-13 08:59:39 -0300114 complete(&ir->completion);
115 }
116 break;
Sean Young26ff6312012-07-15 13:31:00 -0300117 case CMD_TX_OVERFLOW:
118 ir->tx_overflow = true;
119 case CMD_RECEIVER_OFF:
120 case CMD_RECEIVER_ON:
121 case CMD_SEND:
122 complete(&ir->completion);
123 break;
124 case CMD_RX_OVERFLOW:
125 dev_warn(ir->dev, "receive overflow\n");
126 break;
127 default:
128 dev_warn(ir->dev, "control code %02x received\n",
129 ir->buf_in[3]);
130 break;
131 }
132 } else if (len >= 7) {
133 DEFINE_IR_RAW_EVENT(rawir);
134 unsigned i;
135
136 init_ir_raw_event(&rawir);
137
138 for (i = 0; i < 7; i++) {
139 if (ir->buf_in[i] == 0x80) {
140 rawir.pulse = false;
141 rawir.duration = US_TO_NS(21845);
142 } else {
143 rawir.pulse = (ir->buf_in[i] & 0x80) == 0;
144 rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) *
145 21330;
146 }
147
148 ir_raw_event_store_with_filter(ir->rc, &rawir);
149 }
150
151 ir_raw_event_handle(ir->rc);
152 }
153}
154
155static void iguanair_rx(struct urb *urb)
156{
157 struct iguanair *ir;
Sean Young7c0bd962012-08-13 08:59:43 -0300158 int rc;
Sean Young26ff6312012-07-15 13:31:00 -0300159
160 if (!urb)
161 return;
162
163 ir = urb->context;
164 if (!ir) {
165 usb_unlink_urb(urb);
166 return;
167 }
168
169 switch (urb->status) {
170 case 0:
171 process_ir_data(ir, urb->actual_length);
172 break;
173 case -ECONNRESET:
174 case -ENOENT:
175 case -ESHUTDOWN:
176 usb_unlink_urb(urb);
177 return;
178 case -EPIPE:
179 default:
180 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
181 break;
182 }
183
Sean Young7c0bd962012-08-13 08:59:43 -0300184 rc = usb_submit_urb(urb, GFP_ATOMIC);
185 if (rc && rc != -ENODEV)
186 dev_warn(ir->dev, "failed to resubmit urb: %d\n", rc);
Sean Young26ff6312012-07-15 13:31:00 -0300187}
188
Sean Younge99a7cf2012-08-13 08:59:39 -0300189static int iguanair_send(struct iguanair *ir, void *data, unsigned size)
Sean Young26ff6312012-07-15 13:31:00 -0300190{
Sean Young26ff6312012-07-15 13:31:00 -0300191 int rc, transferred;
192
Sean Younge99a7cf2012-08-13 08:59:39 -0300193 INIT_COMPLETION(ir->completion);
Sean Young26ff6312012-07-15 13:31:00 -0300194
Sean Younge99a7cf2012-08-13 08:59:39 -0300195 rc = usb_interrupt_msg(ir->udev, ir->pipe_out, data, size,
196 &transferred, TIMEOUT);
197 if (rc)
198 return rc;
Sean Young26ff6312012-07-15 13:31:00 -0300199
Sean Younge99a7cf2012-08-13 08:59:39 -0300200 if (transferred != size)
201 return -EIO;
Sean Young26ff6312012-07-15 13:31:00 -0300202
Sean Younge99a7cf2012-08-13 08:59:39 -0300203 if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0)
204 return -ETIMEDOUT;
Sean Young26ff6312012-07-15 13:31:00 -0300205
206 return rc;
207}
208
209static int iguanair_get_features(struct iguanair *ir)
210{
211 struct packet packet;
Sean Younge99a7cf2012-08-13 08:59:39 -0300212 int rc;
Sean Young26ff6312012-07-15 13:31:00 -0300213
214 packet.start = 0;
215 packet.direction = DIR_OUT;
216 packet.cmd = CMD_GET_VERSION;
217
Sean Younge99a7cf2012-08-13 08:59:39 -0300218 rc = iguanair_send(ir, &packet, sizeof(packet));
Sean Young26ff6312012-07-15 13:31:00 -0300219 if (rc) {
220 dev_info(ir->dev, "failed to get version\n");
221 goto out;
222 }
223
Sean Young0797b482012-08-13 08:59:40 -0300224 if (ir->version < 0x205) {
225 dev_err(ir->dev, "firmware 0x%04x is too old\n", ir->version);
226 rc = -ENODEV;
227 goto out;
228 }
229
Sean Young26ff6312012-07-15 13:31:00 -0300230 ir->bufsize = 150;
231 ir->cycle_overhead = 65;
232
233 packet.cmd = CMD_GET_BUFSIZE;
234
Sean Younge99a7cf2012-08-13 08:59:39 -0300235 rc = iguanair_send(ir, &packet, sizeof(packet));
Sean Young26ff6312012-07-15 13:31:00 -0300236 if (rc) {
237 dev_info(ir->dev, "failed to get buffer size\n");
238 goto out;
239 }
240
Sean Young26ff6312012-07-15 13:31:00 -0300241 packet.cmd = CMD_GET_FEATURES;
242
Sean Younge99a7cf2012-08-13 08:59:39 -0300243 rc = iguanair_send(ir, &packet, sizeof(packet));
Sean Young26ff6312012-07-15 13:31:00 -0300244 if (rc) {
245 dev_info(ir->dev, "failed to get features\n");
246 goto out;
247 }
248
Sean Young26ff6312012-07-15 13:31:00 -0300249out:
250 return rc;
251}
252
253static int iguanair_receiver(struct iguanair *ir, bool enable)
254{
255 struct packet packet = { 0, DIR_OUT, enable ?
256 CMD_RECEIVER_ON : CMD_RECEIVER_OFF };
Sean Young26ff6312012-07-15 13:31:00 -0300257
Sean Younge99a7cf2012-08-13 08:59:39 -0300258 return iguanair_send(ir, &packet, sizeof(packet));
Sean Young26ff6312012-07-15 13:31:00 -0300259}
260
261/*
262 * The iguana ir creates the carrier by busy spinning after each pulse or
263 * space. This is counted in CPU cycles, with the CPU running at 24MHz. It is
264 * broken down into 7-cycles and 4-cyles delays, with a preference for
265 * 4-cycle delays.
266 */
267static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier)
268{
269 struct iguanair *ir = dev->priv;
270
271 if (carrier < 25000 || carrier > 150000)
272 return -EINVAL;
273
274 mutex_lock(&ir->lock);
275
276 if (carrier != ir->carrier) {
277 uint32_t cycles, fours, sevens;
278
279 ir->carrier = carrier;
280
281 cycles = DIV_ROUND_CLOSEST(24000000, carrier * 2) -
282 ir->cycle_overhead;
283
284 /* make up the the remainer of 4-cycle blocks */
285 switch (cycles & 3) {
286 case 0:
287 sevens = 0;
288 break;
289 case 1:
290 sevens = 3;
291 break;
292 case 2:
293 sevens = 2;
294 break;
295 case 3:
296 sevens = 1;
297 break;
298 }
299
300 fours = (cycles - sevens * 7) / 4;
301
302 /* magic happens here */
303 ir->busy7 = (4 - sevens) * 2;
304 ir->busy4 = 110 - fours;
305 }
306
307 mutex_unlock(&ir->lock);
308
309 return carrier;
310}
311
312static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask)
313{
314 struct iguanair *ir = dev->priv;
315
316 if (mask > 15)
317 return 4;
318
319 mutex_lock(&ir->lock);
320 ir->channels = mask;
321 mutex_unlock(&ir->lock);
322
323 return 0;
324}
325
326static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count)
327{
328 struct iguanair *ir = dev->priv;
329 uint8_t space, *payload;
330 unsigned i, size, rc;
331 struct send_packet *packet;
332
333 mutex_lock(&ir->lock);
334
335 /* convert from us to carrier periods */
336 for (i = size = 0; i < count; i++) {
337 txbuf[i] = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000);
338 size += (txbuf[i] + 126) / 127;
339 }
340
341 packet = kmalloc(sizeof(*packet) + size, GFP_KERNEL);
342 if (!packet) {
343 rc = -ENOMEM;
344 goto out;
345 }
346
347 if (size > ir->bufsize) {
348 rc = -E2BIG;
349 goto out;
350 }
351
352 packet->header.start = 0;
353 packet->header.direction = DIR_OUT;
354 packet->header.cmd = CMD_SEND;
355 packet->length = size;
356 packet->channels = ir->channels << 4;
357 packet->busy7 = ir->busy7;
358 packet->busy4 = ir->busy4;
359
360 space = 0;
361 payload = packet->payload;
362
363 for (i = 0; i < count; i++) {
364 unsigned periods = txbuf[i];
365
366 while (periods > 127) {
367 *payload++ = 127 | space;
368 periods -= 127;
369 }
370
371 *payload++ = periods | space;
372 space ^= 0x80;
373 }
374
375 if (ir->receiver_on) {
376 rc = iguanair_receiver(ir, false);
377 if (rc) {
378 dev_warn(ir->dev, "disable receiver before transmit failed\n");
379 goto out;
380 }
381 }
382
383 ir->tx_overflow = false;
384
Sean Younge99a7cf2012-08-13 08:59:39 -0300385 rc = iguanair_send(ir, packet, size + 8);
Sean Young26ff6312012-07-15 13:31:00 -0300386
Sean Younge99a7cf2012-08-13 08:59:39 -0300387 if (rc == 0 && ir->tx_overflow)
388 rc = -EOVERFLOW;
Sean Young26ff6312012-07-15 13:31:00 -0300389
390 if (ir->receiver_on) {
391 if (iguanair_receiver(ir, true))
392 dev_warn(ir->dev, "re-enable receiver after transmit failed\n");
393 }
394
395out:
396 mutex_unlock(&ir->lock);
397 kfree(packet);
398
399 return rc;
400}
401
402static int iguanair_open(struct rc_dev *rdev)
403{
404 struct iguanair *ir = rdev->priv;
405 int rc;
406
407 mutex_lock(&ir->lock);
408
Sean Young26ff6312012-07-15 13:31:00 -0300409 BUG_ON(ir->receiver_on);
410
411 rc = iguanair_receiver(ir, true);
412 if (rc == 0)
413 ir->receiver_on = true;
414
415 mutex_unlock(&ir->lock);
416
417 return rc;
418}
419
420static void iguanair_close(struct rc_dev *rdev)
421{
422 struct iguanair *ir = rdev->priv;
423 int rc;
424
425 mutex_lock(&ir->lock);
426
427 rc = iguanair_receiver(ir, false);
428 ir->receiver_on = false;
Sean Young7c0bd962012-08-13 08:59:43 -0300429 if (rc && rc != -ENODEV)
Sean Young26ff6312012-07-15 13:31:00 -0300430 dev_warn(ir->dev, "failed to disable receiver: %d\n", rc);
431
Sean Young26ff6312012-07-15 13:31:00 -0300432 mutex_unlock(&ir->lock);
433}
434
435static int __devinit iguanair_probe(struct usb_interface *intf,
436 const struct usb_device_id *id)
437{
438 struct usb_device *udev = interface_to_usbdev(intf);
439 struct iguanair *ir;
440 struct rc_dev *rc;
Sean Younge99a7cf2012-08-13 08:59:39 -0300441 int ret, pipein;
Sean Young26ff6312012-07-15 13:31:00 -0300442 struct usb_host_interface *idesc;
443
444 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
445 rc = rc_allocate_device();
446 if (!ir || !rc) {
447 ret = ENOMEM;
448 goto out;
449 }
450
Sean Younge99a7cf2012-08-13 08:59:39 -0300451 ir->buf_in = usb_alloc_coherent(udev, MAX_PACKET_SIZE, GFP_KERNEL,
Sean Young26ff6312012-07-15 13:31:00 -0300452 &ir->dma_in);
453 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
454
455 if (!ir->buf_in || !ir->urb_in) {
456 ret = ENOMEM;
457 goto out;
458 }
459
460 idesc = intf->altsetting;
461
462 if (idesc->desc.bNumEndpoints < 2) {
463 ret = -ENODEV;
464 goto out;
465 }
466
467 ir->rc = rc;
468 ir->dev = &intf->dev;
469 ir->udev = udev;
Sean Young26ff6312012-07-15 13:31:00 -0300470 ir->pipe_out = usb_sndintpipe(udev,
471 idesc->endpoint[1].desc.bEndpointAddress);
472 mutex_init(&ir->lock);
473 init_completion(&ir->completion);
474
Sean Younge99a7cf2012-08-13 08:59:39 -0300475 pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress);
476 usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in,
Sean Young26ff6312012-07-15 13:31:00 -0300477 MAX_PACKET_SIZE, iguanair_rx, ir,
478 idesc->endpoint[0].desc.bInterval);
479 ir->urb_in->transfer_dma = ir->dma_in;
480 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
481
Sean Younge99a7cf2012-08-13 08:59:39 -0300482 ret = usb_submit_urb(ir->urb_in, GFP_KERNEL);
483 if (ret) {
484 dev_warn(&intf->dev, "failed to submit urb: %d\n", ret);
485 goto out;
486 }
487
488 ret = iguanair_get_features(ir);
489 if (ret)
490 goto out2;
491
Sean Young26ff6312012-07-15 13:31:00 -0300492 snprintf(ir->name, sizeof(ir->name),
Sean Young0797b482012-08-13 08:59:40 -0300493 "IguanaWorks USB IR Transceiver version 0x%04x", ir->version);
Sean Young26ff6312012-07-15 13:31:00 -0300494
495 usb_make_path(ir->udev, ir->phys, sizeof(ir->phys));
496
497 rc->input_name = ir->name;
498 rc->input_phys = ir->phys;
499 usb_to_input_id(ir->udev, &rc->input_id);
500 rc->dev.parent = &intf->dev;
501 rc->driver_type = RC_DRIVER_IR_RAW;
502 rc->allowed_protos = RC_TYPE_ALL;
503 rc->priv = ir;
504 rc->open = iguanair_open;
505 rc->close = iguanair_close;
506 rc->s_tx_mask = iguanair_set_tx_mask;
507 rc->s_tx_carrier = iguanair_set_tx_carrier;
508 rc->tx_ir = iguanair_tx;
509 rc->driver_name = DRIVER_NAME;
510 rc->map_name = RC_MAP_EMPTY;
511
512 iguanair_set_tx_carrier(rc, 38000);
513
514 ret = rc_register_device(rc);
515 if (ret < 0) {
516 dev_err(&intf->dev, "failed to register rc device %d", ret);
Sean Younge99a7cf2012-08-13 08:59:39 -0300517 goto out2;
Sean Young26ff6312012-07-15 13:31:00 -0300518 }
519
520 usb_set_intfdata(intf, ir);
521
Sean Young26ff6312012-07-15 13:31:00 -0300522 return 0;
Sean Younge99a7cf2012-08-13 08:59:39 -0300523out2:
524 usb_kill_urb(ir->urb_in);
Sean Young26ff6312012-07-15 13:31:00 -0300525out:
526 if (ir) {
527 usb_free_urb(ir->urb_in);
528 usb_free_coherent(udev, MAX_PACKET_SIZE, ir->buf_in,
529 ir->dma_in);
530 }
531 rc_free_device(rc);
532 kfree(ir);
533 return ret;
534}
535
536static void __devexit iguanair_disconnect(struct usb_interface *intf)
537{
538 struct iguanair *ir = usb_get_intfdata(intf);
539
Sean Young7c0bd962012-08-13 08:59:43 -0300540 rc_unregister_device(ir->rc);
Sean Young26ff6312012-07-15 13:31:00 -0300541 usb_set_intfdata(intf, NULL);
Sean Young26ff6312012-07-15 13:31:00 -0300542 usb_kill_urb(ir->urb_in);
543 usb_free_urb(ir->urb_in);
544 usb_free_coherent(ir->udev, MAX_PACKET_SIZE, ir->buf_in, ir->dma_in);
Sean Young26ff6312012-07-15 13:31:00 -0300545 kfree(ir);
546}
547
548static int iguanair_suspend(struct usb_interface *intf, pm_message_t message)
549{
550 struct iguanair *ir = usb_get_intfdata(intf);
551 int rc = 0;
552
553 mutex_lock(&ir->lock);
554
555 if (ir->receiver_on) {
556 rc = iguanair_receiver(ir, false);
557 if (rc)
558 dev_warn(ir->dev, "failed to disable receiver for suspend\n");
559 }
560
Sean Young7c0bd962012-08-13 08:59:43 -0300561 usb_kill_urb(ir->urb_in);
562
Sean Young26ff6312012-07-15 13:31:00 -0300563 mutex_unlock(&ir->lock);
564
565 return rc;
566}
567
568static int iguanair_resume(struct usb_interface *intf)
569{
570 struct iguanair *ir = usb_get_intfdata(intf);
571 int rc = 0;
572
573 mutex_lock(&ir->lock);
574
Sean Young7c0bd962012-08-13 08:59:43 -0300575 rc = usb_submit_urb(ir->urb_in, GFP_KERNEL);
576 if (rc)
577 dev_warn(&intf->dev, "failed to submit urb: %d\n", rc);
578
Sean Young26ff6312012-07-15 13:31:00 -0300579 if (ir->receiver_on) {
580 rc = iguanair_receiver(ir, true);
581 if (rc)
582 dev_warn(ir->dev, "failed to enable receiver after resume\n");
583 }
584
585 mutex_unlock(&ir->lock);
586
587 return rc;
588}
589
590static const struct usb_device_id iguanair_table[] = {
591 { USB_DEVICE(0x1781, 0x0938) },
592 { }
593};
594
595static struct usb_driver iguanair_driver = {
596 .name = DRIVER_NAME,
597 .probe = iguanair_probe,
598 .disconnect = __devexit_p(iguanair_disconnect),
599 .suspend = iguanair_suspend,
600 .resume = iguanair_resume,
601 .reset_resume = iguanair_resume,
Sean Young7c0bd962012-08-13 08:59:43 -0300602 .id_table = iguanair_table,
603 .soft_unbind = 1 /* we want to disable receiver on unbind */
Sean Young26ff6312012-07-15 13:31:00 -0300604};
605
606module_usb_driver(iguanair_driver);
607
608MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
609MODULE_AUTHOR("Sean Young <sean@mess.org>");
610MODULE_LICENSE("GPL");
611MODULE_DEVICE_TABLE(usb, iguanair_table);
612