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/moduleparam.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/input.h>
33 module_param(debug, int, 0644); /* debug level (0,1,2) */
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);
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);
44 #define DEVNAME "bttv-input"
46 /* ---------------------------------------------------------------------- */
48 static void ir_handle_key(struct bttv *btv)
50 struct card_ir *ir = btv->remote;
54 gpio = bttv_gpio_read(&btv->c);
56 if (ir->last_gpio == gpio)
62 data = ir_extract_bits(gpio, ir->mask_keycode);
63 dprintk(KERN_INFO DEVNAME ": irq gpio=0x%x code=%d | %s%s%s\n",
65 ir->polling ? "poll" : "irq",
66 (gpio & ir->mask_keydown) ? " down" : "",
67 (gpio & ir->mask_keyup) ? " up" : "");
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);
73 ir_input_nokey(ir->dev,&ir->ir);
78 void bttv_input_irq(struct bttv *btv)
80 struct card_ir *ir = btv->remote;
86 static void bttv_input_timer(unsigned long data)
88 struct bttv *btv = (struct bttv*)data;
89 struct card_ir *ir = btv->remote;
92 mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
95 /* ---------------------------------------------------------------*/
97 static int bttv_rc5_irq(struct bttv *btv)
99 struct card_ir *ir = btv->remote;
103 unsigned long current_jiffies;
106 gpio = bttv_gpio_read(&btv->c);
112 /* get time of bit */
113 current_jiffies = jiffies;
114 do_gettimeofday(&tv);
116 /* avoid overflow with gap >1s */
117 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
120 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
121 tv.tv_usec - ir->base_time.tv_usec;
124 /* active code => add bit */
126 /* only if in the code (otherwise spurious IRQ or timer
128 if (ir->last_bit < 28) {
129 ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
131 ir->code |= 1 << ir->last_bit;
133 /* starting new code */
140 mod_timer(&ir->timer_end,
141 current_jiffies + msecs_to_jiffies(30));
144 /* toggle GPIO pin 4 to reset the irq */
145 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
146 bttv_gpio_write(&btv->c, gpio | (1 << 4));
150 /* ---------------------------------------------------------------------- */
152 static void bttv_ir_start(struct bttv *btv, struct card_ir *ir)
155 setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
156 ir->timer.expires = jiffies + HZ;
157 add_timer(&ir->timer);
158 } else if (ir->rc5_gpio) {
159 /* set timer_end for code completion */
160 init_timer(&ir->timer_end);
161 ir->timer_end.function = ir_rc5_timer_end;
162 ir->timer_end.data = (unsigned long)ir;
164 init_timer(&ir->timer_keyup);
165 ir->timer_keyup.function = ir_rc5_timer_keyup;
166 ir->timer_keyup.data = (unsigned long)ir;
170 ir->rc5_key_timeout = ir_rc5_key_timeout;
171 ir->rc5_remote_gap = ir_rc5_remote_gap;
175 static void bttv_ir_stop(struct bttv *btv)
177 if (btv->remote->polling) {
178 del_timer_sync(&btv->remote->timer);
179 flush_scheduled_work();
182 if (btv->remote->rc5_gpio) {
185 del_timer_sync(&btv->remote->timer_end);
186 flush_scheduled_work();
188 gpio = bttv_gpio_read(&btv->c);
189 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
193 int bttv_input_init(struct bttv *btv)
196 IR_KEYTAB_TYPE *ir_codes = NULL;
197 struct input_dev *input_dev;
198 int ir_type = IR_TYPE_OTHER;
201 if (!btv->has_remote)
204 ir = kzalloc(sizeof(*ir),GFP_KERNEL);
205 input_dev = input_allocate_device();
206 if (!ir || !input_dev)
209 /* detect & configure */
210 switch (btv->c.type) {
211 case BTTV_BOARD_AVERMEDIA:
212 case BTTV_BOARD_AVPHONE98:
213 case BTTV_BOARD_AVERMEDIA98:
214 ir_codes = ir_codes_avermedia;
215 ir->mask_keycode = 0xf88000;
216 ir->mask_keydown = 0x010000;
217 ir->polling = 50; // ms
220 case BTTV_BOARD_AVDVBT_761:
221 case BTTV_BOARD_AVDVBT_771:
222 ir_codes = ir_codes_avermedia_dvbt;
223 ir->mask_keycode = 0x0f00c0;
224 ir->mask_keydown = 0x000020;
225 ir->polling = 50; // ms
228 case BTTV_BOARD_PXELVWPLTVPAK:
229 ir_codes = ir_codes_pixelview;
230 ir->mask_keycode = 0x003e00;
231 ir->mask_keyup = 0x010000;
232 ir->polling = 50; // ms
234 case BTTV_BOARD_PV_M4900:
235 case BTTV_BOARD_PV_BT878P_9B:
236 case BTTV_BOARD_PV_BT878P_PLUS:
237 ir_codes = ir_codes_pixelview;
238 ir->mask_keycode = 0x001f00;
239 ir->mask_keyup = 0x008000;
240 ir->polling = 50; // ms
243 case BTTV_BOARD_WINFAST2000:
244 ir_codes = ir_codes_winfast;
245 ir->mask_keycode = 0x1f8;
247 case BTTV_BOARD_MAGICTVIEW061:
248 case BTTV_BOARD_MAGICTVIEW063:
249 ir_codes = ir_codes_winfast;
250 ir->mask_keycode = 0x0008e000;
251 ir->mask_keydown = 0x00200000;
253 case BTTV_BOARD_APAC_VIEWCOMP:
254 ir_codes = ir_codes_apac_viewcomp;
255 ir->mask_keycode = 0x001f00;
256 ir->mask_keyup = 0x008000;
257 ir->polling = 50; // ms
259 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
260 case BTTV_BOARD_CONTVFMI:
261 ir_codes = ir_codes_pixelview;
262 ir->mask_keycode = 0x001F00;
263 ir->mask_keyup = 0x006000;
264 ir->polling = 50; // ms
266 case BTTV_BOARD_NEBULA_DIGITV:
267 ir_codes = ir_codes_nebula;
268 btv->custom_irq = bttv_rc5_irq;
271 case BTTV_BOARD_MACHTV_MAGICTV:
272 ir_codes = ir_codes_apac_viewcomp;
273 ir->mask_keycode = 0x001F00;
274 ir->mask_keyup = 0x004000;
275 ir->polling = 50; /* ms */
278 if (NULL == ir_codes) {
279 dprintk(KERN_INFO "Ooops: IR config error [card=%d]\n", btv->c.type);
286 /* enable remote irq */
287 bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
288 gpio = bttv_gpio_read(&btv->c);
289 bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
290 bttv_gpio_write(&btv->c, gpio | (1 << 4));
292 /* init hardware-specific stuff */
293 bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
296 /* init input device */
299 snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
301 snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
302 pci_name(btv->c.pci));
304 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
305 input_dev->name = ir->name;
306 input_dev->phys = ir->phys;
307 input_dev->id.bustype = BUS_PCI;
308 input_dev->id.version = 1;
309 if (btv->c.pci->subsystem_vendor) {
310 input_dev->id.vendor = btv->c.pci->subsystem_vendor;
311 input_dev->id.product = btv->c.pci->subsystem_device;
313 input_dev->id.vendor = btv->c.pci->vendor;
314 input_dev->id.product = btv->c.pci->device;
316 input_dev->dev.parent = &btv->c.pci->dev;
319 bttv_ir_start(btv, ir);
322 err = input_register_device(btv->remote->dev);
326 /* the remote isn't as bouncy as a keyboard */
327 ir->dev->rep[REP_DELAY] = repeat_delay;
328 ir->dev->rep[REP_PERIOD] = repeat_period;
336 input_free_device(input_dev);
341 void bttv_input_fini(struct bttv *btv)
343 if (btv->remote == NULL)
347 input_unregister_device(btv->remote->dev);