]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/media/video/bt8xx/bttv-input.c
[media] ir-core: remove remaining users of the ir-functions keyhandlers
[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);
9160723e 41
7d341a6a
MCC
42#undef dprintk
43#define dprintk(arg...) do { \
44 if (ir_debug >= 1) \
45 printk(arg); \
46} while (0)
47
4abdfed5 48#define DEVNAME "bttv-input"
1da177e4 49
727e625c
MCC
50#define MODULE_NAME "bttv"
51
1da177e4
LT
52/* ---------------------------------------------------------------------- */
53
4abdfed5 54static void ir_handle_key(struct bttv *btv)
1da177e4 55{
9160723e 56 struct card_ir *ir = btv->remote;
1da177e4
LT
57 u32 gpio,data;
58
59 /* read gpio value */
4abdfed5 60 gpio = bttv_gpio_read(&btv->c);
1da177e4
LT
61 if (ir->polling) {
62 if (ir->last_gpio == gpio)
63 return;
64 ir->last_gpio = gpio;
65 }
66
67 /* extract data */
68 data = ir_extract_bits(gpio, ir->mask_keycode);
4abdfed5 69 dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
1da177e4
LT
70 gpio, data,
71 ir->polling ? "poll" : "irq",
72 (gpio & ir->mask_keydown) ? " down" : "",
73 (gpio & ir->mask_keyup) ? " up" : "");
74
62c65031
DH
75 if ((ir->mask_keydown && (gpio & ir->mask_keydown)) ||
76 (ir->mask_keyup && !(gpio & ir->mask_keyup))) {
77 ir_keydown_notimeout(ir->dev, data, 0);
1da177e4 78 } else {
b3d98135
MCC
79 /* HACK: Probably, ir->mask_keydown is missing
80 for this board */
81 if (btv->c.type == BTTV_BOARD_WINFAST2000)
62c65031 82 ir_keydown_notimeout(ir->dev, data, 0);
b3d98135 83
62c65031 84 ir_keyup(ir->dev);
1da177e4 85 }
1da177e4
LT
86}
87
7d341a6a
MCC
88static void ir_enltv_handle_key(struct bttv *btv)
89{
90 struct card_ir *ir = btv->remote;
91 u32 gpio, data, keyup;
92
93 /* read gpio value */
94 gpio = bttv_gpio_read(&btv->c);
95
96 /* extract data */
97 data = ir_extract_bits(gpio, ir->mask_keycode);
98
99 /* Check if it is keyup */
100 keyup = (gpio & ir->mask_keyup) ? 1 << 31 : 0;
101
102 if ((ir->last_gpio & 0x7f) != data) {
103 dprintk(KERN_INFO DEVNAME ": gpio=0x%x code=%d | %s\n",
104 gpio, data,
105 (gpio & ir->mask_keyup) ? " up" : "up/down");
106
62c65031 107 ir_keydown_notimeout(ir->dev, data, 0);
7d341a6a 108 if (keyup)
62c65031 109 ir_keyup(ir->dev);
7d341a6a
MCC
110 } else {
111 if ((ir->last_gpio & 1 << 31) == keyup)
112 return;
113
114 dprintk(KERN_INFO DEVNAME ":(cnt) gpio=0x%x code=%d | %s\n",
115 gpio, data,
116 (gpio & ir->mask_keyup) ? " up" : "down");
117
118 if (keyup)
62c65031 119 ir_keyup(ir->dev);
7d341a6a 120 else
62c65031 121 ir_keydown_notimeout(ir->dev, data, 0);
7d341a6a
MCC
122 }
123
124 ir->last_gpio = data | keyup;
125}
126
4abdfed5 127void bttv_input_irq(struct bttv *btv)
1da177e4 128{
9160723e 129 struct card_ir *ir = btv->remote;
1da177e4 130
4abdfed5
RC
131 if (!ir->polling)
132 ir_handle_key(btv);
1da177e4
LT
133}
134
4abdfed5 135static void bttv_input_timer(unsigned long data)
1da177e4 136{
4abdfed5 137 struct bttv *btv = (struct bttv*)data;
9160723e 138 struct card_ir *ir = btv->remote;
1da177e4 139
7d341a6a
MCC
140 if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
141 ir_enltv_handle_key(btv);
142 else
143 ir_handle_key(btv);
c1904956 144 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
1da177e4
LT
145}
146
6c6c0b2c
MW
147/* ---------------------------------------------------------------*/
148
4abdfed5 149static int bttv_rc5_irq(struct bttv *btv)
6c6c0b2c 150{
9160723e 151 struct card_ir *ir = btv->remote;
6c6c0b2c
MW
152 struct timeval tv;
153 u32 gpio;
154 u32 gap;
c1904956 155 unsigned long current_jiffies;
6c6c0b2c
MW
156
157 /* read gpio port */
4abdfed5 158 gpio = bttv_gpio_read(&btv->c);
6c6c0b2c
MW
159
160 /* remote IRQ? */
161 if (!(gpio & 0x20))
162 return 0;
163
164 /* get time of bit */
165 current_jiffies = jiffies;
166 do_gettimeofday(&tv);
167
168 /* avoid overflow with gap >1s */
169 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
170 gap = 200000;
171 } else {
172 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
173 tv.tv_usec - ir->base_time.tv_usec;
174 }
175
176 /* active code => add bit */
177 if (ir->active) {
178 /* only if in the code (otherwise spurious IRQ or timer
179 late) */
180 if (ir->last_bit < 28) {
9160723e
HP
181 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
182 ir_rc5_remote_gap;
6c6c0b2c
MW
183 ir->code |= 1 << ir->last_bit;
184 }
185 /* starting new code */
186 } else {
187 ir->active = 1;
188 ir->code = 0;
189 ir->base_time = tv;
190 ir->last_bit = 0;
191
c1904956
DT
192 mod_timer(&ir->timer_end,
193 current_jiffies + msecs_to_jiffies(30));
6c6c0b2c
MW
194 }
195
196 /* toggle GPIO pin 4 to reset the irq */
4abdfed5
RC
197 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
198 bttv_gpio_write(&btv->c, gpio | (1 << 4));
6c6c0b2c
MW
199 return 1;
200}
201
1da177e4
LT
202/* ---------------------------------------------------------------------- */
203
9160723e 204static void bttv_ir_start(struct bttv *btv, struct card_ir *ir)
b07b4783
DT
205{
206 if (ir->polling) {
c1904956 207 setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
fe06fe0a 208 ir->timer.expires = jiffies + msecs_to_jiffies(1000);
b07b4783
DT
209 add_timer(&ir->timer);
210 } else if (ir->rc5_gpio) {
211 /* set timer_end for code completion */
212 init_timer(&ir->timer_end);
9160723e 213 ir->timer_end.function = ir_rc5_timer_end;
b07b4783 214 ir->timer_end.data = (unsigned long)ir;
9160723e
HP
215 ir->shift_by = 1;
216 ir->start = 3;
217 ir->addr = 0x0;
9160723e 218 ir->rc5_remote_gap = ir_rc5_remote_gap;
b07b4783
DT
219 }
220}
221
222static void bttv_ir_stop(struct bttv *btv)
223{
224 if (btv->remote->polling) {
225 del_timer_sync(&btv->remote->timer);
226 flush_scheduled_work();
227 }
228
229 if (btv->remote->rc5_gpio) {
230 u32 gpio;
231
232 del_timer_sync(&btv->remote->timer_end);
233 flush_scheduled_work();
234
235 gpio = bttv_gpio_read(&btv->c);
236 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
237 }
238}
239
c0c46826
MCC
240/*
241 * Get_key functions used by I2C remotes
242 */
243
244static int get_key_pv951(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
245{
246 unsigned char b;
247
248 /* poll IR chip */
249 if (1 != i2c_master_recv(ir->c, &b, 1)) {
250 dprintk(KERN_INFO DEVNAME ": read error\n");
251 return -EIO;
252 }
253
254 /* ignore 0xaa */
255 if (b==0xaa)
256 return 0;
257 dprintk(KERN_INFO DEVNAME ": key %02x\n", b);
258
259 *ir_key = b;
260 *ir_raw = b;
261 return 1;
262}
263
264/* Instantiate the I2C IR receiver device, if present */
265void __devinit init_bttv_i2c_ir(struct bttv *btv)
266{
267 const unsigned short addr_list[] = {
268 0x1a, 0x18, 0x64, 0x30, 0x71,
269 I2C_CLIENT_END
270 };
271 struct i2c_board_info info;
272
273 if (0 != btv->i2c_rc)
274 return;
275
276 memset(&info, 0, sizeof(struct i2c_board_info));
277 memset(&btv->init_data, 0, sizeof(btv->init_data));
278 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
279
280 switch (btv->c.type) {
281 case BTTV_BOARD_PV951:
282 btv->init_data.name = "PV951";
283 btv->init_data.get_key = get_key_pv951;
284 btv->init_data.ir_codes = RC_MAP_PV951;
c0c46826
MCC
285 info.addr = 0x4b;
286 break;
287 default:
288 /*
289 * The external IR receiver is at i2c address 0x34 (0x35 for
290 * reads). Future Hauppauge cards will have an internal
291 * receiver at 0x30 (0x31 for reads). In theory, both can be
292 * fitted, and Hauppauge suggest an external overrides an
293 * internal.
294 * That's why we probe 0x1a (~0x34) first. CB
295 */
296
297 i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list, NULL);
298 return;
299 }
300
301 if (btv->init_data.name)
302 info.platform_data = &btv->init_data;
303 i2c_new_device(&btv->c.i2c_adap, &info);
304
305 return;
306}
307
308int __devexit fini_bttv_i2c(struct bttv *btv)
309{
310 if (0 != btv->i2c_rc)
311 return 0;
312
313 return i2c_del_adapter(&btv->c.i2c_adap);
314}
315
4abdfed5 316int bttv_input_init(struct bttv *btv)
1da177e4 317{
9160723e 318 struct card_ir *ir;
02858eed 319 char *ir_codes = NULL;
4abdfed5 320 struct input_dev *input_dev;
b07b4783 321 int err = -ENOMEM;
1da177e4 322
4abdfed5
RC
323 if (!btv->has_remote)
324 return -ENODEV;
325
326 ir = kzalloc(sizeof(*ir),GFP_KERNEL);
b7df3910 327 input_dev = input_allocate_device();
b07b4783
DT
328 if (!ir || !input_dev)
329 goto err_out_free;
1da177e4
LT
330
331 /* detect & configure */
4abdfed5 332 switch (btv->c.type) {
5a25e84b
MCC
333 case BTTV_BOARD_AVERMEDIA:
334 case BTTV_BOARD_AVPHONE98:
335 case BTTV_BOARD_AVERMEDIA98:
02858eed 336 ir_codes = RC_MAP_AVERMEDIA;
1da177e4
LT
337 ir->mask_keycode = 0xf88000;
338 ir->mask_keydown = 0x010000;
339 ir->polling = 50; // ms
340 break;
341
5a25e84b
MCC
342 case BTTV_BOARD_AVDVBT_761:
343 case BTTV_BOARD_AVDVBT_771:
02858eed 344 ir_codes = RC_MAP_AVERMEDIA_DVBT;
1da177e4
LT
345 ir->mask_keycode = 0x0f00c0;
346 ir->mask_keydown = 0x000020;
347 ir->polling = 50; // ms
348 break;
349
5a25e84b 350 case BTTV_BOARD_PXELVWPLTVPAK:
02858eed 351 ir_codes = RC_MAP_PIXELVIEW;
1da177e4
LT
352 ir->mask_keycode = 0x003e00;
353 ir->mask_keyup = 0x010000;
354 ir->polling = 50; // ms
4ac97914 355 break;
c663155c 356 case BTTV_BOARD_PV_M4900:
5a25e84b
MCC
357 case BTTV_BOARD_PV_BT878P_9B:
358 case BTTV_BOARD_PV_BT878P_PLUS:
02858eed 359 ir_codes = RC_MAP_PIXELVIEW;
1da177e4
LT
360 ir->mask_keycode = 0x001f00;
361 ir->mask_keyup = 0x008000;
362 ir->polling = 50; // ms
4ac97914 363 break;
1da177e4 364
5a25e84b 365 case BTTV_BOARD_WINFAST2000:
02858eed 366 ir_codes = RC_MAP_WINFAST;
1da177e4
LT
367 ir->mask_keycode = 0x1f8;
368 break;
5a25e84b
MCC
369 case BTTV_BOARD_MAGICTVIEW061:
370 case BTTV_BOARD_MAGICTVIEW063:
02858eed 371 ir_codes = RC_MAP_WINFAST;
1da177e4
LT
372 ir->mask_keycode = 0x0008e000;
373 ir->mask_keydown = 0x00200000;
374 break;
5a25e84b 375 case BTTV_BOARD_APAC_VIEWCOMP:
02858eed 376 ir_codes = RC_MAP_APAC_VIEWCOMP;
1da177e4
LT
377 ir->mask_keycode = 0x001f00;
378 ir->mask_keyup = 0x008000;
379 ir->polling = 50; // ms
380 break;
ed44f66e 381 case BTTV_BOARD_ASKEY_CPH03X:
5a25e84b 382 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
4ab2b99b 383 case BTTV_BOARD_CONTVFMI:
02858eed 384 ir_codes = RC_MAP_PIXELVIEW;
cc9d8d49
RC
385 ir->mask_keycode = 0x001F00;
386 ir->mask_keyup = 0x006000;
387 ir->polling = 50; // ms
388 break;
6c6c0b2c 389 case BTTV_BOARD_NEBULA_DIGITV:
02858eed 390 ir_codes = RC_MAP_NEBULA;
4abdfed5 391 btv->custom_irq = bttv_rc5_irq;
6c6c0b2c 392 ir->rc5_gpio = 1;
674434c6 393 break;
2d05ae6b 394 case BTTV_BOARD_MACHTV_MAGICTV:
02858eed 395 ir_codes = RC_MAP_APAC_VIEWCOMP;
2d05ae6b
JC
396 ir->mask_keycode = 0x001F00;
397 ir->mask_keyup = 0x004000;
398 ir->polling = 50; /* ms */
399 break;
e80faad3 400 case BTTV_BOARD_KOZUMI_KTV_01C:
02858eed 401 ir_codes = RC_MAP_PCTV_SEDNA;
e80faad3
ML
402 ir->mask_keycode = 0x001f00;
403 ir->mask_keyup = 0x006000;
404 ir->polling = 50; /* ms */
405 break;
7d341a6a 406 case BTTV_BOARD_ENLTV_FM_2:
02858eed 407 ir_codes = RC_MAP_ENCORE_ENLTV2;
7d341a6a
MCC
408 ir->mask_keycode = 0x00fd00;
409 ir->mask_keyup = 0x000080;
410 ir->polling = 1; /* ms */
411 ir->last_gpio = ir_extract_bits(bttv_gpio_read(&btv->c),
412 ir->mask_keycode);
413 break;
1da177e4
LT
414 }
415 if (NULL == ir_codes) {
b07b4783
DT
416 dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
417 err = -ENODEV;
418 goto err_out_free;
1da177e4
LT
419 }
420
6c6c0b2c
MW
421 if (ir->rc5_gpio) {
422 u32 gpio;
657de3cd 423 /* enable remote irq */
4abdfed5
RC
424 bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
425 gpio = bttv_gpio_read(&btv->c);
426 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
427 bttv_gpio_write(&btv->c, gpio | (1 << 4));
6c6c0b2c
MW
428 } else {
429 /* init hardware-specific stuff */
4abdfed5 430 bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
6c6c0b2c 431 }
1da177e4
LT
432
433 /* init input device */
4abdfed5
RC
434 ir->dev = input_dev;
435
1da177e4 436 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
4abdfed5 437 btv->c.type);
1da177e4 438 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
4abdfed5 439 pci_name(btv->c.pci));
1da177e4 440
b7df3910
DT
441 input_dev->name = ir->name;
442 input_dev->phys = ir->phys;
443 input_dev->id.bustype = BUS_PCI;
444 input_dev->id.version = 1;
4abdfed5
RC
445 if (btv->c.pci->subsystem_vendor) {
446 input_dev->id.vendor = btv->c.pci->subsystem_vendor;
447 input_dev->id.product = btv->c.pci->subsystem_device;
1da177e4 448 } else {
4abdfed5
RC
449 input_dev->id.vendor = btv->c.pci->vendor;
450 input_dev->id.product = btv->c.pci->device;
1da177e4 451 }
2c8a3a33 452 input_dev->dev.parent = &btv->c.pci->dev;
1da177e4 453
4abdfed5 454 btv->remote = ir;
b07b4783 455 bttv_ir_start(btv, ir);
1da177e4
LT
456
457 /* all done */
02858eed 458 err = ir_input_register(btv->remote->dev, ir_codes, NULL, MODULE_NAME);
b07b4783
DT
459 if (err)
460 goto err_out_stop;
6c6c0b2c
MW
461
462 /* the remote isn't as bouncy as a keyboard */
4abdfed5
RC
463 ir->dev->rep[REP_DELAY] = repeat_delay;
464 ir->dev->rep[REP_PERIOD] = repeat_period;
1da177e4
LT
465
466 return 0;
b07b4783
DT
467
468 err_out_stop:
469 bttv_ir_stop(btv);
470 btv->remote = NULL;
471 err_out_free:
b07b4783
DT
472 kfree(ir);
473 return err;
1da177e4
LT
474}
475
4abdfed5 476void bttv_input_fini(struct bttv *btv)
1da177e4 477{
4abdfed5
RC
478 if (btv->remote == NULL)
479 return;
1da177e4 480
b07b4783 481 bttv_ir_stop(btv);
38ef6aa8 482 ir_input_unregister(btv->remote->dev);
4abdfed5
RC
483 kfree(btv->remote);
484 btv->remote = NULL;
1da177e4 485}