Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/lrg/voltage-2.6
[pandora-kernel.git] / drivers / staging / lirc / lirc_ttusbir.c
1 /*
2  * lirc_ttusbir.c
3  *
4  * lirc_ttusbir - LIRC device driver for the TechnoTrend USB IR Receiver
5  *
6  * Copyright (C) 2007 Stefan Macher <st_maker-lirc@yahoo.de>
7  *
8  * This LIRC driver provides access to the TechnoTrend USB IR Receiver.
9  * The receiver delivers the IR signal as raw sampled true/false data in
10  * isochronous USB packets each of size 128 byte.
11  * Currently the driver reduces the sampling rate by factor of 8 as this
12  * is still more than enough to decode RC-5 - others should be analyzed.
13  * But the driver does not rely on RC-5 it should be able to decode every
14  * IR signal that is not too fast.
15  */
16
17 /*
18  *  This program is free software; you can redistribute it and/or modify
19  *  it under the terms of the GNU General Public License as published by
20  *  the Free Software Foundation; either version 2 of the License, or
21  *  (at your option) any later version.
22  *
23  *  This program is distributed in the hope that it will be useful,
24  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *  GNU General Public License for more details.
27  *
28  *  You should have received a copy of the GNU General Public License
29  *  along with this program; if not, write to the Free Software
30  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  */
32
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/module.h>
36 #include <linux/errno.h>
37 #include <linux/slab.h>
38 #include <linux/usb.h>
39
40 #include <media/lirc.h>
41 #include <media/lirc_dev.h>
42
43 MODULE_DESCRIPTION("TechnoTrend USB IR device driver for LIRC");
44 MODULE_AUTHOR("Stefan Macher (st_maker-lirc@yahoo.de)");
45 MODULE_LICENSE("GPL");
46
47 /* #define DEBUG */
48 #ifdef DEBUG
49 #define DPRINTK printk
50 #else
51 #define DPRINTK(_x_, a...)
52 #endif
53
54 /* function declarations */
55 static int probe(struct usb_interface *intf, const struct usb_device_id *id);
56 static void disconnect(struct usb_interface *intf);
57 static void urb_complete(struct urb *urb);
58 static int set_use_inc(void *data);
59 static void set_use_dec(void *data);
60
61 static int num_urbs = 2;
62 module_param(num_urbs, int, S_IRUGO);
63 MODULE_PARM_DESC(num_urbs,
64                  "Number of URBs in queue. Try to increase to 4 in case "
65                  "of problems (default: 2; minimum: 2)");
66
67 /* table of devices that work with this driver */
68 static struct usb_device_id device_id_table[] = {
69         /* TechnoTrend USB IR Receiver */
70         { USB_DEVICE(0x0B48, 0x2003) },
71         /* Terminating entry */
72         { }
73 };
74 MODULE_DEVICE_TABLE(usb, device_id_table);
75
76 /* USB driver definition */
77 static struct usb_driver usb_driver = {
78         .name = "TTUSBIR",
79         .id_table = &(device_id_table[0]),
80         .probe = probe,
81         .disconnect = disconnect,
82 };
83
84 /* USB device definition */
85 struct ttusbir_device {
86         struct usb_driver *usb_driver;
87         struct usb_device *udev;
88         struct usb_interface *interf;
89         struct usb_class_driver class_driver;
90         unsigned int ifnum; /* Interface number to use */
91         unsigned int alt_setting; /* alternate setting to use */
92         unsigned int endpoint; /* Endpoint to use */
93         struct urb **urb; /* num_urb URB pointers*/
94         char **buffer; /* 128 byte buffer for each URB */
95         struct lirc_buffer rbuf; /* Buffer towards LIRC */
96         struct lirc_driver driver;
97         int minor;
98         int last_pulse; /* remembers if last received byte was pulse or space */
99         int last_num; /* remembers how many last bytes appeared */
100         int opened;
101 };
102
103 /*** LIRC specific functions ***/
104 static int set_use_inc(void *data)
105 {
106         int i, retval;
107         struct ttusbir_device *ttusbir = data;
108
109         DPRINTK("Sending first URBs\n");
110         /* @TODO Do I need to check if I am already opened */
111         ttusbir->opened = 1;
112
113         for (i = 0; i < num_urbs; i++) {
114                 retval = usb_submit_urb(ttusbir->urb[i], GFP_KERNEL);
115                 if (retval) {
116                         err("%s: usb_submit_urb failed on urb %d",
117                             __func__, i);
118                         return retval;
119                 }
120         }
121         return 0;
122 }
123
124 static void set_use_dec(void *data)
125 {
126         struct ttusbir_device *ttusbir = data;
127
128         DPRINTK("Device closed\n");
129
130         ttusbir->opened = 0;
131 }
132
133 /*** USB specific functions ***/
134
135 /*
136  * This mapping table is used to do a very simple filtering of the
137  * input signal.
138  * For a value with at least 4 bits set it returns 0xFF otherwise
139  * 0x00.  For faster IR signals this can not be used. But for RC-5 we
140  * still have about 14 samples per pulse/space, i.e. we sample with 14
141  * times higher frequency than the signal frequency
142  */
143 const unsigned char map_table[] = {
144         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
145         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
146         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
147         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
148         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
149         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
150         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
151         0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
152         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
154         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
155         0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
156         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
157         0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
158         0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
159         0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
160         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
161         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
162         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
163         0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
164         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
165         0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
166         0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
167         0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
168         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
169         0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
170         0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
171         0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
172         0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
173         0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
174         0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
175         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
176 };
177
178 static void urb_complete(struct urb *urb)
179 {
180         struct ttusbir_device *ttusbir;
181         unsigned char *buf;
182         int i;
183         int l;
184
185         ttusbir = urb->context;
186
187         if (!ttusbir->opened)
188                 return;
189
190         buf = (unsigned char *)urb->transfer_buffer;
191
192         for (i = 0; i < 128; i++) {
193                 /* Here we do the filtering and some kind of down sampling */
194                 buf[i] = ~map_table[buf[i]];
195                 if (ttusbir->last_pulse == buf[i]) {
196                         if (ttusbir->last_num < PULSE_MASK/63)
197                                 ttusbir->last_num++;
198                 /*
199                  * else we are in a idle period and do not need to
200                  * increment any longer
201                  */
202                 } else {
203                         l = ttusbir->last_num * 62; /* about 62 = us/byte */
204                         if (ttusbir->last_pulse) /* pulse or space? */
205                                 l |= PULSE_BIT;
206                         if (!lirc_buffer_full(&ttusbir->rbuf)) {
207                                 lirc_buffer_write(&ttusbir->rbuf, (void *)&l);
208                                 wake_up_interruptible(&ttusbir->rbuf.wait_poll);
209                         }
210                         ttusbir->last_num = 0;
211                         ttusbir->last_pulse = buf[i];
212                 }
213         }
214         usb_submit_urb(urb, GFP_ATOMIC); /* keep data rolling :-) */
215 }
216
217 /*
218  * Called whenever the USB subsystem thinks we could be the right driver
219  * to handle this device
220  */
221 static int probe(struct usb_interface *intf, const struct usb_device_id *id)
222 {
223         int alt_set, endp;
224         int found = 0;
225         int i, j;
226         int struct_size;
227         struct usb_host_interface *host_interf;
228         struct usb_interface_descriptor *interf_desc;
229         struct usb_host_endpoint *host_endpoint;
230         struct ttusbir_device *ttusbir;
231
232         DPRINTK("Module ttusbir probe\n");
233
234         /* To reduce memory fragmentation we use only one allocation */
235         struct_size =  sizeof(struct ttusbir_device) +
236                 (sizeof(struct urb *) * num_urbs) +
237                 (sizeof(char *) * num_urbs) +
238                 (num_urbs * 128);
239         ttusbir = kzalloc(struct_size, GFP_KERNEL);
240         if (!ttusbir)
241                 return -ENOMEM;
242
243         ttusbir->urb = (struct urb **)((char *)ttusbir +
244                                       sizeof(struct ttusbir_device));
245         ttusbir->buffer = (char **)((char *)ttusbir->urb +
246                                    (sizeof(struct urb *) * num_urbs));
247         for (i = 0; i < num_urbs; i++)
248                 ttusbir->buffer[i] = (char *)ttusbir->buffer +
249                         (sizeof(char *)*num_urbs) + (i * 128);
250
251         ttusbir->usb_driver = &usb_driver;
252         ttusbir->alt_setting = -1;
253         /* @TODO check if error can be returned */
254         ttusbir->udev = usb_get_dev(interface_to_usbdev(intf));
255         ttusbir->interf = intf;
256         ttusbir->last_pulse = 0x00;
257         ttusbir->last_num = 0;
258
259         /*
260          * Now look for interface setting we can handle
261          * We are searching for the alt setting where end point
262          * 0x82 has max packet size 16
263          */
264         for (alt_set = 0; alt_set < intf->num_altsetting && !found; alt_set++) {
265                 host_interf = &intf->altsetting[alt_set];
266                 interf_desc = &host_interf->desc;
267                 for (endp = 0; endp < interf_desc->bNumEndpoints; endp++) {
268                         host_endpoint = &host_interf->endpoint[endp];
269                         if ((host_endpoint->desc.bEndpointAddress == 0x82) &&
270                             (host_endpoint->desc.wMaxPacketSize == 0x10)) {
271                                 ttusbir->alt_setting = alt_set;
272                                 ttusbir->endpoint = endp;
273                                 found = 1;
274                                 break;
275                         }
276                 }
277         }
278         if (ttusbir->alt_setting != -1)
279                 DPRINTK("alt setting: %d\n", ttusbir->alt_setting);
280         else {
281                 err("Could not find alternate setting\n");
282                 kfree(ttusbir);
283                 return -EINVAL;
284         }
285
286         /* OK lets setup this interface setting */
287         usb_set_interface(ttusbir->udev, 0, ttusbir->alt_setting);
288
289         /* Store device info in interface structure */
290         usb_set_intfdata(intf, ttusbir);
291
292         /* Register as a LIRC driver */
293         if (lirc_buffer_init(&ttusbir->rbuf, sizeof(int), 256) < 0) {
294                 err("Could not get memory for LIRC data buffer\n");
295                 usb_set_intfdata(intf, NULL);
296                 kfree(ttusbir);
297                 return -ENOMEM;
298         }
299         strcpy(ttusbir->driver.name, "TTUSBIR");
300         ttusbir->driver.minor = -1;
301         ttusbir->driver.code_length = 1;
302         ttusbir->driver.sample_rate = 0;
303         ttusbir->driver.data = ttusbir;
304         ttusbir->driver.add_to_buf = NULL;
305         ttusbir->driver.rbuf = &ttusbir->rbuf;
306         ttusbir->driver.set_use_inc = set_use_inc;
307         ttusbir->driver.set_use_dec = set_use_dec;
308         ttusbir->driver.dev = &intf->dev;
309         ttusbir->driver.owner = THIS_MODULE;
310         ttusbir->driver.features = LIRC_CAN_REC_MODE2;
311         ttusbir->minor = lirc_register_driver(&ttusbir->driver);
312         if (ttusbir->minor < 0) {
313                 err("Error registering as LIRC driver\n");
314                 usb_set_intfdata(intf, NULL);
315                 lirc_buffer_free(&ttusbir->rbuf);
316                 kfree(ttusbir);
317                 return -EIO;
318         }
319
320         /* Allocate and setup the URB that we will use to talk to the device */
321         for (i = 0; i < num_urbs; i++) {
322                 ttusbir->urb[i] = usb_alloc_urb(8, GFP_KERNEL);
323                 if (!ttusbir->urb[i]) {
324                         err("Could not allocate memory for the URB\n");
325                         for (j = i - 1; j >= 0; j--)
326                                 kfree(ttusbir->urb[j]);
327                         lirc_buffer_free(&ttusbir->rbuf);
328                         lirc_unregister_driver(ttusbir->minor);
329                         kfree(ttusbir);
330                         usb_set_intfdata(intf, NULL);
331                         return -ENOMEM;
332                 }
333                 ttusbir->urb[i]->dev = ttusbir->udev;
334                 ttusbir->urb[i]->context = ttusbir;
335                 ttusbir->urb[i]->pipe = usb_rcvisocpipe(ttusbir->udev,
336                                                         ttusbir->endpoint);
337                 ttusbir->urb[i]->interval = 1;
338                 ttusbir->urb[i]->transfer_flags = URB_ISO_ASAP;
339                 ttusbir->urb[i]->transfer_buffer = &ttusbir->buffer[i][0];
340                 ttusbir->urb[i]->complete = urb_complete;
341                 ttusbir->urb[i]->number_of_packets = 8;
342                 ttusbir->urb[i]->transfer_buffer_length = 128;
343                 for (j = 0; j < 8; j++) {
344                         ttusbir->urb[i]->iso_frame_desc[j].offset = j*16;
345                         ttusbir->urb[i]->iso_frame_desc[j].length = 16;
346                 }
347         }
348         return 0;
349 }
350
351 /**
352  * Called when the driver is unloaded or the device is unplugged
353  */
354 static void disconnect(struct usb_interface *intf)
355 {
356         int i;
357         struct ttusbir_device *ttusbir;
358
359         DPRINTK("Module ttusbir disconnect\n");
360
361         ttusbir = (struct ttusbir_device *) usb_get_intfdata(intf);
362         usb_set_intfdata(intf, NULL);
363         lirc_unregister_driver(ttusbir->minor);
364         DPRINTK("unregistered\n");
365
366         for (i = 0; i < num_urbs; i++) {
367                 usb_kill_urb(ttusbir->urb[i]);
368                 usb_free_urb(ttusbir->urb[i]);
369         }
370         DPRINTK("URBs killed\n");
371         lirc_buffer_free(&ttusbir->rbuf);
372         kfree(ttusbir);
373 }
374
375 static int ttusbir_init_module(void)
376 {
377         int result;
378
379         DPRINTK(KERN_DEBUG "Module ttusbir init\n");
380
381         /* register this driver with the USB subsystem */
382         result = usb_register(&usb_driver);
383         if (result)
384                 err("usb_register failed. Error number %d", result);
385         return result;
386 }
387
388 static void ttusbir_exit_module(void)
389 {
390         printk(KERN_DEBUG "Module ttusbir exit\n");
391         usb_deregister(&usb_driver);
392 }
393
394 module_init(ttusbir_init_module);
395 module_exit(ttusbir_exit_module);