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, 2005 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>
33 #include <media/ir-common.h>
35 /* ---------------------------------------------------------------------- */
37 /* DigitalNow DNTV Live DVB-T Remote */
38 static IR_KEYTAB_TYPE ir_codes_dntv_live_dvb_t[IR_KEYTAB_SIZE] = {
39 [0x00] = KEY_ESC, /* 'go up a level?' */
52 [0x0b] = KEY_TUNER, /* tv/fm */
53 [0x0c] = KEY_SEARCH, /* scan */
56 [0x0f] = KEY_LIST, /* source */
59 [0x11] = KEY_REWIND, /* backward << */
61 [0x13] = KEY_S, /* snap */
62 [0x14] = KEY_AUDIO, /* stereo */
63 [0x15] = KEY_CLEAR, /* reset */
66 [0x18] = KEY_ZOOM, /* full screen */
67 [0x19] = KEY_FASTFORWARD, /* forward >> */
68 [0x1a] = KEY_CHANNELUP,
69 [0x1b] = KEY_VOLUMEUP,
70 [0x1c] = KEY_INFO, /* preview */
71 [0x1d] = KEY_RECORD, /* record */
72 [0x1e] = KEY_CHANNELDOWN,
73 [0x1f] = KEY_VOLUMEDOWN,
76 /* ---------------------------------------------------------------------- */
78 /* IO-DATA BCTV7E Remote */
79 static IR_KEYTAB_TYPE ir_codes_iodata_bctv7e[IR_KEYTAB_SIZE] = {
81 [0x20] = KEY_RADIO, /* FM */
86 [0x44] = KEY_KP0, /* 10 */
97 [0x10] = KEY_L, /* Live */
98 [0x08] = KEY_T, /* Time Shift */
100 [0x18] = KEY_PLAYPAUSE, /* Play */
102 [0x24] = KEY_ENTER, /* 11 */
103 [0x64] = KEY_ESC, /* 12 */
104 [0x04] = KEY_M, /* Multi */
107 [0x34] = KEY_CHANNELUP,
108 [0x74] = KEY_VOLUMEUP,
111 [0x4c] = KEY_S, /* SVIDEO */
112 [0x2c] = KEY_CHANNELDOWN,
113 [0x6c] = KEY_VOLUMEDOWN,
117 [0x3c] = KEY_C, /* || (red) */
118 [0x7c] = KEY_RECORD, /* recording */
121 [0x41] = KEY_REWIND, /* backward << */
123 [0x61] = KEY_FASTFORWARD, /* forward >> */
124 [0x01] = KEY_NEXT, /* skip >| */
127 /* ---------------------------------------------------------------------- */
129 /* ADS Tech Instant TV DVB-T PCI Remote */
130 static IR_KEYTAB_TYPE ir_codes_adstech_dvb_t_pci[IR_KEYTAB_SIZE] = {
147 [0x17] = KEY_EPG, /* Guide */
153 [0x0e] = KEY_SELECT, /* Enter */
156 [0x59] = KEY_PREVIOUS,
159 [0x50] = KEY_FORWARD,
160 [0x44] = KEY_PLAYPAUSE,
163 [0x13] = KEY_TUNER, /* Live */
166 [0x03] = KEY_PROG1, /* 1 */
167 [0x01] = KEY_PROG2, /* 2 */
168 [0x00] = KEY_PROG3, /* 3 */
170 [0x48] = KEY_AUX, /* Photo */
172 [0x19] = KEY_AUDIO, /* Music */
173 [0x0b] = KEY_CHANNELUP,
174 [0x08] = KEY_CHANNELDOWN,
175 [0x15] = KEY_VOLUMEUP,
176 [0x1c] = KEY_VOLUMEDOWN,
179 /* ---------------------------------------------------------------------- */
181 /* MSI TV@nywhere remote */
182 static IR_KEYTAB_TYPE ir_codes_msi_tvanywhere[IR_KEYTAB_SIZE] = {
196 [0x0f] = KEY_SCREEN, /* Full Screen */
197 [0x10] = KEY_F, /* Funtion */
198 [0x11] = KEY_T, /* Time shift */
200 [0x13] = KEY_MEDIA, /* MTS */
202 [0x16] = KEY_REWIND, /* backward << */
203 [0x17] = KEY_ENTER, /* Return */
204 [0x18] = KEY_FASTFORWARD, /* forward >> */
205 [0x1a] = KEY_CHANNELUP,
206 [0x1b] = KEY_VOLUMEUP,
207 [0x1e] = KEY_CHANNELDOWN,
208 [0x1f] = KEY_VOLUMEDOWN,
211 /* ---------------------------------------------------------------------- */
213 /* Cinergy 1400 DVB-T */
214 static IR_KEYTAB_TYPE ir_codes_cinergy_1400[IR_KEYTAB_SIZE] = {
228 [0x0d] = KEY_REFRESH,
244 [0x1b] = KEY_CHANNELUP,
245 [0x1c] = KEY_VOLUMEUP,
247 [0x1e] = KEY_VOLUMEDOWN,
248 [0x1f] = KEY_CHANNELDOWN,
253 [0x54] = KEY_PREVIOUS,
258 /* ---------------------------------------------------------------------- */
260 /* AVERTV STUDIO 303 Remote */
261 static IR_KEYTAB_TYPE ir_codes_avertv_303[IR_KEYTAB_SIZE] = {
273 [ 0x02 ] = KEY_POWER,
274 [ 0x22 ] = KEY_VIDEO,
275 [ 0x42 ] = KEY_AUDIO,
281 [ 0x16 ] = KEY_SUBTITLE,
282 [ 0x1e ] = KEY_REWIND,
283 [ 0x06 ] = KEY_PRINT,
285 [ 0x2e ] = KEY_SEARCH,
286 [ 0x36 ] = KEY_SLEEP,
287 [ 0x3e ] = KEY_SHUFFLE,
290 [ 0x4e ] = KEY_RECORD,
291 [ 0x56 ] = KEY_PAUSE,
296 [ 0x0b ] = KEY_GREEN,
297 [ 0x66 ] = KEY_YELLOW,
301 [ 0x7e ] = KEY_RIGHT,
306 /* ---------------------------------------------------------------------- */
308 /* DigitalNow DNTV Live! DVB-T Pro Remote */
309 static IR_KEYTAB_TYPE ir_codes_dntv_live_dvbt_pro[IR_KEYTAB_SIZE] = {
310 [ 0x16 ] = KEY_POWER,
313 [ 0x55 ] = KEY_TV, /* live tv */
314 [ 0x58 ] = KEY_TUNER, /* digital Radio */
315 [ 0x5a ] = KEY_RADIO, /* FM radio */
316 [ 0x59 ] = KEY_DVD, /* dvd menu */
326 [ 0x0c ] = KEY_CANCEL,
328 [ 0x4a ] = KEY_CLEAR,
334 [ 0x52 ] = KEY_RIGHT,
336 [ 0x1e ] = KEY_VOLUMEUP,
337 [ 0x0a ] = KEY_VOLUMEDOWN,
338 [ 0x02 ] = KEY_CHANNELDOWN,
339 [ 0x05 ] = KEY_CHANNELUP,
340 [ 0x11 ] = KEY_RECORD,
342 [ 0x4c ] = KEY_PAUSE,
344 [ 0x40 ] = KEY_REWIND,
345 [ 0x12 ] = KEY_FASTFORWARD,
346 [ 0x41 ] = KEY_PREVIOUSSONG, /* replay |< */
347 [ 0x42 ] = KEY_NEXTSONG, /* skip >| */
348 [ 0x54 ] = KEY_CAMERA, /* capture */
349 [ 0x50 ] = KEY_LANGUAGE, /* sap */
350 [ 0x47 ] = KEY_TV2, /* pip */
351 [ 0x4d ] = KEY_SCREEN,
352 [ 0x43 ] = KEY_SUBTITLE,
354 [ 0x49 ] = KEY_AUDIO, /* l/r */
355 [ 0x07 ] = KEY_SLEEP,
356 [ 0x08 ] = KEY_VIDEO, /* a/v */
357 [ 0x0e ] = KEY_PREVIOUS, /* recall */
358 [ 0x45 ] = KEY_ZOOM, /* zoom + */
359 [ 0x46 ] = KEY_ANGLE, /* zoom - */
361 [ 0x57 ] = KEY_GREEN,
362 [ 0x5c ] = KEY_YELLOW,
366 /* ---------------------------------------------------------------------- */
369 struct cx88_core *core;
370 struct input_dev *input;
371 struct ir_input_state ir;
375 /* sample from gpio pin 16 */
379 unsigned long release;
381 /* poll external decoder */
383 struct work_struct work;
384 struct timer_list timer;
392 static int ir_debug = 0;
393 module_param(ir_debug, int, 0644); /* debug level [IR] */
394 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
396 #define ir_dprintk(fmt, arg...) if (ir_debug) \
397 printk(KERN_DEBUG "%s IR: " fmt , ir->core->name , ##arg)
399 /* ---------------------------------------------------------------------- */
401 static void cx88_ir_handle_key(struct cx88_IR *ir)
403 struct cx88_core *core = ir->core;
406 /* read gpio value */
407 gpio = cx_read(ir->gpio_addr);
409 if (ir->last_gpio == gpio)
411 ir->last_gpio = gpio;
415 data = ir_extract_bits(gpio, ir->mask_keycode);
416 ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
418 ir->polling ? "poll" : "irq",
419 (gpio & ir->mask_keydown) ? " down" : "",
420 (gpio & ir->mask_keyup) ? " up" : "");
422 if (ir->mask_keydown) {
423 /* bit set on keydown */
424 if (gpio & ir->mask_keydown) {
425 ir_input_keydown(ir->input, &ir->ir, data, data);
427 ir_input_nokey(ir->input, &ir->ir);
430 } else if (ir->mask_keyup) {
431 /* bit cleared on keydown */
432 if (0 == (gpio & ir->mask_keyup)) {
433 ir_input_keydown(ir->input, &ir->ir, data, data);
435 ir_input_nokey(ir->input, &ir->ir);
439 /* can't distinguish keydown/up :-/ */
440 ir_input_keydown(ir->input, &ir->ir, data, data);
441 ir_input_nokey(ir->input, &ir->ir);
445 static void ir_timer(unsigned long data)
447 struct cx88_IR *ir = (struct cx88_IR *)data;
449 schedule_work(&ir->work);
452 static void cx88_ir_work(void *data)
454 struct cx88_IR *ir = data;
455 unsigned long timeout;
457 cx88_ir_handle_key(ir);
458 timeout = jiffies + (ir->polling * HZ / 1000);
459 mod_timer(&ir->timer, timeout);
462 /* ---------------------------------------------------------------------- */
464 int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
467 struct input_dev *input_dev;
468 IR_KEYTAB_TYPE *ir_codes = NULL;
469 int ir_type = IR_TYPE_OTHER;
471 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
472 input_dev = input_allocate_device();
473 if (!ir || !input_dev) {
475 input_free_device(input_dev);
479 ir->input = input_dev;
481 /* detect & configure */
482 switch (core->board) {
483 case CX88_BOARD_DNTV_LIVE_DVB_T:
484 case CX88_BOARD_KWORLD_DVB_T:
485 case CX88_BOARD_KWORLD_DVB_T_CX22702:
486 ir_codes = ir_codes_dntv_live_dvb_t;
487 ir->gpio_addr = MO_GP1_IO;
488 ir->mask_keycode = 0x1f;
489 ir->mask_keyup = 0x60;
490 ir->polling = 50; /* ms */
492 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
493 ir_codes = ir_codes_cinergy_1400;
494 ir_type = IR_TYPE_PD;
495 ir->sampling = 0xeb04; /* address */
497 case CX88_BOARD_HAUPPAUGE:
498 case CX88_BOARD_HAUPPAUGE_DVB_T1:
499 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
500 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
501 case CX88_BOARD_HAUPPAUGE_HVR1100:
502 ir_codes = ir_codes_hauppauge_new;
503 ir_type = IR_TYPE_RC5;
506 case CX88_BOARD_WINFAST2000XP_EXPERT:
507 ir_codes = ir_codes_winfast;
508 ir->gpio_addr = MO_GP0_IO;
509 ir->mask_keycode = 0x8f8;
510 ir->mask_keyup = 0x100;
511 ir->polling = 1; /* ms */
513 case CX88_BOARD_IODATA_GVBCTV7E:
514 ir_codes = ir_codes_iodata_bctv7e;
515 ir->gpio_addr = MO_GP0_IO;
516 ir->mask_keycode = 0xfd;
517 ir->mask_keydown = 0x02;
518 ir->polling = 5; /* ms */
520 case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
521 ir_codes = ir_codes_pixelview;
522 ir->gpio_addr = MO_GP1_IO;
523 ir->mask_keycode = 0x1f;
524 ir->mask_keyup = 0x80;
525 ir->polling = 1; /* ms */
527 case CX88_BOARD_ADSTECH_DVB_T_PCI:
528 ir_codes = ir_codes_adstech_dvb_t_pci;
529 ir->gpio_addr = MO_GP1_IO;
530 ir->mask_keycode = 0xbf;
531 ir->mask_keyup = 0x40;
532 ir->polling = 50; /* ms */
534 case CX88_BOARD_MSI_TVANYWHERE_MASTER:
535 ir_codes = ir_codes_msi_tvanywhere;
536 ir->gpio_addr = MO_GP1_IO;
537 ir->mask_keycode = 0x1f;
538 ir->mask_keyup = 0x40;
539 ir->polling = 1; /* ms */
541 case CX88_BOARD_AVERTV_303:
542 case CX88_BOARD_AVERTV_STUDIO_303:
543 ir_codes = ir_codes_avertv_303;
544 ir->gpio_addr = MO_GP2_IO;
545 ir->mask_keycode = 0xfb;
546 ir->mask_keydown = 0x02;
547 ir->polling = 50; /* ms */
549 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
550 ir_codes = ir_codes_dntv_live_dvbt_pro;
551 ir_type = IR_TYPE_PD;
552 ir->sampling = 0xff00; /* address */
556 if (NULL == ir_codes) {
558 input_free_device(input_dev);
562 /* init input device */
563 snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)",
564 cx88_boards[core->board].name);
565 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(pci));
567 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
568 input_dev->name = ir->name;
569 input_dev->phys = ir->phys;
570 input_dev->id.bustype = BUS_PCI;
571 input_dev->id.version = 1;
572 if (pci->subsystem_vendor) {
573 input_dev->id.vendor = pci->subsystem_vendor;
574 input_dev->id.product = pci->subsystem_device;
576 input_dev->id.vendor = pci->vendor;
577 input_dev->id.product = pci->device;
579 input_dev->cdev.dev = &pci->dev;
580 /* record handles to ourself */
585 INIT_WORK(&ir->work, cx88_ir_work, ir);
586 init_timer(&ir->timer);
587 ir->timer.function = ir_timer;
588 ir->timer.data = (unsigned long)ir;
589 schedule_work(&ir->work);
592 core->pci_irqmask |= (1 << 18); /* IR_SMP_INT */
593 cx_write(MO_DDS_IO, 0xa80a80); /* 4 kHz sample rate */
594 cx_write(MO_DDSCFG_IO, 0x5); /* enable */
598 input_register_device(ir->input);
603 int cx88_ir_fini(struct cx88_core *core)
605 struct cx88_IR *ir = core->ir;
607 /* skip detach on non attached boards */
612 cx_write(MO_DDSCFG_IO, 0x0);
613 core->pci_irqmask &= ~(1 << 18);
616 del_timer(&ir->timer);
617 flush_scheduled_work();
620 input_unregister_device(ir->input);
628 /* ---------------------------------------------------------------------- */
630 void cx88_ir_irq(struct cx88_core *core)
632 struct cx88_IR *ir = core->ir;
641 samples = cx_read(MO_SAMPLE_IO);
642 if (0 != samples && 0xffffffff != samples) {
643 /* record sample data */
644 if (ir->scount < ARRAY_SIZE(ir->samples))
645 ir->samples[ir->scount++] = samples;
649 /* nothing to sample */
650 if (ir->ir.keypressed && time_after(jiffies, ir->release))
651 ir_input_nokey(ir->input, &ir->ir);
655 /* have a complete sample */
656 if (ir->scount < ARRAY_SIZE(ir->samples))
657 ir->samples[ir->scount++] = samples;
658 for (i = 0; i < ir->scount; i++)
659 ir->samples[i] = ~ir->samples[i];
661 ir_dump_samples(ir->samples, ir->scount);
664 switch (core->board) {
665 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
666 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
667 ircode = ir_decode_pulsedistance(ir->samples, ir->scount, 1, 4);
669 if (ircode == 0xffffffff) { /* decoding error */
670 ir_dprintk("pulse distance decoding error\n");
674 ir_dprintk("pulse distance decoded: %x\n", ircode);
676 if (ircode == 0) { /* key still pressed */
677 ir_dprintk("pulse distance decoded repeat code\n");
678 ir->release = jiffies + msecs_to_jiffies(120);
682 if ((ircode & 0xffff) != (ir->sampling & 0xffff)) { /* wrong address */
683 ir_dprintk("pulse distance decoded wrong address\n");
687 if (((~ircode >> 24) & 0xff) != ((ircode >> 16) & 0xff)) { /* wrong checksum */
688 ir_dprintk("pulse distance decoded wrong check sum\n");
692 ir_dprintk("Key Code: %x\n", (ircode >> 16) & 0x7f);
694 ir_input_keydown(ir->input, &ir->ir, (ircode >> 16) & 0x7f, (ircode >> 16) & 0xff);
695 ir->release = jiffies + msecs_to_jiffies(120);
697 case CX88_BOARD_HAUPPAUGE:
698 case CX88_BOARD_HAUPPAUGE_DVB_T1:
699 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
700 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
701 case CX88_BOARD_HAUPPAUGE_HVR1100:
702 ircode = ir_decode_biphase(ir->samples, ir->scount, 5, 7);
703 ir_dprintk("biphase decoded: %x\n", ircode);
704 if ((ircode & 0xfffff000) != 0x3000)
706 ir_input_keydown(ir->input, &ir->ir, ircode & 0x3f, ircode);
707 ir->release = jiffies + msecs_to_jiffies(120);
715 /* ---------------------------------------------------------------------- */
717 MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
718 MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
719 MODULE_LICENSE("GPL");