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