GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / lirc / lirc_i2c.c
blob6df2c0e8d721951604acafe63df2467a07108715
1 /*
2 * lirc_i2c.c
4 * i2c IR driver for the onboard IR port on many TV tuner cards, including:
5 * -Flavors of the Hauppauge PVR-150/250/350
6 * -Hauppauge HVR-1300
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>
60 struct IR {
61 struct lirc_driver l;
62 struct i2c_client c;
63 int nextkey;
64 unsigned char b[3];
65 unsigned char bits;
66 unsigned char flag;
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...) \
76 do { \
77 if (debug) \
78 printk(KERN_DEBUG DEVICE_NAME ": " fmt, \
79 ## args); \
80 } while (0)
82 static int reverse(int data, int bits)
84 int i;
85 int c;
87 for (c = 0, i = 0; i < bits; i++)
88 c |= ((data & (1<<i)) ? 1 : 0) << (bits-1-i);
90 return c;
93 static int add_to_buf_adap(void *data, struct lirc_buffer *buf)
95 struct IR *ir = data;
96 unsigned char keybuf[4];
98 keybuf[0] = 0x00;
99 i2c_master_send(&ir->c, keybuf, 1);
100 /* poll IR chip */
101 if (i2c_master_recv(&ir->c, keybuf, sizeof(keybuf)) != sizeof(keybuf)) {
102 dprintk("read error\n");
103 return -EIO;
106 dprintk("key (0x%02x%02x%02x%02x)\n",
107 keybuf[0], keybuf[1], keybuf[2], keybuf[3]);
109 /* key pressed ? */
110 if (keybuf[2] == 0xff)
111 return -ENODATA;
113 /* remove repeat bit */
114 keybuf[2] &= 0x7f;
115 keybuf[3] |= 0x80;
117 lirc_buffer_write(buf, keybuf);
118 return 0;
121 static int add_to_buf_pcf8574(void *data, struct lirc_buffer *buf)
123 struct IR *ir = data;
124 int rc;
125 unsigned char all, mask;
126 unsigned char key;
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;
134 /* send bit mask */
135 rc = i2c_smbus_write_byte(&ir->c, (0xff & all) | mask);
137 /* receive scan code */
138 rc = i2c_smbus_read_byte(&ir->c);
140 if (rc == -1) {
141 dprintk("%s read error\n", ir->c.name);
142 return -EIO;
145 /* drop duplicate polls */
146 if (ir->b[0] == (rc & all))
147 return -ENODATA;
149 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 */
155 if (rc & ir->flag)
156 return -ENODATA;
158 /* set valid key code */
159 key = rc & ir->bits;
160 lirc_buffer_write(buf, &key);
161 return 0;
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;
169 __u16 code;
170 unsigned char codes[2];
171 int ret;
173 /* poll IR chip */
174 ret = i2c_master_recv(&ir->c, keybuf, size);
175 if (ret == 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]);
181 } else {
182 dprintk("read error (ret=%d)\n", ret);
183 /* keep last successful read buffer */
186 /* key pressed ? */
187 if ((ir->b[0] & 0x80) == 0)
188 return -ENODATA;
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;
196 /* return it */
197 dprintk("sending code 0x%02x%02x to lirc\n", codes[0], codes[1]);
198 lirc_buffer_write(buf, codes);
199 return 0;
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;
222 unsigned char key;
223 s32 flags;
224 s32 code;
226 /* poll IR chip */
227 flags = i2c_smbus_read_byte_data(&ir->c, 0x10);
228 if (-1 == flags) {
229 dprintk("read error\n");
230 return -ENODATA;
232 /* key pressed ? */
233 if (0 == (flags & 0x80))
234 return -ENODATA;
236 /* read actual key code */
237 code = i2c_smbus_read_byte_data(&ir->c, 0x00);
238 if (-1 == code) {
239 dprintk("read error\n");
240 return -ENODATA;
243 key = code & 0xFF;
245 dprintk("IR Key/Flags: (0x%02x/0x%02x)\n", key, flags & 0xFF);
247 /* return it */
248 lirc_buffer_write(buf, &key);
249 return 0;
252 static int add_to_buf_pixelview(void *data, struct lirc_buffer *buf)
254 struct IR *ir = data;
255 unsigned char key;
257 /* poll IR chip */
258 if (1 != i2c_master_recv(&ir->c, &key, 1)) {
259 dprintk("read error\n");
260 return -1;
262 dprintk("key %02x\n", key);
264 /* return it */
265 lirc_buffer_write(buf, &key);
266 return 0;
269 static int add_to_buf_pv951(void *data, struct lirc_buffer *buf)
271 struct IR *ir = data;
272 unsigned char key;
273 unsigned char codes[4];
275 /* poll IR chip */
276 if (1 != i2c_master_recv(&ir->c, &key, 1)) {
277 dprintk("read error\n");
278 return -ENODATA;
280 /* ignore 0xaa */
281 if (key == 0xaa)
282 return -ENODATA;
283 dprintk("key %02x\n", key);
285 codes[0] = 0x61;
286 codes[1] = 0xD6;
287 codes[2] = reverse(key, 8);
288 codes[3] = (~codes[2])&0xff;
290 lirc_buffer_write(buf, codes);
291 return 0;
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;
298 unsigned char key;
300 /* poll IR chip */
301 if (1 != i2c_master_recv(&ir->c, &key, 1)) {
302 dprintk("read error\n");
303 return -ENODATA;
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);
314 if (key == 0xFF)
315 return -ENODATA;
317 if (key == 0xFE)
318 key = last_key;
320 last_key = key;
321 lirc_buffer_write(buf, &key);
323 return 0;
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);
335 return 0;
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 = {
348 .name = "lirc_i2c",
349 .set_use_inc = set_use_inc,
350 .set_use_dec = set_use_dec,
351 .dev = NULL,
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 */
361 { "ir_video", 0 },
362 /* IR device specific entries could be added here */
366 static struct i2c_driver driver = {
367 .driver = {
368 .owner = THIS_MODULE,
369 .name = "i2c ir driver",
371 .probe = ir_probe,
372 .remove = ir_remove,
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;
389 ir->flag = 0x04;
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;
393 ir->flag = 0x20;
396 return;
399 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
401 struct IR *ir;
402 struct i2c_adapter *adap = client->adapter;
403 unsigned short addr = client->addr;
404 int retval;
406 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
407 if (!ir)
408 return -ENOMEM;
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);
413 ir->l.data = ir;
414 ir->l.minor = minor;
415 ir->l.sample_rate = 10;
416 ir->l.dev = &ir->c.dev;
417 ir->nextkey = -1;
419 switch (addr) {
420 case 0x64:
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;
424 break;
425 case 0x4b:
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;
429 break;
430 case 0x71:
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;
442 break;
443 case 0x6b:
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;
447 break;
448 case 0x18:
449 case 0x1a:
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;
459 break;
460 case 0x30:
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;
464 break;
465 case 0x21:
466 case 0x23:
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;
471 break;
472 default:
473 /* shouldn't happen */
474 printk("lirc_i2c: Huh? unknown i2c address (0x%02x)?\n", addr);
475 kfree(ir);
476 return -EINVAL;
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);
483 if (retval < 0) {
484 printk(KERN_ERR "lirc_i2c: failed to register driver!\n");
485 kfree(ir);
486 return retval;
489 ir->l.minor = retval;
491 return 0;
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);
501 /* free memory */
502 kfree(ir);
503 return 0;
506 static int ir_command(struct i2c_client *client, unsigned int cmd, void *arg)
508 /* nothing */
509 return 0;
512 static int __init lirc_i2c_init(void)
514 i2c_add_driver(&driver);
515 return 0;
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);