1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/device.h>
31#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/ktime.h>
34#include <linux/usb.h>
35#include <linux/usb/input.h>
36#include <media/rc-core.h>
37
38#define DRIVER_VERSION "1.61"
39#define DRIVER_NAME "streamzap"
40#define DRIVER_DESC "Streamzap Remote Control driver"
41
42#define USB_STREAMZAP_VENDOR_ID 0x0e9c
43#define USB_STREAMZAP_PRODUCT_ID 0x0000
44
45
46static const struct usb_device_id streamzap_table[] = {
47
48 { USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
49
50 { }
51};
52
53MODULE_DEVICE_TABLE(usb, streamzap_table);
54
55#define SZ_PULSE_MASK 0xf0
56#define SZ_SPACE_MASK 0x0f
57#define SZ_TIMEOUT 0xff
58#define SZ_RESOLUTION 256
59
60
61#define SZ_BUF_LEN 128
62
63enum StreamzapDecoderState {
64 PulseSpace,
65 FullPulse,
66 FullSpace,
67 IgnorePulse
68};
69
70
71struct streamzap_ir {
72
73 struct rc_dev *rdev;
74
75
76 struct device *dev;
77
78
79 struct usb_device *usbdev;
80 struct usb_interface *interface;
81 struct usb_endpoint_descriptor *endpoint;
82 struct urb *urb_in;
83
84
85 unsigned char *buf_in;
86 dma_addr_t dma_in;
87 unsigned int buf_in_len;
88
89
90 enum StreamzapDecoderState decoder_state;
91
92 bool idle;
93
94 unsigned long sum;
95
96 ktime_t signal_last;
97 ktime_t signal_start;
98 bool timeout_enabled;
99
100 char name[128];
101 char phys[64];
102};
103
104
105
106static int streamzap_probe(struct usb_interface *interface,
107 const struct usb_device_id *id);
108static void streamzap_disconnect(struct usb_interface *interface);
109static void streamzap_callback(struct urb *urb);
110static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
111static int streamzap_resume(struct usb_interface *intf);
112
113
114static struct usb_driver streamzap_driver = {
115 .name = DRIVER_NAME,
116 .probe = streamzap_probe,
117 .disconnect = streamzap_disconnect,
118 .suspend = streamzap_suspend,
119 .resume = streamzap_resume,
120 .id_table = streamzap_table,
121};
122
123static void sz_push(struct streamzap_ir *sz, struct ir_raw_event rawir)
124{
125 dev_dbg(sz->dev, "Storing %s with duration %u us\n",
126 (rawir.pulse ? "pulse" : "space"), rawir.duration);
127 ir_raw_event_store_with_filter(sz->rdev, &rawir);
128}
129
130static void sz_push_full_pulse(struct streamzap_ir *sz,
131 unsigned char value)
132{
133 DEFINE_IR_RAW_EVENT(rawir);
134
135 if (sz->idle) {
136 int delta;
137
138 sz->signal_last = sz->signal_start;
139 sz->signal_start = ktime_get_real();
140
141 delta = ktime_us_delta(sz->signal_start, sz->signal_last);
142 rawir.pulse = false;
143 if (delta > (15 * USEC_PER_SEC)) {
144
145 rawir.duration = IR_MAX_DURATION;
146 } else {
147 rawir.duration = delta;
148 rawir.duration -= sz->sum;
149 rawir.duration = US_TO_NS(rawir.duration);
150 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
151 IR_MAX_DURATION : rawir.duration;
152 }
153 sz_push(sz, rawir);
154
155 sz->idle = false;
156 sz->sum = 0;
157 }
158
159 rawir.pulse = true;
160 rawir.duration = ((int) value) * SZ_RESOLUTION;
161 rawir.duration += SZ_RESOLUTION / 2;
162 sz->sum += rawir.duration;
163 rawir.duration = US_TO_NS(rawir.duration);
164 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
165 IR_MAX_DURATION : rawir.duration;
166 sz_push(sz, rawir);
167}
168
169static void sz_push_half_pulse(struct streamzap_ir *sz,
170 unsigned char value)
171{
172 sz_push_full_pulse(sz, (value & SZ_PULSE_MASK) >> 4);
173}
174
175static void sz_push_full_space(struct streamzap_ir *sz,
176 unsigned char value)
177{
178 DEFINE_IR_RAW_EVENT(rawir);
179
180 rawir.pulse = false;
181 rawir.duration = ((int) value) * SZ_RESOLUTION;
182 rawir.duration += SZ_RESOLUTION / 2;
183 sz->sum += rawir.duration;
184 rawir.duration = US_TO_NS(rawir.duration);
185 sz_push(sz, rawir);
186}
187
188static void sz_push_half_space(struct streamzap_ir *sz,
189 unsigned long value)
190{
191 sz_push_full_space(sz, value & SZ_SPACE_MASK);
192}
193
194
195
196
197
198
199
200static void streamzap_callback(struct urb *urb)
201{
202 struct streamzap_ir *sz;
203 unsigned int i;
204 int len;
205
206 if (!urb)
207 return;
208
209 sz = urb->context;
210 len = urb->actual_length;
211
212 switch (urb->status) {
213 case -ECONNRESET:
214 case -ENOENT:
215 case -ESHUTDOWN:
216
217
218
219
220 dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
221 return;
222 default:
223 break;
224 }
225
226 dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
227 for (i = 0; i < len; i++) {
228 dev_dbg(sz->dev, "sz->buf_in[%d]: %x\n",
229 i, (unsigned char)sz->buf_in[i]);
230 switch (sz->decoder_state) {
231 case PulseSpace:
232 if ((sz->buf_in[i] & SZ_PULSE_MASK) ==
233 SZ_PULSE_MASK) {
234 sz->decoder_state = FullPulse;
235 continue;
236 } else if ((sz->buf_in[i] & SZ_SPACE_MASK)
237 == SZ_SPACE_MASK) {
238 sz_push_half_pulse(sz, sz->buf_in[i]);
239 sz->decoder_state = FullSpace;
240 continue;
241 } else {
242 sz_push_half_pulse(sz, sz->buf_in[i]);
243 sz_push_half_space(sz, sz->buf_in[i]);
244 }
245 break;
246 case FullPulse:
247 sz_push_full_pulse(sz, sz->buf_in[i]);
248 sz->decoder_state = IgnorePulse;
249 break;
250 case FullSpace:
251 if (sz->buf_in[i] == SZ_TIMEOUT) {
252 DEFINE_IR_RAW_EVENT(rawir);
253
254 rawir.pulse = false;
255 rawir.duration = sz->rdev->timeout;
256 sz->idle = true;
257 if (sz->timeout_enabled)
258 sz_push(sz, rawir);
259 ir_raw_event_handle(sz->rdev);
260 ir_raw_event_reset(sz->rdev);
261 } else {
262 sz_push_full_space(sz, sz->buf_in[i]);
263 }
264 sz->decoder_state = PulseSpace;
265 break;
266 case IgnorePulse:
267 if ((sz->buf_in[i] & SZ_SPACE_MASK) ==
268 SZ_SPACE_MASK) {
269 sz->decoder_state = FullSpace;
270 continue;
271 }
272 sz_push_half_space(sz, sz->buf_in[i]);
273 sz->decoder_state = PulseSpace;
274 break;
275 }
276 }
277
278 ir_raw_event_handle(sz->rdev);
279 usb_submit_urb(urb, GFP_ATOMIC);
280
281 return;
282}
283
284static struct rc_dev *streamzap_init_rc_dev(struct streamzap_ir *sz)
285{
286 struct rc_dev *rdev;
287 struct device *dev = sz->dev;
288 int ret;
289
290 rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
291 if (!rdev) {
292 dev_err(dev, "remote dev allocation failed\n");
293 goto out;
294 }
295
296 snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared Receiver (%04x:%04x)",
297 le16_to_cpu(sz->usbdev->descriptor.idVendor),
298 le16_to_cpu(sz->usbdev->descriptor.idProduct));
299 usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
300 strlcat(sz->phys, "/input0", sizeof(sz->phys));
301
302 rdev->device_name = sz->name;
303 rdev->input_phys = sz->phys;
304 usb_to_input_id(sz->usbdev, &rdev->input_id);
305 rdev->dev.parent = dev;
306 rdev->priv = sz;
307 rdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
308 rdev->driver_name = DRIVER_NAME;
309 rdev->map_name = RC_MAP_STREAMZAP;
310
311 ret = rc_register_device(rdev);
312 if (ret < 0) {
313 dev_err(dev, "remote input device register failed\n");
314 goto out;
315 }
316
317 return rdev;
318
319out:
320 rc_free_device(rdev);
321 return NULL;
322}
323
324
325
326
327
328
329
330
331static int streamzap_probe(struct usb_interface *intf,
332 const struct usb_device_id *id)
333{
334 struct usb_device *usbdev = interface_to_usbdev(intf);
335 struct usb_host_interface *iface_host;
336 struct streamzap_ir *sz = NULL;
337 char buf[63], name[128] = "";
338 int retval = -ENOMEM;
339 int pipe, maxp;
340
341
342 sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
343 if (!sz)
344 return -ENOMEM;
345
346 sz->usbdev = usbdev;
347 sz->interface = intf;
348
349
350 iface_host = intf->cur_altsetting;
351
352 if (iface_host->desc.bNumEndpoints != 1) {
353 dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
354 __func__, iface_host->desc.bNumEndpoints);
355 retval = -ENODEV;
356 goto free_sz;
357 }
358
359 sz->endpoint = &(iface_host->endpoint[0].desc);
360 if (!usb_endpoint_dir_in(sz->endpoint)) {
361 dev_err(&intf->dev, "%s: endpoint doesn't match input device 02%02x\n",
362 __func__, sz->endpoint->bEndpointAddress);
363 retval = -ENODEV;
364 goto free_sz;
365 }
366
367 if (!usb_endpoint_xfer_int(sz->endpoint)) {
368 dev_err(&intf->dev, "%s: endpoint attributes don't match xfer 02%02x\n",
369 __func__, sz->endpoint->bmAttributes);
370 retval = -ENODEV;
371 goto free_sz;
372 }
373
374 pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress);
375 maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));
376
377 if (maxp == 0) {
378 dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
379 __func__);
380 retval = -ENODEV;
381 goto free_sz;
382 }
383
384
385 sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
386 if (!sz->buf_in)
387 goto free_sz;
388
389 sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
390 if (!sz->urb_in)
391 goto free_buf_in;
392
393 sz->dev = &intf->dev;
394 sz->buf_in_len = maxp;
395
396 if (usbdev->descriptor.iManufacturer
397 && usb_string(usbdev, usbdev->descriptor.iManufacturer,
398 buf, sizeof(buf)) > 0)
399 strlcpy(name, buf, sizeof(name));
400
401 if (usbdev->descriptor.iProduct
402 && usb_string(usbdev, usbdev->descriptor.iProduct,
403 buf, sizeof(buf)) > 0)
404 snprintf(name + strlen(name), sizeof(name) - strlen(name),
405 " %s", buf);
406
407 sz->rdev = streamzap_init_rc_dev(sz);
408 if (!sz->rdev)
409 goto rc_dev_fail;
410
411 sz->idle = true;
412 sz->decoder_state = PulseSpace;
413
414 sz->timeout_enabled = true;
415 sz->rdev->timeout = ((US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION) &
416 IR_MAX_DURATION) | 0x03000000);
417 #if 0
418
419
420 sz->min_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION);
421 sz->max_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION);
422 #endif
423
424 sz->signal_start = ktime_get_real();
425
426
427 usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
428 maxp, (usb_complete_t)streamzap_callback,
429 sz, sz->endpoint->bInterval);
430 sz->urb_in->transfer_dma = sz->dma_in;
431 sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
432
433 usb_set_intfdata(intf, sz);
434
435 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
436 dev_err(sz->dev, "urb submit failed\n");
437
438 dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
439 usbdev->bus->busnum, usbdev->devnum);
440
441 return 0;
442
443rc_dev_fail:
444 usb_free_urb(sz->urb_in);
445free_buf_in:
446 usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
447free_sz:
448 kfree(sz);
449
450 return retval;
451}
452
453
454
455
456
457
458
459
460
461
462
463static void streamzap_disconnect(struct usb_interface *interface)
464{
465 struct streamzap_ir *sz = usb_get_intfdata(interface);
466 struct usb_device *usbdev = interface_to_usbdev(interface);
467
468 usb_set_intfdata(interface, NULL);
469
470 if (!sz)
471 return;
472
473 sz->usbdev = NULL;
474 rc_unregister_device(sz->rdev);
475 usb_kill_urb(sz->urb_in);
476 usb_free_urb(sz->urb_in);
477 usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
478
479 kfree(sz);
480}
481
482static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
483{
484 struct streamzap_ir *sz = usb_get_intfdata(intf);
485
486 usb_kill_urb(sz->urb_in);
487
488 return 0;
489}
490
491static int streamzap_resume(struct usb_interface *intf)
492{
493 struct streamzap_ir *sz = usb_get_intfdata(intf);
494
495 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
496 dev_err(sz->dev, "Error submitting urb\n");
497 return -EIO;
498 }
499
500 return 0;
501}
502
503module_usb_driver(streamzap_driver);
504
505MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
506MODULE_DESCRIPTION(DRIVER_DESC);
507MODULE_LICENSE("GPL");
508