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