blob: d3bd3f98e0080979983b03414d0a122aa963f2a9 [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
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030015#include <linux/workqueue.h>
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030016#include <linux/spinlock.h>
David Härdeman724e2492010-04-08 13:10:00 -030017#include <linux/sched.h>
Mauro Carvalho Chehab3f113e32010-04-08 15:10:27 -030018#include "ir-core-priv.h"
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030019
David Härdeman724e2492010-04-08 13:10:00 -030020/* Define the max number of pulse/space transitions to buffer */
21#define MAX_IR_EVENT_SIZE 512
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030022
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030023/* Used to handle IR raw handler extensions */
24static LIST_HEAD(ir_raw_handler_list);
Mauro Carvalho Chehab6eb94352010-04-07 16:19:36 -030025static DEFINE_SPINLOCK(ir_raw_handler_lock);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030026
27/**
28 * RUN_DECODER() - runs an operation on all IR decoders
29 * @ops: IR raw handler operation to be called
30 * @arg: arguments to be passed to the callback
31 *
32 * Calls ir_raw_handler::ops for all registered IR handlers. It prevents
33 * new decode addition/removal while running, by locking ir_raw_handler_lock
Jarod Wilsonc2284262010-05-29 14:17:27 -030034 * mutex. If an error occurs, we keep going, as in the decode case, each
35 * decoder must have a crack at decoding the data. We return a sum of the
36 * return codes, which will be either 0 or negative for current callers.
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030037 */
38#define RUN_DECODER(ops, ...) ({ \
39 struct ir_raw_handler *_ir_raw_handler; \
40 int _sumrc = 0, _rc; \
41 spin_lock(&ir_raw_handler_lock); \
42 list_for_each_entry(_ir_raw_handler, &ir_raw_handler_list, list) { \
43 if (_ir_raw_handler->ops) { \
44 _rc = _ir_raw_handler->ops(__VA_ARGS__); \
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030045 _sumrc += _rc; \
46 } \
47 } \
48 spin_unlock(&ir_raw_handler_lock); \
49 _sumrc; \
50})
51
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -030052#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030053/* Used to load the decoders */
54static struct work_struct wq_load;
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -030055#endif
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030056
David Härdeman724e2492010-04-08 13:10:00 -030057static void ir_raw_event_work(struct work_struct *work)
58{
David Härdemane40b1122010-04-15 18:46:00 -030059 struct ir_raw_event ev;
David Härdeman724e2492010-04-08 13:10:00 -030060 struct ir_raw_event_ctrl *raw =
61 container_of(work, struct ir_raw_event_ctrl, rx_work);
62
David Härdemane40b1122010-04-15 18:46:00 -030063 while (kfifo_out(&raw->kfifo, &ev, sizeof(ev)) == sizeof(ev))
64 RUN_DECODER(decode, raw->input_dev, ev);
David Härdeman724e2492010-04-08 13:10:00 -030065}
66
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030067int ir_raw_event_register(struct input_dev *input_dev)
68{
69 struct ir_input_dev *ir = input_get_drvdata(input_dev);
David Härdeman724e2492010-04-08 13:10:00 -030070 int rc;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030071
72 ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030073 if (!ir->raw)
74 return -ENOMEM;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030075
David Härdeman724e2492010-04-08 13:10:00 -030076 ir->raw->input_dev = input_dev;
77 INIT_WORK(&ir->raw->rx_work, ir_raw_event_work);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030078
David Härdeman724e2492010-04-08 13:10:00 -030079 rc = kfifo_alloc(&ir->raw->kfifo, sizeof(s64) * MAX_IR_EVENT_SIZE,
80 GFP_KERNEL);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030081 if (rc < 0) {
82 kfree(ir->raw);
83 ir->raw = NULL;
84 return rc;
85 }
86
87 rc = RUN_DECODER(raw_register, input_dev);
88 if (rc < 0) {
89 kfifo_free(&ir->raw->kfifo);
90 kfree(ir->raw);
91 ir->raw = NULL;
92 return rc;
93 }
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030094
95 return rc;
96}
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030097
98void ir_raw_event_unregister(struct input_dev *input_dev)
99{
100 struct ir_input_dev *ir = input_get_drvdata(input_dev);
101
102 if (!ir->raw)
103 return;
104
David Härdeman724e2492010-04-08 13:10:00 -0300105 cancel_work_sync(&ir->raw->rx_work);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300106 RUN_DECODER(raw_unregister, input_dev);
107
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300108 kfifo_free(&ir->raw->kfifo);
109 kfree(ir->raw);
110 ir->raw = NULL;
111}
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300112
David Härdeman724e2492010-04-08 13:10:00 -0300113/**
114 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
115 * @input_dev: the struct input_dev device descriptor
David Härdemane40b1122010-04-15 18:46:00 -0300116 * @ev: the struct ir_raw_event descriptor of the pulse/space
David Härdeman724e2492010-04-08 13:10:00 -0300117 *
118 * This routine (which may be called from an interrupt context) stores a
119 * pulse/space duration for the raw ir decoding state machines. Pulses are
120 * signalled as positive values and spaces as negative values. A zero value
121 * will reset the decoding state machines.
122 */
David Härdemane40b1122010-04-15 18:46:00 -0300123int ir_raw_event_store(struct input_dev *input_dev, struct ir_raw_event *ev)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300124{
David Härdeman724e2492010-04-08 13:10:00 -0300125 struct ir_input_dev *ir = input_get_drvdata(input_dev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300126
127 if (!ir->raw)
128 return -EINVAL;
129
David Härdemane40b1122010-04-15 18:46:00 -0300130 if (kfifo_in(&ir->raw->kfifo, ev, sizeof(*ev)) != sizeof(*ev))
David Härdeman724e2492010-04-08 13:10:00 -0300131 return -ENOMEM;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300132
David Härdeman724e2492010-04-08 13:10:00 -0300133 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300134}
135EXPORT_SYMBOL_GPL(ir_raw_event_store);
136
David Härdeman724e2492010-04-08 13:10:00 -0300137/**
138 * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
139 * @input_dev: the struct input_dev device descriptor
140 * @type: the type of the event that has occurred
141 *
142 * This routine (which may be called from an interrupt context) is used to
143 * store the beginning of an ir pulse or space (or the start/end of ir
144 * reception) for the raw ir decoding state machines. This is used by
145 * hardware which does not provide durations directly but only interrupts
146 * (or similar events) on state change.
147 */
148int ir_raw_event_store_edge(struct input_dev *input_dev, enum raw_event_type type)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300149{
David Härdeman724e2492010-04-08 13:10:00 -0300150 struct ir_input_dev *ir = input_get_drvdata(input_dev);
151 ktime_t now;
152 s64 delta; /* ns */
David Härdemane40b1122010-04-15 18:46:00 -0300153 struct ir_raw_event ev;
David Härdeman724e2492010-04-08 13:10:00 -0300154 int rc = 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300155
David Härdeman724e2492010-04-08 13:10:00 -0300156 if (!ir->raw)
157 return -EINVAL;
158
159 now = ktime_get();
160 delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
161
162 /* Check for a long duration since last event or if we're
163 * being called for the first time, note that delta can't
164 * possibly be negative.
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300165 */
David Härdemane40b1122010-04-15 18:46:00 -0300166 ev.duration = 0;
167 if (delta > IR_MAX_DURATION || !ir->raw->last_type)
David Härdeman724e2492010-04-08 13:10:00 -0300168 type |= IR_START_EVENT;
David Härdemane40b1122010-04-15 18:46:00 -0300169 else
170 ev.duration = delta;
David Härdeman724e2492010-04-08 13:10:00 -0300171
172 if (type & IR_START_EVENT)
173 ir_raw_event_reset(input_dev);
David Härdemane40b1122010-04-15 18:46:00 -0300174 else if (ir->raw->last_type & IR_SPACE) {
175 ev.pulse = false;
176 rc = ir_raw_event_store(input_dev, &ev);
177 } else if (ir->raw->last_type & IR_PULSE) {
178 ev.pulse = true;
179 rc = ir_raw_event_store(input_dev, &ev);
180 } else
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300181 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300182
David Härdeman724e2492010-04-08 13:10:00 -0300183 ir->raw->last_event = now;
184 ir->raw->last_type = type;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300185 return rc;
186}
David Härdeman724e2492010-04-08 13:10:00 -0300187EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
188
189/**
190 * ir_raw_event_handle() - schedules the decoding of stored ir data
191 * @input_dev: the struct input_dev device descriptor
192 *
193 * This routine will signal the workqueue to start decoding stored ir data.
194 */
195void ir_raw_event_handle(struct input_dev *input_dev)
196{
197 struct ir_input_dev *ir = input_get_drvdata(input_dev);
198
199 if (!ir->raw)
200 return;
201
202 schedule_work(&ir->raw->rx_work);
203}
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300204EXPORT_SYMBOL_GPL(ir_raw_event_handle);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300205
206/*
207 * Extension interface - used to register the IR decoders
208 */
209
210int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
211{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300212 spin_lock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300213 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300214 spin_unlock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300215 return 0;
216}
217EXPORT_SYMBOL(ir_raw_handler_register);
218
219void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
220{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300221 spin_lock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300222 list_del(&ir_raw_handler->list);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300223 spin_unlock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300224}
225EXPORT_SYMBOL(ir_raw_handler_unregister);
226
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -0300227#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300228static void init_decoders(struct work_struct *work)
229{
230 /* Load the decoder modules */
231
232 load_nec_decode();
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300233 load_rc5_decode();
David Härdeman784a4932010-04-08 20:04:40 -0300234 load_rc6_decode();
David Härdemanbf670f62010-04-15 18:46:05 -0300235 load_jvc_decode();
David Härdeman3fe29c82010-04-15 18:46:10 -0300236 load_sony_decode();
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300237
238 /* If needed, we may later add some init code. In this case,
239 it is needed to change the CONFIG_MODULE test at ir-core.h
240 */
241}
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -0300242#endif
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300243
244void ir_raw_init(void)
245{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300246#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300247 INIT_WORK(&wq_load, init_decoders);
248 schedule_work(&wq_load);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300249#endif
250}