blob: db62c652dfc590ed1cbef3ee2317848159ae1306 [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 */
David Härdemane40b1122010-04-15 18:46:00 -030020#define NEC_HEADER_PULSE (16 * NEC_UNIT)
21#define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */
22#define NEC_HEADER_SPACE (8 * NEC_UNIT)
23#define NEC_REPEAT_SPACE (8 * NEC_UNIT)
24#define NEC_BIT_PULSE (1 * NEC_UNIT)
25#define NEC_BIT_0_SPACE (1 * NEC_UNIT)
26#define NEC_BIT_1_SPACE (3 * NEC_UNIT)
27#define NEC_TRAILER_PULSE (1 * NEC_UNIT)
28#define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -030029
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030030/* Used to register nec_decoder clients */
31static LIST_HEAD(decoder_list);
Mauro Carvalho Chehab6eb94352010-04-07 16:19:36 -030032static DEFINE_SPINLOCK(decoder_lock);
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030033
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030034enum nec_state {
35 STATE_INACTIVE,
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030036 STATE_HEADER_SPACE,
David Härdeman724e2492010-04-08 13:10:00 -030037 STATE_BIT_PULSE,
38 STATE_BIT_SPACE,
39 STATE_TRAILER_PULSE,
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030040 STATE_TRAILER_SPACE,
41};
42
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030043struct decoder_data {
44 struct list_head list;
45 struct ir_input_dev *ir_dev;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030046
47 /* State machine control */
48 enum nec_state state;
David Härdeman724e2492010-04-08 13:10:00 -030049 u32 nec_bits;
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030050 unsigned count;
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030051};
52
53
54/**
55 * get_decoder_data() - gets decoder data
56 * @input_dev: input device
57 *
58 * Returns the struct decoder_data that corresponds to a device
59 */
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -030060static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
61{
62 struct decoder_data *data = NULL;
63
64 spin_lock(&decoder_lock);
65 list_for_each_entry(data, &decoder_list, list) {
66 if (data->ir_dev == ir_dev)
67 break;
68 }
69 spin_unlock(&decoder_lock);
70 return data;
71}
72
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -030073/**
Mauro Carvalho Chehab587835a2010-04-04 10:44:51 -030074 * ir_nec_decode() - Decode one NEC pulse or space
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -030075 * @input_dev: the struct input_dev descriptor of the device
David Härdemane40b1122010-04-15 18:46:00 -030076 * @duration: the struct ir_raw_event descriptor of the pulse/space
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030077 *
78 * This function returns -EINVAL if the pulse violates the state machine
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -030079 */
David Härdemane40b1122010-04-15 18:46:00 -030080static int ir_nec_decode(struct input_dev *input_dev, struct ir_raw_event ev)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030081{
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030082 struct decoder_data *data;
83 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
David Härdeman724e2492010-04-08 13:10:00 -030084 u32 scancode;
85 u8 address, not_address, command, not_command;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030086
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030087 data = get_decoder_data(ir_dev);
88 if (!data)
89 return -EINVAL;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030090
David Härdeman667c9eb2010-06-13 17:29:31 -030091 if (!(ir_dev->raw->enabled_protocols & IR_TYPE_NEC))
Mauro Carvalho Chehab7f20d322010-04-04 14:45:04 -030092 return 0;
93
David Härdemane40b1122010-04-15 18:46:00 -030094 if (IS_RESET(ev)) {
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -030095 data->state = STATE_INACTIVE;
96 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030097 }
98
David Härdemane40b1122010-04-15 18:46:00 -030099 IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
100 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
David Härdeman724e2492010-04-08 13:10:00 -0300101
102 switch (data->state) {
103
104 case STATE_INACTIVE:
David Härdemane40b1122010-04-15 18:46:00 -0300105 if (!ev.pulse)
106 break;
107
108 if (!eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT / 2) &&
109 !eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
110 break;
111
112 data->count = 0;
113 data->state = STATE_HEADER_SPACE;
David Härdeman724e2492010-04-08 13:10:00 -0300114 return 0;
115
116 case STATE_HEADER_SPACE:
David Härdemane40b1122010-04-15 18:46:00 -0300117 if (ev.pulse)
118 break;
119
120 if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT / 2)) {
David Härdeman724e2492010-04-08 13:10:00 -0300121 data->state = STATE_BIT_PULSE;
122 return 0;
David Härdemane40b1122010-04-15 18:46:00 -0300123 } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
David Härdeman724e2492010-04-08 13:10:00 -0300124 ir_repeat(input_dev);
125 IR_dprintk(1, "Repeat last key\n");
126 data->state = STATE_TRAILER_PULSE;
127 return 0;
128 }
David Härdemane40b1122010-04-15 18:46:00 -0300129
David Härdeman724e2492010-04-08 13:10:00 -0300130 break;
131
132 case STATE_BIT_PULSE:
David Härdemane40b1122010-04-15 18:46:00 -0300133 if (!ev.pulse)
134 break;
135
136 if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
137 break;
138
139 data->state = STATE_BIT_SPACE;
140 return 0;
David Härdeman724e2492010-04-08 13:10:00 -0300141
142 case STATE_BIT_SPACE:
David Härdemane40b1122010-04-15 18:46:00 -0300143 if (ev.pulse)
David Härdeman724e2492010-04-08 13:10:00 -0300144 break;
145
146 data->nec_bits <<= 1;
David Härdemane40b1122010-04-15 18:46:00 -0300147 if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
David Härdeman724e2492010-04-08 13:10:00 -0300148 data->nec_bits |= 1;
David Härdemane40b1122010-04-15 18:46:00 -0300149 else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
150 break;
David Härdeman724e2492010-04-08 13:10:00 -0300151 data->count++;
152
David Härdemane40b1122010-04-15 18:46:00 -0300153 if (data->count == NEC_NBITS)
154 data->state = STATE_TRAILER_PULSE;
155 else
David Härdeman724e2492010-04-08 13:10:00 -0300156 data->state = STATE_BIT_PULSE;
David Härdemane40b1122010-04-15 18:46:00 -0300157
158 return 0;
159
160 case STATE_TRAILER_PULSE:
161 if (!ev.pulse)
162 break;
163
164 if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
165 break;
166
167 data->state = STATE_TRAILER_SPACE;
168 return 0;
169
170 case STATE_TRAILER_SPACE:
171 if (ev.pulse)
172 break;
173
174 if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
175 break;
David Härdeman724e2492010-04-08 13:10:00 -0300176
177 address = bitrev8((data->nec_bits >> 24) & 0xff);
178 not_address = bitrev8((data->nec_bits >> 16) & 0xff);
179 command = bitrev8((data->nec_bits >> 8) & 0xff);
180 not_command = bitrev8((data->nec_bits >> 0) & 0xff);
181
182 if ((command ^ not_command) != 0xff) {
183 IR_dprintk(1, "NEC checksum error: received 0x%08x\n",
184 data->nec_bits);
185 break;
186 }
187
188 if ((address ^ not_address) != 0xff) {
189 /* Extended NEC */
190 scancode = address << 16 |
191 not_address << 8 |
192 command;
193 IR_dprintk(1, "NEC (Ext) scancode 0x%06x\n", scancode);
194 } else {
David Härdemane40b1122010-04-15 18:46:00 -0300195 /* Normal NEC */
David Härdeman724e2492010-04-08 13:10:00 -0300196 scancode = address << 8 | command;
197 IR_dprintk(1, "NEC scancode 0x%04x\n", scancode);
198 }
199
200 ir_keydown(input_dev, scancode, 0);
David Härdemane40b1122010-04-15 18:46:00 -0300201 data->state = STATE_INACTIVE;
David Härdeman724e2492010-04-08 13:10:00 -0300202 return 0;
David Härdeman724e2492010-04-08 13:10:00 -0300203 }
204
David Härdemane40b1122010-04-15 18:46:00 -0300205 IR_dprintk(1, "NEC decode failed at state %d (%uus %s)\n",
206 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
Mauro Carvalho Chehab2f16f632010-04-03 18:51:50 -0300207 data->state = STATE_INACTIVE;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300208 return -EINVAL;
209}
Mauro Carvalho Chehabada39632010-03-21 12:24:24 -0300210
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300211static int ir_nec_register(struct input_dev *input_dev)
212{
213 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
214 struct decoder_data *data;
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300215
216 data = kzalloc(sizeof(*data), GFP_KERNEL);
David Härdeman667c9eb2010-06-13 17:29:31 -0300217 if (!data)
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300218 return -ENOMEM;
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300219
220 data->ir_dev = ir_dev;
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300221
222 spin_lock(&decoder_lock);
223 list_add_tail(&data->list, &decoder_list);
224 spin_unlock(&decoder_lock);
225
226 return 0;
227}
228
229static int ir_nec_unregister(struct input_dev *input_dev)
230{
231 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
232 static struct decoder_data *data;
233
234 data = get_decoder_data(ir_dev);
235 if (!data)
236 return 0;
237
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300238 spin_lock(&decoder_lock);
239 list_del(&data->list);
240 spin_unlock(&decoder_lock);
241
242 return 0;
243}
244
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300245static struct ir_raw_handler nec_handler = {
David Härdeman667c9eb2010-06-13 17:29:31 -0300246 .protocols = IR_TYPE_NEC,
Mauro Carvalho Chehab20d5f112010-03-25 23:49:46 -0300247 .decode = ir_nec_decode,
248 .raw_register = ir_nec_register,
249 .raw_unregister = ir_nec_unregister,
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300250};
251
252static int __init ir_nec_decode_init(void)
253{
254 ir_raw_handler_register(&nec_handler);
255
256 printk(KERN_INFO "IR NEC protocol handler initialized\n");
257 return 0;
258}
259
260static void __exit ir_nec_decode_exit(void)
261{
262 ir_raw_handler_unregister(&nec_handler);
263}
264
265module_init(ir_nec_decode_init);
266module_exit(ir_nec_decode_exit);
267
268MODULE_LICENSE("GPL");
269MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
270MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
271MODULE_DESCRIPTION("NEC IR protocol decoder");