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