xref: /openbmc/linux/drivers/media/rc/igorplugusb.c (revision 2a98e887)
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 
41b3f820b9SOliver Neukum 	u8 *buf_in;
42b1c97193SSean Young 
43b1c97193SSean Young 	char phys[64];
44b1c97193SSean Young };
45b1c97193SSean Young 
46b1c97193SSean Young static void igorplugusb_cmd(struct igorplugusb *ir, int cmd);
47b1c97193SSean Young 
igorplugusb_irdata(struct igorplugusb * ir,unsigned len)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 {
678fede658SSean Young 		if (overflow > 0) {
68b1c97193SSean Young 			dev_warn(ir->dev, "receive overflow, at least %u lost",
69b1c97193SSean Young 								overflow);
70950170d6SSean Young 			ir_raw_event_overflow(ir->rc);
718fede658SSean 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 
igorplugusb_callback(struct urb * urb)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 		return;
114b1c97193SSean Young 	default:
115b1c97193SSean Young 		dev_warn(ir->dev, "Error: urb status = %d\n", urb->status);
116b1c97193SSean Young 		igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY);
117b1c97193SSean Young 		break;
118b1c97193SSean Young 	}
119b1c97193SSean Young }
120b1c97193SSean Young 
igorplugusb_cmd(struct igorplugusb * ir,int cmd)121b1c97193SSean Young static void igorplugusb_cmd(struct igorplugusb *ir, int cmd)
122b1c97193SSean Young {
123b1c97193SSean Young 	int ret;
124b1c97193SSean Young 
125b1c97193SSean Young 	ir->request.bRequest = cmd;
126b1c97193SSean Young 	ir->urb->transfer_flags = 0;
127b1c97193SSean Young 	ret = usb_submit_urb(ir->urb, GFP_ATOMIC);
1282a774594SOliver Neukum 	if (ret && ret != -EPERM)
129b1c97193SSean Young 		dev_err(ir->dev, "submit urb failed: %d", ret);
130b1c97193SSean Young }
131b1c97193SSean Young 
igorplugusb_timer(struct timer_list * t)132b17ec78aSKees Cook static void igorplugusb_timer(struct timer_list *t)
133b1c97193SSean Young {
134b17ec78aSKees Cook 	struct igorplugusb *ir = from_timer(ir, t, timer);
135b1c97193SSean Young 
136b1c97193SSean Young 	igorplugusb_cmd(ir, GET_INFRACODE);
137b1c97193SSean Young }
138b1c97193SSean Young 
igorplugusb_probe(struct usb_interface * intf,const struct usb_device_id * id)139b1c97193SSean Young static int igorplugusb_probe(struct usb_interface *intf,
140b1c97193SSean Young 					const struct usb_device_id *id)
141b1c97193SSean Young {
142b1c97193SSean Young 	struct usb_device *udev;
143b1c97193SSean Young 	struct usb_host_interface *idesc;
144b1c97193SSean Young 	struct usb_endpoint_descriptor *ep;
145b1c97193SSean Young 	struct igorplugusb *ir;
146b1c97193SSean Young 	struct rc_dev *rc;
147a40973ffSSean Young 	int ret = -ENOMEM;
148b1c97193SSean Young 
149b1c97193SSean Young 	udev = interface_to_usbdev(intf);
150b1c97193SSean Young 	idesc = intf->cur_altsetting;
151b1c97193SSean Young 
152b1c97193SSean Young 	if (idesc->desc.bNumEndpoints != 1) {
153b1c97193SSean Young 		dev_err(&intf->dev, "incorrect number of endpoints");
154b1c97193SSean Young 		return -ENODEV;
155b1c97193SSean Young 	}
156b1c97193SSean Young 
157b1c97193SSean Young 	ep = &idesc->endpoint[0].desc;
158b1c97193SSean Young 	if (!usb_endpoint_dir_in(ep) || !usb_endpoint_xfer_control(ep)) {
159b1c97193SSean Young 		dev_err(&intf->dev, "endpoint incorrect");
160b1c97193SSean Young 		return -ENODEV;
161b1c97193SSean Young 	}
162b1c97193SSean Young 
163b1c97193SSean Young 	ir = devm_kzalloc(&intf->dev, sizeof(*ir), GFP_KERNEL);
164b1c97193SSean Young 	if (!ir)
165b1c97193SSean Young 		return -ENOMEM;
166b1c97193SSean Young 
167b1c97193SSean Young 	ir->dev = &intf->dev;
168b1c97193SSean Young 
169b17ec78aSKees Cook 	timer_setup(&ir->timer, igorplugusb_timer, 0);
170b1c97193SSean Young 
171b1c97193SSean Young 	ir->request.bRequest = GET_INFRACODE;
172b1c97193SSean Young 	ir->request.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
173*2a98e887SYang Yingliang 	ir->request.wLength = cpu_to_le16(MAX_PACKET);
174b1c97193SSean Young 
175b1c97193SSean Young 	ir->urb = usb_alloc_urb(0, GFP_KERNEL);
176b1c97193SSean Young 	if (!ir->urb)
177a40973ffSSean Young 		goto fail;
178b1c97193SSean Young 
179b3f820b9SOliver Neukum 	ir->buf_in = kmalloc(MAX_PACKET, GFP_KERNEL);
180b3f820b9SOliver Neukum 	if (!ir->buf_in)
181b3f820b9SOliver Neukum 		goto fail;
182b1c97193SSean Young 	usb_fill_control_urb(ir->urb, udev,
183b1c97193SSean Young 		usb_rcvctrlpipe(udev, 0), (uint8_t *)&ir->request,
184*2a98e887SYang Yingliang 		ir->buf_in, MAX_PACKET, igorplugusb_callback, ir);
185b1c97193SSean Young 
186b1c97193SSean Young 	usb_make_path(udev, ir->phys, sizeof(ir->phys));
187b1c97193SSean Young 
1880f7499fdSAndi Shyti 	rc = rc_allocate_device(RC_DRIVER_IR_RAW);
189a40973ffSSean Young 	if (!rc)
190a40973ffSSean Young 		goto fail;
191a40973ffSSean Young 
192518f4b26SSean Young 	rc->device_name = DRIVER_DESC;
193b1c97193SSean Young 	rc->input_phys = ir->phys;
194b1c97193SSean Young 	usb_to_input_id(udev, &rc->input_id);
195b1c97193SSean Young 	rc->dev.parent = &intf->dev;
196b1c97193SSean Young 	/*
197b1c97193SSean Young 	 * This device can only store 36 pulses + spaces, which is not enough
198b1c97193SSean Young 	 * for the NEC protocol and many others.
199b1c97193SSean Young 	 */
2006d741bfeSSean Young 	rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER &
2016d741bfeSSean Young 		~(RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX | RC_PROTO_BIT_NEC32 |
2026d741bfeSSean Young 		  RC_PROTO_BIT_RC6_6A_20 | RC_PROTO_BIT_RC6_6A_24 |
2036d741bfeSSean Young 		  RC_PROTO_BIT_RC6_6A_32 | RC_PROTO_BIT_RC6_MCE |
2046d741bfeSSean Young 		  RC_PROTO_BIT_SONY20 | RC_PROTO_BIT_SANYO);
205b1c97193SSean Young 
206b1c97193SSean Young 	rc->priv = ir;
207b1c97193SSean Young 	rc->driver_name = DRIVER_NAME;
208b1c97193SSean Young 	rc->map_name = RC_MAP_HAUPPAUGE;
209528222d8SSean Young 	rc->timeout = MS_TO_US(100);
210528222d8SSean Young 	rc->rx_resolution = 85;
211b1c97193SSean Young 
212b1c97193SSean Young 	ir->rc = rc;
213b1c97193SSean Young 	ret = rc_register_device(rc);
214b1c97193SSean Young 	if (ret) {
215b1c97193SSean Young 		dev_err(&intf->dev, "failed to register rc device: %d", ret);
216a40973ffSSean Young 		goto fail;
217b1c97193SSean Young 	}
218b1c97193SSean Young 
219b1c97193SSean Young 	usb_set_intfdata(intf, ir);
220b1c97193SSean Young 
221b1c97193SSean Young 	igorplugusb_cmd(ir, SET_INFRABUFFER_EMPTY);
222b1c97193SSean Young 
223b1c97193SSean Young 	return 0;
224a40973ffSSean Young fail:
2252a774594SOliver Neukum 	usb_poison_urb(ir->urb);
226a40973ffSSean Young 	del_timer(&ir->timer);
2272a774594SOliver Neukum 	usb_unpoison_urb(ir->urb);
228522f1d7dSOliver Neukum 	usb_free_urb(ir->urb);
229522f1d7dSOliver Neukum 	rc_free_device(ir->rc);
230b3f820b9SOliver Neukum 	kfree(ir->buf_in);
231a40973ffSSean Young 
232a40973ffSSean Young 	return ret;
233b1c97193SSean Young }
234b1c97193SSean Young 
igorplugusb_disconnect(struct usb_interface * intf)235b1c97193SSean Young static void igorplugusb_disconnect(struct usb_interface *intf)
236b1c97193SSean Young {
237b1c97193SSean Young 	struct igorplugusb *ir = usb_get_intfdata(intf);
238b1c97193SSean Young 
239b1c97193SSean Young 	rc_unregister_device(ir->rc);
2402a774594SOliver Neukum 	usb_poison_urb(ir->urb);
241b1c97193SSean Young 	del_timer_sync(&ir->timer);
242b1c97193SSean Young 	usb_set_intfdata(intf, NULL);
2432a774594SOliver Neukum 	usb_unpoison_urb(ir->urb);
244b1c97193SSean Young 	usb_free_urb(ir->urb);
245b3f820b9SOliver Neukum 	kfree(ir->buf_in);
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