saa7134-input.c 29.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 21 22 23 24 25
/*
 *
 * handle saa7134 IR remotes via linux kernel input layer.
 *
 * 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
 *
 */

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

#include "saa7134-reg.h"
#include "saa7134.h"

31 32
#define MODULE_NAME "saa7134"

33
static unsigned int disable_ir;
L
Linus Torvalds 已提交
34 35 36
module_param(disable_ir, int, 0444);
MODULE_PARM_DESC(disable_ir,"disable infrared remote support");

37
static unsigned int ir_debug;
L
Linus Torvalds 已提交
38 39 40
module_param(ir_debug, int, 0644);
MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");

41
static int pinnacle_remote;
42 43 44
module_param(pinnacle_remote, int, 0644);    /* Choose Pinnacle PCTV remote */
MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");

45
static int ir_rc5_remote_gap = 885;
46
module_param(ir_rc5_remote_gap, int, 0644);
47
static int ir_rc5_key_timeout = 115;
48 49
module_param(ir_rc5_key_timeout, int, 0644);

50 51 52 53 54
static int repeat_delay = 500;
module_param(repeat_delay, int, 0644);
MODULE_PARM_DESC(repeat_delay, "delay before key repeat started");
static int repeat_period = 33;
module_param(repeat_period, int, 0644);
55
MODULE_PARM_DESC(repeat_period, "repeat period between "
56 57
    "keypresses when key is down");

58 59 60 61 62
static unsigned int disable_other_ir;
module_param(disable_other_ir, int, 0644);
MODULE_PARM_DESC(disable_other_ir, "disable full codes of "
    "alternative remotes from other manufacturers");

L
Linus Torvalds 已提交
63 64
#define dprintk(fmt, arg...)	if (ir_debug) \
	printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
65
#define i2cdprintk(fmt, arg...)    if (ir_debug) \
66
	printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg)
L
Linus Torvalds 已提交
67

68
/* Helper functions for RC5 and NEC decoding at GPIO16 or GPIO18 */
69
static int saa7134_rc5_irq(struct saa7134_dev *dev);
70
static int saa7134_nec_irq(struct saa7134_dev *dev);
71
static int saa7134_raw_decode_irq(struct saa7134_dev *dev);
72 73
static void nec_task(unsigned long data);
static void saa7134_nec_timer(unsigned long data);
74

75
/* -------------------- GPIO generic keycode builder -------------------- */
L
Linus Torvalds 已提交
76 77 78

static int build_key(struct saa7134_dev *dev)
{
79
	struct card_ir *ir = dev->remote;
L
Linus Torvalds 已提交
80 81
	u32 gpio, data;

82 83 84 85 86 87 88
	/* here comes the additional handshake steps for some cards */
	switch (dev->board) {
	case SAA7134_BOARD_GOTVIEW_7135:
		saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
		saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
		break;
	}
L
Linus Torvalds 已提交
89 90 91 92 93
	/* rising SAA7134_GPIO_GPRESCAN reads the status */
	saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
	saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);

	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
94 95 96 97 98
	if (ir->polling) {
		if (ir->last_gpio == gpio)
			return 0;
		ir->last_gpio = gpio;
	}
L
Linus Torvalds 已提交
99

100
	data = ir_extract_bits(gpio, ir->mask_keycode);
L
Linus Torvalds 已提交
101 102 103
	dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
		gpio, ir->mask_keycode, data);

104 105 106 107 108
	switch (dev->board) {
	case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
		if (data == ir->mask_keycode)
			ir_input_nokey(ir->dev, &ir->ir);
		else
109
			ir_input_keydown(ir->dev, &ir->ir, data);
110 111 112
		return 0;
	}

113 114 115
	if (ir->polling) {
		if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
		    (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
116
			ir_input_keydown(ir->dev, &ir->ir, data);
117 118 119
		} else {
			ir_input_nokey(ir->dev, &ir->ir);
		}
L
Linus Torvalds 已提交
120
	}
121 122 123
	else {	/* IRQ driven mode - handle key press and release in one go */
		if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
		    (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
124
			ir_input_keydown(ir->dev, &ir->ir, data);
125 126 127 128
			ir_input_nokey(ir->dev, &ir->ir);
		}
	}

L
Linus Torvalds 已提交
129 130 131
	return 0;
}

