Merge /spare/repo/linux-2.6/
[pandora-kernel.git] / drivers / media / dvb / ttpci / av7110_ir.c
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>
8
9 #include "av7110.h"
10 #include "av7110_hw.h"
11
12 #define UP_TIMEOUT (HZ*7/25)
13
14 /* enable ir debugging by or'ing debug with 16 */
15
16 static int av_cnt;
17 static struct av7110 *av_list[4];
18 static struct input_dev input_dev;
19
20 static u16 key_map [256] = {
21         KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7,
22         KEY_8, KEY_9, KEY_BACK, 0, KEY_POWER, KEY_MUTE, 0, KEY_INFO,
23         KEY_VOLUMEUP, KEY_VOLUMEDOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24         KEY_CHANNELUP, KEY_CHANNELDOWN, 0, 0, 0, 0, 0, 0,
25         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
26         0, 0, 0, 0, KEY_TEXT, 0, 0, KEY_TV, 0, 0, 0, 0, 0, KEY_SETUP, 0, 0,
27         0, 0, 0, KEY_SUBTITLE, 0, 0, KEY_LANGUAGE, 0,
28         KEY_RADIO, 0, 0, 0, 0, KEY_EXIT, 0, 0,
29         KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_OK, 0, 0, 0,
30         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RED, KEY_GREEN, KEY_YELLOW,
31         KEY_BLUE, 0, 0, 0, 0, 0, 0, 0, KEY_MENU, KEY_LIST, 0, 0, 0, 0, 0, 0,
32         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, KEY_UP, KEY_UP, KEY_DOWN, KEY_DOWN,
37         0, 0, 0, 0, KEY_EPG, 0, 0, 0,
38         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_VCR
41 };
42
43
44 static void av7110_emit_keyup(unsigned long data)
45 {
46         if (!data || !test_bit(data, input_dev.key))
47                 return;
48
49         input_event(&input_dev, EV_KEY, data, !!0);
50 }
51
52
53 static struct timer_list keyup_timer = { .function = av7110_emit_keyup };
54
55
56 static void av7110_emit_key(unsigned long parm)
57 {
58         struct av7110 *av7110 = (struct av7110 *) parm;
59         u32 ir_config = av7110->ir_config;
60         u32 ircom = av7110->ir_command;
61         u8 data;
62         u8 addr;
63         static u16 old_toggle = 0;
64         u16 new_toggle;
65         u16 keycode;
66
67         /* extract device address and data */
68         switch (ir_config & 0x0003) {
69         case 0: /* RC5: 5 bits device address, 6 bits data */
70                 data = ircom & 0x3f;
71                 addr = (ircom >> 6) & 0x1f;
72                 break;
73
74         case 1: /* RCMM: 8(?) bits device address, 8(?) bits data */
75                 data = ircom & 0xff;
76                 addr = (ircom >> 8) & 0xff;
77                 break;
78
79         case 2: /* extended RC5: 5 bits device address, 7 bits data */
80                 data = ircom & 0x3f;
81                 addr = (ircom >> 6) & 0x1f;
82                 /* invert 7th data bit for backward compatibility with RC5 keymaps */
83                 if (!(ircom & 0x1000))
84                         data |= 0x40;
85                 break;
86
87         default:
88                 printk("invalid ir_config %x\n", ir_config);
89                 return;
90         }
91
92         keycode = key_map[data];
93
94         dprintk(16, "code %08x -> addr %i data 0x%02x -> keycode %i\n",
95                 ircom, addr, data, keycode);
96
97         /* check device address (if selected) */
98         if (ir_config & 0x4000)
99                 if (addr != ((ir_config >> 16) & 0xff))
100                         return;
101
102         if (!keycode) {
103                 printk ("%s: unknown key 0x%02x!!\n", __FUNCTION__, data);
104                 return;
105         }
106
107         if ((ir_config & 0x0003) == 1)
108                 new_toggle = 0; /* RCMM */
109         else
110                 new_toggle = (ircom & 0x800); /* RC5, extended RC5 */
111
112         if (timer_pending(&keyup_timer)) {
113                 del_timer(&keyup_timer);
114                 if (keyup_timer.data != keycode || new_toggle != old_toggle) {
115                         input_event(&input_dev, EV_KEY, keyup_timer.data, !!0);
116                         input_event(&input_dev, EV_KEY, keycode, !0);
117                 } else
118                         input_event(&input_dev, EV_KEY, keycode, 2);
119
120         } else
121                 input_event(&input_dev, EV_KEY, keycode, !0);
122
123         keyup_timer.expires = jiffies + UP_TIMEOUT;
124         keyup_timer.data = keycode;
125
126         add_timer(&keyup_timer);
127
128         old_toggle = new_toggle;
129 }
130
131 static void input_register_keys(void)
132 {
133         int i;
134
135         memset(input_dev.keybit, 0, sizeof(input_dev.keybit));
136
137         for (i = 0; i < sizeof(key_map) / sizeof(key_map[0]); i++) {
138                 if (key_map[i] > KEY_MAX)
139                         key_map[i] = 0;
140                 else if (key_map[i] > KEY_RESERVED)
141                         set_bit(key_map[i], input_dev.keybit);
142         }
143 }
144
145
146 static void input_repeat_key(unsigned long data)
147 {
148        /* dummy routine to disable autorepeat in the input driver */
149 }
150
151
152 static int av7110_ir_write_proc(struct file *file, const char __user *buffer,
153                                 unsigned long count, void *data)
154 {
155         char *page;
156         int size = 4 + 256 * sizeof(u16);
157         u32 ir_config;
158         int i;
159
160         if (count < size)
161                 return -EINVAL;
162
163         page = (char *) vmalloc(size);
164         if (!page)
165                 return -ENOMEM;
166
167         if (copy_from_user(page, buffer, size)) {
168                 vfree(page);
169                 return -EFAULT;
170         }
171
172         memcpy(&ir_config, page, 4);
173         memcpy(&key_map, page + 4, 256 * sizeof(u16));
174         vfree(page);
175         if (FW_VERSION(av_list[0]->arm_app) >= 0x2620 && !(ir_config & 0x0001))
176                 ir_config |= 0x0002; /* enable extended RC5 */
177         for (i = 0; i < av_cnt; i++)
178                 av7110_setup_irc_config(av_list[i], ir_config);
179         input_register_keys();
180         return count;
181 }
182
183
184 int av7110_setup_irc_config(struct av7110 *av7110, u32 ir_config)
185 {
186         int ret = 0;
187
188         dprintk(4, "%p\n", av7110);
189         if (av7110) {
190                 ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, SetIR, 1, ir_config);
191                 av7110->ir_config = ir_config;
192         }
193         return ret;
194 }
195
196
197 static void ir_handler(struct av7110 *av7110, u32 ircom)
198 {
199         dprintk(4, "ircommand = %08x\n", ircom);
200         av7110->ir_command = ircom;
201         tasklet_schedule(&av7110->ir_tasklet);
202 }
203
204
205 int __init av7110_ir_init(struct av7110 *av7110)
206 {
207         static struct proc_dir_entry *e;
208
209         if (av_cnt >= sizeof av_list/sizeof av_list[0])
210                 return -ENOSPC;
211
212         av7110_setup_irc_config(av7110, 0x0001);
213         av_list[av_cnt++] = av7110;
214
215         if (av_cnt == 1) {
216                 init_timer(&keyup_timer);
217                 keyup_timer.data = 0;
218
219                 input_dev.name = "DVB on-card IR receiver";
220                 set_bit(EV_KEY, input_dev.evbit);
221                 set_bit(EV_REP, input_dev.evbit);
222                 input_register_keys();
223                 input_register_device(&input_dev);
224                 input_dev.timer.function = input_repeat_key;
225
226                 e = create_proc_entry("av7110_ir", S_IFREG | S_IRUGO | S_IWUSR, NULL);
227                 if (e) {
228                         e->write_proc = av7110_ir_write_proc;
229                         e->size = 4 + 256 * sizeof(u16);
230                 }
231         }
232
233         tasklet_init(&av7110->ir_tasklet, av7110_emit_key, (unsigned long) av7110);
234         av7110->ir_handler = ir_handler;
235
236         return 0;
237 }
238
239
240 void __exit av7110_ir_exit(struct av7110 *av7110)
241 {
242         int i;
243
244         if (av_cnt == 0)
245                 return;
246
247         av7110->ir_handler = NULL;
248         tasklet_kill(&av7110->ir_tasklet);
249         for (i = 0; i < av_cnt; i++)
250                 if (av_list[i] == av7110) {
251                         av_list[i] = av_list[av_cnt-1];
252                         av_list[av_cnt-1] = NULL;
253                         break;
254                 }
255
256         if (av_cnt == 1) {
257                 del_timer_sync(&keyup_timer);
258                 remove_proc_entry("av7110_ir", NULL);
259                 input_unregister_device(&input_dev);
260         }
261
262         av_cnt--;
263 }
264
265 //MODULE_AUTHOR("Holger Waechtler <holger@convergence.de>");
266 //MODULE_LICENSE("GPL");