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 PULSE(16)
21 #define NECX_HEADER_PULSE PULSE(8) /* Less common NEC variant */
22 #define NEC_HEADER_SPACE SPACE(8)
23 #define NEC_REPEAT_SPACE SPACE(4)
24 #define NEC_BIT_PULSE PULSE(1)
25 #define NEC_BIT_0_SPACE SPACE(1)
26 #define NEC_BIT_1_SPACE SPACE(3)
28 /* Used to register nec_decoder clients */
29 static LIST_HEAD(decoder_list);
30 static DEFINE_SPINLOCK(decoder_lock);
42 struct list_head list;
43 struct ir_input_dev *ir_dev;
46 /* State machine control */
54 * get_decoder_data() - gets decoder data
55 * @input_dev: input device
57 * Returns the struct decoder_data that corresponds to a device
59 static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
61 struct decoder_data *data = NULL;
63 spin_lock(&decoder_lock);
64 list_for_each_entry(data, &decoder_list, list) {
65 if (data->ir_dev == ir_dev)
68 spin_unlock(&decoder_lock);
72 static ssize_t store_enabled(struct device *d,
73 struct device_attribute *mattr,
78 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
79 struct decoder_data *data = get_decoder_data(ir_dev);
84 if (strict_strtoul(buf, 10, &value) || value > 1)
87 data->enabled = value;
92 static ssize_t show_enabled(struct device *d,
93 struct device_attribute *mattr, char *buf)
95 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
96 struct decoder_data *data = get_decoder_data(ir_dev);
102 return sprintf(buf, "1\n");
104 return sprintf(buf, "0\n");
107 static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
109 static struct attribute *decoder_attributes[] = {
110 &dev_attr_enabled.attr,
114 static struct attribute_group decoder_attribute_group = {
115 .name = "nec_decoder",
116 .attrs = decoder_attributes,
120 * ir_nec_decode() - Decode one NEC pulse or space
121 * @input_dev: the struct input_dev descriptor of the device
122 * @duration: duration in ns of pulse/space
124 * This function returns -EINVAL if the pulse violates the state machine
126 static int ir_nec_decode(struct input_dev *input_dev, s64 duration)
128 struct decoder_data *data;
129 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
132 u8 address, not_address, command, not_command;
134 data = get_decoder_data(ir_dev);
141 if (IS_RESET(duration)) {
142 data->state = STATE_INACTIVE;
146 u = TO_UNITS(duration, NEC_UNIT);
147 if (DURATION(u) == 0)
150 IR_dprintk(2, "NEC decode started at state %d (%i units, %ius)\n",
151 data->state, u, TO_US(duration));
153 switch (data->state) {
156 if (u == NEC_HEADER_PULSE || u == NECX_HEADER_PULSE) {
158 data->state = STATE_HEADER_SPACE;
162 case STATE_HEADER_SPACE:
163 if (u == NEC_HEADER_SPACE) {
164 data->state = STATE_BIT_PULSE;
166 } else if (u == NEC_REPEAT_SPACE) {
167 ir_repeat(input_dev);
168 IR_dprintk(1, "Repeat last key\n");
169 data->state = STATE_TRAILER_PULSE;
174 case STATE_BIT_PULSE:
175 if (u == NEC_BIT_PULSE) {
176 data->state = STATE_BIT_SPACE;
181 case STATE_BIT_SPACE:
182 if (u != NEC_BIT_0_SPACE && u != NEC_BIT_1_SPACE)
185 data->nec_bits <<= 1;
186 if (u == NEC_BIT_1_SPACE)
190 if (data->count != NEC_NBITS) {
191 data->state = STATE_BIT_PULSE;
195 address = bitrev8((data->nec_bits >> 24) & 0xff);
196 not_address = bitrev8((data->nec_bits >> 16) & 0xff);
197 command = bitrev8((data->nec_bits >> 8) & 0xff);
198 not_command = bitrev8((data->nec_bits >> 0) & 0xff);
200 if ((command ^ not_command) != 0xff) {
201 IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
206 if ((address ^ not_address) != 0xff) {
208 scancode = address << 16 |
211 IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
214 scancode = address << 8 | command;
215 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
218 ir_keydown(input_dev, scancode, 0);
219 data->state = STATE_TRAILER_PULSE;
222 case STATE_TRAILER_PULSE:
224 data->state = STATE_TRAILER_SPACE;
229 case STATE_TRAILER_SPACE:
231 data->state = STATE_INACTIVE;
239 IR_dprintk(1, "NEC decode failed at state %d (%i units, %ius)\n",
240 data->state, u, TO_US(duration));
241 data->state = STATE_INACTIVE;
245 static int ir_nec_register(struct input_dev *input_dev)
247 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
248 struct decoder_data *data;
251 rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
255 data = kzalloc(sizeof(*data), GFP_KERNEL);
257 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
261 data->ir_dev = ir_dev;
264 spin_lock(&decoder_lock);
265 list_add_tail(&data->list, &decoder_list);
266 spin_unlock(&decoder_lock);
271 static int ir_nec_unregister(struct input_dev *input_dev)
273 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
274 static struct decoder_data *data;
276 data = get_decoder_data(ir_dev);
280 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
282 spin_lock(&decoder_lock);
283 list_del(&data->list);
284 spin_unlock(&decoder_lock);
289 static struct ir_raw_handler nec_handler = {
290 .decode = ir_nec_decode,
291 .raw_register = ir_nec_register,
292 .raw_unregister = ir_nec_unregister,
295 static int __init ir_nec_decode_init(void)
297 ir_raw_handler_register(&nec_handler);
299 printk(KERN_INFO "IR NEC protocol handler initialized\n");
303 static void __exit ir_nec_decode_exit(void)
305 ir_raw_handler_unregister(&nec_handler);
308 module_init(ir_nec_decode_init);
309 module_exit(ir_nec_decode_exit);
311 MODULE_LICENSE("GPL");
312 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
313 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
314 MODULE_DESCRIPTION("NEC IR protocol decoder");