Merge tag 'for-5.1-rc2-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[linux-2.6-block.git] / drivers / media / rc / streamzap.c
CommitLineData
19770693
JW
1/*
2 * Streamzap Remote Control driver
3 *
4 * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de>
8e9e6064 5 * Copyright (c) 2010 Jarod Wilson <jarod@wilsonet.com>
19770693
JW
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 *
8e9e6064
JW
14 * Ported to in-kernel ir-core interface by Jarod Wilson
15 *
19770693
JW
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.
19770693
JW
28 */
29
8e9e6064 30#include <linux/device.h>
19770693 31#include <linux/module.h>
8e9e6064 32#include <linux/slab.h>
dd4c22a6 33#include <linux/ktime.h>
635f76b2
PB
34#include <linux/usb.h>
35#include <linux/usb/input.h>
6bda9644 36#include <media/rc-core.h>
19770693 37
7a569f52 38#define DRIVER_VERSION "1.61"
8e9e6064 39#define DRIVER_NAME "streamzap"
19770693
JW
40#define DRIVER_DESC "Streamzap Remote Control driver"
41
19770693
JW
42#define USB_STREAMZAP_VENDOR_ID 0x0e9c
43#define USB_STREAMZAP_PRODUCT_ID 0x0000
44
19770693 45/* table of devices that work with this driver */
5fad16b5 46static const struct usb_device_id streamzap_table[] = {
19770693
JW
47 /* Streamzap Remote Control */
48 { USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
49 /* Terminating entry */
50 { }
51};
52
53MODULE_DEVICE_TABLE(usb, streamzap_table);
54
b768d47e
JW
55#define SZ_PULSE_MASK 0xf0
56#define SZ_SPACE_MASK 0x0f
57#define SZ_TIMEOUT 0xff
58#define SZ_RESOLUTION 256
19770693
JW
59
60/* number of samples buffered */
8e9e6064 61#define SZ_BUF_LEN 128
19770693
JW
62
63enum StreamzapDecoderState {
64 PulseSpace,
65 FullPulse,
66 FullSpace,
67 IgnorePulse
68};
69
8e9e6064
JW
70/* structure to hold our device specific stuff */
71struct streamzap_ir {
8e9e6064 72 /* ir-core */
d8b4b582 73 struct rc_dev *rdev;
8e9e6064
JW
74
75 /* core device info */
76 struct device *dev;
19770693
JW
77
78 /* usb */
8e9e6064 79 struct usb_device *usbdev;
19770693 80 struct usb_interface *interface;
8e9e6064
JW
81 struct usb_endpoint_descriptor *endpoint;
82 struct urb *urb_in;
19770693
JW
83
84 /* buffer & dma */
85 unsigned char *buf_in;
86 dma_addr_t dma_in;
87 unsigned int buf_in_len;
88
8e9e6064
JW
89 /* track what state we're in */
90 enum StreamzapDecoderState decoder_state;
19770693 91 /* tracks whether we are currently receiving some signal */
8e9e6064 92 bool idle;
19770693
JW
93 /* sum of signal lengths received since signal start */
94 unsigned long sum;
95 /* start time of signal; necessary for gap tracking */
dd4c22a6
TR
96 ktime_t signal_last;
97 ktime_t signal_start;
7a569f52 98 bool timeout_enabled;
8e9e6064
JW
99
100 char name[128];
101 char phys[64];
19770693
JW
102};
103
104
105/* local function prototypes */
106static int streamzap_probe(struct usb_interface *interface,
107 const struct usb_device_id *id);
108static void streamzap_disconnect(struct usb_interface *interface);
8e9e6064 109static void streamzap_callback(struct urb *urb);
19770693
JW
110static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
111static int streamzap_resume(struct usb_interface *intf);
112
113/* usb specific object needed to register this driver with the usb subsystem */
19770693
JW
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
7a569f52 123static void sz_push(struct streamzap_ir *sz, struct ir_raw_event rawir)
19770693 124{
1338c925
JW
125 dev_dbg(sz->dev, "Storing %s with duration %u us\n",
126 (rawir.pulse ? "pulse" : "space"), rawir.duration);
d8b4b582 127 ir_raw_event_store_with_filter(sz->rdev, &rawir);
19770693
JW
128}
129
8e9e6064
JW
130static void sz_push_full_pulse(struct streamzap_ir *sz,
131 unsigned char value)
19770693 132{
183e19f5 133 struct ir_raw_event rawir = {};
7a569f52 134
19770693 135 if (sz->idle) {
dd4c22a6 136 int delta;
19770693
JW
137
138 sz->signal_last = sz->signal_start;
dd4c22a6 139 sz->signal_start = ktime_get_real();
19770693 140
dd4c22a6 141 delta = ktime_us_delta(sz->signal_start, sz->signal_last);
7a569f52 142 rawir.pulse = false;
dd4c22a6 143 if (delta > (15 * USEC_PER_SEC)) {
19770693 144 /* really long time */
7a569f52 145 rawir.duration = IR_MAX_DURATION;
19770693 146 } else {
dd4c22a6 147 rawir.duration = delta;
7a569f52 148 rawir.duration -= sz->sum;
b4608fae 149 rawir.duration = US_TO_NS(rawir.duration);
95cf60aa
MCC
150 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
151 IR_MAX_DURATION : rawir.duration;
19770693 152 }
7a569f52 153 sz_push(sz, rawir);
19770693 154
7a569f52 155 sz->idle = false;
19770693
JW
156 sz->sum = 0;
157 }
158
7a569f52 159 rawir.pulse = true;
b768d47e
JW
160 rawir.duration = ((int) value) * SZ_RESOLUTION;
161 rawir.duration += SZ_RESOLUTION / 2;
7a569f52 162 sz->sum += rawir.duration;
b4608fae 163 rawir.duration = US_TO_NS(rawir.duration);
95cf60aa
MCC
164 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
165 IR_MAX_DURATION : rawir.duration;
7a569f52 166 sz_push(sz, rawir);
19770693
JW
167}
168
8e9e6064
JW
169static void sz_push_half_pulse(struct streamzap_ir *sz,
170 unsigned char value)
19770693 171{
b768d47e 172 sz_push_full_pulse(sz, (value & SZ_PULSE_MASK) >> 4);
19770693
JW
173}
174
8e9e6064
JW
175static void sz_push_full_space(struct streamzap_ir *sz,
176 unsigned char value)
19770693 177{
183e19f5 178 struct ir_raw_event rawir = {};
7a569f52
JW
179
180 rawir.pulse = false;
b768d47e
JW
181 rawir.duration = ((int) value) * SZ_RESOLUTION;
182 rawir.duration += SZ_RESOLUTION / 2;
7a569f52 183 sz->sum += rawir.duration;
b4608fae 184 rawir.duration = US_TO_NS(rawir.duration);
7a569f52 185 sz_push(sz, rawir);
19770693
JW
186}
187
8e9e6064
JW
188static void sz_push_half_space(struct streamzap_ir *sz,
189 unsigned long value)
19770693 190{
b768d47e 191 sz_push_full_space(sz, value & SZ_SPACE_MASK);
19770693
JW
192}
193
cba862dc 194/*
8e9e6064 195 * streamzap_callback - usb IRQ handler callback
19770693
JW
196 *
197 * This procedure is invoked on reception of data from
198 * the usb remote.
199 */
8e9e6064 200static void streamzap_callback(struct urb *urb)
19770693 201{
8e9e6064
JW
202 struct streamzap_ir *sz;
203 unsigned int i;
204 int len;
19770693
JW
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 * this urb is terminated, clean up.
218 * sz might already be invalid at this point
219 */
8e9e6064 220 dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
19770693
JW
221 return;
222 default:
223 break;
224 }
225
8e9e6064 226 dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
7a569f52 227 for (i = 0; i < len; i++) {
1338c925 228 dev_dbg(sz->dev, "sz->buf_in[%d]: %x\n",
7a569f52
JW
229 i, (unsigned char)sz->buf_in[i]);
230 switch (sz->decoder_state) {
231 case PulseSpace:
b768d47e
JW
232 if ((sz->buf_in[i] & SZ_PULSE_MASK) ==
233 SZ_PULSE_MASK) {
7a569f52
JW
234 sz->decoder_state = FullPulse;
235 continue;
b768d47e
JW
236 } else if ((sz->buf_in[i] & SZ_SPACE_MASK)
237 == SZ_SPACE_MASK) {
7a569f52
JW
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]);
8e9e6064 243 sz_push_half_space(sz, sz->buf_in[i]);
19770693 244 }
7a569f52
JW
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:
b768d47e 251 if (sz->buf_in[i] == SZ_TIMEOUT) {
183e19f5
SY
252 struct ir_raw_event rawir = {
253 .pulse = false,
254 .duration = sz->rdev->timeout
255 };
7a569f52
JW
256 sz->idle = true;
257 if (sz->timeout_enabled)
258 sz_push(sz, rawir);
d8b4b582 259 ir_raw_event_handle(sz->rdev);
56b0ec30 260 ir_raw_event_reset(sz->rdev);
7a569f52
JW
261 } else {
262 sz_push_full_space(sz, sz->buf_in[i]);
263 }
264 sz->decoder_state = PulseSpace;
265 break;
266 case IgnorePulse:
b768d47e
JW
267 if ((sz->buf_in[i] & SZ_SPACE_MASK) ==
268 SZ_SPACE_MASK) {
7a569f52
JW
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;
19770693
JW
275 }
276 }
277
56b0ec30 278 ir_raw_event_handle(sz->rdev);
19770693
JW
279 usb_submit_urb(urb, GFP_ATOMIC);
280
281 return;
282}
283
d8b4b582 284static struct rc_dev *streamzap_init_rc_dev(struct streamzap_ir *sz)
8e9e6064 285{
d8b4b582 286 struct rc_dev *rdev;
8e9e6064
JW
287 struct device *dev = sz->dev;
288 int ret;
289
0f7499fd 290 rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
d8b4b582
DH
291 if (!rdev) {
292 dev_err(dev, "remote dev allocation failed\n");
293 goto out;
8e9e6064
JW
294 }
295
25ec587c 296 snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared Receiver (%04x:%04x)",
8e9e6064
JW
297 le16_to_cpu(sz->usbdev->descriptor.idVendor),
298 le16_to_cpu(sz->usbdev->descriptor.idProduct));
8e9e6064
JW
299 usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
300 strlcat(sz->phys, "/input0", sizeof(sz->phys));
8e9e6064 301
518f4b26 302 rdev->device_name = sz->name;
d8b4b582 303 rdev->input_phys = sz->phys;
5ad1a555
PB
304 usb_to_input_id(sz->usbdev, &rdev->input_id);
305 rdev->dev.parent = dev;
d8b4b582 306 rdev->priv = sz;
6d741bfe 307 rdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
d8b4b582
DH
308 rdev->driver_name = DRIVER_NAME;
309 rdev->map_name = RC_MAP_STREAMZAP;
635f76b2 310
d8b4b582 311 ret = rc_register_device(rdev);
8e9e6064
JW
312 if (ret < 0) {
313 dev_err(dev, "remote input device register failed\n");
d8b4b582 314 goto out;
8e9e6064
JW
315 }
316
d8b4b582 317 return rdev;
8e9e6064 318
d8b4b582
DH
319out:
320 rc_free_device(rdev);
8e9e6064
JW
321 return NULL;
322}
323
cba862dc 324/*
19770693
JW
325 * streamzap_probe
326 *
327 * Called by usb-core to associated with a candidate device
328 * On any failure the return value is the ERROR
329 * On success return 0
330 */
4c62e976
GKH
331static int streamzap_probe(struct usb_interface *intf,
332 const struct usb_device_id *id)
19770693 333{
8e9e6064 334 struct usb_device *usbdev = interface_to_usbdev(intf);
19770693 335 struct usb_host_interface *iface_host;
8e9e6064 336 struct streamzap_ir *sz = NULL;
19770693
JW
337 char buf[63], name[128] = "";
338 int retval = -ENOMEM;
8e9e6064 339 int pipe, maxp;
19770693
JW
340
341 /* Allocate space for device driver specific data */
8e9e6064
JW
342 sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
343 if (!sz)
19770693
JW
344 return -ENOMEM;
345
8e9e6064
JW
346 sz->usbdev = usbdev;
347 sz->interface = intf;
19770693
JW
348
349 /* Check to ensure endpoint information matches requirements */
8e9e6064 350 iface_host = intf->cur_altsetting;
19770693
JW
351
352 if (iface_host->desc.bNumEndpoints != 1) {
8e9e6064
JW
353 dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
354 __func__, iface_host->desc.bNumEndpoints);
19770693
JW
355 retval = -ENODEV;
356 goto free_sz;
357 }
358
359 sz->endpoint = &(iface_host->endpoint[0].desc);
5611588b 360 if (!usb_endpoint_dir_in(sz->endpoint)) {
25ec587c
MCC
361 dev_err(&intf->dev, "%s: endpoint doesn't match input device 02%02x\n",
362 __func__, sz->endpoint->bEndpointAddress);
19770693
JW
363 retval = -ENODEV;
364 goto free_sz;
365 }
366
5611588b 367 if (!usb_endpoint_xfer_int(sz->endpoint)) {
25ec587c
MCC
368 dev_err(&intf->dev, "%s: endpoint attributes don't match xfer 02%02x\n",
369 __func__, sz->endpoint->bmAttributes);
19770693
JW
370 retval = -ENODEV;
371 goto free_sz;
372 }
373
8e9e6064
JW
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__);
19770693
JW
380 retval = -ENODEV;
381 goto free_sz;
382 }
383
384 /* Allocate the USB buffer and IRQ URB */
8e9e6064
JW
385 sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
386 if (!sz->buf_in)
19770693
JW
387 goto free_sz;
388
389 sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
8e9e6064
JW
390 if (!sz->urb_in)
391 goto free_buf_in;
19770693 392
8e9e6064
JW
393 sz->dev = &intf->dev;
394 sz->buf_in_len = maxp;
19770693 395
8e9e6064
JW
396 if (usbdev->descriptor.iManufacturer
397 && usb_string(usbdev, usbdev->descriptor.iManufacturer,
19770693 398 buf, sizeof(buf)) > 0)
c0decac1 399 strscpy(name, buf, sizeof(name));
19770693 400
8e9e6064
JW
401 if (usbdev->descriptor.iProduct
402 && usb_string(usbdev, usbdev->descriptor.iProduct,
19770693
JW
403 buf, sizeof(buf)) > 0)
404 snprintf(name + strlen(name), sizeof(name) - strlen(name),
405 " %s", buf);
406
d8b4b582
DH
407 sz->rdev = streamzap_init_rc_dev(sz);
408 if (!sz->rdev)
409 goto rc_dev_fail;
19770693 410
8e9e6064
JW
411 sz->idle = true;
412 sz->decoder_state = PulseSpace;
7a569f52
JW
413 /* FIXME: don't yet have a way to set this */
414 sz->timeout_enabled = true;
b4608fae 415 sz->rdev->timeout = ((US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION) &
1338c925 416 IR_MAX_DURATION) | 0x03000000);
8e9e6064
JW
417 #if 0
418 /* not yet supported, depends on patches from maxim */
419 /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
b4608fae
JW
420 sz->min_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION);
421 sz->max_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION);
8e9e6064 422 #endif
19770693 423
dd4c22a6 424 sz->signal_start = ktime_get_real();
19770693 425
8e9e6064
JW
426 /* Complete final initialisations */
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;
19770693 432
8e9e6064 433 usb_set_intfdata(intf, sz);
19770693 434
7a569f52
JW
435 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
436 dev_err(sz->dev, "urb submit failed\n");
19770693 437
8e9e6064
JW
438 dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
439 usbdev->bus->busnum, usbdev->devnum);
19770693 440
8e9e6064 441 return 0;
19770693 442
d8b4b582 443rc_dev_fail:
8e9e6064
JW
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);
19770693 449
8e9e6064 450 return retval;
19770693
JW
451}
452
cba862dc 453/*
19770693
JW
454 * streamzap_disconnect
455 *
456 * Called by the usb core when the device is removed from the system.
457 *
458 * This routine guarantees that the driver will not submit any more urbs
8e9e6064 459 * by clearing dev->usbdev. It is also supposed to terminate any currently
19770693
JW
460 * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
461 * does not provide any way to do this.
462 */
463static void streamzap_disconnect(struct usb_interface *interface)
464{
8e9e6064
JW
465 struct streamzap_ir *sz = usb_get_intfdata(interface);
466 struct usb_device *usbdev = interface_to_usbdev(interface);
19770693 467
8e9e6064 468 usb_set_intfdata(interface, NULL);
19770693 469
8e9e6064
JW
470 if (!sz)
471 return;
19770693 472
8e9e6064 473 sz->usbdev = NULL;
d8b4b582 474 rc_unregister_device(sz->rdev);
8e9e6064 475 usb_kill_urb(sz->urb_in);
19770693 476 usb_free_urb(sz->urb_in);
8e9e6064 477 usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
19770693 478
19770693 479 kfree(sz);
19770693
JW
480}
481
482static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
483{
8e9e6064 484 struct streamzap_ir *sz = usb_get_intfdata(intf);
19770693 485
8e9e6064 486 usb_kill_urb(sz->urb_in);
19770693 487
19770693
JW
488 return 0;
489}
490
491static int streamzap_resume(struct usb_interface *intf)
492{
8e9e6064 493 struct streamzap_ir *sz = usb_get_intfdata(intf);
19770693 494
8e9e6064 495 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
1c195cb1 496 dev_err(sz->dev, "Error submitting urb\n");
8e9e6064 497 return -EIO;
19770693 498 }
8e9e6064 499
19770693
JW
500 return 0;
501}
502
ecb3b2b3 503module_usb_driver(streamzap_driver);
19770693 504
8e9e6064 505MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
19770693
JW
506MODULE_DESCRIPTION(DRIVER_DESC);
507MODULE_LICENSE("GPL");