V4L/DVB (6079): Cleanup: remove linux/moduleparam.h from drivers/media files
[linux-2.6.git] / 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
27 #include "bttv.h"
28 #include "bttvp.h"
29
30
31 static int debug;
32 module_param(debug, int, 0644);    /* debug level (0,1,2) */
33 static int repeat_delay = 500;
34 module_param(repeat_delay, int, 0644);
35 static int repeat_period = 33;
36 module_param(repeat_period, int, 0644);
37
38 static int ir_rc5_remote_gap = 885;
39 module_param(ir_rc5_remote_gap, int, 0644);
40 static int ir_rc5_key_timeout = 200;
41 module_param(ir_rc5_key_timeout, int, 0644);
42
43 #define DEVNAME "bttv-input"
44
45 /* ---------------------------------------------------------------------- */
46
47 static void ir_handle_key(struct bttv *btv)
48 {
49         struct card_ir *ir = btv->remote;
50         u32 gpio,data;
51
52         /* read gpio value */
53         gpio = bttv_gpio_read(&btv->c);
54         if (ir->polling) {
55                 if (ir->last_gpio == gpio)
56                         return;
57                 ir->last_gpio = gpio;
58         }
59
60         /* extract data */
61         data = ir_extract_bits(gpio, ir->mask_keycode);
62         dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
63                 gpio, data,
64                 ir->polling               ? "poll"  : "irq",
65                 (gpio & ir->mask_keydown) ? " down" : "",
66                 (gpio & ir->mask_keyup)   ? " up"   : "");
67
68         if ((ir->mask_keydown  &&  (0 != (gpio & ir->mask_keydown))) ||
69             (ir->mask_keyup    &&  (0 == (gpio & ir->mask_keyup)))) {
70                 ir_input_keydown(ir->dev,&ir->ir,data,data);
71         } else {
72                 ir_input_nokey(ir->dev,&ir->ir);
73         }
74
75 }
76
77 void bttv_input_irq(struct bttv *btv)
78 {
79         struct card_ir *ir = btv->remote;
80
81         if (!ir->polling)
82                 ir_handle_key(btv);
83 }
84
85 static void bttv_input_timer(unsigned long data)
86 {
87         struct bttv *btv = (struct bttv*)data;
88         struct card_ir *ir = btv->remote;
89
90         ir_handle_key(btv);
91         mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
92 }
93
94 /* ---------------------------------------------------------------*/
95
96 static int bttv_rc5_irq(struct bttv *btv)
97 {
98         struct card_ir *ir = btv->remote;
99         struct timeval tv;
100         u32 gpio;
101         u32 gap;
102         unsigned long current_jiffies;
103
104         /* read gpio port */
105         gpio = bttv_gpio_read(&btv->c);
106
107         /* remote IRQ? */
108         if (!(gpio & 0x20))
109                 return 0;
110
111         /* get time of bit */
112         current_jiffies = jiffies;
113         do_gettimeofday(&tv);
114
115         /* avoid overflow with gap >1s */
116         if (tv.tv_sec - ir->base_time.tv_sec > 1) {
117                 gap = 200000;
118         } else {
119                 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
120                     tv.tv_usec - ir->base_time.tv_usec;
121         }
122
123         /* active code => add bit */
124         if (ir->active) {
125                 /* only if in the code (otherwise spurious IRQ or timer
126                    late) */
127                 if (ir->last_bit < 28) {
128                         ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
129                             ir_rc5_remote_gap;
130                         ir->code |= 1 << ir->last_bit;
131                 }
132                 /* starting new code */
133         } else {
134                 ir->active = 1;
135                 ir->code = 0;
136                 ir->base_time = tv;
137                 ir->last_bit = 0;
138
139                 mod_timer(&ir->timer_end,
140                           current_jiffies + msecs_to_jiffies(30));
141         }
142
143         /* toggle GPIO pin 4 to reset the irq */
144         bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
145         bttv_gpio_write(&btv->c, gpio | (1 << 4));
146         return 1;
147 }
148
149 /* ---------------------------------------------------------------------- */
150
151 static void bttv_ir_start(struct bttv *btv, struct card_ir *ir)
152 {
153         if (ir->polling) {
154                 setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
155                 ir->timer.expires  = jiffies + msecs_to_jiffies(1000);
156                 add_timer(&ir->timer);
157         } else if (ir->rc5_gpio) {
158                 /* set timer_end for code completion */
159                 init_timer(&ir->timer_end);
160                 ir->timer_end.function = ir_rc5_timer_end;
161                 ir->timer_end.data = (unsigned long)ir;
162
163                 init_timer(&ir->timer_keyup);
164                 ir->timer_keyup.function = ir_rc5_timer_keyup;
165                 ir->timer_keyup.data = (unsigned long)ir;
166                 ir->shift_by = 1;
167                 ir->start = 3;
168                 ir->addr = 0x0;
169                 ir->rc5_key_timeout = ir_rc5_key_timeout;
170                 ir->rc5_remote_gap = ir_rc5_remote_gap;
171         }
172 }
173
174 static void bttv_ir_stop(struct bttv *btv)
175 {
176         if (btv->remote->polling) {
177                 del_timer_sync(&btv->remote->timer);
178                 flush_scheduled_work();
179         }
180
181         if (btv->remote->rc5_gpio) {
182                 u32 gpio;
183
184                 del_timer_sync(&btv->remote->timer_end);
185                 flush_scheduled_work();
186
187                 gpio = bttv_gpio_read(&btv->c);
188                 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
189         }
190 }
191
192 int bttv_input_init(struct bttv *btv)
193 {
194         struct card_ir *ir;
195         IR_KEYTAB_TYPE *ir_codes = NULL;
196         struct input_dev *input_dev;
197         int ir_type = IR_TYPE_OTHER;
198         int err = -ENOMEM;
199
200         if (!btv->has_remote)
201                 return -ENODEV;
202
203         ir = kzalloc(sizeof(*ir),GFP_KERNEL);
204         input_dev = input_allocate_device();
205         if (!ir || !input_dev)
206                 goto err_out_free;
207
208         /* detect & configure */
209         switch (btv->c.type) {
210         case BTTV_BOARD_AVERMEDIA:
211         case BTTV_BOARD_AVPHONE98:
212         case BTTV_BOARD_AVERMEDIA98:
213                 ir_codes         = ir_codes_avermedia;
214                 ir->mask_keycode = 0xf88000;
215                 ir->mask_keydown = 0x010000;
216                 ir->polling      = 50; // ms
217                 break;
218
219         case BTTV_BOARD_AVDVBT_761:
220         case BTTV_BOARD_AVDVBT_771:
221                 ir_codes         = ir_codes_avermedia_dvbt;
222                 ir->mask_keycode = 0x0f00c0;
223                 ir->mask_keydown = 0x000020;
224                 ir->polling      = 50; // ms
225                 break;
226
227         case BTTV_BOARD_PXELVWPLTVPAK:
228                 ir_codes         = ir_codes_pixelview;
229                 ir->mask_keycode = 0x003e00;
230                 ir->mask_keyup   = 0x010000;
231                 ir->polling      = 50; // ms
232                 break;
233         case BTTV_BOARD_PV_M4900:
234         case BTTV_BOARD_PV_BT878P_9B:
235         case BTTV_BOARD_PV_BT878P_PLUS:
236                 ir_codes         = ir_codes_pixelview;
237                 ir->mask_keycode = 0x001f00;
238                 ir->mask_keyup   = 0x008000;
239                 ir->polling      = 50; // ms
240                 break;
241
242         case BTTV_BOARD_WINFAST2000:
243                 ir_codes         = ir_codes_winfast;
244                 ir->mask_keycode = 0x1f8;
245                 break;
246         case BTTV_BOARD_MAGICTVIEW061:
247         case BTTV_BOARD_MAGICTVIEW063:
248                 ir_codes         = ir_codes_winfast;
249                 ir->mask_keycode = 0x0008e000;
250                 ir->mask_keydown = 0x00200000;
251                 break;
252         case BTTV_BOARD_APAC_VIEWCOMP:
253                 ir_codes         = ir_codes_apac_viewcomp;
254                 ir->mask_keycode = 0x001f00;
255                 ir->mask_keyup   = 0x008000;
256                 ir->polling      = 50; // ms
257                 break;
258         case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
259         case BTTV_BOARD_CONTVFMI:
260                 ir_codes         = ir_codes_pixelview;
261                 ir->mask_keycode = 0x001F00;
262                 ir->mask_keyup   = 0x006000;
263                 ir->polling      = 50; // ms
264                 break;
265         case BTTV_BOARD_NEBULA_DIGITV:
266                 ir_codes = ir_codes_nebula;
267                 btv->custom_irq = bttv_rc5_irq;
268                 ir->rc5_gpio = 1;
269                 break;
270         case BTTV_BOARD_MACHTV_MAGICTV:
271                 ir_codes         = ir_codes_apac_viewcomp;
272                 ir->mask_keycode = 0x001F00;
273                 ir->mask_keyup   = 0x004000;
274                 ir->polling      = 50; /* ms */
275                 break;
276         }
277         if (NULL == ir_codes) {
278                 dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
279                 err = -ENODEV;
280                 goto err_out_free;
281         }
282
283         if (ir->rc5_gpio) {
284                 u32 gpio;
285                 /* enable remote irq */
286                 bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
287                 gpio = bttv_gpio_read(&btv->c);
288                 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
289                 bttv_gpio_write(&btv->c, gpio | (1 << 4));
290         } else {
291                 /* init hardware-specific stuff */
292                 bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
293         }
294
295         /* init input device */
296         ir->dev = input_dev;
297
298         snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
299                  btv->c.type);
300         snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
301                  pci_name(btv->c.pci));
302
303         ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
304         input_dev->name = ir->name;
305         input_dev->phys = ir->phys;
306         input_dev->id.bustype = BUS_PCI;
307         input_dev->id.version = 1;
308         if (btv->c.pci->subsystem_vendor) {
309                 input_dev->id.vendor  = btv->c.pci->subsystem_vendor;
310                 input_dev->id.product = btv->c.pci->subsystem_device;
311         } else {
312                 input_dev->id.vendor  = btv->c.pci->vendor;
313                 input_dev->id.product = btv->c.pci->device;
314         }
315         input_dev->dev.parent = &btv->c.pci->dev;
316
317         btv->remote = ir;
318         bttv_ir_start(btv, ir);
319
320         /* all done */
321         err = input_register_device(btv->remote->dev);
322         if (err)
323                 goto err_out_stop;
324
325         /* the remote isn't as bouncy as a keyboard */
326         ir->dev->rep[REP_DELAY] = repeat_delay;
327         ir->dev->rep[REP_PERIOD] = repeat_period;
328
329         return 0;
330
331  err_out_stop:
332         bttv_ir_stop(btv);
333         btv->remote = NULL;
334  err_out_free:
335         input_free_device(input_dev);
336         kfree(ir);
337         return err;
338 }
339
340 void bttv_input_fini(struct bttv *btv)
341 {
342         if (btv->remote == NULL)
343                 return;
344
345         bttv_ir_stop(btv);
346         input_unregister_device(btv->remote->dev);
347         kfree(btv->remote);
348         btv->remote = NULL;
349 }
350
351
352 /*
353  * Local variables:
354  * c-basic-offset: 8
355  * End:
356  */