saa7134-input.c 24.0 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
/*
 *
 * 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>
25
#include <linux/slab.h>
L
Linus Torvalds 已提交
26 27 28 29

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

30
#define MODULE_NAME "saa7134"
31

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

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

40
static int pinnacle_remote;
41 42 43
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)");

44 45 46 47 48
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 已提交
49 50
#define dprintk(fmt, arg...)	if (ir_debug) \
	printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
51
#define i2cdprintk(fmt, arg...)    if (ir_debug) \
52
	printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg)
L
Linus Torvalds 已提交
53

54
/* Helper function for raw decoding at GPIO16 or GPIO18 */
55
static int saa7134_raw_decode_irq(struct saa7134_dev *dev);
56

57
/* -------------------- GPIO generic keycode builder -------------------- */
L
Linus Torvalds 已提交
58 59 60

static int build_key(struct saa7134_dev *dev)
{
61
	struct saa7134_card_ir *ir = dev->remote;
L
Linus Torvalds 已提交
62 63
	u32 gpio, data;

64 65 66 67 68 69 70
	/* 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 已提交
71 72 73 74 75
	/* 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);
76 77 78 79 80
	if (ir->polling) {
		if (ir->last_gpio == gpio)
			return 0;
		ir->last_gpio = gpio;
	}
L
Linus Torvalds 已提交
81

82
	data = ir_extract_bits(gpio, ir->mask_keycode);
L
Linus Torvalds 已提交
83 84 85
	dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
		gpio, ir->mask_keycode, data);

86 87 88
	switch (dev->board) {
	case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
		if (data == ir->mask_keycode)
89
			rc_keyup(ir->dev);
90
		else
91
			rc_keydown_notimeout(ir->dev, data, 0);
92 93 94
		return 0;
	}

95 96 97
	if (ir->polling) {
		if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
		    (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
98
			rc_keydown_notimeout(ir->dev, data, 0);
99
		} else {
100
			rc_keyup(ir->dev);
101
		}
L
Linus Torvalds 已提交
102
	}
103 104 105
	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)))) {
106 107
			rc_keydown_notimeout(ir->dev, data, 0);
			rc_keyup(ir->dev);
108 109 110
		}
	}

L
Linus Torvalds 已提交
111 112 113
	return 0;
}

114 115
/* --------------------- Chip specific I2C key builders ----------------- */

116 117 118 119 120 121 122 123 124 125
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) {
126 127
		i2cdprintk("get_key_flydvb_trio: "
			   "ir->c->adapter->algo_data is NULL!\n");
128 129 130 131 132 133 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
		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;
}

171 172 173 174 175 176 177
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. */
178
	struct saa7134_dev *dev = ir->c->adapter->algo_data;
