]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/cx88/cx88-input.c
V4L/DVB (3393): Move all IR keymaps to ir-common module
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / cx88 / cx88-input.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 *
3 * Device driver for GPIO attached remote control interfaces
4 * on Conexant 2388x based TV/DVB cards.
5 *
6 * Copyright (c) 2003 Pavel Machek
7 * Copyright (c) 2004 Gerd Knorr
fc40b261 8 * Copyright (c) 2004, 2005 Chris Pascoe
1da177e4
LT
9 *
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.
14 *
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.
19 *
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
23 */
24
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>
31
1da177e4 32#include "cx88.h"
d21838dd 33#include <media/ir-common.h>
1da177e4
LT
34
35/* ---------------------------------------------------------------------- */
36
1da177e4 37struct cx88_IR {
41ef7c1e 38 struct cx88_core *core;
b7df3910 39 struct input_dev *input;
41ef7c1e
MCC
40 struct ir_input_state ir;
41 char name[32];
42 char phys[32];
1da177e4
LT
43
44 /* sample from gpio pin 16 */
fc40b261 45 u32 sampling;
41ef7c1e
MCC
46 u32 samples[16];
47 int scount;
48 unsigned long release;
1da177e4
LT
49
50 /* poll external decoder */
41ef7c1e
MCC
51 int polling;
52 struct work_struct work;
53 struct timer_list timer;
54 u32 gpio_addr;
55 u32 last_gpio;
56 u32 mask_keycode;
57 u32 mask_keydown;
58 u32 mask_keyup;
1da177e4
LT
59};
60
61static int ir_debug = 0;
41ef7c1e 62module_param(ir_debug, int, 0644); /* debug level [IR] */
1da177e4
LT
63MODULE_PARM_DESC(ir_debug, "enable debug messages [IR]");
64
65#define ir_dprintk(fmt, arg...) if (ir_debug) \
e52e98a7 66 printk(KERN_DEBUG "%s IR: " fmt , ir->core->name , ##arg)
1da177e4
LT
67
68/* ---------------------------------------------------------------------- */
69
70static void cx88_ir_handle_key(struct cx88_IR *ir)
71{
72 struct cx88_core *core = ir->core;
73 u32 gpio, data;
74
75 /* read gpio value */
76 gpio = cx_read(ir->gpio_addr);
77 if (ir->polling) {
78 if (ir->last_gpio == gpio)
79 return;
80 ir->last_gpio = gpio;
81 }
82
83 /* extract data */
84 data = ir_extract_bits(gpio, ir->mask_keycode);
85 ir_dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
41ef7c1e
MCC
86 gpio, data,
87 ir->polling ? "poll" : "irq",
88 (gpio & ir->mask_keydown) ? " down" : "",
89 (gpio & ir->mask_keyup) ? " up" : "");
1da177e4
LT
90
91 if (ir->mask_keydown) {
92 /* bit set on keydown */
93 if (gpio & ir->mask_keydown) {
b7df3910 94 ir_input_keydown(ir->input, &ir->ir, data, data);
1da177e4 95 } else {
b7df3910 96 ir_input_nokey(ir->input, &ir->ir);
1da177e4
LT
97 }
98
99 } else if (ir->mask_keyup) {
100 /* bit cleared on keydown */
101 if (0 == (gpio & ir->mask_keyup)) {
b7df3910 102 ir_input_keydown(ir->input, &ir->ir, data, data);
1da177e4 103 } else {
b7df3910 104 ir_input_nokey(ir->input, &ir->ir);
1da177e4
LT
105 }
106
107 } else {
108 /* can't distinguish keydown/up :-/ */
b7df3910
DT
109 ir_input_keydown(ir->input, &ir->ir, data, data);
110 ir_input_nokey(ir->input, &ir->ir);
1da177e4
LT
111 }
112}
113
114static void ir_timer(unsigned long data)
115{
41ef7c1e 116 struct cx88_IR *ir = (struct cx88_IR *)data;
1da177e4
LT
117
118 schedule_work(&ir->work);
119}
120
121static void cx88_ir_work(void *data)
122{
123 struct cx88_IR *ir = data;
124 unsigned long timeout;
125
126 cx88_ir_handle_key(ir);
127 timeout = jiffies + (ir->polling * HZ / 1000);
128 mod_timer(&ir->timer, timeout);
129}
130
131/* ---------------------------------------------------------------------- */
132
133int cx88_ir_init(struct cx88_core *core, struct pci_dev *pci)
134{
135 struct cx88_IR *ir;
b7df3910 136 struct input_dev *input_dev;
1da177e4
LT
137 IR_KEYTAB_TYPE *ir_codes = NULL;
138 int ir_type = IR_TYPE_OTHER;
139
b7df3910
DT
140 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
141 input_dev = input_allocate_device();
142 if (!ir || !input_dev) {
143 kfree(ir);
144 input_free_device(input_dev);
1da177e4 145 return -ENOMEM;
b7df3910
DT
146 }
147
148 ir->input = input_dev;
1da177e4
LT
149
150 /* detect & configure */
151 switch (core->board) {
152 case CX88_BOARD_DNTV_LIVE_DVB_T:
b45009b0 153 case CX88_BOARD_KWORLD_DVB_T:
41ef7c1e
MCC
154 ir_codes = ir_codes_dntv_live_dvb_t;
155 ir->gpio_addr = MO_GP1_IO;
1da177e4 156 ir->mask_keycode = 0x1f;
41ef7c1e
MCC
157 ir->mask_keyup = 0x60;
158 ir->polling = 50; /* ms */
1da177e4 159 break;
e52e98a7
MCC
160 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
161 ir_codes = ir_codes_cinergy_1400;
162 ir_type = IR_TYPE_PD;
fc40b261 163 ir->sampling = 0xeb04; /* address */
e52e98a7 164 break;
1da177e4
LT
165 case CX88_BOARD_HAUPPAUGE:
166 case CX88_BOARD_HAUPPAUGE_DVB_T1:
fb56cb65
ST
167 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
168 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
611900c1 169 case CX88_BOARD_HAUPPAUGE_HVR1100:
41ef7c1e
MCC
170 ir_codes = ir_codes_hauppauge_new;
171 ir_type = IR_TYPE_RC5;
172 ir->sampling = 1;
1da177e4
LT
173 break;
174 case CX88_BOARD_WINFAST2000XP_EXPERT:
41ef7c1e
MCC
175 ir_codes = ir_codes_winfast;
176 ir->gpio_addr = MO_GP0_IO;
1da177e4 177 ir->mask_keycode = 0x8f8;
41ef7c1e
MCC
178 ir->mask_keyup = 0x100;
179 ir->polling = 1; /* ms */
1da177e4
LT
180 break;
181 case CX88_BOARD_IODATA_GVBCTV7E:
41ef7c1e
MCC
182 ir_codes = ir_codes_iodata_bctv7e;
183 ir->gpio_addr = MO_GP0_IO;
1da177e4
LT
184 ir->mask_keycode = 0xfd;
185 ir->mask_keydown = 0x02;
41ef7c1e 186 ir->polling = 5; /* ms */
1da177e4 187 break;
239df2e2 188 case CX88_BOARD_PIXELVIEW_PLAYTV_ULTRA_PRO:
41ef7c1e
MCC
189 ir_codes = ir_codes_pixelview;
190 ir->gpio_addr = MO_GP1_IO;
239df2e2 191 ir->mask_keycode = 0x1f;
41ef7c1e
MCC
192 ir->mask_keyup = 0x80;
193 ir->polling = 1; /* ms */
239df2e2 194 break;
a82decf6 195 case CX88_BOARD_ADSTECH_DVB_T_PCI:
41ef7c1e
MCC
196 ir_codes = ir_codes_adstech_dvb_t_pci;
197 ir->gpio_addr = MO_GP1_IO;
a82decf6 198 ir->mask_keycode = 0xbf;
41ef7c1e
MCC
199 ir->mask_keyup = 0x40;
200 ir->polling = 50; /* ms */
201 break;
202 case CX88_BOARD_MSI_TVANYWHERE_MASTER:
203 ir_codes = ir_codes_msi_tvanywhere;
204 ir->gpio_addr = MO_GP1_IO;
205 ir->mask_keycode = 0x1f;
206 ir->mask_keyup = 0x40;
207 ir->polling = 1; /* ms */
a82decf6 208 break;
899ad11b 209 case CX88_BOARD_AVERTV_303:
565f4949 210 case CX88_BOARD_AVERTV_STUDIO_303:
899ad11b
GG
211 ir_codes = ir_codes_avertv_303;
212 ir->gpio_addr = MO_GP2_IO;
213 ir->mask_keycode = 0xfb;
214 ir->mask_keydown = 0x02;
215 ir->polling = 50; /* ms */
216 break;
fc40b261
CP
217 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
218 ir_codes = ir_codes_dntv_live_dvbt_pro;
219 ir_type = IR_TYPE_PD;
220 ir->sampling = 0xff00; /* address */
221 break;
1da177e4 222 }
b45009b0 223
1da177e4
LT
224 if (NULL == ir_codes) {
225 kfree(ir);
b7df3910 226 input_free_device(input_dev);
1da177e4
LT
227 return -ENODEV;
228 }
229
230 /* init input device */
231 snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)",
232 cx88_boards[core->board].name);
41ef7c1e 233 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(pci));
1da177e4 234
b7df3910
DT
235 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
236 input_dev->name = ir->name;
237 input_dev->phys = ir->phys;
238 input_dev->id.bustype = BUS_PCI;
239 input_dev->id.version = 1;
1da177e4 240 if (pci->subsystem_vendor) {
b7df3910
DT
241 input_dev->id.vendor = pci->subsystem_vendor;
242 input_dev->id.product = pci->subsystem_device;
1da177e4 243 } else {
b7df3910
DT
244 input_dev->id.vendor = pci->vendor;
245 input_dev->id.product = pci->device;
1da177e4 246 }
b7df3910 247 input_dev->cdev.dev = &pci->dev;
1da177e4
LT
248 /* record handles to ourself */
249 ir->core = core;
250 core->ir = ir;
251
252 if (ir->polling) {
253 INIT_WORK(&ir->work, cx88_ir_work, ir);
254 init_timer(&ir->timer);
255 ir->timer.function = ir_timer;
41ef7c1e 256 ir->timer.data = (unsigned long)ir;
1da177e4
LT
257 schedule_work(&ir->work);
258 }
259 if (ir->sampling) {
41ef7c1e
MCC
260 core->pci_irqmask |= (1 << 18); /* IR_SMP_INT */
261 cx_write(MO_DDS_IO, 0xa80a80); /* 4 kHz sample rate */
262 cx_write(MO_DDSCFG_IO, 0x5); /* enable */
1da177e4
LT
263 }
264
265 /* all done */
b7df3910 266 input_register_device(ir->input);
1da177e4
LT
267
268 return 0;
269}
270
271int cx88_ir_fini(struct cx88_core *core)
272{
273 struct cx88_IR *ir = core->ir;
274
275 /* skip detach on non attached boards */
276 if (NULL == ir)
277 return 0;
278
fc40b261
CP
279 if (ir->sampling) {
280 cx_write(MO_DDSCFG_IO, 0x0);
281 core->pci_irqmask &= ~(1 << 18);
282 }
1da177e4
LT
283 if (ir->polling) {
284 del_timer(&ir->timer);
285 flush_scheduled_work();
286 }
287
b7df3910 288 input_unregister_device(ir->input);
1da177e4
LT
289 kfree(ir);
290
291 /* done */
292 core->ir = NULL;
293 return 0;
294}
295
296/* ---------------------------------------------------------------------- */
297
298void cx88_ir_irq(struct cx88_core *core)
299{
300 struct cx88_IR *ir = core->ir;
e52e98a7 301 u32 samples, ircode;
1da177e4
LT
302 int i;
303
304 if (NULL == ir)
305 return;
306 if (!ir->sampling)
307 return;
308
309 samples = cx_read(MO_SAMPLE_IO);
41ef7c1e 310 if (0 != samples && 0xffffffff != samples) {
1da177e4
LT
311 /* record sample data */
312 if (ir->scount < ARRAY_SIZE(ir->samples))
313 ir->samples[ir->scount++] = samples;
314 return;
315 }
316 if (!ir->scount) {
317 /* nothing to sample */
41ef7c1e 318 if (ir->ir.keypressed && time_after(jiffies, ir->release))
b7df3910 319 ir_input_nokey(ir->input, &ir->ir);
1da177e4
LT
320 return;
321 }
322
323 /* have a complete sample */
324 if (ir->scount < ARRAY_SIZE(ir->samples))
325 ir->samples[ir->scount++] = samples;
326 for (i = 0; i < ir->scount; i++)
327 ir->samples[i] = ~ir->samples[i];
328 if (ir_debug)
41ef7c1e 329 ir_dump_samples(ir->samples, ir->scount);
1da177e4
LT
330
331 /* decode it */
332 switch (core->board) {
e52e98a7 333 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
fc40b261 334 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
e52e98a7
MCC
335 ircode = ir_decode_pulsedistance(ir->samples, ir->scount, 1, 4);
336
337 if (ircode == 0xffffffff) { /* decoding error */
338 ir_dprintk("pulse distance decoding error\n");
339 break;
340 }
341
342 ir_dprintk("pulse distance decoded: %x\n", ircode);
343
344 if (ircode == 0) { /* key still pressed */
345 ir_dprintk("pulse distance decoded repeat code\n");
346 ir->release = jiffies + msecs_to_jiffies(120);
347 break;
348 }
349
fc40b261 350 if ((ircode & 0xffff) != (ir->sampling & 0xffff)) { /* wrong address */
e52e98a7 351 ir_dprintk("pulse distance decoded wrong address\n");
4ac97914 352 break;
e52e98a7
MCC
353 }
354
355 if (((~ircode >> 24) & 0xff) != ((ircode >> 16) & 0xff)) { /* wrong checksum */
356 ir_dprintk("pulse distance decoded wrong check sum\n");
357 break;
358 }
359
360 ir_dprintk("Key Code: %x\n", (ircode >> 16) & 0x7f);
361
b7df3910 362 ir_input_keydown(ir->input, &ir->ir, (ircode >> 16) & 0x7f, (ircode >> 16) & 0xff);
e52e98a7
MCC
363 ir->release = jiffies + msecs_to_jiffies(120);
364 break;
1da177e4
LT
365 case CX88_BOARD_HAUPPAUGE:
366 case CX88_BOARD_HAUPPAUGE_DVB_T1:
fb56cb65
ST
367 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
368 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
30367bfd 369 case CX88_BOARD_HAUPPAUGE_HVR1100:
e52e98a7
MCC
370 ircode = ir_decode_biphase(ir->samples, ir->scount, 5, 7);
371 ir_dprintk("biphase decoded: %x\n", ircode);
372 if ((ircode & 0xfffff000) != 0x3000)
1da177e4 373 break;
b7df3910 374 ir_input_keydown(ir->input, &ir->ir, ircode & 0x3f, ircode);
1da177e4
LT
375 ir->release = jiffies + msecs_to_jiffies(120);
376 break;
377 }
378
379 ir->scount = 0;
380 return;
381}
382
383/* ---------------------------------------------------------------------- */
384
385MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
386MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
387MODULE_LICENSE("GPL");
1da177e4
LT
388/*
389 * Local variables:
390 * c-basic-offset: 8
391 * End:
392 */