blob: 51f65daa086bb7d5a651e785e00a296b06ed0193 [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
David Härdemanc2163692010-06-13 17:29:36 -030023/* Used to keep track of IR raw clients, protected by ir_raw_handler_lock */
24static LIST_HEAD(ir_raw_client_list);
25
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030026/* Used to handle IR raw handler extensions */
Mauro Carvalho Chehab6eb94352010-04-07 16:19:36 -030027static DEFINE_SPINLOCK(ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -030028static LIST_HEAD(ir_raw_handler_list);
29static u64 available_protocols;
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030030
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -030031#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030032/* Used to load the decoders */
33static struct work_struct wq_load;
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -030034#endif
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030035
David Härdeman724e2492010-04-08 13:10:00 -030036static void ir_raw_event_work(struct work_struct *work)
37{
David Härdemane40b1122010-04-15 18:46:00 -030038 struct ir_raw_event ev;
David Härdemanc2163692010-06-13 17:29:36 -030039 struct ir_raw_handler *handler;
David Härdeman724e2492010-04-08 13:10:00 -030040 struct ir_raw_event_ctrl *raw =
41 container_of(work, struct ir_raw_event_ctrl, rx_work);
42
David Härdemanc2163692010-06-13 17:29:36 -030043 while (kfifo_out(&raw->kfifo, &ev, sizeof(ev)) == sizeof(ev)) {
44 spin_lock(&ir_raw_handler_lock);
45 list_for_each_entry(handler, &ir_raw_handler_list, list)
46 handler->decode(raw->input_dev, ev);
47 spin_unlock(&ir_raw_handler_lock);
48 raw->prev_ev = ev;
49 }
David Härdeman724e2492010-04-08 13:10:00 -030050}
51
David Härdeman724e2492010-04-08 13:10:00 -030052/**
53 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
54 * @input_dev: the struct input_dev device descriptor
David Härdemane40b1122010-04-15 18:46:00 -030055 * @ev: the struct ir_raw_event descriptor of the pulse/space
David Härdeman724e2492010-04-08 13:10:00 -030056 *
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.
61 */
David Härdemane40b1122010-04-15 18:46:00 -030062int ir_raw_event_store(struct input_dev *input_dev, struct ir_raw_event *ev)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030063{
David Härdeman724e2492010-04-08 13:10:00 -030064 struct ir_input_dev *ir = input_get_drvdata(input_dev);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030065
66 if (!ir->raw)
67 return -EINVAL;
68
Maxim Levitsky510fcb72010-07-31 11:59:15 -030069 IR_dprintk(2, "sample: (05%dus %s)\n",
70 TO_US(ev->duration), TO_STR(ev->pulse));
71
David Härdemane40b1122010-04-15 18:46:00 -030072 if (kfifo_in(&ir->raw->kfifo, ev, sizeof(*ev)) != sizeof(*ev))
David Härdeman724e2492010-04-08 13:10:00 -030073 return -ENOMEM;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030074
David Härdeman724e2492010-04-08 13:10:00 -030075 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030076}
77EXPORT_SYMBOL_GPL(ir_raw_event_store);
78
David Härdeman724e2492010-04-08 13:10:00 -030079/**
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
83 *
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.
89 */
90int ir_raw_event_store_edge(struct input_dev *input_dev, enum raw_event_type type)
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030091{
David Härdeman724e2492010-04-08 13:10:00 -030092 struct ir_input_dev *ir = input_get_drvdata(input_dev);
93 ktime_t now;
94 s64 delta; /* ns */
David Härdemane40b1122010-04-15 18:46:00 -030095 struct ir_raw_event ev;
David Härdeman724e2492010-04-08 13:10:00 -030096 int rc = 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030097
David Härdeman724e2492010-04-08 13:10:00 -030098 if (!ir->raw)
99 return -EINVAL;
100
101 now = ktime_get();
102 delta = ktime_to_ns(ktime_sub(now, ir->raw->last_event));
103
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.
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300107 */
David Härdemane40b1122010-04-15 18:46:00 -0300108 ev.duration = 0;
109 if (delta > IR_MAX_DURATION || !ir->raw->last_type)
David Härdeman724e2492010-04-08 13:10:00 -0300110 type |= IR_START_EVENT;
David Härdemane40b1122010-04-15 18:46:00 -0300111 else
112 ev.duration = delta;
David Härdeman724e2492010-04-08 13:10:00 -0300113
114 if (type & IR_START_EVENT)
115 ir_raw_event_reset(input_dev);
David Härdemane40b1122010-04-15 18:46:00 -0300116 else if (ir->raw->last_type & IR_SPACE) {
117 ev.pulse = false;
118 rc = ir_raw_event_store(input_dev, &ev);
119 } else if (ir->raw->last_type & IR_PULSE) {
120 ev.pulse = true;
121 rc = ir_raw_event_store(input_dev, &ev);
122 } else
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300123 return 0;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300124
David Härdeman724e2492010-04-08 13:10:00 -0300125 ir->raw->last_event = now;
126 ir->raw->last_type = type;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300127 return rc;
128}
David Härdeman724e2492010-04-08 13:10:00 -0300129EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
130
131/**
132 * ir_raw_event_handle() - schedules the decoding of stored ir data
133 * @input_dev: the struct input_dev device descriptor
134 *
135 * This routine will signal the workqueue to start decoding stored ir data.
136 */
137void ir_raw_event_handle(struct input_dev *input_dev)
138{
139 struct ir_input_dev *ir = input_get_drvdata(input_dev);
140
141 if (!ir->raw)
142 return;
143
144 schedule_work(&ir->raw->rx_work);
145}
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300146EXPORT_SYMBOL_GPL(ir_raw_event_handle);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300147
David Härdeman667c9eb2010-06-13 17:29:31 -0300148/* used internally by the sysfs interface */
149u64
150ir_raw_get_allowed_protocols()
151{
152 u64 protocols;
153 spin_lock(&ir_raw_handler_lock);
154 protocols = available_protocols;
155 spin_unlock(&ir_raw_handler_lock);
156 return protocols;
157}
158
159/*
160 * Used to (un)register raw event clients
161 */
162int ir_raw_event_register(struct input_dev *input_dev)
163{
164 struct ir_input_dev *ir = input_get_drvdata(input_dev);
165 int rc;
David Härdemanc2163692010-06-13 17:29:36 -0300166 struct ir_raw_handler *handler;
David Härdeman667c9eb2010-06-13 17:29:31 -0300167
168 ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
169 if (!ir->raw)
170 return -ENOMEM;
171
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,
176 GFP_KERNEL);
177 if (rc < 0) {
178 kfree(ir->raw);
179 ir->raw = NULL;
180 return rc;
181 }
182
David Härdemanc2163692010-06-13 17:29:36 -0300183 spin_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 spin_unlock(&ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -0300189
David Härdemanc2163692010-06-13 17:29:36 -0300190 return 0;
David Härdeman667c9eb2010-06-13 17:29:31 -0300191}
192
193void ir_raw_event_unregister(struct input_dev *input_dev)
194{
195 struct ir_input_dev *ir = input_get_drvdata(input_dev);
David Härdemanc2163692010-06-13 17:29:36 -0300196 struct ir_raw_handler *handler;
David Härdeman667c9eb2010-06-13 17:29:31 -0300197
198 if (!ir->raw)
199 return;
200
201 cancel_work_sync(&ir->raw->rx_work);
David Härdemanc2163692010-06-13 17:29:36 -0300202
203 spin_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 spin_unlock(&ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -0300209
210 kfifo_free(&ir->raw->kfifo);
211 kfree(ir->raw);
212 ir->raw = NULL;
213}
214
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300215/*
216 * Extension interface - used to register the IR decoders
217 */
218
219int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
220{
David Härdemanc2163692010-06-13 17:29:36 -0300221 struct ir_raw_event_ctrl *raw;
222
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300223 spin_lock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300224 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
David Härdemanc2163692010-06-13 17:29:36 -0300225 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);
David Härdeman667c9eb2010-06-13 17:29:31 -0300228 available_protocols |= ir_raw_handler->protocols;
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300229 spin_unlock(&ir_raw_handler_lock);
David Härdeman667c9eb2010-06-13 17:29:31 -0300230
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300231 return 0;
232}
233EXPORT_SYMBOL(ir_raw_handler_register);
234
235void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
236{
David Härdemanc2163692010-06-13 17:29:36 -0300237 struct ir_raw_event_ctrl *raw;
238
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300239 spin_lock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300240 list_del(&ir_raw_handler->list);
David Härdemanc2163692010-06-13 17:29:36 -0300241 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);
David Härdeman667c9eb2010-06-13 17:29:31 -0300244 available_protocols &= ~ir_raw_handler->protocols;
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300245 spin_unlock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300246}
247EXPORT_SYMBOL(ir_raw_handler_unregister);
248
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -0300249#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300250static void init_decoders(struct work_struct *work)
251{
252 /* Load the decoder modules */
253
254 load_nec_decode();
Mauro Carvalho Chehabdb1423a2010-04-04 10:27:20 -0300255 load_rc5_decode();
David Härdeman784a4932010-04-08 20:04:40 -0300256 load_rc6_decode();
David Härdemanbf670f62010-04-15 18:46:05 -0300257 load_jvc_decode();
David Härdeman3fe29c82010-04-15 18:46:10 -0300258 load_sony_decode();
Jarod Wilsonca414692010-07-03 01:07:53 -0300259 load_lirc_codec();
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300260
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
263 */
264}
Mauro Carvalho Chehab5fa29892010-04-08 19:04:06 -0300265#endif
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300266
267void ir_raw_init(void)
268{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300269#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300270 INIT_WORK(&wq_load, init_decoders);
271 schedule_work(&wq_load);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300272#endif
273}