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/slab.h>
27 #include "saa7134-reg.h"
30 #define MODULE_NAME "saa7134"
32 static unsigned int disable_ir;
33 module_param(disable_ir, int, 0444);
34 MODULE_PARM_DESC(disable_ir,"disable infrared remote support");
36 static unsigned int ir_debug;
37 module_param(ir_debug, int, 0644);
38 MODULE_PARM_DESC(ir_debug,"enable debug messages [IR]");
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)");
44 static int ir_rc5_remote_gap = 885;
45 module_param(ir_rc5_remote_gap, int, 0644);
47 static unsigned int disable_other_ir;
48 module_param(disable_other_ir, int, 0644);
49 MODULE_PARM_DESC(disable_other_ir, "disable full codes of "
50 "alternative remotes from other manufacturers");
52 #define dprintk(fmt, arg...) if (ir_debug) \
53 printk(KERN_DEBUG "%s/ir: " fmt, dev->name , ## arg)
54 #define i2cdprintk(fmt, arg...) if (ir_debug) \
55 printk(KERN_DEBUG "%s/ir: " fmt, ir->name , ## arg)
57 /* Helper functions for RC5 and NEC decoding at GPIO16 or GPIO18 */
58 static int saa7134_rc5_irq(struct saa7134_dev *dev);
59 static int saa7134_nec_irq(struct saa7134_dev *dev);
60 static int saa7134_raw_decode_irq(struct saa7134_dev *dev);
61 static void nec_task(unsigned long data);
63 /* -------------------- GPIO generic keycode builder -------------------- */
65 static int build_key(struct saa7134_dev *dev)
67 struct card_ir *ir = dev->remote;
70 /* here comes the additional handshake steps for some cards */
72 case SAA7134_BOARD_GOTVIEW_7135:
73 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
74 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
77 /* rising SAA7134_GPIO_GPRESCAN reads the status */
78 saa_clearb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
79 saa_setb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN);
81 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
83 if (ir->last_gpio == gpio)
88 data = ir_extract_bits(gpio, ir->mask_keycode);
89 dprintk("build_key gpio=0x%x mask=0x%x data=%d\n",
90 gpio, ir->mask_keycode, data);
93 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
94 if (data == ir->mask_keycode)
97 ir_keydown_notimeout(ir->dev, data, 0);
102 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
103 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
104 ir_keydown_notimeout(ir->dev, data, 0);
109 else { /* IRQ driven mode - handle key press and release in one go */
110 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
111 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
112 ir_keydown_notimeout(ir->dev, data, 0);
120 /* --------------------- Chip specific I2C key builders ----------------- */
122 static int get_key_flydvb_trio(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
128 /* We need this to access GPI Used by the saa_readl macro. */
129 struct saa7134_dev *dev = ir->c->adapter->algo_data;
132 i2cdprintk("get_key_flydvb_trio: "
133 "ir->c->adapter->algo_data is NULL!\n");
137 /* rising SAA7134_GPIGPRESCAN reads the status */
138 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
139 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
141 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
144 return 0; /* No button press */
146 /* No button press - only before first key pressed */
151 /* weak up the IR chip */
154 while (1 != i2c_master_send(ir->c, &b, 1)) {
155 if ((attempt++) < 10) {
157 * wait a bit for next attempt -
158 * I don't know how make it better
163 i2cdprintk("send wake up byte to pic16C505 (IR chip)"
164 "failed %dx\n", attempt);
167 if (1 != i2c_master_recv(ir->c, &b, 1)) {
168 i2cdprintk("read error\n");
177 static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir, u32 *ir_key,
183 /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
184 struct saa7134_dev *dev = ir->c->adapter->algo_data;
186 i2cdprintk("get_key_msi_tvanywhere_plus: "
187 "ir->c->adapter->algo_data is NULL!\n");
191 /* rising SAA7134_GPIO_GPRESCAN reads the status */
193 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
194 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
196 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
198 /* GPIO&0x40 is pulsed low when a button is pressed. Don't do
199 I2C receive if gpio&0x40 is not low. */
202 return 0; /* No button press */
204 /* GPIO says there is a button press. Get it. */
206 if (1 != i2c_master_recv(ir->c, &b, 1)) {
207 i2cdprintk("read error\n");
211 /* No button press */
218 dprintk("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
224 static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
229 if (1 != i2c_master_recv(ir->c, &b, 1)) {
230 i2cdprintk("read error\n");
234 /* no button press */
247 static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
249 unsigned char buf[5], cod4, code3, code4;
252 if (5 != i2c_master_recv(ir->c, buf, 5))
269 static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
271 unsigned char data[12];
274 struct saa7134_dev *dev = ir->c->adapter->algo_data;
276 /* rising SAA7134_GPIO_GPRESCAN reads the status */
277 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
278 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
280 gpio = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2);
282 if (0x400000 & ~gpio)
283 return 0; /* No button press */
285 ir->c->addr = 0x5a >> 1;
287 if (12 != i2c_master_recv(ir->c, data, 12)) {
288 i2cdprintk("read error\n");
291 /* IR of this card normally decode signals NEC-standard from
292 * - Sven IHOO MT 5.1R remote. xxyye718
293 * - Sven DVD HD-10xx remote. xxyyf708
296 * So, skip not our, if disable full codes mode.
298 if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
301 /* Wrong data decode fix */
302 if (data[9] != (unsigned char)(~data[8]))
311 /* Common (grey or coloured) pinnacle PCTV remote handling
314 static int get_key_pinnacle(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
315 int parity_offset, int marker, int code_modulo)
318 unsigned int start = 0,parity = 0,code = 0;
321 if (4 != i2c_master_recv(ir->c, b, 4)) {
322 i2cdprintk("read error\n");
326 for (start = 0; start < ARRAY_SIZE(b); start++) {
327 if (b[start] == marker) {
328 code=b[(start+parity_offset + 1) % 4];
329 parity=b[(start+parity_offset) % 4];
338 if (ir->old == parity)
343 /* drop special codes when a key is held down a long time for the grey controller
344 In this case, the second bit of the code is asserted */
345 if (marker == 0xfe && (code & 0x40))
353 i2cdprintk("Pinnacle PCTV key %02x\n", code);
358 /* The grey pinnacle PCTV remote
360 * There are one issue with this remote:
361 * - I2c packet does not change when the same key is pressed quickly. The workaround
362 * is to hold down each key for about half a second, so that another code is generated
363 * in the i2c packet, and the function can distinguish key presses.
365 * Sylvain Pasche <sylvain.pasche@gmail.com>
367 static int get_key_pinnacle_grey(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
370 return get_key_pinnacle(ir, ir_key, ir_raw, 1, 0xfe, 0xff);
374 /* The new pinnacle PCTV remote (with the colored buttons)
376 * Ricardo Cerqueira <v4l@cerqueira.org>
378 static int get_key_pinnacle_color(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
380 /* code_modulo parameter (0x88) is used to reduce code value to fit inside IR_KEYTAB_SIZE
382 * this is the only value that results in 42 unique
386 return get_key_pinnacle(ir, ir_key, ir_raw, 2, 0x80, 0x88);
389 void saa7134_input_irq(struct saa7134_dev *dev)
393 if (!dev || !dev->remote)
401 saa7134_nec_irq(dev);
402 } else if (!ir->polling && !ir->rc5_gpio && !ir->raw_decode) {
404 } else if (ir->rc5_gpio) {
405 saa7134_rc5_irq(dev);
406 } else if (ir->raw_decode) {
407 saa7134_raw_decode_irq(dev);
411 static void saa7134_input_timer(unsigned long data)
413 struct saa7134_dev *dev = (struct saa7134_dev *)data;
414 struct card_ir *ir = dev->remote;
417 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
420 static void ir_raw_decode_timer_end(unsigned long data)
422 struct saa7134_dev *dev = (struct saa7134_dev *)data;
423 struct card_ir *ir = dev->remote;
425 ir_raw_event_handle(dev->remote->dev);
430 static int __saa7134_ir_start(void *priv)
432 struct saa7134_dev *dev = priv;
447 setup_timer(&ir->timer, saa7134_input_timer,
449 ir->timer.expires = jiffies + HZ;
450 add_timer(&ir->timer);
451 } else if (ir->rc5_gpio) {
452 /* set timer_end for code completion */
453 init_timer(&ir->timer_end);
454 ir->timer_end.function = ir_rc5_timer_end;
455 ir->timer_end.data = (unsigned long)ir;
459 ir->rc5_remote_gap = ir_rc5_remote_gap;
460 } else if (ir->nec_gpio) {
461 tasklet_init(&ir->tlet, nec_task, (unsigned long)dev);
462 } else if (ir->raw_decode) {
463 /* set timer_end for code completion */
464 init_timer(&ir->timer_end);
465 ir->timer_end.function = ir_raw_decode_timer_end;
466 ir->timer_end.data = (unsigned long)dev;
473 static void __saa7134_ir_stop(void *priv)
475 struct saa7134_dev *dev = priv;
487 if (dev->remote->polling)
488 del_timer_sync(&dev->remote->timer);
489 else if (ir->rc5_gpio)
490 del_timer_sync(&ir->timer_end);
491 else if (ir->nec_gpio)
492 tasklet_kill(&ir->tlet);
493 else if (ir->raw_decode) {
494 del_timer_sync(&ir->timer_end);
503 int saa7134_ir_start(struct saa7134_dev *dev)
505 if (dev->remote->users)
506 return __saa7134_ir_start(dev);
511 void saa7134_ir_stop(struct saa7134_dev *dev)
513 if (dev->remote->users)
514 __saa7134_ir_stop(dev);
517 static int saa7134_ir_open(struct rc_dev *rc)
519 struct saa7134_dev *dev = rc->priv;
521 dev->remote->users++;
522 return __saa7134_ir_start(dev);
525 static void saa7134_ir_close(struct rc_dev *rc)
527 struct saa7134_dev *dev = rc->priv;
529 dev->remote->users--;
530 if (!dev->remote->users)
531 __saa7134_ir_stop(dev);
535 static int saa7134_ir_change_protocol(struct rc_dev *rc, u64 ir_type)
537 struct saa7134_dev *dev = rc->priv;
538 struct card_ir *ir = dev->remote;
539 u32 nec_gpio, rc5_gpio;
541 if (ir_type == IR_TYPE_RC5) {
542 dprintk("Changing protocol to RC5\n");
545 } else if (ir_type == IR_TYPE_NEC) {
546 dprintk("Changing protocol to NEC\n");
550 dprintk("IR protocol type %ud is not supported\n",
556 saa7134_ir_stop(dev);
557 ir->nec_gpio = nec_gpio;
558 ir->rc5_gpio = rc5_gpio;
559 saa7134_ir_start(dev);
561 ir->nec_gpio = nec_gpio;
562 ir->rc5_gpio = rc5_gpio;
568 int saa7134_input_init1(struct saa7134_dev *dev)
572 char *ir_codes = NULL;
573 u32 mask_keycode = 0;
574 u32 mask_keydown = 0;
580 int allow_protocol_change = 0;
583 if (dev->has_remote != SAA7134_REMOTE_GPIO)
588 /* detect & configure */
589 switch (dev->board) {
590 case SAA7134_BOARD_FLYVIDEO2000:
591 case SAA7134_BOARD_FLYVIDEO3000:
592 case SAA7134_BOARD_FLYTVPLATINUM_FM:
593 case SAA7134_BOARD_FLYTVPLATINUM_MINI2:
594 case SAA7134_BOARD_ROVERMEDIA_LINK_PRO_FM:
595 ir_codes = RC_MAP_FLYVIDEO;
596 mask_keycode = 0xEC00000;
597 mask_keydown = 0x0040000;
599 case SAA7134_BOARD_CINERGY400:
600 case SAA7134_BOARD_CINERGY600:
601 case SAA7134_BOARD_CINERGY600_MK3:
602 ir_codes = RC_MAP_CINERGY;
603 mask_keycode = 0x00003f;
604 mask_keyup = 0x040000;
606 case SAA7134_BOARD_ECS_TVP3XP:
607 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
608 ir_codes = RC_MAP_EZTV;
609 mask_keycode = 0x00017c;
610 mask_keyup = 0x000002;
613 case SAA7134_BOARD_KWORLD_XPERT:
614 case SAA7134_BOARD_AVACSSMARTTV:
615 ir_codes = RC_MAP_PIXELVIEW;
616 mask_keycode = 0x00001F;
617 mask_keyup = 0x000020;
620 case SAA7134_BOARD_MD2819:
621 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
622 case SAA7134_BOARD_AVERMEDIA_305:
623 case SAA7134_BOARD_AVERMEDIA_307:
624 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
625 case SAA7134_BOARD_AVERMEDIA_STUDIO_505:
626 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
627 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
628 case SAA7134_BOARD_AVERMEDIA_STUDIO_507UA:
629 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
630 case SAA7134_BOARD_AVERMEDIA_M102:
631 case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
632 ir_codes = RC_MAP_AVERMEDIA;
633 mask_keycode = 0x0007C8;
634 mask_keydown = 0x000010;
636 /* Set GPIO pin2 to high to enable the IR controller */
637 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
638 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
640 case SAA7134_BOARD_AVERMEDIA_M135A:
641 ir_codes = RC_MAP_AVERMEDIA_M135A;
642 mask_keydown = 0x0040000; /* Enable GPIO18 line on both edges */
643 mask_keyup = 0x0040000;
644 mask_keycode = 0xffff;
647 case SAA7134_BOARD_AVERMEDIA_M733A:
648 ir_codes = RC_MAP_AVERMEDIA_M733A_RM_K6;
649 mask_keydown = 0x0040000;
650 mask_keyup = 0x0040000;
651 mask_keycode = 0xffff;
654 case SAA7134_BOARD_AVERMEDIA_777:
655 case SAA7134_BOARD_AVERMEDIA_A16AR:
656 ir_codes = RC_MAP_AVERMEDIA;
657 mask_keycode = 0x02F200;
658 mask_keydown = 0x000400;
660 /* Without this we won't receive key up events */
661 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
662 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
664 case SAA7134_BOARD_AVERMEDIA_A16D:
665 ir_codes = RC_MAP_AVERMEDIA_A16D;
666 mask_keycode = 0x02F200;
667 mask_keydown = 0x000400;
668 polling = 50; /* ms */
669 /* Without this we won't receive key up events */
670 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
671 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
673 case SAA7134_BOARD_KWORLD_TERMINATOR:
674 ir_codes = RC_MAP_PIXELVIEW;
675 mask_keycode = 0x00001f;
676 mask_keyup = 0x000060;
679 case SAA7134_BOARD_MANLI_MTV001:
680 case SAA7134_BOARD_MANLI_MTV002:
681 ir_codes = RC_MAP_MANLI;
682 mask_keycode = 0x001f00;
683 mask_keyup = 0x004000;
684 polling = 50; /* ms */
686 case SAA7134_BOARD_BEHOLD_409FM:
687 case SAA7134_BOARD_BEHOLD_401:
688 case SAA7134_BOARD_BEHOLD_403:
689 case SAA7134_BOARD_BEHOLD_403FM:
690 case SAA7134_BOARD_BEHOLD_405:
691 case SAA7134_BOARD_BEHOLD_405FM:
692 case SAA7134_BOARD_BEHOLD_407:
693 case SAA7134_BOARD_BEHOLD_407FM:
694 case SAA7134_BOARD_BEHOLD_409:
695 case SAA7134_BOARD_BEHOLD_505FM:
696 case SAA7134_BOARD_BEHOLD_505RDS_MK5:
697 case SAA7134_BOARD_BEHOLD_505RDS_MK3:
698 case SAA7134_BOARD_BEHOLD_507_9FM:
699 case SAA7134_BOARD_BEHOLD_507RDS_MK3:
700 case SAA7134_BOARD_BEHOLD_507RDS_MK5:
701 ir_codes = RC_MAP_MANLI;
702 mask_keycode = 0x003f00;
703 mask_keyup = 0x004000;
704 polling = 50; /* ms */
706 case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
707 ir_codes = RC_MAP_BEHOLD_COLUMBUS;
708 mask_keycode = 0x003f00;
709 mask_keyup = 0x004000;
712 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
713 ir_codes = RC_MAP_PCTV_SEDNA;
714 mask_keycode = 0x001f00;
715 mask_keyup = 0x004000;
718 case SAA7134_BOARD_GOTVIEW_7135:
719 ir_codes = RC_MAP_GOTVIEW7135;
720 mask_keycode = 0x0003CC;
721 mask_keydown = 0x000010;
722 polling = 5; /* ms */
723 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
725 case SAA7134_BOARD_VIDEOMATE_TV_PVR:
726 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
727 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
728 ir_codes = RC_MAP_VIDEOMATE_TV_PVR;
729 mask_keycode = 0x00003F;
730 mask_keyup = 0x400000;
733 case SAA7134_BOARD_PROTEUS_2309:
734 ir_codes = RC_MAP_PROTEUS_2309;
735 mask_keycode = 0x00007F;
736 mask_keyup = 0x000080;
739 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
740 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
741 ir_codes = RC_MAP_VIDEOMATE_TV_PVR;
742 mask_keycode = 0x003F00;
743 mask_keyup = 0x040000;
745 case SAA7134_BOARD_FLYDVBS_LR300:
746 case SAA7134_BOARD_FLYDVBT_LR301:
747 case SAA7134_BOARD_FLYDVBTDUO:
748 ir_codes = RC_MAP_FLYDVB;
749 mask_keycode = 0x0001F00;
750 mask_keydown = 0x0040000;
752 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
753 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
754 case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
755 ir_codes = RC_MAP_ASUS_PC39;
756 mask_keydown = 0x0040000; /* Enable GPIO18 line on both edges */
757 mask_keyup = 0x0040000;
758 mask_keycode = 0xffff;
761 case SAA7134_BOARD_ENCORE_ENLTV:
762 case SAA7134_BOARD_ENCORE_ENLTV_FM:
763 ir_codes = RC_MAP_ENCORE_ENLTV;
764 mask_keycode = 0x00007f;
765 mask_keyup = 0x040000;
768 case SAA7134_BOARD_ENCORE_ENLTV_FM53:
769 ir_codes = RC_MAP_ENCORE_ENLTV_FM53;
770 mask_keydown = 0x0040000;
771 mask_keycode = 0x00007f;
774 case SAA7134_BOARD_10MOONSTVMASTER3:
775 ir_codes = RC_MAP_ENCORE_ENLTV;
776 mask_keycode = 0x5f80000;
777 mask_keyup = 0x8000000;
780 case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
781 ir_codes = RC_MAP_GENIUS_TVGO_A11MCE;
783 mask_keydown = 0xf00000;
784 polling = 50; /* ms */
786 case SAA7134_BOARD_REAL_ANGEL_220:
787 ir_codes = RC_MAP_REAL_AUDIO_220_32_KEYS;
788 mask_keycode = 0x3f00;
790 polling = 50; /* ms */
792 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
793 ir_codes = RC_MAP_KWORLD_PLUS_TV_ANALOG;
795 polling = 40; /* ms */
797 case SAA7134_BOARD_VIDEOMATE_S350:
798 ir_codes = RC_MAP_VIDEOMATE_S350;
799 mask_keycode = 0x003f00;
800 mask_keydown = 0x040000;
802 case SAA7134_BOARD_LEADTEK_WINFAST_DTV1000S:
803 ir_codes = RC_MAP_WINFAST;
804 mask_keycode = 0x5f00;
805 mask_keyup = 0x020000;
806 polling = 50; /* ms */
809 if (NULL == ir_codes) {
810 printk("%s: Oops: IR config error [card=%d]\n",
811 dev->name, dev->board);
815 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
816 rc = rc_allocate_device();
827 /* init hardware-specific stuff */
828 ir->mask_keycode = mask_keycode;
829 ir->mask_keydown = mask_keydown;
830 ir->mask_keyup = mask_keyup;
831 ir->polling = polling;
832 ir->rc5_gpio = rc5_gpio;
833 ir->nec_gpio = nec_gpio;
834 ir->raw_decode = raw_decode;
836 /* init input device */
837 snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
838 saa7134_boards[dev->board].name);
839 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
843 rc->open = saa7134_ir_open;
844 rc->close = saa7134_ir_close;
846 rc->driver_type = RC_DRIVER_IR_RAW;
848 if (!raw_decode && allow_protocol_change) {
849 rc->allowed_protos = IR_TYPE_RC5 | IR_TYPE_NEC;
850 rc->change_protocol = saa7134_ir_change_protocol;
853 rc->input_name = ir->name;
854 rc->input_phys = ir->phys;
855 rc->input_id.bustype = BUS_PCI;
856 rc->input_id.version = 1;
857 if (dev->pci->subsystem_vendor) {
858 rc->input_id.vendor = dev->pci->subsystem_vendor;
859 rc->input_id.product = dev->pci->subsystem_device;
861 rc->input_id.vendor = dev->pci->vendor;
862 rc->input_id.product = dev->pci->device;
864 rc->dev.parent = &dev->pci->dev;
865 rc->map_name = ir_codes;
866 rc->driver_name = MODULE_NAME;
868 err = rc_register_device(rc);
881 void saa7134_input_fini(struct saa7134_dev *dev)
883 if (NULL == dev->remote)
886 saa7134_ir_stop(dev);
887 rc_unregister_device(dev->remote->dev);
892 void saa7134_probe_i2c_ir(struct saa7134_dev *dev)
894 struct i2c_board_info info;
896 struct i2c_msg msg_msi = {
906 dprintk("IR has been disabled, not probing for i2c remote\n");
910 memset(&info, 0, sizeof(struct i2c_board_info));
911 memset(&dev->init_data, 0, sizeof(dev->init_data));
912 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
914 switch (dev->board) {
915 case SAA7134_BOARD_PINNACLE_PCTV_110i:
916 case SAA7134_BOARD_PINNACLE_PCTV_310i:
917 dev->init_data.name = "Pinnacle PCTV";
918 if (pinnacle_remote == 0) {
919 dev->init_data.get_key = get_key_pinnacle_color;
920 dev->init_data.ir_codes = RC_MAP_PINNACLE_COLOR;
923 dev->init_data.get_key = get_key_pinnacle_grey;
924 dev->init_data.ir_codes = RC_MAP_PINNACLE_GREY;
928 case SAA7134_BOARD_UPMOST_PURPLE_TV:
929 dev->init_data.name = "Purple TV";
930 dev->init_data.get_key = get_key_purpletv;
931 dev->init_data.ir_codes = RC_MAP_PURPLETV;
934 case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
935 dev->init_data.name = "MSI TV@nywhere Plus";
936 dev->init_data.get_key = get_key_msi_tvanywhere_plus;
937 dev->init_data.ir_codes = RC_MAP_MSI_TVANYWHERE_PLUS;
939 * MSI TV@nyware Plus requires more frequent polling
940 * otherwise it will miss some keypresses
942 dev->init_data.polling_interval = 50;
944 /* MSI TV@nywhere Plus controller doesn't seem to
945 respond to probes unless we read something from
946 an existing device. Weird...
947 REVISIT: might no longer be needed */
948 rc = i2c_transfer(&dev->i2c_adap, &msg_msi, 1);
949 dprintk("probe 0x%02x @ %s: %s\n",
950 msg_msi.addr, dev->i2c_adap.name,
951 (1 == rc) ? "yes" : "no");
953 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
954 dev->init_data.name = "HVR 1110";
955 dev->init_data.get_key = get_key_hvr1110;
956 dev->init_data.ir_codes = RC_MAP_HAUPPAUGE_NEW;
959 case SAA7134_BOARD_BEHOLD_607FM_MK3:
960 case SAA7134_BOARD_BEHOLD_607FM_MK5:
961 case SAA7134_BOARD_BEHOLD_609FM_MK3:
962 case SAA7134_BOARD_BEHOLD_609FM_MK5:
963 case SAA7134_BOARD_BEHOLD_607RDS_MK3:
964 case SAA7134_BOARD_BEHOLD_607RDS_MK5:
965 case SAA7134_BOARD_BEHOLD_609RDS_MK3:
966 case SAA7134_BOARD_BEHOLD_609RDS_MK5:
967 case SAA7134_BOARD_BEHOLD_M6:
968 case SAA7134_BOARD_BEHOLD_M63:
969 case SAA7134_BOARD_BEHOLD_M6_EXTRA:
970 case SAA7134_BOARD_BEHOLD_H6:
971 case SAA7134_BOARD_BEHOLD_X7:
972 case SAA7134_BOARD_BEHOLD_H7:
973 case SAA7134_BOARD_BEHOLD_A7:
974 dev->init_data.name = "BeholdTV";
975 dev->init_data.get_key = get_key_beholdm6xx;
976 dev->init_data.ir_codes = RC_MAP_BEHOLD;
977 dev->init_data.type = IR_TYPE_NEC;
980 case SAA7134_BOARD_AVERMEDIA_CARDBUS_501:
981 case SAA7134_BOARD_AVERMEDIA_CARDBUS_506:
984 case SAA7134_BOARD_FLYDVB_TRIO:
985 dev->init_data.name = "FlyDVB Trio";
986 dev->init_data.get_key = get_key_flydvb_trio;
987 dev->init_data.ir_codes = RC_MAP_FLYDVB;
991 dprintk("No I2C IR support for board %x\n", dev->board);
995 if (dev->init_data.name)
996 info.platform_data = &dev->init_data;
997 i2c_new_device(&dev->i2c_adap, &info);
1000 static int saa7134_raw_decode_irq(struct saa7134_dev *dev)
1002 struct card_ir *ir = dev->remote;
1003 unsigned long timeout;
1006 /* Generate initial event */
1007 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1008 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1009 space = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
1010 ir_raw_event_store_edge(dev->remote->dev, space ? IR_SPACE : IR_PULSE);
1014 * Wait 15 ms from the start of the first IR event before processing
1015 * the event. This time is enough for NEC protocol. May need adjustments
1016 * to work with other protocols.
1019 timeout = jiffies + jiffies_to_msecs(15);
1020 mod_timer(&ir->timer_end, timeout);
1027 static int saa7134_rc5_irq(struct saa7134_dev *dev)
1029 struct card_ir *ir = dev->remote;
1032 unsigned long current_jiffies, timeout;
1034 /* get time of bit */
1035 current_jiffies = jiffies;
1036 do_gettimeofday(&tv);
1038 /* avoid overflow with gap >1s */
1039 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
1042 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
1043 tv.tv_usec - ir->base_time.tv_usec;
1046 /* active code => add bit */
1048 /* only if in the code (otherwise spurious IRQ or timer
1050 if (ir->last_bit < 28) {
1051 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
1053 ir->code |= 1 << ir->last_bit;
1055 /* starting new code */
1062 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
1063 mod_timer(&ir->timer_end, timeout);
1069 static void nec_task(unsigned long data)
1071 struct saa7134_dev *dev = (struct saa7134_dev *) data;
1074 int count, pulse, oldpulse, gap;
1075 u32 ircode = 0, not_code = 0;
1079 printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
1080 /* GPIO will be kept disabled */
1086 /* rising SAA7134_GPIO_GPRESCAN reads the status */
1087 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1088 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1090 oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
1093 do_gettimeofday(&tv);
1096 /* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
1097 Unfortunately, using IRQ to decode pulse didn't work, since it uses
1098 a pulse train of 38KHz. This means one pulse on each 52 us
1101 /* Wait until the end of pulse/space or 5 ms */
1102 for (count = 0; count < 500; count++) {
1104 /* rising SAA7134_GPIO_GPRESCAN reads the status */
1105 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1106 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
1107 pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
1109 if (pulse != oldpulse)
1113 do_gettimeofday(&tv);
1114 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
1115 tv.tv_usec - ir->base_time.tv_usec;
1118 /* Bit 0 has 560 us, while bit 1 has 1120 us.
1119 Do something only if bit == 1
1121 if (ngap && (gap > 560 + 280)) {
1122 unsigned int shift = ngap - 1;
1124 /* Address first, then command */
1127 ircode |= 1 << shift;
1128 } else if (shift < 16) {
1129 not_code |= 1 << shift;
1130 } else if (shift < 24) {
1132 ircode |= 1 << shift;
1135 not_code |= 1 << shift;
1144 /* TIMEOUT - Long pulse */
1148 } while (ngap < 32);
1151 /* FIXME: should check if not_code == ~ircode */
1152 ir->code = ir_extract_bits(ircode, ir->mask_keycode);
1154 dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
1155 ir->code, ircode, not_code);
1157 ir_keydown(ir->dev, ir->code, 0);
1159 dprintk("Repeat last key\n");
1163 saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18_P);
1166 static int saa7134_nec_irq(struct saa7134_dev *dev)
1168 struct card_ir *ir = dev->remote;
1170 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18_P);
1171 tasklet_schedule(&ir->tlet);