4 * i2c IR driver for the onboard IR port on many TV tuner cards, including:
5 * -Flavors of the Hauppauge PVR-150/250/350
7 * -PixelView (BT878P+W/FM)
8 * -KNC ONE TV Station/Anubis Typhoon TView Tuner
9 * -Asus TV-Box and Creative/VisionTek BreakOut-Box
10 * -Leadtek Winfast PVR2000
12 * Copyright (c) 2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
13 * modified for PixelView (BT878P+W/FM) by
14 * Michal Kochanowicz <mkochano@pld.org.pl>
15 * Christoph Bartelmus <lirc@bartelmus.de>
16 * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by
17 * Ulrich Mueller <ulrich.mueller42@web.de>
18 * modified for Asus TV-Box and Creative/VisionTek BreakOut-Box by
19 * Stefan Jahn <stefan@lkcc.org>
20 * modified for inclusion into kernel sources by
21 * Jerome Brock <jbrock@users.sourceforge.net>
22 * modified for Leadtek Winfast PVR2000 by
23 * Thomas Reitmayr (treitmayr@yahoo.com)
24 * modified for Hauppauge HVR-1300 by
25 * Jan Frey (jfrey@gmx.de)
27 * parts are cut&pasted from the old lirc_haup.c driver
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
45 #include <linux/version.h>
46 #include <linux/module.h>
47 #include <linux/kmod.h>
48 #include <linux/kernel.h>
49 #include <linux/sched.h>
50 #include <linux/string.h>
51 #include <linux/timer.h>
52 #include <linux/delay.h>
53 #include <linux/errno.h>
54 #include <linux/slab.h>
55 #include <linux/i2c.h>
56 #include <linux/i2c-algo-bit.h>
58 #include <media/lirc_dev.h>
69 #define DEVICE_NAME "lirc_i2c"
71 /* module parameters */
72 static int debug
; /* debug output */
73 static int minor
= -1; /* minor number */
75 #define dprintk(fmt, args...) \
78 printk(KERN_DEBUG DEVICE_NAME ": " fmt, \
82 static int reverse(int data
, int bits
)
87 for (c
= 0, i
= 0; i
< bits
; i
++)
88 c
|= ((data
& (1<<i
)) ? 1 : 0) << (bits
-1-i
);
93 static int add_to_buf_adap(void *data
, struct lirc_buffer
*buf
)
96 unsigned char keybuf
[4];
99 i2c_master_send(&ir
->c
, keybuf
, 1);
101 if (i2c_master_recv(&ir
->c
, keybuf
, sizeof(keybuf
)) != sizeof(keybuf
)) {
102 dprintk("read error\n");
106 dprintk("key (0x%02x%02x%02x%02x)\n",
107 keybuf
[0], keybuf
[1], keybuf
[2], keybuf
[3]);
110 if (keybuf
[2] == 0xff)
113 /* remove repeat bit */
117 lirc_buffer_write(buf
, keybuf
);
121 static int add_to_buf_pcf8574(void *data
, struct lirc_buffer
*buf
)
123 struct IR
*ir
= data
;
125 unsigned char all
, mask
;
128 /* compute all valid bits (key code + pressed/release flag) */
129 all
= ir
->bits
| ir
->flag
;
131 /* save IR writable mask bits */
132 mask
= i2c_smbus_read_byte(&ir
->c
) & ~all
;
135 rc
= i2c_smbus_write_byte(&ir
->c
, (0xff & all
) | mask
);
137 /* receive scan code */
138 rc
= i2c_smbus_read_byte(&ir
->c
);
141 dprintk("%s read error\n", ir
->c
.name
);
145 /* drop duplicate polls */
146 if (ir
->b
[0] == (rc
& all
))
151 dprintk("%s key 0x%02X %s\n", ir
->c
.name
, rc
& ir
->bits
,
152 (rc
& ir
->flag
) ? "released" : "pressed");
154 /* ignore released buttons */
158 /* set valid key code */
160 lirc_buffer_write(buf
, &key
);
164 /* common for Hauppauge IR receivers */
165 static int add_to_buf_haup_common(void *data
, struct lirc_buffer
*buf
,
166 unsigned char *keybuf
, int size
, int offset
)
168 struct IR
*ir
= data
;
170 unsigned char codes
[2];
174 ret
= i2c_master_recv(&ir
->c
, keybuf
, size
);
176 ir
->b
[0] = keybuf
[offset
];
177 ir
->b
[1] = keybuf
[offset
+1];
178 ir
->b
[2] = keybuf
[offset
+2];
179 if (ir
->b
[0] != 0x00 && ir
->b
[1] != 0x00)
180 dprintk("key (0x%02x/0x%02x)\n", ir
->b
[0], ir
->b
[1]);
182 dprintk("read error (ret=%d)\n", ret
);
183 /* keep last successful read buffer */
187 if ((ir
->b
[0] & 0x80) == 0)
190 /* look what we have */
191 code
= (((__u16
)ir
->b
[0]&0x7f)<<6) | (ir
->b
[1]>>2);
193 codes
[0] = (code
>> 8) & 0xff;
194 codes
[1] = code
& 0xff;
197 dprintk("sending code 0x%02x%02x to lirc\n", codes
[0], codes
[1]);
198 lirc_buffer_write(buf
, codes
);
202 /* specific for the Hauppauge PVR150 IR receiver */
203 static int add_to_buf_haup_pvr150(void *data
, struct lirc_buffer
*buf
)
205 unsigned char keybuf
[6];
206 /* fetch 6 bytes, first relevant is at offset 3 */
207 return add_to_buf_haup_common(data
, buf
, keybuf
, 6, 3);
210 /* used for all Hauppauge IR receivers but the PVR150 */
211 static int add_to_buf_haup(void *data
, struct lirc_buffer
*buf
)
213 unsigned char keybuf
[3];
214 /* fetch 3 bytes, first relevant is at offset 0 */
215 return add_to_buf_haup_common(data
, buf
, keybuf
, 3, 0);
219 static int add_to_buf_pvr2000(void *data
, struct lirc_buffer
*buf
)
221 struct IR
*ir
= data
;
227 flags
= i2c_smbus_read_byte_data(&ir
->c
, 0x10);
229 dprintk("read error\n");
233 if (0 == (flags
& 0x80))
236 /* read actual key code */
237 code
= i2c_smbus_read_byte_data(&ir
->c
, 0x00);
239 dprintk("read error\n");
245 dprintk("IR Key/Flags: (0x%02x/0x%02x)\n", key
, flags
& 0xFF);
248 lirc_buffer_write(buf
, &key
);
252 static int add_to_buf_pixelview(void *data
, struct lirc_buffer
*buf
)
254 struct IR
*ir
= data
;
258 if (1 != i2c_master_recv(&ir
->c
, &key
, 1)) {
259 dprintk("read error\n");
262 dprintk("key %02x\n", key
);
265 lirc_buffer_write(buf
, &key
);
269 static int add_to_buf_pv951(void *data
, struct lirc_buffer
*buf
)
271 struct IR
*ir
= data
;
273 unsigned char codes
[4];
276 if (1 != i2c_master_recv(&ir
->c
, &key
, 1)) {
277 dprintk("read error\n");
283 dprintk("key %02x\n", key
);
287 codes
[2] = reverse(key
, 8);
288 codes
[3] = (~codes
[2])&0xff;
290 lirc_buffer_write(buf
, codes
);
294 static int add_to_buf_knc1(void *data
, struct lirc_buffer
*buf
)
296 static unsigned char last_key
= 0xFF;
297 struct IR
*ir
= data
;
301 if (1 != i2c_master_recv(&ir
->c
, &key
, 1)) {
302 dprintk("read error\n");
307 * it seems that 0xFE indicates that a button is still held
308 * down, while 0xFF indicates that no button is held
309 * down. 0xFE sequences are sometimes interrupted by 0xFF
312 dprintk("key %02x\n", key
);
321 lirc_buffer_write(buf
, &key
);
326 static int set_use_inc(void *data
)
328 struct IR
*ir
= data
;
330 dprintk("%s called\n", __func__
);
332 /* lock bttv in memory while /dev/lirc is in use */
333 i2c_use_client(&ir
->c
);
338 static void set_use_dec(void *data
)
340 struct IR
*ir
= data
;
342 dprintk("%s called\n", __func__
);
344 i2c_release_client(&ir
->c
);
347 static struct lirc_driver lirc_template
= {
349 .set_use_inc
= set_use_inc
,
350 .set_use_dec
= set_use_dec
,
352 .owner
= THIS_MODULE
,
355 static int ir_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
);
356 static int ir_remove(struct i2c_client
*client
);
357 static int ir_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
);
359 static const struct i2c_device_id ir_receiver_id
[] = {
360 /* Generic entry for any IR receiver */
362 /* IR device specific entries could be added here */
366 static struct i2c_driver driver
= {
368 .owner
= THIS_MODULE
,
369 .name
= "i2c ir driver",
373 .id_table
= ir_receiver_id
,
374 .command
= ir_command
,
377 static void pcf_probe(struct i2c_client
*client
, struct IR
*ir
)
379 int ret1
, ret2
, ret3
, ret4
;
381 ret1
= i2c_smbus_write_byte(client
, 0xff);
382 ret2
= i2c_smbus_read_byte(client
);
383 ret3
= i2c_smbus_write_byte(client
, 0x00);
384 ret4
= i2c_smbus_read_byte(client
);
386 /* in the Asus TV-Box: bit 1-0 */
387 if (((ret2
& 0x03) == 0x03) && ((ret4
& 0x03) == 0x00)) {
388 ir
->bits
= (unsigned char) ~0x07;
390 /* in the Creative/VisionTek BreakOut-Box: bit 7-6 */
391 } else if (((ret2
& 0xc0) == 0xc0) && ((ret4
& 0xc0) == 0x00)) {
392 ir
->bits
= (unsigned char) ~0xe0;
399 static int ir_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
402 struct i2c_adapter
*adap
= client
->adapter
;
403 unsigned short addr
= client
->addr
;
406 ir
= kzalloc(sizeof(struct IR
), GFP_KERNEL
);
409 memcpy(&ir
->l
, &lirc_template
, sizeof(struct lirc_driver
));
410 memcpy(&ir
->c
, client
, sizeof(struct i2c_client
));
412 i2c_set_clientdata(client
, ir
);
415 ir
->l
.sample_rate
= 10;
416 ir
->l
.dev
= &ir
->c
.dev
;
421 strlcpy(ir
->c
.name
, "Pixelview IR", I2C_NAME_SIZE
);
422 ir
->l
.code_length
= 8;
423 ir
->l
.add_to_buf
= add_to_buf_pixelview
;
426 strlcpy(ir
->c
.name
, "PV951 IR", I2C_NAME_SIZE
);
427 ir
->l
.code_length
= 32;
428 ir
->l
.add_to_buf
= add_to_buf_pv951
;
431 if (adap
->id
== I2C_HW_B_CX2388x
)
432 strlcpy(ir
->c
.name
, "Hauppauge HVR1300", I2C_NAME_SIZE
);
433 else /* bt8xx or cx2341x */
435 * The PVR150 IR receiver uses the same protocol as
436 * other Hauppauge cards, but the data flow is
437 * different, so we need to deal with it by its own.
439 strlcpy(ir
->c
.name
, "Hauppauge PVR150", I2C_NAME_SIZE
);
440 ir
->l
.code_length
= 13;
441 ir
->l
.add_to_buf
= add_to_buf_haup_pvr150
;
444 strlcpy(ir
->c
.name
, "Adaptec IR", I2C_NAME_SIZE
);
445 ir
->l
.code_length
= 32;
446 ir
->l
.add_to_buf
= add_to_buf_adap
;
450 if (adap
->id
== I2C_HW_B_CX2388x
) {
451 strlcpy(ir
->c
.name
, "Leadtek IR", I2C_NAME_SIZE
);
452 ir
->l
.code_length
= 8;
453 ir
->l
.add_to_buf
= add_to_buf_pvr2000
;
454 } else { /* bt8xx or cx2341x */
455 strlcpy(ir
->c
.name
, "Hauppauge IR", I2C_NAME_SIZE
);
456 ir
->l
.code_length
= 13;
457 ir
->l
.add_to_buf
= add_to_buf_haup
;
461 strlcpy(ir
->c
.name
, "KNC ONE IR", I2C_NAME_SIZE
);
462 ir
->l
.code_length
= 8;
463 ir
->l
.add_to_buf
= add_to_buf_knc1
;
467 pcf_probe(client
, ir
);
468 strlcpy(ir
->c
.name
, "TV-Box IR", I2C_NAME_SIZE
);
469 ir
->l
.code_length
= 8;
470 ir
->l
.add_to_buf
= add_to_buf_pcf8574
;
473 /* shouldn't happen */
474 printk("lirc_i2c: Huh? unknown i2c address (0x%02x)?\n", addr
);
478 printk(KERN_INFO
"lirc_i2c: chip 0x%x found @ 0x%02x (%s)\n",
479 adap
->id
, addr
, ir
->c
.name
);
481 retval
= lirc_register_driver(&ir
->l
);
484 printk(KERN_ERR
"lirc_i2c: failed to register driver!\n");
489 ir
->l
.minor
= retval
;
494 static int ir_remove(struct i2c_client
*client
)
496 struct IR
*ir
= i2c_get_clientdata(client
);
498 /* unregister device */
499 lirc_unregister_driver(ir
->l
.minor
);
506 static int ir_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
)
512 static int __init
lirc_i2c_init(void)
514 i2c_add_driver(&driver
);
518 static void __exit
lirc_i2c_exit(void)
520 i2c_del_driver(&driver
);
523 MODULE_DESCRIPTION("Infrared receiver driver for Hauppauge and "
524 "Pixelview cards (i2c stack)");
525 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
526 "Ulrich Mueller, Stefan Jahn, Jerome Brock");
527 MODULE_LICENSE("GPL");
529 module_param(minor
, int, S_IRUGO
);
530 MODULE_PARM_DESC(minor
, "Preferred minor device number");
532 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
533 MODULE_PARM_DESC(debug
, "Enable debugging messages");
535 module_init(lirc_i2c_init
);
536 module_exit(lirc_i2c_exit
);