1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IguanaWorks USB IR Transceiver support
8 #include <linux/device.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 #include <linux/usb/input.h>
13 #include <linux/slab.h>
14 #include <linux/completion.h>
15 #include <media/rc-core.h>
23 struct usb_device *udev;
27 uint8_t cycle_overhead;
29 /* receiver support */
31 dma_addr_t dma_in, dma_out;
33 struct urb *urb_in, *urb_out;
34 struct completion completion;
36 /* transmit support */
39 struct send_packet *packet;
46 #define CMD_GET_VERSION 0x01
47 #define CMD_GET_BUFSIZE 0x11
48 #define CMD_GET_FEATURES 0x10
50 #define CMD_EXECUTE 0x1f
51 #define CMD_RX_OVERFLOW 0x31
52 #define CMD_TX_OVERFLOW 0x32
53 #define CMD_RECEIVER_ON 0x12
54 #define CMD_RECEIVER_OFF 0x14
59 #define MAX_IN_PACKET 8u
60 #define MAX_OUT_PACKET (sizeof(struct send_packet) + BUF_SIZE)
62 #define RX_RESOLUTION 21
79 static void process_ir_data(struct iguanair *ir, unsigned len)
81 if (len >= 4 && ir->buf_in[0] == 0 && ir->buf_in[1] == 0) {
82 switch (ir->buf_in[3]) {
85 ir->version = (ir->buf_in[5] << 8) |
87 complete(&ir->completion);
92 ir->bufsize = ir->buf_in[4];
93 complete(&ir->completion);
96 case CMD_GET_FEATURES:
98 ir->cycle_overhead = ir->buf_in[5];
99 complete(&ir->completion);
102 case CMD_TX_OVERFLOW:
103 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");
112 ir_raw_event_overflow(ir->rc);
115 dev_warn(ir->dev, "control code %02x received\n",
119 } else if (len >= 7) {
120 struct ir_raw_event rawir = {};
124 for (i = 0; i < 7; i++) {
125 if (ir->buf_in[i] == 0x80) {
127 rawir.duration = 21845;
129 rawir.pulse = (ir->buf_in[i] & 0x80) == 0;
130 rawir.duration = ((ir->buf_in[i] & 0x7f) + 1) *
134 if (ir_raw_event_store_with_filter(ir->rc, &rawir))
139 ir_raw_event_handle(ir->rc);
143 static void iguanair_rx(struct urb *urb)
155 switch (urb->status) {
157 process_ir_data(ir, urb->actual_length);
165 dev_dbg(ir->dev, "Error: urb status = %d\n", urb->status);
169 rc = usb_submit_urb(urb, GFP_ATOMIC);
170 if (rc && rc != -ENODEV)
171 dev_warn(ir->dev, "failed to resubmit urb: %d\n", rc);
174 static void iguanair_irq_out(struct urb *urb)
176 struct iguanair *ir = urb->context;
179 dev_dbg(ir->dev, "Error: out urb status = %d\n", urb->status);
181 /* if we sent an nop packet, do not expect a response */
182 if (urb->status == 0 && ir->packet->header.cmd == CMD_NOP)
183 complete(&ir->completion);
186 static int iguanair_send(struct iguanair *ir, unsigned size)
190 reinit_completion(&ir->completion);
192 ir->urb_out->transfer_buffer_length = size;
193 rc = usb_submit_urb(ir->urb_out, GFP_KERNEL);
197 if (wait_for_completion_timeout(&ir->completion, TIMEOUT) == 0)
203 static int iguanair_get_features(struct iguanair *ir)
208 * On cold boot, the iguanair initializes on the first packet
209 * received but does not process that packet. Send an empty
212 ir->packet->header.start = 0;
213 ir->packet->header.direction = DIR_OUT;
214 ir->packet->header.cmd = CMD_NOP;
215 iguanair_send(ir, sizeof(ir->packet->header));
217 ir->packet->header.cmd = CMD_GET_VERSION;
218 rc = iguanair_send(ir, sizeof(ir->packet->header));
220 dev_info(ir->dev, "failed to get version\n");
224 if (ir->version < 0x205) {
225 dev_err(ir->dev, "firmware 0x%04x is too old\n", ir->version);
231 ir->cycle_overhead = 65;
233 ir->packet->header.cmd = CMD_GET_BUFSIZE;
235 rc = iguanair_send(ir, sizeof(ir->packet->header));
237 dev_info(ir->dev, "failed to get buffer size\n");
241 if (ir->bufsize > BUF_SIZE) {
242 dev_info(ir->dev, "buffer size %u larger than expected\n",
244 ir->bufsize = BUF_SIZE;
247 ir->packet->header.cmd = CMD_GET_FEATURES;
249 rc = iguanair_send(ir, sizeof(ir->packet->header));
251 dev_info(ir->dev, "failed to get features\n");
256 static int iguanair_receiver(struct iguanair *ir, bool enable)
258 ir->packet->header.start = 0;
259 ir->packet->header.direction = DIR_OUT;
260 ir->packet->header.cmd = enable ? CMD_RECEIVER_ON : CMD_RECEIVER_OFF;
262 return iguanair_send(ir, sizeof(ir->packet->header));
266 * The iguanair creates the carrier by busy spinning after each half period.
267 * This is counted in CPU cycles, with the CPU running at 24MHz. It is
268 * broken down into 7-cycles and 4-cyles delays, with a preference for
269 * 4-cycle delays, minus the overhead of the loop itself (cycle_overhead).
271 static int iguanair_set_tx_carrier(struct rc_dev *dev, uint32_t carrier)
273 struct iguanair *ir = dev->priv;
275 if (carrier < 25000 || carrier > 150000)
278 if (carrier != ir->carrier) {
279 uint32_t cycles, fours, sevens;
281 ir->carrier = carrier;
283 cycles = DIV_ROUND_CLOSEST(24000000, carrier * 2) -
287 * Calculate minimum number of 7 cycles needed so
288 * we are left with a multiple of 4; so we want to have
289 * (sevens * 7) & 3 == cycles & 3
291 sevens = (4 - cycles) & 3;
292 fours = (cycles - sevens * 7) / 4;
295 * The firmware interprets these values as a relative offset
296 * for a branch. Immediately following the branches, there
297 * 4 instructions of 7 cycles (2 bytes each) and 110
298 * instructions of 4 cycles (1 byte each). A relative branch
299 * of 0 will execute all of them, branch further for less
302 ir->packet->busy7 = (4 - sevens) * 2;
303 ir->packet->busy4 = 110 - fours;
309 static int iguanair_set_tx_mask(struct rc_dev *dev, uint32_t mask)
311 struct iguanair *ir = dev->priv;
316 ir->packet->channels = mask << 4;
321 static int iguanair_tx(struct rc_dev *dev, unsigned *txbuf, unsigned count)
323 struct iguanair *ir = dev->priv;
324 unsigned int i, size, p, periods;
327 /* convert from us to carrier periods */
328 for (i = size = 0; i < count; i++) {
329 periods = DIV_ROUND_CLOSEST(txbuf[i] * ir->carrier, 1000000);
331 p = min(periods, 127u);
332 if (size >= ir->bufsize) {
336 ir->packet->payload[size++] = p | ((i & 1) ? 0x80 : 0);
341 ir->packet->header.start = 0;
342 ir->packet->header.direction = DIR_OUT;
343 ir->packet->header.cmd = CMD_SEND;
344 ir->packet->length = size;
346 ir->tx_overflow = false;
348 rc = iguanair_send(ir, sizeof(*ir->packet) + size);
350 if (rc == 0 && ir->tx_overflow)
354 return rc ? rc : count;
357 static int iguanair_open(struct rc_dev *rdev)
359 struct iguanair *ir = rdev->priv;
362 rc = iguanair_receiver(ir, true);
364 ir->receiver_on = true;
369 static void iguanair_close(struct rc_dev *rdev)
371 struct iguanair *ir = rdev->priv;
374 rc = iguanair_receiver(ir, false);
375 ir->receiver_on = false;
376 if (rc && rc != -ENODEV)
377 dev_warn(ir->dev, "failed to disable receiver: %d\n", rc);
380 static int iguanair_probe(struct usb_interface *intf,
381 const struct usb_device_id *id)
383 struct usb_device *udev = interface_to_usbdev(intf);
386 int ret, pipein, pipeout;
387 struct usb_host_interface *idesc;
389 idesc = intf->cur_altsetting;
390 if (idesc->desc.bNumEndpoints < 2)
393 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
394 rc = rc_allocate_device(RC_DRIVER_IR_RAW);
400 ir->buf_in = usb_alloc_coherent(udev, MAX_IN_PACKET, GFP_KERNEL,
402 ir->packet = usb_alloc_coherent(udev, MAX_OUT_PACKET, GFP_KERNEL,
404 ir->urb_in = usb_alloc_urb(0, GFP_KERNEL);
405 ir->urb_out = usb_alloc_urb(0, GFP_KERNEL);
407 if (!ir->buf_in || !ir->packet || !ir->urb_in || !ir->urb_out ||
408 !usb_endpoint_is_int_in(&idesc->endpoint[0].desc) ||
409 !usb_endpoint_is_int_out(&idesc->endpoint[1].desc)) {
415 ir->dev = &intf->dev;
418 init_completion(&ir->completion);
419 pipeout = usb_sndintpipe(udev,
420 idesc->endpoint[1].desc.bEndpointAddress);
421 usb_fill_int_urb(ir->urb_out, udev, pipeout, ir->packet, MAX_OUT_PACKET,
422 iguanair_irq_out, ir, 1);
423 ir->urb_out->transfer_dma = ir->dma_out;
424 ir->urb_out->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
426 pipein = usb_rcvintpipe(udev, idesc->endpoint[0].desc.bEndpointAddress);
427 usb_fill_int_urb(ir->urb_in, udev, pipein, ir->buf_in, MAX_IN_PACKET,
429 ir->urb_in->transfer_dma = ir->dma_in;
430 ir->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
432 ret = usb_submit_urb(ir->urb_in, GFP_KERNEL);
434 dev_warn(&intf->dev, "failed to submit urb: %d\n", ret);
438 ret = iguanair_get_features(ir);
442 snprintf(ir->name, sizeof(ir->name),
443 "IguanaWorks USB IR Transceiver version 0x%04x", ir->version);
445 usb_make_path(ir->udev, ir->phys, sizeof(ir->phys));
447 rc->device_name = ir->name;
448 rc->input_phys = ir->phys;
449 usb_to_input_id(ir->udev, &rc->input_id);
450 rc->dev.parent = &intf->dev;
451 rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
453 rc->open = iguanair_open;
454 rc->close = iguanair_close;
455 rc->s_tx_mask = iguanair_set_tx_mask;
456 rc->s_tx_carrier = iguanair_set_tx_carrier;
457 rc->tx_ir = iguanair_tx;
458 rc->driver_name = KBUILD_MODNAME;
459 rc->map_name = RC_MAP_RC6_MCE;
461 rc->timeout = IR_DEFAULT_TIMEOUT;
462 rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
463 rc->rx_resolution = RX_RESOLUTION;
465 iguanair_set_tx_carrier(rc, 38000);
466 iguanair_set_tx_mask(rc, 0);
468 ret = rc_register_device(rc);
470 dev_err(&intf->dev, "failed to register rc device %d", ret);
474 usb_set_intfdata(intf, ir);
478 usb_kill_urb(ir->urb_in);
479 usb_kill_urb(ir->urb_out);
482 usb_free_urb(ir->urb_in);
483 usb_free_urb(ir->urb_out);
484 usb_free_coherent(udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in);
485 usb_free_coherent(udev, MAX_OUT_PACKET, ir->packet,
493 static void iguanair_disconnect(struct usb_interface *intf)
495 struct iguanair *ir = usb_get_intfdata(intf);
497 rc_unregister_device(ir->rc);
498 usb_set_intfdata(intf, NULL);
499 usb_kill_urb(ir->urb_in);
500 usb_kill_urb(ir->urb_out);
501 usb_free_urb(ir->urb_in);
502 usb_free_urb(ir->urb_out);
503 usb_free_coherent(ir->udev, MAX_IN_PACKET, ir->buf_in, ir->dma_in);
504 usb_free_coherent(ir->udev, MAX_OUT_PACKET, ir->packet, ir->dma_out);
508 static int iguanair_suspend(struct usb_interface *intf, pm_message_t message)
510 struct iguanair *ir = usb_get_intfdata(intf);
513 if (ir->receiver_on) {
514 rc = iguanair_receiver(ir, false);
516 dev_warn(ir->dev, "failed to disable receiver for suspend\n");
519 usb_kill_urb(ir->urb_in);
520 usb_kill_urb(ir->urb_out);
525 static int iguanair_resume(struct usb_interface *intf)
527 struct iguanair *ir = usb_get_intfdata(intf);
530 rc = usb_submit_urb(ir->urb_in, GFP_KERNEL);
532 dev_warn(&intf->dev, "failed to submit urb: %d\n", rc);
534 if (ir->receiver_on) {
535 rc = iguanair_receiver(ir, true);
537 dev_warn(ir->dev, "failed to enable receiver after resume\n");
543 static const struct usb_device_id iguanair_table[] = {
544 { USB_DEVICE(0x1781, 0x0938) },
548 static struct usb_driver iguanair_driver = {
549 .name = KBUILD_MODNAME,
550 .probe = iguanair_probe,
551 .disconnect = iguanair_disconnect,
552 .suspend = iguanair_suspend,
553 .resume = iguanair_resume,
554 .reset_resume = iguanair_resume,
555 .id_table = iguanair_table,
556 .soft_unbind = 1 /* we want to disable receiver on unbind */
559 module_usb_driver(iguanair_driver);
561 MODULE_DESCRIPTION("IguanaWorks USB IR Transceiver");
563 MODULE_LICENSE("GPL");
564 MODULE_DEVICE_TABLE(usb, iguanair_table);