132 133
/* --------------------- Chip specific I2C key builders ----------------- */

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
static int get_key_flydvb_trio(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
{
	int gpio;
	int attempt = 0;
	unsigned char b;

	/* We need this to access GPI Used by the saa_readl macro. */
	struct saa7134_dev *dev = ir->c->adapter->algo_data;

	if (dev == NULL) {
		dprintk("get_key_flydvb_trio: "
			 "gir->c->adapter->algo_data is NULL!\n");
		return -EIO;
	}

	/* rising SAA7134_GPIGPRESCAN reads the status */
	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);

	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);

	if (0x40000 & ~gpio)
		return 0; /* No button press */

	/* No button press - only before first key pressed */
	if (b == 0xFF)
		return 0;

	/* poll IR chip */
	/* weak up the IR chip */
	b = 0;

	while (1 != i2c_master_send(ir->c, &b, 1)) {
		if ((attempt++) < 10) {
			/*
			 * wait a bit for next attempt -
			 * I don't know how make it better
			 */
			msleep(10);
			continue;
		}
		i2cdprintk("send wake up byte to pic16C505 (IR chip)"
			   "failed %dx\n", attempt);
		return -EIO;
	}
	if (1 != i2c_master_recv(ir->c, &b, 1)) {
		i2cdprintk("read error\n");
		return -EIO;
	}

	*ir_key = b;
	*ir_raw = b;
	return 1;
}

189 190 191 192 193 194 195
static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir, u32 *ir_key,
				       u32 *ir_raw)
{
	unsigned char b;
	int gpio;

	/* <dev> is needed to access GPIO. Used by the saa_readl macro. */
196
	struct saa7134_dev *dev = ir->c->adapter->algo_data;
197 198
	if (dev == NULL) {
		dprintk("get_key_msi_tvanywhere_plus: "
199
			"gir->c->adapter->algo_data is NULL!\n");
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
		return -EIO;
	}

	/* rising SAA7134_GPIO_GPRESCAN reads the status */

	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);

	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);

	/* GPIO&0x40 is pulsed low when a button is pressed. Don't do
	   I2C receive if gpio&0x40 is not low. */

	if (gpio & 0x40)
		return 0;       /* No button press */

	/* GPIO says there is a button press. Get it. */

218
	if (1 != i2c_master_recv(ir->c, &b, 1)) {
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
		i2cdprintk("read error\n");
		return -EIO;
	}

	/* No button press */

	if (b == 0xff)
		return 0;

	/* Button pressed */

	dprintk("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
	*ir_key = b;
	*ir_raw = b;
	return 1;
}

236 237 238 239 240
static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
{
	unsigned char b;

	/* poll IR chip */
241
	if (1 != i2c_master_recv(ir->c, &b, 1)) {
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
		i2cdprintk("read error\n");
		return -EIO;
	}

	/* no button press */
	if (b==0)
		return 0;

	/* repeating */
	if (b & 0x80)
		return 1;

	*ir_key = b;
	*ir_raw = b;
	return 1;
}

259 260 261 262 263
static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
{
	unsigned char buf[5], cod4, code3, code4;

	/* poll IR chip */
264
	if (5 != i2c_master_recv(ir->c, buf, 5))
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
		return -EIO;

	cod4	= buf[4];
	code4	= (cod4 >> 2);
	code3	= buf[3];
	if (code3 == 0)
		/* no key pressed */
		return 0;

	/* return key */
	*ir_key = code4;
	*ir_raw = code4;
	return 1;
}

280 281 282 283 284 285

static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
{
	unsigned char data[12];
	u32 gpio;

286
	struct saa7134_dev *dev = ir->c->adapter->algo_data;
287 288 289 290 291 292 293

	/* rising SAA7134_GPIO_GPRESCAN reads the status */
	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);

	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);

294
	if (0x400000 & ~gpio)
295 296
		return 0; /* No button press */

297
	ir->c->addr = 0x5a >> 1;
298

299
	if (12 != i2c_master_recv(ir->c, data, 12)) {
300 301 302 303 304 305 306 307 308 309 310 311 312
		i2cdprintk("read error\n");
		return -EIO;
	}
	/* IR of this card normally decode signals NEC-standard from
	 * - Sven IHOO MT 5.1R remote. xxyye718
	 * - Sven DVD HD-10xx remote. xxyyf708
	 * - BBK ...
	 * - mayby others
	 * So, skip not our, if disable full codes mode.
	 */
	if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
		return 0;

313 314 315 316
	/* Wrong data decode fix */
	if (data[9] != (unsigned char)(~data[8]))
		return 0;

317 318 319 320 321 322
	*ir_key = data[9];
	*ir_raw = data[9];

	return 1;
}

323 324 325 326 327 328 329 330 331 332
/* Common (grey or coloured) pinnacle PCTV remote handling
 *
 */
static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
			    int parity_offset, int marker, int code_modulo)
{
	unsigned char b[4];
	unsigned int start = 0,parity = 0,code = 0;

	/* poll IR chip */
333
	if (4 != i2c_master_recv(ir->c, b, 4)) {
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 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 396 397 398 399 400
		i2cdprintk("read error\n");
		return -EIO;
	}

	for (start = 0; start < ARRAY_SIZE(b); start++) {
		if (b[start] == marker) {
			code=b[(start+parity_offset + 1) % 4];
			parity=b[(start+parity_offset) % 4];
		}
	}

	/* Empty Request */
	if (parity == 0)
		return 0;

	/* Repeating... */
	if (ir->old == parity)
		return 0;

	ir->old = parity;

	/* drop special codes when a key is held down a long time for the grey controller
	   In this case, the second bit of the code is asserted */
	if (marker == 0xfe && (code & 0x40))
		return 0;

	code %= code_modulo;

	*ir_raw = code;
	*ir_key = code;

	i2cdprintk("Pinnacle PCTV key %02x\n", code);

	return 1;
}

