blob: a374e2a0ac3d84b7ba2b50f1583c3fa481c2b357 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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 *
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 *
32 */
33
34#include <asm/unaligned.h>
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/kernel.h>
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>
45
46#include <media/rc-core.h>
47#include <media/i2c/ir-kbd-i2c.h>
48
49/* ----------------------------------------------------------------------- */
50/* insmod parameters */
51
52static int debug;
53module_param(debug, int, 0644); /* debug level (0,1,2) */
54
55
56#define MODULE_NAME "ir-kbd-i2c"
57#define dprintk(level, fmt, arg...) if (debug >= level) \
58 printk(KERN_DEBUG MODULE_NAME ": " fmt , ## arg)
59
60/* ----------------------------------------------------------------------- */
61
62static int get_key_haup_common(struct IR_i2c *ir, enum rc_proto *protocol,
63 u32 *scancode, u8 *ptoggle, int size)
64{
65 unsigned char buf[6];
66 int start, range, toggle, dev, code, ircode, vendor;
67
68 /* poll IR chip */
69 if (size != i2c_master_recv(ir->c, buf, size))
70 return -EIO;
71
72 if (buf[0] & 0x80) {
73 int offset = (size == 6) ? 3 : 0;
74
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;
81
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;
90
91 /* filter out invalid key presses */
92 ircode = (start << 12) | (toggle << 11) | (dev << 6) | code;
93 if ((ircode & 0x1fff) == 0x1fff)
94 return 0;
95
96 if (!range)
97 code += 64;
98
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_PROTO_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_PROTO_RC6_MCE;
115 dev &= 0x7f;
116 dprintk(1, "ir hauppauge (rc6-mce): t%d vendor=%d dev=%d code=%d\n",
117 *ptoggle, vendor, dev, code);
118 } else {
119 *ptoggle = 0;
120 *protocol = RC_PROTO_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;
131}
132
133static 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
139static 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
158static int get_key_pixelview(struct IR_i2c *ir, enum rc_proto *protocol,
159 u32 *scancode, u8 *toggle)
160{
161 unsigned char b;
162
163 /* poll IR chip */
164 if (1 != i2c_master_recv(ir->c, &b, 1)) {
165 dprintk(1,"read error\n");
166 return -EIO;
167 }
168
169 *protocol = RC_PROTO_OTHER;
170 *scancode = b;
171 *toggle = 0;
172 return 1;
173}
174
175static int get_key_fusionhdtv(struct IR_i2c *ir, enum rc_proto *protocol,
176 u32 *scancode, u8 *toggle)
177{
178 unsigned char buf[4];
179
180 /* poll IR chip */
181 if (4 != i2c_master_recv(ir->c, buf, 4)) {
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)
187 dprintk(2, "%s: 0x%2x 0x%2x 0x%2x 0x%2x\n", __func__,
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
194 *protocol = RC_PROTO_UNKNOWN;
195 *scancode = buf[2];
196 *toggle = 0;
197 return 1;
198}
199
200static int get_key_knc1(struct IR_i2c *ir, enum rc_proto *protocol,
201 u32 *scancode, u8 *toggle)
202{
203 unsigned char b;
204
205 /* poll IR chip */
206 if (1 != i2c_master_recv(ir->c, &b, 1)) {
207 dprintk(1,"read error\n");
208 return -EIO;
209 }
210
211 /* it seems that 0xFE indicates that a button is still hold
212 down, while 0xff indicates that no button is hold
213 down. 0xfe sequences are sometimes interrupted by 0xFF */
214
215 dprintk(2,"key %02x\n", b);
216
217 if (b == 0xff)
218 return 0;
219
220 if (b == 0xfe)
221 /* keep old data */
222 return 1;
223
224 *protocol = RC_PROTO_UNKNOWN;
225 *scancode = b;
226 *toggle = 0;
227 return 1;
228}
229
230static int get_key_avermedia_cardbus(struct IR_i2c *ir, enum rc_proto *protocol,
231 u32 *scancode, u8 *toggle)
232{
233 unsigned char subaddr, key, keygroup;
234 struct i2c_msg msg[] = { { .addr = ir->c->addr, .flags = 0,
235 .buf = &subaddr, .len = 1},
236 { .addr = ir->c->addr, .flags = I2C_M_RD,
237 .buf = &key, .len = 1} };
238 subaddr = 0x0d;
239 if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
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;
249 if (2 != i2c_transfer(ir->c->adapter, msg, 2)) {
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);
258 if (keygroup < 2 || keygroup > 4) {
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
265 *protocol = RC_PROTO_UNKNOWN;
266 *scancode = key;
267 if (ir->c->addr == 0x41) /* AVerMedia EM78P153 */
268 *scancode |= keygroup << 8;
269 *toggle = 0;
270 return 1;
271}
272
273/* ----------------------------------------------------------------------- */
274
275static int ir_key_poll(struct IR_i2c *ir)
276{
277 enum rc_proto protocol;
278 u32 scancode;
279 u8 toggle;
280 int rc;
281
282 dprintk(3, "%s\n", __func__);
283 rc = ir->get_key(ir, &protocol, &scancode, &toggle);
284 if (rc < 0) {
285 dprintk(2,"error\n");
286 return rc;
287 }
288
289 if (rc) {
290 dprintk(1, "%s: proto = 0x%04x, scancode = 0x%08x\n",
291 __func__, protocol, scancode);
292 rc_keydown(ir->rc, protocol, scancode, toggle);
293 }
294 return 0;
295}
296
297static void ir_work(struct work_struct *work)
298{
299 int rc;
300 struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work);
301
302 rc = ir_key_poll(ir);
303 if (rc == -ENODEV) {
304 rc_unregister_device(ir->rc);
305 ir->rc = NULL;
306 return;
307 }
308
309 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling_interval));
310}
311
312/* ----------------------------------------------------------------------- */
313
314static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
315{
316 char *ir_codes = NULL;
317 const char *name = NULL;
318 u64 rc_proto = RC_PROTO_BIT_UNKNOWN;
319 struct IR_i2c *ir;
320 struct rc_dev *rc = NULL;
321 struct i2c_adapter *adap = client->adapter;
322 unsigned short addr = client->addr;
323 int err;
324
325 ir = devm_kzalloc(&client->dev, sizeof(*ir), GFP_KERNEL);
326 if (!ir)
327 return -ENOMEM;
328
329 ir->c = client;
330 ir->polling_interval = DEFAULT_POLLING_INTERVAL;
331 i2c_set_clientdata(client, ir);
332
333 switch(addr) {
334 case 0x64:
335 name = "Pixelview";
336 ir->get_key = get_key_pixelview;
337 rc_proto = RC_PROTO_BIT_OTHER;
338 ir_codes = RC_MAP_EMPTY;
339 break;
340 case 0x18:
341 case 0x1f:
342 case 0x1a:
343 name = "Hauppauge";
344 ir->get_key = get_key_haup;
345 rc_proto = RC_PROTO_BIT_RC5;
346 ir_codes = RC_MAP_HAUPPAUGE;
347 break;
348 case 0x30:
349 name = "KNC One";
350 ir->get_key = get_key_knc1;
351 rc_proto = RC_PROTO_BIT_OTHER;
352 ir_codes = RC_MAP_EMPTY;
353 break;
354 case 0x6b:
355 name = "FusionHDTV";
356 ir->get_key = get_key_fusionhdtv;
357 rc_proto = RC_PROTO_BIT_UNKNOWN;
358 ir_codes = RC_MAP_FUSIONHDTV_MCE;
359 break;
360 case 0x40:
361 name = "AVerMedia Cardbus remote";
362 ir->get_key = get_key_avermedia_cardbus;
363 rc_proto = RC_PROTO_BIT_OTHER;
364 ir_codes = RC_MAP_AVERMEDIA_CARDBUS;
365 break;
366 case 0x41:
367 name = "AVerMedia EM78P153";
368 ir->get_key = get_key_avermedia_cardbus;
369 rc_proto = RC_PROTO_BIT_OTHER;
370 /* RM-KV remote, seems to be same as RM-K6 */
371 ir_codes = RC_MAP_AVERMEDIA_M733A_RM_K6;
372 break;
373 case 0x71:
374 name = "Hauppauge/Zilog Z8";
375 ir->get_key = get_key_haup_xvr;
376 rc_proto = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_MCE |
377 RC_PROTO_BIT_RC6_6A_32;
378 ir_codes = RC_MAP_HAUPPAUGE;
379 break;
380 }
381
382 /* Let the caller override settings */
383 if (client->dev.platform_data) {
384 const struct IR_i2c_init_data *init_data =
385 client->dev.platform_data;
386
387 ir_codes = init_data->ir_codes;
388 rc = init_data->rc_dev;
389
390 name = init_data->name;
391 if (init_data->type)
392 rc_proto = init_data->type;
393
394 if (init_data->polling_interval)
395 ir->polling_interval = init_data->polling_interval;
396
397 switch (init_data->internal_get_key_func) {
398 case IR_KBD_GET_KEY_CUSTOM:
399 /* The bridge driver provided us its own function */
400 ir->get_key = init_data->get_key;
401 break;
402 case IR_KBD_GET_KEY_PIXELVIEW:
403 ir->get_key = get_key_pixelview;
404 break;
405 case IR_KBD_GET_KEY_HAUP:
406 ir->get_key = get_key_haup;
407 break;
408 case IR_KBD_GET_KEY_KNC1:
409 ir->get_key = get_key_knc1;
410 break;
411 case IR_KBD_GET_KEY_FUSIONHDTV:
412 ir->get_key = get_key_fusionhdtv;
413 break;
414 case IR_KBD_GET_KEY_HAUP_XVR:
415 ir->get_key = get_key_haup_xvr;
416 break;
417 case IR_KBD_GET_KEY_AVERMEDIA_CARDBUS:
418 ir->get_key = get_key_avermedia_cardbus;
419 break;
420 }
421 }
422
423 if (!rc) {
424 /*
425 * If platform_data doesn't specify rc_dev, initialize it
426 * internally
427 */
428 rc = rc_allocate_device(RC_DRIVER_SCANCODE);
429 if (!rc)
430 return -ENOMEM;
431 }
432 ir->rc = rc;
433
434 /* Make sure we are all setup before going on */
435 if (!name || !ir->get_key || !rc_proto || !ir_codes) {
436 dprintk(1, ": Unsupported device at address 0x%02x\n",
437 addr);
438 err = -ENODEV;
439 goto err_out_free;
440 }
441
442 /* Sets name */
443 snprintf(ir->name, sizeof(ir->name), "i2c IR (%s)", name);
444 ir->ir_codes = ir_codes;
445
446 snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0",
447 dev_name(&adap->dev),
448 dev_name(&client->dev));
449
450 /*
451 * Initialize input_dev fields
452 * It doesn't make sense to allow overriding them via platform_data
453 */
454 rc->input_id.bustype = BUS_I2C;
455 rc->input_phys = ir->phys;
456 rc->device_name = ir->name;
457
458 /*
459 * Initialize the other fields of rc_dev
460 */
461 rc->map_name = ir->ir_codes;
462 rc->allowed_protocols = rc_proto;
463 rc->enabled_protocols = rc_proto;
464 if (!rc->driver_name)
465 rc->driver_name = MODULE_NAME;
466
467 err = rc_register_device(rc);
468 if (err)
469 goto err_out_free;
470
471 printk(MODULE_NAME ": %s detected at %s [%s]\n",
472 ir->name, ir->phys, adap->name);
473
474 /* start polling via eventd */
475 INIT_DELAYED_WORK(&ir->work, ir_work);
476 schedule_delayed_work(&ir->work, 0);
477
478 return 0;
479
480 err_out_free:
481 /* Only frees rc if it were allocated internally */
482 rc_free_device(rc);
483 return err;
484}
485
486static int ir_remove(struct i2c_client *client)
487{
488 struct IR_i2c *ir = i2c_get_clientdata(client);
489
490 /* kill outstanding polls */
491 cancel_delayed_work_sync(&ir->work);
492
493 /* unregister device */
494 rc_unregister_device(ir->rc);
495
496 /* free memory */
497 return 0;
498}
499
500static const struct i2c_device_id ir_kbd_id[] = {
501 /* Generic entry for any IR receiver */
502 { "ir_video", 0 },
503 /* IR device specific entries should be added here */
504 { "ir_rx_z8f0811_haup", 0 },
505 { "ir_rx_z8f0811_hdpvr", 0 },
506 { }
507};
508
509static struct i2c_driver ir_kbd_driver = {
510 .driver = {
511 .name = "ir-kbd-i2c",
512 },
513 .probe = ir_probe,
514 .remove = ir_remove,
515 .id_table = ir_kbd_id,
516};
517
518module_i2c_driver(ir_kbd_driver);
519
520/* ----------------------------------------------------------------------- */
521
522MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
523MODULE_DESCRIPTION("input driver for i2c IR remote controls");
524MODULE_LICENSE("GPL");