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 dev_dbg(&dev
->dev
, "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 (data
->count
== CHECK_RC5X_NBITS
)
92 data
->state
= STATE_CHECK_RC5X
;
94 data
->state
= STATE_BIT_START
;
96 decrease_duration(&ev
, RC5_BIT_END
);
99 case STATE_CHECK_RC5X
:
100 if (!ev
.pulse
&& geq_margin(ev
.duration
, RC5X_SPACE
, RC5_UNIT
/ 2)) {
101 data
->is_rc5x
= true;
102 decrease_duration(&ev
, RC5X_SPACE
);
104 data
->is_rc5x
= false;
105 data
->state
= STATE_BIT_START
;
112 if (data
->is_rc5x
&& data
->count
== RC5X_NBITS
) {
114 u8 xdata
, command
, system
;
115 if (!(dev
->enabled_protocols
& RC_PROTO_BIT_RC5X_20
)) {
116 data
->state
= STATE_INACTIVE
;
119 xdata
= (data
->bits
& 0x0003F) >> 0;
120 command
= (data
->bits
& 0x00FC0) >> 6;
121 system
= (data
->bits
& 0x1F000) >> 12;
122 toggle
= (data
->bits
& 0x20000) ? 1 : 0;
123 command
+= (data
->bits
& 0x40000) ? 0 : 0x40;
124 scancode
= system
<< 16 | command
<< 8 | xdata
;
125 protocol
= RC_PROTO_RC5X_20
;
127 } else if (!data
->is_rc5x
&& data
->count
== RC5_NBITS
) {
130 if (!(dev
->enabled_protocols
& RC_PROTO_BIT_RC5
)) {
131 data
->state
= STATE_INACTIVE
;
134 command
= (data
->bits
& 0x0003F) >> 0;
135 system
= (data
->bits
& 0x007C0) >> 6;
136 toggle
= (data
->bits
& 0x00800) ? 1 : 0;
137 command
+= (data
->bits
& 0x01000) ? 0 : 0x40;
138 scancode
= system
<< 8 | command
;
139 protocol
= RC_PROTO_RC5
;
141 } else if (!data
->is_rc5x
&& data
->count
== RC5_SZ_NBITS
) {
144 if (!(dev
->enabled_protocols
& RC_PROTO_BIT_RC5_SZ
)) {
145 data
->state
= STATE_INACTIVE
;
148 command
= (data
->bits
& 0x0003F) >> 0;
149 system
= (data
->bits
& 0x02FC0) >> 6;
150 toggle
= (data
->bits
& 0x01000) ? 1 : 0;
151 scancode
= system
<< 6 | command
;
152 protocol
= RC_PROTO_RC5_SZ
;
157 dev_dbg(&dev
->dev
, "RC5(x/sz) scancode 0x%06x (p: %u, t: %u)\n",
158 scancode
, protocol
, toggle
);
160 rc_keydown(dev
, protocol
, scancode
, toggle
);
161 data
->state
= STATE_INACTIVE
;
166 dev_dbg(&dev
->dev
, "RC5(x/sz) decode failed at state %i count %d (%uus %s)\n",
167 data
->state
, data
->count
, TO_US(ev
.duration
), TO_STR(ev
.pulse
));
168 data
->state
= STATE_INACTIVE
;
172 static const struct ir_raw_timings_manchester ir_rc5_timings
= {
173 .leader_pulse
= RC5_UNIT
,
175 .trailer_space
= RC5_UNIT
* 10,
178 static const struct ir_raw_timings_manchester ir_rc5x_timings
[2] = {
180 .leader_pulse
= RC5_UNIT
,
182 .trailer_space
= RC5X_SPACE
,
186 .trailer_space
= RC5_UNIT
* 10,
190 static const struct ir_raw_timings_manchester ir_rc5_sz_timings
= {
191 .leader_pulse
= RC5_UNIT
,
193 .trailer_space
= RC5_UNIT
* 10,
197 * ir_rc5_encode() - Encode a scancode as a stream of raw events
199 * @protocol: protocol variant to encode
200 * @scancode: scancode to encode
201 * @events: array of raw ir events to write into
202 * @max: maximum size of @events
204 * Returns: The number of events written.
205 * -ENOBUFS if there isn't enough space in the array to fit the
206 * encoding. In this case all @max events will have been written.
207 * -EINVAL if the scancode is ambiguous or invalid.
209 static int ir_rc5_encode(enum rc_proto protocol
, u32 scancode
,
210 struct ir_raw_event
*events
, unsigned int max
)
213 struct ir_raw_event
*e
= events
;
214 unsigned int data
, xdata
, command
, commandx
, system
, pre_space_data
;
216 /* Detect protocol and convert scancode to raw data */
217 if (protocol
== RC_PROTO_RC5
) {
218 /* decode scancode */
219 command
= (scancode
& 0x003f) >> 0;
220 commandx
= (scancode
& 0x0040) >> 6;
221 system
= (scancode
& 0x1f00) >> 8;
223 data
= !commandx
<< 12 | system
<< 6 | command
;
225 /* First bit is encoded by leader_pulse */
226 ret
= ir_raw_gen_manchester(&e
, max
, &ir_rc5_timings
,
227 RC5_NBITS
- 1, data
);
230 } else if (protocol
== RC_PROTO_RC5X_20
) {
231 /* decode scancode */
232 xdata
= (scancode
& 0x00003f) >> 0;
233 command
= (scancode
& 0x003f00) >> 8;
234 commandx
= !(scancode
& 0x004000);
235 system
= (scancode
& 0x1f0000) >> 16;
238 data
= commandx
<< 18 | system
<< 12 | command
<< 6 | xdata
;
240 /* First bit is encoded by leader_pulse */
241 pre_space_data
= data
>> (RC5X_NBITS
- CHECK_RC5X_NBITS
);
242 ret
= ir_raw_gen_manchester(&e
, max
, &ir_rc5x_timings
[0],
243 CHECK_RC5X_NBITS
- 1,
247 ret
= ir_raw_gen_manchester(&e
, max
- (e
- events
),
249 RC5X_NBITS
- CHECK_RC5X_NBITS
,
253 } else if (protocol
== RC_PROTO_RC5_SZ
) {
254 /* RC5-SZ scancode is raw enough for Manchester as it is */
255 /* First bit is encoded by leader_pulse */
256 ret
= ir_raw_gen_manchester(&e
, max
, &ir_rc5_sz_timings
,
268 static struct ir_raw_handler rc5_handler
= {
269 .protocols
= RC_PROTO_BIT_RC5
| RC_PROTO_BIT_RC5X_20
|
271 .decode
= ir_rc5_decode
,
272 .encode
= ir_rc5_encode
,
274 .min_timeout
= RC5_TRAILER
,
277 static int __init
ir_rc5_decode_init(void)
279 ir_raw_handler_register(&rc5_handler
);
281 printk(KERN_INFO
"IR RC5(x/sz) protocol handler initialized\n");
285 static void __exit
ir_rc5_decode_exit(void)
287 ir_raw_handler_unregister(&rc5_handler
);
290 module_init(ir_rc5_decode_init
);
291 module_exit(ir_rc5_decode_exit
);
293 MODULE_LICENSE("GPL v2");
294 MODULE_AUTHOR("Mauro Carvalho Chehab and Jarod Wilson");
295 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
296 MODULE_DESCRIPTION("RC5(x/sz) IR protocol decoder");