bttv-input.c 13.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *
 * Copyright (c) 2003 Gerd Knorr
 * Copyright (c) 2003 Pavel Machek
 *
 * 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
 */

21 22
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
23 24 25 26 27
#include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/input.h>
28
#include <linux/slab.h>
L
Linus Torvalds 已提交
29 30

#include "bttv.h"
31
#include "bttvp.h"
L
Linus Torvalds 已提交
32

33

34 35
static int ir_debug;
module_param(ir_debug, int, 0644);
L
Linus Torvalds 已提交
36

37
static int ir_rc5_remote_gap = 885;
38 39
module_param(ir_rc5_remote_gap, int, 0644);

40
#undef dprintk
41 42 43 44
#define dprintk(fmt, ...)			\
do {						\
	if (ir_debug >= 1)			\
		pr_info(fmt, ##__VA_ARGS__);	\
45 46
} while (0)

47
#define DEVNAME "bttv-input"
L
Linus Torvalds 已提交
48

49 50
#define MODULE_NAME "bttv"

L
Linus Torvalds 已提交
51 52
/* ---------------------------------------------------------------------- */

53
static void ir_handle_key(struct bttv *btv)
L
Linus Torvalds 已提交
54
{
55
	struct bttv_ir *ir = btv->remote;
L
Linus Torvalds 已提交
56 57 58
	u32 gpio,data;

	/* read gpio value */
59
	gpio = bttv_gpio_read(&btv->c);
L
Linus Torvalds 已提交
60 61 62 63 64 65 66 67
	if (ir->polling) {
		if (ir->last_gpio == gpio)
			return;
		ir->last_gpio = gpio;
	}

	/* extract data */
	data = ir_extract_bits(gpio, ir->mask_keycode);
68
	dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
L
Linus Torvalds 已提交
69 70 71 72 73
		gpio, data,
		ir->polling               ? "poll"  : "irq",
		(gpio & ir->mask_keydown) ? " down" : "",
		(gpio & ir->mask_keyup)   ? " up"   : "");

74 75
	if ((ir->mask_keydown && (gpio & ir->mask_keydown)) ||
	    (ir->mask_keyup   && !(gpio & ir->mask_keyup))) {
76
		rc_keydown_notimeout(ir->dev, data, 0);
L
Linus Torvalds 已提交
77
	} else {
78 79 80
		/* HACK: Probably, ir->mask_keydown is missing
		   for this board */
		if (btv->c.type == BTTV_BOARD_WINFAST2000)
81
			rc_keydown_notimeout(ir->dev, data, 0);
82

83
		rc_keyup(ir->dev);
L
Linus Torvalds 已提交
84 85 86
	}
}

87 88
static void ir_enltv_handle_key(struct bttv *btv)
{
89
	struct bttv_ir *ir = btv->remote;
90 91 92 93 94 95 96 97 98 99 100 101
	u32 gpio, data, keyup;

	/* read gpio value */
	gpio = bttv_gpio_read(&btv->c);

	/* extract data */
	data = ir_extract_bits(gpio, ir->mask_keycode);

	/* Check if it is keyup */
	keyup = (gpio & ir->mask_keyup) ? 1 << 31 : 0;

	if ((ir->last_gpio & 0x7f) != data) {
102
		dprintk("gpio=0x%x code=%d | %s\n",
103 104 105
			gpio, data,
			(gpio & ir->mask_keyup) ? " up" : "up/down");

106
		rc_keydown_notimeout(ir->dev, data, 0);
107
		if (keyup)
108
			rc_keyup(ir->dev);
109 110 111 112
	} else {
		if ((ir->last_gpio & 1 << 31) == keyup)
			return;

113
		dprintk("(cnt) gpio=0x%x code=%d | %s\n",
114 115 116 117
			gpio, data,
			(gpio & ir->mask_keyup) ? " up" : "down");

		if (keyup)
118
			rc_keyup(ir->dev);
119
		else
120
			rc_keydown_notimeout(ir->dev, data, 0);
121 122 123 124 125
	}

	ir->last_gpio = data | keyup;
}

126 127
static int bttv_rc5_irq(struct bttv *btv);

