1 // SPDX-License-Identifier: GPL-2.0
2 // ir-rc5-decoder.c - decoder for RC5(x) and StreamZap protocols
4 // Copyright (C) 2010 by Mauro Carvalho Chehab
5 // Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com>
8 * This decoder handles the 14 bit RC5 protocol, 15 bit "StreamZap" protocol
9 * and 20 bit RC5x protocol.
12 #include "rc-core-priv.h"
13 #include <linux/module.h>
16 #define RC5_SZ_NBITS 15
18 #define CHECK_RC5X_NBITS 8
19 #define RC5_UNIT 888888 /* ns */
20 #define RC5_BIT_START (1 * RC5_UNIT)
21 #define RC5_BIT_END (1 * RC5_UNIT)
22 #define RC5X_SPACE (4 * RC5_UNIT)
23 #define RC5_TRAILER (6 * RC5_UNIT) /* In reality, approx 100 */
34 * ir_rc5_decode() - Decode one RC-5 pulse or space
35 * @dev: the struct rc_dev descriptor of the device
36 * @ev: the struct ir_raw_event descriptor of the pulse/space
38 * This function returns -EINVAL if the pulse violates the state machine
40 static int ir_rc5_decode(struct rc_dev
*dev
, struct ir_raw_event ev
)
42 struct rc5_dec
*data
= &dev
->raw
->rc5
;
45 enum rc_proto protocol
;
47 if (!is_timing_event(ev
)) {
49 data
->state
= STATE_INACTIVE
;
53 if (!geq_margin(ev
.duration
, RC5_UNIT
, RC5_UNIT
/ 2))
57 IR_dprintk(2, "RC5(x/sz) decode started at state %i (%uus %s)\n",
58 data
->state
, TO_US(ev
.duration
), TO_STR(ev
.pulse
));
60 if (!geq_margin(ev
.duration
, RC5_UNIT
, RC5_UNIT
/ 2))
63 switch (data
->state
) {
69 data
->state
= STATE_BIT_START
;
71 decrease_duration(&ev
, RC5_BIT_START
);
75 if (!ev
.pulse
&& geq_margin(ev
.duration
, RC5_TRAILER
, RC5_UNIT
/ 2)) {
76 data
->state
= STATE_FINISHED
;
80 if (!eq_margin(ev
.duration
, RC5_BIT_START
, RC5_UNIT
/ 2))
87 data
->state
= STATE_BIT_END
;
91 if (!is_transition(&ev
, &dev
->raw
->prev_ev
))
94 if (data
->count
== CHECK_RC5X_NBITS
)
95 data
->state
= STATE_CHECK_RC5X
;
97 data
->state
= STATE_BIT_START
;
99 decrease_duration(&ev
, RC5_BIT_END
);
102 case STATE_CHECK_RC5X
:
103 if (!ev
.pulse
&& geq_margin(ev
.duration
, RC5X_SPACE
, RC5_UNIT
/ 2)) {
104 data
->is_rc5x
= true;
105 decrease_duration(&ev
, RC5X_SPACE
);
107 data
->is_rc5x
= false;
108 data
->state
= STATE_BIT_START
;
115 if (data
->is_rc5x
&& data
->count
== RC5X_NBITS
) {
117 u8 xdata
, command
, system
;
118 if (!(dev
->enabled_protocols
& RC_PROTO_BIT_RC5X_20
)) {
119 data
->state
= STATE_INACTIVE
;
122 xdata
= (data
->bits
& 0x0003F) >> 0;
123 command
= (data
->bits
& 0x00FC0) >> 6;
124 system
= (data
->bits
& 0x1F000) >> 12;
125 toggle
= (data
->bits
& 0x20000) ? 1 : 0;
126 command
+= (data
->bits
& 0x40000) ? 0 : 0x40;
127 scancode
= system
<< 16 | command
<< 8 | xdata
;
128 protocol
= RC_PROTO_RC5X_20
;
130 } else if (!data
->is_rc5x
&& data
->count
== RC5_NBITS
) {
133 if (!(dev
->enabled_protocols
& RC_PROTO_BIT_RC5
)) {
134 data
->state
= STATE_INACTIVE
;
137 command
= (data
->bits
& 0x0003F) >> 0;
138 system
= (data
->bits
& 0x007C0) >> 6;
139 toggle
= (data
->bits
& 0x00800) ? 1 : 0;
140 command
+= (data
->bits
& 0x01000) ? 0 : 0x40;
141 scancode
= system
<< 8 | command
;
142 protocol
= RC_PROTO_RC5
;
144 } else if (!data
->is_rc5x
&& data
->count
== RC5_SZ_NBITS
) {
147 if (!(dev
->enabled_protocols
& RC_PROTO_BIT_RC5_SZ
)) {
148 data
->state
= STATE_INACTIVE
;
151 command
= (data
->bits
& 0x0003F) >> 0;
152 system
= (data
->bits
& 0x02FC0) >> 6;
153 toggle
= (data
->bits
& 0x01000) ? 1 : 0;
154 scancode
= system
<< 6 | command
;
155 protocol
= RC_PROTO_RC5_SZ
;
160 IR_dprintk(1, "RC5(x/sz) scancode 0x%06x (p: %u, t: %u)\n",
161 scancode
, protocol
, toggle
);
163 rc_keydown(dev
, protocol
, scancode
, toggle
);
164 data
->state
= STATE_INACTIVE
;
169 IR_dprintk(1, "RC5(x/sz) decode failed at state %i count %d (%uus %s)\n",
170 data
->state
, data
->count
, TO_US(ev
.duration
), TO_STR(ev
.pulse
));
171 data
->state
= STATE_INACTIVE
;
175 static const struct ir_raw_timings_manchester ir_rc5_timings
= {
176 .leader_pulse
= RC5_UNIT
,
178 .trailer_space
= RC5_UNIT
* 10,
181 static const struct ir_raw_timings_manchester ir_rc5x_timings
[2] = {
183 .leader_pulse
= RC5_UNIT
,
185 .trailer_space
= RC5X_SPACE
,
189 .trailer_space
= RC5_UNIT
* 10,
193 static const struct ir_raw_timings_manchester ir_rc5_sz_timings
= {
194 .leader_pulse
= RC5_UNIT
,
196 .trailer_space
= RC5_UNIT
* 10,
200 * ir_rc5_encode() - Encode a scancode as a stream of raw events
202 * @protocol: protocol variant to encode
203 * @scancode: scancode to encode
204 * @events: array of raw ir events to write into
205 * @max: maximum size of @events
207 * Returns: The number of events written.
208 * -ENOBUFS if there isn't enough space in the array to fit the
209 * encoding. In this case all @max events will have been written.
210 * -EINVAL if the scancode is ambiguous or invalid.
212 static int ir_rc5_encode(enum rc_proto protocol
, u32 scancode
,
213 struct ir_raw_event
*events
, unsigned int max
)
216 struct ir_raw_event
*e
= events
;
217 unsigned int data
, xdata
, command
, commandx
, system
, pre_space_data
;
219 /* Detect protocol and convert scancode to raw data */
220 if (protocol
== RC_PROTO_RC5
) {
221 /* decode scancode */
222 command
= (scancode
& 0x003f) >> 0;
223 commandx
= (scancode
& 0x0040) >> 6;
224 system
= (scancode
& 0x1f00) >> 8;
226 data
= !commandx
<< 12 | system
<< 6 | command
;
228 /* First bit is encoded by leader_pulse */
229 ret
= ir_raw_gen_manchester(&e
, max
, &ir_rc5_timings
,
230 RC5_NBITS
- 1, data
);
233 } else if (protocol
== RC_PROTO_RC5X_20
) {
234 /* decode scancode */
235 xdata
= (scancode
& 0x00003f) >> 0;
236 command
= (scancode
& 0x003f00) >> 8;
237 commandx
= !(scancode
& 0x004000);
238 system
= (scancode
& 0x1f0000) >> 16;
241 data
= commandx
<< 18 | system
<< 12 | command
<< 6 | xdata
;
243 /* First bit is encoded by leader_pulse */
244 pre_space_data
= data
>> (RC5X_NBITS
- CHECK_RC5X_NBITS
);
245 ret
= ir_raw_gen_manchester(&e
, max
, &ir_rc5x_timings
[0],
246 CHECK_RC5X_NBITS
- 1,
250 ret
= ir_raw_gen_manchester(&e
, max
- (e
- events
),
252 RC5X_NBITS
- CHECK_RC5X_NBITS
,
256 } else if (protocol
== RC_PROTO_RC5_SZ
) {
257 /* RC5-SZ scancode is raw enough for Manchester as it is */
258 /* First bit is encoded by leader_pulse */
259 ret
= ir_raw_gen_manchester(&e
, max
, &ir_rc5_sz_timings
,
271 static struct ir_raw_handler rc5_handler
= {
272 .protocols
= RC_PROTO_BIT_RC5
| RC_PROTO_BIT_RC5X_20
|
274 .decode
= ir_rc5_decode
,
275 .encode
= ir_rc5_encode
,
279 static int __init
ir_rc5_decode_init(void)
281 ir_raw_handler_register(&rc5_handler
);
283 printk(KERN_INFO
"IR RC5(x/sz) protocol handler initialized\n");
287 static void __exit
ir_rc5_decode_exit(void)
289 ir_raw_handler_unregister(&rc5_handler
);
292 module_init(ir_rc5_decode_init
);
293 module_exit(ir_rc5_decode_exit
);
295 MODULE_LICENSE("GPL v2");
296 MODULE_AUTHOR("Mauro Carvalho Chehab and Jarod Wilson");
297 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
298 MODULE_DESCRIPTION("RC5(x/sz) IR protocol decoder");