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