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