blob: 33b260f517f509efd9c55067eb89c8cd748ed12c [file] [log] [blame]
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -03001/* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -03002 *
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
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030017#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
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -030025/* 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)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030028
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -030029/* Bit 1 time: 2.25ms us */
30#define MIN_BIT1_TIME 2050000
31#define MAX_BIT1_TIME 2450000
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030032
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -030033/* Bit 0 time: 1.12ms us */
34#define MIN_BIT0_TIME 920000
35#define MAX_BIT0_TIME 1320000
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030036
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -030037/* 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
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030050/* Used to register nec_decoder clients */
51static LIST_HEAD(decoder_list);
52static spinlock_t decoder_lock;
53
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030054enum 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
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030071struct decoder_data {
72 struct list_head list;
73 struct ir_input_dev *ir_dev;
74 int enabled:1;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030075
76 /* State machine control */
77 enum nec_state state;
78 struct nec_code nec_code;
79 unsigned count;
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030080};
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
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300151/**
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300152 * handle_event() - Decode one NEC pulse or space
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300153 * @input_dev: the struct input_dev descriptor of the device
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300154 * @ev: event array with type/duration of pulse/space
155 *
156 * This function returns -EINVAL if the pulse violates the state machine
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300157 */
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300158static int handle_event(struct input_dev *input_dev,
159 struct ir_raw_event *ev)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300160{
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300161 struct decoder_data *data;
162 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
163 int bit, last_bit;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300164
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300165 data = get_decoder_data(ir_dev);
166 if (!data)
167 return -EINVAL;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300168
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300169 /* Except for the initial event, what matters is the previous bit */
170 bit = (ev->type & IR_PULSE) ? 1 : 0;
171
172 last_bit = !bit;
173
174 /* Discards spurious space last_bits when inactive */
175
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;
179
180 if (ev->type & IR_START_EVENT)
181 data->state = STATE_INACTIVE;
182
183 switch (data->state) {
184 case STATE_INACTIVE:
185 if (!bit) /* PULSE marks the start event */
186 return 0;
187
188 data->count = 0;
189 data->state = STATE_HEADER_MARK;
190 memset (&data->nec_code, 0, sizeof(data->nec_code));
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300191 return 0;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300192 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;
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300204 return 0;
205 }
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300206
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300207 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 }
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300213 goto err;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300214 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)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300224 goto err;
225
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300226 if ((ev->delta.tv_nsec >= NEC_0_SYMBOL - NEC_UNIT / 2) &&
227 (ev->delta.tv_nsec < NEC_0_SYMBOL + NEC_UNIT / 2))
228 bit = 0;
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 }
240
241 /* Ok, we've got a valid bit. proccess it */
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300242 if (bit) {
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300243 int shift = data->count;
244
245 /*
246 * NEC transmit bytes on this temporal order:
247 * address | not address | command | not command
248 */
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300249 if (shift < 8) {
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300250 data->nec_code.address |= 1 << shift;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300251 } else if (shift < 16) {
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300252 data->nec_code.not_address |= 1 << (shift - 8);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300253 } else if (shift < 24) {
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300254 data->nec_code.command |= 1 << (shift - 16);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300255 } else {
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300256 data->nec_code.not_command |= 1 << (shift - 24);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300257 }
258 }
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300259 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;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300266
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300267 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);
280
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;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300295 }
296
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300297err:
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300298 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;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300305
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300306checksum_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);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300313 return -EINVAL;
314}
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300315
316/**
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300317 * ir_nec_decode() - Decodes all NEC pulsecodes on a given array
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300318 * @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
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300321 * This function returns the number of decoded pulses
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300322 */
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300323static int ir_nec_decode(struct input_dev *input_dev,
324 struct ir_raw_event *evs,
325 int len)
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300326{
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300327 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
328 struct decoder_data *data;
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300329 int pos = 0;
330 int rc = 0;
331
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300332 data = get_decoder_data(ir_dev);
333 if (!data || !data->enabled)
334 return 0;
335
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300336 for (pos = 0; pos < len; pos++)
337 handle_event(input_dev, &evs[pos]);
338
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300339 return rc;
340}
341
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300342static 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
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300386static struct ir_raw_handler nec_handler = {
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300387 .decode = ir_nec_decode,
388 .raw_register = ir_nec_register,
389 .raw_unregister = ir_nec_unregister,
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300390};
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");