2 * $Id: cx88-input.c,v 1.15 2005/07/07 13:58:38 mchehab Exp $
4 * Device driver for GPIO attached remote control interfaces
5 * on Conexant 2388x based TV/DVB cards.
7 * Copyright (c) 2003 Pavel Machek
8 * Copyright (c) 2004 Gerd Knorr
9 * Copyright (c) 2004 Chris Pascoe
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/input.h>
29 #include <linux/pci.h>
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
33 #include <media/ir-common.h>
37 /* ---------------------------------------------------------------------- */
39 /* DigitalNow DNTV Live DVB-T Remote */
40 static IR_KEYTAB_TYPE ir_codes_dntv_live_dvb_t[IR_KEYTAB_SIZE] = {
41 [0x00] = KEY_ESC, /* 'go up a level?' */
54 [0x0b] = KEY_TUNER, /* tv/fm */
55 [0x0c] = KEY_SEARCH, /* scan */
58 [0x0f] = KEY_LIST, /* source */
61 [0x11] = KEY_REWIND, /* backward << */
63 [0x13] = KEY_S, /* snap */
64 [0x14] = KEY_AUDIO, /* stereo */
65 [0x15] = KEY_CLEAR, /* reset */
68 [0x18] = KEY_ZOOM, /* full screen */
69 [0x19] = KEY_FASTFORWARD, /* forward >> */
70 [0x1a] = KEY_CHANNELUP,
71 [0x1b] = KEY_VOLUMEUP,
72 [0x1c] = KEY_INFO, /* preview */
73 [0x1d] = KEY_RECORD, /* record */
74 [0x1e] = KEY_CHANNELDOWN,
75 [0x1f] = KEY_VOLUMEDOWN,
78 /* ---------------------------------------------------------------------- */
80 /* IO-DATA BCTV7E Remote */
81 static IR_KEYTAB_TYPE ir_codes_iodata_bctv7e[IR_KEYTAB_SIZE] = {
83 [0x20] = KEY_RADIO, /* FM */
88 [0x44] = KEY_KP0, /* 10 */
99 [0x10] = KEY_L, /* Live */
100 [0x08] = KEY_T, /* Time Shift */
102 [0x18] = KEY_PLAYPAUSE, /* Play */
104 [0x24] = KEY_ENTER, /* 11 */
105 [0x64] = KEY_ESC, /* 12 */
106 [0x04] = KEY_M, /* Multi */
109 [0x34] = KEY_CHANNELUP,
110 [0x74] = KEY_VOLUMEUP,
113 [0x4c] = KEY_S, /* SVIDEO */
114 [0x2c] = KEY_CHANNELDOWN,
115 [0x6c] = KEY_VOLUMEDOWN,
119 [0x3c] = KEY_C, /* || (red) */
120 [0x7c] = KEY_RECORD, /* recording */
123 [0x41] = KEY_REWIND, /* backward << */
125 [0x61] = KEY_FASTFORWARD, /* forward >> */
126 [0x01] = KEY_NEXT, /* skip >| */
129 /* ---------------------------------------------------------------------- */
131 /* ADS Tech Instant TV DVB-T PCI Remote */
132 static IR_KEYTAB_TYPE ir_codes_adstech_dvb_t_pci[IR_KEYTAB_SIZE] = {
149 [0x17] = KEY_EPG, /* Guide */
155 [0x0e] = KEY_SELECT, /* Enter */
158 [0x59] = KEY_PREVIOUS,
161 [0x50] = KEY_FORWARD,
162 [0x44] = KEY_PLAYPAUSE,
165 [0x13] = KEY_TUNER, /* Live */
168 [0x03] = KEY_PROG1, /* 1 */
169 [0x01] = KEY_PROG2, /* 2 */
170 [0x00] = KEY_PROG3, /* 3 */
172 [0x48] = KEY_AUX, /* Photo */
174 [0x19] = KEY_AUDIO, /* Music */
175 [0x0b] = KEY_CHANNELUP,
176 [0x08] = KEY_CHANNELDOWN,
177 [0x15] = KEY_VOLUMEUP,
178 [0x1c] = KEY_VOLUMEDOWN,
181 /* ---------------------------------------------------------------------- */
183 /* MSI TV@nywhere remote */
184 static IR_KEYTAB_TYPE ir_codes_msi_tvanywhere[IR_KEYTAB_SIZE] = {
198 [0x0f] = KEY_SCREEN, /* Full Screen */
199 [0x10] = KEY_F, /* Funtion */
200 [0x11] = KEY_T, /* Time shift */
202 [0x13] = KEY_MEDIA, /* MTS */
204 [0x16] = KEY_REWIND, /* backward << */
205 [0x17] = KEY_ENTER, /* Return */
206 [0x18] = KEY_FASTFORWARD, /* forward >> */
207 [0x1a] = KEY_CHANNELUP,
208 [0x1b] = KEY_VOLUMEUP,
209 [0x1e] = KEY_CHANNELDOWN,
210 [0x1f] = KEY_VOLUMEDOWN,
213 /* ---------------------------------------------------------------------- */
216 struct cx88_core *core;
217 struct input_dev input;
218 struct ir_input_state ir;
222 /* sample from gpio pin 16 */
226 unsigned long release;
228 /* poll external decoder */
230 struct work_struct work;
231 struct timer_list timer;
239 static int ir_debug = 0;
240 module_param(ir_debug, int, 0644); /* debug level [IR] */
241 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
243 #define ir_dprintk(fmt, arg...) if (ir_debug) \
244 printk(KERN_DEBUG "%s IR: " fmt , ir->core->name, ## arg)
246 /* ---------------------------------------------------------------------- */
248 static void cx88_ir_handle_key(struct cx88_IR *ir)
250 struct cx88_core *core = ir->core;
253 /* read gpio value */
254 gpio = cx_read(ir->gpio_addr);
256 if (ir->last_gpio == gpio)
258 ir->last_gpio = gpio;
262 data = ir_extract_bits(gpio, ir->mask_keycode);
263 ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
265 ir->polling ? "poll" : "irq",
266 (gpio & ir->mask_keydown) ? " down" : "",
267 (gpio & ir->mask_keyup) ? " up" : "");
269 if (ir->mask_keydown) {
270 /* bit set on keydown */
271 if (gpio & ir->mask_keydown) {
272 ir_input_keydown(&ir->input, &ir->ir, data, data);
274 ir_input_nokey(&ir->input, &ir->ir);
277 } else if (ir->mask_keyup) {
278 /* bit cleared on keydown */
279 if (0 == (gpio & ir->mask_keyup)) {
280 ir_input_keydown(&ir->input, &ir->ir, data, data);
282 ir_input_nokey(&ir->input, &ir->ir);
286 /* can't distinguish keydown/up :-/ */
287 ir_input_keydown(&ir->input, &ir->ir, data, data);
288 ir_input_nokey(&ir->input, &ir->ir);
292 static void ir_timer(unsigned long data)
294 struct cx88_IR *ir = (struct cx88_IR *)data;
296 schedule_work(&ir->work);
299 static void cx88_ir_work(void *data)
301 struct cx88_IR *ir = data;
302 unsigned long timeout;
304 cx88_ir_handle_key(ir);
305 timeout = jiffies + (ir->polling * HZ / 1000);
306 mod_timer(&ir->timer, timeout);
309 /* ---------------------------------------------------------------------- */
311 int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
314 IR_KEYTAB_TYPE *ir_codes = NULL;
315 int ir_type = IR_TYPE_OTHER;
317 ir = kmalloc(sizeof(*ir), GFP_KERNEL);
320 memset(ir, 0, sizeof(*ir));
322 /* detect & configure */
323 switch (core->board) {
324 case CX88_BOARD_DNTV_LIVE_DVB_T:
325 case CX88_BOARD_KWORLD_DVB_T:
326 ir_codes = ir_codes_dntv_live_dvb_t;
327 ir->gpio_addr = MO_GP1_IO;
328 ir->mask_keycode = 0x1f;
329 ir->mask_keyup = 0x60;
330 ir->polling = 50; /* ms */
332 case CX88_BOARD_HAUPPAUGE:
333 case CX88_BOARD_HAUPPAUGE_DVB_T1:
334 ir_codes = ir_codes_hauppauge_new;
335 ir_type = IR_TYPE_RC5;
338 case CX88_BOARD_WINFAST2000XP_EXPERT:
339 ir_codes = ir_codes_winfast;
340 ir->gpio_addr = MO_GP0_IO;
341 ir->mask_keycode = 0x8f8;
342 ir->mask_keyup = 0x100;
343 ir->polling = 1; /* ms */
345 case CX88_BOARD_IODATA_GVBCTV7E:
346 ir_codes = ir_codes_iodata_bctv7e;
347 ir->gpio_addr = MO_GP0_IO;
348 ir->mask_keycode = 0xfd;
349 ir->mask_keydown = 0x02;
350 ir->polling = 5; /* ms */
352 case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
353 ir_codes = ir_codes_pixelview;
354 ir->gpio_addr = MO_GP1_IO;
355 ir->mask_keycode = 0x1f;
356 ir->mask_keyup = 0x80;
357 ir->polling = 1; /* ms */
359 case CX88_BOARD_ADSTECH_DVB_T_PCI:
360 ir_codes = ir_codes_adstech_dvb_t_pci;
361 ir->gpio_addr = MO_GP1_IO;
362 ir->mask_keycode = 0xbf;
363 ir->mask_keyup = 0x40;
364 ir->polling = 50; /* ms */
366 case CX88_BOARD_MSI_TVANYWHERE_MASTER:
367 ir_codes = ir_codes_msi_tvanywhere;
368 ir->gpio_addr = MO_GP1_IO;
369 ir->mask_keycode = 0x1f;
370 ir->mask_keyup = 0x40;
371 ir->polling = 1; /* ms */
375 if (NULL == ir_codes) {
380 /* init input device */
381 snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)",
382 cx88_boards[core->board].name);
383 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(pci));
385 ir_input_init(&ir->input, &ir->ir, ir_type, ir_codes);
386 ir->input.name = ir->name;
387 ir->input.phys = ir->phys;
388 ir->input.id.bustype = BUS_PCI;
389 ir->input.id.version = 1;
390 if (pci->subsystem_vendor) {
391 ir->input.id.vendor = pci->subsystem_vendor;
392 ir->input.id.product = pci->subsystem_device;
394 ir->input.id.vendor = pci->vendor;
395 ir->input.id.product = pci->device;
398 /* record handles to ourself */
403 INIT_WORK(&ir->work, cx88_ir_work, ir);
404 init_timer(&ir->timer);
405 ir->timer.function = ir_timer;
406 ir->timer.data = (unsigned long)ir;
407 schedule_work(&ir->work);
410 core->pci_irqmask |= (1 << 18); /* IR_SMP_INT */
411 cx_write(MO_DDS_IO, 0xa80a80); /* 4 kHz sample rate */
412 cx_write(MO_DDSCFG_IO, 0x5); /* enable */
416 input_register_device(&ir->input);
417 printk("%s: registered IR remote control\n", core->name);
422 int cx88_ir_fini(struct cx88_core *core)
424 struct cx88_IR *ir = core->ir;
426 /* skip detach on non attached boards */
431 del_timer(&ir->timer);
432 flush_scheduled_work();
435 input_unregister_device(&ir->input);
443 /* ---------------------------------------------------------------------- */
445 void cx88_ir_irq(struct cx88_core *core)
447 struct cx88_IR *ir = core->ir;
456 samples = cx_read(MO_SAMPLE_IO);
457 if (0 != samples && 0xffffffff != samples) {
458 /* record sample data */
459 if (ir->scount < ARRAY_SIZE(ir->samples))
460 ir->samples[ir->scount++] = samples;
464 /* nothing to sample */
465 if (ir->ir.keypressed && time_after(jiffies, ir->release))
466 ir_input_nokey(&ir->input, &ir->ir);
470 /* have a complete sample */
471 if (ir->scount < ARRAY_SIZE(ir->samples))
472 ir->samples[ir->scount++] = samples;
473 for (i = 0; i < ir->scount; i++)
474 ir->samples[i] = ~ir->samples[i];
476 ir_dump_samples(ir->samples, ir->scount);
479 switch (core->board) {
480 case CX88_BOARD_HAUPPAUGE:
481 case CX88_BOARD_HAUPPAUGE_DVB_T1:
482 rc5 = ir_decode_biphase(ir->samples, ir->scount, 5, 7);
483 ir_dprintk("biphase decoded: %x\n", rc5);
484 if ((rc5 & 0xfffff000) != 0x3000)
486 ir_input_keydown(&ir->input, &ir->ir, rc5 & 0x3f, rc5);
487 ir->release = jiffies + msecs_to_jiffies(120);
495 /* ---------------------------------------------------------------------- */
497 MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
498 MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
499 MODULE_LICENSE("GPL");