aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/rc
diff options
context:
space:
mode:
authorSean Young <sean@mess.org>2012-08-13 07:59:46 -0400
committerMauro Carvalho Chehab <mchehab@redhat.com>2012-08-13 15:19:11 -0400
commit0938069fa08970f1c898970c1331a029efe9a1ce (patch)
treecb9db086f9c1ebba56dcef7a5400df225dd79dee /drivers/media/rc
parent640583836b9488c806349e3839c9d8b0f15da1fa (diff)
[media] rc: Add support for the TechnoTrend USB IR Receiver
This driver adds support for TechnoTrend USB IR Receiver. It is a complete rewrite of the staging/media/lirc/lirc_ttusbir driver. It adds more accurate sample reporting and led control. Signed-off-by: Sean Young <sean@mess.org> Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media/rc')
-rw-r--r--drivers/media/rc/Kconfig13
-rw-r--r--drivers/media/rc/Makefile1
-rw-r--r--drivers/media/rc/keymaps/rc-tt-1500.c2
-rw-r--r--drivers/media/rc/ttusbir.c400
4 files changed, 415 insertions, 1 deletions
diff --git a/drivers/media/rc/Kconfig b/drivers/media/rc/Kconfig
index 2e91e664d024..64be610fe27d 100644
--- a/drivers/media/rc/Kconfig
+++ b/drivers/media/rc/Kconfig
@@ -274,6 +274,19 @@ config IR_IGUANA
274 To compile this driver as a module, choose M here: the module will 274 To compile this driver as a module, choose M here: the module will
275 be called iguanair. 275 be called iguanair.
276 276
277config IR_TTUSBIR
278 tristate "TechnoTrend USB IR Receiver"
279 depends on RC_CORE
280 select USB
281 select NEW_LEDS
282 select LEDS_CLASS
283 ---help---
284 Say Y here if you want to use the TechnoTrend USB IR Receiver. The
285 driver can control the led.
286
287 To compile this driver as a module, choose M here: the module will
288 be called ttusbir.
289
277config RC_LOOPBACK 290config RC_LOOPBACK
278 tristate "Remote Control Loopback Driver" 291 tristate "Remote Control Loopback Driver"
279 depends on RC_CORE 292 depends on RC_CORE
diff --git a/drivers/media/rc/Makefile b/drivers/media/rc/Makefile
index f871d1986c21..66c8baed565d 100644
--- a/drivers/media/rc/Makefile
+++ b/drivers/media/rc/Makefile
@@ -28,3 +28,4 @@ obj-$(CONFIG_IR_WINBOND_CIR) += winbond-cir.o
28obj-$(CONFIG_RC_LOOPBACK) += rc-loopback.o 28obj-$(CONFIG_RC_LOOPBACK) += rc-loopback.o
29obj-$(CONFIG_IR_GPIO_CIR) += gpio-ir-recv.o 29obj-$(CONFIG_IR_GPIO_CIR) += gpio-ir-recv.o
30obj-$(CONFIG_IR_IGUANA) += iguanair.o 30obj-$(CONFIG_IR_IGUANA) += iguanair.o
31obj-$(CONFIG_IR_TTUSBIR) += ttusbir.o
diff --git a/drivers/media/rc/keymaps/rc-tt-1500.c b/drivers/media/rc/keymaps/rc-tt-1500.c
index caeff85603e3..80217ffc91db 100644
--- a/drivers/media/rc/keymaps/rc-tt-1500.c
+++ b/drivers/media/rc/keymaps/rc-tt-1500.c
@@ -61,7 +61,7 @@ static struct rc_map_list tt_1500_map = {
61 .map = { 61 .map = {
62 .scan = tt_1500, 62 .scan = tt_1500,
63 .size = ARRAY_SIZE(tt_1500), 63 .size = ARRAY_SIZE(tt_1500),
64 .rc_type = RC_TYPE_UNKNOWN, /* Legacy IR type */ 64 .rc_type = RC_TYPE_RC5,
65 .name = RC_MAP_TT_1500, 65 .name = RC_MAP_TT_1500,
66 } 66 }
67}; 67};
diff --git a/drivers/media/rc/ttusbir.c b/drivers/media/rc/ttusbir.c
new file mode 100644
index 000000000000..71f03acabac8
--- /dev/null
+++ b/drivers/media/rc/ttusbir.c
@@ -0,0 +1,400 @@
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 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/module.h>
22#include <linux/usb.h>
23#include <linux/usb/input.h>
24#include <linux/slab.h>
25#include <linux/leds.h>
26#include <media/rc-core.h>
27
28#define DRIVER_NAME "ttusbir"
29#define DRIVER_DESC "TechnoTrend USB IR Receiver"
30/*
31 * The Windows driver uses 8 URBS, the original lirc drivers has a
32 * configurable amount (2 default, 4 max). This device generates about 125
33 * messages per second (!), whether IR is idle or not.
34 */
35#define NUM_URBS 4
36#define NS_PER_BYTE 62500
37#define NS_PER_BIT (NS_PER_BYTE/8)
38
39struct ttusbir {
40 struct rc_dev *rc;
41 struct device *dev;
42 struct usb_device *udev;
43
44 struct urb *urb[NUM_URBS];
45
46 struct led_classdev led;
47 struct urb *bulk_urb;
48 uint8_t bulk_buffer[5];
49 int bulk_out_endp, iso_in_endp;
50 bool led_on, is_led_on;
51 atomic_t led_complete;
52
53 char phys[64];
54};
55
56static enum led_brightness ttusbir_brightness_get(struct led_classdev *led_dev)
57{
58 struct ttusbir *tt = container_of(led_dev, struct ttusbir, led);
59
60 return tt->led_on ? LED_FULL : LED_OFF;
61}
62
63static void ttusbir_set_led(struct ttusbir *tt)
64{
65 int ret;
66
67 smp_mb();
68
69 if (tt->led_on != tt->is_led_on &&
70 atomic_add_unless(&tt->led_complete, 1, 1)) {
71 tt->bulk_buffer[4] = tt->is_led_on = tt->led_on;
72 ret = usb_submit_urb(tt->bulk_urb, GFP_ATOMIC);
73 if (ret && ret != -ENODEV) {
74 dev_warn(tt->dev, "failed to submit bulk urb: %d\n",
75 ret);
76 atomic_dec(&tt->led_complete);
77 }
78 }
79}
80
81static void ttusbir_brightness_set(struct led_classdev *led_dev, enum
82 led_brightness brightness)
83{
84 struct ttusbir *tt = container_of(led_dev, struct ttusbir, led);
85
86 tt->led_on = brightness != LED_OFF;
87
88 ttusbir_set_led(tt);
89}
90
91/*
92 * The urb cannot be reused until the urb completes
93 */
94static void ttusbir_bulk_complete(struct urb *urb)
95{
96 struct ttusbir *tt = urb->context;
97
98 atomic_dec(&tt->led_complete);
99
100 switch (urb->status) {
101 case 0:
102 break;
103 case -ECONNRESET:
104 case -ENOENT:
105 case -ESHUTDOWN:
106 usb_unlink_urb(urb);
107 return;
108 case -EPIPE:
109 default:
110 dev_dbg(tt->dev, "Error: urb status = %d\n", urb->status);
111 break;
112 }
113
114 ttusbir_set_led(tt);
115}
116
117/*
118 * The data is one bit per sample, a set bit signifying silence and samples
119 * being MSB first. Bit 0 can contain garbage so take it to be whatever
120 * bit 1 is, so we don't have unexpected edges.
121 */
122static void ttusbir_process_ir_data(struct ttusbir *tt, uint8_t *buf)
123{
124 unsigned i, v, b;
125 DEFINE_IR_RAW_EVENT(rawir);
126
127 init_ir_raw_event(&rawir);
128
129 for (i = 0; i < 128; i++) {
130 v = buf[i] & 0xfe;
131 switch (v) {
132 case 0xfe:
133 rawir.pulse = false;
134 rawir.duration = NS_PER_BYTE;
135 ir_raw_event_store_with_filter(tt->rc, &rawir);
136 break;
137 case 0:
138 rawir.pulse = true;
139 rawir.duration = NS_PER_BYTE;
140 ir_raw_event_store_with_filter(tt->rc, &rawir);
141 break;
142 default:
143 /* one edge per byte */
144 if (v & 2) {
145 b = ffz(v | 1);
146 rawir.pulse = true;
147 } else {
148 b = ffs(v) - 1;
149 rawir.pulse = false;
150 }
151
152 rawir.duration = NS_PER_BIT * (8 - b);
153 ir_raw_event_store_with_filter(tt->rc, &rawir);
154
155 rawir.pulse = !rawir.pulse;
156 rawir.duration = NS_PER_BIT * b;
157 ir_raw_event_store_with_filter(tt->rc, &rawir);
158 break;
159 }
160 }
161
162 ir_raw_event_handle(tt->rc);
163}
164
165static void ttusbir_urb_complete(struct urb *urb)
166{
167 struct ttusbir *tt = urb->context;
168 int rc;
169
170 switch (urb->status) {
171 case 0:
172 ttusbir_process_ir_data(tt, urb->transfer_buffer);
173 break;
174 case -ECONNRESET:
175 case -ENOENT:
176 case -ESHUTDOWN:
177 usb_unlink_urb(urb);
178 return;
179 case -EPIPE:
180 default:
181 dev_dbg(tt->dev, "Error: urb status = %d\n", urb->status);
182 break;
183 }
184
185 rc = usb_submit_urb(urb, GFP_ATOMIC);
186 if (rc && rc != -ENODEV)
187 dev_warn(tt->dev, "failed to resubmit urb: %d\n", rc);
188}
189
190static int __devinit ttusbir_probe(struct usb_interface *intf,
191 const struct usb_device_id *id)
192{
193 struct ttusbir *tt;
194 struct usb_interface_descriptor *idesc;
195 struct usb_endpoint_descriptor *desc;
196 struct rc_dev *rc;
197 int i, j, ret;
198 int altsetting = -1;
199
200 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
201 rc = rc_allocate_device();
202 if (!tt || !rc) {
203 ret = -ENOMEM;
204 goto out;
205 }
206
207 /* find the correct alt setting */
208 for (i = 0; i < intf->num_altsetting && altsetting == -1; i++) {
209 int bulk_out_endp = -1, iso_in_endp = -1;
210
211 idesc = &intf->altsetting[i].desc;
212
213 for (j = 0; j < idesc->bNumEndpoints; j++) {
214 desc = &intf->altsetting[i].endpoint[j].desc;
215 if (usb_endpoint_dir_in(desc) &&
216 usb_endpoint_xfer_isoc(desc) &&
217 desc->wMaxPacketSize == 0x10)
218 iso_in_endp = j;
219 else if (usb_endpoint_dir_out(desc) &&
220 usb_endpoint_xfer_bulk(desc) &&
221 desc->wMaxPacketSize == 0x20)
222 bulk_out_endp = j;
223
224 if (bulk_out_endp != -1 && iso_in_endp != -1) {
225 tt->bulk_out_endp = bulk_out_endp;
226 tt->iso_in_endp = iso_in_endp;
227 altsetting = i;
228 break;
229 }
230 }
231 }
232
233 if (altsetting == -1) {
234 dev_err(&intf->dev, "cannot find expected altsetting\n");
235 ret = -ENODEV;
236 goto out;
237 }
238
239 tt->dev = &intf->dev;
240 tt->udev = interface_to_usbdev(intf);
241 tt->rc = rc;
242
243 ret = usb_set_interface(tt->udev, 0, altsetting);
244 if (ret)
245 goto out;
246
247 for (i = 0; i < NUM_URBS; i++) {
248 struct urb *urb = usb_alloc_urb(8, GFP_KERNEL);
249 void *buffer;
250
251 if (!urb) {
252 ret = -ENOMEM;
253 goto out;
254 }
255
256 urb->dev = tt->udev;
257 urb->context = tt;
258 urb->pipe = usb_rcvisocpipe(tt->udev, tt->iso_in_endp);
259 urb->interval = 1;
260 buffer = usb_alloc_coherent(tt->udev, 128, GFP_KERNEL,
261 &urb->transfer_dma);
262 if (!buffer) {
263 usb_free_urb(urb);
264 ret = -ENOMEM;
265 goto out;
266 }
267 urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP | URB_ISO_ASAP;
268 urb->transfer_buffer = buffer;
269 urb->complete = ttusbir_urb_complete;
270 urb->number_of_packets = 8;
271 urb->transfer_buffer_length = 128;
272
273 for (j = 0; j < 8; j++) {
274 urb->iso_frame_desc[j].offset = j * 16;
275 urb->iso_frame_desc[j].length = 16;
276 }
277
278 tt->urb[i] = urb;
279 }
280
281 tt->bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
282 if (!tt->bulk_urb) {
283 ret = -ENOMEM;
284 goto out;
285 }
286
287 tt->bulk_buffer[0] = 0xaa;
288 tt->bulk_buffer[1] = 0x01;
289 tt->bulk_buffer[2] = 0x05;
290 tt->bulk_buffer[3] = 0x01;
291
292 usb_fill_bulk_urb(tt->bulk_urb, tt->udev, usb_sndbulkpipe(tt->udev,
293 tt->bulk_out_endp), tt->bulk_buffer, sizeof(tt->bulk_buffer),
294 ttusbir_bulk_complete, tt);
295
296 tt->led.name = "ttusbir:yellow:power";
297 tt->led.brightness_set = ttusbir_brightness_set;
298 tt->led.brightness_get = ttusbir_brightness_get;
299 tt->is_led_on = tt->led_on = true;
300 atomic_set(&tt->led_complete, 0);
301 ret = led_classdev_register(&intf->dev, &tt->led);
302 if (ret)
303 goto out;
304
305 usb_make_path(tt->udev, tt->phys, sizeof(tt->phys));
306
307 rc->input_name = DRIVER_DESC;
308 rc->input_phys = tt->phys;
309 usb_to_input_id(tt->udev, &rc->input_id);
310 rc->dev.parent = &intf->dev;
311 rc->driver_type = RC_DRIVER_IR_RAW;
312 rc->allowed_protos = RC_TYPE_ALL;
313 rc->priv = tt;
314 rc->driver_name = DRIVER_NAME;
315 rc->map_name = RC_MAP_TT_1500;
316 rc->timeout = MS_TO_NS(100);
317 /*
318 * The precision is NS_PER_BIT, but since every 8th bit can be
319 * overwritten with garbage the accuracy is at best 2 * NS_PER_BIT.
320 */
321 rc->rx_resolution = NS_PER_BIT;
322
323 ret = rc_register_device(rc);
324 if (ret) {
325 dev_err(&intf->dev, "failed to register rc device %d\n", ret);
326 goto out2;
327 }
328
329 usb_set_intfdata(intf, tt);
330
331 for (i = 0; i < NUM_URBS; i++) {
332 ret = usb_submit_urb(tt->urb[i], GFP_KERNEL);
333 if (ret) {
334 dev_err(tt->dev, "failed to submit urb %d\n", ret);
335 goto out3;
336 }
337 }
338
339 return 0;
340out3:
341 rc_unregister_device(rc);
342out2:
343 led_classdev_unregister(&tt->led);
344out:
345 if (tt) {
346 for (i = 0; i < NUM_URBS && tt->urb[i]; i++) {
347 struct urb *urb = tt->urb[i];
348
349 usb_kill_urb(urb);
350 usb_free_coherent(tt->udev, 128, urb->transfer_buffer,
351 urb->transfer_dma);
352 usb_free_urb(urb);
353 }
354 usb_kill_urb(tt->bulk_urb);
355 usb_free_urb(tt->bulk_urb);
356 kfree(tt);
357 }
358 rc_free_device(rc);
359
360 return ret;
361}
362
363static void __devexit ttusbir_disconnect(struct usb_interface *intf)
364{
365 struct ttusbir *tt = usb_get_intfdata(intf);
366 int i;
367
368 rc_unregister_device(tt->rc);
369 led_classdev_unregister(&tt->led);
370 for (i = 0; i < NUM_URBS; i++) {
371 usb_kill_urb(tt->urb[i]);
372 usb_free_coherent(tt->udev, 128, tt->urb[i]->transfer_buffer,
373 tt->urb[i]->transfer_dma);
374 usb_free_urb(tt->urb[i]);
375 }
376 usb_kill_urb(tt->bulk_urb);
377 usb_free_urb(tt->bulk_urb);
378 usb_set_intfdata(intf, NULL);
379 kfree(tt);
380}
381
382static const struct usb_device_id ttusbir_table[] = {
383 { USB_DEVICE(0x0b48, 0x2003) },
384 { }
385};
386
387static struct usb_driver ttusbir_driver = {
388 .name = DRIVER_NAME,
389 .id_table = ttusbir_table,
390 .probe = ttusbir_probe,
391 .disconnect = __devexit_p(ttusbir_disconnect)
392};
393
394module_usb_driver(ttusbir_driver);
395
396MODULE_DESCRIPTION(DRIVER_DESC);
397MODULE_AUTHOR("Sean Young <sean@mess.org>");
398MODULE_LICENSE("GPL");
399MODULE_DEVICE_TABLE(usb, ttusbir_table);
400