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>
26 #include <linux/slab.h>
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);
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);
45 #define dprintk(arg...) do { \
50 #define DEVNAME "bttv-input"
52 /* ---------------------------------------------------------------------- */
54 static void ir_handle_key(struct bttv
*btv
)
56 struct card_ir
*ir
= btv
->remote
;
60 gpio
= bttv_gpio_read(&btv
->c
);
62 if (ir
->last_gpio
== gpio
)
68 data
= ir_extract_bits(gpio
, ir
->mask_keycode
);
69 dprintk(KERN_INFO DEVNAME
": irq gpio=0x%x code=%d | %s%s%s\n",
71 ir
->polling
? "poll" : "irq",
72 (gpio
& ir
->mask_keydown
) ? " down" : "",
73 (gpio
& ir
->mask_keyup
) ? " up" : "");
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
);
79 /* HACK: Probably, ir->mask_keydown is missing
81 if (btv
->c
.type
== BTTV_BOARD_WINFAST2000
)
82 ir_input_keydown(ir
->dev
, &ir
->ir
, data
);
84 ir_input_nokey(ir
->dev
,&ir
->ir
);
89 static void ir_enltv_handle_key(struct bttv
*btv
)
91 struct card_ir
*ir
= btv
->remote
;
92 u32 gpio
, data
, keyup
;
95 gpio
= bttv_gpio_read(&btv
->c
);
98 data
= ir_extract_bits(gpio
, ir
->mask_keycode
);
100 /* Check if it is keyup */
101 keyup
= (gpio
& ir
->mask_keyup
) ? 1 << 31 : 0;
103 if ((ir
->last_gpio
& 0x7f) != data
) {
104 dprintk(KERN_INFO DEVNAME
": gpio=0x%x code=%d | %s\n",
106 (gpio
& ir
->mask_keyup
) ? " up" : "up/down");
108 ir_input_keydown(ir
->dev
, &ir
->ir
, data
);
110 ir_input_nokey(ir
->dev
, &ir
->ir
);
112 if ((ir
->last_gpio
& 1 << 31) == keyup
)
115 dprintk(KERN_INFO DEVNAME
":(cnt) gpio=0x%x code=%d | %s\n",
117 (gpio
& ir
->mask_keyup
) ? " up" : "down");
120 ir_input_nokey(ir
->dev
, &ir
->ir
);
122 ir_input_keydown(ir
->dev
, &ir
->ir
, data
);
125 ir
->last_gpio
= data
| keyup
;
128 void bttv_input_irq(struct bttv
*btv
)
130 struct card_ir
*ir
= btv
->remote
;
136 static void bttv_input_timer(unsigned long data
)
138 struct bttv
*btv
= (struct bttv
*)data
;
139 struct card_ir
*ir
= btv
->remote
;
141 if (btv
->c
.type
== BTTV_BOARD_ENLTV_FM_2
)
142 ir_enltv_handle_key(btv
);
145 mod_timer(&ir
->timer
, jiffies
+ msecs_to_jiffies(ir
->polling
));
148 /* ---------------------------------------------------------------*/
150 static int bttv_rc5_irq(struct bttv
*btv
)
152 struct card_ir
*ir
= btv
->remote
;
156 unsigned long current_jiffies
;
159 gpio
= bttv_gpio_read(&btv
->c
);
165 /* get time of bit */
166 current_jiffies
= jiffies
;
167 do_gettimeofday(&tv
);
169 /* avoid overflow with gap >1s */
170 if (tv
.tv_sec
- ir
->base_time
.tv_sec
> 1) {
173 gap
= 1000000 * (tv
.tv_sec
- ir
->base_time
.tv_sec
) +
174 tv
.tv_usec
- ir
->base_time
.tv_usec
;
177 /* active code => add bit */
179 /* only if in the code (otherwise spurious IRQ or timer
181 if (ir
->last_bit
< 28) {
182 ir
->last_bit
= (gap
- ir_rc5_remote_gap
/ 2) /
184 ir
->code
|= 1 << ir
->last_bit
;
186 /* starting new code */
193 mod_timer(&ir
->timer_end
,
194 current_jiffies
+ msecs_to_jiffies(30));
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));
203 /* ---------------------------------------------------------------------- */
205 static void bttv_ir_start(struct bttv
*btv
, struct card_ir
*ir
)
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
;
217 init_timer(&ir
->timer_keyup
);
218 ir
->timer_keyup
.function
= ir_rc5_timer_keyup
;
219 ir
->timer_keyup
.data
= (unsigned long)ir
;
223 ir
->rc5_key_timeout
= ir_rc5_key_timeout
;
224 ir
->rc5_remote_gap
= ir_rc5_remote_gap
;
228 static void bttv_ir_stop(struct bttv
*btv
)
230 if (btv
->remote
->polling
) {
231 del_timer_sync(&btv
->remote
->timer
);
232 flush_scheduled_work();
235 if (btv
->remote
->rc5_gpio
) {
238 del_timer_sync(&btv
->remote
->timer_end
);
239 flush_scheduled_work();
241 gpio
= bttv_gpio_read(&btv
->c
);
242 bttv_gpio_write(&btv
->c
, gpio
& ~(1 << 4));
246 int bttv_input_init(struct bttv
*btv
)
249 struct ir_scancode_table
*ir_codes
= NULL
;
250 struct input_dev
*input_dev
;
251 u64 ir_type
= IR_TYPE_OTHER
;
254 if (!btv
->has_remote
)
257 ir
= kzalloc(sizeof(*ir
),GFP_KERNEL
);
258 input_dev
= input_allocate_device();
259 if (!ir
|| !input_dev
)
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
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
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
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
296 case BTTV_BOARD_WINFAST2000
:
297 ir_codes
= &ir_codes_winfast_table
;
298 ir
->mask_keycode
= 0x1f8;
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;
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
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
320 case BTTV_BOARD_NEBULA_DIGITV
:
321 ir_codes
= &ir_codes_nebula_table
;
322 btv
->custom_irq
= bttv_rc5_irq
;
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 */
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 */
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
),
346 if (NULL
== ir_codes
) {
347 dprintk(KERN_INFO
"Ooops: IR config error [card=%d]\n", btv
->c
.type
);
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));
360 /* init hardware-specific stuff */
361 bttv_gpio_inout(&btv
->c
, ir
->mask_keycode
| ir
->mask_keydown
, 0);
364 /* init input device */
367 snprintf(ir
->name
, sizeof(ir
->name
), "bttv IR (card=%d)",
369 snprintf(ir
->phys
, sizeof(ir
->phys
), "pci-%s/ir0",
370 pci_name(btv
->c
.pci
));
372 err
= ir_input_init(input_dev
, &ir
->ir
, ir_type
);
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
;
384 input_dev
->id
.vendor
= btv
->c
.pci
->vendor
;
385 input_dev
->id
.product
= btv
->c
.pci
->device
;
387 input_dev
->dev
.parent
= &btv
->c
.pci
->dev
;
390 bttv_ir_start(btv
, ir
);
393 err
= ir_input_register(btv
->remote
->dev
, ir_codes
, NULL
);
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
;
411 void bttv_input_fini(struct bttv
*btv
)
413 if (btv
->remote
== NULL
)
417 ir_input_unregister(btv
->remote
->dev
);