V4L/DVB: ir-core: allow specifying multiple protocols at one open/write
[deliverable/linux.git] / drivers / media / IR / ir-rc6-decoder.c
CommitLineData
784a4931
DH
1/* 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
15#include "ir-core-priv.h"
16
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
e40b1127
DH
29#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)
784a4931
DH
35#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
39/* Used to register rc6_decoder clients */
40static LIST_HEAD(decoder_list);
41static DEFINE_SPINLOCK(decoder_lock);
42
43enum rc6_mode {
44 RC6_MODE_0,
45 RC6_MODE_6A,
46 RC6_MODE_UNKNOWN,
47};
48
49enum rc6_state {
50 STATE_INACTIVE,
51 STATE_PREFIX_SPACE,
52 STATE_HEADER_BIT_START,
53 STATE_HEADER_BIT_END,
54 STATE_TOGGLE_START,
55 STATE_TOGGLE_END,
56 STATE_BODY_BIT_START,
57 STATE_BODY_BIT_END,
58 STATE_FINISHED,
59};
60
61struct decoder_data {
62 struct list_head list;
63 struct ir_input_dev *ir_dev;
784a4931
DH
64
65 /* State machine control */
66 enum rc6_state state;
67 u8 header;
68 u32 body;
e40b1127 69 struct ir_raw_event prev_ev;
784a4931
DH
70 bool toggle;
71 unsigned count;
72 unsigned wanted_bits;
73};
74
75
76/**
77 * get_decoder_data() - gets decoder data
78 * @input_dev: input device
79 *
80 * Returns the struct decoder_data that corresponds to a device
81 */
82static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
83{
84 struct decoder_data *data = NULL;
85
86 spin_lock(&decoder_lock);
87 list_for_each_entry(data, &decoder_list, list) {
88 if (data->ir_dev == ir_dev)
89 break;
90 }
91 spin_unlock(&decoder_lock);
92 return data;
93}
94
784a4931
DH
95static enum rc6_mode rc6_mode(struct decoder_data *data) {
96 switch (data->header & RC6_MODE_MASK) {
97 case 0:
98 return RC6_MODE_0;
99 case 6:
100 if (!data->toggle)
101 return RC6_MODE_6A;
102 /* fall through */
103 default:
104 return RC6_MODE_UNKNOWN;
105 }
106}
107
108/**
109 * ir_rc6_decode() - Decode one RC6 pulse or space
110 * @input_dev: the struct input_dev descriptor of the device
e40b1127 111 * @ev: the struct ir_raw_event descriptor of the pulse/space
784a4931
DH
112 *
113 * This function returns -EINVAL if the pulse violates the state machine
114 */
e40b1127 115static int ir_rc6_decode(struct input_dev *input_dev, struct ir_raw_event ev)
784a4931
DH
116{
117 struct decoder_data *data;
118 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
119 u32 scancode;
120 u8 toggle;
784a4931
DH
121
122 data = get_decoder_data(ir_dev);
123 if (!data)
124 return -EINVAL;
125
667c9ebe 126 if (!(ir_dev->raw->enabled_protocols & IR_TYPE_RC6))
784a4931
DH
127 return 0;
128
e40b1127 129 if (IS_RESET(ev)) {
784a4931
DH
130 data->state = STATE_INACTIVE;
131 return 0;
132 }
133
e40b1127 134 if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
784a4931
DH
135 goto out;
136
137again:
e40b1127
DH
138 IR_dprintk(2, "RC6 decode started at state %i (%uus %s)\n",
139 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
784a4931 140
e40b1127 141 if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
784a4931
DH
142 return 0;
143
144 switch (data->state) {
145
146 case STATE_INACTIVE:
e40b1127
DH
147 if (!ev.pulse)
148 break;
149
150 /* Note: larger margin on first pulse since each RC6_UNIT
151 is quite short and some hardware takes some time to
152 adjust to the signal */
153 if (!eq_margin(ev.duration, RC6_PREFIX_PULSE, RC6_UNIT))
154 break;
155
156 data->state = STATE_PREFIX_SPACE;
157 data->count = 0;
158 return 0;
784a4931
DH
159
160 case STATE_PREFIX_SPACE:
e40b1127
DH
161 if (ev.pulse)
162 break;
163
164 if (!eq_margin(ev.duration, RC6_PREFIX_SPACE, RC6_UNIT / 2))
165 break;
166
167 data->state = STATE_HEADER_BIT_START;
168 return 0;
784a4931
DH
169
170 case STATE_HEADER_BIT_START:
e40b1127
DH
171 if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2))
172 break;
173
174 data->header <<= 1;
175 if (ev.pulse)
176 data->header |= 1;
177 data->count++;
178 data->prev_ev = ev;
179 data->state = STATE_HEADER_BIT_END;
180 return 0;
784a4931
DH
181
182 case STATE_HEADER_BIT_END:
e40b1127
DH
183 if (!is_transition(&ev, &data->prev_ev))
184 break;
784a4931 185
e40b1127
DH
186 if (data->count == RC6_HEADER_NBITS)
187 data->state = STATE_TOGGLE_START;
188 else
189 data->state = STATE_HEADER_BIT_START;
190
191 decrease_duration(&ev, RC6_BIT_END);
192 goto again;
784a4931
DH
193
194 case STATE_TOGGLE_START:
e40b1127
DH
195 if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2))
196 break;
197
198 data->toggle = ev.pulse;
199 data->prev_ev = ev;
200 data->state = STATE_TOGGLE_END;
201 return 0;
784a4931
DH
202
203 case STATE_TOGGLE_END:
e40b1127
DH
204 if (!is_transition(&ev, &data->prev_ev) ||
205 !geq_margin(ev.duration, RC6_TOGGLE_END, RC6_UNIT / 2))
206 break;
784a4931 207
e40b1127
DH
208 if (!(data->header & RC6_STARTBIT_MASK)) {
209 IR_dprintk(1, "RC6 invalid start bit\n");
210 break;
211 }
784a4931 212
e40b1127
DH
213 data->state = STATE_BODY_BIT_START;
214 data->prev_ev = ev;
215 decrease_duration(&ev, RC6_TOGGLE_END);
216 data->count = 0;
217
218 switch (rc6_mode(data)) {
219 case RC6_MODE_0:
220 data->wanted_bits = RC6_0_NBITS;
221 break;
222 case RC6_MODE_6A:
223 /* This might look weird, but we basically
224 check the value of the first body bit to
225 determine the number of bits in mode 6A */
226 if ((!ev.pulse && !geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2)) ||
227 geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
228 data->wanted_bits = RC6_6A_LARGE_NBITS;
229 else
230 data->wanted_bits = RC6_6A_SMALL_NBITS;
231 break;
232 default:
233 IR_dprintk(1, "RC6 unknown mode\n");
234 goto out;
784a4931 235 }
e40b1127 236 goto again;
784a4931
DH
237
238 case STATE_BODY_BIT_START:
e40b1127
DH
239 if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2))
240 break;
784a4931 241
e40b1127
DH
242 data->body <<= 1;
243 if (ev.pulse)
244 data->body |= 1;
245 data->count++;
246 data->prev_ev = ev;
247
248 data->state = STATE_BODY_BIT_END;
249 return 0;
784a4931
DH
250
251 case STATE_BODY_BIT_END:
e40b1127
DH
252 if (!is_transition(&ev, &data->prev_ev))
253 break;
784a4931 254
e40b1127
DH
255 if (data->count == data->wanted_bits)
256 data->state = STATE_FINISHED;
257 else
258 data->state = STATE_BODY_BIT_START;
259
260 decrease_duration(&ev, RC6_BIT_END);
261 goto again;
784a4931
DH
262
263 case STATE_FINISHED:
e40b1127
DH
264 if (ev.pulse)
265 break;
266
784a4931
DH
267 switch (rc6_mode(data)) {
268 case RC6_MODE_0:
269 scancode = data->body & 0xffff;
270 toggle = data->toggle;
271 IR_dprintk(1, "RC6(0) scancode 0x%04x (toggle: %u)\n",
272 scancode, toggle);
273 break;
274 case RC6_MODE_6A:
275 if (data->wanted_bits == RC6_6A_LARGE_NBITS) {
276 toggle = data->body & RC6_6A_MCE_TOGGLE_MASK ? 1 : 0;
277 scancode = data->body & ~RC6_6A_MCE_TOGGLE_MASK;
278 } else {
279 toggle = 0;
280 scancode = data->body & 0xffffff;
281 }
282
283 IR_dprintk(1, "RC6(6A) scancode 0x%08x (toggle: %u)\n",
284 scancode, toggle);
285 break;
286 default:
287 IR_dprintk(1, "RC6 unknown mode\n");
288 goto out;
289 }
290
291 ir_keydown(input_dev, scancode, toggle);
292 data->state = STATE_INACTIVE;
293 return 0;
294 }
295
296out:
e40b1127
DH
297 IR_dprintk(1, "RC6 decode failed at state %i (%uus %s)\n",
298 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
784a4931
DH
299 data->state = STATE_INACTIVE;
300 return -EINVAL;
301}
302
303static int ir_rc6_register(struct input_dev *input_dev)
304{
305 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
306 struct decoder_data *data;
784a4931
DH
307
308 data = kzalloc(sizeof(*data), GFP_KERNEL);
667c9ebe 309 if (!data)
784a4931 310 return -ENOMEM;
784a4931
DH
311
312 data->ir_dev = ir_dev;
784a4931
DH
313
314 spin_lock(&decoder_lock);
315 list_add_tail(&data->list, &decoder_list);
316 spin_unlock(&decoder_lock);
317
318 return 0;
319}
320
321static int ir_rc6_unregister(struct input_dev *input_dev)
322{
323 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
324 static struct decoder_data *data;
325
326 data = get_decoder_data(ir_dev);
327 if (!data)
328 return 0;
329
784a4931
DH
330 spin_lock(&decoder_lock);
331 list_del(&data->list);
332 spin_unlock(&decoder_lock);
333
334 return 0;
335}
336
337static struct ir_raw_handler rc6_handler = {
667c9ebe 338 .protocols = IR_TYPE_RC6,
784a4931
DH
339 .decode = ir_rc6_decode,
340 .raw_register = ir_rc6_register,
341 .raw_unregister = ir_rc6_unregister,
342};
343
344static int __init ir_rc6_decode_init(void)
345{
346 ir_raw_handler_register(&rc6_handler);
347
348 printk(KERN_INFO "IR RC6 protocol handler initialized\n");
349 return 0;
350}
351
352static void __exit ir_rc6_decode_exit(void)
353{
354 ir_raw_handler_unregister(&rc6_handler);
355}
356
357module_init(ir_rc6_decode_init);
358module_exit(ir_rc6_decode_exit);
359
360MODULE_LICENSE("GPL");
361MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
362MODULE_DESCRIPTION("RC6 IR protocol decoder");
This page took 0.11131 seconds and 5 git commands to generate.