streamzap.c 14.1 KB
Newer Older
1 2 3 4
/*
 * Streamzap Remote Control driver
 *
 * Copyright (c) 2005 Christoph Bartelmus <lirc@bartelmus.de>
5
 * Copyright (c) 2010 Jarod Wilson <jarod@wilsonet.com>
6 7 8 9 10 11 12 13
 *
 * This driver was based on the work of Greg Wickham and Adrian
 * Dewhurst. It was substantially rewritten to support correct signal
 * gaps and now maintains a delay buffer, which is used to present
 * consistent timing behaviour to user space applications. Without the
 * delay buffer an ugly hack would be required in lircd, which can
 * cause sluggish signal decoding in certain situations.
 *
14 15
 * Ported to in-kernel ir-core interface by Jarod Wilson
 *
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 * This driver is based on the USB skeleton driver packaged with the
 * kernel; copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  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 38
#include <linux/usb.h>
#include <linux/usb/input.h>
39
#include <media/rc-core.h>
40

41
#define DRIVER_VERSION	"1.61"
42
#define DRIVER_NAME	"streamzap"
43 44
#define DRIVER_DESC	"Streamzap Remote Control driver"

45 46 47
#ifdef CONFIG_USB_DEBUG
static int debug = 1;
#else
48
static int debug;
49
#endif
50 51 52 53 54 55 56 57 58 59 60 61 62 63

#define USB_STREAMZAP_VENDOR_ID		0x0e9c
#define USB_STREAMZAP_PRODUCT_ID	0x0000

/* table of devices that work with this driver */
static struct usb_device_id streamzap_table[] = {
	/* Streamzap Remote Control */
	{ USB_DEVICE(USB_STREAMZAP_VENDOR_ID, USB_STREAMZAP_PRODUCT_ID) },
	/* Terminating entry */
	{ }
};

MODULE_DEVICE_TABLE(usb, streamzap_table);

64 65 66 67
#define SZ_PULSE_MASK 0xf0
#define SZ_SPACE_MASK 0x0f
#define SZ_TIMEOUT    0xff
#define SZ_RESOLUTION 256
68 69

/* number of samples buffered */
70
#define SZ_BUF_LEN 128
71

72 73 74 75
/* from ir-rc5-sz-decoder.c */
#ifdef CONFIG_IR_RC5_SZ_DECODER_MODULE
#define load_rc5_sz_decode()    request_module("ir-rc5-sz-decoder")
#else
76
#define load_rc5_sz_decode()    {}
77 78
#endif

79 80 81 82 83 84 85
enum StreamzapDecoderState {
	PulseSpace,
	FullPulse,
	FullSpace,
	IgnorePulse
};

86 87 88
/* structure to hold our device specific stuff */
struct streamzap_ir {
	/* ir-core */
89
	struct rc_dev *rdev;
90 91 92

	/* core device info */
	struct device *dev;
93 94

	/* usb */
95
	struct usb_device	*usbdev;
96
	struct usb_interface	*interface;
97 98
	struct usb_endpoint_descriptor *endpoint;
	struct urb		*urb_in;
99 100 101 102 103 104

	/* buffer & dma */
	unsigned char		*buf_in;
	dma_addr_t		dma_in;
	unsigned int		buf_in_len;

105 106
	/* track what state we're in */
	enum StreamzapDecoderState decoder_state;
107
	/* tracks whether we are currently receiving some signal */
108
	bool			idle;
109 110 111 112 113
	/* sum of signal lengths received since signal start */
	unsigned long		sum;
	/* start time of signal; necessary for gap tracking */
	struct timeval		signal_last;
	struct timeval		signal_start;
114
	bool			timeout_enabled;
115 116 117

	char			name[128];
	char			phys[64];
118 119 120 121 122 123 124
};


/* local function prototypes */
static int streamzap_probe(struct usb_interface *interface,
			   const struct usb_device_id *id);
