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
225 struct bttv_sub_device *sub;
226 struct input_dev *input;
227 struct ir_input_state ir;
236 struct work_struct work;
237 struct timer_list timer;
241 module_param(debug, int, 0644); /* debug level (0,1,2) */
243 #define DEVNAME "ir-kbd-gpio"
244 #define dprintk(fmt, arg...) if (debug) \
245 printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
247 static void ir_irq(struct bttv_sub_device *sub);
248 static int ir_probe(struct device *dev);
249 static int ir_remove(struct device *dev);
251 static struct bttv_sub_driver driver = {
260 /* ---------------------------------------------------------------------- */
262 static void ir_handle_key(struct IR *ir)
266 /* read gpio value */
267 gpio = bttv_gpio_read(ir->sub->core);
269 if (ir->last_gpio == gpio)
271 ir->last_gpio = gpio;
275 data = ir_extract_bits(gpio, ir->mask_keycode);
276 dprintk(DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
278 ir->polling ? "poll" : "irq",
279 (gpio & ir->mask_keydown) ? " down" : "",
280 (gpio & ir->mask_keyup) ? " up" : "");
282 if (ir->mask_keydown) {
283 /* bit set on keydown */
284 if (gpio & ir->mask_keydown) {
285 ir_input_keydown(ir->input, &ir->ir, data, data);
287 ir_input_nokey(ir->input, &ir->ir);
290 } else if (ir->mask_keyup) {
291 /* bit cleared on keydown */
292 if (0 == (gpio & ir->mask_keyup)) {
293 ir_input_keydown(ir->input, &ir->ir, data, data);
295 ir_input_nokey(ir->input, &ir->ir);
299 /* can't disturgissh keydown/up :-/ */
300 ir_input_keydown(ir->input, &ir->ir, data, data);
301 ir_input_nokey(ir->input, &ir->ir);
305 static void ir_irq(struct bttv_sub_device *sub)
307 struct IR *ir = dev_get_drvdata(&sub->dev);
313 static void ir_timer(unsigned long data)
315 struct IR *ir = (struct IR*)data;
317 schedule_work(&ir->work);
320 static void ir_work(void *data)
322 struct IR *ir = data;
323 unsigned long timeout;
326 timeout = jiffies + (ir->polling * HZ / 1000);
327 mod_timer(&ir->timer, timeout);
330 /* ---------------------------------------------------------------------- */
332 static int ir_probe(struct device *dev)
334 struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
336 struct input_dev *input_dev;
337 IR_KEYTAB_TYPE *ir_codes = NULL;
338 int ir_type = IR_TYPE_OTHER;
340 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
341 input_dev = input_allocate_device();
342 if (!ir || !input_dev) {
344 input_free_device(input_dev);
348 /* detect & configure */
349 switch (sub->core->type) {
350 case BTTV_BOARD_AVERMEDIA:
351 case BTTV_BOARD_AVPHONE98:
352 case BTTV_BOARD_AVERMEDIA98:
353 ir_codes = ir_codes_avermedia;
354 ir->mask_keycode = 0xf88000;
355 ir->mask_keydown = 0x010000;
356 ir->polling = 50; // ms
359 case BTTV_BOARD_AVDVBT_761:
360 case BTTV_BOARD_AVDVBT_771:
361 ir_codes = ir_codes_avermedia_dvbt;
362 ir->mask_keycode = 0x0f00c0;
363 ir->mask_keydown = 0x000020;
364 ir->polling = 50; // ms
367 case BTTV_BOARD_PXELVWPLTVPAK:
368 ir_codes = ir_codes_pixelview;
369 ir->mask_keycode = 0x003e00;
370 ir->mask_keyup = 0x010000;
371 ir->polling = 50; // ms
373 case BTTV_BOARD_PV_BT878P_9B:
374 case BTTV_BOARD_PV_BT878P_PLUS:
375 ir_codes = ir_codes_pixelview;
376 ir->mask_keycode = 0x001f00;
377 ir->mask_keyup = 0x008000;
378 ir->polling = 50; // ms
381 case BTTV_BOARD_WINFAST2000:
382 ir_codes = ir_codes_winfast;
383 ir->mask_keycode = 0x1f8;
385 case BTTV_BOARD_MAGICTVIEW061:
386 case BTTV_BOARD_MAGICTVIEW063:
387 ir_codes = ir_codes_winfast;
388 ir->mask_keycode = 0x0008e000;
389 ir->mask_keydown = 0x00200000;
391 case BTTV_BOARD_APAC_VIEWCOMP:
392 ir_codes = ir_codes_apac_viewcomp;
393 ir->mask_keycode = 0x001f00;
394 ir->mask_keyup = 0x008000;
395 ir->polling = 50; // ms
397 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
398 ir_codes = ir_codes_conceptronic;
399 ir->mask_keycode = 0x001F00;
400 ir->mask_keyup = 0x006000;
401 ir->polling = 50; // ms
404 if (NULL == ir_codes) {
406 input_free_device(input_dev);
410 /* init hardware-specific stuff */
411 bttv_gpio_inout(sub->core, ir->mask_keycode | ir->mask_keydown, 0);
414 /* init input device */
415 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
417 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
418 pci_name(sub->core->pci));
420 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
421 input_dev->name = ir->name;
422 input_dev->phys = ir->phys;
423 input_dev->id.bustype = BUS_PCI;
424 input_dev->id.version = 1;
425 if (sub->core->pci->subsystem_vendor) {
426 input_dev->id.vendor = sub->core->pci->subsystem_vendor;
427 input_dev->id.product = sub->core->pci->subsystem_device;
429 input_dev->id.vendor = sub->core->pci->vendor;
430 input_dev->id.product = sub->core->pci->device;
432 input_dev->cdev.dev = &sub->core->pci->dev;
435 INIT_WORK(&ir->work, ir_work, ir);
436 init_timer(&ir->timer);
437 ir->timer.function = ir_timer;
438 ir->timer.data = (unsigned long)ir;
439 schedule_work(&ir->work);
443 dev_set_drvdata(dev, ir);
444 input_register_device(ir->input);
449 static int ir_remove(struct device *dev)
451 struct IR *ir = dev_get_drvdata(dev);
454 del_timer(&ir->timer);
455 flush_scheduled_work();
458 input_unregister_device(ir->input);
463 /* ---------------------------------------------------------------------- */
465 MODULE_AUTHOR("Gerd Knorr, Pavel Machek");
466 MODULE_DESCRIPTION("input driver for bt8x8 gpio IR remote controls");
467 MODULE_LICENSE("GPL");
469 static int ir_init(void)
471 return bttv_sub_register(&driver, "remote");
474 static void ir_fini(void)
476 bttv_sub_unregister(&driver);
479 module_init(ir_init);
480 module_exit(ir_fini);