1 /* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol
3 * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
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.
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.
15 #include <linux/bitrev.h>
16 #include "ir-core-priv.h"
19 #define NEC_UNIT 562500 /* ns */
20 #define NEC_HEADER_PULSE (16 * NEC_UNIT)
21 #define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */
22 #define NEC_HEADER_SPACE (8 * NEC_UNIT)
23 #define NEC_REPEAT_SPACE (8 * NEC_UNIT)
24 #define NEC_BIT_PULSE (1 * NEC_UNIT)
25 #define NEC_BIT_0_SPACE (1 * NEC_UNIT)
26 #define NEC_BIT_1_SPACE (3 * NEC_UNIT)
27 #define NEC_TRAILER_PULSE (1 * NEC_UNIT)
28 #define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */
30 /* Used to register nec_decoder clients */
31 static LIST_HEAD(decoder_list);
32 static DEFINE_SPINLOCK(decoder_lock);
44 struct list_head list;
45 struct ir_input_dev *ir_dev;
48 /* State machine control */
56 * get_decoder_data() - gets decoder data
57 * @input_dev: input device
59 * Returns the struct decoder_data that corresponds to a device
61 static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
63 struct decoder_data *data = NULL;
65 spin_lock(&decoder_lock);
66 list_for_each_entry(data, &decoder_list, list) {
67 if (data->ir_dev == ir_dev)
70 spin_unlock(&decoder_lock);
74 static ssize_t store_enabled(struct device *d,
75 struct device_attribute *mattr,
80 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
81 struct decoder_data *data = get_decoder_data(ir_dev);
86 if (strict_strtoul(buf, 10, &value) || value > 1)
89 data->enabled = value;
94 static ssize_t show_enabled(struct device *d,
95 struct device_attribute *mattr, char *buf)
97 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
98 struct decoder_data *data = get_decoder_data(ir_dev);
104 return sprintf(buf, "1\n");
106 return sprintf(buf, "0\n");
109 static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
111 static struct attribute *decoder_attributes[] = {
112 &dev_attr_enabled.attr,
116 static struct attribute_group decoder_attribute_group = {
117 .name = "nec_decoder",
118 .attrs = decoder_attributes,
122 * ir_nec_decode() - Decode one NEC pulse or space
123 * @input_dev: the struct input_dev descriptor of the device
124 * @duration: the struct ir_raw_event descriptor of the pulse/space
126 * This function returns -EINVAL if the pulse violates the state machine
128 static int ir_nec_decode(struct input_dev *input_dev, struct ir_raw_event ev)
130 struct decoder_data *data;
131 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
133 u8 address, not_address, command, not_command;
135 data = get_decoder_data(ir_dev);
143 data->state = STATE_INACTIVE;
147 IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
148 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
150 switch (data->state) {
156 if (!eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2) &&
157 !eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
161 data->state = STATE_HEADER_SPACE;
164 case STATE_HEADER_SPACE:
168 if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) {
169 data->state = STATE_BIT_PULSE;
171 } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
172 ir_repeat(input_dev);
173 IR_dprintk(1, "Repeat last key\n");
174 data->state = STATE_TRAILER_PULSE;
180 case STATE_BIT_PULSE:
184 if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
187 data->state = STATE_BIT_SPACE;
190 case STATE_BIT_SPACE:
194 data->nec_bits <<= 1;
195 if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
197 else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
201 if (data->count == NEC_NBITS)
202 data->state = STATE_TRAILER_PULSE;
204 data->state = STATE_BIT_PULSE;
208 case STATE_TRAILER_PULSE:
212 if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
215 data->state = STATE_TRAILER_SPACE;
218 case STATE_TRAILER_SPACE:
222 if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
225 address = bitrev8((data->nec_bits >> 24) & 0xff);
226 not_address = bitrev8((data->nec_bits >> 16) & 0xff);
227 command = bitrev8((data->nec_bits >> 8) & 0xff);
228 not_command = bitrev8((data->nec_bits >> 0) & 0xff);
230 if ((command ^ not_command) != 0xff) {
231 IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
236 if ((address ^ not_address) != 0xff) {
238 scancode = address << 16 |
241 IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
244 scancode = address << 8 | command;
245 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
248 ir_keydown(input_dev, scancode, 0);
249 data->state = STATE_INACTIVE;
253 IR_dprintk(1, "NEC decode failed at state %d (%uus %s)\n",
254 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
255 data->state = STATE_INACTIVE;
259 static int ir_nec_register(struct input_dev *input_dev)
261 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
262 struct decoder_data *data;
265 rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
269 data = kzalloc(sizeof(*data), GFP_KERNEL);
271 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
275 data->ir_dev = ir_dev;
278 spin_lock(&decoder_lock);
279 list_add_tail(&data->list, &decoder_list);
280 spin_unlock(&decoder_lock);
285 static int ir_nec_unregister(struct input_dev *input_dev)
287 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
288 static struct decoder_data *data;
290 data = get_decoder_data(ir_dev);
294 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
296 spin_lock(&decoder_lock);
297 list_del(&data->list);
298 spin_unlock(&decoder_lock);
303 static struct ir_raw_handler nec_handler = {
304 .decode = ir_nec_decode,
305 .raw_register = ir_nec_register,
306 .raw_unregister = ir_nec_unregister,
309 static int __init ir_nec_decode_init(void)
311 ir_raw_handler_register(&nec_handler);
313 printk(KERN_INFO "IR NEC protocol handler initialized\n");
317 static void __exit ir_nec_decode_exit(void)
319 ir_raw_handler_unregister(&nec_handler);
322 module_init(ir_nec_decode_init);
323 module_exit(ir_nec_decode_exit);
325 MODULE_LICENSE("GPL");
326 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
327 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
328 MODULE_DESCRIPTION("NEC IR protocol decoder");