/* The grey pinnacle PCTV remote
 *
 *  There are one issue with this remote:
 *   - I2c packet does not change when the same key is pressed quickly. The workaround
 *     is to hold down each key for about half a second, so that another code is generated
 *     in the i2c packet, and the function can distinguish key presses.
 *
 * Sylvain Pasche <sylvain.pasche@gmail.com>
 */
static int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
{

	return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
}


/* The new pinnacle PCTV remote (with the colored buttons)
 *
 * Ricardo Cerqueira <v4l@cerqueira.org>
 */
static int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
{
	/* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
	 *
	 * this is the only value that results in 42 unique
	 * codes < 128
	 */

	return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
}

L
Linus Torvalds 已提交
401 402
void saa7134_input_irq(struct saa7134_dev *dev)
{
403
	struct card_ir *ir = dev->remote;
L
Linus Torvalds 已提交
404

405 406
	if (ir->nec_gpio) {
		saa7134_nec_irq(dev);
407
	} else if (!ir->polling && !ir->rc5_gpio && !ir->raw_decode) {
L
Linus Torvalds 已提交
408
		build_key(dev);
409 410
	} else if (ir->rc5_gpio) {
		saa7134_rc5_irq(dev);
411 412
	} else if (ir->raw_decode) {
		saa7134_raw_decode_irq(dev);
413
	}
L
Linus Torvalds 已提交
414 415 416 417
}

static void saa7134_input_timer(unsigned long data)
{
418
	struct saa7134_dev *dev = (struct saa7134_dev *)data;
419
	struct card_ir *ir = dev->remote;
L
Linus Torvalds 已提交
420 421

	build_key(dev);
422
	mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
L
Linus Torvalds 已提交
423 424
}

425 426 427
void ir_raw_decode_timer_end(unsigned long data)
{
	struct saa7134_dev *dev = (struct saa7134_dev *)data;
428
	struct card_ir *ir = dev->remote;
429

430
	ir_raw_event_handle(dev->remote->dev);
431 432

	ir->active = 0;
433 434
}

435
void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
436
{
437 438 439 440
	if (ir->running)
		return;

	ir->running = 1;
441
	if (ir->polling) {
442 443
		setup_timer(&ir->timer, saa7134_input_timer,
			    (unsigned long)dev);
444 445
		ir->timer.expires  = jiffies + HZ;
		add_timer(&ir->timer);
446 447 448 449 450 451 452 453 454 455 456 457 458
	} else if (ir->rc5_gpio) {
		/* set timer_end for code completion */
		init_timer(&ir->timer_end);
		ir->timer_end.function = ir_rc5_timer_end;
		ir->timer_end.data = (unsigned long)ir;
		init_timer(&ir->timer_keyup);
		ir->timer_keyup.function = ir_rc5_timer_keyup;
		ir->timer_keyup.data = (unsigned long)ir;
		ir->shift_by = 2;
		ir->start = 0x2;
		ir->addr = 0x17;
		ir->rc5_key_timeout = ir_rc5_key_timeout;
		ir->rc5_remote_gap = ir_rc5_remote_gap;
459 460 461 462
	} else if (ir->nec_gpio) {
		setup_timer(&ir->timer_keyup, saa7134_nec_timer,
			    (unsigned long)dev);
		tasklet_init(&ir->tlet, nec_task, (unsigned long)dev);
463 464 465 466 467
	} else if (ir->raw_decode) {
		/* set timer_end for code completion */
		init_timer(&ir->timer_end);
		ir->timer_end.function = ir_raw_decode_timer_end;
		ir->timer_end.data = (unsigned long)dev;
468
		ir->active = 0;
469 470 471
	}
}

472
void saa7134_ir_stop(struct saa7134_dev *dev)
473
{
474 475 476 477
	struct card_ir *ir = dev->remote;

	if (!ir->running)
		return;
478 479
	if (dev->remote->polling)
		del_timer_sync(&dev->remote->timer);
480 481 482 483
	else if (ir->rc5_gpio)
		del_timer_sync(&ir->timer_end);
	else if (ir->nec_gpio)
		tasklet_kill(&ir->tlet);
484
	else if (ir->raw_decode) {
485
		del_timer_sync(&ir->timer_end);
486 487
		ir->active = 0;
	}
488

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
	ir->running = 0;
}

