2 * Copyright (c) 2003 Gerd Knorr
3 * Copyright (c) 2003 Pavel Machek
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/input.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 [ 24 ] = KEY_EJECTCD, // Unmarked on my controller
49 [ 9 ] = BTN_LEFT, // DISPLAY/L
50 [ 25 ] = BTN_RIGHT, // LOOP/R
55 [ 15 ] = KEY_VOLUMEDOWN,
56 [ 31 ] = KEY_VOLUMEUP,
58 [ 16 ] = KEY_TUNER, // TV/FM
62 [ 6 ] = KEY_ZOOM, // full screen
63 [ 1 ] = KEY_INFO, // preview
64 [ 21 ] = KEY_SEARCH, // autoscan
65 [ 13 ] = KEY_STOP, // freeze
66 [ 29 ] = KEY_RECORD, // capture
67 [ 3 ] = KEY_PLAY, // unmarked
68 [ 24 ] = KEY_RED, // unmarked
69 [ 7 ] = KEY_GREEN, // unmarked
72 [ 16 ] = KEY_YELLOW, // unmarked
73 [ 8 ] = KEY_CHANNELDOWN,
74 [ 24 ] = KEY_CHANNELUP,
75 [ 0 ] = KEY_BLUE, // unmarked
79 static IR_KEYTAB_TYPE winfast_codes[IR_KEYTAB_SIZE] = {
92 // [ 27 ] = MTS button
93 [ 2 ] = KEY_TUNER, // TV/FM
95 // [ 22 ] = display button
97 [ 8 ] = KEY_VOLUMEDOWN,
98 [ 12 ] = KEY_CHANNELUP,
99 [ 16 ] = KEY_CHANNELDOWN,
100 [ 3 ] = KEY_ZOOM, // fullscreen
101 [ 31 ] = KEY_SUBTITLE, // closed caption/teletext
109 [ 24 ] = KEY_KPPLUS, //fine tune +
110 [ 25 ] = KEY_KPMINUS, //fine tune -
111 // [ 42 ] = picture in picture
113 [ 19 ] = KEY_KPENTER,
116 [ 35 ] = KEY_PLAYPAUSE,
118 // [ 37 ] = time shifting
124 static IR_KEYTAB_TYPE ir_codes_pixelview[IR_KEYTAB_SIZE] = {
136 [ 3 ] = KEY_TUNER, // TV/FM
137 [ 7 ] = KEY_SEARCH, // scan
138 [ 28 ] = KEY_ZOOM, // full screen
140 [ 23 ] = KEY_VOLUMEDOWN,
141 [ 31 ] = KEY_VOLUMEUP,
142 [ 20 ] = KEY_CHANNELDOWN,
143 [ 22 ] = KEY_CHANNELUP,
146 [ 0 ] = KEY_LIST, // source
147 [ 19 ] = KEY_INFO, // loop
148 [ 16 ] = KEY_LAST, // +100
149 [ 13 ] = KEY_CLEAR, // reset
150 [ 12 ] = BTN_RIGHT, // fun++
151 [ 4 ] = BTN_LEFT, // fun--
152 [ 14 ] = KEY_GOTO, // function
153 [ 15 ] = KEY_STOP, // freeze
156 /* ---------------------------------------------------------------------- */
159 struct bttv_sub_device *sub;
160 struct input_dev input;
161 struct ir_input_state ir;
170 struct work_struct work;
171 struct timer_list timer;
175 module_param(debug, int, 0644); /* debug level (0,1,2) */
177 #define DEVNAME "ir-kbd-gpio"
178 #define dprintk(fmt, arg...) if (debug) \
179 printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
181 static void ir_irq(struct bttv_sub_device *sub);
182 static int ir_probe(struct device *dev);
183 static int ir_remove(struct device *dev);
185 static struct bttv_sub_driver driver = {
194 /* ---------------------------------------------------------------------- */
196 static void ir_handle_key(struct IR *ir)
200 /* read gpio value */
201 gpio = bttv_gpio_read(ir->sub->core);
203 if (ir->last_gpio == gpio)
205 ir->last_gpio = gpio;
209 data = ir_extract_bits(gpio, ir->mask_keycode);
210 dprintk(DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
212 ir->polling ? "poll" : "irq",
213 (gpio & ir->mask_keydown) ? " down" : "",
214 (gpio & ir->mask_keyup) ? " up" : "");
216 if (ir->mask_keydown) {
217 /* bit set on keydown */
218 if (gpio & ir->mask_keydown) {
219 ir_input_keydown(&ir->input,&ir->ir,data,data);
221 ir_input_nokey(&ir->input,&ir->ir);
224 } else if (ir->mask_keyup) {
225 /* bit cleared on keydown */
226 if (0 == (gpio & ir->mask_keyup)) {
227 ir_input_keydown(&ir->input,&ir->ir,data,data);
229 ir_input_nokey(&ir->input,&ir->ir);
233 /* can't disturgissh keydown/up :-/ */
234 ir_input_keydown(&ir->input,&ir->ir,data,data);
235 ir_input_nokey(&ir->input,&ir->ir);
239 static void ir_irq(struct bttv_sub_device *sub)
241 struct IR *ir = dev_get_drvdata(&sub->dev);
247 static void ir_timer(unsigned long data)
249 struct IR *ir = (struct IR*)data;
251 schedule_work(&ir->work);
254 static void ir_work(void *data)
256 struct IR *ir = data;
257 unsigned long timeout;
260 timeout = jiffies + (ir->polling * HZ / 1000);
261 mod_timer(&ir->timer, timeout);
264 /* ---------------------------------------------------------------------- */
266 static int ir_probe(struct device *dev)
268 struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
270 IR_KEYTAB_TYPE *ir_codes = NULL;
271 int ir_type = IR_TYPE_OTHER;
273 ir = kmalloc(sizeof(*ir),GFP_KERNEL);
276 memset(ir,0,sizeof(*ir));
278 /* detect & configure */
279 switch (sub->core->type) {
282 case BTTV_AVERMEDIA98:
283 ir_codes = ir_codes_avermedia;
284 ir->mask_keycode = 0xf80000;
285 ir->mask_keydown = 0x010000;
288 case BTTV_PXELVWPLTVPAK:
289 ir_codes = ir_codes_pixelview;
290 ir->mask_keycode = 0x003e00;
291 ir->mask_keyup = 0x010000;
292 ir->polling = 50; // ms
294 case BTTV_PV_BT878P_9B:
295 case BTTV_PV_BT878P_PLUS:
296 ir_codes = ir_codes_pixelview;
297 ir->mask_keycode = 0x001f00;
298 ir->mask_keyup = 0x008000;
299 ir->polling = 50; // ms
302 case BTTV_WINFAST2000:
303 ir_codes = winfast_codes;
304 ir->mask_keycode = 0x8f8;
306 case BTTV_MAGICTVIEW061:
307 case BTTV_MAGICTVIEW063:
308 ir_codes = winfast_codes;
309 ir->mask_keycode = 0x0008e000;
310 ir->mask_keydown = 0x00200000;
313 if (NULL == ir_codes) {
318 /* init hardware-specific stuff */
319 bttv_gpio_inout(sub->core, ir->mask_keycode | ir->mask_keydown, 0);
322 /* init input device */
323 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
325 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
326 pci_name(sub->core->pci));
328 ir_input_init(&ir->input, &ir->ir, ir_type, ir_codes);
329 ir->input.name = ir->name;
330 ir->input.phys = ir->phys;
331 ir->input.id.bustype = BUS_PCI;
332 ir->input.id.version = 1;
333 if (sub->core->pci->subsystem_vendor) {
334 ir->input.id.vendor = sub->core->pci->subsystem_vendor;
335 ir->input.id.product = sub->core->pci->subsystem_device;
337 ir->input.id.vendor = sub->core->pci->vendor;
338 ir->input.id.product = sub->core->pci->device;
342 INIT_WORK(&ir->work, ir_work, ir);
343 init_timer(&ir->timer);
344 ir->timer.function = ir_timer;
345 ir->timer.data = (unsigned long)ir;
346 schedule_work(&ir->work);
350 dev_set_drvdata(dev,ir);
351 input_register_device(&ir->input);
352 printk(DEVNAME ": %s detected at %s\n",ir->input.name,ir->input.phys);
357 static int ir_remove(struct device *dev)
359 struct IR *ir = dev_get_drvdata(dev);
362 del_timer(&ir->timer);
363 flush_scheduled_work();
366 input_unregister_device(&ir->input);
371 /* ---------------------------------------------------------------------- */
373 MODULE_AUTHOR("Gerd Knorr, Pavel Machek");
374 MODULE_DESCRIPTION("input driver for bt8x8 gpio IR remote controls");
375 MODULE_LICENSE("GPL");
377 static int ir_init(void)
379 return bttv_sub_register(&driver, "remote");
382 static void ir_fini(void)
384 bttv_sub_unregister(&driver);
387 module_init(ir_init);
388 module_exit(ir_fini);