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