1 /* ir-rc5-decoder.c - handle RC5(x) IR Pulse/Space protocol
3 * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 * This code handles 14 bits RC5 protocols and 20 bits RC5x protocols.
17 * There are other variants that use a different number of bits.
18 * This is currently unsupported.
19 * It considers a carrier of 36 kHz, with a total of 14/20 bits, where
20 * the first two bits are start bits, and a third one is a filing bit
23 #include "rc-core-priv.h"
24 #include <linux/module.h>
28 #define CHECK_RC5X_NBITS 8
29 #define RC5_UNIT 888888 /* ns */
30 #define RC5_BIT_START (1 * RC5_UNIT)
31 #define RC5_BIT_END (1 * RC5_UNIT)
32 #define RC5X_SPACE (4 * RC5_UNIT)
43 * ir_rc5_decode() - Decode one RC-5 pulse or space
44 * @dev: the struct rc_dev descriptor of the device
45 * @ev: the struct ir_raw_event descriptor of the pulse/space
47 * This function returns -EINVAL if the pulse violates the state machine
49 static int ir_rc5_decode(struct rc_dev
*dev
, struct ir_raw_event ev
)
51 struct rc5_dec
*data
= &dev
->raw
->rc5
;
55 if (!(dev
->raw
->enabled_protocols
& (RC_BIT_RC5
| RC_BIT_RC5X
)))
58 if (!is_timing_event(ev
)) {
60 data
->state
= STATE_INACTIVE
;
64 if (!geq_margin(ev
.duration
, RC5_UNIT
, RC5_UNIT
/ 2))
68 IR_dprintk(2, "RC5(x) decode started at state %i (%uus %s)\n",
69 data
->state
, TO_US(ev
.duration
), TO_STR(ev
.pulse
));
71 if (!geq_margin(ev
.duration
, RC5_UNIT
, RC5_UNIT
/ 2))
74 switch (data
->state
) {
80 data
->state
= STATE_BIT_START
;
82 /* We just need enough bits to get to STATE_CHECK_RC5X */
83 data
->wanted_bits
= RC5X_NBITS
;
84 decrease_duration(&ev
, RC5_BIT_START
);
88 if (!eq_margin(ev
.duration
, RC5_BIT_START
, RC5_UNIT
/ 2))
95 data
->state
= STATE_BIT_END
;
99 if (!is_transition(&ev
, &dev
->raw
->prev_ev
))
102 if (data
->count
== data
->wanted_bits
)
103 data
->state
= STATE_FINISHED
;
104 else if (data
->count
== CHECK_RC5X_NBITS
)
105 data
->state
= STATE_CHECK_RC5X
;
107 data
->state
= STATE_BIT_START
;
109 decrease_duration(&ev
, RC5_BIT_END
);
112 case STATE_CHECK_RC5X
:
113 if (!ev
.pulse
&& geq_margin(ev
.duration
, RC5X_SPACE
, RC5_UNIT
/ 2)) {
115 data
->wanted_bits
= RC5X_NBITS
;
116 decrease_duration(&ev
, RC5X_SPACE
);
119 data
->wanted_bits
= RC5_NBITS
;
121 data
->state
= STATE_BIT_START
;
128 if (data
->wanted_bits
== RC5X_NBITS
) {
130 u8 xdata
, command
, system
;
131 if (!(dev
->raw
->enabled_protocols
& RC_BIT_RC5X
)) {
132 data
->state
= STATE_INACTIVE
;
135 xdata
= (data
->bits
& 0x0003F) >> 0;
136 command
= (data
->bits
& 0x00FC0) >> 6;
137 system
= (data
->bits
& 0x1F000) >> 12;
138 toggle
= (data
->bits
& 0x20000) ? 1 : 0;
139 command
+= (data
->bits
& 0x01000) ? 0 : 0x40;
140 scancode
= system
<< 16 | command
<< 8 | xdata
;
142 IR_dprintk(1, "RC5X scancode 0x%06x (toggle: %u)\n",
148 if (!(dev
->raw
->enabled_protocols
& RC_BIT_RC5
)) {
149 data
->state
= STATE_INACTIVE
;
152 command
= (data
->bits
& 0x0003F) >> 0;
153 system
= (data
->bits
& 0x007C0) >> 6;
154 toggle
= (data
->bits
& 0x00800) ? 1 : 0;
155 command
+= (data
->bits
& 0x01000) ? 0 : 0x40;
156 scancode
= system
<< 8 | command
;
158 IR_dprintk(1, "RC5 scancode 0x%04x (toggle: %u)\n",
162 rc_keydown(dev
, scancode
, toggle
);
163 data
->state
= STATE_INACTIVE
;
168 IR_dprintk(1, "RC5(x) decode failed at state %i (%uus %s)\n",
169 data
->state
, TO_US(ev
.duration
), TO_STR(ev
.pulse
));
170 data
->state
= STATE_INACTIVE
;
174 static struct ir_raw_handler rc5_handler
= {
175 .protocols
= RC_BIT_RC5
| RC_BIT_RC5X
,
176 .decode
= ir_rc5_decode
,
179 static int __init
ir_rc5_decode_init(void)
181 ir_raw_handler_register(&rc5_handler
);
183 printk(KERN_INFO
"IR RC5(x) protocol handler initialized\n");
187 static void __exit
ir_rc5_decode_exit(void)
189 ir_raw_handler_unregister(&rc5_handler
);
192 module_init(ir_rc5_decode_init
);
193 module_exit(ir_rc5_decode_exit
);
195 MODULE_LICENSE("GPL");
196 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
197 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
198 MODULE_DESCRIPTION("RC5(x) IR protocol decoder");