]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/IR/ir-raw-event.c
V4L/DVB: ir-core: dynamically load the compiled IR protocols
[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>
a3572c34
MCC
17
18/* Define the max number of bit transitions per IR keycode */
19#define MAX_IR_EVENT_SIZE 256
20
995187be
MCC
21/* Used to handle IR raw handler extensions */
22static LIST_HEAD(ir_raw_handler_list);
23static DEFINE_MUTEX(ir_raw_handler_lock);
24
25/* Used to load the decoders */
26static struct work_struct wq_load;
27
9f154782
MCC
28static void ir_keyup_timer(unsigned long data)
29{
30 struct input_dev *input_dev = (struct input_dev *)data;
31
32 ir_keyup(input_dev);
33}
34
a3572c34
MCC
35int ir_raw_event_register(struct input_dev *input_dev)
36{
37 struct ir_input_dev *ir = input_get_drvdata(input_dev);
38 int rc, size;
39
40 ir->raw = kzalloc(sizeof(*ir->raw), GFP_KERNEL);
41
42 size = sizeof(struct ir_raw_event) * MAX_IR_EVENT_SIZE * 2;
43 size = roundup_pow_of_two(size);
44
9f154782
MCC
45 init_timer(&ir->raw->timer_keyup);
46 ir->raw->timer_keyup.function = ir_keyup_timer;
47 ir->raw->timer_keyup.data = (unsigned long)input_dev;
48 set_bit(EV_REP, input_dev->evbit);
49
a3572c34
MCC
50 rc = kfifo_alloc(&ir->raw->kfifo, size, GFP_KERNEL);
51
52 return rc;
53}
54EXPORT_SYMBOL_GPL(ir_raw_event_register);
55
56void ir_raw_event_unregister(struct input_dev *input_dev)
57{
58 struct ir_input_dev *ir = input_get_drvdata(input_dev);
59
60 if (!ir->raw)
61 return;
62
9f154782
MCC
63 del_timer_sync(&ir->raw->timer_keyup);
64
a3572c34
MCC
65 kfifo_free(&ir->raw->kfifo);
66 kfree(ir->raw);
67 ir->raw = NULL;
68}
69EXPORT_SYMBOL_GPL(ir_raw_event_unregister);
70
71int ir_raw_event_store(struct input_dev *input_dev, enum raw_event_type type)
72{
73 struct ir_input_dev *ir = input_get_drvdata(input_dev);
74 struct timespec ts;
75 struct ir_raw_event event;
76 int rc;
77
78 if (!ir->raw)
79 return -EINVAL;
80
81 event.type = type;
82 event.delta.tv_sec = 0;
83 event.delta.tv_nsec = 0;
84
85 ktime_get_ts(&ts);
86
87 if (timespec_equal(&ir->raw->last_event, &event.delta))
88 event.type |= IR_START_EVENT;
89 else
90 event.delta = timespec_sub(ts, ir->raw->last_event);
91
92 memcpy(&ir->raw->last_event, &ts, sizeof(ts));
93
94 if (event.delta.tv_sec) {
95 event.type |= IR_START_EVENT;
96 event.delta.tv_sec = 0;
97 event.delta.tv_nsec = 0;
98 }
99
100 kfifo_in(&ir->raw->kfifo, &event, sizeof(event));
101
102 return rc;
103}
104EXPORT_SYMBOL_GPL(ir_raw_event_store);
105
106int ir_raw_event_handle(struct input_dev *input_dev)
107{
108 struct ir_input_dev *ir = input_get_drvdata(input_dev);
109 int rc;
110 struct ir_raw_event *evs;
111 int len, i;
995187be 112 struct ir_raw_handler *ir_raw_handler;
a3572c34
MCC
113
114 /*
115 * Store the events into a temporary buffer. This allows calling more than
116 * one decoder to deal with the received data
117 */
118 len = kfifo_len(&ir->raw->kfifo) / sizeof(*evs);
119 if (!len)
120 return 0;
121 evs = kmalloc(len * sizeof(*evs), GFP_ATOMIC);
122
123 for (i = 0; i < len; i++) {
124 rc = kfifo_out(&ir->raw->kfifo, &evs[i], sizeof(*evs));
125 if (rc != sizeof(*evs)) {
126 IR_dprintk(1, "overflow error: received %d instead of %zd\n",
127 rc, sizeof(*evs));
128 return -EINVAL;
129 }
130 IR_dprintk(2, "event type %d, time before event: %07luus\n",
131 evs[i].type, (evs[i].delta.tv_nsec + 500) / 1000);
132 }
133
995187be
MCC
134 /*
135 * Call all ir decoders. This allows decoding the same event with
136 * more than one protocol handler.
137 * FIXME: better handle the returned code: does it make sense to use
138 * other decoders, if the first one already handled the IR?
139 */
140 list_for_each_entry(ir_raw_handler, &ir_raw_handler_list, list) {
141 rc = ir_raw_handler->decode(input_dev, evs, len);
142 }
a3572c34
MCC
143
144 kfree(evs);
145
146 return rc;
147}
148EXPORT_SYMBOL_GPL(ir_raw_event_handle);
995187be
MCC
149
150/*
151 * Extension interface - used to register the IR decoders
152 */
153
154int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
155{
156 mutex_lock(&ir_raw_handler_lock);
157 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
158 mutex_unlock(&ir_raw_handler_lock);
159 return 0;
160}
161EXPORT_SYMBOL(ir_raw_handler_register);
162
163void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
164{
165 mutex_lock(&ir_raw_handler_lock);
166 list_del(&ir_raw_handler->list);
167 mutex_unlock(&ir_raw_handler_lock);
168}
169EXPORT_SYMBOL(ir_raw_handler_unregister);
170
171static void init_decoders(struct work_struct *work)
172{
173 /* Load the decoder modules */
174
175 load_nec_decode();
176
177 /* If needed, we may later add some init code. In this case,
178 it is needed to change the CONFIG_MODULE test at ir-core.h
179 */
180}
181
182void ir_raw_init(void)
183{
184 INIT_WORK(&wq_load, init_decoders);
185 schedule_work(&wq_load);
186}