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"
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->c.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;
78 /* here comes the additional handshake steps for some cards */
80 case SAA7134_BOARD_GOTVIEW_7135:
81 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x80);
82 saa_clearb(SAA7134_GPIO_GPSTATUS1, 0x80);
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);
91 if (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);
105 ir_input_keydown(ir->dev, &ir->ir, data, data);
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);
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);
128 /* --------------------- Chip specific I2C key builders ----------------- */
130 static int get_key_msi_tvanywhere_plus(struct IR_i2c *ir, u32 *ir_key,
136 /* <dev> is needed to access GPIO. Used by the saa_readl macro. */
137 struct saa7134_dev *dev = ir->c.adapter->algo_data;
139 dprintk("get_key_msi_tvanywhere_plus: "
140 "gir->c.adapter->algo_data is NULL!\n");
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. */
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");
164 /* No button press */
171 dprintk("get_key_msi_tvanywhere_plus: Key = 0x%02X\n", b);
177 static int get_key_purpletv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
182 if (1 != i2c_master_recv(&ir->c,&b,1)) {
183 i2cdprintk("read error\n");
187 /* no button press */
200 static int get_key_hvr1110(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
202 unsigned char buf[5], cod4, code3, code4;
205 if (5 != i2c_master_recv(&ir->c,buf,5))
222 static int get_key_beholdm6xx(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
224 unsigned char data[12];
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");
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
249 * So, skip not our, if disable full codes mode.
251 if (data[10] != 0x6b && data[11] != 0x86 && disable_other_ir)
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)
267 unsigned int start = 0,parity = 0,code = 0;
270 if (4 != i2c_master_recv(&ir->c, b, 4)) {
271 i2cdprintk("read error\n");
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];
287 if (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))
302 i2cdprintk("Pinnacle PCTV key %02x\n", code);
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
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;
343 saa7134_nec_irq(dev);
344 } else if (!ir->polling && !ir->rc5_gpio) {
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;
357 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
360 void saa7134_ir_start(struct saa7134_dev *dev, struct card_ir *ir)
363 setup_timer(&ir->timer, saa7134_input_timer,
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;
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,
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)
396 struct input_dev *input_dev;
397 IR_KEYTAB_TYPE *ir_codes = NULL;
398 u32 mask_keycode = 0;
399 u32 mask_keydown = 0;
404 int ir_type = IR_TYPE_OTHER;
407 if (dev->has_remote != SAA7134_REMOTE_GPIO)
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 ir_codes = ir_codes_flyvideo;
419 mask_keycode = 0xEC00000;
420 mask_keydown = 0x0040000;
422 case SAA7134_BOARD_CINERGY400:
423 case SAA7134_BOARD_CINERGY600:
424 case SAA7134_BOARD_CINERGY600_MK3:
425 ir_codes = ir_codes_cinergy;
426 mask_keycode = 0x00003f;
427 mask_keyup = 0x040000;
429 case SAA7134_BOARD_ECS_TVP3XP:
430 case SAA7134_BOARD_ECS_TVP3XP_4CB5:
431 ir_codes = ir_codes_eztv;
432 mask_keycode = 0x00017c;
433 mask_keyup = 0x000002;
436 case SAA7134_BOARD_KWORLD_XPERT:
437 case SAA7134_BOARD_AVACSSMARTTV:
438 ir_codes = ir_codes_pixelview;
439 mask_keycode = 0x00001F;
440 mask_keyup = 0x000020;
443 case SAA7134_BOARD_MD2819:
444 case SAA7134_BOARD_KWORLD_VSTREAM_XPERT:
445 case SAA7134_BOARD_AVERMEDIA_305:
446 case SAA7134_BOARD_AVERMEDIA_307:
447 case SAA7134_BOARD_AVERMEDIA_STUDIO_305:
448 case SAA7134_BOARD_AVERMEDIA_STUDIO_307:
449 case SAA7134_BOARD_AVERMEDIA_STUDIO_507:
450 case SAA7134_BOARD_AVERMEDIA_GO_007_FM:
451 case SAA7134_BOARD_AVERMEDIA_M102:
452 case SAA7134_BOARD_AVERMEDIA_GO_007_FM_PLUS:
453 ir_codes = ir_codes_avermedia;
454 mask_keycode = 0x0007C8;
455 mask_keydown = 0x000010;
457 /* Set GPIO pin2 to high to enable the IR controller */
458 saa_setb(SAA7134_GPIO_GPMODE0, 0x4);
459 saa_setb(SAA7134_GPIO_GPSTATUS0, 0x4);
461 case SAA7134_BOARD_AVERMEDIA_M135A:
462 ir_codes = ir_codes_avermedia_m135a;
463 mask_keydown = 0x0040000;
464 mask_keycode = 0x00013f;
467 case SAA7134_BOARD_AVERMEDIA_777:
468 case SAA7134_BOARD_AVERMEDIA_A16AR:
469 ir_codes = ir_codes_avermedia;
470 mask_keycode = 0x02F200;
471 mask_keydown = 0x000400;
473 /* Without this we won't receive key up events */
474 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
475 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
477 case SAA7134_BOARD_AVERMEDIA_A16D:
478 ir_codes = ir_codes_avermedia_a16d;
479 mask_keycode = 0x02F200;
480 mask_keydown = 0x000400;
481 polling = 50; /* ms */
482 /* Without this we won't receive key up events */
483 saa_setb(SAA7134_GPIO_GPMODE1, 0x1);
484 saa_setb(SAA7134_GPIO_GPSTATUS1, 0x1);
486 case SAA7134_BOARD_KWORLD_TERMINATOR:
487 ir_codes = ir_codes_pixelview;
488 mask_keycode = 0x00001f;
489 mask_keyup = 0x000060;
492 case SAA7134_BOARD_MANLI_MTV001:
493 case SAA7134_BOARD_MANLI_MTV002:
494 ir_codes = ir_codes_manli;
495 mask_keycode = 0x001f00;
496 mask_keyup = 0x004000;
497 polling = 50; /* ms */
499 case SAA7134_BOARD_BEHOLD_409FM:
500 case SAA7134_BOARD_BEHOLD_401:
501 case SAA7134_BOARD_BEHOLD_403:
502 case SAA7134_BOARD_BEHOLD_403FM:
503 case SAA7134_BOARD_BEHOLD_405:
504 case SAA7134_BOARD_BEHOLD_405FM:
505 case SAA7134_BOARD_BEHOLD_407:
506 case SAA7134_BOARD_BEHOLD_407FM:
507 case SAA7134_BOARD_BEHOLD_409:
508 case SAA7134_BOARD_BEHOLD_505FM:
509 case SAA7134_BOARD_BEHOLD_507_9FM:
510 ir_codes = ir_codes_manli;
511 mask_keycode = 0x003f00;
512 mask_keyup = 0x004000;
513 polling = 50; /* ms */
515 case SAA7134_BOARD_BEHOLD_COLUMBUS_TVFM:
516 ir_codes = ir_codes_behold_columbus;
517 mask_keycode = 0x003f00;
518 mask_keyup = 0x004000;
521 case SAA7134_BOARD_SEDNA_PC_TV_CARDBUS:
522 ir_codes = ir_codes_pctv_sedna;
523 mask_keycode = 0x001f00;
524 mask_keyup = 0x004000;
527 case SAA7134_BOARD_GOTVIEW_7135:
528 ir_codes = ir_codes_gotview7135;
529 mask_keycode = 0x0003CC;
530 mask_keydown = 0x000010;
531 polling = 5; /* ms */
532 saa_setb(SAA7134_GPIO_GPMODE1, 0x80);
534 case SAA7134_BOARD_VIDEOMATE_TV_PVR:
535 case SAA7134_BOARD_VIDEOMATE_GOLD_PLUS:
536 case SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII:
537 ir_codes = ir_codes_videomate_tv_pvr;
538 mask_keycode = 0x00003F;
539 mask_keyup = 0x400000;
542 case SAA7134_BOARD_PROTEUS_2309:
543 ir_codes = ir_codes_proteus_2309;
544 mask_keycode = 0x00007F;
545 mask_keyup = 0x000080;
548 case SAA7134_BOARD_VIDEOMATE_DVBT_300:
549 case SAA7134_BOARD_VIDEOMATE_DVBT_200:
550 ir_codes = ir_codes_videomate_tv_pvr;
551 mask_keycode = 0x003F00;
552 mask_keyup = 0x040000;
554 case SAA7134_BOARD_FLYDVBS_LR300:
555 case SAA7134_BOARD_FLYDVBT_LR301:
556 case SAA7134_BOARD_FLYDVBTDUO:
557 ir_codes = ir_codes_flydvb;
558 mask_keycode = 0x0001F00;
559 mask_keydown = 0x0040000;
561 case SAA7134_BOARD_ASUSTeK_P7131_DUAL:
562 case SAA7134_BOARD_ASUSTeK_P7131_HYBRID_LNA:
563 case SAA7134_BOARD_ASUSTeK_P7131_ANALOG:
564 ir_codes = ir_codes_asus_pc39;
565 mask_keydown = 0x0040000;
568 case SAA7134_BOARD_ENCORE_ENLTV:
569 case SAA7134_BOARD_ENCORE_ENLTV_FM:
570 ir_codes = ir_codes_encore_enltv;
571 mask_keycode = 0x00007f;
572 mask_keyup = 0x040000;
575 case SAA7134_BOARD_ENCORE_ENLTV_FM53:
576 ir_codes = ir_codes_encore_enltv_fm53;
577 mask_keydown = 0x0040000;
578 mask_keycode = 0x00007f;
581 case SAA7134_BOARD_10MOONSTVMASTER3:
582 ir_codes = ir_codes_encore_enltv;
583 mask_keycode = 0x5f80000;
584 mask_keyup = 0x8000000;
587 case SAA7134_BOARD_GENIUS_TVGO_A11MCE:
588 ir_codes = ir_codes_genius_tvgo_a11mce;
590 mask_keydown = 0xf00000;
591 polling = 50; /* ms */
593 case SAA7134_BOARD_REAL_ANGEL_220:
594 ir_codes = ir_codes_real_audio_220_32_keys;
595 mask_keycode = 0x3f00;
597 polling = 50; /* ms */
599 case SAA7134_BOARD_KWORLD_PLUS_TV_ANALOG:
600 ir_codes = ir_codes_kworld_plus_tv_analog;
602 polling = 40; /* ms */
605 if (NULL == ir_codes) {
606 printk("%s: Oops: IR config error [card=%d]\n",
607 dev->name, dev->board);
611 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
612 input_dev = input_allocate_device();
613 if (!ir || !input_dev) {
620 /* init hardware-specific stuff */
621 ir->mask_keycode = mask_keycode;
622 ir->mask_keydown = mask_keydown;
623 ir->mask_keyup = mask_keyup;
624 ir->polling = polling;
625 ir->rc5_gpio = rc5_gpio;
626 ir->nec_gpio = nec_gpio;
628 /* init input device */
629 snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
630 saa7134_boards[dev->board].name);
631 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
634 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
635 input_dev->name = ir->name;
636 input_dev->phys = ir->phys;
637 input_dev->id.bustype = BUS_PCI;
638 input_dev->id.version = 1;
639 if (dev->pci->subsystem_vendor) {
640 input_dev->id.vendor = dev->pci->subsystem_vendor;
641 input_dev->id.product = dev->pci->subsystem_device;
643 input_dev->id.vendor = dev->pci->vendor;
644 input_dev->id.product = dev->pci->device;
646 input_dev->dev.parent = &dev->pci->dev;
649 saa7134_ir_start(dev, ir);
651 err = input_register_device(ir->dev);
655 /* the remote isn't as bouncy as a keyboard */
656 ir->dev->rep[REP_DELAY] = repeat_delay;
657 ir->dev->rep[REP_PERIOD] = repeat_period;
662 saa7134_ir_stop(dev);
665 input_free_device(input_dev);
670 void saa7134_input_fini(struct saa7134_dev *dev)
672 if (NULL == dev->remote)
675 saa7134_ir_stop(dev);
676 input_unregister_device(dev->remote->dev);
681 void saa7134_set_i2c_ir(struct saa7134_dev *dev, struct IR_i2c *ir)
684 dprintk("Found supported i2c remote, but IR has been disabled\n");
689 switch (dev->board) {
690 case SAA7134_BOARD_PINNACLE_PCTV_110i:
691 case SAA7134_BOARD_PINNACLE_PCTV_310i:
692 snprintf(ir->c.name, sizeof(ir->c.name), "Pinnacle PCTV");
693 if (pinnacle_remote == 0) {
694 ir->get_key = get_key_pinnacle_color;
695 ir->ir_codes = ir_codes_pinnacle_color;
697 ir->get_key = get_key_pinnacle_grey;
698 ir->ir_codes = ir_codes_pinnacle_grey;
701 case SAA7134_BOARD_UPMOST_PURPLE_TV:
702 snprintf(ir->c.name, sizeof(ir->c.name), "Purple TV");
703 ir->get_key = get_key_purpletv;
704 ir->ir_codes = ir_codes_purpletv;
706 case SAA7134_BOARD_MSI_TVATANYWHERE_PLUS:
707 snprintf(ir->c.name, sizeof(ir->c.name), "MSI TV@nywhere Plus");
708 ir->get_key = get_key_msi_tvanywhere_plus;
709 ir->ir_codes = ir_codes_msi_tvanywhere_plus;
711 case SAA7134_BOARD_HAUPPAUGE_HVR1110:
712 snprintf(ir->c.name, sizeof(ir->c.name), "HVR 1110");
713 ir->get_key = get_key_hvr1110;
714 ir->ir_codes = ir_codes_hauppauge_new;
716 case SAA7134_BOARD_BEHOLD_607_9FM:
717 case SAA7134_BOARD_BEHOLD_M6:
718 case SAA7134_BOARD_BEHOLD_M63:
719 case SAA7134_BOARD_BEHOLD_M6_EXTRA:
720 case SAA7134_BOARD_BEHOLD_H6:
721 snprintf(ir->c.name, sizeof(ir->c.name), "BeholdTV");
722 ir->get_key = get_key_beholdm6xx;
723 ir->ir_codes = ir_codes_behold;
726 dprintk("Shouldn't get here: Unknown board %x for I2C IR?\n",dev->board);
732 static int saa7134_rc5_irq(struct saa7134_dev *dev)
734 struct card_ir *ir = dev->remote;
737 unsigned long current_jiffies, timeout;
739 /* get time of bit */
740 current_jiffies = jiffies;
741 do_gettimeofday(&tv);
743 /* avoid overflow with gap >1s */
744 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
747 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
748 tv.tv_usec - ir->base_time.tv_usec;
751 /* active code => add bit */
753 /* only if in the code (otherwise spurious IRQ or timer
755 if (ir->last_bit < 28) {
756 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
758 ir->code |= 1 << ir->last_bit;
760 /* starting new code */
767 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
768 mod_timer(&ir->timer_end, timeout);
775 /* On NEC protocol, One has 2.25 ms, and zero has 1.125 ms
776 The first pulse (start) has 9 + 4.5 ms
779 static void saa7134_nec_timer(unsigned long data)
781 struct saa7134_dev *dev = (struct saa7134_dev *) data;
782 struct card_ir *ir = dev->remote;
784 dprintk("Cancel key repeat\n");
786 ir_input_nokey(ir->dev, &ir->ir);
789 static void nec_task(unsigned long data)
791 struct saa7134_dev *dev = (struct saa7134_dev *) data;
794 int count, pulse, oldpulse, gap;
795 u32 ircode = 0, not_code = 0;
799 printk(KERN_ERR "saa713x/ir: Can't recover dev struct\n");
800 /* GPIO will be kept disabled */
806 /* rising SAA7134_GPIO_GPRESCAN reads the status */
807 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
808 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
810 oldpulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & ir->mask_keydown;
813 do_gettimeofday(&tv);
816 /* Decode NEC pulsecode. This code can take up to 76.5 ms to run.
817 Unfortunately, using IRQ to decode pulse didn't work, since it uses
818 a pulse train of 38KHz. This means one pulse on each 52 us
821 /* Wait until the end of pulse/space or 5 ms */
822 for (count = 0; count < 500; count++) {
824 /* rising SAA7134_GPIO_GPRESCAN reads the status */
825 saa_clearb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
826 saa_setb(SAA7134_GPIO_GPMODE3, SAA7134_GPIO_GPRESCAN);
827 pulse = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2)
829 if (pulse != oldpulse)
833 do_gettimeofday(&tv);
834 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
835 tv.tv_usec - ir->base_time.tv_usec;
838 /* Bit 0 has 560 us, while bit 1 has 1120 us.
839 Do something only if bit == 1
841 if (ngap && (gap > 560 + 280)) {
842 unsigned int shift = ngap - 1;
844 /* Address first, then command */
847 ircode |= 1 << shift;
848 } else if (shift < 16) {
849 not_code |= 1 << shift;
850 } else if (shift < 24) {
852 ircode |= 1 << shift;
855 not_code |= 1 << shift;
864 /* TIMEOUT - Long pulse */
871 /* FIXME: should check if not_code == ~ircode */
872 ir->code = ir_extract_bits(ircode, ir->mask_keycode);
874 dprintk("scancode = 0x%02x (code = 0x%02x, notcode= 0x%02x)\n",
875 ir->code, ircode, not_code);
877 ir_input_keydown(ir->dev, &ir->ir, ir->code, ir->code);
879 dprintk("Repeat last key\n");
881 /* Keep repeating the last key */
882 mod_timer(&ir->timer_keyup, jiffies + msecs_to_jiffies(150));
884 saa_setl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
887 static int saa7134_nec_irq(struct saa7134_dev *dev)
889 struct card_ir *ir = dev->remote;
891 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
892 tasklet_schedule(&ir->tlet);