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/string.h>
25 #include <linux/jiffies.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 /* -------------------------------------------------------------------------- */
39 static void ir_input_key_event(struct input_dev
*dev
, struct ir_input_state
*ir
)
41 if (KEY_RESERVED
== ir
->keycode
) {
42 printk(KERN_INFO
"%s: unknown key: key=0x%02x down=%d\n",
43 dev
->name
, ir
->ir_key
, ir
->keypressed
);
46 IR_dprintk(1,"%s: key event code=%d down=%d\n",
47 dev
->name
,ir
->keycode
,ir
->keypressed
);
48 input_report_key(dev
,ir
->keycode
,ir
->keypressed
);
52 /* -------------------------------------------------------------------------- */
54 int ir_input_init(struct input_dev
*dev
, struct ir_input_state
*ir
,
55 int ir_type
, struct ir_scancode_table
*ir_codes
)
57 ir
->ir_type
= ir_type
;
60 set_bit(EV_REP
, dev
->evbit
);
62 ir_input_register(dev
, ir_codes
);
66 EXPORT_SYMBOL_GPL(ir_input_init
);
69 void ir_input_nokey(struct input_dev
*dev
, struct ir_input_state
*ir
)
73 ir_input_key_event(dev
,ir
);
76 EXPORT_SYMBOL_GPL(ir_input_nokey
);
78 void ir_input_keydown(struct input_dev
*dev
, struct ir_input_state
*ir
,
81 u32 keycode
= ir_g_keycode_from_table(dev
, ir_key
);
83 if (ir
->keypressed
&& ir
->keycode
!= keycode
) {
85 ir_input_key_event(dev
,ir
);
87 if (!ir
->keypressed
) {
89 ir
->keycode
= keycode
;
91 ir_input_key_event(dev
,ir
);
94 EXPORT_SYMBOL_GPL(ir_input_keydown
);
96 /* -------------------------------------------------------------------------- */
97 /* extract mask bits out of data and pack them into the result */
98 u32
ir_extract_bits(u32 data
, u32 mask
)
100 u32 vbit
= 1, value
= 0;
113 EXPORT_SYMBOL_GPL(ir_extract_bits
);
115 static int inline getbit(u32
*samples
, int bit
)
117 return (samples
[bit
/32] & (1 << (31-(bit
%32)))) ? 1 : 0;
120 /* sump raw samples for visual debugging ;) */
121 int ir_dump_samples(u32
*samples
, int count
)
125 printk(KERN_DEBUG
"ir samples: ");
127 for (i
= 0; i
< count
* 32; i
++) {
128 bit
= getbit(samples
,i
);
133 printk("%s", bit
? "#" : "_");
138 EXPORT_SYMBOL_GPL(ir_dump_samples
);
140 /* decode raw samples, pulse distance coding used by NEC remotes */
141 int ir_decode_pulsedistance(u32
*samples
, int count
, int low
, int high
)
147 /* find start burst */
148 for (i
= len
= 0; i
< count
* 32; i
++) {
149 bit
= getbit(samples
,i
);
159 /* start burst to short */
163 /* find start silence */
164 for (len
= 0; i
< count
* 32; i
++) {
165 bit
= getbit(samples
,i
);
173 /* silence to short */
180 value
= 0; curBit
= 1;
181 for (; i
< count
* 32; i
++) {
182 bit
= getbit(samples
,i
);
191 if (len
> (low
+ high
) /2)
205 EXPORT_SYMBOL_GPL(ir_decode_pulsedistance
);
207 /* decode raw samples, biphase coding, used by rc5 for example */
208 int ir_decode_biphase(u32
*samples
, int count
, int low
, int high
)
210 int i
,last
,bit
,len
,flips
;
213 /* find start bit (1) */
214 for (i
= 0; i
< 32; i
++) {
215 bit
= getbit(samples
,i
);
224 for (; i
< count
* 32; i
++) {
230 bit
= getbit(samples
,i
);
247 EXPORT_SYMBOL_GPL(ir_decode_biphase
);
249 /* RC5 decoding stuff, moved from bttv-input.c to share it with
252 /* decode raw bit pattern to RC5 code */
253 u32
ir_rc5_decode(unsigned int code
)
255 unsigned int org_code
= code
;
257 unsigned int rc5
= 0;
260 for (i
= 0; i
< 14; ++i
) {
273 IR_dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code
);
277 IR_dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
278 "instr=%x\n", rc5
, org_code
, RC5_START(rc5
),
279 RC5_TOGGLE(rc5
), RC5_ADDR(rc5
), RC5_INSTR(rc5
));
282 EXPORT_SYMBOL_GPL(ir_rc5_decode
);
284 void ir_rc5_timer_end(unsigned long data
)
286 struct card_ir
*ir
= (struct card_ir
*)data
;
288 unsigned long current_jiffies
, timeout
;
293 current_jiffies
= jiffies
;
294 do_gettimeofday(&tv
);
296 /* avoid overflow with gap >1s */
297 if (tv
.tv_sec
- ir
->base_time
.tv_sec
> 1) {
300 gap
= 1000000 * (tv
.tv_sec
- ir
->base_time
.tv_sec
) +
301 tv
.tv_usec
- ir
->base_time
.tv_usec
;
304 /* signal we're ready to start a new code */
307 /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
309 IR_dprintk(1, "ir-common: spurious timer_end\n");
313 if (ir
->last_bit
< 20) {
314 /* ignore spurious codes (caused by light/other remotes) */
315 IR_dprintk(1, "ir-common: short code: %x\n", ir
->code
);
317 ir
->code
= (ir
->code
<< ir
->shift_by
) | 1;
318 rc5
= ir_rc5_decode(ir
->code
);
320 /* two start bits? */
321 if (RC5_START(rc5
) != ir
->start
) {
322 IR_dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5
));
325 } else if (RC5_ADDR(rc5
) == ir
->addr
) {
326 u32 toggle
= RC5_TOGGLE(rc5
);
327 u32 instr
= RC5_INSTR(rc5
);
329 /* Good code, decide if repeat/repress */
330 if (toggle
!= RC5_TOGGLE(ir
->last_rc5
) ||
331 instr
!= RC5_INSTR(ir
->last_rc5
)) {
332 IR_dprintk(1, "ir-common: instruction %x, toggle %x\n", instr
,
334 ir_input_nokey(ir
->dev
, &ir
->ir
);
335 ir_input_keydown(ir
->dev
, &ir
->ir
, instr
);
338 /* Set/reset key-up timer */
339 timeout
= current_jiffies
+
340 msecs_to_jiffies(ir
->rc5_key_timeout
);
341 mod_timer(&ir
->timer_keyup
, timeout
);
343 /* Save code for repeat test */
348 EXPORT_SYMBOL_GPL(ir_rc5_timer_end
);
350 void ir_rc5_timer_keyup(unsigned long data
)
352 struct card_ir
*ir
= (struct card_ir
*)data
;
354 IR_dprintk(1, "ir-common: key released\n");
355 ir_input_nokey(ir
->dev
, &ir
->ir
);
357 EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup
);