3 * Copyright (c) 2003 Gerd Knorr
4 * Copyright (c) 2003 Pavel Machek
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/input.h>
27 #include <linux/pci.h>
29 #include <media/ir-common.h>
33 /* ---------------------------------------------------------------------- */
35 static IR_KEYTAB_TYPE ir_codes_avermedia
[IR_KEYTAB_SIZE
] = {
47 [ 48 ] = KEY_EJECTCD
, // Unmarked on my controller
49 [ 18 ] = BTN_LEFT
, // DISPLAY/L
50 [ 50 ] = BTN_RIGHT
, // LOOP/R
55 [ 30 ] = KEY_VOLUMEDOWN
,
56 [ 62 ] = KEY_VOLUMEUP
,
58 [ 32 ] = KEY_TUNER
, // TV/FM
62 [ 12 ] = KEY_ZOOM
, // full screen
63 [ 2 ] = KEY_INFO
, // preview
64 [ 42 ] = KEY_SEARCH
, // autoscan
65 [ 26 ] = KEY_STOP
, // freeze
66 [ 58 ] = KEY_RECORD
, // capture
67 [ 6 ] = KEY_PLAY
, // unmarked
68 [ 46 ] = KEY_RED
, // unmarked
69 [ 14 ] = KEY_GREEN
, // unmarked
71 [ 33 ] = KEY_YELLOW
, // unmarked
72 [ 17 ] = KEY_CHANNELDOWN
,
73 [ 49 ] = KEY_CHANNELUP
,
74 [ 1 ] = KEY_BLUE
, // unmarked
77 /* Matt Jesson <dvb@jesson.eclipse.co.uk */
78 static IR_KEYTAB_TYPE ir_codes_avermedia_dvbt
[IR_KEYTAB_SIZE
] = {
79 [ 0x28 ] = KEY_KP0
, //'0' / 'enter'
80 [ 0x22 ] = KEY_KP1
, //'1'
81 [ 0x12 ] = KEY_KP2
, //'2' / 'up arrow'
82 [ 0x32 ] = KEY_KP3
, //'3'
83 [ 0x24 ] = KEY_KP4
, //'4' / 'left arrow'
84 [ 0x14 ] = KEY_KP5
, //'5'
85 [ 0x34 ] = KEY_KP6
, //'6' / 'right arrow'
86 [ 0x26 ] = KEY_KP7
, //'7'
87 [ 0x16 ] = KEY_KP8
, //'8' / 'down arrow'
88 [ 0x36 ] = KEY_KP9
, //'9'
90 [ 0x20 ] = KEY_LIST
, // 'source'
91 [ 0x10 ] = KEY_TEXT
, // 'teletext'
92 [ 0x00 ] = KEY_POWER
, // 'power'
93 [ 0x04 ] = KEY_AUDIO
, // 'audio'
94 [ 0x06 ] = KEY_ZOOM
, // 'full screen'
95 [ 0x18 ] = KEY_VIDEO
, // 'display'
96 [ 0x38 ] = KEY_SEARCH
, // 'loop'
97 [ 0x08 ] = KEY_INFO
, // 'preview'
98 [ 0x2a ] = KEY_REWIND
, // 'backward <<'
99 [ 0x1a ] = KEY_FASTFORWARD
, // 'forward >>'
100 [ 0x3a ] = KEY_RECORD
, // 'capture'
101 [ 0x0a ] = KEY_MUTE
, // 'mute'
102 [ 0x2c ] = KEY_RECORD
, // 'record'
103 [ 0x1c ] = KEY_PAUSE
, // 'pause'
104 [ 0x3c ] = KEY_STOP
, // 'stop'
105 [ 0x0c ] = KEY_PLAY
, // 'play'
106 [ 0x2e ] = KEY_RED
, // 'red'
107 [ 0x01 ] = KEY_BLUE
, // 'blue' / 'cancel'
108 [ 0x0e ] = KEY_YELLOW
, // 'yellow' / 'ok'
109 [ 0x21 ] = KEY_GREEN
, // 'green'
110 [ 0x11 ] = KEY_CHANNELDOWN
, // 'channel -'
111 [ 0x31 ] = KEY_CHANNELUP
, // 'channel +'
112 [ 0x1e ] = KEY_VOLUMEDOWN
, // 'volume -'
113 [ 0x3e ] = KEY_VOLUMEUP
, // 'volume +'
116 /* Attila Kondoros <attila.kondoros@chello.hu> */
117 static IR_KEYTAB_TYPE ir_codes_apac_viewcomp
[IR_KEYTAB_SIZE
] = {
129 [ 23 ] = KEY_LAST
, // +100
130 [ 10 ] = KEY_LIST
, // recall
133 [ 28 ] = KEY_TUNER
, // TV/FM
134 [ 21 ] = KEY_SEARCH
, // scan
135 [ 18 ] = KEY_POWER
, // power
136 [ 31 ] = KEY_VOLUMEDOWN
, // vol up
137 [ 27 ] = KEY_VOLUMEUP
, // vol down
138 [ 30 ] = KEY_CHANNELDOWN
, // chn up
139 [ 26 ] = KEY_CHANNELUP
, // chn down
141 [ 17 ] = KEY_VIDEO
, // video
142 [ 15 ] = KEY_ZOOM
, // full screen
143 [ 19 ] = KEY_MUTE
, // mute/unmute
144 [ 16 ] = KEY_TEXT
, // min
146 [ 13 ] = KEY_STOP
, // freeze
147 [ 14 ] = KEY_RECORD
, // record
148 [ 29 ] = KEY_PLAYPAUSE
, // stop
149 [ 25 ] = KEY_PLAY
, // play
151 [ 22 ] = KEY_GOTO
, // osd
152 [ 20 ] = KEY_REFRESH
, // default
153 [ 12 ] = KEY_KPPLUS
, // fine tune >>>>
154 [ 24 ] = KEY_KPMINUS
// fine tune <<<<
157 /* ---------------------------------------------------------------------- */
159 /* Ricardo Cerqueira <v4l@cerqueira.org> */
160 /* Weird matching, since the remote has "uncommon" keys */
162 static IR_KEYTAB_TYPE ir_codes_conceptronic
[IR_KEYTAB_SIZE
] = {
164 [ 30 ] = KEY_POWER
, // power
165 [ 7 ] = KEY_MEDIA
, // source
166 [ 28 ] = KEY_SEARCH
, // scan
168 /* FIXME: duplicate keycodes?
170 * These four keys seem to share the same GPIO as CH+, CH-, <<< and >>>
171 * The GPIO values are
172 * 6397fb for both "Scan <" and "CH -",
173 * 639ffb for "Scan >" and "CH+",
174 * 6384fb for "Tune <" and "<<<",
175 * 638cfb for "Tune >" and ">>>", regardless of the mask.
177 * [ 23 ] = KEY_BACK, // fm scan <<
178 * [ 31 ] = KEY_FORWARD, // fm scan >>
180 * [ 4 ] = KEY_LEFT, // fm tuning <
181 * [ 12 ] = KEY_RIGHT, // fm tuning >
183 * For now, these four keys are disabled. Pressing them will generate
184 * the CH+/CH-/<<</>>> events
187 [ 3 ] = KEY_TUNER
, // TV/FM
193 [ 26 ] = KEY_PLAYPAUSE
, // freeze
194 [ 25 ] = KEY_ZOOM
, // zoom
195 [ 15 ] = KEY_TEXT
, // min
207 [ 16 ] = KEY_LAST
, // +100
208 [ 19 ] = KEY_LIST
, // recall
210 [ 31 ] = KEY_CHANNELUP
, // chn down
211 [ 23 ] = KEY_CHANNELDOWN
, // chn up
212 [ 22 ] = KEY_VOLUMEUP
, // vol down
213 [ 20 ] = KEY_VOLUMEDOWN
, // vol up
215 [ 4 ] = KEY_KPMINUS
, // <<<
216 [ 14 ] = KEY_SETUP
, // function
217 [ 12 ] = KEY_KPPLUS
, // >>>
219 [ 13 ] = KEY_GOTO
, // mts
220 [ 29 ] = KEY_REFRESH
, // reset
221 [ 24 ] = KEY_MUTE
// mute/unmute
224 static IR_KEYTAB_TYPE ir_codes_nebula
[IR_KEYTAB_SIZE
] = {
239 [0x0e] = KEY_MHP
, /* labelled 'Picture' */
242 [0x11] = KEY_F13
, /* 16:9 */
243 [0x12] = KEY_F14
, /* 14:9 */
252 [0x1b] = KEY_CHANNELUP
,
253 [0x1c] = KEY_CHANNELDOWN
,
254 [0x1d] = KEY_VOLUMEUP
,
255 [0x1e] = KEY_VOLUMEDOWN
,
260 [0x23] = KEY_SUBTITLE
,
261 [0x24] = KEY_F15
, /* AD */
267 [0x2a] = KEY_FASTFORWARD
,
268 [0x2b] = KEY_F16
, /* chapter */
272 [0x2f] = KEY_F17
, /* picture in picture */
273 [0x30] = KEY_KPPLUS
, /* zoom in */
274 [0x31] = KEY_KPMINUS
, /* zoom out */
275 [0x32] = KEY_F18
, /* capture */
276 [0x33] = KEY_F19
, /* web */
283 struct bttv_sub_device
*sub
;
284 struct input_dev
*input
;
285 struct ir_input_state ir
;
289 /* Usual gpio signalling */
296 struct work_struct work
;
297 struct timer_list timer
;
302 struct timer_list timer_end
; /* timer_end for code completion */
303 struct timer_list timer_keyup
; /* timer_end for key release */
304 u32 last_rc5
; /* last good rc5 code */
305 u32 last_bit
; /* last raw bit seen */
306 u32 code
; /* raw code under construction */
307 struct timeval base_time
; /* time of last seen code */
308 int active
; /* building raw code */
312 module_param(debug
, int, 0644); /* debug level (0,1,2) */
313 static int repeat_delay
= 500;
314 module_param(repeat_delay
, int, 0644);
315 static int repeat_period
= 33;
316 module_param(repeat_period
, int, 0644);
318 #define DEVNAME "ir-kbd-gpio"
319 #define dprintk(fmt, arg...) if (debug) \
320 printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
322 static void ir_irq(struct bttv_sub_device
*sub
);
323 static int ir_probe(struct device
*dev
);
324 static int ir_remove(struct device
*dev
);
326 static struct bttv_sub_driver driver
= {
335 /* ---------------------------------------------------------------------- */
337 static void ir_handle_key(struct IR
*ir
)
341 /* read gpio value */
342 gpio
= bttv_gpio_read(ir
->sub
->core
);
344 if (ir
->last_gpio
== gpio
)
346 ir
->last_gpio
= gpio
;
350 data
= ir_extract_bits(gpio
, ir
->mask_keycode
);
351 dprintk(DEVNAME
": irq gpio=0x%x code=%d | %s%s%s\n",
353 ir
->polling
? "poll" : "irq",
354 (gpio
& ir
->mask_keydown
) ? " down" : "",
355 (gpio
& ir
->mask_keyup
) ? " up" : "");
357 if (ir
->mask_keydown
) {
358 /* bit set on keydown */
359 if (gpio
& ir
->mask_keydown
) {
360 ir_input_keydown(ir
->input
, &ir
->ir
, data
, data
);
362 ir_input_nokey(ir
->input
, &ir
->ir
);
365 } else if (ir
->mask_keyup
) {
366 /* bit cleared on keydown */
367 if (0 == (gpio
& ir
->mask_keyup
)) {
368 ir_input_keydown(ir
->input
, &ir
->ir
, data
, data
);
370 ir_input_nokey(ir
->input
, &ir
->ir
);
374 /* can't disturgissh keydown/up :-/ */
375 ir_input_keydown(ir
->input
, &ir
->ir
, data
, data
);
376 ir_input_nokey(ir
->input
, &ir
->ir
);
380 static void ir_irq(struct bttv_sub_device
*sub
)
382 struct IR
*ir
= dev_get_drvdata(&sub
->dev
);
388 static void ir_timer(unsigned long data
)
390 struct IR
*ir
= (struct IR
*)data
;
392 schedule_work(&ir
->work
);
395 static void ir_work(void *data
)
397 struct IR
*ir
= data
;
398 unsigned long timeout
;
401 timeout
= jiffies
+ (ir
->polling
* HZ
/ 1000);
402 mod_timer(&ir
->timer
, timeout
);
405 /* ---------------------------------------------------------------*/
407 static int rc5_remote_gap
= 885;
408 module_param(rc5_remote_gap
, int, 0644);
409 static int rc5_key_timeout
= 200;
410 module_param(rc5_key_timeout
, int, 0644);
412 #define RC5_START(x) (((x)>>12)&3)
413 #define RC5_TOGGLE(x) (((x)>>11)&1)
414 #define RC5_ADDR(x) (((x)>>6)&31)
415 #define RC5_INSTR(x) ((x)&63)
417 /* decode raw bit pattern to RC5 code */
418 static u32
rc5_decode(unsigned int code
)
420 unsigned int org_code
= code
;
422 unsigned int rc5
= 0;
425 code
= (code
<< 1) | 1;
426 for (i
= 0; i
< 14; ++i
) {
439 dprintk("bad code: %x\n", org_code
);
443 dprintk("code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
444 "instr=%x\n", rc5
, org_code
, RC5_START(rc5
),
445 RC5_TOGGLE(rc5
), RC5_ADDR(rc5
), RC5_INSTR(rc5
));
449 static int ir_rc5_irq(struct bttv_sub_device
*sub
)
451 struct IR
*ir
= dev_get_drvdata(&sub
->dev
);
455 unsigned long current_jiffies
, timeout
;
458 gpio
= bttv_gpio_read(ir
->sub
->core
);
464 /* get time of bit */
465 current_jiffies
= jiffies
;
466 do_gettimeofday(&tv
);
468 /* avoid overflow with gap >1s */
469 if (tv
.tv_sec
- ir
->base_time
.tv_sec
> 1) {
472 gap
= 1000000 * (tv
.tv_sec
- ir
->base_time
.tv_sec
) +
473 tv
.tv_usec
- ir
->base_time
.tv_usec
;
476 /* active code => add bit */
478 /* only if in the code (otherwise spurious IRQ or timer
480 if (ir
->last_bit
< 28) {
481 ir
->last_bit
= (gap
- rc5_remote_gap
/ 2) /
483 ir
->code
|= 1 << ir
->last_bit
;
485 /* starting new code */
492 timeout
= current_jiffies
+ (500 + 30 * HZ
) / 1000;
493 mod_timer(&ir
->timer_end
, timeout
);
496 /* toggle GPIO pin 4 to reset the irq */
497 bttv_gpio_write(ir
->sub
->core
, gpio
& ~(1 << 4));
498 bttv_gpio_write(ir
->sub
->core
, gpio
| (1 << 4));
502 static void ir_rc5_timer_end(unsigned long data
)
504 struct IR
*ir
= (struct IR
*)data
;
506 unsigned long current_jiffies
, timeout
;
510 current_jiffies
= jiffies
;
511 do_gettimeofday(&tv
);
513 /* avoid overflow with gap >1s */
514 if (tv
.tv_sec
- ir
->base_time
.tv_sec
> 1) {
517 gap
= 1000000 * (tv
.tv_sec
- ir
->base_time
.tv_sec
) +
518 tv
.tv_usec
- ir
->base_time
.tv_usec
;
521 /* Allow some timmer jitter (RC5 is ~24ms anyway so this is ok) */
523 dprintk("spurious timer_end\n");
528 if (ir
->last_bit
< 20) {
529 /* ignore spurious codes (caused by light/other remotes) */
530 dprintk("short code: %x\n", ir
->code
);
532 u32 rc5
= rc5_decode(ir
->code
);
534 /* two start bits? */
535 if (RC5_START(rc5
) != 3) {
536 dprintk("rc5 start bits invalid: %u\n", RC5_START(rc5
));
539 } else if (RC5_ADDR(rc5
) == 0x0) {
540 u32 toggle
= RC5_TOGGLE(rc5
);
541 u32 instr
= RC5_INSTR(rc5
);
543 /* Good code, decide if repeat/repress */
544 if (toggle
!= RC5_TOGGLE(ir
->last_rc5
) ||
545 instr
!= RC5_INSTR(ir
->last_rc5
)) {
546 dprintk("instruction %x, toggle %x\n", instr
,
548 ir_input_nokey(ir
->input
, &ir
->ir
);
549 ir_input_keydown(ir
->input
, &ir
->ir
, instr
,
553 /* Set/reset key-up timer */
554 timeout
= current_jiffies
+ (500 + rc5_key_timeout
556 mod_timer(&ir
->timer_keyup
, timeout
);
558 /* Save code for repeat test */
564 static void ir_rc5_timer_keyup(unsigned long data
)
566 struct IR
*ir
= (struct IR
*)data
;
568 dprintk("key released\n");
569 ir_input_nokey(ir
->input
, &ir
->ir
);
572 /* ---------------------------------------------------------------------- */
574 static int ir_probe(struct device
*dev
)
576 struct bttv_sub_device
*sub
= to_bttv_sub_dev(dev
);
578 struct input_dev
*input_dev
;
579 IR_KEYTAB_TYPE
*ir_codes
= NULL
;
580 int ir_type
= IR_TYPE_OTHER
;
582 ir
= kzalloc(sizeof(*ir
), GFP_KERNEL
);
583 input_dev
= input_allocate_device();
584 if (!ir
|| !input_dev
) {
586 input_free_device(input_dev
);
590 /* detect & configure */
591 switch (sub
->core
->type
) {
592 case BTTV_BOARD_AVERMEDIA
:
593 case BTTV_BOARD_AVPHONE98
:
594 case BTTV_BOARD_AVERMEDIA98
:
595 ir_codes
= ir_codes_avermedia
;
596 ir
->mask_keycode
= 0xf88000;
597 ir
->mask_keydown
= 0x010000;
598 ir
->polling
= 50; // ms
601 case BTTV_BOARD_AVDVBT_761
:
602 case BTTV_BOARD_AVDVBT_771
:
603 ir_codes
= ir_codes_avermedia_dvbt
;
604 ir
->mask_keycode
= 0x0f00c0;
605 ir
->mask_keydown
= 0x000020;
606 ir
->polling
= 50; // ms
609 case BTTV_BOARD_PXELVWPLTVPAK
:
610 ir_codes
= ir_codes_pixelview
;
611 ir
->mask_keycode
= 0x003e00;
612 ir
->mask_keyup
= 0x010000;
613 ir
->polling
= 50; // ms
615 case BTTV_BOARD_PV_BT878P_9B
:
616 case BTTV_BOARD_PV_BT878P_PLUS
:
617 ir_codes
= ir_codes_pixelview
;
618 ir
->mask_keycode
= 0x001f00;
619 ir
->mask_keyup
= 0x008000;
620 ir
->polling
= 50; // ms
623 case BTTV_BOARD_WINFAST2000
:
624 ir_codes
= ir_codes_winfast
;
625 ir
->mask_keycode
= 0x1f8;
627 case BTTV_BOARD_MAGICTVIEW061
:
628 case BTTV_BOARD_MAGICTVIEW063
:
629 ir_codes
= ir_codes_winfast
;
630 ir
->mask_keycode
= 0x0008e000;
631 ir
->mask_keydown
= 0x00200000;
633 case BTTV_BOARD_APAC_VIEWCOMP
:
634 ir_codes
= ir_codes_apac_viewcomp
;
635 ir
->mask_keycode
= 0x001f00;
636 ir
->mask_keyup
= 0x008000;
637 ir
->polling
= 50; // ms
639 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2
:
640 ir_codes
= ir_codes_conceptronic
;
641 ir
->mask_keycode
= 0x001F00;
642 ir
->mask_keyup
= 0x006000;
643 ir
->polling
= 50; // ms
645 case BTTV_BOARD_NEBULA_DIGITV
:
646 ir_codes
= ir_codes_nebula
;
647 driver
.any_irq
= ir_rc5_irq
;
648 driver
.gpio_irq
= NULL
;
652 if (NULL
== ir_codes
) {
654 input_free_device(input_dev
);
660 /* enable remote irq */
661 bttv_gpio_inout(sub
->core
, (1 << 4), 1 << 4);
662 gpio
= bttv_gpio_read(sub
->core
);
663 bttv_gpio_write(sub
->core
, gpio
& ~(1 << 4));
664 bttv_gpio_write(sub
->core
, gpio
| (1 << 4));
666 /* init hardware-specific stuff */
667 bttv_gpio_inout(sub
->core
, ir
->mask_keycode
| ir
->mask_keydown
, 0);
670 /* init input device */
671 snprintf(ir
->name
, sizeof(ir
->name
), "bttv IR (card=%d)",
673 snprintf(ir
->phys
, sizeof(ir
->phys
), "pci-%s/ir0",
674 pci_name(sub
->core
->pci
));
676 ir_input_init(input_dev
, &ir
->ir
, ir_type
, ir_codes
);
677 input_dev
->name
= ir
->name
;
678 input_dev
->phys
= ir
->phys
;
679 input_dev
->id
.bustype
= BUS_PCI
;
680 input_dev
->id
.version
= 1;
681 if (sub
->core
->pci
->subsystem_vendor
) {
682 input_dev
->id
.vendor
= sub
->core
->pci
->subsystem_vendor
;
683 input_dev
->id
.product
= sub
->core
->pci
->subsystem_device
;
685 input_dev
->id
.vendor
= sub
->core
->pci
->vendor
;
686 input_dev
->id
.product
= sub
->core
->pci
->device
;
688 input_dev
->cdev
.dev
= &sub
->core
->pci
->dev
;
690 ir
->input
= input_dev
;
694 INIT_WORK(&ir
->work
, ir_work
, ir
);
695 init_timer(&ir
->timer
);
696 ir
->timer
.function
= ir_timer
;
697 ir
->timer
.data
= (unsigned long)ir
;
698 schedule_work(&ir
->work
);
699 } else if (ir
->rc5_gpio
) {
700 /* set timer_end for code completion */
701 init_timer(&ir
->timer_end
);
702 ir
->timer_end
.function
= ir_rc5_timer_end
;
703 ir
->timer_end
.data
= (unsigned long)ir
;
705 init_timer(&ir
->timer_keyup
);
706 ir
->timer_keyup
.function
= ir_rc5_timer_keyup
;
707 ir
->timer_keyup
.data
= (unsigned long)ir
;
711 dev_set_drvdata(dev
, ir
);
712 input_register_device(ir
->input
);
714 /* the remote isn't as bouncy as a keyboard */
715 ir
->input
->rep
[REP_DELAY
] = repeat_delay
;
716 ir
->input
->rep
[REP_PERIOD
] = repeat_period
;
721 static int ir_remove(struct device
*dev
)
723 struct IR
*ir
= dev_get_drvdata(dev
);
726 del_timer(&ir
->timer
);
727 flush_scheduled_work();
732 del_timer(&ir
->timer_end
);
733 flush_scheduled_work();
735 gpio
= bttv_gpio_read(ir
->sub
->core
);
736 bttv_gpio_write(ir
->sub
->core
, gpio
& ~(1 << 4));
739 input_unregister_device(ir
->input
);
744 /* ---------------------------------------------------------------------- */
746 MODULE_AUTHOR("Gerd Knorr, Pavel Machek");
747 MODULE_DESCRIPTION("input driver for bt8x8 gpio IR remote controls");
748 MODULE_LICENSE("GPL");
750 static int ir_init(void)
752 return bttv_sub_register(&driver
, "remote");
755 static void ir_fini(void)
757 bttv_sub_unregister(&driver
);
760 module_init(ir_init
);
761 module_exit(ir_fini
);