3 * Device driver for GPIO attached remote control interfaces
4 * on Conexant 2388x based TV/DVB cards.
6 * Copyright (c) 2003 Pavel Machek
7 * Copyright (c) 2004 Gerd Knorr
8 * Copyright (c) 2004 Chris Pascoe
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/input.h>
28 #include <linux/pci.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
32 #include <media/ir-common.h>
36 /* ---------------------------------------------------------------------- */
38 /* DigitalNow DNTV Live DVB-T Remote */
39 static IR_KEYTAB_TYPE ir_codes_dntv_live_dvb_t[IR_KEYTAB_SIZE] = {
40 [0x00] = KEY_ESC, /* 'go up a level?' */
53 [0x0b] = KEY_TUNER, /* tv/fm */
54 [0x0c] = KEY_SEARCH, /* scan */
57 [0x0f] = KEY_LIST, /* source */
60 [0x11] = KEY_REWIND, /* backward << */
62 [0x13] = KEY_S, /* snap */
63 [0x14] = KEY_AUDIO, /* stereo */
64 [0x15] = KEY_CLEAR, /* reset */
67 [0x18] = KEY_ZOOM, /* full screen */
68 [0x19] = KEY_FASTFORWARD, /* forward >> */
69 [0x1a] = KEY_CHANNELUP,
70 [0x1b] = KEY_VOLUMEUP,
71 [0x1c] = KEY_INFO, /* preview */
72 [0x1d] = KEY_RECORD, /* record */
73 [0x1e] = KEY_CHANNELDOWN,
74 [0x1f] = KEY_VOLUMEDOWN,
77 /* ---------------------------------------------------------------------- */
79 /* IO-DATA BCTV7E Remote */
80 static IR_KEYTAB_TYPE ir_codes_iodata_bctv7e[IR_KEYTAB_SIZE] = {
82 [0x20] = KEY_RADIO, /* FM */
87 [0x44] = KEY_KP0, /* 10 */
98 [0x10] = KEY_L, /* Live */
99 [0x08] = KEY_T, /* Time Shift */
101 [0x18] = KEY_PLAYPAUSE, /* Play */
103 [0x24] = KEY_ENTER, /* 11 */
104 [0x64] = KEY_ESC, /* 12 */
105 [0x04] = KEY_M, /* Multi */
108 [0x34] = KEY_CHANNELUP,
109 [0x74] = KEY_VOLUMEUP,
112 [0x4c] = KEY_S, /* SVIDEO */
113 [0x2c] = KEY_CHANNELDOWN,
114 [0x6c] = KEY_VOLUMEDOWN,
118 [0x3c] = KEY_C, /* || (red) */
119 [0x7c] = KEY_RECORD, /* recording */
122 [0x41] = KEY_REWIND, /* backward << */
124 [0x61] = KEY_FASTFORWARD, /* forward >> */
125 [0x01] = KEY_NEXT, /* skip >| */
128 /* ---------------------------------------------------------------------- */
130 /* ADS Tech Instant TV DVB-T PCI Remote */
131 static IR_KEYTAB_TYPE ir_codes_adstech_dvb_t_pci[IR_KEYTAB_SIZE] = {
148 [0x17] = KEY_EPG, /* Guide */
154 [0x0e] = KEY_SELECT, /* Enter */
157 [0x59] = KEY_PREVIOUS,
160 [0x50] = KEY_FORWARD,
161 [0x44] = KEY_PLAYPAUSE,
164 [0x13] = KEY_TUNER, /* Live */
167 [0x03] = KEY_PROG1, /* 1 */
168 [0x01] = KEY_PROG2, /* 2 */
169 [0x00] = KEY_PROG3, /* 3 */
171 [0x48] = KEY_AUX, /* Photo */
173 [0x19] = KEY_AUDIO, /* Music */
174 [0x0b] = KEY_CHANNELUP,
175 [0x08] = KEY_CHANNELDOWN,
176 [0x15] = KEY_VOLUMEUP,
177 [0x1c] = KEY_VOLUMEDOWN,
180 /* ---------------------------------------------------------------------- */
182 /* MSI TV@nywhere remote */
183 static IR_KEYTAB_TYPE ir_codes_msi_tvanywhere[IR_KEYTAB_SIZE] = {
197 [0x0f] = KEY_SCREEN, /* Full Screen */
198 [0x10] = KEY_F, /* Funtion */
199 [0x11] = KEY_T, /* Time shift */
201 [0x13] = KEY_MEDIA, /* MTS */
203 [0x16] = KEY_REWIND, /* backward << */
204 [0x17] = KEY_ENTER, /* Return */
205 [0x18] = KEY_FASTFORWARD, /* forward >> */
206 [0x1a] = KEY_CHANNELUP,
207 [0x1b] = KEY_VOLUMEUP,
208 [0x1e] = KEY_CHANNELDOWN,
209 [0x1f] = KEY_VOLUMEDOWN,
212 /* ---------------------------------------------------------------------- */
214 /* Cinergy 1400 DVB-T */
215 static IR_KEYTAB_TYPE ir_codes_cinergy_1400[IR_KEYTAB_SIZE] = {
229 [0x0d] = KEY_REFRESH,
245 [0x1b] = KEY_CHANNELUP,
246 [0x1c] = KEY_VOLUMEUP,
248 [0x1e] = KEY_VOLUMEDOWN,
249 [0x1f] = KEY_CHANNELDOWN,
254 [0x54] = KEY_PREVIOUS,
259 /* ---------------------------------------------------------------------- */
261 /* AVERTV STUDIO 303 Remote */
262 static IR_KEYTAB_TYPE ir_codes_avertv_303[IR_KEYTAB_SIZE] = {
274 [ 0x02 ] = KEY_POWER,
275 [ 0x22 ] = KEY_VIDEO,
276 [ 0x42 ] = KEY_AUDIO,
282 [ 0x16 ] = KEY_SUBTITLE,
283 [ 0x1e ] = KEY_REWIND,
284 [ 0x06 ] = KEY_PRINT,
286 [ 0x2e ] = KEY_SEARCH,
287 [ 0x36 ] = KEY_SLEEP,
288 [ 0x3e ] = KEY_SHUFFLE,
291 [ 0x4e ] = KEY_RECORD,
292 [ 0x56 ] = KEY_PAUSE,
297 [ 0x0b ] = KEY_GREEN,
298 [ 0x66 ] = KEY_YELLOW,
302 [ 0x7e ] = KEY_RIGHT,
307 /* ---------------------------------------------------------------------- */
310 struct cx88_core *core;
311 struct input_dev *input;
312 struct ir_input_state ir;
316 /* sample from gpio pin 16 */
320 unsigned long release;
322 /* poll external decoder */
324 struct work_struct work;
325 struct timer_list timer;
333 static int ir_debug = 0;
334 module_param(ir_debug, int, 0644); /* debug level [IR] */
335 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
337 #define ir_dprintk(fmt, arg...) if (ir_debug) \
338 printk(KERN_DEBUG "%s IR: " fmt , ir->core->name , ##arg)
340 /* ---------------------------------------------------------------------- */
342 static void cx88_ir_handle_key(struct cx88_IR *ir)
344 struct cx88_core *core = ir->core;
347 /* read gpio value */
348 gpio = cx_read(ir->gpio_addr);
350 if (ir->last_gpio == gpio)
352 ir->last_gpio = gpio;
356 data = ir_extract_bits(gpio, ir->mask_keycode);
357 ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
359 ir->polling ? "poll" : "irq",
360 (gpio & ir->mask_keydown) ? " down" : "",
361 (gpio & ir->mask_keyup) ? " up" : "");
363 if (ir->mask_keydown) {
364 /* bit set on keydown */
365 if (gpio & ir->mask_keydown) {
366 ir_input_keydown(ir->input, &ir->ir, data, data);
368 ir_input_nokey(ir->input, &ir->ir);
371 } else if (ir->mask_keyup) {
372 /* bit cleared on keydown */
373 if (0 == (gpio & ir->mask_keyup)) {
374 ir_input_keydown(ir->input, &ir->ir, data, data);
376 ir_input_nokey(ir->input, &ir->ir);
380 /* can't distinguish keydown/up :-/ */
381 ir_input_keydown(ir->input, &ir->ir, data, data);
382 ir_input_nokey(ir->input, &ir->ir);
386 static void ir_timer(unsigned long data)
388 struct cx88_IR *ir = (struct cx88_IR *)data;
390 schedule_work(&ir->work);
393 static void cx88_ir_work(void *data)
395 struct cx88_IR *ir = data;
396 unsigned long timeout;
398 cx88_ir_handle_key(ir);
399 timeout = jiffies + (ir->polling * HZ / 1000);
400 mod_timer(&ir->timer, timeout);
403 /* ---------------------------------------------------------------------- */
405 int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
408 struct input_dev *input_dev;
409 IR_KEYTAB_TYPE *ir_codes = NULL;
410 int ir_type = IR_TYPE_OTHER;
412 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
413 input_dev = input_allocate_device();
414 if (!ir || !input_dev) {
416 input_free_device(input_dev);
420 ir->input = input_dev;
422 /* detect & configure */
423 switch (core->board) {
424 case CX88_BOARD_DNTV_LIVE_DVB_T:
425 case CX88_BOARD_KWORLD_DVB_T:
426 ir_codes = ir_codes_dntv_live_dvb_t;
427 ir->gpio_addr = MO_GP1_IO;
428 ir->mask_keycode = 0x1f;
429 ir->mask_keyup = 0x60;
430 ir->polling = 50; /* ms */
432 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
433 ir_codes = ir_codes_cinergy_1400;
434 ir_type = IR_TYPE_PD;
437 case CX88_BOARD_HAUPPAUGE:
438 case CX88_BOARD_HAUPPAUGE_DVB_T1:
439 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
440 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
441 case CX88_BOARD_HAUPPAUGE_HVR1100:
442 ir_codes = ir_codes_hauppauge_new;
443 ir_type = IR_TYPE_RC5;
446 case CX88_BOARD_WINFAST2000XP_EXPERT:
447 ir_codes = ir_codes_winfast;
448 ir->gpio_addr = MO_GP0_IO;
449 ir->mask_keycode = 0x8f8;
450 ir->mask_keyup = 0x100;
451 ir->polling = 1; /* ms */
453 case CX88_BOARD_IODATA_GVBCTV7E:
454 ir_codes = ir_codes_iodata_bctv7e;
455 ir->gpio_addr = MO_GP0_IO;
456 ir->mask_keycode = 0xfd;
457 ir->mask_keydown = 0x02;
458 ir->polling = 5; /* ms */
460 case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
461 ir_codes = ir_codes_pixelview;
462 ir->gpio_addr = MO_GP1_IO;
463 ir->mask_keycode = 0x1f;
464 ir->mask_keyup = 0x80;
465 ir->polling = 1; /* ms */
467 case CX88_BOARD_ADSTECH_DVB_T_PCI:
468 ir_codes = ir_codes_adstech_dvb_t_pci;
469 ir->gpio_addr = MO_GP1_IO;
470 ir->mask_keycode = 0xbf;
471 ir->mask_keyup = 0x40;
472 ir->polling = 50; /* ms */
474 case CX88_BOARD_MSI_TVANYWHERE_MASTER:
475 ir_codes = ir_codes_msi_tvanywhere;
476 ir->gpio_addr = MO_GP1_IO;
477 ir->mask_keycode = 0x1f;
478 ir->mask_keyup = 0x40;
479 ir->polling = 1; /* ms */
481 case CX88_BOARD_AVERTV_303:
482 ir_codes = ir_codes_avertv_303;
483 ir->gpio_addr = MO_GP2_IO;
484 ir->mask_keycode = 0xfb;
485 ir->mask_keydown = 0x02;
486 ir->polling = 50; /* ms */
490 if (NULL == ir_codes) {
492 input_free_device(input_dev);
496 /* init input device */
497 snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)",
498 cx88_boards[core->board].name);
499 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(pci));
501 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
502 input_dev->name = ir->name;
503 input_dev->phys = ir->phys;
504 input_dev->id.bustype = BUS_PCI;
505 input_dev->id.version = 1;
506 if (pci->subsystem_vendor) {
507 input_dev->id.vendor = pci->subsystem_vendor;
508 input_dev->id.product = pci->subsystem_device;
510 input_dev->id.vendor = pci->vendor;
511 input_dev->id.product = pci->device;
513 input_dev->cdev.dev = &pci->dev;
514 /* record handles to ourself */
519 INIT_WORK(&ir->work, cx88_ir_work, ir);
520 init_timer(&ir->timer);
521 ir->timer.function = ir_timer;
522 ir->timer.data = (unsigned long)ir;
523 schedule_work(&ir->work);
526 core->pci_irqmask |= (1 << 18); /* IR_SMP_INT */
527 cx_write(MO_DDS_IO, 0xa80a80); /* 4 kHz sample rate */
528 cx_write(MO_DDSCFG_IO, 0x5); /* enable */
532 input_register_device(ir->input);
537 int cx88_ir_fini(struct cx88_core *core)
539 struct cx88_IR *ir = core->ir;
541 /* skip detach on non attached boards */
546 del_timer(&ir->timer);
547 flush_scheduled_work();
550 input_unregister_device(ir->input);
558 /* ---------------------------------------------------------------------- */
560 void cx88_ir_irq(struct cx88_core *core)
562 struct cx88_IR *ir = core->ir;
571 samples = cx_read(MO_SAMPLE_IO);
572 if (0 != samples && 0xffffffff != samples) {
573 /* record sample data */
574 if (ir->scount < ARRAY_SIZE(ir->samples))
575 ir->samples[ir->scount++] = samples;
579 /* nothing to sample */
580 if (ir->ir.keypressed && time_after(jiffies, ir->release))
581 ir_input_nokey(ir->input, &ir->ir);
585 /* have a complete sample */
586 if (ir->scount < ARRAY_SIZE(ir->samples))
587 ir->samples[ir->scount++] = samples;
588 for (i = 0; i < ir->scount; i++)
589 ir->samples[i] = ~ir->samples[i];
591 ir_dump_samples(ir->samples, ir->scount);
594 switch (core->board) {
595 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
596 ircode = ir_decode_pulsedistance(ir->samples, ir->scount, 1, 4);
598 if (ircode == 0xffffffff) { /* decoding error */
599 ir_dprintk("pulse distance decoding error\n");
603 ir_dprintk("pulse distance decoded: %x\n", ircode);
605 if (ircode == 0) { /* key still pressed */
606 ir_dprintk("pulse distance decoded repeat code\n");
607 ir->release = jiffies + msecs_to_jiffies(120);
611 if ((ircode & 0xffff) != 0xeb04) { /* wrong address */
612 ir_dprintk("pulse distance decoded wrong address\n");
616 if (((~ircode >> 24) & 0xff) != ((ircode >> 16) & 0xff)) { /* wrong checksum */
617 ir_dprintk("pulse distance decoded wrong check sum\n");
621 ir_dprintk("Key Code: %x\n", (ircode >> 16) & 0x7f);
623 ir_input_keydown(ir->input, &ir->ir, (ircode >> 16) & 0x7f, (ircode >> 16) & 0xff);
624 ir->release = jiffies + msecs_to_jiffies(120);
626 case CX88_BOARD_HAUPPAUGE:
627 case CX88_BOARD_HAUPPAUGE_DVB_T1:
628 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
629 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
630 ircode = ir_decode_biphase(ir->samples, ir->scount, 5, 7);
631 ir_dprintk("biphase decoded: %x\n", ircode);
632 if ((ircode & 0xfffff000) != 0x3000)
634 ir_input_keydown(ir->input, &ir->ir, ircode & 0x3f, ircode);
635 ir->release = jiffies + msecs_to_jiffies(120);
643 /* ---------------------------------------------------------------------- */
645 MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
646 MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
647 MODULE_LICENSE("GPL");