]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/IR/ir-nec-decoder.c
V4L/DVB: ir-nec-decoder: Reimplement the entire decoder
[net-next-2.6.git] / drivers / media / IR / ir-nec-decoder.c
CommitLineData
995187be 1/* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol
a3572c34
MCC
2 *
3 * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
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 <media/ir-core.h>
16
2f16f631
MCC
17#define NEC_UNIT 559979 /* ns */
18#define NEC_HEADER_MARK (16 * NEC_UNIT)
19#define NEC_HEADER_SPACE (8 * NEC_UNIT)
20#define NEC_REPEAT_SPACE (4 * NEC_UNIT)
21#define NEC_MARK (NEC_UNIT)
22#define NEC_0_SYMBOL (NEC_UNIT)
23#define NEC_1_SYMBOL (3 * NEC_UNIT)
24
9f154782
MCC
25/* Start time: 4.5 ms + 560 us of the next pulse */
26#define MIN_START_TIME (3900000 + 560000)
27#define MAX_START_TIME (5100000 + 560000)
a3572c34 28
9f154782
MCC
29/* Bit 1 time: 2.25ms us */
30#define MIN_BIT1_TIME 2050000
31#define MAX_BIT1_TIME 2450000
a3572c34 32
9f154782
MCC
33/* Bit 0 time: 1.12ms us */
34#define MIN_BIT0_TIME 920000
35#define MAX_BIT0_TIME 1320000
a3572c34 36
9f154782
MCC
37/* Total IR code is 110 ms, including the 9 ms for the start pulse */
38#define MAX_NEC_TIME 4000000
39
40/* Total IR code is 110 ms, including the 9 ms for the start pulse */
41#define MIN_REPEAT_TIME 99000000
42#define MAX_REPEAT_TIME 112000000
43
44/* Repeat time: 2.25ms us */
45#define MIN_REPEAT_START_TIME 2050000
46#define MAX_REPEAT_START_TIME 3000000
47
48#define REPEAT_TIME 240 /* ms */
49
20d5f116
MCC
50/* Used to register nec_decoder clients */
51static LIST_HEAD(decoder_list);
52static spinlock_t decoder_lock;
53
2f16f631
MCC
54enum nec_state {
55 STATE_INACTIVE,
56 STATE_HEADER_MARK,
57 STATE_HEADER_SPACE,
58 STATE_MARK,
59 STATE_SPACE,
60 STATE_TRAILER_MARK,
61 STATE_TRAILER_SPACE,
62};
63
64struct nec_code {
65 u8 address;
66 u8 not_address;
67 u8 command;
68 u8 not_command;
69};
70
20d5f116
MCC
71struct decoder_data {
72 struct list_head list;
73 struct ir_input_dev *ir_dev;
74 int enabled:1;
2f16f631
MCC
75
76 /* State machine control */
77 enum nec_state state;
78 struct nec_code nec_code;
79 unsigned count;
20d5f116
MCC
80};
81
82
83/**
84 * get_decoder_data() - gets decoder data
85 * @input_dev: input device
86 *
87 * Returns the struct decoder_data that corresponds to a device
88 */
89
90static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
91{
92 struct decoder_data *data = NULL;
93
94 spin_lock(&decoder_lock);
95 list_for_each_entry(data, &decoder_list, list) {
96 if (data->ir_dev == ir_dev)
97 break;
98 }
99 spin_unlock(&decoder_lock);
100 return data;
101}
102
103static ssize_t store_enabled(struct device *d,
104 struct device_attribute *mattr,
105 const char *buf,
106 size_t len)
107{
108 unsigned long value;
109 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
110 struct decoder_data *data = get_decoder_data(ir_dev);
111
112 if (!data)
113 return -EINVAL;
114
115 if (strict_strtoul(buf, 10, &value) || value > 1)
116 return -EINVAL;
117
118 data->enabled = value;
119
120 return len;
121}
122
123static ssize_t show_enabled(struct device *d,
124 struct device_attribute *mattr, char *buf)
125{
126 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
127 struct decoder_data *data = get_decoder_data(ir_dev);
128
129 if (!data)
130 return -EINVAL;
131
132 if (data->enabled)
133 return sprintf(buf, "1\n");
134 else
135 return sprintf(buf, "0\n");
136}
137
138static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
139
140static struct attribute *decoder_attributes[] = {
141 &dev_attr_enabled.attr,
142 NULL
143};
144
145static struct attribute_group decoder_attribute_group = {
146 .name = "nec_decoder",
147 .attrs = decoder_attributes,
148};
149
150
2f16f631
MCC
151/**
152 * handle_event() - Decode one NEC pulse or space
9f154782 153 * @input_dev: the struct input_dev descriptor of the device
2f16f631
MCC
154 * @ev: event array with type/duration of pulse/space
155 *
156 * This function returns -EINVAL if the pulse violates the state machine
9f154782 157 */
2f16f631
MCC
158static int handle_event(struct input_dev *input_dev,
159 struct ir_raw_event *ev)
9f154782 160{
2f16f631
MCC
161 struct decoder_data *data;
162 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
163 int bit, last_bit;
9f154782 164
2f16f631
MCC
165 data = get_decoder_data(ir_dev);
166 if (!data)
167 return -EINVAL;
9f154782 168
2f16f631
MCC
169 /* Except for the initial event, what matters is the previous bit */
170 bit = (ev->type & IR_PULSE) ? 1 : 0;
9f154782 171
2f16f631 172 last_bit = !bit;
a3572c34 173
2f16f631 174 /* Discards spurious space last_bits when inactive */
a3572c34 175
2f16f631
MCC
176 /* Very long delays are considered as start events */
177 if (ev->delta.tv_nsec > NEC_HEADER_MARK + NEC_HEADER_SPACE - NEC_UNIT / 2)
178 data->state = STATE_INACTIVE;
a3572c34 179
2f16f631
MCC
180 if (ev->type & IR_START_EVENT)
181 data->state = STATE_INACTIVE;
9f154782 182
2f16f631
MCC
183 switch (data->state) {
184 case STATE_INACTIVE:
185 if (!bit) /* PULSE marks the start event */
186 return 0;
a3572c34 187
2f16f631
MCC
188 data->count = 0;
189 data->state = STATE_HEADER_MARK;
190 memset (&data->nec_code, 0, sizeof(data->nec_code));
191 return 0;
192 case STATE_HEADER_MARK:
193 if (!last_bit)
194 goto err;
195 if (ev->delta.tv_nsec < NEC_HEADER_MARK - 6 * NEC_UNIT)
196 goto err;
197 data->state = STATE_HEADER_SPACE;
198 return 0;
199 case STATE_HEADER_SPACE:
200 if (last_bit)
201 goto err;
202 if (ev->delta.tv_nsec >= NEC_HEADER_SPACE - NEC_UNIT / 2) {
203 data->state = STATE_MARK;
9f154782
MCC
204 return 0;
205 }
9f154782 206
2f16f631
MCC
207 if (ev->delta.tv_nsec >= NEC_REPEAT_SPACE - NEC_UNIT / 2) {
208 ir_repeat(input_dev);
209 IR_dprintk(1, "Repeat last key\n");
210 data->state = STATE_TRAILER_MARK;
211 return 0;
212 }
9f154782 213 goto err;
2f16f631
MCC
214 case STATE_MARK:
215 if (!last_bit)
216 goto err;
217 if ((ev->delta.tv_nsec > NEC_MARK + NEC_UNIT / 2) ||
218 (ev->delta.tv_nsec < NEC_MARK - NEC_UNIT / 2))
219 goto err;
220 data->state = STATE_SPACE;
221 return 0;
222 case STATE_SPACE:
223 if (last_bit)
224 goto err;
a3572c34 225
2f16f631
MCC
226 if ((ev->delta.tv_nsec >= NEC_0_SYMBOL - NEC_UNIT / 2) &&
227 (ev->delta.tv_nsec < NEC_0_SYMBOL + NEC_UNIT / 2))
a3572c34 228 bit = 0;
2f16f631
MCC
229 else if ((ev->delta.tv_nsec >= NEC_1_SYMBOL - NEC_UNIT / 2) &&
230 (ev->delta.tv_nsec < NEC_1_SYMBOL + NEC_UNIT / 2))
231 bit = 1;
232 else {
233 IR_dprintk(1, "Decode failed at %d-th bit (%s) @%luus\n",
234 data->count,
235 last_bit ? "pulse" : "space",
236 (ev->delta.tv_nsec + 500) / 1000);
237
238 goto err2;
239 }
a3572c34 240
2f16f631 241 /* Ok, we've got a valid bit. proccess it */
a3572c34 242 if (bit) {
2f16f631
MCC
243 int shift = data->count;
244
245 /*
246 * NEC transmit bytes on this temporal order:
247 * address | not address | command | not command
248 */
a3572c34 249 if (shift < 8) {
2f16f631 250 data->nec_code.address |= 1 << shift;
a3572c34 251 } else if (shift < 16) {
2f16f631 252 data->nec_code.not_address |= 1 << (shift - 8);
a3572c34 253 } else if (shift < 24) {
2f16f631 254 data->nec_code.command |= 1 << (shift - 16);
a3572c34 255 } else {
2f16f631 256 data->nec_code.not_command |= 1 << (shift - 24);
a3572c34
MCC
257 }
258 }
2f16f631
MCC
259 if (++data->count == 32) {
260 u32 scancode;
261 /*
262 * Fixme: may need to accept Extended NEC protocol?
263 */
264 if ((data->nec_code.command ^ data->nec_code.not_command) != 0xff)
265 goto checksum_err;
266
267 if ((data->nec_code.address ^ data->nec_code.not_address) != 0xff) {
268 /* Extended NEC */
269 scancode = data->nec_code.address << 16 |
270 data->nec_code.not_address << 8 |
271 data->nec_code.command;
272 IR_dprintk(1, "NEC scancode 0x%06x\n", scancode);
273 } else {
274 /* normal NEC */
275 scancode = data->nec_code.address << 8 |
276 data->nec_code.command;
277 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
278 }
279 ir_keydown(input_dev, scancode, 0);
a3572c34 280
2f16f631
MCC
281 data->state = STATE_TRAILER_MARK;
282 } else
283 data->state = STATE_MARK;
284 return 0;
285 case STATE_TRAILER_MARK:
286 if (!last_bit)
287 goto err;
288 data->state = STATE_TRAILER_SPACE;
289 return 0;
290 case STATE_TRAILER_SPACE:
291 if (last_bit)
292 goto err;
293 data->state = STATE_INACTIVE;
294 return 0;
295 }
a3572c34 296
a3572c34 297err:
2f16f631
MCC
298 IR_dprintk(1, "NEC decoded failed at state %d (%s) @ %luus\n",
299 data->state,
300 bit ? "pulse" : "space",
301 (ev->delta.tv_nsec + 500) / 1000);
302err2:
303 data->state = STATE_INACTIVE;
304 return -EINVAL;
a3572c34 305
2f16f631
MCC
306checksum_err:
307 data->state = STATE_INACTIVE;
308 IR_dprintk(1, "NEC checksum error: received 0x%02x%02x%02x%02x\n",
309 data->nec_code.address,
310 data->nec_code.not_address,
311 data->nec_code.command,
312 data->nec_code.not_command);
a3572c34
MCC
313 return -EINVAL;
314}
ada39630
MCC
315
316/**
2f16f631 317 * ir_nec_decode() - Decodes all NEC pulsecodes on a given array
ada39630
MCC
318 * @input_dev: the struct input_dev descriptor of the device
319 * @evs: event array with type/duration of pulse/space
320 * @len: length of the array
93c312ff 321 * This function returns the number of decoded pulses
ada39630 322 */
995187be
MCC
323static int ir_nec_decode(struct input_dev *input_dev,
324 struct ir_raw_event *evs,
325 int len)
ada39630 326{
20d5f116
MCC
327 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
328 struct decoder_data *data;
ada39630
MCC
329 int pos = 0;
330 int rc = 0;
331
20d5f116
MCC
332 data = get_decoder_data(ir_dev);
333 if (!data || !data->enabled)
334 return 0;
335
2f16f631
MCC
336 for (pos = 0; pos < len; pos++)
337 handle_event(input_dev, &evs[pos]);
338
ada39630
MCC
339 return rc;
340}
341
20d5f116
MCC
342static int ir_nec_register(struct input_dev *input_dev)
343{
344 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
345 struct decoder_data *data;
346 int rc;
347
348 rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
349 if (rc < 0)
350 return rc;
351
352 data = kzalloc(sizeof(*data), GFP_KERNEL);
353 if (!data) {
354 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
355 return -ENOMEM;
356 }
357
358 data->ir_dev = ir_dev;
359 data->enabled = 1;
360
361 spin_lock(&decoder_lock);
362 list_add_tail(&data->list, &decoder_list);
363 spin_unlock(&decoder_lock);
364
365 return 0;
366}
367
368static int ir_nec_unregister(struct input_dev *input_dev)
369{
370 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
371 static struct decoder_data *data;
372
373 data = get_decoder_data(ir_dev);
374 if (!data)
375 return 0;
376
377 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
378
379 spin_lock(&decoder_lock);
380 list_del(&data->list);
381 spin_unlock(&decoder_lock);
382
383 return 0;
384}
385
995187be 386static struct ir_raw_handler nec_handler = {
20d5f116
MCC
387 .decode = ir_nec_decode,
388 .raw_register = ir_nec_register,
389 .raw_unregister = ir_nec_unregister,
995187be
MCC
390};
391
392static int __init ir_nec_decode_init(void)
393{
394 ir_raw_handler_register(&nec_handler);
395
396 printk(KERN_INFO "IR NEC protocol handler initialized\n");
397 return 0;
398}
399
400static void __exit ir_nec_decode_exit(void)
401{
402 ir_raw_handler_unregister(&nec_handler);
403}
404
405module_init(ir_nec_decode_init);
406module_exit(ir_nec_decode_exit);
407
408MODULE_LICENSE("GPL");
409MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
410MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
411MODULE_DESCRIPTION("NEC IR protocol decoder");