Added lirc.
[irreco.git] / lirc-0.8.4a / drivers / lirc_i2c / lirc_i2c.c
blob4fcb66dcfbf355c81beacf1a162992663ac162bc
1 /* $Id: lirc_i2c.c,v 1.47 2008/08/12 20:50:39 lirc Exp $ */
3 /*
4 * i2c IR lirc plugin for Hauppauge and Pixelview cards - new 2.3.x i2c stack
6 * Copyright (c) 2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
7 * modified for PixelView (BT878P+W/FM) by
8 * Michal Kochanowicz <mkochano@pld.org.pl>
9 * Christoph Bartelmus <lirc@bartelmus.de>
10 * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by
11 * Ulrich Mueller <ulrich.mueller42@web.de>
12 * modified for Asus TV-Box and Creative/VisionTek BreakOut-Box by
13 * Stefan Jahn <stefan@lkcc.org>
14 * modified for inclusion into kernel sources by
15 * Jerome Brock <jbrock@users.sourceforge.net>
16 * modified for Leadtek Winfast PVR2000 by
17 * Thomas Reitmayr (treitmayr@yahoo.com)
18 * modified for Hauppauge HVR-1300 by
19 * Jan Frey (jfrey@gmx.de)
21 * parts are cut&pasted from the old lirc_haup.c driver
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
44 #include <linux/version.h>
45 #include <linux/module.h>
46 #include <linux/kmod.h>
47 #include <linux/kernel.h>
48 #include <linux/sched.h>
49 #include <linux/string.h>
50 #include <linux/timer.h>
51 #include <linux/delay.h>
52 #include <linux/errno.h>
53 #include <linux/slab.h>
54 #include <linux/i2c.h>
55 #include <linux/i2c-algo-bit.h>
57 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
58 #include <asm/semaphore.h>
59 #else
60 #include <linux/semaphore.h>
61 #endif
63 #include "drivers/kcompat.h"
64 #include "drivers/lirc_dev/lirc_dev.h"
66 struct IR {
67 struct lirc_plugin l;
68 struct i2c_client c;
69 int nextkey;
70 unsigned char b[3];
71 unsigned char bits;
72 unsigned char flag;
75 /* ----------------------------------------------------------------------- */
77 #define DEVICE_NAME "lirc_i2c"
79 /* ----------------------------------------------------------------------- */
80 /* insmod parameters */
82 static int debug; /* debug output */
83 static int minor = -1; /* minor number */
85 #define dprintk(fmt, args...) \
86 do { \
87 if (debug) \
88 printk(KERN_DEBUG DEVICE_NAME ": " fmt, \
89 ## args); \
90 } while (0)
92 /* ----------------------------------------------------------------------- */
94 static inline int reverse(int data, int bits)
96 int i;
97 int c;
99 for (c = 0, i = 0; i < bits; i++)
100 c |= (((data & (1<<i)) ? 1:0)) << (bits-1-i);
102 return c;
105 static int add_to_buf_adap(void *data, struct lirc_buffer *buf)
107 struct IR *ir = data;
108 unsigned char keybuf[4];
110 keybuf[0] = 0x00;
111 i2c_master_send(&ir->c, keybuf, 1);
112 /* poll IR chip */
113 if (i2c_master_recv(&ir->c, keybuf, sizeof(keybuf)) != sizeof(keybuf)) {
114 dprintk("read error\n");
115 return -EIO;
118 dprintk("key (0x%02x%02x%02x%02x)\n",
119 keybuf[0], keybuf[1], keybuf[2], keybuf[3]);
121 /* key pressed ? */
122 if (keybuf[2] == 0xff)
123 return -ENODATA;
125 /* remove repeat bit */
126 keybuf[2] &= 0x7f;
127 keybuf[3] |= 0x80;
129 lirc_buffer_write_1(buf, keybuf);
130 return 0;
133 static int add_to_buf_pcf8574(void *data, struct lirc_buffer *buf)
135 struct IR *ir = data;
136 int rc;
137 unsigned char all, mask;
138 unsigned char key;
140 /* compute all valid bits (key code + pressed/release flag) */
141 all = ir->bits | ir->flag;
143 /* save IR writable mask bits */
144 mask = i2c_smbus_read_byte(&ir->c) & ~all;
146 /* send bit mask */
147 rc = i2c_smbus_write_byte(&ir->c, (0xff & all) | mask);
149 /* receive scan code */
150 rc = i2c_smbus_read_byte(&ir->c);
152 if (rc == -1) {
153 dprintk("%s read error\n", ir->c.name);
154 return -EIO;
157 /* drop duplicate polls */
158 if (ir->b[0] == (rc & all))
159 return -ENODATA;
161 ir->b[0] = rc & all;
163 dprintk("%s key 0x%02X %s\n", ir->c.name, rc & ir->bits,
164 (rc & ir->flag) ? "released" : "pressed");
166 if (rc & ir->flag) {
167 /* ignore released buttons */
168 return -ENODATA;
171 /* set valid key code */
172 key = rc & ir->bits;
173 lirc_buffer_write_1(buf, &key);
174 return 0;
177 /* common for Hauppauge IR receivers */
178 static int add_to_buf_haup_common(void *data, struct lirc_buffer *buf,
179 unsigned char *keybuf, int size, int offset)
181 struct IR *ir = data;
182 __u16 code;
183 unsigned char codes[2];
185 /* poll IR chip */
186 if (size == i2c_master_recv(&ir->c, keybuf, size)) {
187 ir->b[0] = keybuf[offset];
188 ir->b[1] = keybuf[offset+1];
189 ir->b[2] = keybuf[offset+2];
190 dprintk("key (0x%02x/0x%02x)\n", ir->b[0], ir->b[1]);
191 } else {
192 dprintk("read error\n");
193 /* keep last successfull read buffer */
196 /* key pressed ? */
197 if ((ir->b[0] & 0x80) == 0)
198 return -ENODATA;
200 /* look what we have */
201 code = (((__u16)ir->b[0]&0x7f)<<6) | (ir->b[1]>>2);
203 codes[0] = (code >> 8) & 0xff;
204 codes[1] = code & 0xff;
206 /* return it */
207 lirc_buffer_write_1(buf, codes);
208 return 0;
211 /* specific for the Hauppauge PVR150 IR receiver */
212 static int add_to_buf_haup_pvr150(void *data, struct lirc_buffer *buf)
214 unsigned char keybuf[6];
215 /* fetch 6 bytes, first relevant is at offset 3 */
216 return add_to_buf_haup_common(data, buf, keybuf, 6, 3);
219 /* used for all Hauppauge IR receivers but the PVR150 */
220 static int add_to_buf_haup(void *data, struct lirc_buffer *buf)
222 unsigned char keybuf[3];
223 /* fetch 3 bytes, first relevant is at offset 0 */
224 return add_to_buf_haup_common(data, buf, keybuf, 3, 0);
228 static int add_to_buf_pvr2000(void *data, struct lirc_buffer *buf)
230 struct IR *ir = data;
231 unsigned char key;
232 s32 flags;
233 s32 code;
235 /* poll IR chip */
236 flags = i2c_smbus_read_byte_data(&ir->c, 0x10);
237 if (-1 == flags) {
238 dprintk("read error\n");
239 return -ENODATA;
241 /* key pressed ? */
242 if (0 == (flags & 0x80))
243 return -ENODATA;
245 /* read actual key code */
246 code = i2c_smbus_read_byte_data(&ir->c, 0x00);
247 if (-1 == code) {
248 dprintk("read error\n");
249 return -ENODATA;
252 key = code & 0xFF;
254 dprintk("IR Key/Flags: (0x%02x/0x%02x)\n", key, flags & 0xFF);
256 /* return it */
257 lirc_buffer_write_1(buf, &key);
258 return 0;
261 static int add_to_buf_pixelview(void *data, struct lirc_buffer *buf)
263 struct IR *ir = data;
264 unsigned char key;
266 /* poll IR chip */
267 if (1 != i2c_master_recv(&ir->c, &key, 1)) {
268 dprintk("read error\n");
269 return -1;
271 dprintk("key %02x\n", key);
273 /* return it */
274 lirc_buffer_write_1(buf, &key);
275 return 0;
278 static int add_to_buf_pv951(void *data, struct lirc_buffer *buf)
280 struct IR *ir = data;
281 unsigned char key;
282 unsigned char codes[4];
284 /* poll IR chip */
285 if (1 != i2c_master_recv(&ir->c, &key, 1)) {
286 dprintk("read error\n");
287 return -ENODATA;
289 /* ignore 0xaa */
290 if (key == 0xaa)
291 return -ENODATA;
292 dprintk("key %02x\n", key);
294 codes[0] = 0x61;
295 codes[1] = 0xD6;
296 codes[2] = reverse(key, 8);
297 codes[3] = (~codes[2])&0xff;
299 lirc_buffer_write_1(buf, codes);
300 return 0;
303 static int add_to_buf_knc1(void *data, struct lirc_buffer *buf)
305 static unsigned char last_key = 0xFF;
306 struct IR *ir = data;
307 unsigned char key;
309 /* poll IR chip */
310 if (1 != i2c_master_recv(&ir->c, &key, 1)) {
311 dprintk("read error\n");
312 return -ENODATA;
315 /* it seems that 0xFE indicates that a button is still hold
316 down, while 0xFF indicates that no button is hold
317 down. 0xFE sequences are sometimes interrupted by 0xFF */
319 dprintk("key %02x\n", key);
321 if (key == 0xFF)
322 return -ENODATA;
324 if (key == 0xFE)
325 key = last_key;
327 last_key = key;
328 lirc_buffer_write_1(buf, &key);
330 return 0;
333 static int set_use_inc(void *data)
335 struct IR *ir = data;
337 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
338 i2c_use_client(&ir->c);
339 #else
340 int ret;
342 /* lock bttv in memory while /dev/lirc is in use */
343 ret = i2c_use_client(&ir->c);
344 if (ret != 0)
345 return ret;
346 #endif
348 MOD_INC_USE_COUNT;
349 return 0;
352 static void set_use_dec(void *data)
354 struct IR *ir = data;
356 i2c_release_client(&ir->c);
357 MOD_DEC_USE_COUNT;
360 static struct lirc_plugin lirc_template = {
361 .name = "lirc_i2c",
362 .set_use_inc = set_use_inc,
363 .set_use_dec = set_use_dec,
364 .dev = NULL,
365 .owner = THIS_MODULE,
368 /* ----------------------------------------------------------------------- */
370 static int ir_attach(struct i2c_adapter *adap, int addr,
371 unsigned short flags, int kind);
372 static int ir_detach(struct i2c_client *client);
373 static int ir_probe(struct i2c_adapter *adap);
374 static int ir_command(struct i2c_client *client, unsigned int cmd, void *arg);
376 static struct i2c_driver driver = {
377 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
378 .name = "i2c ir driver",
379 .flags = I2C_DF_NOTIFY,
380 #else
381 .driver = {
382 .owner = THIS_MODULE,
383 .name = "i2c ir driver",
385 #endif
386 .id = I2C_DRIVERID_EXP3, /* FIXME */
387 .attach_adapter = ir_probe,
388 .detach_client = ir_detach,
389 .command = ir_command,
392 static struct i2c_client client_template = {
393 .name = "unset",
394 .driver = &driver
397 static int ir_attach(struct i2c_adapter *adap, int addr,
398 unsigned short flags, int kind)
400 struct IR *ir;
401 int err;
403 client_template.adapter = adap;
404 client_template.addr = addr;
406 ir = kmalloc(sizeof(struct IR), GFP_KERNEL);
407 if (!ir)
408 return -ENOMEM;
409 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_plugin));
410 memcpy(&ir->c, &client_template, sizeof(struct i2c_client));
412 ir->c.adapter = adap;
413 ir->c.addr = addr;
414 i2c_set_clientdata(&ir->c, ir);
415 ir->l.data = ir;
416 ir->l.minor = minor;
417 ir->l.sample_rate = 10;
418 ir->nextkey = -1;
420 switch (addr) {
421 case 0x64:
422 strlcpy(ir->c.name, "Pixelview IR", I2C_NAME_SIZE);
423 ir->l.code_length = 8;
424 ir->l.add_to_buf = add_to_buf_pixelview;
425 break;
426 case 0x4b:
427 strlcpy(ir->c.name, "PV951 IR", I2C_NAME_SIZE);
428 ir->l.code_length = 32;
429 ir->l.add_to_buf = add_to_buf_pv951;
430 break;
431 case 0x71:
432 #ifdef I2C_HW_B_CX2341X
433 if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848) ||
434 adap->id == (I2C_ALGO_BIT | I2C_HW_B_CX2341X)) {
435 #else
436 if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848)) {
437 #endif
438 /* The PVR150 IR receiver uses the same protocol as
439 * other Hauppauge cards, but the data flow is
440 * different, so we need to deal with it by its own. */
441 strlcpy(ir->c.name, "Hauppauge PVR150", I2C_NAME_SIZE);
442 } else /* I2C_HW_B_CX2388x */
443 strlcpy(ir->c.name, "Hauppauge HVR1300", I2C_NAME_SIZE);
444 ir->l.code_length = 13;
445 ir->l.add_to_buf = add_to_buf_haup_pvr150;
446 break;
447 case 0x6b:
448 strlcpy(ir->c.name, "Adaptec IR", I2C_NAME_SIZE);
449 ir->l.code_length = 32;
450 ir->l.add_to_buf = add_to_buf_adap;
451 break;
452 case 0x18:
453 case 0x1a:
454 #ifdef I2C_HW_B_CX2341X
455 if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848) ||
456 adap->id == (I2C_ALGO_BIT | I2C_HW_B_CX2341X)) {
457 #else
458 if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848)) {
459 #endif
460 strlcpy(ir->c.name, "Hauppauge IR", I2C_NAME_SIZE);
461 ir->l.code_length = 13;
462 ir->l.add_to_buf = add_to_buf_haup;
463 } else { /* I2C_HW_B_CX2388x */
464 strlcpy(ir->c.name, "Leadtek IR", I2C_NAME_SIZE);
465 ir->l.code_length = 8;
466 ir->l.add_to_buf = add_to_buf_pvr2000;
468 break;
469 case 0x30:
470 strlcpy(ir->c.name, "KNC ONE IR", I2C_NAME_SIZE);
471 ir->l.code_length = 8;
472 ir->l.add_to_buf = add_to_buf_knc1;
473 break;
474 case 0x21:
475 case 0x23:
476 strlcpy(ir->c.name, "TV-Box IR", I2C_NAME_SIZE);
477 ir->l.code_length = 8;
478 ir->l.add_to_buf = add_to_buf_pcf8574;
479 ir->bits = flags & 0xff;
480 ir->flag = (flags >> 8) & 0xff;
481 break;
482 default:
483 /* shouldn't happen */
484 printk("lirc_i2c: Huh? unknown i2c address (0x%02x)?\n", addr);
485 kfree(ir);
486 return -1;
488 printk(KERN_INFO "lirc_i2c: chip 0x%x found @ 0x%02x (%s)\n",
489 adap->id, addr, ir->c.name);
491 /* register device */
492 err = i2c_attach_client(&ir->c);
493 if (err) {
494 kfree(ir);
495 return err;
497 ir->l.minor = lirc_register_plugin(&ir->l);
499 return 0;
502 static int ir_detach(struct i2c_client *client)
504 struct IR *ir = i2c_get_clientdata(client);
506 /* unregister device */
507 lirc_unregister_plugin(ir->l.minor);
508 i2c_detach_client(&ir->c);
510 /* free memory */
511 kfree(ir);
512 return 0;
515 static int ir_probe(struct i2c_adapter *adap)
517 /* The external IR receiver is at i2c address 0x34 (0x35 for
518 * reads). Future Hauppauge cards will have an internal
519 * receiver at 0x30 (0x31 for reads). In theory, both can be
520 * fitted, and Hauppauge suggest an external overrides an
521 * internal.
523 * That's why we probe 0x1a (~0x34) first. CB
525 * The i2c address for the Hauppauge PVR-150 card is 0xe2,
526 * so we need to probe 0x71 as well. */
528 static const int probe[] = {
529 0x1a, /* Hauppauge IR external */
530 0x18, /* Hauppauge IR internal */
531 0x71, /* Hauppauge IR (PVR150) */
532 0x4b, /* PV951 IR */
533 0x64, /* Pixelview IR */
534 0x30, /* KNC ONE IR */
535 0x6b, /* Adaptec IR */
536 -1};
538 #ifdef I2C_HW_B_CX2388x
539 static const int probe_cx88[] = {
540 0x18, /* Leadtek Winfast PVR2000 */
541 0x71, /* Hauppauge HVR-IR */
542 -1};
543 #endif
545 struct i2c_client c;
546 char buf;
547 int i, rc;
549 #ifdef I2C_HW_B_CX2341X
550 if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848) ||
551 adap->id == (I2C_ALGO_BIT | I2C_HW_B_CX2341X))
552 #else
553 if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848))
554 #endif
556 memset(&c, 0, sizeof(c));
557 c.adapter = adap;
558 for (i = 0; -1 != probe[i]; i++) {
559 c.addr = probe[i];
560 rc = i2c_master_recv(&c, &buf, 1);
561 dprintk("probe 0x%02x @ %s: %s\n",
562 probe[i], adap->name,
563 (1 == rc) ? "yes" : "no");
564 if (1 == rc)
565 ir_attach(adap, probe[i], 0, 0);
569 #ifdef I2C_HW_B_CX2388x
570 /* Leadtek Winfast PVR2000 or Hauppauge HVR-1300 */
571 else if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_CX2388x)) {
572 memset(&c, 0, sizeof(c));
573 c.adapter = adap;
574 for (i = 0; -1 != probe_cx88[i]; i++) {
575 c.addr = probe_cx88[i];
576 rc = i2c_master_recv(&c, &buf, 1);
577 dprintk("probe 0x%02x @ %s: %s\n",
578 c.addr, adap->name,
579 (1 == rc) ? "yes" : "no");
580 if (1 == rc)
581 ir_attach(adap, c.addr, 0, 0);
584 #endif
586 /* Asus TV-Box and Creative/VisionTek BreakOut-Box (PCF8574) */
587 else if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_RIVA)) {
588 /* addresses to probe;
589 leave 0x24 and 0x25 because SAA7113H possibly uses it
590 0x21 and 0x22 possibly used by SAA7108E
591 Asus: 0x21 is a correct address (channel 1 of PCF8574)
592 Creative: 0x23 is a correct address (channel 3 of PCF8574)
593 VisionTek: 0x23 is a correct address (channel 3 of PCF8574)
595 static const int pcf_probe[] = { 0x20, 0x21, 0x22, 0x23,
596 0x24, 0x25, 0x26, 0x27, -1 };
597 int ret1, ret2, ret3, ret4;
598 unsigned char bits = 0, flag = 0;
600 memset(&c, 0, sizeof(c));
601 c.adapter = adap;
602 for (i = 0; -1 != pcf_probe[i]; i++) {
603 c.addr = pcf_probe[i];
604 ret1 = i2c_smbus_write_byte(&c, 0xff);
605 ret2 = i2c_smbus_read_byte(&c);
606 ret3 = i2c_smbus_write_byte(&c, 0x00);
607 ret4 = i2c_smbus_read_byte(&c);
609 /* ensure that the writable bitmask works correctly */
610 rc = 0;
611 if (ret1 != -1 && ret2 != -1 &&
612 ret3 != -1 && ret4 != -1) {
613 /* in the Asus TV-Box: bit 1-0 */
614 if (((ret2 & 0x03) == 0x03) &&
615 ((ret4 & 0x03) == 0x00)) {
616 bits = (unsigned char) ~0x07;
617 flag = 0x04;
618 rc = 1;
620 /* in the Creative/VisionTek BreakOut-Box: bit 7-6 */
621 if (((ret2 & 0xc0) == 0xc0) &&
622 ((ret4 & 0xc0) == 0x00)) {
623 bits = (unsigned char) ~0xe0;
624 flag = 0x20;
625 rc = 1;
628 dprintk("probe 0x%02x @ %s: %s\n",
629 c.addr, adap->name, rc ? "yes" : "no");
630 if (rc)
631 ir_attach(adap, pcf_probe[i],
632 bits|(flag<<8), 0);
636 return 0;
639 static int ir_command(struct i2c_client *client, unsigned int cmd, void *arg)
641 /* nothing */
642 return 0;
645 /* ----------------------------------------------------------------------- */
646 #ifdef MODULE
648 int init_module(void)
650 request_module("bttv");
651 request_module("rivatv");
652 request_module("ivtv");
653 request_module("cx8800");
654 i2c_add_driver(&driver);
655 return 0;
658 void cleanup_module(void)
660 i2c_del_driver(&driver);
663 MODULE_DESCRIPTION("Infrared receiver driver for Hauppauge and "
664 "Pixelview cards (i2c stack)");
665 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
666 "Ulrich Mueller, Stefan Jahn, Jerome Brock");
667 MODULE_LICENSE("GPL");
669 module_param(minor, int, 0444);
670 MODULE_PARM_DESC(minor, "Preferred minor device number");
672 module_param(debug, bool, 0644);
673 MODULE_PARM_DESC(debug, "Enable debugging messages");
675 EXPORT_NO_SYMBOLS;
677 #endif /* MODULE */
680 * Overrides for Emacs so that we follow Linus's tabbing style.
681 * ---------------------------------------------------------------------------
682 * Local variables:
683 * c-basic-offset: 8
684 * End: