]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/media/video/bt8xx/bttv-input.c
V4L/DVB: usbvision: remove BKL from usbvision
[mirror_ubuntu-zesty-kernel.git] / drivers / media / video / bt8xx / 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>
1da177e4
LT
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/interrupt.h>
25#include <linux/input.h>
5a0e3ad6 26#include <linux/slab.h>
1da177e4 27
1da177e4 28#include "bttv.h"
4abdfed5 29#include "bttvp.h"
1da177e4 30
6c6c0b2c 31
7d341a6a
MCC
32static int ir_debug;
33module_param(ir_debug, int, 0644);
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
c408a6f6 39static int ir_rc5_remote_gap = 885;
9160723e 40module_param(ir_rc5_remote_gap, int, 0644);
c408a6f6 41static int ir_rc5_key_timeout = 200;
9160723e
HP
42module_param(ir_rc5_key_timeout, int, 0644);
43
7d341a6a
MCC
44#undef dprintk
45#define dprintk(arg...) do { \
46 if (ir_debug >= 1) \
47 printk(arg); \
48} while (0)
49
4abdfed5 50#define DEVNAME "bttv-input"
1da177e4 51
727e625c
MCC
52#define MODULE_NAME "bttv"
53
1da177e4
LT
54/* ---------------------------------------------------------------------- */
55
4abdfed5 56static void ir_handle_key(struct bttv *btv)
1da177e4 57{
9160723e 58 struct card_ir *ir = btv->remote;
1da177e4
LT
59 u32 gpio,data;
60
61 /* read gpio value */
4abdfed5 62 gpio = bttv_gpio_read(&btv->c);
1da177e4
LT
63 if (ir->polling) {
64 if (ir->last_gpio == gpio)
65 return;
66 ir->last_gpio = gpio;
67 }
68
69 /* extract data */
70 data = ir_extract_bits(gpio, ir->mask_keycode);
4abdfed5 71 dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
1da177e4
LT
72 gpio, data,
73 ir->polling ? "poll" : "irq",
74 (gpio & ir->mask_keydown) ? " down" : "",
75 (gpio & ir->mask_keyup) ? " up" : "");
76
4abdfed5
RC
77 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
78 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
8573b74a 79 ir_input_keydown(ir->dev, &ir->ir, data);
1da177e4 80 } else {
b3d98135
MCC
81 /* HACK: Probably, ir->mask_keydown is missing
82 for this board */
83 if (btv->c.type == BTTV_BOARD_WINFAST2000)
8573b74a 84 ir_input_keydown(ir->dev, &ir->ir, data);
b3d98135 85
4abdfed5 86 ir_input_nokey(ir->dev,&ir->ir);
1da177e4 87 }
1da177e4 88
1da177e4
LT
89}
90
7d341a6a
MCC
91static void ir_enltv_handle_key(struct bttv *btv)
92{
93 struct card_ir *ir = btv->remote;
94 u32 gpio, data, keyup;
95
96 /* read gpio value */
97 gpio = bttv_gpio_read(&btv->c);
98
99 /* extract data */
100 data = ir_extract_bits(gpio, ir->mask_keycode);
101
102 /* Check if it is keyup */
103 keyup = (gpio & ir->mask_keyup) ? 1 << 31 : 0;
104
105 if ((ir->last_gpio & 0x7f) != data) {
106 dprintk(KERN_INFO DEVNAME ": gpio=0x%x code=%d | %s\n",
107 gpio, data,
108 (gpio & ir->mask_keyup) ? " up" : "up/down");
109
8573b74a 110 ir_input_keydown(ir->dev, &ir->ir, data);
7d341a6a
MCC
111 if (keyup)
112 ir_input_nokey(ir->dev, &ir->ir);
113 } else {
114 if ((ir->last_gpio & 1 << 31) == keyup)
115 return;
116
117 dprintk(KERN_INFO DEVNAME ":(cnt) gpio=0x%x code=%d | %s\n",
118 gpio, data,
119 (gpio & ir->mask_keyup) ? " up" : "down");
120
121 if (keyup)
122 ir_input_nokey(ir->dev, &ir->ir);
123 else
8573b74a 124 ir_input_keydown(ir->dev, &ir->ir, data);
7d341a6a
MCC
125 }
126
127 ir->last_gpio = data | keyup;
128}
129
4abdfed5 130void bttv_input_irq(struct bttv *btv)
1da177e4 131{
9160723e 132 struct card_ir *ir = btv->remote;
1da177e4 133
4abdfed5
RC
134 if (!ir->polling)
135 ir_handle_key(btv);
1da177e4
LT
136}
137
4abdfed5 138static void bttv_input_timer(unsigned long data)
1da177e4 139{
4abdfed5 140 struct bttv *btv = (struct bttv*)data;
9160723e 141 struct card_ir *ir = btv->remote;
1da177e4 142
7d341a6a
MCC
143 if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
144 ir_enltv_handle_key(btv);
145 else
146 ir_handle_key(btv);
c1904956 147 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
1da177e4
LT
148}
149
6c6c0b2c
MW
150/* ---------------------------------------------------------------*/
151
4abdfed5 152static int bttv_rc5_irq(struct bttv *btv)
6c6c0b2c 153{
9160723e 154 struct card_ir *ir = btv->remote;
6c6c0b2c
MW
155 struct timeval tv;
156 u32 gpio;
157 u32 gap;
c1904956 158 unsigned long current_jiffies;
6c6c0b2c
MW
159
160 /* read gpio port */
4abdfed5 161 gpio = bttv_gpio_read(&btv->c);
6c6c0b2c
MW
162
163 /* remote IRQ? */
164 if (!(gpio & 0x20))
165 return 0;
166
167 /* get time of bit */
168 current_jiffies = jiffies;
169 do_gettimeofday(&tv);
170
171 /* avoid overflow with gap >1s */
172 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
173 gap = 200000;
174 } else {
175 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
176 tv.tv_usec - ir->base_time.tv_usec;
177 }
178
179 /* active code => add bit */
180 if (ir->active) {
181 /* only if in the code (otherwise spurious IRQ or timer
182 late) */
183 if (ir->last_bit < 28) {
9160723e
HP
184 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
185 ir_rc5_remote_gap;
6c6c0b2c
MW
186 ir->code |= 1 << ir->last_bit;
187 }
188 /* starting new code */
189 } else {
190 ir->active = 1;
191 ir->code = 0;
192 ir->base_time = tv;
193 ir->last_bit = 0;
194
c1904956
DT
195 mod_timer(&ir->timer_end,
196 current_jiffies + msecs_to_jiffies(30));
6c6c0b2c
MW
197 }
198
199 /* toggle GPIO pin 4 to reset the irq */
4abdfed5
RC
200 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
201 bttv_gpio_write(&btv->c, gpio | (1 << 4));
6c6c0b2c
MW
202 return 1;
203}
204
1da177e4
LT
205/* ---------------------------------------------------------------------- */
206
9160723e 207static void bttv_ir_start(struct bttv *btv, struct card_ir *ir)
b07b4783
DT
208{
209 if (ir->polling) {
c1904956 210 setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
fe06fe0a 211 ir->timer.expires = jiffies + msecs_to_jiffies(1000);
b07b4783
DT
212 add_timer(&ir->timer);
213 } else if (ir->rc5_gpio) {
214 /* set timer_end for code completion */
215 init_timer(&ir->timer_end);
9160723e 216 ir->timer_end.function = ir_rc5_timer_end;
b07b4783
DT
217 ir->timer_end.data = (unsigned long)ir;
218
219 init_timer(&ir->timer_keyup);
9160723e 220 ir->timer_keyup.function = ir_rc5_timer_keyup;
b07b4783 221 ir->timer_keyup.data = (unsigned long)ir;
9160723e
HP
222 ir->shift_by = 1;
223 ir->start = 3;
224 ir->addr = 0x0;
225 ir->rc5_key_timeout = ir_rc5_key_timeout;
226 ir->rc5_remote_gap = ir_rc5_remote_gap;
b07b4783
DT
227 }
228}
229
230static void bttv_ir_stop(struct bttv *btv)
231{
232 if (btv->remote->polling) {
233 del_timer_sync(&btv->remote->timer);
234 flush_scheduled_work();
235 }
236
237 if (btv->remote->rc5_gpio) {
238 u32 gpio;
239
240 del_timer_sync(&btv->remote->timer_end);
241 flush_scheduled_work();
242
243 gpio = bttv_gpio_read(&btv->c);
244 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
245 }
246}
247
4abdfed5 248int bttv_input_init(struct bttv *btv)
1da177e4 249{
9160723e 250 struct card_ir *ir;
02858eed 251 char *ir_codes = NULL;
4abdfed5 252 struct input_dev *input_dev;
971e8298 253 u64 ir_type = IR_TYPE_OTHER;
b07b4783 254 int err = -ENOMEM;
1da177e4 255
4abdfed5
RC
256 if (!btv->has_remote)
257 return -ENODEV;
258
259 ir = kzalloc(sizeof(*ir),GFP_KERNEL);
b7df3910 260 input_dev = input_allocate_device();
b07b4783
DT
261 if (!ir || !input_dev)
262 goto err_out_free;
1da177e4
LT
263
264 /* detect & configure */
4abdfed5 265 switch (btv->c.type) {
5a25e84b
MCC
266 case BTTV_BOARD_AVERMEDIA:
267 case BTTV_BOARD_AVPHONE98:
268 case BTTV_BOARD_AVERMEDIA98:
02858eed 269 ir_codes = RC_MAP_AVERMEDIA;
1da177e4
LT
270 ir->mask_keycode = 0xf88000;
271 ir->mask_keydown = 0x010000;
272 ir->polling = 50; // ms
273 break;
274
5a25e84b
MCC
275 case BTTV_BOARD_AVDVBT_761:
276 case BTTV_BOARD_AVDVBT_771:
02858eed 277 ir_codes = RC_MAP_AVERMEDIA_DVBT;
1da177e4
LT
278 ir->mask_keycode = 0x0f00c0;
279 ir->mask_keydown = 0x000020;
280 ir->polling = 50; // ms
281 break;
282
5a25e84b 283 case BTTV_BOARD_PXELVWPLTVPAK:
02858eed 284 ir_codes = RC_MAP_PIXELVIEW;
1da177e4
LT
285 ir->mask_keycode = 0x003e00;
286 ir->mask_keyup = 0x010000;
287 ir->polling = 50; // ms
4ac97914 288 break;
c663155c 289 case BTTV_BOARD_PV_M4900:
5a25e84b
MCC
290 case BTTV_BOARD_PV_BT878P_9B:
291 case BTTV_BOARD_PV_BT878P_PLUS:
02858eed 292 ir_codes = RC_MAP_PIXELVIEW;
1da177e4
LT
293 ir->mask_keycode = 0x001f00;
294 ir->mask_keyup = 0x008000;
295 ir->polling = 50; // ms
4ac97914 296 break;
1da177e4 297
5a25e84b 298 case BTTV_BOARD_WINFAST2000:
02858eed 299 ir_codes = RC_MAP_WINFAST;
1da177e4
LT
300 ir->mask_keycode = 0x1f8;
301 break;
5a25e84b
MCC
302 case BTTV_BOARD_MAGICTVIEW061:
303 case BTTV_BOARD_MAGICTVIEW063:
02858eed 304 ir_codes = RC_MAP_WINFAST;
1da177e4
LT
305 ir->mask_keycode = 0x0008e000;
306 ir->mask_keydown = 0x00200000;
307 break;
5a25e84b 308 case BTTV_BOARD_APAC_VIEWCOMP:
02858eed 309 ir_codes = RC_MAP_APAC_VIEWCOMP;
1da177e4
LT
310 ir->mask_keycode = 0x001f00;
311 ir->mask_keyup = 0x008000;
312 ir->polling = 50; // ms
313 break;
ed44f66e 314 case BTTV_BOARD_ASKEY_CPH03X:
5a25e84b 315 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
4ab2b99b 316 case BTTV_BOARD_CONTVFMI:
02858eed 317 ir_codes = RC_MAP_PIXELVIEW;
cc9d8d49
RC
318 ir->mask_keycode = 0x001F00;
319 ir->mask_keyup = 0x006000;
320 ir->polling = 50; // ms
321 break;
6c6c0b2c 322 case BTTV_BOARD_NEBULA_DIGITV:
02858eed 323 ir_codes = RC_MAP_NEBULA;
4abdfed5 324 btv->custom_irq = bttv_rc5_irq;
6c6c0b2c 325 ir->rc5_gpio = 1;
674434c6 326 break;
2d05ae6b 327 case BTTV_BOARD_MACHTV_MAGICTV:
02858eed 328 ir_codes = RC_MAP_APAC_VIEWCOMP;
2d05ae6b
JC
329 ir->mask_keycode = 0x001F00;
330 ir->mask_keyup = 0x004000;
331 ir->polling = 50; /* ms */
332 break;
e80faad3 333 case BTTV_BOARD_KOZUMI_KTV_01C:
02858eed 334 ir_codes = RC_MAP_PCTV_SEDNA;
e80faad3
ML
335 ir->mask_keycode = 0x001f00;
336 ir->mask_keyup = 0x006000;
337 ir->polling = 50; /* ms */
338 break;
7d341a6a 339 case BTTV_BOARD_ENLTV_FM_2:
02858eed 340 ir_codes = RC_MAP_ENCORE_ENLTV2;
7d341a6a
MCC
341 ir->mask_keycode = 0x00fd00;
342 ir->mask_keyup = 0x000080;
343 ir->polling = 1; /* ms */
344 ir->last_gpio = ir_extract_bits(bttv_gpio_read(&btv->c),
345 ir->mask_keycode);
346 break;
1da177e4
LT
347 }
348 if (NULL == ir_codes) {
b07b4783
DT
349 dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
350 err = -ENODEV;
351 goto err_out_free;
1da177e4
LT
352 }
353
6c6c0b2c
MW
354 if (ir->rc5_gpio) {
355 u32 gpio;
657de3cd 356 /* enable remote irq */
4abdfed5
RC
357 bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
358 gpio = bttv_gpio_read(&btv->c);
359 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
360 bttv_gpio_write(&btv->c, gpio | (1 << 4));
6c6c0b2c
MW
361 } else {
362 /* init hardware-specific stuff */
4abdfed5 363 bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
6c6c0b2c 364 }
1da177e4
LT
365
366 /* init input device */
4abdfed5
RC
367 ir->dev = input_dev;
368
1da177e4 369 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
4abdfed5 370 btv->c.type);
1da177e4 371 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
4abdfed5 372 pci_name(btv->c.pci));
1da177e4 373
579e7d60 374 err = ir_input_init(input_dev, &ir->ir, ir_type);
055cd556
MCC
375 if (err < 0)
376 goto err_out_free;
377
b7df3910
DT
378 input_dev->name = ir->name;
379 input_dev->phys = ir->phys;
380 input_dev->id.bustype = BUS_PCI;
381 input_dev->id.version = 1;
4abdfed5
RC
382 if (btv->c.pci->subsystem_vendor) {
383 input_dev->id.vendor = btv->c.pci->subsystem_vendor;
384 input_dev->id.product = btv->c.pci->subsystem_device;
1da177e4 385 } else {
4abdfed5
RC
386 input_dev->id.vendor = btv->c.pci->vendor;
387 input_dev->id.product = btv->c.pci->device;
1da177e4 388 }
2c8a3a33 389 input_dev->dev.parent = &btv->c.pci->dev;
1da177e4 390
4abdfed5 391 btv->remote = ir;
b07b4783 392 bttv_ir_start(btv, ir);
1da177e4
LT
393
394 /* all done */
02858eed 395 err = ir_input_register(btv->remote->dev, ir_codes, NULL, MODULE_NAME);
b07b4783
DT
396 if (err)
397 goto err_out_stop;
6c6c0b2c
MW
398
399 /* the remote isn't as bouncy as a keyboard */
4abdfed5
RC
400 ir->dev->rep[REP_DELAY] = repeat_delay;
401 ir->dev->rep[REP_PERIOD] = repeat_period;
1da177e4
LT
402
403 return 0;
b07b4783
DT
404
405 err_out_stop:
406 bttv_ir_stop(btv);
407 btv->remote = NULL;
408 err_out_free:
b07b4783
DT
409 kfree(ir);
410 return err;
1da177e4
LT
411}
412
4abdfed5 413void bttv_input_fini(struct bttv *btv)
1da177e4 414{
4abdfed5
RC
415 if (btv->remote == NULL)
416 return;
1da177e4 417
b07b4783 418 bttv_ir_stop(btv);
38ef6aa8 419 ir_input_unregister(btv->remote->dev);
4abdfed5
RC
420 kfree(btv->remote);
421 btv->remote = NULL;
1da177e4
LT
422}
423
1da177e4
LT
424
425/*
426 * Local variables:
427 * c-basic-offset: 8
428 * End:
429 */