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