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"
37 struct usb_device
*udev
;
41 uint8_t cycle_overhead
;
45 /* receiver support */
47 dma_addr_t dma_in
, dma_out
;
49 struct urb
*urb_in
, *urb_out
;
50 struct completion completion
;
52 /* transmit support */
55 struct send_packet
*packet
;
62 #define CMD_GET_VERSION 0x01
63 #define CMD_GET_BUFSIZE 0x11
64 #define CMD_GET_FEATURES 0x10
66 #define CMD_EXECUTE 0x1f
67 #define CMD_RX_OVERFLOW 0x31
68 #define CMD_TX_OVERFLOW 0x32
69 #define CMD_RECEIVER_ON 0x12
70 #define CMD_RECEIVER_OFF 0x14
75 #define MAX_IN_PACKET 8u
76 #define MAX_OUT_PACKET (sizeof(struct send_packet) + BUF_SIZE)
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 void iguanair_irq_out(struct urb
*urb
)
196 struct iguanair
*ir
= urb
->context
;
199 dev_dbg(ir
->dev
, "Error: out urb status = %d\n", urb
->status
);
201 /* if we sent an nop packet, do not expect a response */
202 if (urb
->status
== 0 && ir
->packet
->header
.cmd
== CMD_NOP
)
203 complete(&ir
->completion
);
206 static int iguanair_send(struct iguanair
*ir
, unsigned size
)
210 INIT_COMPLETION(ir
->completion
);
212 ir
->urb_out
->transfer_buffer_length
= size
;
213 rc
= usb_submit_urb(ir
->urb_out
, GFP_KERNEL
);
217 if (wait_for_completion_timeout(&ir
->completion
, TIMEOUT
) == 0)
223 static int iguanair_get_features(struct iguanair
*ir
)
228 * On cold boot, the iguanair initializes on the first packet
229 * received but does not process that packet. Send an empty
232 ir
->packet
->header
.start
= 0;
233 ir
->packet
->header
.direction
= DIR_OUT
;
234 ir
->packet
->header
.cmd
= CMD_NOP
;
235 iguanair_send(ir
, sizeof(ir
->packet
->header
));
237 ir
->packet
->header
.cmd
= CMD_GET_VERSION
;
238 rc
= iguanair_send(ir
, sizeof(ir
->packet
->header
));
240 dev_info(ir
->dev
, "failed to get version\n");
244 if (ir
->version
< 0x205) {
245 dev_err(ir
->dev
, "firmware 0x%04x is too old\n", ir
->version
);
251 ir
->cycle_overhead
= 65;
253 ir
->packet
->header
.cmd
= CMD_GET_BUFSIZE
;
255 rc
= iguanair_send(ir
, sizeof(ir
->packet
->header
));
257 dev_info(ir
->dev
, "failed to get buffer size\n");
261 if (ir
->bufsize
> BUF_SIZE
) {
262 dev_info(ir
->dev
, "buffer size %u larger than expected\n",
264 ir
->bufsize
= BUF_SIZE
;
267 ir
->packet
->header
.cmd
= CMD_GET_FEATURES
;
269 rc
= iguanair_send(ir
, sizeof(ir
->packet
->header
));
271 dev_info(ir
->dev
, "failed to get features\n");
276 static int iguanair_receiver(struct iguanair
*ir
, bool enable
)
278 ir
->packet
->header
.start
= 0;
279 ir
->packet
->header
.direction
= DIR_OUT
;
280 ir
->packet
->header
.cmd
= enable
? CMD_RECEIVER_ON
: CMD_RECEIVER_OFF
;
283 ir_raw_event_reset(ir
->rc
);
285 return iguanair_send(ir
, sizeof(ir
->packet
->header
));
289 * The iguana ir creates the carrier by busy spinning after each pulse or
290 * space. This is counted in CPU cycles, with the CPU running at 24MHz. It is
291 * broken down into 7-cycles and 4-cyles delays, with a preference for
294 static int iguanair_set_tx_carrier(struct rc_dev
*dev
, uint32_t carrier
)
296 struct iguanair
*ir
= dev
->priv
;
298 if (carrier
< 25000 || carrier
> 150000)
301 mutex_lock(&ir
->lock
);
303 if (carrier
!= ir
->carrier
) {
304 uint32_t cycles
, fours
, sevens
;
306 ir
->carrier
= carrier
;
308 cycles
= DIV_ROUND_CLOSEST(24000000, carrier
* 2) -
311 /* make up the the remainer of 4-cycle blocks */
312 switch (cycles
& 3) {
327 fours
= (cycles
- sevens
* 7) / 4;
329 /* magic happens here */
330 ir
->packet
->busy7
= (4 - sevens
) * 2;
331 ir
->packet
->busy4
= 110 - fours
;
334 mutex_unlock(&ir
->lock
);
339 static int iguanair_set_tx_mask(struct rc_dev
*dev
, uint32_t mask
)
341 struct iguanair
*ir
= dev
->priv
;
346 mutex_lock(&ir
->lock
);
347 ir
->packet
->channels
= mask
<< 4;
348 mutex_unlock(&ir
->lock
);
353 static int iguanair_tx(struct rc_dev
*dev
, unsigned *txbuf
, unsigned count
)
355 struct iguanair
*ir
= dev
->priv
;
357 unsigned i
, size
, periods
, bytes
;
360 mutex_lock(&ir
->lock
);
362 /* convert from us to carrier periods */
363 for (i
= space
= size
= 0; i
< count
; i
++) {
364 periods
= DIV_ROUND_CLOSEST(txbuf
[i
] * ir
->carrier
, 1000000);
365 bytes
= DIV_ROUND_UP(periods
, 127);
366 if (size
+ bytes
> ir
->bufsize
) {
370 while (periods
> 127) {
371 ir
->packet
->payload
[size
++] = 127 | space
;
375 ir
->packet
->payload
[size
++] = periods
| space
;
384 ir
->packet
->header
.start
= 0;
385 ir
->packet
->header
.direction
= DIR_OUT
;
386 ir
->packet
->header
.cmd
= CMD_SEND
;
387 ir
->packet
->length
= size
;
389 ir
->tx_overflow
= false;
391 rc
= iguanair_send(ir
, sizeof(*ir
->packet
) + size
);
393 if (rc
== 0 && ir
->tx_overflow
)
397 mutex_unlock(&ir
->lock
);
399 return rc
? rc
: count
;
402 static int iguanair_open(struct rc_dev
*rdev
)
404 struct iguanair
*ir
= rdev
->priv
;
407 mutex_lock(&ir
->lock
);
409 rc
= iguanair_receiver(ir
, true);
411 ir
->receiver_on
= true;
413 mutex_unlock(&ir
->lock
);
418 static void iguanair_close(struct rc_dev
*rdev
)
420 struct iguanair
*ir
= rdev
->priv
;
423 mutex_lock(&ir
->lock
);
425 rc
= iguanair_receiver(ir
, false);
426 ir
->receiver_on
= false;
427 if (rc
&& rc
!= -ENODEV
)
428 dev_warn(ir
->dev
, "failed to disable receiver: %d\n", rc
);
430 mutex_unlock(&ir
->lock
);
433 static int iguanair_probe(struct usb_interface
*intf
,
434 const struct usb_device_id
*id
)
436 struct usb_device
*udev
= interface_to_usbdev(intf
);
439 int ret
, pipein
, pipeout
;
440 struct usb_host_interface
*idesc
;
442 ir
= kzalloc(sizeof(*ir
), GFP_KERNEL
);
443 rc
= rc_allocate_device();
449 ir
->buf_in
= usb_alloc_coherent(udev
, MAX_IN_PACKET
, GFP_KERNEL
,
451 ir
->packet
= usb_alloc_coherent(udev
, MAX_OUT_PACKET
, GFP_KERNEL
,
453 ir
->urb_in
= usb_alloc_urb(0, GFP_KERNEL
);
454 ir
->urb_out
= usb_alloc_urb(0, GFP_KERNEL
);
456 if (!ir
->buf_in
|| !ir
->packet
|| !ir
->urb_in
|| !ir
->urb_out
) {
461 idesc
= intf
->altsetting
;
463 if (idesc
->desc
.bNumEndpoints
< 2) {
469 ir
->dev
= &intf
->dev
;
471 mutex_init(&ir
->lock
);
473 init_completion(&ir
->completion
);
474 pipeout
= usb_sndintpipe(udev
,
475 idesc
->endpoint
[1].desc
.bEndpointAddress
);
476 usb_fill_int_urb(ir
->urb_out
, udev
, pipeout
, ir
->packet
, MAX_OUT_PACKET
,
477 iguanair_irq_out
, ir
, 1);
478 ir
->urb_out
->transfer_dma
= ir
->dma_out
;
479 ir
->urb_out
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
481 pipein
= usb_rcvintpipe(udev
, idesc
->endpoint
[0].desc
.bEndpointAddress
);
482 usb_fill_int_urb(ir
->urb_in
, udev
, pipein
, ir
->buf_in
, MAX_IN_PACKET
,
484 ir
->urb_in
->transfer_dma
= ir
->dma_in
;
485 ir
->urb_in
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
487 ret
= usb_submit_urb(ir
->urb_in
, GFP_KERNEL
);
489 dev_warn(&intf
->dev
, "failed to submit urb: %d\n", ret
);
493 ret
= iguanair_get_features(ir
);
497 snprintf(ir
->name
, sizeof(ir
->name
),
498 "IguanaWorks USB IR Transceiver version 0x%04x", ir
->version
);
500 usb_make_path(ir
->udev
, ir
->phys
, sizeof(ir
->phys
));
502 rc
->input_name
= ir
->name
;
503 rc
->input_phys
= ir
->phys
;
504 usb_to_input_id(ir
->udev
, &rc
->input_id
);
505 rc
->dev
.parent
= &intf
->dev
;
506 rc
->driver_type
= RC_DRIVER_IR_RAW
;
507 rc
->allowed_protos
= RC_BIT_ALL
;
509 rc
->open
= iguanair_open
;
510 rc
->close
= iguanair_close
;
511 rc
->s_tx_mask
= iguanair_set_tx_mask
;
512 rc
->s_tx_carrier
= iguanair_set_tx_carrier
;
513 rc
->tx_ir
= iguanair_tx
;
514 rc
->driver_name
= DRIVER_NAME
;
515 rc
->map_name
= RC_MAP_RC6_MCE
;
516 rc
->timeout
= MS_TO_NS(100);
517 rc
->rx_resolution
= RX_RESOLUTION
;
519 iguanair_set_tx_carrier(rc
, 38000);
520 iguanair_set_tx_mask(rc
, 0);
522 ret
= rc_register_device(rc
);
524 dev_err(&intf
->dev
, "failed to register rc device %d", ret
);
528 usb_set_intfdata(intf
, ir
);
532 usb_kill_urb(ir
->urb_in
);
533 usb_kill_urb(ir
->urb_out
);
536 usb_free_urb(ir
->urb_in
);
537 usb_free_urb(ir
->urb_out
);
538 usb_free_coherent(udev
, MAX_IN_PACKET
, ir
->buf_in
, ir
->dma_in
);
539 usb_free_coherent(udev
, MAX_OUT_PACKET
, ir
->packet
,
547 static void iguanair_disconnect(struct usb_interface
*intf
)
549 struct iguanair
*ir
= usb_get_intfdata(intf
);
551 rc_unregister_device(ir
->rc
);
552 usb_set_intfdata(intf
, NULL
);
553 usb_kill_urb(ir
->urb_in
);
554 usb_kill_urb(ir
->urb_out
);
555 usb_free_urb(ir
->urb_in
);
556 usb_free_urb(ir
->urb_out
);
557 usb_free_coherent(ir
->udev
, MAX_IN_PACKET
, ir
->buf_in
, ir
->dma_in
);
558 usb_free_coherent(ir
->udev
, MAX_OUT_PACKET
, ir
->packet
, ir
->dma_out
);
562 static int iguanair_suspend(struct usb_interface
*intf
, pm_message_t message
)
564 struct iguanair
*ir
= usb_get_intfdata(intf
);
567 mutex_lock(&ir
->lock
);
569 if (ir
->receiver_on
) {
570 rc
= iguanair_receiver(ir
, false);
572 dev_warn(ir
->dev
, "failed to disable receiver for suspend\n");
575 usb_kill_urb(ir
->urb_in
);
576 usb_kill_urb(ir
->urb_out
);
578 mutex_unlock(&ir
->lock
);
583 static int iguanair_resume(struct usb_interface
*intf
)
585 struct iguanair
*ir
= usb_get_intfdata(intf
);
588 mutex_lock(&ir
->lock
);
590 rc
= usb_submit_urb(ir
->urb_in
, GFP_KERNEL
);
592 dev_warn(&intf
->dev
, "failed to submit urb: %d\n", rc
);
594 if (ir
->receiver_on
) {
595 rc
= iguanair_receiver(ir
, true);
597 dev_warn(ir
->dev
, "failed to enable receiver after resume\n");
600 mutex_unlock(&ir
->lock
);
605 static const struct usb_device_id iguanair_table
[] = {
606 { USB_DEVICE(0x1781, 0x0938) },
610 static struct usb_driver iguanair_driver
= {
612 .probe
= iguanair_probe
,
613 .disconnect
= iguanair_disconnect
,
614 .suspend
= iguanair_suspend
,
615 .resume
= iguanair_resume
,
616 .reset_resume
= iguanair_resume
,
617 .id_table
= iguanair_table
,
618 .soft_unbind
= 1 /* we want to disable receiver on unbind */
621 module_usb_driver(iguanair_driver
);
623 MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
624 MODULE_AUTHOR("Sean Young <sean@mess.org>");
625 MODULE_LICENSE("GPL");
626 MODULE_DEVICE_TABLE(usb
, iguanair_table
);