ext3: Update MAINTAINERS for ext3 and JBD
[linux/fpc-iii.git] / drivers / media / video / saa7134 / saa7134-input.c
blobe1e83c7b966ed47844f3c6612f6ccb1cc32bbc85
1 /*
3 * handle saa7134 IR remotes via linux kernel input layer.
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.
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.
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
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/input.h>
27 #include "saa7134-reg.h"
28 #include "saa7134.h"
30 static unsigned int disable_ir;
31 module_param(disable_ir, int, 0444);
32 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
34 static unsigned int ir_debug;
35 module_param(ir_debug, int, 0644);
36 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
38 static int pinnacle_remote;
39 module_param(pinnacle_remote, int, 0644); /* Choose Pinnacle PCTV remote */
40 MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
42 static int ir_rc5_remote_gap = 885;
43 module_param(ir_rc5_remote_gap, int, 0644);
44 static int ir_rc5_key_timeout = 115;
45 module_param(ir_rc5_key_timeout, int, 0644);
47 static int repeat_delay = 500;
48 module_param(repeat_delay, int, 0644);
49 MODULE_PARM_DESC(repeat_delay, "delay before key repeat started");
50 static int repeat_period = 33;
51 module_param(repeat_period, int, 0644);
52 MODULE_PARM_DESC(repeat_period, "repeat period between "
53 "keypresses when key is down");
55 static unsigned int disable_other_ir;
56 module_param(disable_other_ir, int, 0644);
57 MODULE_PARM_DESC(disable_other_ir, "disable full codes of "
58 "alternative remotes from other manufacturers");
60 #define dprintk(fmt, arg...) if (ir_debug) \
61 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
62 #define i2cdprintk(fmt, arg...) if (ir_debug) \
63 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg)
65 /* Helper functions for RC5 and NEC decoding at GPIO16 or GPIO18 */
66 static int saa7134_rc5_irq(struct saa7134_dev *dev);
67 static int saa7134_nec_irq(struct saa7134_dev *dev);
68 static void nec_task(unsigned long data);
69 static void saa7134_nec_timer(unsigned long data);
71 /* -------------------- GPIO generic keycode builder -------------------- */
73 static int build_key(struct saa7134_dev *dev)
75 struct card_ir *ir = dev->remote;
76 u32 gpio, data;
78 /* here comes the additional handshake steps for some cards */
79 switch (dev->board) {
80 case SAA7134_BOARD_GOTVIEW_7135:
81 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
82 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
83 break;
85 /* rising SAA7134_GPIO_GPRESCAN reads the status */
86 saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
87 saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
89 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
90 if (ir->polling) {
91 if (ir->last_gpio == gpio)
92 return 0;
93 ir->last_gpio = gpio;
96 data = ir_extract_bits(gpio, ir->mask_keycode);
97 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
98 gpio, ir->mask_keycode, data);
100 switch (dev->board) {
101 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
102 if (data == ir->mask_keycode)
103 ir_input_nokey(ir->dev, &ir->ir);
104 else
105 ir_input_keydown(ir->dev, &ir->ir, data, data);
106 return 0;
109 if (ir->polling) {
110 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
111 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
112 ir_input_keydown(ir->dev, &ir->ir, data, data);
113 } else {
114 ir_input_nokey(ir->dev, &ir->ir);
117 else { /* IRQ driven mode - handle key press and release in one go */
118 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
119 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
120 ir_input_keydown(ir->dev, &ir->ir, data, data);
121 ir_input_nokey(ir->dev, &ir->ir);
125 return 0;
128 /* --------------------- Chip specific I2C key builders ----------------- */
130 static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir, u32 *ir_key,
131 u32 *ir_raw)
133 unsigned char b;
134 int gpio;
136 /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
137 struct saa7134_dev *dev = ir->c->adapter->algo_data;
138 if (dev == NULL) {
139 dprintk("get_key_msi_tvanywhere_plus: "
140 "gir->c->adapter->algo_data is NULL!\n");
141 return -EIO;
144 /* rising SAA7134_GPIO_GPRESCAN reads the status */
146 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
147 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
149 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
151 /* GPIO&0x40 is pulsed low when a button is pressed. Don't do
152 I2C receive if gpio&0x40 is not low. */
154 if (gpio & 0x40)
155 return 0; /* No button press */
157 /* GPIO says there is a button press. Get it. */
159 if (1 != i2c_master_recv(ir->c, &b, 1)) {
160 i2cdprintk("read error\n");
161 return -EIO;
164 /* No button press */
166 if (b == 0xff)
167 return 0;
169 /* Button pressed */
171 dprintk("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
172 *ir_key = b;
173 *ir_raw = b;
174 return 1;
177 static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
179 unsigned char b;
181 /* poll IR chip */
182 if (1 != i2c_master_recv(ir->c, &b, 1)) {
183 i2cdprintk("read error\n");
184 return -EIO;
187 /* no button press */
188 if (b==0)
189 return 0;
191 /* repeating */
192 if (b & 0x80)
193 return 1;
195 *ir_key = b;
196 *ir_raw = b;
197 return 1;
200 static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
202 unsigned char buf[5], cod4, code3, code4;
204 /* poll IR chip */
205 if (5 != i2c_master_recv(ir->c, buf, 5))
206 return -EIO;
208 cod4 = buf[4];
209 code4 = (cod4 >> 2);
210 code3 = buf[3];
211 if (code3 == 0)
212 /* no key pressed */
213 return 0;
215 /* return key */
216 *ir_key = code4;
217 *ir_raw = code4;
218 return 1;
222 static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
224 unsigned char data[12];
225 u32 gpio;
227 struct saa7134_dev *dev = ir->c->adapter->algo_data;
229 /* rising SAA7134_GPIO_GPRESCAN reads the status */
230 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
231 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
233 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
235 if (0x400000 & ~gpio)
236 return 0; /* No button press */
238 ir->c->addr = 0x5a >> 1;
240 if (12 != i2c_master_recv(ir->c, data, 12)) {
241 i2cdprintk("read error\n");
242 return -EIO;
244 /* IR of this card normally decode signals NEC-standard from
245 * - Sven IHOO MT 5.1R remote. xxyye718
246 * - Sven DVD HD-10xx remote. xxyyf708
247 * - BBK ...
248 * - mayby others
249 * So, skip not our, if disable full codes mode.
251 if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
252 return 0;
254 *ir_key = data[9];
255 *ir_raw = data[9];
257 return 1;
260 /* Common (grey or coloured) pinnacle PCTV remote handling
263 static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
264 int parity_offset, int marker, int code_modulo)
266 unsigned char b[4];
267 unsigned int start = 0,parity = 0,code = 0;
269 /* poll IR chip */
270 if (4 != i2c_master_recv(ir->c, b, 4)) {
271 i2cdprintk("read error\n");
272 return -EIO;
275 for (start = 0; start < ARRAY_SIZE(b); start++) {
276 if (b[start] == marker) {
277 code=b[(start+parity_offset + 1) % 4];
278 parity=b[(start+parity_offset) % 4];
282 /* Empty Request */
283 if (parity == 0)
284 return 0;
286 /* Repeating... */
287 if (ir->old == parity)
288 return 0;
290 ir->old = parity;
292 /* drop special codes when a key is held down a long time for the grey controller
293 In this case, the second bit of the code is asserted */
294 if (marker == 0xfe && (code & 0x40))
295 return 0;
297 code %= code_modulo;
299 *ir_raw = code;
300 *ir_key = code;
302 i2cdprintk("Pinnacle PCTV key %02x\n", code);
304 return 1;
307 /* The grey pinnacle PCTV remote
309 * There are one issue with this remote:
310 * - I2c packet does not change when the same key is pressed quickly. The workaround
311 * is to hold down each key for about half a second, so that another code is generated
312 * in the i2c packet, and the function can distinguish key presses.
314 * Sylvain Pasche <sylvain.pasche@gmail.com>
316 static int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
319 return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
323 /* The new pinnacle PCTV remote (with the colored buttons)
325 * Ricardo Cerqueira <v4l@cerqueira.org>
327 static int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
329 /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
331 * this is the only value that results in 42 unique
332 * codes < 128
335 return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
338 void saa7134_input_irq(struct saa7134_dev *dev)
340 struct card_ir *ir = dev->remote;
342 if (ir->nec_gpio) {
343 saa7134_nec_irq(dev);
344 } else if (!ir->polling && !ir->rc5_gpio) {
345 build_key(dev);
346 } else if (ir->rc5_gpio) {
347 saa7134_rc5_irq(dev);
351 static void saa7134_input_timer(unsigned long data)
353 struct saa7134_dev *dev = (struct saa7134_dev *)data;
354 struct card_ir *ir = dev->remote;
356 build_key(dev);
357 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
360 void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
362 if (ir->polling) {
363 setup_timer(&ir->timer, saa7134_input_timer,
364 (unsigned long)dev);
365 ir->timer.expires = jiffies + HZ;
366 add_timer(&ir->timer);
367 } else if (ir->rc5_gpio) {
368 /* set timer_end for code completion */
369 init_timer(&ir->timer_end);
370 ir->timer_end.function = ir_rc5_timer_end;
371 ir->timer_end.data = (unsigned long)ir;
372 init_timer(&ir->timer_keyup);
373 ir->timer_keyup.function = ir_rc5_timer_keyup;
374 ir->timer_keyup.data = (unsigned long)ir;
375 ir->shift_by = 2;
376 ir->start = 0x2;
377 ir->addr = 0x17;
378 ir->rc5_key_timeout = ir_rc5_key_timeout;
379 ir->rc5_remote_gap = ir_rc5_remote_gap;
380 } else if (ir->nec_gpio) {
381 setup_timer(&ir->timer_keyup, saa7134_nec_timer,
382 (unsigned long)dev);
383 tasklet_init(&ir->tlet, nec_task, (unsigned long)dev);
387 void saa7134_ir_stop(struct saa7134_dev *dev)
389 if (dev->remote->polling)
390 del_timer_sync(&dev->remote->timer);
393 int saa7134_input_init1(struct saa7134_dev *dev)
395 struct card_ir *ir;
396 struct input_dev *input_dev;
397 struct ir_scancode_table *ir_codes = NULL;
398 u32 mask_keycode = 0;
399 u32 mask_keydown = 0;
400 u32 mask_keyup = 0;
401 int polling = 0;
402 int rc5_gpio = 0;
403 int nec_gpio = 0;
404 int ir_type = IR_TYPE_OTHER;
405 int err;
407 if (dev->has_remote != SAA7134_REMOTE_GPIO)
408 return -ENODEV;
409 if (disable_ir)
410 return -ENODEV;
412 /* detect & configure */
413 switch (dev->board) {
414 case SAA7134_BOARD_FLYVIDEO2000:
415 case SAA7134_BOARD_FLYVIDEO3000:
416 case SAA7134_BOARD_FLYTVPLATINUM_FM:
417 case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
418 case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
419 ir_codes = &ir_codes_flyvideo_table;
420 mask_keycode = 0xEC00000;
421 mask_keydown = 0x0040000;
422 break;
423 case SAA7134_BOARD_CINERGY400:
424 case SAA7134_BOARD_CINERGY600:
425 case SAA7134_BOARD_CINERGY600_MK3:
426 ir_codes = &ir_codes_cinergy_table;
427 mask_keycode = 0x00003f;
428 mask_keyup = 0x040000;
429 break;
430 case SAA7134_BOARD_ECS_TVP3XP:
431 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
432 ir_codes = &ir_codes_eztv_table;
433 mask_keycode = 0x00017c;
434 mask_keyup = 0x000002;
435 polling = 50; // ms
436 break;
437 case SAA7134_BOARD_KWORLD_XPERT:
438 case SAA7134_BOARD_AVACSSMARTTV:
439 ir_codes = &ir_codes_pixelview_table;
440 mask_keycode = 0x00001F;
441 mask_keyup = 0x000020;
442 polling = 50; // ms
443 break;
444 case SAA7134_BOARD_MD2819:
445 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
446 case SAA7134_BOARD_AVERMEDIA_305:
447 case SAA7134_BOARD_AVERMEDIA_307:
448 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
449 case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
450 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
451 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
452 case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
453 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
454 case SAA7134_BOARD_AVERMEDIA_M102:
455 case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
456 ir_codes = &ir_codes_avermedia_table;
457 mask_keycode = 0x0007C8;
458 mask_keydown = 0x000010;
459 polling = 50; // ms
460 /* Set GPIO pin2 to high to enable the IR controller */
461 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
462 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
463 break;
464 case SAA7134_BOARD_AVERMEDIA_M135A:
465 ir_codes = &ir_codes_avermedia_m135a_table;
466 mask_keydown = 0x0040000;
467 mask_keycode = 0x00013f;
468 nec_gpio = 1;
469 break;
470 case SAA7134_BOARD_AVERMEDIA_777:
471 case SAA7134_BOARD_AVERMEDIA_A16AR:
472 ir_codes = &ir_codes_avermedia_table;
473 mask_keycode = 0x02F200;
474 mask_keydown = 0x000400;
475 polling = 50; // ms
476 /* Without this we won't receive key up events */
477 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
478 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
479 break;
480 case SAA7134_BOARD_AVERMEDIA_A16D:
481 ir_codes = &ir_codes_avermedia_a16d_table;
482 mask_keycode = 0x02F200;
483 mask_keydown = 0x000400;
484 polling = 50; /* ms */
485 /* Without this we won't receive key up events */
486 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
487 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
488 break;
489 case SAA7134_BOARD_KWORLD_TERMINATOR:
490 ir_codes = &ir_codes_pixelview_table;
491 mask_keycode = 0x00001f;
492 mask_keyup = 0x000060;
493 polling = 50; // ms
494 break;
495 case SAA7134_BOARD_MANLI_MTV001:
496 case SAA7134_BOARD_MANLI_MTV002:
497 ir_codes = &ir_codes_manli_table;
498 mask_keycode = 0x001f00;
499 mask_keyup = 0x004000;
500 polling = 50; /* ms */
501 break;
502 case SAA7134_BOARD_BEHOLD_409FM:
503 case SAA7134_BOARD_BEHOLD_401:
504 case SAA7134_BOARD_BEHOLD_403:
505 case SAA7134_BOARD_BEHOLD_403FM:
506 case SAA7134_BOARD_BEHOLD_405:
507 case SAA7134_BOARD_BEHOLD_405FM:
508 case SAA7134_BOARD_BEHOLD_407:
509 case SAA7134_BOARD_BEHOLD_407FM:
510 case SAA7134_BOARD_BEHOLD_409:
511 case SAA7134_BOARD_BEHOLD_505FM:
512 case SAA7134_BOARD_BEHOLD_505RDS:
513 case SAA7134_BOARD_BEHOLD_507_9FM:
514 case SAA7134_BOARD_BEHOLD_507RDS_MK3:
515 case SAA7134_BOARD_BEHOLD_507RDS_MK5:
516 ir_codes = &ir_codes_manli_table;
517 mask_keycode = 0x003f00;
518 mask_keyup = 0x004000;
519 polling = 50; /* ms */
520 break;
521 case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
522 ir_codes = &ir_codes_behold_columbus_table;
523 mask_keycode = 0x003f00;
524 mask_keyup = 0x004000;
525 polling = 50; // ms
526 break;
527 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
528 ir_codes = &ir_codes_pctv_sedna_table;
529 mask_keycode = 0x001f00;
530 mask_keyup = 0x004000;
531 polling = 50; // ms
532 break;
533 case SAA7134_BOARD_GOTVIEW_7135:
534 ir_codes = &ir_codes_gotview7135_table;
535 mask_keycode = 0x0003CC;
536 mask_keydown = 0x000010;
537 polling = 5; /* ms */
538 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
539 break;
540 case SAA7134_BOARD_VIDEOMATE_TV_PVR:
541 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
542 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
543 ir_codes = &ir_codes_videomate_tv_pvr_table;
544 mask_keycode = 0x00003F;
545 mask_keyup = 0x400000;
546 polling = 50; // ms
547 break;
548 case SAA7134_BOARD_PROTEUS_2309:
549 ir_codes = &ir_codes_proteus_2309_table;
550 mask_keycode = 0x00007F;
551 mask_keyup = 0x000080;
552 polling = 50; // ms
553 break;
554 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
555 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
556 ir_codes = &ir_codes_videomate_tv_pvr_table;
557 mask_keycode = 0x003F00;
558 mask_keyup = 0x040000;
559 break;
560 case SAA7134_BOARD_FLYDVBS_LR300:
561 case SAA7134_BOARD_FLYDVBT_LR301:
562 case SAA7134_BOARD_FLYDVBTDUO:
563 ir_codes = &ir_codes_flydvb_table;
564 mask_keycode = 0x0001F00;
565 mask_keydown = 0x0040000;
566 break;
567 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
568 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
569 case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
570 ir_codes = &ir_codes_asus_pc39_table;
571 mask_keydown = 0x0040000;
572 rc5_gpio = 1;
573 break;
574 case SAA7134_BOARD_ENCORE_ENLTV:
575 case SAA7134_BOARD_ENCORE_ENLTV_FM:
576 ir_codes = &ir_codes_encore_enltv_table;
577 mask_keycode = 0x00007f;
578 mask_keyup = 0x040000;
579 polling = 50; // ms
580 break;
581 case SAA7134_BOARD_ENCORE_ENLTV_FM53:
582 ir_codes = &ir_codes_encore_enltv_fm53_table;
583 mask_keydown = 0x0040000;
584 mask_keycode = 0x00007f;
585 nec_gpio = 1;
586 break;
587 case SAA7134_BOARD_10MOONSTVMASTER3:
588 ir_codes = &ir_codes_encore_enltv_table;
589 mask_keycode = 0x5f80000;
590 mask_keyup = 0x8000000;
591 polling = 50; //ms
592 break;
593 case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
594 ir_codes = &ir_codes_genius_tvgo_a11mce_table;
595 mask_keycode = 0xff;
596 mask_keydown = 0xf00000;
597 polling = 50; /* ms */
598 break;
599 case SAA7134_BOARD_REAL_ANGEL_220:
600 ir_codes = &ir_codes_real_audio_220_32_keys_table;
601 mask_keycode = 0x3f00;
602 mask_keyup = 0x4000;
603 polling = 50; /* ms */
604 break;
605 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
606 ir_codes = &ir_codes_kworld_plus_tv_analog_table;
607 mask_keycode = 0x7f;
608 polling = 40; /* ms */
609 break;
610 case SAA7134_BOARD_VIDEOMATE_S350:
611 ir_codes = &ir_codes_videomate_s350_table;
612 mask_keycode = 0x003f00;
613 mask_keydown = 0x040000;
614 break;
616 if (NULL == ir_codes) {
617 printk("%s: Oops: IR config error [card=%d]\n",
618 dev->name, dev->board);
619 return -ENODEV;
622 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
623 input_dev = input_allocate_device();
624 if (!ir || !input_dev) {
625 err = -ENOMEM;
626 goto err_out_free;
629 ir->dev = input_dev;
631 /* init hardware-specific stuff */
632 ir->mask_keycode = mask_keycode;
633 ir->mask_keydown = mask_keydown;
634 ir->mask_keyup = mask_keyup;
635 ir->polling = polling;
636 ir->rc5_gpio = rc5_gpio;
637 ir->nec_gpio = nec_gpio;
639 /* init input device */
640 snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
641 saa7134_boards[dev->board].name);
642 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
643 pci_name(dev->pci));
645 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
646 input_dev->name = ir->name;
647 input_dev->phys = ir->phys;
648 input_dev->id.bustype = BUS_PCI;
649 input_dev->id.version = 1;
650 if (dev->pci->subsystem_vendor) {
651 input_dev->id.vendor = dev->pci->subsystem_vendor;
652 input_dev->id.product = dev->pci->subsystem_device;
653 } else {
654 input_dev->id.vendor = dev->pci->vendor;
655 input_dev->id.product = dev->pci->device;
657 input_dev->dev.parent = &dev->pci->dev;
659 dev->remote = ir;
660 saa7134_ir_start(dev, ir);
662 err = input_register_device(ir->dev);
663 if (err)
664 goto err_out_stop;
666 /* the remote isn't as bouncy as a keyboard */
667 ir->dev->rep[REP_DELAY] = repeat_delay;
668 ir->dev->rep[REP_PERIOD] = repeat_period;
670 return 0;
672 err_out_stop:
673 saa7134_ir_stop(dev);
674 dev->remote = NULL;
675 err_out_free:
676 input_free_device(input_dev);
677 kfree(ir);
678 return err;
681 void saa7134_input_fini(struct saa7134_dev *dev)
683 if (NULL == dev->remote)
684 return;
686 saa7134_ir_stop(dev);
687 input_unregister_device(dev->remote->dev);
688 kfree(dev->remote);
689 dev->remote = NULL;
692 void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
694 const unsigned short addr_list[] = {
695 0x7a, 0x47, 0x71, 0x2d,
696 I2C_CLIENT_END
699 struct i2c_msg msg_msi = {
700 .addr = 0x50,
701 .flags = I2C_M_RD,
702 .len = 0,
703 .buf = NULL,
706 int rc;
708 if (disable_ir) {
709 dprintk("IR has been disabled, not probing for i2c remote\n");
710 return;
713 memset(&dev->info, 0, sizeof(dev->info));
714 memset(&dev->init_data, 0, sizeof(dev->init_data));
715 strlcpy(dev->info.type, "ir_video", I2C_NAME_SIZE);
717 switch (dev->board) {
718 case SAA7134_BOARD_PINNACLE_PCTV_110i:
719 case SAA7134_BOARD_PINNACLE_PCTV_310i:
720 dev->init_data.name = "Pinnacle PCTV";
721 if (pinnacle_remote == 0) {
722 dev->init_data.get_key = get_key_pinnacle_color;
723 dev->init_data.ir_codes = &ir_codes_pinnacle_color_table;
724 dev->info.addr = 0x47;
725 } else {
726 dev->init_data.get_key = get_key_pinnacle_grey;
727 dev->init_data.ir_codes = &ir_codes_pinnacle_grey_table;
728 dev->info.addr = 0x47;
730 break;
731 case SAA7134_BOARD_UPMOST_PURPLE_TV:
732 dev->init_data.name = "Purple TV";
733 dev->init_data.get_key = get_key_purpletv;
734 dev->init_data.ir_codes = &ir_codes_purpletv_table;
735 break;
736 case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
737 dev->init_data.name = "MSI TV@nywhere Plus";
738 dev->init_data.get_key = get_key_msi_tvanywhere_plus;
739 dev->init_data.ir_codes = &ir_codes_msi_tvanywhere_plus_table;
740 dev->info.addr = 0x30;
741 /* MSI TV@nywhere Plus controller doesn't seem to
742 respond to probes unless we read something from
743 an existing device. Weird...
744 REVISIT: might no longer be needed */
745 rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
746 dprintk(KERN_DEBUG "probe 0x%02x @ %s: %s\n",
747 msg_msi.addr, dev->i2c_adap.name,
748 (1 == rc) ? "yes" : "no");
749 break;
750 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
751 dev->init_data.name = "HVR 1110";
752 dev->init_data.get_key = get_key_hvr1110;
753 dev->init_data.ir_codes = &ir_codes_hauppauge_new_table;
754 break;
755 case SAA7134_BOARD_BEHOLD_607FM_MK3:
756 case SAA7134_BOARD_BEHOLD_607FM_MK5:
757 case SAA7134_BOARD_BEHOLD_609FM_MK3:
758 case SAA7134_BOARD_BEHOLD_609FM_MK5:
759 case SAA7134_BOARD_BEHOLD_607RDS_MK3:
760 case SAA7134_BOARD_BEHOLD_607RDS_MK5:
761 case SAA7134_BOARD_BEHOLD_609RDS_MK3:
762 case SAA7134_BOARD_BEHOLD_609RDS_MK5:
763 case SAA7134_BOARD_BEHOLD_M6:
764 case SAA7134_BOARD_BEHOLD_M63:
765 case SAA7134_BOARD_BEHOLD_M6_EXTRA:
766 case SAA7134_BOARD_BEHOLD_H6:
767 case SAA7134_BOARD_BEHOLD_X7:
768 dev->init_data.name = "BeholdTV";
769 dev->init_data.get_key = get_key_beholdm6xx;
770 dev->init_data.ir_codes = &ir_codes_behold_table;
771 break;
772 case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
773 case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
774 dev->info.addr = 0x40;
775 break;
778 if (dev->init_data.name)
779 dev->info.platform_data = &dev->init_data;
780 /* No need to probe if address is known */
781 if (dev->info.addr) {
782 i2c_new_device(&dev->i2c_adap, &dev->info);
783 return;
786 /* Address not known, fallback to probing */
787 i2c_new_probed_device(&dev->i2c_adap, &dev->info, addr_list);
790 static int saa7134_rc5_irq(struct saa7134_dev *dev)
792 struct card_ir *ir = dev->remote;
793 struct timeval tv;
794 u32 gap;
795 unsigned long current_jiffies, timeout;
797 /* get time of bit */
798 current_jiffies = jiffies;
799 do_gettimeofday(&tv);
801 /* avoid overflow with gap >1s */
802 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
803 gap = 200000;
804 } else {
805 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
806 tv.tv_usec - ir->base_time.tv_usec;
809 /* active code => add bit */
810 if (ir->active) {
811 /* only if in the code (otherwise spurious IRQ or timer
812 late) */
813 if (ir->last_bit < 28) {
814 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
815 ir_rc5_remote_gap;
816 ir->code |= 1 << ir->last_bit;
818 /* starting new code */
819 } else {
820 ir->active = 1;
821 ir->code = 0;
822 ir->base_time = tv;
823 ir->last_bit = 0;
825 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
826 mod_timer(&ir->timer_end, timeout);
829 return 1;
833 /* On NEC protocol, One has 2.25 ms, and zero has 1.125 ms
834 The first pulse (start) has 9 + 4.5 ms
837 static void saa7134_nec_timer(unsigned long data)
839 struct saa7134_dev *dev = (struct saa7134_dev *) data;
840 struct card_ir *ir = dev->remote;
842 dprintk("Cancel key repeat\n");
844 ir_input_nokey(ir->dev, &ir->ir);
847 static void nec_task(unsigned long data)
849 struct saa7134_dev *dev = (struct saa7134_dev *) data;
850 struct card_ir *ir;
851 struct timeval tv;
852 int count, pulse, oldpulse, gap;
853 u32 ircode = 0, not_code = 0;
854 int ngap = 0;
856 if (!data) {
857 printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
858 /* GPIO will be kept disabled */
859 return;
862 ir = dev->remote;
864 /* rising SAA7134_GPIO_GPRESCAN reads the status */
865 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
866 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
868 oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
869 pulse = oldpulse;
871 do_gettimeofday(&tv);
872 ir->base_time = tv;
874 /* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
875 Unfortunately, using IRQ to decode pulse didn't work, since it uses
876 a pulse train of 38KHz. This means one pulse on each 52 us
878 do {
879 /* Wait until the end of pulse/space or 5 ms */
880 for (count = 0; count < 500; count++) {
881 udelay(10);
882 /* rising SAA7134_GPIO_GPRESCAN reads the status */
883 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
884 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
885 pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
886 & ir->mask_keydown;
887 if (pulse != oldpulse)
888 break;
891 do_gettimeofday(&tv);
892 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
893 tv.tv_usec - ir->base_time.tv_usec;
895 if (!pulse) {
896 /* Bit 0 has 560 us, while bit 1 has 1120 us.
897 Do something only if bit == 1
899 if (ngap && (gap > 560 + 280)) {
900 unsigned int shift = ngap - 1;
902 /* Address first, then command */
903 if (shift < 8) {
904 shift += 8;
905 ircode |= 1 << shift;
906 } else if (shift < 16) {
907 not_code |= 1 << shift;
908 } else if (shift < 24) {
909 shift -= 16;
910 ircode |= 1 << shift;
911 } else {
912 shift -= 24;
913 not_code |= 1 << shift;
916 ngap++;
920 ir->base_time = tv;
922 /* TIMEOUT - Long pulse */
923 if (gap >= 5000)
924 break;
925 oldpulse = pulse;
926 } while (ngap < 32);
928 if (ngap == 32) {
929 /* FIXME: should check if not_code == ~ircode */
930 ir->code = ir_extract_bits(ircode, ir->mask_keycode);
932 dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
933 ir->code, ircode, not_code);
935 ir_input_keydown(ir->dev, &ir->ir, ir->code, ir->code);
936 } else
937 dprintk("Repeat last key\n");
939 /* Keep repeating the last key */
940 mod_timer(&ir->timer_keyup, jiffies + msecs_to_jiffies(150));
942 saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
945 static int saa7134_nec_irq(struct saa7134_dev *dev)
947 struct card_ir *ir = dev->remote;
949 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
950 tasklet_schedule(&ir->tlet);
952 return 1;