2 * IgorPlug-USB IR Receiver
4 * Copyright (C) 2014 Sean Young <sean@mess.org>
6 * Supports the standard homebrew IgorPlugUSB receiver with Igor's firmware.
7 * See http://www.cesko.host.sk/IgorPlugUSB/IgorPlug-USB%20(AVR)_eng.htm
9 * Based on the lirc_igorplugusb.c driver:
10 * Copyright (C) 2004 Jan M. Hochstein
11 * <hochstein@algo.informatik.tu-darmstadt.de>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 #include <linux/device.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/usb.h>
27 #include <linux/usb/input.h>
28 #include <media/rc-core.h>
30 #define DRIVER_DESC "IgorPlug-USB IR Receiver"
31 #define DRIVER_NAME "igorplugusb"
35 #define MAX_PACKET (HEADERLEN + BUFLEN)
37 #define SET_INFRABUFFER_EMPTY 1
38 #define GET_INFRACODE 2
46 struct usb_ctrlrequest request;
48 struct timer_list timer;
50 uint8_t buf_in[MAX_PACKET];
55 static void igorplugusb_cmd(struct igorplugusb *ir, int cmd);
57 static void igorplugusb_irdata(struct igorplugusb *ir, unsigned len)
59 DEFINE_IR_RAW_EVENT(rawir);
60 unsigned i, start, overflow;
62 dev_dbg(ir->dev, "irdata: %*ph (len=%u)", len, ir->buf_in, len);
65 * If more than 36 pulses and spaces follow each other, the igorplugusb
66 * overwrites its buffer from the beginning. The overflow value is the
67 * last offset which was not overwritten. Everything from this offset
68 * onwards occurred before everything until this offset.
70 overflow = ir->buf_in[2];
71 i = start = overflow + HEADERLEN;
74 dev_err(ir->dev, "receive overflow invalid: %u", overflow);
77 dev_warn(ir->dev, "receive overflow, at least %u lost",
79 ir_raw_event_reset(ir->rc);
83 rawir.duration = ir->buf_in[i] * 85333;
86 ir_raw_event_store_with_filter(ir->rc, &rawir);
92 /* add a trailing space */
93 rawir.duration = ir->rc->timeout;
95 ir_raw_event_store_with_filter(ir->rc, &rawir);
97 ir_raw_event_handle(ir->rc);
100 igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY);
103 static void igorplugusb_callback(struct urb *urb)
105 struct usb_ctrlrequest *req;
106 struct igorplugusb *ir = urb->context;
108 req = (struct usb_ctrlrequest *)urb->setup_packet;
110 switch (urb->status) {
112 if (req->bRequest == GET_INFRACODE &&
113 urb->actual_length > HEADERLEN)
114 igorplugusb_irdata(ir, urb->actual_length);
115 else /* request IR */
116 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(50));
125 dev_warn(ir->dev, "Error: urb status = %d\n", urb->status);
126 igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY);
131 static void igorplugusb_cmd(struct igorplugusb *ir, int cmd)
135 ir->request.bRequest = cmd;
136 ir->urb->transfer_flags = 0;
137 ret = usb_submit_urb(ir->urb, GFP_ATOMIC);
139 dev_err(ir->dev, "submit urb failed: %d", ret);
142 static void igorplugusb_timer(unsigned long data)
144 struct igorplugusb *ir = (struct igorplugusb *)data;
146 igorplugusb_cmd(ir, GET_INFRACODE);
149 static int igorplugusb_probe(struct usb_interface *intf,
150 const struct usb_device_id *id)
152 struct usb_device *udev;
153 struct usb_host_interface *idesc;
154 struct usb_endpoint_descriptor *ep;
155 struct igorplugusb *ir;
159 udev = interface_to_usbdev(intf);
160 idesc = intf->cur_altsetting;
162 if (idesc->desc.bNumEndpoints != 1) {
163 dev_err(&intf->dev, "incorrect number of endpoints");
167 ep = &idesc->endpoint[0].desc;
168 if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_control(ep)) {
169 dev_err(&intf->dev, "endpoint incorrect");
173 ir = devm_kzalloc(&intf->dev, sizeof(*ir), GFP_KERNEL);
177 ir->dev = &intf->dev;
179 setup_timer(&ir->timer, igorplugusb_timer, (unsigned long)ir);
181 ir->request.bRequest = GET_INFRACODE;
182 ir->request.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
183 ir->request.wLength = cpu_to_le16(sizeof(ir->buf_in));
185 ir->urb = usb_alloc_urb(0, GFP_KERNEL);
189 usb_fill_control_urb(ir->urb, udev,
190 usb_rcvctrlpipe(udev, 0), (uint8_t *)&ir->request,
191 ir->buf_in, sizeof(ir->buf_in), igorplugusb_callback, ir);
193 usb_make_path(udev, ir->phys, sizeof(ir->phys));
195 rc = rc_allocate_device();
199 rc->input_name = DRIVER_DESC;
200 rc->input_phys = ir->phys;
201 usb_to_input_id(udev, &rc->input_id);
202 rc->dev.parent = &intf->dev;
203 rc->driver_type = RC_DRIVER_IR_RAW;
205 * This device can only store 36 pulses + spaces, which is not enough
206 * for the NEC protocol and many others.
208 rc->allowed_protocols = RC_BIT_ALL & ~(RC_BIT_NEC | RC_BIT_NECX |
209 RC_BIT_NEC32 | RC_BIT_RC6_6A_20 |
210 RC_BIT_RC6_6A_24 | RC_BIT_RC6_6A_32 | RC_BIT_RC6_MCE |
211 RC_BIT_SONY20 | RC_BIT_MCE_KBD | RC_BIT_SANYO);
214 rc->driver_name = DRIVER_NAME;
215 rc->map_name = RC_MAP_HAUPPAUGE;
216 rc->timeout = MS_TO_NS(100);
217 rc->rx_resolution = 85333;
220 ret = rc_register_device(rc);
222 dev_err(&intf->dev, "failed to register rc device: %d", ret);
226 usb_set_intfdata(intf, ir);
228 igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY);
232 rc_free_device(ir->rc);
233 usb_free_urb(ir->urb);
234 del_timer(&ir->timer);
239 static void igorplugusb_disconnect(struct usb_interface *intf)
241 struct igorplugusb *ir = usb_get_intfdata(intf);
243 rc_unregister_device(ir->rc);
244 del_timer_sync(&ir->timer);
245 usb_set_intfdata(intf, NULL);
246 usb_kill_urb(ir->urb);
247 usb_free_urb(ir->urb);
250 static struct usb_device_id igorplugusb_table[] = {
251 /* Igor Plug USB (Atmel's Manufact. ID) */
252 { USB_DEVICE(0x03eb, 0x0002) },
253 /* Fit PC2 Infrared Adapter */
254 { USB_DEVICE(0x03eb, 0x21fe) },
255 /* Terminating entry */
259 static struct usb_driver igorplugusb_driver = {
261 .probe = igorplugusb_probe,
262 .disconnect = igorplugusb_disconnect,
263 .id_table = igorplugusb_table
266 module_usb_driver(igorplugusb_driver);
268 MODULE_DESCRIPTION(DRIVER_DESC);
269 MODULE_AUTHOR("Sean Young <sean@mess.org>");
270 MODULE_LICENSE("GPL");
271 MODULE_DEVICE_TABLE(usb, igorplugusb_table);