]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/i2c/ir-kbd-i2c.c
Merge branches 'for-4.11/upstream-fixes', 'for-4.12/accutouch', 'for-4.12/cp2112...
[mirror_ubuntu-artful-kernel.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>
1da177e4
LT
21 *
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
1da177e4
LT
32 */
33
00bb8207 34#include <asm/unaligned.h>
1da177e4 35#include <linux/module.h>
1da177e4
LT
36#include <linux/init.h>
37#include <linux/kernel.h>
1da177e4
LT
38#include <linux/string.h>
39#include <linux/timer.h>
40#include <linux/delay.h>
41#include <linux/errno.h>
42#include <linux/slab.h>
43#include <linux/i2c.h>
44#include <linux/workqueue.h>
674434c6 45
6bda9644 46#include <media/rc-core.h>
b5dcee22 47#include <media/i2c/ir-kbd-i2c.h>
c365864f 48
1da177e4
LT
49/* ----------------------------------------------------------------------- */
50/* insmod parameters */
51
52static int debug;
53module_param(debug, int, 0644); /* debug level (0,1,2) */
54
bf47e4e4 55
727e625c 56#define MODULE_NAME "ir-kbd-i2c"
1da177e4 57#define dprintk(level, fmt, arg...) if (debug >= level) \
727e625c 58 printk(KERN_DEBUG MODULE_NAME ": " fmt , ## arg)
1da177e4
LT
59
60/* ----------------------------------------------------------------------- */
61
4dd9bb91 62static int get_key_haup_common(struct IR_i2c *ir, enum rc_type *protocol,
00bb8207 63 u32 *scancode, u8 *ptoggle, int size)
1da177e4 64{
39cf1e81 65 unsigned char buf[6];
00bb8207 66 int start, range, toggle, dev, code, ircode, vendor;
1da177e4
LT
67
68 /* poll IR chip */
c668f32d 69 if (size != i2c_master_recv(ir->c, buf, size))
1da177e4
LT
70 return -EIO;
71
00bb8207
SY
72 if (buf[0] & 0x80) {
73 int offset = (size == 6) ? 3 : 0;
1da177e4 74
00bb8207
SY
75 /* split rc5 data block ... */
76 start = (buf[offset] >> 7) & 1;
77 range = (buf[offset] >> 6) & 1;
78 toggle = (buf[offset] >> 5) & 1;
79 dev = buf[offset] & 0x1f;
80 code = (buf[offset+1] >> 2) & 0x3f;
4dd9bb91 81
00bb8207
SY
82 /* rc5 has two start bits
83 * the first bit must be one
84 * the second bit defines the command range:
85 * 1 = 0-63, 0 = 64 - 127
86 */
87 if (!start)
88 /* no key pressed */
89 return 0;
34c08029 90
00bb8207
SY
91 /* filter out invalid key presses */
92 ircode = (start << 12) | (toggle << 11) | (dev << 6) | code;
93 if ((ircode & 0x1fff) == 0x1fff)
94 return 0;
cc7093df 95
00bb8207
SY
96 if (!range)
97 code += 64;
1da177e4 98
00bb8207
SY
99 dprintk(1, "ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n",
100 start, range, toggle, dev, code);
101
102 *protocol = RC_TYPE_RC5;
103 *scancode = RC_SCANCODE_RC5(dev, code);
104 *ptoggle = toggle;
105
106 return 1;
107 } else if (size == 6 && (buf[0] & 0x40)) {
108 code = buf[4];
109 dev = buf[3];
110 vendor = get_unaligned_be16(buf + 1);
111
112 if (vendor == 0x800f) {
113 *ptoggle = (dev & 0x80) != 0;
114 *protocol = RC_TYPE_RC6_MCE;
115 dev &= 0x7f;
116 dprintk(1, "ir hauppauge (rc6-mce): t%d vendor=%d dev=%d code=%d\n",
9cdbe14f 117 *ptoggle, vendor, dev, code);
00bb8207
SY
118 } else {
119 *ptoggle = 0;
120 *protocol = RC_TYPE_RC6_6A_32;
121 dprintk(1, "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;
1da177e4
LT
131}
132
4dd9bb91
DH
133static int get_key_haup(struct IR_i2c *ir, enum rc_type *protocol,
134 u32 *scancode, u8 *toggle)
39cf1e81 135{
00bb8207 136 return get_key_haup_common(ir, protocol, scancode, toggle, 3);
39cf1e81
JF
137}
138
4dd9bb91
DH
139static int get_key_haup_xvr(struct IR_i2c *ir, enum rc_type *protocol,
140 u32 *scancode, u8 *toggle)
39cf1e81 141{
8df59918
JW
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
00bb8207 155 return get_key_haup_common(ir, protocol, scancode, toggle, 6);
39cf1e81
JF
156}
157
4dd9bb91
DH
158static int get_key_pixelview(struct IR_i2c *ir, enum rc_type *protocol,
159 u32 *scancode, u8 *toggle)
1da177e4 160{
4ac97914 161 unsigned char b;
1da177e4
LT
162
163 /* poll IR chip */
c668f32d 164 if (1 != i2c_master_recv(ir->c, &b, 1)) {
1da177e4
LT
165 dprintk(1,"read error\n");
166 return -EIO;
167 }
4dd9bb91
DH
168
169 *protocol = RC_TYPE_OTHER;
170 *scancode = b;
171 *toggle = 0;
1da177e4
LT
172 return 1;
173}
174
4dd9bb91
DH
175static int get_key_fusionhdtv(struct IR_i2c *ir, enum rc_type *protocol,
176 u32 *scancode, u8 *toggle)
d54d6980
CZ
177{
178 unsigned char buf[4];
179
180 /* poll IR chip */
c668f32d 181 if (4 != i2c_master_recv(ir->c, buf, 4)) {
d54d6980
CZ
182 dprintk(1,"read error\n");
183 return -EIO;
184 }
185
186 if(buf[0] !=0 || buf[1] !=0 || buf[2] !=0 || buf[3] != 0)
7e28adb2 187 dprintk(2, "%s: 0x%2x 0x%2x 0x%2x 0x%2x\n", __func__,
d54d6980
CZ
188 buf[0], buf[1], buf[2], buf[3]);
189
190 /* no key pressed or signal from other ir remote */
191 if(buf[0] != 0x1 || buf[1] != 0xfe)
192 return 0;
193
4dd9bb91
DH
194 *protocol = RC_TYPE_UNKNOWN;
195 *scancode = buf[2];
196 *toggle = 0;
d54d6980 197 return 1;
d54d6980
CZ
198}
199
4dd9bb91
DH
200static int get_key_knc1(struct IR_i2c *ir, enum rc_type *protocol,
201 u32 *scancode, u8 *toggle)
1da177e4
LT
202{
203 unsigned char b;
204
205 /* poll IR chip */
c668f32d 206 if (1 != i2c_master_recv(ir->c, &b, 1)) {
1da177e4
LT
207 dprintk(1,"read error\n");
208 return -EIO;
209 }
210
211 /* it seems that 0xFE indicates that a button is still hold
4f9c05aa
MCC
212 down, while 0xff indicates that no button is hold
213 down. 0xfe sequences are sometimes interrupted by 0xFF */
1da177e4
LT
214
215 dprintk(2,"key %02x\n", b);
216
4f9c05aa 217 if (b == 0xff)
1da177e4
LT
218 return 0;
219
4f9c05aa 220 if (b == 0xfe)
1da177e4
LT
221 /* keep old data */
222 return 1;
223
4dd9bb91
DH
224 *protocol = RC_TYPE_UNKNOWN;
225 *scancode = b;
226 *toggle = 0;
1da177e4
LT
227 return 1;
228}
229
4dd9bb91
DH
230static int get_key_avermedia_cardbus(struct IR_i2c *ir, enum rc_type *protocol,
231 u32 *scancode, u8 *toggle)
cb3bf504
OJ
232{
233 unsigned char subaddr, key, keygroup;
c668f32d 234 struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0,
cb3bf504 235 .buf = &subaddr, .len = 1},
c668f32d 236 { .addr = ir->c->addr, .flags = I2C_M_RD,
cb3bf504
OJ
237 .buf = &key, .len = 1} };
238 subaddr = 0x0d;
c668f32d 239 if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
cb3bf504
OJ
240 dprintk(1, "read error\n");
241 return -EIO;
242 }
243
244 if (key == 0xff)
245 return 0;
246
247 subaddr = 0x0b;
248 msg[1].buf = &keygroup;
c668f32d 249 if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
cb3bf504
OJ
250 dprintk(1, "read error\n");
251 return -EIO;
252 }
253
254 if (keygroup == 0xff)
255 return 0;
256
257 dprintk(1, "read key 0x%02x/0x%02x\n", key, keygroup);
34fe2784 258 if (keygroup < 2 || keygroup > 4) {
cb3bf504
OJ
259 /* Only a warning */
260 dprintk(1, "warning: invalid key group 0x%02x for key 0x%02x\n",
261 keygroup, key);
262 }
263 key |= (keygroup & 1) << 6;
264
4dd9bb91
DH
265 *protocol = RC_TYPE_UNKNOWN;
266 *scancode = key;
267 if (ir->c->addr == 0x41) /* AVerMedia EM78P153 */
268 *scancode |= keygroup << 8;
269 *toggle = 0;
cb3bf504
OJ
270 return 1;
271}
272
1da177e4
LT
273/* ----------------------------------------------------------------------- */
274
90bf3aab 275static int ir_key_poll(struct IR_i2c *ir)
1da177e4 276{
4dd9bb91
DH
277 enum rc_type protocol;
278 u32 scancode;
279 u8 toggle;
1da177e4
LT
280 int rc;
281
e6bcb2f3 282 dprintk(3, "%s\n", __func__);
4dd9bb91 283 rc = ir->get_key(ir, &protocol, &scancode, &toggle);
1da177e4
LT
284 if (rc < 0) {
285 dprintk(2,"error\n");
90bf3aab 286 return rc;
1da177e4
LT
287 }
288
e6bcb2f3 289 if (rc) {
120703f9
DH
290 dprintk(1, "%s: proto = 0x%04x, scancode = 0x%08x\n",
291 __func__, protocol, scancode);
292 rc_keydown(ir->rc, protocol, scancode, toggle);
e6bcb2f3 293 }
90bf3aab 294 return 0;
1da177e4
LT
295}
296
c4028958 297static void ir_work(struct work_struct *work)
1da177e4 298{
90bf3aab 299 int rc;
c1089bdc 300 struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work);
8083c520 301
90bf3aab
MCC
302 rc = ir_key_poll(ir);
303 if (rc == -ENODEV) {
304 rc_unregister_device(ir->rc);
305 ir->rc = NULL;
306 return;
307 }
308
c72ba8e6 309 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling_interval));
1da177e4
LT
310}
311
312/* ----------------------------------------------------------------------- */
313
c668f32d 314static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1da177e4 315{
02858eed 316 char *ir_codes = NULL;
9a4cc5ac 317 const char *name = NULL;
c003ab1b 318 u64 rc_type = RC_BIT_UNKNOWN;
afd1a0c9 319 struct IR_i2c *ir;
2eb25832 320 struct rc_dev *rc = NULL;
c668f32d
JD
321 struct i2c_adapter *adap = client->adapter;
322 unsigned short addr = client->addr;
b07b4783 323 int err;
1da177e4 324
c02b211d 325 ir = devm_kzalloc(&client->dev, sizeof(*ir), GFP_KERNEL);
2eb25832
MCC
326 if (!ir)
327 return -ENOMEM;
b7df3910 328
c668f32d 329 ir->c = client;
c72ba8e6 330 ir->polling_interval = DEFAULT_POLLING_INTERVAL;
c668f32d 331 i2c_set_clientdata(client, ir);
d21838dd 332
1da177e4
LT
333 switch(addr) {
334 case 0x64:
335 name = "Pixelview";
336 ir->get_key = get_key_pixelview;
c003ab1b 337 rc_type = RC_BIT_OTHER;
02858eed 338 ir_codes = RC_MAP_EMPTY;
1da177e4 339 break;
1da177e4 340 case 0x18:
ca39d84d 341 case 0x1f:
1da177e4
LT
342 case 0x1a:
343 name = "Hauppauge";
344 ir->get_key = get_key_haup;
c003ab1b 345 rc_type = RC_BIT_RC5;
20624106 346 ir_codes = RC_MAP_HAUPPAUGE;
1da177e4
LT
347 break;
348 case 0x30:
d5e52653
MCC
349 name = "KNC One";
350 ir->get_key = get_key_knc1;
c003ab1b 351 rc_type = RC_BIT_OTHER;
02858eed 352 ir_codes = RC_MAP_EMPTY;
1da177e4 353 break;
d54d6980 354 case 0x6b:
3c44358c
MK
355 name = "FusionHDTV";
356 ir->get_key = get_key_fusionhdtv;
4dd9bb91 357 rc_type = RC_BIT_UNKNOWN;
02858eed 358 ir_codes = RC_MAP_FUSIONHDTV_MCE;
d54d6980 359 break;
cb3bf504
OJ
360 case 0x40:
361 name = "AVerMedia Cardbus remote";
362 ir->get_key = get_key_avermedia_cardbus;
c003ab1b 363 rc_type = RC_BIT_OTHER;
02858eed 364 ir_codes = RC_MAP_AVERMEDIA_CARDBUS;
cb3bf504 365 break;
34fe2784
OZ
366 case 0x41:
367 name = "AVerMedia EM78P153";
368 ir->get_key = get_key_avermedia_cardbus;
369 rc_type = RC_BIT_OTHER;
370 /* RM-KV remote, seems to be same as RM-K6 */
371 ir_codes = RC_MAP_AVERMEDIA_M733A_RM_K6;
372 break;
c69a4af6
AW
373 case 0x71:
374 name = "Hauppauge/Zilog Z8";
375 ir->get_key = get_key_haup_xvr;
00bb8207 376 rc_type = RC_BIT_RC5 | RC_BIT_RC6_MCE | RC_BIT_RC6_6A_32;
20624106 377 ir_codes = RC_MAP_HAUPPAUGE;
c69a4af6 378 break;
1da177e4
LT
379 }
380
4d7a2d67
JD
381 /* Let the caller override settings */
382 if (client->dev.platform_data) {
383 const struct IR_i2c_init_data *init_data =
384 client->dev.platform_data;
385
386 ir_codes = init_data->ir_codes;
2eb25832
MCC
387 rc = init_data->rc_dev;
388
4d7a2d67 389 name = init_data->name;
1b6e59e3 390 if (init_data->type)
52b66144 391 rc_type = init_data->type;
1b6e59e3 392
c72ba8e6
MCC
393 if (init_data->polling_interval)
394 ir->polling_interval = init_data->polling_interval;
395
1b6e59e3
AW
396 switch (init_data->internal_get_key_func) {
397 case IR_KBD_GET_KEY_CUSTOM:
398 /* The bridge driver provided us its own function */
399 ir->get_key = init_data->get_key;
400 break;
401 case IR_KBD_GET_KEY_PIXELVIEW:
402 ir->get_key = get_key_pixelview;
403 break;
1b6e59e3
AW
404 case IR_KBD_GET_KEY_HAUP:
405 ir->get_key = get_key_haup;
406 break;
407 case IR_KBD_GET_KEY_KNC1:
408 ir->get_key = get_key_knc1;
409 break;
410 case IR_KBD_GET_KEY_FUSIONHDTV:
411 ir->get_key = get_key_fusionhdtv;
412 break;
413 case IR_KBD_GET_KEY_HAUP_XVR:
414 ir->get_key = get_key_haup_xvr;
415 break;
416 case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS:
417 ir->get_key = get_key_avermedia_cardbus;
418 break;
419 }
4d7a2d67
JD
420 }
421
2eb25832
MCC
422 if (!rc) {
423 /*
39c1cb2b 424 * If platform_data doesn't specify rc_dev, initialize it
2eb25832
MCC
425 * internally
426 */
0f7499fd 427 rc = rc_allocate_device(RC_DRIVER_SCANCODE);
c02b211d
LP
428 if (!rc)
429 return -ENOMEM;
2eb25832
MCC
430 }
431 ir->rc = rc;
432
9a4cc5ac 433 /* Make sure we are all setup before going on */
52b66144 434 if (!name || !ir->get_key || !rc_type || !ir_codes) {
d995a187 435 dprintk(1, ": Unsupported device at address 0x%02x\n",
9a4cc5ac
JD
436 addr);
437 err = -ENODEV;
438 goto err_out_free;
439 }
440
ac9cd976 441 /* Sets name */
1df8e986 442 snprintf(ir->name, sizeof(ir->name), "i2c IR (%s)", name);
b07b4783 443 ir->ir_codes = ir_codes;
d5e52653 444
ac9cd976 445 snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0",
c668f32d
JD
446 dev_name(&adap->dev),
447 dev_name(&client->dev));
ac9cd976 448
2eb25832
MCC
449 /*
450 * Initialize input_dev fields
451 * It doesn't make sense to allow overriding them via platform_data
452 */
d8b4b582 453 rc->input_id.bustype = BUS_I2C;
d8b4b582 454 rc->input_phys = ir->phys;
2eb25832
MCC
455 rc->input_name = ir->name;
456
457 /*
458 * Initialize the other fields of rc_dev
459 */
460 rc->map_name = ir->ir_codes;
c5540fbb
DH
461 rc->allowed_protocols = rc_type;
462 rc->enabled_protocols = rc_type;
2eb25832
MCC
463 if (!rc->driver_name)
464 rc->driver_name = MODULE_NAME;
b7df3910 465
d8b4b582 466 err = rc_register_device(rc);
b07b4783 467 if (err)
c668f32d 468 goto err_out_free;
b07b4783 469
727e625c 470 printk(MODULE_NAME ": %s detected at %s [%s]\n",
d8b4b582 471 ir->name, ir->phys, adap->name);
1da177e4
LT
472
473 /* start polling via eventd */
c1089bdc
JD
474 INIT_DELAYED_WORK(&ir->work, ir_work);
475 schedule_delayed_work(&ir->work, 0);
1da177e4
LT
476
477 return 0;
b07b4783 478
b07b4783 479 err_out_free:
2eb25832 480 /* Only frees rc if it were allocated internally */
d8b4b582 481 rc_free_device(rc);
b07b4783 482 return err;
1da177e4
LT
483}
484
c668f32d 485static int ir_remove(struct i2c_client *client)
1da177e4 486{
4ac97914 487 struct IR_i2c *ir = i2c_get_clientdata(client);
1da177e4
LT
488
489 /* kill outstanding polls */
c1089bdc 490 cancel_delayed_work_sync(&ir->work);
1da177e4 491
c668f32d 492 /* unregister device */
8f1aeedf 493 rc_unregister_device(ir->rc);
1da177e4
LT
494
495 /* free memory */
1da177e4
LT
496 return 0;
497}
498
c668f32d
JD
499static const struct i2c_device_id ir_kbd_id[] = {
500 /* Generic entry for any IR receiver */
501 { "ir_video", 0 },
f5d887ae
AW
502 /* IR device specific entries should be added here */
503 { "ir_rx_z8f0811_haup", 0 },
a9cd591e 504 { "ir_rx_z8f0811_hdpvr", 0 },
c668f32d
JD
505 { }
506};
cb3bf504 507
c6e8d86f 508static struct i2c_driver ir_kbd_driver = {
c668f32d
JD
509 .driver = {
510 .name = "ir-kbd-i2c",
511 },
512 .probe = ir_probe,
513 .remove = ir_remove,
514 .id_table = ir_kbd_id,
515};
1da177e4 516
c6e8d86f
AL
517module_i2c_driver(ir_kbd_driver);
518
1da177e4
LT
519/* ----------------------------------------------------------------------- */
520
521MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
522MODULE_DESCRIPTION("input driver for i2c IR remote controls");
523MODULE_LICENSE("GPL");