Merge branch 'for-linus' of git://neil.brown.name/md
[sfrench/cifs-2.6.git] / drivers / media / IR / streamzap.c
1 /*
2  * Streamzap Remote Control driver
3  *
4  * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de>
5  * Copyright (c) 2010 Jarod Wilson <jarod@wilsonet.com>
6  *
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.
13  *
14  * Ported to in-kernel ir-core interface by Jarod Wilson
15  *
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)
18  *
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.
23  *
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.
28  *
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
32  */
33
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>
40
41 #define DRIVER_VERSION  "1.60"
42 #define DRIVER_NAME     "streamzap"
43 #define DRIVER_DESC     "Streamzap Remote Control driver"
44
45 #ifdef CONFIG_USB_DEBUG
46 static int debug = 1;
47 #else
48 static int debug;
49 #endif
50
51 #define USB_STREAMZAP_VENDOR_ID         0x0e9c
52 #define USB_STREAMZAP_PRODUCT_ID        0x0000
53
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 */
59         { }
60 };
61
62 MODULE_DEVICE_TABLE(usb, streamzap_table);
63
64 #define STREAMZAP_PULSE_MASK 0xf0
65 #define STREAMZAP_SPACE_MASK 0x0f
66 #define STREAMZAP_TIMEOUT    0xff
67 #define STREAMZAP_RESOLUTION 256
68
69 /* number of samples buffered */
70 #define SZ_BUF_LEN 128
71
72 enum StreamzapDecoderState {
73         PulseSpace,
74         FullPulse,
75         FullSpace,
76         IgnorePulse
77 };
78
79 /* structure to hold our device specific stuff */
80 struct streamzap_ir {
81
82         /* ir-core */
83         struct ir_dev_props *props;
84         struct ir_raw_event rawir;
85
86         /* core device info */
87         struct device *dev;
88         struct input_dev *idev;
89
90         /* usb */
91         struct usb_device       *usbdev;
92         struct usb_interface    *interface;
93         struct usb_endpoint_descriptor *endpoint;
94         struct urb              *urb_in;
95
96         /* buffer & dma */
97         unsigned char           *buf_in;
98         dma_addr_t              dma_in;
99         unsigned int            buf_in_len;
100
101         /* timer used to support delay buffering */
102         struct timer_list       delay_timer;
103         bool                    timer_running;
104         spinlock_t              timer_lock;
105         struct timer_list       flush_timer;
106         bool                    flush;
107
108         /* delay buffer */
109         struct kfifo fifo;
110         bool fifo_initialized;
111
112         /* track what state we're in */
113         enum StreamzapDecoderState decoder_state;
114         /* tracks whether we are currently receiving some signal */
115         bool                    idle;
116         /* sum of signal lengths received since signal start */
117         unsigned long           sum;
118         /* start time of signal; necessary for gap tracking */
119         struct timeval          signal_last;
120         struct timeval          signal_start;
121         /* bool                 timeout_enabled; */
122
123         char                    name[128];
124         char                    phys[64];
125 };
126
127
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);
135
136 /* usb specific object needed to register this driver with the usb subsystem */
137 static struct usb_driver streamzap_driver = {
138         .name =         DRIVER_NAME,
139         .probe =        streamzap_probe,
140         .disconnect =   streamzap_disconnect,
141         .suspend =      streamzap_suspend,
142         .resume =       streamzap_resume,
143         .id_table =     streamzap_table,
144 };
145
146 static void streamzap_stop_timer(struct streamzap_ir *sz)
147 {
148         unsigned long flags;
149
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);
155         } else {
156                 spin_unlock_irqrestore(&sz->timer_lock, flags);
157         }
158 }
159
160 static void streamzap_flush_timeout(unsigned long arg)
161 {
162         struct streamzap_ir *sz = (struct streamzap_ir *)arg;
163
164         dev_info(sz->dev, "%s: callback firing\n", __func__);
165
166         /* finally start accepting data */
167         sz->flush = false;
168 }
169
170 static void streamzap_delay_timeout(unsigned long arg)
171 {
172         struct streamzap_ir *sz = (struct streamzap_ir *)arg;
173         struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
174         unsigned long flags;
175         int len, ret;
176         static unsigned long delay;
177         bool wake = false;
178
179         /* deliver data every 10 ms */
180         delay = msecs_to_jiffies(10);
181
182         spin_lock_irqsave(&sz->timer_lock, flags);
183
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);
189                 wake = true;
190         }
191
192         len = kfifo_len(&sz->fifo);
193         if (len > 0) {
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);
200                         wake = true;
201                         len = kfifo_len(&sz->fifo);
202                 }
203                 if (sz->timer_running)
204                         mod_timer(&sz->delay_timer, jiffies + delay);
205
206         } else {
207                 sz->timer_running = false;
208         }
209
210         if (wake)
211                 ir_raw_event_handle(sz->idev);
212
213         spin_unlock_irqrestore(&sz->timer_lock, flags);
214 }
215
216 static void streamzap_flush_delay_buffer(struct streamzap_ir *sz)
217 {
218         struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
219         bool wake = false;
220         int ret;
221
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);
227                 wake = true;
228         }
229
230         if (wake)
231                 ir_raw_event_handle(sz->idev);
232 }
233
234 static void sz_push(struct streamzap_ir *sz)
235 {
236         struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
237         unsigned long flags;
238         int ret;
239
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);
246         }
247
248         kfifo_in(&sz->fifo, &sz->rawir, sizeof(rawir));
249
250         if (!sz->timer_running) {
251                 sz->delay_timer.expires = jiffies + (HZ / 10);
252                 add_timer(&sz->delay_timer);
253                 sz->timer_running = true;
254         }
255
256         spin_unlock_irqrestore(&sz->timer_lock, flags);
257 }
258
259 static void sz_push_full_pulse(struct streamzap_ir *sz,
260                                unsigned char value)
261 {
262         if (sz->idle) {
263                 long deltv;
264
265                 sz->signal_last = sz->signal_start;
266                 do_gettimeofday(&sz->signal_start);
267
268                 deltv = sz->signal_start.tv_sec - sz->signal_last.tv_sec;
269                 sz->rawir.pulse = false;
270                 if (deltv > 15) {
271                         /* really long time */
272                         sz->rawir.duration = IR_MAX_DURATION;
273                 } else {
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;
280                 }
281                 dev_dbg(sz->dev, "ls %u\n", sz->rawir.duration);
282                 sz_push(sz);
283
284                 sz->idle = 0;
285                 sz->sum = 0;
286         }
287
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);
295         sz_push(sz);
296 }
297
298 static void sz_push_half_pulse(struct streamzap_ir *sz,
299                                unsigned char value)
300 {
301         sz_push_full_pulse(sz, (value & STREAMZAP_PULSE_MASK) >> 4);
302 }
303
304 static void sz_push_full_space(struct streamzap_ir *sz,
305                                unsigned char value)
306 {
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);
313         sz_push(sz);
314 }
315
316 static void sz_push_half_space(struct streamzap_ir *sz,
317                                unsigned long value)
318 {
319         sz_push_full_space(sz, value & STREAMZAP_SPACE_MASK);
320 }
321
322 /**
323  * streamzap_callback - usb IRQ handler callback
324  *
325  * This procedure is invoked on reception of data from
326  * the usb remote.
327  */
328 static void streamzap_callback(struct urb *urb)
329 {
330         struct streamzap_ir *sz;
331         unsigned int i;
332         int len;
333         #if 0
334         static int timeout = (((STREAMZAP_TIMEOUT * STREAMZAP_RESOLUTION) &
335                                 IR_MAX_DURATION) | 0x03000000);
336         #endif
337
338         if (!urb)
339                 return;
340
341         sz = urb->context;
342         len = urb->actual_length;
343
344         switch (urb->status) {
345         case -ECONNRESET:
346         case -ENOENT:
347         case -ESHUTDOWN:
348                 /*
349                  * this urb is terminated, clean up.
350                  * sz might already be invalid at this point
351                  */
352                 dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
353                 return;
354         default:
355                 break;
356         }
357
358         dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
359         if (!sz->flush) {
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) {
364                         case PulseSpace:
365                                 if ((sz->buf_in[i] & STREAMZAP_PULSE_MASK) ==
366                                     STREAMZAP_PULSE_MASK) {
367                                         sz->decoder_state = FullPulse;
368                                         continue;
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;
373                                         continue;
374                                 } else {
375                                         sz_push_half_pulse(sz, sz->buf_in[i]);
376                                         sz_push_half_space(sz, sz->buf_in[i]);
377                                 }
378                                 break;
379                         case FullPulse:
380                                 sz_push_full_pulse(sz, sz->buf_in[i]);
381                                 sz->decoder_state = IgnorePulse;
382                                 break;
383                         case FullSpace:
384                                 if (sz->buf_in[i] == STREAMZAP_TIMEOUT) {
385                                         sz->idle = 1;
386                                         streamzap_stop_timer(sz);
387                                         #if 0
388                                         if (sz->timeout_enabled) {
389                                                 sz->rawir.pulse = false;
390                                                 sz->rawir.duration = timeout;
391                                                 sz->rawir.duration *= 1000;
392                                                 sz_push(sz);
393                                         }
394                                         #endif
395                                         streamzap_flush_delay_buffer(sz);
396                                 } else
397                                         sz_push_full_space(sz, sz->buf_in[i]);
398                                 sz->decoder_state = PulseSpace;
399                                 break;
400                         case IgnorePulse:
401                                 if ((sz->buf_in[i]&STREAMZAP_SPACE_MASK) ==
402                                     STREAMZAP_SPACE_MASK) {
403                                         sz->decoder_state = FullSpace;
404                                         continue;
405                                 }
406                                 sz_push_half_space(sz, sz->buf_in[i]);
407                                 sz->decoder_state = PulseSpace;
408                                 break;
409                         }
410                 }
411         }
412
413         usb_submit_urb(urb, GFP_ATOMIC);
414
415         return;
416 }
417
418 static struct input_dev *streamzap_init_input_dev(struct streamzap_ir *sz)
419 {
420         struct input_dev *idev;
421         struct ir_dev_props *props;
422         struct device *dev = sz->dev;
423         int ret;
424
425         idev = input_allocate_device();
426         if (!idev) {
427                 dev_err(dev, "remote input dev allocation failed\n");
428                 goto idev_alloc_failed;
429         }
430
431         props = kzalloc(sizeof(struct ir_dev_props), GFP_KERNEL);
432         if (!props) {
433                 dev_err(dev, "remote ir dev props allocation failed\n");
434                 goto props_alloc_failed;
435         }
436
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));
441
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;
446
447         props->priv = sz;
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;
451
452         sz->props = props;
453
454         ret = ir_input_register(idev, RC_MAP_RC5_STREAMZAP, props, DRIVER_NAME);
455         if (ret < 0) {
456                 dev_err(dev, "remote input device register failed\n");
457                 goto irdev_failed;
458         }
459
460         return idev;
461
462 irdev_failed:
463         kfree(props);
464 props_alloc_failed:
465         input_free_device(idev);
466 idev_alloc_failed:
467         return NULL;
468 }
469
470 static int streamzap_delay_buf_init(struct streamzap_ir *sz)
471 {
472         int ret;
473
474         ret = kfifo_alloc(&sz->fifo, sizeof(int) * SZ_BUF_LEN,
475                           GFP_KERNEL);
476         if (ret == 0)
477                 sz->fifo_initialized = 1;
478
479         return ret;
480 }
481
482 static void streamzap_start_flush_timer(struct streamzap_ir *sz)
483 {
484         sz->flush_timer.expires = jiffies + HZ;
485         sz->flush = true;
486         add_timer(&sz->flush_timer);
487
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");
491 }
492
493 /**
494  *      streamzap_probe
495  *
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
499  */
500 static int __devinit streamzap_probe(struct usb_interface *intf,
501                                      const struct usb_device_id *id)
502 {
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;
508         int pipe, maxp;
509
510         /* Allocate space for device driver specific data */
511         sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
512         if (!sz)
513                 return -ENOMEM;
514
515         sz->usbdev = usbdev;
516         sz->interface = intf;
517
518         /* Check to ensure endpoint information matches requirements */
519         iface_host = intf->cur_altsetting;
520
521         if (iface_host->desc.bNumEndpoints != 1) {
522                 dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
523                         __func__, iface_host->desc.bNumEndpoints);
524                 retval = -ENODEV;
525                 goto free_sz;
526         }
527
528         sz->endpoint = &(iface_host->endpoint[0].desc);
529         if ((sz->endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
530             != USB_DIR_IN) {
531                 dev_err(&intf->dev, "%s: endpoint doesn't match input device "
532                         "02%02x\n", __func__, sz->endpoint->bEndpointAddress);
533                 retval = -ENODEV;
534                 goto free_sz;
535         }
536
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);
541                 retval = -ENODEV;
542                 goto free_sz;
543         }
544
545         pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress);
546         maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));
547
548         if (maxp == 0) {
549                 dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
550                         __func__);
551                 retval = -ENODEV;
552                 goto free_sz;
553         }
554
555         /* Allocate the USB buffer and IRQ URB */
556         sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
557         if (!sz->buf_in)
558                 goto free_sz;
559
560         sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
561         if (!sz->urb_in)
562                 goto free_buf_in;
563
564         sz->dev = &intf->dev;
565         sz->buf_in_len = maxp;
566
567         if (usbdev->descriptor.iManufacturer
568             && usb_string(usbdev, usbdev->descriptor.iManufacturer,
569                           buf, sizeof(buf)) > 0)
570                 strlcpy(name, buf, sizeof(name));
571
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),
576                          " %s", buf);
577
578         retval = streamzap_delay_buf_init(sz);
579         if (retval) {
580                 dev_err(&intf->dev, "%s: delay buffer init failed\n", __func__);
581                 goto free_urb_in;
582         }
583
584         sz->idev = streamzap_init_input_dev(sz);
585         if (!sz->idev)
586                 goto input_dev_fail;
587
588         sz->idle = true;
589         sz->decoder_state = PulseSpace;
590         #if 0
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;
596         #endif
597
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);
602
603         init_timer(&sz->flush_timer);
604         sz->flush_timer.function = streamzap_flush_timeout;
605         sz->flush_timer.data = (unsigned long)sz;
606
607         do_gettimeofday(&sz->signal_start);
608
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;
615
616         usb_set_intfdata(intf, sz);
617
618         streamzap_start_flush_timer(sz);
619
620         dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
621                  usbdev->bus->busnum, usbdev->devnum);
622
623         return 0;
624
625 input_dev_fail:
626         kfifo_free(&sz->fifo);
627 free_urb_in:
628         usb_free_urb(sz->urb_in);
629 free_buf_in:
630         usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
631 free_sz:
632         kfree(sz);
633
634         return retval;
635 }
636
637 /**
638  * streamzap_disconnect
639  *
640  * Called by the usb core when the device is removed from the system.
641  *
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.
646  */
647 static void streamzap_disconnect(struct usb_interface *interface)
648 {
649         struct streamzap_ir *sz = usb_get_intfdata(interface);
650         struct usb_device *usbdev = interface_to_usbdev(interface);
651
652         usb_set_intfdata(interface, NULL);
653
654         if (!sz)
655                 return;
656
657         if (sz->flush) {
658                 sz->flush = false;
659                 del_timer_sync(&sz->flush_timer);
660         }
661
662         streamzap_stop_timer(sz);
663
664         sz->usbdev = NULL;
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);
669
670         kfree(sz);
671 }
672
673 static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
674 {
675         struct streamzap_ir *sz = usb_get_intfdata(intf);
676
677         if (sz->flush) {
678                 sz->flush = false;
679                 del_timer_sync(&sz->flush_timer);
680         }
681
682         streamzap_stop_timer(sz);
683
684         usb_kill_urb(sz->urb_in);
685
686         return 0;
687 }
688
689 static int streamzap_resume(struct usb_interface *intf)
690 {
691         struct streamzap_ir *sz = usb_get_intfdata(intf);
692
693         if (sz->fifo_initialized)
694                 kfifo_reset(&sz->fifo);
695
696         sz->flush_timer.expires = jiffies + HZ;
697         sz->flush = true;
698         add_timer(&sz->flush_timer);
699
700         if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
701                 dev_err(sz->dev, "Error sumbiting urb\n");
702                 return -EIO;
703         }
704
705         return 0;
706 }
707
708 /**
709  *      streamzap_init
710  */
711 static int __init streamzap_init(void)
712 {
713         int ret;
714
715         /* register this driver with the USB subsystem */
716         ret = usb_register(&streamzap_driver);
717         if (ret < 0)
718                 printk(KERN_ERR DRIVER_NAME ": usb register failed, "
719                        "result = %d\n", ret);
720
721         return ret;
722 }
723
724 /**
725  *      streamzap_exit
726  */
727 static void __exit streamzap_exit(void)
728 {
729         usb_deregister(&streamzap_driver);
730 }
731
732
733 module_init(streamzap_init);
734 module_exit(streamzap_exit);
735
736 MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
737 MODULE_DESCRIPTION(DRIVER_DESC);
738 MODULE_LICENSE("GPL");
739
740 module_param(debug, bool, S_IRUGO | S_IWUSR);
741 MODULE_PARM_DESC(debug, "Enable debugging messages");