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 #define DEVNAME "bttv-input"
41 /* ---------------------------------------------------------------------- */
43 static void ir_handle_key(struct bttv
*btv
)
45 struct bttv_ir
*ir
= btv
->remote
;
49 gpio
= bttv_gpio_read(&btv
->c
);
51 if (ir
->last_gpio
== gpio
)
57 data
= ir_extract_bits(gpio
, ir
->mask_keycode
);
58 dprintk(KERN_INFO DEVNAME
": irq gpio=0x%x code=%d | %s%s%s\n",
60 ir
->polling
? "poll" : "irq",
61 (gpio
& ir
->mask_keydown
) ? " down" : "",
62 (gpio
& ir
->mask_keyup
) ? " up" : "");
64 if ((ir
->mask_keydown
&& (0 != (gpio
& ir
->mask_keydown
))) ||
65 (ir
->mask_keyup
&& (0 == (gpio
& ir
->mask_keyup
)))) {
66 ir_input_keydown(ir
->dev
,&ir
->ir
,data
,data
);
68 ir_input_nokey(ir
->dev
,&ir
->ir
);
73 void bttv_input_irq(struct bttv
*btv
)
75 struct bttv_ir
*ir
= btv
->remote
;
81 static void bttv_input_timer(unsigned long data
)
83 struct bttv
*btv
= (struct bttv
*)data
;
84 struct bttv_ir
*ir
= btv
->remote
;
85 unsigned long timeout
;
88 timeout
= jiffies
+ (ir
->polling
* HZ
/ 1000);
89 mod_timer(&ir
->timer
, timeout
);
92 /* ---------------------------------------------------------------*/
94 static int rc5_remote_gap
= 885;
95 module_param(rc5_remote_gap
, int, 0644);
96 static int rc5_key_timeout
= 200;
97 module_param(rc5_key_timeout
, int, 0644);
99 #define RC5_START(x) (((x)>>12)&3)
100 #define RC5_TOGGLE(x) (((x)>>11)&1)
101 #define RC5_ADDR(x) (((x)>>6)&31)
102 #define RC5_INSTR(x) ((x)&63)
104 /* decode raw bit pattern to RC5 code */
105 static u32
rc5_decode(unsigned int code
)
107 unsigned int org_code
= code
;
109 unsigned int rc5
= 0;
112 code
= (code
<< 1) | 1;
113 for (i
= 0; i
< 14; ++i
) {
126 dprintk(KERN_WARNING
"bad code: %x\n", org_code
);
130 dprintk(KERN_WARNING
"code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
131 "instr=%x\n", rc5
, org_code
, RC5_START(rc5
),
132 RC5_TOGGLE(rc5
), RC5_ADDR(rc5
), RC5_INSTR(rc5
));
136 static int bttv_rc5_irq(struct bttv
*btv
)
138 struct bttv_ir
*ir
= btv
->remote
;
142 unsigned long current_jiffies
, timeout
;
145 gpio
= bttv_gpio_read(&btv
->c
);
151 /* get time of bit */
152 current_jiffies
= jiffies
;
153 do_gettimeofday(&tv
);
155 /* avoid overflow with gap >1s */
156 if (tv
.tv_sec
- ir
->base_time
.tv_sec
> 1) {
159 gap
= 1000000 * (tv
.tv_sec
- ir
->base_time
.tv_sec
) +
160 tv
.tv_usec
- ir
->base_time
.tv_usec
;
163 /* active code => add bit */
165 /* only if in the code (otherwise spurious IRQ or timer
167 if (ir
->last_bit
< 28) {
168 ir
->last_bit
= (gap
- rc5_remote_gap
/ 2) /
170 ir
->code
|= 1 << ir
->last_bit
;
172 /* starting new code */
179 timeout
= current_jiffies
+ (500 + 30 * HZ
) / 1000;
180 mod_timer(&ir
->timer_end
, timeout
);
183 /* toggle GPIO pin 4 to reset the irq */
184 bttv_gpio_write(&btv
->c
, gpio
& ~(1 << 4));
185 bttv_gpio_write(&btv
->c
, gpio
| (1 << 4));
190 static void bttv_rc5_timer_end(unsigned long data
)
192 struct bttv_ir
*ir
= (struct bttv_ir
*)data
;
194 unsigned long current_jiffies
, timeout
;
198 current_jiffies
= jiffies
;
199 do_gettimeofday(&tv
);
201 /* avoid overflow with gap >1s */
202 if (tv
.tv_sec
- ir
->base_time
.tv_sec
> 1) {
205 gap
= 1000000 * (tv
.tv_sec
- ir
->base_time
.tv_sec
) +
206 tv
.tv_usec
- ir
->base_time
.tv_usec
;
209 /* Allow some timmer jitter (RC5 is ~24ms anyway so this is ok) */
211 dprintk(KERN_WARNING
"spurious timer_end\n");
216 if (ir
->last_bit
< 20) {
217 /* ignore spurious codes (caused by light/other remotes) */
218 dprintk(KERN_WARNING
"short code: %x\n", ir
->code
);
220 u32 rc5
= rc5_decode(ir
->code
);
222 /* two start bits? */
223 if (RC5_START(rc5
) != 3) {
224 dprintk(KERN_WARNING
"rc5 start bits invalid: %u\n", RC5_START(rc5
));
227 } else if (RC5_ADDR(rc5
) == 0x0) {
228 u32 toggle
= RC5_TOGGLE(rc5
);
229 u32 instr
= RC5_INSTR(rc5
);
231 /* Good code, decide if repeat/repress */
232 if (toggle
!= RC5_TOGGLE(ir
->last_rc5
) ||
233 instr
!= RC5_INSTR(ir
->last_rc5
)) {
234 dprintk(KERN_WARNING
"instruction %x, toggle %x\n", instr
,
236 ir_input_nokey(ir
->dev
, &ir
->ir
);
237 ir_input_keydown(ir
->dev
, &ir
->ir
, instr
,
241 /* Set/reset key-up timer */
242 timeout
= current_jiffies
+ (500 + rc5_key_timeout
244 mod_timer(&ir
->timer_keyup
, timeout
);
246 /* Save code for repeat test */
252 static void bttv_rc5_timer_keyup(unsigned long data
)
254 struct bttv_ir
*ir
= (struct bttv_ir
*)data
;
256 dprintk(KERN_DEBUG
"key released\n");
257 ir_input_nokey(ir
->dev
, &ir
->ir
);
260 /* ---------------------------------------------------------------------- */
262 static void bttv_ir_start(struct bttv
*btv
, struct bttv_ir
*ir
)
265 init_timer(&ir
->timer
);
266 ir
->timer
.function
= bttv_input_timer
;
267 ir
->timer
.data
= (unsigned long)btv
;
268 ir
->timer
.expires
= jiffies
+ HZ
;
269 add_timer(&ir
->timer
);
270 } else if (ir
->rc5_gpio
) {
271 /* set timer_end for code completion */
272 init_timer(&ir
->timer_end
);
273 ir
->timer_end
.function
= bttv_rc5_timer_end
;
274 ir
->timer_end
.data
= (unsigned long)ir
;
276 init_timer(&ir
->timer_keyup
);
277 ir
->timer_keyup
.function
= bttv_rc5_timer_keyup
;
278 ir
->timer_keyup
.data
= (unsigned long)ir
;
282 static void bttv_ir_stop(struct bttv
*btv
)
284 if (btv
->remote
->polling
) {
285 del_timer_sync(&btv
->remote
->timer
);
286 flush_scheduled_work();
289 if (btv
->remote
->rc5_gpio
) {
292 del_timer_sync(&btv
->remote
->timer_end
);
293 flush_scheduled_work();
295 gpio
= bttv_gpio_read(&btv
->c
);
296 bttv_gpio_write(&btv
->c
, gpio
& ~(1 << 4));
300 int bttv_input_init(struct bttv
*btv
)
303 IR_KEYTAB_TYPE
*ir_codes
= NULL
;
304 struct input_dev
*input_dev
;
305 int ir_type
= IR_TYPE_OTHER
;
308 if (!btv
->has_remote
)
311 ir
= kzalloc(sizeof(*ir
),GFP_KERNEL
);
312 input_dev
= input_allocate_device();
313 if (!ir
|| !input_dev
)
316 /* detect & configure */
317 switch (btv
->c
.type
) {
318 case BTTV_BOARD_AVERMEDIA
:
319 case BTTV_BOARD_AVPHONE98
:
320 case BTTV_BOARD_AVERMEDIA98
:
321 ir_codes
= ir_codes_avermedia
;
322 ir
->mask_keycode
= 0xf88000;
323 ir
->mask_keydown
= 0x010000;
324 ir
->polling
= 50; // ms
327 case BTTV_BOARD_AVDVBT_761
:
328 case BTTV_BOARD_AVDVBT_771
:
329 ir_codes
= ir_codes_avermedia_dvbt
;
330 ir
->mask_keycode
= 0x0f00c0;
331 ir
->mask_keydown
= 0x000020;
332 ir
->polling
= 50; // ms
335 case BTTV_BOARD_PXELVWPLTVPAK
:
336 ir_codes
= ir_codes_pixelview
;
337 ir
->mask_keycode
= 0x003e00;
338 ir
->mask_keyup
= 0x010000;
339 ir
->polling
= 50; // ms
341 case BTTV_BOARD_PV_M4900
:
342 case BTTV_BOARD_PV_BT878P_9B
:
343 case BTTV_BOARD_PV_BT878P_PLUS
:
344 ir_codes
= ir_codes_pixelview
;
345 ir
->mask_keycode
= 0x001f00;
346 ir
->mask_keyup
= 0x008000;
347 ir
->polling
= 50; // ms
350 case BTTV_BOARD_WINFAST2000
:
351 ir_codes
= ir_codes_winfast
;
352 ir
->mask_keycode
= 0x1f8;
354 case BTTV_BOARD_MAGICTVIEW061
:
355 case BTTV_BOARD_MAGICTVIEW063
:
356 ir_codes
= ir_codes_winfast
;
357 ir
->mask_keycode
= 0x0008e000;
358 ir
->mask_keydown
= 0x00200000;
360 case BTTV_BOARD_APAC_VIEWCOMP
:
361 ir_codes
= ir_codes_apac_viewcomp
;
362 ir
->mask_keycode
= 0x001f00;
363 ir
->mask_keyup
= 0x008000;
364 ir
->polling
= 50; // ms
366 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2
:
367 case BTTV_BOARD_CONTVFMI
:
368 ir_codes
= ir_codes_pixelview
;
369 ir
->mask_keycode
= 0x001F00;
370 ir
->mask_keyup
= 0x006000;
371 ir
->polling
= 50; // ms
373 case BTTV_BOARD_NEBULA_DIGITV
:
374 ir_codes
= ir_codes_nebula
;
375 btv
->custom_irq
= bttv_rc5_irq
;
378 case BTTV_BOARD_MACHTV_MAGICTV
:
379 ir_codes
= ir_codes_apac_viewcomp
;
380 ir
->mask_keycode
= 0x001F00;
381 ir
->mask_keyup
= 0x004000;
382 ir
->polling
= 50; /* ms */
385 if (NULL
== ir_codes
) {
386 dprintk(KERN_INFO
"Ooops: IR config error [card=%d]\n", btv
->c
.type
);
393 /* enable remote irq */
394 bttv_gpio_inout(&btv
->c
, (1 << 4), 1 << 4);
395 gpio
= bttv_gpio_read(&btv
->c
);
396 bttv_gpio_write(&btv
->c
, gpio
& ~(1 << 4));
397 bttv_gpio_write(&btv
->c
, gpio
| (1 << 4));
399 /* init hardware-specific stuff */
400 bttv_gpio_inout(&btv
->c
, ir
->mask_keycode
| ir
->mask_keydown
, 0);
403 /* init input device */
406 snprintf(ir
->name
, sizeof(ir
->name
), "bttv IR (card=%d)",
408 snprintf(ir
->phys
, sizeof(ir
->phys
), "pci-%s/ir0",
409 pci_name(btv
->c
.pci
));
411 ir_input_init(input_dev
, &ir
->ir
, ir_type
, ir_codes
);
412 input_dev
->name
= ir
->name
;
413 input_dev
->phys
= ir
->phys
;
414 input_dev
->id
.bustype
= BUS_PCI
;
415 input_dev
->id
.version
= 1;
416 if (btv
->c
.pci
->subsystem_vendor
) {
417 input_dev
->id
.vendor
= btv
->c
.pci
->subsystem_vendor
;
418 input_dev
->id
.product
= btv
->c
.pci
->subsystem_device
;
420 input_dev
->id
.vendor
= btv
->c
.pci
->vendor
;
421 input_dev
->id
.product
= btv
->c
.pci
->device
;
423 input_dev
->cdev
.dev
= &btv
->c
.pci
->dev
;
426 bttv_ir_start(btv
, ir
);
429 err
= input_register_device(btv
->remote
->dev
);
433 /* the remote isn't as bouncy as a keyboard */
434 ir
->dev
->rep
[REP_DELAY
] = repeat_delay
;
435 ir
->dev
->rep
[REP_PERIOD
] = repeat_period
;
443 input_free_device(input_dev
);
448 void bttv_input_fini(struct bttv
*btv
)
450 if (btv
->remote
== NULL
)
454 input_unregister_device(btv
->remote
->dev
);