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 #define MODULE_NAME "bttv"
54 /* ---------------------------------------------------------------------- */
56 static void ir_handle_key(struct bttv
*btv
)
58 struct card_ir
*ir
= btv
->remote
;
62 gpio
= bttv_gpio_read(&btv
->c
);
64 if (ir
->last_gpio
== gpio
)
70 data
= ir_extract_bits(gpio
, ir
->mask_keycode
);
71 dprintk(KERN_INFO DEVNAME
": irq gpio=0x%x code=%d | %s%s%s\n",
73 ir
->polling
? "poll" : "irq",
74 (gpio
& ir
->mask_keydown
) ? " down" : "",
75 (gpio
& ir
->mask_keyup
) ? " up" : "");
77 if ((ir
->mask_keydown
&& (0 != (gpio
& ir
->mask_keydown
))) ||
78 (ir
->mask_keyup
&& (0 == (gpio
& ir
->mask_keyup
)))) {
79 ir_input_keydown(ir
->dev
, &ir
->ir
, data
);
81 /* HACK: Probably, ir->mask_keydown is missing
83 if (btv
->c
.type
== BTTV_BOARD_WINFAST2000
)
84 ir_input_keydown(ir
->dev
, &ir
->ir
, data
);
86 ir_input_nokey(ir
->dev
,&ir
->ir
);
91 static void ir_enltv_handle_key(struct bttv
*btv
)
93 struct card_ir
*ir
= btv
->remote
;
94 u32 gpio
, data
, keyup
;
97 gpio
= bttv_gpio_read(&btv
->c
);
100 data
= ir_extract_bits(gpio
, ir
->mask_keycode
);
102 /* Check if it is keyup */
103 keyup
= (gpio
& ir
->mask_keyup
) ? 1 << 31 : 0;
105 if ((ir
->last_gpio
& 0x7f) != data
) {
106 dprintk(KERN_INFO DEVNAME
": gpio=0x%x code=%d | %s\n",
108 (gpio
& ir
->mask_keyup
) ? " up" : "up/down");
110 ir_input_keydown(ir
->dev
, &ir
->ir
, data
);
112 ir_input_nokey(ir
->dev
, &ir
->ir
);
114 if ((ir
->last_gpio
& 1 << 31) == keyup
)
117 dprintk(KERN_INFO DEVNAME
":(cnt) gpio=0x%x code=%d | %s\n",
119 (gpio
& ir
->mask_keyup
) ? " up" : "down");
122 ir_input_nokey(ir
->dev
, &ir
->ir
);
124 ir_input_keydown(ir
->dev
, &ir
->ir
, data
);
127 ir
->last_gpio
= data
| keyup
;
130 void bttv_input_irq(struct bttv
*btv
)
132 struct card_ir
*ir
= btv
->remote
;
138 static void bttv_input_timer(unsigned long data
)
140 struct bttv
*btv
= (struct bttv
*)data
;
141 struct card_ir
*ir
= btv
->remote
;
143 if (btv
->c
.type
== BTTV_BOARD_ENLTV_FM_2
)
144 ir_enltv_handle_key(btv
);
147 mod_timer(&ir
->timer
, jiffies
+ msecs_to_jiffies(ir
->polling
));
150 /* ---------------------------------------------------------------*/
152 static int bttv_rc5_irq(struct bttv
*btv
)
154 struct card_ir
*ir
= btv
->remote
;
158 unsigned long current_jiffies
;
161 gpio
= bttv_gpio_read(&btv
->c
);
167 /* get time of bit */
168 current_jiffies
= jiffies
;
169 do_gettimeofday(&tv
);
171 /* avoid overflow with gap >1s */
172 if (tv
.tv_sec
- ir
->base_time
.tv_sec
> 1) {
175 gap
= 1000000 * (tv
.tv_sec
- ir
->base_time
.tv_sec
) +
176 tv
.tv_usec
- ir
->base_time
.tv_usec
;
179 /* active code => add bit */
181 /* only if in the code (otherwise spurious IRQ or timer
183 if (ir
->last_bit
< 28) {
184 ir
->last_bit
= (gap
- ir_rc5_remote_gap
/ 2) /
186 ir
->code
|= 1 << ir
->last_bit
;
188 /* starting new code */
195 mod_timer(&ir
->timer_end
,
196 current_jiffies
+ msecs_to_jiffies(30));
199 /* toggle GPIO pin 4 to reset the irq */
200 bttv_gpio_write(&btv
->c
, gpio
& ~(1 << 4));
201 bttv_gpio_write(&btv
->c
, gpio
| (1 << 4));
205 /* ---------------------------------------------------------------------- */
207 static void bttv_ir_start(struct bttv
*btv
, struct card_ir
*ir
)
210 setup_timer(&ir
->timer
, bttv_input_timer
, (unsigned long)btv
);
211 ir
->timer
.expires
= jiffies
+ msecs_to_jiffies(1000);
212 add_timer(&ir
->timer
);
213 } else if (ir
->rc5_gpio
) {
214 /* set timer_end for code completion */
215 init_timer(&ir
->timer_end
);
216 ir
->timer_end
.function
= ir_rc5_timer_end
;
217 ir
->timer_end
.data
= (unsigned long)ir
;
219 init_timer(&ir
->timer_keyup
);
220 ir
->timer_keyup
.function
= ir_rc5_timer_keyup
;
221 ir
->timer_keyup
.data
= (unsigned long)ir
;
225 ir
->rc5_key_timeout
= ir_rc5_key_timeout
;
226 ir
->rc5_remote_gap
= ir_rc5_remote_gap
;
230 static void bttv_ir_stop(struct bttv
*btv
)
232 if (btv
->remote
->polling
) {
233 del_timer_sync(&btv
->remote
->timer
);
234 flush_scheduled_work();
237 if (btv
->remote
->rc5_gpio
) {
240 del_timer_sync(&btv
->remote
->timer_end
);
241 flush_scheduled_work();
243 gpio
= bttv_gpio_read(&btv
->c
);
244 bttv_gpio_write(&btv
->c
, gpio
& ~(1 << 4));
249 * Get_key functions used by I2C remotes
252 static int get_key_pv951(struct IR_i2c
*ir
, u32
*ir_key
, u32
*ir_raw
)
257 if (1 != i2c_master_recv(ir
->c
, &b
, 1)) {
258 dprintk(KERN_INFO DEVNAME
": read error\n");
265 dprintk(KERN_INFO DEVNAME
": key %02x\n", b
);
272 /* Instantiate the I2C IR receiver device, if present */
273 void __devinit
init_bttv_i2c_ir(struct bttv
*btv
)
275 const unsigned short addr_list
[] = {
276 0x1a, 0x18, 0x64, 0x30, 0x71,
279 struct i2c_board_info info
;
281 if (0 != btv
->i2c_rc
)
284 memset(&info
, 0, sizeof(struct i2c_board_info
));
285 memset(&btv
->init_data
, 0, sizeof(btv
->init_data
));
286 strlcpy(info
.type
, "ir_video", I2C_NAME_SIZE
);
288 switch (btv
->c
.type
) {
289 case BTTV_BOARD_PV951
:
290 btv
->init_data
.name
= "PV951";
291 btv
->init_data
.get_key
= get_key_pv951
;
292 btv
->init_data
.ir_codes
= RC_MAP_PV951
;
293 btv
->init_data
.type
= IR_TYPE_OTHER
;
298 * The external IR receiver is at i2c address 0x34 (0x35 for
299 * reads). Future Hauppauge cards will have an internal
300 * receiver at 0x30 (0x31 for reads). In theory, both can be
301 * fitted, and Hauppauge suggest an external overrides an
303 * That's why we probe 0x1a (~0x34) first. CB
306 i2c_new_probed_device(&btv
->c
.i2c_adap
, &info
, addr_list
, NULL
);
310 if (btv
->init_data
.name
)
311 info
.platform_data
= &btv
->init_data
;
312 i2c_new_device(&btv
->c
.i2c_adap
, &info
);
317 int __devexit
fini_bttv_i2c(struct bttv
*btv
)
319 if (0 != btv
->i2c_rc
)
322 return i2c_del_adapter(&btv
->c
.i2c_adap
);
325 int bttv_input_init(struct bttv
*btv
)
328 char *ir_codes
= NULL
;
329 struct input_dev
*input_dev
;
330 u64 ir_type
= IR_TYPE_OTHER
;
333 if (!btv
->has_remote
)
336 ir
= kzalloc(sizeof(*ir
),GFP_KERNEL
);
337 input_dev
= input_allocate_device();
338 if (!ir
|| !input_dev
)
341 /* detect & configure */
342 switch (btv
->c
.type
) {
343 case BTTV_BOARD_AVERMEDIA
:
344 case BTTV_BOARD_AVPHONE98
:
345 case BTTV_BOARD_AVERMEDIA98
:
346 ir_codes
= RC_MAP_AVERMEDIA
;
347 ir
->mask_keycode
= 0xf88000;
348 ir
->mask_keydown
= 0x010000;
349 ir
->polling
= 50; // ms
352 case BTTV_BOARD_AVDVBT_761
:
353 case BTTV_BOARD_AVDVBT_771
:
354 ir_codes
= RC_MAP_AVERMEDIA_DVBT
;
355 ir
->mask_keycode
= 0x0f00c0;
356 ir
->mask_keydown
= 0x000020;
357 ir
->polling
= 50; // ms
360 case BTTV_BOARD_PXELVWPLTVPAK
:
361 ir_codes
= RC_MAP_PIXELVIEW
;
362 ir
->mask_keycode
= 0x003e00;
363 ir
->mask_keyup
= 0x010000;
364 ir
->polling
= 50; // ms
366 case BTTV_BOARD_PV_M4900
:
367 case BTTV_BOARD_PV_BT878P_9B
:
368 case BTTV_BOARD_PV_BT878P_PLUS
:
369 ir_codes
= RC_MAP_PIXELVIEW
;
370 ir
->mask_keycode
= 0x001f00;
371 ir
->mask_keyup
= 0x008000;
372 ir
->polling
= 50; // ms
375 case BTTV_BOARD_WINFAST2000
:
376 ir_codes
= RC_MAP_WINFAST
;
377 ir
->mask_keycode
= 0x1f8;
379 case BTTV_BOARD_MAGICTVIEW061
:
380 case BTTV_BOARD_MAGICTVIEW063
:
381 ir_codes
= RC_MAP_WINFAST
;
382 ir
->mask_keycode
= 0x0008e000;
383 ir
->mask_keydown
= 0x00200000;
385 case BTTV_BOARD_APAC_VIEWCOMP
:
386 ir_codes
= RC_MAP_APAC_VIEWCOMP
;
387 ir
->mask_keycode
= 0x001f00;
388 ir
->mask_keyup
= 0x008000;
389 ir
->polling
= 50; // ms
391 case BTTV_BOARD_ASKEY_CPH03X
:
392 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2
:
393 case BTTV_BOARD_CONTVFMI
:
394 ir_codes
= RC_MAP_PIXELVIEW
;
395 ir
->mask_keycode
= 0x001F00;
396 ir
->mask_keyup
= 0x006000;
397 ir
->polling
= 50; // ms
399 case BTTV_BOARD_NEBULA_DIGITV
:
400 ir_codes
= RC_MAP_NEBULA
;
401 btv
->custom_irq
= bttv_rc5_irq
;
404 case BTTV_BOARD_MACHTV_MAGICTV
:
405 ir_codes
= RC_MAP_APAC_VIEWCOMP
;
406 ir
->mask_keycode
= 0x001F00;
407 ir
->mask_keyup
= 0x004000;
408 ir
->polling
= 50; /* ms */
410 case BTTV_BOARD_KOZUMI_KTV_01C
:
411 ir_codes
= RC_MAP_PCTV_SEDNA
;
412 ir
->mask_keycode
= 0x001f00;
413 ir
->mask_keyup
= 0x006000;
414 ir
->polling
= 50; /* ms */
416 case BTTV_BOARD_ENLTV_FM_2
:
417 ir_codes
= RC_MAP_ENCORE_ENLTV2
;
418 ir
->mask_keycode
= 0x00fd00;
419 ir
->mask_keyup
= 0x000080;
420 ir
->polling
= 1; /* ms */
421 ir
->last_gpio
= ir_extract_bits(bttv_gpio_read(&btv
->c
),
425 if (NULL
== ir_codes
) {
426 dprintk(KERN_INFO
"Ooops: IR config error [card=%d]\n", btv
->c
.type
);
433 /* enable remote irq */
434 bttv_gpio_inout(&btv
->c
, (1 << 4), 1 << 4);
435 gpio
= bttv_gpio_read(&btv
->c
);
436 bttv_gpio_write(&btv
->c
, gpio
& ~(1 << 4));
437 bttv_gpio_write(&btv
->c
, gpio
| (1 << 4));
439 /* init hardware-specific stuff */
440 bttv_gpio_inout(&btv
->c
, ir
->mask_keycode
| ir
->mask_keydown
, 0);
443 /* init input device */
446 snprintf(ir
->name
, sizeof(ir
->name
), "bttv IR (card=%d)",
448 snprintf(ir
->phys
, sizeof(ir
->phys
), "pci-%s/ir0",
449 pci_name(btv
->c
.pci
));
451 err
= ir_input_init(input_dev
, &ir
->ir
, ir_type
);
455 input_dev
->name
= ir
->name
;
456 input_dev
->phys
= ir
->phys
;
457 input_dev
->id
.bustype
= BUS_PCI
;
458 input_dev
->id
.version
= 1;
459 if (btv
->c
.pci
->subsystem_vendor
) {
460 input_dev
->id
.vendor
= btv
->c
.pci
->subsystem_vendor
;
461 input_dev
->id
.product
= btv
->c
.pci
->subsystem_device
;
463 input_dev
->id
.vendor
= btv
->c
.pci
->vendor
;
464 input_dev
->id
.product
= btv
->c
.pci
->device
;
466 input_dev
->dev
.parent
= &btv
->c
.pci
->dev
;
469 bttv_ir_start(btv
, ir
);
472 err
= ir_input_register(btv
->remote
->dev
, ir_codes
, NULL
, MODULE_NAME
);
476 /* the remote isn't as bouncy as a keyboard */
477 ir
->dev
->rep
[REP_DELAY
] = repeat_delay
;
478 ir
->dev
->rep
[REP_PERIOD
] = repeat_period
;
490 void bttv_input_fini(struct bttv
*btv
)
492 if (btv
->remote
== NULL
)
496 ir_input_unregister(btv
->remote
->dev
);