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;
301 struct timer_list timer_end; /* timer_end for code completion */
302 struct timer_list timer_keyup; /* timer_end for key release */
303 u32 last_rc5; /* last good rc5 code */
304 u32 last_bit; /* last raw bit seen */
305 u32 code; /* raw code under construction */
306 struct timeval base_time; /* time of last seen code */
307 int active; /* building raw code */
311 module_param(debug, int, 0644); /* debug level (0,1,2) */
312 static int repeat_delay = 500;
313 module_param(repeat_delay, int, 0644);
314 static int repeat_period = 33;
315 module_param(repeat_period, int, 0644);
317 #define DEVNAME "ir-kbd-gpio"
318 #define dprintk(fmt, arg...) if (debug) \
319 printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
321 static void ir_irq(struct bttv_sub_device *sub);
322 static int ir_probe(struct device *dev);
323 static int ir_remove(struct device *dev);
325 static struct bttv_sub_driver driver = {
334 /* ---------------------------------------------------------------------- */
336 static void ir_handle_key(struct IR *ir)
340 /* read gpio value */
341 gpio = bttv_gpio_read(ir->sub->core);
343 if (ir->last_gpio == gpio)
345 ir->last_gpio = gpio;
349 data = ir_extract_bits(gpio, ir->mask_keycode);
350 dprintk(DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
352 ir->polling ? "poll" : "irq",
353 (gpio & ir->mask_keydown) ? " down" : "",
354 (gpio & ir->mask_keyup) ? " up" : "");
356 if (ir->mask_keydown) {
357 /* bit set on keydown */
358 if (gpio & ir->mask_keydown) {
359 ir_input_keydown(ir->input, &ir->ir, data, data);
361 ir_input_nokey(ir->input, &ir->ir);
364 } else if (ir->mask_keyup) {
365 /* bit cleared on keydown */
366 if (0 == (gpio & ir->mask_keyup)) {
367 ir_input_keydown(ir->input, &ir->ir, data, data);
369 ir_input_nokey(ir->input, &ir->ir);
373 /* can't disturgissh keydown/up :-/ */
374 ir_input_keydown(ir->input, &ir->ir, data, data);
375 ir_input_nokey(ir->input, &ir->ir);
379 static void ir_irq(struct bttv_sub_device *sub)
381 struct IR *ir = dev_get_drvdata(&sub->dev);
387 static void ir_timer(unsigned long data)
389 struct IR *ir = (struct IR*)data;
391 schedule_work(&ir->work);
394 static void ir_work(void *data)
396 struct IR *ir = data;
397 unsigned long timeout;
400 timeout = jiffies + (ir->polling * HZ / 1000);
401 mod_timer(&ir->timer, timeout);
404 /* ---------------------------------------------------------------*/
406 static int rc5_remote_gap = 885;
407 module_param(rc5_remote_gap, int, 0644);
408 static int rc5_key_timeout = 200;
409 module_param(rc5_key_timeout, int, 0644);
411 #define RC5_START(x) (((x)>>12)&3)
412 #define RC5_TOGGLE(x) (((x)>>11)&1)
413 #define RC5_ADDR(x) (((x)>>6)&31)
414 #define RC5_INSTR(x) ((x)&63)
416 /* decode raw bit pattern to RC5 code */
417 static u32 rc5_decode(unsigned int code)
419 unsigned int org_code = code;
421 unsigned int rc5 = 0;
424 code = (code << 1) | 1;
425 for (i = 0; i < 14; ++i) {
438 dprintk("bad code: %x\n", org_code);
442 dprintk("code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
443 "instr=%x\n", rc5, org_code, RC5_START(rc5),
444 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
448 static int ir_rc5_irq(struct bttv_sub_device *sub)
450 struct IR *ir = dev_get_drvdata(&sub->dev);
454 unsigned long current_jiffies, timeout;
457 gpio = bttv_gpio_read(ir->sub->core);
463 /* get time of bit */
464 current_jiffies = jiffies;
465 do_gettimeofday(&tv);
467 /* avoid overflow with gap >1s */
468 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
471 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
472 tv.tv_usec - ir->base_time.tv_usec;
475 /* active code => add bit */
477 /* only if in the code (otherwise spurious IRQ or timer
479 if (ir->last_bit < 28) {
480 ir->last_bit = (gap - rc5_remote_gap / 2) /
482 ir->code |= 1 << ir->last_bit;
484 /* starting new code */
491 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
492 mod_timer(&ir->timer_end, timeout);
495 /* toggle GPIO pin 4 to reset the irq */
496 bttv_gpio_write(ir->sub->core, gpio & ~(1 << 4));
497 bttv_gpio_write(ir->sub->core, gpio | (1 << 4));
501 static void ir_rc5_timer_end(unsigned long data)
503 struct IR *ir = (struct IR *)data;
505 unsigned long current_jiffies, timeout;
509 current_jiffies = jiffies;
510 do_gettimeofday(&tv);
512 /* avoid overflow with gap >1s */
513 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
516 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
517 tv.tv_usec - ir->base_time.tv_usec;
520 /* Allow some timmer jitter (RC5 is ~24ms anyway so this is ok) */
522 dprintk("spurious timer_end\n");
527 if (ir->last_bit < 20) {
528 /* ignore spurious codes (caused by light/other remotes) */
529 dprintk("short code: %x\n", ir->code);
531 u32 rc5 = rc5_decode(ir->code);
533 /* two start bits? */
534 if (RC5_START(rc5) != 3) {
535 dprintk("rc5 start bits invalid: %u\n", RC5_START(rc5));
538 } else if (RC5_ADDR(rc5) == 0x0) {
539 u32 toggle = RC5_TOGGLE(rc5);
540 u32 instr = RC5_INSTR(rc5);
542 /* Good code, decide if repeat/repress */
543 if (toggle != RC5_TOGGLE(ir->last_rc5) ||
544 instr != RC5_INSTR(ir->last_rc5)) {
545 dprintk("instruction %x, toggle %x\n", instr,
547 ir_input_nokey(ir->input, &ir->ir);
548 ir_input_keydown(ir->input, &ir->ir, instr,
552 /* Set/reset key-up timer */
553 timeout = current_jiffies + (500 + rc5_key_timeout
555 mod_timer(&ir->timer_keyup, timeout);
557 /* Save code for repeat test */
563 static void ir_rc5_timer_keyup(unsigned long data)
565 struct IR *ir = (struct IR *)data;
567 dprintk("key released\n");
568 ir_input_nokey(ir->input, &ir->ir);
571 /* ---------------------------------------------------------------------- */
573 static int ir_probe(struct device *dev)
575 struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
577 struct input_dev *input_dev;
578 IR_KEYTAB_TYPE *ir_codes = NULL;
579 int ir_type = IR_TYPE_OTHER;
581 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
582 input_dev = input_allocate_device();
583 if (!ir || !input_dev) {
585 input_free_device(input_dev);
589 /* detect & configure */
590 switch (sub->core->type) {
591 case BTTV_BOARD_AVERMEDIA:
592 case BTTV_BOARD_AVPHONE98:
593 case BTTV_BOARD_AVERMEDIA98:
594 ir_codes = ir_codes_avermedia;
595 ir->mask_keycode = 0xf88000;
596 ir->mask_keydown = 0x010000;
597 ir->polling = 50; // ms
600 case BTTV_BOARD_AVDVBT_761:
601 case BTTV_BOARD_AVDVBT_771:
602 ir_codes = ir_codes_avermedia_dvbt;
603 ir->mask_keycode = 0x0f00c0;
604 ir->mask_keydown = 0x000020;
605 ir->polling = 50; // ms
608 case BTTV_BOARD_PXELVWPLTVPAK:
609 ir_codes = ir_codes_pixelview;
610 ir->mask_keycode = 0x003e00;
611 ir->mask_keyup = 0x010000;
612 ir->polling = 50; // ms
614 case BTTV_BOARD_PV_BT878P_9B:
615 case BTTV_BOARD_PV_BT878P_PLUS:
616 ir_codes = ir_codes_pixelview;
617 ir->mask_keycode = 0x001f00;
618 ir->mask_keyup = 0x008000;
619 ir->polling = 50; // ms
622 case BTTV_BOARD_WINFAST2000:
623 ir_codes = ir_codes_winfast;
624 ir->mask_keycode = 0x1f8;
626 case BTTV_BOARD_MAGICTVIEW061:
627 case BTTV_BOARD_MAGICTVIEW063:
628 ir_codes = ir_codes_winfast;
629 ir->mask_keycode = 0x0008e000;
630 ir->mask_keydown = 0x00200000;
632 case BTTV_BOARD_APAC_VIEWCOMP:
633 ir_codes = ir_codes_apac_viewcomp;
634 ir->mask_keycode = 0x001f00;
635 ir->mask_keyup = 0x008000;
636 ir->polling = 50; // ms
638 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
639 ir_codes = ir_codes_conceptronic;
640 ir->mask_keycode = 0x001F00;
641 ir->mask_keyup = 0x006000;
642 ir->polling = 50; // ms
644 case BTTV_BOARD_NEBULA_DIGITV:
645 ir_codes = ir_codes_nebula;
646 driver.any_irq = ir_rc5_irq;
647 driver.gpio_irq = NULL;
651 if (NULL == ir_codes) {
653 input_free_device(input_dev);
659 /* enable remote irq */
660 bttv_gpio_inout(sub->core, (1 << 4), 1 << 4);
661 gpio = bttv_gpio_read(sub->core);
662 bttv_gpio_write(sub->core, gpio & ~(1 << 4));
663 bttv_gpio_write(sub->core, gpio | (1 << 4));
665 /* init hardware-specific stuff */
666 bttv_gpio_inout(sub->core, ir->mask_keycode | ir->mask_keydown, 0);
669 /* init input device */
670 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
672 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
673 pci_name(sub->core->pci));
675 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
676 input_dev->name = ir->name;
677 input_dev->phys = ir->phys;
678 input_dev->id.bustype = BUS_PCI;
679 input_dev->id.version = 1;
680 if (sub->core->pci->subsystem_vendor) {
681 input_dev->id.vendor = sub->core->pci->subsystem_vendor;
682 input_dev->id.product = sub->core->pci->subsystem_device;
684 input_dev->id.vendor = sub->core->pci->vendor;
685 input_dev->id.product = sub->core->pci->device;
687 input_dev->cdev.dev = &sub->core->pci->dev;
689 ir->input = input_dev;
693 INIT_WORK(&ir->work, ir_work, ir);
694 init_timer(&ir->timer);
695 ir->timer.function = ir_timer;
696 ir->timer.data = (unsigned long)ir;
697 schedule_work(&ir->work);
698 } else if (ir->rc5_gpio) {
699 /* set timer_end for code completion */
700 init_timer(&ir->timer_end);
701 ir->timer_end.function = ir_rc5_timer_end;
702 ir->timer_end.data = (unsigned long)ir;
704 init_timer(&ir->timer_keyup);
705 ir->timer_keyup.function = ir_rc5_timer_keyup;
706 ir->timer_keyup.data = (unsigned long)ir;
710 dev_set_drvdata(dev, ir);
711 input_register_device(ir->input);
713 /* the remote isn't as bouncy as a keyboard */
714 ir->input->rep[REP_DELAY] = repeat_delay;
715 ir->input->rep[REP_PERIOD] = repeat_period;
720 static int ir_remove(struct device *dev)
722 struct IR *ir = dev_get_drvdata(dev);
725 del_timer(&ir->timer);
726 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);