int saa7134_ir_change_protocol(void *priv, u64 ir_type)
{
	struct saa7134_dev *dev = priv;
	struct card_ir *ir = dev->remote;
	u32 nec_gpio, rc5_gpio;

	if (ir_type == IR_TYPE_RC5) {
		dprintk("Changing protocol to RC5\n");
		nec_gpio = 0;
		rc5_gpio = 1;
	} else if (ir_type == IR_TYPE_NEC) {
		dprintk("Changing protocol to NEC\n");
		nec_gpio = 1;
		rc5_gpio = 0;
	} else {
		dprintk("IR protocol type %ud is not supported\n",
			(unsigned)ir_type);
		return -EINVAL;
	}

	if (ir->running) {
		saa7134_ir_stop(dev);
		ir->nec_gpio = nec_gpio;
		ir->rc5_gpio = rc5_gpio;
		saa7134_ir_start(dev, ir);
	} else {
		ir->nec_gpio = nec_gpio;
		ir->rc5_gpio = rc5_gpio;
	}

	return 0;
523 524
}

L
Linus Torvalds 已提交
525 526
int saa7134_input_init1(struct saa7134_dev *dev)
{
527
	struct card_ir *ir;
528
	struct input_dev *input_dev;
529
	struct ir_scancode_table *ir_codes = NULL;
L
Linus Torvalds 已提交
530 531 532 533
	u32 mask_keycode = 0;
	u32 mask_keydown = 0;
	u32 mask_keyup   = 0;
	int polling      = 0;
534
	int rc5_gpio	 = 0;
535
	int nec_gpio	 = 0;
536
	int raw_decode   = 0;
537
	u64 ir_type = IR_TYPE_OTHER;
538
	int err;
L
Linus Torvalds 已提交
539

540
	if (dev->has_remote != SAA7134_REMOTE_GPIO)
L
Linus Torvalds 已提交
541 542 543 544 545 546 547 548
		return -ENODEV;
	if (disable_ir)
		return -ENODEV;

	/* detect & configure */
	switch (dev->board) {
	case SAA7134_BOARD_FLYVIDEO2000:
	case SAA7134_BOARD_FLYVIDEO3000:
549
	case SAA7134_BOARD_HAWELL_HW_404M7:
550
	case SAA7134_BOARD_FLYTVPLATINUM_FM:
551
	case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
552
	case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
553
		ir_codes     = &ir_codes_flyvideo_table;
L
Linus Torvalds 已提交
554 555 556 557 558 559
		mask_keycode = 0xEC00000;
		mask_keydown = 0x0040000;
		break;
	case SAA7134_BOARD_CINERGY400:
	case SAA7134_BOARD_CINERGY600:
	case SAA7134_BOARD_CINERGY600_MK3:
560
		ir_codes     = &ir_codes_cinergy_table;
L
Linus Torvalds 已提交
561 562 563 564 565
		mask_keycode = 0x00003f;
		mask_keyup   = 0x040000;
		break;
	case SAA7134_BOARD_ECS_TVP3XP:
	case SAA7134_BOARD_ECS_TVP3XP_4CB5:
566
		ir_codes     = &ir_codes_eztv_table;
567 568
		mask_keycode = 0x00017c;
		mask_keyup   = 0x000002;
L
Linus Torvalds 已提交
569
		polling      = 50; // ms
570 571
		break;
	case SAA7134_BOARD_KWORLD_XPERT:
L
Linus Torvalds 已提交
572
	case SAA7134_BOARD_AVACSSMARTTV:
573
		ir_codes     = &ir_codes_pixelview_table;
L
Linus Torvalds 已提交
574 575 576 577 578
		mask_keycode = 0x00001F;
		mask_keyup   = 0x000020;
		polling      = 50; // ms
		break;
	case SAA7134_BOARD_MD2819:
579
	case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
L
Linus Torvalds 已提交
580 581
	case SAA7134_BOARD_AVERMEDIA_305:
	case SAA7134_BOARD_AVERMEDIA_307:
582
	case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
583
	case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
584
	case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
585
	case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
586
	case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
587
	case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
588
	case SAA7134_BOARD_AVERMEDIA_M102:
589
	case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
590
		ir_codes     = &ir_codes_avermedia_table;
L
Linus Torvalds 已提交
591 592 593 594 595 596 597
		mask_keycode = 0x0007C8;
		mask_keydown = 0x000010;
		polling      = 50; // ms
		/* Set GPIO pin2 to high to enable the IR controller */
		saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
		saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
		break;
598
	case SAA7134_BOARD_AVERMEDIA_M135A:
599
		ir_codes     = &ir_codes_avermedia_m135a_rm_jx_table;
600
		mask_keydown = 0x0040000;
601
		mask_keycode = 0xffff;
602
		raw_decode   = 1;
603
		break;
604
	case SAA7134_BOARD_AVERMEDIA_777:
605
	case SAA7134_BOARD_AVERMEDIA_A16AR:
606
		ir_codes     = &ir_codes_avermedia_table;
607 608 609 610 611 612
		mask_keycode = 0x02F200;
		mask_keydown = 0x000400;
		polling      = 50; // ms
		/* Without this we won't receive key up events */
		saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
		saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
613
		break;
614
	case SAA7134_BOARD_AVERMEDIA_A16D:
615
		ir_codes     = &ir_codes_avermedia_a16d_table;
616 617 618 619 620 621 622
		mask_keycode = 0x02F200;
		mask_keydown = 0x000400;
		polling      = 50; /* ms */
		/* Without this we won't receive key up events */
		saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
		saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
		break;
623
	case SAA7134_BOARD_KWORLD_TERMINATOR:
624
		ir_codes     = &ir_codes_pixelview_table;
625 626 627 628
		mask_keycode = 0x00001f;
		mask_keyup   = 0x000060;
		polling      = 50; // ms
		break;
629 630
	case SAA7134_BOARD_MANLI_MTV001:
	case SAA7134_BOARD_MANLI_MTV002:
631
		ir_codes     = &ir_codes_manli_table;
632 633 634 635
		mask_keycode = 0x001f00;
		mask_keyup   = 0x004000;
		polling      = 50; /* ms */
		break;
636
	case SAA7134_BOARD_BEHOLD_409FM:
637 638 639 640 641 642 643 644 645
	case SAA7134_BOARD_BEHOLD_401:
	case SAA7134_BOARD_BEHOLD_403:
	case SAA7134_BOARD_BEHOLD_403FM:
	case SAA7134_BOARD_BEHOLD_405:
	case SAA7134_BOARD_BEHOLD_405FM:
	case SAA7134_BOARD_BEHOLD_407:
	case SAA7134_BOARD_BEHOLD_407FM:
	case SAA7134_BOARD_BEHOLD_409:
	case SAA7134_BOARD_BEHOLD_505FM:
646 647
	case SAA7134_BOARD_BEHOLD_505RDS_MK5:
	case SAA7134_BOARD_BEHOLD_505RDS_MK3:
648
	case SAA7134_BOARD_BEHOLD_507_9FM:
649 650
	case SAA7134_BOARD_BEHOLD_507RDS_MK3:
	case SAA7134_BOARD_BEHOLD_507RDS_MK5:
651
		ir_codes     = &ir_codes_manli_table;
652 653 654 655 656
		mask_keycode = 0x003f00;
		mask_keyup   = 0x004000;
		polling      = 50; /* ms */
		break;
	case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
657
		ir_codes     = &ir_codes_behold_columbus_table;
658
		mask_keycode = 0x003f00;
659 660 661
		mask_keyup   = 0x004000;
		polling      = 50; // ms
		break;
662
	case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
663
		ir_codes     = &ir_codes_pctv_sedna_table;
664 665 666 667
		mask_keycode = 0x001f00;
		mask_keyup   = 0x004000;
		polling      = 50; // ms
		break;
668
	case SAA7134_BOARD_GOTVIEW_7135:
669
		ir_codes     = &ir_codes_gotview7135_table;
670
		mask_keycode = 0x0003CC;
671
		mask_keydown = 0x000010;
672 673
		polling	     = 5; /* ms */
		saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
674
		break;
L
Linus Torvalds 已提交
675
	case SAA7134_BOARD_VIDEOMATE_TV_PVR:
676
	case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
677
	case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
678
		ir_codes     = &ir_codes_videomate_tv_pvr_table;
L
Linus Torvalds 已提交
679 680 681 682
		mask_keycode = 0x00003F;
		mask_keyup   = 0x400000;
		polling      = 50; // ms
		break;
683
	case SAA7134_BOARD_PROTEUS_2309:
684
		ir_codes     = &ir_codes_proteus_2309_table;
685 686 687 688
		mask_keycode = 0x00007F;
		mask_keyup   = 0x000080;
		polling      = 50; // ms
		break;
689 690
	case SAA7134_BOARD_VIDEOMATE_DVBT_300:
	case SAA7134_BOARD_VIDEOMATE_DVBT_200:
691
		ir_codes     = &ir_codes_videomate_tv_pvr_table;
692 693 694
		mask_keycode = 0x003F00;
		mask_keyup   = 0x040000;
		break;
695
	case SAA7134_BOARD_FLYDVBS_LR300:
696
	case SAA7134_BOARD_FLYDVBT_LR301:
697
	case SAA7134_BOARD_FLYDVBTDUO:
698
		ir_codes     = &ir_codes_flydvb_table;
699 700 701
		mask_keycode = 0x0001F00;
		mask_keydown = 0x0040000;
		break;
702
	case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
703
	case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
704
	case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
705
		ir_codes     = &ir_codes_asus_pc39_table;
706 707 708
		mask_keydown = 0x0040000;
		rc5_gpio = 1;
		break;
709 710
	case SAA7134_BOARD_ENCORE_ENLTV:
	case SAA7134_BOARD_ENCORE_ENLTV_FM:
711
		ir_codes     = &ir_codes_encore_enltv_table;
712 713 714 715
		mask_keycode = 0x00007f;
		mask_keyup   = 0x040000;
		polling      = 50; // ms
		break;
716
	case SAA7134_BOARD_ENCORE_ENLTV_FM53:
717
		ir_codes     = &ir_codes_encore_enltv_fm53_table;
718 719 720 721
		mask_keydown = 0x0040000;
		mask_keycode = 0x00007f;
		nec_gpio = 1;
		break;
722
	case SAA7134_BOARD_10MOONSTVMASTER3:
723
		ir_codes     = &ir_codes_encore_enltv_table;
724 725 726 727
		mask_keycode = 0x5f80000;
		mask_keyup   = 0x8000000;
		polling      = 50; //ms
		break;
728
	case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
729
		ir_codes     = &ir_codes_genius_tvgo_a11mce_table;
730 731 732 733
		mask_keycode = 0xff;
		mask_keydown = 0xf00000;
		polling = 50; /* ms */
		break;
734
	case SAA7134_BOARD_REAL_ANGEL_220:
735
		ir_codes     = &ir_codes_real_audio_220_32_keys_table;
736 737 738 739
		mask_keycode = 0x3f00;
		mask_keyup   = 0x4000;
		polling = 50; /* ms */
		break;
740
	case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
741
		ir_codes     = &ir_codes_kworld_plus_tv_analog_table;
742 743 744
		mask_keycode = 0x7f;
		polling = 40; /* ms */
		break;
745
	case SAA7134_BOARD_VIDEOMATE_S350:
746
		ir_codes     = &ir_codes_videomate_s350_table;
747 748 749
		mask_keycode = 0x003f00;
		mask_keydown = 0x040000;
		break;
750 751 752 753
	case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
		ir_codes     = &ir_codes_winfast_table;
		mask_keycode = 0x5f00;
		mask_keyup   = 0x020000;
754
		polling      = 50; /* ms */
755
		break;
756
	break;
L
Linus Torvalds 已提交
757 758 759 760 761 762 763
	}
	if (NULL == ir_codes) {
		printk("%s: Oops: IR config error [card=%d]\n",
		       dev->name, dev->board);
		return -ENODEV;
	}

764 765 766
	ir = kzalloc(sizeof(*ir), GFP_KERNEL);
	input_dev = input_allocate_device();
	if (!ir || !input_dev) {
767 768
		err = -ENOMEM;
		goto err_out_free;
769
	}
L
Linus Torvalds 已提交
770

771
	ir->dev = input_dev;
772 773 774
	dev->remote = ir;

	ir->running = 0;
775

L
Linus Torvalds 已提交
776 777 778 779
	/* init hardware-specific stuff */
	ir->mask_keycode = mask_keycode;
	ir->mask_keydown = mask_keydown;
	ir->mask_keyup   = mask_keyup;
780
	ir->polling      = polling;
781
	ir->rc5_gpio	 = rc5_gpio;
782
	ir->nec_gpio	 = nec_gpio;
783
	ir->raw_decode	 = raw_decode;
L
Linus Torvalds 已提交
784 785 786 787 788 789 790

	/* init input device */
	snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
		 saa7134_boards[dev->board].name);
	snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
		 pci_name(dev->pci));

