3 * some common structs and functions to handle infrared remotes via
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
] = {
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) */
77 [ 0x32 ] = KEY_REWIND
,
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
] = {
103 [ 2 ] = KEY_TUNER
, /* TV/FM */
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 */
117 [ 24 ] = KEY_KPPLUS
, /* fine tune + */
118 [ 25 ] = KEY_KPMINUS
, /* fine tune - */
120 [ 19 ] = KEY_KPENTER
,
122 [ 35 ] = KEY_PLAYPAUSE
,
127 EXPORT_SYMBOL_GPL(ir_codes_winfast
);
129 IR_KEYTAB_TYPE ir_codes_pinnacle
[IR_KEYTAB_SIZE
] = {
131 [ 0x4a ] = KEY_POWER
,
135 [ 0x3d ] = KEY_PRINT
,
138 [ 0x04 ] = KEY_GREEN
,
139 [ 0x11 ] = KEY_YELLOW
,
142 [ 0x2d ] = KEY_VOLUMEUP
,
143 [ 0x1e ] = KEY_VOLUMEDOWN
,
147 [ 0x16 ] = KEY_CHANNELUP
,
148 [ 0x17 ] = KEY_CHANNELDOWN
,
153 [ 0x23 ] = KEY_RIGHT
,
154 [ 0x0d ] = KEY_SELECT
,
159 [ 0x07 ] = KEY_REFRESH
,
162 [ 0x29 ] = KEY_RECORD
,
164 [ 0x4b ] = KEY_PAUSE
,
165 [ 0x4d ] = KEY_REWIND
,
167 [ 0x4e ] = KEY_FORWARD
,
168 [ 0x53 ] = KEY_PREVIOUS
,
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
] = {
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
] = {
211 [ 0x0a ] = KEY_TEXT
, /* keypad asterisk as well */
212 [ 0x0b ] = KEY_RED
, /* red button */
213 [ 0x0c ] = KEY_RADIO
,
215 [ 0x0e ] = KEY_SUBTITLE
, /* also the # key */
217 [ 0x10 ] = KEY_VOLUMEUP
,
218 [ 0x11 ] = KEY_VOLUMEDOWN
,
219 [ 0x12 ] = KEY_PREVIOUS
, /* previous channel */
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
232 [ 0x1b ] = KEY_EPG
, /* Guide */
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 >> */
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
] = {
269 [ 3 ] = KEY_TUNER
, /* TV/FM */
270 [ 7 ] = KEY_SEARCH
, /* scan */
271 [ 28 ] = KEY_ZOOM
, /* full screen */
273 [ 23 ] = KEY_VOLUMEDOWN
,
274 [ 31 ] = KEY_VOLUMEUP
,
275 [ 20 ] = KEY_CHANNELDOWN
,
276 [ 22 ] = KEY_CHANNELUP
,
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
);
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
);
305 /* -------------------------------------------------------------------------- */
307 void ir_input_init(struct input_dev
*dev
, struct ir_input_state
*ir
,
308 int ir_type
, IR_KEYTAB_TYPE
*ir_codes
)
312 ir
->ir_type
= ir_type
;
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
);
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
) {
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
) {
344 ir_input_key_event(dev
,ir
);
346 if (!ir
->keypressed
) {
349 ir
->keycode
= keycode
;
351 ir_input_key_event(dev
,ir
);
355 /* -------------------------------------------------------------------------- */
357 u32
ir_extract_bits(u32 data
, u32 mask
)
364 for (mbit
= 0; mbit
< 32; mbit
++) {
365 if (!(mask
& ((u32
)1 << mbit
)))
367 if (data
& ((u32
)1 << mbit
))
368 value
|= (1 << vbit
);
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
)
384 printk(KERN_DEBUG
"ir samples: ");
386 for (i
= 0; i
< count
* 32; i
++) {
387 bit
= getbit(samples
,i
);
392 printk("%s", bit
? "#" : "_");
398 /* decode raw samples, pulse distance coding used by NEC remotes */
399 int ir_decode_pulsedistance(u32
*samples
, int count
, int low
, int high
)
405 /* find start burst */
406 for (i
= len
= 0; i
< count
* 32; i
++) {
407 bit
= getbit(samples
,i
);
417 /* start burst to short */
421 /* find start silence */
422 for (len
= 0; i
< count
* 32; i
++) {
423 bit
= getbit(samples
,i
);
431 /* silence to short */
438 value
= 0; curBit
= 1;
439 for (; i
< count
* 32; i
++) {
440 bit
= getbit(samples
,i
);
449 if (len
> (low
+ high
) /2)
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
;
470 /* find start bit (1) */
471 for (i
= 0; i
< 32; i
++) {
472 bit
= getbit(samples
,i
);
481 for (; i
< count
* 32; i
++) {
487 bit
= getbit(samples
,i
);
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
);