blob: fb3336c37191290049845a48181e299a30997b0a [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 */
Mauro Carvalho Chehab6eb94352010-04-07 16:19:36 -030024static DEFINE_SPINLOCK(ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -030025static LIST_HEAD(ir_raw_handler_list);
26static u64 available_protocols;
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030027
28/**
29 * RUN_DECODER() - runs an operation on all IR decoders
30 * @ops: IR raw handler operation to be called
31 * @arg: arguments to be passed to the callback
32 *
33 * Calls ir_raw_handler::ops for all registered IR handlers. It prevents
34 * new decode addition/removal while running, by locking ir_raw_handler_lock
Jarod Wilsonc2284262010-05-29 14:17:27 -030035 * mutex. If an error occurs, we keep going, as in the decode case, each
36 * decoder must have a crack at decoding the data. We return a sum of the
37 * return codes, which will be either 0 or negative for current callers.
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030038 */
39#define RUN_DECODER(ops, ...) ({ \
40 struct ir_raw_handler *_ir_raw_handler; \
41 int _sumrc = 0, _rc; \
42 spin_lock(&ir_raw_handler_lock); \
43 list_for_each_entry(_ir_raw_handler, &ir_raw_handler_list, list) { \
44 if (_ir_raw_handler->ops) { \
45 _rc = _ir_raw_handler->ops(__VA_ARGS__); \
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030046 _sumrc += _rc; \
47 } \
48 } \
49 spin_unlock(&ir_raw_handler_lock); \
50 _sumrc; \
51})
52
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -030053#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030054/* Used to load the decoders */
55static struct work_struct wq_load;
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -030056#endif
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030057
David Härdeman724e2492010-04-08 13:10:00 -030058static void ir_raw_event_work(struct work_struct *work)
59{
David Härdemane40b1122010-04-15 18:46:00 -030060 struct ir_raw_event ev;
David Härdeman724e2492010-04-08 13:10:00 -030061 struct ir_raw_event_ctrl *raw =
62 container_of(work, struct ir_raw_event_ctrl, rx_work);
63
David Härdemane40b1122010-04-15 18:46:00 -030064 while (kfifo_out(&raw->kfifo, &ev, sizeof(ev)) == sizeof(ev))
65 RUN_DECODER(decode, raw->input_dev, ev);
David Härdeman724e2492010-04-08 13:10:00 -030066}
67
David Härdeman724e2492010-04-08 13:10:00 -030068/**
69 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
70 * @input_dev: the struct input_dev device descriptor
David Härdemane40b1122010-04-15 18:46:00 -030071 * @ev: the struct ir_raw_event descriptor of the pulse/space
David Härdeman724e2492010-04-08 13:10:00 -030072 *
73 * This routine (which may be called from an interrupt context) stores a
74 * pulse/space duration for the raw ir decoding state machines. Pulses are
75 * signalled as positive values and spaces as negative values. A zero value
76 * will reset the decoding state machines.
77 */
David Härdemane40b1122010-04-15 18:46:00 -030078int ir_raw_event_store(struct input_dev *input_dev, struct ir_raw_event *ev)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030079{
David Härdeman724e2492010-04-08 13:10:00 -030080 struct ir_input_dev *ir = input_get_drvdata(input_dev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030081
82 if (!ir->raw)
83 return -EINVAL;
84
David Härdemane40b1122010-04-15 18:46:00 -030085 if (kfifo_in(&ir->raw->kfifo, ev, sizeof(*ev)) != sizeof(*ev))
David Härdeman724e2492010-04-08 13:10:00 -030086 return -ENOMEM;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030087
David Härdeman724e2492010-04-08 13:10:00 -030088 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030089}
90EXPORT_SYMBOL_GPL(ir_raw_event_store);
91
David Härdeman724e2492010-04-08 13:10:00 -030092/**
93 * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
94 * @input_dev: the struct input_dev device descriptor
95 * @type: the type of the event that has occurred
96 *
97 * This routine (which may be called from an interrupt context) is used to
98 * store the beginning of an ir pulse or space (or the start/end of ir
99 * reception) for the raw ir decoding state machines. This is used by
100 * hardware which does not provide durations directly but only interrupts
101 * (or similar events) on state change.
102 */
103int ir_raw_event_store_edge(struct input_dev *input_dev, enum raw_event_type type)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300104{
David Härdeman724e2492010-04-08 13:10:00 -0300105 struct ir_input_dev *ir = input_get_drvdata(input_dev);
106 ktime_t now;
107 s64 delta; /* ns */
David Härdemane40b1122010-04-15 18:46:00 -0300108 struct ir_raw_event ev;
David Härdeman724e2492010-04-08 13:10:00 -0300109 int rc = 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300110
David Härdeman724e2492010-04-08 13:10:00 -0300111 if (!ir->raw)
112 return -EINVAL;
113
114 now = ktime_get();
115 delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
116
117 /* Check for a long duration since last event or if we're
118 * being called for the first time, note that delta can't
119 * possibly be negative.
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300120 */
David Härdemane40b1122010-04-15 18:46:00 -0300121 ev.duration = 0;
122 if (delta > IR_MAX_DURATION || !ir->raw->last_type)
David Härdeman724e2492010-04-08 13:10:00 -0300123 type |= IR_START_EVENT;
David Härdemane40b1122010-04-15 18:46:00 -0300124 else
125 ev.duration = delta;
David Härdeman724e2492010-04-08 13:10:00 -0300126
127 if (type & IR_START_EVENT)
128 ir_raw_event_reset(input_dev);
David Härdemane40b1122010-04-15 18:46:00 -0300129 else if (ir->raw->last_type & IR_SPACE) {
130 ev.pulse = false;
131 rc = ir_raw_event_store(input_dev, &ev);
132 } else if (ir->raw->last_type & IR_PULSE) {
133 ev.pulse = true;
134 rc = ir_raw_event_store(input_dev, &ev);
135 } else
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300136 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300137
David Härdeman724e2492010-04-08 13:10:00 -0300138 ir->raw->last_event = now;
139 ir->raw->last_type = type;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300140 return rc;
141}
David Härdeman724e2492010-04-08 13:10:00 -0300142EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
143
144/**
145 * ir_raw_event_handle() - schedules the decoding of stored ir data
146 * @input_dev: the struct input_dev device descriptor
147 *
148 * This routine will signal the workqueue to start decoding stored ir data.
149 */
150void ir_raw_event_handle(struct input_dev *input_dev)
151{
152 struct ir_input_dev *ir = input_get_drvdata(input_dev);
153
154 if (!ir->raw)
155 return;
156
157 schedule_work(&ir->raw->rx_work);
158}
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300159EXPORT_SYMBOL_GPL(ir_raw_event_handle);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300160
David Härdeman667c9eb2010-06-13 17:29:31 -0300161/* used internally by the sysfs interface */
162u64
163ir_raw_get_allowed_protocols()
164{
165 u64 protocols;
166 spin_lock(&ir_raw_handler_lock);
167 protocols = available_protocols;
168 spin_unlock(&ir_raw_handler_lock);
169 return protocols;
170}
171
172/*
173 * Used to (un)register raw event clients
174 */
175int ir_raw_event_register(struct input_dev *input_dev)
176{
177 struct ir_input_dev *ir = input_get_drvdata(input_dev);
178 int rc;
179
180 ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
181 if (!ir->raw)
182 return -ENOMEM;
183
184 ir->raw->input_dev = input_dev;
185 INIT_WORK(&ir->raw->rx_work, ir_raw_event_work);
186 ir->raw->enabled_protocols = ~0;
187 rc = kfifo_alloc(&ir->raw->kfifo, sizeof(s64) * MAX_IR_EVENT_SIZE,
188 GFP_KERNEL);
189 if (rc < 0) {
190 kfree(ir->raw);
191 ir->raw = NULL;
192 return rc;
193 }
194
195 rc = RUN_DECODER(raw_register, input_dev);
196 if (rc < 0) {
197 kfifo_free(&ir->raw->kfifo);
198 kfree(ir->raw);
199 ir->raw = NULL;
200 return rc;
201 }
202
203 return rc;
204}
205
206void ir_raw_event_unregister(struct input_dev *input_dev)
207{
208 struct ir_input_dev *ir = input_get_drvdata(input_dev);
209
210 if (!ir->raw)
211 return;
212
213 cancel_work_sync(&ir->raw->rx_work);
214 RUN_DECODER(raw_unregister, input_dev);
215
216 kfifo_free(&ir->raw->kfifo);
217 kfree(ir->raw);
218 ir->raw = NULL;
219}
220
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300221/*
222 * Extension interface - used to register the IR decoders
223 */
224
225int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
226{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300227 spin_lock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300228 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
David Härdeman667c9eb2010-06-13 17:29:31 -0300229 available_protocols |= ir_raw_handler->protocols;
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300230 spin_unlock(&ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -0300231
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300232 return 0;
233}
234EXPORT_SYMBOL(ir_raw_handler_register);
235
236void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
237{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300238 spin_lock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300239 list_del(&ir_raw_handler->list);
David Härdeman667c9eb2010-06-13 17:29:31 -0300240 available_protocols &= ~ir_raw_handler->protocols;
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300241 spin_unlock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300242}
243EXPORT_SYMBOL(ir_raw_handler_unregister);
244
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -0300245#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300246static void init_decoders(struct work_struct *work)
247{
248 /* Load the decoder modules */
249
250 load_nec_decode();
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300251 load_rc5_decode();
David Härdeman784a4932010-04-08 20:04:40 -0300252 load_rc6_decode();
David Härdemanbf670f62010-04-15 18:46:05 -0300253 load_jvc_decode();
David Härdeman3fe29c82010-04-15 18:46:10 -0300254 load_sony_decode();
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300255
256 /* If needed, we may later add some init code. In this case,
257 it is needed to change the CONFIG_MODULE test at ir-core.h
258 */
259}
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -0300260#endif
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300261
262void ir_raw_init(void)
263{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300264#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300265 INIT_WORK(&wq_load, init_decoders);
266 schedule_work(&wq_load);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300267#endif
268}