1 /* ir-rc5-decoder.c - handle RC-5 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 only handles 14 bits RC-5 protocols. There are other variants
17 * that use a different number of bits. This is currently unsupported
20 #include <media/ir-core.h>
23 #define RC5_HALFBIT 888888 /* ns */
24 #define RC5_BIT (RC5_HALFBIT * 2)
25 #define RC5_DURATION (RC5_BIT * RC5_NBITS)
27 #define is_rc5_halfbit(nsec) ((ev->delta.tv_nsec >= RC5_HALFBIT / 2) && \
28 (ev->delta.tv_nsec < RC5_HALFBIT + RC5_HALFBIT / 2))
30 #define n_half(nsec) ((ev->delta.tv_nsec + RC5_HALFBIT / 2) / RC5_HALFBIT)
32 /* Used to register rc5_decoder clients */
33 static LIST_HEAD(decoder_list);
34 static spinlock_t decoder_lock;
44 static char *st_name[] = {
59 struct list_head list;
60 struct ir_input_dev *ir_dev;
63 /* State machine control */
65 struct rc5_code rc5_code;
72 * get_decoder_data() - gets decoder data
73 * @input_dev: input device
75 * Returns the struct decoder_data that corresponds to a device
78 static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
80 struct decoder_data *data = NULL;
82 spin_lock(&decoder_lock);
83 list_for_each_entry(data, &decoder_list, list) {
84 if (data->ir_dev == ir_dev)
87 spin_unlock(&decoder_lock);
91 static ssize_t store_enabled(struct device *d,
92 struct device_attribute *mattr,
97 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
98 struct decoder_data *data = get_decoder_data(ir_dev);
103 if (strict_strtoul(buf, 10, &value) || value > 1)
106 data->enabled = value;
111 static ssize_t show_enabled(struct device *d,
112 struct device_attribute *mattr, char *buf)
114 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
115 struct decoder_data *data = get_decoder_data(ir_dev);
121 return sprintf(buf, "1\n");
123 return sprintf(buf, "0\n");
126 static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
128 static struct attribute *decoder_attributes[] = {
129 &dev_attr_enabled.attr,
133 static struct attribute_group decoder_attribute_group = {
134 .name = "rc5_decoder",
135 .attrs = decoder_attributes,
139 * handle_event() - Decode one RC-5 pulse or space
140 * @input_dev: the struct input_dev descriptor of the device
141 * @ev: event array with type/duration of pulse/space
143 * This function returns -EINVAL if the pulse violates the state machine
145 static int ir_rc5_decode(struct input_dev *input_dev,
146 struct ir_raw_event *ev)
148 struct decoder_data *data;
149 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
150 int bit, last_bit, n_half;
152 data = get_decoder_data(ir_dev);
159 /* Except for the initial event, what matters is the previous bit */
160 bit = (ev->type & IR_PULSE) ? 1 : 0;
164 /* Discards spurious space last_bits when inactive */
166 /* Very long delays are considered as start events */
167 if (ev->delta.tv_nsec > RC5_DURATION + RC5_HALFBIT / 2)
168 data->state = STATE_INACTIVE;
170 if (ev->type & IR_START_EVENT)
171 data->state = STATE_INACTIVE;
173 switch (data->state) {
175 IR_dprintk(1, "currently inative. Received bit (%s) @%luus\n",
176 last_bit ? "pulse" : "space",
177 (ev->delta.tv_nsec + 500) / 1000);
179 /* Discards the initial start space */
184 memset (&data->rc5_code, 0, sizeof(data->rc5_code));
186 data->state = STATE_START2_SPACE;
188 case STATE_START2_SPACE:
191 if (!is_rc5_halfbit(ev->delta.tv_nsec))
193 data->state = STATE_START2_MARK;
195 case STATE_START2_MARK:
199 if (!is_rc5_halfbit(ev->delta.tv_nsec))
202 data->state = STATE_MARKSPACE;
204 case STATE_MARKSPACE:
205 n_half = n_half(ev->delta.tv_nsec);
206 if (n_half < 1 || n_half > 3) {
207 IR_dprintk(1, "Decode failed at %d-th bit (%s) @%luus\n",
209 last_bit ? "pulse" : "space",
210 (ev->delta.tv_nsec + 500) / 1000);
211 printk("%d halves\n", n_half);
214 data->n_half += n_half;
219 /* Got one complete mark/space cycle */
221 bit = ((data->count + 1) * 2)/ data->n_half;
223 printk("%d halves, %d bits\n", n_half, bit);
225 #if 1 /* SANITY check - while testing the decoder */
227 IR_dprintk(1, "Decoder HAS failed at %d-th bit (%s) @%luus\n",
229 last_bit ? "pulse" : "space",
230 (ev->delta.tv_nsec + 500) / 1000);
235 /* Ok, we've got a valid bit. proccess it */
237 int shift = data->count;
240 * RC-5 transmit bytes on this temporal order:
241 * address | not address | command | not command
244 data->rc5_code.address |= 1 << shift;
246 data->rc5_code.command |= 1 << (shift - 8);
249 IR_dprintk(1, "RC-5: bit #%d: %d (%d)\n",
250 data->count, bit, data->n_half);
251 if (++data->count >= RC5_NBITS) {
253 scancode = data->rc5_code.address << 8 |
254 data->rc5_code.command;
255 IR_dprintk(1, "RC-5 scancode 0x%04x\n", scancode);
257 ir_keydown(input_dev, scancode, 0);
259 data->state = STATE_TRAILER_MARK;
262 case STATE_TRAILER_MARK:
265 data->state = STATE_INACTIVE;
270 IR_dprintk(1, "RC-5 decoded failed at state %s (%s) @ %luus\n",
271 st_name[data->state],
272 bit ? "pulse" : "space",
273 (ev->delta.tv_nsec + 500) / 1000);
275 data->state = STATE_INACTIVE;
279 static int ir_rc5_register(struct input_dev *input_dev)
281 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
282 struct decoder_data *data;
285 rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
289 data = kzalloc(sizeof(*data), GFP_KERNEL);
291 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
295 data->ir_dev = ir_dev;
298 spin_lock(&decoder_lock);
299 list_add_tail(&data->list, &decoder_list);
300 spin_unlock(&decoder_lock);
305 static int ir_rc5_unregister(struct input_dev *input_dev)
307 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
308 static struct decoder_data *data;
310 data = get_decoder_data(ir_dev);
314 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
316 spin_lock(&decoder_lock);
317 list_del(&data->list);
318 spin_unlock(&decoder_lock);
323 static struct ir_raw_handler rc5_handler = {
324 .decode = ir_rc5_decode,
325 .raw_register = ir_rc5_register,
326 .raw_unregister = ir_rc5_unregister,
329 static int __init ir_rc5_decode_init(void)
331 ir_raw_handler_register(&rc5_handler);
333 printk(KERN_INFO "IR RC-5 protocol handler initialized\n");
337 static void __exit ir_rc5_decode_exit(void)
339 ir_raw_handler_unregister(&rc5_handler);
342 module_init(ir_rc5_decode_init);
343 module_exit(ir_rc5_decode_exit);
345 MODULE_LICENSE("GPL");
346 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
347 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
348 MODULE_DESCRIPTION("RC-5 IR protocol decoder");