···6363 help6464 Driver for the SIR IrDA port65656666-config LIRC_TTUSBIR6767- tristate "Technotrend USB IR Receiver"6868- depends on LIRC && USB6969- help7070- Driver for the Technotrend USB IR Receiver7171-7266config LIRC_ZILOG7367 tristate "Zilog/Hauppauge IR Transmitter"7468 depends on LIRC && I2C
···11-/*22- * lirc_ttusbir.c33- *44- * lirc_ttusbir - LIRC device driver for the TechnoTrend USB IR Receiver55- *66- * Copyright (C) 2007 Stefan Macher <st_maker-lirc@yahoo.de>77- *88- * This LIRC driver provides access to the TechnoTrend USB IR Receiver.99- * The receiver delivers the IR signal as raw sampled true/false data in1010- * isochronous USB packets each of size 128 byte.1111- * Currently the driver reduces the sampling rate by factor of 8 as this1212- * is still more than enough to decode RC-5 - others should be analyzed.1313- * But the driver does not rely on RC-5 it should be able to decode every1414- * IR signal that is not too fast.1515- */1616-1717-/*1818- * This program is free software; you can redistribute it and/or modify1919- * it under the terms of the GNU General Public License as published by2020- * the Free Software Foundation; either version 2 of the License, or2121- * (at your option) any later version.2222- *2323- * This program is distributed in the hope that it will be useful,2424- * but WITHOUT ANY WARRANTY; without even the implied warranty of2525- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the2626- * GNU General Public License for more details.2727- *2828- * You should have received a copy of the GNU General Public License2929- * along with this program; if not, write to the Free Software3030- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA3131- */3232-3333-#include <linux/kernel.h>3434-#include <linux/init.h>3535-#include <linux/module.h>3636-#include <linux/errno.h>3737-#include <linux/slab.h>3838-#include <linux/usb.h>3939-4040-#include <media/lirc.h>4141-#include <media/lirc_dev.h>4242-4343-MODULE_DESCRIPTION("TechnoTrend USB IR device driver for LIRC");4444-MODULE_AUTHOR("Stefan Macher (st_maker-lirc@yahoo.de)");4545-MODULE_LICENSE("GPL");4646-4747-/* #define DEBUG */4848-#ifdef DEBUG4949-#define DPRINTK printk5050-#else5151-#define DPRINTK(_x_, a...)5252-#endif5353-5454-/* function declarations */5555-static int probe(struct usb_interface *intf, const struct usb_device_id *id);5656-static void disconnect(struct usb_interface *intf);5757-static void urb_complete(struct urb *urb);5858-static int set_use_inc(void *data);5959-static void set_use_dec(void *data);6060-6161-static int num_urbs = 2;6262-module_param(num_urbs, int, S_IRUGO);6363-MODULE_PARM_DESC(num_urbs,6464- "Number of URBs in queue. Try to increase to 4 in case "6565- "of problems (default: 2; minimum: 2)");6666-6767-/* table of devices that work with this driver */6868-static struct usb_device_id device_id_table[] = {6969- /* TechnoTrend USB IR Receiver */7070- { USB_DEVICE(0x0B48, 0x2003) },7171- /* Terminating entry */7272- { }7373-};7474-MODULE_DEVICE_TABLE(usb, device_id_table);7575-7676-/* USB driver definition */7777-static struct usb_driver usb_driver = {7878- .name = "TTUSBIR",7979- .id_table = &(device_id_table[0]),8080- .probe = probe,8181- .disconnect = disconnect,8282-};8383-8484-/* USB device definition */8585-struct ttusbir_device {8686- struct usb_driver *usb_driver;8787- struct usb_device *udev;8888- struct usb_interface *interf;8989- struct usb_class_driver class_driver;9090- unsigned int ifnum; /* Interface number to use */9191- unsigned int alt_setting; /* alternate setting to use */9292- unsigned int endpoint; /* Endpoint to use */9393- struct urb **urb; /* num_urb URB pointers*/9494- char **buffer; /* 128 byte buffer for each URB */9595- struct lirc_buffer rbuf; /* Buffer towards LIRC */9696- struct lirc_driver driver;9797- int minor;9898- int last_pulse; /* remembers if last received byte was pulse or space */9999- int last_num; /* remembers how many last bytes appeared */100100- int opened;101101-};102102-103103-/*** LIRC specific functions ***/104104-static int set_use_inc(void *data)105105-{106106- int i, retval;107107- struct ttusbir_device *ttusbir = data;108108-109109- DPRINTK("Sending first URBs\n");110110- /* @TODO Do I need to check if I am already opened */111111- ttusbir->opened = 1;112112-113113- for (i = 0; i < num_urbs; i++) {114114- retval = usb_submit_urb(ttusbir->urb[i], GFP_KERNEL);115115- if (retval) {116116- dev_err(&ttusbir->interf->dev,117117- "%s: usb_submit_urb failed on urb %d\n",118118- __func__, i);119119- return retval;120120- }121121- }122122- return 0;123123-}124124-125125-static void set_use_dec(void *data)126126-{127127- struct ttusbir_device *ttusbir = data;128128-129129- DPRINTK("Device closed\n");130130-131131- ttusbir->opened = 0;132132-}133133-134134-/*** USB specific functions ***/135135-136136-/*137137- * This mapping table is used to do a very simple filtering of the138138- * input signal.139139- * For a value with at least 4 bits set it returns 0xFF otherwise140140- * 0x00. For faster IR signals this can not be used. But for RC-5 we141141- * still have about 14 samples per pulse/space, i.e. we sample with 14142142- * times higher frequency than the signal frequency143143- */144144-const unsigned char map_table[] = {145145- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,146146- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,147147- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,148148- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,149149- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,150150- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,151151- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,152152- 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,153153- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,154154- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,155155- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,156156- 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,157157- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,158158- 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,159159- 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,160160- 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,161161- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,162162- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,163163- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,164164- 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,165165- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,166166- 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,167167- 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,168168- 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,169169- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,170170- 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,171171- 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,172172- 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,173173- 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,174174- 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,175175- 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,176176- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF177177-};178178-179179-static void urb_complete(struct urb *urb)180180-{181181- struct ttusbir_device *ttusbir;182182- unsigned char *buf;183183- int i;184184- int l;185185-186186- ttusbir = urb->context;187187-188188- if (!ttusbir->opened)189189- return;190190-191191- buf = (unsigned char *)urb->transfer_buffer;192192-193193- for (i = 0; i < 128; i++) {194194- /* Here we do the filtering and some kind of down sampling */195195- buf[i] = ~map_table[buf[i]];196196- if (ttusbir->last_pulse == buf[i]) {197197- if (ttusbir->last_num < PULSE_MASK/63)198198- ttusbir->last_num++;199199- /*200200- * else we are in a idle period and do not need to201201- * increment any longer202202- */203203- } else {204204- l = ttusbir->last_num * 62; /* about 62 = us/byte */205205- if (ttusbir->last_pulse) /* pulse or space? */206206- l |= PULSE_BIT;207207- if (!lirc_buffer_full(&ttusbir->rbuf)) {208208- lirc_buffer_write(&ttusbir->rbuf, (void *)&l);209209- wake_up_interruptible(&ttusbir->rbuf.wait_poll);210210- }211211- ttusbir->last_num = 0;212212- ttusbir->last_pulse = buf[i];213213- }214214- }215215- usb_submit_urb(urb, GFP_ATOMIC); /* keep data rolling :-) */216216-}217217-218218-/*219219- * Called whenever the USB subsystem thinks we could be the right driver220220- * to handle this device221221- */222222-static int probe(struct usb_interface *intf, const struct usb_device_id *id)223223-{224224- int alt_set, endp;225225- int found = 0;226226- int i, j;227227- int struct_size;228228- struct usb_host_interface *host_interf;229229- struct usb_interface_descriptor *interf_desc;230230- struct usb_host_endpoint *host_endpoint;231231- struct ttusbir_device *ttusbir;232232-233233- DPRINTK("Module ttusbir probe\n");234234-235235- /* To reduce memory fragmentation we use only one allocation */236236- struct_size = sizeof(struct ttusbir_device) +237237- (sizeof(struct urb *) * num_urbs) +238238- (sizeof(char *) * num_urbs) +239239- (num_urbs * 128);240240- ttusbir = kzalloc(struct_size, GFP_KERNEL);241241- if (!ttusbir)242242- return -ENOMEM;243243-244244- ttusbir->urb = (struct urb **)((char *)ttusbir +245245- sizeof(struct ttusbir_device));246246- ttusbir->buffer = (char **)((char *)ttusbir->urb +247247- (sizeof(struct urb *) * num_urbs));248248- for (i = 0; i < num_urbs; i++)249249- ttusbir->buffer[i] = (char *)ttusbir->buffer +250250- (sizeof(char *)*num_urbs) + (i * 128);251251-252252- ttusbir->usb_driver = &usb_driver;253253- ttusbir->alt_setting = -1;254254- /* @TODO check if error can be returned */255255- ttusbir->udev = usb_get_dev(interface_to_usbdev(intf));256256- ttusbir->interf = intf;257257- ttusbir->last_pulse = 0x00;258258- ttusbir->last_num = 0;259259-260260- /*261261- * Now look for interface setting we can handle262262- * We are searching for the alt setting where end point263263- * 0x82 has max packet size 16264264- */265265- for (alt_set = 0; alt_set < intf->num_altsetting && !found; alt_set++) {266266- host_interf = &intf->altsetting[alt_set];267267- interf_desc = &host_interf->desc;268268- for (endp = 0; endp < interf_desc->bNumEndpoints; endp++) {269269- host_endpoint = &host_interf->endpoint[endp];270270- if ((host_endpoint->desc.bEndpointAddress == 0x82) &&271271- (host_endpoint->desc.wMaxPacketSize == 0x10)) {272272- ttusbir->alt_setting = alt_set;273273- ttusbir->endpoint = endp;274274- found = 1;275275- break;276276- }277277- }278278- }279279- if (ttusbir->alt_setting != -1)280280- DPRINTK("alt setting: %d\n", ttusbir->alt_setting);281281- else {282282- dev_err(&intf->dev, "Could not find alternate setting\n");283283- kfree(ttusbir);284284- return -EINVAL;285285- }286286-287287- /* OK lets setup this interface setting */288288- usb_set_interface(ttusbir->udev, 0, ttusbir->alt_setting);289289-290290- /* Store device info in interface structure */291291- usb_set_intfdata(intf, ttusbir);292292-293293- /* Register as a LIRC driver */294294- if (lirc_buffer_init(&ttusbir->rbuf, sizeof(int), 256) < 0) {295295- dev_err(&intf->dev, "Could not get memory for LIRC data buffer\n");296296- usb_set_intfdata(intf, NULL);297297- kfree(ttusbir);298298- return -ENOMEM;299299- }300300- strcpy(ttusbir->driver.name, "TTUSBIR");301301- ttusbir->driver.minor = -1;302302- ttusbir->driver.code_length = 1;303303- ttusbir->driver.sample_rate = 0;304304- ttusbir->driver.data = ttusbir;305305- ttusbir->driver.add_to_buf = NULL;306306- ttusbir->driver.rbuf = &ttusbir->rbuf;307307- ttusbir->driver.set_use_inc = set_use_inc;308308- ttusbir->driver.set_use_dec = set_use_dec;309309- ttusbir->driver.dev = &intf->dev;310310- ttusbir->driver.owner = THIS_MODULE;311311- ttusbir->driver.features = LIRC_CAN_REC_MODE2;312312- ttusbir->minor = lirc_register_driver(&ttusbir->driver);313313- if (ttusbir->minor < 0) {314314- dev_err(&intf->dev, "Error registering as LIRC driver\n");315315- usb_set_intfdata(intf, NULL);316316- lirc_buffer_free(&ttusbir->rbuf);317317- kfree(ttusbir);318318- return -EIO;319319- }320320-321321- /* Allocate and setup the URB that we will use to talk to the device */322322- for (i = 0; i < num_urbs; i++) {323323- ttusbir->urb[i] = usb_alloc_urb(8, GFP_KERNEL);324324- if (!ttusbir->urb[i]) {325325- dev_err(&intf->dev, "Could not allocate memory for the URB\n");326326- for (j = i - 1; j >= 0; j--)327327- kfree(ttusbir->urb[j]);328328- lirc_buffer_free(&ttusbir->rbuf);329329- lirc_unregister_driver(ttusbir->minor);330330- kfree(ttusbir);331331- usb_set_intfdata(intf, NULL);332332- return -ENOMEM;333333- }334334- ttusbir->urb[i]->dev = ttusbir->udev;335335- ttusbir->urb[i]->context = ttusbir;336336- ttusbir->urb[i]->pipe = usb_rcvisocpipe(ttusbir->udev,337337- ttusbir->endpoint);338338- ttusbir->urb[i]->interval = 1;339339- ttusbir->urb[i]->transfer_flags = URB_ISO_ASAP;340340- ttusbir->urb[i]->transfer_buffer = &ttusbir->buffer[i][0];341341- ttusbir->urb[i]->complete = urb_complete;342342- ttusbir->urb[i]->number_of_packets = 8;343343- ttusbir->urb[i]->transfer_buffer_length = 128;344344- for (j = 0; j < 8; j++) {345345- ttusbir->urb[i]->iso_frame_desc[j].offset = j*16;346346- ttusbir->urb[i]->iso_frame_desc[j].length = 16;347347- }348348- }349349- return 0;350350-}351351-352352-/**353353- * Called when the driver is unloaded or the device is unplugged354354- */355355-static void disconnect(struct usb_interface *intf)356356-{357357- int i;358358- struct ttusbir_device *ttusbir;359359-360360- DPRINTK("Module ttusbir disconnect\n");361361-362362- ttusbir = (struct ttusbir_device *) usb_get_intfdata(intf);363363- usb_set_intfdata(intf, NULL);364364- lirc_unregister_driver(ttusbir->minor);365365- DPRINTK("unregistered\n");366366-367367- for (i = 0; i < num_urbs; i++) {368368- usb_kill_urb(ttusbir->urb[i]);369369- usb_free_urb(ttusbir->urb[i]);370370- }371371- DPRINTK("URBs killed\n");372372- lirc_buffer_free(&ttusbir->rbuf);373373- kfree(ttusbir);374374-}375375-376376-module_usb_driver(usb_driver);