1 /*
2  *
3  * handle saa7134 IR remotes via linux kernel input layer.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16 
17 #include "saa7134.h"
18 #include "saa7134-reg.h"
19 
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/interrupt.h>
24 #include <linux/slab.h>
25 
26 #define MODULE_NAME "saa7134"
27 
28 static unsigned int disable_ir;
29 module_param(disable_ir, int, 0444);
30 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
31 
32 static unsigned int ir_debug;
33 module_param(ir_debug, int, 0644);
34 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
35 
36 static int pinnacle_remote;
37 module_param(pinnacle_remote, int, 0644);    /* Choose Pinnacle PCTV remote */
38 MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
39 
40 #define input_dbg(fmt, arg...) do { \
41 	if (ir_debug) \
42 		printk(KERN_DEBUG pr_fmt("input: " fmt), ## arg); \
43 	} while (0)
44 #define ir_dbg(ir, fmt, arg...) do { \
45 	if (ir_debug) \
46 		printk(KERN_DEBUG pr_fmt("ir %s: " fmt), ir->name, ## arg); \
47 	} while (0)
48 
49 /* Helper function for raw decoding at GPIO16 or GPIO18 */
50 static int saa7134_raw_decode_irq(struct saa7134_dev *dev);
51 
52 /* -------------------- GPIO generic keycode builder -------------------- */
53 
54 static int build_key(struct saa7134_dev *dev)
55 {
56 	struct saa7134_card_ir *ir = dev->remote;
57 	u32 gpio, data;
58 
59 	/* here comes the additional handshake steps for some cards */
60 	switch (dev->board) {
61 	case SAA7134_BOARD_GOTVIEW_7135:
62 		saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
63 		saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
64 		break;
65 	}
66 	/* rising SAA7134_GPIO_GPRESCAN reads the status */
67 	saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
68 	saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
69 
70 	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
71 	if (ir->polling) {
72 		if (ir->last_gpio == gpio)
73 			return 0;
74 		ir->last_gpio = gpio;
75 	}
76 
77 	data = ir_extract_bits(gpio, ir->mask_keycode);
78 	input_dbg("build_key gpio=0x%x mask=0x%x data=%d\n",
79 		gpio, ir->mask_keycode, data);
80 
81 	switch (dev->board) {
82 	case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
83 		if (data == ir->mask_keycode)
84 			rc_keyup(ir->dev);
85 		else
86 			rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
87 					     0);
88 		return 0;
89 	}
90 
91 	if (ir->polling) {
92 		if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
93 		    (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
94 			rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
95 					     0);
96 		} else {
97 			rc_keyup(ir->dev);
98 		}
99 	}
100 	else {	/* IRQ driven mode - handle key press and release in one go */
101 		if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
102 		    (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
103 			rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
104 					     0);
105 			rc_keyup(ir->dev);
106 		}
107 	}
108 
109 	return 0;
110 }
111 
112 /* --------------------- Chip specific I2C key builders ----------------- */
113 
114 static int get_key_flydvb_trio(struct IR_i2c *ir, enum rc_proto *protocol,
115 			       u32 *scancode, u8 *toggle)
116 {
117 	int gpio;
118 	int attempt = 0;
119 	unsigned char b;
120 
121 	/* We need this to access GPI Used by the saa_readl macro. */
122 	struct saa7134_dev *dev = ir->c->adapter->algo_data;
123 
124 	if (dev == NULL) {
125 		ir_dbg(ir, "get_key_flydvb_trio: ir->c->adapter->algo_data is NULL!\n");
126 		return -EIO;
127 	}
128 
129 	/* rising SAA7134_GPIGPRESCAN reads the status */
130 	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
131 	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
132 
133 	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
134 
135 	if (0x40000 & ~gpio)
136 		return 0; /* No button press */
137 
138 	/* poll IR chip */
139 	/* weak up the IR chip */
140 	b = 0;
141 
142 	while (1 != i2c_master_send(ir->c, &b, 1)) {
143 		if ((attempt++) < 10) {
144 			/*
145 			 * wait a bit for next attempt -
146 			 * I don't know how make it better
147 			 */
148 			msleep(10);
149 			continue;
150 		}
151 		ir_dbg(ir, "send wake up byte to pic16C505 (IR chip)failed %dx\n",
152 		       attempt);
153 		return -EIO;
154 	}
155 	if (1 != i2c_master_recv(ir->c, &b, 1)) {
156 		ir_dbg(ir, "read error\n");
157 		return -EIO;
158 	}
159 
160 	*protocol = RC_PROTO_UNKNOWN;
161 	*scancode = b;
162 	*toggle = 0;
163 	return 1;
164 }
165 
166 static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir,
167 				       enum rc_proto *protocol,
168 				       u32 *scancode, u8 *toggle)
169 {
170 	unsigned char b;
171 	int gpio;
172 
173 	/* <dev> is needed to access GPIO. Used by the saa_readl macro. */
174 	struct saa7134_dev *dev = ir->c->adapter->algo_data;
175 	if (dev == NULL) {
176 		ir_dbg(ir, "get_key_msi_tvanywhere_plus: ir->c->adapter->algo_data is NULL!\n");
177 		return -EIO;
178 	}
179 
180 	/* rising SAA7134_GPIO_GPRESCAN reads the status */
181 
182 	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
183 	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
184 
185 	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
186 
187 	/* GPIO&0x40 is pulsed low when a button is pressed. Don't do
188 	   I2C receive if gpio&0x40 is not low. */
189 
190 	if (gpio & 0x40)
191 		return 0;       /* No button press */
192 
193 	/* GPIO says there is a button press. Get it. */
194 
195 	if (1 != i2c_master_recv(ir->c, &b, 1)) {
196 		ir_dbg(ir, "read error\n");
197 		return -EIO;
198 	}
199 
200 	/* No button press */
201 
202 	if (b == 0xff)
203 		return 0;
204 
205 	/* Button pressed */
206 
207 	input_dbg("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
208 	*protocol = RC_PROTO_UNKNOWN;
209 	*scancode = b;
210 	*toggle = 0;
211 	return 1;
212 }
213 
214 /* copied and modified from get_key_msi_tvanywhere_plus() */
215 static int get_key_kworld_pc150u(struct IR_i2c *ir, enum rc_proto *protocol,
216 				 u32 *scancode, u8 *toggle)
217 {
218 	unsigned char b;
219 	unsigned int gpio;
220 
221 	/* <dev> is needed to access GPIO. Used by the saa_readl macro. */
222 	struct saa7134_dev *dev = ir->c->adapter->algo_data;
223 	if (dev == NULL) {
224 		ir_dbg(ir, "get_key_kworld_pc150u: ir->c->adapter->algo_data is NULL!\n");
225 		return -EIO;
226 	}
227 
228 	/* rising SAA7134_GPIO_GPRESCAN reads the status */
229 
230 	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
231 	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
232 
233 	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
234 
235 	/* GPIO&0x100 is pulsed low when a button is pressed. Don't do
236 	   I2C receive if gpio&0x100 is not low. */
237 
238 	if (gpio & 0x100)
239 		return 0;       /* No button press */
240 
241 	/* GPIO says there is a button press. Get it. */
242 
243 	if (1 != i2c_master_recv(ir->c, &b, 1)) {
244 		ir_dbg(ir, "read error\n");
245 		return -EIO;
246 	}
247 
248 	/* No button press */
249 
250 	if (b == 0xff)
251 		return 0;
252 
253 	/* Button pressed */
254 
255 	input_dbg("get_key_kworld_pc150u: Key = 0x%02X\n", b);
256 	*protocol = RC_PROTO_UNKNOWN;
257 	*scancode = b;
258 	*toggle = 0;
259 	return 1;
260 }
261 
262 static int get_key_purpletv(struct IR_i2c *ir, enum rc_proto *protocol,
263 			    u32 *scancode, u8 *toggle)
264 {
265 	unsigned char b;
266 
267 	/* poll IR chip */
268 	if (1 != i2c_master_recv(ir->c, &b, 1)) {
269 		ir_dbg(ir, "read error\n");
270 		return -EIO;
271 	}
272 
273 	/* no button press */
274 	if (b==0)
275 		return 0;
276 
277 	/* repeating */
278 	if (b & 0x80)
279 		return 1;
280 
281 	*protocol = RC_PROTO_UNKNOWN;
282 	*scancode = b;
283 	*toggle = 0;
284 	return 1;
285 }
286 
287 static int get_key_hvr1110(struct IR_i2c *ir, enum rc_proto *protocol,
288 			   u32 *scancode, u8 *toggle)
289 {
290 	unsigned char buf[5];
291 
292 	/* poll IR chip */
293 	if (5 != i2c_master_recv(ir->c, buf, 5))
294 		return -EIO;
295 
296 	/* Check if some key were pressed */
297 	if (!(buf[0] & 0x80))
298 		return 0;
299 
300 	/*
301 	 * buf[3] & 0x80 is always high.
302 	 * buf[3] & 0x40 is a parity bit. A repeat event is marked
303 	 * by preserving it into two separate readings
304 	 * buf[4] bits 0 and 1, and buf[1] and buf[2] are always
305 	 * zero.
306 	 *
307 	 * Note that the keymap which the hvr1110 uses is RC5.
308 	 *
309 	 * FIXME: start bits could maybe be used...?
310 	 */
311 	*protocol = RC_PROTO_RC5;
312 	*scancode = RC_SCANCODE_RC5(buf[3] & 0x1f, buf[4] >> 2);
313 	*toggle = !!(buf[3] & 0x40);
314 	return 1;
315 }
316 
317 
318 static int get_key_beholdm6xx(struct IR_i2c *ir, enum rc_proto *protocol,
319 			      u32 *scancode, u8 *toggle)
320 {
321 	unsigned char data[12];
322 	u32 gpio;
323 
324 	struct saa7134_dev *dev = ir->c->adapter->algo_data;
325 
326 	/* rising SAA7134_GPIO_GPRESCAN reads the status */
327 	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
328 	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
329 
330 	gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
331 
332 	if (0x400000 & ~gpio)
333 		return 0; /* No button press */
334 
335 	ir->c->addr = 0x5a >> 1;
336 
337 	if (12 != i2c_master_recv(ir->c, data, 12)) {
338 		ir_dbg(ir, "read error\n");
339 		return -EIO;
340 	}
341 
342 	if (data[9] != (unsigned char)(~data[8]))
343 		return 0;
344 
345 	*protocol = RC_PROTO_NECX;
346 	*scancode = RC_SCANCODE_NECX(data[11] << 8 | data[10], data[9]);
347 	*toggle = 0;
348 	return 1;
349 }
350 
351 /* Common (grey or coloured) pinnacle PCTV remote handling
352  *
353  */
354 static int get_key_pinnacle(struct IR_i2c *ir, enum rc_proto *protocol,
355 			    u32 *scancode, u8 *toggle, int parity_offset,
356 			    int marker, int code_modulo)
357 {
358 	unsigned char b[4];
359 	unsigned int start = 0,parity = 0,code = 0;
360 
361 	/* poll IR chip */
362 	if (4 != i2c_master_recv(ir->c, b, 4)) {
363 		ir_dbg(ir, "read error\n");
364 		return -EIO;
365 	}
366 
367 	for (start = 0; start < ARRAY_SIZE(b); start++) {
368 		if (b[start] == marker) {
369 			code=b[(start+parity_offset + 1) % 4];
370 			parity=b[(start+parity_offset) % 4];
371 		}
372 	}
373 
374 	/* Empty Request */
375 	if (parity == 0)
376 		return 0;
377 
378 	/* Repeating... */
379 	if (ir->old == parity)
380 		return 0;
381 
382 	ir->old = parity;
383 
384 	/* drop special codes when a key is held down a long time for the grey controller
385 	   In this case, the second bit of the code is asserted */
386 	if (marker == 0xfe && (code & 0x40))
387 		return 0;
388 
389 	code %= code_modulo;
390 
391 	*protocol = RC_PROTO_UNKNOWN;
392 	*scancode = code;
393 	*toggle = 0;
394 
395 	ir_dbg(ir, "Pinnacle PCTV key %02x\n", code);
396 	return 1;
397 }
398 
399 /* The grey pinnacle PCTV remote
400  *
401  *  There are one issue with this remote:
402  *   - I2c packet does not change when the same key is pressed quickly. The workaround
403  *     is to hold down each key for about half a second, so that another code is generated
404  *     in the i2c packet, and the function can distinguish key presses.
405  *
406  * Sylvain Pasche <sylvain.pasche@gmail.com>
407  */
408 static int get_key_pinnacle_grey(struct IR_i2c *ir, enum rc_proto *protocol,
409 				 u32 *scancode, u8 *toggle)
410 {
411 
412 	return get_key_pinnacle(ir, protocol, scancode, toggle, 1, 0xfe, 0xff);
413 }
414 
415 
416 /* The new pinnacle PCTV remote (with the colored buttons)
417  *
418  * Ricardo Cerqueira <v4l@cerqueira.org>
419  */
420 static int get_key_pinnacle_color(struct IR_i2c *ir, enum rc_proto *protocol,
421 				  u32 *scancode, u8 *toggle)
422 {
423 	/* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
424 	 *
425 	 * this is the only value that results in 42 unique
426 	 * codes < 128
427 	 */
428 
429 	return get_key_pinnacle(ir, protocol, scancode, toggle, 2, 0x80, 0x88);
430 }
431 
432 void saa7134_input_irq(struct saa7134_dev *dev)
433 {
434 	struct saa7134_card_ir *ir;
435 
436 	if (!dev || !dev->remote)
437 		return;
438 
439 	ir = dev->remote;
440 	if (!ir->running)
441 		return;
442 
443 	if (!ir->polling && !ir->raw_decode) {
444 		build_key(dev);
445 	} else if (ir->raw_decode) {
446 		saa7134_raw_decode_irq(dev);
447 	}
448 }
449 
450 static void saa7134_input_timer(struct timer_list *t)
451 {
452 	struct saa7134_card_ir *ir = from_timer(ir, t, timer);
453 	struct saa7134_dev *dev = ir->dev->priv;
454 
455 	build_key(dev);
456 	mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
457 }
458 
459 static int __saa7134_ir_start(void *priv)
460 {
461 	struct saa7134_dev *dev = priv;
462 	struct saa7134_card_ir *ir;
463 
464 	if (!dev || !dev->remote)
465 		return -EINVAL;
466 
467 	ir  = dev->remote;
468 	if (ir->running)
469 		return 0;
470 
471 	/* Moved here from saa7134_input_init1() because the latter
472 	 * is not called on device resume */
473 	switch (dev->board) {
474 	case SAA7134_BOARD_MD2819:
475 	case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
476 	case SAA7134_BOARD_AVERMEDIA_305:
477 	case SAA7134_BOARD_AVERMEDIA_307:
478 	case SAA7134_BOARD_AVERMEDIA_505:
479 	case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
480 	case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
481 	case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
482 	case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
483 	case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
484 	case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
485 	case SAA7134_BOARD_AVERMEDIA_M102:
486 	case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
487 		/* Without this we won't receive key up events */
488 		saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
489 		saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
490 		break;
491 	case SAA7134_BOARD_AVERMEDIA_777:
492 	case SAA7134_BOARD_AVERMEDIA_A16AR:
493 		/* Without this we won't receive key up events */
494 		saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
495 		saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
496 		break;
497 	case SAA7134_BOARD_AVERMEDIA_A16D:
498 		/* Without this we won't receive key up events */
499 		saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
500 		saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
501 		break;
502 	case SAA7134_BOARD_GOTVIEW_7135:
503 		saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
504 		break;
505 	}
506 
507 	ir->running = true;
508 
509 	if (ir->polling) {
510 		timer_setup(&ir->timer, saa7134_input_timer, 0);
511 		ir->timer.expires = jiffies + HZ;
512 		add_timer(&ir->timer);
513 	}
514 
515 	return 0;
516 }
517 
518 static void __saa7134_ir_stop(void *priv)
519 {
520 	struct saa7134_dev *dev = priv;
521 	struct saa7134_card_ir *ir;
522 
523 	if (!dev || !dev->remote)
524 		return;
525 
526 	ir  = dev->remote;
527 	if (!ir->running)
528 		return;
529 
530 	if (ir->polling)
531 		del_timer_sync(&ir->timer);
532 
533 	ir->running = false;
534 
535 	return;
536 }
537 
538 int saa7134_ir_start(struct saa7134_dev *dev)
539 {
540 	if (dev->remote->users)
541 		return __saa7134_ir_start(dev);
542 
543 	return 0;
544 }
545 
546 void saa7134_ir_stop(struct saa7134_dev *dev)
547 {
548 	if (dev->remote->users)
549 		__saa7134_ir_stop(dev);
550 }
551 
552 static int saa7134_ir_open(struct rc_dev *rc)
553 {
554 	struct saa7134_dev *dev = rc->priv;
555 
556 	dev->remote->users++;
557 	return __saa7134_ir_start(dev);
558 }
559 
560 static void saa7134_ir_close(struct rc_dev *rc)
561 {
562 	struct saa7134_dev *dev = rc->priv;
563 
564 	dev->remote->users--;
565 	if (!dev->remote->users)
566 		__saa7134_ir_stop(dev);
567 }
568 
569 int saa7134_input_init1(struct saa7134_dev *dev)
570 {
571 	struct saa7134_card_ir *ir;
572 	struct rc_dev *rc;
573 	char *ir_codes = NULL;
574 	u32 mask_keycode = 0;
575 	u32 mask_keydown = 0;
576 	u32 mask_keyup   = 0;
577 	unsigned polling = 0;
578 	bool raw_decode  = false;
579 	int err;
580 
581 	if (dev->has_remote != SAA7134_REMOTE_GPIO)
582 		return -ENODEV;
583 	if (disable_ir)
584 		return -ENODEV;
585 
586 	/* detect & configure */
587 	switch (dev->board) {
588 	case SAA7134_BOARD_FLYVIDEO2000:
589 	case SAA7134_BOARD_FLYVIDEO3000:
590 	case SAA7134_BOARD_FLYTVPLATINUM_FM:
591 	case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
592 	case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
593 		ir_codes     = RC_MAP_FLYVIDEO;
594 		mask_keycode = 0xEC00000;
595 		mask_keydown = 0x0040000;
596 		break;
597 	case SAA7134_BOARD_CINERGY400:
598 	case SAA7134_BOARD_CINERGY600:
599 	case SAA7134_BOARD_CINERGY600_MK3:
600 		ir_codes     = RC_MAP_CINERGY;
601 		mask_keycode = 0x00003f;
602 		mask_keyup   = 0x040000;
603 		break;
604 	case SAA7134_BOARD_ECS_TVP3XP:
605 	case SAA7134_BOARD_ECS_TVP3XP_4CB5:
606 		ir_codes     = RC_MAP_EZTV;
607 		mask_keycode = 0x00017c;
608 		mask_keyup   = 0x000002;
609 		polling      = 50; // ms
610 		break;
611 	case SAA7134_BOARD_KWORLD_XPERT:
612 	case SAA7134_BOARD_AVACSSMARTTV:
613 		ir_codes     = RC_MAP_PIXELVIEW;
614 		mask_keycode = 0x00001F;
615 		mask_keyup   = 0x000020;
616 		polling      = 50; // ms
617 		break;
618 	case SAA7134_BOARD_MD2819:
619 	case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
620 	case SAA7134_BOARD_AVERMEDIA_305:
621 	case SAA7134_BOARD_AVERMEDIA_307:
622 	case SAA7134_BOARD_AVERMEDIA_505:
623 	case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
624 	case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
625 	case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
626 	case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
627 	case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
628 	case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
629 	case SAA7134_BOARD_AVERMEDIA_M102:
630 	case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
631 		ir_codes     = RC_MAP_AVERMEDIA;
632 		mask_keycode = 0x0007C8;
633 		mask_keydown = 0x000010;
634 		polling      = 50; // ms
635 		/* GPIO stuff moved to __saa7134_ir_start() */
636 		break;
637 	case SAA7134_BOARD_AVERMEDIA_M135A:
638 		ir_codes     = RC_MAP_AVERMEDIA_M135A;
639 		mask_keydown = 0x0040000;	/* Enable GPIO18 line on both edges */
640 		mask_keyup   = 0x0040000;
641 		mask_keycode = 0xffff;
642 		raw_decode   = true;
643 		break;
644 	case SAA7134_BOARD_AVERMEDIA_M733A:
645 		ir_codes     = RC_MAP_AVERMEDIA_M733A_RM_K6;
646 		mask_keydown = 0x0040000;
647 		mask_keyup   = 0x0040000;
648 		mask_keycode = 0xffff;
649 		raw_decode   = true;
650 		break;
651 	case SAA7134_BOARD_AVERMEDIA_777:
652 	case SAA7134_BOARD_AVERMEDIA_A16AR:
653 		ir_codes     = RC_MAP_AVERMEDIA;
654 		mask_keycode = 0x02F200;
655 		mask_keydown = 0x000400;
656 		polling      = 50; // ms
657 		/* GPIO stuff moved to __saa7134_ir_start() */
658 		break;
659 	case SAA7134_BOARD_AVERMEDIA_A16D:
660 		ir_codes     = RC_MAP_AVERMEDIA_A16D;
661 		mask_keycode = 0x02F200;
662 		mask_keydown = 0x000400;
663 		polling      = 50; /* ms */
664 		/* GPIO stuff moved to __saa7134_ir_start() */
665 		break;
666 	case SAA7134_BOARD_KWORLD_TERMINATOR:
667 		ir_codes     = RC_MAP_PIXELVIEW;
668 		mask_keycode = 0x00001f;
669 		mask_keyup   = 0x000060;
670 		polling      = 50; // ms
671 		break;
672 	case SAA7134_BOARD_MANLI_MTV001:
673 	case SAA7134_BOARD_MANLI_MTV002:
674 		ir_codes     = RC_MAP_MANLI;
675 		mask_keycode = 0x001f00;
676 		mask_keyup   = 0x004000;
677 		polling      = 50; /* ms */
678 		break;
679 	case SAA7134_BOARD_BEHOLD_409FM:
680 	case SAA7134_BOARD_BEHOLD_401:
681 	case SAA7134_BOARD_BEHOLD_403:
682 	case SAA7134_BOARD_BEHOLD_403FM:
683 	case SAA7134_BOARD_BEHOLD_405:
684 	case SAA7134_BOARD_BEHOLD_405FM:
685 	case SAA7134_BOARD_BEHOLD_407:
686 	case SAA7134_BOARD_BEHOLD_407FM:
687 	case SAA7134_BOARD_BEHOLD_409:
688 	case SAA7134_BOARD_BEHOLD_505FM:
689 	case SAA7134_BOARD_BEHOLD_505RDS_MK5:
690 	case SAA7134_BOARD_BEHOLD_505RDS_MK3:
691 	case SAA7134_BOARD_BEHOLD_507_9FM:
692 	case SAA7134_BOARD_BEHOLD_507RDS_MK3:
693 	case SAA7134_BOARD_BEHOLD_507RDS_MK5:
694 		ir_codes     = RC_MAP_MANLI;
695 		mask_keycode = 0x003f00;
696 		mask_keyup   = 0x004000;
697 		polling      = 50; /* ms */
698 		break;
699 	case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
700 		ir_codes     = RC_MAP_BEHOLD_COLUMBUS;
701 		mask_keycode = 0x003f00;
702 		mask_keyup   = 0x004000;
703 		polling      = 50; // ms
704 		break;
705 	case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
706 		ir_codes     = RC_MAP_PCTV_SEDNA;
707 		mask_keycode = 0x001f00;
708 		mask_keyup   = 0x004000;
709 		polling      = 50; // ms
710 		break;
711 	case SAA7134_BOARD_GOTVIEW_7135:
712 		ir_codes     = RC_MAP_GOTVIEW7135;
713 		mask_keycode = 0x0003CC;
714 		mask_keydown = 0x000010;
715 		polling	     = 5; /* ms */
716 		/* GPIO stuff moved to __saa7134_ir_start() */
717 		break;
718 	case SAA7134_BOARD_VIDEOMATE_TV_PVR:
719 	case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
720 	case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
721 		ir_codes     = RC_MAP_VIDEOMATE_TV_PVR;
722 		mask_keycode = 0x00003F;
723 		mask_keyup   = 0x400000;
724 		polling      = 50; // ms
725 		break;
726 	case SAA7134_BOARD_PROTEUS_2309:
727 		ir_codes     = RC_MAP_PROTEUS_2309;
728 		mask_keycode = 0x00007F;
729 		mask_keyup   = 0x000080;
730 		polling      = 50; // ms
731 		break;
732 	case SAA7134_BOARD_VIDEOMATE_DVBT_300:
733 	case SAA7134_BOARD_VIDEOMATE_DVBT_200:
734 		ir_codes     = RC_MAP_VIDEOMATE_TV_PVR;
735 		mask_keycode = 0x003F00;
736 		mask_keyup   = 0x040000;
737 		break;
738 	case SAA7134_BOARD_FLYDVBS_LR300:
739 	case SAA7134_BOARD_FLYDVBT_LR301:
740 	case SAA7134_BOARD_FLYDVBTDUO:
741 		ir_codes     = RC_MAP_FLYDVB;
742 		mask_keycode = 0x0001F00;
743 		mask_keydown = 0x0040000;
744 		break;
745 	case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
746 	case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
747 	case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
748 		ir_codes     = RC_MAP_ASUS_PC39;
749 		mask_keydown = 0x0040000;	/* Enable GPIO18 line on both edges */
750 		mask_keyup   = 0x0040000;
751 		mask_keycode = 0xffff;
752 		raw_decode   = true;
753 		break;
754 	case SAA7134_BOARD_ASUSTeK_PS3_100:
755 		ir_codes     = RC_MAP_ASUS_PS3_100;
756 		mask_keydown = 0x0040000;
757 		mask_keyup   = 0x0040000;
758 		mask_keycode = 0xffff;
759 		raw_decode   = true;
760 		break;
761 	case SAA7134_BOARD_ENCORE_ENLTV:
762 	case SAA7134_BOARD_ENCORE_ENLTV_FM:
763 		ir_codes     = RC_MAP_ENCORE_ENLTV;
764 		mask_keycode = 0x00007f;
765 		mask_keyup   = 0x040000;
766 		polling      = 50; // ms
767 		break;
768 	case SAA7134_BOARD_ENCORE_ENLTV_FM53:
769 	case SAA7134_BOARD_ENCORE_ENLTV_FM3:
770 		ir_codes     = RC_MAP_ENCORE_ENLTV_FM53;
771 		mask_keydown = 0x0040000;	/* Enable GPIO18 line on both edges */
772 		mask_keyup   = 0x0040000;
773 		mask_keycode = 0xffff;
774 		raw_decode   = true;
775 		break;
776 	case SAA7134_BOARD_10MOONSTVMASTER3:
777 		ir_codes     = RC_MAP_ENCORE_ENLTV;
778 		mask_keycode = 0x5f80000;
779 		mask_keyup   = 0x8000000;
780 		polling      = 50; //ms
781 		break;
782 	case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
783 		ir_codes     = RC_MAP_GENIUS_TVGO_A11MCE;
784 		mask_keycode = 0xff;
785 		mask_keydown = 0xf00000;
786 		polling = 50; /* ms */
787 		break;
788 	case SAA7134_BOARD_REAL_ANGEL_220:
789 		ir_codes     = RC_MAP_REAL_AUDIO_220_32_KEYS;
790 		mask_keycode = 0x3f00;
791 		mask_keyup   = 0x4000;
792 		polling = 50; /* ms */
793 		break;
794 	case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
795 		ir_codes     = RC_MAP_KWORLD_PLUS_TV_ANALOG;
796 		mask_keycode = 0x7f;
797 		polling = 40; /* ms */
798 		break;
799 	case SAA7134_BOARD_VIDEOMATE_S350:
800 		ir_codes     = RC_MAP_VIDEOMATE_S350;
801 		mask_keycode = 0x003f00;
802 		mask_keydown = 0x040000;
803 		break;
804 	case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
805 		ir_codes     = RC_MAP_WINFAST;
806 		mask_keycode = 0x5f00;
807 		mask_keyup   = 0x020000;
808 		polling      = 50; /* ms */
809 		break;
810 	case SAA7134_BOARD_VIDEOMATE_M1F:
811 		ir_codes     = RC_MAP_VIDEOMATE_K100;
812 		mask_keycode = 0x0ff00;
813 		mask_keyup   = 0x040000;
814 		break;
815 	case SAA7134_BOARD_HAUPPAUGE_HVR1150:
816 	case SAA7134_BOARD_HAUPPAUGE_HVR1120:
817 		ir_codes     = RC_MAP_HAUPPAUGE;
818 		mask_keydown = 0x0040000;	/* Enable GPIO18 line on both edges */
819 		mask_keyup   = 0x0040000;
820 		mask_keycode = 0xffff;
821 		raw_decode   = true;
822 		break;
823 	case SAA7134_BOARD_LEADTEK_WINFAST_TV2100_FM:
824 		ir_codes     = RC_MAP_LEADTEK_Y04G0051;
825 		mask_keydown = 0x0040000;	/* Enable GPIO18 line on both edges */
826 		mask_keyup   = 0x0040000;
827 		mask_keycode = 0xffff;
828 		raw_decode   = true;
829 		break;
830 	}
831 	if (NULL == ir_codes) {
832 		pr_err("Oops: IR config error [card=%d]\n", dev->board);
833 		return -ENODEV;
834 	}
835 
836 	ir = kzalloc(sizeof(*ir), GFP_KERNEL);
837 	rc = rc_allocate_device(RC_DRIVER_SCANCODE);
838 	if (!ir || !rc) {
839 		err = -ENOMEM;
840 		goto err_out_free;
841 	}
842 
843 	ir->dev = rc;
844 	dev->remote = ir;
845 
846 	/* init hardware-specific stuff */
847 	ir->mask_keycode = mask_keycode;
848 	ir->mask_keydown = mask_keydown;
849 	ir->mask_keyup   = mask_keyup;
850 	ir->polling      = polling;
851 	ir->raw_decode	 = raw_decode;
852 
853 	/* init input device */
854 	snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
855 		 saa7134_boards[dev->board].name);
856 	snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
857 		 pci_name(dev->pci));
858 
859 	rc->priv = dev;
860 	rc->open = saa7134_ir_open;
861 	rc->close = saa7134_ir_close;
862 	if (raw_decode) {
863 		rc->driver_type = RC_DRIVER_IR_RAW;
864 		rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
865 	}
866 
867 	rc->device_name = ir->name;
868 	rc->input_phys = ir->phys;
869 	rc->input_id.bustype = BUS_PCI;
870 	rc->input_id.version = 1;
871 	if (dev->pci->subsystem_vendor) {
872 		rc->input_id.vendor  = dev->pci->subsystem_vendor;
873 		rc->input_id.product = dev->pci->subsystem_device;
874 	} else {
875 		rc->input_id.vendor  = dev->pci->vendor;
876 		rc->input_id.product = dev->pci->device;
877 	}
878 	rc->dev.parent = &dev->pci->dev;
879 	rc->map_name = ir_codes;
880 	rc->driver_name = MODULE_NAME;
881 	rc->min_timeout = 1;
882 	rc->timeout = IR_DEFAULT_TIMEOUT;
883 	rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
884 
885 	err = rc_register_device(rc);
886 	if (err)
887 		goto err_out_free;
888 
889 	return 0;
890 
891 err_out_free:
892 	rc_free_device(rc);
893 	dev->remote = NULL;
894 	kfree(ir);
895 	return err;
896 }
897 
898 void saa7134_input_fini(struct saa7134_dev *dev)
899 {
900 	if (NULL == dev->remote)
901 		return;
902 
903 	saa7134_ir_stop(dev);
904 	rc_unregister_device(dev->remote->dev);
905 	kfree(dev->remote);
906 	dev->remote = NULL;
907 }
908 
909 void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
910 {
911 	struct i2c_board_info info;
912 	struct i2c_msg msg_msi = {
913 		.addr = 0x50,
914 		.flags = I2C_M_RD,
915 		.len = 0,
916 		.buf = NULL,
917 	};
918 	int rc;
919 
920 	if (disable_ir) {
921 		input_dbg("IR has been disabled, not probing for i2c remote\n");
922 		return;
923 	}
924 
925 	memset(&info, 0, sizeof(struct i2c_board_info));
926 	memset(&dev->init_data, 0, sizeof(dev->init_data));
927 	strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
928 
929 	switch (dev->board) {
930 	case SAA7134_BOARD_PINNACLE_PCTV_110i:
931 	case SAA7134_BOARD_PINNACLE_PCTV_310i:
932 		dev->init_data.name = "Pinnacle PCTV";
933 		if (pinnacle_remote == 0) {
934 			dev->init_data.get_key = get_key_pinnacle_color;
935 			dev->init_data.ir_codes = RC_MAP_PINNACLE_COLOR;
936 			info.addr = 0x47;
937 		} else {
938 			dev->init_data.get_key = get_key_pinnacle_grey;
939 			dev->init_data.ir_codes = RC_MAP_PINNACLE_GREY;
940 			info.addr = 0x47;
941 		}
942 		break;
943 	case SAA7134_BOARD_UPMOST_PURPLE_TV:
944 		dev->init_data.name = "Purple TV";
945 		dev->init_data.get_key = get_key_purpletv;
946 		dev->init_data.ir_codes = RC_MAP_PURPLETV;
947 		info.addr = 0x7a;
948 		break;
949 	case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
950 		dev->init_data.name = "MSI TV@nywhere Plus";
951 		dev->init_data.get_key = get_key_msi_tvanywhere_plus;
952 		dev->init_data.ir_codes = RC_MAP_MSI_TVANYWHERE_PLUS;
953 		/*
954 		 * MSI TV@nyware Plus requires more frequent polling
955 		 * otherwise it will miss some keypresses
956 		 */
957 		dev->init_data.polling_interval = 50;
958 		info.addr = 0x30;
959 		/* MSI TV@nywhere Plus controller doesn't seem to
960 		   respond to probes unless we read something from
961 		   an existing device. Weird...
962 		   REVISIT: might no longer be needed */
963 		rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
964 		input_dbg("probe 0x%02x @ %s: %s\n",
965 			msg_msi.addr, dev->i2c_adap.name,
966 			(1 == rc) ? "yes" : "no");
967 		break;
968 	case SAA7134_BOARD_SNAZIO_TVPVR_PRO:
969 		dev->init_data.name = "SnaZio* TVPVR PRO";
970 		dev->init_data.get_key = get_key_msi_tvanywhere_plus;
971 		dev->init_data.ir_codes = RC_MAP_MSI_TVANYWHERE_PLUS;
972 		/*
973 		 * MSI TV@nyware Plus requires more frequent polling
974 		 * otherwise it will miss some keypresses
975 		 */
976 		dev->init_data.polling_interval = 50;
977 		info.addr = 0x30;
978 		/*
979 		 * MSI TV@nywhere Plus controller doesn't seem to
980 		 *  respond to probes unless we read something from
981 		 *  an existing device. Weird...
982 		 * REVISIT: might no longer be needed
983 		 */
984 		rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
985 		input_dbg("probe 0x%02x @ %s: %s\n",
986 			msg_msi.addr, dev->i2c_adap.name,
987 			(rc == 1) ? "yes" : "no");
988 		break;
989 	case SAA7134_BOARD_KWORLD_PC150U:
990 		/* copied and modified from MSI TV@nywhere Plus */
991 		dev->init_data.name = "Kworld PC150-U";
992 		dev->init_data.get_key = get_key_kworld_pc150u;
993 		dev->init_data.ir_codes = RC_MAP_KWORLD_PC150U;
994 		info.addr = 0x30;
995 		/* MSI TV@nywhere Plus controller doesn't seem to
996 		   respond to probes unless we read something from
997 		   an existing device. Weird...
998 		   REVISIT: might no longer be needed */
999 		rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
1000 		input_dbg("probe 0x%02x @ %s: %s\n",
1001 			msg_msi.addr, dev->i2c_adap.name,
1002 			(1 == rc) ? "yes" : "no");
1003 		break;
1004 	case SAA7134_BOARD_HAUPPAUGE_HVR1110:
1005 		dev->init_data.name = "HVR 1110";
1006 		dev->init_data.get_key = get_key_hvr1110;
1007 		dev->init_data.ir_codes = RC_MAP_HAUPPAUGE;
1008 		info.addr = 0x71;
1009 		break;
1010 	case SAA7134_BOARD_BEHOLD_607FM_MK3:
1011 	case SAA7134_BOARD_BEHOLD_607FM_MK5:
1012 	case SAA7134_BOARD_BEHOLD_609FM_MK3:
1013 	case SAA7134_BOARD_BEHOLD_609FM_MK5:
1014 	case SAA7134_BOARD_BEHOLD_607RDS_MK3:
1015 	case SAA7134_BOARD_BEHOLD_607RDS_MK5:
1016 	case SAA7134_BOARD_BEHOLD_609RDS_MK3:
1017 	case SAA7134_BOARD_BEHOLD_609RDS_MK5:
1018 	case SAA7134_BOARD_BEHOLD_M6:
1019 	case SAA7134_BOARD_BEHOLD_M63:
1020 	case SAA7134_BOARD_BEHOLD_M6_EXTRA:
1021 	case SAA7134_BOARD_BEHOLD_H6:
1022 	case SAA7134_BOARD_BEHOLD_X7:
1023 	case SAA7134_BOARD_BEHOLD_H7:
1024 	case SAA7134_BOARD_BEHOLD_A7:
1025 		dev->init_data.name = "BeholdTV";
1026 		dev->init_data.get_key = get_key_beholdm6xx;
1027 		dev->init_data.ir_codes = RC_MAP_BEHOLD;
1028 		dev->init_data.type = RC_PROTO_BIT_NECX;
1029 		info.addr = 0x2d;
1030 		break;
1031 	case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
1032 	case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
1033 		info.addr = 0x40;
1034 		break;
1035 	case SAA7134_BOARD_AVERMEDIA_A706:
1036 		info.addr = 0x41;
1037 		break;
1038 	case SAA7134_BOARD_FLYDVB_TRIO:
1039 		dev->init_data.name = "FlyDVB Trio";
1040 		dev->init_data.get_key = get_key_flydvb_trio;
1041 		dev->init_data.ir_codes = RC_MAP_FLYDVB;
1042 		info.addr = 0x0b;
1043 		break;
1044 	default:
1045 		input_dbg("No I2C IR support for board %x\n", dev->board);
1046 		return;
1047 	}
1048 
1049 	if (dev->init_data.name)
1050 		info.platform_data = &dev->init_data;
1051 	i2c_new_device(&dev->i2c_adap, &info);
1052 }
1053 
1054 static int saa7134_raw_decode_irq(struct saa7134_dev *dev)
1055 {
1056 	struct saa7134_card_ir *ir = dev->remote;
1057 	int space;
1058 
1059 	/* Generate initial event */
1060 	saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1061 	saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1062 	space = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
1063 	ir_raw_event_store_edge(dev->remote->dev, !space);
1064 
1065 	return 1;
1066 }
1067