V4L/DVB (13533): ir: use dynamic tables, instead of static ones
[safe/jmp/linux-2.6] / drivers / media / common / ir-functions.c
1 /*
2  *
3  * some common structs and functions to handle infrared remotes via
4  * input layer ...
5  *
6  * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <linux/module.h>
24 #include <linux/string.h>
25 #include <linux/jiffies.h>
26 #include <media/ir-common.h>
27
28 /* -------------------------------------------------------------------------- */
29
30 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
31 MODULE_LICENSE("GPL");
32
33 static int repeat = 1;
34 module_param(repeat, int, 0444);
35 MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
36
37 int media_ir_debug;    /* media_ir_debug level (0,1,2) */
38 module_param_named(debug, media_ir_debug, int, 0644);
39
40 /* -------------------------------------------------------------------------- */
41
42 static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
43 {
44         if (KEY_RESERVED == ir->keycode) {
45                 printk(KERN_INFO "%s: unknown key: key=0x%02x down=%d\n",
46                        dev->name, ir->ir_key, ir->keypressed);
47                 return;
48         }
49         IR_dprintk(1,"%s: key event code=%d down=%d\n",
50                 dev->name,ir->keycode,ir->keypressed);
51         input_report_key(dev,ir->keycode,ir->keypressed);
52         input_sync(dev);
53 }
54
55 /* -------------------------------------------------------------------------- */
56
57 void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
58                    int ir_type, struct ir_scancode_table *ir_codes)
59 {
60         ir->ir_type = ir_type;
61
62         ir_set_keycode_table(dev, ir_codes);
63
64         clear_bit(0, dev->keybit);
65
66         set_bit(EV_KEY, dev->evbit);
67         if (repeat)
68                 set_bit(EV_REP, dev->evbit);
69 }
70 EXPORT_SYMBOL_GPL(ir_input_init);
71
72 void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
73 {
74         if (ir->keypressed) {
75                 ir->keypressed = 0;
76                 ir_input_key_event(dev,ir);
77         }
78 }
79 EXPORT_SYMBOL_GPL(ir_input_nokey);
80
81 void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
82                       u32 ir_key)
83 {
84         u32 keycode = ir_g_keycode_from_table(dev, ir_key);
85
86         if (ir->keypressed && ir->keycode != keycode) {
87                 ir->keypressed = 0;
88                 ir_input_key_event(dev,ir);
89         }
90         if (!ir->keypressed) {
91                 ir->ir_key  = ir_key;
92                 ir->keycode = keycode;
93                 ir->keypressed = 1;
94                 ir_input_key_event(dev,ir);
95         }
96 }
97 EXPORT_SYMBOL_GPL(ir_input_keydown);
98
99 /* -------------------------------------------------------------------------- */
100 /* extract mask bits out of data and pack them into the result */
101 u32 ir_extract_bits(u32 data, u32 mask)
102 {
103         u32 vbit = 1, value = 0;
104
105         do {
106             if (mask&1) {
107                 if (data&1)
108                         value |= vbit;
109                 vbit<<=1;
110             }
111             data>>=1;
112         } while (mask>>=1);
113
114         return value;
115 }
116 EXPORT_SYMBOL_GPL(ir_extract_bits);
117
118 static int inline getbit(u32 *samples, int bit)
119 {
120         return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
121 }
122
123 /* sump raw samples for visual debugging ;) */
124 int ir_dump_samples(u32 *samples, int count)
125 {
126         int i, bit, start;
127
128         printk(KERN_DEBUG "ir samples: ");
129         start = 0;
130         for (i = 0; i < count * 32; i++) {
131                 bit = getbit(samples,i);
132                 if (bit)
133                         start = 1;
134                 if (0 == start)
135                         continue;
136                 printk("%s", bit ? "#" : "_");
137         }
138         printk("\n");
139         return 0;
140 }
141 EXPORT_SYMBOL_GPL(ir_dump_samples);
142
143 /* decode raw samples, pulse distance coding used by NEC remotes */
144 int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
145 {
146         int i,last,bit,len;
147         u32 curBit;
148         u32 value;
149
150         /* find start burst */
151         for (i = len = 0; i < count * 32; i++) {
152                 bit = getbit(samples,i);
153                 if (bit) {
154                         len++;
155                 } else {
156                         if (len >= 29)
157                                 break;
158                         len = 0;
159                 }
160         }
161
162         /* start burst to short */
163         if (len < 29)
164                 return 0xffffffff;
165
166         /* find start silence */
167         for (len = 0; i < count * 32; i++) {
168                 bit = getbit(samples,i);
169                 if (bit) {
170                         break;
171                 } else {
172                         len++;
173                 }
174         }
175
176         /* silence to short */
177         if (len < 7)
178                 return 0xffffffff;
179
180         /* go decoding */
181         len   = 0;
182         last = 1;
183         value = 0; curBit = 1;
184         for (; i < count * 32; i++) {
185                 bit  = getbit(samples,i);
186                 if (last) {
187                         if(bit) {
188                                 continue;
189                         } else {
190                                 len = 1;
191                         }
192                 } else {
193                         if (bit) {
194                                 if (len > (low + high) /2)
195                                         value |= curBit;
196                                 curBit <<= 1;
197                                 if (curBit == 1)
198                                         break;
199                         } else {
200                                 len++;
201                         }
202                 }
203                 last = bit;
204         }
205
206         return value;
207 }
208 EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
209
210 /* decode raw samples, biphase coding, used by rc5 for example */
211 int ir_decode_biphase(u32 *samples, int count, int low, int high)
212 {
213         int i,last,bit,len,flips;
214         u32 value;
215
216         /* find start bit (1) */
217         for (i = 0; i < 32; i++) {
218                 bit = getbit(samples,i);
219                 if (bit)
220                         break;
221         }
222
223         /* go decoding */
224         len   = 0;
225         flips = 0;
226         value = 1;
227         for (; i < count * 32; i++) {
228                 if (len > high)
229                         break;
230                 if (flips > 1)
231                         break;
232                 last = bit;
233                 bit  = getbit(samples,i);
234                 if (last == bit) {
235                         len++;
236                         continue;
237                 }
238                 if (len < low) {
239                         len++;
240                         flips++;
241                         continue;
242                 }
243                 value <<= 1;
244                 value |= bit;
245                 flips = 0;
246                 len   = 1;
247         }
248         return value;
249 }
250 EXPORT_SYMBOL_GPL(ir_decode_biphase);
251
252 /* RC5 decoding stuff, moved from bttv-input.c to share it with
253  * saa7134 */
254
255 /* decode raw bit pattern to RC5 code */
256 u32 ir_rc5_decode(unsigned int code)
257 {
258         unsigned int org_code = code;
259         unsigned int pair;
260         unsigned int rc5 = 0;
261         int i;
262
263         for (i = 0; i < 14; ++i) {
264                 pair = code & 0x3;
265                 code >>= 2;
266
267                 rc5 <<= 1;
268                 switch (pair) {
269                 case 0:
270                 case 2:
271                         break;
272                 case 1:
273                         rc5 |= 1;
274                         break;
275                 case 3:
276                         IR_dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
277                         return 0;
278                 }
279         }
280         IR_dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
281                 "instr=%x\n", rc5, org_code, RC5_START(rc5),
282                 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
283         return rc5;
284 }
285 EXPORT_SYMBOL_GPL(ir_rc5_decode);
286
287 void ir_rc5_timer_end(unsigned long data)
288 {
289         struct card_ir *ir = (struct card_ir *)data;
290         struct timeval tv;
291         unsigned long current_jiffies, timeout;
292         u32 gap;
293         u32 rc5 = 0;
294
295         /* get time */
296         current_jiffies = jiffies;
297         do_gettimeofday(&tv);
298
299         /* avoid overflow with gap >1s */
300         if (tv.tv_sec - ir->base_time.tv_sec > 1) {
301                 gap = 200000;
302         } else {
303                 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
304                     tv.tv_usec - ir->base_time.tv_usec;
305         }
306
307         /* signal we're ready to start a new code */
308         ir->active = 0;
309
310         /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
311         if (gap < 28000) {
312                 IR_dprintk(1, "ir-common: spurious timer_end\n");
313                 return;
314         }
315
316         if (ir->last_bit < 20) {
317                 /* ignore spurious codes (caused by light/other remotes) */
318                 IR_dprintk(1, "ir-common: short code: %x\n", ir->code);
319         } else {
320                 ir->code = (ir->code << ir->shift_by) | 1;
321                 rc5 = ir_rc5_decode(ir->code);
322
323                 /* two start bits? */
324                 if (RC5_START(rc5) != ir->start) {
325                         IR_dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
326
327                         /* right address? */
328                 } else if (RC5_ADDR(rc5) == ir->addr) {
329                         u32 toggle = RC5_TOGGLE(rc5);
330                         u32 instr = RC5_INSTR(rc5);
331
332                         /* Good code, decide if repeat/repress */
333                         if (toggle != RC5_TOGGLE(ir->last_rc5) ||
334                             instr != RC5_INSTR(ir->last_rc5)) {
335                                 IR_dprintk(1, "ir-common: instruction %x, toggle %x\n", instr,
336                                         toggle);
337                                 ir_input_nokey(ir->dev, &ir->ir);
338                                 ir_input_keydown(ir->dev, &ir->ir, instr);
339                         }
340
341                         /* Set/reset key-up timer */
342                         timeout = current_jiffies +
343                                   msecs_to_jiffies(ir->rc5_key_timeout);
344                         mod_timer(&ir->timer_keyup, timeout);
345
346                         /* Save code for repeat test */
347                         ir->last_rc5 = rc5;
348                 }
349         }
350 }
351 EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
352
353 void ir_rc5_timer_keyup(unsigned long data)
354 {
355         struct card_ir *ir = (struct card_ir *)data;
356
357         IR_dprintk(1, "ir-common: key released\n");
358         ir_input_nokey(ir->dev, &ir->ir);
359 }
360 EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);