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 handle_event(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);
156 /* Except for the initial event, what matters is the previous bit */
157 bit = (ev->type & IR_PULSE) ? 1 : 0;
161 /* Discards spurious space last_bits when inactive */
163 /* Very long delays are considered as start events */
164 if (ev->delta.tv_nsec > RC5_DURATION + RC5_HALFBIT / 2)
165 data->state = STATE_INACTIVE;
167 if (ev->type & IR_START_EVENT)
168 data->state = STATE_INACTIVE;
170 switch (data->state) {
172 IR_dprintk(1, "currently inative. Received bit (%s) @%luus\n",
173 last_bit ? "pulse" : "space",
174 (ev->delta.tv_nsec + 500) / 1000);
176 /* Discards the initial start space */
181 memset (&data->rc5_code, 0, sizeof(data->rc5_code));
183 data->state = STATE_START2_SPACE;
185 case STATE_START2_SPACE:
188 if (!is_rc5_halfbit(ev->delta.tv_nsec))
190 data->state = STATE_START2_MARK;
192 case STATE_START2_MARK:
196 if (!is_rc5_halfbit(ev->delta.tv_nsec))
199 data->state = STATE_MARKSPACE;
201 case STATE_MARKSPACE:
202 n_half = n_half(ev->delta.tv_nsec);
203 if (n_half < 1 || n_half > 3) {
204 IR_dprintk(1, "Decode failed at %d-th bit (%s) @%luus\n",
206 last_bit ? "pulse" : "space",
207 (ev->delta.tv_nsec + 500) / 1000);
208 printk("%d halves\n", n_half);
211 data->n_half += n_half;
216 /* Got one complete mark/space cycle */
218 bit = ((data->count + 1) * 2)/ data->n_half;
220 printk("%d halves, %d bits\n", n_half, bit);
222 #if 1 /* SANITY check - while testing the decoder */
224 IR_dprintk(1, "Decoder HAS failed at %d-th bit (%s) @%luus\n",
226 last_bit ? "pulse" : "space",
227 (ev->delta.tv_nsec + 500) / 1000);
232 /* Ok, we've got a valid bit. proccess it */
234 int shift = data->count;
237 * RC-5 transmit bytes on this temporal order:
238 * address | not address | command | not command
241 data->rc5_code.address |= 1 << shift;
243 data->rc5_code.command |= 1 << (shift - 8);
246 IR_dprintk(1, "RC-5: bit #%d: %d (%d)\n",
247 data->count, bit, data->n_half);
248 if (++data->count >= RC5_NBITS) {
250 scancode = data->rc5_code.address << 8 |
251 data->rc5_code.command;
252 IR_dprintk(1, "RC-5 scancode 0x%04x\n", scancode);
254 ir_keydown(input_dev, scancode, 0);
256 data->state = STATE_TRAILER_MARK;
259 case STATE_TRAILER_MARK:
262 data->state = STATE_INACTIVE;
267 IR_dprintk(1, "RC-5 decoded failed at state %s (%s) @ %luus\n",
268 st_name[data->state],
269 bit ? "pulse" : "space",
270 (ev->delta.tv_nsec + 500) / 1000);
272 data->state = STATE_INACTIVE;
277 * ir_rc5_decode() - Decodes all RC-5 pulsecodes on a given array
278 * @input_dev: the struct input_dev descriptor of the device
279 * @evs: event array with type/duration of pulse/space
280 * @len: length of the array
281 * This function returns the number of decoded pulses
283 static int ir_rc5_decode(struct input_dev *input_dev,
284 struct ir_raw_event *evs,
287 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
288 struct decoder_data *data;
292 data = get_decoder_data(ir_dev);
293 if (!data || !data->enabled)
296 for (pos = 0; pos < len; pos++)
297 handle_event(input_dev, &evs[pos]);
302 static int ir_rc5_register(struct input_dev *input_dev)
304 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
305 struct decoder_data *data;
308 rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
312 data = kzalloc(sizeof(*data), GFP_KERNEL);
314 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
318 data->ir_dev = ir_dev;
321 spin_lock(&decoder_lock);
322 list_add_tail(&data->list, &decoder_list);
323 spin_unlock(&decoder_lock);
328 static int ir_rc5_unregister(struct input_dev *input_dev)
330 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
331 static struct decoder_data *data;
333 data = get_decoder_data(ir_dev);
337 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
339 spin_lock(&decoder_lock);
340 list_del(&data->list);
341 spin_unlock(&decoder_lock);
346 static struct ir_raw_handler rc5_handler = {
347 .decode = ir_rc5_decode,
348 .raw_register = ir_rc5_register,
349 .raw_unregister = ir_rc5_unregister,
352 static int __init ir_rc5_decode_init(void)
354 ir_raw_handler_register(&rc5_handler);
356 printk(KERN_INFO "IR RC-5 protocol handler initialized\n");
360 static void __exit ir_rc5_decode_exit(void)
362 ir_raw_handler_unregister(&rc5_handler);
365 module_init(ir_rc5_decode_init);
366 module_exit(ir_rc5_decode_exit);
368 MODULE_LICENSE("GPL");
369 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
370 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
371 MODULE_DESCRIPTION("RC-5 IR protocol decoder");