xref: /linux/drivers/media/i2c/ir-kbd-i2c.c (revision f9bff0e31881d03badf191d3b0005839391f5f2b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  * keyboard input driver for i2c IR remote controls
5  *
6  * Copyright (c) 2000-2003 Gerd Knorr <kraxel@bytesex.org>
7  * modified for PixelView (BT878P+W/FM) by
8  *      Michal Kochanowicz <mkochano@pld.org.pl>
9  *      Christoph Bartelmus <lirc@bartelmus.de>
10  * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by
11  *      Ulrich Mueller <ulrich.mueller42@web.de>
12  * modified for em2820 based USB TV tuners by
13  *      Markus Rechberger <mrechberger@gmail.com>
14  * modified for DViCO Fusion HDTV 5 RT GOLD by
15  *      Chaogui Zhang <czhang1974@gmail.com>
16  * modified for MSI TV@nywhere Plus by
17  *      Henry Wong <henry@stuffedcow.net>
18  *      Mark Schultz <n9xmj@yahoo.com>
19  *      Brian Rogers <brian_rogers@comcast.net>
20  * modified for AVerMedia Cardbus by
21  *      Oldrich Jedlicka <oldium.pro@seznam.cz>
22  * Zilog Transmitter portions/ideas were derived from GPLv2+ sources:
23  *  - drivers/char/pctv_zilogir.[ch] from Hauppauge Broadway product
24  *	Copyright 2011 Hauppauge Computer works
25  *  - drivers/staging/media/lirc/lirc_zilog.c
26  *	Copyright (c) 2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
27  *	Michal Kochanowicz <mkochano@pld.org.pl>
28  *	Christoph Bartelmus <lirc@bartelmus.de>
29  *	Ulrich Mueller <ulrich.mueller42@web.de>
30  *	Stefan Jahn <stefan@lkcc.org>
31  *	Jerome Brock <jbrock@users.sourceforge.net>
32  *	Thomas Reitmayr (treitmayr@yahoo.com)
33  *	Mark Weaver <mark@npsl.co.uk>
34  *	Jarod Wilson <jarod@redhat.com>
35  *	Copyright (C) 2011 Andy Walls <awalls@md.metrocast.net>
36  */
37 
38 #include <asm/unaligned.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/kernel.h>
42 #include <linux/string.h>
43 #include <linux/timer.h>
44 #include <linux/delay.h>
45 #include <linux/errno.h>
46 #include <linux/slab.h>
47 #include <linux/i2c.h>
48 #include <linux/workqueue.h>
49 
50 #include <media/rc-core.h>
51 #include <media/i2c/ir-kbd-i2c.h>
52 
53 #define FLAG_TX		1
54 #define FLAG_HDPVR	2
55 
56 static bool enable_hdpvr;
57 module_param(enable_hdpvr, bool, 0644);
58 
59 static int get_key_haup_common(struct IR_i2c *ir, enum rc_proto *protocol,
60 			       u32 *scancode, u8 *ptoggle, int size)
61 {
62 	unsigned char buf[6];
63 	int start, range, toggle, dev, code, ircode, vendor;
64 
65 	/* poll IR chip */
66 	if (size != i2c_master_recv(ir->c, buf, size))
67 		return -EIO;
68 
69 	if (buf[0] & 0x80) {
70 		int offset = (size == 6) ? 3 : 0;
71 
72 		/* split rc5 data block ... */
73 		start  = (buf[offset] >> 7) &    1;
74 		range  = (buf[offset] >> 6) &    1;
75 		toggle = (buf[offset] >> 5) &    1;
76 		dev    =  buf[offset]       & 0x1f;
77 		code   = (buf[offset+1] >> 2) & 0x3f;
78 
79 		/* rc5 has two start bits
80 		 * the first bit must be one
81 		 * the second bit defines the command range:
82 		 * 1 = 0-63, 0 = 64 - 127
83 		 */
84 		if (!start)
85 			/* no key pressed */
86 			return 0;
87 
88 		/* filter out invalid key presses */
89 		ircode = (start << 12) | (toggle << 11) | (dev << 6) | code;
90 		if ((ircode & 0x1fff) == 0x1fff)
91 			return 0;
92 
93 		if (!range)
94 			code += 64;
95 
96 		dev_dbg(&ir->rc->dev,
97 			"ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n",
98 			start, range, toggle, dev, code);
99 
100 		*protocol = RC_PROTO_RC5;
101 		*scancode = RC_SCANCODE_RC5(dev, code);
102 		*ptoggle = toggle;
103 
104 		return 1;
105 	} else if (size == 6 && (buf[0] & 0x40)) {
106 		code = buf[4];
107 		dev = buf[3];
108 		vendor = get_unaligned_be16(buf + 1);
109 
110 		if (vendor == 0x800f) {
111 			*ptoggle = (dev & 0x80) != 0;
112 			*protocol = RC_PROTO_RC6_MCE;
113 			dev &= 0x7f;
114 			dev_dbg(&ir->rc->dev,
115 				"ir hauppauge (rc6-mce): t%d vendor=%d dev=%d code=%d\n",
116 				*ptoggle, vendor, dev, code);
117 		} else {
118 			*ptoggle = 0;
119 			*protocol = RC_PROTO_RC6_6A_32;
120 			dev_dbg(&ir->rc->dev,
121 				"ir hauppauge (rc6-6a-32): vendor=%d dev=%d code=%d\n",
122 				vendor, dev, code);
123 		}
124 
125 		*scancode = RC_SCANCODE_RC6_6A(vendor, dev, code);
126 
127 		return 1;
128 	}
129 
130 	return 0;
131 }
132 
133 static int get_key_haup(struct IR_i2c *ir, enum rc_proto *protocol,
134 			u32 *scancode, u8 *toggle)
135 {
136 	return get_key_haup_common(ir, protocol, scancode, toggle, 3);
137 }
138 
139 static int get_key_haup_xvr(struct IR_i2c *ir, enum rc_proto *protocol,
140 			    u32 *scancode, u8 *toggle)
141 {
142 	int ret;
143 	unsigned char buf[1] = { 0 };
144 
145 	/*
146 	 * This is the same apparent "are you ready?" poll command observed
147 	 * watching Windows driver traffic and implemented in lirc_zilog. With
148 	 * this added, we get far saner remote behavior with z8 chips on usb
149 	 * connected devices, even with the default polling interval of 100ms.
150 	 */
151 	ret = i2c_master_send(ir->c, buf, 1);
152 	if (ret != 1)
153 		return (ret < 0) ? ret : -EINVAL;
154 
155 	return get_key_haup_common(ir, protocol, scancode, toggle, 6);
156 }
157 
158 static int get_key_pixelview(struct IR_i2c *ir, enum rc_proto *protocol,
159 			     u32 *scancode, u8 *toggle)
160 {
161 	int rc;
162 	unsigned char b;
163 
164 	/* poll IR chip */
165 	rc = i2c_master_recv(ir->c, &b, 1);
166 	if (rc != 1) {
167 		dev_dbg(&ir->rc->dev, "read error\n");
168 		if (rc < 0)
169 			return rc;
170 		return -EIO;
171 	}
172 
173 	*protocol = RC_PROTO_OTHER;
174 	*scancode = b;
175 	*toggle = 0;
176 	return 1;
177 }
178 
179 static int get_key_fusionhdtv(struct IR_i2c *ir, enum rc_proto *protocol,
180 			      u32 *scancode, u8 *toggle)
181 {
182 	int rc;
183 	unsigned char buf[4];
184 
185 	/* poll IR chip */
186 	rc = i2c_master_recv(ir->c, buf, 4);
187 	if (rc != 4) {
188 		dev_dbg(&ir->rc->dev, "read error\n");
189 		if (rc < 0)
190 			return rc;
191 		return -EIO;
192 	}
193 
194 	if (buf[0] != 0 || buf[1] != 0 || buf[2] != 0 || buf[3] != 0)
195 		dev_dbg(&ir->rc->dev, "%s: %*ph\n", __func__, 4, buf);
196 
197 	/* no key pressed or signal from other ir remote */
198 	if(buf[0] != 0x1 ||  buf[1] != 0xfe)
199 		return 0;
200 
201 	*protocol = RC_PROTO_UNKNOWN;
202 	*scancode = buf[2];
203 	*toggle = 0;
204 	return 1;
205 }
206 
207 static int get_key_knc1(struct IR_i2c *ir, enum rc_proto *protocol,
208 			u32 *scancode, u8 *toggle)
209 {
210 	int rc;
211 	unsigned char b;
212 
213 	/* poll IR chip */
214 	rc = i2c_master_recv(ir->c, &b, 1);
215 	if (rc != 1) {
216 		dev_dbg(&ir->rc->dev, "read error\n");
217 		if (rc < 0)
218 			return rc;
219 		return -EIO;
220 	}
221 
222 	/* it seems that 0xFE indicates that a button is still hold
223 	   down, while 0xff indicates that no button is hold
224 	   down. 0xfe sequences are sometimes interrupted by 0xFF */
225 
226 	dev_dbg(&ir->rc->dev, "key %02x\n", b);
227 
228 	if (b == 0xff)
229 		return 0;
230 
231 	if (b == 0xfe)
232 		/* keep old data */
233 		return 1;
234 
235 	*protocol = RC_PROTO_UNKNOWN;
236 	*scancode = b;
237 	*toggle = 0;
238 	return 1;
239 }
240 
241 static int get_key_geniatech(struct IR_i2c *ir, enum rc_proto *protocol,
242 			     u32 *scancode, u8 *toggle)
243 {
244 	int i, rc;
245 	unsigned char b;
246 
247 	/* poll IR chip */
248 	for (i = 0; i < 4; i++) {
249 		rc = i2c_master_recv(ir->c, &b, 1);
250 		if (rc == 1)
251 			break;
252 		msleep(20);
253 	}
254 	if (rc != 1) {
255 		dev_dbg(&ir->rc->dev, "read error\n");
256 		if (rc < 0)
257 			return rc;
258 		return -EIO;
259 	}
260 
261 	/* don't repeat the key */
262 	if (ir->old == b)
263 		return 0;
264 	ir->old = b;
265 
266 	/* decode to RC5 */
267 	b &= 0x7f;
268 	b = (b - 1) / 2;
269 
270 	dev_dbg(&ir->rc->dev, "key %02x\n", b);
271 
272 	*protocol = RC_PROTO_RC5;
273 	*scancode = b;
274 	*toggle = ir->old >> 7;
275 	return 1;
276 }
277 
278 static int get_key_avermedia_cardbus(struct IR_i2c *ir, enum rc_proto *protocol,
279 				     u32 *scancode, u8 *toggle)
280 {
281 	unsigned char subaddr, key, keygroup;
282 	struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0,
283 				   .buf = &subaddr, .len = 1},
284 				 { .addr = ir->c->addr, .flags = I2C_M_RD,
285 				  .buf = &key, .len = 1} };
286 	subaddr = 0x0d;
287 	if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
288 		dev_dbg(&ir->rc->dev, "read error\n");
289 		return -EIO;
290 	}
291 
292 	if (key == 0xff)
293 		return 0;
294 
295 	subaddr = 0x0b;
296 	msg[1].buf = &keygroup;
297 	if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
298 		dev_dbg(&ir->rc->dev, "read error\n");
299 		return -EIO;
300 	}
301 
302 	if (keygroup == 0xff)
303 		return 0;
304 
305 	dev_dbg(&ir->rc->dev, "read key 0x%02x/0x%02x\n", key, keygroup);
306 	if (keygroup < 2 || keygroup > 4) {
307 		dev_warn(&ir->rc->dev, "warning: invalid key group 0x%02x for key 0x%02x\n",
308 			 keygroup, key);
309 	}
310 	key |= (keygroup & 1) << 6;
311 
312 	*protocol = RC_PROTO_UNKNOWN;
313 	*scancode = key;
314 	if (ir->c->addr == 0x41) /* AVerMedia EM78P153 */
315 		*scancode |= keygroup << 8;
316 	*toggle = 0;
317 	return 1;
318 }
319 
320 /* ----------------------------------------------------------------------- */
321 
322 static int ir_key_poll(struct IR_i2c *ir)
323 {
324 	enum rc_proto protocol;
325 	u32 scancode;
326 	u8 toggle;
327 	int rc;
328 
329 	dev_dbg(&ir->rc->dev, "%s\n", __func__);
330 	rc = ir->get_key(ir, &protocol, &scancode, &toggle);
331 	if (rc < 0) {
332 		dev_warn(&ir->rc->dev, "error %d\n", rc);
333 		return rc;
334 	}
335 
336 	if (rc) {
337 		dev_dbg(&ir->rc->dev, "%s: proto = 0x%04x, scancode = 0x%08x\n",
338 			__func__, protocol, scancode);
339 		rc_keydown(ir->rc, protocol, scancode, toggle);
340 	}
341 	return 0;
342 }
343 
344 static void ir_work(struct work_struct *work)
345 {
346 	int rc;
347 	struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work);
348 
349 	/*
350 	 * If the transmit code is holding the lock, skip polling for
351 	 * IR, we'll get it to it next time round
352 	 */
353 	if (mutex_trylock(&ir->lock)) {
354 		rc = ir_key_poll(ir);
355 		mutex_unlock(&ir->lock);
356 		if (rc == -ENODEV) {
357 			rc_unregister_device(ir->rc);
358 			ir->rc = NULL;
359 			return;
360 		}
361 	}
362 
363 	schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling_interval));
364 }
365 
366 static int ir_open(struct rc_dev *dev)
367 {
368 	struct IR_i2c *ir = dev->priv;
369 
370 	schedule_delayed_work(&ir->work, 0);
371 
372 	return 0;
373 }
374 
375 static void ir_close(struct rc_dev *dev)
376 {
377 	struct IR_i2c *ir = dev->priv;
378 
379 	cancel_delayed_work_sync(&ir->work);
380 }
381 
382 /* Zilog Transmit Interface */
383 #define XTAL_FREQ		18432000
384 
385 #define ZILOG_SEND		0x80
386 #define ZILOG_UIR_END		0x40
387 #define ZILOG_INIT_END		0x20
388 #define ZILOG_LIR_END		0x10
389 
390 #define ZILOG_STATUS_OK		0x80
391 #define ZILOG_STATUS_TX		0x40
392 #define ZILOG_STATUS_SET	0x20
393 
394 /*
395  * As you can see here, very few different lengths of pulse and space
396  * can be encoded. This means that the hardware does not work well with
397  * recorded IR. It's best to work with generated IR, like from ir-ctl or
398  * the in-kernel encoders.
399  */
400 struct code_block {
401 	u8	length;
402 	u16	pulse[7];	/* not aligned */
403 	u8	carrier_pulse;
404 	u8	carrier_space;
405 	u16	space[8];	/* not aligned */
406 	u8	codes[61];
407 	u8	csum[2];
408 } __packed;
409 
410 static int send_data_block(struct IR_i2c *ir, int cmd,
411 			   struct code_block *code_block)
412 {
413 	int i, j, ret;
414 	u8 buf[5], *p;
415 
416 	p = &code_block->length;
417 	for (i = 0; p < code_block->csum; i++)
418 		code_block->csum[i & 1] ^= *p++;
419 
420 	p = &code_block->length;
421 
422 	for (i = 0; i < sizeof(*code_block);) {
423 		int tosend = sizeof(*code_block) - i;
424 
425 		if (tosend > 4)
426 			tosend = 4;
427 		buf[0] = i + 1;
428 		for (j = 0; j < tosend; ++j)
429 			buf[1 + j] = p[i + j];
430 		dev_dbg(&ir->rc->dev, "%*ph", tosend + 1, buf);
431 		ret = i2c_master_send(ir->tx_c, buf, tosend + 1);
432 		if (ret != tosend + 1) {
433 			dev_dbg(&ir->rc->dev,
434 				"i2c_master_send failed with %d\n", ret);
435 			return ret < 0 ? ret : -EIO;
436 		}
437 		i += tosend;
438 	}
439 
440 	buf[0] = 0;
441 	buf[1] = cmd;
442 	ret = i2c_master_send(ir->tx_c, buf, 2);
443 	if (ret != 2) {
444 		dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret);
445 		return ret < 0 ? ret : -EIO;
446 	}
447 
448 	usleep_range(2000, 5000);
449 
450 	ret = i2c_master_send(ir->tx_c, buf, 1);
451 	if (ret != 1) {
452 		dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret);
453 		return ret < 0 ? ret : -EIO;
454 	}
455 
456 	return 0;
457 }
458 
459 static int zilog_init(struct IR_i2c *ir)
460 {
461 	struct code_block code_block = { .length = sizeof(code_block) };
462 	u8 buf[4];
463 	int ret;
464 
465 	put_unaligned_be16(0x1000, &code_block.pulse[3]);
466 
467 	ret = send_data_block(ir, ZILOG_INIT_END, &code_block);
468 	if (ret)
469 		return ret;
470 
471 	ret = i2c_master_recv(ir->tx_c, buf, 4);
472 	if (ret != 4) {
473 		dev_err(&ir->c->dev, "failed to retrieve firmware version: %d\n",
474 			ret);
475 		return ret < 0 ? ret : -EIO;
476 	}
477 
478 	dev_info(&ir->c->dev, "Zilog/Hauppauge IR blaster firmware version %d.%d.%d\n",
479 		 buf[1], buf[2], buf[3]);
480 
481 	return 0;
482 }
483 
484 /*
485  * If the last slot for pulse is the same as the current slot for pulse,
486  * then use slot no 7.
487  */
488 static void copy_codes(u8 *dst, u8 *src, unsigned int count)
489 {
490 	u8 c, last = 0xff;
491 
492 	while (count--) {
493 		c = *src++;
494 		if ((c & 0xf0) == last) {
495 			*dst++ = 0x70 | (c & 0xf);
496 		} else {
497 			*dst++ = c;
498 			last = c & 0xf0;
499 		}
500 	}
501 }
502 
503 /*
504  * When looking for repeats, we don't care about the trailing space. This
505  * is set to the shortest possible anyway.
506  */
507 static int cmp_no_trail(u8 *a, u8 *b, unsigned int count)
508 {
509 	while (--count) {
510 		if (*a++ != *b++)
511 			return 1;
512 	}
513 
514 	return (*a & 0xf0) - (*b & 0xf0);
515 }
516 
517 static int find_slot(u16 *array, unsigned int size, u16 val)
518 {
519 	int i;
520 
521 	for (i = 0; i < size; i++) {
522 		if (get_unaligned_be16(&array[i]) == val) {
523 			return i;
524 		} else if (!array[i]) {
525 			put_unaligned_be16(val, &array[i]);
526 			return i;
527 		}
528 	}
529 
530 	return -1;
531 }
532 
533 static int zilog_ir_format(struct rc_dev *rcdev, unsigned int *txbuf,
534 			   unsigned int count, struct code_block *code_block)
535 {
536 	struct IR_i2c *ir = rcdev->priv;
537 	int rep, i, l, p = 0, s, c = 0;
538 	bool repeating;
539 	u8 codes[174];
540 
541 	code_block->carrier_pulse = DIV_ROUND_CLOSEST(
542 			ir->duty_cycle * XTAL_FREQ / 1000, ir->carrier);
543 	code_block->carrier_space = DIV_ROUND_CLOSEST(
544 			(100 - ir->duty_cycle) * XTAL_FREQ / 1000, ir->carrier);
545 
546 	for (i = 0; i < count; i++) {
547 		if (c >= ARRAY_SIZE(codes) - 1) {
548 			dev_warn(&rcdev->dev, "IR too long, cannot transmit\n");
549 			return -EINVAL;
550 		}
551 
552 		/*
553 		 * Lengths more than 142220us cannot be encoded; also
554 		 * this checks for multiply overflow
555 		 */
556 		if (txbuf[i] > 142220)
557 			return -EINVAL;
558 
559 		l = DIV_ROUND_CLOSEST((XTAL_FREQ / 1000) * txbuf[i], 40000);
560 
561 		if (i & 1) {
562 			s = find_slot(code_block->space,
563 				      ARRAY_SIZE(code_block->space), l);
564 			if (s == -1) {
565 				dev_warn(&rcdev->dev, "Too many different lengths spaces, cannot transmit");
566 				return -EINVAL;
567 			}
568 
569 			/* We have a pulse and space */
570 			codes[c++] = (p << 4) | s;
571 		} else {
572 			p = find_slot(code_block->pulse,
573 				      ARRAY_SIZE(code_block->pulse), l);
574 			if (p == -1) {
575 				dev_warn(&rcdev->dev, "Too many different lengths pulses, cannot transmit");
576 				return -EINVAL;
577 			}
578 		}
579 	}
580 
581 	/* We have to encode the trailing pulse. Find the shortest space */
582 	s = 0;
583 	for (i = 1; i < ARRAY_SIZE(code_block->space); i++) {
584 		u16 d = get_unaligned_be16(&code_block->space[i]);
585 
586 		if (get_unaligned_be16(&code_block->space[s]) > d)
587 			s = i;
588 	}
589 
590 	codes[c++] = (p << 4) | s;
591 
592 	dev_dbg(&rcdev->dev, "generated %d codes\n", c);
593 
594 	/*
595 	 * Are the last N codes (so pulse + space) repeating 3 times?
596 	 * if so we can shorten the codes list and use code 0xc0 to repeat
597 	 * them.
598 	 */
599 	repeating = false;
600 
601 	for (rep = c / 3; rep >= 1; rep--) {
602 		if (!memcmp(&codes[c - rep * 3], &codes[c - rep * 2], rep) &&
603 		    !cmp_no_trail(&codes[c - rep], &codes[c - rep * 2], rep)) {
604 			repeating = true;
605 			break;
606 		}
607 	}
608 
609 	if (repeating) {
610 		/* first copy any leading non-repeating */
611 		int leading = c - rep * 3;
612 
613 		if (leading >= ARRAY_SIZE(code_block->codes) - 3 - rep) {
614 			dev_warn(&rcdev->dev, "IR too long, cannot transmit\n");
615 			return -EINVAL;
616 		}
617 
618 		dev_dbg(&rcdev->dev, "found trailing %d repeat\n", rep);
619 		copy_codes(code_block->codes, codes, leading);
620 		code_block->codes[leading] = 0x82;
621 		copy_codes(code_block->codes + leading + 1, codes + leading,
622 			   rep);
623 		c = leading + 1 + rep;
624 		code_block->codes[c++] = 0xc0;
625 	} else {
626 		if (c >= ARRAY_SIZE(code_block->codes) - 3) {
627 			dev_warn(&rcdev->dev, "IR too long, cannot transmit\n");
628 			return -EINVAL;
629 		}
630 
631 		dev_dbg(&rcdev->dev, "found no trailing repeat\n");
632 		code_block->codes[0] = 0x82;
633 		copy_codes(code_block->codes + 1, codes, c);
634 		c++;
635 		code_block->codes[c++] = 0xc4;
636 	}
637 
638 	while (c < ARRAY_SIZE(code_block->codes))
639 		code_block->codes[c++] = 0x83;
640 
641 	return 0;
642 }
643 
644 static int zilog_tx(struct rc_dev *rcdev, unsigned int *txbuf,
645 		    unsigned int count)
646 {
647 	struct IR_i2c *ir = rcdev->priv;
648 	struct code_block code_block = { .length = sizeof(code_block) };
649 	u8 buf[2];
650 	int ret, i;
651 
652 	ret = zilog_ir_format(rcdev, txbuf, count, &code_block);
653 	if (ret)
654 		return ret;
655 
656 	ret = mutex_lock_interruptible(&ir->lock);
657 	if (ret)
658 		return ret;
659 
660 	ret = send_data_block(ir, ZILOG_UIR_END, &code_block);
661 	if (ret)
662 		goto out_unlock;
663 
664 	ret = i2c_master_recv(ir->tx_c, buf, 1);
665 	if (ret != 1) {
666 		dev_err(&ir->rc->dev, "i2c_master_recv failed with %d\n", ret);
667 		goto out_unlock;
668 	}
669 
670 	dev_dbg(&ir->rc->dev, "code set status: %02x\n", buf[0]);
671 
672 	if (buf[0] != (ZILOG_STATUS_OK | ZILOG_STATUS_SET)) {
673 		dev_err(&ir->rc->dev, "unexpected IR TX response %02x\n",
674 			buf[0]);
675 		ret = -EIO;
676 		goto out_unlock;
677 	}
678 
679 	buf[0] = 0x00;
680 	buf[1] = ZILOG_SEND;
681 
682 	ret = i2c_master_send(ir->tx_c, buf, 2);
683 	if (ret != 2) {
684 		dev_err(&ir->rc->dev, "i2c_master_send failed with %d\n", ret);
685 		if (ret >= 0)
686 			ret = -EIO;
687 		goto out_unlock;
688 	}
689 
690 	dev_dbg(&ir->rc->dev, "send command sent\n");
691 
692 	/*
693 	 * This bit NAKs until the device is ready, so we retry it
694 	 * sleeping a bit each time.  This seems to be what the windows
695 	 * driver does, approximately.
696 	 * Try for up to 1s.
697 	 */
698 	for (i = 0; i < 20; ++i) {
699 		set_current_state(TASK_UNINTERRUPTIBLE);
700 		schedule_timeout(msecs_to_jiffies(50));
701 		ret = i2c_master_send(ir->tx_c, buf, 1);
702 		if (ret == 1)
703 			break;
704 		dev_dbg(&ir->rc->dev,
705 			"NAK expected: i2c_master_send failed with %d (try %d)\n",
706 			ret, i + 1);
707 	}
708 
709 	if (ret != 1) {
710 		dev_err(&ir->rc->dev,
711 			"IR TX chip never got ready: last i2c_master_send failed with %d\n",
712 			ret);
713 		if (ret >= 0)
714 			ret = -EIO;
715 		goto out_unlock;
716 	}
717 
718 	ret = i2c_master_recv(ir->tx_c, buf, 1);
719 	if (ret != 1) {
720 		dev_err(&ir->rc->dev, "i2c_master_recv failed with %d\n", ret);
721 		ret = -EIO;
722 		goto out_unlock;
723 	} else if (buf[0] != ZILOG_STATUS_OK) {
724 		dev_err(&ir->rc->dev, "unexpected IR TX response #2: %02x\n",
725 			buf[0]);
726 		ret = -EIO;
727 		goto out_unlock;
728 	}
729 	dev_dbg(&ir->rc->dev, "transmit complete\n");
730 
731 	/* Oh good, it worked */
732 	ret = count;
733 out_unlock:
734 	mutex_unlock(&ir->lock);
735 
736 	return ret;
737 }
738 
739 static int zilog_tx_carrier(struct rc_dev *dev, u32 carrier)
740 {
741 	struct IR_i2c *ir = dev->priv;
742 
743 	if (carrier > 500000 || carrier < 20000)
744 		return -EINVAL;
745 
746 	ir->carrier = carrier;
747 
748 	return 0;
749 }
750 
751 static int zilog_tx_duty_cycle(struct rc_dev *dev, u32 duty_cycle)
752 {
753 	struct IR_i2c *ir = dev->priv;
754 
755 	ir->duty_cycle = duty_cycle;
756 
757 	return 0;
758 }
759 
760 static int ir_probe(struct i2c_client *client)
761 {
762 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
763 	char *ir_codes = NULL;
764 	const char *name = NULL;
765 	u64 rc_proto = RC_PROTO_BIT_UNKNOWN;
766 	struct IR_i2c *ir;
767 	struct rc_dev *rc = NULL;
768 	struct i2c_adapter *adap = client->adapter;
769 	unsigned short addr = client->addr;
770 	bool probe_tx = (id->driver_data & FLAG_TX) != 0;
771 	int err;
772 
773 	if ((id->driver_data & FLAG_HDPVR) && !enable_hdpvr) {
774 		dev_err(&client->dev, "IR for HDPVR is known to cause problems during recording, use enable_hdpvr modparam to enable\n");
775 		return -ENODEV;
776 	}
777 
778 	ir = devm_kzalloc(&client->dev, sizeof(*ir), GFP_KERNEL);
779 	if (!ir)
780 		return -ENOMEM;
781 
782 	ir->c = client;
783 	ir->polling_interval = DEFAULT_POLLING_INTERVAL;
784 	i2c_set_clientdata(client, ir);
785 
786 	switch(addr) {
787 	case 0x64:
788 		name        = "Pixelview";
789 		ir->get_key = get_key_pixelview;
790 		rc_proto    = RC_PROTO_BIT_OTHER;
791 		ir_codes    = RC_MAP_EMPTY;
792 		break;
793 	case 0x18:
794 	case 0x1f:
795 	case 0x1a:
796 		name        = "Hauppauge";
797 		ir->get_key = get_key_haup;
798 		rc_proto    = RC_PROTO_BIT_RC5;
799 		ir_codes    = RC_MAP_HAUPPAUGE;
800 		break;
801 	case 0x30:
802 		name        = "KNC One";
803 		ir->get_key = get_key_knc1;
804 		rc_proto    = RC_PROTO_BIT_OTHER;
805 		ir_codes    = RC_MAP_EMPTY;
806 		break;
807 	case 0x33:
808 		name        = "Geniatech";
809 		ir->get_key = get_key_geniatech;
810 		rc_proto    = RC_PROTO_BIT_RC5;
811 		ir_codes    = RC_MAP_TOTAL_MEDIA_IN_HAND_02;
812 		ir->old     = 0xfc;
813 		break;
814 	case 0x6b:
815 		name        = "FusionHDTV";
816 		ir->get_key = get_key_fusionhdtv;
817 		rc_proto    = RC_PROTO_BIT_UNKNOWN;
818 		ir_codes    = RC_MAP_FUSIONHDTV_MCE;
819 		break;
820 	case 0x40:
821 		name        = "AVerMedia Cardbus remote";
822 		ir->get_key = get_key_avermedia_cardbus;
823 		rc_proto    = RC_PROTO_BIT_OTHER;
824 		ir_codes    = RC_MAP_AVERMEDIA_CARDBUS;
825 		break;
826 	case 0x41:
827 		name        = "AVerMedia EM78P153";
828 		ir->get_key = get_key_avermedia_cardbus;
829 		rc_proto    = RC_PROTO_BIT_OTHER;
830 		/* RM-KV remote, seems to be same as RM-K6 */
831 		ir_codes    = RC_MAP_AVERMEDIA_M733A_RM_K6;
832 		break;
833 	case 0x71:
834 		name        = "Hauppauge/Zilog Z8";
835 		ir->get_key = get_key_haup_xvr;
836 		rc_proto    = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_MCE |
837 							RC_PROTO_BIT_RC6_6A_32;
838 		ir_codes    = RC_MAP_HAUPPAUGE;
839 		ir->polling_interval = 125;
840 		probe_tx = true;
841 		break;
842 	}
843 
844 	/* Let the caller override settings */
845 	if (client->dev.platform_data) {
846 		const struct IR_i2c_init_data *init_data =
847 						client->dev.platform_data;
848 
849 		ir_codes = init_data->ir_codes;
850 		rc = init_data->rc_dev;
851 
852 		name = init_data->name;
853 		if (init_data->type)
854 			rc_proto = init_data->type;
855 
856 		if (init_data->polling_interval)
857 			ir->polling_interval = init_data->polling_interval;
858 
859 		switch (init_data->internal_get_key_func) {
860 		case IR_KBD_GET_KEY_CUSTOM:
861 			/* The bridge driver provided us its own function */
862 			ir->get_key = init_data->get_key;
863 			break;
864 		case IR_KBD_GET_KEY_PIXELVIEW:
865 			ir->get_key = get_key_pixelview;
866 			break;
867 		case IR_KBD_GET_KEY_HAUP:
868 			ir->get_key = get_key_haup;
869 			break;
870 		case IR_KBD_GET_KEY_KNC1:
871 			ir->get_key = get_key_knc1;
872 			break;
873 		case IR_KBD_GET_KEY_GENIATECH:
874 			ir->get_key = get_key_geniatech;
875 			break;
876 		case IR_KBD_GET_KEY_FUSIONHDTV:
877 			ir->get_key = get_key_fusionhdtv;
878 			break;
879 		case IR_KBD_GET_KEY_HAUP_XVR:
880 			ir->get_key = get_key_haup_xvr;
881 			break;
882 		case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS:
883 			ir->get_key = get_key_avermedia_cardbus;
884 			break;
885 		}
886 	}
887 
888 	if (!rc) {
889 		/*
890 		 * If platform_data doesn't specify rc_dev, initialize it
891 		 * internally
892 		 */
893 		rc = rc_allocate_device(RC_DRIVER_SCANCODE);
894 		if (!rc)
895 			return -ENOMEM;
896 	}
897 	ir->rc = rc;
898 
899 	/* Make sure we are all setup before going on */
900 	if (!name || !ir->get_key || !rc_proto || !ir_codes) {
901 		dev_warn(&client->dev, "Unsupported device at address 0x%02x\n",
902 			 addr);
903 		err = -ENODEV;
904 		goto err_out_free;
905 	}
906 
907 	ir->ir_codes = ir_codes;
908 
909 	snprintf(ir->phys, sizeof(ir->phys), "%s/%s", dev_name(&adap->dev),
910 		 dev_name(&client->dev));
911 
912 	/*
913 	 * Initialize input_dev fields
914 	 * It doesn't make sense to allow overriding them via platform_data
915 	 */
916 	rc->input_id.bustype = BUS_I2C;
917 	rc->input_phys       = ir->phys;
918 	rc->device_name	     = name;
919 	rc->dev.parent       = &client->dev;
920 	rc->priv             = ir;
921 	rc->open             = ir_open;
922 	rc->close            = ir_close;
923 
924 	/*
925 	 * Initialize the other fields of rc_dev
926 	 */
927 	rc->map_name       = ir->ir_codes;
928 	rc->allowed_protocols = rc_proto;
929 	if (!rc->driver_name)
930 		rc->driver_name = KBUILD_MODNAME;
931 
932 	mutex_init(&ir->lock);
933 
934 	INIT_DELAYED_WORK(&ir->work, ir_work);
935 
936 	if (probe_tx) {
937 		ir->tx_c = i2c_new_dummy_device(client->adapter, 0x70);
938 		if (IS_ERR(ir->tx_c)) {
939 			dev_err(&client->dev, "failed to setup tx i2c address");
940 			err = PTR_ERR(ir->tx_c);
941 			goto err_out_free;
942 		} else if (!zilog_init(ir)) {
943 			ir->carrier = 38000;
944 			ir->duty_cycle = 40;
945 			rc->tx_ir = zilog_tx;
946 			rc->s_tx_carrier = zilog_tx_carrier;
947 			rc->s_tx_duty_cycle = zilog_tx_duty_cycle;
948 		}
949 	}
950 
951 	err = rc_register_device(rc);
952 	if (err)
953 		goto err_out_free;
954 
955 	return 0;
956 
957  err_out_free:
958 	if (!IS_ERR(ir->tx_c))
959 		i2c_unregister_device(ir->tx_c);
960 
961 	/* Only frees rc if it were allocated internally */
962 	rc_free_device(rc);
963 	return err;
964 }
965 
966 static void ir_remove(struct i2c_client *client)
967 {
968 	struct IR_i2c *ir = i2c_get_clientdata(client);
969 
970 	cancel_delayed_work_sync(&ir->work);
971 
972 	i2c_unregister_device(ir->tx_c);
973 
974 	rc_unregister_device(ir->rc);
975 }
976 
977 static const struct i2c_device_id ir_kbd_id[] = {
978 	/* Generic entry for any IR receiver */
979 	{ "ir_video", 0 },
980 	/* IR device specific entries should be added here */
981 	{ "ir_z8f0811_haup", FLAG_TX },
982 	{ "ir_z8f0811_hdpvr", FLAG_TX | FLAG_HDPVR },
983 	{ }
984 };
985 MODULE_DEVICE_TABLE(i2c, ir_kbd_id);
986 
987 static struct i2c_driver ir_kbd_driver = {
988 	.driver = {
989 		.name   = "ir-kbd-i2c",
990 	},
991 	.probe          = ir_probe,
992 	.remove         = ir_remove,
993 	.id_table       = ir_kbd_id,
994 };
995 
996 module_i2c_driver(ir_kbd_driver);
997 
998 /* ----------------------------------------------------------------------- */
999 
1000 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
1001 MODULE_DESCRIPTION("input driver for i2c IR remote controls");
1002 MODULE_LICENSE("GPL");
1003