blob: 6df2c0e8d721951604acafe63df2467a07108715 [file] [log] [blame]
Jarod Wilson085ffd02010-07-26 20:28:37 -03001/*
2 * lirc_i2c.c
3 *
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
11 *
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)
26 *
27 * parts are cut&pasted from the old lirc_haup.c driver
28 *
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.
33 *
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.
38 *
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
42 */
43
44
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>
57
58#include <media/lirc_dev.h>
59
60struct 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;
67};
68
69#define DEVICE_NAME "lirc_i2c"
70
71/* module parameters */
72static int debug; /* debug output */
73static int minor = -1; /* minor number */
74
75#define dprintk(fmt, args...) \
76 do { \
77 if (debug) \
78 printk(KERN_DEBUG DEVICE_NAME ": " fmt, \
79 ## args); \
80 } while (0)
81
82static int reverse(int data, int bits)
83{
84 int i;
85 int c;
86
87 for (c = 0, i = 0; i < bits; i++)
88 c |= ((data & (1<<i)) ? 1 : 0) << (bits-1-i);
89
90 return c;
91}
92
93static int add_to_buf_adap(void *data, struct lirc_buffer *buf)
94{
95 struct IR *ir = data;
96 unsigned char keybuf[4];
97
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;
104 }
105
106 dprintk("key (0x%02x%02x%02x%02x)\n",
107 keybuf[0], keybuf[1], keybuf[2], keybuf[3]);
108
109 /* key pressed ? */
110 if (keybuf[2] == 0xff)
111 return -ENODATA;
112
113 /* remove repeat bit */
114 keybuf[2] &= 0x7f;
115 keybuf[3] |= 0x80;
116
117 lirc_buffer_write(buf, keybuf);
118 return 0;
119}
120
121static int add_to_buf_pcf8574(void *data, struct lirc_buffer *buf)
122{
123 struct IR *ir = data;
124 int rc;
125 unsigned char all, mask;
126 unsigned char key;
127
128 /* compute all valid bits (key code + pressed/release flag) */
129 all = ir->bits | ir->flag;
130
131 /* save IR writable mask bits */
132 mask = i2c_smbus_read_byte(&ir->c) & ~all;
133
134 /* send bit mask */
135 rc = i2c_smbus_write_byte(&ir->c, (0xff & all) | mask);
136
137 /* receive scan code */
138 rc = i2c_smbus_read_byte(&ir->c);
139
140 if (rc == -1) {
141 dprintk("%s read error\n", ir->c.name);
142 return -EIO;
143 }
144
145 /* drop duplicate polls */
146 if (ir->b[0] == (rc & all))
147 return -ENODATA;
148
149 ir->b[0] = rc & all;
150
151 dprintk("%s key 0x%02X %s\n", ir->c.name, rc & ir->bits,
152 (rc & ir->flag) ? "released" : "pressed");
153
154 /* ignore released buttons */
155 if (rc & ir->flag)
156 return -ENODATA;
157
158 /* set valid key code */
159 key = rc & ir->bits;
160 lirc_buffer_write(buf, &key);
161 return 0;
162}
163
164/* common for Hauppauge IR receivers */
165static int add_to_buf_haup_common(void *data, struct lirc_buffer *buf,
166 unsigned char *keybuf, int size, int offset)
167{
168 struct IR *ir = data;
169 __u16 code;
170 unsigned char codes[2];
171 int ret;
172
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 */
184 }
185
186 /* key pressed ? */
187 if ((ir->b[0] & 0x80) == 0)
188 return -ENODATA;
189
190 /* look what we have */
191 code = (((__u16)ir->b[0]&0x7f)<<6) | (ir->b[1]>>2);
192
193 codes[0] = (code >> 8) & 0xff;
194 codes[1] = code & 0xff;
195
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;
200}
201
202/* specific for the Hauppauge PVR150 IR receiver */
203static int add_to_buf_haup_pvr150(void *data, struct lirc_buffer *buf)
204{
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);
208}
209
210/* used for all Hauppauge IR receivers but the PVR150 */
211static int add_to_buf_haup(void *data, struct lirc_buffer *buf)
212{
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);
216}
217
218
219static int add_to_buf_pvr2000(void *data, struct lirc_buffer *buf)
220{
221 struct IR *ir = data;
222 unsigned char key;
223 s32 flags;
224 s32 code;
225
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;
231 }
232 /* key pressed ? */
233 if (0 == (flags & 0x80))
234 return -ENODATA;
235
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;
241 }
242
243 key = code & 0xFF;
244
245 dprintk("IR Key/Flags: (0x%02x/0x%02x)\n", key, flags & 0xFF);
246
247 /* return it */
248 lirc_buffer_write(buf, &key);
249 return 0;
250}
251
252static int add_to_buf_pixelview(void *data, struct lirc_buffer *buf)
253{
254 struct IR *ir = data;
255 unsigned char key;
256
257 /* poll IR chip */
258 if (1 != i2c_master_recv(&ir->c, &key, 1)) {
259 dprintk("read error\n");
260 return -1;
261 }
262 dprintk("key %02x\n", key);
263
264 /* return it */
265 lirc_buffer_write(buf, &key);
266 return 0;
267}
268
269static int add_to_buf_pv951(void *data, struct lirc_buffer *buf)
270{
271 struct IR *ir = data;
272 unsigned char key;
273 unsigned char codes[4];
274
275 /* poll IR chip */
276 if (1 != i2c_master_recv(&ir->c, &key, 1)) {
277 dprintk("read error\n");
278 return -ENODATA;
279 }
280 /* ignore 0xaa */
281 if (key == 0xaa)
282 return -ENODATA;
283 dprintk("key %02x\n", key);
284
285 codes[0] = 0x61;
286 codes[1] = 0xD6;
287 codes[2] = reverse(key, 8);
288 codes[3] = (~codes[2])&0xff;
289
290 lirc_buffer_write(buf, codes);
291 return 0;
292}
293
294static int add_to_buf_knc1(void *data, struct lirc_buffer *buf)
295{
296 static unsigned char last_key = 0xFF;
297 struct IR *ir = data;
298 unsigned char key;
299
300 /* poll IR chip */
301 if (1 != i2c_master_recv(&ir->c, &key, 1)) {
302 dprintk("read error\n");
303 return -ENODATA;
304 }
305
306 /*
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
310 */
311
312 dprintk("key %02x\n", key);
313
314 if (key == 0xFF)
315 return -ENODATA;
316
317 if (key == 0xFE)
318 key = last_key;
319
320 last_key = key;
321 lirc_buffer_write(buf, &key);
322
323 return 0;
324}
325
326static int set_use_inc(void *data)
327{
328 struct IR *ir = data;
329
330 dprintk("%s called\n", __func__);
331
332 /* lock bttv in memory while /dev/lirc is in use */
333 i2c_use_client(&ir->c);
334
335 return 0;
336}
337
338static void set_use_dec(void *data)
339{
340 struct IR *ir = data;
341
342 dprintk("%s called\n", __func__);
343
344 i2c_release_client(&ir->c);
345}
346
347static 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,
353};
354
355static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
356static int ir_remove(struct i2c_client *client);
357static int ir_command(struct i2c_client *client, unsigned int cmd, void *arg);
358
359static 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 */
363 { }
364};
365
366static struct i2c_driver driver = {
367 .driver = {
368 .owner = THIS_MODULE,
369 .name = "i2c ir driver",
370 },
371 .probe = ir_probe,
372 .remove = ir_remove,
373 .id_table = ir_receiver_id,
374 .command = ir_command,
375};
376
377static void pcf_probe(struct i2c_client *client, struct IR *ir)
378{
379 int ret1, ret2, ret3, ret4;
380
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);
385
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;
394 }
395
396 return;
397}
398
399static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
400{
401 struct IR *ir;
402 struct i2c_adapter *adap = client->adapter;
403 unsigned short addr = client->addr;
404 int retval;
405
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));
411
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;
418
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 */
434 /*
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.
438 */
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;
458 }
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;
477 }
478 printk(KERN_INFO "lirc_i2c: chip 0x%x found @ 0x%02x (%s)\n",
479 adap->id, addr, ir->c.name);
480
481 retval = lirc_register_driver(&ir->l);
482
483 if (retval < 0) {
484 printk(KERN_ERR "lirc_i2c: failed to register driver!\n");
485 kfree(ir);
486 return retval;
487 }
488
489 ir->l.minor = retval;
490
491 return 0;
492}
493
494static int ir_remove(struct i2c_client *client)
495{
496 struct IR *ir = i2c_get_clientdata(client);
497
498 /* unregister device */
499 lirc_unregister_driver(ir->l.minor);
500
501 /* free memory */
502 kfree(ir);
503 return 0;
504}
505
506static int ir_command(struct i2c_client *client, unsigned int cmd, void *arg)
507{
508 /* nothing */
509 return 0;
510}
511
512static int __init lirc_i2c_init(void)
513{
514 i2c_add_driver(&driver);
515 return 0;
516}
517
518static void __exit lirc_i2c_exit(void)
519{
520 i2c_del_driver(&driver);
521}
522
523MODULE_DESCRIPTION("Infrared receiver driver for Hauppauge and "
524 "Pixelview cards (i2c stack)");
525MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
526 "Ulrich Mueller, Stefan Jahn, Jerome Brock");
527MODULE_LICENSE("GPL");
528
529module_param(minor, int, S_IRUGO);
530MODULE_PARM_DESC(minor, "Preferred minor device number");
531
532module_param(debug, bool, S_IRUGO | S_IWUSR);
533MODULE_PARM_DESC(debug, "Enable debugging messages");
534
535module_init(lirc_i2c_init);
536module_exit(lirc_i2c_exit);