1 /* ir-rc6-decoder.c - A decoder for the RC6 IR protocol
3 * Copyright (C) 2010 by David Härdeman <david@hardeman.nu>
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.
15 #include "ir-core-priv.h"
18 * This decoder currently supports:
19 * RC6-0-16 (standard toggle bit in header)
20 * RC6-6A-24 (no toggle bit)
21 * RC6-6A-32 (MCE version with toggle bit in body)
24 #define RC6_UNIT 444444 /* us */
25 #define RC6_HEADER_NBITS 4 /* not including toggle bit */
26 #define RC6_0_NBITS 16
27 #define RC6_6A_SMALL_NBITS 24
28 #define RC6_6A_LARGE_NBITS 32
29 #define RC6_PREFIX_PULSE (6 * RC6_UNIT)
30 #define RC6_PREFIX_SPACE (2 * RC6_UNIT)
31 #define RC6_BIT_START (1 * RC6_UNIT)
32 #define RC6_BIT_END (1 * RC6_UNIT)
33 #define RC6_TOGGLE_START (2 * RC6_UNIT)
34 #define RC6_TOGGLE_END (2 * RC6_UNIT)
35 #define RC6_MODE_MASK 0x07 /* for the header bits */
36 #define RC6_STARTBIT_MASK 0x08 /* for the header bits */
37 #define RC6_6A_MCE_TOGGLE_MASK 0x8000 /* for the body bits */
39 /* Used to register rc6_decoder clients */
40 static LIST_HEAD(decoder_list);
41 static DEFINE_SPINLOCK(decoder_lock);
52 STATE_HEADER_BIT_START,
62 struct list_head list;
63 struct ir_input_dev *ir_dev;
66 /* State machine control */
70 struct ir_raw_event prev_ev;
78 * get_decoder_data() - gets decoder data
79 * @input_dev: input device
81 * Returns the struct decoder_data that corresponds to a device
83 static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
85 struct decoder_data *data = NULL;
87 spin_lock(&decoder_lock);
88 list_for_each_entry(data, &decoder_list, list) {
89 if (data->ir_dev == ir_dev)
92 spin_unlock(&decoder_lock);
96 static ssize_t store_enabled(struct device *d,
97 struct device_attribute *mattr,
102 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
103 struct decoder_data *data = get_decoder_data(ir_dev);
108 if (strict_strtoul(buf, 10, &value) || value > 1)
111 data->enabled = value;
116 static ssize_t show_enabled(struct device *d,
117 struct device_attribute *mattr, char *buf)
119 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
120 struct decoder_data *data = get_decoder_data(ir_dev);
126 return sprintf(buf, "1\n");
128 return sprintf(buf, "0\n");
131 static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
133 static struct attribute *decoder_attributes[] = {
134 &dev_attr_enabled.attr,
138 static struct attribute_group decoder_attribute_group = {
139 .name = "rc6_decoder",
140 .attrs = decoder_attributes,
143 static enum rc6_mode rc6_mode(struct decoder_data *data) {
144 switch (data->header & RC6_MODE_MASK) {
152 return RC6_MODE_UNKNOWN;
157 * ir_rc6_decode() - Decode one RC6 pulse or space
158 * @input_dev: the struct input_dev descriptor of the device
159 * @ev: the struct ir_raw_event descriptor of the pulse/space
161 * This function returns -EINVAL if the pulse violates the state machine
163 static int ir_rc6_decode(struct input_dev *input_dev, struct ir_raw_event ev)
165 struct decoder_data *data;
166 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
170 data = get_decoder_data(ir_dev);
178 data->state = STATE_INACTIVE;
182 if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
186 IR_dprintk(2, "RC6 decode started at state %i (%uus %s)\n",
187 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
189 if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
192 switch (data->state) {
198 /* Note: larger margin on first pulse since each RC6_UNIT
199 is quite short and some hardware takes some time to
200 adjust to the signal */
201 if (!eq_margin(ev.duration, RC6_PREFIX_PULSE, RC6_UNIT))
204 data->state = STATE_PREFIX_SPACE;
208 case STATE_PREFIX_SPACE:
212 if (!eq_margin(ev.duration, RC6_PREFIX_SPACE, RC6_UNIT / 2))
215 data->state = STATE_HEADER_BIT_START;
218 case STATE_HEADER_BIT_START:
219 if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2))
227 data->state = STATE_HEADER_BIT_END;
230 case STATE_HEADER_BIT_END:
231 if (!is_transition(&ev, &data->prev_ev))
234 if (data->count == RC6_HEADER_NBITS)
235 data->state = STATE_TOGGLE_START;
237 data->state = STATE_HEADER_BIT_START;
239 decrease_duration(&ev, RC6_BIT_END);
242 case STATE_TOGGLE_START:
243 if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2))
246 data->toggle = ev.pulse;
248 data->state = STATE_TOGGLE_END;
251 case STATE_TOGGLE_END:
252 if (!is_transition(&ev, &data->prev_ev) ||
253 !geq_margin(ev.duration, RC6_TOGGLE_END, RC6_UNIT / 2))
256 if (!(data->header & RC6_STARTBIT_MASK)) {
257 IR_dprintk(1, "RC6 invalid start bit\n");
261 data->state = STATE_BODY_BIT_START;
263 decrease_duration(&ev, RC6_TOGGLE_END);
266 switch (rc6_mode(data)) {
268 data->wanted_bits = RC6_0_NBITS;
271 /* This might look weird, but we basically
272 check the value of the first body bit to
273 determine the number of bits in mode 6A */
274 if ((!ev.pulse && !geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2)) ||
275 geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
276 data->wanted_bits = RC6_6A_LARGE_NBITS;
278 data->wanted_bits = RC6_6A_SMALL_NBITS;
281 IR_dprintk(1, "RC6 unknown mode\n");
286 case STATE_BODY_BIT_START:
287 if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2))
296 data->state = STATE_BODY_BIT_END;
299 case STATE_BODY_BIT_END:
300 if (!is_transition(&ev, &data->prev_ev))
303 if (data->count == data->wanted_bits)
304 data->state = STATE_FINISHED;
306 data->state = STATE_BODY_BIT_START;
308 decrease_duration(&ev, RC6_BIT_END);
315 switch (rc6_mode(data)) {
317 scancode = data->body & 0xffff;
318 toggle = data->toggle;
319 IR_dprintk(1, "RC6(0) scancode 0x%04x (toggle: %u)\n",
323 if (data->wanted_bits == RC6_6A_LARGE_NBITS) {
324 toggle = data->body & RC6_6A_MCE_TOGGLE_MASK ? 1 : 0;
325 scancode = data->body & ~RC6_6A_MCE_TOGGLE_MASK;
328 scancode = data->body & 0xffffff;
331 IR_dprintk(1, "RC6(6A) scancode 0x%08x (toggle: %u)\n",
335 IR_dprintk(1, "RC6 unknown mode\n");
339 ir_keydown(input_dev, scancode, toggle);
340 data->state = STATE_INACTIVE;
345 IR_dprintk(1, "RC6 decode failed at state %i (%uus %s)\n",
346 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
347 data->state = STATE_INACTIVE;
351 static int ir_rc6_register(struct input_dev *input_dev)
353 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
354 struct decoder_data *data;
357 rc = sysfs_create_group(&ir_dev->dev.kobj, &decoder_attribute_group);
361 data = kzalloc(sizeof(*data), GFP_KERNEL);
363 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
367 data->ir_dev = ir_dev;
370 spin_lock(&decoder_lock);
371 list_add_tail(&data->list, &decoder_list);
372 spin_unlock(&decoder_lock);
377 static int ir_rc6_unregister(struct input_dev *input_dev)
379 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
380 static struct decoder_data *data;
382 data = get_decoder_data(ir_dev);
386 sysfs_remove_group(&ir_dev->dev.kobj, &decoder_attribute_group);
388 spin_lock(&decoder_lock);
389 list_del(&data->list);
390 spin_unlock(&decoder_lock);
395 static struct ir_raw_handler rc6_handler = {
396 .decode = ir_rc6_decode,
397 .raw_register = ir_rc6_register,
398 .raw_unregister = ir_rc6_unregister,
401 static int __init ir_rc6_decode_init(void)
403 ir_raw_handler_register(&rc6_handler);
405 printk(KERN_INFO "IR RC6 protocol handler initialized\n");
409 static void __exit ir_rc6_decode_exit(void)
411 ir_raw_handler_unregister(&rc6_handler);
414 module_init(ir_rc6_decode_init);
415 module_exit(ir_rc6_decode_exit);
417 MODULE_LICENSE("GPL");
418 MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
419 MODULE_DESCRIPTION("RC6 IR protocol decoder");