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