aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/IR
diff options
context:
space:
mode:
authorMauro Carvalho Chehab <mchehab@redhat.com>2010-04-06 01:29:42 -0400
committerMauro Carvalho Chehab <mchehab@redhat.com>2010-05-19 11:57:00 -0400
commit9b09df51b8c2b4615376e5ada3e2eb7eeed3cf5d (patch)
tree63eb4aa851e522f0ab21790bbe5f43f51b133d27 /drivers/media/IR
parent7f20d32d446097789ade5ada6b645742ddac4ece (diff)
V4L/DVB: ir-rc5-decoder: fix state machine
Reimplement the RC-5 decoder state machine. Code is now clear, and works properly. It is also simpler than the previous implementations. Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
Diffstat (limited to 'drivers/media/IR')
-rw-r--r--drivers/media/IR/ir-rc5-decoder.c161
1 files changed, 52 insertions, 109 deletions
diff --git a/drivers/media/IR/ir-rc5-decoder.c b/drivers/media/IR/ir-rc5-decoder.c
index 4fb3ce410e24..a62277b625a8 100644
--- a/drivers/media/IR/ir-rc5-decoder.c
+++ b/drivers/media/IR/ir-rc5-decoder.c
@@ -15,19 +15,17 @@
15/* 15/*
16 * This code only handles 14 bits RC-5 protocols. There are other variants 16 * This code only handles 14 bits RC-5 protocols. There are other variants
17 * that use a different number of bits. This is currently unsupported 17 * that use a different number of bits. This is currently unsupported
18 * It considers a carrier of 36 kHz, with a total of 14 bits, where
19 * the first two bits are start bits, and a third one is a filing bit
18 */ 20 */
19 21
20#include <media/ir-core.h> 22#include <media/ir-core.h>
21 23
22#define RC5_NBITS 14 24static unsigned int ir_rc5_remote_gap = 888888;
23#define RC5_HALFBIT 888888 /* ns */
24#define RC5_BIT (RC5_HALFBIT * 2)
25#define RC5_DURATION (RC5_BIT * RC5_NBITS)
26
27#define is_rc5_halfbit(nsec) ((ev->delta.tv_nsec >= RC5_HALFBIT / 2) && \
28 (ev->delta.tv_nsec < RC5_HALFBIT + RC5_HALFBIT / 2))
29 25
30#define n_half(nsec) ((ev->delta.tv_nsec + RC5_HALFBIT / 2) / RC5_HALFBIT) 26#define RC5_NBITS 14
27#define RC5_BIT (ir_rc5_remote_gap * 2)
28#define RC5_DURATION (ir_rc5_remote_gap * RC5_NBITS)
31 29
32/* Used to register rc5_decoder clients */ 30/* Used to register rc5_decoder clients */
33static LIST_HEAD(decoder_list); 31static LIST_HEAD(decoder_list);
@@ -35,19 +33,8 @@ static spinlock_t decoder_lock;
35 33
36enum rc5_state { 34enum rc5_state {
37 STATE_INACTIVE, 35 STATE_INACTIVE,
38 STATE_START2_SPACE,
39 STATE_START2_MARK,
40 STATE_MARKSPACE, 36 STATE_MARKSPACE,
41 STATE_TRAILER_MARK, 37 STATE_TRAILER,
42};
43
44static char *st_name[] = {
45 "Inactive",
46 "start2 sapce",
47 "start2 mark",
48 "mark",
49 "space",
50 "trailer"
51}; 38};
52 39
53struct rc5_code { 40struct rc5_code {
@@ -63,8 +50,7 @@ struct decoder_data {
63 /* State machine control */ 50 /* State machine control */
64 enum rc5_state state; 51 enum rc5_state state;
65 struct rc5_code rc5_code; 52 struct rc5_code rc5_code;
66 unsigned n_half; 53 unsigned code, elapsed, last_bit, last_code;
67 unsigned count;
68}; 54};
69 55
70 56
@@ -147,7 +133,7 @@ static int ir_rc5_decode(struct input_dev *input_dev,
147{ 133{
148 struct decoder_data *data; 134 struct decoder_data *data;
149 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); 135 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
150 int bit, last_bit, n_half; 136 int is_pulse, scancode, delta, toggle;
151 137
152 data = get_decoder_data(ir_dev); 138 data = get_decoder_data(ir_dev);
153 if (!data) 139 if (!data)
@@ -156,122 +142,79 @@ static int ir_rc5_decode(struct input_dev *input_dev,
156 if (!data->enabled) 142 if (!data->enabled)
157 return 0; 143 return 0;
158 144
159 /* Except for the initial event, what matters is the previous bit */ 145 delta = DIV_ROUND_CLOSEST(ev->delta.tv_nsec, ir_rc5_remote_gap);
160 bit = (ev->type & IR_PULSE) ? 1 : 0;
161
162 last_bit = !bit;
163 146
164 /* Discards spurious space last_bits when inactive */ 147 /* The duration time refers to the last bit time */
148 is_pulse = (ev->type & IR_PULSE) ? 1 : 0;
165 149
166 /* Very long delays are considered as start events */ 150 /* Very long delays are considered as start events */
167 if (ev->delta.tv_nsec > RC5_DURATION + RC5_HALFBIT / 2) 151 if (delta > RC5_DURATION || (ev->type & IR_START_EVENT))
168 data->state = STATE_INACTIVE;
169
170 if (ev->type & IR_START_EVENT)
171 data->state = STATE_INACTIVE; 152 data->state = STATE_INACTIVE;
172 153
173 switch (data->state) { 154 switch (data->state) {
174 case STATE_INACTIVE: 155 case STATE_INACTIVE:
175IR_dprintk(1, "currently inative. Received bit (%s) @%luus\n", 156 IR_dprintk(2, "currently inative. Start bit (%s) @%uus\n",
176 last_bit ? "pulse" : "space", 157 is_pulse ? "pulse" : "space",
177 (ev->delta.tv_nsec + 500) / 1000); 158 (unsigned)(ev->delta.tv_nsec + 500) / 1000);
178 159
179 /* Discards the initial start space */ 160 /* Discards the initial start space */
180 if (bit) 161 if (!is_pulse)
181 return 0;
182 data->count = 0;
183 data->n_half = 0;
184 memset (&data->rc5_code, 0, sizeof(data->rc5_code));
185
186 data->state = STATE_START2_SPACE;
187 return 0;
188 case STATE_START2_SPACE:
189 if (last_bit)
190 goto err;
191 if (!is_rc5_halfbit(ev->delta.tv_nsec))
192 goto err;
193 data->state = STATE_START2_MARK;
194 return 0;
195 case STATE_START2_MARK:
196 if (!last_bit)
197 goto err; 162 goto err;
198 163 data->code = 1;
199 if (!is_rc5_halfbit(ev->delta.tv_nsec)) 164 data->last_bit = 1;
200 goto err; 165 data->elapsed = 0;
201 166 memset(&data->rc5_code, 0, sizeof(data->rc5_code));
202 data->state = STATE_MARKSPACE; 167 data->state = STATE_MARKSPACE;
203 return 0; 168 return 0;
204 case STATE_MARKSPACE: 169 case STATE_MARKSPACE:
205 n_half = n_half(ev->delta.tv_nsec); 170 if (delta != 1)
206 if (n_half < 1 || n_half > 3) { 171 data->last_bit = data->last_bit ? 0 : 1;
207 IR_dprintk(1, "Decode failed at %d-th bit (%s) @%luus\n",
208 data->count,
209 last_bit ? "pulse" : "space",
210 (ev->delta.tv_nsec + 500) / 1000);
211printk("%d halves\n", n_half);
212 goto err2;
213 }
214 data->n_half += n_half;
215 172
216 if (!last_bit) 173 data->elapsed += delta;
217 return 0;
218 174
219 /* Got one complete mark/space cycle */ 175 if ((data->elapsed % 2) == 1)
176 return 0;
220 177
221 bit = ((data->count + 1) * 2)/ data->n_half; 178 data->code <<= 1;
179 data->code |= data->last_bit;
222 180
223printk("%d halves, %d bits\n", n_half, bit); 181 /* Fill the 2 unused bits at the command with 0 */
182 if (data->elapsed / 2 == 6)
183 data->code <<= 2;
224 184
225#if 1 /* SANITY check - while testing the decoder */ 185 if (data->elapsed >= (RC5_NBITS - 1) * 2) {
226 if (bit > 1) { 186 scancode = data->code;
227 IR_dprintk(1, "Decoder HAS failed at %d-th bit (%s) @%luus\n",
228 data->count,
229 last_bit ? "pulse" : "space",
230 (ev->delta.tv_nsec + 500) / 1000);
231 187
232 goto err2; 188 /* Check for the start bits */
233 } 189 if ((scancode & 0xc000) != 0xc000) {
234#endif 190 IR_dprintk(1, "Code 0x%04x doesn't have two start bits. It is not RC-5\n", scancode);
235 /* Ok, we've got a valid bit. proccess it */ 191 goto err;
236 if (bit) {
237 int shift = data->count;
238
239 /*
240 * RC-5 transmit bytes on this temporal order:
241 * address | not address | command | not command
242 */
243 if (shift < 8) {
244 data->rc5_code.address |= 1 << shift;
245 } else {
246 data->rc5_code.command |= 1 << (shift - 8);
247 } 192 }
248 }
249 IR_dprintk(1, "RC-5: bit #%d: %d (%d)\n",
250 data->count, bit, data->n_half);
251 if (++data->count >= RC5_NBITS) {
252 u32 scancode;
253 scancode = data->rc5_code.address << 8 |
254 data->rc5_code.command;
255 IR_dprintk(1, "RC-5 scancode 0x%04x\n", scancode);
256 193
257 ir_keydown(input_dev, scancode, 0); 194 toggle = (scancode & 0x2000) ? 1 : 0;
195
196 if (scancode == data->last_code) {
197 IR_dprintk(1, "RC-5 repeat\n");
198 ir_repeat(input_dev);
199 } else {
200 data->last_code = scancode;
201 scancode &= 0x1fff;
202 IR_dprintk(1, "RC-5 scancode 0x%04x\n", scancode);
258 203
259 data->state = STATE_TRAILER_MARK; 204 ir_keydown(input_dev, scancode, 0);
205 }
206 data->state = STATE_TRAILER;
260 } 207 }
261 return 0; 208 return 0;
262 case STATE_TRAILER_MARK: 209 case STATE_TRAILER:
263 if (!last_bit)
264 goto err;
265 data->state = STATE_INACTIVE; 210 data->state = STATE_INACTIVE;
266 return 0; 211 return 0;
267 } 212 }
268 213
269err: 214err:
270 IR_dprintk(1, "RC-5 decoded failed at state %s (%s) @ %luus\n", 215 IR_dprintk(1, "RC-5 decoded failed at %s @ %luus\n",
271 st_name[data->state], 216 is_pulse ? "pulse" : "space",
272 bit ? "pulse" : "space",
273 (ev->delta.tv_nsec + 500) / 1000); 217 (ev->delta.tv_nsec + 500) / 1000);
274err2:
275 data->state = STATE_INACTIVE; 218 data->state = STATE_INACTIVE;
276 return -EINVAL; 219 return -EINVAL;
277} 220}