2 * $Id: cx88-input.c,v 1.9 2005/03/04 09:12:23 kraxel 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 ir_codes
= ir_codes_dntv_live_dvb_t
;
239 ir
->gpio_addr
= MO_GP1_IO
;
240 ir
->mask_keycode
= 0x1f;
241 ir
->mask_keyup
= 0x60;
242 ir
->polling
= 50; // ms
244 case CX88_BOARD_HAUPPAUGE
:
245 case CX88_BOARD_HAUPPAUGE_DVB_T1
:
246 ir_codes
= ir_codes_hauppauge_new
;
247 ir_type
= IR_TYPE_RC5
;
250 case CX88_BOARD_WINFAST2000XP_EXPERT
:
251 ir_codes
= ir_codes_winfast
;
252 ir
->gpio_addr
= MO_GP0_IO
;
253 ir
->mask_keycode
= 0x8f8;
254 ir
->mask_keyup
= 0x100;
255 ir
->polling
= 1; // ms
257 case CX88_BOARD_IODATA_GVBCTV7E
:
258 ir_codes
= ir_codes_iodata_bctv7e
;
259 ir
->gpio_addr
= MO_GP0_IO
;
260 ir
->mask_keycode
= 0xfd;
261 ir
->mask_keydown
= 0x02;
262 ir
->polling
= 5; // ms
265 if (NULL
== ir_codes
) {
270 /* init input device */
271 snprintf(ir
->name
, sizeof(ir
->name
), "cx88 IR (%s)",
272 cx88_boards
[core
->board
].name
);
273 snprintf(ir
->phys
, sizeof(ir
->phys
), "pci-%s/ir0",
276 ir_input_init(&ir
->input
, &ir
->ir
, ir_type
, ir_codes
);
277 ir
->input
.name
= ir
->name
;
278 ir
->input
.phys
= ir
->phys
;
279 ir
->input
.id
.bustype
= BUS_PCI
;
280 ir
->input
.id
.version
= 1;
281 if (pci
->subsystem_vendor
) {
282 ir
->input
.id
.vendor
= pci
->subsystem_vendor
;
283 ir
->input
.id
.product
= pci
->subsystem_device
;
285 ir
->input
.id
.vendor
= pci
->vendor
;
286 ir
->input
.id
.product
= pci
->device
;
289 /* record handles to ourself */
294 INIT_WORK(&ir
->work
, cx88_ir_work
, ir
);
295 init_timer(&ir
->timer
);
296 ir
->timer
.function
= ir_timer
;
297 ir
->timer
.data
= (unsigned long)ir
;
298 schedule_work(&ir
->work
);
301 core
->pci_irqmask
|= (1<<18); // IR_SMP_INT
302 cx_write(MO_DDS_IO
, 0xa80a80); // 4 kHz sample rate
303 cx_write(MO_DDSCFG_IO
, 0x5); // enable
307 input_register_device(&ir
->input
);
308 printk("%s: registered IR remote control\n", core
->name
);
313 int cx88_ir_fini(struct cx88_core
*core
)
315 struct cx88_IR
*ir
= core
->ir
;
317 /* skip detach on non attached boards */
322 del_timer(&ir
->timer
);
323 flush_scheduled_work();
326 input_unregister_device(&ir
->input
);
334 /* ---------------------------------------------------------------------- */
336 void cx88_ir_irq(struct cx88_core
*core
)
338 struct cx88_IR
*ir
= core
->ir
;
347 samples
= cx_read(MO_SAMPLE_IO
);
348 if (0 != samples
&& 0xffffffff != samples
) {
349 /* record sample data */
350 if (ir
->scount
< ARRAY_SIZE(ir
->samples
))
351 ir
->samples
[ir
->scount
++] = samples
;
355 /* nothing to sample */
356 if (ir
->ir
.keypressed
&& time_after(jiffies
,ir
->release
))
357 ir_input_nokey(&ir
->input
,&ir
->ir
);
361 /* have a complete sample */
362 if (ir
->scount
< ARRAY_SIZE(ir
->samples
))
363 ir
->samples
[ir
->scount
++] = samples
;
364 for (i
= 0; i
< ir
->scount
; i
++)
365 ir
->samples
[i
] = ~ir
->samples
[i
];
367 ir_dump_samples(ir
->samples
,ir
->scount
);
370 switch (core
->board
) {
371 case CX88_BOARD_HAUPPAUGE
:
372 case CX88_BOARD_HAUPPAUGE_DVB_T1
:
373 rc5
= ir_decode_biphase(ir
->samples
,ir
->scount
,5,7);
374 ir_dprintk("biphase decoded: %x\n",rc5
);
375 if ((rc5
& 0xfffff000) != 0x3000)
377 ir_input_keydown(&ir
->input
, &ir
->ir
, rc5
& 0x3f, rc5
);
378 ir
->release
= jiffies
+ msecs_to_jiffies(120);
386 /* ---------------------------------------------------------------------- */
388 MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
389 MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
390 MODULE_LICENSE("GPL");