1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2b1c97193SSean Young /* 3b1c97193SSean Young * IgorPlug-USB IR Receiver 4b1c97193SSean Young * 5b1c97193SSean Young * Copyright (C) 2014 Sean Young <sean@mess.org> 6b1c97193SSean Young * 7b1c97193SSean Young * Supports the standard homebrew IgorPlugUSB receiver with Igor's firmware. 8b1c97193SSean Young * See http://www.cesko.host.sk/IgorPlugUSB/IgorPlug-USB%20(AVR)_eng.htm 9b1c97193SSean Young * 10b1c97193SSean Young * Based on the lirc_igorplugusb.c driver: 11b1c97193SSean Young * Copyright (C) 2004 Jan M. Hochstein 12b1c97193SSean Young * <hochstein@algo.informatik.tu-darmstadt.de> 13b1c97193SSean Young */ 14b1c97193SSean Young #include <linux/device.h> 15b1c97193SSean Young #include <linux/kernel.h> 16b1c97193SSean Young #include <linux/module.h> 17b1c97193SSean Young #include <linux/usb.h> 18b1c97193SSean Young #include <linux/usb/input.h> 19b1c97193SSean Young #include <media/rc-core.h> 20b1c97193SSean Young 21b1c97193SSean Young #define DRIVER_DESC "IgorPlug-USB IR Receiver" 22b1c97193SSean Young #define DRIVER_NAME "igorplugusb" 23b1c97193SSean Young 24b1c97193SSean Young #define HEADERLEN 3 25b1c97193SSean Young #define BUFLEN 36 26b1c97193SSean Young #define MAX_PACKET (HEADERLEN + BUFLEN) 27b1c97193SSean Young 28b1c97193SSean Young #define SET_INFRABUFFER_EMPTY 1 29b1c97193SSean Young #define GET_INFRACODE 2 30b1c97193SSean Young 31b1c97193SSean Young 32b1c97193SSean Young struct igorplugusb { 33b1c97193SSean Young struct rc_dev *rc; 34b1c97193SSean Young struct device *dev; 35b1c97193SSean Young 36b1c97193SSean Young struct urb *urb; 37b1c97193SSean Young struct usb_ctrlrequest request; 38b1c97193SSean Young 39b1c97193SSean Young struct timer_list timer; 40b1c97193SSean Young 41b1c97193SSean Young uint8_t buf_in[MAX_PACKET]; 42b1c97193SSean Young 43b1c97193SSean Young char phys[64]; 44b1c97193SSean Young }; 45b1c97193SSean Young 46b1c97193SSean Young static void igorplugusb_cmd(struct igorplugusb *ir, int cmd); 47b1c97193SSean Young 48b1c97193SSean Young static void igorplugusb_irdata(struct igorplugusb *ir, unsigned len) 49b1c97193SSean Young { 50183e19f5SSean Young struct ir_raw_event rawir = {}; 51b1c97193SSean Young unsigned i, start, overflow; 52b1c97193SSean Young 53b1c97193SSean Young dev_dbg(ir->dev, "irdata: %*ph (len=%u)", len, ir->buf_in, len); 54b1c97193SSean Young 55b1c97193SSean Young /* 56b1c97193SSean Young * If more than 36 pulses and spaces follow each other, the igorplugusb 57b1c97193SSean Young * overwrites its buffer from the beginning. The overflow value is the 58b1c97193SSean Young * last offset which was not overwritten. Everything from this offset 59b1c97193SSean Young * onwards occurred before everything until this offset. 60b1c97193SSean Young */ 61b1c97193SSean Young overflow = ir->buf_in[2]; 62b1c97193SSean Young i = start = overflow + HEADERLEN; 63b1c97193SSean Young 64b1c97193SSean Young if (start >= len) { 65b1c97193SSean Young dev_err(ir->dev, "receive overflow invalid: %u", overflow); 66b1c97193SSean Young } else { 67*8fede658SSean Young if (overflow > 0) { 68b1c97193SSean Young dev_warn(ir->dev, "receive overflow, at least %u lost", 69b1c97193SSean Young overflow); 70*8fede658SSean Young ir_raw_event_reset(ir->rc); 71*8fede658SSean Young } 72b1c97193SSean Young 73b1c97193SSean Young do { 74528222d8SSean Young rawir.duration = ir->buf_in[i] * 85; 75b1c97193SSean Young rawir.pulse = i & 1; 76b1c97193SSean Young 77b1c97193SSean Young ir_raw_event_store_with_filter(ir->rc, &rawir); 78b1c97193SSean Young 79b1c97193SSean Young if (++i == len) 80b1c97193SSean Young i = HEADERLEN; 81b1c97193SSean Young } while (i != start); 82b1c97193SSean Young 83b1c97193SSean Young /* add a trailing space */ 84b1c97193SSean Young rawir.duration = ir->rc->timeout; 85b1c97193SSean Young rawir.pulse = false; 86b1c97193SSean Young ir_raw_event_store_with_filter(ir->rc, &rawir); 87b1c97193SSean Young 88b1c97193SSean Young ir_raw_event_handle(ir->rc); 89b1c97193SSean Young } 90b1c97193SSean Young 91b1c97193SSean Young igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY); 92b1c97193SSean Young } 93b1c97193SSean Young 94b1c97193SSean Young static void igorplugusb_callback(struct urb *urb) 95b1c97193SSean Young { 96b1c97193SSean Young struct usb_ctrlrequest *req; 97b1c97193SSean Young struct igorplugusb *ir = urb->context; 98b1c97193SSean Young 99b1c97193SSean Young req = (struct usb_ctrlrequest *)urb->setup_packet; 100b1c97193SSean Young 101b1c97193SSean Young switch (urb->status) { 102b1c97193SSean Young case 0: 103b1c97193SSean Young if (req->bRequest == GET_INFRACODE && 104b1c97193SSean Young urb->actual_length > HEADERLEN) 105b1c97193SSean Young igorplugusb_irdata(ir, urb->actual_length); 106b1c97193SSean Young else /* request IR */ 107b1c97193SSean Young mod_timer(&ir->timer, jiffies + msecs_to_jiffies(50)); 108b1c97193SSean Young break; 109b1c97193SSean Young case -EPROTO: 110b1c97193SSean Young case -ECONNRESET: 111b1c97193SSean Young case -ENOENT: 112b1c97193SSean Young case -ESHUTDOWN: 113b1c97193SSean Young usb_unlink_urb(urb); 114b1c97193SSean Young return; 115b1c97193SSean Young default: 116b1c97193SSean Young dev_warn(ir->dev, "Error: urb status = %d\n", urb->status); 117b1c97193SSean Young igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY); 118b1c97193SSean Young break; 119b1c97193SSean Young } 120b1c97193SSean Young } 121b1c97193SSean Young 122b1c97193SSean Young static void igorplugusb_cmd(struct igorplugusb *ir, int cmd) 123b1c97193SSean Young { 124b1c97193SSean Young int ret; 125b1c97193SSean Young 126b1c97193SSean Young ir->request.bRequest = cmd; 127b1c97193SSean Young ir->urb->transfer_flags = 0; 128b1c97193SSean Young ret = usb_submit_urb(ir->urb, GFP_ATOMIC); 129b1c97193SSean Young if (ret) 130b1c97193SSean Young dev_err(ir->dev, "submit urb failed: %d", ret); 131b1c97193SSean Young } 132b1c97193SSean Young 133b17ec78aSKees Cook static void igorplugusb_timer(struct timer_list *t) 134b1c97193SSean Young { 135b17ec78aSKees Cook struct igorplugusb *ir = from_timer(ir, t, timer); 136b1c97193SSean Young 137b1c97193SSean Young igorplugusb_cmd(ir, GET_INFRACODE); 138b1c97193SSean Young } 139b1c97193SSean Young 140b1c97193SSean Young static int igorplugusb_probe(struct usb_interface *intf, 141b1c97193SSean Young const struct usb_device_id *id) 142b1c97193SSean Young { 143b1c97193SSean Young struct usb_device *udev; 144b1c97193SSean Young struct usb_host_interface *idesc; 145b1c97193SSean Young struct usb_endpoint_descriptor *ep; 146b1c97193SSean Young struct igorplugusb *ir; 147b1c97193SSean Young struct rc_dev *rc; 148a40973ffSSean Young int ret = -ENOMEM; 149b1c97193SSean Young 150b1c97193SSean Young udev = interface_to_usbdev(intf); 151b1c97193SSean Young idesc = intf->cur_altsetting; 152b1c97193SSean Young 153b1c97193SSean Young if (idesc->desc.bNumEndpoints != 1) { 154b1c97193SSean Young dev_err(&intf->dev, "incorrect number of endpoints"); 155b1c97193SSean Young return -ENODEV; 156b1c97193SSean Young } 157b1c97193SSean Young 158b1c97193SSean Young ep = &idesc->endpoint[0].desc; 159b1c97193SSean Young if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_control(ep)) { 160b1c97193SSean Young dev_err(&intf->dev, "endpoint incorrect"); 161b1c97193SSean Young return -ENODEV; 162b1c97193SSean Young } 163b1c97193SSean Young 164b1c97193SSean Young ir = devm_kzalloc(&intf->dev, sizeof(*ir), GFP_KERNEL); 165b1c97193SSean Young if (!ir) 166b1c97193SSean Young return -ENOMEM; 167b1c97193SSean Young 168b1c97193SSean Young ir->dev = &intf->dev; 169b1c97193SSean Young 170b17ec78aSKees Cook timer_setup(&ir->timer, igorplugusb_timer, 0); 171b1c97193SSean Young 172b1c97193SSean Young ir->request.bRequest = GET_INFRACODE; 173b1c97193SSean Young ir->request.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN; 174b1c97193SSean Young ir->request.wLength = cpu_to_le16(sizeof(ir->buf_in)); 175b1c97193SSean Young 176b1c97193SSean Young ir->urb = usb_alloc_urb(0, GFP_KERNEL); 177b1c97193SSean Young if (!ir->urb) 178a40973ffSSean Young goto fail; 179b1c97193SSean Young 180b1c97193SSean Young usb_fill_control_urb(ir->urb, udev, 181b1c97193SSean Young usb_rcvctrlpipe(udev, 0), (uint8_t *)&ir->request, 182b1c97193SSean Young ir->buf_in, sizeof(ir->buf_in), igorplugusb_callback, ir); 183b1c97193SSean Young 184b1c97193SSean Young usb_make_path(udev, ir->phys, sizeof(ir->phys)); 185b1c97193SSean Young 1860f7499fdSAndi Shyti rc = rc_allocate_device(RC_DRIVER_IR_RAW); 187a40973ffSSean Young if (!rc) 188a40973ffSSean Young goto fail; 189a40973ffSSean Young 190518f4b26SSean Young rc->device_name = DRIVER_DESC; 191b1c97193SSean Young rc->input_phys = ir->phys; 192b1c97193SSean Young usb_to_input_id(udev, &rc->input_id); 193b1c97193SSean Young rc->dev.parent = &intf->dev; 194b1c97193SSean Young /* 195b1c97193SSean Young * This device can only store 36 pulses + spaces, which is not enough 196b1c97193SSean Young * for the NEC protocol and many others. 197b1c97193SSean Young */ 1986d741bfeSSean Young rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER & 1996d741bfeSSean Young ~(RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX | RC_PROTO_BIT_NEC32 | 2006d741bfeSSean Young RC_PROTO_BIT_RC6_6A_20 | RC_PROTO_BIT_RC6_6A_24 | 2016d741bfeSSean Young RC_PROTO_BIT_RC6_6A_32 | RC_PROTO_BIT_RC6_MCE | 2026d741bfeSSean Young RC_PROTO_BIT_SONY20 | RC_PROTO_BIT_SANYO); 203b1c97193SSean Young 204b1c97193SSean Young rc->priv = ir; 205b1c97193SSean Young rc->driver_name = DRIVER_NAME; 206b1c97193SSean Young rc->map_name = RC_MAP_HAUPPAUGE; 207528222d8SSean Young rc->timeout = MS_TO_US(100); 208528222d8SSean Young rc->rx_resolution = 85; 209b1c97193SSean Young 210b1c97193SSean Young ir->rc = rc; 211b1c97193SSean Young ret = rc_register_device(rc); 212b1c97193SSean Young if (ret) { 213b1c97193SSean Young dev_err(&intf->dev, "failed to register rc device: %d", ret); 214a40973ffSSean Young goto fail; 215b1c97193SSean Young } 216b1c97193SSean Young 217b1c97193SSean Young usb_set_intfdata(intf, ir); 218b1c97193SSean Young 219b1c97193SSean Young igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY); 220b1c97193SSean Young 221b1c97193SSean Young return 0; 222a40973ffSSean Young fail: 223a40973ffSSean Young rc_free_device(ir->rc); 224a40973ffSSean Young usb_free_urb(ir->urb); 225a40973ffSSean Young del_timer(&ir->timer); 226a40973ffSSean Young 227a40973ffSSean Young return ret; 228b1c97193SSean Young } 229b1c97193SSean Young 230b1c97193SSean Young static void igorplugusb_disconnect(struct usb_interface *intf) 231b1c97193SSean Young { 232b1c97193SSean Young struct igorplugusb *ir = usb_get_intfdata(intf); 233b1c97193SSean Young 234b1c97193SSean Young rc_unregister_device(ir->rc); 235b1c97193SSean Young del_timer_sync(&ir->timer); 236b1c97193SSean Young usb_set_intfdata(intf, NULL); 237b1c97193SSean Young usb_kill_urb(ir->urb); 238b1c97193SSean Young usb_free_urb(ir->urb); 239b1c97193SSean Young } 240b1c97193SSean Young 2415fad16b5SArvind Yadav static const struct usb_device_id igorplugusb_table[] = { 242b1c97193SSean Young /* Igor Plug USB (Atmel's Manufact. ID) */ 243b1c97193SSean Young { USB_DEVICE(0x03eb, 0x0002) }, 244b1c97193SSean Young /* Fit PC2 Infrared Adapter */ 245b1c97193SSean Young { USB_DEVICE(0x03eb, 0x21fe) }, 246b1c97193SSean Young /* Terminating entry */ 247b1c97193SSean Young { } 248b1c97193SSean Young }; 249b1c97193SSean Young 250b1c97193SSean Young static struct usb_driver igorplugusb_driver = { 251b1c97193SSean Young .name = DRIVER_NAME, 252b1c97193SSean Young .probe = igorplugusb_probe, 253b1c97193SSean Young .disconnect = igorplugusb_disconnect, 254b1c97193SSean Young .id_table = igorplugusb_table 255b1c97193SSean Young }; 256b1c97193SSean Young 257b1c97193SSean Young module_usb_driver(igorplugusb_driver); 258b1c97193SSean Young 259b1c97193SSean Young MODULE_DESCRIPTION(DRIVER_DESC); 260b1c97193SSean Young MODULE_AUTHOR("Sean Young <sean@mess.org>"); 261b1c97193SSean Young MODULE_LICENSE("GPL"); 262b1c97193SSean Young MODULE_DEVICE_TABLE(usb, igorplugusb_table); 263