Linux v2.6.15-rc7
[pohmelfs.git] / drivers / media / common / ir-common.c
blob97fa3fc571c419d34ec043f56f12863f9ea7132b
1 /*
3 * some common structs and functions to handle infrared remotes via
4 * input layer ...
6 * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
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.
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.
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
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/string.h>
26 #include <media/ir-common.h>
28 /* -------------------------------------------------------------------------- */
30 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
31 MODULE_LICENSE("GPL");
33 static int repeat = 1;
34 module_param(repeat, int, 0444);
35 MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
37 static int debug = 0; /* debug level (0,1,2) */
38 module_param(debug, int, 0644);
40 #define dprintk(level, fmt, arg...) if (debug >= level) \
41 printk(KERN_DEBUG fmt , ## arg)
43 /* -------------------------------------------------------------------------- */
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,
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 */
86 EXPORT_SYMBOL_GPL(ir_codes_rc5_tv);
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,
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
127 EXPORT_SYMBOL_GPL(ir_codes_winfast);
129 IR_KEYTAB_TYPE ir_codes_pinnacle[IR_KEYTAB_SIZE] = {
130 [ 0x59 ] = KEY_MUTE,
131 [ 0x4a ] = KEY_POWER,
133 [ 0x18 ] = KEY_TEXT,
134 [ 0x26 ] = KEY_TV,
135 [ 0x3d ] = KEY_PRINT,
137 [ 0x48 ] = KEY_RED,
138 [ 0x04 ] = KEY_GREEN,
139 [ 0x11 ] = KEY_YELLOW,
140 [ 0x00 ] = KEY_BLUE,
142 [ 0x2d ] = KEY_VOLUMEUP,
143 [ 0x1e ] = KEY_VOLUMEDOWN,
145 [ 0x49 ] = KEY_MENU,
147 [ 0x16 ] = KEY_CHANNELUP,
148 [ 0x17 ] = KEY_CHANNELDOWN,
150 [ 0x20 ] = KEY_UP,
151 [ 0x21 ] = KEY_DOWN,
152 [ 0x22 ] = KEY_LEFT,
153 [ 0x23 ] = KEY_RIGHT,
154 [ 0x0d ] = KEY_SELECT,
158 [ 0x08 ] = KEY_BACK,
159 [ 0x07 ] = KEY_REFRESH,
161 [ 0x2f ] = KEY_ZOOM,
162 [ 0x29 ] = KEY_RECORD,
164 [ 0x4b ] = KEY_PAUSE,
165 [ 0x4d ] = KEY_REWIND,
166 [ 0x2e ] = KEY_PLAY,
167 [ 0x4e ] = KEY_FORWARD,
168 [ 0x53 ] = KEY_PREVIOUS,
169 [ 0x4c ] = KEY_STOP,
170 [ 0x54 ] = KEY_NEXT,
172 [ 0x69 ] = KEY_KP0,
173 [ 0x6a ] = KEY_KP1,
174 [ 0x6b ] = KEY_KP2,
175 [ 0x6c ] = KEY_KP3,
176 [ 0x6d ] = KEY_KP4,
177 [ 0x6e ] = KEY_KP5,
178 [ 0x6f ] = KEY_KP6,
179 [ 0x70 ] = KEY_KP7,
180 [ 0x71 ] = KEY_KP8,
181 [ 0x72 ] = KEY_KP9,
183 [ 0x74 ] = KEY_CHANNEL,
184 [ 0x0a ] = KEY_BACKSPACE,
187 EXPORT_SYMBOL_GPL(ir_codes_pinnacle);
189 /* empty keytable, can be used as placeholder for not-yet created keytables */
190 IR_KEYTAB_TYPE ir_codes_empty[IR_KEYTAB_SIZE] = {
191 [ 42 ] = KEY_COFFEE,
193 EXPORT_SYMBOL_GPL(ir_codes_empty);
195 /* Hauppauge: the newer, gray remotes (seems there are multiple
196 * slightly different versions), shipped with cx88+ivtv cards.
197 * almost rc5 coding, but some non-standard keys */
198 IR_KEYTAB_TYPE ir_codes_hauppauge_new[IR_KEYTAB_SIZE] = {
199 /* Keys 0 to 9 */
200 [ 0x00 ] = KEY_KP0,
201 [ 0x01 ] = KEY_KP1,
202 [ 0x02 ] = KEY_KP2,
203 [ 0x03 ] = KEY_KP3,
204 [ 0x04 ] = KEY_KP4,
205 [ 0x05 ] = KEY_KP5,
206 [ 0x06 ] = KEY_KP6,
207 [ 0x07 ] = KEY_KP7,
208 [ 0x08 ] = KEY_KP8,
209 [ 0x09 ] = KEY_KP9,
211 [ 0x0a ] = KEY_TEXT, /* keypad asterisk as well */
212 [ 0x0b ] = KEY_RED, /* red button */
213 [ 0x0c ] = KEY_RADIO,
214 [ 0x0d ] = KEY_MENU,
215 [ 0x0e ] = KEY_SUBTITLE, /* also the # key */
216 [ 0x0f ] = KEY_MUTE,
217 [ 0x10 ] = KEY_VOLUMEUP,
218 [ 0x11 ] = KEY_VOLUMEDOWN,
219 [ 0x12 ] = KEY_PREVIOUS, /* previous channel */
220 [ 0x14 ] = KEY_UP,
221 [ 0x15 ] = KEY_DOWN,
222 [ 0x16 ] = KEY_LEFT,
223 [ 0x17 ] = KEY_RIGHT,
224 [ 0x18 ] = KEY_VIDEO, /* Videos */
225 [ 0x19 ] = KEY_AUDIO, /* Music */
226 /* 0x1a: Pictures - presume this means
227 "Multimedia Home Platform" -
228 no "PICTURES" key in input.h
230 [ 0x1a ] = KEY_MHP,
232 [ 0x1b ] = KEY_EPG, /* Guide */
233 [ 0x1c ] = KEY_TV,
234 [ 0x1e ] = KEY_NEXTSONG, /* skip >| */
235 [ 0x1f ] = KEY_EXIT, /* back/exit */
236 [ 0x20 ] = KEY_CHANNELUP, /* channel / program + */
237 [ 0x21 ] = KEY_CHANNELDOWN, /* channel / program - */
238 [ 0x22 ] = KEY_CHANNEL, /* source (old black remote) */
239 [ 0x24 ] = KEY_PREVIOUSSONG, /* replay |< */
240 [ 0x25 ] = KEY_ENTER, /* OK */
241 [ 0x26 ] = KEY_SLEEP, /* minimize (old black remote) */
242 [ 0x29 ] = KEY_BLUE, /* blue key */
243 [ 0x2e ] = KEY_GREEN, /* green button */
244 [ 0x30 ] = KEY_PAUSE, /* pause */
245 [ 0x32 ] = KEY_REWIND, /* backward << */
246 [ 0x34 ] = KEY_FASTFORWARD, /* forward >> */
247 [ 0x35 ] = KEY_PLAY,
248 [ 0x36 ] = KEY_STOP,
249 [ 0x37 ] = KEY_RECORD, /* recording */
250 [ 0x38 ] = KEY_YELLOW, /* yellow key */
251 [ 0x3b ] = KEY_SELECT, /* top right button */
252 [ 0x3c ] = KEY_ZOOM, /* full */
253 [ 0x3d ] = KEY_POWER, /* system power (green button) */
255 EXPORT_SYMBOL(ir_codes_hauppauge_new);
257 IR_KEYTAB_TYPE ir_codes_pixelview[IR_KEYTAB_SIZE] = {
258 [ 2 ] = KEY_KP0,
259 [ 1 ] = KEY_KP1,
260 [ 11 ] = KEY_KP2,
261 [ 27 ] = KEY_KP3,
262 [ 5 ] = KEY_KP4,
263 [ 9 ] = KEY_KP5,
264 [ 21 ] = KEY_KP6,
265 [ 6 ] = KEY_KP7,
266 [ 10 ] = KEY_KP8,
267 [ 18 ] = KEY_KP9,
269 [ 3 ] = KEY_TUNER, /* TV/FM */
270 [ 7 ] = KEY_SEARCH, /* scan */
271 [ 28 ] = KEY_ZOOM, /* full screen */
272 [ 30 ] = KEY_POWER,
273 [ 23 ] = KEY_VOLUMEDOWN,
274 [ 31 ] = KEY_VOLUMEUP,
275 [ 20 ] = KEY_CHANNELDOWN,
276 [ 22 ] = KEY_CHANNELUP,
277 [ 24 ] = KEY_MUTE,
279 [ 0 ] = KEY_LIST, /* source */
280 [ 19 ] = KEY_INFO, /* loop */
281 [ 16 ] = KEY_LAST, /* +100 */
282 [ 13 ] = KEY_CLEAR, /* reset */
283 [ 12 ] = BTN_RIGHT, /* fun++ */
284 [ 4 ] = BTN_LEFT, /* fun-- */
285 [ 14 ] = KEY_GOTO, /* function */
286 [ 15 ] = KEY_STOP, /* freeze */
288 EXPORT_SYMBOL(ir_codes_pixelview);
290 /* -------------------------------------------------------------------------- */
292 static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
294 if (KEY_RESERVED == ir->keycode) {
295 printk(KERN_INFO "%s: unknown key: key=0x%02x raw=0x%02x down=%d\n",
296 dev->name,ir->ir_key,ir->ir_raw,ir->keypressed);
297 return;
299 dprintk(1,"%s: key event code=%d down=%d\n",
300 dev->name,ir->keycode,ir->keypressed);
301 input_report_key(dev,ir->keycode,ir->keypressed);
302 input_sync(dev);
305 /* -------------------------------------------------------------------------- */
307 void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
308 int ir_type, IR_KEYTAB_TYPE *ir_codes)
310 int i;
312 ir->ir_type = ir_type;
313 if (ir_codes)
314 memcpy(ir->ir_codes, ir_codes, sizeof(ir->ir_codes));
317 dev->keycode = ir->ir_codes;
318 dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
319 dev->keycodemax = IR_KEYTAB_SIZE;
320 for (i = 0; i < IR_KEYTAB_SIZE; i++)
321 set_bit(ir->ir_codes[i], dev->keybit);
322 clear_bit(0, dev->keybit);
324 set_bit(EV_KEY, dev->evbit);
325 if (repeat)
326 set_bit(EV_REP, dev->evbit);
329 void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
331 if (ir->keypressed) {
332 ir->keypressed = 0;
333 ir_input_key_event(dev,ir);
337 void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
338 u32 ir_key, u32 ir_raw)
340 u32 keycode = IR_KEYCODE(ir->ir_codes, ir_key);
342 if (ir->keypressed && ir->keycode != keycode) {
343 ir->keypressed = 0;
344 ir_input_key_event(dev,ir);
346 if (!ir->keypressed) {
347 ir->ir_key = ir_key;
348 ir->ir_raw = ir_raw;
349 ir->keycode = keycode;
350 ir->keypressed = 1;
351 ir_input_key_event(dev,ir);
355 /* -------------------------------------------------------------------------- */
357 u32 ir_extract_bits(u32 data, u32 mask)
359 int mbit, vbit;
360 u32 value;
362 value = 0;
363 vbit = 0;
364 for (mbit = 0; mbit < 32; mbit++) {
365 if (!(mask & ((u32)1 << mbit)))
366 continue;
367 if (data & ((u32)1 << mbit))
368 value |= (1 << vbit);
369 vbit++;
371 return value;
374 static int inline getbit(u32 *samples, int bit)
376 return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
379 /* sump raw samples for visual debugging ;) */
380 int ir_dump_samples(u32 *samples, int count)
382 int i, bit, start;
384 printk(KERN_DEBUG "ir samples: ");
385 start = 0;
386 for (i = 0; i < count * 32; i++) {
387 bit = getbit(samples,i);
388 if (bit)
389 start = 1;
390 if (0 == start)
391 continue;
392 printk("%s", bit ? "#" : "_");
394 printk("\n");
395 return 0;
398 /* decode raw samples, pulse distance coding used by NEC remotes */
399 int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
401 int i,last,bit,len;
402 u32 curBit;
403 u32 value;
405 /* find start burst */
406 for (i = len = 0; i < count * 32; i++) {
407 bit = getbit(samples,i);
408 if (bit) {
409 len++;
410 } else {
411 if (len >= 29)
412 break;
413 len = 0;
417 /* start burst to short */
418 if (len < 29)
419 return 0xffffffff;
421 /* find start silence */
422 for (len = 0; i < count * 32; i++) {
423 bit = getbit(samples,i);
424 if (bit) {
425 break;
426 } else {
427 len++;
431 /* silence to short */
432 if (len < 7)
433 return 0xffffffff;
435 /* go decoding */
436 len = 0;
437 last = 1;
438 value = 0; curBit = 1;
439 for (; i < count * 32; i++) {
440 bit = getbit(samples,i);
441 if (last) {
442 if(bit) {
443 continue;
444 } else {
445 len = 1;
447 } else {
448 if (bit) {
449 if (len > (low + high) /2)
450 value |= curBit;
451 curBit <<= 1;
452 if (curBit == 1)
453 break;
454 } else {
455 len++;
458 last = bit;
461 return value;
464 /* decode raw samples, biphase coding, used by rc5 for example */
465 int ir_decode_biphase(u32 *samples, int count, int low, int high)
467 int i,last,bit,len,flips;
468 u32 value;
470 /* find start bit (1) */
471 for (i = 0; i < 32; i++) {
472 bit = getbit(samples,i);
473 if (bit)
474 break;
477 /* go decoding */
478 len = 0;
479 flips = 0;
480 value = 1;
481 for (; i < count * 32; i++) {
482 if (len > high)
483 break;
484 if (flips > 1)
485 break;
486 last = bit;
487 bit = getbit(samples,i);
488 if (last == bit) {
489 len++;
490 continue;
492 if (len < low) {
493 len++;
494 flips++;
495 continue;
497 value <<= 1;
498 value |= bit;
499 flips = 0;
500 len = 1;
502 return value;
505 EXPORT_SYMBOL_GPL(ir_input_init);
506 EXPORT_SYMBOL_GPL(ir_input_nokey);
507 EXPORT_SYMBOL_GPL(ir_input_keydown);
509 EXPORT_SYMBOL_GPL(ir_extract_bits);
510 EXPORT_SYMBOL_GPL(ir_dump_samples);
511 EXPORT_SYMBOL_GPL(ir_decode_biphase);
512 EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
515 * Local variables:
516 * c-basic-offset: 8
517 * End: