gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / media / rc / streamzap.c
blob79a41fc7161cb303b0ca83872eac540eb1ee36f2
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Streamzap Remote Control driver
5 * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de>
6 * Copyright (c) 2010 Jarod Wilson <jarod@wilsonet.com>
8 * This driver was based on the work of Greg Wickham and Adrian
9 * Dewhurst. It was substantially rewritten to support correct signal
10 * gaps and now maintains a delay buffer, which is used to present
11 * consistent timing behaviour to user space applications. Without the
12 * delay buffer an ugly hack would be required in lircd, which can
13 * cause sluggish signal decoding in certain situations.
15 * Ported to in-kernel ir-core interface by Jarod Wilson
17 * This driver is based on the USB skeleton driver packaged with the
18 * kernel; copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
21 #include <linux/device.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/ktime.h>
25 #include <linux/usb.h>
26 #include <linux/usb/input.h>
27 #include <media/rc-core.h>
29 #define DRIVER_VERSION "1.61"
30 #define DRIVER_NAME "streamzap"
31 #define DRIVER_DESC "Streamzap Remote Control driver"
33 #define USB_STREAMZAP_VENDOR_ID 0x0e9c
34 #define USB_STREAMZAP_PRODUCT_ID 0x0000
36 /* table of devices that work with this driver */
37 static const struct usb_device_id streamzap_table[] = {
38 /* Streamzap Remote Control */
39 { USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
40 /* Terminating entry */
41 { }
44 MODULE_DEVICE_TABLE(usb, streamzap_table);
46 #define SZ_PULSE_MASK 0xf0
47 #define SZ_SPACE_MASK 0x0f
48 #define SZ_TIMEOUT 0xff
49 #define SZ_RESOLUTION 256
51 /* number of samples buffered */
52 #define SZ_BUF_LEN 128
54 enum StreamzapDecoderState {
55 PulseSpace,
56 FullPulse,
57 FullSpace,
58 IgnorePulse
61 /* structure to hold our device specific stuff */
62 struct streamzap_ir {
63 /* ir-core */
64 struct rc_dev *rdev;
66 /* core device info */
67 struct device *dev;
69 /* usb */
70 struct usb_device *usbdev;
71 struct usb_interface *interface;
72 struct usb_endpoint_descriptor *endpoint;
73 struct urb *urb_in;
75 /* buffer & dma */
76 unsigned char *buf_in;
77 dma_addr_t dma_in;
78 unsigned int buf_in_len;
80 /* track what state we're in */
81 enum StreamzapDecoderState decoder_state;
82 /* tracks whether we are currently receiving some signal */
83 bool idle;
84 /* sum of signal lengths received since signal start */
85 unsigned long sum;
86 /* start time of signal; necessary for gap tracking */
87 ktime_t signal_last;
88 ktime_t signal_start;
89 bool timeout_enabled;
91 char name[128];
92 char phys[64];
96 /* local function prototypes */
97 static int streamzap_probe(struct usb_interface *interface,
98 const struct usb_device_id *id);
99 static void streamzap_disconnect(struct usb_interface *interface);
100 static void streamzap_callback(struct urb *urb);
101 static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
102 static int streamzap_resume(struct usb_interface *intf);
104 /* usb specific object needed to register this driver with the usb subsystem */
105 static struct usb_driver streamzap_driver = {
106 .name = DRIVER_NAME,
107 .probe = streamzap_probe,
108 .disconnect = streamzap_disconnect,
109 .suspend = streamzap_suspend,
110 .resume = streamzap_resume,
111 .id_table = streamzap_table,
114 static void sz_push(struct streamzap_ir *sz, struct ir_raw_event rawir)
116 dev_dbg(sz->dev, "Storing %s with duration %u us\n",
117 (rawir.pulse ? "pulse" : "space"), rawir.duration);
118 ir_raw_event_store_with_filter(sz->rdev, &rawir);
121 static void sz_push_full_pulse(struct streamzap_ir *sz,
122 unsigned char value)
124 struct ir_raw_event rawir = {};
126 if (sz->idle) {
127 int delta;
129 sz->signal_last = sz->signal_start;
130 sz->signal_start = ktime_get_real();
132 delta = ktime_us_delta(sz->signal_start, sz->signal_last);
133 rawir.pulse = false;
134 if (delta > (15 * USEC_PER_SEC)) {
135 /* really long time */
136 rawir.duration = IR_MAX_DURATION;
137 } else {
138 rawir.duration = delta;
139 rawir.duration -= sz->sum;
140 rawir.duration = US_TO_NS(rawir.duration);
141 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
142 IR_MAX_DURATION : rawir.duration;
144 sz_push(sz, rawir);
146 sz->idle = false;
147 sz->sum = 0;
150 rawir.pulse = true;
151 rawir.duration = ((int) value) * SZ_RESOLUTION;
152 rawir.duration += SZ_RESOLUTION / 2;
153 sz->sum += rawir.duration;
154 rawir.duration = US_TO_NS(rawir.duration);
155 rawir.duration = (rawir.duration > IR_MAX_DURATION) ?
156 IR_MAX_DURATION : rawir.duration;
157 sz_push(sz, rawir);
160 static void sz_push_half_pulse(struct streamzap_ir *sz,
161 unsigned char value)
163 sz_push_full_pulse(sz, (value & SZ_PULSE_MASK) >> 4);
166 static void sz_push_full_space(struct streamzap_ir *sz,
167 unsigned char value)
169 struct ir_raw_event rawir = {};
171 rawir.pulse = false;
172 rawir.duration = ((int) value) * SZ_RESOLUTION;
173 rawir.duration += SZ_RESOLUTION / 2;
174 sz->sum += rawir.duration;
175 rawir.duration = US_TO_NS(rawir.duration);
176 sz_push(sz, rawir);
179 static void sz_push_half_space(struct streamzap_ir *sz,
180 unsigned long value)
182 sz_push_full_space(sz, value & SZ_SPACE_MASK);
186 * streamzap_callback - usb IRQ handler callback
188 * This procedure is invoked on reception of data from
189 * the usb remote.
191 static void streamzap_callback(struct urb *urb)
193 struct streamzap_ir *sz;
194 unsigned int i;
195 int len;
197 if (!urb)
198 return;
200 sz = urb->context;
201 len = urb->actual_length;
203 switch (urb->status) {
204 case -ECONNRESET:
205 case -ENOENT:
206 case -ESHUTDOWN:
208 * this urb is terminated, clean up.
209 * sz might already be invalid at this point
211 dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
212 return;
213 default:
214 break;
217 dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
218 for (i = 0; i < len; i++) {
219 dev_dbg(sz->dev, "sz->buf_in[%d]: %x\n",
220 i, (unsigned char)sz->buf_in[i]);
221 switch (sz->decoder_state) {
222 case PulseSpace:
223 if ((sz->buf_in[i] & SZ_PULSE_MASK) ==
224 SZ_PULSE_MASK) {
225 sz->decoder_state = FullPulse;
226 continue;
227 } else if ((sz->buf_in[i] & SZ_SPACE_MASK)
228 == SZ_SPACE_MASK) {
229 sz_push_half_pulse(sz, sz->buf_in[i]);
230 sz->decoder_state = FullSpace;
231 continue;
232 } else {
233 sz_push_half_pulse(sz, sz->buf_in[i]);
234 sz_push_half_space(sz, sz->buf_in[i]);
236 break;
237 case FullPulse:
238 sz_push_full_pulse(sz, sz->buf_in[i]);
239 sz->decoder_state = IgnorePulse;
240 break;
241 case FullSpace:
242 if (sz->buf_in[i] == SZ_TIMEOUT) {
243 struct ir_raw_event rawir = {
244 .pulse = false,
245 .duration = sz->rdev->timeout
247 sz->idle = true;
248 if (sz->timeout_enabled)
249 sz_push(sz, rawir);
250 ir_raw_event_handle(sz->rdev);
251 ir_raw_event_reset(sz->rdev);
252 } else {
253 sz_push_full_space(sz, sz->buf_in[i]);
255 sz->decoder_state = PulseSpace;
256 break;
257 case IgnorePulse:
258 if ((sz->buf_in[i] & SZ_SPACE_MASK) ==
259 SZ_SPACE_MASK) {
260 sz->decoder_state = FullSpace;
261 continue;
263 sz_push_half_space(sz, sz->buf_in[i]);
264 sz->decoder_state = PulseSpace;
265 break;
269 ir_raw_event_handle(sz->rdev);
270 usb_submit_urb(urb, GFP_ATOMIC);
272 return;
275 static struct rc_dev *streamzap_init_rc_dev(struct streamzap_ir *sz)
277 struct rc_dev *rdev;
278 struct device *dev = sz->dev;
279 int ret;
281 rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
282 if (!rdev) {
283 dev_err(dev, "remote dev allocation failed\n");
284 goto out;
287 snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared Receiver (%04x:%04x)",
288 le16_to_cpu(sz->usbdev->descriptor.idVendor),
289 le16_to_cpu(sz->usbdev->descriptor.idProduct));
290 usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
291 strlcat(sz->phys, "/input0", sizeof(sz->phys));
293 rdev->device_name = sz->name;
294 rdev->input_phys = sz->phys;
295 usb_to_input_id(sz->usbdev, &rdev->input_id);
296 rdev->dev.parent = dev;
297 rdev->priv = sz;
298 rdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
299 rdev->driver_name = DRIVER_NAME;
300 rdev->map_name = RC_MAP_STREAMZAP;
302 ret = rc_register_device(rdev);
303 if (ret < 0) {
304 dev_err(dev, "remote input device register failed\n");
305 goto out;
308 return rdev;
310 out:
311 rc_free_device(rdev);
312 return NULL;
316 * streamzap_probe
318 * Called by usb-core to associated with a candidate device
319 * On any failure the return value is the ERROR
320 * On success return 0
322 static int streamzap_probe(struct usb_interface *intf,
323 const struct usb_device_id *id)
325 struct usb_device *usbdev = interface_to_usbdev(intf);
326 struct usb_host_interface *iface_host;
327 struct streamzap_ir *sz = NULL;
328 char buf[63], name[128] = "";
329 int retval = -ENOMEM;
330 int pipe, maxp;
332 /* Allocate space for device driver specific data */
333 sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
334 if (!sz)
335 return -ENOMEM;
337 sz->usbdev = usbdev;
338 sz->interface = intf;
340 /* Check to ensure endpoint information matches requirements */
341 iface_host = intf->cur_altsetting;
343 if (iface_host->desc.bNumEndpoints != 1) {
344 dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
345 __func__, iface_host->desc.bNumEndpoints);
346 retval = -ENODEV;
347 goto free_sz;
350 sz->endpoint = &(iface_host->endpoint[0].desc);
351 if (!usb_endpoint_dir_in(sz->endpoint)) {
352 dev_err(&intf->dev, "%s: endpoint doesn't match input device 02%02x\n",
353 __func__, sz->endpoint->bEndpointAddress);
354 retval = -ENODEV;
355 goto free_sz;
358 if (!usb_endpoint_xfer_int(sz->endpoint)) {
359 dev_err(&intf->dev, "%s: endpoint attributes don't match xfer 02%02x\n",
360 __func__, sz->endpoint->bmAttributes);
361 retval = -ENODEV;
362 goto free_sz;
365 pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress);
366 maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));
368 if (maxp == 0) {
369 dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
370 __func__);
371 retval = -ENODEV;
372 goto free_sz;
375 /* Allocate the USB buffer and IRQ URB */
376 sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
377 if (!sz->buf_in)
378 goto free_sz;
380 sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
381 if (!sz->urb_in)
382 goto free_buf_in;
384 sz->dev = &intf->dev;
385 sz->buf_in_len = maxp;
387 if (usbdev->descriptor.iManufacturer
388 && usb_string(usbdev, usbdev->descriptor.iManufacturer,
389 buf, sizeof(buf)) > 0)
390 strscpy(name, buf, sizeof(name));
392 if (usbdev->descriptor.iProduct
393 && usb_string(usbdev, usbdev->descriptor.iProduct,
394 buf, sizeof(buf)) > 0)
395 snprintf(name + strlen(name), sizeof(name) - strlen(name),
396 " %s", buf);
398 sz->rdev = streamzap_init_rc_dev(sz);
399 if (!sz->rdev)
400 goto rc_dev_fail;
402 sz->idle = true;
403 sz->decoder_state = PulseSpace;
404 /* FIXME: don't yet have a way to set this */
405 sz->timeout_enabled = true;
406 sz->rdev->timeout = ((US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION) &
407 IR_MAX_DURATION) | 0x03000000);
408 #if 0
409 /* not yet supported, depends on patches from maxim */
410 /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
411 sz->min_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION);
412 sz->max_timeout = US_TO_NS(SZ_TIMEOUT * SZ_RESOLUTION);
413 #endif
415 sz->signal_start = ktime_get_real();
417 /* Complete final initialisations */
418 usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
419 maxp, (usb_complete_t)streamzap_callback,
420 sz, sz->endpoint->bInterval);
421 sz->urb_in->transfer_dma = sz->dma_in;
422 sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
424 usb_set_intfdata(intf, sz);
426 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
427 dev_err(sz->dev, "urb submit failed\n");
429 dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
430 usbdev->bus->busnum, usbdev->devnum);
432 return 0;
434 rc_dev_fail:
435 usb_free_urb(sz->urb_in);
436 free_buf_in:
437 usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
438 free_sz:
439 kfree(sz);
441 return retval;
445 * streamzap_disconnect
447 * Called by the usb core when the device is removed from the system.
449 * This routine guarantees that the driver will not submit any more urbs
450 * by clearing dev->usbdev. It is also supposed to terminate any currently
451 * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
452 * does not provide any way to do this.
454 static void streamzap_disconnect(struct usb_interface *interface)
456 struct streamzap_ir *sz = usb_get_intfdata(interface);
457 struct usb_device *usbdev = interface_to_usbdev(interface);
459 usb_set_intfdata(interface, NULL);
461 if (!sz)
462 return;
464 sz->usbdev = NULL;
465 rc_unregister_device(sz->rdev);
466 usb_kill_urb(sz->urb_in);
467 usb_free_urb(sz->urb_in);
468 usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
470 kfree(sz);
473 static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
475 struct streamzap_ir *sz = usb_get_intfdata(intf);
477 usb_kill_urb(sz->urb_in);
479 return 0;
482 static int streamzap_resume(struct usb_interface *intf)
484 struct streamzap_ir *sz = usb_get_intfdata(intf);
486 if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
487 dev_err(sz->dev, "Error submitting urb\n");
488 return -EIO;
491 return 0;
494 module_usb_driver(streamzap_driver);
496 MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
497 MODULE_DESCRIPTION(DRIVER_DESC);
498 MODULE_LICENSE("GPL");