179
	if (dev == NULL) {
180 181
		i2cdprintk("get_key_msi_tvanywhere_plus: "
			   "ir->c->adapter->algo_data is NULL!\n");
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
		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. */

200
	if (1 != i2c_master_recv(ir->c, &b, 1)) {
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
		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;
}

218 219 220 221 222
static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
{
	unsigned char b;

	/* poll IR chip */
223
	if (1 != i2c_master_recv(ir->c, &b, 1)) {
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
		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;
}

241 242 243 244 245
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 */
246
	if (5 != i2c_master_recv(ir->c, buf, 5))
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
		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;
}

262 263 264 265 266 267

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

268
	struct saa7134_dev *dev = ir->c->adapter->algo_data;
269 270 271 272 273 274 275

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

276
	if (0x400000 & ~gpio)
277 278
		return 0; /* No button press */

279
	ir->c->addr = 0x5a >> 1;
280

281
	if (12 != i2c_master_recv(ir->c, data, 12)) {
282 283 284 285 286 287 288 289 290 291 292 293 294
		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;

295 296 297 298
	/* Wrong data decode fix */
	if (data[9] != (unsigned char)(~data[8]))
		return 0;

299 300 301 302 303 304
	*ir_key = data[9];
	*ir_raw = data[9];

	return 1;
}

305 306 307 308 309 310 311 312 313 314
/* 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 */
315
	if (4 != i2c_master_recv(ir->c, b, 4)) {
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 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
		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 已提交
383 384
void saa7134_input_irq(struct saa7134_dev *dev)
{
385
	struct saa7134_card_ir *ir;
386 387 388 389 390 391 392

	if (!dev || !dev->remote)
		return;

	ir = dev->remote;
	if (!ir->running)
		return;
L
Linus Torvalds 已提交
393

394
	if (!ir->polling && !ir->raw_decode) {
L
Linus Torvalds 已提交
395
		build_key(dev);
396 397
	} else if (ir->raw_decode) {
		saa7134_raw_decode_irq(dev);
398
	}
L
Linus Torvalds 已提交
399 400 401 402
}

static void saa7134_input_timer(unsigned long data)
{
403
	struct saa7134_dev *dev = (struct saa7134_dev *)data;
404
	struct saa7134_card_ir *ir = dev->remote;
L
Linus Torvalds 已提交
405 406

	build_key(dev);
407
	mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
L
Linus Torvalds 已提交
408 409
}

410
static void ir_raw_decode_timer_end(unsigned long data)
411 412
{
	struct saa7134_dev *dev = (struct saa7134_dev *)data;
413
	struct saa7134_card_ir *ir = dev->remote;
414

415
	ir_raw_event_handle(dev->remote->dev);
416

417
	ir->active = false;
418 419
}

420
static int __saa7134_ir_start(void *priv)
421
{
422
	struct saa7134_dev *dev = priv;
423
	struct saa7134_card_ir *ir;
424 425 426 427 428 429 430 431

	if (!dev)
		return -EINVAL;

	ir  = dev->remote;
	if (!ir)
		return -EINVAL;

432
	if (ir->running)
433
		return 0;
434

435
	ir->running = true;
436
	if (ir->polling) {
437 438
		setup_timer(&ir->timer, saa7134_input_timer,
			    (unsigned long)dev);
439 440
		ir->timer.expires  = jiffies + HZ;
		add_timer(&ir->timer);
441 442 443 444 445
	} 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;
446
		ir->active = false;
447
	}
448 449

	return 0;
450 451
}

452
static void __saa7134_ir_stop(void *priv)
453
{
454
	struct saa7134_dev *dev = priv;
455
	struct saa7134_card_ir *ir;
456 457 458 459 460 461 462

	if (!dev)
		return;

	ir  = dev->remote;
	if (!ir)
		return;
463 464 465

	if (!ir->running)
		return;
466 467
	if (dev->remote->polling)
		del_timer_sync(&dev->remote->timer);
468
	else if (ir->raw_decode) {
469
		del_timer_sync(&ir->timer_end);
470
		ir->active = false;
471
	}
472

473
	ir->running = false;
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491

	return;
}

int saa7134_ir_start(struct saa7134_dev *dev)
{
	if (dev->remote->users)
		return __saa7134_ir_start(dev);

	return 0;
}

void saa7134_ir_stop(struct saa7134_dev *dev)
{
	if (dev->remote->users)
		__saa7134_ir_stop(dev);
}

492
static int saa7134_ir_open(struct rc_dev *rc)
493
{
494
	struct saa7134_dev *dev = rc->priv;
495 496 497 498 499

	dev->remote->users++;
	return __saa7134_ir_start(dev);
}

500
static void saa7134_ir_close(struct rc_dev *rc)
501
{
502
	struct saa7134_dev *dev = rc->priv;
503 504 505 506

	dev->remote->users--;
	if (!dev->remote->users)
		__saa7134_ir_stop(dev);
507 508
}

L
Linus Torvalds 已提交
509 510
int saa7134_input_init1(struct saa7134_dev *dev)
{
511
	struct saa7134_card_ir *ir;
512
	struct rc_dev *rc;
513
	char *ir_codes = NULL;
L
Linus Torvalds 已提交
514 515 516 517
	u32 mask_keycode = 0;
	u32 mask_keydown = 0;
	u32 mask_keyup   = 0;
	int polling      = 0;
518
	int raw_decode   = 0;
519
	int err;
L
Linus Torvalds 已提交
520

521
	if (dev->has_remote != SAA7134_REMOTE_GPIO)
L
Linus Torvalds 已提交
522 523 524 525 526 527 528 529
		return -ENODEV;
	if (disable_ir)
		return -ENODEV;

	/* detect & configure */
	switch (dev->board) {
	case SAA7134_BOARD_FLYVIDEO2000:
	case SAA7134_BOARD_FLYVIDEO3000:
530
	case SAA7134_BOARD_FLYTVPLATINUM_FM:
531
	case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
532
	case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
533
		ir_codes     = RC_MAP_FLYVIDEO;
L
Linus Torvalds 已提交
534 535 536 537 538 539
		mask_keycode = 0xEC00000;
		mask_keydown = 0x0040000;
		break;
	case SAA7134_BOARD_CINERGY400:
	case SAA7134_BOARD_CINERGY600:
	case SAA7134_BOARD_CINERGY600_MK3:
540
		ir_codes     = RC_MAP_CINERGY;
L
Linus Torvalds 已提交
541 542 543 544 545
		mask_keycode = 0x00003f;
		mask_keyup   = 0x040000;
		break;
	case SAA7134_BOARD_ECS_TVP3XP:
	case SAA7134_BOARD_ECS_TVP3XP_4CB5:
546
		ir_codes     = RC_MAP_EZTV;
547 548
		mask_keycode = 0x00017c;
		mask_keyup   = 0x000002;
L
Linus Torvalds 已提交
549
		polling      = 50; // ms
550 551
		break;
	case SAA7134_BOARD_KWORLD_XPERT:
L
Linus Torvalds 已提交
552
	case SAA7134_BOARD_AVACSSMARTTV:
553
		ir_codes     = RC_MAP_PIXELVIEW;
L
Linus Torvalds 已提交
554 555 556 557 558
		mask_keycode = 0x00001F;
		mask_keyup   = 0x000020;
		polling      = 50; // ms
		break;
	case SAA7134_BOARD_MD2819:
559
	case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
L
Linus Torvalds 已提交
560 561
	case SAA7134_BOARD_AVERMEDIA_305:
	case SAA7134_BOARD_AVERMEDIA_307:
562
	case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
563
	case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
564
	case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
565
	case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
566
	case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
567
	case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
568
	case SAA7134_BOARD_AVERMEDIA_M102:
569
	case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
570
		ir_codes     = RC_MAP_AVERMEDIA;
L
Linus Torvalds 已提交
571 572 573 574 575 576 577
		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;
578
	case SAA7134_BOARD_AVERMEDIA_M135A:
579
		ir_codes     = RC_MAP_AVERMEDIA_M135A;
580 581
		mask_keydown = 0x0040000;	/* Enable GPIO18 line on both edges */
		mask_keyup   = 0x0040000;
582
		mask_keycode = 0xffff;
583
		raw_decode   = 1;
584
		break;
585 586 587 588 589 590 591
	case SAA7134_BOARD_AVERMEDIA_M733A:
		ir_codes     = RC_MAP_AVERMEDIA_M733A_RM_K6;
		mask_keydown = 0x0040000;
		mask_keyup   = 0x0040000;
		mask_keycode = 0xffff;
		raw_decode   = 1;
		break;
592
	case SAA7134_BOARD_AVERMEDIA_777:
593
	case SAA7134_BOARD_AVERMEDIA_A16AR:
594
		ir_codes     = RC_MAP_AVERMEDIA;
595 596 597 598 599 600
		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);
601
		break;
602
	case SAA7134_BOARD_AVERMEDIA_A16D:
603
		ir_codes     = RC_MAP_AVERMEDIA_A16D;
604 605 606 607 608 609 610
		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;
611
	case SAA7134_BOARD_KWORLD_TERMINATOR:
612
		ir_codes     = RC_MAP_PIXELVIEW;
613 614 615 616
		mask_keycode = 0x00001f;
		mask_keyup   = 0x000060;
		polling      = 50; // ms
		break;
617 618
	case SAA7134_BOARD_MANLI_MTV001:
	case SAA7134_BOARD_MANLI_MTV002:
619
		ir_codes     = RC_MAP_MANLI;
620 621 622 623
		mask_keycode = 0x001f00;
		mask_keyup   = 0x004000;
		polling      = 50; /* ms */
		break;
624
	case SAA7134_BOARD_BEHOLD_409FM:
625 626 627 628 629 630 631 632 633
	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:
634 635
	case SAA7134_BOARD_BEHOLD_505RDS_MK5:
	case SAA7134_BOARD_BEHOLD_505RDS_MK3:
636
	case SAA7134_BOARD_BEHOLD_507_9FM:
637 638
	case SAA7134_BOARD_BEHOLD_507RDS_MK3:
	case SAA7134_BOARD_BEHOLD_507RDS_MK5:
639
		ir_codes     = RC_MAP_MANLI;
640 641 642 643 644
		mask_keycode = 0x003f00;
		mask_keyup   = 0x004000;
		polling      = 50; /* ms */
		break;
	case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
645
		ir_codes     = RC_MAP_BEHOLD_COLUMBUS;
646
		mask_keycode = 0x003f00;
647 648 649
		mask_keyup   = 0x004000;
		polling      = 50; // ms
		break;
650
	case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
651
		ir_codes     = RC_MAP_PCTV_SEDNA;
652 653 654 655
		mask_keycode = 0x001f00;
		mask_keyup   = 0x004000;
		polling      = 50; // ms
		break;
656
	case SAA7134_BOARD_GOTVIEW_7135:
657
		ir_codes     = RC_MAP_GOTVIEW7135;
658
		mask_keycode = 0x0003CC;
659
		mask_keydown = 0x000010;
660 661
		polling	     = 5; /* ms */
		saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
662
		break;
L
Linus Torvalds 已提交
663
	case SAA7134_BOARD_VIDEOMATE_TV_PVR:
664
	case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
665
	case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
666
		ir_codes     = RC_MAP_VIDEOMATE_TV_PVR;
L
Linus Torvalds 已提交
667 668 669 670
		mask_keycode = 0x00003F;
		mask_keyup   = 0x400000;
		polling      = 50; // ms
		break;
671
	case SAA7134_BOARD_PROTEUS_2309:
672
		ir_codes     = RC_MAP_PROTEUS_2309;
673 674 675 676
		mask_keycode = 0x00007F;
		mask_keyup   = 0x000080;
		polling      = 50; // ms
		break;
677 678
	case SAA7134_BOARD_VIDEOMATE_DVBT_300:
	case SAA7134_BOARD_VIDEOMATE_DVBT_200:
679
		ir_codes     = RC_MAP_VIDEOMATE_TV_PVR;
680 681 682
		mask_keycode = 0x003F00;
		mask_keyup   = 0x040000;
		break;
683
	case SAA7134_BOARD_FLYDVBS_LR300:
684
	case SAA7134_BOARD_FLYDVBT_LR301:
685
	case SAA7134_BOARD_FLYDVBTDUO:
686
		ir_codes     = RC_MAP_FLYDVB;
687 688 689
		mask_keycode = 0x0001F00;
		mask_keydown = 0x0040000;
		break;
690
	case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
691
	case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
692
	case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
693
		ir_codes     = RC_MAP_ASUS_PC39;
694 695 696 697
		mask_keydown = 0x0040000;	/* Enable GPIO18 line on both edges */
		mask_keyup   = 0x0040000;
		mask_keycode = 0xffff;
		raw_decode   = 1;
698
		break;
699 700
	case SAA7134_BOARD_ENCORE_ENLTV:
	case SAA7134_BOARD_ENCORE_ENLTV_FM:
701
		ir_codes     = RC_MAP_ENCORE_ENLTV;
702 703 704 705
		mask_keycode = 0x00007f;
		mask_keyup   = 0x040000;
		polling      = 50; // ms
		break;
706
	case SAA7134_BOARD_ENCORE_ENLTV_FM53:
707
		ir_codes     = RC_MAP_ENCORE_ENLTV_FM53;
708 709 710 711
		mask_keydown = 0x0040000;	/* Enable GPIO18 line on both edges */
		mask_keyup   = 0x0040000;
		mask_keycode = 0xffff;
		raw_decode   = 1;
712
		break;
713
	case SAA7134_BOARD_10MOONSTVMASTER3:
714
		ir_codes     = RC_MAP_ENCORE_ENLTV;
715 716 717 718
		mask_keycode = 0x5f80000;
		mask_keyup   = 0x8000000;
		polling      = 50; //ms
		break;
719
	case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
720
		ir_codes     = RC_MAP_GENIUS_TVGO_A11MCE;
721 722 723 724
		mask_keycode = 0xff;
		mask_keydown = 0xf00000;
		polling = 50; /* ms */
		break;
725
	case SAA7134_BOARD_REAL_ANGEL_220:
726
		ir_codes     = RC_MAP_REAL_AUDIO_220_32_KEYS;
727 728 729 730
		mask_keycode = 0x3f00;
		mask_keyup   = 0x4000;
		polling = 50; /* ms */
		break;
731
	case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
732
		ir_codes     = RC_MAP_KWORLD_PLUS_TV_ANALOG;
733 734 735
		mask_keycode = 0x7f;
		polling = 40; /* ms */
		break;
736
	case SAA7134_BOARD_VIDEOMATE_S350:
737
		ir_codes     = RC_MAP_VIDEOMATE_S350;
738 739 740
		mask_keycode = 0x003f00;
		mask_keydown = 0x040000;
		break;
741
	case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
742
		ir_codes     = RC_MAP_WINFAST;
743 744
		mask_keycode = 0x5f00;
		mask_keyup   = 0x020000;
745
		polling      = 50; /* ms */
746
		break;
L
Linus Torvalds 已提交
747 748 749 750 751 752 753
	}
	if (NULL == ir_codes) {
		printk("%s: Oops: IR config error [card=%d]\n",
		       dev->name, dev->board);
		return -ENODEV;
	}

754
	ir = kzalloc(sizeof(*ir), GFP_KERNEL);
755 756
	rc = rc_allocate_device();
	if (!ir || !rc) {
757 758
		err = -ENOMEM;
		goto err_out_free;
759
	}
L
Linus Torvalds 已提交
760

761
	ir->dev = rc;
762 763
	dev->remote = ir;

764
	ir->running = false;
765

L
Linus Torvalds 已提交
766 767 768 769
	/* init hardware-specific stuff */
	ir->mask_keycode = mask_keycode;
	ir->mask_keydown = mask_keydown;
	ir->mask_keyup   = mask_keyup;
770
	ir->polling      = polling;
771
	ir->raw_decode	 = raw_decode;
L
Linus Torvalds 已提交
772 773 774 775 776 777 778

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

779 780 781
	rc->priv = dev;
	rc->open = saa7134_ir_open;
	rc->close = saa7134_ir_close;
782
	if (raw_decode)
783
		rc->driver_type = RC_DRIVER_IR_RAW;
784

785 786 787 788
	rc->input_name = ir->name;
	rc->input_phys = ir->phys;
	rc->input_id.bustype = BUS_PCI;
	rc->input_id.version = 1;
L
Linus Torvalds 已提交
789
	if (dev->pci->subsystem_vendor) {
790 791
		rc->input_id.vendor  = dev->pci->subsystem_vendor;
		rc->input_id.product = dev->pci->subsystem_device;
L
Linus Torvalds 已提交
792
	} else {
793 794
		rc->input_id.vendor  = dev->pci->vendor;
		rc->input_id.product = dev->pci->device;
L
Linus Torvalds 已提交
795
	}
796 797 798
	rc->dev.parent = &dev->pci->dev;
	rc->map_name = ir_codes;
	rc->driver_name = MODULE_NAME;
L
Linus Torvalds 已提交
799

800
	err = rc_register_device(rc);
801
	if (err)
802
		goto err_out_free;
L
Linus Torvalds 已提交
803 804

	return 0;
805

806
err_out_free:
807
	rc_free_device(rc);
808 809 810
	dev->remote = NULL;
	kfree(ir);
	return err;
L
Linus Torvalds 已提交
811 812 813 814 815 816 817
}

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

