]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/IR/ir-raw-event.c
V4L/DVB: ir-nec-decoder: Add sysfs node to enable/disable per irrcv
[net-next-2.6.git] / drivers / media / IR / ir-raw-event.c
CommitLineData
a3572c34
MCC
1/* 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>
995187be 16#include <linux/workqueue.h>
93c312ff 17#include <linux/spinlock.h>
a3572c34
MCC
18
19/* Define the max number of bit transitions per IR keycode */
20#define MAX_IR_EVENT_SIZE 256
21
995187be
MCC
22/* Used to handle IR raw handler extensions */
23static LIST_HEAD(ir_raw_handler_list);
93c312ff
MCC
24static 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
995187be
MCC
52
53/* Used to load the decoders */
54static struct work_struct wq_load;
55
9f154782
MCC
56static 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
a3572c34
MCC
63int 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);
93c312ff
MCC
69 if (!ir->raw)
70 return -ENOMEM;
a3572c34
MCC
71
72 size = sizeof(struct ir_raw_event) * MAX_IR_EVENT_SIZE * 2;
73 size = roundup_pow_of_two(size);
74
9f154782
MCC
75 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
a3572c34 80 rc = kfifo_alloc(&ir->raw->kfifo, size, GFP_KERNEL);
93c312ff
MCC
81 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 }
a3572c34
MCC
94
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
9f154782
MCC
106 del_timer_sync(&ir->raw->timer_keyup);
107
93c312ff
MCC
108 RUN_DECODER(raw_unregister, input_dev);
109
a3572c34
MCC
110 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
995187be
MCC
178 /*
179 * Call all ir decoders. This allows decoding the same event with
93c312ff
MCC
180 * more than one protocol handler. It returns the number of keystrokes
181 * sent to the event interface
995187be 182 */
93c312ff 183 rc = RUN_DECODER(decode, input_dev, evs, len);
a3572c34
MCC
184
185 kfree(evs);
186
187 return rc;
188}
189EXPORT_SYMBOL_GPL(ir_raw_event_handle);
995187be
MCC
190
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{
93c312ff 197 spin_lock(&ir_raw_handler_lock);
995187be 198 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
93c312ff 199 spin_unlock(&ir_raw_handler_lock);
995187be
MCC
200 return 0;
201}
202EXPORT_SYMBOL(ir_raw_handler_register);
203
204void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
205{
93c312ff 206 spin_lock(&ir_raw_handler_lock);
995187be 207 list_del(&ir_raw_handler->list);
93c312ff 208 spin_unlock(&ir_raw_handler_lock);
995187be
MCC
209}
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{
93c312ff 225 spin_lock_init(&ir_raw_handler_lock);
20d5f116 226
93c312ff 227#ifdef MODULE
995187be
MCC
228 INIT_WORK(&wq_load, init_decoders);
229 schedule_work(&wq_load);
93c312ff
MCC
230#endif
231}