1 /* $Id: lirc_i2c.c,v 1.47 2008/08/12 20:50:39 lirc Exp $ */
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
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>
60 #include <linux/semaphore.h>
63 #include "drivers/kcompat.h"
64 #include "drivers/lirc_dev/lirc_dev.h"
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...) \
88 printk(KERN_DEBUG DEVICE_NAME ": " fmt, \
92 /* ----------------------------------------------------------------------- */
94 static inline int reverse(int data
, int bits
)
99 for (c
= 0, i
= 0; i
< bits
; i
++)
100 c
|= (((data
& (1<<i
)) ? 1:0)) << (bits
-1-i
);
105 static int add_to_buf_adap(void *data
, struct lirc_buffer
*buf
)
107 struct IR
*ir
= data
;
108 unsigned char keybuf
[4];
111 i2c_master_send(&ir
->c
, keybuf
, 1);
113 if (i2c_master_recv(&ir
->c
, keybuf
, sizeof(keybuf
)) != sizeof(keybuf
)) {
114 dprintk("read error\n");
118 dprintk("key (0x%02x%02x%02x%02x)\n",
119 keybuf
[0], keybuf
[1], keybuf
[2], keybuf
[3]);
122 if (keybuf
[2] == 0xff)
125 /* remove repeat bit */
129 lirc_buffer_write_1(buf
, keybuf
);
133 static int add_to_buf_pcf8574(void *data
, struct lirc_buffer
*buf
)
135 struct IR
*ir
= data
;
137 unsigned char all
, mask
;
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
;
147 rc
= i2c_smbus_write_byte(&ir
->c
, (0xff & all
) | mask
);
149 /* receive scan code */
150 rc
= i2c_smbus_read_byte(&ir
->c
);
153 dprintk("%s read error\n", ir
->c
.name
);
157 /* drop duplicate polls */
158 if (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");
167 /* ignore released buttons */
171 /* set valid key code */
173 lirc_buffer_write_1(buf
, &key
);
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
;
183 unsigned char codes
[2];
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]);
192 dprintk("read error\n");
193 /* keep last successfull read buffer */
197 if ((ir
->b
[0] & 0x80) == 0)
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;
207 lirc_buffer_write_1(buf
, codes
);
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
;
236 flags
= i2c_smbus_read_byte_data(&ir
->c
, 0x10);
238 dprintk("read error\n");
242 if (0 == (flags
& 0x80))
245 /* read actual key code */
246 code
= i2c_smbus_read_byte_data(&ir
->c
, 0x00);
248 dprintk("read error\n");
254 dprintk("IR Key/Flags: (0x%02x/0x%02x)\n", key
, flags
& 0xFF);
257 lirc_buffer_write_1(buf
, &key
);
261 static int add_to_buf_pixelview(void *data
, struct lirc_buffer
*buf
)
263 struct IR
*ir
= data
;
267 if (1 != i2c_master_recv(&ir
->c
, &key
, 1)) {
268 dprintk("read error\n");
271 dprintk("key %02x\n", key
);
274 lirc_buffer_write_1(buf
, &key
);
278 static int add_to_buf_pv951(void *data
, struct lirc_buffer
*buf
)
280 struct IR
*ir
= data
;
282 unsigned char codes
[4];
285 if (1 != i2c_master_recv(&ir
->c
, &key
, 1)) {
286 dprintk("read error\n");
292 dprintk("key %02x\n", key
);
296 codes
[2] = reverse(key
, 8);
297 codes
[3] = (~codes
[2])&0xff;
299 lirc_buffer_write_1(buf
, codes
);
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
;
310 if (1 != i2c_master_recv(&ir
->c
, &key
, 1)) {
311 dprintk("read error\n");
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
);
328 lirc_buffer_write_1(buf
, &key
);
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
);
342 /* lock bttv in memory while /dev/lirc is in use */
343 ret
= i2c_use_client(&ir
->c
);
352 static void set_use_dec(void *data
)
354 struct IR
*ir
= data
;
356 i2c_release_client(&ir
->c
);
360 static struct lirc_plugin lirc_template
= {
362 .set_use_inc
= set_use_inc
,
363 .set_use_dec
= set_use_dec
,
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
,
382 .owner
= THIS_MODULE
,
383 .name
= "i2c ir driver",
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
= {
397 static int ir_attach(struct i2c_adapter
*adap
, int addr
,
398 unsigned short flags
, int kind
)
403 client_template
.adapter
= adap
;
404 client_template
.addr
= addr
;
406 ir
= kmalloc(sizeof(struct IR
), GFP_KERNEL
);
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
;
414 i2c_set_clientdata(&ir
->c
, ir
);
417 ir
->l
.sample_rate
= 10;
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
;
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
;
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
)) {
436 if (adap
->id
== (I2C_ALGO_BIT
| I2C_HW_B_BT848
)) {
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
;
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
;
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
)) {
458 if (adap
->id
== (I2C_ALGO_BIT
| I2C_HW_B_BT848
)) {
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
;
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
;
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;
483 /* shouldn't happen */
484 printk("lirc_i2c: Huh? unknown i2c address (0x%02x)?\n", addr
);
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
);
497 ir
->l
.minor
= lirc_register_plugin(&ir
->l
);
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
);
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
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) */
533 0x64, /* Pixelview IR */
534 0x30, /* KNC ONE IR */
535 0x6b, /* Adaptec IR */
538 #ifdef I2C_HW_B_CX2388x
539 static const int probe_cx88
[] = {
540 0x18, /* Leadtek Winfast PVR2000 */
541 0x71, /* Hauppauge HVR-IR */
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
))
553 if (adap
->id
== (I2C_ALGO_BIT
| I2C_HW_B_BT848
))
556 memset(&c
, 0, sizeof(c
));
558 for (i
= 0; -1 != probe
[i
]; 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");
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
));
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",
579 (1 == rc
) ? "yes" : "no");
581 ir_attach(adap
, c
.addr
, 0, 0);
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
));
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 */
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;
620 /* in the Creative/VisionTek BreakOut-Box: bit 7-6 */
621 if (((ret2
& 0xc0) == 0xc0) &&
622 ((ret4
& 0xc0) == 0x00)) {
623 bits
= (unsigned char) ~0xe0;
628 dprintk("probe 0x%02x @ %s: %s\n",
629 c
.addr
, adap
->name
, rc
? "yes" : "no");
631 ir_attach(adap
, pcf_probe
[i
],
639 static int ir_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
)
645 /* ----------------------------------------------------------------------- */
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
);
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");
680 * Overrides for Emacs so that we follow Linus's tabbing style.
681 * ---------------------------------------------------------------------------