File: lirc_i2c.c

package info (click to toggle)
lirc 0.7.1pre2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,852 kB
  • ctags: 2,924
  • sloc: ansic: 31,205; sh: 12,021; makefile: 631
file content (592 lines) | stat: -rw-r--r-- 14,937 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*      $Id: lirc_i2c.c,v 1.32 2005/02/19 15:13:00 lirc Exp $      */

/*
 * i2c IR lirc plugin for Hauppauge and Pixelview cards - new 2.3.x i2c stack
 *
 * Copyright (c) 2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
 * modified for PixelView (BT878P+W/FM) by
 *      Michal Kochanowicz <mkochano@pld.org.pl>
 *      Christoph Bartelmus <lirc@bartelmus.de>
 * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by
 *      Ulrich Mueller <ulrich.mueller42@web.de>
 * modified for Asus TV-Box and Creative/VisionTek BreakOut-Box by
 *      Stefan Jahn <stefan@lkcc.org>
 * modified for inclusion into kernel sources by
 *      Jerome Brock <jbrock@users.sourceforge.net>
 * modified for Leadtek Winfast PVR2000 by
 *      Thomas Reitmayr (treitmayr@yahoo.com)
 *
 * parts are cut&pasted from the old lirc_haup.c driver
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <linux/version.h>
#include <linux/module.h>
#include <linux/kmod.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/i2c-algo-bit.h>

#include <asm/semaphore.h>

#include "drivers/kcompat.h"
#include "drivers/lirc_dev/lirc_dev.h"

struct IR {
	struct lirc_plugin l;
	struct i2c_client  c;
	int nextkey;
	unsigned char b[3];
	unsigned char bits;
	unsigned char flag;
};

/* ----------------------------------------------------------------------- */

#define DEVICE_NAME "lirc_i2c"

/* ----------------------------------------------------------------------- */
/* insmod parameters                                                       */

static int debug   = 0;    /* debug output */
static int minor   = -1;   /* minor number */