128
void bttv_input_irq(struct bttv *btv)
L
Linus Torvalds 已提交
129
{
130
	struct bttv_ir *ir = btv->remote;
L
Linus Torvalds 已提交
131

132 133 134
	if (ir->rc5_gpio)
		bttv_rc5_irq(btv);
	else if (!ir->polling)
135
		ir_handle_key(btv);
L
Linus Torvalds 已提交
136 137
}

138
static void bttv_input_timer(unsigned long data)
L
Linus Torvalds 已提交
139
{
140
	struct bttv *btv = (struct bttv*)data;
141
	struct bttv_ir *ir = btv->remote;
L
Linus Torvalds 已提交
142

143 144 145 146
	if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
		ir_enltv_handle_key(btv);
	else
		ir_handle_key(btv);
147
	mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
L
Linus Torvalds 已提交
148 149
}

150 151 152 153 154 155 156
/*
 * FIXME: Nebula digi uses the legacy way to decode RC5, instead of relying
 * on the rc-core way. As we need to be sure that both IRQ transitions are
 * properly triggered, Better to touch it only with this hardware for
 * testing.
 */

157 158 159 160 161
#define RC5_START(x)	(((x) >> 12) & 3)
#define RC5_TOGGLE(x)	(((x) >> 11) & 1)
#define RC5_ADDR(x)	(((x) >> 6) & 31)
#define RC5_INSTR(x)	((x) & 63)

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
/* decode raw bit pattern to RC5 code */
static u32 bttv_rc5_decode(unsigned int code)
{
	unsigned int org_code = code;
	unsigned int pair;
	unsigned int rc5 = 0;
	int i;

	for (i = 0; i < 14; ++i) {
		pair = code & 0x3;
		code >>= 2;

		rc5 <<= 1;
		switch (pair) {
		case 0:
		case 2:
			break;
		case 1:
			rc5 |= 1;
		break;
		case 3:
183
			dprintk("rc5_decode(%x) bad code\n",
184 185 186 187
				org_code);
			return 0;
		}
	}
188
	dprintk("code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
189 190 191 192 193
		"instr=%x\n", rc5, org_code, RC5_START(rc5),
		RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
	return rc5;
}

194
static void bttv_rc5_timer_end(unsigned long data)
195
{
196
	struct bttv_ir *ir = (struct bttv_ir *)data;
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	struct timeval tv;
	u32 gap;
	u32 rc5 = 0;

	/* get time */
	do_gettimeofday(&tv);

	/* avoid overflow with gap >1s */
	if (tv.tv_sec - ir->base_time.tv_sec > 1) {
		gap = 200000;
	} else {
		gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
		    tv.tv_usec - ir->base_time.tv_usec;
	}

	/* signal we're ready to start a new code */
213
	ir->active = false;
214 215 216

	/* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
	if (gap < 28000) {
217
		dprintk("spurious timer_end\n");
218 219 220 221 222
		return;
	}

	if (ir->last_bit < 20) {
		/* ignore spurious codes (caused by light/other remotes) */
223
		dprintk("short code: %x\n", ir->code);
224 225 226 227 228 229
	} else {
		ir->code = (ir->code << ir->shift_by) | 1;
		rc5 = bttv_rc5_decode(ir->code);

		/* two start bits? */
		if (RC5_START(rc5) != ir->start) {
230
			pr_info(DEVNAME ":"
231 232 233 234 235 236 237 238
			       " rc5 start bits invalid: %u\n", RC5_START(rc5));

			/* right address? */
		} else if (RC5_ADDR(rc5) == ir->addr) {
			u32 toggle = RC5_TOGGLE(rc5);
			u32 instr = RC5_INSTR(rc5);

			/* Good code */
239
			rc_keydown(ir->dev, instr, toggle);
240
			dprintk("instruction %x, toggle %x\n",
241 242 243 244
				instr, toggle);
		}
	}
}
245

