2 * IguanaWorks USB IR Transceiver support
4 * Copyright (C) 2012 Sean Young <sean@mess.org>
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.
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.
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
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>
30 #define DRIVER_NAME "iguanair"
36 struct usb_device *udev;
44 /* receiver support */
49 struct completion completion;
51 /* transmit support */
54 uint8_t cycle_overhead;
63 #define CMD_GET_VERSION 0x01
64 #define CMD_GET_BUFSIZE 0x11
65 #define CMD_GET_FEATURES 0x10
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
76 #define MAX_PACKET_SIZE 8u
78 #define RX_RESOLUTION 21333
95 static void process_ir_data(struct iguanair *ir, unsigned len)
97 if (len >= 4 && ir->buf_in[0] == 0 && ir->buf_in[1] == 0) {
98 switch (ir->buf_in[3]) {
101 ir->version = (ir->buf_in[5] << 8) |
103 complete(&ir->completion);
106 case CMD_GET_BUFSIZE:
108 ir->bufsize = ir->buf_in[4];
109 complete(&ir->completion);
112 case CMD_GET_FEATURES:
114 ir->cycle_overhead = ir->buf_in[5];
115 complete(&ir->completion);
118 case CMD_TX_OVERFLOW:
119 ir->tx_overflow = true;
120 case CMD_RECEIVER_OFF:
121 case CMD_RECEIVER_ON:
123 complete(&ir->completion);
125 case CMD_RX_OVERFLOW:
126 dev_warn(ir->dev, "receive overflow\n");
127 ir_raw_event_reset(ir->rc);
130 dev_warn(ir->dev, "control code %02x received\n",
134 } else if (len >= 7) {
135 DEFINE_IR_RAW_EVENT(rawir);
139 init_ir_raw_event(&rawir);
141 for (i = 0; i < 7; i++) {
142 if (ir->buf_in[i] == 0x80) {
144 rawir.duration = US_TO_NS(21845);
146 rawir.pulse = (ir->buf_in[i] & 0x80) == 0;
147 rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) *
151 if (ir_raw_event_store_with_filter(ir->rc, &rawir))
156 ir_raw_event_handle(ir->rc);
160 static void iguanair_rx(struct urb *urb)
174 switch (urb->status) {
176 process_ir_data(ir, urb->actual_length);
185 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
189 rc = usb_submit_urb(urb, GFP_ATOMIC);
190 if (rc && rc != -ENODEV)
191 dev_warn(ir->dev, "failed to resubmit urb: %d\n", rc);
194 static int iguanair_send(struct iguanair *ir, void *data, unsigned size)
198 INIT_COMPLETION(ir->completion);
200 rc = usb_interrupt_msg(ir->udev, ir->pipe_out, data, size,
201 &transferred, TIMEOUT);
205 if (transferred != size)
208 if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0)
214 static int iguanair_get_features(struct iguanair *ir)
216 struct packet packet;
220 packet.direction = DIR_OUT;
221 packet.cmd = CMD_GET_VERSION;
223 rc = iguanair_send(ir, &packet, sizeof(packet));
225 dev_info(ir->dev, "failed to get version\n");
229 if (ir->version < 0x205) {
230 dev_err(ir->dev, "firmware 0x%04x is too old\n", ir->version);
236 ir->cycle_overhead = 65;
238 packet.cmd = CMD_GET_BUFSIZE;
240 rc = iguanair_send(ir, &packet, sizeof(packet));
242 dev_info(ir->dev, "failed to get buffer size\n");
246 packet.cmd = CMD_GET_FEATURES;
248 rc = iguanair_send(ir, &packet, sizeof(packet));
250 dev_info(ir->dev, "failed to get features\n");
258 static int iguanair_receiver(struct iguanair *ir, bool enable)
260 struct packet packet = { 0, DIR_OUT, enable ?
261 CMD_RECEIVER_ON : CMD_RECEIVER_OFF };
264 ir_raw_event_reset(ir->rc);
266 return iguanair_send(ir, &packet, sizeof(packet));
270 * The iguana ir creates the carrier by busy spinning after each pulse or
271 * space. This is counted in CPU cycles, with the CPU running at 24MHz. It is
272 * broken down into 7-cycles and 4-cyles delays, with a preference for
275 static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier)
277 struct iguanair *ir = dev->priv;
279 if (carrier < 25000 || carrier > 150000)
282 mutex_lock(&ir->lock);
284 if (carrier != ir->carrier) {
285 uint32_t cycles, fours, sevens;
287 ir->carrier = carrier;
289 cycles = DIV_ROUND_CLOSEST(24000000, carrier * 2) -
292 /* make up the the remainer of 4-cycle blocks */
293 switch (cycles & 3) {
308 fours = (cycles - sevens * 7) / 4;
310 /* magic happens here */
311 ir->busy7 = (4 - sevens) * 2;
312 ir->busy4 = 110 - fours;
315 mutex_unlock(&ir->lock);
320 static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask)
322 struct iguanair *ir = dev->priv;
327 mutex_lock(&ir->lock);
329 mutex_unlock(&ir->lock);
334 static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count)
336 struct iguanair *ir = dev->priv;
338 unsigned i, size, periods, bytes;
340 struct send_packet *packet;
342 mutex_lock(&ir->lock);
344 packet = kmalloc(sizeof(*packet) + ir->bufsize, GFP_KERNEL);
350 /* convert from us to carrier periods */
351 for (i = space = size = 0; i < count; i++) {
352 periods = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000);
353 bytes = DIV_ROUND_UP(periods, 127);
354 if (size + bytes > ir->bufsize) {
358 while (periods > 127) {
359 packet->payload[size++] = 127 | space;
363 packet->payload[size++] = periods | space;
372 packet->header.start = 0;
373 packet->header.direction = DIR_OUT;
374 packet->header.cmd = CMD_SEND;
375 packet->length = size;
376 packet->channels = ir->channels << 4;
377 packet->busy7 = ir->busy7;
378 packet->busy4 = ir->busy4;
380 if (ir->receiver_on) {
381 rc = iguanair_receiver(ir, false);
383 dev_warn(ir->dev, "disable receiver before transmit failed\n");
388 ir->tx_overflow = false;
390 rc = iguanair_send(ir, packet, size + 8);
392 if (rc == 0 && ir->tx_overflow)
395 if (ir->receiver_on) {
396 if (iguanair_receiver(ir, true))
397 dev_warn(ir->dev, "re-enable receiver after transmit failed\n");
402 mutex_unlock(&ir->lock);
404 return rc ? rc : count;
407 static int iguanair_open(struct rc_dev *rdev)
409 struct iguanair *ir = rdev->priv;
412 mutex_lock(&ir->lock);
414 BUG_ON(ir->receiver_on);
416 rc = iguanair_receiver(ir, true);
418 ir->receiver_on = true;
420 mutex_unlock(&ir->lock);
425 static void iguanair_close(struct rc_dev *rdev)
427 struct iguanair *ir = rdev->priv;
430 mutex_lock(&ir->lock);
432 rc = iguanair_receiver(ir, false);
433 ir->receiver_on = false;
434 if (rc && rc != -ENODEV)
435 dev_warn(ir->dev, "failed to disable receiver: %d\n", rc);
437 mutex_unlock(&ir->lock);
440 static int __devinit iguanair_probe(struct usb_interface *intf,
441 const struct usb_device_id *id)
443 struct usb_device *udev = interface_to_usbdev(intf);
447 struct usb_host_interface *idesc;
449 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
450 rc = rc_allocate_device();
456 ir->buf_in = usb_alloc_coherent(udev, MAX_PACKET_SIZE, GFP_KERNEL,
458 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
460 if (!ir->buf_in || !ir->urb_in) {
465 idesc = intf->altsetting;
467 if (idesc->desc.bNumEndpoints < 2) {
473 ir->dev = &intf->dev;
475 ir->pipe_out = usb_sndintpipe(udev,
476 idesc->endpoint[1].desc.bEndpointAddress);
477 mutex_init(&ir->lock);
478 init_completion(&ir->completion);
480 pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress);
481 usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in, MAX_PACKET_SIZE,
483 ir->urb_in->transfer_dma = ir->dma_in;
484 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
486 ret = usb_submit_urb(ir->urb_in, GFP_KERNEL);
488 dev_warn(&intf->dev, "failed to submit urb: %d\n", ret);
492 ret = iguanair_get_features(ir);
496 snprintf(ir->name, sizeof(ir->name),
497 "IguanaWorks USB IR Transceiver version 0x%04x", ir->version);
499 usb_make_path(ir->udev, ir->phys, sizeof(ir->phys));
501 rc->input_name = ir->name;
502 rc->input_phys = ir->phys;
503 usb_to_input_id(ir->udev, &rc->input_id);
504 rc->dev.parent = &intf->dev;
505 rc->driver_type = RC_DRIVER_IR_RAW;
506 rc->allowed_protos = RC_TYPE_ALL;
508 rc->open = iguanair_open;
509 rc->close = iguanair_close;
510 rc->s_tx_mask = iguanair_set_tx_mask;
511 rc->s_tx_carrier = iguanair_set_tx_carrier;
512 rc->tx_ir = iguanair_tx;
513 rc->driver_name = DRIVER_NAME;
514 rc->map_name = RC_MAP_RC6_MCE;
515 rc->timeout = MS_TO_NS(100);
516 rc->rx_resolution = RX_RESOLUTION;
518 iguanair_set_tx_carrier(rc, 38000);
520 ret = rc_register_device(rc);
522 dev_err(&intf->dev, "failed to register rc device %d", ret);
526 usb_set_intfdata(intf, ir);
530 usb_kill_urb(ir->urb_in);
533 usb_free_urb(ir->urb_in);
534 usb_free_coherent(udev, MAX_PACKET_SIZE, ir->buf_in,
542 static void __devexit iguanair_disconnect(struct usb_interface *intf)
544 struct iguanair *ir = usb_get_intfdata(intf);
546 rc_unregister_device(ir->rc);
547 usb_set_intfdata(intf, NULL);
548 usb_kill_urb(ir->urb_in);
549 usb_free_urb(ir->urb_in);
550 usb_free_coherent(ir->udev, MAX_PACKET_SIZE, ir->buf_in, ir->dma_in);
554 static int iguanair_suspend(struct usb_interface *intf, pm_message_t message)
556 struct iguanair *ir = usb_get_intfdata(intf);
559 mutex_lock(&ir->lock);
561 if (ir->receiver_on) {
562 rc = iguanair_receiver(ir, false);
564 dev_warn(ir->dev, "failed to disable receiver for suspend\n");
567 usb_kill_urb(ir->urb_in);
569 mutex_unlock(&ir->lock);
574 static int iguanair_resume(struct usb_interface *intf)
576 struct iguanair *ir = usb_get_intfdata(intf);
579 mutex_lock(&ir->lock);
581 rc = usb_submit_urb(ir->urb_in, GFP_KERNEL);
583 dev_warn(&intf->dev, "failed to submit urb: %d\n", rc);
585 if (ir->receiver_on) {
586 rc = iguanair_receiver(ir, true);
588 dev_warn(ir->dev, "failed to enable receiver after resume\n");
591 mutex_unlock(&ir->lock);
596 static const struct usb_device_id iguanair_table[] = {
597 { USB_DEVICE(0x1781, 0x0938) },
601 static struct usb_driver iguanair_driver = {
603 .probe = iguanair_probe,
604 .disconnect = __devexit_p(iguanair_disconnect),
605 .suspend = iguanair_suspend,
606 .resume = iguanair_resume,
607 .reset_resume = iguanair_resume,
608 .id_table = iguanair_table,
609 .soft_unbind = 1 /* we want to disable receiver on unbind */
612 module_usb_driver(iguanair_driver);
614 MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
615 MODULE_AUTHOR("Sean Young <sean@mess.org>");
616 MODULE_LICENSE("GPL");
617 MODULE_DEVICE_TABLE(usb, iguanair_table);