blob: dd5a4d5f25fdebe832dff99cf48f031b6fd340d2 [file] [log] [blame]
David Härdeman733419b2010-04-08 20:04:30 -03001/* ir-rc5-decoder.c - handle RC5(x) IR Pulse/Space protocol
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -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/*
David Härdeman733419b2010-04-08 20:04:30 -030016 * This code handles 14 bits RC5 protocols and 20 bits RC5x protocols.
17 * There are other variants that use a different number of bits.
18 * This is currently unsupported.
19 * It considers a carrier of 36 kHz, with a total of 14/20 bits, where
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -030020 * the first two bits are start bits, and a third one is a filing bit
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030021 */
22
Mauro Carvalho Chehab3f113e32010-04-08 15:10:27 -030023#include "ir-core-priv.h"
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030024
25#define RC5_NBITS 14
David Härdeman733419b2010-04-08 20:04:30 -030026#define RC5X_NBITS 20
27#define CHECK_RC5X_NBITS 8
28#define RC5X_SPACE SPACE(4)
David Härdeman724e2492010-04-08 13:10:00 -030029#define RC5_UNIT 888888 /* ns */
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030030
31/* Used to register rc5_decoder clients */
32static LIST_HEAD(decoder_list);
Mauro Carvalho Chehab6eb94352010-04-07 16:19:36 -030033static DEFINE_SPINLOCK(decoder_lock);
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030034
35enum rc5_state {
36 STATE_INACTIVE,
David Härdeman724e2492010-04-08 13:10:00 -030037 STATE_BIT_START,
38 STATE_BIT_END,
David Härdeman733419b2010-04-08 20:04:30 -030039 STATE_CHECK_RC5X,
David Härdeman724e2492010-04-08 13:10:00 -030040 STATE_FINISHED,
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030041};
42
43struct decoder_data {
44 struct list_head list;
45 struct ir_input_dev *ir_dev;
46 int enabled:1;
47
48 /* State machine control */
49 enum rc5_state state;
David Härdeman724e2492010-04-08 13:10:00 -030050 u32 rc5_bits;
51 int last_unit;
52 unsigned count;
David Härdeman733419b2010-04-08 20:04:30 -030053 unsigned wanted_bits;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030054};
55
56
57/**
58 * get_decoder_data() - gets decoder data
59 * @input_dev: input device
60 *
61 * Returns the struct decoder_data that corresponds to a device
62 */
63
64static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
65{
66 struct decoder_data *data = NULL;
67
68 spin_lock(&decoder_lock);
69 list_for_each_entry(data, &decoder_list, list) {
70 if (data->ir_dev == ir_dev)
71 break;
72 }
73 spin_unlock(&decoder_lock);
74 return data;
75}
76
77static ssize_t store_enabled(struct device *d,
78 struct device_attribute *mattr,
79 const char *buf,
80 size_t len)
81{
82 unsigned long value;
83 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
84 struct decoder_data *data = get_decoder_data(ir_dev);
85
86 if (!data)
87 return -EINVAL;
88
89 if (strict_strtoul(buf, 10, &value) || value > 1)
90 return -EINVAL;
91
92 data->enabled = value;
93
94 return len;
95}
96
97static ssize_t show_enabled(struct device *d,
98 struct device_attribute *mattr, char *buf)
99{
100 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
101 struct decoder_data *data = get_decoder_data(ir_dev);
102
103 if (!data)
104 return -EINVAL;
105
106 if (data->enabled)
107 return sprintf(buf, "1\n");
108 else
109 return sprintf(buf, "0\n");
110}
111
112static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
113
114static struct attribute *decoder_attributes[] = {
115 &dev_attr_enabled.attr,
116 NULL
117};
118
119static struct attribute_group decoder_attribute_group = {
120 .name = "rc5_decoder",
121 .attrs = decoder_attributes,
122};
123
124/**
David Härdeman724e2492010-04-08 13:10:00 -0300125 * ir_rc5_decode() - Decode one RC-5 pulse or space
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300126 * @input_dev: the struct input_dev descriptor of the device
David Härdeman724e2492010-04-08 13:10:00 -0300127 * @duration: duration of pulse/space in ns
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300128 *
129 * This function returns -EINVAL if the pulse violates the state machine
130 */
David Härdeman724e2492010-04-08 13:10:00 -0300131static int ir_rc5_decode(struct input_dev *input_dev, s64 duration)
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300132{
133 struct decoder_data *data;
134 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
David Härdeman733419b2010-04-08 20:04:30 -0300135 u8 toggle;
David Härdeman724e2492010-04-08 13:10:00 -0300136 u32 scancode;
137 int u;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300138
139 data = get_decoder_data(ir_dev);
140 if (!data)
141 return -EINVAL;
142
Mauro Carvalho Chehab7f20d322010-04-04 14:45:04 -0300143 if (!data->enabled)
144 return 0;
145
David Härdeman724e2492010-04-08 13:10:00 -0300146 if (IS_RESET(duration)) {
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300147 data->state = STATE_INACTIVE;
148 return 0;
149 }
150
David Härdeman724e2492010-04-08 13:10:00 -0300151 u = TO_UNITS(duration, RC5_UNIT);
152 if (DURATION(u) == 0)
153 goto out;
154
155again:
David Härdeman733419b2010-04-08 20:04:30 -0300156 IR_dprintk(2, "RC5(x) decode started at state %i (%i units, %ius)\n",
David Härdeman724e2492010-04-08 13:10:00 -0300157 data->state, u, TO_US(duration));
158
159 if (DURATION(u) == 0 && data->state != STATE_FINISHED)
160 return 0;
161
162 switch (data->state) {
163
164 case STATE_INACTIVE:
165 if (IS_PULSE(u)) {
166 data->state = STATE_BIT_START;
167 data->count = 1;
David Härdeman733419b2010-04-08 20:04:30 -0300168 /* We just need enough bits to get to STATE_CHECK_RC5X */
169 data->wanted_bits = RC5X_NBITS;
David Härdeman724e2492010-04-08 13:10:00 -0300170 DECREASE_DURATION(u, 1);
171 goto again;
172 }
173 break;
174
175 case STATE_BIT_START:
176 if (DURATION(u) == 1) {
177 data->rc5_bits <<= 1;
178 if (IS_SPACE(u))
179 data->rc5_bits |= 1;
180 data->count++;
181 data->last_unit = u;
182
183 /*
184 * If the last bit is zero, a space will merge
185 * with the silence after the command.
186 */
David Härdeman733419b2010-04-08 20:04:30 -0300187 if (IS_PULSE(u) && data->count == data->wanted_bits) {
David Härdeman724e2492010-04-08 13:10:00 -0300188 data->state = STATE_FINISHED;
189 goto again;
190 }
191
192 data->state = STATE_BIT_END;
193 return 0;
194 }
195 break;
196
197 case STATE_BIT_END:
198 if (IS_TRANSITION(u, data->last_unit)) {
David Härdeman733419b2010-04-08 20:04:30 -0300199 if (data->count == data->wanted_bits)
David Härdeman724e2492010-04-08 13:10:00 -0300200 data->state = STATE_FINISHED;
David Härdeman733419b2010-04-08 20:04:30 -0300201 else if (data->count == CHECK_RC5X_NBITS)
202 data->state = STATE_CHECK_RC5X;
David Härdeman724e2492010-04-08 13:10:00 -0300203 else
204 data->state = STATE_BIT_START;
205
206 DECREASE_DURATION(u, 1);
207 goto again;
208 }
209 break;
210
David Härdeman733419b2010-04-08 20:04:30 -0300211 case STATE_CHECK_RC5X:
212 if (IS_SPACE(u) && DURATION(u) >= DURATION(RC5X_SPACE)) {
213 /* RC5X */
214 data->wanted_bits = RC5X_NBITS;
215 DECREASE_DURATION(u, DURATION(RC5X_SPACE));
216 } else {
217 /* RC5 */
218 data->wanted_bits = RC5_NBITS;
219 }
220 data->state = STATE_BIT_START;
221 goto again;
David Härdeman724e2492010-04-08 13:10:00 -0300222
David Härdeman733419b2010-04-08 20:04:30 -0300223 case STATE_FINISHED:
224 if (data->wanted_bits == RC5X_NBITS) {
225 /* RC5X */
226 u8 xdata, command, system;
227 xdata = (data->rc5_bits & 0x0003F) >> 0;
228 command = (data->rc5_bits & 0x00FC0) >> 6;
229 system = (data->rc5_bits & 0x1F000) >> 12;
230 toggle = (data->rc5_bits & 0x20000) ? 1 : 0;
231 command += (data->rc5_bits & 0x01000) ? 0 : 0x40;
232 scancode = system << 16 | command << 8 | xdata;
233
234 IR_dprintk(1, "RC5X scancode 0x%06x (toggle: %u)\n",
235 scancode, toggle);
236
237 } else {
238 /* RC5 */
239 u8 command, system;
240 command = (data->rc5_bits & 0x0003F) >> 0;
241 system = (data->rc5_bits & 0x007C0) >> 6;
242 toggle = (data->rc5_bits & 0x00800) ? 1 : 0;
243 command += (data->rc5_bits & 0x01000) ? 0 : 0x40;
244 scancode = system << 8 | command;
245
246 IR_dprintk(1, "RC5 scancode 0x%04x (toggle: %u)\n",
247 scancode, toggle);
248 }
249
David Härdeman724e2492010-04-08 13:10:00 -0300250 ir_keydown(input_dev, scancode, toggle);
251 data->state = STATE_INACTIVE;
252 return 0;
253 }
254
255out:
David Härdeman733419b2010-04-08 20:04:30 -0300256 IR_dprintk(1, "RC5(x) decode failed at state %i (%i units, %ius)\n",
David Härdeman724e2492010-04-08 13:10:00 -0300257 data->state, u, TO_US(duration));
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300258 data->state = STATE_INACTIVE;
259 return -EINVAL;
260}
261
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300262static int ir_rc5_register(struct input_dev *input_dev)
263{
264 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
265 struct decoder_data *data;
266 int rc;
267
268 rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
269 if (rc < 0)
270 return rc;
271
272 data = kzalloc(sizeof(*data), GFP_KERNEL);
273 if (!data) {
274 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
275 return -ENOMEM;
276 }
277
278 data->ir_dev = ir_dev;
279 data->enabled = 1;
280
281 spin_lock(&decoder_lock);
282 list_add_tail(&data->list, &decoder_list);
283 spin_unlock(&decoder_lock);
284
285 return 0;
286}
287
288static int ir_rc5_unregister(struct input_dev *input_dev)
289{
290 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
291 static struct decoder_data *data;
292
293 data = get_decoder_data(ir_dev);
294 if (!data)
295 return 0;
296
297 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
298
299 spin_lock(&decoder_lock);
300 list_del(&data->list);
301 spin_unlock(&decoder_lock);
302
303 return 0;
304}
305
306static struct ir_raw_handler rc5_handler = {
307 .decode = ir_rc5_decode,
308 .raw_register = ir_rc5_register,
309 .raw_unregister = ir_rc5_unregister,
310};
311
312static int __init ir_rc5_decode_init(void)
313{
314 ir_raw_handler_register(&rc5_handler);
315
David Härdeman733419b2010-04-08 20:04:30 -0300316 printk(KERN_INFO "IR RC5(x) protocol handler initialized\n");
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300317 return 0;
318}
319
320static void __exit ir_rc5_decode_exit(void)
321{
322 ir_raw_handler_unregister(&rc5_handler);
323}
324
325module_init(ir_rc5_decode_init);
326module_exit(ir_rc5_decode_exit);
327
328MODULE_LICENSE("GPL");
329MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
330MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
David Härdeman733419b2010-04-08 20:04:30 -0300331MODULE_DESCRIPTION("RC5(x) IR protocol decoder");