246
static int bttv_rc5_irq(struct bttv *btv)
247
{
248
	struct bttv_ir *ir = btv->remote;
249 250 251
	struct timeval tv;
	u32 gpio;
	u32 gap;
252
	unsigned long current_jiffies;
253 254

	/* read gpio port */
255
	gpio = bttv_gpio_read(&btv->c);
256 257 258 259 260 261 262 263 264 265 266 267 268

	/* get time of bit */
	current_jiffies = jiffies;
	do_gettimeofday(&tv);

	/* avoid overflow with gap >1s */
	if (tv.tv_sec - ir->base_time.tv_sec > 1) {
		gap = 200000;
	} else {
		gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
		    tv.tv_usec - ir->base_time.tv_usec;
	}

269
	dprintk("RC5 IRQ: gap %d us for %s\n",
270 271 272 273 274 275
		gap, (gpio & 0x20) ? "mark" : "space");

	/* remote IRQ? */
	if (!(gpio & 0x20))
		return 0;

276 277 278 279 280
	/* active code => add bit */
	if (ir->active) {
		/* only if in the code (otherwise spurious IRQ or timer
		   late) */
		if (ir->last_bit < 28) {
281 282
			ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
			    ir_rc5_remote_gap;
283 284 285 286
			ir->code |= 1 << ir->last_bit;
		}
		/* starting new code */
	} else {
287
		ir->active = true;
288 289 290 291
		ir->code = 0;
		ir->base_time = tv;
		ir->last_bit = 0;

292
		mod_timer(&ir->timer, current_jiffies + msecs_to_jiffies(30));
293 294 295
	}

	/* toggle GPIO pin 4 to reset the irq */
296 297
	bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
	bttv_gpio_write(&btv->c, gpio | (1 << 4));
298 299 300
	return 1;
}

L
Linus Torvalds 已提交
301 302
/* ---------------------------------------------------------------------- */

303
static void bttv_ir_start(struct bttv *btv, struct bttv_ir *ir)
304 305
{
	if (ir->polling) {
306
		setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
307
		ir->timer.expires  = jiffies + msecs_to_jiffies(1000);
308 309 310
		add_timer(&ir->timer);
	} else if (ir->rc5_gpio) {
		/* set timer_end for code completion */
311
		setup_timer(&ir->timer, bttv_rc5_timer_end, (unsigned long)ir);
312 313 314 315
		ir->shift_by = 1;
		ir->start = 3;
		ir->addr = 0x0;
		ir->rc5_remote_gap = ir_rc5_remote_gap;
316 317 318 319 320
	}
}

static void bttv_ir_stop(struct bttv *btv)
{
321
	if (btv->remote->polling)
322 323 324 325 326
		del_timer_sync(&btv->remote->timer);

	if (btv->remote->rc5_gpio) {
		u32 gpio;

327
		del_timer_sync(&btv->remote->timer);
328 329 330 331 332 333

		gpio = bttv_gpio_read(&btv->c);
		bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
	}
}

334 335 336 337 338 339 340 341 342 343
/*
 * Get_key functions used by I2C remotes
 */

static int get_key_pv951(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
{
	unsigned char b;

	/* poll IR chip */
	if (1 != i2c_master_recv(ir->c, &b, 1)) {
344
		dprintk("read error\n");
345 346 347 348 349 350
		return -EIO;
	}

	/* ignore 0xaa */
	if (b==0xaa)
		return 0;
351
	dprintk("key %02x\n", b);
352

353 354 355 356 357 358 359 360 361 362 363 364
	/*
	 * NOTE:
	 * lirc_i2c maps the pv951 code as:
	 *	addr = 0x61D6
	 * 	cmd = bit_reverse (b)
	 * So, it seems that this device uses NEC extended
	 * I decided to not fix the table, due to two reasons:
	 * 	1) Without the actual device, this is only a guess;
	 * 	2) As the addr is not reported via I2C, nor can be changed,
	 * 	   the device is bound to the vendor-provided RC.
	 */

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
	*ir_key = b;
	*ir_raw = b;
	return 1;
}

/* Instantiate the I2C IR receiver device, if present */
void __devinit init_bttv_i2c_ir(struct bttv *btv)
{
	const unsigned short addr_list[] = {
		0x1a, 0x18, 0x64, 0x30, 0x71,
		I2C_CLIENT_END
	};
	struct i2c_board_info info;

	if (0 != btv->i2c_rc)
		return;

	memset(&info, 0, sizeof(struct i2c_board_info));
	memset(&btv->init_data, 0, sizeof(btv->init_data));
	strlcpy(info.type, "ir_video", I2C_NAME_SIZE);

	switch (btv->c.type) {
	case BTTV_BOARD_PV951:
		btv->init_data.name = "PV951";
		btv->init_data.get_key = get_key_pv951;
		btv->init_data.ir_codes = RC_MAP_PV951;
		info.addr = 0x4b;
		break;
	default:
		/*
		 * The external IR receiver is at i2c address 0x34 (0x35 for
396 397 398 399
		 * reads).  Future Hauppauge cards will have an internal
		 * receiver at 0x30 (0x31 for reads).  In theory, both can be
		 * fitted, and Hauppauge suggest an external overrides an
		 * internal.
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
		 * That's why we probe 0x1a (~0x34) first. CB
		 */

		i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list, NULL);
		return;
	}

	if (btv->init_data.name)
		info.platform_data = &btv->init_data;
	i2c_new_device(&btv->c.i2c_adap, &info);

	return;
}

