[PATCH] I2C: remove devfs support from i2c-dev driver
[safe/jmp/linux-2.6] / drivers / media / common / ir-common.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 <media/ir-common.h>
26
27 /* -------------------------------------------------------------------------- */
28
29 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
30 MODULE_LICENSE("GPL");
31
32 static int repeat = 1;
33 module_param(repeat, int, 0444);
34 MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
35
36 static int debug = 0;    /* debug level (0,1,2) */
37 module_param(debug, int, 0644);
38
39 #define dprintk(level, fmt, arg...)     if (debug >= level) \
40         printk(KERN_DEBUG fmt , ## arg)
41
42 /* -------------------------------------------------------------------------- */
43
44 /* generic RC5 keytable                                          */
45 /* see http://users.pandora.be/nenya/electronics/rc5/codes00.htm */
46 /* used by old (black) Hauppauge remotes                         */
47 IR_KEYTAB_TYPE ir_codes_rc5_tv[IR_KEYTAB_SIZE] = {
48         /* Keys 0 to 9 */
49         [ 0x00 ] = KEY_KP0,
50         [ 0x01 ] = KEY_KP1,
51         [ 0x02 ] = KEY_KP2,
52         [ 0x03 ] = KEY_KP3,
53         [ 0x04 ] = KEY_KP4,
54         [ 0x05 ] = KEY_KP5,
55         [ 0x06 ] = KEY_KP6,
56         [ 0x07 ] = KEY_KP7,
57         [ 0x08 ] = KEY_KP8,
58         [ 0x09 ] = KEY_KP9,
59
60         [ 0x0b ] = KEY_CHANNEL,         /* channel / program (japan: 11) */
61         [ 0x0c ] = KEY_POWER,           /* standby */
62         [ 0x0d ] = KEY_MUTE,            /* mute / demute */
63         [ 0x0f ] = KEY_TV,              /* display */
64         [ 0x10 ] = KEY_VOLUMEUP,
65         [ 0x11 ] = KEY_VOLUMEDOWN,
66         [ 0x12 ] = KEY_BRIGHTNESSUP,
67         [ 0x13 ] = KEY_BRIGHTNESSDOWN,
68         [ 0x1e ] = KEY_SEARCH,          /* search + */
69         [ 0x20 ] = KEY_CHANNELUP,       /* channel / program + */
70         [ 0x21 ] = KEY_CHANNELDOWN,     /* channel / program - */
71         [ 0x22 ] = KEY_CHANNEL,         /* alt / channel */
72         [ 0x23 ] = KEY_LANGUAGE,        /* 1st / 2nd language */
73         [ 0x26 ] = KEY_SLEEP,           /* sleeptimer */
74         [ 0x2e ] = KEY_MENU,            /* 2nd controls (USA: menu) */
75         [ 0x30 ] = KEY_PAUSE,
76         [ 0x32 ] = KEY_REWIND,
77         [ 0x33 ] = KEY_GOTO,
78         [ 0x35 ] = KEY_PLAY,
79         [ 0x36 ] = KEY_STOP,
80         [ 0x37 ] = KEY_RECORD,          /* recording */
81         [ 0x3c ] = KEY_TEXT,            /* teletext submode (Japan: 12) */
82         [ 0x3d ] = KEY_SUSPEND,         /* system standby */
83
84 };
85 EXPORT_SYMBOL_GPL(ir_codes_rc5_tv);
86
87 /* Table for Leadtek Winfast Remote Controls - used by both bttv and cx88 */
88 IR_KEYTAB_TYPE ir_codes_winfast[IR_KEYTAB_SIZE] = {
89         /* Keys 0 to 9 */
90         [ 18 ] = KEY_KP0,
91         [  5 ] = KEY_KP1,
92         [  6 ] = KEY_KP2,
93         [  7 ] = KEY_KP3,
94         [  9 ] = KEY_KP4,
95         [ 10 ] = KEY_KP5,
96         [ 11 ] = KEY_KP6,
97         [ 13 ] = KEY_KP7,
98         [ 14 ] = KEY_KP8,
99         [ 15 ] = KEY_KP9,
100
101         [  0 ] = KEY_POWER,
102         [  2 ] = KEY_TUNER,             /* TV/FM */
103         [ 30 ] = KEY_VIDEO,
104         [  4 ] = KEY_VOLUMEUP,
105         [  8 ] = KEY_VOLUMEDOWN,
106         [ 12 ] = KEY_CHANNELUP,
107         [ 16 ] = KEY_CHANNELDOWN,
108         [  3 ] = KEY_ZOOM,              /* fullscreen */
109         [ 31 ] = KEY_SUBTITLE,          /* closed caption/teletext */
110         [ 32 ] = KEY_SLEEP,
111         [ 20 ] = KEY_MUTE,
112         [ 43 ] = KEY_RED,
113         [ 44 ] = KEY_GREEN,
114         [ 45 ] = KEY_YELLOW,
115         [ 46 ] = KEY_BLUE,
116         [ 24 ] = KEY_KPPLUS,            /* fine tune + */
117         [ 25 ] = KEY_KPMINUS,           /* fine tune - */
118         [ 33 ] = KEY_KPDOT,
119         [ 19 ] = KEY_KPENTER,
120         [ 34 ] = KEY_BACK,
121         [ 35 ] = KEY_PLAYPAUSE,
122         [ 36 ] = KEY_NEXT,
123         [ 38 ] = KEY_STOP,
124         [ 39 ] = KEY_RECORD
125 };
126 EXPORT_SYMBOL_GPL(ir_codes_winfast);
127
128 /* empty keytable, can be used as placeholder for not-yet created keytables */
129 IR_KEYTAB_TYPE ir_codes_empty[IR_KEYTAB_SIZE] = {
130         [ 42 ] = KEY_COFFEE,
131 };
132 EXPORT_SYMBOL_GPL(ir_codes_empty);
133
134 /* Hauppauge: the newer, gray remotes (seems there are multiple
135  * slightly different versions), shipped with cx88+ivtv cards.
136  * almost rc5 coding, but some non-standard keys */
137 IR_KEYTAB_TYPE ir_codes_hauppauge_new[IR_KEYTAB_SIZE] = {
138         /* Keys 0 to 9 */
139         [ 0x00 ] = KEY_KP0,
140         [ 0x01 ] = KEY_KP1,
141         [ 0x02 ] = KEY_KP2,
142         [ 0x03 ] = KEY_KP3,
143         [ 0x04 ] = KEY_KP4,
144         [ 0x05 ] = KEY_KP5,
145         [ 0x06 ] = KEY_KP6,
146         [ 0x07 ] = KEY_KP7,
147         [ 0x08 ] = KEY_KP8,
148         [ 0x09 ] = KEY_KP9,
149
150         [ 0x0a ] = KEY_TEXT,            /* keypad asterisk as well */
151         [ 0x0b ] = KEY_RED,             /* red button */
152         [ 0x0c ] = KEY_RADIO,
153         [ 0x0d ] = KEY_MENU,
154         [ 0x0e ] = KEY_SUBTITLE,        /* also the # key */
155         [ 0x0f ] = KEY_MUTE,
156         [ 0x10 ] = KEY_VOLUMEUP,
157         [ 0x11 ] = KEY_VOLUMEDOWN,
158         [ 0x12 ] = KEY_PREVIOUS,        /* previous channel */
159         [ 0x14 ] = KEY_UP,
160         [ 0x15 ] = KEY_DOWN,
161         [ 0x16 ] = KEY_LEFT,
162         [ 0x17 ] = KEY_RIGHT,
163         [ 0x18 ] = KEY_VIDEO,           /* Videos */
164         [ 0x19 ] = KEY_AUDIO,           /* Music */
165         /* 0x1a: Pictures - presume this means
166            "Multimedia Home Platform" -
167            no "PICTURES" key in input.h
168          */
169         [ 0x1a ] = KEY_MHP,
170
171         [ 0x1b ] = KEY_EPG,             /* Guide */
172         [ 0x1c ] = KEY_TV,
173         [ 0x1e ] = KEY_NEXTSONG,        /* skip >| */
174         [ 0x1f ] = KEY_EXIT,            /* back/exit */
175         [ 0x20 ] = KEY_CHANNELUP,       /* channel / program + */
176         [ 0x21 ] = KEY_CHANNELDOWN,     /* channel / program - */
177         [ 0x22 ] = KEY_CHANNEL,         /* source (old black remote) */
178         [ 0x24 ] = KEY_PREVIOUSSONG,    /* replay |< */
179         [ 0x25 ] = KEY_ENTER,           /* OK */
180         [ 0x26 ] = KEY_SLEEP,           /* minimize (old black remote) */
181         [ 0x29 ] = KEY_BLUE,            /* blue key */
182         [ 0x2e ] = KEY_GREEN,           /* green button */
183         [ 0x30 ] = KEY_PAUSE,           /* pause */
184         [ 0x32 ] = KEY_REWIND,          /* backward << */
185         [ 0x34 ] = KEY_FASTFORWARD,     /* forward >> */
186         [ 0x35 ] = KEY_PLAY,
187         [ 0x36 ] = KEY_STOP,
188         [ 0x37 ] = KEY_RECORD,          /* recording */
189         [ 0x38 ] = KEY_YELLOW,          /* yellow key */
190         [ 0x3b ] = KEY_SELECT,          /* top right button */
191         [ 0x3c ] = KEY_ZOOM,            /* full */
192         [ 0x3d ] = KEY_POWER,           /* system power (green button) */
193 };
194 EXPORT_SYMBOL(ir_codes_hauppauge_new);
195
196 IR_KEYTAB_TYPE ir_codes_pixelview[IR_KEYTAB_SIZE] = {
197         [  2 ] = KEY_KP0,
198         [  1 ] = KEY_KP1,
199         [ 11 ] = KEY_KP2,
200         [ 27 ] = KEY_KP3,
201         [  5 ] = KEY_KP4,
202         [  9 ] = KEY_KP5,
203         [ 21 ] = KEY_KP6,
204         [  6 ] = KEY_KP7,
205         [ 10 ] = KEY_KP8,
206         [ 18 ] = KEY_KP9,
207
208         [  3 ] = KEY_TUNER,             /* TV/FM */
209         [  7 ] = KEY_SEARCH,            /* scan */
210         [ 28 ] = KEY_ZOOM,              /* full screen */
211         [ 30 ] = KEY_POWER,
212         [ 23 ] = KEY_VOLUMEDOWN,
213         [ 31 ] = KEY_VOLUMEUP,
214         [ 20 ] = KEY_CHANNELDOWN,
215         [ 22 ] = KEY_CHANNELUP,
216         [ 24 ] = KEY_MUTE,
217
218         [  0 ] = KEY_LIST,              /* source */
219         [ 19 ] = KEY_INFO,              /* loop */
220         [ 16 ] = KEY_LAST,              /* +100 */
221         [ 13 ] = KEY_CLEAR,             /* reset */
222         [ 12 ] = BTN_RIGHT,             /* fun++ */
223         [  4 ] = BTN_LEFT,              /* fun-- */
224         [ 14 ] = KEY_GOTO,              /* function */
225         [ 15 ] = KEY_STOP,              /* freeze */
226 };
227 EXPORT_SYMBOL(ir_codes_pixelview);
228
229 /* -------------------------------------------------------------------------- */
230
231 static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
232 {
233         if (KEY_RESERVED == ir->keycode) {
234                 printk(KERN_INFO "%s: unknown key: key=0x%02x raw=0x%02x down=%d\n",
235                        dev->name,ir->ir_key,ir->ir_raw,ir->keypressed);
236                 return;
237         }
238         dprintk(1,"%s: key event code=%d down=%d\n",
239                 dev->name,ir->keycode,ir->keypressed);
240         input_report_key(dev,ir->keycode,ir->keypressed);
241         input_sync(dev);
242 }
243
244 /* -------------------------------------------------------------------------- */
245
246 void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
247                    int ir_type, IR_KEYTAB_TYPE *ir_codes)
248 {
249         int i;
250
251         ir->ir_type = ir_type;
252         if (ir_codes)
253                 memcpy(ir->ir_codes, ir_codes, sizeof(ir->ir_codes));
254
255         dev->keycode     = ir->ir_codes;
256         dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
257         dev->keycodemax  = IR_KEYTAB_SIZE;
258         for (i = 0; i < IR_KEYTAB_SIZE; i++)
259                 set_bit(ir->ir_codes[i], dev->keybit);
260         clear_bit(0, dev->keybit);
261
262         set_bit(EV_KEY, dev->evbit);
263         if (repeat)
264                 set_bit(EV_REP, dev->evbit);
265 }
266
267 void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
268 {
269         if (ir->keypressed) {
270                 ir->keypressed = 0;
271                 ir_input_key_event(dev,ir);
272         }
273 }
274
275 void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
276                       u32 ir_key, u32 ir_raw)
277 {
278         u32 keycode = IR_KEYCODE(ir->ir_codes, ir_key);
279
280         if (ir->keypressed && ir->keycode != keycode) {
281                 ir->keypressed = 0;
282                 ir_input_key_event(dev,ir);
283         }
284         if (!ir->keypressed) {
285                 ir->ir_key  = ir_key;
286                 ir->ir_raw  = ir_raw;
287                 ir->keycode = keycode;
288                 ir->keypressed = 1;
289                 ir_input_key_event(dev,ir);
290         }
291 }
292
293 /* -------------------------------------------------------------------------- */
294
295 u32 ir_extract_bits(u32 data, u32 mask)
296 {
297         int mbit, vbit;
298         u32 value;
299
300         value = 0;
301         vbit  = 0;
302         for (mbit = 0; mbit < 32; mbit++) {
303                 if (!(mask & ((u32)1 << mbit)))
304                         continue;
305                 if (data & ((u32)1 << mbit))
306                         value |= (1 << vbit);
307                 vbit++;
308         }
309         return value;
310 }
311
312 static int inline getbit(u32 *samples, int bit)
313 {
314         return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
315 }
316
317 /* sump raw samples for visual debugging ;) */
318 int ir_dump_samples(u32 *samples, int count)
319 {
320         int i, bit, start;
321
322         printk(KERN_DEBUG "ir samples: ");
323         start = 0;
324         for (i = 0; i < count * 32; i++) {
325                 bit = getbit(samples,i);
326                 if (bit)
327                         start = 1;
328                 if (0 == start)
329                         continue;
330                 printk("%s", bit ? "#" : "_");
331         }
332         printk("\n");
333         return 0;
334 }
335
336 /* decode raw samples, pulse distance coding used by NEC remotes */
337 int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
338 {
339         int i,last,bit,len;
340         u32 curBit;
341         u32 value;
342
343         /* find start burst */
344         for (i = len = 0; i < count * 32; i++) {
345                 bit = getbit(samples,i);
346                 if (bit) {
347                         len++;
348                 } else {
349                         if (len >= 29)
350                                 break;
351                         len = 0;
352                 }
353         }
354
355         /* start burst to short */
356         if (len < 29)
357                 return 0xffffffff;
358
359         /* find start silence */
360         for (len = 0; i < count * 32; i++) {
361                 bit = getbit(samples,i);
362                 if (bit) {
363                         break;
364                 } else {
365                         len++;
366                 }
367         }
368
369         /* silence to short */
370         if (len < 7)
371                 return 0xffffffff;
372
373         /* go decoding */
374         len   = 0;
375         last = 1;
376         value = 0; curBit = 1;
377         for (; i < count * 32; i++) {
378                 bit  = getbit(samples,i);
379                 if (last) {
380                         if(bit) {
381                                 continue;
382                         } else {
383                                 len = 1;
384                         }
385                 } else {
386                         if (bit) {
387                                 if (len > (low + high) /2)
388                                         value |= curBit;
389                                 curBit <<= 1;
390                                 if (curBit == 1)
391                                         break;
392                         } else {
393                                 len++;
394                         }
395                 }
396                 last = bit;
397         }
398
399         return value;
400 }
401
402 /* decode raw samples, biphase coding, used by rc5 for example */
403 int ir_decode_biphase(u32 *samples, int count, int low, int high)
404 {
405         int i,last,bit,len,flips;
406         u32 value;
407
408         /* find start bit (1) */
409         for (i = 0; i < 32; i++) {
410                 bit = getbit(samples,i);
411                 if (bit)
412                         break;
413         }
414
415         /* go decoding */
416         len   = 0;
417         flips = 0;
418         value = 1;
419         for (; i < count * 32; i++) {
420                 if (len > high)
421                         break;
422                 if (flips > 1)
423                         break;
424                 last = bit;
425                 bit  = getbit(samples,i);
426                 if (last == bit) {
427                         len++;
428                         continue;
429                 }
430                 if (len < low) {
431                         len++;
432                         flips++;
433                         continue;
434                 }
435                 value <<= 1;
436                 value |= bit;
437                 flips = 0;
438                 len   = 1;
439         }
440         return value;
441 }
442
443 EXPORT_SYMBOL_GPL(ir_input_init);
444 EXPORT_SYMBOL_GPL(ir_input_nokey);
445 EXPORT_SYMBOL_GPL(ir_input_keydown);
446
447 EXPORT_SYMBOL_GPL(ir_extract_bits);
448 EXPORT_SYMBOL_GPL(ir_dump_samples);
449 EXPORT_SYMBOL_GPL(ir_decode_biphase);
450 EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
451
452 /*
453  * Local variables:
454  * c-basic-offset: 8
455  * End:
456  */
457