1 /* ir-rc6-decoder.c - A decoder for the RC6 IR protocol
3 * Copyright (C) 2010 by David Härdeman <david@hardeman.nu>
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.
15 #include "rc-core-priv.h"
16 #include <linux/module.h>
19 * This decoder currently supports:
20 * RC6-0-16 (standard toggle bit in header)
21 * RC6-6A-20 (no toggle bit)
22 * RC6-6A-24 (no toggle bit)
23 * RC6-6A-32 (MCE version with toggle bit in body)
26 #define RC6_UNIT 444444 /* nanosecs */
27 #define RC6_HEADER_NBITS 4 /* not including toggle bit */
28 #define RC6_0_NBITS 16
29 #define RC6_6A_32_NBITS 32
30 #define RC6_6A_NBITS 128 /* Variable 8..128 */
31 #define RC6_PREFIX_PULSE (6 * RC6_UNIT)
32 #define RC6_PREFIX_SPACE (2 * RC6_UNIT)
33 #define RC6_BIT_START (1 * RC6_UNIT)
34 #define RC6_BIT_END (1 * RC6_UNIT)
35 #define RC6_TOGGLE_START (2 * RC6_UNIT)
36 #define RC6_TOGGLE_END (2 * RC6_UNIT)
37 #define RC6_SUFFIX_SPACE (6 * RC6_UNIT)
38 #define RC6_MODE_MASK 0x07 /* for the header bits */
39 #define RC6_STARTBIT_MASK 0x08 /* for the header bits */
40 #define RC6_6A_MCE_TOGGLE_MASK 0x8000 /* for the body bits */
41 #define RC6_6A_LCC_MASK 0xffff0000 /* RC6-6A-32 long customer code mask */
42 #define RC6_6A_MCE_CC 0x800f0000 /* MCE customer code */
44 #define CHAR_BIT 8 /* Normally in <limits.h> */
56 STATE_HEADER_BIT_START
,
65 static enum rc6_mode
rc6_mode(struct rc6_dec
*data
)
67 switch (data
->header
& RC6_MODE_MASK
) {
75 return RC6_MODE_UNKNOWN
;
80 * ir_rc6_decode() - Decode one RC6 pulse or space
81 * @dev: the struct rc_dev descriptor of the device
82 * @ev: the struct ir_raw_event descriptor of the pulse/space
84 * This function returns -EINVAL if the pulse violates the state machine
86 static int ir_rc6_decode(struct rc_dev
*dev
, struct ir_raw_event ev
)
88 struct rc6_dec
*data
= &dev
->raw
->rc6
;
91 enum rc_type protocol
;
93 if (!is_timing_event(ev
)) {
95 data
->state
= STATE_INACTIVE
;
99 if (!geq_margin(ev
.duration
, RC6_UNIT
, RC6_UNIT
/ 2))
103 IR_dprintk(2, "RC6 decode started at state %i (%uus %s)\n",
104 data
->state
, TO_US(ev
.duration
), TO_STR(ev
.pulse
));
106 if (!geq_margin(ev
.duration
, RC6_UNIT
, RC6_UNIT
/ 2))
109 switch (data
->state
) {
115 /* Note: larger margin on first pulse since each RC6_UNIT
116 is quite short and some hardware takes some time to
117 adjust to the signal */
118 if (!eq_margin(ev
.duration
, RC6_PREFIX_PULSE
, RC6_UNIT
))
121 data
->state
= STATE_PREFIX_SPACE
;
125 case STATE_PREFIX_SPACE
:
129 if (!eq_margin(ev
.duration
, RC6_PREFIX_SPACE
, RC6_UNIT
/ 2))
132 data
->state
= STATE_HEADER_BIT_START
;
136 case STATE_HEADER_BIT_START
:
137 if (!eq_margin(ev
.duration
, RC6_BIT_START
, RC6_UNIT
/ 2))
144 data
->state
= STATE_HEADER_BIT_END
;
147 case STATE_HEADER_BIT_END
:
148 if (!is_transition(&ev
, &dev
->raw
->prev_ev
))
151 if (data
->count
== RC6_HEADER_NBITS
)
152 data
->state
= STATE_TOGGLE_START
;
154 data
->state
= STATE_HEADER_BIT_START
;
156 decrease_duration(&ev
, RC6_BIT_END
);
159 case STATE_TOGGLE_START
:
160 if (!eq_margin(ev
.duration
, RC6_TOGGLE_START
, RC6_UNIT
/ 2))
163 data
->toggle
= ev
.pulse
;
164 data
->state
= STATE_TOGGLE_END
;
167 case STATE_TOGGLE_END
:
168 if (!is_transition(&ev
, &dev
->raw
->prev_ev
) ||
169 !geq_margin(ev
.duration
, RC6_TOGGLE_END
, RC6_UNIT
/ 2))
172 if (!(data
->header
& RC6_STARTBIT_MASK
)) {
173 IR_dprintk(1, "RC6 invalid start bit\n");
177 data
->state
= STATE_BODY_BIT_START
;
178 decrease_duration(&ev
, RC6_TOGGLE_END
);
182 switch (rc6_mode(data
)) {
184 data
->wanted_bits
= RC6_0_NBITS
;
187 data
->wanted_bits
= RC6_6A_NBITS
;
190 IR_dprintk(1, "RC6 unknown mode\n");
195 case STATE_BODY_BIT_START
:
196 if (eq_margin(ev
.duration
, RC6_BIT_START
, RC6_UNIT
/ 2)) {
197 /* Discard LSB's that won't fit in data->body */
198 if (data
->count
++ < CHAR_BIT
* sizeof data
->body
) {
203 data
->state
= STATE_BODY_BIT_END
;
205 } else if (RC6_MODE_6A
== rc6_mode(data
) && !ev
.pulse
&&
206 geq_margin(ev
.duration
, RC6_SUFFIX_SPACE
, RC6_UNIT
/ 2)) {
207 data
->state
= STATE_FINISHED
;
212 case STATE_BODY_BIT_END
:
213 if (!is_transition(&ev
, &dev
->raw
->prev_ev
))
216 if (data
->count
== data
->wanted_bits
)
217 data
->state
= STATE_FINISHED
;
219 data
->state
= STATE_BODY_BIT_START
;
221 decrease_duration(&ev
, RC6_BIT_END
);
228 switch (rc6_mode(data
)) {
230 scancode
= data
->body
;
231 toggle
= data
->toggle
;
232 protocol
= RC_TYPE_RC6_0
;
233 IR_dprintk(1, "RC6(0) scancode 0x%04x (toggle: %u)\n",
238 if (data
->count
> CHAR_BIT
* sizeof data
->body
) {
239 IR_dprintk(1, "RC6 too many (%u) data bits\n",
244 scancode
= data
->body
;
245 switch (data
->count
) {
247 protocol
= RC_TYPE_RC6_6A_20
;
251 protocol
= RC_TYPE_RC6_6A_24
;
255 if ((scancode
& RC6_6A_LCC_MASK
) == RC6_6A_MCE_CC
) {
256 protocol
= RC_TYPE_RC6_MCE
;
257 toggle
= !!(scancode
& RC6_6A_MCE_TOGGLE_MASK
);
258 scancode
&= ~RC6_6A_MCE_TOGGLE_MASK
;
260 protocol
= RC_TYPE_RC6_6A_32
;
265 IR_dprintk(1, "RC6(6A) unsupported length\n");
269 IR_dprintk(1, "RC6(6A) proto 0x%04x, scancode 0x%08x (toggle: %u)\n",
270 protocol
, scancode
, toggle
);
273 IR_dprintk(1, "RC6 unknown mode\n");
277 rc_keydown(dev
, protocol
, scancode
, toggle
);
278 data
->state
= STATE_INACTIVE
;
283 IR_dprintk(1, "RC6 decode failed at state %i (%uus %s)\n",
284 data
->state
, TO_US(ev
.duration
), TO_STR(ev
.pulse
));
285 data
->state
= STATE_INACTIVE
;
289 static const struct ir_raw_timings_manchester ir_rc6_timings
[4] = {
291 .leader
= RC6_PREFIX_PULSE
,
292 .pulse_space_start
= 0,
295 .trailer_space
= RC6_PREFIX_SPACE
,
302 .clock
= RC6_UNIT
* 2,
308 .trailer_space
= RC6_SUFFIX_SPACE
,
313 * ir_rc6_encode() - Encode a scancode as a stream of raw events
315 * @protocol: protocol to encode
316 * @scancode: scancode to encode
317 * @events: array of raw ir events to write into
318 * @max: maximum size of @events
320 * Returns: The number of events written.
321 * -ENOBUFS if there isn't enough space in the array to fit the
322 * encoding. In this case all @max events will have been written.
323 * -EINVAL if the scancode is ambiguous or invalid.
325 static int ir_rc6_encode(enum rc_type protocol
, u32 scancode
,
326 struct ir_raw_event
*events
, unsigned int max
)
329 struct ir_raw_event
*e
= events
;
331 if (protocol
== RC_TYPE_RC6_0
) {
332 /* Modulate the preamble */
333 ret
= ir_raw_gen_manchester(&e
, max
, &ir_rc6_timings
[0], 0, 0);
337 /* Modulate the header (Start Bit & Mode-0) */
338 ret
= ir_raw_gen_manchester(&e
, max
- (e
- events
),
340 RC6_HEADER_NBITS
, (1 << 3));
344 /* Modulate Trailer Bit */
345 ret
= ir_raw_gen_manchester(&e
, max
- (e
- events
),
346 &ir_rc6_timings
[2], 1, 0);
350 /* Modulate rest of the data */
351 ret
= ir_raw_gen_manchester(&e
, max
- (e
- events
),
352 &ir_rc6_timings
[3], RC6_0_NBITS
,
361 case RC_TYPE_RC6_MCE
:
362 case RC_TYPE_RC6_6A_32
:
365 case RC_TYPE_RC6_6A_24
:
368 case RC_TYPE_RC6_6A_20
:
375 /* Modulate the preamble */
376 ret
= ir_raw_gen_manchester(&e
, max
, &ir_rc6_timings
[0], 0, 0);
380 /* Modulate the header (Start Bit & Header-version 6 */
381 ret
= ir_raw_gen_manchester(&e
, max
- (e
- events
),
383 RC6_HEADER_NBITS
, (1 << 3 | 6));
387 /* Modulate Trailer Bit */
388 ret
= ir_raw_gen_manchester(&e
, max
- (e
- events
),
389 &ir_rc6_timings
[2], 1, 0);
393 /* Modulate rest of the data */
394 ret
= ir_raw_gen_manchester(&e
, max
- (e
- events
),
405 static struct ir_raw_handler rc6_handler
= {
406 .protocols
= RC_BIT_RC6_0
| RC_BIT_RC6_6A_20
|
407 RC_BIT_RC6_6A_24
| RC_BIT_RC6_6A_32
|
409 .decode
= ir_rc6_decode
,
410 .encode
= ir_rc6_encode
,
413 static int __init
ir_rc6_decode_init(void)
415 ir_raw_handler_register(&rc6_handler
);
417 printk(KERN_INFO
"IR RC6 protocol handler initialized\n");
421 static void __exit
ir_rc6_decode_exit(void)
423 ir_raw_handler_unregister(&rc6_handler
);
426 module_init(ir_rc6_decode_init
);
427 module_exit(ir_rc6_decode_exit
);
429 MODULE_LICENSE("GPL");
430 MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
431 MODULE_DESCRIPTION("RC6 IR protocol decoder");