791
	if (ir_codes->ir_type != IR_TYPE_OTHER && !raw_decode) {
792 793 794 795 796 797 798
		ir->props.allowed_protos = IR_TYPE_RC5 | IR_TYPE_NEC;
		ir->props.priv = dev;
		ir->props.change_protocol = saa7134_ir_change_protocol;

		/* Set IR protocol */
		saa7134_ir_change_protocol(ir->props.priv, ir_codes->ir_type);
	}
799
	err = ir_input_init(input_dev, &ir->ir, ir_type);
800 801 802
	if (err < 0)
		goto err_out_free;

803 804 805 806
	input_dev->name = ir->name;
	input_dev->phys = ir->phys;
	input_dev->id.bustype = BUS_PCI;
	input_dev->id.version = 1;
L
Linus Torvalds 已提交
807
	if (dev->pci->subsystem_vendor) {
808 809
		input_dev->id.vendor  = dev->pci->subsystem_vendor;
		input_dev->id.product = dev->pci->subsystem_device;
L
Linus Torvalds 已提交
810
	} else {
811 812
		input_dev->id.vendor  = dev->pci->vendor;
		input_dev->id.product = dev->pci->device;
L
Linus Torvalds 已提交
813
	}
814
	input_dev->dev.parent = &dev->pci->dev;
