include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / media / video / bt8xx / bttv-input.c
1 /*
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/init.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/input.h>
26 #include <linux/slab.h>
27
28 #include "bttv.h"
29 #include "bttvp.h"
30
31
32 static int ir_debug;
33 module_param(ir_debug, int, 0644);
34 static int repeat_delay = 500;
35 module_param(repeat_delay, int, 0644);
36 static int repeat_period = 33;
37 module_param(repeat_period, int, 0644);
38
39 static int ir_rc5_remote_gap = 885;
40 module_param(ir_rc5_remote_gap, int, 0644);
41 static int ir_rc5_key_timeout = 200;
42 module_param(ir_rc5_key_timeout, int, 0644);
43
44 #undef dprintk
45 #define dprintk(arg...) do {    \
46         if (ir_debug >= 1)      \
47                 printk(arg);    \
48 } while (0)
49
50 #define DEVNAME "bttv-input"
51
52 /* ---------------------------------------------------------------------- */
53
54 static void ir_handle_key(struct bttv *btv)
55 {
56         struct card_ir *ir = btv->remote;
57         u32 gpio,data;
58
59         /* read gpio value */
60         gpio = bttv_gpio_read(&btv->c);
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);
69         dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
70                 gpio, data,
71                 ir->polling               ? "poll"  : "irq",
72                 (gpio & ir->mask_keydown) ? " down" : "",
73                 (gpio & ir->mask_keyup)   ? " up"   : "");
74
75         if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
76             (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
77                 ir_input_keydown(ir->dev, &ir->ir, data);
78         } else {
79                 /* HACK: Probably, ir->mask_keydown is missing
80                    for this board */
81                 if (btv->c.type == BTTV_BOARD_WINFAST2000)
82                         ir_input_keydown(ir->dev, &ir->ir, data);
83
84                 ir_input_nokey(ir->dev,&ir->ir);
85         }
86
87 }
88
89 static 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
108                 ir_input_keydown(ir->dev, &ir->ir, data);
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
122                         ir_input_keydown(ir->dev, &ir->ir, data);
123         }
124
125         ir->last_gpio = data | keyup;
126 }
127
128 void bttv_input_irq(struct bttv *btv)
129 {
130         struct card_ir *ir = btv->remote;
131
132         if (!ir->polling)
133                 ir_handle_key(btv);
134 }
135
136 static void bttv_input_timer(unsigned long data)
137 {
138         struct bttv *btv = (struct bttv*)data;
139         struct card_ir *ir = btv->remote;
140
141         if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
142                 ir_enltv_handle_key(btv);
143         else
144                 ir_handle_key(btv);
145         mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
146 }
147
148 /* ---------------------------------------------------------------*/
149
150 static int bttv_rc5_irq(struct bttv *btv)
151 {
152         struct card_ir *ir = btv->remote;
153         struct timeval tv;
154         u32 gpio;
155         u32 gap;
156         unsigned long current_jiffies;
157
158         /* read gpio port */
159         gpio = bttv_gpio_read(&btv->c);
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) {
182                         ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
183                             ir_rc5_remote_gap;
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
193                 mod_timer(&ir->timer_end,
194                           current_jiffies + msecs_to_jiffies(30));
195         }
196
197         /* toggle GPIO pin 4 to reset the irq */
198         bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
199         bttv_gpio_write(&btv->c, gpio | (1 << 4));
200         return 1;
201 }
202
203 /* ---------------------------------------------------------------------- */
204
205 static void bttv_ir_start(struct bttv *btv, struct card_ir *ir)
206 {
207         if (ir->polling) {
208                 setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
209                 ir->timer.expires  = jiffies + msecs_to_jiffies(1000);
210                 add_timer(&ir->timer);
211         } else if (ir->rc5_gpio) {
212                 /* set timer_end for code completion */
213                 init_timer(&ir->timer_end);
214                 ir->timer_end.function = ir_rc5_timer_end;
215                 ir->timer_end.data = (unsigned long)ir;
216
217                 init_timer(&ir->timer_keyup);
218                 ir->timer_keyup.function = ir_rc5_timer_keyup;
219                 ir->timer_keyup.data = (unsigned long)ir;
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;
225         }
226 }
227
228 static 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
246 int bttv_input_init(struct bttv *btv)
247 {
248         struct card_ir *ir;
249         struct ir_scancode_table *ir_codes = NULL;
250         struct input_dev *input_dev;
251         u64 ir_type = IR_TYPE_OTHER;
252         int err = -ENOMEM;
253
254         if (!btv->has_remote)
255                 return -ENODEV;
256
257         ir = kzalloc(sizeof(*ir),GFP_KERNEL);
258         input_dev = input_allocate_device();
259         if (!ir || !input_dev)
260                 goto err_out_free;
261
262         /* detect & configure */
263         switch (btv->c.type) {
264         case BTTV_BOARD_AVERMEDIA:
265         case BTTV_BOARD_AVPHONE98:
266         case BTTV_BOARD_AVERMEDIA98:
267                 ir_codes         = &ir_codes_avermedia_table;
268                 ir->mask_keycode = 0xf88000;
269                 ir->mask_keydown = 0x010000;
270                 ir->polling      = 50; // ms
271                 break;
272
273         case BTTV_BOARD_AVDVBT_761:
274         case BTTV_BOARD_AVDVBT_771:
275                 ir_codes         = &ir_codes_avermedia_dvbt_table;
276                 ir->mask_keycode = 0x0f00c0;
277                 ir->mask_keydown = 0x000020;
278                 ir->polling      = 50; // ms
279                 break;
280
281         case BTTV_BOARD_PXELVWPLTVPAK:
282                 ir_codes         = &ir_codes_pixelview_table;
283                 ir->mask_keycode = 0x003e00;
284                 ir->mask_keyup   = 0x010000;
285                 ir->polling      = 50; // ms
286                 break;
287         case BTTV_BOARD_PV_M4900:
288         case BTTV_BOARD_PV_BT878P_9B:
289         case BTTV_BOARD_PV_BT878P_PLUS:
290                 ir_codes         = &ir_codes_pixelview_table;
291                 ir->mask_keycode = 0x001f00;
292                 ir->mask_keyup   = 0x008000;
293                 ir->polling      = 50; // ms
294                 break;
295
296         case BTTV_BOARD_WINFAST2000:
297                 ir_codes         = &ir_codes_winfast_table;
298                 ir->mask_keycode = 0x1f8;
299                 break;
300         case BTTV_BOARD_MAGICTVIEW061:
301         case BTTV_BOARD_MAGICTVIEW063:
302                 ir_codes         = &ir_codes_winfast_table;
303                 ir->mask_keycode = 0x0008e000;
304                 ir->mask_keydown = 0x00200000;
305                 break;
306         case BTTV_BOARD_APAC_VIEWCOMP:
307                 ir_codes         = &ir_codes_apac_viewcomp_table;
308                 ir->mask_keycode = 0x001f00;
309                 ir->mask_keyup   = 0x008000;
310                 ir->polling      = 50; // ms
311                 break;
312         case BTTV_BOARD_ASKEY_CPH03X:
313         case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
314         case BTTV_BOARD_CONTVFMI:
315                 ir_codes         = &ir_codes_pixelview_table;
316                 ir->mask_keycode = 0x001F00;
317                 ir->mask_keyup   = 0x006000;
318                 ir->polling      = 50; // ms
319                 break;
320         case BTTV_BOARD_NEBULA_DIGITV:
321                 ir_codes = &ir_codes_nebula_table;
322                 btv->custom_irq = bttv_rc5_irq;
323                 ir->rc5_gpio = 1;
324                 break;
325         case BTTV_BOARD_MACHTV_MAGICTV:
326                 ir_codes         = &ir_codes_apac_viewcomp_table;
327                 ir->mask_keycode = 0x001F00;
328                 ir->mask_keyup   = 0x004000;
329                 ir->polling      = 50; /* ms */
330                 break;
331         case BTTV_BOARD_KOZUMI_KTV_01C:
332                 ir_codes         = &ir_codes_pctv_sedna_table;
333                 ir->mask_keycode = 0x001f00;
334                 ir->mask_keyup   = 0x006000;
335                 ir->polling      = 50; /* ms */
336                 break;
337         case BTTV_BOARD_ENLTV_FM_2:
338                 ir_codes         = &ir_codes_encore_enltv2_table;
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;
345         }
346         if (NULL == ir_codes) {
347                 dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
348                 err = -ENODEV;
349                 goto err_out_free;
350         }
351
352         if (ir->rc5_gpio) {
353                 u32 gpio;
354                 /* enable remote irq */
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));
359         } else {
360                 /* init hardware-specific stuff */
361                 bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
362         }
363
364         /* init input device */
365         ir->dev = input_dev;
366
367         snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
368                  btv->c.type);
369         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
370                  pci_name(btv->c.pci));
371
372         err = ir_input_init(input_dev, &ir->ir, ir_type);
373         if (err < 0)
374                 goto err_out_free;
375
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;
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;
383         } else {
384                 input_dev->id.vendor  = btv->c.pci->vendor;
385                 input_dev->id.product = btv->c.pci->device;
386         }
387         input_dev->dev.parent = &btv->c.pci->dev;
388
389         btv->remote = ir;
390         bttv_ir_start(btv, ir);
391
392         /* all done */
393         err = ir_input_register(btv->remote->dev, ir_codes, NULL);
394         if (err)
395                 goto err_out_stop;
396
397         /* the remote isn't as bouncy as a keyboard */
398         ir->dev->rep[REP_DELAY] = repeat_delay;
399         ir->dev->rep[REP_PERIOD] = repeat_period;
400
401         return 0;
402
403  err_out_stop:
404         bttv_ir_stop(btv);
405         btv->remote = NULL;
406  err_out_free:
407         kfree(ir);
408         return err;
409 }
410
411 void bttv_input_fini(struct bttv *btv)
412 {
413         if (btv->remote == NULL)
414                 return;
415
416         bttv_ir_stop(btv);
417         ir_input_unregister(btv->remote->dev);
418         kfree(btv->remote);
419         btv->remote = NULL;
420 }
421
422
423 /*
424  * Local variables:
425  * c-basic-offset: 8
426  * End:
427  */