#define dprintk(fmt, args...)                                           \
	do{                                                             \
		if(debug) printk(KERN_DEBUG DEVICE_NAME ": " fmt,       \
				 ## args);                              \
	}while(0)

/* ----------------------------------------------------------------------- */

static inline int reverse(int data, int bits)
{
	int i;
	int c;
	
	for (c=0,i=0; i<bits; i++) {
		c |= (((data & (1<<i)) ? 1:0)) << (bits-1-i);
	}

	return c;
}

static int add_to_buf_pcf8574(void* data, struct lirc_buffer* buf)
{
	struct IR *ir = data;
	int rc;
	unsigned char all, mask;
	unsigned char key;

	/* compute all valid bits (key code + pressed/release flag) */
	all = ir->bits | ir->flag;

	/* save IR writable mask bits */
	mask = i2c_smbus_read_byte(&ir->c) & ~all;

	/* send bit mask */
	rc = i2c_smbus_write_byte(&ir->c, (0xff & all) | mask);

	/* receive scan code */
	rc = i2c_smbus_read_byte(&ir->c);

	if (rc == -1) {
		dprintk("%s read error\n", ir->c.name);
		return -EIO;
	}

	/* drop duplicate polls */
	if (ir->b[0] == (rc & all)) {
		return -ENODATA;
	}
	ir->b[0] = rc & all;

	dprintk("%s key 0x%02X %s\n", ir->c.name, rc & ir->bits,
		(rc & ir->flag) ? "released" : "pressed");

	if (rc & ir->flag) {
		/* ignore released buttons */
		return -ENODATA;
	}

	/* set valid key code */
	key  = rc & ir->bits;
	lirc_buffer_write_1( buf, &key );
	return 0;
}

/* common for Hauppauge IR receivers */
static int add_to_buf_haup_common(void* data, struct lirc_buffer* buf,
		unsigned char* keybuf, int size, int offset)
{
	struct IR *ir = data;
	__u16 code;
	unsigned char codes[2];

	/* poll IR chip */
	if (size == i2c_master_recv(&ir->c,keybuf,size)) {
		ir->b[0] = keybuf[offset];
		ir->b[1] = keybuf[offset+1];
		ir->b[2] = keybuf[offset+2];
		dprintk("key (0x%02x/0x%02x)\n", ir->b[0], ir->b[1]);
	} else {
		dprintk("read error\n");
		/* keep last successfull read buffer */
	}

	/* key pressed ? */
	if ((ir->b[0] & 0x80) == 0)
		return -ENODATA;
	
	/* look what we have */
	code = (((__u16)ir->b[0]&0x7f)<<6) | (ir->b[1]>>2);
	
	codes[0] = (code >> 8) & 0xff;
	codes[1] = code & 0xff;

	/* return it */
	lirc_buffer_write_1( buf, codes );
	return 0;
}

/* specific for the Hauppauge PVR150 IR receiver */
static int add_to_buf_haup_pvr150(void* data, struct lirc_buffer* buf)
{
	unsigned char keybuf[6];
	/* fetch 6 bytes, first relevant is at offset 3 */
	return add_to_buf_haup_common(data, buf, keybuf, 6, 3);
}

/* used for all Hauppauge IR receivers but the PVR150 */
static int add_to_buf_haup(void* data, struct lirc_buffer* buf)
{
	unsigned char keybuf[3];
	/* fetch 3 bytes, first relevant is at offset 0 */
	return add_to_buf_haup_common(data, buf, keybuf, 3, 0);
}


static int add_to_buf_pvr2000(void* data, struct lirc_buffer* buf)
{
	struct IR *ir = data;
	unsigned char key;
	s32 flags;
	s32 code;

	/* poll IR chip */
	if (-1 == (flags = i2c_smbus_read_byte_data(&ir->c,0x10))) {
		dprintk("read error\n");
		return -ENODATA;
	}
	/* key pressed ? */
	if (0 == (flags & 0x80))
		return -ENODATA;

	/* read actual key code */
	if (-1 == (code = i2c_smbus_read_byte_data(&ir->c,0x00))) {
		dprintk("read error\n");
		return -ENODATA;
	}

	key = code & 0xFF;

	dprintk("IR Key/Flags: (0x%02x/0x%02x)\n", key, flags & 0xFF);

	/* return it */
	lirc_buffer_write_1( buf, &key );
	return 0;
}

static int add_to_buf_pixelview(void* data, struct lirc_buffer* buf)
{
	struct IR *ir = data;
	unsigned char key;
	
	/* poll IR chip */
	if (1 != i2c_master_recv(&ir->c,&key,1)) {
		dprintk("read error\n");
		return -1;
	}
	dprintk("key %02x\n", key);

	/* return it */
	lirc_buffer_write_1( buf, &key );
	return 0;
}

static int add_to_buf_pv951(void* data, struct lirc_buffer* buf)
{
	struct IR *ir = data;
	unsigned char key;
	unsigned char codes[4];
	
	/* poll IR chip */
	if (1 != i2c_master_recv(&ir->c,&key,1)) {
		dprintk("read error\n");
		return -ENODATA;
	}
	/* ignore 0xaa */
	if (key==0xaa)
		return -ENODATA;
	dprintk("key %02x\n", key);

	codes[0] = 0x61;
	codes[1] = 0xD6;
	codes[2] = reverse(key,8);
	codes[3] = (~codes[2])&0xff;
	
	lirc_buffer_write_1( buf, codes );
	return 0;
}

static int add_to_buf_knc1(void *data, struct lirc_buffer* buf)
{
	static unsigned char last_key = 0xFF;
	struct IR *ir = data;
	unsigned char key;
	
	/* poll IR chip */
	if (1 != i2c_master_recv(&ir->c,&key,1)) {
		dprintk("read error\n");
		return -ENODATA;
	}
	
	/* it seems that 0xFE indicates that a button is still hold
	   down, while 0xFF indicates that no button is hold
	   down. 0xFE sequences are sometimes interrupted by 0xFF */
	
	dprintk("key %02x\n", key);
	
	if( key == 0xFF )
		return -ENODATA;
	
	if ( key == 0xFE )
		key = last_key;

	last_key = key;
	lirc_buffer_write_1( buf, &key );

	return 0;
}

static int set_use_inc(void* data)
{
	struct IR *ir = data;

	/* lock bttv in memory while /dev/lirc is in use  */
	/* this is completely broken code. lirc_unregister_plugin()
	   must be possible even when the device is open */
	i2c_use_client(&ir->c);

	MOD_INC_USE_COUNT;
	return 0;
}

static void set_use_dec(void* data)
{
	struct IR *ir = data;

	i2c_release_client(&ir->c);
	MOD_DEC_USE_COUNT;
}

static struct lirc_plugin lirc_template = {
	name:        "lirc_i2c",
	set_use_inc: set_use_inc,
	set_use_dec: set_use_dec,
	owner:       THIS_MODULE
};

/* ----------------------------------------------------------------------- */

static int ir_attach(struct i2c_adapter *adap, int addr,
		      unsigned short flags, int kind);
static int ir_detach(struct i2c_client *client);
static int ir_probe(struct i2c_adapter *adap);
static int ir_command(struct i2c_client *client, unsigned int cmd, void *arg);

static struct i2c_driver driver = {
        name:           "i2c ir driver",
        id:             I2C_DRIVERID_EXP3, /* FIXME */
        flags:          I2C_DF_NOTIFY,
        attach_adapter: ir_probe,
        detach_client:  ir_detach,
        command:        ir_command,
};

static struct i2c_client client_template = 
{
        name:   "unset",
        driver: &driver
};

static int ir_attach(struct i2c_adapter *adap, int addr,
		     unsigned short flags, int kind)
{
        struct IR *ir;
	
        client_template.adapter = adap;
        client_template.addr = addr;
	
        if (NULL == (ir = kmalloc(sizeof(struct IR),GFP_KERNEL)))
                return -ENOMEM;
        memcpy(&ir->l,&lirc_template,sizeof(struct lirc_plugin));
        memcpy(&ir->c,&client_template,sizeof(struct i2c_client));
	
	ir->c.adapter = adap;
	ir->c.addr    = addr;
	i2c_set_clientdata(&ir->c, ir);
	ir->l.data    = ir;
	ir->l.minor   = minor;
	ir->l.sample_rate = 10;
	ir->nextkey   = -1;

	switch(addr)
	{
	case 0x64:
		strcpy(ir->c.name,"Pixelview IR");
		ir->l.code_length = 8;
		ir->l.add_to_buf=add_to_buf_pixelview;
		break;
	case 0x4b:
		strcpy(ir->c.name,"PV951 IR");
		ir->l.code_length = 32;
		ir->l.add_to_buf=add_to_buf_pv951;
		break;
	case 0x71:
		/* The PVR150 IR receiver uses the same protocol as other 
		   Hauppauge cards, but the data flow is different, so we need
		   to deal with it by its own.
		 */
		strcpy(ir->c.name,"Hauppauge IR (PVR150)");
		ir->l.code_length = 13;
		ir->l.add_to_buf=add_to_buf_haup_pvr150;
		break;
	case 0x18:
	case 0x1a:
		if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848)) {
			strcpy(ir->c.name,"Hauppauge IR");
			ir->l.code_length = 13;
			ir->l.add_to_buf=add_to_buf_haup;
		}
		else {
			strcpy(ir->c.name,"Leadtek IR");
			ir->l.code_length = 8;
			ir->l.add_to_buf=add_to_buf_pvr2000;
		}
		break;
	case 0x30:
		strcpy(ir->c.name,"KNC ONE IR");
		ir->l.code_length = 8;
		ir->l.add_to_buf=add_to_buf_knc1;
		break;
	case 0x21:
	case 0x23:
		strcpy(ir->c.name,"TV-Box IR");
		ir->l.code_length = 8;
		ir->l.add_to_buf=add_to_buf_pcf8574;
		ir->bits = flags & 0xff;
		ir->flag = (flags >> 8) & 0xff;
		break;
		
	default:
		/* shouldn't happen */
		printk("lirc_i2c: Huh? unknown i2c address (0x%02x)?\n",addr);
		kfree(ir);
		return -1;
	}
	printk("lirc_i2c: chip found @ 0x%02x (%s)\n",addr,ir->c.name);
	
	/* register device */
	i2c_attach_client(&ir->c);
	ir->l.minor = lirc_register_plugin(&ir->l);
	i2c_use_client(&ir->c);
	
	return 0;
}

