2 * Streamzap Remote Control driver
4 * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de>
5 * Copyright (c) 2010 Jarod Wilson <jarod@wilsonet.com>
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.
14 * Ported to in-kernel ir-core interface by Jarod Wilson
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)
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.
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.
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
34 #include <linux/device.h>
35 #include <linux/module.h>
36 #include <linux/slab.h>
37 #include <linux/ktime.h>
38 #include <linux/usb.h>
39 #include <linux/usb/input.h>
40 #include <media/rc-core.h>
42 #define DRIVER_VERSION "1.61"
43 #define DRIVER_NAME "streamzap"
44 #define DRIVER_DESC "Streamzap Remote Control driver"
46 #define USB_STREAMZAP_VENDOR_ID 0x0e9c
47 #define USB_STREAMZAP_PRODUCT_ID 0x0000
49 /* table of devices that work with this driver */
50 static struct usb_device_id streamzap_table
[] = {
51 /* Streamzap Remote Control */
52 { USB_DEVICE(USB_STREAMZAP_VENDOR_ID
, USB_STREAMZAP_PRODUCT_ID
) },
53 /* Terminating entry */
57 MODULE_DEVICE_TABLE(usb
, streamzap_table
);
59 #define SZ_PULSE_MASK 0xf0
60 #define SZ_SPACE_MASK 0x0f
61 #define SZ_TIMEOUT 0xff
62 #define SZ_RESOLUTION 256
64 /* number of samples buffered */
65 #define SZ_BUF_LEN 128
67 enum StreamzapDecoderState
{
74 /* structure to hold our device specific stuff */
79 /* core device info */
83 struct usb_device
*usbdev
;
84 struct usb_interface
*interface
;
85 struct usb_endpoint_descriptor
*endpoint
;
89 unsigned char *buf_in
;
91 unsigned int buf_in_len
;
93 /* track what state we're in */
94 enum StreamzapDecoderState decoder_state
;
95 /* tracks whether we are currently receiving some signal */
97 /* sum of signal lengths received since signal start */
99 /* start time of signal; necessary for gap tracking */
101 ktime_t signal_start
;
102 bool timeout_enabled
;
109 /* local function prototypes */
110 static int streamzap_probe(struct usb_interface
*interface
,
111 const struct usb_device_id
*id
);
112 static void streamzap_disconnect(struct usb_interface
*interface
);
113 static void streamzap_callback(struct urb
*urb
);
114 static int streamzap_suspend(struct usb_interface
*intf
, pm_message_t message
);
115 static int streamzap_resume(struct usb_interface
*intf
);
117 /* usb specific object needed to register this driver with the usb subsystem */
118 static struct usb_driver streamzap_driver
= {
120 .probe
= streamzap_probe
,
121 .disconnect
= streamzap_disconnect
,
122 .suspend
= streamzap_suspend
,
123 .resume
= streamzap_resume
,
124 .id_table
= streamzap_table
,
127 static void sz_push(struct streamzap_ir
*sz
, struct ir_raw_event rawir
)
129 dev_dbg(sz
->dev
, "Storing %s with duration %u us\n",
130 (rawir
.pulse
? "pulse" : "space"), rawir
.duration
);
131 ir_raw_event_store_with_filter(sz
->rdev
, &rawir
);
134 static void sz_push_full_pulse(struct streamzap_ir
*sz
,
137 DEFINE_IR_RAW_EVENT(rawir
);
142 sz
->signal_last
= sz
->signal_start
;
143 sz
->signal_start
= ktime_get_real();
145 delta
= ktime_us_delta(sz
->signal_start
, sz
->signal_last
);
147 if (delta
> (15 * USEC_PER_SEC
)) {
148 /* really long time */
149 rawir
.duration
= IR_MAX_DURATION
;
151 rawir
.duration
= delta
;
152 rawir
.duration
-= sz
->sum
;
153 rawir
.duration
= US_TO_NS(rawir
.duration
);
154 rawir
.duration
= (rawir
.duration
> IR_MAX_DURATION
) ?
155 IR_MAX_DURATION
: rawir
.duration
;
164 rawir
.duration
= ((int) value
) * SZ_RESOLUTION
;
165 rawir
.duration
+= SZ_RESOLUTION
/ 2;
166 sz
->sum
+= rawir
.duration
;
167 rawir
.duration
= US_TO_NS(rawir
.duration
);
168 rawir
.duration
= (rawir
.duration
> IR_MAX_DURATION
) ?
169 IR_MAX_DURATION
: rawir
.duration
;
173 static void sz_push_half_pulse(struct streamzap_ir
*sz
,
176 sz_push_full_pulse(sz
, (value
& SZ_PULSE_MASK
) >> 4);
179 static void sz_push_full_space(struct streamzap_ir
*sz
,
182 DEFINE_IR_RAW_EVENT(rawir
);
185 rawir
.duration
= ((int) value
) * SZ_RESOLUTION
;
186 rawir
.duration
+= SZ_RESOLUTION
/ 2;
187 sz
->sum
+= rawir
.duration
;
188 rawir
.duration
= US_TO_NS(rawir
.duration
);
192 static void sz_push_half_space(struct streamzap_ir
*sz
,
195 sz_push_full_space(sz
, value
& SZ_SPACE_MASK
);
199 * streamzap_callback - usb IRQ handler callback
201 * This procedure is invoked on reception of data from
204 static void streamzap_callback(struct urb
*urb
)
206 struct streamzap_ir
*sz
;
214 len
= urb
->actual_length
;
216 switch (urb
->status
) {
221 * this urb is terminated, clean up.
222 * sz might already be invalid at this point
224 dev_err(sz
->dev
, "urb terminated, status: %d\n", urb
->status
);
230 dev_dbg(sz
->dev
, "%s: received urb, len %d\n", __func__
, len
);
231 for (i
= 0; i
< len
; i
++) {
232 dev_dbg(sz
->dev
, "sz->buf_in[%d]: %x\n",
233 i
, (unsigned char)sz
->buf_in
[i
]);
234 switch (sz
->decoder_state
) {
236 if ((sz
->buf_in
[i
] & SZ_PULSE_MASK
) ==
238 sz
->decoder_state
= FullPulse
;
240 } else if ((sz
->buf_in
[i
] & SZ_SPACE_MASK
)
242 sz_push_half_pulse(sz
, sz
->buf_in
[i
]);
243 sz
->decoder_state
= FullSpace
;
246 sz_push_half_pulse(sz
, sz
->buf_in
[i
]);
247 sz_push_half_space(sz
, sz
->buf_in
[i
]);
251 sz_push_full_pulse(sz
, sz
->buf_in
[i
]);
252 sz
->decoder_state
= IgnorePulse
;
255 if (sz
->buf_in
[i
] == SZ_TIMEOUT
) {
256 DEFINE_IR_RAW_EVENT(rawir
);
259 rawir
.duration
= sz
->rdev
->timeout
;
261 if (sz
->timeout_enabled
)
263 ir_raw_event_handle(sz
->rdev
);
264 ir_raw_event_reset(sz
->rdev
);
266 sz_push_full_space(sz
, sz
->buf_in
[i
]);
268 sz
->decoder_state
= PulseSpace
;
271 if ((sz
->buf_in
[i
] & SZ_SPACE_MASK
) ==
273 sz
->decoder_state
= FullSpace
;
276 sz_push_half_space(sz
, sz
->buf_in
[i
]);
277 sz
->decoder_state
= PulseSpace
;
282 ir_raw_event_handle(sz
->rdev
);
283 usb_submit_urb(urb
, GFP_ATOMIC
);
288 static struct rc_dev
*streamzap_init_rc_dev(struct streamzap_ir
*sz
)
291 struct device
*dev
= sz
->dev
;
294 rdev
= rc_allocate_device();
296 dev_err(dev
, "remote dev allocation failed\n");
300 snprintf(sz
->name
, sizeof(sz
->name
), "Streamzap PC Remote Infrared Receiver (%04x:%04x)",
301 le16_to_cpu(sz
->usbdev
->descriptor
.idVendor
),
302 le16_to_cpu(sz
->usbdev
->descriptor
.idProduct
));
303 usb_make_path(sz
->usbdev
, sz
->phys
, sizeof(sz
->phys
));
304 strlcat(sz
->phys
, "/input0", sizeof(sz
->phys
));
306 rdev
->input_name
= sz
->name
;
307 rdev
->input_phys
= sz
->phys
;
308 usb_to_input_id(sz
->usbdev
, &rdev
->input_id
);
309 rdev
->dev
.parent
= dev
;
311 rdev
->driver_type
= RC_DRIVER_IR_RAW
;
312 rdev
->allowed_protocols
= RC_BIT_ALL
;
313 rdev
->driver_name
= DRIVER_NAME
;
314 rdev
->map_name
= RC_MAP_STREAMZAP
;
316 ret
= rc_register_device(rdev
);
318 dev_err(dev
, "remote input device register failed\n");
325 rc_free_device(rdev
);
332 * Called by usb-core to associated with a candidate device
333 * On any failure the return value is the ERROR
334 * On success return 0
336 static int streamzap_probe(struct usb_interface
*intf
,
337 const struct usb_device_id
*id
)
339 struct usb_device
*usbdev
= interface_to_usbdev(intf
);
340 struct usb_host_interface
*iface_host
;
341 struct streamzap_ir
*sz
= NULL
;
342 char buf
[63], name
[128] = "";
343 int retval
= -ENOMEM
;
346 /* Allocate space for device driver specific data */
347 sz
= kzalloc(sizeof(struct streamzap_ir
), GFP_KERNEL
);
352 sz
->interface
= intf
;
354 /* Check to ensure endpoint information matches requirements */
355 iface_host
= intf
->cur_altsetting
;
357 if (iface_host
->desc
.bNumEndpoints
!= 1) {
358 dev_err(&intf
->dev
, "%s: Unexpected desc.bNumEndpoints (%d)\n",
359 __func__
, iface_host
->desc
.bNumEndpoints
);
364 sz
->endpoint
= &(iface_host
->endpoint
[0].desc
);
365 if (!usb_endpoint_dir_in(sz
->endpoint
)) {
366 dev_err(&intf
->dev
, "%s: endpoint doesn't match input device 02%02x\n",
367 __func__
, sz
->endpoint
->bEndpointAddress
);
372 if (!usb_endpoint_xfer_int(sz
->endpoint
)) {
373 dev_err(&intf
->dev
, "%s: endpoint attributes don't match xfer 02%02x\n",
374 __func__
, sz
->endpoint
->bmAttributes
);
379 pipe
= usb_rcvintpipe(usbdev
, sz
->endpoint
->bEndpointAddress
);
380 maxp
= usb_maxpacket(usbdev
, pipe
, usb_pipeout(pipe
));
383 dev_err(&intf
->dev
, "%s: endpoint Max Packet Size is 0!?!\n",
389 /* Allocate the USB buffer and IRQ URB */
390 sz
->buf_in
= usb_alloc_coherent(usbdev
, maxp
, GFP_ATOMIC
, &sz
->dma_in
);
394 sz
->urb_in
= usb_alloc_urb(0, GFP_KERNEL
);
398 sz
->dev
= &intf
->dev
;
399 sz
->buf_in_len
= maxp
;
401 if (usbdev
->descriptor
.iManufacturer
402 && usb_string(usbdev
, usbdev
->descriptor
.iManufacturer
,
403 buf
, sizeof(buf
)) > 0)
404 strlcpy(name
, buf
, sizeof(name
));
406 if (usbdev
->descriptor
.iProduct
407 && usb_string(usbdev
, usbdev
->descriptor
.iProduct
,
408 buf
, sizeof(buf
)) > 0)
409 snprintf(name
+ strlen(name
), sizeof(name
) - strlen(name
),
412 sz
->rdev
= streamzap_init_rc_dev(sz
);
417 sz
->decoder_state
= PulseSpace
;
418 /* FIXME: don't yet have a way to set this */
419 sz
->timeout_enabled
= true;
420 sz
->rdev
->timeout
= ((US_TO_NS(SZ_TIMEOUT
* SZ_RESOLUTION
) &
421 IR_MAX_DURATION
) | 0x03000000);
423 /* not yet supported, depends on patches from maxim */
424 /* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
425 sz
->min_timeout
= US_TO_NS(SZ_TIMEOUT
* SZ_RESOLUTION
);
426 sz
->max_timeout
= US_TO_NS(SZ_TIMEOUT
* SZ_RESOLUTION
);
429 sz
->signal_start
= ktime_get_real();
431 /* Complete final initialisations */
432 usb_fill_int_urb(sz
->urb_in
, usbdev
, pipe
, sz
->buf_in
,
433 maxp
, (usb_complete_t
)streamzap_callback
,
434 sz
, sz
->endpoint
->bInterval
);
435 sz
->urb_in
->transfer_dma
= sz
->dma_in
;
436 sz
->urb_in
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
438 usb_set_intfdata(intf
, sz
);
440 if (usb_submit_urb(sz
->urb_in
, GFP_ATOMIC
))
441 dev_err(sz
->dev
, "urb submit failed\n");
443 dev_info(sz
->dev
, "Registered %s on usb%d:%d\n", name
,
444 usbdev
->bus
->busnum
, usbdev
->devnum
);
449 usb_free_urb(sz
->urb_in
);
451 usb_free_coherent(usbdev
, maxp
, sz
->buf_in
, sz
->dma_in
);
459 * streamzap_disconnect
461 * Called by the usb core when the device is removed from the system.
463 * This routine guarantees that the driver will not submit any more urbs
464 * by clearing dev->usbdev. It is also supposed to terminate any currently
465 * active urbs. Unfortunately, usb_bulk_msg(), used in streamzap_read(),
466 * does not provide any way to do this.
468 static void streamzap_disconnect(struct usb_interface
*interface
)
470 struct streamzap_ir
*sz
= usb_get_intfdata(interface
);
471 struct usb_device
*usbdev
= interface_to_usbdev(interface
);
473 usb_set_intfdata(interface
, NULL
);
479 rc_unregister_device(sz
->rdev
);
480 usb_kill_urb(sz
->urb_in
);
481 usb_free_urb(sz
->urb_in
);
482 usb_free_coherent(usbdev
, sz
->buf_in_len
, sz
->buf_in
, sz
->dma_in
);
487 static int streamzap_suspend(struct usb_interface
*intf
, pm_message_t message
)
489 struct streamzap_ir
*sz
= usb_get_intfdata(intf
);
491 usb_kill_urb(sz
->urb_in
);
496 static int streamzap_resume(struct usb_interface
*intf
)
498 struct streamzap_ir
*sz
= usb_get_intfdata(intf
);
500 if (usb_submit_urb(sz
->urb_in
, GFP_ATOMIC
)) {
501 dev_err(sz
->dev
, "Error submitting urb\n");
508 module_usb_driver(streamzap_driver
);
510 MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
511 MODULE_DESCRIPTION(DRIVER_DESC
);
512 MODULE_LICENSE("GPL");