streamzap.c 14.0 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 76 77 78
/* 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
#define load_rc5_sz_decode()    0
#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 319 320 321 322 323
	rdev->input_name = sz->name;
	rdev->input_phys = sz->phys;
	rdev->priv = sz;
	rdev->driver_type = RC_DRIVER_IR_RAW;
	rdev->allowed_protos = IR_TYPE_ALL;
	rdev->driver_name = DRIVER_NAME;
	rdev->map_name = RC_MAP_STREAMZAP;
324

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

331
	return rdev;
332

333 334
out:
	rc_free_device(rdev);
335 336 337
	return NULL;
}

338 339 340 341 342 343 344
/**
 *	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
 */
345 346
static int __devinit streamzap_probe(struct usb_interface *intf,
				     const struct usb_device_id *id)
347
{
348
	struct usb_device *usbdev = interface_to_usbdev(intf);
349
	struct usb_host_interface *iface_host;
350
	struct streamzap_ir *sz = NULL;
351 352
	char buf[63], name[128] = "";
	int retval = -ENOMEM;
353
	int pipe, maxp;
354 355

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

360 361
	sz->usbdev = usbdev;
	sz->interface = intf;
362 363

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

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

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

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

390 391 392 393 394 395
	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__);
396 397 398 399 400
		retval = -ENODEV;
		goto free_sz;
	}

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

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

409 410
	sz->dev = &intf->dev;
	sz->buf_in_len = maxp;
411

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

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

423 424 425
	sz->rdev = streamzap_init_rc_dev(sz);
	if (!sz->rdev)
		goto rc_dev_fail;
426

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

440
	do_gettimeofday(&sz->signal_start);
441

442 443 444 445 446 447
	/* 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;
448

449
	usb_set_intfdata(intf, sz);
450

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

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

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

460
	return 0;
461

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

469
	return retval;
470 471 472 473 474 475 476 477
}

/**
 * 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
478
 * by clearing dev->usbdev.  It is also supposed to terminate any currently
479 480 481 482 483
 * 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)
{
484 485
	struct streamzap_ir *sz = usb_get_intfdata(interface);
	struct usb_device *usbdev = interface_to_usbdev(interface);
486

487
	usb_set_intfdata(interface, NULL);
488

489 490
	if (!sz)
		return;
491

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

	kfree(sz);
}

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

505
	usb_kill_urb(sz->urb_in);
506 507 508 509 510 511

	return 0;
}

static int streamzap_resume(struct usb_interface *intf)
{
512
	struct streamzap_ir *sz = usb_get_intfdata(intf);
513

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

519 520 521 522
	return 0;
}

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

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

535
	return ret;
536 537 538
}

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


547 548
module_init(streamzap_init);
module_exit(streamzap_exit);
549

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

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