Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Streamzap Remote Control driver |
| 3 | * |
| 4 | * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de> |
| 5 | * |
| 6 | * This driver was based on the work of Greg Wickham and Adrian |
| 7 | * Dewhurst. It was substantially rewritten to support correct signal |
| 8 | * gaps and now maintains a delay buffer, which is used to present |
| 9 | * consistent timing behaviour to user space applications. Without the |
| 10 | * delay buffer an ugly hack would be required in lircd, which can |
| 11 | * cause sluggish signal decoding in certain situations. |
| 12 | * |
| 13 | * This driver is based on the USB skeleton driver packaged with the |
| 14 | * kernel; copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com) |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License as published by |
| 18 | * the Free Software Foundation; either version 2 of the License, or |
| 19 | * (at your option) any later version. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 29 | */ |
| 30 | |
| 31 | #include <linux/kernel.h> |
| 32 | #include <linux/errno.h> |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/smp_lock.h> |
| 37 | #include <linux/completion.h> |
| 38 | #include <linux/uaccess.h> |
| 39 | #include <linux/usb.h> |
| 40 | |
| 41 | #include <media/lirc.h> |
| 42 | #include <media/lirc_dev.h> |
| 43 | |
| 44 | #define DRIVER_VERSION "1.28" |
| 45 | #define DRIVER_NAME "lirc_streamzap" |
| 46 | #define DRIVER_DESC "Streamzap Remote Control driver" |
| 47 | |
| 48 | static int debug; |
| 49 | |
| 50 | #define USB_STREAMZAP_VENDOR_ID 0x0e9c |
| 51 | #define USB_STREAMZAP_PRODUCT_ID 0x0000 |
| 52 | |
| 53 | /* Use our own dbg macro */ |
| 54 | #define dprintk(fmt, args...) \ |
| 55 | do { \ |
| 56 | if (debug) \ |
| 57 | printk(KERN_DEBUG DRIVER_NAME "[%d]: " \ |
| 58 | fmt "\n", ## args); \ |
| 59 | } while (0) |
| 60 | |
| 61 | /* table of devices that work with this driver */ |
| 62 | static struct usb_device_id streamzap_table[] = { |
| 63 | /* Streamzap Remote Control */ |
| 64 | { USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) }, |
| 65 | /* Terminating entry */ |
| 66 | { } |
| 67 | }; |
| 68 | |
| 69 | MODULE_DEVICE_TABLE(usb, streamzap_table); |
| 70 | |
| 71 | #define STREAMZAP_PULSE_MASK 0xf0 |
| 72 | #define STREAMZAP_SPACE_MASK 0x0f |
| 73 | #define STREAMZAP_TIMEOUT 0xff |
| 74 | #define STREAMZAP_RESOLUTION 256 |
| 75 | |
| 76 | /* number of samples buffered */ |
| 77 | #define STREAMZAP_BUF_LEN 128 |
| 78 | |
| 79 | enum StreamzapDecoderState { |
| 80 | PulseSpace, |
| 81 | FullPulse, |
| 82 | FullSpace, |
| 83 | IgnorePulse |
| 84 | }; |
| 85 | |
| 86 | /* Structure to hold all of our device specific stuff |
| 87 | * |
| 88 | * some remarks regarding locking: |
| 89 | * theoretically this struct can be accessed from three threads: |
| 90 | * |
| 91 | * - from lirc_dev through set_use_inc/set_use_dec |
| 92 | * |
| 93 | * - from the USB layer throuh probe/disconnect/irq |
| 94 | * |
| 95 | * Careful placement of lirc_register_driver/lirc_unregister_driver |
| 96 | * calls will prevent conflicts. lirc_dev makes sure that |
| 97 | * set_use_inc/set_use_dec are not being executed and will not be |
| 98 | * called after lirc_unregister_driver returns. |
| 99 | * |
| 100 | * - by the timer callback |
| 101 | * |
| 102 | * The timer is only running when the device is connected and the |
| 103 | * LIRC device is open. Making sure the timer is deleted by |
| 104 | * set_use_dec will make conflicts impossible. |
| 105 | */ |
| 106 | struct usb_streamzap { |
| 107 | |
| 108 | /* usb */ |
| 109 | /* save off the usb device pointer */ |
| 110 | struct usb_device *udev; |
| 111 | /* the interface for this device */ |
| 112 | struct usb_interface *interface; |
| 113 | |
| 114 | /* buffer & dma */ |
| 115 | unsigned char *buf_in; |
| 116 | dma_addr_t dma_in; |
| 117 | unsigned int buf_in_len; |
| 118 | |
| 119 | struct usb_endpoint_descriptor *endpoint; |
| 120 | |
| 121 | /* IRQ */ |
| 122 | struct urb *urb_in; |
| 123 | |
| 124 | /* lirc */ |
| 125 | struct lirc_driver *driver; |
| 126 | struct lirc_buffer *delay_buf; |
| 127 | |
| 128 | /* timer used to support delay buffering */ |
| 129 | struct timer_list delay_timer; |
| 130 | int timer_running; |
| 131 | spinlock_t timer_lock; |
| 132 | |
| 133 | /* tracks whether we are currently receiving some signal */ |
| 134 | int idle; |
| 135 | /* sum of signal lengths received since signal start */ |
| 136 | unsigned long sum; |
| 137 | /* start time of signal; necessary for gap tracking */ |
| 138 | struct timeval signal_last; |
| 139 | struct timeval signal_start; |
| 140 | enum StreamzapDecoderState decoder_state; |
| 141 | struct timer_list flush_timer; |
| 142 | int flush; |
| 143 | int in_use; |
| 144 | int timeout_enabled; |
| 145 | }; |
| 146 | |
| 147 | |
| 148 | /* local function prototypes */ |
| 149 | static int streamzap_probe(struct usb_interface *interface, |
| 150 | const struct usb_device_id *id); |
| 151 | static void streamzap_disconnect(struct usb_interface *interface); |
| 152 | static void usb_streamzap_irq(struct urb *urb); |
| 153 | static int streamzap_use_inc(void *data); |
| 154 | static void streamzap_use_dec(void *data); |
| 155 | static long streamzap_ioctl(struct file *filep, unsigned int cmd, |
| 156 | unsigned long arg); |
| 157 | static int streamzap_suspend(struct usb_interface *intf, pm_message_t message); |
| 158 | static int streamzap_resume(struct usb_interface *intf); |
| 159 | |
| 160 | /* usb specific object needed to register this driver with the usb subsystem */ |
| 161 | |
| 162 | static struct usb_driver streamzap_driver = { |
| 163 | .name = DRIVER_NAME, |
| 164 | .probe = streamzap_probe, |
| 165 | .disconnect = streamzap_disconnect, |
| 166 | .suspend = streamzap_suspend, |
| 167 | .resume = streamzap_resume, |
| 168 | .id_table = streamzap_table, |
| 169 | }; |
| 170 | |
| 171 | static void stop_timer(struct usb_streamzap *sz) |
| 172 | { |
| 173 | unsigned long flags; |
| 174 | |
| 175 | spin_lock_irqsave(&sz->timer_lock, flags); |
| 176 | if (sz->timer_running) { |
| 177 | sz->timer_running = 0; |
| 178 | spin_unlock_irqrestore(&sz->timer_lock, flags); |
| 179 | del_timer_sync(&sz->delay_timer); |
| 180 | } else { |
| 181 | spin_unlock_irqrestore(&sz->timer_lock, flags); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | static void flush_timeout(unsigned long arg) |
| 186 | { |
| 187 | struct usb_streamzap *sz = (struct usb_streamzap *) arg; |
| 188 | |
| 189 | /* finally start accepting data */ |
| 190 | sz->flush = 0; |
| 191 | } |
| 192 | static void delay_timeout(unsigned long arg) |
| 193 | { |
| 194 | unsigned long flags; |
| 195 | /* deliver data every 10 ms */ |
| 196 | static unsigned long timer_inc = |
| 197 | (10000/(1000000/HZ)) == 0 ? 1 : (10000/(1000000/HZ)); |
| 198 | struct usb_streamzap *sz = (struct usb_streamzap *) arg; |
| 199 | int data; |
| 200 | |
| 201 | spin_lock_irqsave(&sz->timer_lock, flags); |
| 202 | |
| 203 | if (!lirc_buffer_empty(sz->delay_buf) && |
| 204 | !lirc_buffer_full(sz->driver->rbuf)) { |
| 205 | lirc_buffer_read(sz->delay_buf, (unsigned char *) &data); |
| 206 | lirc_buffer_write(sz->driver->rbuf, (unsigned char *) &data); |
| 207 | } |
| 208 | if (!lirc_buffer_empty(sz->delay_buf)) { |
| 209 | while (lirc_buffer_available(sz->delay_buf) < |
| 210 | STREAMZAP_BUF_LEN / 2 && |
| 211 | !lirc_buffer_full(sz->driver->rbuf)) { |
| 212 | lirc_buffer_read(sz->delay_buf, |
| 213 | (unsigned char *) &data); |
| 214 | lirc_buffer_write(sz->driver->rbuf, |
| 215 | (unsigned char *) &data); |
| 216 | } |
| 217 | if (sz->timer_running) { |
| 218 | sz->delay_timer.expires = jiffies + timer_inc; |
| 219 | add_timer(&sz->delay_timer); |
| 220 | } |
| 221 | } else { |
| 222 | sz->timer_running = 0; |
| 223 | } |
| 224 | |
| 225 | if (!lirc_buffer_empty(sz->driver->rbuf)) |
| 226 | wake_up(&sz->driver->rbuf->wait_poll); |
| 227 | |
| 228 | spin_unlock_irqrestore(&sz->timer_lock, flags); |
| 229 | } |
| 230 | |
| 231 | static void flush_delay_buffer(struct usb_streamzap *sz) |
| 232 | { |
| 233 | int data; |
| 234 | int empty = 1; |
| 235 | |
| 236 | while (!lirc_buffer_empty(sz->delay_buf)) { |
| 237 | empty = 0; |
| 238 | lirc_buffer_read(sz->delay_buf, (unsigned char *) &data); |
| 239 | if (!lirc_buffer_full(sz->driver->rbuf)) { |
| 240 | lirc_buffer_write(sz->driver->rbuf, |
| 241 | (unsigned char *) &data); |
| 242 | } else { |
| 243 | dprintk("buffer overflow", sz->driver->minor); |
| 244 | } |
| 245 | } |
| 246 | if (!empty) |
| 247 | wake_up(&sz->driver->rbuf->wait_poll); |
| 248 | } |
| 249 | |
| 250 | static void push(struct usb_streamzap *sz, unsigned char *data) |
| 251 | { |
| 252 | unsigned long flags; |
| 253 | |
| 254 | spin_lock_irqsave(&sz->timer_lock, flags); |
| 255 | if (lirc_buffer_full(sz->delay_buf)) { |
| 256 | int read_data; |
| 257 | |
| 258 | lirc_buffer_read(sz->delay_buf, |
| 259 | (unsigned char *) &read_data); |
| 260 | if (!lirc_buffer_full(sz->driver->rbuf)) { |
| 261 | lirc_buffer_write(sz->driver->rbuf, |
| 262 | (unsigned char *) &read_data); |
| 263 | } else { |
| 264 | dprintk("buffer overflow", sz->driver->minor); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | lirc_buffer_write(sz->delay_buf, data); |
| 269 | |
| 270 | if (!sz->timer_running) { |
| 271 | sz->delay_timer.expires = jiffies + HZ/10; |
| 272 | add_timer(&sz->delay_timer); |
| 273 | sz->timer_running = 1; |
| 274 | } |
| 275 | |
| 276 | spin_unlock_irqrestore(&sz->timer_lock, flags); |
| 277 | } |
| 278 | |
| 279 | static void push_full_pulse(struct usb_streamzap *sz, |
| 280 | unsigned char value) |
| 281 | { |
| 282 | int pulse; |
| 283 | |
| 284 | if (sz->idle) { |
| 285 | long deltv; |
| 286 | int tmp; |
| 287 | |
| 288 | sz->signal_last = sz->signal_start; |
| 289 | do_gettimeofday(&sz->signal_start); |
| 290 | |
| 291 | deltv = sz->signal_start.tv_sec-sz->signal_last.tv_sec; |
| 292 | if (deltv > 15) { |
| 293 | /* really long time */ |
| 294 | tmp = LIRC_SPACE(LIRC_VALUE_MASK); |
| 295 | } else { |
| 296 | tmp = (int) (deltv*1000000+ |
| 297 | sz->signal_start.tv_usec - |
| 298 | sz->signal_last.tv_usec); |
| 299 | tmp -= sz->sum; |
| 300 | tmp = LIRC_SPACE(tmp); |
| 301 | } |
| 302 | dprintk("ls %u", sz->driver->minor, tmp); |
| 303 | push(sz, (char *)&tmp); |
| 304 | |
| 305 | sz->idle = 0; |
| 306 | sz->sum = 0; |
| 307 | } |
| 308 | |
| 309 | pulse = ((int) value) * STREAMZAP_RESOLUTION; |
| 310 | pulse += STREAMZAP_RESOLUTION / 2; |
| 311 | sz->sum += pulse; |
| 312 | pulse = LIRC_PULSE(pulse); |
| 313 | |
| 314 | dprintk("p %u", sz->driver->minor, pulse & PULSE_MASK); |
| 315 | push(sz, (char *)&pulse); |
| 316 | } |
| 317 | |
| 318 | static void push_half_pulse(struct usb_streamzap *sz, |
| 319 | unsigned char value) |
| 320 | { |
| 321 | push_full_pulse(sz, (value & STREAMZAP_PULSE_MASK)>>4); |
| 322 | } |
| 323 | |
| 324 | static void push_full_space(struct usb_streamzap *sz, |
| 325 | unsigned char value) |
| 326 | { |
| 327 | int space; |
| 328 | |
| 329 | space = ((int) value)*STREAMZAP_RESOLUTION; |
| 330 | space += STREAMZAP_RESOLUTION/2; |
| 331 | sz->sum += space; |
| 332 | space = LIRC_SPACE(space); |
| 333 | dprintk("s %u", sz->driver->minor, space); |
| 334 | push(sz, (char *)&space); |
| 335 | } |
| 336 | |
| 337 | static void push_half_space(struct usb_streamzap *sz, |
| 338 | unsigned char value) |
| 339 | { |
| 340 | push_full_space(sz, value & STREAMZAP_SPACE_MASK); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * usb_streamzap_irq - IRQ handler |
| 345 | * |
| 346 | * This procedure is invoked on reception of data from |
| 347 | * the usb remote. |
| 348 | */ |
| 349 | static void usb_streamzap_irq(struct urb *urb) |
| 350 | { |
| 351 | struct usb_streamzap *sz; |
| 352 | int len; |
| 353 | unsigned int i = 0; |
| 354 | |
| 355 | if (!urb) |
| 356 | return; |
| 357 | |
| 358 | sz = urb->context; |
| 359 | len = urb->actual_length; |
| 360 | |
| 361 | switch (urb->status) { |
| 362 | case -ECONNRESET: |
| 363 | case -ENOENT: |
| 364 | case -ESHUTDOWN: |
| 365 | /* |
| 366 | * this urb is terminated, clean up. |
| 367 | * sz might already be invalid at this point |
| 368 | */ |
| 369 | dprintk("urb status: %d", -1, urb->status); |
| 370 | return; |
| 371 | default: |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | dprintk("received %d", sz->driver->minor, urb->actual_length); |
| 376 | if (!sz->flush) { |
| 377 | for (i = 0; i < urb->actual_length; i++) { |
| 378 | dprintk("%d: %x", sz->driver->minor, |
| 379 | i, (unsigned char) sz->buf_in[i]); |
| 380 | switch (sz->decoder_state) { |
| 381 | case PulseSpace: |
| 382 | if ((sz->buf_in[i]&STREAMZAP_PULSE_MASK) == |
| 383 | STREAMZAP_PULSE_MASK) { |
| 384 | sz->decoder_state = FullPulse; |
| 385 | continue; |
| 386 | } else if ((sz->buf_in[i]&STREAMZAP_SPACE_MASK) |
| 387 | == STREAMZAP_SPACE_MASK) { |
| 388 | push_half_pulse(sz, sz->buf_in[i]); |
| 389 | sz->decoder_state = FullSpace; |
| 390 | continue; |
| 391 | } else { |
| 392 | push_half_pulse(sz, sz->buf_in[i]); |
| 393 | push_half_space(sz, sz->buf_in[i]); |
| 394 | } |
| 395 | break; |
| 396 | case FullPulse: |
| 397 | push_full_pulse(sz, sz->buf_in[i]); |
| 398 | sz->decoder_state = IgnorePulse; |
| 399 | break; |
| 400 | case FullSpace: |
| 401 | if (sz->buf_in[i] == STREAMZAP_TIMEOUT) { |
| 402 | sz->idle = 1; |
| 403 | stop_timer(sz); |
| 404 | if (sz->timeout_enabled) { |
| 405 | int timeout = |
| 406 | LIRC_TIMEOUT |
| 407 | (STREAMZAP_TIMEOUT * |
| 408 | STREAMZAP_RESOLUTION); |
| 409 | push(sz, (char *)&timeout); |
| 410 | } |
| 411 | flush_delay_buffer(sz); |
| 412 | } else |
| 413 | push_full_space(sz, sz->buf_in[i]); |
| 414 | sz->decoder_state = PulseSpace; |
| 415 | break; |
| 416 | case IgnorePulse: |
| 417 | if ((sz->buf_in[i]&STREAMZAP_SPACE_MASK) == |
| 418 | STREAMZAP_SPACE_MASK) { |
| 419 | sz->decoder_state = FullSpace; |
| 420 | continue; |
| 421 | } |
| 422 | push_half_space(sz, sz->buf_in[i]); |
| 423 | sz->decoder_state = PulseSpace; |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | usb_submit_urb(urb, GFP_ATOMIC); |
| 430 | |
| 431 | return; |
| 432 | } |
| 433 | |
Mauro Carvalho Chehab | 0f9313a | 2010-07-27 18:44:45 -0300 | [diff] [blame] | 434 | static const struct file_operations streamzap_fops = { |
Jarod Wilson | 1977069 | 2010-07-26 20:32:49 -0300 | [diff] [blame] | 435 | .owner = THIS_MODULE, |
| 436 | .unlocked_ioctl = streamzap_ioctl, |
| 437 | .read = lirc_dev_fop_read, |
| 438 | .write = lirc_dev_fop_write, |
| 439 | .poll = lirc_dev_fop_poll, |
| 440 | .open = lirc_dev_fop_open, |
| 441 | .release = lirc_dev_fop_close, |
| 442 | }; |
| 443 | |
| 444 | |
| 445 | /** |
| 446 | * streamzap_probe |
| 447 | * |
| 448 | * Called by usb-core to associated with a candidate device |
| 449 | * On any failure the return value is the ERROR |
| 450 | * On success return 0 |
| 451 | */ |
| 452 | static int streamzap_probe(struct usb_interface *interface, |
| 453 | const struct usb_device_id *id) |
| 454 | { |
| 455 | struct usb_device *udev = interface_to_usbdev(interface); |
| 456 | struct usb_host_interface *iface_host; |
| 457 | struct usb_streamzap *sz; |
| 458 | struct lirc_driver *driver; |
| 459 | struct lirc_buffer *lirc_buf; |
| 460 | struct lirc_buffer *delay_buf; |
| 461 | char buf[63], name[128] = ""; |
| 462 | int retval = -ENOMEM; |
| 463 | int minor = 0; |
| 464 | |
| 465 | /* Allocate space for device driver specific data */ |
| 466 | sz = kzalloc(sizeof(struct usb_streamzap), GFP_KERNEL); |
| 467 | if (sz == NULL) |
| 468 | return -ENOMEM; |
| 469 | |
| 470 | sz->udev = udev; |
| 471 | sz->interface = interface; |
| 472 | |
| 473 | /* Check to ensure endpoint information matches requirements */ |
| 474 | iface_host = interface->cur_altsetting; |
| 475 | |
| 476 | if (iface_host->desc.bNumEndpoints != 1) { |
| 477 | err("%s: Unexpected desc.bNumEndpoints (%d)", __func__, |
| 478 | iface_host->desc.bNumEndpoints); |
| 479 | retval = -ENODEV; |
| 480 | goto free_sz; |
| 481 | } |
| 482 | |
| 483 | sz->endpoint = &(iface_host->endpoint[0].desc); |
| 484 | if ((sz->endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) |
| 485 | != USB_DIR_IN) { |
| 486 | err("%s: endpoint doesn't match input device 02%02x", |
| 487 | __func__, sz->endpoint->bEndpointAddress); |
| 488 | retval = -ENODEV; |
| 489 | goto free_sz; |
| 490 | } |
| 491 | |
| 492 | if ((sz->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
| 493 | != USB_ENDPOINT_XFER_INT) { |
| 494 | err("%s: endpoint attributes don't match xfer 02%02x", |
| 495 | __func__, sz->endpoint->bmAttributes); |
| 496 | retval = -ENODEV; |
| 497 | goto free_sz; |
| 498 | } |
| 499 | |
| 500 | if (sz->endpoint->wMaxPacketSize == 0) { |
| 501 | err("%s: endpoint message size==0? ", __func__); |
| 502 | retval = -ENODEV; |
| 503 | goto free_sz; |
| 504 | } |
| 505 | |
| 506 | /* Allocate the USB buffer and IRQ URB */ |
| 507 | |
| 508 | sz->buf_in_len = sz->endpoint->wMaxPacketSize; |
| 509 | sz->buf_in = usb_alloc_coherent(sz->udev, sz->buf_in_len, |
| 510 | GFP_ATOMIC, &sz->dma_in); |
| 511 | if (sz->buf_in == NULL) |
| 512 | goto free_sz; |
| 513 | |
| 514 | sz->urb_in = usb_alloc_urb(0, GFP_KERNEL); |
| 515 | if (sz->urb_in == NULL) |
| 516 | goto free_sz; |
| 517 | |
| 518 | /* Connect this device to the LIRC sub-system */ |
| 519 | driver = kzalloc(sizeof(struct lirc_driver), GFP_KERNEL); |
| 520 | if (!driver) |
| 521 | goto free_sz; |
| 522 | |
| 523 | lirc_buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL); |
| 524 | if (!lirc_buf) |
| 525 | goto free_driver; |
| 526 | if (lirc_buffer_init(lirc_buf, sizeof(int), STREAMZAP_BUF_LEN)) |
| 527 | goto kfree_lirc_buf; |
| 528 | |
| 529 | delay_buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL); |
| 530 | if (!delay_buf) |
| 531 | goto free_lirc_buf; |
| 532 | if (lirc_buffer_init(delay_buf, sizeof(int), STREAMZAP_BUF_LEN)) |
| 533 | goto kfree_delay_buf; |
| 534 | |
| 535 | sz->driver = driver; |
| 536 | strcpy(sz->driver->name, DRIVER_NAME); |
| 537 | sz->driver->minor = -1; |
| 538 | sz->driver->sample_rate = 0; |
| 539 | sz->driver->code_length = sizeof(int) * 8; |
| 540 | sz->driver->features = LIRC_CAN_REC_MODE2 | |
| 541 | LIRC_CAN_GET_REC_RESOLUTION | |
| 542 | LIRC_CAN_SET_REC_TIMEOUT; |
| 543 | sz->driver->data = sz; |
| 544 | sz->driver->min_timeout = STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION; |
| 545 | sz->driver->max_timeout = STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION; |
| 546 | sz->driver->rbuf = lirc_buf; |
| 547 | sz->delay_buf = delay_buf; |
| 548 | sz->driver->set_use_inc = &streamzap_use_inc; |
| 549 | sz->driver->set_use_dec = &streamzap_use_dec; |
| 550 | sz->driver->fops = &streamzap_fops; |
| 551 | sz->driver->dev = &interface->dev; |
| 552 | sz->driver->owner = THIS_MODULE; |
| 553 | |
| 554 | sz->idle = 1; |
| 555 | sz->decoder_state = PulseSpace; |
| 556 | init_timer(&sz->delay_timer); |
| 557 | sz->delay_timer.function = delay_timeout; |
| 558 | sz->delay_timer.data = (unsigned long) sz; |
| 559 | sz->timer_running = 0; |
| 560 | spin_lock_init(&sz->timer_lock); |
| 561 | |
| 562 | init_timer(&sz->flush_timer); |
| 563 | sz->flush_timer.function = flush_timeout; |
| 564 | sz->flush_timer.data = (unsigned long) sz; |
| 565 | /* Complete final initialisations */ |
| 566 | |
| 567 | usb_fill_int_urb(sz->urb_in, udev, |
| 568 | usb_rcvintpipe(udev, sz->endpoint->bEndpointAddress), |
| 569 | sz->buf_in, sz->buf_in_len, usb_streamzap_irq, sz, |
| 570 | sz->endpoint->bInterval); |
| 571 | sz->urb_in->transfer_dma = sz->dma_in; |
| 572 | sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 573 | |
| 574 | if (udev->descriptor.iManufacturer |
| 575 | && usb_string(udev, udev->descriptor.iManufacturer, |
| 576 | buf, sizeof(buf)) > 0) |
| 577 | strlcpy(name, buf, sizeof(name)); |
| 578 | |
| 579 | if (udev->descriptor.iProduct |
| 580 | && usb_string(udev, udev->descriptor.iProduct, |
| 581 | buf, sizeof(buf)) > 0) |
| 582 | snprintf(name + strlen(name), sizeof(name) - strlen(name), |
| 583 | " %s", buf); |
| 584 | |
| 585 | minor = lirc_register_driver(driver); |
| 586 | |
| 587 | if (minor < 0) |
| 588 | goto free_delay_buf; |
| 589 | |
| 590 | sz->driver->minor = minor; |
| 591 | |
| 592 | usb_set_intfdata(interface, sz); |
| 593 | |
| 594 | printk(KERN_INFO DRIVER_NAME "[%d]: %s on usb%d:%d attached\n", |
| 595 | sz->driver->minor, name, |
| 596 | udev->bus->busnum, sz->udev->devnum); |
| 597 | |
| 598 | return 0; |
| 599 | |
| 600 | free_delay_buf: |
| 601 | lirc_buffer_free(sz->delay_buf); |
| 602 | kfree_delay_buf: |
| 603 | kfree(delay_buf); |
| 604 | free_lirc_buf: |
| 605 | lirc_buffer_free(sz->driver->rbuf); |
| 606 | kfree_lirc_buf: |
| 607 | kfree(lirc_buf); |
| 608 | free_driver: |
| 609 | kfree(driver); |
| 610 | free_sz: |
| 611 | if (retval == -ENOMEM) |
| 612 | err("Out of memory"); |
| 613 | |
| 614 | if (sz) { |
| 615 | usb_free_urb(sz->urb_in); |
| 616 | usb_free_coherent(udev, sz->buf_in_len, sz->buf_in, sz->dma_in); |
| 617 | kfree(sz); |
| 618 | } |
| 619 | |
| 620 | return retval; |
| 621 | } |
| 622 | |
| 623 | static int streamzap_use_inc(void *data) |
| 624 | { |
| 625 | struct usb_streamzap *sz = data; |
| 626 | |
| 627 | if (!sz) { |
| 628 | dprintk("%s called with no context", -1, __func__); |
| 629 | return -EINVAL; |
| 630 | } |
| 631 | dprintk("set use inc", sz->driver->minor); |
| 632 | |
| 633 | lirc_buffer_clear(sz->driver->rbuf); |
| 634 | lirc_buffer_clear(sz->delay_buf); |
| 635 | |
| 636 | sz->flush_timer.expires = jiffies + HZ; |
| 637 | sz->flush = 1; |
| 638 | add_timer(&sz->flush_timer); |
| 639 | |
| 640 | sz->urb_in->dev = sz->udev; |
| 641 | if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) { |
| 642 | dprintk("open result = -EIO error submitting urb", |
| 643 | sz->driver->minor); |
| 644 | return -EIO; |
| 645 | } |
| 646 | sz->in_use++; |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | static void streamzap_use_dec(void *data) |
| 652 | { |
| 653 | struct usb_streamzap *sz = data; |
| 654 | |
| 655 | if (!sz) { |
| 656 | dprintk("%s called with no context", -1, __func__); |
| 657 | return; |
| 658 | } |
| 659 | dprintk("set use dec", sz->driver->minor); |
| 660 | |
| 661 | if (sz->flush) { |
| 662 | sz->flush = 0; |
| 663 | del_timer_sync(&sz->flush_timer); |
| 664 | } |
| 665 | |
| 666 | usb_kill_urb(sz->urb_in); |
| 667 | |
| 668 | stop_timer(sz); |
| 669 | |
| 670 | sz->in_use--; |
| 671 | } |
| 672 | |
| 673 | static long streamzap_ioctl(struct file *filep, unsigned int cmd, |
| 674 | unsigned long arg) |
| 675 | { |
| 676 | int result = 0; |
| 677 | int val; |
| 678 | struct usb_streamzap *sz = lirc_get_pdata(filep); |
| 679 | |
| 680 | switch (cmd) { |
| 681 | case LIRC_GET_REC_RESOLUTION: |
| 682 | result = put_user(STREAMZAP_RESOLUTION, (unsigned int *) arg); |
| 683 | break; |
| 684 | case LIRC_SET_REC_TIMEOUT: |
| 685 | result = get_user(val, (int *)arg); |
| 686 | if (result == 0) { |
| 687 | if (val == STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION) |
| 688 | sz->timeout_enabled = 1; |
| 689 | else if (val == 0) |
| 690 | sz->timeout_enabled = 0; |
| 691 | else |
| 692 | result = -EINVAL; |
| 693 | } |
| 694 | break; |
| 695 | default: |
| 696 | return lirc_dev_fop_ioctl(filep, cmd, arg); |
| 697 | } |
| 698 | return result; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * streamzap_disconnect |
| 703 | * |
| 704 | * Called by the usb core when the device is removed from the system. |
| 705 | * |
| 706 | * This routine guarantees that the driver will not submit any more urbs |
| 707 | * by clearing dev->udev. It is also supposed to terminate any currently |
| 708 | * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(), |
| 709 | * does not provide any way to do this. |
| 710 | */ |
| 711 | static void streamzap_disconnect(struct usb_interface *interface) |
| 712 | { |
| 713 | struct usb_streamzap *sz; |
| 714 | int errnum; |
| 715 | int minor; |
| 716 | |
| 717 | sz = usb_get_intfdata(interface); |
| 718 | |
| 719 | /* unregister from the LIRC sub-system */ |
| 720 | |
| 721 | errnum = lirc_unregister_driver(sz->driver->minor); |
| 722 | if (errnum != 0) |
| 723 | dprintk("error in lirc_unregister: (returned %d)", |
| 724 | sz->driver->minor, errnum); |
| 725 | |
| 726 | lirc_buffer_free(sz->delay_buf); |
| 727 | lirc_buffer_free(sz->driver->rbuf); |
| 728 | |
| 729 | /* unregister from the USB sub-system */ |
| 730 | |
| 731 | usb_free_urb(sz->urb_in); |
| 732 | |
| 733 | usb_free_coherent(sz->udev, sz->buf_in_len, sz->buf_in, sz->dma_in); |
| 734 | |
| 735 | minor = sz->driver->minor; |
| 736 | kfree(sz->driver->rbuf); |
| 737 | kfree(sz->driver); |
| 738 | kfree(sz->delay_buf); |
| 739 | kfree(sz); |
| 740 | |
| 741 | printk(KERN_INFO DRIVER_NAME "[%d]: disconnected\n", minor); |
| 742 | } |
| 743 | |
| 744 | static int streamzap_suspend(struct usb_interface *intf, pm_message_t message) |
| 745 | { |
| 746 | struct usb_streamzap *sz = usb_get_intfdata(intf); |
| 747 | |
| 748 | printk(KERN_INFO DRIVER_NAME "[%d]: suspend\n", sz->driver->minor); |
| 749 | if (sz->in_use) { |
| 750 | if (sz->flush) { |
| 751 | sz->flush = 0; |
| 752 | del_timer_sync(&sz->flush_timer); |
| 753 | } |
| 754 | |
| 755 | stop_timer(sz); |
| 756 | |
| 757 | usb_kill_urb(sz->urb_in); |
| 758 | } |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static int streamzap_resume(struct usb_interface *intf) |
| 763 | { |
| 764 | struct usb_streamzap *sz = usb_get_intfdata(intf); |
| 765 | |
| 766 | lirc_buffer_clear(sz->driver->rbuf); |
| 767 | lirc_buffer_clear(sz->delay_buf); |
| 768 | |
| 769 | if (sz->in_use) { |
| 770 | sz->flush_timer.expires = jiffies + HZ; |
| 771 | sz->flush = 1; |
| 772 | add_timer(&sz->flush_timer); |
| 773 | |
| 774 | sz->urb_in->dev = sz->udev; |
| 775 | if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) { |
| 776 | dprintk("open result = -EIO error submitting urb", |
| 777 | sz->driver->minor); |
| 778 | return -EIO; |
| 779 | } |
| 780 | } |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * usb_streamzap_init |
| 786 | */ |
| 787 | static int __init usb_streamzap_init(void) |
| 788 | { |
| 789 | int result; |
| 790 | |
| 791 | /* register this driver with the USB subsystem */ |
| 792 | result = usb_register(&streamzap_driver); |
| 793 | |
| 794 | if (result) { |
| 795 | err("usb_register failed. Error number %d", |
| 796 | result); |
| 797 | return result; |
| 798 | } |
| 799 | |
| 800 | printk(KERN_INFO DRIVER_NAME " " DRIVER_VERSION " registered\n"); |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * usb_streamzap_exit |
| 806 | */ |
| 807 | static void __exit usb_streamzap_exit(void) |
| 808 | { |
| 809 | usb_deregister(&streamzap_driver); |
| 810 | } |
| 811 | |
| 812 | |
| 813 | module_init(usb_streamzap_init); |
| 814 | module_exit(usb_streamzap_exit); |
| 815 | |
| 816 | MODULE_AUTHOR("Christoph Bartelmus, Greg Wickham, Adrian Dewhurst"); |
| 817 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 818 | MODULE_LICENSE("GPL"); |
| 819 | |
| 820 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 821 | MODULE_PARM_DESC(debug, "Enable debugging messages"); |