xref: /openbmc/linux/drivers/media/rc/ttusbir.c (revision 0c7beb2d)
1 /*
2  * TechnoTrend USB IR Receiver
3  *
4  * Copyright (C) 2012 Sean Young <sean@mess.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16 
17 #include <linux/module.h>
18 #include <linux/usb.h>
19 #include <linux/usb/input.h>
20 #include <linux/slab.h>
21 #include <linux/leds.h>
22 #include <media/rc-core.h>
23 
24 #define DRIVER_NAME	"ttusbir"
25 #define DRIVER_DESC	"TechnoTrend USB IR Receiver"
26 /*
27  * The Windows driver uses 8 URBS, the original lirc drivers has a
28  * configurable amount (2 default, 4 max). This device generates about 125
29  * messages per second (!), whether IR is idle or not.
30  */
31 #define NUM_URBS	4
32 #define NS_PER_BYTE	62500
33 #define NS_PER_BIT	(NS_PER_BYTE/8)
34 
35 struct ttusbir {
36 	struct rc_dev *rc;
37 	struct device *dev;
38 	struct usb_device *udev;
39 
40 	struct urb *urb[NUM_URBS];
41 
42 	struct led_classdev led;
43 	struct urb *bulk_urb;
44 	uint8_t bulk_buffer[5];
45 	int bulk_out_endp, iso_in_endp;
46 	bool led_on, is_led_on;
47 	atomic_t led_complete;
48 
49 	char phys[64];
50 };
51 
52 static enum led_brightness ttusbir_brightness_get(struct led_classdev *led_dev)
53 {
54 	struct ttusbir *tt = container_of(led_dev, struct ttusbir, led);
55 
56 	return tt->led_on ? LED_FULL : LED_OFF;
57 }
58 
59 static void ttusbir_set_led(struct ttusbir *tt)
60 {
61 	int ret;
62 
63 	smp_mb();
64 
65 	if (tt->led_on != tt->is_led_on && tt->udev &&
66 				atomic_add_unless(&tt->led_complete, 1, 1)) {
67 		tt->bulk_buffer[4] = tt->is_led_on = tt->led_on;
68 		ret = usb_submit_urb(tt->bulk_urb, GFP_ATOMIC);
69 		if (ret) {
70 			dev_warn(tt->dev, "failed to submit bulk urb: %d\n",
71 									ret);
72 			atomic_dec(&tt->led_complete);
73 		}
74 	}
75 }
76 
77 static void ttusbir_brightness_set(struct led_classdev *led_dev, enum
78 						led_brightness brightness)
79 {
80 	struct ttusbir *tt = container_of(led_dev, struct ttusbir, led);
81 
82 	tt->led_on = brightness != LED_OFF;
83 
84 	ttusbir_set_led(tt);
85 }
86 
87 /*
88  * The urb cannot be reused until the urb completes
89  */
90 static void ttusbir_bulk_complete(struct urb *urb)
91 {
92 	struct ttusbir *tt = urb->context;
93 
94 	atomic_dec(&tt->led_complete);
95 
96 	switch (urb->status) {
97 	case 0:
98 		break;
99 	case -ECONNRESET:
100 	case -ENOENT:
101 	case -ESHUTDOWN:
102 		usb_unlink_urb(urb);
103 		return;
104 	case -EPIPE:
105 	default:
106 		dev_dbg(tt->dev, "Error: urb status = %d\n", urb->status);
107 		break;
108 	}
109 
110 	ttusbir_set_led(tt);
111 }
112 
113 /*
114  * The data is one bit per sample, a set bit signifying silence and samples
115  * being MSB first. Bit 0 can contain garbage so take it to be whatever
116  * bit 1 is, so we don't have unexpected edges.
117  */
118 static void ttusbir_process_ir_data(struct ttusbir *tt, uint8_t *buf)
119 {
120 	struct ir_raw_event rawir = {};
121 	unsigned i, v, b;
122 	bool event = false;
123 
124 	for (i = 0; i < 128; i++) {
125 		v = buf[i] & 0xfe;
126 		switch (v) {
127 		case 0xfe:
128 			rawir.pulse = false;
129 			rawir.duration = NS_PER_BYTE;
130 			if (ir_raw_event_store_with_filter(tt->rc, &rawir))
131 				event = true;
132 			break;
133 		case 0:
134 			rawir.pulse = true;
135 			rawir.duration = NS_PER_BYTE;
136 			if (ir_raw_event_store_with_filter(tt->rc, &rawir))
137 				event = true;
138 			break;
139 		default:
140 			/* one edge per byte */
141 			if (v & 2) {
142 				b = ffz(v | 1);
143 				rawir.pulse = true;
144 			} else {
145 				b = ffs(v) - 1;
146 				rawir.pulse = false;
147 			}
148 
149 			rawir.duration = NS_PER_BIT * (8 - b);
150 			if (ir_raw_event_store_with_filter(tt->rc, &rawir))
151 				event = true;
152 
153 			rawir.pulse = !rawir.pulse;
154 			rawir.duration = NS_PER_BIT * b;
155 			if (ir_raw_event_store_with_filter(tt->rc, &rawir))
156 				event = true;
157 			break;
158 		}
159 	}
160 
161 	/* don't wakeup when there's nothing to do */
162 	if (event)
163 		ir_raw_event_handle(tt->rc);
164 }
165 
166 static void ttusbir_urb_complete(struct urb *urb)
167 {
168 	struct ttusbir *tt = urb->context;
169 	int rc;
170 
171 	switch (urb->status) {
172 	case 0:
173 		ttusbir_process_ir_data(tt, urb->transfer_buffer);
174 		break;
175 	case -ECONNRESET:
176 	case -ENOENT:
177 	case -ESHUTDOWN:
178 		usb_unlink_urb(urb);
179 		return;
180 	case -EPIPE:
181 	default:
182 		dev_dbg(tt->dev, "Error: urb status = %d\n", urb->status);
183 		break;
184 	}
185 
186 	rc = usb_submit_urb(urb, GFP_ATOMIC);
187 	if (rc && rc != -ENODEV)
188 		dev_warn(tt->dev, "failed to resubmit urb: %d\n", rc);
189 }
190 
191 static int ttusbir_probe(struct usb_interface *intf,
192 			 const struct usb_device_id *id)
193 {
194 	struct ttusbir *tt;
195 	struct usb_interface_descriptor *idesc;
196 	struct usb_endpoint_descriptor *desc;
197 	struct rc_dev *rc;
198 	int i, j, ret;
199 	int altsetting = -1;
200 
201 	tt = kzalloc(sizeof(*tt), GFP_KERNEL);
202 	rc = rc_allocate_device(RC_DRIVER_IR_RAW);
203 	if (!tt || !rc) {
204 		ret = -ENOMEM;
205 		goto out;
206 	}
207 
208 	/* find the correct alt setting */
209 	for (i = 0; i < intf->num_altsetting && altsetting == -1; i++) {
210 		int max_packet, bulk_out_endp = -1, iso_in_endp = -1;
211 
212 		idesc = &intf->altsetting[i].desc;
213 
214 		for (j = 0; j < idesc->bNumEndpoints; j++) {
215 			desc = &intf->altsetting[i].endpoint[j].desc;
216 			max_packet = le16_to_cpu(desc->wMaxPacketSize);
217 			if (usb_endpoint_dir_in(desc) &&
218 					usb_endpoint_xfer_isoc(desc) &&
219 					max_packet == 0x10)
220 				iso_in_endp = j;
221 			else if (usb_endpoint_dir_out(desc) &&
222 					usb_endpoint_xfer_bulk(desc) &&
223 					max_packet == 0x20)
224 				bulk_out_endp = j;
225 
226 			if (bulk_out_endp != -1 && iso_in_endp != -1) {
227 				tt->bulk_out_endp = bulk_out_endp;
228 				tt->iso_in_endp = iso_in_endp;
229 				altsetting = i;
230 				break;
231 			}
232 		}
233 	}
234 
235 	if (altsetting == -1) {
236 		dev_err(&intf->dev, "cannot find expected altsetting\n");
237 		ret = -ENODEV;
238 		goto out;
239 	}
240 
241 	tt->dev = &intf->dev;
242 	tt->udev = interface_to_usbdev(intf);
243 	tt->rc = rc;
244 
245 	ret = usb_set_interface(tt->udev, 0, altsetting);
246 	if (ret)
247 		goto out;
248 
249 	for (i = 0; i < NUM_URBS; i++) {
250 		struct urb *urb = usb_alloc_urb(8, GFP_KERNEL);
251 		void *buffer;
252 
253 		if (!urb) {
254 			ret = -ENOMEM;
255 			goto out;
256 		}
257 
258 		urb->dev = tt->udev;
259 		urb->context = tt;
260 		urb->pipe = usb_rcvisocpipe(tt->udev, tt->iso_in_endp);
261 		urb->interval = 1;
262 		buffer = usb_alloc_coherent(tt->udev, 128, GFP_KERNEL,
263 						&urb->transfer_dma);
264 		if (!buffer) {
265 			usb_free_urb(urb);
266 			ret = -ENOMEM;
267 			goto out;
268 		}
269 		urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP | URB_ISO_ASAP;
270 		urb->transfer_buffer = buffer;
271 		urb->complete = ttusbir_urb_complete;
272 		urb->number_of_packets = 8;
273 		urb->transfer_buffer_length = 128;
274 
275 		for (j = 0; j < 8; j++) {
276 			urb->iso_frame_desc[j].offset = j * 16;
277 			urb->iso_frame_desc[j].length = 16;
278 		}
279 
280 		tt->urb[i] = urb;
281 	}
282 
283 	tt->bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
284 	if (!tt->bulk_urb) {
285 		ret = -ENOMEM;
286 		goto out;
287 	}
288 
289 	tt->bulk_buffer[0] = 0xaa;
290 	tt->bulk_buffer[1] = 0x01;
291 	tt->bulk_buffer[2] = 0x05;
292 	tt->bulk_buffer[3] = 0x01;
293 
294 	usb_fill_bulk_urb(tt->bulk_urb, tt->udev, usb_sndbulkpipe(tt->udev,
295 		tt->bulk_out_endp), tt->bulk_buffer, sizeof(tt->bulk_buffer),
296 						ttusbir_bulk_complete, tt);
297 
298 	tt->led.name = "ttusbir:green:power";
299 	tt->led.default_trigger = "rc-feedback";
300 	tt->led.brightness_set = ttusbir_brightness_set;
301 	tt->led.brightness_get = ttusbir_brightness_get;
302 	tt->is_led_on = tt->led_on = true;
303 	atomic_set(&tt->led_complete, 0);
304 	ret = led_classdev_register(&intf->dev, &tt->led);
305 	if (ret)
306 		goto out;
307 
308 	usb_make_path(tt->udev, tt->phys, sizeof(tt->phys));
309 
310 	rc->device_name = DRIVER_DESC;
311 	rc->input_phys = tt->phys;
312 	usb_to_input_id(tt->udev, &rc->input_id);
313 	rc->dev.parent = &intf->dev;
314 	rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
315 	rc->priv = tt;
316 	rc->driver_name = DRIVER_NAME;
317 	rc->map_name = RC_MAP_TT_1500;
318 	rc->min_timeout = 1;
319 	rc->timeout = IR_DEFAULT_TIMEOUT;
320 	rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
321 
322 	/*
323 	 * The precision is NS_PER_BIT, but since every 8th bit can be
324 	 * overwritten with garbage the accuracy is at best 2 * NS_PER_BIT.
325 	 */
326 	rc->rx_resolution = NS_PER_BIT;
327 
328 	ret = rc_register_device(rc);
329 	if (ret) {
330 		dev_err(&intf->dev, "failed to register rc device %d\n", ret);
331 		goto out2;
332 	}
333 
334 	usb_set_intfdata(intf, tt);
335 
336 	for (i = 0; i < NUM_URBS; i++) {
337 		ret = usb_submit_urb(tt->urb[i], GFP_KERNEL);
338 		if (ret) {
339 			dev_err(tt->dev, "failed to submit urb %d\n", ret);
340 			goto out3;
341 		}
342 	}
343 
344 	return 0;
345 out3:
346 	rc_unregister_device(rc);
347 	rc = NULL;
348 out2:
349 	led_classdev_unregister(&tt->led);
350 out:
351 	if (tt) {
352 		for (i = 0; i < NUM_URBS && tt->urb[i]; i++) {
353 			struct urb *urb = tt->urb[i];
354 
355 			usb_kill_urb(urb);
356 			usb_free_coherent(tt->udev, 128, urb->transfer_buffer,
357 							urb->transfer_dma);
358 			usb_free_urb(urb);
359 		}
360 		usb_kill_urb(tt->bulk_urb);
361 		usb_free_urb(tt->bulk_urb);
362 		kfree(tt);
363 	}
364 	rc_free_device(rc);
365 
366 	return ret;
367 }
368 
369 static void ttusbir_disconnect(struct usb_interface *intf)
370 {
371 	struct ttusbir *tt = usb_get_intfdata(intf);
372 	struct usb_device *udev = tt->udev;
373 	int i;
374 
375 	tt->udev = NULL;
376 
377 	rc_unregister_device(tt->rc);
378 	led_classdev_unregister(&tt->led);
379 	for (i = 0; i < NUM_URBS; i++) {
380 		usb_kill_urb(tt->urb[i]);
381 		usb_free_coherent(udev, 128, tt->urb[i]->transfer_buffer,
382 						tt->urb[i]->transfer_dma);
383 		usb_free_urb(tt->urb[i]);
384 	}
385 	usb_kill_urb(tt->bulk_urb);
386 	usb_free_urb(tt->bulk_urb);
387 	usb_set_intfdata(intf, NULL);
388 	kfree(tt);
389 }
390 
391 static int ttusbir_suspend(struct usb_interface *intf, pm_message_t message)
392 {
393 	struct ttusbir *tt = usb_get_intfdata(intf);
394 	int i;
395 
396 	for (i = 0; i < NUM_URBS; i++)
397 		usb_kill_urb(tt->urb[i]);
398 
399 	led_classdev_suspend(&tt->led);
400 	usb_kill_urb(tt->bulk_urb);
401 
402 	return 0;
403 }
404 
405 static int ttusbir_resume(struct usb_interface *intf)
406 {
407 	struct ttusbir *tt = usb_get_intfdata(intf);
408 	int i, rc;
409 
410 	tt->is_led_on = true;
411 	led_classdev_resume(&tt->led);
412 
413 	for (i = 0; i < NUM_URBS; i++) {
414 		rc = usb_submit_urb(tt->urb[i], GFP_KERNEL);
415 		if (rc) {
416 			dev_warn(tt->dev, "failed to submit urb: %d\n", rc);
417 			break;
418 		}
419 	}
420 
421 	return rc;
422 }
423 
424 static const struct usb_device_id ttusbir_table[] = {
425 	{ USB_DEVICE(0x0b48, 0x2003) },
426 	{ }
427 };
428 
429 static struct usb_driver ttusbir_driver = {
430 	.name = DRIVER_NAME,
431 	.id_table = ttusbir_table,
432 	.probe = ttusbir_probe,
433 	.suspend = ttusbir_suspend,
434 	.resume = ttusbir_resume,
435 	.reset_resume = ttusbir_resume,
436 	.disconnect = ttusbir_disconnect,
437 };
438 
439 module_usb_driver(ttusbir_driver);
440 
441 MODULE_DESCRIPTION(DRIVER_DESC);
442 MODULE_AUTHOR("Sean Young <sean@mess.org>");
443 MODULE_LICENSE("GPL");
444 MODULE_DEVICE_TABLE(usb, ttusbir_table);
445 
446