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 <media/ir-core.h>
18 #define NEC_UNIT 559979 /* ns */
19 #define NEC_HEADER_MARK (16 * NEC_UNIT)
20 #define NEC_HEADER_SPACE (8 * NEC_UNIT)
21 #define NEC_REPEAT_SPACE (4 * NEC_UNIT)
22 #define NEC_MARK (NEC_UNIT)
23 #define NEC_0_SPACE (NEC_UNIT)
24 #define NEC_1_SPACE (3 * NEC_UNIT)
26 /* Used to register nec_decoder clients */
27 static LIST_HEAD(decoder_list);
28 static spinlock_t decoder_lock;
48 struct list_head list;
49 struct ir_input_dev *ir_dev;
52 /* State machine control */
54 struct nec_code nec_code;
60 * get_decoder_data() - gets decoder data
61 * @input_dev: input device
63 * Returns the struct decoder_data that corresponds to a device
66 static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
68 struct decoder_data *data = NULL;
70 spin_lock(&decoder_lock);
71 list_for_each_entry(data, &decoder_list, list) {
72 if (data->ir_dev == ir_dev)
75 spin_unlock(&decoder_lock);
79 static ssize_t store_enabled(struct device *d,
80 struct device_attribute *mattr,
85 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
86 struct decoder_data *data = get_decoder_data(ir_dev);
91 if (strict_strtoul(buf, 10, &value) || value > 1)
94 data->enabled = value;
99 static ssize_t show_enabled(struct device *d,
100 struct device_attribute *mattr, char *buf)
102 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
103 struct decoder_data *data = get_decoder_data(ir_dev);
109 return sprintf(buf, "1\n");
111 return sprintf(buf, "0\n");
114 static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
116 static struct attribute *decoder_attributes[] = {
117 &dev_attr_enabled.attr,
121 static struct attribute_group decoder_attribute_group = {
122 .name = "nec_decoder",
123 .attrs = decoder_attributes,
128 * ir_nec_decode() - Decode one NEC pulse or space
129 * @input_dev: the struct input_dev descriptor of the device
130 * @ev: event array with type/duration of pulse/space
132 * This function returns -EINVAL if the pulse violates the state machine
134 static int ir_nec_decode(struct input_dev *input_dev,
135 struct ir_raw_event *ev)
137 struct decoder_data *data;
138 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
141 data = get_decoder_data(ir_dev);
145 /* Except for the initial event, what matters is the previous bit */
146 bit = (ev->type & IR_PULSE) ? 1 : 0;
150 /* Discards spurious space last_bits when inactive */
152 /* Very long delays are considered as start events */
153 if (ev->delta.tv_nsec > NEC_HEADER_MARK + NEC_HEADER_SPACE - NEC_UNIT / 2)
154 data->state = STATE_INACTIVE;
156 if (ev->type & IR_START_EVENT)
157 data->state = STATE_INACTIVE;
159 switch (data->state) {
161 if (!bit) /* PULSE marks the start event */
165 data->state = STATE_HEADER_MARK;
166 memset (&data->nec_code, 0, sizeof(data->nec_code));
168 case STATE_HEADER_MARK:
171 if (ev->delta.tv_nsec < NEC_HEADER_MARK - 6 * NEC_UNIT)
173 data->state = STATE_HEADER_SPACE;
175 case STATE_HEADER_SPACE:
178 if (ev->delta.tv_nsec >= NEC_HEADER_SPACE - NEC_UNIT / 2) {
179 data->state = STATE_MARK;
183 if (ev->delta.tv_nsec >= NEC_REPEAT_SPACE - NEC_UNIT / 2) {
184 ir_repeat(input_dev);
185 IR_dprintk(1, "Repeat last key\n");
186 data->state = STATE_TRAILER_MARK;
193 if ((ev->delta.tv_nsec > NEC_MARK + NEC_UNIT / 2) ||
194 (ev->delta.tv_nsec < NEC_MARK - NEC_UNIT / 2))
196 data->state = STATE_SPACE;
202 if ((ev->delta.tv_nsec >= NEC_0_SPACE - NEC_UNIT / 2) &&
203 (ev->delta.tv_nsec < NEC_0_SPACE + NEC_UNIT / 2))
205 else if ((ev->delta.tv_nsec >= NEC_1_SPACE - NEC_UNIT / 2) &&
206 (ev->delta.tv_nsec < NEC_1_SPACE + NEC_UNIT / 2))
209 IR_dprintk(1, "Decode failed at %d-th bit (%s) @%luus\n",
211 last_bit ? "pulse" : "space",
212 (ev->delta.tv_nsec + 500) / 1000);
217 /* Ok, we've got a valid bit. proccess it */
219 int shift = data->count;
222 * NEC transmit bytes on this temporal order:
223 * address | not address | command | not command
226 data->nec_code.address |= 1 << shift;
227 } else if (shift < 16) {
228 data->nec_code.not_address |= 1 << (shift - 8);
229 } else if (shift < 24) {
230 data->nec_code.command |= 1 << (shift - 16);
232 data->nec_code.not_command |= 1 << (shift - 24);
235 if (++data->count == NEC_NBITS) {
238 * Fixme: may need to accept Extended NEC protocol?
240 if ((data->nec_code.command ^ data->nec_code.not_command) != 0xff)
243 if ((data->nec_code.address ^ data->nec_code.not_address) != 0xff) {
245 scancode = data->nec_code.address << 16 |
246 data->nec_code.not_address << 8 |
247 data->nec_code.command;
248 IR_dprintk(1, "NEC scancode 0x%06x\n", scancode);
251 scancode = data->nec_code.address << 8 |
252 data->nec_code.command;
253 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
255 ir_keydown(input_dev, scancode, 0);
257 data->state = STATE_TRAILER_MARK;
259 data->state = STATE_MARK;
261 case STATE_TRAILER_MARK:
264 data->state = STATE_TRAILER_SPACE;
266 case STATE_TRAILER_SPACE:
269 data->state = STATE_INACTIVE;
274 IR_dprintk(1, "NEC decoded failed at state %d (%s) @ %luus\n",
276 bit ? "pulse" : "space",
277 (ev->delta.tv_nsec + 500) / 1000);
279 data->state = STATE_INACTIVE;
283 data->state = STATE_INACTIVE;
284 IR_dprintk(1, "NEC checksum error: received 0x%02x%02x%02x%02x\n",
285 data->nec_code.address,
286 data->nec_code.not_address,
287 data->nec_code.command,
288 data->nec_code.not_command);
292 static int ir_nec_register(struct input_dev *input_dev)
294 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
295 struct decoder_data *data;
298 rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
302 data = kzalloc(sizeof(*data), GFP_KERNEL);
304 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
308 data->ir_dev = ir_dev;
311 spin_lock(&decoder_lock);
312 list_add_tail(&data->list, &decoder_list);
313 spin_unlock(&decoder_lock);
318 static int ir_nec_unregister(struct input_dev *input_dev)
320 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
321 static struct decoder_data *data;
323 data = get_decoder_data(ir_dev);
327 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
329 spin_lock(&decoder_lock);
330 list_del(&data->list);
331 spin_unlock(&decoder_lock);
336 static struct ir_raw_handler nec_handler = {
337 .decode = ir_nec_decode,
338 .raw_register = ir_nec_register,
339 .raw_unregister = ir_nec_unregister,
342 static int __init ir_nec_decode_init(void)
344 ir_raw_handler_register(&nec_handler);
346 printk(KERN_INFO "IR NEC protocol handler initialized\n");
350 static void __exit ir_nec_decode_exit(void)
352 ir_raw_handler_unregister(&nec_handler);
355 module_init(ir_nec_decode_init);
356 module_exit(ir_nec_decode_exit);
358 MODULE_LICENSE("GPL");
359 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
360 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
361 MODULE_DESCRIPTION("NEC IR protocol decoder");