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>
26 #include <linux/slab.h>
28 #include "saa7134-reg.h"
31 #define MODULE_NAME "saa7134"
33 static unsigned int disable_ir;
34 module_param(disable_ir, int, 0444);
35 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
37 static unsigned int ir_debug;
38 module_param(ir_debug, int, 0644);
39 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
41 static int pinnacle_remote;
42 module_param(pinnacle_remote, int, 0644); /* Choose Pinnacle PCTV remote */
43 MODULE_PARM_DESC(pinnacle_remote, "Specify Pinnacle PCTV remote: 0=coloured, 1=grey (defaults to 0)");
45 static int ir_rc5_remote_gap = 885;
46 module_param(ir_rc5_remote_gap, int, 0644);
47 static int ir_rc5_key_timeout = 115;
48 module_param(ir_rc5_key_timeout, int, 0644);
50 static int repeat_delay = 500;
51 module_param(repeat_delay, int, 0644);
52 MODULE_PARM_DESC(repeat_delay, "delay before key repeat started");
53 static int repeat_period = 33;
54 module_param(repeat_period, int, 0644);
55 MODULE_PARM_DESC(repeat_period, "repeat period between "
56 "keypresses when key is down");
58 static unsigned int disable_other_ir;
59 module_param(disable_other_ir, int, 0644);
60 MODULE_PARM_DESC(disable_other_ir, "disable full codes of "
61 "alternative remotes from other manufacturers");
63 #define dprintk(fmt, arg...) if (ir_debug) \
64 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
65 #define i2cdprintk(fmt, arg...) if (ir_debug) \
66 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg)
68 /* Helper functions for RC5 and NEC decoding at GPIO16 or GPIO18 */
69 static int saa7134_rc5_irq(struct saa7134_dev *dev);
70 static int saa7134_nec_irq(struct saa7134_dev *dev);
71 static int saa7134_raw_decode_irq(struct saa7134_dev *dev);
72 static void nec_task(unsigned long data);
73 static void saa7134_nec_timer(unsigned long data);
75 /* -------------------- GPIO generic keycode builder -------------------- */
77 static int build_key(struct saa7134_dev *dev)
79 struct card_ir *ir = dev->remote;
82 /* here comes the additional handshake steps for some cards */
84 case SAA7134_BOARD_GOTVIEW_7135:
85 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
86 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
89 /* rising SAA7134_GPIO_GPRESCAN reads the status */
90 saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
91 saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
93 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
95 if (ir->last_gpio == gpio)
100 data = ir_extract_bits(gpio, ir->mask_keycode);
101 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
102 gpio, ir->mask_keycode, data);
104 switch (dev->board) {
105 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
106 if (data == ir->mask_keycode)
107 ir_input_nokey(ir->dev, &ir->ir);
109 ir_input_keydown(ir->dev, &ir->ir, data);
114 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
115 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
116 ir_input_keydown(ir->dev, &ir->ir, data);
118 ir_input_nokey(ir->dev, &ir->ir);
121 else { /* IRQ driven mode - handle key press and release in one go */
122 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
123 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
124 ir_input_keydown(ir->dev, &ir->ir, data);
125 ir_input_nokey(ir->dev, &ir->ir);
132 /* --------------------- Chip specific I2C key builders ----------------- */
134 static int get_key_flydvb_trio(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
140 /* We need this to access GPI Used by the saa_readl macro. */
141 struct saa7134_dev *dev = ir->c->adapter->algo_data;
144 dprintk("get_key_flydvb_trio: "
145 "gir->c->adapter->algo_data is NULL!\n");
149 /* rising SAA7134_GPIGPRESCAN reads the status */
150 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
151 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
153 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
156 return 0; /* No button press */
158 /* No button press - only before first key pressed */
163 /* weak up the IR chip */
166 while (1 != i2c_master_send(ir->c, &b, 1)) {
167 if ((attempt++) < 10) {
169 * wait a bit for next attempt -
170 * I don't know how make it better
175 i2cdprintk("send wake up byte to pic16C505 (IR chip)"
176 "failed %dx\n", attempt);
179 if (1 != i2c_master_recv(ir->c, &b, 1)) {
180 i2cdprintk("read error\n");
189 static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir, u32 *ir_key,
195 /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
196 struct saa7134_dev *dev = ir->c->adapter->algo_data;
198 dprintk("get_key_msi_tvanywhere_plus: "
199 "gir->c->adapter->algo_data is NULL!\n");
203 /* rising SAA7134_GPIO_GPRESCAN reads the status */
205 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
206 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
208 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
210 /* GPIO&0x40 is pulsed low when a button is pressed. Don't do
211 I2C receive if gpio&0x40 is not low. */
214 return 0; /* No button press */
216 /* GPIO says there is a button press. Get it. */
218 if (1 != i2c_master_recv(ir->c, &b, 1)) {
219 i2cdprintk("read error\n");
223 /* No button press */
230 dprintk("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
236 static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
241 if (1 != i2c_master_recv(ir->c, &b, 1)) {
242 i2cdprintk("read error\n");
246 /* no button press */
259 static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
261 unsigned char buf[5], cod4, code3, code4;
264 if (5 != i2c_master_recv(ir->c, buf, 5))
281 static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
283 unsigned char data[12];
286 struct saa7134_dev *dev = ir->c->adapter->algo_data;
288 /* rising SAA7134_GPIO_GPRESCAN reads the status */
289 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
290 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
292 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
294 if (0x400000 & ~gpio)
295 return 0; /* No button press */
297 ir->c->addr = 0x5a >> 1;
299 if (12 != i2c_master_recv(ir->c, data, 12)) {
300 i2cdprintk("read error\n");
303 /* IR of this card normally decode signals NEC-standard from
304 * - Sven IHOO MT 5.1R remote. xxyye718
305 * - Sven DVD HD-10xx remote. xxyyf708
308 * So, skip not our, if disable full codes mode.
310 if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
313 /* Wrong data decode fix */
314 if (data[9] != (unsigned char)(~data[8]))
323 /* Common (grey or coloured) pinnacle PCTV remote handling
326 static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
327 int parity_offset, int marker, int code_modulo)
330 unsigned int start = 0,parity = 0,code = 0;
333 if (4 != i2c_master_recv(ir->c, b, 4)) {
334 i2cdprintk("read error\n");
338 for (start = 0; start < ARRAY_SIZE(b); start++) {
339 if (b[start] == marker) {
340 code=b[(start+parity_offset + 1) % 4];
341 parity=b[(start+parity_offset) % 4];
350 if (ir->old == parity)
355 /* drop special codes when a key is held down a long time for the grey controller
356 In this case, the second bit of the code is asserted */
357 if (marker == 0xfe && (code & 0x40))
365 i2cdprintk("Pinnacle PCTV key %02x\n", code);
370 /* The grey pinnacle PCTV remote
372 * There are one issue with this remote:
373 * - I2c packet does not change when the same key is pressed quickly. The workaround
374 * is to hold down each key for about half a second, so that another code is generated
375 * in the i2c packet, and the function can distinguish key presses.
377 * Sylvain Pasche <sylvain.pasche@gmail.com>
379 static int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
382 return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
386 /* The new pinnacle PCTV remote (with the colored buttons)
388 * Ricardo Cerqueira <v4l@cerqueira.org>
390 static int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
392 /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
394 * this is the only value that results in 42 unique
398 return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
401 void saa7134_input_irq(struct saa7134_dev *dev)
403 struct card_ir *ir = dev->remote;
406 saa7134_nec_irq(dev);
407 } else if (!ir->polling && !ir->rc5_gpio && !ir->raw_decode) {
409 } else if (ir->rc5_gpio) {
410 saa7134_rc5_irq(dev);
411 } else if (ir->raw_decode) {
412 saa7134_raw_decode_irq(dev);
416 static void saa7134_input_timer(unsigned long data)
418 struct saa7134_dev *dev = (struct saa7134_dev *)data;
419 struct card_ir *ir = dev->remote;
422 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
425 void ir_raw_decode_timer_end(unsigned long data)
427 struct saa7134_dev *dev = (struct saa7134_dev *)data;
428 struct card_ir *ir = dev->remote;
430 ir_raw_event_handle(dev->remote->dev);
435 void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
442 setup_timer(&ir->timer, saa7134_input_timer,
444 ir->timer.expires = jiffies + HZ;
445 add_timer(&ir->timer);
446 } else if (ir->rc5_gpio) {
447 /* set timer_end for code completion */
448 init_timer(&ir->timer_end);
449 ir->timer_end.function = ir_rc5_timer_end;
450 ir->timer_end.data = (unsigned long)ir;
451 init_timer(&ir->timer_keyup);
452 ir->timer_keyup.function = ir_rc5_timer_keyup;
453 ir->timer_keyup.data = (unsigned long)ir;
457 ir->rc5_key_timeout = ir_rc5_key_timeout;
458 ir->rc5_remote_gap = ir_rc5_remote_gap;
459 } else if (ir->nec_gpio) {
460 setup_timer(&ir->timer_keyup, saa7134_nec_timer,
462 tasklet_init(&ir->tlet, nec_task, (unsigned long)dev);
463 } else if (ir->raw_decode) {
464 /* set timer_end for code completion */
465 init_timer(&ir->timer_end);
466 ir->timer_end.function = ir_raw_decode_timer_end;
467 ir->timer_end.data = (unsigned long)dev;
472 void saa7134_ir_stop(struct saa7134_dev *dev)
474 struct card_ir *ir = dev->remote;
478 if (dev->remote->polling)
479 del_timer_sync(&dev->remote->timer);
480 else if (ir->rc5_gpio)
481 del_timer_sync(&ir->timer_end);
482 else if (ir->nec_gpio)
483 tasklet_kill(&ir->tlet);
484 else if (ir->raw_decode) {
485 del_timer_sync(&ir->timer_end);
492 int saa7134_ir_change_protocol(void *priv, u64 ir_type)
494 struct saa7134_dev *dev = priv;
495 struct card_ir *ir = dev->remote;
496 u32 nec_gpio, rc5_gpio;
498 if (ir_type == IR_TYPE_RC5) {
499 dprintk("Changing protocol to RC5\n");
502 } else if (ir_type == IR_TYPE_NEC) {
503 dprintk("Changing protocol to NEC\n");
507 dprintk("IR protocol type %ud is not supported\n",
513 saa7134_ir_stop(dev);
514 ir->nec_gpio = nec_gpio;
515 ir->rc5_gpio = rc5_gpio;
516 saa7134_ir_start(dev, ir);
518 ir->nec_gpio = nec_gpio;
519 ir->rc5_gpio = rc5_gpio;
525 int saa7134_input_init1(struct saa7134_dev *dev)
528 struct input_dev *input_dev;
529 struct ir_scancode_table *ir_codes = NULL;
530 u32 mask_keycode = 0;
531 u32 mask_keydown = 0;
537 u64 ir_type = IR_TYPE_OTHER;
540 if (dev->has_remote != SAA7134_REMOTE_GPIO)
545 /* detect & configure */
546 switch (dev->board) {
547 case SAA7134_BOARD_FLYVIDEO2000:
548 case SAA7134_BOARD_FLYVIDEO3000:
549 case SAA7134_BOARD_HAWELL_HW_404M7:
550 case SAA7134_BOARD_FLYTVPLATINUM_FM:
551 case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
552 case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
553 ir_codes = &ir_codes_flyvideo_table;
554 mask_keycode = 0xEC00000;
555 mask_keydown = 0x0040000;
557 case SAA7134_BOARD_CINERGY400:
558 case SAA7134_BOARD_CINERGY600:
559 case SAA7134_BOARD_CINERGY600_MK3:
560 ir_codes = &ir_codes_cinergy_table;
561 mask_keycode = 0x00003f;
562 mask_keyup = 0x040000;
564 case SAA7134_BOARD_ECS_TVP3XP:
565 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
566 ir_codes = &ir_codes_eztv_table;
567 mask_keycode = 0x00017c;
568 mask_keyup = 0x000002;
571 case SAA7134_BOARD_KWORLD_XPERT:
572 case SAA7134_BOARD_AVACSSMARTTV:
573 ir_codes = &ir_codes_pixelview_table;
574 mask_keycode = 0x00001F;
575 mask_keyup = 0x000020;
578 case SAA7134_BOARD_MD2819:
579 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
580 case SAA7134_BOARD_AVERMEDIA_305:
581 case SAA7134_BOARD_AVERMEDIA_307:
582 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
583 case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
584 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
585 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
586 case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
587 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
588 case SAA7134_BOARD_AVERMEDIA_M102:
589 case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
590 ir_codes = &ir_codes_avermedia_table;
591 mask_keycode = 0x0007C8;
592 mask_keydown = 0x000010;
594 /* Set GPIO pin2 to high to enable the IR controller */
595 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
596 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
598 case SAA7134_BOARD_AVERMEDIA_M135A:
599 ir_codes = &ir_codes_avermedia_m135a_rm_jx_table;
600 mask_keydown = 0x0040000;
601 mask_keycode = 0xffff;
604 case SAA7134_BOARD_AVERMEDIA_777:
605 case SAA7134_BOARD_AVERMEDIA_A16AR:
606 ir_codes = &ir_codes_avermedia_table;
607 mask_keycode = 0x02F200;
608 mask_keydown = 0x000400;
610 /* Without this we won't receive key up events */
611 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
612 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
614 case SAA7134_BOARD_AVERMEDIA_A16D:
615 ir_codes = &ir_codes_avermedia_a16d_table;
616 mask_keycode = 0x02F200;
617 mask_keydown = 0x000400;
618 polling = 50; /* ms */
619 /* Without this we won't receive key up events */
620 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
621 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
623 case SAA7134_BOARD_KWORLD_TERMINATOR:
624 ir_codes = &ir_codes_pixelview_table;
625 mask_keycode = 0x00001f;
626 mask_keyup = 0x000060;
629 case SAA7134_BOARD_MANLI_MTV001:
630 case SAA7134_BOARD_MANLI_MTV002:
631 ir_codes = &ir_codes_manli_table;
632 mask_keycode = 0x001f00;
633 mask_keyup = 0x004000;
634 polling = 50; /* ms */
636 case SAA7134_BOARD_BEHOLD_409FM:
637 case SAA7134_BOARD_BEHOLD_401:
638 case SAA7134_BOARD_BEHOLD_403:
639 case SAA7134_BOARD_BEHOLD_403FM:
640 case SAA7134_BOARD_BEHOLD_405:
641 case SAA7134_BOARD_BEHOLD_405FM:
642 case SAA7134_BOARD_BEHOLD_407:
643 case SAA7134_BOARD_BEHOLD_407FM:
644 case SAA7134_BOARD_BEHOLD_409:
645 case SAA7134_BOARD_BEHOLD_505FM:
646 case SAA7134_BOARD_BEHOLD_505RDS_MK5:
647 case SAA7134_BOARD_BEHOLD_505RDS_MK3:
648 case SAA7134_BOARD_BEHOLD_507_9FM:
649 case SAA7134_BOARD_BEHOLD_507RDS_MK3:
650 case SAA7134_BOARD_BEHOLD_507RDS_MK5:
651 ir_codes = &ir_codes_manli_table;
652 mask_keycode = 0x003f00;
653 mask_keyup = 0x004000;
654 polling = 50; /* ms */
656 case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
657 ir_codes = &ir_codes_behold_columbus_table;
658 mask_keycode = 0x003f00;
659 mask_keyup = 0x004000;
662 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
663 ir_codes = &ir_codes_pctv_sedna_table;
664 mask_keycode = 0x001f00;
665 mask_keyup = 0x004000;
668 case SAA7134_BOARD_GOTVIEW_7135:
669 ir_codes = &ir_codes_gotview7135_table;
670 mask_keycode = 0x0003CC;
671 mask_keydown = 0x000010;
672 polling = 5; /* ms */
673 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
675 case SAA7134_BOARD_VIDEOMATE_TV_PVR:
676 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
677 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
678 ir_codes = &ir_codes_videomate_tv_pvr_table;
679 mask_keycode = 0x00003F;
680 mask_keyup = 0x400000;
683 case SAA7134_BOARD_PROTEUS_2309:
684 ir_codes = &ir_codes_proteus_2309_table;
685 mask_keycode = 0x00007F;
686 mask_keyup = 0x000080;
689 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
690 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
691 ir_codes = &ir_codes_videomate_tv_pvr_table;
692 mask_keycode = 0x003F00;
693 mask_keyup = 0x040000;
695 case SAA7134_BOARD_FLYDVBS_LR300:
696 case SAA7134_BOARD_FLYDVBT_LR301:
697 case SAA7134_BOARD_FLYDVBTDUO:
698 ir_codes = &ir_codes_flydvb_table;
699 mask_keycode = 0x0001F00;
700 mask_keydown = 0x0040000;
702 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
703 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
704 case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
705 ir_codes = &ir_codes_asus_pc39_table;
706 mask_keydown = 0x0040000;
709 case SAA7134_BOARD_ENCORE_ENLTV:
710 case SAA7134_BOARD_ENCORE_ENLTV_FM:
711 ir_codes = &ir_codes_encore_enltv_table;
712 mask_keycode = 0x00007f;
713 mask_keyup = 0x040000;
716 case SAA7134_BOARD_ENCORE_ENLTV_FM53:
717 ir_codes = &ir_codes_encore_enltv_fm53_table;
718 mask_keydown = 0x0040000;
719 mask_keycode = 0x00007f;
722 case SAA7134_BOARD_10MOONSTVMASTER3:
723 ir_codes = &ir_codes_encore_enltv_table;
724 mask_keycode = 0x5f80000;
725 mask_keyup = 0x8000000;
728 case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
729 ir_codes = &ir_codes_genius_tvgo_a11mce_table;
731 mask_keydown = 0xf00000;
732 polling = 50; /* ms */
734 case SAA7134_BOARD_REAL_ANGEL_220:
735 ir_codes = &ir_codes_real_audio_220_32_keys_table;
736 mask_keycode = 0x3f00;
738 polling = 50; /* ms */
740 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
741 ir_codes = &ir_codes_kworld_plus_tv_analog_table;
743 polling = 40; /* ms */
745 case SAA7134_BOARD_VIDEOMATE_S350:
746 ir_codes = &ir_codes_videomate_s350_table;
747 mask_keycode = 0x003f00;
748 mask_keydown = 0x040000;
750 case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
751 ir_codes = &ir_codes_winfast_table;
752 mask_keycode = 0x5f00;
753 mask_keyup = 0x020000;
754 polling = 50; /* ms */
758 if (NULL == ir_codes) {
759 printk("%s: Oops: IR config error [card=%d]\n",
760 dev->name, dev->board);
764 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
765 input_dev = input_allocate_device();
766 if (!ir || !input_dev) {
776 /* init hardware-specific stuff */
777 ir->mask_keycode = mask_keycode;
778 ir->mask_keydown = mask_keydown;
779 ir->mask_keyup = mask_keyup;
780 ir->polling = polling;
781 ir->rc5_gpio = rc5_gpio;
782 ir->nec_gpio = nec_gpio;
783 ir->raw_decode = raw_decode;
785 /* init input device */
786 snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
787 saa7134_boards[dev->board].name);
788 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
791 if (ir_codes->ir_type != IR_TYPE_OTHER && !raw_decode) {
792 ir->props.allowed_protos = IR_TYPE_RC5 | IR_TYPE_NEC;
793 ir->props.priv = dev;
794 ir->props.change_protocol = saa7134_ir_change_protocol;
796 /* Set IR protocol */
797 saa7134_ir_change_protocol(ir->props.priv, ir_codes->ir_type);
799 err = ir_input_init(input_dev, &ir->ir, ir_type);
803 input_dev->name = ir->name;
804 input_dev->phys = ir->phys;
805 input_dev->id.bustype = BUS_PCI;
806 input_dev->id.version = 1;
807 if (dev->pci->subsystem_vendor) {
808 input_dev->id.vendor = dev->pci->subsystem_vendor;
809 input_dev->id.product = dev->pci->subsystem_device;
811 input_dev->id.vendor = dev->pci->vendor;
812 input_dev->id.product = dev->pci->device;
814 input_dev->dev.parent = &dev->pci->dev;
816 err = ir_input_register(ir->dev, ir_codes, &ir->props, MODULE_NAME);
819 if (ir_codes->ir_type != IR_TYPE_OTHER) {
820 err = ir_raw_event_register(ir->dev);
825 saa7134_ir_start(dev, ir);
827 /* the remote isn't as bouncy as a keyboard */
828 ir->dev->rep[REP_DELAY] = repeat_delay;
829 ir->dev->rep[REP_PERIOD] = repeat_period;
834 saa7134_ir_stop(dev);
841 void saa7134_input_fini(struct saa7134_dev *dev)
843 if (NULL == dev->remote)
846 saa7134_ir_stop(dev);
847 ir_raw_event_unregister(dev->remote->dev);
848 ir_input_unregister(dev->remote->dev);
853 void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
855 struct i2c_board_info info;
857 struct i2c_msg msg_msi = {
867 dprintk("IR has been disabled, not probing for i2c remote\n");
871 memset(&info, 0, sizeof(struct i2c_board_info));
872 memset(&dev->init_data, 0, sizeof(dev->init_data));
873 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
875 switch (dev->board) {
876 case SAA7134_BOARD_PINNACLE_PCTV_110i:
877 case SAA7134_BOARD_PINNACLE_PCTV_310i:
878 dev->init_data.name = "Pinnacle PCTV";
879 if (pinnacle_remote == 0) {
880 dev->init_data.get_key = get_key_pinnacle_color;
881 dev->init_data.ir_codes = &ir_codes_pinnacle_color_table;
884 dev->init_data.get_key = get_key_pinnacle_grey;
885 dev->init_data.ir_codes = &ir_codes_pinnacle_grey_table;
889 case SAA7134_BOARD_UPMOST_PURPLE_TV:
890 dev->init_data.name = "Purple TV";
891 dev->init_data.get_key = get_key_purpletv;
892 dev->init_data.ir_codes = &ir_codes_purpletv_table;
895 case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
896 dev->init_data.name = "MSI TV@nywhere Plus";
897 dev->init_data.get_key = get_key_msi_tvanywhere_plus;
898 dev->init_data.ir_codes = &ir_codes_msi_tvanywhere_plus_table;
900 /* MSI TV@nywhere Plus controller doesn't seem to
901 respond to probes unless we read something from
902 an existing device. Weird...
903 REVISIT: might no longer be needed */
904 rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
905 dprintk(KERN_DEBUG "probe 0x%02x @ %s: %s\n",
906 msg_msi.addr, dev->i2c_adap.name,
907 (1 == rc) ? "yes" : "no");
909 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
910 dev->init_data.name = "HVR 1110";
911 dev->init_data.get_key = get_key_hvr1110;
912 dev->init_data.ir_codes = &ir_codes_hauppauge_new_table;
915 case SAA7134_BOARD_BEHOLD_607FM_MK3:
916 case SAA7134_BOARD_BEHOLD_607FM_MK5:
917 case SAA7134_BOARD_BEHOLD_609FM_MK3:
918 case SAA7134_BOARD_BEHOLD_609FM_MK5:
919 case SAA7134_BOARD_BEHOLD_607RDS_MK3:
920 case SAA7134_BOARD_BEHOLD_607RDS_MK5:
921 case SAA7134_BOARD_BEHOLD_609RDS_MK3:
922 case SAA7134_BOARD_BEHOLD_609RDS_MK5:
923 case SAA7134_BOARD_BEHOLD_M6:
924 case SAA7134_BOARD_BEHOLD_M63:
925 case SAA7134_BOARD_BEHOLD_M6_EXTRA:
926 case SAA7134_BOARD_BEHOLD_H6:
927 case SAA7134_BOARD_BEHOLD_X7:
928 dev->init_data.name = "BeholdTV";
929 dev->init_data.get_key = get_key_beholdm6xx;
930 dev->init_data.ir_codes = &ir_codes_behold_table;
931 dev->init_data.type = IR_TYPE_NEC;
934 case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
935 case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
938 case SAA7134_BOARD_FLYDVB_TRIO:
939 dev->init_data.name = "FlyDVB Trio";
940 dev->init_data.get_key = get_key_flydvb_trio;
941 dev->init_data.ir_codes = &ir_codes_flydvb_table;
945 dprintk("No I2C IR support for board %x\n", dev->board);
949 if (dev->init_data.name)
950 info.platform_data = &dev->init_data;
951 i2c_new_device(&dev->i2c_adap, &info);
954 static int saa7134_raw_decode_irq(struct saa7134_dev *dev)
956 struct card_ir *ir = dev->remote;
957 unsigned long timeout;
958 int count, pulse, oldpulse;
960 /* Generate initial event */
961 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
962 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
963 pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
964 ir_raw_event_store(dev->remote->dev, pulse? IR_PULSE : IR_SPACE);
968 * Wait 15 ms from the start of the first IR event before processing
969 * the event. This time is enough for NEC protocol. May need adjustments
970 * to work with other protocols.
973 timeout = jiffies + jiffies_to_msecs(15);
974 mod_timer(&ir->timer_end, timeout);
981 static int saa7134_rc5_irq(struct saa7134_dev *dev)
983 struct card_ir *ir = dev->remote;
986 unsigned long current_jiffies, timeout;
988 /* get time of bit */
989 current_jiffies = jiffies;
990 do_gettimeofday(&tv);
992 /* avoid overflow with gap >1s */
993 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
996 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
997 tv.tv_usec - ir->base_time.tv_usec;
1000 /* active code => add bit */
1002 /* only if in the code (otherwise spurious IRQ or timer
1004 if (ir->last_bit < 28) {
1005 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
1007 ir->code |= 1 << ir->last_bit;
1009 /* starting new code */
1016 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
1017 mod_timer(&ir->timer_end, timeout);
1023 /* On NEC protocol, One has 2.25 ms, and zero has 1.125 ms
1024 The first pulse (start) has 9 + 4.5 ms
1027 static void saa7134_nec_timer(unsigned long data)
1029 struct saa7134_dev *dev = (struct saa7134_dev *) data;
1030 struct card_ir *ir = dev->remote;
1032 dprintk("Cancel key repeat\n");
1034 ir_input_nokey(ir->dev, &ir->ir);
1037 static void nec_task(unsigned long data)
1039 struct saa7134_dev *dev = (struct saa7134_dev *) data;
1042 int count, pulse, oldpulse, gap;
1043 u32 ircode = 0, not_code = 0;
1047 printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
1048 /* GPIO will be kept disabled */
1054 /* rising SAA7134_GPIO_GPRESCAN reads the status */
1055 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1056 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1058 oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
1061 do_gettimeofday(&tv);
1064 /* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
1065 Unfortunately, using IRQ to decode pulse didn't work, since it uses
1066 a pulse train of 38KHz. This means one pulse on each 52 us
1069 /* Wait until the end of pulse/space or 5 ms */
1070 for (count = 0; count < 500; count++) {
1072 /* rising SAA7134_GPIO_GPRESCAN reads the status */
1073 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1074 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1075 pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
1077 if (pulse != oldpulse)
1081 do_gettimeofday(&tv);
1082 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
1083 tv.tv_usec - ir->base_time.tv_usec;
1086 /* Bit 0 has 560 us, while bit 1 has 1120 us.
1087 Do something only if bit == 1
1089 if (ngap && (gap > 560 + 280)) {
1090 unsigned int shift = ngap - 1;
1092 /* Address first, then command */
1095 ircode |= 1 << shift;
1096 } else if (shift < 16) {
1097 not_code |= 1 << shift;
1098 } else if (shift < 24) {
1100 ircode |= 1 << shift;
1103 not_code |= 1 << shift;
1112 /* TIMEOUT - Long pulse */
1116 } while (ngap < 32);
1119 /* FIXME: should check if not_code == ~ircode */
1120 ir->code = ir_extract_bits(ircode, ir->mask_keycode);
1122 dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
1123 ir->code, ircode, not_code);
1125 ir_input_keydown(ir->dev, &ir->ir, ir->code);
1127 dprintk("Repeat last key\n");
1129 /* Keep repeating the last key */
1130 mod_timer(&ir->timer_keyup, jiffies + msecs_to_jiffies(150));
1132 saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
1135 static int saa7134_nec_irq(struct saa7134_dev *dev)
1137 struct card_ir *ir = dev->remote;
1139 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
1140 tasklet_schedule(&ir->tlet);