static void streamzap_disconnect(struct usb_interface *interface);
125
static void streamzap_callback(struct urb *urb);
126 127 128 129 130 131 132 133 134 135 136 137 138
static int streamzap_suspend(struct usb_interface *intf, pm_message_t message);
static int streamzap_resume(struct usb_interface *intf);

/* usb specific object needed to register this driver with the usb subsystem */
static struct usb_driver streamzap_driver = {
	.name =		DRIVER_NAME,
	.probe =	streamzap_probe,
	.disconnect =	streamzap_disconnect,
	.suspend =	streamzap_suspend,
	.resume =	streamzap_resume,
	.id_table =	streamzap_table,
};

139
static void sz_push(struct streamzap_ir *sz, struct ir_raw_event rawir)
140
{
141 142
	dev_dbg(sz->dev, "Storing %s with duration %u us\n",
		(rawir.pulse ? "pulse" : "space"), rawir.duration);
143
	ir_raw_event_store_with_filter(sz->rdev, &rawir);
144 145
}

146 147
static void sz_push_full_pulse(struct streamzap_ir *sz,
			       unsigned char value)
148
{
149
	DEFINE_IR_RAW_EVENT(rawir);
150

151 152 153 154 155 156
	if (sz->idle) {
		long deltv;

		sz->signal_last = sz->signal_start;
		do_gettimeofday(&sz->signal_start);

157
		deltv = sz->signal_start.tv_sec - sz->signal_last.tv_sec;
158
		rawir.pulse = false;
159 160
		if (deltv > 15) {
			/* really long time */
161
			rawir.duration = IR_MAX_DURATION;
162
		} else {
163
			rawir.duration = (int)(deltv * 1000000 +
164 165
				sz->signal_start.tv_usec -
				sz->signal_last.tv_usec);
166 167 168
			rawir.duration -= sz->sum;
			rawir.duration *= 1000;
			rawir.duration &= IR_MAX_DURATION;
169
		}
170
		sz_push(sz, rawir);
171

172
		sz->idle = false;
173 174 175
		sz->sum = 0;
	}

176
	rawir.pulse = true;
177 178
	rawir.duration = ((int) value) * SZ_RESOLUTION;
	rawir.duration += SZ_RESOLUTION / 2;
179 180 181 182
	sz->sum += rawir.duration;
	rawir.duration *= 1000;
	rawir.duration &= IR_MAX_DURATION;
	sz_push(sz, rawir);
183 184
}

185 186
static void sz_push_half_pulse(struct streamzap_ir *sz,
			       unsigned char value)
187
{
188
	sz_push_full_pulse(sz, (value & SZ_PULSE_MASK) >> 4);
189 190
}

191 192
static void sz_push_full_space(struct streamzap_ir *sz,
			       unsigned char value)
193
{
194
	DEFINE_IR_RAW_EVENT(rawir);
195 196

	rawir.pulse = false;
197 198
	rawir.duration = ((int) value) * SZ_RESOLUTION;
	rawir.duration += SZ_RESOLUTION / 2;
199 200 201
	sz->sum += rawir.duration;
	rawir.duration *= 1000;
	sz_push(sz, rawir);
202 203
}

204 205
static void sz_push_half_space(struct streamzap_ir *sz,
			       unsigned long value)
206
{
207
	sz_push_full_space(sz, value & SZ_SPACE_MASK);
208 209 210
}

/**
211
 * streamzap_callback - usb IRQ handler callback
212 213 214 215
 *
 * This procedure is invoked on reception of data from
 * the usb remote.
 */
