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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/interrupt.h>
27 #include <linux/input.h>
28 #include <linux/slab.h>
35 module_param(ir_debug
, int, 0644);
37 static int ir_rc5_remote_gap
= 885;
38 module_param(ir_rc5_remote_gap
, int, 0644);
41 #define dprintk(fmt, ...) \
44 pr_info(fmt, ##__VA_ARGS__); \
47 #define DEVNAME "bttv-input"
49 #define MODULE_NAME "bttv"
51 /* ---------------------------------------------------------------------- */
53 static void ir_handle_key(struct bttv
*btv
)
55 struct bttv_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("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
&& (gpio
& ir
->mask_keydown
)) ||
75 (ir
->mask_keyup
&& !(gpio
& ir
->mask_keyup
))) {
76 rc_keydown_notimeout(ir
->dev
, data
, 0);
78 /* HACK: Probably, ir->mask_keydown is missing
80 if (btv
->c
.type
== BTTV_BOARD_WINFAST2000
)
81 rc_keydown_notimeout(ir
->dev
, data
, 0);
87 static void ir_enltv_handle_key(struct bttv
*btv
)
89 struct bttv_ir
*ir
= btv
->remote
;
90 u32 gpio
, data
, keyup
;
93 gpio
= bttv_gpio_read(&btv
->c
);
96 data
= ir_extract_bits(gpio
, ir
->mask_keycode
);
98 /* Check if it is keyup */
99 keyup
= (gpio
& ir
->mask_keyup
) ? 1 << 31 : 0;
101 if ((ir
->last_gpio
& 0x7f) != data
) {
102 dprintk("gpio=0x%x code=%d | %s\n",
104 (gpio
& ir
->mask_keyup
) ? " up" : "up/down");
106 rc_keydown_notimeout(ir
->dev
, data
, 0);
110 if ((ir
->last_gpio
& 1 << 31) == keyup
)
113 dprintk("(cnt) gpio=0x%x code=%d | %s\n",
115 (gpio
& ir
->mask_keyup
) ? " up" : "down");
120 rc_keydown_notimeout(ir
->dev
, data
, 0);
123 ir
->last_gpio
= data
| keyup
;
126 static int bttv_rc5_irq(struct bttv
*btv
);
128 void bttv_input_irq(struct bttv
*btv
)
130 struct bttv_ir
*ir
= btv
->remote
;
134 else if (!ir
->polling
)
138 static void bttv_input_timer(unsigned long data
)
140 struct bttv
*btv
= (struct bttv
*)data
;
141 struct bttv_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
));
151 * FIXME: Nebula digi uses the legacy way to decode RC5, instead of relying
152 * on the rc-core way. As we need to be sure that both IRQ transitions are
153 * properly triggered, Better to touch it only with this hardware for
157 #define RC5_START(x) (((x) >> 12) & 3)
158 #define RC5_TOGGLE(x) (((x) >> 11) & 1)
159 #define RC5_ADDR(x) (((x) >> 6) & 31)
160 #define RC5_INSTR(x) ((x) & 63)
162 /* decode raw bit pattern to RC5 code */
163 static u32
bttv_rc5_decode(unsigned int code
)
165 unsigned int org_code
= code
;
167 unsigned int rc5
= 0;
170 for (i
= 0; i
< 14; ++i
) {
183 dprintk("rc5_decode(%x) bad code\n",
188 dprintk("code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
189 "instr=%x\n", rc5
, org_code
, RC5_START(rc5
),
190 RC5_TOGGLE(rc5
), RC5_ADDR(rc5
), RC5_INSTR(rc5
));
194 static void bttv_rc5_timer_end(unsigned long data
)
196 struct bttv_ir
*ir
= (struct bttv_ir
*)data
;
202 do_gettimeofday(&tv
);
204 /* avoid overflow with gap >1s */
205 if (tv
.tv_sec
- ir
->base_time
.tv_sec
> 1) {
208 gap
= 1000000 * (tv
.tv_sec
- ir
->base_time
.tv_sec
) +
209 tv
.tv_usec
- ir
->base_time
.tv_usec
;
212 /* signal we're ready to start a new code */
215 /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
217 dprintk("spurious timer_end\n");
221 if (ir
->last_bit
< 20) {
222 /* ignore spurious codes (caused by light/other remotes) */
223 dprintk("short code: %x\n", ir
->code
);
225 ir
->code
= (ir
->code
<< ir
->shift_by
) | 1;
226 rc5
= bttv_rc5_decode(ir
->code
);
228 /* two start bits? */
229 if (RC5_START(rc5
) != ir
->start
) {
231 " rc5 start bits invalid: %u\n", RC5_START(rc5
));
234 } else if (RC5_ADDR(rc5
) == ir
->addr
) {
235 u32 toggle
= RC5_TOGGLE(rc5
);
236 u32 instr
= RC5_INSTR(rc5
);
239 rc_keydown(ir
->dev
, instr
, toggle
);
240 dprintk("instruction %x, toggle %x\n",
246 static int bttv_rc5_irq(struct bttv
*btv
)
248 struct bttv_ir
*ir
= btv
->remote
;
252 unsigned long current_jiffies
;
255 gpio
= bttv_gpio_read(&btv
->c
);
257 /* get time of bit */
258 current_jiffies
= jiffies
;
259 do_gettimeofday(&tv
);
261 /* avoid overflow with gap >1s */
262 if (tv
.tv_sec
- ir
->base_time
.tv_sec
> 1) {
265 gap
= 1000000 * (tv
.tv_sec
- ir
->base_time
.tv_sec
) +
266 tv
.tv_usec
- ir
->base_time
.tv_usec
;
269 dprintk("RC5 IRQ: gap %d us for %s\n",
270 gap
, (gpio
& 0x20) ? "mark" : "space");
276 /* active code => add bit */
278 /* only if in the code (otherwise spurious IRQ or timer
280 if (ir
->last_bit
< 28) {
281 ir
->last_bit
= (gap
- ir_rc5_remote_gap
/ 2) /
283 ir
->code
|= 1 << ir
->last_bit
;
285 /* starting new code */
292 mod_timer(&ir
->timer
, current_jiffies
+ msecs_to_jiffies(30));
295 /* toggle GPIO pin 4 to reset the irq */
296 bttv_gpio_write(&btv
->c
, gpio
& ~(1 << 4));
297 bttv_gpio_write(&btv
->c
, gpio
| (1 << 4));
301 /* ---------------------------------------------------------------------- */
303 static void bttv_ir_start(struct bttv
*btv
, struct bttv_ir
*ir
)
306 setup_timer(&ir
->timer
, bttv_input_timer
, (unsigned long)btv
);
307 ir
->timer
.expires
= jiffies
+ msecs_to_jiffies(1000);
308 add_timer(&ir
->timer
);
309 } else if (ir
->rc5_gpio
) {
310 /* set timer_end for code completion */
311 setup_timer(&ir
->timer
, bttv_rc5_timer_end
, (unsigned long)ir
);
315 ir
->rc5_remote_gap
= ir_rc5_remote_gap
;
319 static void bttv_ir_stop(struct bttv
*btv
)
321 if (btv
->remote
->polling
)
322 del_timer_sync(&btv
->remote
->timer
);
324 if (btv
->remote
->rc5_gpio
) {
327 del_timer_sync(&btv
->remote
->timer
);
329 gpio
= bttv_gpio_read(&btv
->c
);
330 bttv_gpio_write(&btv
->c
, gpio
& ~(1 << 4));
335 * Get_key functions used by I2C remotes
338 static int get_key_pv951(struct IR_i2c
*ir
, u32
*ir_key
, u32
*ir_raw
)
343 if (1 != i2c_master_recv(ir
->c
, &b
, 1)) {
344 dprintk("read error\n");
351 dprintk("key %02x\n", b
);
355 * lirc_i2c maps the pv951 code as:
357 * cmd = bit_reverse (b)
358 * So, it seems that this device uses NEC extended
359 * I decided to not fix the table, due to two reasons:
360 * 1) Without the actual device, this is only a guess;
361 * 2) As the addr is not reported via I2C, nor can be changed,
362 * the device is bound to the vendor-provided RC.
370 /* Instantiate the I2C IR receiver device, if present */
371 void init_bttv_i2c_ir(struct bttv
*btv
)
373 const unsigned short addr_list
[] = {
374 0x1a, 0x18, 0x64, 0x30, 0x71,
377 struct i2c_board_info info
;
378 struct i2c_client
*i2c_dev
;
380 if (0 != btv
->i2c_rc
)
383 memset(&info
, 0, sizeof(struct i2c_board_info
));
384 memset(&btv
->init_data
, 0, sizeof(btv
->init_data
));
385 strlcpy(info
.type
, "ir_video", I2C_NAME_SIZE
);
387 switch (btv
->c
.type
) {
388 case BTTV_BOARD_PV951
:
389 btv
->init_data
.name
= "PV951";
390 btv
->init_data
.get_key
= get_key_pv951
;
391 btv
->init_data
.ir_codes
= RC_MAP_PV951
;
396 if (btv
->init_data
.name
) {
397 info
.platform_data
= &btv
->init_data
;
398 i2c_dev
= i2c_new_device(&btv
->c
.i2c_adap
, &info
);
401 * The external IR receiver is at i2c address 0x34 (0x35 for
402 * reads). Future Hauppauge cards will have an internal
403 * receiver at 0x30 (0x31 for reads). In theory, both can be
404 * fitted, and Hauppauge suggest an external overrides an
406 * That's why we probe 0x1a (~0x34) first. CB
408 i2c_dev
= i2c_new_probed_device(&btv
->c
.i2c_adap
, &info
, addr_list
, NULL
);
413 #if defined(CONFIG_MODULES) && defined(MODULE)
414 request_module("ir-kbd-i2c");
418 int bttv_input_init(struct bttv
*btv
)
421 char *ir_codes
= NULL
;
425 if (!btv
->has_remote
)
428 ir
= kzalloc(sizeof(*ir
),GFP_KERNEL
);
429 rc
= rc_allocate_device();
433 /* detect & configure */
434 switch (btv
->c
.type
) {
435 case BTTV_BOARD_AVERMEDIA
:
436 case BTTV_BOARD_AVPHONE98
:
437 case BTTV_BOARD_AVERMEDIA98
:
438 ir_codes
= RC_MAP_AVERMEDIA
;
439 ir
->mask_keycode
= 0xf88000;
440 ir
->mask_keydown
= 0x010000;
441 ir
->polling
= 50; // ms
444 case BTTV_BOARD_AVDVBT_761
:
445 case BTTV_BOARD_AVDVBT_771
:
446 ir_codes
= RC_MAP_AVERMEDIA_DVBT
;
447 ir
->mask_keycode
= 0x0f00c0;
448 ir
->mask_keydown
= 0x000020;
449 ir
->polling
= 50; // ms
452 case BTTV_BOARD_PXELVWPLTVPAK
:
453 ir_codes
= RC_MAP_PIXELVIEW
;
454 ir
->mask_keycode
= 0x003e00;
455 ir
->mask_keyup
= 0x010000;
456 ir
->polling
= 50; // ms
458 case BTTV_BOARD_PV_M4900
:
459 case BTTV_BOARD_PV_BT878P_9B
:
460 case BTTV_BOARD_PV_BT878P_PLUS
:
461 ir_codes
= RC_MAP_PIXELVIEW
;
462 ir
->mask_keycode
= 0x001f00;
463 ir
->mask_keyup
= 0x008000;
464 ir
->polling
= 50; // ms
467 case BTTV_BOARD_WINFAST2000
:
468 ir_codes
= RC_MAP_WINFAST
;
469 ir
->mask_keycode
= 0x1f8;
471 case BTTV_BOARD_MAGICTVIEW061
:
472 case BTTV_BOARD_MAGICTVIEW063
:
473 ir_codes
= RC_MAP_WINFAST
;
474 ir
->mask_keycode
= 0x0008e000;
475 ir
->mask_keydown
= 0x00200000;
477 case BTTV_BOARD_APAC_VIEWCOMP
:
478 ir_codes
= RC_MAP_APAC_VIEWCOMP
;
479 ir
->mask_keycode
= 0x001f00;
480 ir
->mask_keyup
= 0x008000;
481 ir
->polling
= 50; // ms
483 case BTTV_BOARD_ASKEY_CPH03X
:
484 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2
:
485 case BTTV_BOARD_CONTVFMI
:
486 ir_codes
= RC_MAP_PIXELVIEW
;
487 ir
->mask_keycode
= 0x001F00;
488 ir
->mask_keyup
= 0x006000;
489 ir
->polling
= 50; // ms
491 case BTTV_BOARD_NEBULA_DIGITV
:
492 ir_codes
= RC_MAP_NEBULA
;
495 case BTTV_BOARD_MACHTV_MAGICTV
:
496 ir_codes
= RC_MAP_APAC_VIEWCOMP
;
497 ir
->mask_keycode
= 0x001F00;
498 ir
->mask_keyup
= 0x004000;
499 ir
->polling
= 50; /* ms */
501 case BTTV_BOARD_KOZUMI_KTV_01C
:
502 ir_codes
= RC_MAP_PCTV_SEDNA
;
503 ir
->mask_keycode
= 0x001f00;
504 ir
->mask_keyup
= 0x006000;
505 ir
->polling
= 50; /* ms */
507 case BTTV_BOARD_ENLTV_FM_2
:
508 ir_codes
= RC_MAP_ENCORE_ENLTV2
;
509 ir
->mask_keycode
= 0x00fd00;
510 ir
->mask_keyup
= 0x000080;
511 ir
->polling
= 1; /* ms */
512 ir
->last_gpio
= ir_extract_bits(bttv_gpio_read(&btv
->c
),
516 if (NULL
== ir_codes
) {
517 dprintk("Ooops: IR config error [card=%d]\n", btv
->c
.type
);
524 /* enable remote irq */
525 bttv_gpio_inout(&btv
->c
, (1 << 4), 1 << 4);
526 gpio
= bttv_gpio_read(&btv
->c
);
527 bttv_gpio_write(&btv
->c
, gpio
& ~(1 << 4));
528 bttv_gpio_write(&btv
->c
, gpio
| (1 << 4));
530 /* init hardware-specific stuff */
531 bttv_gpio_inout(&btv
->c
, ir
->mask_keycode
| ir
->mask_keydown
, 0);
534 /* init input device */
537 snprintf(ir
->name
, sizeof(ir
->name
), "bttv IR (card=%d)",
539 snprintf(ir
->phys
, sizeof(ir
->phys
), "pci-%s/ir0",
540 pci_name(btv
->c
.pci
));
542 rc
->input_name
= ir
->name
;
543 rc
->input_phys
= ir
->phys
;
544 rc
->input_id
.bustype
= BUS_PCI
;
545 rc
->input_id
.version
= 1;
546 if (btv
->c
.pci
->subsystem_vendor
) {
547 rc
->input_id
.vendor
= btv
->c
.pci
->subsystem_vendor
;
548 rc
->input_id
.product
= btv
->c
.pci
->subsystem_device
;
550 rc
->input_id
.vendor
= btv
->c
.pci
->vendor
;
551 rc
->input_id
.product
= btv
->c
.pci
->device
;
553 rc
->dev
.parent
= &btv
->c
.pci
->dev
;
554 rc
->map_name
= ir_codes
;
555 rc
->driver_name
= MODULE_NAME
;
558 bttv_ir_start(btv
, ir
);
561 err
= rc_register_device(rc
);
576 void bttv_input_fini(struct bttv
*btv
)
578 if (btv
->remote
== NULL
)
582 rc_unregister_device(btv
->remote
->dev
);