]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/media/video/cx88/cx88-input.c
V4L/DVB (3294): Fix [Bug 5895] to correct snd_87x autodetect
[mirror_ubuntu-hirsute-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;
b639f9d2
NS
195 case CX88_BOARD_KWORLD_LTV883:
196 ir_codes = ir_codes_pixelview;
197 ir->gpio_addr = MO_GP1_IO;
198 ir->mask_keycode = 0x1f;
199 ir->mask_keyup = 0x60;
200 ir->polling = 1; /* ms */
201 break;
a82decf6 202 case CX88_BOARD_ADSTECH_DVB_T_PCI:
41ef7c1e
MCC
203 ir_codes = ir_codes_adstech_dvb_t_pci;
204 ir->gpio_addr = MO_GP1_IO;
a82decf6 205 ir->mask_keycode = 0xbf;
41ef7c1e
MCC
206 ir->mask_keyup = 0x40;
207 ir->polling = 50; /* ms */
208 break;
209 case CX88_BOARD_MSI_TVANYWHERE_MASTER:
210 ir_codes = ir_codes_msi_tvanywhere;
211 ir->gpio_addr = MO_GP1_IO;
212 ir->mask_keycode = 0x1f;
213 ir->mask_keyup = 0x40;
214 ir->polling = 1; /* ms */
a82decf6 215 break;
899ad11b 216 case CX88_BOARD_AVERTV_303:
565f4949 217 case CX88_BOARD_AVERTV_STUDIO_303:
899ad11b
GG
218 ir_codes = ir_codes_avertv_303;
219 ir->gpio_addr = MO_GP2_IO;
220 ir->mask_keycode = 0xfb;
221 ir->mask_keydown = 0x02;
222 ir->polling = 50; /* ms */
223 break;
fc40b261
CP
224 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
225 ir_codes = ir_codes_dntv_live_dvbt_pro;
226 ir_type = IR_TYPE_PD;
227 ir->sampling = 0xff00; /* address */
228 break;
1da177e4 229 }
b45009b0 230
1da177e4
LT
231 if (NULL == ir_codes) {
232 kfree(ir);
b7df3910 233 input_free_device(input_dev);
1da177e4
LT
234 return -ENODEV;
235 }
236
237 /* init input device */
238 snprintf(ir->name, sizeof(ir->name), "cx88 IR (%s)",
239 cx88_boards[core->board].name);
41ef7c1e 240 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0", pci_name(pci));
1da177e4 241
b7df3910
DT
242 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
243 input_dev->name = ir->name;
244 input_dev->phys = ir->phys;
245 input_dev->id.bustype = BUS_PCI;
246 input_dev->id.version = 1;
1da177e4 247 if (pci->subsystem_vendor) {
b7df3910
DT
248 input_dev->id.vendor = pci->subsystem_vendor;
249 input_dev->id.product = pci->subsystem_device;
1da177e4 250 } else {
b7df3910
DT
251 input_dev->id.vendor = pci->vendor;
252 input_dev->id.product = pci->device;
1da177e4 253 }
b7df3910 254 input_dev->cdev.dev = &pci->dev;
1da177e4
LT
255 /* record handles to ourself */
256 ir->core = core;
257 core->ir = ir;
258
259 if (ir->polling) {
260 INIT_WORK(&ir->work, cx88_ir_work, ir);
261 init_timer(&ir->timer);
262 ir->timer.function = ir_timer;
41ef7c1e 263 ir->timer.data = (unsigned long)ir;
1da177e4
LT
264 schedule_work(&ir->work);
265 }
266 if (ir->sampling) {
41ef7c1e
MCC
267 core->pci_irqmask |= (1 << 18); /* IR_SMP_INT */
268 cx_write(MO_DDS_IO, 0xa80a80); /* 4 kHz sample rate */
269 cx_write(MO_DDSCFG_IO, 0x5); /* enable */
1da177e4
LT
270 }
271
272 /* all done */
b7df3910 273 input_register_device(ir->input);
1da177e4
LT
274
275 return 0;
276}
277
278int cx88_ir_fini(struct cx88_core *core)
279{
280 struct cx88_IR *ir = core->ir;
281
282 /* skip detach on non attached boards */
283 if (NULL == ir)
284 return 0;
285
fc40b261
CP
286 if (ir->sampling) {
287 cx_write(MO_DDSCFG_IO, 0x0);
288 core->pci_irqmask &= ~(1 << 18);
289 }
1da177e4
LT
290 if (ir->polling) {
291 del_timer(&ir->timer);
292 flush_scheduled_work();
293 }
294
b7df3910 295 input_unregister_device(ir->input);
1da177e4
LT
296 kfree(ir);
297
298 /* done */
299 core->ir = NULL;
300 return 0;
301}
302
303/* ---------------------------------------------------------------------- */
304
305void cx88_ir_irq(struct cx88_core *core)
306{
307 struct cx88_IR *ir = core->ir;
e52e98a7 308 u32 samples, ircode;
1da177e4
LT
309 int i;
310
311 if (NULL == ir)
312 return;
313 if (!ir->sampling)
314 return;
315
316 samples = cx_read(MO_SAMPLE_IO);
41ef7c1e 317 if (0 != samples && 0xffffffff != samples) {
1da177e4
LT
318 /* record sample data */
319 if (ir->scount < ARRAY_SIZE(ir->samples))
320 ir->samples[ir->scount++] = samples;
321 return;
322 }
323 if (!ir->scount) {
324 /* nothing to sample */
41ef7c1e 325 if (ir->ir.keypressed && time_after(jiffies, ir->release))
b7df3910 326 ir_input_nokey(ir->input, &ir->ir);
1da177e4
LT
327 return;
328 }
329
330 /* have a complete sample */
331 if (ir->scount < ARRAY_SIZE(ir->samples))
332 ir->samples[ir->scount++] = samples;
333 for (i = 0; i < ir->scount; i++)
334 ir->samples[i] = ~ir->samples[i];
335 if (ir_debug)
41ef7c1e 336 ir_dump_samples(ir->samples, ir->scount);
1da177e4
LT
337
338 /* decode it */
339 switch (core->board) {
e52e98a7 340 case CX88_BOARD_TERRATEC_CINERGY_1400_DVB_T1:
fc40b261 341 case CX88_BOARD_DNTV_LIVE_DVB_T_PRO:
e52e98a7
MCC
342 ircode = ir_decode_pulsedistance(ir->samples, ir->scount, 1, 4);
343
344 if (ircode == 0xffffffff) { /* decoding error */
345 ir_dprintk("pulse distance decoding error\n");
346 break;
347 }
348
349 ir_dprintk("pulse distance decoded: %x\n", ircode);
350
351 if (ircode == 0) { /* key still pressed */
352 ir_dprintk("pulse distance decoded repeat code\n");
353 ir->release = jiffies + msecs_to_jiffies(120);
354 break;
355 }
356
fc40b261 357 if ((ircode & 0xffff) != (ir->sampling & 0xffff)) { /* wrong address */
e52e98a7 358 ir_dprintk("pulse distance decoded wrong address\n");
4ac97914 359 break;
e52e98a7
MCC
360 }
361
362 if (((~ircode >> 24) & 0xff) != ((ircode >> 16) & 0xff)) { /* wrong checksum */
363 ir_dprintk("pulse distance decoded wrong check sum\n");
364 break;
365 }
366
367 ir_dprintk("Key Code: %x\n", (ircode >> 16) & 0x7f);
368
b7df3910 369 ir_input_keydown(ir->input, &ir->ir, (ircode >> 16) & 0x7f, (ircode >> 16) & 0xff);
e52e98a7
MCC
370 ir->release = jiffies + msecs_to_jiffies(120);
371 break;
1da177e4
LT
372 case CX88_BOARD_HAUPPAUGE:
373 case CX88_BOARD_HAUPPAUGE_DVB_T1:
fb56cb65
ST
374 case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
375 case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
30367bfd 376 case CX88_BOARD_HAUPPAUGE_HVR1100:
e52e98a7
MCC
377 ircode = ir_decode_biphase(ir->samples, ir->scount, 5, 7);
378 ir_dprintk("biphase decoded: %x\n", ircode);
379 if ((ircode & 0xfffff000) != 0x3000)
1da177e4 380 break;
b7df3910 381 ir_input_keydown(ir->input, &ir->ir, ircode & 0x3f, ircode);
1da177e4
LT
382 ir->release = jiffies + msecs_to_jiffies(120);
383 break;
384 }
385
386 ir->scount = 0;
387 return;
388}
389
390/* ---------------------------------------------------------------------- */
391
392MODULE_AUTHOR("Gerd Knorr, Pavel Machek, Chris Pascoe");
393MODULE_DESCRIPTION("input driver for cx88 GPIO-based IR remote controls");
394MODULE_LICENSE("GPL");
1da177e4
LT
395/*
396 * Local variables:
397 * c-basic-offset: 8
398 * End:
399 */