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/workqueue.h>
16 #include <linux/mutex.h>
17 #include <linux/sched.h>
18 #include "ir-core-priv.h"
20 /* Define the max number of pulse/space transitions to buffer */
21 #define MAX_IR_EVENT_SIZE 512
23 /* Used to keep track of IR raw clients, protected by ir_raw_handler_lock */
24 static LIST_HEAD(ir_raw_client_list);
26 /* Used to handle IR raw handler extensions */
27 static DEFINE_MUTEX(ir_raw_handler_lock);
28 static LIST_HEAD(ir_raw_handler_list);
29 static u64 available_protocols;
32 /* Used to load the decoders */
33 static struct work_struct wq_load;
36 static void ir_raw_event_work(struct work_struct *work)
38 struct ir_raw_event ev;
39 struct ir_raw_handler *handler;
40 struct ir_raw_event_ctrl *raw =
41 container_of(work, struct ir_raw_event_ctrl, rx_work);
43 while (kfifo_out(&raw->kfifo, &ev, sizeof(ev)) == sizeof(ev)) {
44 mutex_lock(&ir_raw_handler_lock);
45 list_for_each_entry(handler, &ir_raw_handler_list, list)
46 handler->decode(raw->input_dev, ev);
47 mutex_unlock(&ir_raw_handler_lock);
53 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
54 * @input_dev: the struct input_dev device descriptor
55 * @ev: the struct ir_raw_event descriptor of the pulse/space
57 * This routine (which may be called from an interrupt context) stores a
58 * pulse/space duration for the raw ir decoding state machines. Pulses are
59 * signalled as positive values and spaces as negative values. A zero value
60 * will reset the decoding state machines.
62 int ir_raw_event_store(struct input_dev *input_dev, struct ir_raw_event *ev)
64 struct ir_input_dev *ir = input_get_drvdata(input_dev);
69 IR_dprintk(2, "sample: (05%dus %s)\n",
70 TO_US(ev->duration), TO_STR(ev->pulse));
72 if (kfifo_in(&ir->raw->kfifo, ev, sizeof(*ev)) != sizeof(*ev))
77 EXPORT_SYMBOL_GPL(ir_raw_event_store);
80 * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
81 * @input_dev: the struct input_dev device descriptor
82 * @type: the type of the event that has occurred
84 * This routine (which may be called from an interrupt context) is used to
85 * store the beginning of an ir pulse or space (or the start/end of ir
86 * reception) for the raw ir decoding state machines. This is used by
87 * hardware which does not provide durations directly but only interrupts
88 * (or similar events) on state change.
90 int ir_raw_event_store_edge(struct input_dev *input_dev, enum raw_event_type type)
92 struct ir_input_dev *ir = input_get_drvdata(input_dev);
95 struct ir_raw_event ev;
102 delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
104 /* Check for a long duration since last event or if we're
105 * being called for the first time, note that delta can't
106 * possibly be negative.
109 if (delta > IR_MAX_DURATION || !ir->raw->last_type)
110 type |= IR_START_EVENT;
114 if (type & IR_START_EVENT)
115 ir_raw_event_reset(input_dev);
116 else if (ir->raw->last_type & IR_SPACE) {
118 rc = ir_raw_event_store(input_dev, &ev);
119 } else if (ir->raw->last_type & IR_PULSE) {
121 rc = ir_raw_event_store(input_dev, &ev);
125 ir->raw->last_event = now;
126 ir->raw->last_type = type;
129 EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
132 * ir_raw_event_handle() - schedules the decoding of stored ir data
133 * @input_dev: the struct input_dev device descriptor
135 * This routine will signal the workqueue to start decoding stored ir data.
137 void ir_raw_event_handle(struct input_dev *input_dev)
139 struct ir_input_dev *ir = input_get_drvdata(input_dev);
144 schedule_work(&ir->raw->rx_work);
146 EXPORT_SYMBOL_GPL(ir_raw_event_handle);
148 /* used internally by the sysfs interface */
150 ir_raw_get_allowed_protocols()
153 mutex_lock(&ir_raw_handler_lock);
154 protocols = available_protocols;
155 mutex_unlock(&ir_raw_handler_lock);
160 * Used to (un)register raw event clients
162 int ir_raw_event_register(struct input_dev *input_dev)
164 struct ir_input_dev *ir = input_get_drvdata(input_dev);
166 struct ir_raw_handler *handler;
168 ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
172 ir->raw->input_dev = input_dev;
173 INIT_WORK(&ir->raw->rx_work, ir_raw_event_work);
174 ir->raw->enabled_protocols = ~0;
175 rc = kfifo_alloc(&ir->raw->kfifo, sizeof(s64) * MAX_IR_EVENT_SIZE,
183 mutex_lock(&ir_raw_handler_lock);
184 list_add_tail(&ir->raw->list, &ir_raw_client_list);
185 list_for_each_entry(handler, &ir_raw_handler_list, list)
186 if (handler->raw_register)
187 handler->raw_register(ir->raw->input_dev);
188 mutex_unlock(&ir_raw_handler_lock);
193 void ir_raw_event_unregister(struct input_dev *input_dev)
195 struct ir_input_dev *ir = input_get_drvdata(input_dev);
196 struct ir_raw_handler *handler;
201 cancel_work_sync(&ir->raw->rx_work);
203 mutex_lock(&ir_raw_handler_lock);
204 list_del(&ir->raw->list);
205 list_for_each_entry(handler, &ir_raw_handler_list, list)
206 if (handler->raw_unregister)
207 handler->raw_unregister(ir->raw->input_dev);
208 mutex_unlock(&ir_raw_handler_lock);
210 kfifo_free(&ir->raw->kfifo);
216 * Extension interface - used to register the IR decoders
219 int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
221 struct ir_raw_event_ctrl *raw;
223 mutex_lock(&ir_raw_handler_lock);
224 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
225 if (ir_raw_handler->raw_register)
226 list_for_each_entry(raw, &ir_raw_client_list, list)
227 ir_raw_handler->raw_register(raw->input_dev);
228 available_protocols |= ir_raw_handler->protocols;
229 mutex_unlock(&ir_raw_handler_lock);
233 EXPORT_SYMBOL(ir_raw_handler_register);
235 void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
237 struct ir_raw_event_ctrl *raw;
239 mutex_lock(&ir_raw_handler_lock);
240 list_del(&ir_raw_handler->list);
241 if (ir_raw_handler->raw_unregister)
242 list_for_each_entry(raw, &ir_raw_client_list, list)
243 ir_raw_handler->raw_unregister(raw->input_dev);
244 available_protocols &= ~ir_raw_handler->protocols;
245 mutex_unlock(&ir_raw_handler_lock);
247 EXPORT_SYMBOL(ir_raw_handler_unregister);
250 static void init_decoders(struct work_struct *work)
252 /* Load the decoder modules */
261 /* If needed, we may later add some init code. In this case,
262 it is needed to change the CONFIG_MODULE test at ir-core.h
267 void ir_raw_init(void)
270 INIT_WORK(&wq_load, init_decoders);
271 schedule_work(&wq_load);