1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (c) 2003 Gerd Knorr
5 * Copyright (c) 2003 Pavel Machek
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/interrupt.h>
14 #include <linux/input.h>
15 #include <linux/slab.h>
22 module_param(ir_debug
, int, 0644);
24 static int ir_rc5_remote_gap
= 885;
25 module_param(ir_rc5_remote_gap
, int, 0644);
28 #define dprintk(fmt, ...) \
31 pr_info(fmt, ##__VA_ARGS__); \
34 #define DEVNAME "bttv-input"
36 #define MODULE_NAME "bttv"
38 /* ---------------------------------------------------------------------- */
40 static void ir_handle_key(struct bttv
*btv
)
42 struct bttv_ir
*ir
= btv
->remote
;
46 gpio
= bttv_gpio_read(&btv
->c
);
48 if (ir
->last_gpio
== gpio
)
54 data
= ir_extract_bits(gpio
, ir
->mask_keycode
);
55 dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
57 ir
->polling
? "poll" : "irq",
58 (gpio
& ir
->mask_keydown
) ? " down" : "",
59 (gpio
& ir
->mask_keyup
) ? " up" : "");
61 if ((ir
->mask_keydown
&& (gpio
& ir
->mask_keydown
)) ||
62 (ir
->mask_keyup
&& !(gpio
& ir
->mask_keyup
))) {
63 rc_keydown_notimeout(ir
->dev
, RC_PROTO_UNKNOWN
, data
, 0);
65 /* HACK: Probably, ir->mask_keydown is missing
67 if (btv
->c
.type
== BTTV_BOARD_WINFAST2000
)
68 rc_keydown_notimeout(ir
->dev
, RC_PROTO_UNKNOWN
, data
,
75 static void ir_enltv_handle_key(struct bttv
*btv
)
77 struct bttv_ir
*ir
= btv
->remote
;
78 u32 gpio
, data
, keyup
;
81 gpio
= bttv_gpio_read(&btv
->c
);
84 data
= ir_extract_bits(gpio
, ir
->mask_keycode
);
86 /* Check if it is keyup */
87 keyup
= (gpio
& ir
->mask_keyup
) ? 1UL << 31 : 0;
89 if ((ir
->last_gpio
& 0x7f) != data
) {
90 dprintk("gpio=0x%x code=%d | %s\n",
92 (gpio
& ir
->mask_keyup
) ? " up" : "up/down");
94 rc_keydown_notimeout(ir
->dev
, RC_PROTO_UNKNOWN
, data
, 0);
98 if ((ir
->last_gpio
& 1UL << 31) == keyup
)
101 dprintk("(cnt) gpio=0x%x code=%d | %s\n",
103 (gpio
& ir
->mask_keyup
) ? " up" : "down");
108 rc_keydown_notimeout(ir
->dev
, RC_PROTO_UNKNOWN
, data
,
112 ir
->last_gpio
= data
| keyup
;
115 static int bttv_rc5_irq(struct bttv
*btv
);
117 void bttv_input_irq(struct bttv
*btv
)
119 struct bttv_ir
*ir
= btv
->remote
;
123 else if (!ir
->polling
)
127 static void bttv_input_timer(struct timer_list
*t
)
129 struct bttv_ir
*ir
= from_timer(ir
, t
, timer
);
130 struct bttv
*btv
= ir
->btv
;
132 if (btv
->c
.type
== BTTV_BOARD_ENLTV_FM_2
)
133 ir_enltv_handle_key(btv
);
136 mod_timer(&ir
->timer
, jiffies
+ msecs_to_jiffies(ir
->polling
));
140 * FIXME: Nebula digi uses the legacy way to decode RC5, instead of relying
141 * on the rc-core way. As we need to be sure that both IRQ transitions are
142 * properly triggered, Better to touch it only with this hardware for
146 #define RC5_START(x) (((x) >> 12) & 0x03)
147 #define RC5_TOGGLE(x) (((x) >> 11) & 0x01)
148 #define RC5_ADDR(x) (((x) >> 6) & 0x1f)
149 #define RC5_INSTR(x) (((x) >> 0) & 0x3f)
151 /* decode raw bit pattern to RC5 code */
152 static u32
bttv_rc5_decode(unsigned int code
)
154 unsigned int org_code
= code
;
156 unsigned int rc5
= 0;
159 for (i
= 0; i
< 14; ++i
) {
172 dprintk("rc5_decode(%x) bad code\n",
177 dprintk("code=%x, rc5=%x, start=%x, toggle=%x, address=%x, instr=%x\n",
178 rc5
, org_code
, RC5_START(rc5
),
179 RC5_TOGGLE(rc5
), RC5_ADDR(rc5
), RC5_INSTR(rc5
));
183 static void bttv_rc5_timer_end(struct timer_list
*t
)
185 struct bttv_ir
*ir
= from_timer(ir
, t
, timer
);
187 u32 gap
, rc5
, scancode
;
188 u8 toggle
, command
, system
;
193 gap
= ktime_to_us(ktime_sub(tv
, ir
->base_time
));
194 /* avoid overflow with gap >1s */
195 if (gap
> USEC_PER_SEC
) {
198 /* signal we're ready to start a new code */
201 /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
203 dprintk("spurious timer_end\n");
207 if (ir
->last_bit
< 20) {
208 /* ignore spurious codes (caused by light/other remotes) */
209 dprintk("short code: %x\n", ir
->code
);
213 ir
->code
= (ir
->code
<< ir
->shift_by
) | 1;
214 rc5
= bttv_rc5_decode(ir
->code
);
216 toggle
= RC5_TOGGLE(rc5
);
217 system
= RC5_ADDR(rc5
);
218 command
= RC5_INSTR(rc5
);
220 switch (RC5_START(rc5
)) {
230 scancode
= RC_SCANCODE_RC5(system
, command
);
231 rc_keydown(ir
->dev
, RC_PROTO_RC5
, scancode
, toggle
);
232 dprintk("scancode %x, toggle %x\n", scancode
, toggle
);
235 static int bttv_rc5_irq(struct bttv
*btv
)
237 struct bttv_ir
*ir
= btv
->remote
;
241 unsigned long current_jiffies
;
244 gpio
= bttv_gpio_read(&btv
->c
);
246 /* get time of bit */
247 current_jiffies
= jiffies
;
250 gap
= ktime_to_us(ktime_sub(tv
, ir
->base_time
));
251 /* avoid overflow with gap >1s */
252 if (gap
> USEC_PER_SEC
) {
256 dprintk("RC5 IRQ: gap %d us for %s\n",
257 gap
, (gpio
& 0x20) ? "mark" : "space");
263 /* active code => add bit */
265 /* only if in the code (otherwise spurious IRQ or timer
267 if (ir
->last_bit
< 28) {
268 ir
->last_bit
= (gap
- ir_rc5_remote_gap
/ 2) /
270 ir
->code
|= 1 << ir
->last_bit
;
272 /* starting new code */
279 mod_timer(&ir
->timer
, current_jiffies
+ msecs_to_jiffies(30));
282 /* toggle GPIO pin 4 to reset the irq */
283 bttv_gpio_write(&btv
->c
, gpio
& ~(1 << 4));
284 bttv_gpio_write(&btv
->c
, gpio
| (1 << 4));
288 /* ---------------------------------------------------------------------- */
290 static void bttv_ir_start(struct bttv_ir
*ir
)
293 timer_setup(&ir
->timer
, bttv_input_timer
, 0);
294 ir
->timer
.expires
= jiffies
+ msecs_to_jiffies(1000);
295 add_timer(&ir
->timer
);
296 } else if (ir
->rc5_gpio
) {
297 /* set timer_end for code completion */
298 timer_setup(&ir
->timer
, bttv_rc5_timer_end
, 0);
300 ir
->rc5_remote_gap
= ir_rc5_remote_gap
;
304 static void bttv_ir_stop(struct bttv
*btv
)
306 if (btv
->remote
->polling
)
307 del_timer_sync(&btv
->remote
->timer
);
309 if (btv
->remote
->rc5_gpio
) {
312 del_timer_sync(&btv
->remote
->timer
);
314 gpio
= bttv_gpio_read(&btv
->c
);
315 bttv_gpio_write(&btv
->c
, gpio
& ~(1 << 4));
320 * Get_key functions used by I2C remotes
323 static int get_key_pv951(struct IR_i2c
*ir
, enum rc_proto
*protocol
,
324 u32
*scancode
, u8
*toggle
)
330 rc
= i2c_master_recv(ir
->c
, &b
, 1);
332 dprintk("read error\n");
341 dprintk("key %02x\n", b
);
345 * lirc_i2c maps the pv951 code as:
347 * cmd = bit_reverse (b)
348 * So, it seems that this device uses NEC extended
349 * I decided to not fix the table, due to two reasons:
350 * 1) Without the actual device, this is only a guess;
351 * 2) As the addr is not reported via I2C, nor can be changed,
352 * the device is bound to the vendor-provided RC.
355 *protocol
= RC_PROTO_UNKNOWN
;
361 /* Instantiate the I2C IR receiver device, if present */
362 void init_bttv_i2c_ir(struct bttv
*btv
)
364 static const unsigned short addr_list
[] = {
365 0x1a, 0x18, 0x64, 0x30, 0x71,
368 struct i2c_board_info info
;
369 struct i2c_client
*i2c_dev
;
371 if (0 != btv
->i2c_rc
)
374 memset(&info
, 0, sizeof(struct i2c_board_info
));
375 memset(&btv
->init_data
, 0, sizeof(btv
->init_data
));
376 strscpy(info
.type
, "ir_video", I2C_NAME_SIZE
);
378 switch (btv
->c
.type
) {
379 case BTTV_BOARD_PV951
:
380 btv
->init_data
.name
= "PV951";
381 btv
->init_data
.get_key
= get_key_pv951
;
382 btv
->init_data
.ir_codes
= RC_MAP_PV951
;
387 if (btv
->init_data
.name
) {
388 info
.platform_data
= &btv
->init_data
;
389 i2c_dev
= i2c_new_client_device(&btv
->c
.i2c_adap
, &info
);
392 * The external IR receiver is at i2c address 0x34 (0x35 for
393 * reads). Future Hauppauge cards will have an internal
394 * receiver at 0x30 (0x31 for reads). In theory, both can be
395 * fitted, and Hauppauge suggest an external overrides an
397 * That's why we probe 0x1a (~0x34) first. CB
399 i2c_dev
= i2c_new_scanned_device(&btv
->c
.i2c_adap
, &info
, addr_list
, NULL
);
404 #if defined(CONFIG_MODULES) && defined(MODULE)
405 request_module("ir-kbd-i2c");
409 int bttv_input_init(struct bttv
*btv
)
412 char *ir_codes
= NULL
;
416 if (!btv
->has_remote
)
419 ir
= kzalloc(sizeof(*ir
),GFP_KERNEL
);
420 rc
= rc_allocate_device(RC_DRIVER_SCANCODE
);
424 /* detect & configure */
425 switch (btv
->c
.type
) {
426 case BTTV_BOARD_AVERMEDIA
:
427 case BTTV_BOARD_AVPHONE98
:
428 case BTTV_BOARD_AVERMEDIA98
:
429 ir_codes
= RC_MAP_AVERMEDIA
;
430 ir
->mask_keycode
= 0xf88000;
431 ir
->mask_keydown
= 0x010000;
432 ir
->polling
= 50; // ms
435 case BTTV_BOARD_AVDVBT_761
:
436 case BTTV_BOARD_AVDVBT_771
:
437 ir_codes
= RC_MAP_AVERMEDIA_DVBT
;
438 ir
->mask_keycode
= 0x0f00c0;
439 ir
->mask_keydown
= 0x000020;
440 ir
->polling
= 50; // ms
443 case BTTV_BOARD_PXELVWPLTVPAK
:
444 ir_codes
= RC_MAP_PIXELVIEW
;
445 ir
->mask_keycode
= 0x003e00;
446 ir
->mask_keyup
= 0x010000;
447 ir
->polling
= 50; // ms
449 case BTTV_BOARD_PV_M4900
:
450 case BTTV_BOARD_PV_BT878P_9B
:
451 case BTTV_BOARD_PV_BT878P_PLUS
:
452 ir_codes
= RC_MAP_PIXELVIEW
;
453 ir
->mask_keycode
= 0x001f00;
454 ir
->mask_keyup
= 0x008000;
455 ir
->polling
= 50; // ms
458 case BTTV_BOARD_WINFAST2000
:
459 ir_codes
= RC_MAP_WINFAST
;
460 ir
->mask_keycode
= 0x1f8;
462 case BTTV_BOARD_MAGICTVIEW061
:
463 case BTTV_BOARD_MAGICTVIEW063
:
464 ir_codes
= RC_MAP_WINFAST
;
465 ir
->mask_keycode
= 0x0008e000;
466 ir
->mask_keydown
= 0x00200000;
468 case BTTV_BOARD_APAC_VIEWCOMP
:
469 ir_codes
= RC_MAP_APAC_VIEWCOMP
;
470 ir
->mask_keycode
= 0x001f00;
471 ir
->mask_keyup
= 0x008000;
472 ir
->polling
= 50; // ms
474 case BTTV_BOARD_ASKEY_CPH03X
:
475 case BTTV_BOARD_CONCEPTRONIC_CTVFMI2
:
476 case BTTV_BOARD_CONTVFMI
:
477 case BTTV_BOARD_KWORLD_VSTREAM_XPERT
:
478 ir_codes
= RC_MAP_PIXELVIEW
;
479 ir
->mask_keycode
= 0x001F00;
480 ir
->mask_keyup
= 0x006000;
481 ir
->polling
= 50; // ms
483 case BTTV_BOARD_NEBULA_DIGITV
:
484 ir_codes
= RC_MAP_NEBULA
;
487 case BTTV_BOARD_MACHTV_MAGICTV
:
488 ir_codes
= RC_MAP_APAC_VIEWCOMP
;
489 ir
->mask_keycode
= 0x001F00;
490 ir
->mask_keyup
= 0x004000;
491 ir
->polling
= 50; /* ms */
493 case BTTV_BOARD_KOZUMI_KTV_01C
:
494 ir_codes
= RC_MAP_PCTV_SEDNA
;
495 ir
->mask_keycode
= 0x001f00;
496 ir
->mask_keyup
= 0x006000;
497 ir
->polling
= 50; /* ms */
499 case BTTV_BOARD_ENLTV_FM_2
:
500 ir_codes
= RC_MAP_ENCORE_ENLTV2
;
501 ir
->mask_keycode
= 0x00fd00;
502 ir
->mask_keyup
= 0x000080;
503 ir
->polling
= 1; /* ms */
504 ir
->last_gpio
= ir_extract_bits(bttv_gpio_read(&btv
->c
),
510 dprintk("Ooops: IR config error [card=%d]\n", btv
->c
.type
);
517 /* enable remote irq */
518 bttv_gpio_inout(&btv
->c
, (1 << 4), 1 << 4);
519 gpio
= bttv_gpio_read(&btv
->c
);
520 bttv_gpio_write(&btv
->c
, gpio
& ~(1 << 4));
521 bttv_gpio_write(&btv
->c
, gpio
| (1 << 4));
523 /* init hardware-specific stuff */
524 bttv_gpio_inout(&btv
->c
, ir
->mask_keycode
| ir
->mask_keydown
, 0);
527 /* init input device */
531 snprintf(ir
->name
, sizeof(ir
->name
), "bttv IR (card=%d)",
533 snprintf(ir
->phys
, sizeof(ir
->phys
), "pci-%s/ir0",
534 pci_name(btv
->c
.pci
));
536 rc
->device_name
= ir
->name
;
537 rc
->input_phys
= ir
->phys
;
538 rc
->input_id
.bustype
= BUS_PCI
;
539 rc
->input_id
.version
= 1;
540 if (btv
->c
.pci
->subsystem_vendor
) {
541 rc
->input_id
.vendor
= btv
->c
.pci
->subsystem_vendor
;
542 rc
->input_id
.product
= btv
->c
.pci
->subsystem_device
;
544 rc
->input_id
.vendor
= btv
->c
.pci
->vendor
;
545 rc
->input_id
.product
= btv
->c
.pci
->device
;
547 rc
->dev
.parent
= &btv
->c
.pci
->dev
;
548 rc
->map_name
= ir_codes
;
549 rc
->driver_name
= MODULE_NAME
;
555 err
= rc_register_device(rc
);
570 void bttv_input_fini(struct bttv
*btv
)
572 if (btv
->remote
== NULL
)
576 rc_unregister_device(btv
->remote
->dev
);