2 * $Id: ir-kbd-gpio.c,v 1.13 2005/05/15 19:01:26 mchehab 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 /* Attila Kondoros <attila.kondoros@chello.hu> */
118 static IR_KEYTAB_TYPE ir_codes_apac_viewcomp[IR_KEYTAB_SIZE] = {
130 [ 23 ] = KEY_LAST, // +100
131 [ 10 ] = KEY_LIST, // recall
134 [ 28 ] = KEY_TUNER, // TV/FM
135 [ 21 ] = KEY_SEARCH, // scan
136 [ 18 ] = KEY_POWER, // power
137 [ 31 ] = KEY_VOLUMEDOWN, // vol up
138 [ 27 ] = KEY_VOLUMEUP, // vol down
139 [ 30 ] = KEY_CHANNELDOWN, // chn up
140 [ 26 ] = KEY_CHANNELUP, // chn down
142 [ 17 ] = KEY_VIDEO, // video
143 [ 15 ] = KEY_ZOOM, // full screen
144 [ 19 ] = KEY_MUTE, // mute/unmute
145 [ 16 ] = KEY_TEXT, // min
147 [ 13 ] = KEY_STOP, // freeze
148 [ 14 ] = KEY_RECORD, // record
149 [ 29 ] = KEY_PLAYPAUSE, // stop
150 [ 25 ] = KEY_PLAY, // play
152 [ 22 ] = KEY_GOTO, // osd
153 [ 20 ] = KEY_REFRESH, // default
154 [ 12 ] = KEY_KPPLUS, // fine tune >>>>
155 [ 24 ] = KEY_KPMINUS // fine tune <<<<
158 /* ---------------------------------------------------------------------- */
161 struct bttv_sub_device *sub;
162 struct input_dev input;
163 struct ir_input_state ir;
172 struct work_struct work;
173 struct timer_list timer;
177 module_param(debug, int, 0644); /* debug level (0,1,2) */
179 #define DEVNAME "ir-kbd-gpio"
180 #define dprintk(fmt, arg...) if (debug) \
181 printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
183 static void ir_irq(struct bttv_sub_device *sub);
184 static int ir_probe(struct device *dev);
185 static int ir_remove(struct device *dev);
187 static struct bttv_sub_driver driver = {
196 /* ---------------------------------------------------------------------- */
198 static void ir_handle_key(struct IR *ir)
202 /* read gpio value */
203 gpio = bttv_gpio_read(ir->sub->core);
205 if (ir->last_gpio == gpio)
207 ir->last_gpio = gpio;
211 data = ir_extract_bits(gpio, ir->mask_keycode);
212 dprintk(DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
214 ir->polling ? "poll" : "irq",
215 (gpio & ir->mask_keydown) ? " down" : "",
216 (gpio & ir->mask_keyup) ? " up" : "");
218 if (ir->mask_keydown) {
219 /* bit set on keydown */
220 if (gpio & ir->mask_keydown) {
221 ir_input_keydown(&ir->input,&ir->ir,data,data);
223 ir_input_nokey(&ir->input,&ir->ir);
226 } else if (ir->mask_keyup) {
227 /* bit cleared on keydown */
228 if (0 == (gpio & ir->mask_keyup)) {
229 ir_input_keydown(&ir->input,&ir->ir,data,data);
231 ir_input_nokey(&ir->input,&ir->ir);
235 /* can't disturgissh keydown/up :-/ */
236 ir_input_keydown(&ir->input,&ir->ir,data,data);
237 ir_input_nokey(&ir->input,&ir->ir);
241 static void ir_irq(struct bttv_sub_device *sub)
243 struct IR *ir = dev_get_drvdata(&sub->dev);
249 static void ir_timer(unsigned long data)
251 struct IR *ir = (struct IR*)data;
253 schedule_work(&ir->work);
256 static void ir_work(void *data)
258 struct IR *ir = data;
259 unsigned long timeout;
262 timeout = jiffies + (ir->polling * HZ / 1000);
263 mod_timer(&ir->timer, timeout);
266 /* ---------------------------------------------------------------------- */
268 static int ir_probe(struct device *dev)
270 struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
272 IR_KEYTAB_TYPE *ir_codes = NULL;
273 int ir_type = IR_TYPE_OTHER;
275 ir = kmalloc(sizeof(*ir),GFP_KERNEL);
278 memset(ir,0,sizeof(*ir));
280 /* detect & configure */
281 switch (sub->core->type) {
284 case BTTV_AVERMEDIA98:
285 ir_codes = ir_codes_avermedia;
286 ir->mask_keycode = 0xf88000;
287 ir->mask_keydown = 0x010000;
288 ir->polling = 50; // ms
291 case BTTV_AVDVBT_761:
292 case BTTV_AVDVBT_771:
293 ir_codes = ir_codes_avermedia_dvbt;
294 ir->mask_keycode = 0x0f00c0;
295 ir->mask_keydown = 0x000020;
296 ir->polling = 50; // ms
299 case BTTV_PXELVWPLTVPAK:
300 ir_codes = ir_codes_pixelview;
301 ir->mask_keycode = 0x003e00;
302 ir->mask_keyup = 0x010000;
303 ir->polling = 50; // ms
305 case BTTV_PV_BT878P_9B:
306 case BTTV_PV_BT878P_PLUS:
307 ir_codes = ir_codes_pixelview;
308 ir->mask_keycode = 0x001f00;
309 ir->mask_keyup = 0x008000;
310 ir->polling = 50; // ms
313 case BTTV_WINFAST2000:
314 ir_codes = ir_codes_winfast;
315 ir->mask_keycode = 0x1f8;
317 case BTTV_MAGICTVIEW061:
318 case BTTV_MAGICTVIEW063:
319 ir_codes = ir_codes_winfast;
320 ir->mask_keycode = 0x0008e000;
321 ir->mask_keydown = 0x00200000;
323 case BTTV_APAC_VIEWCOMP:
324 ir_codes = ir_codes_apac_viewcomp;
325 ir->mask_keycode = 0x001f00;
326 ir->mask_keyup = 0x008000;
327 ir->polling = 50; // ms
330 if (NULL == ir_codes) {
335 /* init hardware-specific stuff */
336 bttv_gpio_inout(sub->core, ir->mask_keycode | ir->mask_keydown, 0);
339 /* init input device */
340 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
342 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
343 pci_name(sub->core->pci));
345 ir_input_init(&ir->input, &ir->ir, ir_type, ir_codes);
346 ir->input.name = ir->name;
347 ir->input.phys = ir->phys;
348 ir->input.id.bustype = BUS_PCI;
349 ir->input.id.version = 1;
350 if (sub->core->pci->subsystem_vendor) {
351 ir->input.id.vendor = sub->core->pci->subsystem_vendor;
352 ir->input.id.product = sub->core->pci->subsystem_device;
354 ir->input.id.vendor = sub->core->pci->vendor;
355 ir->input.id.product = sub->core->pci->device;
359 INIT_WORK(&ir->work, ir_work, ir);
360 init_timer(&ir->timer);
361 ir->timer.function = ir_timer;
362 ir->timer.data = (unsigned long)ir;
363 schedule_work(&ir->work);
367 dev_set_drvdata(dev,ir);
368 input_register_device(&ir->input);
369 printk(DEVNAME ": %s detected at %s\n",ir->input.name,ir->input.phys);
374 static int ir_remove(struct device *dev)
376 struct IR *ir = dev_get_drvdata(dev);
379 del_timer(&ir->timer);
380 flush_scheduled_work();
383 input_unregister_device(&ir->input);
388 /* ---------------------------------------------------------------------- */
390 MODULE_AUTHOR("Gerd Knorr, Pavel Machek");
391 MODULE_DESCRIPTION("input driver for bt8x8 gpio IR remote controls");
392 MODULE_LICENSE("GPL");
394 static int ir_init(void)
396 return bttv_sub_register(&driver, "remote");
399 static void ir_fini(void)
401 bttv_sub_unregister(&driver);
404 module_init(ir_init);
405 module_exit(ir_fini);