1 /* ir-raw-event.c - handle IR Pulse/Space event
3 * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
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.
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.
15 #include <media/ir-core.h>
16 #include <linux/workqueue.h>
18 /* Define the max number of bit transitions per IR keycode */
19 #define MAX_IR_EVENT_SIZE 256
21 /* Used to handle IR raw handler extensions */
22 static LIST_HEAD(ir_raw_handler_list
);
23 static DEFINE_MUTEX(ir_raw_handler_lock
);
25 /* Used to load the decoders */
26 static struct work_struct wq_load
;
28 static void ir_keyup_timer(unsigned long data
)
30 struct input_dev
*input_dev
= (struct input_dev
*)data
;
35 int ir_raw_event_register(struct input_dev
*input_dev
)
37 struct ir_input_dev
*ir
= input_get_drvdata(input_dev
);
40 ir
->raw
= kzalloc(sizeof(*ir
->raw
), GFP_KERNEL
);
42 size
= sizeof(struct ir_raw_event
) * MAX_IR_EVENT_SIZE
* 2;
43 size
= roundup_pow_of_two(size
);
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
);
50 rc
= kfifo_alloc(&ir
->raw
->kfifo
, size
, GFP_KERNEL
);
54 EXPORT_SYMBOL_GPL(ir_raw_event_register
);
56 void ir_raw_event_unregister(struct input_dev
*input_dev
)
58 struct ir_input_dev
*ir
= input_get_drvdata(input_dev
);
63 del_timer_sync(&ir
->raw
->timer_keyup
);
65 kfifo_free(&ir
->raw
->kfifo
);
69 EXPORT_SYMBOL_GPL(ir_raw_event_unregister
);
71 int ir_raw_event_store(struct input_dev
*input_dev
, enum raw_event_type type
)
73 struct ir_input_dev
*ir
= input_get_drvdata(input_dev
);
75 struct ir_raw_event event
;
82 event
.delta
.tv_sec
= 0;
83 event
.delta
.tv_nsec
= 0;
87 if (timespec_equal(&ir
->raw
->last_event
, &event
.delta
))
88 event
.type
|= IR_START_EVENT
;
90 event
.delta
= timespec_sub(ts
, ir
->raw
->last_event
);
92 memcpy(&ir
->raw
->last_event
, &ts
, sizeof(ts
));
94 if (event
.delta
.tv_sec
) {
95 event
.type
|= IR_START_EVENT
;
96 event
.delta
.tv_sec
= 0;
97 event
.delta
.tv_nsec
= 0;
100 kfifo_in(&ir
->raw
->kfifo
, &event
, sizeof(event
));
104 EXPORT_SYMBOL_GPL(ir_raw_event_store
);
106 int ir_raw_event_handle(struct input_dev
*input_dev
)
108 struct ir_input_dev
*ir
= input_get_drvdata(input_dev
);
110 struct ir_raw_event
*evs
;
112 struct ir_raw_handler
*ir_raw_handler
;
115 * Store the events into a temporary buffer. This allows calling more than
116 * one decoder to deal with the received data
118 len
= kfifo_len(&ir
->raw
->kfifo
) / sizeof(*evs
);
121 evs
= kmalloc(len
* sizeof(*evs
), GFP_ATOMIC
);
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",
130 IR_dprintk(2, "event type %d, time before event: %07luus\n",
131 evs
[i
].type
, (evs
[i
].delta
.tv_nsec
+ 500) / 1000);
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?
140 list_for_each_entry(ir_raw_handler
, &ir_raw_handler_list
, list
) {
141 rc
= ir_raw_handler
->decode(input_dev
, evs
, len
);
148 EXPORT_SYMBOL_GPL(ir_raw_event_handle
);
151 * Extension interface - used to register the IR decoders
154 int ir_raw_handler_register(struct ir_raw_handler
*ir_raw_handler
)
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
);
161 EXPORT_SYMBOL(ir_raw_handler_register
);
163 void ir_raw_handler_unregister(struct ir_raw_handler
*ir_raw_handler
)
165 mutex_lock(&ir_raw_handler_lock
);
166 list_del(&ir_raw_handler
->list
);
167 mutex_unlock(&ir_raw_handler_lock
);
169 EXPORT_SYMBOL(ir_raw_handler_unregister
);
171 static void init_decoders(struct work_struct
*work
)
173 /* Load the decoder modules */
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
182 void ir_raw_init(void)
184 INIT_WORK(&wq_load
, init_decoders
);
185 schedule_work(&wq_load
);