216
static void streamzap_callback(struct urb *urb)
217
{
218 219 220
	struct streamzap_ir *sz;
	unsigned int i;
	int len;
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

	if (!urb)
		return;

	sz = urb->context;
	len = urb->actual_length;

	switch (urb->status) {
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/*
		 * this urb is terminated, clean up.
		 * sz might already be invalid at this point
		 */
236
		dev_err(sz->dev, "urb terminated, status: %d\n", urb->status);
237 238 239 240 241
		return;
	default:
		break;
	}

242
	dev_dbg(sz->dev, "%s: received urb, len %d\n", __func__, len);
243
	for (i = 0; i < len; i++) {
244
		dev_dbg(sz->dev, "sz->buf_in[%d]: %x\n",
245 246 247
			i, (unsigned char)sz->buf_in[i]);
		switch (sz->decoder_state) {
		case PulseSpace:
248 249
			if ((sz->buf_in[i] & SZ_PULSE_MASK) ==
				SZ_PULSE_MASK) {
250 251
				sz->decoder_state = FullPulse;
				continue;
252 253
			} else if ((sz->buf_in[i] & SZ_SPACE_MASK)
					== SZ_SPACE_MASK) {
254 255 256 257 258
				sz_push_half_pulse(sz, sz->buf_in[i]);
				sz->decoder_state = FullSpace;
				continue;
			} else {
				sz_push_half_pulse(sz, sz->buf_in[i]);
259
				sz_push_half_space(sz, sz->buf_in[i]);
260
			}
261 262 263 264 265 266
			break;
		case FullPulse:
			sz_push_full_pulse(sz, sz->buf_in[i]);
			sz->decoder_state = IgnorePulse;
			break;
		case FullSpace:
267
			if (sz->buf_in[i] == SZ_TIMEOUT) {
268
				DEFINE_IR_RAW_EVENT(rawir);
269 270

				rawir.pulse = false;
271
				rawir.duration = sz->rdev->timeout;
272 273 274
				sz->idle = true;
				if (sz->timeout_enabled)
					sz_push(sz, rawir);
275
				ir_raw_event_handle(sz->rdev);
276 277 278 279 280 281
			} else {
				sz_push_full_space(sz, sz->buf_in[i]);
			}
			sz->decoder_state = PulseSpace;
			break;
		case IgnorePulse:
282 283
			if ((sz->buf_in[i] & SZ_SPACE_MASK) ==
				SZ_SPACE_MASK) {
284 285 286 287 288 289
				sz->decoder_state = FullSpace;
				continue;
			}
			sz_push_half_space(sz, sz->buf_in[i]);
			sz->decoder_state = PulseSpace;
			break;
290 291 292 293 294 295 296 297
		}
	}

	usb_submit_urb(urb, GFP_ATOMIC);

	return;
}

298
static struct rc_dev *streamzap_init_rc_dev(struct streamzap_ir *sz)
299
{
300
	struct rc_dev *rdev;
301 302 303
	struct device *dev = sz->dev;
	int ret;

304 305 306 307
	rdev = rc_allocate_device();
	if (!rdev) {
		dev_err(dev, "remote dev allocation failed\n");
		goto out;
308 309 310 311 312 313 314 315 316
	}

	snprintf(sz->name, sizeof(sz->name), "Streamzap PC Remote Infrared "
		 "Receiver (%04x:%04x)",
		 le16_to_cpu(sz->usbdev->descriptor.idVendor),
		 le16_to_cpu(sz->usbdev->descriptor.idProduct));
	usb_make_path(sz->usbdev, sz->phys, sizeof(sz->phys));
	strlcat(sz->phys, "/input0", sizeof(sz->phys));

317 318
	rdev->input_name = sz->name;
	rdev->input_phys = sz->phys;
319 320
	usb_to_input_id(sz->usbdev, &rdev->input_id);
	rdev->dev.parent = dev;
321 322
	rdev->priv = sz;
	rdev->driver_type = RC_DRIVER_IR_RAW;
323
	rdev->allowed_protos = RC_TYPE_ALL;
324 325
	rdev->driver_name = DRIVER_NAME;
	rdev->map_name = RC_MAP_STREAMZAP;
326

327
	ret = rc_register_device(rdev);
328 329
	if (ret < 0) {
		dev_err(dev, "remote input device register failed\n");
330
		goto out;
331 332
	}

333
	return rdev;
334

335 336
out:
	rc_free_device(rdev);
337 338 339
	return NULL;
}

340 341 342 343 344 345 346
/**
 *	streamzap_probe
 *
 *	Called by usb-core to associated with a candidate device
 *	On any failure the return value is the ERROR
 *	On success return 0
 */
