6323066438b56c1960c2084ec7e308a83bd4a2ed
[safe/jmp/linux-2.6] / drivers / media / IR / ir-rc5-decoder.c
1 /* 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
18  * 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
20  */
21
22 #include <media/ir-core.h>
23
24 static unsigned int ir_rc5_remote_gap = 888888;
25
26 #define RC5_NBITS               14
27 #define RC5_BIT                 (ir_rc5_remote_gap * 2)
28 #define RC5_DURATION            (ir_rc5_remote_gap * RC5_NBITS)
29
30 /* Used to register rc5_decoder clients */
31 static LIST_HEAD(decoder_list);
32 static DEFINE_SPINLOCK(decoder_lock);
33
34 enum rc5_state {
35         STATE_INACTIVE,
36         STATE_MARKSPACE,
37         STATE_TRAILER,
38 };
39
40 struct rc5_code {
41         u8      address;
42         u8      command;
43 };
44
45 struct 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;
53         unsigned                code, elapsed, last_bit, last_code;
54 };
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
64 static 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
77 static 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
97 static 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
112 static DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR, show_enabled, store_enabled);
113
114 static struct attribute *decoder_attributes[] = {
115         &dev_attr_enabled.attr,
116         NULL
117 };
118
119 static 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  */
131 static int ir_rc5_decode(struct input_dev *input_dev,
132                         struct ir_raw_event *ev)
133 {
134         struct decoder_data *data;
135         struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
136         int is_pulse, scancode, delta, toggle;
137
138         data = get_decoder_data(ir_dev);
139         if (!data)
140                 return -EINVAL;
141
142         if (!data->enabled)
143                 return 0;
144
145         delta = DIV_ROUND_CLOSEST(ev->delta.tv_nsec, ir_rc5_remote_gap);
146
147         /* The duration time refers to the last bit time */
148         is_pulse = (ev->type & IR_PULSE) ? 1 : 0;
149
150         /* Very long delays are considered as start events */
151         if (delta > RC5_DURATION || (ev->type & IR_START_EVENT))
152                 data->state = STATE_INACTIVE;
153
154         switch (data->state) {
155         case STATE_INACTIVE:
156         IR_dprintk(2, "currently inative. Start bit (%s) @%uus\n",
157                    is_pulse ? "pulse" : "space",
158                    (unsigned)(ev->delta.tv_nsec + 500) / 1000);
159
160                 /* Discards the initial start space */
161                 if (!is_pulse)
162                         goto err;
163                 data->code = 1;
164                 data->last_bit = 1;
165                 data->elapsed = 0;
166                 memset(&data->rc5_code, 0, sizeof(data->rc5_code));
167                 data->state = STATE_MARKSPACE;
168                 return 0;
169         case STATE_MARKSPACE:
170                 if (delta != 1)
171                         data->last_bit = data->last_bit ? 0 : 1;
172
173                 data->elapsed += delta;
174
175                 if ((data->elapsed % 2) == 1)
176                         return 0;
177
178                 data->code <<= 1;
179                 data->code |= data->last_bit;
180
181                 /* Fill the 2 unused bits at the command with 0 */
182                 if (data->elapsed / 2 == 6)
183                         data->code <<= 2;
184
185                 if (data->elapsed >= (RC5_NBITS - 1) * 2) {
186                         scancode = data->code;
187
188                         /* 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;
192                         }
193
194                         toggle = (scancode & 0x2000) ? 1 : 0;
195
196                         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;
207                 }
208                 return 0;
209         case STATE_TRAILER:
210                 data->state = STATE_INACTIVE;
211                 return 0;
212         }
213
214 err:
215         IR_dprintk(1, "RC-5 decoded failed at %s @ %luus\n",
216                    is_pulse ? "pulse" : "space",
217                    (ev->delta.tv_nsec + 500) / 1000);
218         data->state = STATE_INACTIVE;
219         return -EINVAL;
220 }
221
222 static 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
248 static 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
266 static 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
272 static 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
280 static void __exit ir_rc5_decode_exit(void)
281 {
282         ir_raw_handler_unregister(&rc5_handler);
283 }
284
285 module_init(ir_rc5_decode_init);
286 module_exit(ir_rc5_decode_exit);
287
288 MODULE_LICENSE("GPL");
289 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
290 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
291 MODULE_DESCRIPTION("RC-5 IR protocol decoder");