2 * $Id: ir-kbd-gpio.c,v 1.11 2004/10/25 11:26:36 kraxel Exp $
4 * Copyright (c) 2003 Gerd Knorr
5 * Copyright (c) 2003 Pavel Machek
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/interrupt.h>
27 #include <linux/input.h>
28 #include <linux/pci.h>
30 #include <media/ir-common.h>
34 /* ---------------------------------------------------------------------- */
36 static IR_KEYTAB_TYPE ir_codes_avermedia[IR_KEYTAB_SIZE] = {
48 [ 48 ] = KEY_EJECTCD, // Unmarked on my controller
50 [ 18 ] = BTN_LEFT, // DISPLAY/L
51 [ 50 ] = BTN_RIGHT, // LOOP/R
56 [ 30 ] = KEY_VOLUMEDOWN,
57 [ 62 ] = KEY_VOLUMEUP,
59 [ 32 ] = KEY_TUNER, // TV/FM
63 [ 12 ] = KEY_ZOOM, // full screen
64 [ 2 ] = KEY_INFO, // preview
65 [ 42 ] = KEY_SEARCH, // autoscan
66 [ 26 ] = KEY_STOP, // freeze
67 [ 58 ] = KEY_RECORD, // capture
68 [ 6 ] = KEY_PLAY, // unmarked
69 [ 46 ] = KEY_RED, // unmarked
70 [ 14 ] = KEY_GREEN, // unmarked
72 [ 33 ] = KEY_YELLOW, // unmarked
73 [ 17 ] = KEY_CHANNELDOWN,
74 [ 49 ] = KEY_CHANNELUP,
75 [ 1 ] = KEY_BLUE, // unmarked
78 /* Matt Jesson <dvb@jesson.eclipse.co.uk */
79 static IR_KEYTAB_TYPE ir_codes_avermedia_dvbt[IR_KEYTAB_SIZE] = {
80 [ 0x28 ] = KEY_KP0, //'0' / 'enter'
81 [ 0x22 ] = KEY_KP1, //'1'
82 [ 0x12 ] = KEY_KP2, //'2' / 'up arrow'
83 [ 0x32 ] = KEY_KP3, //'3'
84 [ 0x24 ] = KEY_KP4, //'4' / 'left arrow'
85 [ 0x14 ] = KEY_KP5, //'5'
86 [ 0x34 ] = KEY_KP6, //'6' / 'right arrow'
87 [ 0x26 ] = KEY_KP7, //'7'
88 [ 0x16 ] = KEY_KP8, //'8' / 'down arrow'
89 [ 0x36 ] = KEY_KP9, //'9'
91 [ 0x20 ] = KEY_LIST, // 'source'
92 [ 0x10 ] = KEY_TEXT, // 'teletext'
93 [ 0x00 ] = KEY_POWER, // 'power'
94 [ 0x04 ] = KEY_AUDIO, // 'audio'
95 [ 0x06 ] = KEY_ZOOM, // 'full screen'
96 [ 0x18 ] = KEY_VIDEO, // 'display'
97 [ 0x38 ] = KEY_SEARCH, // 'loop'
98 [ 0x08 ] = KEY_INFO, // 'preview'
99 [ 0x2a ] = KEY_REWIND, // 'backward <<'
100 [ 0x1a ] = KEY_FASTFORWARD, // 'forward >>'
101 [ 0x3a ] = KEY_RECORD, // 'capture'
102 [ 0x0a ] = KEY_MUTE, // 'mute'
103 [ 0x2c ] = KEY_RECORD, // 'record'
104 [ 0x1c ] = KEY_PAUSE, // 'pause'
105 [ 0x3c ] = KEY_STOP, // 'stop'
106 [ 0x0c ] = KEY_PLAY, // 'play'
107 [ 0x2e ] = KEY_RED, // 'red'
108 [ 0x01 ] = KEY_BLUE, // 'blue' / 'cancel'
109 [ 0x0e ] = KEY_YELLOW, // 'yellow' / 'ok'
110 [ 0x21 ] = KEY_GREEN, // 'green'
111 [ 0x11 ] = KEY_CHANNELDOWN, // 'channel -'
112 [ 0x31 ] = KEY_CHANNELUP, // 'channel +'
113 [ 0x1e ] = KEY_VOLUMEDOWN, // 'volume -'
114 [ 0x3e ] = KEY_VOLUMEUP, // 'volume +'
117 static IR_KEYTAB_TYPE winfast_codes[IR_KEYTAB_SIZE] = {
130 // [ 27 ] = MTS button
131 [ 2 ] = KEY_TUNER, // TV/FM
133 // [ 22 ] = display button
134 [ 4 ] = KEY_VOLUMEUP,
135 [ 8 ] = KEY_VOLUMEDOWN,
136 [ 12 ] = KEY_CHANNELUP,
137 [ 16 ] = KEY_CHANNELDOWN,
138 [ 3 ] = KEY_ZOOM, // fullscreen
139 [ 31 ] = KEY_SUBTITLE, // closed caption/teletext
147 [ 24 ] = KEY_KPPLUS, //fine tune +
148 [ 25 ] = KEY_KPMINUS, //fine tune -
149 // [ 42 ] = picture in picture
151 [ 19 ] = KEY_KPENTER,
154 [ 35 ] = KEY_PLAYPAUSE,
156 // [ 37 ] = time shifting
162 static IR_KEYTAB_TYPE ir_codes_pixelview[IR_KEYTAB_SIZE] = {
174 [ 3 ] = KEY_TUNER, // TV/FM
175 [ 7 ] = KEY_SEARCH, // scan
176 [ 28 ] = KEY_ZOOM, // full screen
178 [ 23 ] = KEY_VOLUMEDOWN,
179 [ 31 ] = KEY_VOLUMEUP,
180 [ 20 ] = KEY_CHANNELDOWN,
181 [ 22 ] = KEY_CHANNELUP,
184 [ 0 ] = KEY_LIST, // source
185 [ 19 ] = KEY_INFO, // loop
186 [ 16 ] = KEY_LAST, // +100
187 [ 13 ] = KEY_CLEAR, // reset
188 [ 12 ] = BTN_RIGHT, // fun++
189 [ 4 ] = BTN_LEFT, // fun--
190 [ 14 ] = KEY_GOTO, // function
191 [ 15 ] = KEY_STOP, // freeze
194 /* Attila Kondoros <attila.kondoros@chello.hu> */
195 static IR_KEYTAB_TYPE ir_codes_apac_viewcomp[IR_KEYTAB_SIZE] = {
207 [ 23 ] = KEY_LAST, // +100
208 [ 10 ] = KEY_LIST, // recall
211 [ 28 ] = KEY_TUNER, // TV/FM
212 [ 21 ] = KEY_SEARCH, // scan
213 [ 18 ] = KEY_POWER, // power
214 [ 31 ] = KEY_VOLUMEDOWN, // vol up
215 [ 27 ] = KEY_VOLUMEUP, // vol down
216 [ 30 ] = KEY_CHANNELDOWN, // chn up
217 [ 26 ] = KEY_CHANNELUP, // chn down
219 [ 17 ] = KEY_VIDEO, // video
220 [ 15 ] = KEY_ZOOM, // full screen
221 [ 19 ] = KEY_MUTE, // mute/unmute
222 [ 16 ] = KEY_TEXT, // min
224 [ 13 ] = KEY_STOP, // freeze
225 [ 14 ] = KEY_RECORD, // record
226 [ 29 ] = KEY_PLAYPAUSE, // stop
227 [ 25 ] = KEY_PLAY, // play
229 [ 22 ] = KEY_GOTO, // osd
230 [ 20 ] = KEY_REFRESH, // default
231 [ 12 ] = KEY_KPPLUS, // fine tune >>>>
232 [ 24 ] = KEY_KPMINUS // fine tune <<<<
235 /* ---------------------------------------------------------------------- */
238 struct bttv_sub_device *sub;
239 struct input_dev input;
240 struct ir_input_state ir;
249 struct work_struct work;
250 struct timer_list timer;
254 module_param(debug, int, 0644); /* debug level (0,1,2) */
256 #define DEVNAME "ir-kbd-gpio"
257 #define dprintk(fmt, arg...) if (debug) \
258 printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
260 static void ir_irq(struct bttv_sub_device *sub);
261 static int ir_probe(struct device *dev);
262 static int ir_remove(struct device *dev);
264 static struct bttv_sub_driver driver = {
273 /* ---------------------------------------------------------------------- */
275 static void ir_handle_key(struct IR *ir)
279 /* read gpio value */
280 gpio = bttv_gpio_read(ir->sub->core);
282 if (ir->last_gpio == gpio)
284 ir->last_gpio = gpio;
288 data = ir_extract_bits(gpio, ir->mask_keycode);
289 dprintk(DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
291 ir->polling ? "poll" : "irq",
292 (gpio & ir->mask_keydown) ? " down" : "",
293 (gpio & ir->mask_keyup) ? " up" : "");
295 if (ir->mask_keydown) {
296 /* bit set on keydown */
297 if (gpio & ir->mask_keydown) {
298 ir_input_keydown(&ir->input,&ir->ir,data,data);
300 ir_input_nokey(&ir->input,&ir->ir);
303 } else if (ir->mask_keyup) {
304 /* bit cleared on keydown */
305 if (0 == (gpio & ir->mask_keyup)) {
306 ir_input_keydown(&ir->input,&ir->ir,data,data);
308 ir_input_nokey(&ir->input,&ir->ir);
312 /* can't disturgissh keydown/up :-/ */
313 ir_input_keydown(&ir->input,&ir->ir,data,data);
314 ir_input_nokey(&ir->input,&ir->ir);
318 static void ir_irq(struct bttv_sub_device *sub)
320 struct IR *ir = dev_get_drvdata(&sub->dev);
326 static void ir_timer(unsigned long data)
328 struct IR *ir = (struct IR*)data;
330 schedule_work(&ir->work);
333 static void ir_work(void *data)
335 struct IR *ir = data;
336 unsigned long timeout;
339 timeout = jiffies + (ir->polling * HZ / 1000);
340 mod_timer(&ir->timer, timeout);
343 /* ---------------------------------------------------------------------- */
345 static int ir_probe(struct device *dev)
347 struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
349 IR_KEYTAB_TYPE *ir_codes = NULL;
350 int ir_type = IR_TYPE_OTHER;
352 ir = kmalloc(sizeof(*ir),GFP_KERNEL);
355 memset(ir,0,sizeof(*ir));
357 /* detect & configure */
358 switch (sub->core->type) {
361 case BTTV_AVERMEDIA98:
362 ir_codes = ir_codes_avermedia;
363 ir->mask_keycode = 0xf88000;
364 ir->mask_keydown = 0x010000;
365 ir->polling = 50; // ms
368 case BTTV_AVDVBT_761:
369 case BTTV_AVDVBT_771:
370 ir_codes = ir_codes_avermedia_dvbt;
371 ir->mask_keycode = 0x0f00c0;
372 ir->mask_keydown = 0x000020;
373 ir->polling = 50; // ms
376 case BTTV_PXELVWPLTVPAK:
377 ir_codes = ir_codes_pixelview;
378 ir->mask_keycode = 0x003e00;
379 ir->mask_keyup = 0x010000;
380 ir->polling = 50; // ms
382 case BTTV_PV_BT878P_9B:
383 case BTTV_PV_BT878P_PLUS:
384 ir_codes = ir_codes_pixelview;
385 ir->mask_keycode = 0x001f00;
386 ir->mask_keyup = 0x008000;
387 ir->polling = 50; // ms
390 case BTTV_WINFAST2000:
391 ir_codes = winfast_codes;
392 ir->mask_keycode = 0x1f8;
394 case BTTV_MAGICTVIEW061:
395 case BTTV_MAGICTVIEW063:
396 ir_codes = winfast_codes;
397 ir->mask_keycode = 0x0008e000;
398 ir->mask_keydown = 0x00200000;
400 case BTTV_APAC_VIEWCOMP:
401 ir_codes = ir_codes_apac_viewcomp;
402 ir->mask_keycode = 0x001f00;
403 ir->mask_keyup = 0x008000;
404 ir->polling = 50; // ms
407 if (NULL == ir_codes) {
412 /* init hardware-specific stuff */
413 bttv_gpio_inout(sub->core, ir->mask_keycode | ir->mask_keydown, 0);
416 /* init input device */
417 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
419 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
420 pci_name(sub->core->pci));
422 ir_input_init(&ir->input, &ir->ir, ir_type, ir_codes);
423 ir->input.name = ir->name;
424 ir->input.phys = ir->phys;
425 ir->input.id.bustype = BUS_PCI;
426 ir->input.id.version = 1;
427 if (sub->core->pci->subsystem_vendor) {
428 ir->input.id.vendor = sub->core->pci->subsystem_vendor;
429 ir->input.id.product = sub->core->pci->subsystem_device;
431 ir->input.id.vendor = sub->core->pci->vendor;
432 ir->input.id.product = sub->core->pci->device;
436 INIT_WORK(&ir->work, ir_work, ir);
437 init_timer(&ir->timer);
438 ir->timer.function = ir_timer;
439 ir->timer.data = (unsigned long)ir;
440 schedule_work(&ir->work);
444 dev_set_drvdata(dev,ir);
445 input_register_device(&ir->input);
446 printk(DEVNAME ": %s detected at %s\n",ir->input.name,ir->input.phys);
451 static int ir_remove(struct device *dev)
453 struct IR *ir = dev_get_drvdata(dev);
456 del_timer(&ir->timer);
457 flush_scheduled_work();
460 input_unregister_device(&ir->input);
465 /* ---------------------------------------------------------------------- */
467 MODULE_AUTHOR("Gerd Knorr, Pavel Machek");
468 MODULE_DESCRIPTION("input driver for bt8x8 gpio IR remote controls");
469 MODULE_LICENSE("GPL");
471 static int ir_init(void)
473 return bttv_sub_register(&driver, "remote");
476 static void ir_fini(void)
478 bttv_sub_unregister(&driver);
481 module_init(ir_init);
482 module_exit(ir_fini);