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