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.
17 #include <linux/device.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/usb.h>
21 #include <linux/usb/input.h>
22 #include <linux/slab.h>
23 #include <linux/completion.h>
24 #include <media/rc-core.h>
26 #define DRIVER_NAME "iguanair"
33 struct usb_device
*udev
;
37 uint8_t cycle_overhead
;
41 /* receiver support */
43 dma_addr_t dma_in
, dma_out
;
45 struct urb
*urb_in
, *urb_out
;
46 struct completion completion
;
48 /* transmit support */
51 struct send_packet
*packet
;
58 #define CMD_GET_VERSION 0x01
59 #define CMD_GET_BUFSIZE 0x11
60 #define CMD_GET_FEATURES 0x10
62 #define CMD_EXECUTE 0x1f
63 #define CMD_RX_OVERFLOW 0x31
64 #define CMD_TX_OVERFLOW 0x32
65 #define CMD_RECEIVER_ON 0x12
66 #define CMD_RECEIVER_OFF 0x14
71 #define MAX_IN_PACKET 8u
72 #define MAX_OUT_PACKET (sizeof(struct send_packet) + BUF_SIZE)
74 #define RX_RESOLUTION 21333
91 static void process_ir_data(struct iguanair
*ir
, unsigned len
)
93 if (len
>= 4 && ir
->buf_in
[0] == 0 && ir
->buf_in
[1] == 0) {
94 switch (ir
->buf_in
[3]) {
97 ir
->version
= (ir
->buf_in
[5] << 8) |
99 complete(&ir
->completion
);
102 case CMD_GET_BUFSIZE
:
104 ir
->bufsize
= ir
->buf_in
[4];
105 complete(&ir
->completion
);
108 case CMD_GET_FEATURES
:
110 ir
->cycle_overhead
= ir
->buf_in
[5];
111 complete(&ir
->completion
);
114 case CMD_TX_OVERFLOW
:
115 ir
->tx_overflow
= true;
116 case CMD_RECEIVER_OFF
:
117 case CMD_RECEIVER_ON
:
119 complete(&ir
->completion
);
121 case CMD_RX_OVERFLOW
:
122 dev_warn(ir
->dev
, "receive overflow\n");
123 ir_raw_event_reset(ir
->rc
);
126 dev_warn(ir
->dev
, "control code %02x received\n",
130 } else if (len
>= 7) {
131 DEFINE_IR_RAW_EVENT(rawir
);
135 init_ir_raw_event(&rawir
);
137 for (i
= 0; i
< 7; i
++) {
138 if (ir
->buf_in
[i
] == 0x80) {
140 rawir
.duration
= US_TO_NS(21845);
142 rawir
.pulse
= (ir
->buf_in
[i
] & 0x80) == 0;
143 rawir
.duration
= ((ir
->buf_in
[i
] & 0x7f) + 1) *
147 if (ir_raw_event_store_with_filter(ir
->rc
, &rawir
))
152 ir_raw_event_handle(ir
->rc
);
156 static void iguanair_rx(struct urb
*urb
)
170 switch (urb
->status
) {
172 process_ir_data(ir
, urb
->actual_length
);
181 dev_dbg(ir
->dev
, "Error: urb status = %d\n", urb
->status
);
185 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
186 if (rc
&& rc
!= -ENODEV
)
187 dev_warn(ir
->dev
, "failed to resubmit urb: %d\n", rc
);
190 static void iguanair_irq_out(struct urb
*urb
)
192 struct iguanair
*ir
= urb
->context
;
195 dev_dbg(ir
->dev
, "Error: out urb status = %d\n", urb
->status
);
197 /* if we sent an nop packet, do not expect a response */
198 if (urb
->status
== 0 && ir
->packet
->header
.cmd
== CMD_NOP
)
199 complete(&ir
->completion
);
202 static int iguanair_send(struct iguanair
*ir
, unsigned size
)
206 reinit_completion(&ir
->completion
);
208 ir
->urb_out
->transfer_buffer_length
= size
;
209 rc
= usb_submit_urb(ir
->urb_out
, GFP_KERNEL
);
213 if (wait_for_completion_timeout(&ir
->completion
, TIMEOUT
) == 0)
219 static int iguanair_get_features(struct iguanair
*ir
)
224 * On cold boot, the iguanair initializes on the first packet
225 * received but does not process that packet. Send an empty
228 ir
->packet
->header
.start
= 0;
229 ir
->packet
->header
.direction
= DIR_OUT
;
230 ir
->packet
->header
.cmd
= CMD_NOP
;
231 iguanair_send(ir
, sizeof(ir
->packet
->header
));
233 ir
->packet
->header
.cmd
= CMD_GET_VERSION
;
234 rc
= iguanair_send(ir
, sizeof(ir
->packet
->header
));
236 dev_info(ir
->dev
, "failed to get version\n");
240 if (ir
->version
< 0x205) {
241 dev_err(ir
->dev
, "firmware 0x%04x is too old\n", ir
->version
);
247 ir
->cycle_overhead
= 65;
249 ir
->packet
->header
.cmd
= CMD_GET_BUFSIZE
;
251 rc
= iguanair_send(ir
, sizeof(ir
->packet
->header
));
253 dev_info(ir
->dev
, "failed to get buffer size\n");
257 if (ir
->bufsize
> BUF_SIZE
) {
258 dev_info(ir
->dev
, "buffer size %u larger than expected\n",
260 ir
->bufsize
= BUF_SIZE
;
263 ir
->packet
->header
.cmd
= CMD_GET_FEATURES
;
265 rc
= iguanair_send(ir
, sizeof(ir
->packet
->header
));
267 dev_info(ir
->dev
, "failed to get features\n");
272 static int iguanair_receiver(struct iguanair
*ir
, bool enable
)
274 ir
->packet
->header
.start
= 0;
275 ir
->packet
->header
.direction
= DIR_OUT
;
276 ir
->packet
->header
.cmd
= enable
? CMD_RECEIVER_ON
: CMD_RECEIVER_OFF
;
279 ir_raw_event_reset(ir
->rc
);
281 return iguanair_send(ir
, sizeof(ir
->packet
->header
));
285 * The iguanair creates the carrier by busy spinning after each half period.
286 * This is counted in CPU cycles, with the CPU running at 24MHz. It is
287 * broken down into 7-cycles and 4-cyles delays, with a preference for
288 * 4-cycle delays, minus the overhead of the loop itself (cycle_overhead).
290 static int iguanair_set_tx_carrier(struct rc_dev
*dev
, uint32_t carrier
)
292 struct iguanair
*ir
= dev
->priv
;
294 if (carrier
< 25000 || carrier
> 150000)
297 mutex_lock(&ir
->lock
);
299 if (carrier
!= ir
->carrier
) {
300 uint32_t cycles
, fours
, sevens
;
302 ir
->carrier
= carrier
;
304 cycles
= DIV_ROUND_CLOSEST(24000000, carrier
* 2) -
308 * Calculate minimum number of 7 cycles needed so
309 * we are left with a multiple of 4; so we want to have
310 * (sevens * 7) & 3 == cycles & 3
312 sevens
= (4 - cycles
) & 3;
313 fours
= (cycles
- sevens
* 7) / 4;
316 * The firmware interprets these values as a relative offset
317 * for a branch. Immediately following the branches, there
318 * 4 instructions of 7 cycles (2 bytes each) and 110
319 * instructions of 4 cycles (1 byte each). A relative branch
320 * of 0 will execute all of them, branch further for less
323 ir
->packet
->busy7
= (4 - sevens
) * 2;
324 ir
->packet
->busy4
= 110 - fours
;
327 mutex_unlock(&ir
->lock
);
332 static int iguanair_set_tx_mask(struct rc_dev
*dev
, uint32_t mask
)
334 struct iguanair
*ir
= dev
->priv
;
339 mutex_lock(&ir
->lock
);
340 ir
->packet
->channels
= mask
<< 4;
341 mutex_unlock(&ir
->lock
);
346 static int iguanair_tx(struct rc_dev
*dev
, unsigned *txbuf
, unsigned count
)
348 struct iguanair
*ir
= dev
->priv
;
350 unsigned i
, size
, periods
, bytes
;
353 mutex_lock(&ir
->lock
);
355 /* convert from us to carrier periods */
356 for (i
= space
= size
= 0; i
< count
; i
++) {
357 periods
= DIV_ROUND_CLOSEST(txbuf
[i
] * ir
->carrier
, 1000000);
358 bytes
= DIV_ROUND_UP(periods
, 127);
359 if (size
+ bytes
> ir
->bufsize
) {
364 unsigned p
= min(periods
, 127u);
365 ir
->packet
->payload
[size
++] = p
| space
;
371 ir
->packet
->header
.start
= 0;
372 ir
->packet
->header
.direction
= DIR_OUT
;
373 ir
->packet
->header
.cmd
= CMD_SEND
;
374 ir
->packet
->length
= size
;
376 ir
->tx_overflow
= false;
378 rc
= iguanair_send(ir
, sizeof(*ir
->packet
) + size
);
380 if (rc
== 0 && ir
->tx_overflow
)
384 mutex_unlock(&ir
->lock
);
386 return rc
? rc
: count
;
389 static int iguanair_open(struct rc_dev
*rdev
)
391 struct iguanair
*ir
= rdev
->priv
;
394 mutex_lock(&ir
->lock
);
396 rc
= iguanair_receiver(ir
, true);
398 ir
->receiver_on
= true;
400 mutex_unlock(&ir
->lock
);
405 static void iguanair_close(struct rc_dev
*rdev
)
407 struct iguanair
*ir
= rdev
->priv
;
410 mutex_lock(&ir
->lock
);
412 rc
= iguanair_receiver(ir
, false);
413 ir
->receiver_on
= false;
414 if (rc
&& rc
!= -ENODEV
)
415 dev_warn(ir
->dev
, "failed to disable receiver: %d\n", rc
);
417 mutex_unlock(&ir
->lock
);
420 static int iguanair_probe(struct usb_interface
*intf
,
421 const struct usb_device_id
*id
)
423 struct usb_device
*udev
= interface_to_usbdev(intf
);
426 int ret
, pipein
, pipeout
;
427 struct usb_host_interface
*idesc
;
429 ir
= kzalloc(sizeof(*ir
), GFP_KERNEL
);
430 rc
= rc_allocate_device(RC_DRIVER_IR_RAW
);
436 ir
->buf_in
= usb_alloc_coherent(udev
, MAX_IN_PACKET
, GFP_KERNEL
,
438 ir
->packet
= usb_alloc_coherent(udev
, MAX_OUT_PACKET
, GFP_KERNEL
,
440 ir
->urb_in
= usb_alloc_urb(0, GFP_KERNEL
);
441 ir
->urb_out
= usb_alloc_urb(0, GFP_KERNEL
);
443 if (!ir
->buf_in
|| !ir
->packet
|| !ir
->urb_in
|| !ir
->urb_out
) {
448 idesc
= intf
->altsetting
;
450 if (idesc
->desc
.bNumEndpoints
< 2) {
456 ir
->dev
= &intf
->dev
;
458 mutex_init(&ir
->lock
);
460 init_completion(&ir
->completion
);
461 pipeout
= usb_sndintpipe(udev
,
462 idesc
->endpoint
[1].desc
.bEndpointAddress
);
463 usb_fill_int_urb(ir
->urb_out
, udev
, pipeout
, ir
->packet
, MAX_OUT_PACKET
,
464 iguanair_irq_out
, ir
, 1);
465 ir
->urb_out
->transfer_dma
= ir
->dma_out
;
466 ir
->urb_out
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
468 pipein
= usb_rcvintpipe(udev
, idesc
->endpoint
[0].desc
.bEndpointAddress
);
469 usb_fill_int_urb(ir
->urb_in
, udev
, pipein
, ir
->buf_in
, MAX_IN_PACKET
,
471 ir
->urb_in
->transfer_dma
= ir
->dma_in
;
472 ir
->urb_in
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
474 ret
= usb_submit_urb(ir
->urb_in
, GFP_KERNEL
);
476 dev_warn(&intf
->dev
, "failed to submit urb: %d\n", ret
);
480 ret
= iguanair_get_features(ir
);
484 snprintf(ir
->name
, sizeof(ir
->name
),
485 "IguanaWorks USB IR Transceiver version 0x%04x", ir
->version
);
487 usb_make_path(ir
->udev
, ir
->phys
, sizeof(ir
->phys
));
489 rc
->input_name
= ir
->name
;
490 rc
->input_phys
= ir
->phys
;
491 usb_to_input_id(ir
->udev
, &rc
->input_id
);
492 rc
->dev
.parent
= &intf
->dev
;
493 rc
->allowed_protocols
= RC_BIT_ALL_IR_DECODER
;
495 rc
->open
= iguanair_open
;
496 rc
->close
= iguanair_close
;
497 rc
->s_tx_mask
= iguanair_set_tx_mask
;
498 rc
->s_tx_carrier
= iguanair_set_tx_carrier
;
499 rc
->tx_ir
= iguanair_tx
;
500 rc
->driver_name
= DRIVER_NAME
;
501 rc
->map_name
= RC_MAP_RC6_MCE
;
503 rc
->timeout
= IR_DEFAULT_TIMEOUT
;
504 rc
->max_timeout
= 10 * IR_DEFAULT_TIMEOUT
;
505 rc
->rx_resolution
= RX_RESOLUTION
;
507 iguanair_set_tx_carrier(rc
, 38000);
508 iguanair_set_tx_mask(rc
, 0);
510 ret
= rc_register_device(rc
);
512 dev_err(&intf
->dev
, "failed to register rc device %d", ret
);
516 usb_set_intfdata(intf
, ir
);
520 usb_kill_urb(ir
->urb_in
);
521 usb_kill_urb(ir
->urb_out
);
524 usb_free_urb(ir
->urb_in
);
525 usb_free_urb(ir
->urb_out
);
526 usb_free_coherent(udev
, MAX_IN_PACKET
, ir
->buf_in
, ir
->dma_in
);
527 usb_free_coherent(udev
, MAX_OUT_PACKET
, ir
->packet
,
535 static void iguanair_disconnect(struct usb_interface
*intf
)
537 struct iguanair
*ir
= usb_get_intfdata(intf
);
539 rc_unregister_device(ir
->rc
);
540 usb_set_intfdata(intf
, NULL
);
541 usb_kill_urb(ir
->urb_in
);
542 usb_kill_urb(ir
->urb_out
);
543 usb_free_urb(ir
->urb_in
);
544 usb_free_urb(ir
->urb_out
);
545 usb_free_coherent(ir
->udev
, MAX_IN_PACKET
, ir
->buf_in
, ir
->dma_in
);
546 usb_free_coherent(ir
->udev
, MAX_OUT_PACKET
, ir
->packet
, ir
->dma_out
);
550 static int iguanair_suspend(struct usb_interface
*intf
, pm_message_t message
)
552 struct iguanair
*ir
= usb_get_intfdata(intf
);
555 mutex_lock(&ir
->lock
);
557 if (ir
->receiver_on
) {
558 rc
= iguanair_receiver(ir
, false);
560 dev_warn(ir
->dev
, "failed to disable receiver for suspend\n");
563 usb_kill_urb(ir
->urb_in
);
564 usb_kill_urb(ir
->urb_out
);
566 mutex_unlock(&ir
->lock
);
571 static int iguanair_resume(struct usb_interface
*intf
)
573 struct iguanair
*ir
= usb_get_intfdata(intf
);
576 mutex_lock(&ir
->lock
);
578 rc
= usb_submit_urb(ir
->urb_in
, GFP_KERNEL
);
580 dev_warn(&intf
->dev
, "failed to submit urb: %d\n", rc
);
582 if (ir
->receiver_on
) {
583 rc
= iguanair_receiver(ir
, true);
585 dev_warn(ir
->dev
, "failed to enable receiver after resume\n");
588 mutex_unlock(&ir
->lock
);
593 static const struct usb_device_id iguanair_table
[] = {
594 { USB_DEVICE(0x1781, 0x0938) },
598 static struct usb_driver iguanair_driver
= {
600 .probe
= iguanair_probe
,
601 .disconnect
= iguanair_disconnect
,
602 .suspend
= iguanair_suspend
,
603 .resume
= iguanair_resume
,
604 .reset_resume
= iguanair_resume
,
605 .id_table
= iguanair_table
,
606 .soft_unbind
= 1 /* we want to disable receiver on unbind */
609 module_usb_driver(iguanair_driver
);
611 MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
612 MODULE_AUTHOR("Sean Young <sean@mess.org>");
613 MODULE_LICENSE("GPL");
614 MODULE_DEVICE_TABLE(usb
, iguanair_table
);