static int ir_detach(struct i2c_client *client)
{
	struct IR *ir = i2c_get_clientdata(client);
	
	/* unregister device */
	i2c_release_client(&ir->c);
	lirc_unregister_plugin(ir->l.minor);
	i2c_detach_client(&ir->c);

	/* free memory */
	kfree(ir);
	return 0;
}

static int ir_probe(struct i2c_adapter *adap) {
	
	/* The external IR receiver is at i2c address 0x34 (0x35 for
	   reads).  Future Hauppauge cards will have an internal
	   receiver at 0x30 (0x31 for reads).  In theory, both can be
	   fitted, and Hauppauge suggest an external overrides an
	   internal. 
	   
	   That's why we probe 0x1a (~0x34) first. CB 

	   The i2c address for the Hauppauge PVR-150 card is 0xe2,
	   so we need to probe 0x71 as well.
	*/
	
	static const int probe[] = { 0x1a, 0x18, 0x71, 0x4b, 0x64, 0x30, -1};
	struct i2c_client c; char buf; int i,rc;

	if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848)) {
		memset(&c,0,sizeof(c));
		c.adapter = adap;
		for (i = 0; -1 != probe[i]; i++) {
			c.addr = probe[i];
			rc = i2c_master_recv(&c,&buf,1);
			dprintk("probe 0x%02x @ %s: %s\n",
				probe[i], adap->name, 
				(1 == rc) ? "yes" : "no");
			if (1 == rc)
			{
				ir_attach(adap,probe[i],0,0);
			}
		}
	}

