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;
44 while (!kthread_should_stop()) {
46 spin_lock_irq(&raw->lock);
47 retval = kfifo_out(&raw->kfifo, &ev, sizeof(ev));
50 set_current_state(TASK_INTERRUPTIBLE);
52 if (kthread_should_stop())
53 set_current_state(TASK_RUNNING);
55 spin_unlock_irq(&raw->lock);
60 spin_unlock_irq(&raw->lock);
63 BUG_ON(retval != sizeof(ev));
65 mutex_lock(&ir_raw_handler_lock);
66 list_for_each_entry(handler, &ir_raw_handler_list, list)
67 handler->decode(raw->input_dev, ev);
69 mutex_unlock(&ir_raw_handler_lock);
76 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
77 * @input_dev: the struct input_dev device descriptor
78 * @ev: the struct ir_raw_event descriptor of the pulse/space
80 * This routine (which may be called from an interrupt context) stores a
81 * pulse/space duration for the raw ir decoding state machines. Pulses are
82 * signalled as positive values and spaces as negative values. A zero value
83 * will reset the decoding state machines.
85 int ir_raw_event_store(struct input_dev *input_dev, struct ir_raw_event *ev)
87 struct ir_input_dev *ir = input_get_drvdata(input_dev);
92 IR_dprintk(2, "sample: (05%dus %s)\n",
93 TO_US(ev->duration), TO_STR(ev->pulse));
95 if (kfifo_in(&ir->raw->kfifo, ev, sizeof(*ev)) != sizeof(*ev))
100 EXPORT_SYMBOL_GPL(ir_raw_event_store);
103 * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
104 * @input_dev: the struct input_dev device descriptor
105 * @type: the type of the event that has occurred
107 * This routine (which may be called from an interrupt context) is used to
108 * store the beginning of an ir pulse or space (or the start/end of ir
109 * reception) for the raw ir decoding state machines. This is used by
110 * hardware which does not provide durations directly but only interrupts
111 * (or similar events) on state change.
113 int ir_raw_event_store_edge(struct input_dev *input_dev, enum raw_event_type type)
115 struct ir_input_dev *ir = input_get_drvdata(input_dev);
118 struct ir_raw_event ev;
125 delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
127 /* Check for a long duration since last event or if we're
128 * being called for the first time, note that delta can't
129 * possibly be negative.
132 if (delta > IR_MAX_DURATION || !ir->raw->last_type)
133 type |= IR_START_EVENT;
137 if (type & IR_START_EVENT)
138 ir_raw_event_reset(input_dev);
139 else if (ir->raw->last_type & IR_SPACE) {
141 rc = ir_raw_event_store(input_dev, &ev);
142 } else if (ir->raw->last_type & IR_PULSE) {
144 rc = ir_raw_event_store(input_dev, &ev);
148 ir->raw->last_event = now;
149 ir->raw->last_type = type;
152 EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
155 * ir_raw_event_store_with_filter() - pass next pulse/space to decoders with some processing
156 * @input_dev: the struct input_dev device descriptor
157 * @type: the type of the event that has occurred
159 * This routine (which may be called from an interrupt context) works
160 * in similiar manner to ir_raw_event_store_edge.
161 * This routine is intended for devices with limited internal buffer
162 * It automerges samples of same type, and handles timeouts
164 int ir_raw_event_store_with_filter(struct input_dev *input_dev,
165 struct ir_raw_event *ev)
167 struct ir_input_dev *ir = input_get_drvdata(input_dev);
168 struct ir_raw_event_ctrl *raw = ir->raw;
170 if (!raw || !ir->props)
173 /* Ignore spaces in idle mode */
174 if (ir->idle && !ev->pulse)
177 ir_raw_event_set_idle(input_dev, false);
179 if (!raw->this_ev.duration) {
181 } else if (ev->pulse == raw->this_ev.pulse) {
182 raw->this_ev.duration += ev->duration;
184 ir_raw_event_store(input_dev, &raw->this_ev);
188 /* Enter idle mode if nessesary */
189 if (!ev->pulse && ir->props->timeout &&
190 raw->this_ev.duration >= ir->props->timeout) {
191 ir_raw_event_set_idle(input_dev, true);
195 EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter);
198 * ir_raw_event_set_idle() - hint the ir core if device is receiving
200 * @input_dev: the struct input_dev device descriptor
201 * @idle: the hint value
203 void ir_raw_event_set_idle(struct input_dev *input_dev, bool idle)
205 struct ir_input_dev *ir = input_get_drvdata(input_dev);
206 struct ir_raw_event_ctrl *raw = ir->raw;
208 if (!ir->props || !ir->raw)
211 IR_dprintk(2, "%s idle mode\n", idle ? "enter" : "leave");
214 raw->this_ev.timeout = true;
215 ir_raw_event_store(input_dev, &raw->this_ev);
216 init_ir_raw_event(&raw->this_ev);
219 if (ir->props->s_idle)
220 ir->props->s_idle(ir->props->priv, idle);
223 EXPORT_SYMBOL_GPL(ir_raw_event_set_idle);
226 * ir_raw_event_handle() - schedules the decoding of stored ir data
227 * @input_dev: the struct input_dev device descriptor
229 * This routine will signal the workqueue to start decoding stored ir data.
231 void ir_raw_event_handle(struct input_dev *input_dev)
233 struct ir_input_dev *ir = input_get_drvdata(input_dev);
239 spin_lock_irqsave(&ir->raw->lock, flags);
240 wake_up_process(ir->raw->thread);
241 spin_unlock_irqrestore(&ir->raw->lock, flags);
243 EXPORT_SYMBOL_GPL(ir_raw_event_handle);
245 /* used internally by the sysfs interface */
247 ir_raw_get_allowed_protocols()
250 mutex_lock(&ir_raw_handler_lock);
251 protocols = available_protocols;
252 mutex_unlock(&ir_raw_handler_lock);
257 * Used to (un)register raw event clients
259 int ir_raw_event_register(struct input_dev *input_dev)
261 struct ir_input_dev *ir = input_get_drvdata(input_dev);
263 struct ir_raw_handler *handler;
265 ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
269 ir->raw->input_dev = input_dev;
271 ir->raw->enabled_protocols = ~0;
272 rc = kfifo_alloc(&ir->raw->kfifo, sizeof(s64) * MAX_IR_EVENT_SIZE,
280 spin_lock_init(&ir->raw->lock);
281 ir->raw->thread = kthread_run(ir_raw_event_thread, ir->raw,
282 "rc%u", (unsigned int)ir->devno);
284 if (IS_ERR(ir->raw->thread)) {
285 int ret = PTR_ERR(ir->raw->thread);
292 mutex_lock(&ir_raw_handler_lock);
293 list_add_tail(&ir->raw->list, &ir_raw_client_list);
294 list_for_each_entry(handler, &ir_raw_handler_list, list)
295 if (handler->raw_register)
296 handler->raw_register(ir->raw->input_dev);
297 mutex_unlock(&ir_raw_handler_lock);
302 void ir_raw_event_unregister(struct input_dev *input_dev)
304 struct ir_input_dev *ir = input_get_drvdata(input_dev);
305 struct ir_raw_handler *handler;
310 kthread_stop(ir->raw->thread);
312 mutex_lock(&ir_raw_handler_lock);
313 list_del(&ir->raw->list);
314 list_for_each_entry(handler, &ir_raw_handler_list, list)
315 if (handler->raw_unregister)
316 handler->raw_unregister(ir->raw->input_dev);
317 mutex_unlock(&ir_raw_handler_lock);
319 kfifo_free(&ir->raw->kfifo);
325 * Extension interface - used to register the IR decoders
328 int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
330 struct ir_raw_event_ctrl *raw;
332 mutex_lock(&ir_raw_handler_lock);
333 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
334 if (ir_raw_handler->raw_register)
335 list_for_each_entry(raw, &ir_raw_client_list, list)
336 ir_raw_handler->raw_register(raw->input_dev);
337 available_protocols |= ir_raw_handler->protocols;
338 mutex_unlock(&ir_raw_handler_lock);
342 EXPORT_SYMBOL(ir_raw_handler_register);
344 void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
346 struct ir_raw_event_ctrl *raw;
348 mutex_lock(&ir_raw_handler_lock);
349 list_del(&ir_raw_handler->list);
350 if (ir_raw_handler->raw_unregister)
351 list_for_each_entry(raw, &ir_raw_client_list, list)
352 ir_raw_handler->raw_unregister(raw->input_dev);
353 available_protocols &= ~ir_raw_handler->protocols;
354 mutex_unlock(&ir_raw_handler_lock);
356 EXPORT_SYMBOL(ir_raw_handler_unregister);
359 static void init_decoders(struct work_struct *work)
361 /* Load the decoder modules */
370 /* If needed, we may later add some init code. In this case,
371 it is needed to change the CONFIG_MODULE test at ir-core.h
376 void ir_raw_init(void)
379 INIT_WORK(&wq_load, init_decoders);
380 schedule_work(&wq_load);