xref: /linux/drivers/media/pci/bt8xx/bttv-input.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 /*
2  *
3  * Copyright (c) 2003 Gerd Knorr
4  * Copyright (c) 2003 Pavel Machek
5  *
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.
10  *
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.
15  */
16 
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/interrupt.h>
23 #include <linux/input.h>
24 #include <linux/slab.h>
25 
26 #include "bttv.h"
27 #include "bttvp.h"
28 
29 
30 static int ir_debug;
31 module_param(ir_debug, int, 0644);
32 
33 static int ir_rc5_remote_gap = 885;
34 module_param(ir_rc5_remote_gap, int, 0644);
35 
36 #undef dprintk
37 #define dprintk(fmt, ...)			\
38 do {						\
39 	if (ir_debug >= 1)			\
40 		pr_info(fmt, ##__VA_ARGS__);	\
41 } while (0)
42 
43 #define DEVNAME "bttv-input"
44 
45 #define MODULE_NAME "bttv"
46 
47 /* ---------------------------------------------------------------------- */
48 
49 static void ir_handle_key(struct bttv *btv)
50 {
51 	struct bttv_ir *ir = btv->remote;
52 	u32 gpio,data;
53 
54 	/* read gpio value */
55 	gpio = bttv_gpio_read(&btv->c);
56 	if (ir->polling) {
57 		if (ir->last_gpio == gpio)
58 			return;
59 		ir->last_gpio = gpio;
60 	}
61 
62 	/* extract data */
63 	data = ir_extract_bits(gpio, ir->mask_keycode);
64 	dprintk("irq gpio=0x%x code=%d | %s%s%s\n",
65 		gpio, data,
66 		ir->polling               ? "poll"  : "irq",
67 		(gpio & ir->mask_keydown) ? " down" : "",
68 		(gpio & ir->mask_keyup)   ? " up"   : "");
69 
70 	if ((ir->mask_keydown && (gpio & ir->mask_keydown)) ||
71 	    (ir->mask_keyup   && !(gpio & ir->mask_keyup))) {
72 		rc_keydown_notimeout(ir->dev, RC_TYPE_UNKNOWN, data, 0);
73 	} else {
74 		/* HACK: Probably, ir->mask_keydown is missing
75 		   for this board */
76 		if (btv->c.type == BTTV_BOARD_WINFAST2000)
77 			rc_keydown_notimeout(ir->dev, RC_TYPE_UNKNOWN, data, 0);
78 
79 		rc_keyup(ir->dev);
80 	}
81 }
82 
83 static void ir_enltv_handle_key(struct bttv *btv)
84 {
85 	struct bttv_ir *ir = btv->remote;
86 	u32 gpio, data, keyup;
87 
88 	/* read gpio value */
89 	gpio = bttv_gpio_read(&btv->c);
90 
91 	/* extract data */
92 	data = ir_extract_bits(gpio, ir->mask_keycode);
93 
94 	/* Check if it is keyup */
95 	keyup = (gpio & ir->mask_keyup) ? 1 << 31 : 0;
96 
97 	if ((ir->last_gpio & 0x7f) != data) {
98 		dprintk("gpio=0x%x code=%d | %s\n",
99 			gpio, data,
100 			(gpio & ir->mask_keyup) ? " up" : "up/down");
101 
102 		rc_keydown_notimeout(ir->dev, RC_TYPE_UNKNOWN, data, 0);
103 		if (keyup)
104 			rc_keyup(ir->dev);
105 	} else {
106 		if ((ir->last_gpio & 1 << 31) == keyup)
107 			return;
108 
109 		dprintk("(cnt) gpio=0x%x code=%d | %s\n",
110 			gpio, data,
111 			(gpio & ir->mask_keyup) ? " up" : "down");
112 
113 		if (keyup)
114 			rc_keyup(ir->dev);
115 		else
116 			rc_keydown_notimeout(ir->dev, RC_TYPE_UNKNOWN, data, 0);
117 	}
118 
119 	ir->last_gpio = data | keyup;
120 }
121 
122 static int bttv_rc5_irq(struct bttv *btv);
123 
124 void bttv_input_irq(struct bttv *btv)
125 {
126 	struct bttv_ir *ir = btv->remote;
127 
128 	if (ir->rc5_gpio)
129 		bttv_rc5_irq(btv);
130 	else if (!ir->polling)
131 		ir_handle_key(btv);
132 }
133 
134 static void bttv_input_timer(unsigned long data)
135 {
136 	struct bttv *btv = (struct bttv*)data;
137 	struct bttv_ir *ir = btv->remote;
138 
139 	if (btv->c.type == BTTV_BOARD_ENLTV_FM_2)
140 		ir_enltv_handle_key(btv);
141 	else
142 		ir_handle_key(btv);
143 	mod_timer(&ir->timer, jiffies + msecs_to_jiffies(ir->polling));
144 }
145 
146 /*
147  * FIXME: Nebula digi uses the legacy way to decode RC5, instead of relying
148  * on the rc-core way. As we need to be sure that both IRQ transitions are
149  * properly triggered, Better to touch it only with this hardware for
150  * testing.
151  */
152 
153 #define RC5_START(x)	(((x) >> 12) & 0x03)
154 #define RC5_TOGGLE(x)	(((x) >> 11) & 0x01)
155 #define RC5_ADDR(x)	(((x) >> 6)  & 0x1f)
156 #define RC5_INSTR(x)	(((x) >> 0)  & 0x3f)
157 
158 /* decode raw bit pattern to RC5 code */
159 static u32 bttv_rc5_decode(unsigned int code)
160 {
161 	unsigned int org_code = code;
162 	unsigned int pair;
163 	unsigned int rc5 = 0;
164 	int i;
165 
166 	for (i = 0; i < 14; ++i) {
167 		pair = code & 0x3;
168 		code >>= 2;
169 
170 		rc5 <<= 1;
171 		switch (pair) {
172 		case 0:
173 		case 2:
174 			break;
175 		case 1:
176 			rc5 |= 1;
177 		break;
178 		case 3:
179 			dprintk("rc5_decode(%x) bad code\n",
180 				org_code);
181 			return 0;
182 		}
183 	}
184 	dprintk("code=%x, rc5=%x, start=%x, toggle=%x, address=%x, instr=%x\n",
185 		rc5, org_code, RC5_START(rc5),
186 		RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
187 	return rc5;
188 }
189 
190 static void bttv_rc5_timer_end(unsigned long data)
191 {
192 	struct bttv_ir *ir = (struct bttv_ir *)data;
193 	ktime_t tv;
194 	u32 gap, rc5, scancode;
195 	u8 toggle, command, system;
196 
197 	/* get time */
198 	tv = ktime_get();
199 
200 	gap = ktime_to_us(ktime_sub(tv, ir->base_time));
201 	/* avoid overflow with gap >1s */
202 	if (gap > USEC_PER_SEC) {
203 		gap = 200000;
204 	}
205 	/* signal we're ready to start a new code */
206 	ir->active = false;
207 
208 	/* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
209 	if (gap < 28000) {
210 		dprintk("spurious timer_end\n");
211 		return;
212 	}
213 
214 	if (ir->last_bit < 20) {
215 		/* ignore spurious codes (caused by light/other remotes) */
216 		dprintk("short code: %x\n", ir->code);
217 		return;
218 	}
219 
220 	ir->code = (ir->code << ir->shift_by) | 1;
221 	rc5 = bttv_rc5_decode(ir->code);
222 
223 	toggle = RC5_TOGGLE(rc5);
224 	system = RC5_ADDR(rc5);
225 	command = RC5_INSTR(rc5);
226 
227 	switch (RC5_START(rc5)) {
228 	case 0x3:
229 		break;
230 	case 0x2:
231 		command += 0x40;
232 		break;
233 	default:
234 		return;
235 	}
236 
237 	scancode = RC_SCANCODE_RC5(system, command);
238 	rc_keydown(ir->dev, RC_TYPE_RC5, scancode, toggle);
239 	dprintk("scancode %x, toggle %x\n", scancode, toggle);
240 }
241 
242 static int bttv_rc5_irq(struct bttv *btv)
243 {
244 	struct bttv_ir *ir = btv->remote;
245 	ktime_t tv;
246 	u32 gpio;
247 	u32 gap;
248 	unsigned long current_jiffies;
249 
250 	/* read gpio port */
251 	gpio = bttv_gpio_read(&btv->c);
252 
253 	/* get time of bit */
254 	current_jiffies = jiffies;
255 	tv = ktime_get();
256 
257 	gap = ktime_to_us(ktime_sub(tv, ir->base_time));
258 	/* avoid overflow with gap >1s */
259 	if (gap > USEC_PER_SEC) {
260 		gap = 200000;
261 	}
262 
263 	dprintk("RC5 IRQ: gap %d us for %s\n",
264 		gap, (gpio & 0x20) ? "mark" : "space");
265 
266 	/* remote IRQ? */
267 	if (!(gpio & 0x20))
268 		return 0;
269 
270 	/* active code => add bit */
271 	if (ir->active) {
272 		/* only if in the code (otherwise spurious IRQ or timer
273 		   late) */
274 		if (ir->last_bit < 28) {
275 			ir->last_bit = (gap - ir_rc5_remote_gap / 2) /
276 			    ir_rc5_remote_gap;
277 			ir->code |= 1 << ir->last_bit;
278 		}
279 		/* starting new code */
280 	} else {
281 		ir->active = true;
282 		ir->code = 0;
283 		ir->base_time = tv;
284 		ir->last_bit = 0;
285 
286 		mod_timer(&ir->timer, current_jiffies + msecs_to_jiffies(30));
287 	}
288 
289 	/* toggle GPIO pin 4 to reset the irq */
290 	bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
291 	bttv_gpio_write(&btv->c, gpio | (1 << 4));
292 	return 1;
293 }
294 
295 /* ---------------------------------------------------------------------- */
296 
297 static void bttv_ir_start(struct bttv *btv, struct bttv_ir *ir)
298 {
299 	if (ir->polling) {
300 		setup_timer(&ir->timer, bttv_input_timer, (unsigned long)btv);
301 		ir->timer.expires  = jiffies + msecs_to_jiffies(1000);
302 		add_timer(&ir->timer);
303 	} else if (ir->rc5_gpio) {
304 		/* set timer_end for code completion */
305 		setup_timer(&ir->timer, bttv_rc5_timer_end, (unsigned long)ir);
306 		ir->shift_by = 1;
307 		ir->rc5_remote_gap = ir_rc5_remote_gap;
308 	}
309 }
310 
311 static void bttv_ir_stop(struct bttv *btv)
312 {
313 	if (btv->remote->polling)
314 		del_timer_sync(&btv->remote->timer);
315 
316 	if (btv->remote->rc5_gpio) {
317 		u32 gpio;
318 
319 		del_timer_sync(&btv->remote->timer);
320 
321 		gpio = bttv_gpio_read(&btv->c);
322 		bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
323 	}
324 }
325 
326 /*
327  * Get_key functions used by I2C remotes
328  */
329 
330 static int get_key_pv951(struct IR_i2c *ir, enum rc_type *protocol,
331 			 u32 *scancode, u8 *toggle)
332 {
333 	unsigned char b;
334 
335 	/* poll IR chip */
336 	if (1 != i2c_master_recv(ir->c, &b, 1)) {
337 		dprintk("read error\n");
338 		return -EIO;
339 	}
340 
341 	/* ignore 0xaa */
342 	if (b==0xaa)
343 		return 0;
344 	dprintk("key %02x\n", b);
345 
346 	/*
347 	 * NOTE:
348 	 * lirc_i2c maps the pv951 code as:
349 	 *	addr = 0x61D6
350 	 * 	cmd = bit_reverse (b)
351 	 * So, it seems that this device uses NEC extended
352 	 * I decided to not fix the table, due to two reasons:
353 	 * 	1) Without the actual device, this is only a guess;
354 	 * 	2) As the addr is not reported via I2C, nor can be changed,
355 	 * 	   the device is bound to the vendor-provided RC.
356 	 */
357 
358 	*protocol = RC_TYPE_UNKNOWN;
359 	*scancode = b;
360 	*toggle = 0;
361 	return 1;
362 }
363 
364 /* Instantiate the I2C IR receiver device, if present */
365 void init_bttv_i2c_ir(struct bttv *btv)
366 {
367 	const unsigned short addr_list[] = {
368 		0x1a, 0x18, 0x64, 0x30, 0x71,
369 		I2C_CLIENT_END
370 	};
371 	struct i2c_board_info info;
372 	struct i2c_client *i2c_dev;
373 
374 	if (0 != btv->i2c_rc)
375 		return;
376 
377 	memset(&info, 0, sizeof(struct i2c_board_info));
378 	memset(&btv->init_data, 0, sizeof(btv->init_data));
379 	strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
380 
381 	switch (btv->c.type) {
382 	case BTTV_BOARD_PV951:
383 		btv->init_data.name = "PV951";
384 		btv->init_data.get_key = get_key_pv951;
385 		btv->init_data.ir_codes = RC_MAP_PV951;
386 		info.addr = 0x4b;
387 		break;
388 	}
389 
390 	if (btv->init_data.name) {
391 		info.platform_data = &btv->init_data;
392 		i2c_dev = i2c_new_device(&btv->c.i2c_adap, &info);
393 	} else {
394 		/*
395 		 * The external IR receiver is at i2c address 0x34 (0x35 for
396 		 * reads).  Future Hauppauge cards will have an internal
397 		 * receiver at 0x30 (0x31 for reads).  In theory, both can be
398 		 * fitted, and Hauppauge suggest an external overrides an
399 		 * internal.
400 		 * That's why we probe 0x1a (~0x34) first. CB
401 		 */
402 		i2c_dev = i2c_new_probed_device(&btv->c.i2c_adap, &info, addr_list, NULL);
403 	}
404 	if (NULL == i2c_dev)
405 		return;
406 
407 #if defined(CONFIG_MODULES) && defined(MODULE)
408 	request_module("ir-kbd-i2c");
409 #endif
410 }
411 
412 int bttv_input_init(struct bttv *btv)
413 {
414 	struct bttv_ir *ir;
415 	char *ir_codes = NULL;
416 	struct rc_dev *rc;
417 	int err = -ENOMEM;
418 
419 	if (!btv->has_remote)
420 		return -ENODEV;
421 
422 	ir = kzalloc(sizeof(*ir),GFP_KERNEL);
423 	rc = rc_allocate_device(RC_DRIVER_SCANCODE);
424 	if (!ir || !rc)
425 		goto err_out_free;
426 
427 	/* detect & configure */
428 	switch (btv->c.type) {
429 	case BTTV_BOARD_AVERMEDIA:
430 	case BTTV_BOARD_AVPHONE98:
431 	case BTTV_BOARD_AVERMEDIA98:
432 		ir_codes         = RC_MAP_AVERMEDIA;
433 		ir->mask_keycode = 0xf88000;
434 		ir->mask_keydown = 0x010000;
435 		ir->polling      = 50; // ms
436 		break;
437 
438 	case BTTV_BOARD_AVDVBT_761:
439 	case BTTV_BOARD_AVDVBT_771:
440 		ir_codes         = RC_MAP_AVERMEDIA_DVBT;
441 		ir->mask_keycode = 0x0f00c0;
442 		ir->mask_keydown = 0x000020;
443 		ir->polling      = 50; // ms
444 		break;
445 
446 	case BTTV_BOARD_PXELVWPLTVPAK:
447 		ir_codes         = RC_MAP_PIXELVIEW;
448 		ir->mask_keycode = 0x003e00;
449 		ir->mask_keyup   = 0x010000;
450 		ir->polling      = 50; // ms
451 		break;
452 	case BTTV_BOARD_PV_M4900:
453 	case BTTV_BOARD_PV_BT878P_9B:
454 	case BTTV_BOARD_PV_BT878P_PLUS:
455 		ir_codes         = RC_MAP_PIXELVIEW;
456 		ir->mask_keycode = 0x001f00;
457 		ir->mask_keyup   = 0x008000;
458 		ir->polling      = 50; // ms
459 		break;
460 
461 	case BTTV_BOARD_WINFAST2000:
462 		ir_codes         = RC_MAP_WINFAST;
463 		ir->mask_keycode = 0x1f8;
464 		break;
465 	case BTTV_BOARD_MAGICTVIEW061:
466 	case BTTV_BOARD_MAGICTVIEW063:
467 		ir_codes         = RC_MAP_WINFAST;
468 		ir->mask_keycode = 0x0008e000;
469 		ir->mask_keydown = 0x00200000;
470 		break;
471 	case BTTV_BOARD_APAC_VIEWCOMP:
472 		ir_codes         = RC_MAP_APAC_VIEWCOMP;
473 		ir->mask_keycode = 0x001f00;
474 		ir->mask_keyup   = 0x008000;
475 		ir->polling      = 50; // ms
476 		break;
477 	case BTTV_BOARD_ASKEY_CPH03X:
478 	case BTTV_BOARD_CONCEPTRONIC_CTVFMI2:
479 	case BTTV_BOARD_CONTVFMI:
480 	case BTTV_BOARD_KWORLD_VSTREAM_XPERT:
481 		ir_codes         = RC_MAP_PIXELVIEW;
482 		ir->mask_keycode = 0x001F00;
483 		ir->mask_keyup   = 0x006000;
484 		ir->polling      = 50; // ms
485 		break;
486 	case BTTV_BOARD_NEBULA_DIGITV:
487 		ir_codes         = RC_MAP_NEBULA;
488 		ir->rc5_gpio     = true;
489 		break;
490 	case BTTV_BOARD_MACHTV_MAGICTV:
491 		ir_codes         = RC_MAP_APAC_VIEWCOMP;
492 		ir->mask_keycode = 0x001F00;
493 		ir->mask_keyup   = 0x004000;
494 		ir->polling      = 50; /* ms */
495 		break;
496 	case BTTV_BOARD_KOZUMI_KTV_01C:
497 		ir_codes         = RC_MAP_PCTV_SEDNA;
498 		ir->mask_keycode = 0x001f00;
499 		ir->mask_keyup   = 0x006000;
500 		ir->polling      = 50; /* ms */
501 		break;
502 	case BTTV_BOARD_ENLTV_FM_2:
503 		ir_codes         = RC_MAP_ENCORE_ENLTV2;
504 		ir->mask_keycode = 0x00fd00;
505 		ir->mask_keyup   = 0x000080;
506 		ir->polling      = 1; /* ms */
507 		ir->last_gpio    = ir_extract_bits(bttv_gpio_read(&btv->c),
508 						   ir->mask_keycode);
509 		break;
510 	}
511 
512 	if (!ir_codes) {
513 		dprintk("Ooops: IR config error [card=%d]\n", btv->c.type);
514 		err = -ENODEV;
515 		goto err_out_free;
516 	}
517 
518 	if (ir->rc5_gpio) {
519 		u32 gpio;
520 		/* enable remote irq */
521 		bttv_gpio_inout(&btv->c, (1 << 4), 1 << 4);
522 		gpio = bttv_gpio_read(&btv->c);
523 		bttv_gpio_write(&btv->c, gpio & ~(1 << 4));
524 		bttv_gpio_write(&btv->c, gpio | (1 << 4));
525 	} else {
526 		/* init hardware-specific stuff */
527 		bttv_gpio_inout(&btv->c, ir->mask_keycode | ir->mask_keydown, 0);
528 	}
529 
530 	/* init input device */
531 	ir->dev = rc;
532 
533 	snprintf(ir->name, sizeof(ir->name), "bttv IR (card=%d)",
534 		 btv->c.type);
535 	snprintf(ir->phys, sizeof(ir->phys), "pci-%s/ir0",
536 		 pci_name(btv->c.pci));
537 
538 	rc->input_name = ir->name;
539 	rc->input_phys = ir->phys;
540 	rc->input_id.bustype = BUS_PCI;
541 	rc->input_id.version = 1;
542 	if (btv->c.pci->subsystem_vendor) {
543 		rc->input_id.vendor  = btv->c.pci->subsystem_vendor;
544 		rc->input_id.product = btv->c.pci->subsystem_device;
545 	} else {
546 		rc->input_id.vendor  = btv->c.pci->vendor;
547 		rc->input_id.product = btv->c.pci->device;
548 	}
549 	rc->dev.parent = &btv->c.pci->dev;
550 	rc->map_name = ir_codes;
551 	rc->driver_name = MODULE_NAME;
552 
553 	btv->remote = ir;
554 	bttv_ir_start(btv, ir);
555 
556 	/* all done */
557 	err = rc_register_device(rc);
558 	if (err)
559 		goto err_out_stop;
560 
561 	return 0;
562 
563  err_out_stop:
564 	bttv_ir_stop(btv);
565 	btv->remote = NULL;
566  err_out_free:
567 	rc_free_device(rc);
568 	kfree(ir);
569 	return err;
570 }
571 
572 void bttv_input_fini(struct bttv *btv)
573 {
574 	if (btv->remote == NULL)
575 		return;
576 
577 	bttv_ir_stop(btv);
578 	rc_unregister_device(btv->remote->dev);
579 	kfree(btv->remote);
580 	btv->remote = NULL;
581 }
582