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