2 * $Id: cx88-input.c,v 1.11 2005/05/22 20:57:56 nsh 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?'
42 [ 0x01 ] = KEY_KP1, // '1'
43 [ 0x02 ] = KEY_KP2, // '2'
44 [ 0x03 ] = KEY_KP3, // '3'
45 [ 0x04 ] = KEY_KP4, // '4'
46 [ 0x05 ] = KEY_KP5, // '5'
47 [ 0x06 ] = KEY_KP6, // '6'
48 [ 0x07 ] = KEY_KP7, // '7'
49 [ 0x08 ] = KEY_KP8, // '8'
50 [ 0x09 ] = KEY_KP9, // '9'
51 [ 0x0a ] = KEY_KP0, // '0'
52 [ 0x0b ] = KEY_TUNER, // 'tv/fm'
53 [ 0x0c ] = KEY_SEARCH, // 'scan'
54 [ 0x0d ] = KEY_STOP, // 'stop'
55 [ 0x0e ] = KEY_PAUSE, // 'pause'
56 [ 0x0f ] = KEY_LIST, // 'source'
58 [ 0x10 ] = KEY_MUTE, // 'mute'
59 [ 0x11 ] = KEY_REWIND, // 'backward <<'
60 [ 0x12 ] = KEY_POWER, // 'power'
61 [ 0x13 ] = KEY_S, // 'snap'
62 [ 0x14 ] = KEY_AUDIO, // 'stereo'
63 [ 0x15 ] = KEY_CLEAR, // 'reset'
64 [ 0x16 ] = KEY_PLAY, // 'play'
65 [ 0x17 ] = KEY_ENTER, // 'enter'
66 [ 0x18 ] = KEY_ZOOM, // 'full screen'
67 [ 0x19 ] = KEY_FASTFORWARD, // 'forward >>'
68 [ 0x1a ] = KEY_CHANNELUP, // 'channel +'
69 [ 0x1b ] = KEY_VOLUMEUP, // 'volume +'
70 [ 0x1c ] = KEY_INFO, // 'preview'
71 [ 0x1d ] = KEY_RECORD, // 'record'
72 [ 0x1e ] = KEY_CHANNELDOWN, // 'channel -'
73 [ 0x1f ] = KEY_VOLUMEDOWN, // 'volume -'
76 /* ---------------------------------------------------------------------- */
78 /* IO-DATA BCTV7E Remote */
79 static IR_KEYTAB_TYPE ir_codes_iodata_bctv7e[IR_KEYTAB_SIZE] = {
80 [ 0x40 ] = KEY_TV, // TV
81 [ 0x20 ] = KEY_RADIO, // FM
82 [ 0x60 ] = KEY_EPG, // EPG
83 [ 0x00 ] = KEY_POWER, // power
85 [ 0x50 ] = KEY_KP1, // 1
86 [ 0x30 ] = KEY_KP2, // 2
87 [ 0x70 ] = KEY_KP3, // 3
88 [ 0x10 ] = KEY_L, // Live
90 [ 0x48 ] = KEY_KP4, // 4
91 [ 0x28 ] = KEY_KP5, // 5
92 [ 0x68 ] = KEY_KP6, // 6
93 [ 0x08 ] = KEY_T, // Time Shift
95 [ 0x58 ] = KEY_KP7, // 7
96 [ 0x38 ] = KEY_KP8, // 8
97 [ 0x78 ] = KEY_KP9, // 9
98 [ 0x18 ] = KEY_PLAYPAUSE, // Play
100 [ 0x44 ] = KEY_KP0, // 10
101 [ 0x24 ] = KEY_ENTER, // 11
102 [ 0x64 ] = KEY_ESC, // 12
103 [ 0x04 ] = KEY_M, // Multi
105 [ 0x54 ] = KEY_VIDEO, // VIDEO
106 [ 0x34 ] = KEY_CHANNELUP, // channel +
107 [ 0x74 ] = KEY_VOLUMEUP, // volume +
108 [ 0x14 ] = KEY_MUTE, // Mute
110 [ 0x4c ] = KEY_S, // SVIDEO
111 [ 0x2c ] = KEY_CHANNELDOWN, // channel -
112 [ 0x6c ] = KEY_VOLUMEDOWN, // volume -
113 [ 0x0c ] = KEY_ZOOM, // Zoom
115 [ 0x5c ] = KEY_PAUSE, // pause
116 [ 0x3c ] = KEY_C, // || (red)
117 [ 0x7c ] = KEY_RECORD, // recording
118 [ 0x1c ] = KEY_STOP, // stop
120 [ 0x41 ] = KEY_REWIND, // backward <<
121 [ 0x21 ] = KEY_PLAY, // play
122 [ 0x61 ] = KEY_FASTFORWARD, // forward >>
123 [ 0x01 ] = KEY_NEXT, // skip >|
126 /* ---------------------------------------------------------------------- */
129 struct cx88_core *core;
130 struct input_dev input;
131 struct ir_input_state ir;
135 /* sample from gpio pin 16 */
139 unsigned long release;
141 /* poll external decoder */
143 struct work_struct work;
144 struct timer_list timer;
152 static int ir_debug = 0;
153 module_param(ir_debug, int, 0644); /* debug level [IR] */
154 MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
156 #define ir_dprintk(fmt, arg...) if (ir_debug) \
157 printk(KERN_DEBUG "%s IR: " fmt , ir->core->name, ## arg)
159 /* ---------------------------------------------------------------------- */
161 static void cx88_ir_handle_key(struct cx88_IR *ir)
163 struct cx88_core *core = ir->core;
166 /* read gpio value */
167 gpio = cx_read(ir->gpio_addr);
169 if (ir->last_gpio == gpio)
171 ir->last_gpio = gpio;
175 data = ir_extract_bits(gpio, ir->mask_keycode);
176 ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
178 ir->polling ? "poll" : "irq",
179 (gpio & ir->mask_keydown) ? " down" : "",
180 (gpio & ir->mask_keyup) ? " up" : "");
182 if (ir->mask_keydown) {
183 /* bit set on keydown */
184 if (gpio & ir->mask_keydown) {
185 ir_input_keydown(&ir->input,&ir->ir,data,data);
187 ir_input_nokey(&ir->input,&ir->ir);
190 } else if (ir->mask_keyup) {
191 /* bit cleared on keydown */
192 if (0 == (gpio & ir->mask_keyup)) {
193 ir_input_keydown(&ir->input,&ir->ir,data,data);
195 ir_input_nokey(&ir->input,&ir->ir);
199 /* can't distinguish keydown/up :-/ */
200 ir_input_keydown(&ir->input,&ir->ir,data,data);
201 ir_input_nokey(&ir->input,&ir->ir);
205 static void ir_timer(unsigned long data)
207 struct cx88_IR *ir = (struct cx88_IR*)data;
209 schedule_work(&ir->work);
212 static void cx88_ir_work(void *data)
214 struct cx88_IR *ir = data;
215 unsigned long timeout;
217 cx88_ir_handle_key(ir);
218 timeout = jiffies + (ir->polling * HZ / 1000);
219 mod_timer(&ir->timer, timeout);
222 /* ---------------------------------------------------------------------- */
224 int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
227 IR_KEYTAB_TYPE *ir_codes = NULL;
228 int ir_type = IR_TYPE_OTHER;
230 ir = kmalloc(sizeof(*ir),GFP_KERNEL);
233 memset(ir,0,sizeof(*ir));
235 /* detect & configure */
236 switch (core->board) {
237 case CX88_BOARD_DNTV_LIVE_DVB_T:
238 case CX88_BOARD_KWORLD_DVB_T:
239 ir_codes = ir_codes_dntv_live_dvb_t;
240 ir->gpio_addr = MO_GP1_IO;
241 ir->mask_keycode = 0x1f;
242 ir->mask_keyup = 0x60;
243 ir->polling = 50; // ms
245 case CX88_BOARD_HAUPPAUGE:
246 case CX88_BOARD_HAUPPAUGE_DVB_T1:
247 ir_codes = ir_codes_hauppauge_new;
248 ir_type = IR_TYPE_RC5;
251 case CX88_BOARD_WINFAST2000XP_EXPERT:
252 ir_codes = ir_codes_winfast;
253 ir->gpio_addr = MO_GP0_IO;
254 ir->mask_keycode = 0x8f8;
255 ir->mask_keyup = 0x100;
256 ir->polling = 1; // ms
258 case CX88_BOARD_IODATA_GVBCTV7E:
259 ir_codes = ir_codes_iodata_bctv7e;
260 ir->gpio_addr = MO_GP0_IO;
261 ir->mask_keycode = 0xfd;
262 ir->mask_keydown = 0x02;
263 ir->polling = 5; // ms
265 case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
266 ir_codes = ir_codes_pixelview;
267 ir->gpio_addr = MO_GP1_IO;
268 ir->mask_keycode = 0x1f;
269 ir->mask_keyup = 0x80;
270 ir->polling = 1; // ms
274 if (NULL == ir_codes) {
279 /* init input device */
280 snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)",
281 cx88_boards[core->board].name);
282 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
285 ir_input_init(&ir->input, &ir->ir, ir_type, ir_codes);
286 ir->input.name = ir->name;
287 ir->input.phys = ir->phys;
288 ir->input.id.bustype = BUS_PCI;
289 ir->input.id.version = 1;
290 if (pci->subsystem_vendor) {
291 ir->input.id.vendor = pci->subsystem_vendor;
292 ir->input.id.product = pci->subsystem_device;
294 ir->input.id.vendor = pci->vendor;
295 ir->input.id.product = pci->device;
298 /* record handles to ourself */
303 INIT_WORK(&ir->work, cx88_ir_work, ir);
304 init_timer(&ir->timer);
305 ir->timer.function = ir_timer;
306 ir->timer.data = (unsigned long)ir;
307 schedule_work(&ir->work);
310 core->pci_irqmask |= (1<<18); // IR_SMP_INT
311 cx_write(MO_DDS_IO, 0xa80a80); // 4 kHz sample rate
312 cx_write(MO_DDSCFG_IO, 0x5); // enable
316 input_register_device(&ir->input);
317 printk("%s: registered IR remote control\n", core->name);
322 int cx88_ir_fini(struct cx88_core *core)
324 struct cx88_IR *ir = core->ir;
326 /* skip detach on non attached boards */
331 del_timer(&ir->timer);
332 flush_scheduled_work();
335 input_unregister_device(&ir->input);
343 /* ---------------------------------------------------------------------- */
345 void cx88_ir_irq(struct cx88_core *core)
347 struct cx88_IR *ir = core->ir;
356 samples = cx_read(MO_SAMPLE_IO);
357 if (0 != samples && 0xffffffff != samples) {
358 /* record sample data */
359 if (ir->scount < ARRAY_SIZE(ir->samples))
360 ir->samples[ir->scount++] = samples;
364 /* nothing to sample */
365 if (ir->ir.keypressed && time_after(jiffies,ir->release))
366 ir_input_nokey(&ir->input,&ir->ir);
370 /* have a complete sample */
371 if (ir->scount < ARRAY_SIZE(ir->samples))
372 ir->samples[ir->scount++] = samples;
373 for (i = 0; i < ir->scount; i++)
374 ir->samples[i] = ~ir->samples[i];
376 ir_dump_samples(ir->samples,ir->scount);
379 switch (core->board) {
380 case CX88_BOARD_HAUPPAUGE:
381 case CX88_BOARD_HAUPPAUGE_DVB_T1:
382 rc5 = ir_decode_biphase(ir->samples,ir->scount,5,7);
383 ir_dprintk("biphase decoded: %x\n",rc5);
384 if ((rc5 & 0xfffff000) != 0x3000)
386 ir_input_keydown(&ir->input, &ir->ir, rc5 & 0x3f, rc5);
387 ir->release = jiffies + msecs_to_jiffies(120);
395 /* ---------------------------------------------------------------------- */
397 MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
398 MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
399 MODULE_LICENSE("GPL");