1 // SPDX-License-Identifier: GPL-2.0-only
2 /* ir-rc6-decoder.c - A decoder for the RC6 IR protocol
7 #include "rc-core-priv.h"
8 #include <linux/module.h>
11 * This decoder currently supports:
12 * RC6-0-16 (standard toggle bit in header)
13 * RC6-6A-20 (no toggle bit)
14 * RC6-6A-24 (no toggle bit)
15 * RC6-6A-32 (MCE version with toggle bit in body)
18 #define RC6_UNIT 444 /* microseconds */
19 #define RC6_HEADER_NBITS 4 /* not including toggle bit */
20 #define RC6_0_NBITS 16
21 #define RC6_6A_32_NBITS 32
22 #define RC6_6A_NBITS 128 /* Variable 8..128 */
23 #define RC6_PREFIX_PULSE (6 * RC6_UNIT)
24 #define RC6_PREFIX_SPACE (2 * RC6_UNIT)
25 #define RC6_BIT_START (1 * RC6_UNIT)
26 #define RC6_BIT_END (1 * RC6_UNIT)
27 #define RC6_TOGGLE_START (2 * RC6_UNIT)
28 #define RC6_TOGGLE_END (2 * RC6_UNIT)
29 #define RC6_SUFFIX_SPACE (6 * RC6_UNIT)
30 #define RC6_MODE_MASK 0x07 /* for the header bits */
31 #define RC6_STARTBIT_MASK 0x08 /* for the header bits */
32 #define RC6_6A_MCE_TOGGLE_MASK 0x8000 /* for the body bits */
33 #define RC6_6A_LCC_MASK 0xffff0000 /* RC6-6A-32 long customer code mask */
34 #define RC6_6A_MCE_CC 0x800f0000 /* MCE customer code */
35 #define RC6_6A_ZOTAC_CC 0x80340000 /* Zotac customer code */
36 #define RC6_6A_KATHREIN_CC 0x80460000 /* Kathrein RCU-676 customer code */
38 #define CHAR_BIT 8 /* Normally in <limits.h> */
50 STATE_HEADER_BIT_START,
59 static enum rc6_mode rc6_mode(struct rc6_dec *data)
61 switch (data->header & RC6_MODE_MASK) {
69 return RC6_MODE_UNKNOWN;
74 * ir_rc6_decode() - Decode one RC6 pulse or space
75 * @dev: the struct rc_dev descriptor of the device
76 * @ev: the struct ir_raw_event descriptor of the pulse/space
78 * This function returns -EINVAL if the pulse violates the state machine
80 static int ir_rc6_decode(struct rc_dev *dev, struct ir_raw_event ev)
82 struct rc6_dec *data = &dev->raw->rc6;
85 enum rc_proto protocol;
87 if (!is_timing_event(ev)) {
89 data->state = STATE_INACTIVE;
93 if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
97 dev_dbg(&dev->dev, "RC6 decode started at state %i (%uus %s)\n",
98 data->state, ev.duration, TO_STR(ev.pulse));
100 if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
103 switch (data->state) {
109 /* Note: larger margin on first pulse since each RC6_UNIT
110 is quite short and some hardware takes some time to
111 adjust to the signal */
112 if (!eq_margin(ev.duration, RC6_PREFIX_PULSE, RC6_UNIT))
115 data->state = STATE_PREFIX_SPACE;
119 case STATE_PREFIX_SPACE:
123 if (!eq_margin(ev.duration, RC6_PREFIX_SPACE, RC6_UNIT / 2))
126 data->state = STATE_HEADER_BIT_START;
130 case STATE_HEADER_BIT_START:
131 if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2))
138 data->state = STATE_HEADER_BIT_END;
141 case STATE_HEADER_BIT_END:
142 if (data->count == RC6_HEADER_NBITS)
143 data->state = STATE_TOGGLE_START;
145 data->state = STATE_HEADER_BIT_START;
147 decrease_duration(&ev, RC6_BIT_END);
150 case STATE_TOGGLE_START:
151 if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2))
154 data->toggle = ev.pulse;
155 data->state = STATE_TOGGLE_END;
158 case STATE_TOGGLE_END:
159 if (!(data->header & RC6_STARTBIT_MASK)) {
160 dev_dbg(&dev->dev, "RC6 invalid start bit\n");
164 data->state = STATE_BODY_BIT_START;
165 decrease_duration(&ev, RC6_TOGGLE_END);
169 switch (rc6_mode(data)) {
171 data->wanted_bits = RC6_0_NBITS;
174 data->wanted_bits = RC6_6A_NBITS;
177 dev_dbg(&dev->dev, "RC6 unknown mode\n");
182 case STATE_BODY_BIT_START:
183 if (eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) {
184 /* Discard LSB's that won't fit in data->body */
185 if (data->count++ < CHAR_BIT * sizeof data->body) {
190 data->state = STATE_BODY_BIT_END;
192 } else if (RC6_MODE_6A == rc6_mode(data) && !ev.pulse &&
193 geq_margin(ev.duration, RC6_SUFFIX_SPACE, RC6_UNIT / 2)) {
194 data->state = STATE_FINISHED;
199 case STATE_BODY_BIT_END:
200 if (data->count == data->wanted_bits)
201 data->state = STATE_FINISHED;
203 data->state = STATE_BODY_BIT_START;
205 decrease_duration(&ev, RC6_BIT_END);
212 switch (rc6_mode(data)) {
214 scancode = data->body;
215 toggle = data->toggle;
216 protocol = RC_PROTO_RC6_0;
217 dev_dbg(&dev->dev, "RC6(0) scancode 0x%04x (toggle: %u)\n",
222 if (data->count > CHAR_BIT * sizeof data->body) {
223 dev_dbg(&dev->dev, "RC6 too many (%u) data bits\n",
228 scancode = data->body;
229 switch (data->count) {
231 protocol = RC_PROTO_RC6_6A_20;
235 protocol = RC_PROTO_RC6_6A_24;
239 switch (scancode & RC6_6A_LCC_MASK) {
241 case RC6_6A_KATHREIN_CC:
242 case RC6_6A_ZOTAC_CC:
243 protocol = RC_PROTO_RC6_MCE;
244 toggle = !!(scancode & RC6_6A_MCE_TOGGLE_MASK);
245 scancode &= ~RC6_6A_MCE_TOGGLE_MASK;
248 protocol = RC_PROTO_RC6_6A_32;
254 dev_dbg(&dev->dev, "RC6(6A) unsupported length\n");
258 dev_dbg(&dev->dev, "RC6(6A) proto 0x%04x, scancode 0x%08x (toggle: %u)\n",
259 protocol, scancode, toggle);
262 dev_dbg(&dev->dev, "RC6 unknown mode\n");
266 rc_keydown(dev, protocol, scancode, toggle);
267 data->state = STATE_INACTIVE;
272 dev_dbg(&dev->dev, "RC6 decode failed at state %i (%uus %s)\n",
273 data->state, ev.duration, TO_STR(ev.pulse));
274 data->state = STATE_INACTIVE;
278 static const struct ir_raw_timings_manchester ir_rc6_timings[4] = {
280 .leader_pulse = RC6_PREFIX_PULSE,
281 .leader_space = RC6_PREFIX_SPACE,
286 .clock = RC6_UNIT * 2,
292 .trailer_space = RC6_SUFFIX_SPACE,
297 * ir_rc6_encode() - Encode a scancode as a stream of raw events
299 * @protocol: protocol to encode
300 * @scancode: scancode to encode
301 * @events: array of raw ir events to write into
302 * @max: maximum size of @events
304 * Returns: The number of events written.
305 * -ENOBUFS if there isn't enough space in the array to fit the
306 * encoding. In this case all @max events will have been written.
307 * -EINVAL if the scancode is ambiguous or invalid.
309 static int ir_rc6_encode(enum rc_proto protocol, u32 scancode,
310 struct ir_raw_event *events, unsigned int max)
313 struct ir_raw_event *e = events;
315 if (protocol == RC_PROTO_RC6_0) {
316 /* Modulate the header (Start Bit & Mode-0) */
317 ret = ir_raw_gen_manchester(&e, max - (e - events),
319 RC6_HEADER_NBITS, (1 << 3));
323 /* Modulate Trailer Bit */
324 ret = ir_raw_gen_manchester(&e, max - (e - events),
325 &ir_rc6_timings[1], 1, 0);
329 /* Modulate rest of the data */
330 ret = ir_raw_gen_manchester(&e, max - (e - events),
331 &ir_rc6_timings[2], RC6_0_NBITS,
340 case RC_PROTO_RC6_MCE:
341 case RC_PROTO_RC6_6A_32:
344 case RC_PROTO_RC6_6A_24:
347 case RC_PROTO_RC6_6A_20:
354 /* Modulate the header (Start Bit & Header-version 6 */
355 ret = ir_raw_gen_manchester(&e, max - (e - events),
357 RC6_HEADER_NBITS, (1 << 3 | 6));
361 /* Modulate Trailer Bit */
362 ret = ir_raw_gen_manchester(&e, max - (e - events),
363 &ir_rc6_timings[1], 1, 0);
367 /* Modulate rest of the data */
368 ret = ir_raw_gen_manchester(&e, max - (e - events),
379 static struct ir_raw_handler rc6_handler = {
380 .protocols = RC_PROTO_BIT_RC6_0 | RC_PROTO_BIT_RC6_6A_20 |
381 RC_PROTO_BIT_RC6_6A_24 | RC_PROTO_BIT_RC6_6A_32 |
382 RC_PROTO_BIT_RC6_MCE,
383 .decode = ir_rc6_decode,
384 .encode = ir_rc6_encode,
386 .min_timeout = RC6_SUFFIX_SPACE,
389 static int __init ir_rc6_decode_init(void)
391 ir_raw_handler_register(&rc6_handler);
393 printk(KERN_INFO "IR RC6 protocol handler initialized\n");
397 static void __exit ir_rc6_decode_exit(void)
399 ir_raw_handler_unregister(&rc6_handler);
402 module_init(ir_rc6_decode_init);
403 module_exit(ir_rc6_decode_exit);
405 MODULE_LICENSE("GPL");
407 MODULE_DESCRIPTION("RC6 IR protocol decoder");