3 * Copyright (c) 2003 Gerd Knorr
4 * Copyright (c) 2003 Pavel Machek
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.
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.
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
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>
32 module_param(ir_debug, int, 0644);
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);
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);
44 #define dprintk(arg...) do { \
49 #define DEVNAME "bttv-input"
51 /* ---------------------------------------------------------------------- */
53 static void ir_handle_key(struct bttv *btv)
55 struct card_ir *ir = btv->remote;
59 gpio = bttv_gpio_read(&btv->c);
61 if (ir->last_gpio == gpio)
67 data = ir_extract_bits(gpio, ir->mask_keycode);
68 dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
70 ir->polling ? "poll" : "irq",
71 (gpio & ir->mask_keydown) ? " down" : "",
72 (gpio & ir->mask_keyup) ? " up" : "");
74 if ((ir->mask_keydown && (0 != (gpio & ir->mask_keydown))) ||
75 (ir->mask_keyup && (0 == (gpio & ir->mask_keyup)))) {
76 ir_input_keydown(ir->dev,&ir->ir,data,data);
78 /* HACK: Probably, ir->mask_keydown is missing
80 if (btv->c.type == BTTV_BOARD_WINFAST2000)
81 ir_input_keydown(ir->dev, &ir->ir, data, data);
83 ir_input_nokey(ir->dev,&ir->ir);
88 static void ir_enltv_handle_key(struct bttv *btv)
90 struct card_ir *ir = btv->remote;
91 u32 gpio, data, keyup;
94 gpio = bttv_gpio_read(&btv->c);
97 data = ir_extract_bits(gpio, ir->mask_keycode);
99 /* Check if it is keyup */
100 keyup = (gpio & ir->mask_keyup) ? 1 << 31 : 0;
102 if ((ir->last_gpio & 0x7f) != data) {
103 dprintk(KERN_INFO DEVNAME ": gpio=0x%x code=%d | %s\n",
105 (gpio & ir->mask_keyup) ? " up" : "up/down");
107 ir_input_keydown(ir->dev, &ir->ir, data, data);
109 ir_input_nokey(ir->dev, &ir->ir);
111 if ((ir->last_gpio & 1 << 31) == keyup)
114 dprintk(KERN_INFO DEVNAME ":(cnt) gpio=0x%x code=%d | %s\n",
116 (gpio & ir->mask_keyup) ? " up" : "down");
119 ir_input_nokey(ir->dev, &ir->ir);
121 ir_input_keydown(ir->dev, &ir->ir, data, data);
124 ir->last_gpio = data | keyup;
127 void bttv_input_irq(struct bttv *btv)
129 struct card_ir *ir = btv->remote;
135 static void bttv_input_timer(unsigned long data)
137 struct bttv *btv = (struct bttv*)data;
138 struct card_ir *ir = btv->remote;
140 if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
141 ir_enltv_handle_key(btv);
144 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
147 /* ---------------------------------------------------------------*/
149 static int bttv_rc5_irq(struct bttv *btv)
151 struct card_ir *ir = btv->remote;
155 unsigned long current_jiffies;
158 gpio = bttv_gpio_read(&btv->c);
164 /* get time of bit */
165 current_jiffies = jiffies;
166 do_gettimeofday(&tv);
168 /* avoid overflow with gap >1s */
169 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
172 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
173 tv.tv_usec - ir->base_time.tv_usec;
176 /* active code => add bit */
178 /* only if in the code (otherwise spurious IRQ or timer
180 if (ir->last_bit < 28) {
181 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
183 ir->code |= 1 << ir->last_bit;
185 /* starting new code */
192 mod_timer(&ir->timer_end,
193 current_jiffies + msecs_to_jiffies(30));
196 /* toggle GPIO pin 4 to reset the irq */
197 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
198 bttv_gpio_write(&btv->c, gpio | (1 << 4));
202 /* ---------------------------------------------------------------------- */
204 static void bttv_ir_start(struct bttv *btv, struct card_ir *ir)
207 setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
208 ir->timer.expires = jiffies + msecs_to_jiffies(1000);
209 add_timer(&ir->timer);
210 } else if (ir->rc5_gpio) {
211 /* set timer_end for code completion */
212 init_timer(&ir->timer_end);
213 ir->timer_end.function = ir_rc5_timer_end;
214 ir->timer_end.data = (unsigned long)ir;
216 init_timer(&ir->timer_keyup);
217 ir->timer_keyup.function = ir_rc5_timer_keyup;
218 ir->timer_keyup.data = (unsigned long)ir;
222 ir->rc5_key_timeout = ir_rc5_key_timeout;
223 ir->rc5_remote_gap = ir_rc5_remote_gap;
227 static void bttv_ir_stop(struct bttv *btv)
229 if (btv->remote->polling) {
230 del_timer_sync(&btv->remote->timer);
231 flush_scheduled_work();
234 if (btv->remote->rc5_gpio) {
237 del_timer_sync(&btv->remote->timer_end);
238 flush_scheduled_work();
240 gpio = bttv_gpio_read(&btv->c);
241 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
245 int bttv_input_init(struct bttv *btv)
248 IR_KEYTAB_TYPE *ir_codes = NULL;
249 struct input_dev *input_dev;
250 int ir_type = IR_TYPE_OTHER;
253 if (!btv->has_remote)
256 ir = kzalloc(sizeof(*ir),GFP_KERNEL);
257 input_dev = input_allocate_device();
258 if (!ir || !input_dev)
261 /* detect & configure */
262 switch (btv->c.type) {
263 case BTTV_BOARD_AVERMEDIA:
264 case BTTV_BOARD_AVPHONE98:
265 case BTTV_BOARD_AVERMEDIA98:
266 ir_codes = ir_codes_avermedia;
267 ir->mask_keycode = 0xf88000;
268 ir->mask_keydown = 0x010000;
269 ir->polling = 50; // ms
272 case BTTV_BOARD_AVDVBT_761:
273 case BTTV_BOARD_AVDVBT_771:
274 ir_codes = ir_codes_avermedia_dvbt;
275 ir->mask_keycode = 0x0f00c0;
276 ir->mask_keydown = 0x000020;
277 ir->polling = 50; // ms
280 case BTTV_BOARD_PXELVWPLTVPAK:
281 ir_codes = ir_codes_pixelview;
282 ir->mask_keycode = 0x003e00;
283 ir->mask_keyup = 0x010000;
284 ir->polling = 50; // ms
286 case BTTV_BOARD_PV_M4900:
287 case BTTV_BOARD_PV_BT878P_9B:
288 case BTTV_BOARD_PV_BT878P_PLUS:
289 ir_codes = ir_codes_pixelview;
290 ir->mask_keycode = 0x001f00;
291 ir->mask_keyup = 0x008000;
292 ir->polling = 50; // ms
295 case BTTV_BOARD_WINFAST2000:
296 ir_codes = ir_codes_winfast;
297 ir->mask_keycode = 0x1f8;
299 case BTTV_BOARD_MAGICTVIEW061:
300 case BTTV_BOARD_MAGICTVIEW063:
301 ir_codes = ir_codes_winfast;
302 ir->mask_keycode = 0x0008e000;
303 ir->mask_keydown = 0x00200000;
305 case BTTV_BOARD_APAC_VIEWCOMP:
306 ir_codes = ir_codes_apac_viewcomp;
307 ir->mask_keycode = 0x001f00;
308 ir->mask_keyup = 0x008000;
309 ir->polling = 50; // ms
311 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
312 case BTTV_BOARD_CONTVFMI:
313 ir_codes = ir_codes_pixelview;
314 ir->mask_keycode = 0x001F00;
315 ir->mask_keyup = 0x006000;
316 ir->polling = 50; // ms
318 case BTTV_BOARD_NEBULA_DIGITV:
319 ir_codes = ir_codes_nebula;
320 btv->custom_irq = bttv_rc5_irq;
323 case BTTV_BOARD_MACHTV_MAGICTV:
324 ir_codes = ir_codes_apac_viewcomp;
325 ir->mask_keycode = 0x001F00;
326 ir->mask_keyup = 0x004000;
327 ir->polling = 50; /* ms */
329 case BTTV_BOARD_KOZUMI_KTV_01C:
330 ir_codes = ir_codes_pctv_sedna;
331 ir->mask_keycode = 0x001f00;
332 ir->mask_keyup = 0x006000;
333 ir->polling = 50; /* ms */
335 case BTTV_BOARD_ENLTV_FM_2:
336 ir_codes = ir_codes_encore_enltv2;
337 ir->mask_keycode = 0x00fd00;
338 ir->mask_keyup = 0x000080;
339 ir->polling = 1; /* ms */
340 ir->last_gpio = ir_extract_bits(bttv_gpio_read(&btv->c),
344 if (NULL == ir_codes) {
345 dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
352 /* enable remote irq */
353 bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
354 gpio = bttv_gpio_read(&btv->c);
355 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
356 bttv_gpio_write(&btv->c, gpio | (1 << 4));
358 /* init hardware-specific stuff */
359 bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
362 /* init input device */
365 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
367 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
368 pci_name(btv->c.pci));
370 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
371 input_dev->name = ir->name;
372 input_dev->phys = ir->phys;
373 input_dev->id.bustype = BUS_PCI;
374 input_dev->id.version = 1;
375 if (btv->c.pci->subsystem_vendor) {
376 input_dev->id.vendor = btv->c.pci->subsystem_vendor;
377 input_dev->id.product = btv->c.pci->subsystem_device;
379 input_dev->id.vendor = btv->c.pci->vendor;
380 input_dev->id.product = btv->c.pci->device;
382 input_dev->dev.parent = &btv->c.pci->dev;
385 bttv_ir_start(btv, ir);
388 err = input_register_device(btv->remote->dev);
392 /* the remote isn't as bouncy as a keyboard */
393 ir->dev->rep[REP_DELAY] = repeat_delay;
394 ir->dev->rep[REP_PERIOD] = repeat_period;
402 input_free_device(input_dev);
407 void bttv_input_fini(struct bttv *btv)
409 if (btv->remote == NULL)
413 input_unregister_device(btv->remote->dev);