2 * Streamzap Remote Control driver
4 * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de>
5 * Copyright (c) 2010 Jarod Wilson <jarod@wilsonet.com>
7 * This driver was based on the work of Greg Wickham and Adrian
8 * Dewhurst. It was substantially rewritten to support correct signal
9 * gaps and now maintains a delay buffer, which is used to present
10 * consistent timing behaviour to user space applications. Without the
11 * delay buffer an ugly hack would be required in lircd, which can
12 * cause sluggish signal decoding in certain situations.
14 * Ported to in-kernel ir-core interface by Jarod Wilson
16 * This driver is based on the USB skeleton driver packaged with the
17 * kernel; copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include <linux/device.h>
35 #include <linux/module.h>
36 #include <linux/slab.h>
37 #include <linux/usb.h>
38 #include <linux/input.h>
39 #include <media/ir-core.h>
41 #define DRIVER_VERSION "1.60"
42 #define DRIVER_NAME "streamzap"
43 #define DRIVER_DESC "Streamzap Remote Control driver"
45 #ifdef CONFIG_USB_DEBUG
51 #define USB_STREAMZAP_VENDOR_ID 0x0e9c
52 #define USB_STREAMZAP_PRODUCT_ID 0x0000
54 /* table of devices that work with this driver */
55 static struct usb_device_id streamzap_table[] = {
56 /* Streamzap Remote Control */
57 { USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
58 /* Terminating entry */
62 MODULE_DEVICE_TABLE(usb, streamzap_table);
64 #define STREAMZAP_PULSE_MASK 0xf0
65 #define STREAMZAP_SPACE_MASK 0x0f
66 #define STREAMZAP_TIMEOUT 0xff
67 #define STREAMZAP_RESOLUTION 256
69 /* number of samples buffered */
70 #define SZ_BUF_LEN 128
72 enum StreamzapDecoderState {
79 /* structure to hold our device specific stuff */
83 struct ir_dev_props *props;
84 struct ir_raw_event rawir;
86 /* core device info */
88 struct input_dev *idev;
91 struct usb_device *usbdev;
92 struct usb_interface *interface;
93 struct usb_endpoint_descriptor *endpoint;
97 unsigned char *buf_in;
99 unsigned int buf_in_len;
101 /* timer used to support delay buffering */
102 struct timer_list delay_timer;
104 spinlock_t timer_lock;
105 struct timer_list flush_timer;
110 bool fifo_initialized;
112 /* track what state we're in */
113 enum StreamzapDecoderState decoder_state;
114 /* tracks whether we are currently receiving some signal */
116 /* sum of signal lengths received since signal start */
118 /* start time of signal; necessary for gap tracking */
119 struct timeval signal_last;
120 struct timeval signal_start;
121 /* bool timeout_enabled; */
128 /* local function prototypes */
129 static int streamzap_probe(struct usb_interface *interface,
130 const struct usb_device_id *id);
131 static void streamzap_disconnect(struct usb_interface *interface);
132 static void streamzap_callback(struct urb *urb);
133 static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
134 static int streamzap_resume(struct usb_interface *intf);
136 /* usb specific object needed to register this driver with the usb subsystem */
137 static struct usb_driver streamzap_driver = {
139 .probe = streamzap_probe,
140 .disconnect = streamzap_disconnect,
141 .suspend = streamzap_suspend,
142 .resume = streamzap_resume,
143 .id_table = streamzap_table,
146 static void streamzap_stop_timer(struct streamzap_ir *sz)
150 spin_lock_irqsave(&sz->timer_lock, flags);
151 if (sz->timer_running) {
152 sz->timer_running = false;
153 spin_unlock_irqrestore(&sz->timer_lock, flags);
154 del_timer_sync(&sz->delay_timer);
156 spin_unlock_irqrestore(&sz->timer_lock, flags);
160 static void streamzap_flush_timeout(unsigned long arg)
162 struct streamzap_ir *sz = (struct streamzap_ir *)arg;
164 dev_info(sz->dev, "%s: callback firing\n", __func__);
166 /* finally start accepting data */
170 static void streamzap_delay_timeout(unsigned long arg)
172 struct streamzap_ir *sz = (struct streamzap_ir *)arg;
173 struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
176 static unsigned long delay;
179 /* deliver data every 10 ms */
180 delay = msecs_to_jiffies(10);
182 spin_lock_irqsave(&sz->timer_lock, flags);
184 if (kfifo_len(&sz->fifo) > 0) {
185 ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir));
186 if (ret != sizeof(rawir))
187 dev_err(sz->dev, "Problem w/kfifo_out...\n");
188 ir_raw_event_store(sz->idev, &rawir);
192 len = kfifo_len(&sz->fifo);
194 while ((len < SZ_BUF_LEN / 2) &&
195 (len < SZ_BUF_LEN * sizeof(int))) {
196 ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir));
197 if (ret != sizeof(rawir))
198 dev_err(sz->dev, "Problem w/kfifo_out...\n");
199 ir_raw_event_store(sz->idev, &rawir);
201 len = kfifo_len(&sz->fifo);
203 if (sz->timer_running)
204 mod_timer(&sz->delay_timer, jiffies + delay);
207 sz->timer_running = false;
211 ir_raw_event_handle(sz->idev);
213 spin_unlock_irqrestore(&sz->timer_lock, flags);
216 static void streamzap_flush_delay_buffer(struct streamzap_ir *sz)
218 struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
222 while (kfifo_len(&sz->fifo) > 0) {
223 ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir));
224 if (ret != sizeof(rawir))
225 dev_err(sz->dev, "Problem w/kfifo_out...\n");
226 ir_raw_event_store(sz->idev, &rawir);
231 ir_raw_event_handle(sz->idev);
234 static void sz_push(struct streamzap_ir *sz)
236 struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
240 spin_lock_irqsave(&sz->timer_lock, flags);
241 if (kfifo_len(&sz->fifo) >= sizeof(int) * SZ_BUF_LEN) {
242 ret = kfifo_out(&sz->fifo, &rawir, sizeof(rawir));
243 if (ret != sizeof(rawir))
244 dev_err(sz->dev, "Problem w/kfifo_out...\n");
245 ir_raw_event_store(sz->idev, &rawir);
248 kfifo_in(&sz->fifo, &sz->rawir, sizeof(rawir));
250 if (!sz->timer_running) {
251 sz->delay_timer.expires = jiffies + (HZ / 10);
252 add_timer(&sz->delay_timer);
253 sz->timer_running = true;
256 spin_unlock_irqrestore(&sz->timer_lock, flags);
259 static void sz_push_full_pulse(struct streamzap_ir *sz,
265 sz->signal_last = sz->signal_start;
266 do_gettimeofday(&sz->signal_start);
268 deltv = sz->signal_start.tv_sec - sz->signal_last.tv_sec;
269 sz->rawir.pulse = false;
271 /* really long time */
272 sz->rawir.duration = IR_MAX_DURATION;
274 sz->rawir.duration = (int)(deltv * 1000000 +
275 sz->signal_start.tv_usec -
276 sz->signal_last.tv_usec);
277 sz->rawir.duration -= sz->sum;
278 sz->rawir.duration *= 1000;
279 sz->rawir.duration &= IR_MAX_DURATION;
281 dev_dbg(sz->dev, "ls %u\n", sz->rawir.duration);
288 sz->rawir.pulse = true;
289 sz->rawir.duration = ((int) value) * STREAMZAP_RESOLUTION;
290 sz->rawir.duration += STREAMZAP_RESOLUTION / 2;
291 sz->sum += sz->rawir.duration;
292 sz->rawir.duration *= 1000;
293 sz->rawir.duration &= IR_MAX_DURATION;
294 dev_dbg(sz->dev, "p %u\n", sz->rawir.duration);
298 static void sz_push_half_pulse(struct streamzap_ir *sz,
301 sz_push_full_pulse(sz, (value & STREAMZAP_PULSE_MASK) >> 4);
304 static void sz_push_full_space(struct streamzap_ir *sz,
307 sz->rawir.pulse = false;
308 sz->rawir.duration = ((int) value) * STREAMZAP_RESOLUTION;
309 sz->rawir.duration += STREAMZAP_RESOLUTION / 2;
310 sz->sum += sz->rawir.duration;
311 sz->rawir.duration *= 1000;
312 dev_dbg(sz->dev, "s %u\n", sz->rawir.duration);
316 static void sz_push_half_space(struct streamzap_ir *sz,
319 sz_push_full_space(sz, value & STREAMZAP_SPACE_MASK);
323 * streamzap_callback - usb IRQ handler callback
325 * This procedure is invoked on reception of data from
328 static void streamzap_callback(struct urb *urb)
330 struct streamzap_ir *sz;
334 static int timeout = (((STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION) &
335 IR_MAX_DURATION) | 0x03000000);
342 len = urb->actual_length;
344 switch (urb->status) {
349 * this urb is terminated, clean up.
350 * sz might already be invalid at this point
352 dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
358 dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
360 for (i = 0; i < urb->actual_length; i++) {
361 dev_dbg(sz->dev, "%d: %x\n", i,
362 (unsigned char)sz->buf_in[i]);
363 switch (sz->decoder_state) {
365 if ((sz->buf_in[i] & STREAMZAP_PULSE_MASK) ==
366 STREAMZAP_PULSE_MASK) {
367 sz->decoder_state = FullPulse;
369 } else if ((sz->buf_in[i] & STREAMZAP_SPACE_MASK)
370 == STREAMZAP_SPACE_MASK) {
371 sz_push_half_pulse(sz, sz->buf_in[i]);
372 sz->decoder_state = FullSpace;
375 sz_push_half_pulse(sz, sz->buf_in[i]);
376 sz_push_half_space(sz, sz->buf_in[i]);
380 sz_push_full_pulse(sz, sz->buf_in[i]);
381 sz->decoder_state = IgnorePulse;
384 if (sz->buf_in[i] == STREAMZAP_TIMEOUT) {
386 streamzap_stop_timer(sz);
388 if (sz->timeout_enabled) {
389 sz->rawir.pulse = false;
390 sz->rawir.duration = timeout;
391 sz->rawir.duration *= 1000;
395 streamzap_flush_delay_buffer(sz);
397 sz_push_full_space(sz, sz->buf_in[i]);
398 sz->decoder_state = PulseSpace;
401 if ((sz->buf_in[i]&STREAMZAP_SPACE_MASK) ==
402 STREAMZAP_SPACE_MASK) {
403 sz->decoder_state = FullSpace;
406 sz_push_half_space(sz, sz->buf_in[i]);
407 sz->decoder_state = PulseSpace;
413 usb_submit_urb(urb, GFP_ATOMIC);
418 static struct input_dev *streamzap_init_input_dev(struct streamzap_ir *sz)
420 struct input_dev *idev;
421 struct ir_dev_props *props;
422 struct device *dev = sz->dev;
425 idev = input_allocate_device();
427 dev_err(dev, "remote input dev allocation failed\n");
428 goto idev_alloc_failed;
431 props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
433 dev_err(dev, "remote ir dev props allocation failed\n");
434 goto props_alloc_failed;
437 snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared "
438 "Receiver (%04x:%04x)",
439 le16_to_cpu(sz->usbdev->descriptor.idVendor),
440 le16_to_cpu(sz->usbdev->descriptor.idProduct));
442 idev->name = sz->name;
443 usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
444 strlcat(sz->phys, "/input0", sizeof(sz->phys));
445 idev->phys = sz->phys;
448 props->driver_type = RC_DRIVER_IR_RAW;
449 /* FIXME: not sure about supported protocols, check on this */
450 props->allowed_protos = IR_TYPE_RC5 | IR_TYPE_RC6;
454 ret = ir_input_register(idev, RC_MAP_RC5_STREAMZAP, props, DRIVER_NAME);
456 dev_err(dev, "remote input device register failed\n");
465 input_free_device(idev);
470 static int streamzap_delay_buf_init(struct streamzap_ir *sz)
474 ret = kfifo_alloc(&sz->fifo, sizeof(int) * SZ_BUF_LEN,
477 sz->fifo_initialized = 1;
482 static void streamzap_start_flush_timer(struct streamzap_ir *sz)
484 sz->flush_timer.expires = jiffies + HZ;
486 add_timer(&sz->flush_timer);
488 sz->urb_in->dev = sz->usbdev;
489 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
490 dev_err(sz->dev, "urb submit failed\n");
496 * Called by usb-core to associated with a candidate device
497 * On any failure the return value is the ERROR
498 * On success return 0
500 static int __devinit streamzap_probe(struct usb_interface *intf,
501 const struct usb_device_id *id)
503 struct usb_device *usbdev = interface_to_usbdev(intf);
504 struct usb_host_interface *iface_host;
505 struct streamzap_ir *sz = NULL;
506 char buf[63], name[128] = "";
507 int retval = -ENOMEM;
510 /* Allocate space for device driver specific data */
511 sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
516 sz->interface = intf;
518 /* Check to ensure endpoint information matches requirements */
519 iface_host = intf->cur_altsetting;
521 if (iface_host->desc.bNumEndpoints != 1) {
522 dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
523 __func__, iface_host->desc.bNumEndpoints);
528 sz->endpoint = &(iface_host->endpoint[0].desc);
529 if ((sz->endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
531 dev_err(&intf->dev, "%s: endpoint doesn't match input device "
532 "02%02x\n", __func__, sz->endpoint->bEndpointAddress);
537 if ((sz->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
538 != USB_ENDPOINT_XFER_INT) {
539 dev_err(&intf->dev, "%s: endpoint attributes don't match xfer "
540 "02%02x\n", __func__, sz->endpoint->bmAttributes);
545 pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress);
546 maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));
549 dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
555 /* Allocate the USB buffer and IRQ URB */
556 sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
560 sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
564 sz->dev = &intf->dev;
565 sz->buf_in_len = maxp;
567 if (usbdev->descriptor.iManufacturer
568 && usb_string(usbdev, usbdev->descriptor.iManufacturer,
569 buf, sizeof(buf)) > 0)
570 strlcpy(name, buf, sizeof(name));
572 if (usbdev->descriptor.iProduct
573 && usb_string(usbdev, usbdev->descriptor.iProduct,
574 buf, sizeof(buf)) > 0)
575 snprintf(name + strlen(name), sizeof(name) - strlen(name),
578 retval = streamzap_delay_buf_init(sz);
580 dev_err(&intf->dev, "%s: delay buffer init failed\n", __func__);
584 sz->idev = streamzap_init_input_dev(sz);
589 sz->decoder_state = PulseSpace;
591 /* not yet supported, depends on patches from maxim */
592 /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
593 sz->timeout_enabled = false;
594 sz->min_timeout = STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION * 1000;
595 sz->max_timeout = STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION * 1000;
598 init_timer(&sz->delay_timer);
599 sz->delay_timer.function = streamzap_delay_timeout;
600 sz->delay_timer.data = (unsigned long)sz;
601 spin_lock_init(&sz->timer_lock);
603 init_timer(&sz->flush_timer);
604 sz->flush_timer.function = streamzap_flush_timeout;
605 sz->flush_timer.data = (unsigned long)sz;
607 do_gettimeofday(&sz->signal_start);
609 /* Complete final initialisations */
610 usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
611 maxp, (usb_complete_t)streamzap_callback,
612 sz, sz->endpoint->bInterval);
613 sz->urb_in->transfer_dma = sz->dma_in;
614 sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
616 usb_set_intfdata(intf, sz);
618 streamzap_start_flush_timer(sz);
620 dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
621 usbdev->bus->busnum, usbdev->devnum);
626 kfifo_free(&sz->fifo);
628 usb_free_urb(sz->urb_in);
630 usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
638 * streamzap_disconnect
640 * Called by the usb core when the device is removed from the system.
642 * This routine guarantees that the driver will not submit any more urbs
643 * by clearing dev->usbdev. It is also supposed to terminate any currently
644 * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
645 * does not provide any way to do this.
647 static void streamzap_disconnect(struct usb_interface *interface)
649 struct streamzap_ir *sz = usb_get_intfdata(interface);
650 struct usb_device *usbdev = interface_to_usbdev(interface);
652 usb_set_intfdata(interface, NULL);
659 del_timer_sync(&sz->flush_timer);
662 streamzap_stop_timer(sz);
665 ir_input_unregister(sz->idev);
666 usb_kill_urb(sz->urb_in);
667 usb_free_urb(sz->urb_in);
668 usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
673 static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
675 struct streamzap_ir *sz = usb_get_intfdata(intf);
679 del_timer_sync(&sz->flush_timer);
682 streamzap_stop_timer(sz);
684 usb_kill_urb(sz->urb_in);
689 static int streamzap_resume(struct usb_interface *intf)
691 struct streamzap_ir *sz = usb_get_intfdata(intf);
693 if (sz->fifo_initialized)
694 kfifo_reset(&sz->fifo);
696 sz->flush_timer.expires = jiffies + HZ;
698 add_timer(&sz->flush_timer);
700 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
701 dev_err(sz->dev, "Error sumbiting urb\n");
711 static int __init streamzap_init(void)
715 /* register this driver with the USB subsystem */
716 ret = usb_register(&streamzap_driver);
718 printk(KERN_ERR DRIVER_NAME ": usb register failed, "
719 "result = %d\n", ret);
727 static void __exit streamzap_exit(void)
729 usb_deregister(&streamzap_driver);
733 module_init(streamzap_init);
734 module_exit(streamzap_exit);
736 MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
737 MODULE_DESCRIPTION(DRIVER_DESC);
738 MODULE_LICENSE("GPL");
740 module_param(debug, bool, S_IRUGO | S_IWUSR);
741 MODULE_PARM_DESC(debug, "Enable debugging messages");