28
27
* You should have received a copy of the GNU General Public License
29
28
* along with this program; if not, write to the Free Software
30
29
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34
32
#include <linux/version.h>
57
54
#include "drivers/kcompat.h"
58
55
#include "drivers/lirc_dev/lirc_dev.h"
60
#define DRIVER_VERSION "$Revision: 1.29 $"
57
#define DRIVER_VERSION "$Revision: 1.48 $"
61
58
#define DRIVER_NAME "lirc_streamzap"
62
59
#define DRIVER_DESC "Streamzap Remote Control driver"
64
/* ------------------------------------------------------------------ */
68
63
#define USB_STREAMZAP_VENDOR_ID 0x0e9c
76
71
fmt "\n", ## args); \
80
* table of devices that work with this driver
82
static struct usb_device_id streamzap_table [] = {
74
/* table of devices that work with this driver */
75
static struct usb_device_id streamzap_table[] = {
83
76
/* Streamzap Remote Control */
84
77
{ USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
85
78
/* Terminating entry */
105
/* Structure to hold all of our device specific stuff */
106
/* some remarks regarding locking:
107
theoretically this struct can be accessed from three threads:
109
- from lirc_dev through set_use_inc/set_use_dec
111
- from the USB layer throuh probe/disconnect/irq
113
Careful placement of lirc_register_plugin/lirc_unregister_plugin
114
calls will prevent conflicts. lirc_dev makes sure that
115
set_use_inc/set_use_dec are not being executed and will not be
116
called after lirc_unregister_plugin returns.
118
- by the timer callback
120
The timer is only running when the device is connected and the
121
LIRC device is open. Making sure the timer is deleted by
122
set_use_dec will make conflicts impossible.
98
/* Structure to hold all of our device specific stuff
100
* some remarks regarding locking:
101
* theoretically this struct can be accessed from three threads:
103
* - from lirc_dev through set_use_inc/set_use_dec
105
* - from the USB layer throuh probe/disconnect/irq
107
* Careful placement of lirc_register_driver/lirc_unregister_driver
108
* calls will prevent conflicts. lirc_dev makes sure that
109
* set_use_inc/set_use_dec are not being executed and will not be
110
* called after lirc_unregister_driver returns.
112
* - by the timer callback
114
* The timer is only running when the device is connected and the
115
* LIRC device is open. Making sure the timer is deleted by
116
* set_use_dec will make conflicts impossible.
124
118
struct usb_streamzap {
228
222
unsigned long flags;
229
223
/* deliver data every 10 ms */
230
224
static unsigned long timer_inc =
231
(10000/(1000000/HZ)) == 0 ? 1:(10000/(1000000/HZ));
225
(10000/(1000000/HZ)) == 0 ? 1 : (10000/(1000000/HZ));
232
226
struct usb_streamzap *sz = (struct usb_streamzap *) arg;
237
231
if (!lirc_buffer_empty(&sz->delay_buf) &&
238
232
!lirc_buffer_full(&sz->lirc_buf)) {
239
lirc_buffer_read_1(&sz->delay_buf, (unsigned char *) &data);
240
lirc_buffer_write_1(&sz->lirc_buf, (unsigned char *) &data);
233
lirc_buffer_read(&sz->delay_buf, (unsigned char *) &data);
234
lirc_buffer_write(&sz->lirc_buf, (unsigned char *) &data);
242
236
if (!lirc_buffer_empty(&sz->delay_buf)) {
243
237
while (lirc_buffer_available(&sz->delay_buf) <
244
238
STREAMZAP_BUFFER_SIZE/2 &&
245
239
!lirc_buffer_full(&sz->lirc_buf)) {
246
lirc_buffer_read_1(&sz->delay_buf,
247
(unsigned char *) &data);
248
lirc_buffer_write_1(&sz->lirc_buf,
249
(unsigned char *) &data);
240
lirc_buffer_read(&sz->delay_buf,
241
(unsigned char *) &data);
242
lirc_buffer_write(&sz->lirc_buf,
243
(unsigned char *) &data);
251
245
if (sz->timer_running) {
252
sz->delay_timer.expires += timer_inc;
246
sz->delay_timer.expires = jiffies + timer_inc;
253
247
add_timer(&sz->delay_timer);
262
256
spin_unlock_irqrestore(&sz->timer_lock, flags);
265
static inline void flush_delay_buffer(struct usb_streamzap *sz)
259
static void flush_delay_buffer(struct usb_streamzap *sz)
270
264
while (!lirc_buffer_empty(&sz->delay_buf)) {
272
lirc_buffer_read_1(&sz->delay_buf, (unsigned char *) &data);
266
lirc_buffer_read(&sz->delay_buf, (unsigned char *) &data);
273
267
if (!lirc_buffer_full(&sz->lirc_buf)) {
274
lirc_buffer_write_1(&sz->lirc_buf,
268
lirc_buffer_write(&sz->lirc_buf,
275
269
(unsigned char *) &data);
277
dprintk("buffer overflow\n", sz->plugin.minor);
271
dprintk("buffer overflow", sz->driver.minor);
281
275
wake_up(&sz->lirc_buf.wait_poll);
284
static inline void push(struct usb_streamzap *sz, unsigned char *data)
278
static void push(struct usb_streamzap *sz, unsigned char *data)
286
280
unsigned long flags;
289
283
if (lirc_buffer_full(&sz->delay_buf)) {
292
lirc_buffer_read_1(&sz->delay_buf, (unsigned char *) &data);
286
lirc_buffer_read(&sz->delay_buf, (unsigned char *) &data);
293
287
if (!lirc_buffer_full(&sz->lirc_buf)) {
294
lirc_buffer_write_1(&sz->lirc_buf,
295
(unsigned char *) &data);
288
lirc_buffer_write(&sz->lirc_buf,
289
(unsigned char *) &data);
297
dprintk("buffer overflow", sz->plugin.minor);
291
dprintk("buffer overflow", sz->driver.minor);
301
lirc_buffer_write_1(&sz->delay_buf, data);
295
lirc_buffer_write(&sz->delay_buf, data);
303
297
if (!sz->timer_running) {
304
298
sz->delay_timer.expires = jiffies + HZ/10;
342
336
sz->sum += pulse;
343
337
pulse |= PULSE_BIT;
345
dprintk("p %u", sz->plugin.minor, pulse&PULSE_MASK);
339
dprintk("p %u", sz->driver.minor, pulse&PULSE_MASK);
346
340
push(sz, (char *)&pulse);
349
static inline void push_half_pulse(struct usb_streamzap *sz,
343
static void push_half_pulse(struct usb_streamzap *sz,
350
344
unsigned char value)
352
346
push_full_pulse(sz, (value & STREAMZAP_PULSE_MASK)>>4);
355
static inline void push_full_space(struct usb_streamzap *sz,
349
static void push_full_space(struct usb_streamzap *sz,
356
350
unsigned char value)
360
354
space = ((lirc_t) value)*STREAMZAP_RESOLUTION;
361
355
space += STREAMZAP_RESOLUTION/2;
362
356
sz->sum += space;
363
dprintk("s %u", sz->plugin.minor, space);
357
dprintk("s %u", sz->driver.minor, space);
364
358
push(sz, (char *)&space);
367
static inline void push_half_space(struct usb_streamzap *sz,
361
static void push_half_space(struct usb_streamzap *sz,
368
362
unsigned char value)
370
364
push_full_space(sz, value & STREAMZAP_SPACE_MASK);
374
368
* usb_streamzap_irq - IRQ handler
376
370
* This procedure is invoked on reception of data from
396
390
case -ECONNRESET:
399
/* this urb is terminated, clean up */
400
/* sz might already be invalid at this point */
394
* this urb is terminated, clean up.
395
* sz might already be invalid at this point
401
397
dprintk("urb status: %d", -1, urb->status);
407
dprintk("received %d", sz->plugin.minor, urb->actual_length);
403
dprintk("received %d", sz->driver.minor, urb->actual_length);
408
404
if (!sz->flush) {
409
405
for (i = 0; i < urb->actual_length; i++) {
410
dprintk("%d: %x", sz->plugin.minor,
406
dprintk("%d: %x", sz->driver.minor,
411
407
i, (unsigned char) sz->buf_in[i]);
412
408
switch (sz->decoder_state) {
483
484
struct usb_streamzap *sz = NULL;
484
485
char buf[63], name[128] = "";
486
/***************************************************
487
* Allocate space for device driver specific data
489
sz = kmalloc(sizeof(struct usb_streamzap), GFP_KERNEL);
487
/* Allocate space for device driver specific data */
488
sz = kzalloc(sizeof(struct usb_streamzap), GFP_KERNEL);
493
memset(sz, 0, sizeof(*sz));
495
493
sz->interface = interface;
497
/***************************************************
498
* Check to ensure endpoint information matches requirements
495
/* Check to ensure endpoint information matches requirements */
500
496
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 5)
501
497
iface_host = &interface->altsetting[interface->act_altsetting];
509
505
if (iface_host->bNumEndpoints != 1) {
511
507
#ifdef KERNEL_2_5
512
err("%s: Unexpected desc.bNumEndpoints (%d)", __FUNCTION__,
508
err("%s: Unexpected desc.bNumEndpoints (%d)", __func__,
513
509
iface_host->desc.bNumEndpoints);
515
err("%s: Unexpected desc.bNumEndpoints (%d)", __FUNCTION__,
511
err("%s: Unexpected desc.bNumEndpoints (%d)", __func__,
516
512
iface_host->bNumEndpoints);
518
514
retval = -ENODEV;
527
523
if ((sz->endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
529
525
err("%s: endpoint doesn't match input device 02%02x",
530
__FUNCTION__, sz->endpoint->bEndpointAddress);
526
__func__, sz->endpoint->bEndpointAddress);
531
527
retval = -ENODEV;
535
531
if ((sz->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
536
532
!= USB_ENDPOINT_XFER_INT) {
537
533
err("%s: endpoint attributes don't match xfer 02%02x",
538
__FUNCTION__, sz->endpoint->bmAttributes);
534
__func__, sz->endpoint->bmAttributes);
539
535
retval = -ENODEV;
543
539
if (sz->endpoint->wMaxPacketSize == 0) {
544
err("%s: endpoint message size==0? ", __FUNCTION__);
540
err("%s: endpoint message size==0? ", __func__);
545
541
retval = -ENODEV;
549
/***************************************************
550
* Allocate the USB buffer and IRQ URB
545
/* Allocate the USB buffer and IRQ URB */
553
547
sz->buf_in_len = sz->endpoint->wMaxPacketSize;
554
548
#ifdef KERNEL_2_5
569
563
if (sz->urb_in == NULL)
572
/***************************************************
573
* Connect this device to the LIRC sub-system
566
/* Connect this device to the LIRC sub-system */
576
568
if (lirc_buffer_init(&sz->lirc_buf, sizeof(lirc_t),
577
569
STREAMZAP_BUFFER_SIZE))
586
/***************************************************
587
* As required memory is allocated now populate the plugin structure
590
memset(&sz->plugin, 0, sizeof(sz->plugin));
592
strcpy(sz->plugin.name, DRIVER_NAME);
593
sz->plugin.minor = -1;
594
sz->plugin.sample_rate = 0;
595
sz->plugin.code_length = sizeof(lirc_t) * 8;
596
sz->plugin.features = LIRC_CAN_REC_MODE2|LIRC_CAN_GET_REC_RESOLUTION;
597
sz->plugin.data = sz;
598
sz->plugin.rbuf = &sz->lirc_buf;
599
sz->plugin.set_use_inc = &streamzap_use_inc;
600
sz->plugin.set_use_dec = &streamzap_use_dec;
601
sz->plugin.ioctl = streamzap_ioctl;
578
strcpy(sz->driver.name, DRIVER_NAME);
579
sz->driver.minor = -1;
580
sz->driver.sample_rate = 0;
581
sz->driver.code_length = sizeof(lirc_t) * 8;
582
sz->driver.features = LIRC_CAN_REC_MODE2 | LIRC_CAN_GET_REC_RESOLUTION;
583
sz->driver.data = sz;
584
sz->driver.rbuf = &sz->lirc_buf;
585
sz->driver.set_use_inc = &streamzap_use_inc;
586
sz->driver.set_use_dec = &streamzap_use_dec;
587
sz->driver.fops = &streamzap_fops;
602
588
#ifdef LIRC_HAVE_SYSFS
603
sz->plugin.dev = &udev->dev;
589
sz->driver.dev = &interface->dev;
605
sz->plugin.owner = THIS_MODULE;
591
sz->driver.owner = THIS_MODULE;
608
594
sz->decoder_state = PulseSpace;
615
601
init_timer(&sz->flush_timer);
616
602
sz->flush_timer.function = flush_timeout;
617
603
sz->flush_timer.data = (unsigned long) sz;
618
/***************************************************
619
* Complete final initialisations
604
/* Complete final initialisations */
622
606
usb_fill_int_urb(sz->urb_in, udev,
623
607
usb_rcvintpipe(udev, sz->endpoint->bEndpointAddress),
624
608
sz->buf_in, sz->buf_in_len, usb_streamzap_irq, sz,
625
609
sz->endpoint->bInterval);
611
sz->urb_in->transfer_dma = sz->dma_in;
612
sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
627
615
if (udev->descriptor.iManufacturer
628
&& usb_string(udev, udev->descriptor.iManufacturer, buf, 63) > 0)
629
strncpy(name, buf, 128);
616
&& usb_string(udev, udev->descriptor.iManufacturer,
617
buf, sizeof(buf)) > 0)
618
strlcpy(name, buf, sizeof(name));
631
620
if (udev->descriptor.iProduct
632
&& usb_string(udev, udev->descriptor.iProduct, buf, 63) > 0)
633
snprintf(name, 128, "%s %s", name, buf);
621
&& usb_string(udev, udev->descriptor.iProduct,
622
buf, sizeof(buf)) > 0)
623
snprintf(name + strlen(name), sizeof(name) - strlen(name),
635
626
printk(KERN_INFO DRIVER_NAME "[%d]: %s on usb%d:%d attached\n",
636
sz->plugin.minor, name,
627
sz->driver.minor, name,
637
628
udev->bus->busnum, sz->udev->devnum);
639
630
#ifdef KERNEL_2_5
640
631
usb_set_intfdata(interface, sz);
643
if (lirc_register_plugin(&sz->plugin) < 0) {
634
if (lirc_register_driver(&sz->driver) < 0) {
644
635
lirc_buffer_free(&sz->delay_buf);
645
636
lirc_buffer_free(&sz->lirc_buf);
664
655
err("Out of memory");
669
usb_free_urb(sz->urb_in);
658
usb_free_urb(sz->urb_in);
660
usb_buffer_free(udev, sz->buf_in_len, sz->buf_in, sz->dma_in);
671
662
if (sz->buf_in) {
673
usb_buffer_free(udev, sz->buf_in_len,
674
sz->buf_in, sz->dma_in);
676
663
kfree(sz->buf_in);
691
678
struct usb_streamzap *sz = data;
694
dprintk("%s called with no context", -1, __FUNCTION__);
681
dprintk("%s called with no context", -1, __func__);
697
dprintk("set use inc", sz->plugin.minor);
684
dprintk("set use inc", sz->driver.minor);
699
685
MOD_INC_USE_COUNT;
701
while (!lirc_buffer_empty(&sz->lirc_buf))
702
lirc_buffer_remove_1(&sz->lirc_buf);
703
while (!lirc_buffer_empty(&sz->delay_buf))
704
lirc_buffer_remove_1(&sz->delay_buf);
687
lirc_buffer_clear(&sz->lirc_buf);
688
lirc_buffer_clear(&sz->delay_buf);
706
690
sz->flush_timer.expires = jiffies + HZ;
728
712
struct usb_streamzap *sz = data;
731
dprintk("%s called with no context", -1, __FUNCTION__);
715
dprintk("%s called with no context", -1, __func__);
734
dprintk("set use dec", sz->plugin.minor);
718
dprintk("set use dec", sz->driver.minor);
767
* streamzap_disconnect
769
* Called by the usb core when the device is removed from the system.
771
* This routine guarantees that the driver will not submit any more urbs
772
* by clearing dev->udev. It is also supposed to terminate any currently
773
* active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
774
* does not provide any way to do this.
751
* streamzap_disconnect
753
* Called by the usb core when the device is removed from the system.
755
* This routine guarantees that the driver will not submit any more urbs
756
* by clearing dev->udev. It is also supposed to terminate any currently
757
* active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
758
* does not provide any way to do this.
776
760
#ifdef KERNEL_2_5
777
761
static void streamzap_disconnect(struct usb_interface *interface)
793
* unregister from the LIRC sub-system
776
/* unregister from the LIRC sub-system */
796
errnum = lirc_unregister_plugin(sz->plugin.minor);
778
errnum = lirc_unregister_driver(sz->driver.minor);
798
780
dprintk("error in lirc_unregister: (returned %d)",
799
sz->plugin.minor, errnum);
781
sz->driver.minor, errnum);
801
783
lirc_buffer_free(&sz->delay_buf);
802
784
lirc_buffer_free(&sz->lirc_buf);
805
* unregister from the USB sub-system
786
/* unregister from the USB sub-system */
808
788
usb_free_urb(sz->urb_in);
843
823
struct usb_streamzap *sz = usb_get_intfdata(intf);
845
while (!lirc_buffer_empty(&sz->lirc_buf))
846
lirc_buffer_remove_1(&sz->lirc_buf);
847
while (!lirc_buffer_empty(&sz->delay_buf))
848
lirc_buffer_remove_1(&sz->delay_buf);
825
lirc_buffer_clear(&sz->lirc_buf);
826
lirc_buffer_clear(&sz->delay_buf);
850
828
if (sz->in_use) {
851
829
sz->flush_timer.expires = jiffies + HZ;
908
883
MODULE_DESCRIPTION(DRIVER_DESC);
909
884
MODULE_LICENSE("GPL");
911
module_param(debug, bool, 0644);
886
module_param(debug, bool, S_IRUGO | S_IWUSR);
912
887
MODULE_PARM_DESC(debug, "Enable debugging messages");
914
888
EXPORT_NO_SYMBOLS;
916
889
#endif /* MODULE */