1 /* ir-raw-event.c - handle IR Pulse/Space event
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.
15 #include <linux/kthread.h>
16 #include <linux/mutex.h>
17 #include <linux/sched.h>
18 #include <linux/freezer.h>
19 #include "ir-core-priv.h"
21 /* Define the max number of pulse/space transitions to buffer */
22 #define MAX_IR_EVENT_SIZE 512
24 /* Used to keep track of IR raw clients, protected by ir_raw_handler_lock */
25 static LIST_HEAD(ir_raw_client_list);
27 /* Used to handle IR raw handler extensions */
28 static DEFINE_MUTEX(ir_raw_handler_lock);
29 static LIST_HEAD(ir_raw_handler_list);
30 static u64 available_protocols;
33 /* Used to load the decoders */
34 static struct work_struct wq_load;
37 static int ir_raw_event_thread(void *data)
39 struct ir_raw_event ev;
40 struct ir_raw_handler *handler;
41 struct ir_raw_event_ctrl *raw = (struct ir_raw_event_ctrl *)data;
43 while (!kthread_should_stop()) {
46 mutex_lock(&ir_raw_handler_lock);
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);
54 mutex_unlock(&ir_raw_handler_lock);
56 set_current_state(TASK_INTERRUPTIBLE);
64 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
65 * @input_dev: the struct input_dev device descriptor
66 * @ev: the struct ir_raw_event descriptor of the pulse/space
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.
73 int ir_raw_event_store(struct input_dev *input_dev, struct ir_raw_event *ev)
75 struct ir_input_dev *ir = input_get_drvdata(input_dev);
80 IR_dprintk(2, "sample: (05%dus %s)\n",
81 TO_US(ev->duration), TO_STR(ev->pulse));
83 if (kfifo_in(&ir->raw->kfifo, ev, sizeof(*ev)) != sizeof(*ev))
88 EXPORT_SYMBOL_GPL(ir_raw_event_store);
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
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.
101 int ir_raw_event_store_edge(struct input_dev *input_dev, enum raw_event_type type)
103 struct ir_input_dev *ir = input_get_drvdata(input_dev);
106 struct ir_raw_event ev;
113 delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
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.
120 if (delta > IR_MAX_DURATION || !ir->raw->last_type)
121 type |= IR_START_EVENT;
125 if (type & IR_START_EVENT)
126 ir_raw_event_reset(input_dev);
127 else if (ir->raw->last_type & IR_SPACE) {
129 rc = ir_raw_event_store(input_dev, &ev);
130 } else if (ir->raw->last_type & IR_PULSE) {
132 rc = ir_raw_event_store(input_dev, &ev);
136 ir->raw->last_event = now;
137 ir->raw->last_type = type;
140 EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
143 * 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
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
152 int ir_raw_event_store_with_filter(struct input_dev *input_dev,
153 struct ir_raw_event *ev)
155 struct ir_input_dev *ir = input_get_drvdata(input_dev);
156 struct ir_raw_event_ctrl *raw = ir->raw;
158 if (!raw || !ir->props)
161 /* Ignore spaces in idle mode */
162 if (ir->idle && !ev->pulse)
165 ir_raw_event_set_idle(input_dev, 0);
167 if (!raw->this_ev.duration) {
169 } else if (ev->pulse == raw->this_ev.pulse) {
170 raw->this_ev.duration += ev->duration;
172 ir_raw_event_store(input_dev, &raw->this_ev);
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);
182 EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter);
184 void ir_raw_event_set_idle(struct input_dev *input_dev, int idle)
186 struct ir_input_dev *ir = input_get_drvdata(input_dev);
187 struct ir_raw_event_ctrl *raw = ir->raw;
198 IR_dprintk(2, "enter idle mode\n");
199 raw->last_event = ktime_get();
201 IR_dprintk(2, "exit idle mode\n");
204 delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
206 WARN_ON(raw->this_ev.pulse);
208 raw->this_ev.duration =
209 min(raw->this_ev.duration + delta,
210 (u64)IR_MAX_DURATION);
212 ir_raw_event_store(input_dev, &raw->this_ev);
214 if (raw->this_ev.duration == IR_MAX_DURATION)
215 ir_raw_event_reset(input_dev);
217 raw->this_ev.duration = 0;
220 if (ir->props->s_idle)
221 ir->props->s_idle(ir->props->priv, idle);
224 EXPORT_SYMBOL_GPL(ir_raw_event_set_idle);
227 * ir_raw_event_handle() - schedules the decoding of stored ir data
228 * @input_dev: the struct input_dev device descriptor
230 * This routine will signal the workqueue to start decoding stored ir data.
232 void ir_raw_event_handle(struct input_dev *input_dev)
234 struct ir_input_dev *ir = input_get_drvdata(input_dev);
239 wake_up_process(ir->raw->thread);
241 EXPORT_SYMBOL_GPL(ir_raw_event_handle);
243 /* used internally by the sysfs interface */
245 ir_raw_get_allowed_protocols()
248 mutex_lock(&ir_raw_handler_lock);
249 protocols = available_protocols;
250 mutex_unlock(&ir_raw_handler_lock);
255 * Used to (un)register raw event clients
257 int ir_raw_event_register(struct input_dev *input_dev)
259 struct ir_input_dev *ir = input_get_drvdata(input_dev);
261 struct ir_raw_handler *handler;
263 ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
267 ir->raw->input_dev = input_dev;
269 ir->raw->enabled_protocols = ~0;
270 rc = kfifo_alloc(&ir->raw->kfifo, sizeof(s64) * MAX_IR_EVENT_SIZE,
278 ir->raw->thread = kthread_run(ir_raw_event_thread, ir->raw,
279 "rc%u", (unsigned int)ir->devno);
281 if (IS_ERR(ir->raw->thread)) {
284 return PTR_ERR(ir->raw->thread);
287 mutex_lock(&ir_raw_handler_lock);
288 list_add_tail(&ir->raw->list, &ir_raw_client_list);
289 list_for_each_entry(handler, &ir_raw_handler_list, list)
290 if (handler->raw_register)
291 handler->raw_register(ir->raw->input_dev);
292 mutex_unlock(&ir_raw_handler_lock);
297 void ir_raw_event_unregister(struct input_dev *input_dev)
299 struct ir_input_dev *ir = input_get_drvdata(input_dev);
300 struct ir_raw_handler *handler;
305 kthread_stop(ir->raw->thread);
307 mutex_lock(&ir_raw_handler_lock);
308 list_del(&ir->raw->list);
309 list_for_each_entry(handler, &ir_raw_handler_list, list)
310 if (handler->raw_unregister)
311 handler->raw_unregister(ir->raw->input_dev);
312 mutex_unlock(&ir_raw_handler_lock);
314 kfifo_free(&ir->raw->kfifo);
320 * Extension interface - used to register the IR decoders
323 int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
325 struct ir_raw_event_ctrl *raw;
327 mutex_lock(&ir_raw_handler_lock);
328 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
329 if (ir_raw_handler->raw_register)
330 list_for_each_entry(raw, &ir_raw_client_list, list)
331 ir_raw_handler->raw_register(raw->input_dev);
332 available_protocols |= ir_raw_handler->protocols;
333 mutex_unlock(&ir_raw_handler_lock);
337 EXPORT_SYMBOL(ir_raw_handler_register);
339 void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
341 struct ir_raw_event_ctrl *raw;
343 mutex_lock(&ir_raw_handler_lock);
344 list_del(&ir_raw_handler->list);
345 if (ir_raw_handler->raw_unregister)
346 list_for_each_entry(raw, &ir_raw_client_list, list)
347 ir_raw_handler->raw_unregister(raw->input_dev);
348 available_protocols &= ~ir_raw_handler->protocols;
349 mutex_unlock(&ir_raw_handler_lock);
351 EXPORT_SYMBOL(ir_raw_handler_unregister);
354 static void init_decoders(struct work_struct *work)
356 /* Load the decoder modules */
365 /* If needed, we may later add some init code. In this case,
366 it is needed to change the CONFIG_MODULE test at ir-core.h
371 void ir_raw_init(void)
374 INIT_WORK(&wq_load, init_decoders);
375 schedule_work(&wq_load);