int __devexit fini_bttv_i2c(struct bttv *btv)
{
	if (0 != btv->i2c_rc)
		return 0;

	return i2c_del_adapter(&btv->c.i2c_adap);
}

422
int bttv_input_init(struct bttv *btv)
L
Linus Torvalds 已提交
423
{
424
	struct bttv_ir *ir;
425
	char *ir_codes = NULL;
426
	struct rc_dev *rc;
427
	int err = -ENOMEM;
L
Linus Torvalds 已提交
428

429 430 431 432
	if (!btv->has_remote)
		return -ENODEV;

	ir = kzalloc(sizeof(*ir),GFP_KERNEL);
433 434
	rc = rc_allocate_device();
	if (!ir || !rc)
435
		goto err_out_free;
L
Linus Torvalds 已提交
436 437

	/* detect & configure */
438
	switch (btv->c.type) {
439 440 441
	case BTTV_BOARD_AVERMEDIA:
	case BTTV_BOARD_AVPHONE98:
	case BTTV_BOARD_AVERMEDIA98:
442
		ir_codes         = RC_MAP_AVERMEDIA;
L
Linus Torvalds 已提交
443 444 445 446 447
		ir->mask_keycode = 0xf88000;
		ir->mask_keydown = 0x010000;
		ir->polling      = 50; // ms
		break;

448 449
	case BTTV_BOARD_AVDVBT_761:
	case BTTV_BOARD_AVDVBT_771:
450
		ir_codes         = RC_MAP_AVERMEDIA_DVBT;
L
Linus Torvalds 已提交
451 452 453 454 455
		ir->mask_keycode = 0x0f00c0;
		ir->mask_keydown = 0x000020;
		ir->polling      = 50; // ms
		break;

456
	case BTTV_BOARD_PXELVWPLTVPAK:
457
		ir_codes         = RC_MAP_PIXELVIEW;
L
Linus Torvalds 已提交
458 459 460
		ir->mask_keycode = 0x003e00;
		ir->mask_keyup   = 0x010000;
		ir->polling      = 50; // ms
461
		break;
462
	case BTTV_BOARD_PV_M4900:
463 464
	case BTTV_BOARD_PV_BT878P_9B:
	case BTTV_BOARD_PV_BT878P_PLUS:
465
		ir_codes         = RC_MAP_PIXELVIEW;
L
Linus Torvalds 已提交
466 467 468
		ir->mask_keycode = 0x001f00;
		ir->mask_keyup   = 0x008000;
		ir->polling      = 50; // ms
469
		break;
L
Linus Torvalds 已提交
470

471
	case BTTV_BOARD_WINFAST2000:
472
		ir_codes         = RC_MAP_WINFAST;
L
Linus Torvalds 已提交
473 474
		ir->mask_keycode = 0x1f8;
		break;
475 476
	case BTTV_BOARD_MAGICTVIEW061:
	case BTTV_BOARD_MAGICTVIEW063:
477
		ir_codes         = RC_MAP_WINFAST;
L
Linus Torvalds 已提交
478 479 480
		ir->mask_keycode = 0x0008e000;
		ir->mask_keydown = 0x00200000;
		break;
481
	case BTTV_BOARD_APAC_VIEWCOMP:
482
		ir_codes         = RC_MAP_APAC_VIEWCOMP;
L
Linus Torvalds 已提交
483 484 485 486
		ir->mask_keycode = 0x001f00;
		ir->mask_keyup   = 0x008000;
		ir->polling      = 50; // ms
		break;
487
	case BTTV_BOARD_ASKEY_CPH03X:
488
	case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
489
	case BTTV_BOARD_CONTVFMI:
490
		ir_codes         = RC_MAP_PIXELVIEW;
491 492 493 494
		ir->mask_keycode = 0x001F00;
		ir->mask_keyup   = 0x006000;
		ir->polling      = 50; // ms
		break;
495
	case BTTV_BOARD_NEBULA_DIGITV:
496
		ir_codes = RC_MAP_NEBULA;
497
		ir->rc5_gpio = true;
498
		break;
J
 
Julian Calaby 已提交
499
	case BTTV_BOARD_MACHTV_MAGICTV:
500
		ir_codes         = RC_MAP_APAC_VIEWCOMP;
J
 
Julian Calaby 已提交
501 502 503 504
		ir->mask_keycode = 0x001F00;
		ir->mask_keyup   = 0x004000;
		ir->polling      = 50; /* ms */
		break;
505
	case BTTV_BOARD_KOZUMI_KTV_01C:
506
		ir_codes         = RC_MAP_PCTV_SEDNA;
507 508 509 510
		ir->mask_keycode = 0x001f00;
		ir->mask_keyup   = 0x006000;
		ir->polling      = 50; /* ms */
		break;
511
	case BTTV_BOARD_ENLTV_FM_2:
512
		ir_codes         = RC_MAP_ENCORE_ENLTV2;
513 514 515 516 517 518
		ir->mask_keycode = 0x00fd00;
		ir->mask_keyup   = 0x000080;
		ir->polling      = 1; /* ms */
		ir->last_gpio    = ir_extract_bits(bttv_gpio_read(&btv->c),
						   ir->mask_keycode);
		break;
L
Linus Torvalds 已提交
519 520
	}
	if (NULL == ir_codes) {
521
		dprintk("Ooops: IR config error [card=%d]\n", btv->c.type);
522 523
		err = -ENODEV;
		goto err_out_free;
L
Linus Torvalds 已提交
524 525
	}

526 527
	if (ir->rc5_gpio) {
		u32 gpio;
528
		/* enable remote irq */
529 530 531 532
		bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
		gpio = bttv_gpio_read(&btv->c);
		bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
		bttv_gpio_write(&btv->c, gpio | (1 << 4));
533 534
	} else {
		/* init hardware-specific stuff */
535
		bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
536
	}
L
Linus Torvalds 已提交
537 538

	/* init input device */
539
	ir->dev = rc;
540

L
Linus Torvalds 已提交
541
	snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
542
		 btv->c.type);
