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