ir-kbd-i2c.c revision c1089bdc07f06b90f0bc50d0789c2a4833097df7
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 *
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 *  You should have received a copy of the GNU General Public License
33 *  along with this program; if not, write to the Free Software
34 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35 *
36 */
37
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/kernel.h>
41#include <linux/string.h>
42#include <linux/timer.h>
43#include <linux/delay.h>
44#include <linux/errno.h>
45#include <linux/slab.h>
46#include <linux/i2c.h>
47#include <linux/i2c-id.h>
48#include <linux/workqueue.h>
49
50#include <media/ir-common.h>
51#include <media/ir-kbd-i2c.h>
52
53/* ----------------------------------------------------------------------- */
54/* insmod parameters                                                       */
55
56static int debug;
57module_param(debug, int, 0644);    /* debug level (0,1,2) */
58
59static int hauppauge;
60module_param(hauppauge, int, 0644);    /* Choose Hauppauge remote */
61MODULE_PARM_DESC(hauppauge, "Specify Hauppauge remote: 0=black, 1=grey (defaults to 0)");
62
63
64#define DEVNAME "ir-kbd-i2c"
65#define dprintk(level, fmt, arg...)	if (debug >= level) \
66	printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
67
68/* ----------------------------------------------------------------------- */
69
70static int get_key_haup_common(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw,
71			       int size, int offset)
72{
73	unsigned char buf[6];
74	int start, range, toggle, dev, code, ircode;
75
76	/* poll IR chip */
77	if (size != i2c_master_recv(&ir->c,buf,size))
78		return -EIO;
79
80	/* split rc5 data block ... */
81	start  = (buf[offset] >> 7) &    1;
82	range  = (buf[offset] >> 6) &    1;
83	toggle = (buf[offset] >> 5) &    1;
84	dev    =  buf[offset]       & 0x1f;
85	code   = (buf[offset+1] >> 2) & 0x3f;
86
87	/* rc5 has two start bits
88	 * the first bit must be one
89	 * the second bit defines the command range (1 = 0-63, 0 = 64 - 127)
90	 */
91	if (!start)
92		/* no key pressed */
93		return 0;
94	/*
95	 * Hauppauge remotes (black/silver) always use
96	 * specific device ids. If we do not filter the
97	 * device ids then messages destined for devices
98	 * such as TVs (id=0) will get through causing
99	 * mis-fired events.
100	 *
101	 * We also filter out invalid key presses which
102	 * produce annoying debug log entries.
103	 */
104	ircode= (start << 12) | (toggle << 11) | (dev << 6) | code;
105	if ((ircode & 0x1fff)==0x1fff)
106		/* invalid key press */
107		return 0;
108
109	if (dev!=0x1e && dev!=0x1f)
110		/* not a hauppauge remote */
111		return 0;
112
113	if (!range)
114		code += 64;
115
116	dprintk(1,"ir hauppauge (rc5): s%d r%d t%d dev=%d code=%d\n",
117		start, range, toggle, dev, code);
118
119	/* return key */
120	*ir_key = code;
121	*ir_raw = ircode;
122	return 1;
123}
124
125static inline int get_key_haup(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
126{
127	return get_key_haup_common (ir, ir_key, ir_raw, 3, 0);
128}
129
130static inline int get_key_haup_xvr(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
131{
132	return get_key_haup_common (ir, ir_key, ir_raw, 6, 3);
133}
134
135static int get_key_pixelview(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
136{
137	unsigned char b;
138
139	/* poll IR chip */
140	if (1 != i2c_master_recv(&ir->c,&b,1)) {
141		dprintk(1,"read error\n");
142		return -EIO;
143	}
144	*ir_key = b;
145	*ir_raw = b;
146	return 1;
147}
148
149static int get_key_pv951(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
150{
151	unsigned char b;
152
153	/* poll IR chip */
154	if (1 != i2c_master_recv(&ir->c,&b,1)) {
155		dprintk(1,"read error\n");
156		return -EIO;
157	}
158
159	/* ignore 0xaa */
160	if (b==0xaa)
161		return 0;
162	dprintk(2,"key %02x\n", b);
163
164	*ir_key = b;
165	*ir_raw = b;
166	return 1;
167}
168
169static int get_key_fusionhdtv(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
170{
171	unsigned char buf[4];
172
173	/* poll IR chip */
174	if (4 != i2c_master_recv(&ir->c,buf,4)) {
175		dprintk(1,"read error\n");
176		return -EIO;
177	}
178
179	if(buf[0] !=0 || buf[1] !=0 || buf[2] !=0 || buf[3] != 0)
180		dprintk(2, "%s: 0x%2x 0x%2x 0x%2x 0x%2x\n", __func__,
181			buf[0], buf[1], buf[2], buf[3]);
182
183	/* no key pressed or signal from other ir remote */
184	if(buf[0] != 0x1 ||  buf[1] != 0xfe)
185		return 0;
186
187	*ir_key = buf[2];
188	*ir_raw = (buf[2] << 8) | buf[3];
189
190	return 1;
191}
192
193static int get_key_knc1(struct IR_i2c *ir, u32 *ir_key, u32 *ir_raw)
194{
195	unsigned char b;
196
197	/* poll IR chip */
198	if (1 != i2c_master_recv(&ir->c,&b,1)) {
199		dprintk(1,"read error\n");
200		return -EIO;
201	}
202
203	/* it seems that 0xFE indicates that a button is still hold
204	   down, while 0xff indicates that no button is hold
205	   down. 0xfe sequences are sometimes interrupted by 0xFF */
206
207	dprintk(2,"key %02x\n", b);
208
209	if (b == 0xff)
210		return 0;
211
212	if (b == 0xfe)
213		/* keep old data */
214		return 1;
215
216	*ir_key = b;
217	*ir_raw = b;
218	return 1;
219}
220
221static int get_key_avermedia_cardbus(struct IR_i2c *ir,
222				     u32 *ir_key, u32 *ir_raw)
223{
224	unsigned char subaddr, key, keygroup;
225	struct i2c_msg msg[] = { { .addr = ir->c.addr, .flags = 0,
226				   .buf = &subaddr, .len = 1},
227				 { .addr = ir->c.addr, .flags = I2C_M_RD,
228				  .buf = &key, .len = 1} };
229	subaddr = 0x0d;
230	if (2 != i2c_transfer(ir->c.adapter, msg, 2)) {
231		dprintk(1, "read error\n");
232		return -EIO;
233	}
234
235	if (key == 0xff)
236		return 0;
237
238	subaddr = 0x0b;
239	msg[1].buf = &keygroup;
240	if (2 != i2c_transfer(ir->c.adapter, msg, 2)) {
241		dprintk(1, "read error\n");
242		return -EIO;
243	}
244
245	if (keygroup == 0xff)
246		return 0;
247
248	dprintk(1, "read key 0x%02x/0x%02x\n", key, keygroup);
249	if (keygroup < 2 || keygroup > 3) {
250		/* Only a warning */
251		dprintk(1, "warning: invalid key group 0x%02x for key 0x%02x\n",
252								keygroup, key);
253	}
254	key |= (keygroup & 1) << 6;
255
256	*ir_key = key;
257	*ir_raw = key;
258	return 1;
259}
260
261/* ----------------------------------------------------------------------- */
262
263static void ir_key_poll(struct IR_i2c *ir)
264{
265	static u32 ir_key, ir_raw;
266	int rc;
267
268	dprintk(2,"ir_poll_key\n");
269	rc = ir->get_key(ir, &ir_key, &ir_raw);
270	if (rc < 0) {
271		dprintk(2,"error\n");
272		return;
273	}
274
275	if (0 == rc) {
276		ir_input_nokey(ir->input, &ir->ir);
277	} else {
278		ir_input_keydown(ir->input, &ir->ir, ir_key, ir_raw);
279	}
280}
281
282static void ir_work(struct work_struct *work)
283{
284	struct IR_i2c *ir = container_of(work, struct IR_i2c, work.work);
285	int polling_interval = 100;
286
287	/* MSI TV@nywhere Plus requires more frequent polling
288	   otherwise it will miss some keypresses */
289	if (ir->c.adapter->id == I2C_HW_SAA7134 && ir->c.addr == 0x30)
290		polling_interval = 50;
291
292	ir_key_poll(ir);
293	schedule_delayed_work(&ir->work, msecs_to_jiffies(polling_interval));
294}
295
296/* ----------------------------------------------------------------------- */
297
298static int ir_attach(struct i2c_adapter *adap, int addr,
299		      unsigned short flags, int kind);
300static int ir_detach(struct i2c_client *client);
301static int ir_probe(struct i2c_adapter *adap);
302
303static struct i2c_driver driver = {
304	.driver = {
305		.name   = "ir-kbd-i2c",
306	},
307	.id             = I2C_DRIVERID_INFRARED,
308	.attach_adapter = ir_probe,
309	.detach_client  = ir_detach,
310};
311
312static struct i2c_client client_template =
313{
314	.name = "unset",
315	.driver = &driver
316};
317
318static int ir_attach(struct i2c_adapter *adap, int addr,
319		     unsigned short flags, int kind)
320{
321	IR_KEYTAB_TYPE *ir_codes = NULL;
322	char *name;
323	int ir_type;
324	struct IR_i2c *ir;
325	struct input_dev *input_dev;
326	int err;
327
328	ir = kzalloc(sizeof(struct IR_i2c),GFP_KERNEL);
329	input_dev = input_allocate_device();
330	if (!ir || !input_dev) {
331		err = -ENOMEM;
332		goto err_out_free;
333	}
334
335	ir->c = client_template;
336	ir->input = input_dev;
337
338	ir->c.adapter = adap;
339	ir->c.addr    = addr;
340
341	i2c_set_clientdata(&ir->c, ir);
342
343	switch(addr) {
344	case 0x64:
345		name        = "Pixelview";
346		ir->get_key = get_key_pixelview;
347		ir_type     = IR_TYPE_OTHER;
348		ir_codes    = ir_codes_empty;
349		break;
350	case 0x4b:
351		name        = "PV951";
352		ir->get_key = get_key_pv951;
353		ir_type     = IR_TYPE_OTHER;
354		ir_codes    = ir_codes_pv951;
355		break;
356	case 0x18:
357	case 0x1a:
358		name        = "Hauppauge";
359		ir->get_key = get_key_haup;
360		ir_type     = IR_TYPE_RC5;
361		if (hauppauge == 1) {
362			ir_codes    = ir_codes_hauppauge_new;
363		} else {
364			ir_codes    = ir_codes_rc5_tv;
365		}
366		break;
367	case 0x30:
368		name        = "KNC One";
369		ir->get_key = get_key_knc1;
370		ir_type     = IR_TYPE_OTHER;
371		ir_codes    = ir_codes_empty;
372		break;
373	case 0x6b:
374		name        = "FusionHDTV";
375		ir->get_key = get_key_fusionhdtv;
376		ir_type     = IR_TYPE_RC5;
377		ir_codes    = ir_codes_fusionhdtv_mce;
378		break;
379	case 0x7a:
380	case 0x47:
381	case 0x71:
382	case 0x2d:
383		if (adap->id == I2C_HW_B_CX2388x) {
384			/* Handled by cx88-input */
385			name        = "CX2388x remote";
386			ir_type     = IR_TYPE_RC5;
387			ir->get_key = get_key_haup_xvr;
388			if (hauppauge == 1) {
389				ir_codes    = ir_codes_hauppauge_new;
390			} else {
391				ir_codes    = ir_codes_rc5_tv;
392			}
393		} else {
394			/* Handled by saa7134-input */
395			name        = "SAA713x remote";
396			ir_type     = IR_TYPE_OTHER;
397		}
398		break;
399	case 0x40:
400		name        = "AVerMedia Cardbus remote";
401		ir->get_key = get_key_avermedia_cardbus;
402		ir_type     = IR_TYPE_OTHER;
403		ir_codes    = ir_codes_avermedia_cardbus;
404		break;
405	default:
406		/* shouldn't happen */
407		printk(DEVNAME ": Huh? unknown i2c address (0x%02x)?\n", addr);
408		err = -ENODEV;
409		goto err_out_free;
410	}
411
412	/* Sets name */
413	snprintf(ir->c.name, sizeof(ir->c.name), "i2c IR (%s)", name);
414	ir->ir_codes = ir_codes;
415
416	/* register i2c device
417	 * At device register, IR codes may be changed to be
418	 * board dependent.
419	 */
420	err = i2c_attach_client(&ir->c);
421	if (err)
422		goto err_out_free;
423
424	/* If IR not supported or disabled, unregisters driver */
425	if (ir->get_key == NULL) {
426		err = -ENODEV;
427		goto err_out_detach;
428	}
429
430	/* Phys addr can only be set after attaching (for ir->c.dev) */
431	snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0",
432		 dev_name(&ir->c.adapter->dev),
433		 dev_name(&ir->c.dev));
434
435	/* init + register input device */
436	ir_input_init(input_dev, &ir->ir, ir_type, ir->ir_codes);
437	input_dev->id.bustype = BUS_I2C;
438	input_dev->name       = ir->c.name;
439	input_dev->phys       = ir->phys;
440
441	err = input_register_device(ir->input);
442	if (err)
443		goto err_out_detach;
444
445	printk(DEVNAME ": %s detected at %s [%s]\n",
446	       ir->input->name, ir->input->phys, adap->name);
447
448	/* start polling via eventd */
449	INIT_DELAYED_WORK(&ir->work, ir_work);
450	schedule_delayed_work(&ir->work, 0);
451
452	return 0;
453
454 err_out_detach:
455	i2c_detach_client(&ir->c);
456 err_out_free:
457	input_free_device(input_dev);
458	kfree(ir);
459	return err;
460}
461
462static int ir_detach(struct i2c_client *client)
463{
464	struct IR_i2c *ir = i2c_get_clientdata(client);
465
466	/* kill outstanding polls */
467	cancel_delayed_work_sync(&ir->work);
468
469	/* unregister devices */
470	input_unregister_device(ir->input);
471	i2c_detach_client(&ir->c);
472
473	/* free memory */
474	kfree(ir);
475	return 0;
476}
477
478static int ir_probe(struct i2c_adapter *adap)
479{
480
481	/* The external IR receiver is at i2c address 0x34 (0x35 for
482	   reads).  Future Hauppauge cards will have an internal
483	   receiver at 0x30 (0x31 for reads).  In theory, both can be
484	   fitted, and Hauppauge suggest an external overrides an
485	   internal.
486
487	   That's why we probe 0x1a (~0x34) first. CB
488	*/
489
490	static const int probe_bttv[] = { 0x1a, 0x18, 0x4b, 0x64, 0x30, -1};
491	static const int probe_saa7134[] = { 0x7a, 0x47, 0x71, 0x2d, -1 };
492	static const int probe_em28XX[] = { 0x30, 0x47, -1 };
493	static const int probe_cx88[] = { 0x18, 0x6b, 0x71, -1 };
494	static const int probe_cx23885[] = { 0x6b, -1 };
495	const int *probe;
496	struct i2c_msg msg = {
497		.flags = I2C_M_RD,
498		.len = 0,
499		.buf = NULL,
500	};
501	int i, rc;
502
503	switch (adap->id) {
504	case I2C_HW_B_BT848:
505		probe = probe_bttv;
506		break;
507	case I2C_HW_B_CX2341X:
508		probe = probe_bttv;
509		break;
510	case I2C_HW_SAA7134:
511		probe = probe_saa7134;
512		break;
513	case I2C_HW_B_EM28XX:
514		probe = probe_em28XX;
515		break;
516	case I2C_HW_B_CX2388x:
517		probe = probe_cx88;
518		break;
519	case I2C_HW_B_CX23885:
520		probe = probe_cx23885;
521		break;
522	default:
523		return 0;
524	}
525
526	for (i = 0; -1 != probe[i]; i++) {
527		msg.addr = probe[i];
528		rc = i2c_transfer(adap, &msg, 1);
529		dprintk(1,"probe 0x%02x @ %s: %s\n",
530			probe[i], adap->name,
531			(1 == rc) ? "yes" : "no");
532		if (1 == rc) {
533			ir_attach(adap, probe[i], 0, 0);
534			return 0;
535		}
536	}
537
538	/* Special case for MSI TV@nywhere Plus remote */
539	if (adap->id == I2C_HW_SAA7134) {
540		u8 temp;
541
542		/* MSI TV@nywhere Plus controller doesn't seem to
543		   respond to probes unless we read something from
544		   an existing device. Weird... */
545
546		msg.addr = 0x50;
547		rc = i2c_transfer(adap, &msg, 1);
548			dprintk(1, "probe 0x%02x @ %s: %s\n",
549			msg.addr, adap->name,
550			(1 == rc) ? "yes" : "no");
551
552		/* Now do the probe. The controller does not respond
553		   to 0-byte reads, so we use a 1-byte read instead. */
554		msg.addr = 0x30;
555		msg.len = 1;
556		msg.buf = &temp;
557		rc = i2c_transfer(adap, &msg, 1);
558		dprintk(1, "probe 0x%02x @ %s: %s\n",
559			msg.addr, adap->name,
560			(1 == rc) ? "yes" : "no");
561		if (1 == rc)
562			ir_attach(adap, msg.addr, 0, 0);
563	}
564
565	/* Special case for AVerMedia Cardbus remote */
566	if (adap->id == I2C_HW_SAA7134) {
567		unsigned char subaddr, data;
568		struct i2c_msg msg[] = { { .addr = 0x40, .flags = 0,
569					   .buf = &subaddr, .len = 1},
570					 { .addr = 0x40, .flags = I2C_M_RD,
571					   .buf = &data, .len = 1} };
572		subaddr = 0x0d;
573		rc = i2c_transfer(adap, msg, 2);
574		dprintk(1, "probe 0x%02x/0x%02x @ %s: %s\n",
575			msg[0].addr, subaddr, adap->name,
576			(2 == rc) ? "yes" : "no");
577		if (2 == rc)
578			ir_attach(adap, msg[0].addr, 0, 0);
579	}
580
581	return 0;
582}
583
584/* ----------------------------------------------------------------------- */
585
586MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
587MODULE_DESCRIPTION("input driver for i2c IR remote controls");
588MODULE_LICENSE("GPL");
589
590static int __init ir_init(void)
591{
592	return i2c_add_driver(&driver);
593}
594
595static void __exit ir_fini(void)
596{
597	i2c_del_driver(&driver);
598}
599
600module_init(ir_init);
601module_exit(ir_fini);
602
603/*
604 * Overrides for Emacs so that we follow Linus's tabbing style.
605 * ---------------------------------------------------------------------------
606 * Local variables:
607 * c-basic-offset: 8
608 * End:
609 */
610