Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-arm
[pandora-kernel.git] / 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/moduleparam.h>
25 #include <linux/string.h>
26 #include <linux/jiffies.h>
27 #include <media/ir-common.h>
28
29 /* -------------------------------------------------------------------------- */
30
31 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
32 MODULE_LICENSE("GPL");
33
34 static int repeat = 1;
35 module_param(repeat, int, 0444);
36 MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
37
38 static int debug = 0;    /* debug level (0,1,2) */
39 module_param(debug, int, 0644);
40
41 #define dprintk(level, fmt, arg...)     if (debug >= level) \
42         printk(KERN_DEBUG fmt , ## arg)
43
44 /* -------------------------------------------------------------------------- */
45
46 static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
47 {
48         if (KEY_RESERVED == ir->keycode) {
49                 printk(KERN_INFO "%s: unknown key: key=0x%02x raw=0x%02x down=%d\n",
50                        dev->name,ir->ir_key,ir->ir_raw,ir->keypressed);
51                 return;
52         }
53         dprintk(1,"%s: key event code=%d down=%d\n",
54                 dev->name,ir->keycode,ir->keypressed);
55         input_report_key(dev,ir->keycode,ir->keypressed);
56         input_sync(dev);
57 }
58
59 /* -------------------------------------------------------------------------- */
60
61 void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
62                    int ir_type, IR_KEYTAB_TYPE *ir_codes)
63 {
64         int i;
65
66         ir->ir_type = ir_type;
67         if (ir_codes)
68                 memcpy(ir->ir_codes, ir_codes, sizeof(ir->ir_codes));
69
70
71         dev->keycode     = ir->ir_codes;
72         dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
73         dev->keycodemax  = IR_KEYTAB_SIZE;
74         for (i = 0; i < IR_KEYTAB_SIZE; i++)
75                 set_bit(ir->ir_codes[i], dev->keybit);
76         clear_bit(0, dev->keybit);
77
78         set_bit(EV_KEY, dev->evbit);
79         if (repeat)
80                 set_bit(EV_REP, dev->evbit);
81 }
82
83 void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
84 {
85         if (ir->keypressed) {
86                 ir->keypressed = 0;
87                 ir_input_key_event(dev,ir);
88         }
89 }
90
91 void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
92                       u32 ir_key, u32 ir_raw)
93 {
94         u32 keycode = IR_KEYCODE(ir->ir_codes, ir_key);
95
96         if (ir->keypressed && ir->keycode != keycode) {
97                 ir->keypressed = 0;
98                 ir_input_key_event(dev,ir);
99         }
100         if (!ir->keypressed) {
101                 ir->ir_key  = ir_key;
102                 ir->ir_raw  = ir_raw;
103                 ir->keycode = keycode;
104                 ir->keypressed = 1;
105                 ir_input_key_event(dev,ir);
106         }
107 }
108
109 /* -------------------------------------------------------------------------- */
110 /* extract mask bits out of data and pack them into the result */
111 u32 ir_extract_bits(u32 data, u32 mask)
112 {
113         u32 vbit = 1, value = 0;
114
115         do {
116             if (mask&1) {
117                 if (data&1)
118                         value |= vbit;
119                 vbit<<=1;
120             }
121             data>>=1;
122         } while (mask>>=1);
123
124         return value;
125 }
126
127 static int inline getbit(u32 *samples, int bit)
128 {
129         return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
130 }
131
132 /* sump raw samples for visual debugging ;) */
133 int ir_dump_samples(u32 *samples, int count)
134 {
135         int i, bit, start;
136
137         printk(KERN_DEBUG "ir samples: ");
138         start = 0;
139         for (i = 0; i < count * 32; i++) {
140                 bit = getbit(samples,i);
141                 if (bit)
142                         start = 1;
143                 if (0 == start)
144                         continue;
145                 printk("%s", bit ? "#" : "_");
146         }
147         printk("\n");
148         return 0;
149 }
150
151 /* decode raw samples, pulse distance coding used by NEC remotes */
152 int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
153 {
154         int i,last,bit,len;
155         u32 curBit;
156         u32 value;
157
158         /* find start burst */
159         for (i = len = 0; i < count * 32; i++) {
160                 bit = getbit(samples,i);
161                 if (bit) {
162                         len++;
163                 } else {
164                         if (len >= 29)
165                                 break;
166                         len = 0;
167                 }
168         }
169
170         /* start burst to short */
171         if (len < 29)
172                 return 0xffffffff;
173
174         /* find start silence */
175         for (len = 0; i < count * 32; i++) {
176                 bit = getbit(samples,i);
177                 if (bit) {
178                         break;
179                 } else {
180                         len++;
181                 }
182         }
183
184         /* silence to short */
185         if (len < 7)
186                 return 0xffffffff;
187
188         /* go decoding */
189         len   = 0;
190         last = 1;
191         value = 0; curBit = 1;
192         for (; i < count * 32; i++) {
193                 bit  = getbit(samples,i);
194                 if (last) {
195                         if(bit) {
196                                 continue;
197                         } else {
198                                 len = 1;
199                         }
200                 } else {
201                         if (bit) {
202                                 if (len > (low + high) /2)
203                                         value |= curBit;
204                                 curBit <<= 1;
205                                 if (curBit == 1)
206                                         break;
207                         } else {
208                                 len++;
209                         }
210                 }
211                 last = bit;
212         }
213
214         return value;
215 }
216
217 /* decode raw samples, biphase coding, used by rc5 for example */
218 int ir_decode_biphase(u32 *samples, int count, int low, int high)
219 {
220         int i,last,bit,len,flips;
221         u32 value;
222
223         /* find start bit (1) */
224         for (i = 0; i < 32; i++) {
225                 bit = getbit(samples,i);
226                 if (bit)
227                         break;
228         }
229
230         /* go decoding */
231         len   = 0;
232         flips = 0;
233         value = 1;
234         for (; i < count * 32; i++) {
235                 if (len > high)
236                         break;
237                 if (flips > 1)
238                         break;
239                 last = bit;
240                 bit  = getbit(samples,i);
241                 if (last == bit) {
242                         len++;
243                         continue;
244                 }
245                 if (len < low) {
246                         len++;
247                         flips++;
248                         continue;
249                 }
250                 value <<= 1;
251                 value |= bit;
252                 flips = 0;
253                 len   = 1;
254         }
255         return value;
256 }
257
258 /* RC5 decoding stuff, moved from bttv-input.c to share it with
259  * saa7134 */
260
261 /* decode raw bit pattern to RC5 code */
262 u32 ir_rc5_decode(unsigned int code)
263 {
264         unsigned int org_code = code;
265         unsigned int pair;
266         unsigned int rc5 = 0;
267         int i;
268
269         for (i = 0; i < 14; ++i) {
270                 pair = code & 0x3;
271                 code >>= 2;
272
273                 rc5 <<= 1;
274                 switch (pair) {
275                 case 0:
276                 case 2:
277                         break;
278                 case 1:
279                         rc5 |= 1;
280                         break;
281                 case 3:
282                         dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
283                         return 0;
284                 }
285         }
286         dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
287                 "instr=%x\n", rc5, org_code, RC5_START(rc5),
288                 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
289         return rc5;
290 }
291
292 void ir_rc5_timer_end(unsigned long data)
293 {
294         struct card_ir *ir = (struct card_ir *)data;
295         struct timeval tv;
296         unsigned long current_jiffies, timeout;
297         u32 gap;
298         u32 rc5 = 0;
299
300         /* get time */
301         current_jiffies = jiffies;
302         do_gettimeofday(&tv);
303
304         /* avoid overflow with gap >1s */
305         if (tv.tv_sec - ir->base_time.tv_sec > 1) {
306                 gap = 200000;
307         } else {
308                 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
309                     tv.tv_usec - ir->base_time.tv_usec;
310         }
311
312         /* signal we're ready to start a new code */
313         ir->active = 0;
314
315         /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
316         if (gap < 28000) {
317                 dprintk(1, "ir-common: spurious timer_end\n");
318                 return;
319         }
320
321         if (ir->last_bit < 20) {
322                 /* ignore spurious codes (caused by light/other remotes) */
323                 dprintk(1, "ir-common: short code: %x\n", ir->code);
324         } else {
325                 ir->code = (ir->code << ir->shift_by) | 1;
326                 rc5 = ir_rc5_decode(ir->code);
327
328                 /* two start bits? */
329                 if (RC5_START(rc5) != ir->start) {
330                         dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
331
332                         /* right address? */
333                 } else if (RC5_ADDR(rc5) == ir->addr) {
334                         u32 toggle = RC5_TOGGLE(rc5);
335                         u32 instr = RC5_INSTR(rc5);
336
337                         /* Good code, decide if repeat/repress */
338                         if (toggle != RC5_TOGGLE(ir->last_rc5) ||
339                             instr != RC5_INSTR(ir->last_rc5)) {
340                                 dprintk(1, "ir-common: instruction %x, toggle %x\n", instr,
341                                         toggle);
342                                 ir_input_nokey(ir->dev, &ir->ir);
343                                 ir_input_keydown(ir->dev, &ir->ir, instr,
344                                                  instr);
345                         }
346
347                         /* Set/reset key-up timer */
348                         timeout = current_jiffies +
349                                   msecs_to_jiffies(ir->rc5_key_timeout);
350                         mod_timer(&ir->timer_keyup, timeout);
351
352                         /* Save code for repeat test */
353                         ir->last_rc5 = rc5;
354                 }
355         }
356 }
357
358 void ir_rc5_timer_keyup(unsigned long data)
359 {
360         struct card_ir *ir = (struct card_ir *)data;
361
362         dprintk(1, "ir-common: key released\n");
363         ir_input_nokey(ir->dev, &ir->ir);
364 }
365
366 EXPORT_SYMBOL_GPL(ir_input_init);
367 EXPORT_SYMBOL_GPL(ir_input_nokey);
368 EXPORT_SYMBOL_GPL(ir_input_keydown);
369
370 EXPORT_SYMBOL_GPL(ir_extract_bits);
371 EXPORT_SYMBOL_GPL(ir_dump_samples);
372 EXPORT_SYMBOL_GPL(ir_decode_biphase);
373 EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
374
375 EXPORT_SYMBOL_GPL(ir_rc5_decode);
376 EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
377 EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);
378
379 /*
380  * Local variables:
381  * c-basic-offset: 8
382  * End:
383  */