L
Linus Torvalds 已提交
815

816
	err = ir_input_register(ir->dev, ir_codes, &ir->props, MODULE_NAME);
817 818
	if (err)
		goto err_out_stop;
819 820 821 822 823
	if (ir_codes->ir_type != IR_TYPE_OTHER) {
		err = ir_raw_event_register(ir->dev);
		if (err)
			goto err_out_stop;
	}
L
Linus Torvalds 已提交
824

825 826
	saa7134_ir_start(dev, ir);

827 828 829 830
	/* the remote isn't as bouncy as a keyboard */
	ir->dev->rep[REP_DELAY] = repeat_delay;
	ir->dev->rep[REP_PERIOD] = repeat_period;

L
Linus Torvalds 已提交
831
	return 0;
832 833 834 835 836 837 838

 err_out_stop:
	saa7134_ir_stop(dev);
	dev->remote = NULL;
 err_out_free:
	kfree(ir);
	return err;
L
Linus Torvalds 已提交
839 840 841 842 843 844 845
}

void saa7134_input_fini(struct saa7134_dev *dev)
{
	if (NULL == dev->remote)
		return;

846
	saa7134_ir_stop(dev);
847
	ir_raw_event_unregister(dev->remote->dev);
848
	ir_input_unregister(dev->remote->dev);
L
Linus Torvalds 已提交
849 850 851 852
	kfree(dev->remote);
	dev->remote = NULL;
}