347 348
static int __devinit streamzap_probe(struct usb_interface *intf,
				     const struct usb_device_id *id)
349
{
350
	struct usb_device *usbdev = interface_to_usbdev(intf);
351
	struct usb_host_interface *iface_host;
352
	struct streamzap_ir *sz = NULL;
353 354
	char buf[63], name[128] = "";
	int retval = -ENOMEM;
355
	int pipe, maxp;
356 357

	/* Allocate space for device driver specific data */
358 359
	sz = kzalloc(sizeof(struct streamzap_ir), GFP_KERNEL);
	if (!sz)
360 361
		return -ENOMEM;

362 363
	sz->usbdev = usbdev;
	sz->interface = intf;
364 365

	/* Check to ensure endpoint information matches requirements */
366
	iface_host = intf->cur_altsetting;
367 368

	if (iface_host->desc.bNumEndpoints != 1) {
369 370
		dev_err(&intf->dev, "%s: Unexpected desc.bNumEndpoints (%d)\n",
			__func__, iface_host->desc.bNumEndpoints);
371 372 373 374 375 376 377
		retval = -ENODEV;
		goto free_sz;
	}

	sz->endpoint = &(iface_host->endpoint[0].desc);
	if ((sz->endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
	    != USB_DIR_IN) {
378 379
		dev_err(&intf->dev, "%s: endpoint doesn't match input device "
			"02%02x\n", __func__, sz->endpoint->bEndpointAddress);
380 381 382 383 384 385
		retval = -ENODEV;
		goto free_sz;
	}

	if ((sz->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
	    != USB_ENDPOINT_XFER_INT) {
386 387
		dev_err(&intf->dev, "%s: endpoint attributes don't match xfer "
			"02%02x\n", __func__, sz->endpoint->bmAttributes);
388 389 390 391
		retval = -ENODEV;
		goto free_sz;
	}

392 393 394 395 396 397
	pipe = usb_rcvintpipe(usbdev, sz->endpoint->bEndpointAddress);
	maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));

	if (maxp == 0) {
		dev_err(&intf->dev, "%s: endpoint Max Packet Size is 0!?!\n",
			__func__);
398 399 400 401 402
		retval = -ENODEV;
		goto free_sz;
	}

	/* Allocate the USB buffer and IRQ URB */
403 404
	sz->buf_in = usb_alloc_coherent(usbdev, maxp, GFP_ATOMIC, &sz->dma_in);
	if (!sz->buf_in)
405 406 407
		goto free_sz;

	sz->urb_in = usb_alloc_urb(0, GFP_KERNEL);
408 409
	if (!sz->urb_in)
		goto free_buf_in;
410

411 412
	sz->dev = &intf->dev;
	sz->buf_in_len = maxp;
413

414 415
	if (usbdev->descriptor.iManufacturer
	    && usb_string(usbdev, usbdev->descriptor.iManufacturer,
416 417 418
			  buf, sizeof(buf)) > 0)
		strlcpy(name, buf, sizeof(name));

419 420
	if (usbdev->descriptor.iProduct
	    && usb_string(usbdev, usbdev->descriptor.iProduct,
421 422 423 424
			  buf, sizeof(buf)) > 0)
		snprintf(name + strlen(name), sizeof(name) - strlen(name),
			 " %s", buf);

425 426 427
	sz->rdev = streamzap_init_rc_dev(sz);
	if (!sz->rdev)
		goto rc_dev_fail;
428

429 430
	sz->idle = true;
	sz->decoder_state = PulseSpace;
431 432
	/* FIXME: don't yet have a way to set this */
	sz->timeout_enabled = true;
433
	sz->rdev->timeout = (((SZ_TIMEOUT * SZ_RESOLUTION * 1000) &
434
				IR_MAX_DURATION) | 0x03000000);
435 436 437
	#if 0
	/* not yet supported, depends on patches from maxim */
	/* see also: LIRC_GET_REC_RESOLUTION and LIRC_SET_REC_TIMEOUT */
438 439
	sz->min_timeout = SZ_TIMEOUT * SZ_RESOLUTION * 1000;
	sz->max_timeout = SZ_TIMEOUT * SZ_RESOLUTION * 1000;
440
	#endif
441

442
	do_gettimeofday(&sz->signal_start);
443

444 445 446 447 448 449
	/* Complete final initialisations */
	usb_fill_int_urb(sz->urb_in, usbdev, pipe, sz->buf_in,
			 maxp, (usb_complete_t)streamzap_callback,
			 sz, sz->endpoint->bInterval);
	sz->urb_in->transfer_dma = sz->dma_in;
	sz->urb_in->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
450

451
	usb_set_intfdata(intf, sz);
452

453 454
	if (usb_submit_urb(sz->urb_in, GFP_ATOMIC))
		dev_err(sz->dev, "urb submit failed\n");
455

456 457
	dev_info(sz->dev, "Registered %s on usb%d:%d\n", name,
		 usbdev->bus->busnum, usbdev->devnum);
458

459 460 461
	/* Load the streamzap not-quite-rc5 decoder too */
	load_rc5_sz_decode();

462
	return 0;
463

464
rc_dev_fail:
465 466 467 468 469
	usb_free_urb(sz->urb_in);
free_buf_in:
	usb_free_coherent(usbdev, maxp, sz->buf_in, sz->dma_in);
free_sz:
	kfree(sz);
470

471
	return retval;
472 473 474 475 476 477 478 479
}

