blob: 14609d9580a8818c7dad7b28bf774017acfd40c3 [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
David Härdeman724e2492010-04-08 13:10:00 -030015#include <linux/bitrev.h>
Mauro Carvalho Chehab3f113e32010-04-08 15:10:27 -030016#include "ir-core-priv.h"
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030017
Mauro Carvalho Chehab67780d62010-04-03 20:33:00 -030018#define NEC_NBITS 32
David Härdeman724e2492010-04-08 13:10:00 -030019#define NEC_UNIT 562500 /* ns */
20#define NEC_HEADER_PULSE PULSE(16)
David Härdeman25bb10c2010-04-08 20:04:35 -030021#define NECX_HEADER_PULSE PULSE(8) /* Less common NEC variant */
David Härdeman724e2492010-04-08 13:10:00 -030022#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)
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -030027
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030028/* Used to register nec_decoder clients */
29static LIST_HEAD(decoder_list);
Mauro Carvalho Chehab6eb94352010-04-07 16:19:36 -030030static DEFINE_SPINLOCK(decoder_lock);
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030031
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030032enum nec_state {
33 STATE_INACTIVE,
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030034 STATE_HEADER_SPACE,
David Härdeman724e2492010-04-08 13:10:00 -030035 STATE_BIT_PULSE,
36 STATE_BIT_SPACE,
37 STATE_TRAILER_PULSE,
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030038 STATE_TRAILER_SPACE,
39};
40
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030041struct decoder_data {
42 struct list_head list;
43 struct ir_input_dev *ir_dev;
44 int enabled:1;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030045
46 /* State machine control */
47 enum nec_state state;
David Härdeman724e2492010-04-08 13:10:00 -030048 u32 nec_bits;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030049 unsigned count;
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030050};
51
52
53/**
54 * get_decoder_data() - gets decoder data
55 * @input_dev: input device
56 *
57 * Returns the struct decoder_data that corresponds to a device
58 */
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030059static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
60{
61 struct decoder_data *data = NULL;
62
63 spin_lock(&decoder_lock);
64 list_for_each_entry(data, &decoder_list, list) {
65 if (data->ir_dev == ir_dev)
66 break;
67 }
68 spin_unlock(&decoder_lock);
69 return data;
70}
71
72static ssize_t store_enabled(struct device *d,
73 struct device_attribute *mattr,
74 const char *buf,
75 size_t len)
76{
77 unsigned long value;
78 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
79 struct decoder_data *data = get_decoder_data(ir_dev);
80
81 if (!data)
82 return -EINVAL;
83
84 if (strict_strtoul(buf, 10, &value) || value > 1)
85 return -EINVAL;
86
87 data->enabled = value;
88
89 return len;
90}
91
92static ssize_t show_enabled(struct device *d,
93 struct device_attribute *mattr, char *buf)
94{
95 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
96 struct decoder_data *data = get_decoder_data(ir_dev);
97
98 if (!data)
99 return -EINVAL;
100
101 if (data->enabled)
102 return sprintf(buf, "1\n");
103 else
104 return sprintf(buf, "0\n");
105}
106
107static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
108
109static struct attribute *decoder_attributes[] = {
110 &dev_attr_enabled.attr,
111 NULL
112};
113
114static struct attribute_group decoder_attribute_group = {
115 .name = "nec_decoder",
116 .attrs = decoder_attributes,
117};
118
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300119/**
Mauro Carvalho Chehab587835a2010-04-04 10:44:51 -0300120 * ir_nec_decode() - Decode one NEC pulse or space
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300121 * @input_dev: the struct input_dev descriptor of the device
David Härdeman724e2492010-04-08 13:10:00 -0300122 * @duration: duration in ns of pulse/space
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300123 *
124 * This function returns -EINVAL if the pulse violates the state machine
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300125 */
David Härdeman724e2492010-04-08 13:10:00 -0300126static int ir_nec_decode(struct input_dev *input_dev, s64 duration)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300127{
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300128 struct decoder_data *data;
129 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
David Härdeman724e2492010-04-08 13:10:00 -0300130 int u;
131 u32 scancode;
132 u8 address, not_address, command, not_command;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300133
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300134 data = get_decoder_data(ir_dev);
135 if (!data)
136 return -EINVAL;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300137
Mauro Carvalho Chehab7f20d322010-04-04 14:45:04 -0300138 if (!data->enabled)
139 return 0;
140
David Härdeman724e2492010-04-08 13:10:00 -0300141 if (IS_RESET(duration)) {
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300142 data->state = STATE_INACTIVE;
143 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300144 }
145
David Härdeman724e2492010-04-08 13:10:00 -0300146 u = TO_UNITS(duration, NEC_UNIT);
147 if (DURATION(u) == 0)
148 goto out;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300149
David Härdeman724e2492010-04-08 13:10:00 -0300150 IR_dprintk(2, "NEC decode started at state %d (%i units, %ius)\n",
151 data->state, u, TO_US(duration));
152
153 switch (data->state) {
154
155 case STATE_INACTIVE:
David Härdeman25bb10c2010-04-08 20:04:35 -0300156 if (u == NEC_HEADER_PULSE || u == NECX_HEADER_PULSE) {
David Härdeman724e2492010-04-08 13:10:00 -0300157 data->count = 0;
158 data->state = STATE_HEADER_SPACE;
159 }
160 return 0;
161
162 case STATE_HEADER_SPACE:
163 if (u == NEC_HEADER_SPACE) {
164 data->state = STATE_BIT_PULSE;
165 return 0;
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;
170 return 0;
171 }
172 break;
173
174 case STATE_BIT_PULSE:
175 if (u == NEC_BIT_PULSE) {
176 data->state = STATE_BIT_SPACE;
177 return 0;
178 }
179 break;
180
181 case STATE_BIT_SPACE:
182 if (u != NEC_BIT_0_SPACE && u != NEC_BIT_1_SPACE)
183 break;
184
185 data->nec_bits <<= 1;
186 if (u == NEC_BIT_1_SPACE)
187 data->nec_bits |= 1;
188 data->count++;
189
190 if (data->count != NEC_NBITS) {
191 data->state = STATE_BIT_PULSE;
192 return 0;
193 }
194
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);
199
200 if ((command ^ not_command) != 0xff) {
201 IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
202 data->nec_bits);
203 break;
204 }
205
206 if ((address ^ not_address) != 0xff) {
207 /* Extended NEC */
208 scancode = address << 16 |
209 not_address << 8 |
210 command;
211 IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
212 } else {
213 /* normal NEC */
214 scancode = address << 8 | command;
215 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
216 }
217
218 ir_keydown(input_dev, scancode, 0);
219 data->state = STATE_TRAILER_PULSE;
220 return 0;
221
222 case STATE_TRAILER_PULSE:
223 if (u > 0) {
224 data->state = STATE_TRAILER_SPACE;
225 return 0;
226 }
227 break;
228
229 case STATE_TRAILER_SPACE:
230 if (u < 0) {
231 data->state = STATE_INACTIVE;
232 return 0;
233 }
234
235 break;
236 }
237
238out:
239 IR_dprintk(1, "NEC decode failed at state %d (%i units, %ius)\n",
240 data->state, u, TO_US(duration));
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300241 data->state = STATE_INACTIVE;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300242 return -EINVAL;
243}
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300244
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300245static int ir_nec_register(struct input_dev *input_dev)
246{
247 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
248 struct decoder_data *data;
249 int rc;
250
251 rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
252 if (rc < 0)
253 return rc;
254
255 data = kzalloc(sizeof(*data), GFP_KERNEL);
256 if (!data) {
257 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
258 return -ENOMEM;
259 }
260
261 data->ir_dev = ir_dev;
262 data->enabled = 1;
263
264 spin_lock(&decoder_lock);
265 list_add_tail(&data->list, &decoder_list);
266 spin_unlock(&decoder_lock);
267
268 return 0;
269}
270
271static int ir_nec_unregister(struct input_dev *input_dev)
272{
273 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
274 static struct decoder_data *data;
275
276 data = get_decoder_data(ir_dev);
277 if (!data)
278 return 0;
279
280 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
281
282 spin_lock(&decoder_lock);
283 list_del(&data->list);
284 spin_unlock(&decoder_lock);
285
286 return 0;
287}
288
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300289static struct ir_raw_handler nec_handler = {
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300290 .decode = ir_nec_decode,
291 .raw_register = ir_nec_register,
292 .raw_unregister = ir_nec_unregister,
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300293};
294
295static int __init ir_nec_decode_init(void)
296{
297 ir_raw_handler_register(&nec_handler);
298
299 printk(KERN_INFO "IR NEC protocol handler initialized\n");
300 return 0;
301}
302
303static void __exit ir_nec_decode_exit(void)
304{
305 ir_raw_handler_unregister(&nec_handler);
306}
307
308module_init(ir_nec_decode_init);
309module_exit(ir_nec_decode_exit);
310
311MODULE_LICENSE("GPL");
312MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
313MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
314MODULE_DESCRIPTION("NEC IR protocol decoder");