blob: 9d1ada95aa724197ccac2c3470c2733f60e3f677 [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 Chehab67780d62010-04-03 20:33:00 -030017#define NEC_NBITS 32
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030018#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)
Mauro Carvalho Chehab67780d62010-04-03 20:33:00 -030023#define NEC_0_SPACE (NEC_UNIT)
24#define NEC_1_SPACE (3 * NEC_UNIT)
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -030025
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030026/* Used to register nec_decoder clients */
27static LIST_HEAD(decoder_list);
28static spinlock_t decoder_lock;
29
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030030enum nec_state {
31 STATE_INACTIVE,
32 STATE_HEADER_MARK,
33 STATE_HEADER_SPACE,
34 STATE_MARK,
35 STATE_SPACE,
36 STATE_TRAILER_MARK,
37 STATE_TRAILER_SPACE,
38};
39
40struct nec_code {
41 u8 address;
42 u8 not_address;
43 u8 command;
44 u8 not_command;
45};
46
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030047struct decoder_data {
48 struct list_head list;
49 struct ir_input_dev *ir_dev;
50 int enabled:1;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030051
52 /* State machine control */
53 enum nec_state state;
54 struct nec_code nec_code;
55 unsigned count;
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030056};
57
58
59/**
60 * get_decoder_data() - gets decoder data
61 * @input_dev: input device
62 *
63 * Returns the struct decoder_data that corresponds to a device
64 */
65
66static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
67{
68 struct decoder_data *data = NULL;
69
70 spin_lock(&decoder_lock);
71 list_for_each_entry(data, &decoder_list, list) {
72 if (data->ir_dev == ir_dev)
73 break;
74 }
75 spin_unlock(&decoder_lock);
76 return data;
77}
78
79static ssize_t store_enabled(struct device *d,
80 struct device_attribute *mattr,
81 const char *buf,
82 size_t len)
83{
84 unsigned long value;
85 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
86 struct decoder_data *data = get_decoder_data(ir_dev);
87
88 if (!data)
89 return -EINVAL;
90
91 if (strict_strtoul(buf, 10, &value) || value > 1)
92 return -EINVAL;
93
94 data->enabled = value;
95
96 return len;
97}
98
99static ssize_t show_enabled(struct device *d,
100 struct device_attribute *mattr, char *buf)
101{
102 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
103 struct decoder_data *data = get_decoder_data(ir_dev);
104
105 if (!data)
106 return -EINVAL;
107
108 if (data->enabled)
109 return sprintf(buf, "1\n");
110 else
111 return sprintf(buf, "0\n");
112}
113
114static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
115
116static struct attribute *decoder_attributes[] = {
117 &dev_attr_enabled.attr,
118 NULL
119};
120
121static struct attribute_group decoder_attribute_group = {
122 .name = "nec_decoder",
123 .attrs = decoder_attributes,
124};
125
126
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300127/**
Mauro Carvalho Chehab587835a2010-04-04 10:44:51 -0300128 * ir_nec_decode() - Decode one NEC pulse or space
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300129 * @input_dev: the struct input_dev descriptor of the device
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300130 * @ev: event array with type/duration of pulse/space
131 *
132 * This function returns -EINVAL if the pulse violates the state machine
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300133 */
Mauro Carvalho Chehab587835a2010-04-04 10:44:51 -0300134static int ir_nec_decode(struct input_dev *input_dev,
135 struct ir_raw_event *ev)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300136{
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300137 struct decoder_data *data;
138 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
139 int bit, last_bit;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300140
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300141 data = get_decoder_data(ir_dev);
142 if (!data)
143 return -EINVAL;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300144
Mauro Carvalho Chehab7f20d322010-04-04 14:45:04 -0300145 if (!data->enabled)
146 return 0;
147
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300148 /* Except for the initial event, what matters is the previous bit */
149 bit = (ev->type & IR_PULSE) ? 1 : 0;
150
151 last_bit = !bit;
152
153 /* Discards spurious space last_bits when inactive */
154
155 /* Very long delays are considered as start events */
156 if (ev->delta.tv_nsec > NEC_HEADER_MARK + NEC_HEADER_SPACE - NEC_UNIT / 2)
157 data->state = STATE_INACTIVE;
158
159 if (ev->type & IR_START_EVENT)
160 data->state = STATE_INACTIVE;
161
162 switch (data->state) {
163 case STATE_INACTIVE:
164 if (!bit) /* PULSE marks the start event */
165 return 0;
166
167 data->count = 0;
168 data->state = STATE_HEADER_MARK;
169 memset (&data->nec_code, 0, sizeof(data->nec_code));
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300170 return 0;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300171 case STATE_HEADER_MARK:
172 if (!last_bit)
173 goto err;
174 if (ev->delta.tv_nsec < NEC_HEADER_MARK - 6 * NEC_UNIT)
175 goto err;
176 data->state = STATE_HEADER_SPACE;
177 return 0;
178 case STATE_HEADER_SPACE:
179 if (last_bit)
180 goto err;
181 if (ev->delta.tv_nsec >= NEC_HEADER_SPACE - NEC_UNIT / 2) {
182 data->state = STATE_MARK;
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300183 return 0;
184 }
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300185
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300186 if (ev->delta.tv_nsec >= NEC_REPEAT_SPACE - NEC_UNIT / 2) {
187 ir_repeat(input_dev);
188 IR_dprintk(1, "Repeat last key\n");
189 data->state = STATE_TRAILER_MARK;
190 return 0;
191 }
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300192 goto err;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300193 case STATE_MARK:
194 if (!last_bit)
195 goto err;
196 if ((ev->delta.tv_nsec > NEC_MARK + NEC_UNIT / 2) ||
197 (ev->delta.tv_nsec < NEC_MARK - NEC_UNIT / 2))
198 goto err;
199 data->state = STATE_SPACE;
200 return 0;
201 case STATE_SPACE:
202 if (last_bit)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300203 goto err;
204
Mauro Carvalho Chehab67780d62010-04-03 20:33:00 -0300205 if ((ev->delta.tv_nsec >= NEC_0_SPACE - NEC_UNIT / 2) &&
206 (ev->delta.tv_nsec < NEC_0_SPACE + NEC_UNIT / 2))
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300207 bit = 0;
Mauro Carvalho Chehab67780d62010-04-03 20:33:00 -0300208 else if ((ev->delta.tv_nsec >= NEC_1_SPACE - NEC_UNIT / 2) &&
209 (ev->delta.tv_nsec < NEC_1_SPACE + NEC_UNIT / 2))
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300210 bit = 1;
211 else {
212 IR_dprintk(1, "Decode failed at %d-th bit (%s) @%luus\n",
213 data->count,
214 last_bit ? "pulse" : "space",
215 (ev->delta.tv_nsec + 500) / 1000);
216
217 goto err2;
218 }
219
220 /* Ok, we've got a valid bit. proccess it */
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300221 if (bit) {
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300222 int shift = data->count;
223
224 /*
225 * NEC transmit bytes on this temporal order:
226 * address | not address | command | not command
227 */
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300228 if (shift < 8) {
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300229 data->nec_code.address |= 1 << shift;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300230 } else if (shift < 16) {
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300231 data->nec_code.not_address |= 1 << (shift - 8);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300232 } else if (shift < 24) {
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300233 data->nec_code.command |= 1 << (shift - 16);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300234 } else {
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300235 data->nec_code.not_command |= 1 << (shift - 24);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300236 }
237 }
Mauro Carvalho Chehab67780d62010-04-03 20:33:00 -0300238 if (++data->count == NEC_NBITS) {
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300239 u32 scancode;
240 /*
241 * Fixme: may need to accept Extended NEC protocol?
242 */
243 if ((data->nec_code.command ^ data->nec_code.not_command) != 0xff)
244 goto checksum_err;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300245
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300246 if ((data->nec_code.address ^ data->nec_code.not_address) != 0xff) {
247 /* Extended NEC */
248 scancode = data->nec_code.address << 16 |
249 data->nec_code.not_address << 8 |
250 data->nec_code.command;
251 IR_dprintk(1, "NEC scancode 0x%06x\n", scancode);
252 } else {
253 /* normal NEC */
254 scancode = data->nec_code.address << 8 |
255 data->nec_code.command;
256 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
257 }
258 ir_keydown(input_dev, scancode, 0);
259
260 data->state = STATE_TRAILER_MARK;
261 } else
262 data->state = STATE_MARK;
263 return 0;
264 case STATE_TRAILER_MARK:
265 if (!last_bit)
266 goto err;
267 data->state = STATE_TRAILER_SPACE;
268 return 0;
269 case STATE_TRAILER_SPACE:
270 if (last_bit)
271 goto err;
272 data->state = STATE_INACTIVE;
273 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300274 }
275
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300276err:
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300277 IR_dprintk(1, "NEC decoded failed at state %d (%s) @ %luus\n",
278 data->state,
279 bit ? "pulse" : "space",
280 (ev->delta.tv_nsec + 500) / 1000);
281err2:
282 data->state = STATE_INACTIVE;
283 return -EINVAL;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300284
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300285checksum_err:
286 data->state = STATE_INACTIVE;
287 IR_dprintk(1, "NEC checksum error: received 0x%02x%02x%02x%02x\n",
288 data->nec_code.address,
289 data->nec_code.not_address,
290 data->nec_code.command,
291 data->nec_code.not_command);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300292 return -EINVAL;
293}
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300294
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300295static int ir_nec_register(struct input_dev *input_dev)
296{
297 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
298 struct decoder_data *data;
299 int rc;
300
301 rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
302 if (rc < 0)
303 return rc;
304
305 data = kzalloc(sizeof(*data), GFP_KERNEL);
306 if (!data) {
307 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
308 return -ENOMEM;
309 }
310
311 data->ir_dev = ir_dev;
312 data->enabled = 1;
313
314 spin_lock(&decoder_lock);
315 list_add_tail(&data->list, &decoder_list);
316 spin_unlock(&decoder_lock);
317
318 return 0;
319}
320
321static int ir_nec_unregister(struct input_dev *input_dev)
322{
323 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
324 static struct decoder_data *data;
325
326 data = get_decoder_data(ir_dev);
327 if (!data)
328 return 0;
329
330 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
331
332 spin_lock(&decoder_lock);
333 list_del(&data->list);
334 spin_unlock(&decoder_lock);
335
336 return 0;
337}
338
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300339static struct ir_raw_handler nec_handler = {
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300340 .decode = ir_nec_decode,
341 .raw_register = ir_nec_register,
342 .raw_unregister = ir_nec_unregister,
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300343};
344
345static int __init ir_nec_decode_init(void)
346{
347 ir_raw_handler_register(&nec_handler);
348
349 printk(KERN_INFO "IR NEC protocol handler initialized\n");
350 return 0;
351}
352
353static void __exit ir_nec_decode_exit(void)
354{
355 ir_raw_handler_unregister(&nec_handler);
356}
357
358module_init(ir_nec_decode_init);
359module_exit(ir_nec_decode_exit);
360
361MODULE_LICENSE("GPL");
362MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
363MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
364MODULE_DESCRIPTION("NEC IR protocol decoder");