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
;
38 int pipe_in
, pipe_out
;
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
85 struct response_packet
{
99 static void process_ir_data(struct iguanair
*ir
, unsigned len
)
101 if (len
>= 4 && ir
->buf_in
[0] == 0 && ir
->buf_in
[1] == 0) {
102 switch (ir
->buf_in
[3]) {
103 case CMD_TX_OVERFLOW
:
104 ir
->tx_overflow
= true;
105 case CMD_RECEIVER_OFF
:
106 case CMD_RECEIVER_ON
:
108 complete(&ir
->completion
);
110 case CMD_RX_OVERFLOW
:
111 dev_warn(ir
->dev
, "receive overflow\n");
114 dev_warn(ir
->dev
, "control code %02x received\n",
118 } else if (len
>= 7) {
119 DEFINE_IR_RAW_EVENT(rawir
);
122 init_ir_raw_event(&rawir
);
124 for (i
= 0; i
< 7; i
++) {
125 if (ir
->buf_in
[i
] == 0x80) {
127 rawir
.duration
= US_TO_NS(21845);
129 rawir
.pulse
= (ir
->buf_in
[i
] & 0x80) == 0;
130 rawir
.duration
= ((ir
->buf_in
[i
] & 0x7f) + 1) *
134 ir_raw_event_store_with_filter(ir
->rc
, &rawir
);
137 ir_raw_event_handle(ir
->rc
);
141 static void iguanair_rx(struct urb
*urb
)
154 switch (urb
->status
) {
156 process_ir_data(ir
, urb
->actual_length
);
165 dev_dbg(ir
->dev
, "Error: urb status = %d\n", urb
->status
);
169 usb_submit_urb(urb
, GFP_ATOMIC
);
172 static int iguanair_send(struct iguanair
*ir
, void *data
, unsigned size
,
173 struct response_packet
*response
, unsigned *res_len
)
175 unsigned offset
, len
;
178 for (offset
= 0; offset
< size
; offset
+= MAX_PACKET_SIZE
) {
179 len
= min(size
- offset
, MAX_PACKET_SIZE
);
184 rc
= usb_interrupt_msg(ir
->udev
, ir
->pipe_out
, data
+ offset
,
185 len
, &transferred
, TIMEOUT
);
189 if (transferred
!= len
)
194 rc
= usb_interrupt_msg(ir
->udev
, ir
->pipe_in
, response
,
195 sizeof(*response
), res_len
, TIMEOUT
);
201 static int iguanair_get_features(struct iguanair
*ir
)
203 struct packet packet
;
204 struct response_packet response
;
208 packet
.direction
= DIR_OUT
;
209 packet
.cmd
= CMD_GET_VERSION
;
211 rc
= iguanair_send(ir
, &packet
, sizeof(packet
), &response
, &len
);
213 dev_info(ir
->dev
, "failed to get version\n");
218 dev_info(ir
->dev
, "failed to get version\n");
223 ir
->version
[0] = response
.data
[0];
224 ir
->version
[1] = response
.data
[1];
226 ir
->cycle_overhead
= 65;
228 packet
.cmd
= CMD_GET_BUFSIZE
;
230 rc
= iguanair_send(ir
, &packet
, sizeof(packet
), &response
, &len
);
232 dev_info(ir
->dev
, "failed to get buffer size\n");
237 dev_info(ir
->dev
, "failed to get buffer size\n");
242 ir
->bufsize
= response
.data
[0];
244 if (ir
->version
[0] == 0 || ir
->version
[1] == 0)
247 packet
.cmd
= CMD_GET_FEATURES
;
249 rc
= iguanair_send(ir
, &packet
, sizeof(packet
), &response
, &len
);
251 dev_info(ir
->dev
, "failed to get features\n");
256 dev_info(ir
->dev
, "failed to get features\n");
261 if (len
> 5 && ir
->version
[0] >= 4)
262 ir
->cycle_overhead
= response
.data
[1];
268 static int iguanair_receiver(struct iguanair
*ir
, bool enable
)
270 struct packet packet
= { 0, DIR_OUT
, enable
?
271 CMD_RECEIVER_ON
: CMD_RECEIVER_OFF
};
274 INIT_COMPLETION(ir
->completion
);
276 rc
= iguanair_send(ir
, &packet
, sizeof(packet
), NULL
, NULL
);
280 wait_for_completion_timeout(&ir
->completion
, TIMEOUT
);
286 * The iguana ir creates the carrier by busy spinning after each pulse or
287 * space. This is counted in CPU cycles, with the CPU running at 24MHz. It is
288 * broken down into 7-cycles and 4-cyles delays, with a preference for
291 static int iguanair_set_tx_carrier(struct rc_dev
*dev
, uint32_t carrier
)
293 struct iguanair
*ir
= dev
->priv
;
295 if (carrier
< 25000 || carrier
> 150000)
298 mutex_lock(&ir
->lock
);
300 if (carrier
!= ir
->carrier
) {
301 uint32_t cycles
, fours
, sevens
;
303 ir
->carrier
= carrier
;
305 cycles
= DIV_ROUND_CLOSEST(24000000, carrier
* 2) -
308 /* make up the the remainer of 4-cycle blocks */
309 switch (cycles
& 3) {
324 fours
= (cycles
- sevens
* 7) / 4;
326 /* magic happens here */
327 ir
->busy7
= (4 - sevens
) * 2;
328 ir
->busy4
= 110 - fours
;
331 mutex_unlock(&ir
->lock
);
336 static int iguanair_set_tx_mask(struct rc_dev
*dev
, uint32_t mask
)
338 struct iguanair
*ir
= dev
->priv
;
343 mutex_lock(&ir
->lock
);
345 mutex_unlock(&ir
->lock
);
350 static int iguanair_tx(struct rc_dev
*dev
, unsigned *txbuf
, unsigned count
)
352 struct iguanair
*ir
= dev
->priv
;
353 uint8_t space
, *payload
;
354 unsigned i
, size
, rc
;
355 struct send_packet
*packet
;
357 mutex_lock(&ir
->lock
);
359 /* convert from us to carrier periods */
360 for (i
= size
= 0; i
< count
; i
++) {
361 txbuf
[i
] = DIV_ROUND_CLOSEST(txbuf
[i
] * ir
->carrier
, 1000000);
362 size
+= (txbuf
[i
] + 126) / 127;
365 packet
= kmalloc(sizeof(*packet
) + size
, GFP_KERNEL
);
371 if (size
> ir
->bufsize
) {
376 packet
->header
.start
= 0;
377 packet
->header
.direction
= DIR_OUT
;
378 packet
->header
.cmd
= CMD_SEND
;
379 packet
->length
= size
;
380 packet
->channels
= ir
->channels
<< 4;
381 packet
->busy7
= ir
->busy7
;
382 packet
->busy4
= ir
->busy4
;
385 payload
= packet
->payload
;
387 for (i
= 0; i
< count
; i
++) {
388 unsigned periods
= txbuf
[i
];
390 while (periods
> 127) {
391 *payload
++ = 127 | space
;
395 *payload
++ = periods
| space
;
399 if (ir
->receiver_on
) {
400 rc
= iguanair_receiver(ir
, false);
402 dev_warn(ir
->dev
, "disable receiver before transmit failed\n");
407 ir
->tx_overflow
= false;
409 INIT_COMPLETION(ir
->completion
);
411 rc
= iguanair_send(ir
, packet
, size
+ 8, NULL
, NULL
);
414 wait_for_completion_timeout(&ir
->completion
, TIMEOUT
);
419 ir
->tx_overflow
= false;
421 if (ir
->receiver_on
) {
422 if (iguanair_receiver(ir
, true))
423 dev_warn(ir
->dev
, "re-enable receiver after transmit failed\n");
427 mutex_unlock(&ir
->lock
);
433 static int iguanair_open(struct rc_dev
*rdev
)
435 struct iguanair
*ir
= rdev
->priv
;
438 mutex_lock(&ir
->lock
);
440 usb_submit_urb(ir
->urb_in
, GFP_KERNEL
);
442 BUG_ON(ir
->receiver_on
);
444 rc
= iguanair_receiver(ir
, true);
446 ir
->receiver_on
= true;
448 mutex_unlock(&ir
->lock
);
453 static void iguanair_close(struct rc_dev
*rdev
)
455 struct iguanair
*ir
= rdev
->priv
;
458 mutex_lock(&ir
->lock
);
460 rc
= iguanair_receiver(ir
, false);
461 ir
->receiver_on
= false;
463 dev_warn(ir
->dev
, "failed to disable receiver: %d\n", rc
);
465 usb_kill_urb(ir
->urb_in
);
467 mutex_unlock(&ir
->lock
);
470 static int __devinit
iguanair_probe(struct usb_interface
*intf
,
471 const struct usb_device_id
*id
)
473 struct usb_device
*udev
= interface_to_usbdev(intf
);
477 struct usb_host_interface
*idesc
;
479 ir
= kzalloc(sizeof(*ir
), GFP_KERNEL
);
480 rc
= rc_allocate_device();
486 ir
->buf_in
= usb_alloc_coherent(udev
, MAX_PACKET_SIZE
, GFP_ATOMIC
,
488 ir
->urb_in
= usb_alloc_urb(0, GFP_KERNEL
);
490 if (!ir
->buf_in
|| !ir
->urb_in
) {
495 idesc
= intf
->altsetting
;
497 if (idesc
->desc
.bNumEndpoints
< 2) {
503 ir
->dev
= &intf
->dev
;
505 ir
->pipe_in
= usb_rcvintpipe(udev
,
506 idesc
->endpoint
[0].desc
.bEndpointAddress
);
507 ir
->pipe_out
= usb_sndintpipe(udev
,
508 idesc
->endpoint
[1].desc
.bEndpointAddress
);
509 mutex_init(&ir
->lock
);
510 init_completion(&ir
->completion
);
512 ret
= iguanair_get_features(ir
);
514 dev_warn(&intf
->dev
, "failed to get device features");
518 usb_fill_int_urb(ir
->urb_in
, ir
->udev
, ir
->pipe_in
, ir
->buf_in
,
519 MAX_PACKET_SIZE
, iguanair_rx
, ir
,
520 idesc
->endpoint
[0].desc
.bInterval
);
521 ir
->urb_in
->transfer_dma
= ir
->dma_in
;
522 ir
->urb_in
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
524 snprintf(ir
->name
, sizeof(ir
->name
),
525 "IguanaWorks USB IR Transceiver version %d.%d",
526 ir
->version
[0], ir
->version
[1]);
528 usb_make_path(ir
->udev
, ir
->phys
, sizeof(ir
->phys
));
530 rc
->input_name
= ir
->name
;
531 rc
->input_phys
= ir
->phys
;
532 usb_to_input_id(ir
->udev
, &rc
->input_id
);
533 rc
->dev
.parent
= &intf
->dev
;
534 rc
->driver_type
= RC_DRIVER_IR_RAW
;
535 rc
->allowed_protos
= RC_TYPE_ALL
;
537 rc
->open
= iguanair_open
;
538 rc
->close
= iguanair_close
;
539 rc
->s_tx_mask
= iguanair_set_tx_mask
;
540 rc
->s_tx_carrier
= iguanair_set_tx_carrier
;
541 rc
->tx_ir
= iguanair_tx
;
542 rc
->driver_name
= DRIVER_NAME
;
543 rc
->map_name
= RC_MAP_EMPTY
;
545 iguanair_set_tx_carrier(rc
, 38000);
547 ret
= rc_register_device(rc
);
549 dev_err(&intf
->dev
, "failed to register rc device %d", ret
);
553 usb_set_intfdata(intf
, ir
);
555 dev_info(&intf
->dev
, "Registered %s", ir
->name
);
560 usb_free_urb(ir
->urb_in
);
561 usb_free_coherent(udev
, MAX_PACKET_SIZE
, ir
->buf_in
,
569 static void __devexit
iguanair_disconnect(struct usb_interface
*intf
)
571 struct iguanair
*ir
= usb_get_intfdata(intf
);
573 usb_set_intfdata(intf
, NULL
);
575 usb_kill_urb(ir
->urb_in
);
576 usb_free_urb(ir
->urb_in
);
577 usb_free_coherent(ir
->udev
, MAX_PACKET_SIZE
, ir
->buf_in
, ir
->dma_in
);
578 rc_unregister_device(ir
->rc
);
582 static int iguanair_suspend(struct usb_interface
*intf
, pm_message_t message
)
584 struct iguanair
*ir
= usb_get_intfdata(intf
);
587 mutex_lock(&ir
->lock
);
589 if (ir
->receiver_on
) {
590 rc
= iguanair_receiver(ir
, false);
592 dev_warn(ir
->dev
, "failed to disable receiver for suspend\n");
595 mutex_unlock(&ir
->lock
);
600 static int iguanair_resume(struct usb_interface
*intf
)
602 struct iguanair
*ir
= usb_get_intfdata(intf
);
605 mutex_lock(&ir
->lock
);
607 if (ir
->receiver_on
) {
608 rc
= iguanair_receiver(ir
, true);
610 dev_warn(ir
->dev
, "failed to enable receiver after resume\n");
613 mutex_unlock(&ir
->lock
);
618 static const struct usb_device_id iguanair_table
[] = {
619 { USB_DEVICE(0x1781, 0x0938) },
623 static struct usb_driver iguanair_driver
= {
625 .probe
= iguanair_probe
,
626 .disconnect
= __devexit_p(iguanair_disconnect
),
627 .suspend
= iguanair_suspend
,
628 .resume
= iguanair_resume
,
629 .reset_resume
= iguanair_resume
,
630 .id_table
= iguanair_table
633 module_usb_driver(iguanair_driver
);
635 MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
636 MODULE_AUTHOR("Sean Young <sean@mess.org>");
637 MODULE_LICENSE("GPL");
638 MODULE_DEVICE_TABLE(usb
, iguanair_table
);