blob: 8e0e1b1f8c87ef9f83f05ab6a564e30ddd78aa4c [file] [log] [blame]
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -03001/* ir-raw-event.c - handle IR Pulse/Space event
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
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -030015#include <linux/kthread.h>
Maxim Levitsky45a568f2010-07-31 11:59:16 -030016#include <linux/mutex.h>
David Härdeman724e2492010-04-08 13:10:00 -030017#include <linux/sched.h>
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -030018#include <linux/freezer.h>
Mauro Carvalho Chehab3f113e32010-04-08 15:10:27 -030019#include "ir-core-priv.h"
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030020
David Härdeman724e2492010-04-08 13:10:00 -030021/* Define the max number of pulse/space transitions to buffer */
22#define MAX_IR_EVENT_SIZE 512
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030023
David Härdemanc2163692010-06-13 17:29:36 -030024/* Used to keep track of IR raw clients, protected by ir_raw_handler_lock */
25static LIST_HEAD(ir_raw_client_list);
26
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030027/* Used to handle IR raw handler extensions */
Maxim Levitsky45a568f2010-07-31 11:59:16 -030028static DEFINE_MUTEX(ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -030029static LIST_HEAD(ir_raw_handler_list);
30static u64 available_protocols;
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030031
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -030032#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030033/* Used to load the decoders */
34static struct work_struct wq_load;
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -030035#endif
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030036
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -030037static int ir_raw_event_thread(void *data)
David Härdeman724e2492010-04-08 13:10:00 -030038{
David Härdemane40b1122010-04-15 18:46:00 -030039 struct ir_raw_event ev;
David Härdemanc2163692010-06-13 17:29:36 -030040 struct ir_raw_handler *handler;
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -030041 struct ir_raw_event_ctrl *raw = (struct ir_raw_event_ctrl *)data;
David Härdeman724e2492010-04-08 13:10:00 -030042
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -030043 while (!kthread_should_stop()) {
44 try_to_freeze();
45
Maxim Levitsky45a568f2010-07-31 11:59:16 -030046 mutex_lock(&ir_raw_handler_lock);
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -030047
48 while (kfifo_out(&raw->kfifo, &ev, sizeof(ev)) == sizeof(ev)) {
49 list_for_each_entry(handler, &ir_raw_handler_list, list)
50 handler->decode(raw->input_dev, ev);
51 raw->prev_ev = ev;
52 }
53
Maxim Levitsky45a568f2010-07-31 11:59:16 -030054 mutex_unlock(&ir_raw_handler_lock);
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -030055
56 set_current_state(TASK_INTERRUPTIBLE);
57 schedule();
David Härdemanc2163692010-06-13 17:29:36 -030058 }
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -030059
60 return 0;
David Härdeman724e2492010-04-08 13:10:00 -030061}
62
David Härdeman724e2492010-04-08 13:10:00 -030063/**
64 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
65 * @input_dev: the struct input_dev device descriptor
David Härdemane40b1122010-04-15 18:46:00 -030066 * @ev: the struct ir_raw_event descriptor of the pulse/space
David Härdeman724e2492010-04-08 13:10:00 -030067 *
68 * This routine (which may be called from an interrupt context) stores a
69 * pulse/space duration for the raw ir decoding state machines. Pulses are
70 * signalled as positive values and spaces as negative values. A zero value
71 * will reset the decoding state machines.
72 */
David Härdemane40b1122010-04-15 18:46:00 -030073int ir_raw_event_store(struct input_dev *input_dev, struct ir_raw_event *ev)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030074{
David Härdeman724e2492010-04-08 13:10:00 -030075 struct ir_input_dev *ir = input_get_drvdata(input_dev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030076
77 if (!ir->raw)
78 return -EINVAL;
79
Maxim Levitsky510fcb72010-07-31 11:59:15 -030080 IR_dprintk(2, "sample: (05%dus %s)\n",
81 TO_US(ev->duration), TO_STR(ev->pulse));
82
David Härdemane40b1122010-04-15 18:46:00 -030083 if (kfifo_in(&ir->raw->kfifo, ev, sizeof(*ev)) != sizeof(*ev))
David Härdeman724e2492010-04-08 13:10:00 -030084 return -ENOMEM;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030085
David Härdeman724e2492010-04-08 13:10:00 -030086 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030087}
88EXPORT_SYMBOL_GPL(ir_raw_event_store);
89
David Härdeman724e2492010-04-08 13:10:00 -030090/**
91 * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
92 * @input_dev: the struct input_dev device descriptor
93 * @type: the type of the event that has occurred
94 *
95 * This routine (which may be called from an interrupt context) is used to
96 * store the beginning of an ir pulse or space (or the start/end of ir
97 * reception) for the raw ir decoding state machines. This is used by
98 * hardware which does not provide durations directly but only interrupts
99 * (or similar events) on state change.
100 */
101int ir_raw_event_store_edge(struct input_dev *input_dev, enum raw_event_type type)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300102{
David Härdeman724e2492010-04-08 13:10:00 -0300103 struct ir_input_dev *ir = input_get_drvdata(input_dev);
104 ktime_t now;
105 s64 delta; /* ns */
David Härdemane40b1122010-04-15 18:46:00 -0300106 struct ir_raw_event ev;
David Härdeman724e2492010-04-08 13:10:00 -0300107 int rc = 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300108
David Härdeman724e2492010-04-08 13:10:00 -0300109 if (!ir->raw)
110 return -EINVAL;
111
112 now = ktime_get();
113 delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
114
115 /* Check for a long duration since last event or if we're
116 * being called for the first time, note that delta can't
117 * possibly be negative.
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300118 */
David Härdemane40b1122010-04-15 18:46:00 -0300119 ev.duration = 0;
120 if (delta > IR_MAX_DURATION || !ir->raw->last_type)
David Härdeman724e2492010-04-08 13:10:00 -0300121 type |= IR_START_EVENT;
David Härdemane40b1122010-04-15 18:46:00 -0300122 else
123 ev.duration = delta;
David Härdeman724e2492010-04-08 13:10:00 -0300124
125 if (type & IR_START_EVENT)
126 ir_raw_event_reset(input_dev);
David Härdemane40b1122010-04-15 18:46:00 -0300127 else if (ir->raw->last_type & IR_SPACE) {
128 ev.pulse = false;
129 rc = ir_raw_event_store(input_dev, &ev);
130 } else if (ir->raw->last_type & IR_PULSE) {
131 ev.pulse = true;
132 rc = ir_raw_event_store(input_dev, &ev);
133 } else
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300134 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300135
David Härdeman724e2492010-04-08 13:10:00 -0300136 ir->raw->last_event = now;
137 ir->raw->last_type = type;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300138 return rc;
139}
David Härdeman724e2492010-04-08 13:10:00 -0300140EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
141
142/**
Maxim Levitsky4a702eb2010-07-31 11:59:22 -0300143 * ir_raw_event_store_with_filter() - pass next pulse/space to decoders with some processing
144 * @input_dev: the struct input_dev device descriptor
145 * @type: the type of the event that has occurred
146 *
147 * This routine (which may be called from an interrupt context) works
148 * in similiar manner to ir_raw_event_store_edge.
149 * This routine is intended for devices with limited internal buffer
150 * It automerges samples of same type, and handles timeouts
151 */
152int ir_raw_event_store_with_filter(struct input_dev *input_dev,
153 struct ir_raw_event *ev)
154{
155 struct ir_input_dev *ir = input_get_drvdata(input_dev);
156 struct ir_raw_event_ctrl *raw = ir->raw;
157
158 if (!raw || !ir->props)
159 return -EINVAL;
160
161 /* Ignore spaces in idle mode */
162 if (ir->idle && !ev->pulse)
163 return 0;
164 else if (ir->idle)
165 ir_raw_event_set_idle(input_dev, 0);
166
167 if (!raw->this_ev.duration) {
168 raw->this_ev = *ev;
169 } else if (ev->pulse == raw->this_ev.pulse) {
170 raw->this_ev.duration += ev->duration;
171 } else {
172 ir_raw_event_store(input_dev, &raw->this_ev);
173 raw->this_ev = *ev;
174 }
175
176 /* Enter idle mode if nessesary */
177 if (!ev->pulse && ir->props->timeout &&
178 raw->this_ev.duration >= ir->props->timeout)
179 ir_raw_event_set_idle(input_dev, 1);
180 return 0;
181}
182EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter);
183
184void ir_raw_event_set_idle(struct input_dev *input_dev, int idle)
185{
186 struct ir_input_dev *ir = input_get_drvdata(input_dev);
187 struct ir_raw_event_ctrl *raw = ir->raw;
188 ktime_t now;
189 u64 delta;
190
191 if (!ir->props)
192 return;
193
194 if (!ir->raw)
195 goto out;
196
197 if (idle) {
198 IR_dprintk(2, "enter idle mode\n");
199 raw->last_event = ktime_get();
200 } else {
201 IR_dprintk(2, "exit idle mode\n");
202
203 now = ktime_get();
204 delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
205
206 WARN_ON(raw->this_ev.pulse);
207
208 raw->this_ev.duration =
209 min(raw->this_ev.duration + delta,
210 (u64)IR_MAX_DURATION);
211
212 ir_raw_event_store(input_dev, &raw->this_ev);
213
214 if (raw->this_ev.duration == IR_MAX_DURATION)
215 ir_raw_event_reset(input_dev);
216
217 raw->this_ev.duration = 0;
218 }
219out:
220 if (ir->props->s_idle)
221 ir->props->s_idle(ir->props->priv, idle);
222 ir->idle = idle;
223}
224EXPORT_SYMBOL_GPL(ir_raw_event_set_idle);
225
226/**
David Härdeman724e2492010-04-08 13:10:00 -0300227 * ir_raw_event_handle() - schedules the decoding of stored ir data
228 * @input_dev: the struct input_dev device descriptor
229 *
230 * This routine will signal the workqueue to start decoding stored ir data.
231 */
232void ir_raw_event_handle(struct input_dev *input_dev)
233{
234 struct ir_input_dev *ir = input_get_drvdata(input_dev);
235
236 if (!ir->raw)
237 return;
238
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -0300239 wake_up_process(ir->raw->thread);
David Härdeman724e2492010-04-08 13:10:00 -0300240}
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300241EXPORT_SYMBOL_GPL(ir_raw_event_handle);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300242
David Härdeman667c9eb2010-06-13 17:29:31 -0300243/* used internally by the sysfs interface */
244u64
245ir_raw_get_allowed_protocols()
246{
247 u64 protocols;
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300248 mutex_lock(&ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -0300249 protocols = available_protocols;
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300250 mutex_unlock(&ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -0300251 return protocols;
252}
253
254/*
255 * Used to (un)register raw event clients
256 */
257int ir_raw_event_register(struct input_dev *input_dev)
258{
259 struct ir_input_dev *ir = input_get_drvdata(input_dev);
260 int rc;
David Härdemanc2163692010-06-13 17:29:36 -0300261 struct ir_raw_handler *handler;
David Härdeman667c9eb2010-06-13 17:29:31 -0300262
263 ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
264 if (!ir->raw)
265 return -ENOMEM;
266
267 ir->raw->input_dev = input_dev;
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -0300268
David Härdeman667c9eb2010-06-13 17:29:31 -0300269 ir->raw->enabled_protocols = ~0;
270 rc = kfifo_alloc(&ir->raw->kfifo, sizeof(s64) * MAX_IR_EVENT_SIZE,
271 GFP_KERNEL);
272 if (rc < 0) {
273 kfree(ir->raw);
274 ir->raw = NULL;
275 return rc;
276 }
277
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -0300278 ir->raw->thread = kthread_run(ir_raw_event_thread, ir->raw,
279 "rc%u", (unsigned int)ir->devno);
280
281 if (IS_ERR(ir->raw->thread)) {
Dan Carpenter028816b2010-08-12 04:47:07 -0300282 int ret = PTR_ERR(ir->raw->thread);
283
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -0300284 kfree(ir->raw);
285 ir->raw = NULL;
Dan Carpenter028816b2010-08-12 04:47:07 -0300286 return ret;
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -0300287 }
288
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300289 mutex_lock(&ir_raw_handler_lock);
David Härdemanc2163692010-06-13 17:29:36 -0300290 list_add_tail(&ir->raw->list, &ir_raw_client_list);
291 list_for_each_entry(handler, &ir_raw_handler_list, list)
292 if (handler->raw_register)
293 handler->raw_register(ir->raw->input_dev);
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300294 mutex_unlock(&ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -0300295
David Härdemanc2163692010-06-13 17:29:36 -0300296 return 0;
David Härdeman667c9eb2010-06-13 17:29:31 -0300297}
298
299void ir_raw_event_unregister(struct input_dev *input_dev)
300{
301 struct ir_input_dev *ir = input_get_drvdata(input_dev);
David Härdemanc2163692010-06-13 17:29:36 -0300302 struct ir_raw_handler *handler;
David Härdeman667c9eb2010-06-13 17:29:31 -0300303
304 if (!ir->raw)
305 return;
306
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -0300307 kthread_stop(ir->raw->thread);
David Härdemanc2163692010-06-13 17:29:36 -0300308
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300309 mutex_lock(&ir_raw_handler_lock);
David Härdemanc2163692010-06-13 17:29:36 -0300310 list_del(&ir->raw->list);
311 list_for_each_entry(handler, &ir_raw_handler_list, list)
312 if (handler->raw_unregister)
313 handler->raw_unregister(ir->raw->input_dev);
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300314 mutex_unlock(&ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -0300315
316 kfifo_free(&ir->raw->kfifo);
317 kfree(ir->raw);
318 ir->raw = NULL;
319}
320
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300321/*
322 * Extension interface - used to register the IR decoders
323 */
324
325int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
326{
David Härdemanc2163692010-06-13 17:29:36 -0300327 struct ir_raw_event_ctrl *raw;
328
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300329 mutex_lock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300330 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
David Härdemanc2163692010-06-13 17:29:36 -0300331 if (ir_raw_handler->raw_register)
332 list_for_each_entry(raw, &ir_raw_client_list, list)
333 ir_raw_handler->raw_register(raw->input_dev);
David Härdeman667c9eb2010-06-13 17:29:31 -0300334 available_protocols |= ir_raw_handler->protocols;
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300335 mutex_unlock(&ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -0300336
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300337 return 0;
338}
339EXPORT_SYMBOL(ir_raw_handler_register);
340
341void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
342{
David Härdemanc2163692010-06-13 17:29:36 -0300343 struct ir_raw_event_ctrl *raw;
344
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300345 mutex_lock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300346 list_del(&ir_raw_handler->list);
David Härdemanc2163692010-06-13 17:29:36 -0300347 if (ir_raw_handler->raw_unregister)
348 list_for_each_entry(raw, &ir_raw_client_list, list)
349 ir_raw_handler->raw_unregister(raw->input_dev);
David Härdeman667c9eb2010-06-13 17:29:31 -0300350 available_protocols &= ~ir_raw_handler->protocols;
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300351 mutex_unlock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300352}
353EXPORT_SYMBOL(ir_raw_handler_unregister);
354
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -0300355#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300356static void init_decoders(struct work_struct *work)
357{
358 /* Load the decoder modules */
359
360 load_nec_decode();
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300361 load_rc5_decode();
David Härdeman784a4932010-04-08 20:04:40 -0300362 load_rc6_decode();
David Härdemanbf670f62010-04-15 18:46:05 -0300363 load_jvc_decode();
David Härdeman3fe29c82010-04-15 18:46:10 -0300364 load_sony_decode();
Jarod Wilsonca414692010-07-03 01:07:53 -0300365 load_lirc_codec();
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300366
367 /* If needed, we may later add some init code. In this case,
368 it is needed to change the CONFIG_MODULE test at ir-core.h
369 */
370}
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -0300371#endif
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300372
373void ir_raw_init(void)
374{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300375#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300376 INIT_WORK(&wq_load, init_decoders);
377 schedule_work(&wq_load);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300378#endif
379}