1 #include <linux/types.h>
2 #include <linux/init.h>
3 #include <linux/module.h>
4 #include <linux/moduleparam.h>
5 #include <linux/input.h>
6 #include <linux/proc_fs.h>
7 #include <asm/bitops.h>
10 #include "av7110_hw.h"
12 #define UP_TIMEOUT (HZ*7/25)
14 /* enable ir debugging by or'ing debug with 16 */
17 static struct av7110 *av_list[4];
18 static struct input_dev *input_dev;
20 static u8 delay_timer_finished;
22 static u16 key_map [256] = {
23 KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7,
24 KEY_8, KEY_9, KEY_BACK, 0, KEY_POWER, KEY_MUTE, 0, KEY_INFO,
25 KEY_VOLUMEUP, KEY_VOLUMEDOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
26 KEY_CHANNELUP, KEY_CHANNELDOWN, 0, 0, 0, 0, 0, 0,
27 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28 0, 0, 0, 0, KEY_TEXT, 0, 0, KEY_TV, 0, 0, 0, 0, 0, KEY_SETUP, 0, 0,
29 0, 0, 0, KEY_SUBTITLE, 0, 0, KEY_LANGUAGE, 0,
30 KEY_RADIO, 0, 0, 0, 0, KEY_EXIT, 0, 0,
31 KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_OK, 0, 0, 0,
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RED, KEY_GREEN, KEY_YELLOW,
33 KEY_BLUE, 0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_LIST, 0, 0, 0, 0, 0, 0,
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
38 0, 0, 0, 0, KEY_UP, KEY_UP, KEY_DOWN, KEY_DOWN,
39 0, 0, 0, 0, KEY_EPG, 0, 0, 0,
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_VCR
46 static void av7110_emit_keyup(unsigned long data)
48 if (!data || !test_bit(data, input_dev->key))
51 input_report_key(input_dev, data, 0);
52 input_sync(input_dev);
56 static struct timer_list keyup_timer = { .function = av7110_emit_keyup };
59 static void av7110_emit_key(unsigned long parm)
61 struct av7110 *av7110 = (struct av7110 *) parm;
62 u32 ir_config = av7110->ir_config;
63 u32 ircom = av7110->ir_command;
66 static u16 old_toggle = 0;
70 /* extract device address and data */
71 switch (ir_config & 0x0003) {
72 case 0: /* RC5: 5 bits device address, 6 bits data */
74 addr = (ircom >> 6) & 0x1f;
77 case 1: /* RCMM: 8(?) bits device address, 8(?) bits data */
79 addr = (ircom >> 8) & 0xff;
82 case 2: /* extended RC5: 5 bits device address, 7 bits data */
84 addr = (ircom >> 6) & 0x1f;
85 /* invert 7th data bit for backward compatibility with RC5 keymaps */
86 if (!(ircom & 0x1000))
91 printk("invalid ir_config %x\n", ir_config);
95 keycode = key_map[data];
97 dprintk(16, "code %08x -> addr %i data 0x%02x -> keycode %i\n",
98 ircom, addr, data, keycode);
100 /* check device address (if selected) */
101 if (ir_config & 0x4000)
102 if (addr != ((ir_config >> 16) & 0xff))
106 printk ("%s: unknown key 0x%02x!!\n", __FUNCTION__, data);
110 if ((ir_config & 0x0003) == 1)
111 new_toggle = 0; /* RCMM */
113 new_toggle = (ircom & 0x800); /* RC5, extended RC5 */
115 if (timer_pending(&keyup_timer)) {
116 del_timer(&keyup_timer);
117 if (keyup_timer.data != keycode || new_toggle != old_toggle) {
118 delay_timer_finished = 0;
119 input_event(input_dev, EV_KEY, keyup_timer.data, 0);
120 input_event(input_dev, EV_KEY, keycode, 1);
121 input_sync(input_dev);
122 } else if (delay_timer_finished) {
123 input_event(input_dev, EV_KEY, keycode, 2);
124 input_sync(input_dev);
127 delay_timer_finished = 0;
128 input_event(input_dev, EV_KEY, keycode, 1);
129 input_sync(input_dev);
132 keyup_timer.expires = jiffies + UP_TIMEOUT;
133 keyup_timer.data = keycode;
135 add_timer(&keyup_timer);
137 old_toggle = new_toggle;
140 static void input_register_keys(void)
144 memset(input_dev->keybit, 0, sizeof(input_dev->keybit));
146 for (i = 0; i < ARRAY_SIZE(key_map); i++) {
147 if (key_map[i] > KEY_MAX)
149 else if (key_map[i] > KEY_RESERVED)
150 set_bit(key_map[i], input_dev->keybit);
155 static void input_repeat_key(unsigned long data)
157 /* called by the input driver after rep[REP_DELAY] ms */
158 delay_timer_finished = 1;
162 static int av7110_setup_irc_config(struct av7110 *av7110, u32 ir_config)
166 dprintk(4, "%p\n", av7110);
168 ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetIR, 1, ir_config);
169 av7110->ir_config = ir_config;
175 static int av7110_ir_write_proc(struct file *file, const char __user *buffer,
176 unsigned long count, void *data)
179 int size = 4 + 256 * sizeof(u16);
186 page = (char *) vmalloc(size);
190 if (copy_from_user(page, buffer, size)) {
195 memcpy(&ir_config, page, 4);
196 memcpy(&key_map, page + 4, 256 * sizeof(u16));
198 if (FW_VERSION(av_list[0]->arm_app) >= 0x2620 && !(ir_config & 0x0001))
199 ir_config |= 0x0002; /* enable extended RC5 */
200 for (i = 0; i < av_cnt; i++)
201 av7110_setup_irc_config(av_list[i], ir_config);
202 input_register_keys();
207 static void ir_handler(struct av7110 *av7110, u32 ircom)
209 dprintk(4, "ircommand = %08x\n", ircom);
210 av7110->ir_command = ircom;
211 tasklet_schedule(&av7110->ir_tasklet);
215 int __devinit av7110_ir_init(struct av7110 *av7110)
217 static struct proc_dir_entry *e;
220 if (av_cnt >= sizeof av_list/sizeof av_list[0])
223 av7110_setup_irc_config(av7110, 0x0001);
224 av_list[av_cnt++] = av7110;
227 init_timer(&keyup_timer);
228 keyup_timer.data = 0;
230 input_dev = input_allocate_device();
234 input_dev->name = "DVB on-card IR receiver";
236 set_bit(EV_KEY, input_dev->evbit);
237 set_bit(EV_REP, input_dev->evbit);
238 input_register_keys();
239 err = input_register_device(input_dev);
241 input_free_device(input_dev);
244 input_dev->timer.function = input_repeat_key;
246 e = create_proc_entry("av7110_ir", S_IFREG | S_IRUGO | S_IWUSR, NULL);
248 e->write_proc = av7110_ir_write_proc;
249 e->size = 4 + 256 * sizeof(u16);
253 tasklet_init(&av7110->ir_tasklet, av7110_emit_key, (unsigned long) av7110);
254 av7110->ir_handler = ir_handler;
260 void __devexit av7110_ir_exit(struct av7110 *av7110)
267 av7110->ir_handler = NULL;
268 tasklet_kill(&av7110->ir_tasklet);
269 for (i = 0; i < av_cnt; i++)
270 if (av_list[i] == av7110) {
271 av_list[i] = av_list[av_cnt-1];
272 av_list[av_cnt-1] = NULL;
277 del_timer_sync(&keyup_timer);
278 remove_proc_entry("av7110_ir", NULL);
279 input_unregister_device(input_dev);
285 //MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
286 //MODULE_LICENSE("GPL");