blob: a62277b625a8ed78028e7060a677598eeae03ffe [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
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -030024static unsigned int ir_rc5_remote_gap = 888888;
25
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030026#define RC5_NBITS 14
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -030027#define RC5_BIT (ir_rc5_remote_gap * 2)
28#define RC5_DURATION (ir_rc5_remote_gap * RC5_NBITS)
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030029
30/* Used to register rc5_decoder clients */
31static LIST_HEAD(decoder_list);
32static spinlock_t decoder_lock;
33
34enum rc5_state {
35 STATE_INACTIVE,
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030036 STATE_MARKSPACE,
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -030037 STATE_TRAILER,
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030038};
39
40struct rc5_code {
41 u8 address;
42 u8 command;
43};
44
45struct decoder_data {
46 struct list_head list;
47 struct ir_input_dev *ir_dev;
48 int enabled:1;
49
50 /* State machine control */
51 enum rc5_state state;
52 struct rc5_code rc5_code;
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -030053 unsigned code, elapsed, last_bit, last_code;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -030054};
55
56
57/**
58 * get_decoder_data() - gets decoder data
59 * @input_dev: input device
60 *
61 * Returns the struct decoder_data that corresponds to a device
62 */
63
64static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev)
65{
66 struct decoder_data *data = NULL;
67
68 spin_lock(&decoder_lock);
69 list_for_each_entry(data, &decoder_list, list) {
70 if (data->ir_dev == ir_dev)
71 break;
72 }
73 spin_unlock(&decoder_lock);
74 return data;
75}
76
77static ssize_t store_enabled(struct device *d,
78 struct device_attribute *mattr,
79 const char *buf,
80 size_t len)
81{
82 unsigned long value;
83 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
84 struct decoder_data *data = get_decoder_data(ir_dev);
85
86 if (!data)
87 return -EINVAL;
88
89 if (strict_strtoul(buf, 10, &value) || value > 1)
90 return -EINVAL;
91
92 data->enabled = value;
93
94 return len;
95}
96
97static ssize_t show_enabled(struct device *d,
98 struct device_attribute *mattr, char *buf)
99{
100 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
101 struct decoder_data *data = get_decoder_data(ir_dev);
102
103 if (!data)
104 return -EINVAL;
105
106 if (data->enabled)
107 return sprintf(buf, "1\n");
108 else
109 return sprintf(buf, "0\n");
110}
111
112static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
113
114static struct attribute *decoder_attributes[] = {
115 &dev_attr_enabled.attr,
116 NULL
117};
118
119static struct attribute_group decoder_attribute_group = {
120 .name = "rc5_decoder",
121 .attrs = decoder_attributes,
122};
123
124/**
125 * handle_event() - Decode one RC-5 pulse or space
126 * @input_dev: the struct input_dev descriptor of the device
127 * @ev: event array with type/duration of pulse/space
128 *
129 * This function returns -EINVAL if the pulse violates the state machine
130 */
Mauro Carvalho Chehab587835a2010-04-04 10:44:51 -0300131static int ir_rc5_decode(struct input_dev *input_dev,
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300132 struct ir_raw_event *ev)
133{
134 struct decoder_data *data;
135 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300136 int is_pulse, scancode, delta, toggle;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300137
138 data = get_decoder_data(ir_dev);
139 if (!data)
140 return -EINVAL;
141
Mauro Carvalho Chehab7f20d322010-04-04 14:45:04 -0300142 if (!data->enabled)
143 return 0;
144
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300145 delta = DIV_ROUND_CLOSEST(ev->delta.tv_nsec, ir_rc5_remote_gap);
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300146
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300147 /* The duration time refers to the last bit time */
148 is_pulse = (ev->type & IR_PULSE) ? 1 : 0;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300149
150 /* Very long delays are considered as start events */
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300151 if (delta > RC5_DURATION || (ev->type & IR_START_EVENT))
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300152 data->state = STATE_INACTIVE;
153
154 switch (data->state) {
155 case STATE_INACTIVE:
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300156 IR_dprintk(2, "currently inative. Start bit (%s) @%uus\n",
157 is_pulse ? "pulse" : "space",
158 (unsigned)(ev->delta.tv_nsec + 500) / 1000);
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300159
160 /* Discards the initial start space */
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300161 if (!is_pulse)
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300162 goto err;
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300163 data->code = 1;
164 data->last_bit = 1;
165 data->elapsed = 0;
166 memset(&data->rc5_code, 0, sizeof(data->rc5_code));
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300167 data->state = STATE_MARKSPACE;
168 return 0;
169 case STATE_MARKSPACE:
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300170 if (delta != 1)
171 data->last_bit = data->last_bit ? 0 : 1;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300172
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300173 data->elapsed += delta;
174
175 if ((data->elapsed % 2) == 1)
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300176 return 0;
177
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300178 data->code <<= 1;
179 data->code |= data->last_bit;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300180
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300181 /* Fill the 2 unused bits at the command with 0 */
182 if (data->elapsed / 2 == 6)
183 data->code <<= 2;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300184
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300185 if (data->elapsed >= (RC5_NBITS - 1) * 2) {
186 scancode = data->code;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300187
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300188 /* Check for the start bits */
189 if ((scancode & 0xc000) != 0xc000) {
190 IR_dprintk(1, "Code 0x%04x doesn't have two start bits. It is not RC-5\n", scancode);
191 goto err;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300192 }
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300193
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300194 toggle = (scancode & 0x2000) ? 1 : 0;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300195
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300196 if (scancode == data->last_code) {
197 IR_dprintk(1, "RC-5 repeat\n");
198 ir_repeat(input_dev);
199 } else {
200 data->last_code = scancode;
201 scancode &= 0x1fff;
202 IR_dprintk(1, "RC-5 scancode 0x%04x\n", scancode);
203
204 ir_keydown(input_dev, scancode, 0);
205 }
206 data->state = STATE_TRAILER;
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300207 }
208 return 0;
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300209 case STATE_TRAILER:
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300210 data->state = STATE_INACTIVE;
211 return 0;
212 }
213
214err:
Mauro Carvalho Chehab9b09df52010-04-06 02:29:42 -0300215 IR_dprintk(1, "RC-5 decoded failed at %s @ %luus\n",
216 is_pulse ? "pulse" : "space",
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300217 (ev->delta.tv_nsec + 500) / 1000);
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");