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 { 67b1c97193SSean Young if (overflow > 0) 68b1c97193SSean Young dev_warn(ir->dev, "receive overflow, at least %u lost", 69b1c97193SSean Young overflow); 70b1c97193SSean Young 71b1c97193SSean Young do { 72*528222d8SSean Young rawir.duration = ir->buf_in[i] * 85; 73b1c97193SSean Young rawir.pulse = i & 1; 74b1c97193SSean Young 75b1c97193SSean Young ir_raw_event_store_with_filter(ir->rc, &rawir); 76b1c97193SSean Young 77b1c97193SSean Young if (++i == len) 78b1c97193SSean Young i = HEADERLEN; 79b1c97193SSean Young } while (i != start); 80b1c97193SSean Young 81b1c97193SSean Young /* add a trailing space */ 82b1c97193SSean Young rawir.duration = ir->rc->timeout; 83b1c97193SSean Young rawir.pulse = false; 84b1c97193SSean Young ir_raw_event_store_with_filter(ir->rc, &rawir); 85b1c97193SSean Young 86b1c97193SSean Young ir_raw_event_handle(ir->rc); 87b1c97193SSean Young } 88b1c97193SSean Young 89b1c97193SSean Young igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY); 90b1c97193SSean Young } 91b1c97193SSean Young 92b1c97193SSean Young static void igorplugusb_callback(struct urb *urb) 93b1c97193SSean Young { 94b1c97193SSean Young struct usb_ctrlrequest *req; 95b1c97193SSean Young struct igorplugusb *ir = urb->context; 96b1c97193SSean Young 97b1c97193SSean Young req = (struct usb_ctrlrequest *)urb->setup_packet; 98b1c97193SSean Young 99b1c97193SSean Young switch (urb->status) { 100b1c97193SSean Young case 0: 101b1c97193SSean Young if (req->bRequest == GET_INFRACODE && 102b1c97193SSean Young urb->actual_length > HEADERLEN) 103b1c97193SSean Young igorplugusb_irdata(ir, urb->actual_length); 104b1c97193SSean Young else /* request IR */ 105b1c97193SSean Young mod_timer(&ir->timer, jiffies + msecs_to_jiffies(50)); 106b1c97193SSean Young break; 107b1c97193SSean Young case -EPROTO: 108b1c97193SSean Young case -ECONNRESET: 109b1c97193SSean Young case -ENOENT: 110b1c97193SSean Young case -ESHUTDOWN: 111b1c97193SSean Young usb_unlink_urb(urb); 112b1c97193SSean Young return; 113b1c97193SSean Young default: 114b1c97193SSean Young dev_warn(ir->dev, "Error: urb status = %d\n", urb->status); 115b1c97193SSean Young igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY); 116b1c97193SSean Young break; 117b1c97193SSean Young } 118b1c97193SSean Young } 119b1c97193SSean Young 120b1c97193SSean Young static void igorplugusb_cmd(struct igorplugusb *ir, int cmd) 121b1c97193SSean Young { 122b1c97193SSean Young int ret; 123b1c97193SSean Young 124b1c97193SSean Young ir->request.bRequest = cmd; 125b1c97193SSean Young ir->urb->transfer_flags = 0; 126b1c97193SSean Young ret = usb_submit_urb(ir->urb, GFP_ATOMIC); 127b1c97193SSean Young if (ret) 128b1c97193SSean Young dev_err(ir->dev, "submit urb failed: %d", ret); 129b1c97193SSean Young } 130b1c97193SSean Young 131b17ec78aSKees Cook static void igorplugusb_timer(struct timer_list *t) 132b1c97193SSean Young { 133b17ec78aSKees Cook struct igorplugusb *ir = from_timer(ir, t, timer); 134b1c97193SSean Young 135b1c97193SSean Young igorplugusb_cmd(ir, GET_INFRACODE); 136b1c97193SSean Young } 137b1c97193SSean Young 138b1c97193SSean Young static int igorplugusb_probe(struct usb_interface *intf, 139b1c97193SSean Young const struct usb_device_id *id) 140b1c97193SSean Young { 141b1c97193SSean Young struct usb_device *udev; 142b1c97193SSean Young struct usb_host_interface *idesc; 143b1c97193SSean Young struct usb_endpoint_descriptor *ep; 144b1c97193SSean Young struct igorplugusb *ir; 145b1c97193SSean Young struct rc_dev *rc; 146a40973ffSSean Young int ret = -ENOMEM; 147b1c97193SSean Young 148b1c97193SSean Young udev = interface_to_usbdev(intf); 149b1c97193SSean Young idesc = intf->cur_altsetting; 150b1c97193SSean Young 151b1c97193SSean Young if (idesc->desc.bNumEndpoints != 1) { 152b1c97193SSean Young dev_err(&intf->dev, "incorrect number of endpoints"); 153b1c97193SSean Young return -ENODEV; 154b1c97193SSean Young } 155b1c97193SSean Young 156b1c97193SSean Young ep = &idesc->endpoint[0].desc; 157b1c97193SSean Young if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_control(ep)) { 158b1c97193SSean Young dev_err(&intf->dev, "endpoint incorrect"); 159b1c97193SSean Young return -ENODEV; 160b1c97193SSean Young } 161b1c97193SSean Young 162b1c97193SSean Young ir = devm_kzalloc(&intf->dev, sizeof(*ir), GFP_KERNEL); 163b1c97193SSean Young if (!ir) 164b1c97193SSean Young return -ENOMEM; 165b1c97193SSean Young 166b1c97193SSean Young ir->dev = &intf->dev; 167b1c97193SSean Young 168b17ec78aSKees Cook timer_setup(&ir->timer, igorplugusb_timer, 0); 169b1c97193SSean Young 170b1c97193SSean Young ir->request.bRequest = GET_INFRACODE; 171b1c97193SSean Young ir->request.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN; 172b1c97193SSean Young ir->request.wLength = cpu_to_le16(sizeof(ir->buf_in)); 173b1c97193SSean Young 174b1c97193SSean Young ir->urb = usb_alloc_urb(0, GFP_KERNEL); 175b1c97193SSean Young if (!ir->urb) 176a40973ffSSean Young goto fail; 177b1c97193SSean Young 178b1c97193SSean Young usb_fill_control_urb(ir->urb, udev, 179b1c97193SSean Young usb_rcvctrlpipe(udev, 0), (uint8_t *)&ir->request, 180b1c97193SSean Young ir->buf_in, sizeof(ir->buf_in), igorplugusb_callback, ir); 181b1c97193SSean Young 182b1c97193SSean Young usb_make_path(udev, ir->phys, sizeof(ir->phys)); 183b1c97193SSean Young 1840f7499fdSAndi Shyti rc = rc_allocate_device(RC_DRIVER_IR_RAW); 185a40973ffSSean Young if (!rc) 186a40973ffSSean Young goto fail; 187a40973ffSSean Young 188518f4b26SSean Young rc->device_name = DRIVER_DESC; 189b1c97193SSean Young rc->input_phys = ir->phys; 190b1c97193SSean Young usb_to_input_id(udev, &rc->input_id); 191b1c97193SSean Young rc->dev.parent = &intf->dev; 192b1c97193SSean Young /* 193b1c97193SSean Young * This device can only store 36 pulses + spaces, which is not enough 194b1c97193SSean Young * for the NEC protocol and many others. 195b1c97193SSean Young */ 1966d741bfeSSean Young rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER & 1976d741bfeSSean Young ~(RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX | RC_PROTO_BIT_NEC32 | 1986d741bfeSSean Young RC_PROTO_BIT_RC6_6A_20 | RC_PROTO_BIT_RC6_6A_24 | 1996d741bfeSSean Young RC_PROTO_BIT_RC6_6A_32 | RC_PROTO_BIT_RC6_MCE | 2006d741bfeSSean Young RC_PROTO_BIT_SONY20 | RC_PROTO_BIT_SANYO); 201b1c97193SSean Young 202b1c97193SSean Young rc->priv = ir; 203b1c97193SSean Young rc->driver_name = DRIVER_NAME; 204b1c97193SSean Young rc->map_name = RC_MAP_HAUPPAUGE; 205*528222d8SSean Young rc->timeout = MS_TO_US(100); 206*528222d8SSean Young rc->rx_resolution = 85; 207b1c97193SSean Young 208b1c97193SSean Young ir->rc = rc; 209b1c97193SSean Young ret = rc_register_device(rc); 210b1c97193SSean Young if (ret) { 211b1c97193SSean Young dev_err(&intf->dev, "failed to register rc device: %d", ret); 212a40973ffSSean Young goto fail; 213b1c97193SSean Young } 214b1c97193SSean Young 215b1c97193SSean Young usb_set_intfdata(intf, ir); 216b1c97193SSean Young 217b1c97193SSean Young igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY); 218b1c97193SSean Young 219b1c97193SSean Young return 0; 220a40973ffSSean Young fail: 221a40973ffSSean Young rc_free_device(ir->rc); 222a40973ffSSean Young usb_free_urb(ir->urb); 223a40973ffSSean Young del_timer(&ir->timer); 224a40973ffSSean Young 225a40973ffSSean Young return ret; 226b1c97193SSean Young } 227b1c97193SSean Young 228b1c97193SSean Young static void igorplugusb_disconnect(struct usb_interface *intf) 229b1c97193SSean Young { 230b1c97193SSean Young struct igorplugusb *ir = usb_get_intfdata(intf); 231b1c97193SSean Young 232b1c97193SSean Young rc_unregister_device(ir->rc); 233b1c97193SSean Young del_timer_sync(&ir->timer); 234b1c97193SSean Young usb_set_intfdata(intf, NULL); 235b1c97193SSean Young usb_kill_urb(ir->urb); 236b1c97193SSean Young usb_free_urb(ir->urb); 237b1c97193SSean Young } 238b1c97193SSean Young 2395fad16b5SArvind Yadav static const struct usb_device_id igorplugusb_table[] = { 240b1c97193SSean Young /* Igor Plug USB (Atmel's Manufact. ID) */ 241b1c97193SSean Young { USB_DEVICE(0x03eb, 0x0002) }, 242b1c97193SSean Young /* Fit PC2 Infrared Adapter */ 243b1c97193SSean Young { USB_DEVICE(0x03eb, 0x21fe) }, 244b1c97193SSean Young /* Terminating entry */ 245b1c97193SSean Young { } 246b1c97193SSean Young }; 247b1c97193SSean Young 248b1c97193SSean Young static struct usb_driver igorplugusb_driver = { 249b1c97193SSean Young .name = DRIVER_NAME, 250b1c97193SSean Young .probe = igorplugusb_probe, 251b1c97193SSean Young .disconnect = igorplugusb_disconnect, 252b1c97193SSean Young .id_table = igorplugusb_table 253b1c97193SSean Young }; 254b1c97193SSean Young 255b1c97193SSean Young module_usb_driver(igorplugusb_driver); 256b1c97193SSean Young 257b1c97193SSean Young MODULE_DESCRIPTION(DRIVER_DESC); 258b1c97193SSean Young MODULE_AUTHOR("Sean Young <sean@mess.org>"); 259b1c97193SSean Young MODULE_LICENSE("GPL"); 260b1c97193SSean Young MODULE_DEVICE_TABLE(usb, igorplugusb_table); 261