2 * $Id: ir-kbd-i2c.c,v 1.10 2004/12/09 12:51:35 kraxel Exp $
4 * keyboard input driver for i2c IR remote controls
6 * Copyright (c) 2000-2003 Gerd Knorr <kraxel@bytesex.org>
7 * modified for PixelView (BT878P+W/FM) by
8 * Michal Kochanowicz <mkochano@pld.org.pl>
9 * Christoph Bartelmus <lirc@bartelmus.de>
10 * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by
11 * Ulrich Mueller <ulrich.mueller42@web.de>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/init.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/string.h>
35 #include <linux/timer.h>
36 #include <linux/delay.h>
37 #include <linux/errno.h>
38 #include <linux/slab.h>
39 #include <linux/i2c.h>
40 #include <linux/workqueue.h>
42 #include <asm/semaphore.h>
44 #include <media/ir-common.h>
46 /* Mark Phalan <phalanm@o2.ie> */
47 static IR_KEYTAB_TYPE ir_codes_pv951
[IR_KEYTAB_SIZE
] = {
61 [ 31 ] = KEY_VOLUMEDOWN
,
62 [ 27 ] = KEY_VOLUMEUP
,
63 [ 26 ] = KEY_CHANNELUP
,
64 [ 30 ] = KEY_CHANNELDOWN
,
66 [ 29 ] = KEY_PAGEDOWN
,
69 [ 24 ] = KEY_KPPLUSMINUS
, // CH +/-
70 [ 22 ] = KEY_SUBTITLE
, // CC
71 [ 13 ] = KEY_TEXT
, // TTX
72 [ 11 ] = KEY_TV
, // AIR/CBL
73 [ 17 ] = KEY_PC
, // PC/TV
74 [ 23 ] = KEY_OK
, // CH RTN
75 [ 25 ] = KEY_MODE
, // FUNC
76 [ 12 ] = KEY_SEARCH
, // AUTOSCAN
78 /* Not sure what to do with these ones! */
79 [ 15 ] = KEY_SELECT
, // SOURCE
80 [ 10 ] = KEY_KPPLUS
, // +100
81 [ 20 ] = KEY_KPEQUAL
, // SYNC
82 [ 28 ] = KEY_MEDIA
, // PC/TV
85 static IR_KEYTAB_TYPE ir_codes_purpletv
[IR_KEYTAB_SIZE
] = {
88 [ 0x10 ] = KEY_BACKSPACE
, // Recall
100 [ 0x12 ] = KEY_KPDOT
, // 100+
102 [ 0x7 ] = KEY_VOLUMEUP
,
103 [ 0xb ] = KEY_VOLUMEDOWN
,
104 [ 0x1a ] = KEY_KPPLUS
,
105 [ 0x18 ] = KEY_KPMINUS
,
108 [ 0xf ] = KEY_CHANNELUP
,
109 [ 0x13 ] = KEY_CHANNELDOWN
,
112 [ 0x1b ] = KEY_VIDEO
, // Video source
114 [ 0x1f ] = KEY_S
, // Snapshot
116 [ 0x49 ] = KEY_LANGUAGE
, // MTS Select
117 [ 0x19 ] = KEY_SEARCH
, // Auto Scan
119 [ 0x4b ] = KEY_RECORD
,
121 [ 0x45 ] = KEY_PAUSE
, // Pause
124 [ 0x43 ] = KEY_T
, // Time Shift
125 [ 0x47 ] = KEY_Y
, // Time Shift OFF
126 [ 0x4a ] = KEY_O
, // TOP
127 [ 0x17 ] = KEY_F
, // SURF CH
129 [ 0x40 ] = KEY_FORWARD
, // Forward ?
130 [ 0x42 ] = KEY_REWIND
, // Backward ?
137 struct input_dev input
;
138 struct ir_input_state ir
;
140 struct work_struct work
;
141 struct timer_list timer
;
143 int (*get_key
)(struct IR
*, u32
*, u32
*);
146 /* ----------------------------------------------------------------------- */
147 /* insmod parameters */
150 module_param(debug
, int, 0644); /* debug level (0,1,2) */
152 #define DEVNAME "ir-kbd-i2c"
153 #define dprintk(level, fmt, arg...) if (debug >= level) \
154 printk(KERN_DEBUG DEVNAME ": " fmt , ## arg)
156 /* ----------------------------------------------------------------------- */
158 static int get_key_haup(struct IR
*ir
, u32
*ir_key
, u32
*ir_raw
)
160 unsigned char buf
[3];
161 int start
, toggle
, dev
, code
;
164 if (3 != i2c_master_recv(&ir
->c
,buf
,3))
167 /* split rc5 data block ... */
168 start
= (buf
[0] >> 6) & 3;
169 toggle
= (buf
[0] >> 5) & 1;
171 code
= (buf
[1] >> 2) & 0x3f;
176 dprintk(1,"ir hauppauge (rc5): s%d t%d dev=%d code=%d\n",
177 start
, toggle
, dev
, code
);
181 *ir_raw
= (start
<< 12) | (toggle
<< 11) | (dev
<< 6) | code
;
185 static int get_key_pixelview(struct IR
*ir
, u32
*ir_key
, u32
*ir_raw
)
190 if (1 != i2c_master_recv(&ir
->c
,&b
,1)) {
191 dprintk(1,"read error\n");
199 static int get_key_pv951(struct IR
*ir
, u32
*ir_key
, u32
*ir_raw
)
204 if (1 != i2c_master_recv(&ir
->c
,&b
,1)) {
205 dprintk(1,"read error\n");
212 dprintk(2,"key %02x\n", b
);
219 static int get_key_knc1(struct IR
*ir
, u32
*ir_key
, u32
*ir_raw
)
224 if (1 != i2c_master_recv(&ir
->c
,&b
,1)) {
225 dprintk(1,"read error\n");
229 /* it seems that 0xFE indicates that a button is still hold
230 down, while 0xFF indicates that no button is hold
231 down. 0xFE sequences are sometimes interrupted by 0xFF */
233 dprintk(2,"key %02x\n", b
);
247 static int get_key_purpletv(struct IR
*ir
, u32
*ir_key
, u32
*ir_raw
)
252 if (1 != i2c_master_recv(&ir
->c
,&b
,1)) {
253 dprintk(1,"read error\n");
257 /* no button press */
269 /* ----------------------------------------------------------------------- */
271 static void ir_key_poll(struct IR
*ir
)
273 static u32 ir_key
, ir_raw
;
276 dprintk(2,"ir_poll_key\n");
277 rc
= ir
->get_key(ir
, &ir_key
, &ir_raw
);
279 dprintk(2,"error\n");
284 ir_input_nokey(&ir
->input
,&ir
->ir
);
286 ir_input_keydown(&ir
->input
,&ir
->ir
, ir_key
, ir_raw
);
290 static void ir_timer(unsigned long data
)
292 struct IR
*ir
= (struct IR
*)data
;
293 schedule_work(&ir
->work
);
296 static void ir_work(void *data
)
298 struct IR
*ir
= data
;
300 mod_timer(&ir
->timer
, jiffies
+HZ
/10);
303 /* ----------------------------------------------------------------------- */
305 static int ir_attach(struct i2c_adapter
*adap
, int addr
,
306 unsigned short flags
, int kind
);
307 static int ir_detach(struct i2c_client
*client
);
308 static int ir_probe(struct i2c_adapter
*adap
);
310 static struct i2c_driver driver
= {
311 .name
= "ir remote kbd driver",
312 .id
= I2C_DRIVERID_EXP3
, /* FIXME */
313 .flags
= I2C_DF_NOTIFY
,
314 .attach_adapter
= ir_probe
,
315 .detach_client
= ir_detach
,
318 static struct i2c_client client_template
=
320 I2C_DEVNAME("unset"),
324 static int ir_attach(struct i2c_adapter
*adap
, int addr
,
325 unsigned short flags
, int kind
)
327 IR_KEYTAB_TYPE
*ir_codes
= NULL
;
332 if (NULL
== (ir
= kmalloc(sizeof(struct IR
),GFP_KERNEL
)))
334 memset(ir
,0,sizeof(*ir
));
335 ir
->c
= client_template
;
337 i2c_set_clientdata(&ir
->c
, ir
);
338 ir
->c
.adapter
= adap
;
344 ir
->get_key
= get_key_pixelview
;
345 ir_type
= IR_TYPE_OTHER
;
346 ir_codes
= ir_codes_empty
;
350 ir
->get_key
= get_key_pv951
;
351 ir_type
= IR_TYPE_OTHER
;
352 ir_codes
= ir_codes_pv951
;
357 ir
->get_key
= get_key_haup
;
358 ir_type
= IR_TYPE_RC5
;
359 ir_codes
= ir_codes_rc5_tv
;
363 ir
->get_key
= get_key_knc1
;
364 ir_type
= IR_TYPE_OTHER
;
365 ir_codes
= ir_codes_empty
;
369 ir
->get_key
= get_key_purpletv
;
370 ir_type
= IR_TYPE_OTHER
;
371 ir_codes
= ir_codes_purpletv
;
374 /* shouldn't happen */
375 printk(DEVNAME
": Huh? unknown i2c address (0x%02x)?\n",addr
);
380 /* register i2c device */
381 i2c_attach_client(&ir
->c
);
382 snprintf(ir
->c
.name
, sizeof(ir
->c
.name
), "i2c IR (%s)", name
);
383 snprintf(ir
->phys
, sizeof(ir
->phys
), "%s/%s/ir0",
384 ir
->c
.adapter
->dev
.bus_id
,
387 /* init + register input device */
388 ir_input_init(&ir
->input
,&ir
->ir
,ir_type
,ir_codes
);
389 ir
->input
.id
.bustype
= BUS_I2C
;
390 ir
->input
.name
= ir
->c
.name
;
391 ir
->input
.phys
= ir
->phys
;
392 input_register_device(&ir
->input
);
393 printk(DEVNAME
": %s detected at %s [%s]\n",
394 ir
->input
.name
,ir
->input
.phys
,adap
->name
);
396 /* start polling via eventd */
397 INIT_WORK(&ir
->work
, ir_work
, ir
);
398 init_timer(&ir
->timer
);
399 ir
->timer
.function
= ir_timer
;
400 ir
->timer
.data
= (unsigned long)ir
;
401 schedule_work(&ir
->work
);
406 static int ir_detach(struct i2c_client
*client
)
408 struct IR
*ir
= i2c_get_clientdata(client
);
410 /* kill outstanding polls */
411 del_timer(&ir
->timer
);
412 flush_scheduled_work();
414 /* unregister devices */
415 input_unregister_device(&ir
->input
);
416 i2c_detach_client(&ir
->c
);
423 static int ir_probe(struct i2c_adapter
*adap
)
426 /* The external IR receiver is at i2c address 0x34 (0x35 for
427 reads). Future Hauppauge cards will have an internal
428 receiver at 0x30 (0x31 for reads). In theory, both can be
429 fitted, and Hauppauge suggest an external overrides an
432 That's why we probe 0x1a (~0x34) first. CB
435 static const int probe_bttv
[] = { 0x1a, 0x18, 0x4b, 0x64, 0x30, -1};
436 static const int probe_saa7134
[] = { 0x7a, -1 };
437 const int *probe
= NULL
;
438 struct i2c_client c
; char buf
; int i
,rc
;
441 case I2C_ALGO_BIT
| I2C_HW_B_BT848
:
444 case I2C_ALGO_SAA7134
:
445 probe
= probe_saa7134
;
451 memset(&c
,0,sizeof(c
));
453 for (i
= 0; -1 != probe
[i
]; i
++) {
455 rc
= i2c_master_recv(&c
,&buf
,1);
456 dprintk(1,"probe 0x%02x @ %s: %s\n",
457 probe
[i
], adap
->name
,
458 (1 == rc
) ? "yes" : "no");
460 ir_attach(adap
,probe
[i
],0,0);
467 /* ----------------------------------------------------------------------- */
469 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller");
470 MODULE_DESCRIPTION("input driver for i2c IR remote controls");
471 MODULE_LICENSE("GPL");
473 static int __init
ir_init(void)
475 return i2c_add_driver(&driver
);
478 static void __exit
ir_fini(void)
480 i2c_del_driver(&driver
);
483 module_init(ir_init
);
484 module_exit(ir_fini
);
487 * Overrides for Emacs so that we follow Linus's tabbing style.
488 * ---------------------------------------------------------------------------