3 * keyboard input driver for i2c IR remote controls
5 * Copyright (c) 2000-2003 Gerd Knorr <kraxel@bytesex.org>
6 * modified for PixelView (BT878P+W/FM) by
7 * Michal Kochanowicz <mkochano@pld.org.pl>
8 * Christoph Bartelmus <lirc@bartelmus.de>
9 * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by
10 * Ulrich Mueller <ulrich.mueller42@web.de>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/init.h>
31 #include <linux/kernel.h>
32 #include <linux/sched.h>
33 #include <linux/string.h>
34 #include <linux/timer.h>
35 #include <linux/delay.h>
36 #include <linux/errno.h>
37 #include <linux/slab.h>
38 #include <linux/i2c.h>
39 #include <linux/workqueue.h>
41 #include <asm/semaphore.h>
43 #include <media/ir-common.h>
45 /* Mark Phalan <phalanm@o2.ie> */
46 static IR_KEYTAB_TYPE ir_codes_pv951[IR_KEYTAB_SIZE] = {
60 [ 31 ] = KEY_VOLUMEDOWN,
61 [ 27 ] = KEY_VOLUMEUP,
62 [ 26 ] = KEY_CHANNELUP,
63 [ 30 ] = KEY_CHANNELDOWN,
65 [ 29 ] = KEY_PAGEDOWN,
68 [ 24 ] = KEY_KPPLUSMINUS, /* CH +/- */
69 [ 22 ] = KEY_SUBTITLE, /* CC */
70 [ 13 ] = KEY_TEXT, /* TTX */
71 [ 11 ] = KEY_TV, /* AIR/CBL */
72 [ 17 ] = KEY_PC, /* PC/TV */
73 [ 23 ] = KEY_OK, /* CH RTN */
74 [ 25 ] = KEY_MODE, /* FUNC */
75 [ 12 ] = KEY_SEARCH, /* AUTOSCAN */
77 /* Not sure what to do with these ones! */
78 [ 15 ] = KEY_SELECT, /* SOURCE */
79 [ 10 ] = KEY_KPPLUS, /* +100 */
80 [ 20 ] = KEY_KPEQUAL, /* SYNC */
81 [ 28 ] = KEY_MEDIA, /* PC/TV */
84 static IR_KEYTAB_TYPE ir_codes_purpletv[IR_KEYTAB_SIZE] = {
87 [ 0x10 ] = KEY_BACKSPACE, /* Recall */
99 [ 0x12 ] = KEY_KPDOT, /* 100+ */
101 [ 0x7 ] = KEY_VOLUMEUP,
102 [ 0xb ] = KEY_VOLUMEDOWN,
103 [ 0x1a ] = KEY_KPPLUS,
104 [ 0x18 ] = KEY_KPMINUS,
107 [ 0xf ] = KEY_CHANNELUP,
108 [ 0x13 ] = KEY_CHANNELDOWN,
111 [ 0x1b ] = KEY_VIDEO, /* Video source */
112 [ 0x49 ] = KEY_LANGUAGE, /* MTS Select */
113 [ 0x19 ] = KEY_SEARCH, /* Auto Scan */
115 [ 0x4b ] = KEY_RECORD,
117 [ 0x45 ] = KEY_PAUSE, /* Pause */
119 [ 0x40 ] = KEY_FORWARD, /* Forward ? */
120 [ 0x42 ] = KEY_REWIND, /* Backward ? */
126 struct input_dev *input;
127 struct ir_input_state ir;
129 struct work_struct work;
130 struct timer_list timer;
132 int (*get_key)(struct IR*, u32*, u32*);
135 /* ----------------------------------------------------------------------- */
136 /* insmod parameters */
139 module_param(debug, int, 0644); /* debug level (0,1,2) */
141 #define DEVNAME "ir-kbd-i2c"
142 #define dprintk(level, fmt, arg...) if (debug >= level) \
143 printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
145 /* ----------------------------------------------------------------------- */
147 static int get_key_haup(struct IR *ir, u32 *ir_key, u32 *ir_raw)
149 unsigned char buf[3];
150 int start, toggle, dev, code;
153 if (3 != i2c_master_recv(&ir->c,buf,3))
156 /* split rc5 data block ... */
157 start = (buf[0] >> 6) & 3;
158 toggle = (buf[0] >> 5) & 1;
160 code = (buf[1] >> 2) & 0x3f;
165 dprintk(1,"ir hauppauge (rc5): s%d t%d dev=%d code=%d\n",
166 start, toggle, dev, code);
170 *ir_raw = (start << 12) | (toggle << 11) | (dev << 6) | code;
174 static int get_key_pixelview(struct IR *ir, u32 *ir_key, u32 *ir_raw)
179 if (1 != i2c_master_recv(&ir->c,&b,1)) {
180 dprintk(1,"read error\n");
188 static int get_key_pv951(struct IR *ir, u32 *ir_key, u32 *ir_raw)
193 if (1 != i2c_master_recv(&ir->c,&b,1)) {
194 dprintk(1,"read error\n");
201 dprintk(2,"key %02x\n", b);
208 static int get_key_knc1(struct IR *ir, u32 *ir_key, u32 *ir_raw)
213 if (1 != i2c_master_recv(&ir->c,&b,1)) {
214 dprintk(1,"read error\n");
218 /* it seems that 0xFE indicates that a button is still hold
219 down, while 0xFF indicates that no button is hold
220 down. 0xFE sequences are sometimes interrupted by 0xFF */
222 dprintk(2,"key %02x\n", b);
236 static int get_key_purpletv(struct IR *ir, u32 *ir_key, u32 *ir_raw)
241 if (1 != i2c_master_recv(&ir->c,&b,1)) {
242 dprintk(1,"read error\n");
246 /* no button press */
258 /* ----------------------------------------------------------------------- */
260 static void ir_key_poll(struct IR *ir)
262 static u32 ir_key, ir_raw;
265 dprintk(2,"ir_poll_key\n");
266 rc = ir->get_key(ir, &ir_key, &ir_raw);
268 dprintk(2,"error\n");
273 ir_input_nokey(ir->input, &ir->ir);
275 ir_input_keydown(ir->input, &ir->ir, ir_key, ir_raw);
279 static void ir_timer(unsigned long data)
281 struct IR *ir = (struct IR*)data;
282 schedule_work(&ir->work);
285 static void ir_work(void *data)
287 struct IR *ir = data;
289 mod_timer(&ir->timer, jiffies+HZ/10);
292 /* ----------------------------------------------------------------------- */
294 static int ir_attach(struct i2c_adapter *adap, int addr,
295 unsigned short flags, int kind);
296 static int ir_detach(struct i2c_client *client);
297 static int ir_probe(struct i2c_adapter *adap);
299 static struct i2c_driver driver = {
300 .name = "ir remote kbd driver",
301 .id = I2C_DRIVERID_EXP3, /* FIXME */
302 .flags = I2C_DF_NOTIFY,
303 .attach_adapter = ir_probe,
304 .detach_client = ir_detach,
307 static struct i2c_client client_template =
313 static int ir_attach(struct i2c_adapter *adap, int addr,
314 unsigned short flags, int kind)
316 IR_KEYTAB_TYPE *ir_codes = NULL;
320 struct input_dev *input_dev;
322 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
323 input_dev = input_allocate_device();
324 if (!ir || !input_dev) {
326 input_free_device(input_dev);
330 ir->c = client_template;
331 ir->input = input_dev;
333 i2c_set_clientdata(&ir->c, ir);
334 ir->c.adapter = adap;
340 ir->get_key = get_key_pixelview;
341 ir_type = IR_TYPE_OTHER;
342 ir_codes = ir_codes_empty;
346 ir->get_key = get_key_pv951;
347 ir_type = IR_TYPE_OTHER;
348 ir_codes = ir_codes_pv951;
353 ir->get_key = get_key_haup;
354 ir_type = IR_TYPE_RC5;
355 ir_codes = ir_codes_rc5_tv;
359 ir->get_key = get_key_knc1;
360 ir_type = IR_TYPE_OTHER;
361 ir_codes = ir_codes_empty;
365 ir->get_key = get_key_purpletv;
366 ir_type = IR_TYPE_OTHER;
367 ir_codes = ir_codes_purpletv;
370 /* shouldn't happen */
371 printk(DEVNAME ": Huh? unknown i2c address (0x%02x)?\n",addr);
376 /* register i2c device */
377 i2c_attach_client(&ir->c);
378 snprintf(ir->c.name, sizeof(ir->c.name), "i2c IR (%s)", name);
379 snprintf(ir->phys, sizeof(ir->phys), "%s/%s/ir0",
380 ir->c.adapter->dev.bus_id,
383 /* init + register input device */
384 ir_input_init(input_dev, &ir->ir, ir_type, ir_codes);
385 input_dev->id.bustype = BUS_I2C;
386 input_dev->name = ir->c.name;
387 input_dev->phys = ir->phys;
389 input_register_device(ir->input);
391 /* start polling via eventd */
392 INIT_WORK(&ir->work, ir_work, ir);
393 init_timer(&ir->timer);
394 ir->timer.function = ir_timer;
395 ir->timer.data = (unsigned long)ir;
396 schedule_work(&ir->work);
401 static int ir_detach(struct i2c_client *client)
403 struct IR *ir = i2c_get_clientdata(client);
405 /* kill outstanding polls */
406 del_timer(&ir->timer);
407 flush_scheduled_work();
409 /* unregister devices */
410 input_unregister_device(ir->input);
411 i2c_detach_client(&ir->c);
418 static int ir_probe(struct i2c_adapter *adap)
421 /* The external IR receiver is at i2c address 0x34 (0x35 for
422 reads). Future Hauppauge cards will have an internal
423 receiver at 0x30 (0x31 for reads). In theory, both can be
424 fitted, and Hauppauge suggest an external overrides an
427 That's why we probe 0x1a (~0x34) first. CB
430 static const int probe_bttv[] = { 0x1a, 0x18, 0x4b, 0x64, 0x30, -1};
431 static const int probe_saa7134[] = { 0x7a, -1 };
432 const int *probe = NULL;
433 struct i2c_client c; char buf; int i,rc;
440 probe = probe_saa7134;
446 memset(&c,0,sizeof(c));
448 for (i = 0; -1 != probe[i]; i++) {
450 rc = i2c_master_recv(&c,&buf,1);
451 dprintk(1,"probe 0x%02x @ %s: %s\n",
452 probe[i], adap->name,
453 (1 == rc) ? "yes" : "no");
455 ir_attach(adap,probe[i],0,0);
462 /* ----------------------------------------------------------------------- */
464 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
465 MODULE_DESCRIPTION("input driver for i2c IR remote controls");
466 MODULE_LICENSE("GPL");
468 static int __init ir_init(void)
470 return i2c_add_driver(&driver);
473 static void __exit ir_fini(void)
475 i2c_del_driver(&driver);
478 module_init(ir_init);
479 module_exit(ir_fini);
482 * Overrides for Emacs so that we follow Linus's tabbing style.
483 * ---------------------------------------------------------------------------