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
94 static void process_ir_data(struct iguanair *ir, unsigned len)
96 if (len >= 4 && ir->buf_in[0] == 0 && ir->buf_in[1] == 0) {
97 switch (ir->buf_in[3]) {
100 ir->version = (ir->buf_in[5] << 8) |
102 complete(&ir->completion);
105 case CMD_GET_BUFSIZE:
107 ir->bufsize = ir->buf_in[4];
108 complete(&ir->completion);
111 case CMD_GET_FEATURES:
113 ir->cycle_overhead = ir->buf_in[5];
114 complete(&ir->completion);
117 case CMD_TX_OVERFLOW:
118 ir->tx_overflow = true;
119 case CMD_RECEIVER_OFF:
120 case CMD_RECEIVER_ON:
122 complete(&ir->completion);
124 case CMD_RX_OVERFLOW:
125 dev_warn(ir->dev, "receive overflow\n");
128 dev_warn(ir->dev, "control code %02x received\n",
132 } else if (len >= 7) {
133 DEFINE_IR_RAW_EVENT(rawir);
136 init_ir_raw_event(&rawir);
138 for (i = 0; i < 7; i++) {
139 if (ir->buf_in[i] == 0x80) {
141 rawir.duration = US_TO_NS(21845);
143 rawir.pulse = (ir->buf_in[i] & 0x80) == 0;
144 rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) *
148 ir_raw_event_store_with_filter(ir->rc, &rawir);
151 ir_raw_event_handle(ir->rc);
155 static void iguanair_rx(struct urb *urb)
168 switch (urb->status) {
170 process_ir_data(ir, urb->actual_length);
179 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
183 usb_submit_urb(urb, GFP_ATOMIC);
186 static int iguanair_send(struct iguanair *ir, void *data, unsigned size)
190 INIT_COMPLETION(ir->completion);
192 rc = usb_interrupt_msg(ir->udev, ir->pipe_out, data, size,
193 &transferred, TIMEOUT);
197 if (transferred != size)
200 if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0)
206 static int iguanair_get_features(struct iguanair *ir)
208 struct packet packet;
212 packet.direction = DIR_OUT;
213 packet.cmd = CMD_GET_VERSION;
215 rc = iguanair_send(ir, &packet, sizeof(packet));
217 dev_info(ir->dev, "failed to get version\n");
221 if (ir->version < 0x205) {
222 dev_err(ir->dev, "firmware 0x%04x is too old\n", ir->version);
228 ir->cycle_overhead = 65;
230 packet.cmd = CMD_GET_BUFSIZE;
232 rc = iguanair_send(ir, &packet, sizeof(packet));
234 dev_info(ir->dev, "failed to get buffer size\n");
238 packet.cmd = CMD_GET_FEATURES;
240 rc = iguanair_send(ir, &packet, sizeof(packet));
242 dev_info(ir->dev, "failed to get features\n");
250 static int iguanair_receiver(struct iguanair *ir, bool enable)
252 struct packet packet = { 0, DIR_OUT, enable ?
253 CMD_RECEIVER_ON : CMD_RECEIVER_OFF };
255 return iguanair_send(ir, &packet, sizeof(packet));
259 * The iguana ir creates the carrier by busy spinning after each pulse or
260 * space. This is counted in CPU cycles, with the CPU running at 24MHz. It is
261 * broken down into 7-cycles and 4-cyles delays, with a preference for
264 static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier)
266 struct iguanair *ir = dev->priv;
268 if (carrier < 25000 || carrier > 150000)
271 mutex_lock(&ir->lock);
273 if (carrier != ir->carrier) {
274 uint32_t cycles, fours, sevens;
276 ir->carrier = carrier;
278 cycles = DIV_ROUND_CLOSEST(24000000, carrier * 2) -
281 /* make up the the remainer of 4-cycle blocks */
282 switch (cycles & 3) {
297 fours = (cycles - sevens * 7) / 4;
299 /* magic happens here */
300 ir->busy7 = (4 - sevens) * 2;
301 ir->busy4 = 110 - fours;
304 mutex_unlock(&ir->lock);
309 static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask)
311 struct iguanair *ir = dev->priv;
316 mutex_lock(&ir->lock);
318 mutex_unlock(&ir->lock);
323 static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count)
325 struct iguanair *ir = dev->priv;
326 uint8_t space, *payload;
327 unsigned i, size, rc;
328 struct send_packet *packet;
330 mutex_lock(&ir->lock);
332 /* convert from us to carrier periods */
333 for (i = size = 0; i < count; i++) {
334 txbuf[i] = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000);
335 size += (txbuf[i] + 126) / 127;
338 packet = kmalloc(sizeof(*packet) + size, GFP_KERNEL);
344 if (size > ir->bufsize) {
349 packet->header.start = 0;
350 packet->header.direction = DIR_OUT;
351 packet->header.cmd = CMD_SEND;
352 packet->length = size;
353 packet->channels = ir->channels << 4;
354 packet->busy7 = ir->busy7;
355 packet->busy4 = ir->busy4;
358 payload = packet->payload;
360 for (i = 0; i < count; i++) {
361 unsigned periods = txbuf[i];
363 while (periods > 127) {
364 *payload++ = 127 | space;
368 *payload++ = periods | space;
372 if (ir->receiver_on) {
373 rc = iguanair_receiver(ir, false);
375 dev_warn(ir->dev, "disable receiver before transmit failed\n");
380 ir->tx_overflow = false;
382 rc = iguanair_send(ir, packet, size + 8);
384 if (rc == 0 && ir->tx_overflow)
387 if (ir->receiver_on) {
388 if (iguanair_receiver(ir, true))
389 dev_warn(ir->dev, "re-enable receiver after transmit failed\n");
393 mutex_unlock(&ir->lock);
399 static int iguanair_open(struct rc_dev *rdev)
401 struct iguanair *ir = rdev->priv;
404 mutex_lock(&ir->lock);
406 BUG_ON(ir->receiver_on);
408 rc = iguanair_receiver(ir, true);
410 ir->receiver_on = true;
412 mutex_unlock(&ir->lock);
417 static void iguanair_close(struct rc_dev *rdev)
419 struct iguanair *ir = rdev->priv;
422 mutex_lock(&ir->lock);
424 rc = iguanair_receiver(ir, false);
425 ir->receiver_on = false;
427 dev_warn(ir->dev, "failed to disable receiver: %d\n", rc);
429 mutex_unlock(&ir->lock);
432 static int __devinit iguanair_probe(struct usb_interface *intf,
433 const struct usb_device_id *id)
435 struct usb_device *udev = interface_to_usbdev(intf);
439 struct usb_host_interface *idesc;
441 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
442 rc = rc_allocate_device();
448 ir->buf_in = usb_alloc_coherent(udev, MAX_PACKET_SIZE, GFP_KERNEL,
450 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
452 if (!ir->buf_in || !ir->urb_in) {
457 idesc = intf->altsetting;
459 if (idesc->desc.bNumEndpoints < 2) {
465 ir->dev = &intf->dev;
467 ir->pipe_out = usb_sndintpipe(udev,
468 idesc->endpoint[1].desc.bEndpointAddress);
469 mutex_init(&ir->lock);
470 init_completion(&ir->completion);
472 pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress);
473 usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in,
474 MAX_PACKET_SIZE, iguanair_rx, ir,
475 idesc->endpoint[0].desc.bInterval);
476 ir->urb_in->transfer_dma = ir->dma_in;
477 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
479 ret = usb_submit_urb(ir->urb_in, GFP_KERNEL);
481 dev_warn(&intf->dev, "failed to submit urb: %d\n", ret);
485 ret = iguanair_get_features(ir);
489 snprintf(ir->name, sizeof(ir->name),
490 "IguanaWorks USB IR Transceiver version 0x%04x", ir->version);
492 usb_make_path(ir->udev, ir->phys, sizeof(ir->phys));
494 rc->input_name = ir->name;
495 rc->input_phys = ir->phys;
496 usb_to_input_id(ir->udev, &rc->input_id);
497 rc->dev.parent = &intf->dev;
498 rc->driver_type = RC_DRIVER_IR_RAW;
499 rc->allowed_protos = RC_TYPE_ALL;
501 rc->open = iguanair_open;
502 rc->close = iguanair_close;
503 rc->s_tx_mask = iguanair_set_tx_mask;
504 rc->s_tx_carrier = iguanair_set_tx_carrier;
505 rc->tx_ir = iguanair_tx;
506 rc->driver_name = DRIVER_NAME;
507 rc->map_name = RC_MAP_EMPTY;
509 iguanair_set_tx_carrier(rc, 38000);
511 ret = rc_register_device(rc);
513 dev_err(&intf->dev, "failed to register rc device %d", ret);
517 usb_set_intfdata(intf, ir);
519 dev_info(&intf->dev, "Registered %s", ir->name);
523 usb_kill_urb(ir->urb_in);
526 usb_free_urb(ir->urb_in);
527 usb_free_coherent(udev, MAX_PACKET_SIZE, ir->buf_in,
535 static void __devexit iguanair_disconnect(struct usb_interface *intf)
537 struct iguanair *ir = usb_get_intfdata(intf);
539 usb_set_intfdata(intf, NULL);
541 usb_kill_urb(ir->urb_in);
542 usb_free_urb(ir->urb_in);
543 usb_free_coherent(ir->udev, MAX_PACKET_SIZE, ir->buf_in, ir->dma_in);
544 rc_unregister_device(ir->rc);
548 static int iguanair_suspend(struct usb_interface *intf, pm_message_t message)
550 struct iguanair *ir = usb_get_intfdata(intf);
553 mutex_lock(&ir->lock);
555 if (ir->receiver_on) {
556 rc = iguanair_receiver(ir, false);
558 dev_warn(ir->dev, "failed to disable receiver for suspend\n");
561 mutex_unlock(&ir->lock);
566 static int iguanair_resume(struct usb_interface *intf)
568 struct iguanair *ir = usb_get_intfdata(intf);
571 mutex_lock(&ir->lock);
573 if (ir->receiver_on) {
574 rc = iguanair_receiver(ir, true);
576 dev_warn(ir->dev, "failed to enable receiver after resume\n");
579 mutex_unlock(&ir->lock);
584 static const struct usb_device_id iguanair_table[] = {
585 { USB_DEVICE(0x1781, 0x0938) },
589 static struct usb_driver iguanair_driver = {
591 .probe = iguanair_probe,
592 .disconnect = __devexit_p(iguanair_disconnect),
593 .suspend = iguanair_suspend,
594 .resume = iguanair_resume,
595 .reset_resume = iguanair_resume,
596 .id_table = iguanair_table
599 module_usb_driver(iguanair_driver);
601 MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
602 MODULE_AUTHOR("Sean Young <sean@mess.org>");
603 MODULE_LICENSE("GPL");
604 MODULE_DEVICE_TABLE(usb, iguanair_table);