blob: 0d59ef7d10145539a9c68b67524dd1dde4edd8d2 [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;
Maxim Levitskyc6ef1e72010-09-06 18:26:06 -030042 int retval;
David Härdeman724e2492010-04-08 13:10:00 -030043
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -030044 while (!kthread_should_stop()) {
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -030045
Maxim Levitskyc6ef1e72010-09-06 18:26:06 -030046 spin_lock_irq(&raw->lock);
47 retval = kfifo_out(&raw->kfifo, &ev, sizeof(ev));
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -030048
Maxim Levitskyc6ef1e72010-09-06 18:26:06 -030049 if (!retval) {
50 set_current_state(TASK_INTERRUPTIBLE);
51
52 if (kthread_should_stop())
53 set_current_state(TASK_RUNNING);
54
55 spin_unlock_irq(&raw->lock);
56 schedule();
57 continue;
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -030058 }
59
Maxim Levitskyc6ef1e72010-09-06 18:26:06 -030060 spin_unlock_irq(&raw->lock);
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -030061
Maxim Levitskyc6ef1e72010-09-06 18:26:06 -030062
63 BUG_ON(retval != sizeof(ev));
64
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);
68 raw->prev_ev = ev;
69 mutex_unlock(&ir_raw_handler_lock);
David Härdemanc2163692010-06-13 17:29:36 -030070 }
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -030071
72 return 0;
David Härdeman724e2492010-04-08 13:10:00 -030073}
74
David Härdeman724e2492010-04-08 13:10:00 -030075/**
76 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
77 * @input_dev: the struct input_dev device descriptor
David Härdemane40b1122010-04-15 18:46:00 -030078 * @ev: the struct ir_raw_event descriptor of the pulse/space
David Härdeman724e2492010-04-08 13:10:00 -030079 *
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.
84 */
David Härdemane40b1122010-04-15 18:46:00 -030085int ir_raw_event_store(struct input_dev *input_dev, struct ir_raw_event *ev)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030086{
David Härdeman724e2492010-04-08 13:10:00 -030087 struct ir_input_dev *ir = input_get_drvdata(input_dev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030088
89 if (!ir->raw)
90 return -EINVAL;
91
Maxim Levitsky510fcb72010-07-31 11:59:15 -030092 IR_dprintk(2, "sample: (05%dus %s)\n",
93 TO_US(ev->duration), TO_STR(ev->pulse));
94
David Härdemane40b1122010-04-15 18:46:00 -030095 if (kfifo_in(&ir->raw->kfifo, ev, sizeof(*ev)) != sizeof(*ev))
David Härdeman724e2492010-04-08 13:10:00 -030096 return -ENOMEM;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030097
David Härdeman724e2492010-04-08 13:10:00 -030098 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030099}
100EXPORT_SYMBOL_GPL(ir_raw_event_store);
101
David Härdeman724e2492010-04-08 13:10:00 -0300102/**
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
106 *
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.
112 */
113int ir_raw_event_store_edge(struct input_dev *input_dev, enum raw_event_type type)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300114{
David Härdeman724e2492010-04-08 13:10:00 -0300115 struct ir_input_dev *ir = input_get_drvdata(input_dev);
116 ktime_t now;
117 s64 delta; /* ns */
David Härdemane40b1122010-04-15 18:46:00 -0300118 struct ir_raw_event ev;
David Härdeman724e2492010-04-08 13:10:00 -0300119 int rc = 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300120
David Härdeman724e2492010-04-08 13:10:00 -0300121 if (!ir->raw)
122 return -EINVAL;
123
124 now = ktime_get();
125 delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
126
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.
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300130 */
David Härdemane40b1122010-04-15 18:46:00 -0300131 ev.duration = 0;
132 if (delta > IR_MAX_DURATION || !ir->raw->last_type)
David Härdeman724e2492010-04-08 13:10:00 -0300133 type |= IR_START_EVENT;
David Härdemane40b1122010-04-15 18:46:00 -0300134 else
135 ev.duration = delta;
David Härdeman724e2492010-04-08 13:10:00 -0300136
137 if (type & IR_START_EVENT)
138 ir_raw_event_reset(input_dev);
David Härdemane40b1122010-04-15 18:46:00 -0300139 else if (ir->raw->last_type & IR_SPACE) {
140 ev.pulse = false;
141 rc = ir_raw_event_store(input_dev, &ev);
142 } else if (ir->raw->last_type & IR_PULSE) {
143 ev.pulse = true;
144 rc = ir_raw_event_store(input_dev, &ev);
145 } else
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300146 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300147
David Härdeman724e2492010-04-08 13:10:00 -0300148 ir->raw->last_event = now;
149 ir->raw->last_type = type;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300150 return rc;
151}
David Härdeman724e2492010-04-08 13:10:00 -0300152EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
153
154/**
Maxim Levitsky4a702eb2010-07-31 11:59:22 -0300155 * 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
158 *
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
163 */
164int ir_raw_event_store_with_filter(struct input_dev *input_dev,
165 struct ir_raw_event *ev)
166{
167 struct ir_input_dev *ir = input_get_drvdata(input_dev);
168 struct ir_raw_event_ctrl *raw = ir->raw;
169
170 if (!raw || !ir->props)
171 return -EINVAL;
172
173 /* Ignore spaces in idle mode */
174 if (ir->idle && !ev->pulse)
175 return 0;
176 else if (ir->idle)
Maxim Levitsky46519182010-10-16 19:56:28 -0300177 ir_raw_event_set_idle(input_dev, false);
Maxim Levitsky4a702eb2010-07-31 11:59:22 -0300178
179 if (!raw->this_ev.duration) {
180 raw->this_ev = *ev;
181 } else if (ev->pulse == raw->this_ev.pulse) {
182 raw->this_ev.duration += ev->duration;
183 } else {
184 ir_raw_event_store(input_dev, &raw->this_ev);
185 raw->this_ev = *ev;
186 }
187
188 /* Enter idle mode if nessesary */
189 if (!ev->pulse && ir->props->timeout &&
Maxim Levitsky46519182010-10-16 19:56:28 -0300190 raw->this_ev.duration >= ir->props->timeout) {
191 ir_raw_event_set_idle(input_dev, true);
192 }
Maxim Levitsky4a702eb2010-07-31 11:59:22 -0300193 return 0;
194}
195EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter);
196
Maxim Levitsky46519182010-10-16 19:56:28 -0300197/**
198 * ir_raw_event_set_idle() - hint the ir core if device is receiving
199 * IR data or not
200 * @input_dev: the struct input_dev device descriptor
201 * @idle: the hint value
202 */
203void ir_raw_event_set_idle(struct input_dev *input_dev, bool idle)
Maxim Levitsky4a702eb2010-07-31 11:59:22 -0300204{
205 struct ir_input_dev *ir = input_get_drvdata(input_dev);
206 struct ir_raw_event_ctrl *raw = ir->raw;
Maxim Levitsky4a702eb2010-07-31 11:59:22 -0300207
Maxim Levitsky46519182010-10-16 19:56:28 -0300208 if (!ir->props || !ir->raw)
Maxim Levitsky4a702eb2010-07-31 11:59:22 -0300209 return;
210
Maxim Levitsky46519182010-10-16 19:56:28 -0300211 IR_dprintk(2, "%s idle mode\n", idle ? "enter" : "leave");
Maxim Levitsky4a702eb2010-07-31 11:59:22 -0300212
213 if (idle) {
Maxim Levitsky46519182010-10-16 19:56:28 -0300214 raw->this_ev.timeout = true;
Maxim Levitsky4a702eb2010-07-31 11:59:22 -0300215 ir_raw_event_store(input_dev, &raw->this_ev);
Maxim Levitsky46519182010-10-16 19:56:28 -0300216 init_ir_raw_event(&raw->this_ev);
Maxim Levitsky4a702eb2010-07-31 11:59:22 -0300217 }
Maxim Levitsky46519182010-10-16 19:56:28 -0300218
Maxim Levitsky4a702eb2010-07-31 11:59:22 -0300219 if (ir->props->s_idle)
220 ir->props->s_idle(ir->props->priv, idle);
221 ir->idle = idle;
222}
223EXPORT_SYMBOL_GPL(ir_raw_event_set_idle);
224
225/**
David Härdeman724e2492010-04-08 13:10:00 -0300226 * ir_raw_event_handle() - schedules the decoding of stored ir data
227 * @input_dev: the struct input_dev device descriptor
228 *
229 * This routine will signal the workqueue to start decoding stored ir data.
230 */
231void ir_raw_event_handle(struct input_dev *input_dev)
232{
233 struct ir_input_dev *ir = input_get_drvdata(input_dev);
Maxim Levitskyc6ef1e72010-09-06 18:26:06 -0300234 unsigned long flags;
David Härdeman724e2492010-04-08 13:10:00 -0300235
236 if (!ir->raw)
237 return;
238
Maxim Levitskyc6ef1e72010-09-06 18:26:06 -0300239 spin_lock_irqsave(&ir->raw->lock, flags);
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -0300240 wake_up_process(ir->raw->thread);
Maxim Levitskyc6ef1e72010-09-06 18:26:06 -0300241 spin_unlock_irqrestore(&ir->raw->lock, flags);
David Härdeman724e2492010-04-08 13:10:00 -0300242}
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300243EXPORT_SYMBOL_GPL(ir_raw_event_handle);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300244
David Härdeman667c9eb2010-06-13 17:29:31 -0300245/* used internally by the sysfs interface */
246u64
247ir_raw_get_allowed_protocols()
248{
249 u64 protocols;
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300250 mutex_lock(&ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -0300251 protocols = available_protocols;
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300252 mutex_unlock(&ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -0300253 return protocols;
254}
255
256/*
257 * Used to (un)register raw event clients
258 */
259int ir_raw_event_register(struct input_dev *input_dev)
260{
261 struct ir_input_dev *ir = input_get_drvdata(input_dev);
262 int rc;
David Härdemanc2163692010-06-13 17:29:36 -0300263 struct ir_raw_handler *handler;
David Härdeman667c9eb2010-06-13 17:29:31 -0300264
265 ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
266 if (!ir->raw)
267 return -ENOMEM;
268
269 ir->raw->input_dev = input_dev;
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -0300270
David Härdeman667c9eb2010-06-13 17:29:31 -0300271 ir->raw->enabled_protocols = ~0;
272 rc = kfifo_alloc(&ir->raw->kfifo, sizeof(s64) * MAX_IR_EVENT_SIZE,
273 GFP_KERNEL);
274 if (rc < 0) {
275 kfree(ir->raw);
276 ir->raw = NULL;
277 return rc;
278 }
279
Maxim Levitskyc6ef1e72010-09-06 18:26:06 -0300280 spin_lock_init(&ir->raw->lock);
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -0300281 ir->raw->thread = kthread_run(ir_raw_event_thread, ir->raw,
282 "rc%u", (unsigned int)ir->devno);
283
284 if (IS_ERR(ir->raw->thread)) {
Dan Carpenter028816b2010-08-12 04:47:07 -0300285 int ret = PTR_ERR(ir->raw->thread);
286
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -0300287 kfree(ir->raw);
288 ir->raw = NULL;
Dan Carpenter028816b2010-08-12 04:47:07 -0300289 return ret;
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -0300290 }
291
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300292 mutex_lock(&ir_raw_handler_lock);
David Härdemanc2163692010-06-13 17:29:36 -0300293 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);
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300297 mutex_unlock(&ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -0300298
David Härdemanc2163692010-06-13 17:29:36 -0300299 return 0;
David Härdeman667c9eb2010-06-13 17:29:31 -0300300}
301
302void ir_raw_event_unregister(struct input_dev *input_dev)
303{
304 struct ir_input_dev *ir = input_get_drvdata(input_dev);
David Härdemanc2163692010-06-13 17:29:36 -0300305 struct ir_raw_handler *handler;
David Härdeman667c9eb2010-06-13 17:29:31 -0300306
307 if (!ir->raw)
308 return;
309
Maxim Levitsky0d2cb1d2010-07-31 11:59:17 -0300310 kthread_stop(ir->raw->thread);
David Härdemanc2163692010-06-13 17:29:36 -0300311
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300312 mutex_lock(&ir_raw_handler_lock);
David Härdemanc2163692010-06-13 17:29:36 -0300313 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);
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300317 mutex_unlock(&ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -0300318
319 kfifo_free(&ir->raw->kfifo);
320 kfree(ir->raw);
321 ir->raw = NULL;
322}
323
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300324/*
325 * Extension interface - used to register the IR decoders
326 */
327
328int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
329{
David Härdemanc2163692010-06-13 17:29:36 -0300330 struct ir_raw_event_ctrl *raw;
331
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300332 mutex_lock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300333 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
David Härdemanc2163692010-06-13 17:29:36 -0300334 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);
David Härdeman667c9eb2010-06-13 17:29:31 -0300337 available_protocols |= ir_raw_handler->protocols;
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300338 mutex_unlock(&ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -0300339
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300340 return 0;
341}
342EXPORT_SYMBOL(ir_raw_handler_register);
343
344void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
345{
David Härdemanc2163692010-06-13 17:29:36 -0300346 struct ir_raw_event_ctrl *raw;
347
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300348 mutex_lock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300349 list_del(&ir_raw_handler->list);
David Härdemanc2163692010-06-13 17:29:36 -0300350 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);
David Härdeman667c9eb2010-06-13 17:29:31 -0300353 available_protocols &= ~ir_raw_handler->protocols;
Maxim Levitsky45a568f2010-07-31 11:59:16 -0300354 mutex_unlock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300355}
356EXPORT_SYMBOL(ir_raw_handler_unregister);
357
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -0300358#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300359static void init_decoders(struct work_struct *work)
360{
361 /* Load the decoder modules */
362
363 load_nec_decode();
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300364 load_rc5_decode();
David Härdeman784a4932010-04-08 20:04:40 -0300365 load_rc6_decode();
David Härdemanbf670f62010-04-15 18:46:05 -0300366 load_jvc_decode();
David Härdeman3fe29c82010-04-15 18:46:10 -0300367 load_sony_decode();
Jarod Wilsonca414692010-07-03 01:07:53 -0300368 load_lirc_codec();
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300369
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
372 */
373}
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -0300374#endif
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300375
376void ir_raw_init(void)
377{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300378#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300379 INIT_WORK(&wq_load, init_decoders);
380 schedule_work(&wq_load);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300381#endif
382}