]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/video/bttv-input.c
V4L/DVB (3400): Remove duplicated keymaps and add keymap for KWorld LTV883IR.
[mirror_ubuntu-bionic-kernel.git] / drivers / media / video / bttv-input.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 *
3 * Copyright (c) 2003 Gerd Knorr
4 * Copyright (c) 2003 Pavel Machek
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/init.h>
24#include <linux/delay.h>
25#include <linux/interrupt.h>
26#include <linux/input.h>
1da177e4 27
1da177e4 28#include "bttv.h"
4abdfed5 29#include "bttvp.h"
1da177e4 30
6c6c0b2c 31
1da177e4
LT
32static int debug;
33module_param(debug, int, 0644); /* debug level (0,1,2) */
6c6c0b2c
MW
34static int repeat_delay = 500;
35module_param(repeat_delay, int, 0644);
36static int repeat_period = 33;
37module_param(repeat_period, int, 0644);
1da177e4 38
4abdfed5 39#define DEVNAME "bttv-input"
1da177e4
LT
40
41/* ---------------------------------------------------------------------- */
42
4abdfed5 43static void ir_handle_key(struct bttv *btv)
1da177e4 44{
4abdfed5 45 struct bttv_ir *ir = btv->remote;
1da177e4
LT
46 u32 gpio,data;
47
48 /* read gpio value */
4abdfed5 49 gpio = bttv_gpio_read(&btv->c);
1da177e4
LT
50 if (ir->polling) {
51 if (ir->last_gpio == gpio)
52 return;
53 ir->last_gpio = gpio;
54 }
55
56 /* extract data */
57 data = ir_extract_bits(gpio, ir->mask_keycode);
4abdfed5 58 dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
1da177e4
LT
59 gpio, data,
60 ir->polling ? "poll" : "irq",
61 (gpio & ir->mask_keydown) ? " down" : "",
62 (gpio & ir->mask_keyup) ? " up" : "");
63
4abdfed5
RC
64 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
65 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
66 ir_input_keydown(ir->dev,&ir->ir,data,data);
1da177e4 67 } else {
4abdfed5 68 ir_input_nokey(ir->dev,&ir->ir);
1da177e4 69 }
1da177e4 70
1da177e4
LT
71}
72
4abdfed5 73void bttv_input_irq(struct bttv *btv)
1da177e4 74{
4abdfed5 75 struct bttv_ir *ir = btv->remote;
1da177e4 76
4abdfed5
RC
77 if (!ir->polling)
78 ir_handle_key(btv);
1da177e4
LT
79}
80
4abdfed5 81static void bttv_input_timer(unsigned long data)
1da177e4 82{
4abdfed5
RC
83 struct bttv *btv = (struct bttv*)data;
84 struct bttv_ir *ir = btv->remote;
1da177e4
LT
85 unsigned long timeout;
86
4abdfed5 87 ir_handle_key(btv);
1da177e4
LT
88 timeout = jiffies + (ir->polling * HZ / 1000);
89 mod_timer(&ir->timer, timeout);
90}
91
6c6c0b2c
MW
92/* ---------------------------------------------------------------*/
93
94static int rc5_remote_gap = 885;
95module_param(rc5_remote_gap, int, 0644);
96static int rc5_key_timeout = 200;
97module_param(rc5_key_timeout, int, 0644);
98
99#define RC5_START(x) (((x)>>12)&3)
100#define RC5_TOGGLE(x) (((x)>>11)&1)
101#define RC5_ADDR(x) (((x)>>6)&31)
102#define RC5_INSTR(x) ((x)&63)
103
104/* decode raw bit pattern to RC5 code */
105static u32 rc5_decode(unsigned int code)
106{
107 unsigned int org_code = code;
108 unsigned int pair;
109 unsigned int rc5 = 0;
110 int i;
111
112 code = (code << 1) | 1;
113 for (i = 0; i < 14; ++i) {
114 pair = code & 0x3;
115 code >>= 2;
116
117 rc5 <<= 1;
118 switch (pair) {
119 case 0:
120 case 2:
121 break;
122 case 1:
123 rc5 |= 1;
124 break;
125 case 3:
4abdfed5 126 dprintk(KERN_WARNING "bad code: %x\n", org_code);
6c6c0b2c
MW
127 return 0;
128 }
129 }
4abdfed5 130 dprintk(KERN_WARNING "code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
6c6c0b2c
MW
131 "instr=%x\n", rc5, org_code, RC5_START(rc5),
132 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
133 return rc5;
134}
135
4abdfed5 136static int bttv_rc5_irq(struct bttv *btv)
6c6c0b2c 137{
4abdfed5 138 struct bttv_ir *ir = btv->remote;
6c6c0b2c
MW
139 struct timeval tv;
140 u32 gpio;
141 u32 gap;
142 unsigned long current_jiffies, timeout;
143
144 /* read gpio port */
4abdfed5 145 gpio = bttv_gpio_read(&btv->c);
6c6c0b2c
MW
146
147 /* remote IRQ? */
148 if (!(gpio & 0x20))
149 return 0;
150
151 /* get time of bit */
152 current_jiffies = jiffies;
153 do_gettimeofday(&tv);
154
155 /* avoid overflow with gap >1s */
156 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
157 gap = 200000;
158 } else {
159 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
160 tv.tv_usec - ir->base_time.tv_usec;
161 }
162
163 /* active code => add bit */
164 if (ir->active) {
165 /* only if in the code (otherwise spurious IRQ or timer
166 late) */
167 if (ir->last_bit < 28) {
168 ir->last_bit = (gap - rc5_remote_gap / 2) /
169 rc5_remote_gap;
170 ir->code |= 1 << ir->last_bit;
171 }
172 /* starting new code */
173 } else {
174 ir->active = 1;
175 ir->code = 0;
176 ir->base_time = tv;
177 ir->last_bit = 0;
178
179 timeout = current_jiffies + (500 + 30 * HZ) / 1000;
180 mod_timer(&ir->timer_end, timeout);
181 }
182
183 /* toggle GPIO pin 4 to reset the irq */
4abdfed5
RC
184 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
185 bttv_gpio_write(&btv->c, gpio | (1 << 4));
6c6c0b2c
MW
186 return 1;
187}
188
4abdfed5
RC
189
190static void bttv_rc5_timer_end(unsigned long data)
6c6c0b2c 191{
4abdfed5 192 struct bttv_ir *ir = (struct bttv_ir *)data;
6c6c0b2c
MW
193 struct timeval tv;
194 unsigned long current_jiffies, timeout;
195 u32 gap;
196
197 /* get time */
198 current_jiffies = jiffies;
199 do_gettimeofday(&tv);
200
201 /* avoid overflow with gap >1s */
202 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
203 gap = 200000;
204 } else {
205 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
206 tv.tv_usec - ir->base_time.tv_usec;
207 }
208
209 /* Allow some timmer jitter (RC5 is ~24ms anyway so this is ok) */
210 if (gap < 28000) {
4abdfed5 211 dprintk(KERN_WARNING "spurious timer_end\n");
6c6c0b2c
MW
212 return;
213 }
214
215 ir->active = 0;
216 if (ir->last_bit < 20) {
217 /* ignore spurious codes (caused by light/other remotes) */
4abdfed5 218 dprintk(KERN_WARNING "short code: %x\n", ir->code);
6c6c0b2c
MW
219 } else {
220 u32 rc5 = rc5_decode(ir->code);
221
222 /* two start bits? */
223 if (RC5_START(rc5) != 3) {
4abdfed5 224 dprintk(KERN_WARNING "rc5 start bits invalid: %u\n", RC5_START(rc5));
6c6c0b2c
MW
225
226 /* right address? */
227 } else if (RC5_ADDR(rc5) == 0x0) {
228 u32 toggle = RC5_TOGGLE(rc5);
229 u32 instr = RC5_INSTR(rc5);
230
231 /* Good code, decide if repeat/repress */
232 if (toggle != RC5_TOGGLE(ir->last_rc5) ||
233 instr != RC5_INSTR(ir->last_rc5)) {
4abdfed5 234 dprintk(KERN_WARNING "instruction %x, toggle %x\n", instr,
6c6c0b2c 235 toggle);
4abdfed5
RC
236 ir_input_nokey(ir->dev, &ir->ir);
237 ir_input_keydown(ir->dev, &ir->ir, instr,
6c6c0b2c
MW
238 instr);
239 }
240
241 /* Set/reset key-up timer */
242 timeout = current_jiffies + (500 + rc5_key_timeout
243 * HZ) / 1000;
244 mod_timer(&ir->timer_keyup, timeout);
245
246 /* Save code for repeat test */
247 ir->last_rc5 = rc5;
248 }
249 }
250}
251
4abdfed5 252static void bttv_rc5_timer_keyup(unsigned long data)
6c6c0b2c 253{
4abdfed5 254 struct bttv_ir *ir = (struct bttv_ir *)data;
6c6c0b2c 255
4abdfed5
RC
256 dprintk(KERN_DEBUG "key released\n");
257 ir_input_nokey(ir->dev, &ir->ir);
6c6c0b2c
MW
258}
259
1da177e4
LT
260/* ---------------------------------------------------------------------- */
261
4abdfed5 262int bttv_input_init(struct bttv *btv)
1da177e4 263{
4abdfed5 264 struct bttv_ir *ir;
1da177e4 265 IR_KEYTAB_TYPE *ir_codes = NULL;
4abdfed5 266 struct input_dev *input_dev;
1da177e4
LT
267 int ir_type = IR_TYPE_OTHER;
268
4abdfed5
RC
269 if (!btv->has_remote)
270 return -ENODEV;
271
272 ir = kzalloc(sizeof(*ir),GFP_KERNEL);
b7df3910
DT
273 input_dev = input_allocate_device();
274 if (!ir || !input_dev) {
275 kfree(ir);
276 input_free_device(input_dev);
1da177e4 277 return -ENOMEM;
b7df3910 278 }
4abdfed5 279 memset(ir,0,sizeof(*ir));
1da177e4
LT
280
281 /* detect & configure */
4abdfed5 282 switch (btv->c.type) {
5a25e84b
MCC
283 case BTTV_BOARD_AVERMEDIA:
284 case BTTV_BOARD_AVPHONE98:
285 case BTTV_BOARD_AVERMEDIA98:
1da177e4
LT
286 ir_codes = ir_codes_avermedia;
287 ir->mask_keycode = 0xf88000;
288 ir->mask_keydown = 0x010000;
289 ir->polling = 50; // ms
290 break;
291
5a25e84b
MCC
292 case BTTV_BOARD_AVDVBT_761:
293 case BTTV_BOARD_AVDVBT_771:
1da177e4
LT
294 ir_codes = ir_codes_avermedia_dvbt;
295 ir->mask_keycode = 0x0f00c0;
296 ir->mask_keydown = 0x000020;
297 ir->polling = 50; // ms
298 break;
299
5a25e84b 300 case BTTV_BOARD_PXELVWPLTVPAK:
1da177e4
LT
301 ir_codes = ir_codes_pixelview;
302 ir->mask_keycode = 0x003e00;
303 ir->mask_keyup = 0x010000;
304 ir->polling = 50; // ms
4ac97914 305 break;
5a25e84b
MCC
306 case BTTV_BOARD_PV_BT878P_9B:
307 case BTTV_BOARD_PV_BT878P_PLUS:
1da177e4
LT
308 ir_codes = ir_codes_pixelview;
309 ir->mask_keycode = 0x001f00;
310 ir->mask_keyup = 0x008000;
311 ir->polling = 50; // ms
4ac97914 312 break;
1da177e4 313
5a25e84b 314 case BTTV_BOARD_WINFAST2000:
1da177e4
LT
315 ir_codes = ir_codes_winfast;
316 ir->mask_keycode = 0x1f8;
317 break;
5a25e84b
MCC
318 case BTTV_BOARD_MAGICTVIEW061:
319 case BTTV_BOARD_MAGICTVIEW063:
1da177e4
LT
320 ir_codes = ir_codes_winfast;
321 ir->mask_keycode = 0x0008e000;
322 ir->mask_keydown = 0x00200000;
323 break;
5a25e84b 324 case BTTV_BOARD_APAC_VIEWCOMP:
1da177e4
LT
325 ir_codes = ir_codes_apac_viewcomp;
326 ir->mask_keycode = 0x001f00;
327 ir->mask_keyup = 0x008000;
328 ir->polling = 50; // ms
329 break;
5a25e84b 330 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
b639f9d2 331 ir_codes = ir_codes_pixelview;
cc9d8d49
RC
332 ir->mask_keycode = 0x001F00;
333 ir->mask_keyup = 0x006000;
334 ir->polling = 50; // ms
335 break;
6c6c0b2c
MW
336 case BTTV_BOARD_NEBULA_DIGITV:
337 ir_codes = ir_codes_nebula;
4abdfed5 338 btv->custom_irq = bttv_rc5_irq;
6c6c0b2c 339 ir->rc5_gpio = 1;
674434c6 340 break;
2d05ae6b
JC
341 case BTTV_BOARD_MACHTV_MAGICTV:
342 ir_codes = ir_codes_apac_viewcomp;
343 ir->mask_keycode = 0x001F00;
344 ir->mask_keyup = 0x004000;
345 ir->polling = 50; /* ms */
346 break;
1da177e4
LT
347 }
348 if (NULL == ir_codes) {
4abdfed5 349 dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n",btv->c.type);
1da177e4 350 kfree(ir);
b7df3910 351 input_free_device(input_dev);
1da177e4
LT
352 return -ENODEV;
353 }
354
6c6c0b2c
MW
355 if (ir->rc5_gpio) {
356 u32 gpio;
674434c6 357 /* enable remote irq */
4abdfed5
RC
358 bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
359 gpio = bttv_gpio_read(&btv->c);
360 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
361 bttv_gpio_write(&btv->c, gpio | (1 << 4));
6c6c0b2c
MW
362 } else {
363 /* init hardware-specific stuff */
4abdfed5 364 bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
6c6c0b2c 365 }
1da177e4
LT
366
367 /* init input device */
4abdfed5
RC
368 ir->dev = input_dev;
369
1da177e4 370 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
4abdfed5 371 btv->c.type);
1da177e4 372 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
4abdfed5 373 pci_name(btv->c.pci));
1da177e4 374
b7df3910
DT
375 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
376 input_dev->name = ir->name;
377 input_dev->phys = ir->phys;
378 input_dev->id.bustype = BUS_PCI;
379 input_dev->id.version = 1;
4abdfed5
RC
380 if (btv->c.pci->subsystem_vendor) {
381 input_dev->id.vendor = btv->c.pci->subsystem_vendor;
382 input_dev->id.product = btv->c.pci->subsystem_device;
1da177e4 383 } else {
4abdfed5
RC
384 input_dev->id.vendor = btv->c.pci->vendor;
385 input_dev->id.product = btv->c.pci->device;
1da177e4 386 }
4abdfed5 387 input_dev->cdev.dev = &btv->c.pci->dev;
1da177e4 388
4abdfed5 389 btv->remote = ir;
1da177e4 390 if (ir->polling) {
1da177e4 391 init_timer(&ir->timer);
4abdfed5
RC
392 ir->timer.function = bttv_input_timer;
393 ir->timer.data = (unsigned long)btv;
394 ir->timer.expires = jiffies + HZ;
395 add_timer(&ir->timer);
6c6c0b2c
MW
396 } else if (ir->rc5_gpio) {
397 /* set timer_end for code completion */
398 init_timer(&ir->timer_end);
4abdfed5 399 ir->timer_end.function = bttv_rc5_timer_end;
6c6c0b2c
MW
400 ir->timer_end.data = (unsigned long)ir;
401
402 init_timer(&ir->timer_keyup);
4abdfed5 403 ir->timer_keyup.function = bttv_rc5_timer_keyup;
6c6c0b2c 404 ir->timer_keyup.data = (unsigned long)ir;
1da177e4
LT
405 }
406
407 /* all done */
4abdfed5 408 input_register_device(btv->remote->dev);
7e578191 409 printk(DEVNAME ": %s detected at %s\n",ir->name,ir->phys);
6c6c0b2c
MW
410
411 /* the remote isn't as bouncy as a keyboard */
4abdfed5
RC
412 ir->dev->rep[REP_DELAY] = repeat_delay;
413 ir->dev->rep[REP_PERIOD] = repeat_period;
1da177e4
LT
414
415 return 0;
416}
417
4abdfed5 418void bttv_input_fini(struct bttv *btv)
1da177e4 419{
4abdfed5
RC
420 if (btv->remote == NULL)
421 return;
1da177e4 422
4abdfed5
RC
423 if (btv->remote->polling) {
424 del_timer_sync(&btv->remote->timer);
1da177e4
LT
425 flush_scheduled_work();
426 }
674434c6 427
4abdfed5
RC
428
429 if (btv->remote->rc5_gpio) {
6c6c0b2c
MW
430 u32 gpio;
431
bc61b010 432 del_timer_sync(&btv->remote->timer_end);
6c6c0b2c
MW
433 flush_scheduled_work();
434
4abdfed5
RC
435 gpio = bttv_gpio_read(&btv->c);
436 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
6c6c0b2c 437 }
1da177e4 438
4abdfed5
RC
439 input_unregister_device(btv->remote->dev);
440 kfree(btv->remote);
441 btv->remote = NULL;
1da177e4
LT
442}
443
1da177e4
LT
444
445/*
446 * Local variables:
447 * c-basic-offset: 8
448 * End:
449 */