xref: /linux/drivers/media/rc/streamzap.c (revision 42844992664f03ef9f930e64f7370fa481e9c267)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Streamzap Remote Control driver
4  *
5  * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de>
6  * Copyright (c) 2010 Jarod Wilson <jarod@wilsonet.com>
7  *
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.
14  *
15  * Ported to in-kernel ir-core interface by Jarod Wilson
16  *
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)
19  */
20 
21 #include <linux/device.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/usb.h>
25 #include <linux/usb/input.h>
26 #include <media/rc-core.h>
27 
28 #define DRIVER_NAME	"streamzap"
29 #define DRIVER_DESC	"Streamzap Remote Control driver"
30 
31 #define USB_STREAMZAP_VENDOR_ID		0x0e9c
32 #define USB_STREAMZAP_PRODUCT_ID	0x0000
33 
34 /* table of devices that work with this driver */
35 static const struct usb_device_id streamzap_table[] = {
36 	/* Streamzap Remote Control */
37 	{ USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
38 	/* Terminating entry */
39 	{ }
40 };
41 
42 MODULE_DEVICE_TABLE(usb, streamzap_table);
43 
44 #define SZ_PULSE_MASK 0xf0
45 #define SZ_SPACE_MASK 0x0f
46 #define SZ_TIMEOUT    0xff
47 #define SZ_RESOLUTION 256
48 
49 /* number of samples buffered */
50 #define SZ_BUF_LEN 128
51 
52 enum StreamzapDecoderState {
53 	PulseSpace,
54 	FullPulse,
55 	FullSpace,
56 	IgnorePulse
57 };
58 
59 /* structure to hold our device specific stuff */
60 struct streamzap_ir {
61 	/* ir-core */
62 	struct rc_dev *rdev;
63 
64 	/* core device info */
65 	struct device *dev;
66 
67 	/* usb */
68 	struct urb		*urb_in;
69 
70 	/* buffer & dma */
71 	unsigned char		*buf_in;
72 	dma_addr_t		dma_in;
73 	unsigned int		buf_in_len;
74 
75 	/* track what state we're in */
76 	enum StreamzapDecoderState decoder_state;
77 
78 	char			phys[64];
79 };
80 
81 
82 /* local function prototypes */
83 static int streamzap_probe(struct usb_interface *interface,
84 			   const struct usb_device_id *id);
85 static void streamzap_disconnect(struct usb_interface *interface);
86 static void streamzap_callback(struct urb *urb);
87 static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
88 static int streamzap_resume(struct usb_interface *intf);
89 
90 /* usb specific object needed to register this driver with the usb subsystem */
91 static struct usb_driver streamzap_driver = {
92 	.name =		DRIVER_NAME,
93 	.probe =	streamzap_probe,
94 	.disconnect =	streamzap_disconnect,
95 	.suspend =	streamzap_suspend,
96 	.resume =	streamzap_resume,
97 	.id_table =	streamzap_table,
98 };
99 
100 static void sz_push(struct streamzap_ir *sz, struct ir_raw_event rawir)
101 {
102 	dev_dbg(sz->dev, "Storing %s with duration %u us\n",
103 		(rawir.pulse ? "pulse" : "space"), rawir.duration);
104 	ir_raw_event_store_with_filter(sz->rdev, &rawir);
105 }
106 
107 static void sz_push_full_pulse(struct streamzap_ir *sz,
108 			       unsigned char value)
109 {
110 	struct ir_raw_event rawir = {
111 		.pulse = true,
112 		.duration = value * SZ_RESOLUTION + SZ_RESOLUTION / 2,
113 	};
114 
115 	sz_push(sz, rawir);
116 }
117 
118 static void sz_push_half_pulse(struct streamzap_ir *sz,
119 			       unsigned char value)
120 {
121 	sz_push_full_pulse(sz, (value & SZ_PULSE_MASK) >> 4);
122 }
123 
124 static void sz_push_full_space(struct streamzap_ir *sz,
125 			       unsigned char value)
126 {
127 	struct ir_raw_event rawir = {
128 		.pulse = false,
129 		.duration = value * SZ_RESOLUTION + SZ_RESOLUTION / 2,
130 	};
131 
132 	sz_push(sz, rawir);
133 }
134 
135 static void sz_push_half_space(struct streamzap_ir *sz,
136 			       unsigned long value)
137 {
138 	sz_push_full_space(sz, value & SZ_SPACE_MASK);
139 }
140 
141 static void sz_process_ir_data(struct streamzap_ir *sz, int len)
142 {
143 	unsigned int i;
144 
145 	for (i = 0; i < len; i++) {
146 		dev_dbg(sz->dev, "sz->buf_in[%d]: %x\n",
147 			i, (unsigned char)sz->buf_in[i]);
148 		switch (sz->decoder_state) {
149 		case PulseSpace:
150 			if ((sz->buf_in[i] & SZ_PULSE_MASK) ==
151 				SZ_PULSE_MASK) {
152 				sz->decoder_state = FullPulse;
153 				continue;
154 			} else if ((sz->buf_in[i] & SZ_SPACE_MASK)
155 					== SZ_SPACE_MASK) {
156 				sz_push_half_pulse(sz, sz->buf_in[i]);
157 				sz->decoder_state = FullSpace;
158 				continue;
159 			} else {
160 				sz_push_half_pulse(sz, sz->buf_in[i]);
161 				sz_push_half_space(sz, sz->buf_in[i]);
162 			}
163 			break;
164 		case FullPulse:
165 			sz_push_full_pulse(sz, sz->buf_in[i]);
166 			sz->decoder_state = IgnorePulse;
167 			break;
168 		case FullSpace:
169 			if (sz->buf_in[i] == SZ_TIMEOUT) {
170 				struct ir_raw_event rawir = {
171 					.pulse = false,
172 					.duration = sz->rdev->timeout
173 				};
174 				sz_push(sz, rawir);
175 			} else {
176 				sz_push_full_space(sz, sz->buf_in[i]);
177 			}
178 			sz->decoder_state = PulseSpace;
179 			break;
180 		case IgnorePulse:
181 			if ((sz->buf_in[i] & SZ_SPACE_MASK) ==
182 				SZ_SPACE_MASK) {
183 				sz->decoder_state = FullSpace;
184 				continue;
185 			}
186 			sz_push_half_space(sz, sz->buf_in[i]);
187 			sz->decoder_state = PulseSpace;
188 			break;
189 		}
190 	}
191 
192 	ir_raw_event_handle(sz->rdev);
193 }
194 
195 /*
196  * streamzap_callback - usb IRQ handler callback
197  *
198  * This procedure is invoked on reception of data from
199  * the usb remote.
200  */
201 static void streamzap_callback(struct urb *urb)
202 {
203 	struct streamzap_ir *sz;
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 0:
214 		dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
215 		sz_process_ir_data(sz, len);
216 		break;
217 	case -ECONNRESET:
218 	case -ENOENT:
219 	case -ESHUTDOWN:
220 		/*
221 		 * this urb is terminated, clean up.
222 		 */
223 		dev_dbg(sz->dev, "urb terminated, status: %d\n", urb->status);
224 		return;
225 	default:
226 		break;
227 	}
228 
229 	usb_submit_urb(urb, GFP_ATOMIC);
230 }
231 
232 static struct rc_dev *streamzap_init_rc_dev(struct streamzap_ir *sz,
233 					    struct usb_device *usbdev)
234 {
235 	struct rc_dev *rdev;
236 	struct device *dev = sz->dev;
237 	int ret;
238 
239 	rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
240 	if (!rdev)
241 		goto out;
242 
243 	usb_make_path(usbdev, sz->phys, sizeof(sz->phys));
244 	strlcat(sz->phys, "/input0", sizeof(sz->phys));
245 
246 	rdev->device_name = "Streamzap PC Remote Infrared Receiver";
247 	rdev->input_phys = sz->phys;
248 	usb_to_input_id(usbdev, &rdev->input_id);
249 	rdev->dev.parent = dev;
250 	rdev->priv = sz;
251 	rdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
252 	rdev->driver_name = DRIVER_NAME;
253 	rdev->map_name = RC_MAP_STREAMZAP;
254 	rdev->rx_resolution = SZ_RESOLUTION;
255 
256 	ret = rc_register_device(rdev);
257 	if (ret < 0) {
258 		dev_err(dev, "remote input device register failed\n");
259 		goto out;
260 	}
261 
262 	return rdev;
263 
264 out:
265 	rc_free_device(rdev);
266 	return NULL;
267 }
268 
269 /*
270  *	streamzap_probe
271  *
272  *	Called by usb-core to associated with a candidate device
273  *	On any failure the return value is the ERROR
274  *	On success return 0
275  */
276 static int streamzap_probe(struct usb_interface *intf,
277 			   const struct usb_device_id *id)
278 {
279 	struct usb_device *usbdev = interface_to_usbdev(intf);
280 	struct usb_endpoint_descriptor *endpoint;
281 	struct usb_host_interface *iface_host;
282 	struct streamzap_ir *sz = NULL;
283 	int retval = -ENOMEM;
284 	int pipe, maxp;
285 
286 	/* Allocate space for device driver specific data */
287 	sz = kzalloc_obj(struct streamzap_ir);
288 	if (!sz)
289 		return -ENOMEM;
290 
291 	/* Check to ensure endpoint information matches requirements */
292 	iface_host = intf->cur_altsetting;
293 
294 	if (iface_host->desc.bNumEndpoints != 1) {
295 		dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
296 			__func__, iface_host->desc.bNumEndpoints);
297 		retval = -ENODEV;
298 		goto free_sz;
299 	}
300 
301 	endpoint = &iface_host->endpoint[0].desc;
302 	if (!usb_endpoint_dir_in(endpoint)) {
303 		dev_err(&intf->dev, "%s: endpoint doesn't match input device 02%02x\n",
304 			__func__, endpoint->bEndpointAddress);
305 		retval = -ENODEV;
306 		goto free_sz;
307 	}
308 
309 	if (!usb_endpoint_xfer_int(endpoint)) {
310 		dev_err(&intf->dev, "%s: endpoint attributes don't match xfer 02%02x\n",
311 			__func__, endpoint->bmAttributes);
312 		retval = -ENODEV;
313 		goto free_sz;
314 	}
315 
316 	pipe = usb_rcvintpipe(usbdev, endpoint->bEndpointAddress);
317 	maxp = usb_maxpacket(usbdev, pipe);
318 
319 	if (maxp == 0) {
320 		dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
321 			__func__);
322 		retval = -ENODEV;
323 		goto free_sz;
324 	}
325 
326 	/* Allocate the USB buffer and IRQ URB */
327 	sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
328 	if (!sz->buf_in)
329 		goto free_sz;
330 
331 	sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
332 	if (!sz->urb_in)
333 		goto free_buf_in;
334 
335 	sz->dev = &intf->dev;
336 	sz->buf_in_len = maxp;
337 
338 	sz->rdev = streamzap_init_rc_dev(sz, usbdev);
339 	if (!sz->rdev)
340 		goto rc_dev_fail;
341 
342 	sz->decoder_state = PulseSpace;
343 	/* FIXME: don't yet have a way to set this */
344 	sz->rdev->timeout = SZ_TIMEOUT * SZ_RESOLUTION;
345 	#if 0
346 	/* not yet supported, depends on patches from maxim */
347 	/* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
348 	sz->min_timeout = SZ_TIMEOUT * SZ_RESOLUTION;
349 	sz->max_timeout = SZ_TIMEOUT * SZ_RESOLUTION;
350 	#endif
351 
352 	/* Complete final initialisations */
353 	usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
354 			 maxp, streamzap_callback, sz, endpoint->bInterval);
355 	sz->urb_in->transfer_dma = sz->dma_in;
356 	sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
357 
358 	usb_set_intfdata(intf, sz);
359 
360 	retval = usb_submit_urb(sz->urb_in, GFP_ATOMIC);
361 	if (retval < 0) {
362 		dev_err(sz->dev, "urb submit failed\n");
363 		goto rc_submit_fail;
364 	}
365 
366 	return 0;
367 rc_submit_fail:
368 	rc_free_device(sz->rdev);
369 	usb_set_intfdata(intf, NULL);
370 rc_dev_fail:
371 	usb_free_urb(sz->urb_in);
372 free_buf_in:
373 	usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
374 free_sz:
375 	kfree(sz);
376 
377 	return retval;
378 }
379 
380 /*
381  * streamzap_disconnect
382  *
383  * Called by the usb core when the device is removed from the system.
384  *
385  * This routine guarantees that the driver will not submit any more urbs
386  * by clearing dev->usbdev.  It is also supposed to terminate any currently
387  * active urbs.  Unfortunately, usb_bulk_msg(), used in streamzap_read(),
388  * does not provide any way to do this.
389  */
390 static void streamzap_disconnect(struct usb_interface *interface)
391 {
392 	struct streamzap_ir *sz = usb_get_intfdata(interface);
393 	struct usb_device *usbdev = interface_to_usbdev(interface);
394 
395 	if (!sz)
396 		return;
397 
398 	rc_unregister_device(sz->rdev);
399 	usb_set_intfdata(interface, NULL);
400 
401 	usb_kill_urb(sz->urb_in);
402 	usb_free_urb(sz->urb_in);
403 	usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
404 	rc_free_device(sz->rdev);
405 
406 	kfree(sz);
407 }
408 
409 static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
410 {
411 	struct streamzap_ir *sz = usb_get_intfdata(intf);
412 
413 	usb_kill_urb(sz->urb_in);
414 
415 	return 0;
416 }
417 
418 static int streamzap_resume(struct usb_interface *intf)
419 {
420 	struct streamzap_ir *sz = usb_get_intfdata(intf);
421 
422 	if (usb_submit_urb(sz->urb_in, GFP_NOIO)) {
423 		dev_err(sz->dev, "Error submitting urb\n");
424 		return -EIO;
425 	}
426 
427 	return 0;
428 }
429 
430 module_usb_driver(streamzap_driver);
431 
432 MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
433 MODULE_DESCRIPTION(DRIVER_DESC);
434 MODULE_LICENSE("GPL");
435