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