/**
 * streamzap_disconnect
 *
 * Called by the usb core when the device is removed from the system.
 *
 * This routine guarantees that the driver will not submit any more urbs
480
 * by clearing dev->usbdev.  It is also supposed to terminate any currently
481 482 483 484 485
 * active urbs.  Unfortunately, usb_bulk_msg(), used in streamzap_read(),
 * does not provide any way to do this.
 */
static void streamzap_disconnect(struct usb_interface *interface)
{
486 487
	struct streamzap_ir *sz = usb_get_intfdata(interface);
	struct usb_device *usbdev = interface_to_usbdev(interface);
488

489
	usb_set_intfdata(interface, NULL);
490

491 492
	if (!sz)
		return;
493

494
	sz->usbdev = NULL;
495
	rc_unregister_device(sz->rdev);
496
	usb_kill_urb(sz->urb_in);
497
	usb_free_urb(sz->urb_in);
498
	usb_free_coherent(usbdev, sz->buf_in_len, sz->buf_in, sz->dma_in);
499 500 501 502 503 504

	kfree(sz);
}

static int streamzap_suspend(struct usb_interface *intf, pm_message_t message)
{
505
	struct streamzap_ir *sz = usb_get_intfdata(intf);
506

507
	usb_kill_urb(sz->urb_in);
508 509 510 511 512 513

	return 0;
}

static int streamzap_resume(struct usb_interface *intf)
{
514
	struct streamzap_ir *sz = usb_get_intfdata(intf);
515

516 517 518
	if (usb_submit_urb(sz->urb_in, GFP_ATOMIC)) {
		dev_err(sz->dev, "Error sumbiting urb\n");
		return -EIO;
519
	}
520

521 522 523 524
	return 0;
}

/**
525
 *	streamzap_init
526
 */
527
static int __init streamzap_init(void)
528
{
529
	int ret;
530 531

	/* register this driver with the USB subsystem */
532 533 534 535
	ret = usb_register(&streamzap_driver);
	if (ret < 0)
		printk(KERN_ERR DRIVER_NAME ": usb register failed, "
		       "result = %d\n", ret);
536

537
	return ret;
538 539 540
}

/**
541
 *	streamzap_exit
542
 */
543
static void __exit streamzap_exit(void)
544 545 546 547 548
{
	usb_deregister(&streamzap_driver);
}


549 550
module_init(streamzap_init);
module_exit(streamzap_exit);
551

552
MODULE_AUTHOR("Jarod Wilson <jarod@wilsonet.com>");
553 554 555 556 557
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");

module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Enable debugging messages");