upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / drivers / media / common / ir-common.c
1 /*
2  * $Id: ir-common.c,v 1.5 2004/11/07 13:17:15 kraxel 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
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 IR_KEYTAB_TYPE ir_codes_rc5_tv[IR_KEYTAB_SIZE] = {
48         [ 0x00 ] = KEY_KP0,             // 0
49         [ 0x01 ] = KEY_KP1,             // 1
50         [ 0x02 ] = KEY_KP2,             // 2
51         [ 0x03 ] = KEY_KP3,             // 3
52         [ 0x04 ] = KEY_KP4,             // 4
53         [ 0x05 ] = KEY_KP5,             // 5
54         [ 0x06 ] = KEY_KP6,             // 6
55         [ 0x07 ] = KEY_KP7,             // 7
56         [ 0x08 ] = KEY_KP8,             // 8
57         [ 0x09 ] = KEY_KP9,             // 9
58
59         [ 0x0b ] = KEY_CHANNEL,         // channel / program (japan: 11)
60         [ 0x0c ] = KEY_POWER,           // standby
61         [ 0x0d ] = KEY_MUTE,            // mute / demute
62         [ 0x0f ] = KEY_TV,              // display
63         [ 0x10 ] = KEY_VOLUMEUP,        // volume +
64         [ 0x11 ] = KEY_VOLUMEDOWN,      // volume -
65         [ 0x12 ] = KEY_BRIGHTNESSUP,    // brightness +
66         [ 0x13 ] = KEY_BRIGHTNESSDOWN,  // brightness -
67         [ 0x1e ] = KEY_SEARCH,          // search +
68         [ 0x20 ] = KEY_CHANNELUP,       // channel / program +
69         [ 0x21 ] = KEY_CHANNELDOWN,     // channel / program -
70         [ 0x22 ] = KEY_CHANNEL,         // alt / channel
71         [ 0x23 ] = KEY_LANGUAGE,        // 1st / 2nd language
72         [ 0x26 ] = KEY_SLEEP,           // sleeptimer
73         [ 0x2e ] = KEY_MENU,            // 2nd controls (USA: menu)
74         [ 0x30 ] = KEY_PAUSE,           // pause
75         [ 0x32 ] = KEY_REWIND,          // rewind
76         [ 0x33 ] = KEY_GOTO,            // go to
77         [ 0x35 ] = KEY_PLAY,            // play
78         [ 0x36 ] = KEY_STOP,            // stop
79         [ 0x37 ] = KEY_RECORD,          // recording
80         [ 0x3c ] = KEY_TEXT,            // teletext submode (Japan: 12)
81         [ 0x3d ] = KEY_SUSPEND,         // system standby
82
83 #if 0 /* FIXME */
84         [ 0x0a ] = KEY_RESERVED,        // 1/2/3 digits (japan: 10)
85         [ 0x0e ] = KEY_RESERVED,        // P.P. (personal preference)
86         [ 0x14 ] = KEY_RESERVED,        // colour saturation +
87         [ 0x15 ] = KEY_RESERVED,        // colour saturation -
88         [ 0x16 ] = KEY_RESERVED,        // bass +
89         [ 0x17 ] = KEY_RESERVED,        // bass -
90         [ 0x18 ] = KEY_RESERVED,        // treble +
91         [ 0x19 ] = KEY_RESERVED,        // treble -
92         [ 0x1a ] = KEY_RESERVED,        // balance right
93         [ 0x1b ] = KEY_RESERVED,        // balance left
94         [ 0x1c ] = KEY_RESERVED,        // contrast +
95         [ 0x1d ] = KEY_RESERVED,        // contrast -
96         [ 0x1f ] = KEY_RESERVED,        // tint/hue +
97         [ 0x24 ] = KEY_RESERVED,        // spacial stereo on/off
98         [ 0x25 ] = KEY_RESERVED,        // mono / stereo (USA)
99         [ 0x27 ] = KEY_RESERVED,        // tint / hue -
100         [ 0x28 ] = KEY_RESERVED,        // RF switch/PIP select
101         [ 0x29 ] = KEY_RESERVED,        // vote
102         [ 0x2a ] = KEY_RESERVED,        // timed page/channel clck
103         [ 0x2b ] = KEY_RESERVED,        // increment (USA)
104         [ 0x2c ] = KEY_RESERVED,        // decrement (USA)
105         [ 0x2d ] = KEY_RESERVED,        //
106         [ 0x2f ] = KEY_RESERVED,        // PIP shift
107         [ 0x31 ] = KEY_RESERVED,        // erase
108         [ 0x34 ] = KEY_RESERVED,        // wind
109         [ 0x38 ] = KEY_RESERVED,        // external 1
110         [ 0x39 ] = KEY_RESERVED,        // external 2
111         [ 0x3a ] = KEY_RESERVED,        // PIP display mode
112         [ 0x3b ] = KEY_RESERVED,        // view data mode / advance
113         [ 0x3e ] = KEY_RESERVED,        // crispener on/off
114         [ 0x3f ] = KEY_RESERVED,        // system select
115 #endif
116 };
117 EXPORT_SYMBOL_GPL(ir_codes_rc5_tv);
118
119 /* empty keytable, can be used as placeholder for not-yet created keytables */
120 IR_KEYTAB_TYPE ir_codes_empty[IR_KEYTAB_SIZE] = {
121         [ 42 ] = KEY_COFFEE,
122 };
123 EXPORT_SYMBOL_GPL(ir_codes_empty);
124
125 /* -------------------------------------------------------------------------- */
126
127 static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
128 {
129         if (KEY_RESERVED == ir->keycode) {
130                 printk(KERN_INFO "%s: unknown key: key=0x%02x raw=0x%02x down=%d\n",
131                        dev->name,ir->ir_key,ir->ir_raw,ir->keypressed);
132                 return;
133         }
134         dprintk(1,"%s: key event code=%d down=%d\n",
135                 dev->name,ir->keycode,ir->keypressed);
136         input_report_key(dev,ir->keycode,ir->keypressed);
137         input_sync(dev);
138 }
139
140 /* -------------------------------------------------------------------------- */
141
142 void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
143                    int ir_type, IR_KEYTAB_TYPE *ir_codes)
144 {
145         int i;
146
147         ir->ir_type = ir_type;
148         if (ir_codes)
149                 memcpy(ir->ir_codes, ir_codes, sizeof(ir->ir_codes));
150
151         init_input_dev(dev);
152         dev->keycode     = ir->ir_codes;
153         dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
154         dev->keycodemax  = IR_KEYTAB_SIZE;
155         for (i = 0; i < IR_KEYTAB_SIZE; i++)
156                 set_bit(ir->ir_codes[i], dev->keybit);
157         clear_bit(0, dev->keybit);
158
159         set_bit(EV_KEY, dev->evbit);
160         if (repeat)
161                 set_bit(EV_REP, dev->evbit);
162 }
163
164 void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
165 {
166         if (ir->keypressed) {
167                 ir->keypressed = 0;
168                 ir_input_key_event(dev,ir);
169         }
170 }
171
172 void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
173                       u32 ir_key, u32 ir_raw)
174 {
175         u32 keycode = IR_KEYCODE(ir->ir_codes, ir_key);
176
177         if (ir->keypressed && ir->keycode != keycode) {
178                 ir->keypressed = 0;
179                 ir_input_key_event(dev,ir);
180         }
181         if (!ir->keypressed) {
182                 ir->ir_key  = ir_key;
183                 ir->ir_raw  = ir_raw;
184                 ir->keycode = keycode;
185                 ir->keypressed = 1;
186                 ir_input_key_event(dev,ir);
187         }
188 #if 0
189         /* maybe do something like this ??? */
190         input_event(a, EV_IR, ir->ir_type, ir->ir_raw);
191 #endif
192 }
193
194 u32 ir_extract_bits(u32 data, u32 mask)
195 {
196         int mbit, vbit;
197         u32 value;
198
199         value = 0;
200         vbit  = 0;
201         for (mbit = 0; mbit < 32; mbit++) {
202                 if (!(mask & ((u32)1 << mbit)))
203                         continue;
204                 if (data & ((u32)1 << mbit))
205                         value |= (1 << vbit);
206                 vbit++;
207         }
208         return value;
209 }
210
211 EXPORT_SYMBOL_GPL(ir_input_init);
212 EXPORT_SYMBOL_GPL(ir_input_nokey);
213 EXPORT_SYMBOL_GPL(ir_input_keydown);
214 EXPORT_SYMBOL_GPL(ir_extract_bits);
215
216 /*
217  * Local variables:
218  * c-basic-offset: 8
219  * End:
220  */