blob: 1d0857b69089bf311d6fb8aca5fce5b014480eac [file] [log] [blame]
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -03001/* ir-rc5-decoder.c - handle RC-5 IR Pulse/Space protocol
2 *
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/*
16 * This code only handles 14 bits RC-5 protocols. There are other variants
17 * that use a different number of bits. This is currently unsupported
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -030018 * It considers a carrier of 36 kHz, with a total of 14 bits, where
19 * the first two bits are start bits, and a third one is a filing bit
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030020 */
21
22#include <media/ir-core.h>
23
24#define RC5_NBITS 14
David Härdeman724e2492010-04-08 13:10:00 -030025#define RC5_UNIT 888888 /* ns */
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030026
27/* Used to register rc5_decoder clients */
28static LIST_HEAD(decoder_list);
Mauro Carvalho Chehab6eb94352010-04-07 16:19:36 -030029static DEFINE_SPINLOCK(decoder_lock);
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030030
31enum rc5_state {
32 STATE_INACTIVE,
David Härdeman724e2492010-04-08 13:10:00 -030033 STATE_BIT_START,
34 STATE_BIT_END,
35 STATE_FINISHED,
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030036};
37
38struct decoder_data {
39 struct list_head list;
40 struct ir_input_dev *ir_dev;
41 int enabled:1;
42
43 /* State machine control */
44 enum rc5_state state;
David Härdeman724e2492010-04-08 13:10:00 -030045 u32 rc5_bits;
46 int last_unit;
47 unsigned count;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030048};
49
50
51/**
52 * get_decoder_data() - gets decoder data
53 * @input_dev: input device
54 *
55 * Returns the struct decoder_data that corresponds to a device
56 */
57
58static 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 = "rc5_decoder",
115 .attrs = decoder_attributes,
116};
117
118/**
David Härdeman724e2492010-04-08 13:10:00 -0300119 * ir_rc5_decode() - Decode one RC-5 pulse or space
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300120 * @input_dev: the struct input_dev descriptor of the device
David Härdeman724e2492010-04-08 13:10:00 -0300121 * @duration: duration of pulse/space in ns
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300122 *
123 * This function returns -EINVAL if the pulse violates the state machine
124 */
David Härdeman724e2492010-04-08 13:10:00 -0300125static int ir_rc5_decode(struct input_dev *input_dev, s64 duration)
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300126{
127 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 u8 command, system, toggle;
130 u32 scancode;
131 int u;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300132
133 data = get_decoder_data(ir_dev);
134 if (!data)
135 return -EINVAL;
136
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 Chehabdb1423a2010-04-04 10:27:20 -0300141 data->state = STATE_INACTIVE;
142 return 0;
143 }
144
David Härdeman724e2492010-04-08 13:10:00 -0300145 u = TO_UNITS(duration, RC5_UNIT);
146 if (DURATION(u) == 0)
147 goto out;
148
149again:
150 IR_dprintk(2, "RC5 decode started at state %i (%i units, %ius)\n",
151 data->state, u, TO_US(duration));
152
153 if (DURATION(u) == 0 && data->state != STATE_FINISHED)
154 return 0;
155
156 switch (data->state) {
157
158 case STATE_INACTIVE:
159 if (IS_PULSE(u)) {
160 data->state = STATE_BIT_START;
161 data->count = 1;
162 DECREASE_DURATION(u, 1);
163 goto again;
164 }
165 break;
166
167 case STATE_BIT_START:
168 if (DURATION(u) == 1) {
169 data->rc5_bits <<= 1;
170 if (IS_SPACE(u))
171 data->rc5_bits |= 1;
172 data->count++;
173 data->last_unit = u;
174
175 /*
176 * If the last bit is zero, a space will merge
177 * with the silence after the command.
178 */
179 if (IS_PULSE(u) && data->count == RC5_NBITS) {
180 data->state = STATE_FINISHED;
181 goto again;
182 }
183
184 data->state = STATE_BIT_END;
185 return 0;
186 }
187 break;
188
189 case STATE_BIT_END:
190 if (IS_TRANSITION(u, data->last_unit)) {
191 if (data->count == RC5_NBITS)
192 data->state = STATE_FINISHED;
193 else
194 data->state = STATE_BIT_START;
195
196 DECREASE_DURATION(u, 1);
197 goto again;
198 }
199 break;
200
201 case STATE_FINISHED:
202 command = (data->rc5_bits & 0x0003F) >> 0;
203 system = (data->rc5_bits & 0x007C0) >> 6;
204 toggle = (data->rc5_bits & 0x00800) ? 1 : 0;
205 command += (data->rc5_bits & 0x01000) ? 0 : 0x40;
206 scancode = system << 8 | command;
207
208 IR_dprintk(1, "RC5 scancode 0x%04x (toggle: %u)\n",
209 scancode, toggle);
210 ir_keydown(input_dev, scancode, toggle);
211 data->state = STATE_INACTIVE;
212 return 0;
213 }
214
215out:
216 IR_dprintk(1, "RC5 decode failed at state %i (%i units, %ius)\n",
217 data->state, u, TO_US(duration));
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300218 data->state = STATE_INACTIVE;
219 return -EINVAL;
220}
221
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300222static int ir_rc5_register(struct input_dev *input_dev)
223{
224 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
225 struct decoder_data *data;
226 int rc;
227
228 rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
229 if (rc < 0)
230 return rc;
231
232 data = kzalloc(sizeof(*data), GFP_KERNEL);
233 if (!data) {
234 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
235 return -ENOMEM;
236 }
237
238 data->ir_dev = ir_dev;
239 data->enabled = 1;
240
241 spin_lock(&decoder_lock);
242 list_add_tail(&data->list, &decoder_list);
243 spin_unlock(&decoder_lock);
244
245 return 0;
246}
247
248static int ir_rc5_unregister(struct input_dev *input_dev)
249{
250 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
251 static struct decoder_data *data;
252
253 data = get_decoder_data(ir_dev);
254 if (!data)
255 return 0;
256
257 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
258
259 spin_lock(&decoder_lock);
260 list_del(&data->list);
261 spin_unlock(&decoder_lock);
262
263 return 0;
264}
265
266static struct ir_raw_handler rc5_handler = {
267 .decode = ir_rc5_decode,
268 .raw_register = ir_rc5_register,
269 .raw_unregister = ir_rc5_unregister,
270};
271
272static int __init ir_rc5_decode_init(void)
273{
274 ir_raw_handler_register(&rc5_handler);
275
276 printk(KERN_INFO "IR RC-5 protocol handler initialized\n");
277 return 0;
278}
279
280static void __exit ir_rc5_decode_exit(void)
281{
282 ir_raw_handler_unregister(&rc5_handler);
283}
284
285module_init(ir_rc5_decode_init);
286module_exit(ir_rc5_decode_exit);
287
288MODULE_LICENSE("GPL");
289MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
290MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
291MODULE_DESCRIPTION("RC-5 IR protocol decoder");