853
void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
854
{
855
	struct i2c_board_info info;
856 857 858 859 860 861 862 863 864 865

	struct i2c_msg msg_msi = {
		.addr = 0x50,
		.flags = I2C_M_RD,
		.len = 0,
		.buf = NULL,
	};

	int rc;

866
	if (disable_ir) {
867
		dprintk("IR has been disabled, not probing for i2c remote\n");
868 869 870
		return;
	}

871
	memset(&info, 0, sizeof(struct i2c_board_info));
872
	memset(&dev->init_data, 0, sizeof(dev->init_data));
873
	strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
874

875 876
	switch (dev->board) {
	case SAA7134_BOARD_PINNACLE_PCTV_110i:
877
	case SAA7134_BOARD_PINNACLE_PCTV_310i:
878
		dev->init_data.name = "Pinnacle PCTV";
879
		if (pinnacle_remote == 0) {
880 881
			dev->init_data.get_key = get_key_pinnacle_color;
			dev->init_data.ir_codes = &ir_codes_pinnacle_color_table;
882
			info.addr = 0x47;
883
		} else {
884 885
			dev->init_data.get_key = get_key_pinnacle_grey;
			dev->init_data.ir_codes = &ir_codes_pinnacle_grey_table;
886
			info.addr = 0x47;
887
		}
888 889
		break;
	case SAA7134_BOARD_UPMOST_PURPLE_TV:
890 891 892
		dev->init_data.name = "Purple TV";
		dev->init_data.get_key = get_key_purpletv;
		dev->init_data.ir_codes = &ir_codes_purpletv_table;
893
		info.addr = 0x7a;
894
		break;
895
	case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
896 897 898
		dev->init_data.name = "MSI TV@nywhere Plus";
		dev->init_data.get_key = get_key_msi_tvanywhere_plus;
		dev->init_data.ir_codes = &ir_codes_msi_tvanywhere_plus_table;
899
		info.addr = 0x30;
900 901 902 903 904 905 906 907
		/* MSI TV@nywhere Plus controller doesn't seem to
		   respond to probes unless we read something from
		   an existing device. Weird...
		   REVISIT: might no longer be needed */
		rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
		dprintk(KERN_DEBUG "probe 0x%02x @ %s: %s\n",
			msg_msi.addr, dev->i2c_adap.name,
			(1 == rc) ? "yes" : "no");
908
		break;
909
	case SAA7134_BOARD_HAUPPAUGE_HVR1110:
910 911 912
		dev->init_data.name = "HVR 1110";
		dev->init_data.get_key = get_key_hvr1110;
		dev->init_data.ir_codes = &ir_codes_hauppauge_new_table;
913
		info.addr = 0x71;
914
		break;
915 916 917 918 919 920 921 922
	case SAA7134_BOARD_BEHOLD_607FM_MK3:
	case SAA7134_BOARD_BEHOLD_607FM_MK5:
	case SAA7134_BOARD_BEHOLD_609FM_MK3:
	case SAA7134_BOARD_BEHOLD_609FM_MK5:
	case SAA7134_BOARD_BEHOLD_607RDS_MK3:
	case SAA7134_BOARD_BEHOLD_607RDS_MK5:
	case SAA7134_BOARD_BEHOLD_609RDS_MK3:
	case SAA7134_BOARD_BEHOLD_609RDS_MK5:
923
	case SAA7134_BOARD_BEHOLD_M6:
924 925
	case SAA7134_BOARD_BEHOLD_M63:
	case SAA7134_BOARD_BEHOLD_M6_EXTRA:
926
	case SAA7134_BOARD_BEHOLD_H6:
927
	case SAA7134_BOARD_BEHOLD_X7:
928 929 930
		dev->init_data.name = "BeholdTV";
		dev->init_data.get_key = get_key_beholdm6xx;
		dev->init_data.ir_codes = &ir_codes_behold_table;
931
		dev->init_data.type = IR_TYPE_NEC;
932
		info.addr = 0x2d;
933
		break;
934 935
	case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
	case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
936
		info.addr = 0x40;
937
		break;
938 939 940 941 942 943
	case SAA7134_BOARD_FLYDVB_TRIO:
		dev->init_data.name = "FlyDVB Trio";
		dev->init_data.get_key = get_key_flydvb_trio;
		dev->init_data.ir_codes = &ir_codes_flydvb_table;
		info.addr = 0x0b;
		break;
944 945 946
	default:
		dprintk("No I2C IR support for board %x\n", dev->board);
		return;
947 948
	}

949
	if (dev->init_data.name)
950
		info.platform_data = &dev->init_data;
951
	i2c_new_device(&dev->i2c_adap, &info);
952
}
953

