GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / media / rc / igorplugusb.c
1 /*
2  * IgorPlug-USB IR Receiver
3  *
4  * Copyright (C) 2014 Sean Young <sean@mess.org>
5  *
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
8  *
9  * Based on the lirc_igorplugusb.c driver:
10  *      Copyright (C) 2004 Jan M. Hochstein
11  *      <hochstein@algo.informatik.tu-darmstadt.de>
12  *
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.
17  *
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.
22  */
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>
29
30 #define DRIVER_DESC             "IgorPlug-USB IR Receiver"
31 #define DRIVER_NAME             "igorplugusb"
32
33 #define HEADERLEN               3
34 #define BUFLEN                  36
35 #define MAX_PACKET              (HEADERLEN + BUFLEN)
36
37 #define SET_INFRABUFFER_EMPTY   1
38 #define GET_INFRACODE           2
39
40
41 struct igorplugusb {
42         struct rc_dev *rc;
43         struct device *dev;
44
45         struct urb *urb;
46         struct usb_ctrlrequest request;
47
48         struct timer_list timer;
49
50         uint8_t buf_in[MAX_PACKET];
51
52         char phys[64];
53 };
54
55 static void igorplugusb_cmd(struct igorplugusb *ir, int cmd);
56
57 static void igorplugusb_irdata(struct igorplugusb *ir, unsigned len)
58 {
59         DEFINE_IR_RAW_EVENT(rawir);
60         unsigned i, start, overflow;
61
62         dev_dbg(ir->dev, "irdata: %*ph (len=%u)", len, ir->buf_in, len);
63
64         /*
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.
69          */
70         overflow = ir->buf_in[2];
71         i = start = overflow + HEADERLEN;
72
73         if (start >= len) {
74                 dev_err(ir->dev, "receive overflow invalid: %u", overflow);
75         } else {
76                 if (overflow > 0) {
77                         dev_warn(ir->dev, "receive overflow, at least %u lost",
78                                                                 overflow);
79                         ir_raw_event_reset(ir->rc);
80                 }
81
82                 do {
83                         rawir.duration = ir->buf_in[i] * 85333;
84                         rawir.pulse = i & 1;
85
86                         ir_raw_event_store_with_filter(ir->rc, &rawir);
87
88                         if (++i == len)
89                                 i = HEADERLEN;
90                 } while (i != start);
91
92                 /* add a trailing space */
93                 rawir.duration = ir->rc->timeout;
94                 rawir.pulse = false;
95                 ir_raw_event_store_with_filter(ir->rc, &rawir);
96
97                 ir_raw_event_handle(ir->rc);
98         }
99
100         igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY);
101 }
102
103 static void igorplugusb_callback(struct urb *urb)
104 {
105         struct usb_ctrlrequest *req;
106         struct igorplugusb *ir = urb->context;
107
108         req = (struct usb_ctrlrequest *)urb->setup_packet;
109
110         switch (urb->status) {
111         case 0:
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));
117                 break;
118         case -EPROTO:
119         case -ECONNRESET:
120         case -ENOENT:
121         case -ESHUTDOWN:
122                 usb_unlink_urb(urb);
123                 return;
124         default:
125                 dev_warn(ir->dev, "Error: urb status = %d\n", urb->status);
126                 igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY);
127                 break;
128         }
129 }
130
131 static void igorplugusb_cmd(struct igorplugusb *ir, int cmd)
132 {
133         int ret;
134
135         ir->request.bRequest = cmd;
136         ir->urb->transfer_flags = 0;
137         ret = usb_submit_urb(ir->urb, GFP_ATOMIC);
138         if (ret)
139                 dev_err(ir->dev, "submit urb failed: %d", ret);
140 }
141
142 static void igorplugusb_timer(struct timer_list *t)
143 {
144         struct igorplugusb *ir = from_timer(ir, t, timer);
145
146         igorplugusb_cmd(ir, GET_INFRACODE);
147 }
148
149 static int igorplugusb_probe(struct usb_interface *intf,
150                                         const struct usb_device_id *id)
151 {
152         struct usb_device *udev;
153         struct usb_host_interface *idesc;
154         struct usb_endpoint_descriptor *ep;
155         struct igorplugusb *ir;
156         struct rc_dev *rc;
157         int ret = -ENOMEM;
158
159         udev = interface_to_usbdev(intf);
160         idesc = intf->cur_altsetting;
161
162         if (idesc->desc.bNumEndpoints != 1) {
163                 dev_err(&intf->dev, "incorrect number of endpoints");
164                 return -ENODEV;
165         }
166
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");
170                 return -ENODEV;
171         }
172
173         ir = devm_kzalloc(&intf->dev, sizeof(*ir), GFP_KERNEL);
174         if (!ir)
175                 return -ENOMEM;
176
177         ir->dev = &intf->dev;
178
179         timer_setup(&ir->timer, igorplugusb_timer, 0);
180
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));
184
185         ir->urb = usb_alloc_urb(0, GFP_KERNEL);
186         if (!ir->urb)
187                 goto fail;
188
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);
192
193         usb_make_path(udev, ir->phys, sizeof(ir->phys));
194
195         rc = rc_allocate_device(RC_DRIVER_IR_RAW);
196         if (!rc)
197                 goto fail;
198
199         rc->device_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         /*
204          * This device can only store 36 pulses + spaces, which is not enough
205          * for the NEC protocol and many others.
206          */
207         rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER &
208                 ~(RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX | RC_PROTO_BIT_NEC32 |
209                   RC_PROTO_BIT_RC6_6A_20 | RC_PROTO_BIT_RC6_6A_24 |
210                   RC_PROTO_BIT_RC6_6A_32 | RC_PROTO_BIT_RC6_MCE |
211                   RC_PROTO_BIT_SONY20 | RC_PROTO_BIT_SANYO);
212
213         rc->priv = ir;
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;
218
219         ir->rc = rc;
220         ret = rc_register_device(rc);
221         if (ret) {
222                 dev_err(&intf->dev, "failed to register rc device: %d", ret);
223                 goto fail;
224         }
225
226         usb_set_intfdata(intf, ir);
227
228         igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY);
229
230         return 0;
231 fail:
232         rc_free_device(ir->rc);
233         usb_free_urb(ir->urb);
234         del_timer(&ir->timer);
235
236         return ret;
237 }
238
239 static void igorplugusb_disconnect(struct usb_interface *intf)
240 {
241         struct igorplugusb *ir = usb_get_intfdata(intf);
242
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);
248 }
249
250 static const 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 */
256         { }
257 };
258
259 static struct usb_driver igorplugusb_driver = {
260         .name = DRIVER_NAME,
261         .probe = igorplugusb_probe,
262         .disconnect = igorplugusb_disconnect,
263         .id_table = igorplugusb_table
264 };
265
266 module_usb_driver(igorplugusb_driver);
267
268 MODULE_DESCRIPTION(DRIVER_DESC);
269 MODULE_AUTHOR("Sean Young <sean@mess.org>");
270 MODULE_LICENSE("GPL");
271 MODULE_DEVICE_TABLE(usb, igorplugusb_table);