[PATCH] v4l: update for tuner cards and some V4L chips
[powerpc.git] / drivers / media / common / ir-common.c
1 /*
2  * $Id: ir-common.c,v 1.10 2005/05/22 19:23:39 nsh 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         [ 0x00 ] = KEY_KP0,             // 0
50         [ 0x01 ] = KEY_KP1,             // 1
51         [ 0x02 ] = KEY_KP2,             // 2
52         [ 0x03 ] = KEY_KP3,             // 3
53         [ 0x04 ] = KEY_KP4,             // 4
54         [ 0x05 ] = KEY_KP5,             // 5
55         [ 0x06 ] = KEY_KP6,             // 6
56         [ 0x07 ] = KEY_KP7,             // 7
57         [ 0x08 ] = KEY_KP8,             // 8
58         [ 0x09 ] = KEY_KP9,             // 9
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,        // volume +
65         [ 0x11 ] = KEY_VOLUMEDOWN,      // volume -
66         [ 0x12 ] = KEY_BRIGHTNESSUP,    // brightness +
67         [ 0x13 ] = KEY_BRIGHTNESSDOWN,  // brightness -
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,           // pause
76         [ 0x32 ] = KEY_REWIND,          // rewind
77         [ 0x33 ] = KEY_GOTO,            // go to
78         [ 0x35 ] = KEY_PLAY,            // play
79         [ 0x36 ] = KEY_STOP,            // stop
80         [ 0x37 ] = KEY_RECORD,          // recording
81         [ 0x3c ] = KEY_TEXT,            // teletext submode (Japan: 12)
82         [ 0x3d ] = KEY_SUSPEND,         // system standby
83
84 #if 0 /* FIXME */
85         [ 0x0a ] = KEY_RESERVED,        // 1/2/3 digits (japan: 10)
86         [ 0x0e ] = KEY_RESERVED,        // P.P. (personal preference)
87         [ 0x14 ] = KEY_RESERVED,        // colour saturation +
88         [ 0x15 ] = KEY_RESERVED,        // colour saturation -
89         [ 0x16 ] = KEY_RESERVED,        // bass +
90         [ 0x17 ] = KEY_RESERVED,        // bass -
91         [ 0x18 ] = KEY_RESERVED,        // treble +
92         [ 0x19 ] = KEY_RESERVED,        // treble -
93         [ 0x1a ] = KEY_RESERVED,        // balance right
94         [ 0x1b ] = KEY_RESERVED,        // balance left
95         [ 0x1c ] = KEY_RESERVED,        // contrast +
96         [ 0x1d ] = KEY_RESERVED,        // contrast -
97         [ 0x1f ] = KEY_RESERVED,        // tint/hue +
98         [ 0x24 ] = KEY_RESERVED,        // spacial stereo on/off
99         [ 0x25 ] = KEY_RESERVED,        // mono / stereo (USA)
100         [ 0x27 ] = KEY_RESERVED,        // tint / hue -
101         [ 0x28 ] = KEY_RESERVED,        // RF switch/PIP select
102         [ 0x29 ] = KEY_RESERVED,        // vote
103         [ 0x2a ] = KEY_RESERVED,        // timed page/channel clck
104         [ 0x2b ] = KEY_RESERVED,        // increment (USA)
105         [ 0x2c ] = KEY_RESERVED,        // decrement (USA)
106         [ 0x2d ] = KEY_RESERVED,        //
107         [ 0x2f ] = KEY_RESERVED,        // PIP shift
108         [ 0x31 ] = KEY_RESERVED,        // erase
109         [ 0x34 ] = KEY_RESERVED,        // wind
110         [ 0x38 ] = KEY_RESERVED,        // external 1
111         [ 0x39 ] = KEY_RESERVED,        // external 2
112         [ 0x3a ] = KEY_RESERVED,        // PIP display mode
113         [ 0x3b ] = KEY_RESERVED,        // view data mode / advance
114         [ 0x3e ] = KEY_RESERVED,        // crispener on/off
115         [ 0x3f ] = KEY_RESERVED,        // system select
116 #endif
117 };
118 EXPORT_SYMBOL_GPL(ir_codes_rc5_tv);
119
120 /* Table for Leadtek Winfast Remote Controls - used by both bttv and cx88 */
121 IR_KEYTAB_TYPE ir_codes_winfast[IR_KEYTAB_SIZE] = {
122         [  5 ] = KEY_KP1,
123         [  6 ] = KEY_KP2,
124         [  7 ] = KEY_KP3,
125         [  9 ] = KEY_KP4,
126         [ 10 ] = KEY_KP5,
127         [ 11 ] = KEY_KP6,
128         [ 13 ] = KEY_KP7,
129         [ 14 ] = KEY_KP8,
130         [ 15 ] = KEY_KP9,
131         [ 18 ] = KEY_KP0,
132
133         [  0 ] = KEY_POWER,
134 //      [ 27 ] = MTS button
135         [  2 ] = KEY_TUNER,     // TV/FM
136         [ 30 ] = KEY_VIDEO,
137 //      [ 22 ] = display button
138         [  4 ] = KEY_VOLUMEUP,
139         [  8 ] = KEY_VOLUMEDOWN,
140         [ 12 ] = KEY_CHANNELUP,
141         [ 16 ] = KEY_CHANNELDOWN,
142         [  3 ] = KEY_ZOOM,      // fullscreen
143         [ 31 ] = KEY_SUBTITLE,  // closed caption/teletext
144         [ 32 ] = KEY_SLEEP,
145 //      [ 41 ] = boss key
146         [ 20 ] = KEY_MUTE,
147         [ 43 ] = KEY_RED,
148         [ 44 ] = KEY_GREEN,
149         [ 45 ] = KEY_YELLOW,
150         [ 46 ] = KEY_BLUE,
151         [ 24 ] = KEY_KPPLUS,    //fine tune +
152         [ 25 ] = KEY_KPMINUS,   //fine tune -
153 //      [ 42 ] = picture in picture
154         [ 33 ] = KEY_KPDOT,
155         [ 19 ] = KEY_KPENTER,
156 //      [ 17 ] = recall
157         [ 34 ] = KEY_BACK,
158         [ 35 ] = KEY_PLAYPAUSE,
159         [ 36 ] = KEY_NEXT,
160 //      [ 37 ] = time shifting
161         [ 38 ] = KEY_STOP,
162         [ 39 ] = KEY_RECORD
163 //      [ 40 ] = snapshot
164 };
165 EXPORT_SYMBOL_GPL(ir_codes_winfast);
166
167 /* empty keytable, can be used as placeholder for not-yet created keytables */
168 IR_KEYTAB_TYPE ir_codes_empty[IR_KEYTAB_SIZE] = {
169         [ 42 ] = KEY_COFFEE,
170 };
171 EXPORT_SYMBOL_GPL(ir_codes_empty);
172
173 /* Hauppauge: the newer, gray remotes (seems there are multiple
174  * slightly different versions), shipped with cx88+ivtv cards.
175  * almost rc5 coding, but some non-standard keys */
176 IR_KEYTAB_TYPE ir_codes_hauppauge_new[IR_KEYTAB_SIZE] = {
177         [ 0x00 ] = KEY_KP0,             // 0
178         [ 0x01 ] = KEY_KP1,             // 1
179         [ 0x02 ] = KEY_KP2,             // 2
180         [ 0x03 ] = KEY_KP3,             // 3
181         [ 0x04 ] = KEY_KP4,             // 4
182         [ 0x05 ] = KEY_KP5,             // 5
183         [ 0x06 ] = KEY_KP6,             // 6
184         [ 0x07 ] = KEY_KP7,             // 7
185         [ 0x08 ] = KEY_KP8,             // 8
186         [ 0x09 ] = KEY_KP9,             // 9
187         [ 0x0a ] = KEY_TEXT,            // keypad asterisk as well
188         [ 0x0b ] = KEY_RED,             // red button
189         [ 0x0c ] = KEY_RADIO,           // radio
190         [ 0x0d ] = KEY_MENU,            // menu
191         [ 0x0e ] = KEY_SUBTITLE,        // also the # key
192         [ 0x0f ] = KEY_MUTE,            // mute
193         [ 0x10 ] = KEY_VOLUMEUP,        // volume +
194         [ 0x11 ] = KEY_VOLUMEDOWN,      // volume -
195         [ 0x12 ] = KEY_PREVIOUS,        // previous channel
196         [ 0x14 ] = KEY_UP,              // up
197         [ 0x15 ] = KEY_DOWN,            // down
198         [ 0x16 ] = KEY_LEFT,            // left
199         [ 0x17 ] = KEY_RIGHT,           // right
200         [ 0x18 ] = KEY_VIDEO,           // Videos
201         [ 0x19 ] = KEY_AUDIO,           // Music
202         [ 0x1a ] = KEY_MHP,             // Pictures - presume this means "Multimedia Home Platform"- no "PICTURES" key in input.h
203         [ 0x1b ] = KEY_EPG,             // Guide
204         [ 0x1c ] = KEY_TV,              // TV
205         [ 0x1e ] = KEY_NEXTSONG,        // skip >|
206         [ 0x1f ] = KEY_EXIT,            // back/exit
207         [ 0x20 ] = KEY_CHANNELUP,       // channel / program +
208         [ 0x21 ] = KEY_CHANNELDOWN,     // channel / program -
209         [ 0x22 ] = KEY_CHANNEL,         // source (old black remote)
210         [ 0x24 ] = KEY_PREVIOUSSONG,    // replay |<
211         [ 0x25 ] = KEY_ENTER,           // OK
212         [ 0x26 ] = KEY_SLEEP,           // minimize (old black remote)
213         [ 0x29 ] = KEY_BLUE,            // blue key
214         [ 0x2e ] = KEY_GREEN,           // green button
215         [ 0x30 ] = KEY_PAUSE,           // pause
216         [ 0x32 ] = KEY_REWIND,          // backward <<
217         [ 0x34 ] = KEY_FASTFORWARD,     // forward >>
218         [ 0x35 ] = KEY_PLAY,            // play
219         [ 0x36 ] = KEY_STOP,            // stop
220         [ 0x37 ] = KEY_RECORD,          // recording
221         [ 0x38 ] = KEY_YELLOW,          // yellow key
222         [ 0x3b ] = KEY_SELECT,          // top right button
223         [ 0x3c ] = KEY_ZOOM,            // full
224         [ 0x3d ] = KEY_POWER,           // system power (green button)
225 };
226 EXPORT_SYMBOL(ir_codes_hauppauge_new);
227
228 IR_KEYTAB_TYPE ir_codes_pixelview[IR_KEYTAB_SIZE] = {
229         [  2 ] = KEY_KP0,
230         [  1 ] = KEY_KP1,
231         [ 11 ] = KEY_KP2,
232         [ 27 ] = KEY_KP3,
233         [  5 ] = KEY_KP4,
234         [  9 ] = KEY_KP5,
235         [ 21 ] = KEY_KP6,
236         [  6 ] = KEY_KP7,
237         [ 10 ] = KEY_KP8,
238         [ 18 ] = KEY_KP9,
239
240         [  3 ] = KEY_TUNER,       // TV/FM
241         [  7 ] = KEY_SEARCH,      // scan
242         [ 28 ] = KEY_ZOOM,        // full screen
243         [ 30 ] = KEY_POWER,
244         [ 23 ] = KEY_VOLUMEDOWN,
245         [ 31 ] = KEY_VOLUMEUP,
246         [ 20 ] = KEY_CHANNELDOWN,
247         [ 22 ] = KEY_CHANNELUP,
248         [ 24 ] = KEY_MUTE,
249
250         [  0 ] = KEY_LIST,        // source
251         [ 19 ] = KEY_INFO,        // loop
252         [ 16 ] = KEY_LAST,        // +100
253         [ 13 ] = KEY_CLEAR,       // reset
254         [ 12 ] = BTN_RIGHT,       // fun++
255         [  4 ] = BTN_LEFT,        // fun--
256         [ 14 ] = KEY_GOTO,        // function
257         [ 15 ] = KEY_STOP,         // freeze
258 };
259 EXPORT_SYMBOL(ir_codes_pixelview);
260
261 /* -------------------------------------------------------------------------- */
262
263 static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
264 {
265         if (KEY_RESERVED == ir->keycode) {
266                 printk(KERN_INFO "%s: unknown key: key=0x%02x raw=0x%02x down=%d\n",
267                        dev->name,ir->ir_key,ir->ir_raw,ir->keypressed);
268                 return;
269         }
270         dprintk(1,"%s: key event code=%d down=%d\n",
271                 dev->name,ir->keycode,ir->keypressed);
272         input_report_key(dev,ir->keycode,ir->keypressed);
273         input_sync(dev);
274 }
275
276 /* -------------------------------------------------------------------------- */
277
278 void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
279                    int ir_type, IR_KEYTAB_TYPE *ir_codes)
280 {
281         int i;
282
283         ir->ir_type = ir_type;
284         if (ir_codes)
285                 memcpy(ir->ir_codes, ir_codes, sizeof(ir->ir_codes));
286
287         init_input_dev(dev);
288         dev->keycode     = ir->ir_codes;
289         dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
290         dev->keycodemax  = IR_KEYTAB_SIZE;
291         for (i = 0; i < IR_KEYTAB_SIZE; i++)
292                 set_bit(ir->ir_codes[i], dev->keybit);
293         clear_bit(0, dev->keybit);
294
295         set_bit(EV_KEY, dev->evbit);
296         if (repeat)
297                 set_bit(EV_REP, dev->evbit);
298 }
299
300 void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
301 {
302         if (ir->keypressed) {
303                 ir->keypressed = 0;
304                 ir_input_key_event(dev,ir);
305         }
306 }
307
308 void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
309                       u32 ir_key, u32 ir_raw)
310 {
311         u32 keycode = IR_KEYCODE(ir->ir_codes, ir_key);
312
313         if (ir->keypressed && ir->keycode != keycode) {
314                 ir->keypressed = 0;
315                 ir_input_key_event(dev,ir);
316         }
317         if (!ir->keypressed) {
318                 ir->ir_key  = ir_key;
319                 ir->ir_raw  = ir_raw;
320                 ir->keycode = keycode;
321                 ir->keypressed = 1;
322                 ir_input_key_event(dev,ir);
323         }
324 #if 0
325         /* maybe do something like this ??? */
326         input_event(a, EV_IR, ir->ir_type, ir->ir_raw);
327 #endif
328 }
329
330 /* -------------------------------------------------------------------------- */
331
332 u32 ir_extract_bits(u32 data, u32 mask)
333 {
334         int mbit, vbit;
335         u32 value;
336
337         value = 0;
338         vbit  = 0;
339         for (mbit = 0; mbit < 32; mbit++) {
340                 if (!(mask & ((u32)1 << mbit)))
341                         continue;
342                 if (data & ((u32)1 << mbit))
343                         value |= (1 << vbit);
344                 vbit++;
345         }
346         return value;
347 }
348
349 static int inline getbit(u32 *samples, int bit)
350 {
351         return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
352 }
353
354 /* sump raw samples for visual debugging ;) */
355 int ir_dump_samples(u32 *samples, int count)
356 {
357         int i, bit, start;
358
359         printk(KERN_DEBUG "ir samples: ");
360         start = 0;
361         for (i = 0; i < count * 32; i++) {
362                 bit = getbit(samples,i);
363                 if (bit)
364                         start = 1;
365                 if (0 == start)
366                         continue;
367                 printk("%s", bit ? "#" : "_");
368         }
369         printk("\n");
370         return 0;
371 }
372
373 /* decode raw samples, biphase coding, used by rc5 for example */
374 int ir_decode_biphase(u32 *samples, int count, int low, int high)
375 {
376         int i,last,bit,len,flips;
377         u32 value;
378
379         /* find start bit (1) */
380         for (i = 0; i < 32; i++) {
381                 bit = getbit(samples,i);
382                 if (bit)
383                         break;
384         }
385
386         /* go decoding */
387         len   = 0;
388         flips = 0;
389         value = 1;
390         for (; i < count * 32; i++) {
391                 if (len > high)
392                         break;
393                 if (flips > 1)
394                         break;
395                 last = bit;
396                 bit  = getbit(samples,i);
397                 if (last == bit) {
398                         len++;
399                         continue;
400                 }
401                 if (len < low) {
402                         len++;
403                         flips++;
404                         continue;
405                 }
406                 value <<= 1;
407                 value |= bit;
408                 flips = 0;
409                 len   = 1;
410         }
411         return value;
412 }
413
414 EXPORT_SYMBOL_GPL(ir_input_init);
415 EXPORT_SYMBOL_GPL(ir_input_nokey);
416 EXPORT_SYMBOL_GPL(ir_input_keydown);
417
418 EXPORT_SYMBOL_GPL(ir_extract_bits);
419 EXPORT_SYMBOL_GPL(ir_dump_samples);
420 EXPORT_SYMBOL_GPL(ir_decode_biphase);
421
422 /*
423  * Local variables:
424  * c-basic-offset: 8
425  * End:
426  */
427