954 955 956 957 958 959 960 961 962 963 964 965 966
static int saa7134_raw_decode_irq(struct saa7134_dev *dev)
{
	struct card_ir	*ir = dev->remote;
	unsigned long 	timeout;
	int count, pulse, oldpulse;

	/* Generate initial event */
	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
	pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
	ir_raw_event_store(dev->remote->dev, pulse? IR_PULSE : IR_SPACE);


967 968 969 970 971 972 973 974 975 976
	/*
	 * Wait 15 ms from the start of the first IR event before processing
	 * the event. This time is enough for NEC protocol. May need adjustments
	 * to work with other protocols.
	 */
	if (!ir->active) {
		timeout = jiffies + jiffies_to_msecs(15);
		mod_timer(&ir->timer_end, timeout);
		ir->active = 1;
	}
977 978 979 980

	return 1;
}

981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
static int saa7134_rc5_irq(struct saa7134_dev *dev)
{
	struct card_ir *ir = dev->remote;
	struct timeval tv;
	u32 gap;
	unsigned long current_jiffies, timeout;

	/* 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;
	}

	/* active code => add bit */
	if (ir->active) {
		/* only if in the code (otherwise spurious IRQ or timer
		   late) */
		if (ir->last_bit < 28) {
			ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
			    ir_rc5_remote_gap;
			ir->code |= 1 << ir->last_bit;
		}
		/* starting new code */
	} else {
		ir->active = 1;
		ir->code = 0;
		ir->base_time = tv;
		ir->last_bit = 0;

		timeout = current_jiffies + (500 + 30 * HZ) / 1000;
		mod_timer(&ir->timer_end, timeout);
	}

	return 1;
}

1023 1024
/* On NEC protocol, One has 2.25 ms, and zero has 1.125 ms
   The first pulse (start) has 9 + 4.5 ms
L
Linus Torvalds 已提交
1025
 */
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124

static void saa7134_nec_timer(unsigned long data)
{
	struct saa7134_dev *dev = (struct saa7134_dev *) data;
	struct card_ir *ir = dev->remote;

	dprintk("Cancel key repeat\n");

	ir_input_nokey(ir->dev, &ir->ir);
}

static void nec_task(unsigned long data)
{
	struct saa7134_dev *dev = (struct saa7134_dev *) data;
	struct card_ir *ir;
	struct timeval tv;
	int count, pulse, oldpulse, gap;
	u32 ircode = 0, not_code = 0;
	int ngap = 0;

	if (!data) {
		printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
		/* GPIO will be kept disabled */
		return;
	}

	ir = dev->remote;

	/* rising SAA7134_GPIO_GPRESCAN reads the status */
	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);

	oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
	pulse = oldpulse;

	do_gettimeofday(&tv);
	ir->base_time = tv;

	/* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
	   Unfortunately, using IRQ to decode pulse didn't work, since it uses
	   a pulse train of 38KHz. This means one pulse on each 52 us
	 */
	do {
		/* Wait until the end of pulse/space or 5 ms */
		for (count = 0; count < 500; count++)  {
			udelay(10);
			/* rising SAA7134_GPIO_GPRESCAN reads the status */
			saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
			saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
			pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
				& ir->mask_keydown;
			if (pulse != oldpulse)
				break;
		}

		do_gettimeofday(&tv);
		gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
				tv.tv_usec - ir->base_time.tv_usec;

		if (!pulse) {
			/* Bit 0 has 560 us, while bit 1 has 1120 us.
			   Do something only if bit == 1
			 */
			if (ngap && (gap > 560 + 280)) {
				unsigned int shift = ngap - 1;

				/* Address first, then command */
				if (shift < 8) {
					shift += 8;
					ircode |= 1 << shift;
				} else if (shift < 16) {
					not_code |= 1 << shift;
				} else if (shift < 24) {
					shift -= 16;
					ircode |= 1 << shift;
				} else {
					shift -= 24;
					not_code |= 1 << shift;
				}
			}
			ngap++;
		}


		ir->base_time = tv;

		/* TIMEOUT - Long pulse */
		if (gap >= 5000)
			break;
		oldpulse = pulse;
	} while (ngap < 32);

	if (ngap == 32) {
		/* FIXME: should check if not_code == ~ircode */
		ir->code = ir_extract_bits(ircode, ir->mask_keycode);

		dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
			 ir->code, ircode, not_code);

1125
		ir_input_keydown(ir->dev, &ir->ir, ir->code);
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
	} else
		dprintk("Repeat last key\n");

	/* Keep repeating the last key */
	mod_timer(&ir->timer_keyup, jiffies + msecs_to_jiffies(150));

	saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
}

static int saa7134_nec_irq(struct saa7134_dev *dev)
{
	struct card_ir *ir = dev->remote;

	saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
	tasklet_schedule(&ir->tlet);

	return 1;
}