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