1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  * Copyright (c) 2003 Gerd Knorr
5  * Copyright (c) 2003 Pavel Machek
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
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>
16 
17 #include "bttv.h"
18 #include "bttvp.h"
19 
20 
21 static int ir_debug;
22 module_param(ir_debug, int, 0644);
23 
24 static int ir_rc5_remote_gap = 885;
25 module_param(ir_rc5_remote_gap, int, 0644);
26 
27 #undef dprintk
28 #define dprintk(fmt, ...)			\
29 do {						\
30 	if (ir_debug >= 1)			\
31 		pr_info(fmt, ##__VA_ARGS__);	\
32 } while (0)
33 
34 #define DEVNAME "bttv-input"
35 
36 #define MODULE_NAME "bttv"
37 
38 /* ---------------------------------------------------------------------- */
39 
ir_handle_key(struct bttv * btv)40 static void ir_handle_key(struct bttv *btv)
41 {
42 	struct bttv_ir *ir = btv->remote;
43 	u32 gpio,data;
44 
45 	/* read gpio value */
46 	gpio = bttv_gpio_read(&btv->c);
47 	if (ir->polling) {
48 		if (ir->last_gpio == gpio)
49 			return;
50 		ir->last_gpio = gpio;
51 	}
52 
53 	/* extract data */
54 	data = ir_extract_bits(gpio, ir->mask_keycode);
55 	dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
56 		gpio, data,
57 		ir->polling               ? "poll"  : "irq",
58 		(gpio & ir->mask_keydown) ? " down" : "",
59 		(gpio & ir->mask_keyup)   ? " up"   : "");
60 
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);
64 	} else {
65 		/* HACK: Probably, ir->mask_keydown is missing
66 		   for this board */
67 		if (btv->c.type == BTTV_BOARD_WINFAST2000)
68 			rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
69 					     0);
70 
71 		rc_keyup(ir->dev);
72 	}
73 }
74 
ir_enltv_handle_key(struct bttv * btv)75 static void ir_enltv_handle_key(struct bttv *btv)
76 {
77 	struct bttv_ir *ir = btv->remote;
78 	u32 gpio, data, keyup;
79 
80 	/* read gpio value */
81 	gpio = bttv_gpio_read(&btv->c);
82 
83 	/* extract data */
84 	data = ir_extract_bits(gpio, ir->mask_keycode);
85 
86 	/* Check if it is keyup */
87 	keyup = (gpio & ir->mask_keyup) ? 1UL << 31 : 0;
88 
89 	if ((ir->last_gpio & 0x7f) != data) {
90 		dprintk("gpio=0x%x code=%d | %s\n",
91 			gpio, data,
92 			(gpio & ir->mask_keyup) ? " up" : "up/down");
93 
94 		rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data, 0);
95 		if (keyup)
96 			rc_keyup(ir->dev);
97 	} else {
98 		if ((ir->last_gpio & 1UL << 31) == keyup)
99 			return;
100 
101 		dprintk("(cnt) gpio=0x%x code=%d | %s\n",
102 			gpio, data,
103 			(gpio & ir->mask_keyup) ? " up" : "down");
104 
105 		if (keyup)
106 			rc_keyup(ir->dev);
107 		else
108 			rc_keydown_notimeout(ir->dev, RC_PROTO_UNKNOWN, data,
109 					     0);
110 	}
111 
112 	ir->last_gpio = data | keyup;
113 }
114 
115 static int bttv_rc5_irq(struct bttv *btv);
116 
bttv_input_irq(struct bttv * btv)117 void bttv_input_irq(struct bttv *btv)
118 {
119 	struct bttv_ir *ir = btv->remote;
120 
121 	if (ir->rc5_gpio)
122 		bttv_rc5_irq(btv);
123 	else if (!ir->polling)
124 		ir_handle_key(btv);
125 }
126 
bttv_input_timer(struct timer_list * t)127 static void bttv_input_timer(struct timer_list *t)
128 {
129 	struct bttv_ir *ir = from_timer(ir, t, timer);
130 	struct bttv *btv = ir->btv;
131 
132 	if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
133 		ir_enltv_handle_key(btv);
134 	else
135 		ir_handle_key(btv);
136 	mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
137 }
138 
139 /*
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
143  * testing.
144  */
145 
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)
150 
151 /* decode raw bit pattern to RC5 code */
bttv_rc5_decode(unsigned int code)152 static u32 bttv_rc5_decode(unsigned int code)
153 {
154 	unsigned int org_code = code;
155 	unsigned int pair;
156 	unsigned int rc5 = 0;
157 	int i;
158 
159 	for (i = 0; i < 14; ++i) {
160 		pair = code & 0x3;
161 		code >>= 2;
162 
163 		rc5 <<= 1;
164 		switch (pair) {
165 		case 0:
166 		case 2:
167 			break;
168 		case 1:
169 			rc5 |= 1;
170 		break;
171 		case 3:
172 			dprintk("rc5_decode(%x) bad code\n",
173 				org_code);
174 			return 0;
175 		}
176 	}
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));
180 	return rc5;
181 }
182 
bttv_rc5_timer_end(struct timer_list * t)183 static void bttv_rc5_timer_end(struct timer_list *t)
184 {
185 	struct bttv_ir *ir = from_timer(ir, t, timer);
186 	ktime_t tv;
187 	u32 gap, rc5, scancode;
188 	u8 toggle, command, system;
189 
190 	/* get time */
191 	tv = ktime_get();
192 
193 	gap = ktime_to_us(ktime_sub(tv, ir->base_time));
194 	/* avoid overflow with gap >1s */
195 	if (gap > USEC_PER_SEC) {
196 		gap = 200000;
197 	}
198 	/* signal we're ready to start a new code */
199 	ir->active = false;
200 
201 	/* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
202 	if (gap < 28000) {
203 		dprintk("spurious timer_end\n");
204 		return;
205 	}
206 
207 	if (ir->last_bit < 20) {
208 		/* ignore spurious codes (caused by light/other remotes) */
209 		dprintk("short code: %x\n", ir->code);
210 		return;
211 	}
212 
213 	ir->code = (ir->code << ir->shift_by) | 1;
214 	rc5 = bttv_rc5_decode(ir->code);
215 
216 	toggle = RC5_TOGGLE(rc5);
217 	system = RC5_ADDR(rc5);
218 	command = RC5_INSTR(rc5);
219 
220 	switch (RC5_START(rc5)) {
221 	case 0x3:
222 		break;
223 	case 0x2:
224 		command += 0x40;
225 		break;
226 	default:
227 		return;
228 	}
229 
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);
233 }
234 
bttv_rc5_irq(struct bttv * btv)235 static int bttv_rc5_irq(struct bttv *btv)
236 {
237 	struct bttv_ir *ir = btv->remote;
238 	ktime_t tv;
239 	u32 gpio;
240 	u32 gap;
241 	unsigned long current_jiffies;
242 
243 	/* read gpio port */
244 	gpio = bttv_gpio_read(&btv->c);
245 
246 	/* get time of bit */
247 	current_jiffies = jiffies;
248 	tv = ktime_get();
249 
250 	gap = ktime_to_us(ktime_sub(tv, ir->base_time));
251 	/* avoid overflow with gap >1s */
252 	if (gap > USEC_PER_SEC) {
253 		gap = 200000;
254 	}
255 
256 	dprintk("RC5 IRQ: gap %d us for %s\n",
257 		gap, (gpio & 0x20) ? "mark" : "space");
258 
259 	/* remote IRQ? */
260 	if (!(gpio & 0x20))
261 		return 0;
262 
263 	/* active code => add bit */
264 	if (ir->active) {
265 		/* only if in the code (otherwise spurious IRQ or timer
266 		   late) */
267 		if (ir->last_bit < 28) {
268 			ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
269 			    ir_rc5_remote_gap;
270 			ir->code |= 1 << ir->last_bit;
271 		}
272 		/* starting new code */
273 	} else {
274 		ir->active = true;
275 		ir->code = 0;
276 		ir->base_time = tv;
277 		ir->last_bit = 0;
278 
279 		mod_timer(&ir->timer, current_jiffies + msecs_to_jiffies(30));
280 	}
281 
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));
285 	return 1;
286 }
287 
288 /* ---------------------------------------------------------------------- */
289 
bttv_ir_start(struct bttv_ir * ir)290 static void bttv_ir_start(struct bttv_ir *ir)
291 {
292 	if (ir->polling) {
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);
299 		ir->shift_by = 1;
300 		ir->rc5_remote_gap = ir_rc5_remote_gap;
301 	}
302 }
303 
bttv_ir_stop(struct bttv * btv)304 static void bttv_ir_stop(struct bttv *btv)
305 {
306 	if (btv->remote->polling)
307 		del_timer_sync(&btv->remote->timer);
308 
309 	if (btv->remote->rc5_gpio) {
310 		u32 gpio;
311 
312 		del_timer_sync(&btv->remote->timer);
313 
314 		gpio = bttv_gpio_read(&btv->c);
315 		bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
316 	}
317 }
318 
319 /*
320  * Get_key functions used by I2C remotes
321  */
322 
get_key_pv951(struct IR_i2c * ir,enum rc_proto * protocol,u32 * scancode,u8 * toggle)323 static int get_key_pv951(struct IR_i2c *ir, enum rc_proto *protocol,
324 			 u32 *scancode, u8 *toggle)
325 {
326 	int rc;
327 	unsigned char b;
328 
329 	/* poll IR chip */
330 	rc = i2c_master_recv(ir->c, &b, 1);
331 	if (rc != 1) {
332 		dprintk("read error\n");
333 		if (rc < 0)
334 			return rc;
335 		return -EIO;
336 	}
337 
338 	/* ignore 0xaa */
339 	if (b==0xaa)
340 		return 0;
341 	dprintk("key %02x\n", b);
342 
343 	/*
344 	 * NOTE:
345 	 * lirc_i2c maps the pv951 code as:
346 	 *	addr = 0x61D6
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.
353 	 */
354 
355 	*protocol = RC_PROTO_UNKNOWN;
356 	*scancode = b;
357 	*toggle = 0;
358 	return 1;
359 }
360 
361 /* Instantiate the I2C IR receiver device, if present */
init_bttv_i2c_ir(struct bttv * btv)362 void init_bttv_i2c_ir(struct bttv *btv)
363 {
364 	static const unsigned short addr_list[] = {
365 		0x1a, 0x18, 0x64, 0x30, 0x71,
366 		I2C_CLIENT_END
367 	};
368 	struct i2c_board_info info;
369 	struct i2c_client *i2c_dev;
370 
371 	if (0 != btv->i2c_rc)
372 		return;
373 
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);
377 
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;
383 		info.addr = 0x4b;
384 		break;
385 	}
386 
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);
390 	} else {
391 		/*
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
396 		 * internal.
397 		 * That's why we probe 0x1a (~0x34) first. CB
398 		 */
399 		i2c_dev = i2c_new_scanned_device(&btv->c.i2c_adap, &info, addr_list, NULL);
400 	}
401 	if (IS_ERR(i2c_dev))
402 		return;
403 
404 #if defined(CONFIG_MODULES) && defined(MODULE)
405 	request_module("ir-kbd-i2c");
406 #endif
407 }
408 
bttv_input_init(struct bttv * btv)409 int bttv_input_init(struct bttv *btv)
410 {
411 	struct bttv_ir *ir;
412 	char *ir_codes = NULL;
413 	struct rc_dev *rc;
414 	int err = -ENOMEM;
415 
416 	if (!btv->has_remote)
417 		return -ENODEV;
418 
419 	ir = kzalloc(sizeof(*ir),GFP_KERNEL);
420 	rc = rc_allocate_device(RC_DRIVER_SCANCODE);
421 	if (!ir || !rc)
422 		goto err_out_free;
423 
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
433 		break;
434 
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
441 		break;
442 
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
448 		break;
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
456 		break;
457 
458 	case BTTV_BOARD_WINFAST2000:
459 		ir_codes         = RC_MAP_WINFAST;
460 		ir->mask_keycode = 0x1f8;
461 		break;
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;
467 		break;
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
473 		break;
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
482 		break;
483 	case BTTV_BOARD_NEBULA_DIGITV:
484 		ir_codes         = RC_MAP_NEBULA;
485 		ir->rc5_gpio     = true;
486 		break;
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 */
492 		break;
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 */
498 		break;
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),
505 						   ir->mask_keycode);
506 		break;
507 	}
508 
509 	if (!ir_codes) {
510 		dprintk("Ooops: IR config error [card=%d]\n", btv->c.type);
511 		err = -ENODEV;
512 		goto err_out_free;
513 	}
514 
515 	if (ir->rc5_gpio) {
516 		u32 gpio;
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));
522 	} else {
523 		/* init hardware-specific stuff */
524 		bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
525 	}
526 
527 	/* init input device */
528 	ir->dev = rc;
529 	ir->btv = btv;
530 
531 	snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
532 		 btv->c.type);
533 	snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
534 		 pci_name(btv->c.pci));
535 
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;
543 	} else {
544 		rc->input_id.vendor  = btv->c.pci->vendor;
545 		rc->input_id.product = btv->c.pci->device;
546 	}
547 	rc->dev.parent = &btv->c.pci->dev;
548 	rc->map_name = ir_codes;
549 	rc->driver_name = MODULE_NAME;
550 
551 	btv->remote = ir;
552 	bttv_ir_start(ir);
553 
554 	/* all done */
555 	err = rc_register_device(rc);
556 	if (err)
557 		goto err_out_stop;
558 
559 	return 0;
560 
561  err_out_stop:
562 	bttv_ir_stop(btv);
563 	btv->remote = NULL;
564  err_out_free:
565 	rc_free_device(rc);
566 	kfree(ir);
567 	return err;
568 }
569 
bttv_input_fini(struct bttv * btv)570 void bttv_input_fini(struct bttv *btv)
571 {
572 	if (btv->remote == NULL)
573 		return;
574 
575 	bttv_ir_stop(btv);
576 	rc_unregister_device(btv->remote->dev);
577 	kfree(btv->remote);
578 	btv->remote = NULL;
579 }
580