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
;
61 #define CMD_GET_VERSION 0x01
62 #define CMD_GET_BUFSIZE 0x11
63 #define CMD_GET_FEATURES 0x10
65 #define CMD_EXECUTE 0x1f
66 #define CMD_RX_OVERFLOW 0x31
67 #define CMD_TX_OVERFLOW 0x32
68 #define CMD_RECEIVER_ON 0x12
69 #define CMD_RECEIVER_OFF 0x14
74 #define MAX_IN_PACKET 8u
75 #define MAX_OUT_PACKET (sizeof(struct send_packet) + BUF_SIZE)
77 #define RX_RESOLUTION 21333
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");
126 ir_raw_event_reset(ir
->rc
);
129 dev_warn(ir
->dev
, "control code %02x received\n",
133 } else if (len
>= 7) {
134 DEFINE_IR_RAW_EVENT(rawir
);
138 init_ir_raw_event(&rawir
);
140 for (i
= 0; i
< 7; i
++) {
141 if (ir
->buf_in
[i
] == 0x80) {
143 rawir
.duration
= US_TO_NS(21845);
145 rawir
.pulse
= (ir
->buf_in
[i
] & 0x80) == 0;
146 rawir
.duration
= ((ir
->buf_in
[i
] & 0x7f) + 1) *
150 if (ir_raw_event_store_with_filter(ir
->rc
, &rawir
))
155 ir_raw_event_handle(ir
->rc
);
159 static void iguanair_rx(struct urb
*urb
)
173 switch (urb
->status
) {
175 process_ir_data(ir
, urb
->actual_length
);
184 dev_dbg(ir
->dev
, "Error: urb status = %d\n", urb
->status
);
188 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
189 if (rc
&& rc
!= -ENODEV
)
190 dev_warn(ir
->dev
, "failed to resubmit urb: %d\n", rc
);
193 static void iguanair_irq_out(struct urb
*urb
)
195 struct iguanair
*ir
= urb
->context
;
198 dev_dbg(ir
->dev
, "Error: out urb status = %d\n", urb
->status
);
201 static int iguanair_send(struct iguanair
*ir
, unsigned size
)
205 INIT_COMPLETION(ir
->completion
);
207 ir
->urb_out
->transfer_buffer_length
= size
;
208 rc
= usb_submit_urb(ir
->urb_out
, GFP_KERNEL
);
212 if (wait_for_completion_timeout(&ir
->completion
, TIMEOUT
) == 0)
218 static int iguanair_get_features(struct iguanair
*ir
)
222 ir
->packet
->header
.start
= 0;
223 ir
->packet
->header
.direction
= DIR_OUT
;
224 ir
->packet
->header
.cmd
= CMD_GET_VERSION
;
226 rc
= iguanair_send(ir
, sizeof(ir
->packet
->header
));
228 dev_info(ir
->dev
, "failed to get version\n");
232 if (ir
->version
< 0x205) {
233 dev_err(ir
->dev
, "firmware 0x%04x is too old\n", ir
->version
);
239 ir
->cycle_overhead
= 65;
241 ir
->packet
->header
.cmd
= CMD_GET_BUFSIZE
;
243 rc
= iguanair_send(ir
, sizeof(ir
->packet
->header
));
245 dev_info(ir
->dev
, "failed to get buffer size\n");
249 if (ir
->bufsize
> BUF_SIZE
) {
250 dev_info(ir
->dev
, "buffer size %u larger than expected\n",
252 ir
->bufsize
= BUF_SIZE
;
255 ir
->packet
->header
.cmd
= CMD_GET_FEATURES
;
257 rc
= iguanair_send(ir
, sizeof(ir
->packet
->header
));
259 dev_info(ir
->dev
, "failed to get features\n");
267 static int iguanair_receiver(struct iguanair
*ir
, bool enable
)
271 ir
->packet
->header
.start
= 0;
272 ir
->packet
->header
.direction
= DIR_OUT
;
273 ir
->packet
->header
.cmd
= enable
? CMD_RECEIVER_ON
: CMD_RECEIVER_OFF
;
276 ir_raw_event_reset(ir
->rc
);
278 rc
= iguanair_send(ir
, sizeof(ir
->packet
->header
));
284 * The iguana ir creates the carrier by busy spinning after each pulse or
285 * space. This is counted in CPU cycles, with the CPU running at 24MHz. It is
286 * broken down into 7-cycles and 4-cyles delays, with a preference for
289 static int iguanair_set_tx_carrier(struct rc_dev
*dev
, uint32_t carrier
)
291 struct iguanair
*ir
= dev
->priv
;
293 if (carrier
< 25000 || carrier
> 150000)
296 mutex_lock(&ir
->lock
);
298 if (carrier
!= ir
->carrier
) {
299 uint32_t cycles
, fours
, sevens
;
301 ir
->carrier
= carrier
;
303 cycles
= DIV_ROUND_CLOSEST(24000000, carrier
* 2) -
306 /* make up the the remainer of 4-cycle blocks */
307 switch (cycles
& 3) {
322 fours
= (cycles
- sevens
* 7) / 4;
324 /* magic happens here */
325 ir
->packet
->busy7
= (4 - sevens
) * 2;
326 ir
->packet
->busy4
= 110 - fours
;
329 mutex_unlock(&ir
->lock
);
334 static int iguanair_set_tx_mask(struct rc_dev
*dev
, uint32_t mask
)
336 struct iguanair
*ir
= dev
->priv
;
341 mutex_lock(&ir
->lock
);
342 ir
->packet
->channels
= mask
<< 4;
343 mutex_unlock(&ir
->lock
);
348 static int iguanair_tx(struct rc_dev
*dev
, unsigned *txbuf
, unsigned count
)
350 struct iguanair
*ir
= dev
->priv
;
352 unsigned i
, size
, periods
, bytes
;
355 mutex_lock(&ir
->lock
);
357 /* convert from us to carrier periods */
358 for (i
= space
= size
= 0; i
< count
; i
++) {
359 periods
= DIV_ROUND_CLOSEST(txbuf
[i
] * ir
->carrier
, 1000000);
360 bytes
= DIV_ROUND_UP(periods
, 127);
361 if (size
+ bytes
> ir
->bufsize
) {
365 while (periods
> 127) {
366 ir
->packet
->payload
[size
++] = 127 | space
;
370 ir
->packet
->payload
[size
++] = periods
| space
;
379 ir
->packet
->header
.start
= 0;
380 ir
->packet
->header
.direction
= DIR_OUT
;
381 ir
->packet
->header
.cmd
= CMD_SEND
;
382 ir
->packet
->length
= size
;
384 ir
->tx_overflow
= false;
386 rc
= iguanair_send(ir
, sizeof(*ir
->packet
) + size
);
388 if (rc
== 0 && ir
->tx_overflow
)
392 mutex_unlock(&ir
->lock
);
394 return rc
? rc
: count
;
397 static int iguanair_open(struct rc_dev
*rdev
)
399 struct iguanair
*ir
= rdev
->priv
;
402 mutex_lock(&ir
->lock
);
404 rc
= iguanair_receiver(ir
, true);
406 ir
->receiver_on
= true;
408 mutex_unlock(&ir
->lock
);
413 static void iguanair_close(struct rc_dev
*rdev
)
415 struct iguanair
*ir
= rdev
->priv
;
418 mutex_lock(&ir
->lock
);
420 rc
= iguanair_receiver(ir
, false);
421 ir
->receiver_on
= false;
422 if (rc
&& rc
!= -ENODEV
)
423 dev_warn(ir
->dev
, "failed to disable receiver: %d\n", rc
);
425 mutex_unlock(&ir
->lock
);
428 static int iguanair_probe(struct usb_interface
*intf
,
429 const struct usb_device_id
*id
)
431 struct usb_device
*udev
= interface_to_usbdev(intf
);
434 int ret
, pipein
, pipeout
;
435 struct usb_host_interface
*idesc
;
437 ir
= kzalloc(sizeof(*ir
), GFP_KERNEL
);
438 rc
= rc_allocate_device();
444 ir
->buf_in
= usb_alloc_coherent(udev
, MAX_IN_PACKET
, GFP_KERNEL
,
446 ir
->packet
= usb_alloc_coherent(udev
, MAX_OUT_PACKET
, GFP_KERNEL
,
448 ir
->urb_in
= usb_alloc_urb(0, GFP_KERNEL
);
449 ir
->urb_out
= usb_alloc_urb(0, GFP_KERNEL
);
451 if (!ir
->buf_in
|| !ir
->packet
|| !ir
->urb_in
|| !ir
->urb_out
) {
456 idesc
= intf
->altsetting
;
458 if (idesc
->desc
.bNumEndpoints
< 2) {
464 ir
->dev
= &intf
->dev
;
466 mutex_init(&ir
->lock
);
468 init_completion(&ir
->completion
);
469 pipeout
= usb_sndintpipe(udev
,
470 idesc
->endpoint
[1].desc
.bEndpointAddress
);
471 usb_fill_int_urb(ir
->urb_out
, udev
, pipeout
, ir
->packet
, MAX_OUT_PACKET
,
472 iguanair_irq_out
, ir
, 1);
473 ir
->urb_out
->transfer_dma
= ir
->dma_out
;
474 ir
->urb_out
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
476 pipein
= usb_rcvintpipe(udev
, idesc
->endpoint
[0].desc
.bEndpointAddress
);
477 usb_fill_int_urb(ir
->urb_in
, udev
, pipein
, ir
->buf_in
, MAX_IN_PACKET
,
479 ir
->urb_in
->transfer_dma
= ir
->dma_in
;
480 ir
->urb_in
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
482 ret
= usb_submit_urb(ir
->urb_in
, GFP_KERNEL
);
484 dev_warn(&intf
->dev
, "failed to submit urb: %d\n", ret
);
488 ret
= iguanair_get_features(ir
);
492 snprintf(ir
->name
, sizeof(ir
->name
),
493 "IguanaWorks USB IR Transceiver version 0x%04x", ir
->version
);
495 usb_make_path(ir
->udev
, ir
->phys
, sizeof(ir
->phys
));
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_BIT_ALL
;
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_RC6_MCE
;
511 rc
->timeout
= MS_TO_NS(100);
512 rc
->rx_resolution
= RX_RESOLUTION
;
514 iguanair_set_tx_carrier(rc
, 38000);
516 ret
= rc_register_device(rc
);
518 dev_err(&intf
->dev
, "failed to register rc device %d", ret
);
522 usb_set_intfdata(intf
, ir
);
526 usb_kill_urb(ir
->urb_in
);
527 usb_kill_urb(ir
->urb_out
);
530 usb_free_urb(ir
->urb_in
);
531 usb_free_urb(ir
->urb_out
);
532 usb_free_coherent(udev
, MAX_IN_PACKET
, ir
->buf_in
, ir
->dma_in
);
533 usb_free_coherent(udev
, MAX_OUT_PACKET
, ir
->packet
,
541 static void iguanair_disconnect(struct usb_interface
*intf
)
543 struct iguanair
*ir
= usb_get_intfdata(intf
);
545 rc_unregister_device(ir
->rc
);
546 usb_set_intfdata(intf
, NULL
);
547 usb_kill_urb(ir
->urb_in
);
548 usb_kill_urb(ir
->urb_out
);
549 usb_free_urb(ir
->urb_in
);
550 usb_free_urb(ir
->urb_out
);
551 usb_free_coherent(ir
->udev
, MAX_IN_PACKET
, ir
->buf_in
, ir
->dma_in
);
552 usb_free_coherent(ir
->udev
, MAX_OUT_PACKET
, ir
->packet
, ir
->dma_out
);
556 static int iguanair_suspend(struct usb_interface
*intf
, pm_message_t message
)
558 struct iguanair
*ir
= usb_get_intfdata(intf
);
561 mutex_lock(&ir
->lock
);
563 if (ir
->receiver_on
) {
564 rc
= iguanair_receiver(ir
, false);
566 dev_warn(ir
->dev
, "failed to disable receiver for suspend\n");
569 usb_kill_urb(ir
->urb_in
);
570 usb_kill_urb(ir
->urb_out
);
572 mutex_unlock(&ir
->lock
);
577 static int iguanair_resume(struct usb_interface
*intf
)
579 struct iguanair
*ir
= usb_get_intfdata(intf
);
582 mutex_lock(&ir
->lock
);
584 rc
= usb_submit_urb(ir
->urb_in
, GFP_KERNEL
);
586 dev_warn(&intf
->dev
, "failed to submit urb: %d\n", rc
);
588 if (ir
->receiver_on
) {
589 rc
= iguanair_receiver(ir
, true);
591 dev_warn(ir
->dev
, "failed to enable receiver after resume\n");
594 mutex_unlock(&ir
->lock
);
599 static const struct usb_device_id iguanair_table
[] = {
600 { USB_DEVICE(0x1781, 0x0938) },
604 static struct usb_driver iguanair_driver
= {
606 .probe
= iguanair_probe
,
607 .disconnect
= iguanair_disconnect
,
608 .suspend
= iguanair_suspend
,
609 .resume
= iguanair_resume
,
610 .reset_resume
= iguanair_resume
,
611 .id_table
= iguanair_table
,
612 .soft_unbind
= 1 /* we want to disable receiver on unbind */
615 module_usb_driver(iguanair_driver
);
617 MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
618 MODULE_AUTHOR("Sean Young <sean@mess.org>");
619 MODULE_LICENSE("GPL");
620 MODULE_DEVICE_TABLE(usb
, iguanair_table
);