L
Linus Torvalds 已提交
543
	snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
544
		 pci_name(btv->c.pci));
L
Linus Torvalds 已提交
545

546 547 548 549
	rc->input_name = ir->name;
	rc->input_phys = ir->phys;
	rc->input_id.bustype = BUS_PCI;
	rc->input_id.version = 1;
550
	if (btv->c.pci->subsystem_vendor) {
551 552
		rc->input_id.vendor  = btv->c.pci->subsystem_vendor;
		rc->input_id.product = btv->c.pci->subsystem_device;
L
Linus Torvalds 已提交
553
	} else {
554 555
		rc->input_id.vendor  = btv->c.pci->vendor;
		rc->input_id.product = btv->c.pci->device;
L
Linus Torvalds 已提交
556
	}
557 558 559
	rc->dev.parent = &btv->c.pci->dev;
	rc->map_name = ir_codes;
	rc->driver_name = MODULE_NAME;
L
Linus Torvalds 已提交
560

561
	btv->remote = ir;
562
	bttv_ir_start(btv, ir);
L
Linus Torvalds 已提交
563 564

	/* all done */
565
	err = rc_register_device(rc);
566 567
	if (err)
		goto err_out_stop;
568

L
Linus Torvalds 已提交
569
	return 0;
570 571 572 573 574

 err_out_stop:
	bttv_ir_stop(btv);
	btv->remote = NULL;
 err_out_free:
575
	rc_free_device(rc);
576 577
	kfree(ir);
	return err;
L
Linus Torvalds 已提交
578 579
}

580
void bttv_input_fini(struct bttv *btv)
L
Linus Torvalds 已提交
581
{
582 583
	if (btv->remote == NULL)
		return;
L
Linus Torvalds 已提交
584

585
	bttv_ir_stop(btv);
586
	rc_unregister_device(btv->remote->dev);
587 588
	kfree(btv->remote);
	btv->remote = NULL;
L
Linus Torvalds 已提交
589
}