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