|
Line 0
Link Here
|
|
|
1 |
/* lirc_streamzap - USB StreamZap remote driver |
| 2 |
* Alpha status, currently Works For Me(tm). Feedback welcome. |
| 3 |
* Version 0.06 |
| 4 |
* |
| 5 |
* Copyright (C) 2002-2003 Adrian Dewhurst <sailor-lk@sailorfrag.net> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
# include <config.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <linux/config.h> |
| 27 |
#ifdef CONFIG_SMP |
| 28 |
# define __SMP__ |
| 29 |
#endif |
| 30 |
|
| 31 |
#include <linux/version.h> |
| 32 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 0) |
| 33 |
#error "*******************************************************" |
| 34 |
#error "Sorry, this driver needs kernel version 2.4.0 or higher" |
| 35 |
#error "*******************************************************" |
| 36 |
#endif |
| 37 |
|
| 38 |
#include <linux/module.h> |
| 39 |
#include <linux/slab.h> |
| 40 |
#include <linux/usb.h> |
| 41 |
#include <linux/sched.h> |
| 42 |
#include <linux/kmod.h> |
| 43 |
#include <linux/errno.h> |
| 44 |
|
| 45 |
#include "drivers/lirc.h" |
| 46 |
#include "drivers/lirc_dev/lirc_dev.h" |
| 47 |
|
| 48 |
#ifdef CONFIG_USB_DEBUG |
| 49 |
static int debug = 1; |
| 50 |
#else |
| 51 |
static int debug; |
| 52 |
#endif |
| 53 |
static int minor = -1; |
| 54 |
|
| 55 |
#undef dbg |
| 56 |
#define dbg(format, arg...) do { if (debug) printk(KERN_DEBUG __FILE__ ": " format "\n" , ## arg); } while (0) |
| 57 |
|
| 58 |
#define DRIVER_VERSION "v0.06" |
| 59 |
#define DRIVER_AUTHOR "Adrian Dewhurst, sailor-lk@sailorfrag.net" |
| 60 |
#define DRIVER_DESC "lirc USB StreamZap Remote driver" |
| 61 |
#define LIRC_DRIVER_NAME "lirc_streamzap" |
| 62 |
#define USB_STREAMZAP_VENDOR_ID 0x0e9c |
| 63 |
#define USB_STREAMZAP_PRODUCT_ID 0x0000 |
| 64 |
|
| 65 |
MODULE_PARM(debug, "i"); |
| 66 |
MODULE_PARM_DESC(debug, "Debug enabled or not"); |
| 67 |
MODULE_PARM(minor, "i"); |
| 68 |
MODULE_PARM_DESC(minor, "Preferred minor to use for LIRC device"); |
| 69 |
|
| 70 |
EXPORT_NO_SYMBOLS; |
| 71 |
|
| 72 |
static struct usb_device_id streamzap_table [] = { |
| 73 |
{ USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) }, |
| 74 |
{ } /* Terminating entry */ |
| 75 |
}; |
| 76 |
|
| 77 |
MODULE_DEVICE_TABLE(usb, streamzap_table); |
| 78 |
|
| 79 |
struct usb_streamzap { |
| 80 |
struct usb_device *udev; /* usb device struct; NULL upon disconnect */ |
| 81 |
struct lirc_plugin plugin; /* our lirc_dev device info */ |
| 82 |
struct urb irq; /* our interrupt URB */ |
| 83 |
|
| 84 |
__u16 int_in_size; /* size of interrupt buffer */ |
| 85 |
unsigned char *int_in_buffer; /* URB interrupt buffer */ |
| 86 |
|
| 87 |
unsigned char tmpbuf; /* for tracking long codes between interrupts */ |
| 88 |
unsigned char ign; |
| 89 |
struct lirc_buffer outbuf; /* kernel space -> user space buffer */ |
| 90 |
spinlock_t interrupt_lock; |
| 91 |
|
| 92 |
unsigned int open_count; |
| 93 |
|
| 94 |
struct semaphore lock; /* locks this structure */ |
| 95 |
}; |
| 96 |
|
| 97 |
/* USB functions */ |
| 98 |
static void streamzap_irq(struct urb *urb); |
| 99 |
static void * streamzap_probe(struct usb_device *dev, unsigned intf, const struct usb_device_id *id); |
| 100 |
static void streamzap_disconnect(struct usb_device *dev, void *data); |
| 101 |
|
| 102 |
static struct usb_driver streamzap_driver = { |
| 103 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 4, 20) |
| 104 |
owner: THIS_MODULE, |
| 105 |
#endif |
| 106 |
name: "lirc_streamzap", |
| 107 |
probe: streamzap_probe, |
| 108 |
disconnect: streamzap_disconnect, |
| 109 |
id_table: streamzap_table, |
| 110 |
}; |
| 111 |
|
| 112 |
/* LIRC functions */ |
| 113 |
static int streamzap_use_inc(void *data); |
| 114 |
static void streamzap_use_dec(void *data); |
| 115 |
|
| 116 |
static struct lirc_plugin streamzap_lirc_template = { |
| 117 |
minor: -1, /* Fill this in */ |
| 118 |
sample_rate: 0, |
| 119 |
code_length: 32, |
| 120 |
features: LIRC_CAN_REC_MODE2, |
| 121 |
data: NULL, /* Fill this in */ |
| 122 |
rbuf: NULL, /* Fill this in */ |
| 123 |
set_use_inc: streamzap_use_inc, |
| 124 |
set_use_dec: streamzap_use_dec, |
| 125 |
}; |
| 126 |
|
| 127 |
/* USB */ |
| 128 |
static void * streamzap_probe(struct usb_device *dev, unsigned intf, const struct usb_device_id *id) { |
| 129 |
struct usb_interface *iface; |
| 130 |
struct usb_interface_descriptor *iface_desc; |
| 131 |
struct usb_endpoint_descriptor *endpoint; |
| 132 |
struct usb_streamzap *sz = NULL; |
| 133 |
int pipe; |
| 134 |
int result; |
| 135 |
char buf[63], name[128]=""; |
| 136 |
|
| 137 |
/* Get the device */ |
| 138 |
iface = &dev->actconfig->interface[intf]; |
| 139 |
iface_desc = &iface->altsetting[iface->act_altsetting]; |
| 140 |
|
| 141 |
/* Find out if we want it */ |
| 142 |
if ((dev->descriptor.idVendor != USB_STREAMZAP_VENDOR_ID) || |
| 143 |
(dev->descriptor.idProduct != USB_STREAMZAP_PRODUCT_ID)) { |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
|
| 147 |
/* Find the endpoint */ |
| 148 |
if (iface_desc->bNumEndpoints != 1) return NULL; |
| 149 |
endpoint = iface_desc->endpoint + 0; |
| 150 |
if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN) return NULL; |
| 151 |
if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) return NULL; |
| 152 |
pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress); |
| 153 |
|
| 154 |
/* Allocate our device struct */ |
| 155 |
if (!(sz = kmalloc(sizeof(*sz), GFP_KERNEL))) { |
| 156 |
err("Out of memory allocating streamzap device"); |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
memset(sz, 0, sizeof(*sz)); |
| 160 |
|
| 161 |
/* Initialize locks and data */ |
| 162 |
sz->udev = dev; |
| 163 |
init_MUTEX(&sz->lock); |
| 164 |
spin_lock_init(&sz->interrupt_lock); |
| 165 |
|
| 166 |
/* Allocate the URB buffer */ |
| 167 |
sz->int_in_size = endpoint->wMaxPacketSize; |
| 168 |
if (!(sz->int_in_buffer = kmalloc(sz->int_in_size, GFP_KERNEL))) { |
| 169 |
err("Out of memory allocating streamzap interrupt buffer"); |
| 170 |
goto error; |
| 171 |
} |
| 172 |
FILL_INT_URB(&sz->irq, dev, pipe, sz->int_in_buffer, sz->int_in_size, streamzap_irq, sz, endpoint->bInterval); |
| 173 |
dbg("buffer_size %hu, buffer %p, interval %hu", sz->int_in_size, sz->int_in_buffer, endpoint->bInterval); |
| 174 |
|
| 175 |
/* Allocate the lirc buffer */ |
| 176 |
lirc_buffer_init(&sz->outbuf, 4 /* 32 bits/chunk */, 128 /* the default size of 16 is a bit small */); |
| 177 |
|
| 178 |
/* Set up the lirc plugin struct */ |
| 179 |
memcpy(&sz->plugin, &streamzap_lirc_template, sizeof(struct lirc_plugin)); |
| 180 |
sz->plugin.data = sz; |
| 181 |
sz->plugin.rbuf = &sz->outbuf; |
| 182 |
sz->plugin.minor = minor; |
| 183 |
|
| 184 |
/* Set up the plugin interface */ |
| 185 |
dbg("Attempting to register plugin on minor %d", sz->plugin.minor); |
| 186 |
if ((result = lirc_register_plugin(&sz->plugin)) < 0) { |
| 187 |
#if 0 |
| 188 |
/* It might be just that the preferred minor is in use. Try again |
| 189 |
* without trying for a specific minor. */ |
| 190 |
if (result == -EBUSY) { |
| 191 |
info("lirc_streamzap: usb%d:%d.%d: Preferred minor %d already in use. Trying another.", dev->bus->busnum, dev->devnum, intf, minor); |
| 192 |
sz->plugin.minor = -1; |
| 193 |
if ((result = lirc_register_plugin(&sz->plugin)) < 0) { |
| 194 |
goto error2; |
| 195 |
} |
| 196 |
} else { |
| 197 |
goto error2; |
| 198 |
} |
| 199 |
#else |
| 200 |
goto error2; |
| 201 |
#endif |
| 202 |
} |
| 203 |
sz->plugin.minor = result; |
| 204 |
dbg("Registered on minor %d", sz->plugin.minor); |
| 205 |
|
| 206 |
/* Grab the USB name string */ |
| 207 |
if (dev->descriptor.iManufacturer && usb_string(dev, dev->descriptor.iManufacturer, buf, 63) > 0) strcpy(name, buf); |
| 208 |
if (dev->descriptor.iProduct && usb_string(dev, dev->descriptor.iProduct, buf, 63) > 0) sprintf(name, "%s %s", name, buf); |
| 209 |
info("lirc%d: %s on usb%d:%d.%d", sz->plugin.minor, name, dev->bus->busnum, dev->devnum, intf); |
| 210 |
|
| 211 |
goto exit; |
| 212 |
|
| 213 |
error2: |
| 214 |
err("usb%d:%d.%d: Unable to register plugin with lirc_dev.", dev->bus->busnum, dev->devnum, intf); |
| 215 |
lirc_buffer_free(&sz->outbuf); |
| 216 |
kfree(sz->int_in_buffer); |
| 217 |
error: |
| 218 |
kfree(sz); |
| 219 |
sz=NULL; |
| 220 |
exit: |
| 221 |
return sz; |
| 222 |
} |
| 223 |
|
| 224 |
static void streamzap_disconnect(struct usb_device *udev, void *data) |
| 225 |
{ |
| 226 |
struct usb_streamzap *sz=(struct usb_streamzap *)data; |
| 227 |
int result, flags; |
| 228 |
|
| 229 |
dbg("Entering streamzap_disconnect"); |
| 230 |
|
| 231 |
/* Stop everything else */ |
| 232 |
sz->udev = NULL; |
| 233 |
|
| 234 |
down(&sz->lock); |
| 235 |
if (sz->open_count) { |
| 236 |
spin_lock_irqsave(&sz->interrupt_lock, flags); |
| 237 |
usb_unlink_urb(&sz->irq); |
| 238 |
spin_unlock_irqrestore(&sz->interrupt_lock, flags); |
| 239 |
} |
| 240 |
|
| 241 |
info("lirc%d: disconnected", sz->plugin.minor); |
| 242 |
|
| 243 |
if ((result = lirc_unregister_plugin(sz->plugin.minor))) { |
| 244 |
err("lirc_unregister_plugin(%d) failed: %d", sz->plugin.minor, result); |
| 245 |
} |
| 246 |
lirc_buffer_free(&sz->outbuf); |
| 247 |
kfree(sz->int_in_buffer); |
| 248 |
kfree(sz); |
| 249 |
} |
| 250 |
|
| 251 |
static inline void usb_streamzap_debug_data(const char *function, int size, const unsigned char *data) |
| 252 |
{ |
| 253 |
int i; |
| 254 |
|
| 255 |
if (!debug) return; |
| 256 |
|
| 257 |
printk(KERN_DEBUG __FILE__": %s: length %d, data ", function, size); |
| 258 |
for (i = 0; i < size; ++i) { |
| 259 |
printk("%.2x ", data[i]); |
| 260 |
} |
| 261 |
printk("\n"); |
| 262 |
} |
| 263 |
|
| 264 |
static void streamzap_irq(struct urb *urb) |
| 265 |
{ |
| 266 |
struct usb_streamzap *sz=urb->context; |
| 267 |
unsigned long flags; |
| 268 |
unsigned int i=0; |
| 269 |
uint32_t tmp; |
| 270 |
|
| 271 |
if (urb->status) return; |
| 272 |
if (urb->actual_length == 0) return; |
| 273 |
|
| 274 |
if (!sz) { |
| 275 |
dbg("streamzap_irq called with no context"); |
| 276 |
usb_unlink_urb(urb); |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
usb_streamzap_debug_data(__FUNCTION__, urb->actual_length, urb->transfer_buffer); |
| 281 |
|
| 282 |
spin_lock_irqsave(&sz->interrupt_lock, flags); |
| 283 |
/* I can't really explain how this works (StreamZap's request). It should |
| 284 |
* work properly though. */ |
| 285 |
#define check_buffer() if (lirc_buffer_full(&sz->outbuf)) { err("lirc%d: out of buffer space", sz->plugin.minor); goto out; } |
| 286 |
if (sz->tmpbuf) { |
| 287 |
if ((sz->tmpbuf & 0xf0) == 0xf0) { |
| 288 |
check_buffer(); |
| 289 |
tmp = (sz->int_in_buffer[0] * 256) | 0x01000000; |
| 290 |
lirc_buffer_write_1(&sz->outbuf, (char *)&tmp); |
| 291 |
|
| 292 |
sz->ign = 1; |
| 293 |
} else { |
| 294 |
if (!sz->ign) { |
| 295 |
tmp = (((sz->tmpbuf & 0xf0) >> 4) * 256); |
| 296 |
tmp = (tmp ? tmp : 1) | 0x01000000; |
| 297 |
lirc_buffer_write_1(&sz->outbuf, (char *)&tmp); |
| 298 |
} |
| 299 |
|
| 300 |
check_buffer(); |
| 301 |
tmp = sz->int_in_buffer[0] * 256; |
| 302 |
tmp = tmp ? tmp : 1; |
| 303 |
lirc_buffer_write_1(&sz->outbuf, (char *)&tmp); |
| 304 |
|
| 305 |
sz->ign = 0; |
| 306 |
} |
| 307 |
|
| 308 |
sz->tmpbuf = 0; |
| 309 |
i++; |
| 310 |
} |
| 311 |
|
| 312 |
for (; i < urb->actual_length; i++) { |
| 313 |
if ((sz->int_in_buffer[i] & 0xf0) == 0xf0 || (sz->int_in_buffer[i] & 0x0f) == 0x0f) { |
| 314 |
if (i+1 >= urb->actual_length) { |
| 315 |
sz->tmpbuf = sz->int_in_buffer[i]; |
| 316 |
break; |
| 317 |
} |
| 318 |
|
| 319 |
if ((sz->int_in_buffer[i] & 0xf0) == 0xf0) { |
| 320 |
check_buffer(); |
| 321 |
tmp = sz->int_in_buffer[i+1] * 256; |
| 322 |
tmp = (tmp ? tmp : 1) | 0x01000000; |
| 323 |
lirc_buffer_write_1(&sz->outbuf, (char *)&tmp); |
| 324 |
|
| 325 |
sz->ign = 1; |
| 326 |
} else { |
| 327 |
if (!sz->ign) { |
| 328 |
check_buffer(); |
| 329 |
tmp = ((sz->int_in_buffer[i] & 0xf0) >> 4) * 256; |
| 330 |
tmp = (tmp ? tmp : 1) | 0x01000000; |
| 331 |
lirc_buffer_write_1(&sz->outbuf, (char *)&tmp); |
| 332 |
} |
| 333 |
|
| 334 |
check_buffer(); |
| 335 |
tmp = sz->int_in_buffer[i+1] * 256; |
| 336 |
tmp = (tmp ? tmp : 1); |
| 337 |
lirc_buffer_write_1(&sz->outbuf, (char *)&tmp); |
| 338 |
|
| 339 |
sz->ign = 0; |
| 340 |
} |
| 341 |
i++; |
| 342 |
} else { |
| 343 |
check_buffer(); |
| 344 |
tmp = ((sz->int_in_buffer[i] & 0xf0) >> 4) * 256; |
| 345 |
tmp = (tmp ? tmp : 1) | 0x01000000; |
| 346 |
lirc_buffer_write_1(&sz->outbuf, (char *)&tmp); |
| 347 |
|
| 348 |
check_buffer(); |
| 349 |
tmp = (sz->int_in_buffer[i] & 0x0f) * 256; |
| 350 |
tmp = tmp ? tmp : 1; |
| 351 |
lirc_buffer_write_1(&sz->outbuf, (char *)&tmp); |
| 352 |
} |
| 353 |
} |
| 354 |
#undef check_buffer |
| 355 |
out: |
| 356 |
spin_unlock_irqrestore(&sz->interrupt_lock, flags); |
| 357 |
wake_up(&sz->outbuf.wait_poll); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
/* LIRC */ |
| 362 |
static int streamzap_use_inc(void *data) { |
| 363 |
struct usb_streamzap *sz=(struct usb_streamzap *)data; |
| 364 |
int retval=0; |
| 365 |
|
| 366 |
dbg("Entering streamzap_use_inc for minor %d", sz->plugin.minor); |
| 367 |
MOD_INC_USE_COUNT; |
| 368 |
if (down_interruptible(&sz->lock)) { |
| 369 |
MOD_DEC_USE_COUNT; |
| 370 |
dbg("streamzap_use_inc for minor %d returns -ERESTARTSYS", sz->plugin.minor); |
| 371 |
return -ERESTARTSYS; |
| 372 |
} |
| 373 |
|
| 374 |
if (!sz->udev) { |
| 375 |
up(&sz->lock); |
| 376 |
return -ENODEV; |
| 377 |
} |
| 378 |
|
| 379 |
if (!sz->open_count++) { |
| 380 |
sz->irq.dev = sz->udev; |
| 381 |
if ((retval = usb_submit_urb(&sz->irq))) { |
| 382 |
info("streamzap_use_inc(minor %d): usb_submit_urb returned %d", sz->plugin.minor, retval); |
| 383 |
dbg("streamzap_use_inc for minor %d returns -EIO", sz->plugin.minor); |
| 384 |
retval = -EIO; |
| 385 |
goto fail; |
| 386 |
} |
| 387 |
} |
| 388 |
dbg("streamzap_use_inc for minor %d success (open_count %u)", sz->plugin.minor, sz->open_count); |
| 389 |
goto exit; |
| 390 |
|
| 391 |
fail: |
| 392 |
MOD_DEC_USE_COUNT; |
| 393 |
exit: |
| 394 |
up(&sz->lock); |
| 395 |
return retval; |
| 396 |
} |
| 397 |
|
| 398 |
static void streamzap_use_dec(void *data) { |
| 399 |
struct usb_streamzap *sz=(struct usb_streamzap *)data; |
| 400 |
|
| 401 |
dbg("Entering streamzap_use_dec for minor %d", sz->plugin.minor); |
| 402 |
down(&sz->lock); |
| 403 |
if (sz->open_count == 0) { |
| 404 |
info("streamzap_use_dec called when use count is 0!"); |
| 405 |
return; |
| 406 |
} |
| 407 |
|
| 408 |
if (!--sz->open_count) usb_unlink_urb(&sz->irq); |
| 409 |
|
| 410 |
up(&sz->lock); |
| 411 |
MOD_DEC_USE_COUNT; |
| 412 |
|
| 413 |
dbg("Exiting streamzap_use_dec for minor %d (open_count %u)", sz->plugin.minor, sz->open_count); |
| 414 |
} |
| 415 |
|
| 416 |
/* Module functions */ |
| 417 |
static int __init streamzap_init(void) |
| 418 |
{ |
| 419 |
int result; |
| 420 |
|
| 421 |
if (MAX_IRCTL_DEVICES < minor) { |
| 422 |
err("Parameter minor (%d) must be less than %d!", |
| 423 |
minor, MAX_IRCTL_DEVICES-1); |
| 424 |
return -EINVAL; |
| 425 |
} |
| 426 |
|
| 427 |
if ((result = usb_register(&streamzap_driver)) < 0) { |
| 428 |
err("usb_register failed. Error number %d", result); |
| 429 |
return -EINVAL; |
| 430 |
} |
| 431 |
|
| 432 |
info(DRIVER_AUTHOR " " DRIVER_VERSION); |
| 433 |
info(DRIVER_DESC); |
| 434 |
|
| 435 |
return 0; |
| 436 |
} |
| 437 |
|
| 438 |
static void __exit streamzap_exit(void) |
| 439 |
{ |
| 440 |
usb_deregister(&streamzap_driver); |
| 441 |
dbg("Exiting"); |
| 442 |
} |
| 443 |
|
| 444 |
module_init(streamzap_init); |
| 445 |
module_exit(streamzap_exit); |
| 446 |
|
| 447 |
MODULE_AUTHOR(DRIVER_AUTHOR); |
| 448 |
MODULE_DESCRIPTION(DRIVER_DESC); |
| 449 |
MODULE_LICENSE("GPL"); |