#ifdef I2C_HW_B_CX2388x
	/* Leadtek Winfast PVR2000 */
	else if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_CX2388x)) {
		memset(&c,0,sizeof(c));
		c.adapter = adap;
		c.addr    = 0x18;
		rc = i2c_master_recv(&c,&buf,1);
		dprintk("probe 0x%02x @ %s: %s\n",
			c.addr, adap->name, 
			(1 == rc) ? "yes" : "no");
		if (1 == rc) {
			ir_attach(adap,c.addr,0,0);
		}
	}
#endif

	/* Asus TV-Box and Creative/VisionTek BreakOut-Box (PCF8574) */
	else if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_RIVA)) {
		/* addresses to probe;
		   leave 0x24 and 0x25 because SAA7113H possibly uses it 
		   0x21 and 0x22 possibly used by SAA7108E 
		   Asus:      0x21 is a correct address (channel 1 of PCF8574)
		   Creative:  0x23 is a correct address (channel 3 of PCF8574)
		   VisionTek: 0x23 is a correct address (channel 3 of PCF8574)
		*/
		static const int pcf_probe[] = { 0x20, 0x21, 0x22, 0x23,
						 0x24, 0x25, 0x26, 0x27, -1 };
		int ret1, ret2, ret3, ret4;
		unsigned char bits = 0, flag = 0;

		memset(&c,0,sizeof(c));
		c.adapter = adap;
		for (i = 0; -1 != pcf_probe[i]; i++) {
			c.addr = pcf_probe[i];
			ret1 = i2c_smbus_write_byte(&c, 0xff);
			ret2 = i2c_smbus_read_byte(&c);
			ret3 = i2c_smbus_write_byte(&c, 0x00);
			ret4 = i2c_smbus_read_byte(&c);

			/* ensure that the writable bitmask works correctly */
			rc = 0;
			if (ret1 != -1 && ret2 != -1 && 
			    ret3 != -1 && ret4 != -1) {
				/* in the Asus TV-Box: bit 1-0 */
				if (((ret2 & 0x03) == 0x03) && 
				    ((ret4 & 0x03) == 0x00)) {
					bits = (unsigned char) ~0x07;
					flag = 0x04;
					rc = 1;
				}
				/* in the Creative/VisionTek BreakOut-Box: bit 7-6 */
				if (((ret2 & 0xc0) == 0xc0) && 
				    ((ret4 & 0xc0) == 0x00)) {
					bits = (unsigned char) ~0xe0;
					flag = 0x20;
					rc = 1;
				}
			}
			dprintk("probe 0x%02x @ %s: %s\n",
				c.addr, adap->name, rc ? "yes" : "no");
			if (rc)
				ir_attach(adap,pcf_probe[i],bits|(flag<<8),0);
		}
	}
		
	return 0;
}

static int ir_command(struct i2c_client *client,unsigned int cmd, void *arg)
{
	/* nothing */
	return 0;
}

/* ----------------------------------------------------------------------- */
#ifdef MODULE

int init_module(void)
{
	request_module("bttv");
	request_module("rivatv");
	request_module("ivtv");
	request_module("cx8800");
	i2c_add_driver(&driver);
	return 0;
}

void cleanup_module(void)
{
	i2c_del_driver(&driver);
}

MODULE_DESCRIPTION("Infrared receiver driver for Hauppauge and Pixelview cards (i2c stack)");
MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller, Stefan Jahn, Jerome Brock");
MODULE_LICENSE("GPL");

module_param(minor, int, 0444);
MODULE_PARM_DESC(minor, "Preferred minor device number");

module_param(debug, bool, 0644);
MODULE_PARM_DESC(debug, "Enable debugging messages");

EXPORT_NO_SYMBOLS;

#endif /* MODULE */

/*
 * Overrides for Emacs so that we follow Linus's tabbing style.
 * ---------------------------------------------------------------------------
 * Local variables:
 * c-basic-offset: 8
 * End:
 */