1 /* ir-rc5-decoder.c - handle RC5(x) IR Pulse/Space protocol
3 * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
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.
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.
16 * 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
20 * the first two bits are start bits, and a third one is a filing bit
23 #include "ir-core-priv.h"
27 #define CHECK_RC5X_NBITS 8
28 #define RC5X_SPACE SPACE(4)
29 #define RC5_UNIT 888888 /* ns */
31 /* Used to register rc5_decoder clients */
32 static LIST_HEAD(decoder_list);
33 static DEFINE_SPINLOCK(decoder_lock);
44 struct list_head list;
45 struct ir_input_dev *ir_dev;
48 /* State machine control */
58 * get_decoder_data() - gets decoder data
59 * @input_dev: input device
61 * Returns the struct decoder_data that corresponds to a device
64 static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
66 struct decoder_data *data = NULL;
68 spin_lock(&decoder_lock);
69 list_for_each_entry(data, &decoder_list, list) {
70 if (data->ir_dev == ir_dev)
73 spin_unlock(&decoder_lock);
77 static ssize_t store_enabled(struct device *d,
78 struct device_attribute *mattr,
83 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
84 struct decoder_data *data = get_decoder_data(ir_dev);
89 if (strict_strtoul(buf, 10, &value) || value > 1)
92 data->enabled = value;
97 static ssize_t show_enabled(struct device *d,
98 struct device_attribute *mattr, char *buf)
100 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
101 struct decoder_data *data = get_decoder_data(ir_dev);
107 return sprintf(buf, "1\n");
109 return sprintf(buf, "0\n");
112 static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
114 static struct attribute *decoder_attributes[] = {
115 &dev_attr_enabled.attr,
119 static struct attribute_group decoder_attribute_group = {
120 .name = "rc5_decoder",
121 .attrs = decoder_attributes,
125 * ir_rc5_decode() - Decode one RC-5 pulse or space
126 * @input_dev: the struct input_dev descriptor of the device
127 * @duration: duration of pulse/space in ns
129 * This function returns -EINVAL if the pulse violates the state machine
131 static int ir_rc5_decode(struct input_dev *input_dev, s64 duration)
133 struct decoder_data *data;
134 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
139 data = get_decoder_data(ir_dev);
146 if (IS_RESET(duration)) {
147 data->state = STATE_INACTIVE;
151 u = TO_UNITS(duration, RC5_UNIT);
152 if (DURATION(u) == 0)
156 IR_dprintk(2, "RC5(x) decode started at state %i (%i units, %ius)\n",
157 data->state, u, TO_US(duration));
159 if (DURATION(u) == 0 && data->state != STATE_FINISHED)
162 switch (data->state) {
166 data->state = STATE_BIT_START;
168 /* We just need enough bits to get to STATE_CHECK_RC5X */
169 data->wanted_bits = RC5X_NBITS;
170 DECREASE_DURATION(u, 1);
175 case STATE_BIT_START:
176 if (DURATION(u) == 1) {
177 data->rc5_bits <<= 1;
184 * If the last bit is zero, a space will merge
185 * with the silence after the command.
187 if (IS_PULSE(u) && data->count == data->wanted_bits) {
188 data->state = STATE_FINISHED;
192 data->state = STATE_BIT_END;
198 if (IS_TRANSITION(u, data->last_unit)) {
199 if (data->count == data->wanted_bits)
200 data->state = STATE_FINISHED;
201 else if (data->count == CHECK_RC5X_NBITS)
202 data->state = STATE_CHECK_RC5X;
204 data->state = STATE_BIT_START;
206 DECREASE_DURATION(u, 1);
211 case STATE_CHECK_RC5X:
212 if (IS_SPACE(u) && DURATION(u) >= DURATION(RC5X_SPACE)) {
214 data->wanted_bits = RC5X_NBITS;
215 DECREASE_DURATION(u, DURATION(RC5X_SPACE));
218 data->wanted_bits = RC5_NBITS;
220 data->state = STATE_BIT_START;
224 if (data->wanted_bits == RC5X_NBITS) {
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;
234 IR_dprintk(1, "RC5X scancode 0x%06x (toggle: %u)\n",
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;
246 IR_dprintk(1, "RC5 scancode 0x%04x (toggle: %u)\n",
250 ir_keydown(input_dev, scancode, toggle);
251 data->state = STATE_INACTIVE;
256 IR_dprintk(1, "RC5(x) decode failed at state %i (%i units, %ius)\n",
257 data->state, u, TO_US(duration));
258 data->state = STATE_INACTIVE;
262 static int ir_rc5_register(struct input_dev *input_dev)
264 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
265 struct decoder_data *data;
268 rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
272 data = kzalloc(sizeof(*data), GFP_KERNEL);
274 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
278 data->ir_dev = ir_dev;
281 spin_lock(&decoder_lock);
282 list_add_tail(&data->list, &decoder_list);
283 spin_unlock(&decoder_lock);
288 static int ir_rc5_unregister(struct input_dev *input_dev)
290 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
291 static struct decoder_data *data;
293 data = get_decoder_data(ir_dev);
297 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
299 spin_lock(&decoder_lock);
300 list_del(&data->list);
301 spin_unlock(&decoder_lock);
306 static struct ir_raw_handler rc5_handler = {
307 .decode = ir_rc5_decode,
308 .raw_register = ir_rc5_register,
309 .raw_unregister = ir_rc5_unregister,
312 static int __init ir_rc5_decode_init(void)
314 ir_raw_handler_register(&rc5_handler);
316 printk(KERN_INFO "IR RC5(x) protocol handler initialized\n");
320 static void __exit ir_rc5_decode_exit(void)
322 ir_raw_handler_unregister(&rc5_handler);
325 module_init(ir_rc5_decode_init);
326 module_exit(ir_rc5_decode_exit);
328 MODULE_LICENSE("GPL");
329 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
330 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
331 MODULE_DESCRIPTION("RC5(x) IR protocol decoder");