blob: 2435bbd1dbccd61e5de6a77a053ce6a25bbc4fdd [file] [log] [blame]
David Härdeman784a4932010-04-08 20:04:40 -03001/* ir-rc6-decoder.c - A decoder for the RC6 IR protocol
2 *
3 * Copyright (C) 2010 by David Härdeman <david@hardeman.nu>
4 *
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.
8 *
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.
13 */
14
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030015#include "rc-core-priv.h"
David Härdeman784a4932010-04-08 20:04:40 -030016
17/*
18 * This decoder currently supports:
19 * RC6-0-16 (standard toggle bit in header)
20 * RC6-6A-24 (no toggle bit)
21 * RC6-6A-32 (MCE version with toggle bit in body)
22 */
23
24#define RC6_UNIT 444444 /* us */
25#define RC6_HEADER_NBITS 4 /* not including toggle bit */
26#define RC6_0_NBITS 16
27#define RC6_6A_SMALL_NBITS 24
28#define RC6_6A_LARGE_NBITS 32
David Härdemane40b1122010-04-15 18:46:00 -030029#define RC6_PREFIX_PULSE (6 * RC6_UNIT)
30#define RC6_PREFIX_SPACE (2 * RC6_UNIT)
31#define RC6_BIT_START (1 * RC6_UNIT)
32#define RC6_BIT_END (1 * RC6_UNIT)
33#define RC6_TOGGLE_START (2 * RC6_UNIT)
34#define RC6_TOGGLE_END (2 * RC6_UNIT)
David Härdeman784a4932010-04-08 20:04:40 -030035#define RC6_MODE_MASK 0x07 /* for the header bits */
36#define RC6_STARTBIT_MASK 0x08 /* for the header bits */
37#define RC6_6A_MCE_TOGGLE_MASK 0x8000 /* for the body bits */
38
David Härdeman784a4932010-04-08 20:04:40 -030039enum rc6_mode {
40 RC6_MODE_0,
41 RC6_MODE_6A,
42 RC6_MODE_UNKNOWN,
43};
44
45enum rc6_state {
46 STATE_INACTIVE,
47 STATE_PREFIX_SPACE,
48 STATE_HEADER_BIT_START,
49 STATE_HEADER_BIT_END,
50 STATE_TOGGLE_START,
51 STATE_TOGGLE_END,
52 STATE_BODY_BIT_START,
53 STATE_BODY_BIT_END,
54 STATE_FINISHED,
55};
56
David Härdemanc2163692010-06-13 17:29:36 -030057static enum rc6_mode rc6_mode(struct rc6_dec *data)
David Härdeman784a4932010-04-08 20:04:40 -030058{
David Härdeman784a4932010-04-08 20:04:40 -030059 switch (data->header & RC6_MODE_MASK) {
60 case 0:
61 return RC6_MODE_0;
62 case 6:
63 if (!data->toggle)
64 return RC6_MODE_6A;
65 /* fall through */
66 default:
67 return RC6_MODE_UNKNOWN;
68 }
69}
70
71/**
72 * ir_rc6_decode() - Decode one RC6 pulse or space
David Härdemand8b4b582010-10-29 16:08:23 -030073 * @dev: the struct rc_dev descriptor of the device
David Härdemane40b1122010-04-15 18:46:00 -030074 * @ev: the struct ir_raw_event descriptor of the pulse/space
David Härdeman784a4932010-04-08 20:04:40 -030075 *
76 * This function returns -EINVAL if the pulse violates the state machine
77 */
David Härdemand8b4b582010-10-29 16:08:23 -030078static int ir_rc6_decode(struct rc_dev *dev, struct ir_raw_event ev)
David Härdeman784a4932010-04-08 20:04:40 -030079{
David Härdemand8b4b582010-10-29 16:08:23 -030080 struct rc6_dec *data = &dev->raw->rc6;
David Härdeman784a4932010-04-08 20:04:40 -030081 u32 scancode;
82 u8 toggle;
David Härdeman784a4932010-04-08 20:04:40 -030083
David Härdemand8b4b582010-10-29 16:08:23 -030084 if (!(dev->raw->enabled_protocols & IR_TYPE_RC6))
David Härdeman784a4932010-04-08 20:04:40 -030085 return 0;
86
Maxim Levitsky46519182010-10-16 19:56:28 -030087 if (!is_timing_event(ev)) {
88 if (ev.reset)
89 data->state = STATE_INACTIVE;
David Härdeman784a4932010-04-08 20:04:40 -030090 return 0;
91 }
92
David Härdemane40b1122010-04-15 18:46:00 -030093 if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
David Härdeman784a4932010-04-08 20:04:40 -030094 goto out;
95
96again:
David Härdemane40b1122010-04-15 18:46:00 -030097 IR_dprintk(2, "RC6 decode started at state %i (%uus %s)\n",
98 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
David Härdeman784a4932010-04-08 20:04:40 -030099
David Härdemane40b1122010-04-15 18:46:00 -0300100 if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
David Härdeman784a4932010-04-08 20:04:40 -0300101 return 0;
102
103 switch (data->state) {
104
105 case STATE_INACTIVE:
David Härdemane40b1122010-04-15 18:46:00 -0300106 if (!ev.pulse)
107 break;
108
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))
113 break;
114
115 data->state = STATE_PREFIX_SPACE;
116 data->count = 0;
117 return 0;
David Härdeman784a4932010-04-08 20:04:40 -0300118
119 case STATE_PREFIX_SPACE:
David Härdemane40b1122010-04-15 18:46:00 -0300120 if (ev.pulse)
121 break;
122
123 if (!eq_margin(ev.duration, RC6_PREFIX_SPACE, RC6_UNIT / 2))
124 break;
125
126 data->state = STATE_HEADER_BIT_START;
127 return 0;
David Härdeman784a4932010-04-08 20:04:40 -0300128
129 case STATE_HEADER_BIT_START:
David Härdemane40b1122010-04-15 18:46:00 -0300130 if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2))
131 break;
132
133 data->header <<= 1;
134 if (ev.pulse)
135 data->header |= 1;
136 data->count++;
David Härdemane40b1122010-04-15 18:46:00 -0300137 data->state = STATE_HEADER_BIT_END;
138 return 0;
David Härdeman784a4932010-04-08 20:04:40 -0300139
140 case STATE_HEADER_BIT_END:
David Härdemand8b4b582010-10-29 16:08:23 -0300141 if (!is_transition(&ev, &dev->raw->prev_ev))
David Härdemane40b1122010-04-15 18:46:00 -0300142 break;
David Härdeman784a4932010-04-08 20:04:40 -0300143
David Härdemane40b1122010-04-15 18:46:00 -0300144 if (data->count == RC6_HEADER_NBITS)
145 data->state = STATE_TOGGLE_START;
146 else
147 data->state = STATE_HEADER_BIT_START;
148
149 decrease_duration(&ev, RC6_BIT_END);
150 goto again;
David Härdeman784a4932010-04-08 20:04:40 -0300151
152 case STATE_TOGGLE_START:
David Härdemane40b1122010-04-15 18:46:00 -0300153 if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2))
154 break;
155
156 data->toggle = ev.pulse;
David Härdemane40b1122010-04-15 18:46:00 -0300157 data->state = STATE_TOGGLE_END;
158 return 0;
David Härdeman784a4932010-04-08 20:04:40 -0300159
160 case STATE_TOGGLE_END:
David Härdemand8b4b582010-10-29 16:08:23 -0300161 if (!is_transition(&ev, &dev->raw->prev_ev) ||
David Härdemane40b1122010-04-15 18:46:00 -0300162 !geq_margin(ev.duration, RC6_TOGGLE_END, RC6_UNIT / 2))
163 break;
David Härdeman784a4932010-04-08 20:04:40 -0300164
David Härdemane40b1122010-04-15 18:46:00 -0300165 if (!(data->header & RC6_STARTBIT_MASK)) {
166 IR_dprintk(1, "RC6 invalid start bit\n");
167 break;
David Härdeman784a4932010-04-08 20:04:40 -0300168 }
David Härdemane40b1122010-04-15 18:46:00 -0300169
170 data->state = STATE_BODY_BIT_START;
David Härdemane40b1122010-04-15 18:46:00 -0300171 decrease_duration(&ev, RC6_TOGGLE_END);
172 data->count = 0;
173
174 switch (rc6_mode(data)) {
175 case RC6_MODE_0:
176 data->wanted_bits = RC6_0_NBITS;
177 break;
178 case RC6_MODE_6A:
179 /* This might look weird, but we basically
180 check the value of the first body bit to
181 determine the number of bits in mode 6A */
182 if ((!ev.pulse && !geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2)) ||
183 geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
184 data->wanted_bits = RC6_6A_LARGE_NBITS;
185 else
186 data->wanted_bits = RC6_6A_SMALL_NBITS;
187 break;
188 default:
189 IR_dprintk(1, "RC6 unknown mode\n");
190 goto out;
191 }
192 goto again;
David Härdeman784a4932010-04-08 20:04:40 -0300193
194 case STATE_BODY_BIT_START:
David Härdemane40b1122010-04-15 18:46:00 -0300195 if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2))
196 break;
David Härdeman784a4932010-04-08 20:04:40 -0300197
David Härdemane40b1122010-04-15 18:46:00 -0300198 data->body <<= 1;
199 if (ev.pulse)
200 data->body |= 1;
201 data->count++;
David Härdemane40b1122010-04-15 18:46:00 -0300202 data->state = STATE_BODY_BIT_END;
203 return 0;
David Härdeman784a4932010-04-08 20:04:40 -0300204
205 case STATE_BODY_BIT_END:
David Härdemand8b4b582010-10-29 16:08:23 -0300206 if (!is_transition(&ev, &dev->raw->prev_ev))
David Härdemane40b1122010-04-15 18:46:00 -0300207 break;
David Härdeman784a4932010-04-08 20:04:40 -0300208
David Härdemane40b1122010-04-15 18:46:00 -0300209 if (data->count == data->wanted_bits)
210 data->state = STATE_FINISHED;
211 else
212 data->state = STATE_BODY_BIT_START;
213
214 decrease_duration(&ev, RC6_BIT_END);
215 goto again;
David Härdeman784a4932010-04-08 20:04:40 -0300216
217 case STATE_FINISHED:
David Härdemane40b1122010-04-15 18:46:00 -0300218 if (ev.pulse)
219 break;
220
David Härdeman784a4932010-04-08 20:04:40 -0300221 switch (rc6_mode(data)) {
222 case RC6_MODE_0:
223 scancode = data->body & 0xffff;
224 toggle = data->toggle;
225 IR_dprintk(1, "RC6(0) scancode 0x%04x (toggle: %u)\n",
226 scancode, toggle);
227 break;
228 case RC6_MODE_6A:
229 if (data->wanted_bits == RC6_6A_LARGE_NBITS) {
230 toggle = data->body & RC6_6A_MCE_TOGGLE_MASK ? 1 : 0;
231 scancode = data->body & ~RC6_6A_MCE_TOGGLE_MASK;
232 } else {
233 toggle = 0;
234 scancode = data->body & 0xffffff;
235 }
236
237 IR_dprintk(1, "RC6(6A) scancode 0x%08x (toggle: %u)\n",
238 scancode, toggle);
239 break;
240 default:
241 IR_dprintk(1, "RC6 unknown mode\n");
242 goto out;
243 }
244
David Härdemand8b4b582010-10-29 16:08:23 -0300245 ir_keydown(dev, scancode, toggle);
David Härdeman784a4932010-04-08 20:04:40 -0300246 data->state = STATE_INACTIVE;
247 return 0;
248 }
249
250out:
David Härdemane40b1122010-04-15 18:46:00 -0300251 IR_dprintk(1, "RC6 decode failed at state %i (%uus %s)\n",
252 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
David Härdeman784a4932010-04-08 20:04:40 -0300253 data->state = STATE_INACTIVE;
254 return -EINVAL;
255}
256
David Härdeman784a4932010-04-08 20:04:40 -0300257static struct ir_raw_handler rc6_handler = {
David Härdeman667c9eb2010-06-13 17:29:31 -0300258 .protocols = IR_TYPE_RC6,
David Härdeman784a4932010-04-08 20:04:40 -0300259 .decode = ir_rc6_decode,
David Härdeman784a4932010-04-08 20:04:40 -0300260};
261
262static int __init ir_rc6_decode_init(void)
263{
264 ir_raw_handler_register(&rc6_handler);
265
266 printk(KERN_INFO "IR RC6 protocol handler initialized\n");
267 return 0;
268}
269
270static void __exit ir_rc6_decode_exit(void)
271{
272 ir_raw_handler_unregister(&rc6_handler);
273}
274
275module_init(ir_rc6_decode_init);
276module_exit(ir_rc6_decode_exit);
277
278MODULE_LICENSE("GPL");
279MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
280MODULE_DESCRIPTION("RC6 IR protocol decoder");