818
	saa7134_ir_stop(dev);
819
	rc_unregister_device(dev->remote->dev);
L
Linus Torvalds 已提交
820 821 822 823
	kfree(dev->remote);
	dev->remote = NULL;
}

824
void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
825
{
826
	struct i2c_board_info info;
827 828 829 830 831 832 833 834 835 836

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

	int rc;

837
	if (disable_ir) {
838
		dprintk("IR has been disabled, not probing for i2c remote\n");
839 840 841
		return;
	}

842
	memset(&info, 0, sizeof(struct i2c_board_info));
843
	memset(&dev->init_data, 0, sizeof(dev->init_data));
844
	strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
845

846 847
	switch (dev->board) {
	case SAA7134_BOARD_PINNACLE_PCTV_110i:
848
	case SAA7134_BOARD_PINNACLE_PCTV_310i:
849
		dev->init_data.name = "Pinnacle PCTV";
850
		if (pinnacle_remote == 0) {
851
			dev->init_data.get_key = get_key_pinnacle_color;
852
			dev->init_data.ir_codes = RC_MAP_PINNACLE_COLOR;
853
			info.addr = 0x47;
854
		} else {
855
			dev->init_data.get_key = get_key_pinnacle_grey;
856
			dev->init_data.ir_codes = RC_MAP_PINNACLE_GREY;
857
			info.addr = 0x47;
858
		}
859 860
		break;
	case SAA7134_BOARD_UPMOST_PURPLE_TV:
861 862
		dev->init_data.name = "Purple TV";
		dev->init_data.get_key = get_key_purpletv;
863
		dev->init_data.ir_codes = RC_MAP_PURPLETV;
864
		info.addr = 0x7a;
865
		break;
866
	case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
867 868
		dev->init_data.name = "MSI TV@nywhere Plus";
		dev->init_data.get_key = get_key_msi_tvanywhere_plus;
869
		dev->init_data.ir_codes = RC_MAP_MSI_TVANYWHERE_PLUS;
870 871 872 873 874
		/*
		 * MSI TV@nyware Plus requires more frequent polling
		 * otherwise it will miss some keypresses
		 */
		dev->init_data.polling_interval = 50;
875
		info.addr = 0x30;
876 877 878 879 880
		/* 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);
881
		dprintk("probe 0x%02x @ %s: %s\n",
882 883
			msg_msi.addr, dev->i2c_adap.name,
			(1 == rc) ? "yes" : "no");
884
		break;
885
	case SAA7134_BOARD_HAUPPAUGE_HVR1110:
886 887
		dev->init_data.name = "HVR 1110";
		dev->init_data.get_key = get_key_hvr1110;
888
		dev->init_data.ir_codes = RC_MAP_HAUPPAUGE_NEW;
889
		info.addr = 0x71;
890
		break;
891 892 893 894 895 896 897 898
	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:
899
	case SAA7134_BOARD_BEHOLD_M6:
900 901
	case SAA7134_BOARD_BEHOLD_M63:
	case SAA7134_BOARD_BEHOLD_M6_EXTRA:
902
	case SAA7134_BOARD_BEHOLD_H6:
903
	case SAA7134_BOARD_BEHOLD_X7:
904 905
	case SAA7134_BOARD_BEHOLD_H7:
	case SAA7134_BOARD_BEHOLD_A7:
906 907
		dev->init_data.name = "BeholdTV";
		dev->init_data.get_key = get_key_beholdm6xx;
908
		dev->init_data.ir_codes = RC_MAP_BEHOLD;
909
		dev->init_data.type = RC_TYPE_NEC;
910
		info.addr = 0x2d;
911
		break;
912 913
	case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
	case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
914
		info.addr = 0x40;
915
		break;
916 917 918
	case SAA7134_BOARD_FLYDVB_TRIO:
		dev->init_data.name = "FlyDVB Trio";
		dev->init_data.get_key = get_key_flydvb_trio;
919
		dev->init_data.ir_codes = RC_MAP_FLYDVB;
920 921
		info.addr = 0x0b;
		break;
922 923 924
	default:
		dprintk("No I2C IR support for board %x\n", dev->board);
		return;
925 926
	}

927
	if (dev->init_data.name)
928
		info.platform_data = &dev->init_data;
929
	i2c_new_device(&dev->i2c_adap, &info);
930
}
931

932 933
static int saa7134_raw_decode_irq(struct saa7134_dev *dev)
{
934
	struct saa7134_card_ir	*ir = dev->remote;
935
	unsigned long 	timeout;
936
	int space;
937 938 939 940

	/* Generate initial event */
	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
941
	space = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
942
	ir_raw_event_store_edge(dev->remote->dev, space ? IR_SPACE : IR_PULSE);
943 944


945 946 947 948 949 950 951 952
	/*
	 * 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);
953
		ir->active = true;
954
	}
955 956 957

	return 1;
}