blob: 058e29fd478ce7fc48252c843799f2773fdfd8d9 [file] [log] [blame]
Jarod Wilson19770692010-07-26 20:32:49 -03001/*
2 * Streamzap Remote Control driver
3 *
4 * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de>
Jarod Wilson8e9e6062010-08-03 01:07:04 -03005 * Copyright (c) 2010 Jarod Wilson <jarod@wilsonet.com>
Jarod Wilson19770692010-07-26 20:32:49 -03006 *
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 *
Jarod Wilson8e9e6062010-08-03 01:07:04 -030014 * Ported to in-kernel ir-core interface by Jarod Wilson
15 *
Jarod Wilson19770692010-07-26 20:32:49 -030016 * 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
Jarod Wilson8e9e6062010-08-03 01:07:04 -030034#include <linux/device.h>
Jarod Wilson19770692010-07-26 20:32:49 -030035#include <linux/module.h>
Jarod Wilson8e9e6062010-08-03 01:07:04 -030036#include <linux/slab.h>
Jarod Wilson19770692010-07-26 20:32:49 -030037#include <linux/usb.h>
Jarod Wilson8e9e6062010-08-03 01:07:04 -030038#include <linux/input.h>
39#include <media/ir-core.h>
Jarod Wilson19770692010-07-26 20:32:49 -030040
Jarod Wilson8e9e6062010-08-03 01:07:04 -030041#define DRIVER_VERSION "1.60"
42#define DRIVER_NAME "streamzap"
Jarod Wilson19770692010-07-26 20:32:49 -030043#define DRIVER_DESC "Streamzap Remote Control driver"
44
Jarod Wilson8e9e6062010-08-03 01:07:04 -030045#ifdef CONFIG_USB_DEBUG
46static int debug = 1;
47#else
Jarod Wilson19770692010-07-26 20:32:49 -030048static int debug;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030049#endif
Jarod Wilson19770692010-07-26 20:32:49 -030050
51#define USB_STREAMZAP_VENDOR_ID 0x0e9c
52#define USB_STREAMZAP_PRODUCT_ID 0x0000
53
Jarod Wilson19770692010-07-26 20:32:49 -030054/* table of devices that work with this driver */
55static 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
62MODULE_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 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030070#define SZ_BUF_LEN 128
Jarod Wilson19770692010-07-26 20:32:49 -030071
72enum StreamzapDecoderState {
73 PulseSpace,
74 FullPulse,
75 FullSpace,
76 IgnorePulse
77};
78
Jarod Wilson8e9e6062010-08-03 01:07:04 -030079/* structure to hold our device specific stuff */
80struct 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;
Jarod Wilson19770692010-07-26 20:32:49 -030089
90 /* usb */
Jarod Wilson8e9e6062010-08-03 01:07:04 -030091 struct usb_device *usbdev;
Jarod Wilson19770692010-07-26 20:32:49 -030092 struct usb_interface *interface;
Jarod Wilson8e9e6062010-08-03 01:07:04 -030093 struct usb_endpoint_descriptor *endpoint;
94 struct urb *urb_in;
Jarod Wilson19770692010-07-26 20:32:49 -030095
96 /* buffer & dma */
97 unsigned char *buf_in;
98 dma_addr_t dma_in;
99 unsigned int buf_in_len;
100
Jarod Wilson19770692010-07-26 20:32:49 -0300101 /* timer used to support delay buffering */
102 struct timer_list delay_timer;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300103 bool timer_running;
Jarod Wilson19770692010-07-26 20:32:49 -0300104 spinlock_t timer_lock;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300105 struct timer_list flush_timer;
106 bool flush;
Jarod Wilson19770692010-07-26 20:32:49 -0300107
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300108 /* delay buffer */
109 struct kfifo fifo;
110 bool fifo_initialized;
111
112 /* track what state we're in */
113 enum StreamzapDecoderState decoder_state;
Jarod Wilson19770692010-07-26 20:32:49 -0300114 /* tracks whether we are currently receiving some signal */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300115 bool idle;
Jarod Wilson19770692010-07-26 20:32:49 -0300116 /* 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;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300121 /* bool timeout_enabled; */
122
123 char name[128];
124 char phys[64];
Jarod Wilson19770692010-07-26 20:32:49 -0300125};
126
127
128/* local function prototypes */
129static int streamzap_probe(struct usb_interface *interface,
130 const struct usb_device_id *id);
131static void streamzap_disconnect(struct usb_interface *interface);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300132static void streamzap_callback(struct urb *urb);
Jarod Wilson19770692010-07-26 20:32:49 -0300133static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
134static int streamzap_resume(struct usb_interface *intf);
135
136/* usb specific object needed to register this driver with the usb subsystem */
Jarod Wilson19770692010-07-26 20:32:49 -0300137static 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
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300146static void streamzap_stop_timer(struct streamzap_ir *sz)
Jarod Wilson19770692010-07-26 20:32:49 -0300147{
148 unsigned long flags;
149
150 spin_lock_irqsave(&sz->timer_lock, flags);
151 if (sz->timer_running) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300152 sz->timer_running = false;
Jarod Wilson19770692010-07-26 20:32:49 -0300153 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
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300160static void streamzap_flush_timeout(unsigned long arg)
Jarod Wilson19770692010-07-26 20:32:49 -0300161{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300162 struct streamzap_ir *sz = (struct streamzap_ir *)arg;
163
164 dev_info(sz->dev, "%s: callback firing\n", __func__);
Jarod Wilson19770692010-07-26 20:32:49 -0300165
166 /* finally start accepting data */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300167 sz->flush = false;
Jarod Wilson19770692010-07-26 20:32:49 -0300168}
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300169
170static void streamzap_delay_timeout(unsigned long arg)
Jarod Wilson19770692010-07-26 20:32:49 -0300171{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300172 struct streamzap_ir *sz = (struct streamzap_ir *)arg;
173 struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
Jarod Wilson19770692010-07-26 20:32:49 -0300174 unsigned long flags;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300175 int len, ret;
176 static unsigned long delay;
177 bool wake = false;
178
Jarod Wilson19770692010-07-26 20:32:49 -0300179 /* deliver data every 10 ms */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300180 delay = msecs_to_jiffies(10);
Jarod Wilson19770692010-07-26 20:32:49 -0300181
182 spin_lock_irqsave(&sz->timer_lock, flags);
183
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300184 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;
Jarod Wilson19770692010-07-26 20:32:49 -0300190 }
191
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300192 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);
Jarod Wilson19770692010-07-26 20:32:49 -0300212
213 spin_unlock_irqrestore(&sz->timer_lock, flags);
214}
215
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300216static void streamzap_flush_delay_buffer(struct streamzap_ir *sz)
Jarod Wilson19770692010-07-26 20:32:49 -0300217{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300218 struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
219 bool wake = false;
220 int ret;
Jarod Wilson19770692010-07-26 20:32:49 -0300221
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300222 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;
Jarod Wilson19770692010-07-26 20:32:49 -0300228 }
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300229
230 if (wake)
231 ir_raw_event_handle(sz->idev);
Jarod Wilson19770692010-07-26 20:32:49 -0300232}
233
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300234static void sz_push(struct streamzap_ir *sz)
Jarod Wilson19770692010-07-26 20:32:49 -0300235{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300236 struct ir_raw_event rawir = { .pulse = false, .duration = 0 };
Jarod Wilson19770692010-07-26 20:32:49 -0300237 unsigned long flags;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300238 int ret;
Jarod Wilson19770692010-07-26 20:32:49 -0300239
240 spin_lock_irqsave(&sz->timer_lock, flags);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300241 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);
Jarod Wilson19770692010-07-26 20:32:49 -0300246 }
247
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300248 kfifo_in(&sz->fifo, &sz->rawir, sizeof(rawir));
Jarod Wilson19770692010-07-26 20:32:49 -0300249
250 if (!sz->timer_running) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300251 sz->delay_timer.expires = jiffies + (HZ / 10);
Jarod Wilson19770692010-07-26 20:32:49 -0300252 add_timer(&sz->delay_timer);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300253 sz->timer_running = true;
Jarod Wilson19770692010-07-26 20:32:49 -0300254 }
255
256 spin_unlock_irqrestore(&sz->timer_lock, flags);
257}
258
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300259static void sz_push_full_pulse(struct streamzap_ir *sz,
260 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300261{
Jarod Wilson19770692010-07-26 20:32:49 -0300262 if (sz->idle) {
263 long deltv;
Jarod Wilson19770692010-07-26 20:32:49 -0300264
265 sz->signal_last = sz->signal_start;
266 do_gettimeofday(&sz->signal_start);
267
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300268 deltv = sz->signal_start.tv_sec - sz->signal_last.tv_sec;
269 sz->rawir.pulse = false;
Jarod Wilson19770692010-07-26 20:32:49 -0300270 if (deltv > 15) {
271 /* really long time */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300272 sz->rawir.duration = IR_MAX_DURATION;
Jarod Wilson19770692010-07-26 20:32:49 -0300273 } else {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300274 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;
Jarod Wilson19770692010-07-26 20:32:49 -0300280 }
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300281 dev_dbg(sz->dev, "ls %u\n", sz->rawir.duration);
282 sz_push(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300283
284 sz->idle = 0;
285 sz->sum = 0;
286 }
287
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300288 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);
Jarod Wilson19770692010-07-26 20:32:49 -0300296}
297
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300298static void sz_push_half_pulse(struct streamzap_ir *sz,
299 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300300{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300301 sz_push_full_pulse(sz, (value & STREAMZAP_PULSE_MASK) >> 4);
Jarod Wilson19770692010-07-26 20:32:49 -0300302}
303
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300304static void sz_push_full_space(struct streamzap_ir *sz,
305 unsigned char value)
Jarod Wilson19770692010-07-26 20:32:49 -0300306{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300307 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);
Jarod Wilson19770692010-07-26 20:32:49 -0300314}
315
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300316static void sz_push_half_space(struct streamzap_ir *sz,
317 unsigned long value)
Jarod Wilson19770692010-07-26 20:32:49 -0300318{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300319 sz_push_full_space(sz, value & STREAMZAP_SPACE_MASK);
Jarod Wilson19770692010-07-26 20:32:49 -0300320}
321
322/**
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300323 * streamzap_callback - usb IRQ handler callback
Jarod Wilson19770692010-07-26 20:32:49 -0300324 *
325 * This procedure is invoked on reception of data from
326 * the usb remote.
327 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300328static void streamzap_callback(struct urb *urb)
Jarod Wilson19770692010-07-26 20:32:49 -0300329{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300330 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
Jarod Wilson19770692010-07-26 20:32:49 -0300337
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 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300352 dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
Jarod Wilson19770692010-07-26 20:32:49 -0300353 return;
354 default:
355 break;
356 }
357
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300358 dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
Jarod Wilson19770692010-07-26 20:32:49 -0300359 if (!sz->flush) {
360 for (i = 0; i < urb->actual_length; i++) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300361 dev_dbg(sz->dev, "%d: %x\n", i,
362 (unsigned char)sz->buf_in[i]);
Jarod Wilson19770692010-07-26 20:32:49 -0300363 switch (sz->decoder_state) {
364 case PulseSpace:
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300365 if ((sz->buf_in[i] & STREAMZAP_PULSE_MASK) ==
Jarod Wilson19770692010-07-26 20:32:49 -0300366 STREAMZAP_PULSE_MASK) {
367 sz->decoder_state = FullPulse;
368 continue;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300369 } else if ((sz->buf_in[i] & STREAMZAP_SPACE_MASK)
Jarod Wilson19770692010-07-26 20:32:49 -0300370 == STREAMZAP_SPACE_MASK) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300371 sz_push_half_pulse(sz, sz->buf_in[i]);
Jarod Wilson19770692010-07-26 20:32:49 -0300372 sz->decoder_state = FullSpace;
373 continue;
374 } else {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300375 sz_push_half_pulse(sz, sz->buf_in[i]);
376 sz_push_half_space(sz, sz->buf_in[i]);
Jarod Wilson19770692010-07-26 20:32:49 -0300377 }
378 break;
379 case FullPulse:
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300380 sz_push_full_pulse(sz, sz->buf_in[i]);
Jarod Wilson19770692010-07-26 20:32:49 -0300381 sz->decoder_state = IgnorePulse;
382 break;
383 case FullSpace:
384 if (sz->buf_in[i] == STREAMZAP_TIMEOUT) {
385 sz->idle = 1;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300386 streamzap_stop_timer(sz);
387 #if 0
Jarod Wilson19770692010-07-26 20:32:49 -0300388 if (sz->timeout_enabled) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300389 sz->rawir.pulse = false;
390 sz->rawir.duration = timeout;
391 sz->rawir.duration *= 1000;
392 sz_push(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300393 }
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300394 #endif
395 streamzap_flush_delay_buffer(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300396 } else
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300397 sz_push_full_space(sz, sz->buf_in[i]);
Jarod Wilson19770692010-07-26 20:32:49 -0300398 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 }
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300406 sz_push_half_space(sz, sz->buf_in[i]);
Jarod Wilson19770692010-07-26 20:32:49 -0300407 sz->decoder_state = PulseSpace;
408 break;
409 }
410 }
411 }
412
413 usb_submit_urb(urb, GFP_ATOMIC);
414
415 return;
416}
417
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300418static 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;
Jarod Wilson19770692010-07-26 20:32:49 -0300424
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300425 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
462irdev_failed:
463 kfree(props);
464props_alloc_failed:
465 input_free_device(idev);
466idev_alloc_failed:
467 return NULL;
468}
469
470static 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
482static 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}
Jarod Wilson19770692010-07-26 20:32:49 -0300492
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 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300500static int __devinit streamzap_probe(struct usb_interface *intf,
501 const struct usb_device_id *id)
Jarod Wilson19770692010-07-26 20:32:49 -0300502{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300503 struct usb_device *usbdev = interface_to_usbdev(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300504 struct usb_host_interface *iface_host;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300505 struct streamzap_ir *sz = NULL;
Jarod Wilson19770692010-07-26 20:32:49 -0300506 char buf[63], name[128] = "";
507 int retval = -ENOMEM;
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300508 int pipe, maxp;
Jarod Wilson19770692010-07-26 20:32:49 -0300509
510 /* Allocate space for device driver specific data */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300511 sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
512 if (!sz)
Jarod Wilson19770692010-07-26 20:32:49 -0300513 return -ENOMEM;
514
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300515 sz->usbdev = usbdev;
516 sz->interface = intf;
Jarod Wilson19770692010-07-26 20:32:49 -0300517
518 /* Check to ensure endpoint information matches requirements */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300519 iface_host = intf->cur_altsetting;
Jarod Wilson19770692010-07-26 20:32:49 -0300520
521 if (iface_host->desc.bNumEndpoints != 1) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300522 dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
523 __func__, iface_host->desc.bNumEndpoints);
Jarod Wilson19770692010-07-26 20:32:49 -0300524 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) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300531 dev_err(&intf->dev, "%s: endpoint doesn't match input device "
532 "02%02x\n", __func__, sz->endpoint->bEndpointAddress);
Jarod Wilson19770692010-07-26 20:32:49 -0300533 retval = -ENODEV;
534 goto free_sz;
535 }
536
537 if ((sz->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
538 != USB_ENDPOINT_XFER_INT) {
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300539 dev_err(&intf->dev, "%s: endpoint attributes don't match xfer "
540 "02%02x\n", __func__, sz->endpoint->bmAttributes);
Jarod Wilson19770692010-07-26 20:32:49 -0300541 retval = -ENODEV;
542 goto free_sz;
543 }
544
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300545 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__);
Jarod Wilson19770692010-07-26 20:32:49 -0300551 retval = -ENODEV;
552 goto free_sz;
553 }
554
555 /* Allocate the USB buffer and IRQ URB */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300556 sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
557 if (!sz->buf_in)
Jarod Wilson19770692010-07-26 20:32:49 -0300558 goto free_sz;
559
560 sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300561 if (!sz->urb_in)
562 goto free_buf_in;
Jarod Wilson19770692010-07-26 20:32:49 -0300563
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300564 sz->dev = &intf->dev;
565 sz->buf_in_len = maxp;
Jarod Wilson19770692010-07-26 20:32:49 -0300566
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300567 if (usbdev->descriptor.iManufacturer
568 && usb_string(usbdev, usbdev->descriptor.iManufacturer,
Jarod Wilson19770692010-07-26 20:32:49 -0300569 buf, sizeof(buf)) > 0)
570 strlcpy(name, buf, sizeof(name));
571
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300572 if (usbdev->descriptor.iProduct
573 && usb_string(usbdev, usbdev->descriptor.iProduct,
Jarod Wilson19770692010-07-26 20:32:49 -0300574 buf, sizeof(buf)) > 0)
575 snprintf(name + strlen(name), sizeof(name) - strlen(name),
576 " %s", buf);
577
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300578 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 }
Jarod Wilson19770692010-07-26 20:32:49 -0300583
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300584 sz->idev = streamzap_init_input_dev(sz);
585 if (!sz->idev)
586 goto input_dev_fail;
Jarod Wilson19770692010-07-26 20:32:49 -0300587
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300588 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
Jarod Wilson19770692010-07-26 20:32:49 -0300597
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300598 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);
Jarod Wilson19770692010-07-26 20:32:49 -0300602
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300603 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);
Jarod Wilson19770692010-07-26 20:32:49 -0300622
623 return 0;
624
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300625input_dev_fail:
626 kfifo_free(&sz->fifo);
627free_urb_in:
628 usb_free_urb(sz->urb_in);
629free_buf_in:
630 usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300631free_sz:
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300632 kfree(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300633
634 return retval;
635}
636
Jarod Wilson19770692010-07-26 20:32:49 -0300637/**
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
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300643 * by clearing dev->usbdev. It is also supposed to terminate any currently
Jarod Wilson19770692010-07-26 20:32:49 -0300644 * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
645 * does not provide any way to do this.
646 */
647static void streamzap_disconnect(struct usb_interface *interface)
648{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300649 struct streamzap_ir *sz = usb_get_intfdata(interface);
650 struct usb_device *usbdev = interface_to_usbdev(interface);
Jarod Wilson19770692010-07-26 20:32:49 -0300651
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300652 usb_set_intfdata(interface, NULL);
Jarod Wilson19770692010-07-26 20:32:49 -0300653
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300654 if (!sz)
655 return;
Jarod Wilson19770692010-07-26 20:32:49 -0300656
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300657 if (sz->flush) {
658 sz->flush = false;
659 del_timer_sync(&sz->flush_timer);
660 }
Jarod Wilson19770692010-07-26 20:32:49 -0300661
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300662 streamzap_stop_timer(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300663
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300664 sz->usbdev = NULL;
665 ir_input_unregister(sz->idev);
666 usb_kill_urb(sz->urb_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300667 usb_free_urb(sz->urb_in);
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300668 usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
Jarod Wilson19770692010-07-26 20:32:49 -0300669
Jarod Wilson19770692010-07-26 20:32:49 -0300670 kfree(sz);
Jarod Wilson19770692010-07-26 20:32:49 -0300671}
672
673static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
674{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300675 struct streamzap_ir *sz = usb_get_intfdata(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300676
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300677 if (sz->flush) {
678 sz->flush = false;
679 del_timer_sync(&sz->flush_timer);
Jarod Wilson19770692010-07-26 20:32:49 -0300680 }
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300681
682 streamzap_stop_timer(sz);
683
684 usb_kill_urb(sz->urb_in);
685
Jarod Wilson19770692010-07-26 20:32:49 -0300686 return 0;
687}
688
689static int streamzap_resume(struct usb_interface *intf)
690{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300691 struct streamzap_ir *sz = usb_get_intfdata(intf);
Jarod Wilson19770692010-07-26 20:32:49 -0300692
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300693 if (sz->fifo_initialized)
694 kfifo_reset(&sz->fifo);
Jarod Wilson19770692010-07-26 20:32:49 -0300695
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300696 sz->flush_timer.expires = jiffies + HZ;
697 sz->flush = true;
698 add_timer(&sz->flush_timer);
Jarod Wilson19770692010-07-26 20:32:49 -0300699
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300700 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
701 dev_err(sz->dev, "Error sumbiting urb\n");
702 return -EIO;
Jarod Wilson19770692010-07-26 20:32:49 -0300703 }
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300704
Jarod Wilson19770692010-07-26 20:32:49 -0300705 return 0;
706}
707
708/**
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300709 * streamzap_init
Jarod Wilson19770692010-07-26 20:32:49 -0300710 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300711static int __init streamzap_init(void)
Jarod Wilson19770692010-07-26 20:32:49 -0300712{
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300713 int ret;
Jarod Wilson19770692010-07-26 20:32:49 -0300714
715 /* register this driver with the USB subsystem */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300716 ret = usb_register(&streamzap_driver);
717 if (ret < 0)
718 printk(KERN_ERR DRIVER_NAME ": usb register failed, "
719 "result = %d\n", ret);
Jarod Wilson19770692010-07-26 20:32:49 -0300720
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300721 return ret;
Jarod Wilson19770692010-07-26 20:32:49 -0300722}
723
724/**
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300725 * streamzap_exit
Jarod Wilson19770692010-07-26 20:32:49 -0300726 */
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300727static void __exit streamzap_exit(void)
Jarod Wilson19770692010-07-26 20:32:49 -0300728{
729 usb_deregister(&streamzap_driver);
730}
731
732
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300733module_init(streamzap_init);
734module_exit(streamzap_exit);
Jarod Wilson19770692010-07-26 20:32:49 -0300735
Jarod Wilson8e9e6062010-08-03 01:07:04 -0300736MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
Jarod Wilson19770692010-07-26 20:32:49 -0300737MODULE_DESCRIPTION(DRIVER_DESC);
738MODULE_LICENSE("GPL");
739
740module_param(debug, bool, S_IRUGO | S_IWUSR);
741MODULE_PARM_DESC(debug, "Enable debugging messages");