blob: 11f23f4491b2e6c5c31de5b4977a3cd2349f4bd6 [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
15#include <media/ir-core.h>
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030016#include <linux/workqueue.h>
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030017#include <linux/spinlock.h>
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030018
19/* Define the max number of bit transitions per IR keycode */
20#define MAX_IR_EVENT_SIZE 256
21
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030022/* Used to handle IR raw handler extensions */
23static LIST_HEAD(ir_raw_handler_list);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030024static spinlock_t ir_raw_handler_lock;
25
26/**
27 * RUN_DECODER() - runs an operation on all IR decoders
28 * @ops: IR raw handler operation to be called
29 * @arg: arguments to be passed to the callback
30 *
31 * Calls ir_raw_handler::ops for all registered IR handlers. It prevents
32 * new decode addition/removal while running, by locking ir_raw_handler_lock
33 * mutex. If an error occurs, it stops the ops. Otherwise, it returns a sum
34 * of the return codes.
35 */
36#define RUN_DECODER(ops, ...) ({ \
37 struct ir_raw_handler *_ir_raw_handler; \
38 int _sumrc = 0, _rc; \
39 spin_lock(&ir_raw_handler_lock); \
40 list_for_each_entry(_ir_raw_handler, &ir_raw_handler_list, list) { \
41 if (_ir_raw_handler->ops) { \
42 _rc = _ir_raw_handler->ops(__VA_ARGS__); \
43 if (_rc < 0) \
44 break; \
45 _sumrc += _rc; \
46 } \
47 } \
48 spin_unlock(&ir_raw_handler_lock); \
49 _sumrc; \
50})
51
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030052
53/* Used to load the decoders */
54static struct work_struct wq_load;
55
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -030056static void ir_keyup_timer(unsigned long data)
57{
58 struct input_dev *input_dev = (struct input_dev *)data;
59
60 ir_keyup(input_dev);
61}
62
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030063int ir_raw_event_register(struct input_dev *input_dev)
64{
65 struct ir_input_dev *ir = input_get_drvdata(input_dev);
66 int rc, size;
67
68 ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -030069 if (!ir->raw)
70 return -ENOMEM;
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030071
72 size = sizeof(struct ir_raw_event) * MAX_IR_EVENT_SIZE * 2;
73 size = roundup_pow_of_two(size);
74
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -030075 init_timer(&ir->raw->timer_keyup);
76 ir->raw->timer_keyup.function = ir_keyup_timer;
77 ir->raw->timer_keyup.data = (unsigned long)input_dev;
78 set_bit(EV_REP, input_dev->evbit);
79
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -030080 rc = kfifo_alloc(&ir->raw->kfifo, size, 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}
97EXPORT_SYMBOL_GPL(ir_raw_event_register);
98
99void ir_raw_event_unregister(struct input_dev *input_dev)
100{
101 struct ir_input_dev *ir = input_get_drvdata(input_dev);
102
103 if (!ir->raw)
104 return;
105
Mauro Carvalho Chehab9f154782010-03-21 13:00:55 -0300106 del_timer_sync(&ir->raw->timer_keyup);
107
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300108 RUN_DECODER(raw_unregister, input_dev);
109
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300110 kfifo_free(&ir->raw->kfifo);
111 kfree(ir->raw);
112 ir->raw = NULL;
113}
114EXPORT_SYMBOL_GPL(ir_raw_event_unregister);
115
116int ir_raw_event_store(struct input_dev *input_dev, enum raw_event_type type)
117{
118 struct ir_input_dev *ir = input_get_drvdata(input_dev);
119 struct timespec ts;
120 struct ir_raw_event event;
121 int rc;
122
123 if (!ir->raw)
124 return -EINVAL;
125
126 event.type = type;
127 event.delta.tv_sec = 0;
128 event.delta.tv_nsec = 0;
129
130 ktime_get_ts(&ts);
131
132 if (timespec_equal(&ir->raw->last_event, &event.delta))
133 event.type |= IR_START_EVENT;
134 else
135 event.delta = timespec_sub(ts, ir->raw->last_event);
136
137 memcpy(&ir->raw->last_event, &ts, sizeof(ts));
138
139 if (event.delta.tv_sec) {
140 event.type |= IR_START_EVENT;
141 event.delta.tv_sec = 0;
142 event.delta.tv_nsec = 0;
143 }
144
145 kfifo_in(&ir->raw->kfifo, &event, sizeof(event));
146
147 return rc;
148}
149EXPORT_SYMBOL_GPL(ir_raw_event_store);
150
151int ir_raw_event_handle(struct input_dev *input_dev)
152{
153 struct ir_input_dev *ir = input_get_drvdata(input_dev);
154 int rc;
155 struct ir_raw_event *evs;
156 int len, i;
157
158 /*
159 * Store the events into a temporary buffer. This allows calling more than
160 * one decoder to deal with the received data
161 */
162 len = kfifo_len(&ir->raw->kfifo) / sizeof(*evs);
163 if (!len)
164 return 0;
165 evs = kmalloc(len * sizeof(*evs), GFP_ATOMIC);
166
167 for (i = 0; i < len; i++) {
168 rc = kfifo_out(&ir->raw->kfifo, &evs[i], sizeof(*evs));
169 if (rc != sizeof(*evs)) {
170 IR_dprintk(1, "overflow error: received %d instead of %zd\n",
171 rc, sizeof(*evs));
172 return -EINVAL;
173 }
174 IR_dprintk(2, "event type %d, time before event: %07luus\n",
175 evs[i].type, (evs[i].delta.tv_nsec + 500) / 1000);
176 }
177
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300178 /*
179 * Call all ir decoders. This allows decoding the same event with
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300180 * more than one protocol handler. It returns the number of keystrokes
181 * sent to the event interface
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300182 */
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300183 rc = RUN_DECODER(decode, input_dev, evs, len);
Mauro Carvalho Chehaba3572c32010-03-20 20:59:44 -0300184
185 kfree(evs);
186
187 return rc;
188}
189EXPORT_SYMBOL_GPL(ir_raw_event_handle);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300190
191/*
192 * Extension interface - used to register the IR decoders
193 */
194
195int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
196{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300197 spin_lock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300198 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300199 spin_unlock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300200 return 0;
201}
202EXPORT_SYMBOL(ir_raw_handler_register);
203
204void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
205{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300206 spin_lock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300207 list_del(&ir_raw_handler->list);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300208 spin_unlock(&ir_raw_handler_lock);
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300209}
210EXPORT_SYMBOL(ir_raw_handler_unregister);
211
212static void init_decoders(struct work_struct *work)
213{
214 /* Load the decoder modules */
215
216 load_nec_decode();
217
218 /* If needed, we may later add some init code. In this case,
219 it is needed to change the CONFIG_MODULE test at ir-core.h
220 */
221}
222
223void ir_raw_init(void)
224{
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300225 spin_lock_init(&ir_raw_handler_lock);
226
227#ifdef MODULE
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300228 INIT_WORK(&wq_load, init_decoders);
229 schedule_work(&wq_load);
Mauro Carvalho Chehab93c312f2010-03-25 21:13:43 -0300230#endif
231}