1 /* rc-ir-raw.c - handle IR pulse/space events
3 * Copyright (C) 2010 by Mauro Carvalho Chehab
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 <linux/export.h>
16 #include <linux/kthread.h>
17 #include <linux/mutex.h>
18 #include <linux/kmod.h>
19 #include <linux/sched.h>
20 #include "rc-core-priv.h"
22 /* Used to keep track of IR raw clients, protected by ir_raw_handler_lock */
23 static LIST_HEAD(ir_raw_client_list);
25 /* Used to handle IR raw handler extensions */
26 static DEFINE_MUTEX(ir_raw_handler_lock);
27 static LIST_HEAD(ir_raw_handler_list);
28 static atomic64_t available_protocols = ATOMIC64_INIT(0);
30 static int ir_raw_event_thread(void *data)
32 struct ir_raw_event ev;
33 struct ir_raw_handler *handler;
34 struct ir_raw_event_ctrl *raw = (struct ir_raw_event_ctrl *)data;
37 mutex_lock(&ir_raw_handler_lock);
38 while (kfifo_out(&raw->kfifo, &ev, 1)) {
39 list_for_each_entry(handler, &ir_raw_handler_list, list)
40 if (raw->dev->enabled_protocols &
41 handler->protocols || !handler->protocols)
42 handler->decode(raw->dev, ev);
45 mutex_unlock(&ir_raw_handler_lock);
47 set_current_state(TASK_INTERRUPTIBLE);
49 if (kthread_should_stop()) {
50 __set_current_state(TASK_RUNNING);
52 } else if (!kfifo_is_empty(&raw->kfifo))
53 set_current_state(TASK_RUNNING);
62 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
63 * @dev: the struct rc_dev device descriptor
64 * @ev: the struct ir_raw_event descriptor of the pulse/space
66 * This routine (which may be called from an interrupt context) stores a
67 * pulse/space duration for the raw ir decoding state machines. Pulses are
68 * signalled as positive values and spaces as negative values. A zero value
69 * will reset the decoding state machines.
71 int ir_raw_event_store(struct rc_dev *dev, struct ir_raw_event *ev)
76 IR_dprintk(2, "sample: (%05dus %s)\n",
77 TO_US(ev->duration), TO_STR(ev->pulse));
79 if (!kfifo_put(&dev->raw->kfifo, *ev)) {
80 dev_err(&dev->dev, "IR event FIFO is full!\n");
86 EXPORT_SYMBOL_GPL(ir_raw_event_store);
89 * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
90 * @dev: the struct rc_dev device descriptor
91 * @type: the type of the event that has occurred
93 * This routine (which may be called from an interrupt context) is used to
94 * store the beginning of an ir pulse or space (or the start/end of ir
95 * reception) for the raw ir decoding state machines. This is used by
96 * hardware which does not provide durations directly but only interrupts
97 * (or similar events) on state change.
99 int ir_raw_event_store_edge(struct rc_dev *dev, enum raw_event_type type)
103 DEFINE_IR_RAW_EVENT(ev);
111 delta = ktime_to_ns(ktime_sub(now, dev->raw->last_event));
112 delay = MS_TO_NS(dev->input_dev->rep[REP_DELAY]);
114 /* Check for a long duration since last event or if we're
115 * being called for the first time, note that delta can't
116 * possibly be negative.
118 if (delta > delay || !dev->raw->last_type)
119 type |= IR_START_EVENT;
123 if (type & IR_START_EVENT)
124 ir_raw_event_reset(dev);
125 else if (dev->raw->last_type & IR_SPACE) {
127 rc = ir_raw_event_store(dev, &ev);
128 } else if (dev->raw->last_type & IR_PULSE) {
130 rc = ir_raw_event_store(dev, &ev);
134 dev->raw->last_event = now;
135 dev->raw->last_type = type;
138 EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
141 * ir_raw_event_store_with_filter() - pass next pulse/space to decoders with some processing
142 * @dev: the struct rc_dev device descriptor
143 * @type: the type of the event that has occurred
145 * This routine (which may be called from an interrupt context) works
146 * in similar manner to ir_raw_event_store_edge.
147 * This routine is intended for devices with limited internal buffer
148 * It automerges samples of same type, and handles timeouts. Returns non-zero
149 * if the event was added, and zero if the event was ignored due to idle
152 int ir_raw_event_store_with_filter(struct rc_dev *dev, struct ir_raw_event *ev)
157 /* Ignore spaces in idle mode */
158 if (dev->idle && !ev->pulse)
161 ir_raw_event_set_idle(dev, false);
163 if (!dev->raw->this_ev.duration)
164 dev->raw->this_ev = *ev;
165 else if (ev->pulse == dev->raw->this_ev.pulse)
166 dev->raw->this_ev.duration += ev->duration;
168 ir_raw_event_store(dev, &dev->raw->this_ev);
169 dev->raw->this_ev = *ev;
172 /* Enter idle mode if nessesary */
173 if (!ev->pulse && dev->timeout &&
174 dev->raw->this_ev.duration >= dev->timeout)
175 ir_raw_event_set_idle(dev, true);
179 EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter);
182 * ir_raw_event_set_idle() - provide hint to rc-core when the device is idle or not
183 * @dev: the struct rc_dev device descriptor
184 * @idle: whether the device is idle or not
186 void ir_raw_event_set_idle(struct rc_dev *dev, bool idle)
191 IR_dprintk(2, "%s idle mode\n", idle ? "enter" : "leave");
194 dev->raw->this_ev.timeout = true;
195 ir_raw_event_store(dev, &dev->raw->this_ev);
196 init_ir_raw_event(&dev->raw->this_ev);
200 dev->s_idle(dev, idle);
204 EXPORT_SYMBOL_GPL(ir_raw_event_set_idle);
207 * ir_raw_event_handle() - schedules the decoding of stored ir data
208 * @dev: the struct rc_dev device descriptor
210 * This routine will tell rc-core to start decoding stored ir data.
212 void ir_raw_event_handle(struct rc_dev *dev)
217 wake_up_process(dev->raw->thread);
219 EXPORT_SYMBOL_GPL(ir_raw_event_handle);
221 /* used internally by the sysfs interface */
223 ir_raw_get_allowed_protocols(void)
225 return atomic64_read(&available_protocols);
228 static int change_protocol(struct rc_dev *dev, u64 *rc_type)
230 /* the caller will update dev->enabled_protocols */
234 static void ir_raw_disable_protocols(struct rc_dev *dev, u64 protocols)
236 mutex_lock(&dev->lock);
237 dev->enabled_protocols &= ~protocols;
238 mutex_unlock(&dev->lock);
242 * ir_raw_gen_manchester() - Encode data with Manchester (bi-phase) modulation.
243 * @ev: Pointer to pointer to next free event. *@ev is incremented for
244 * each raw event filled.
245 * @max: Maximum number of raw events to fill.
246 * @timings: Manchester modulation timings.
247 * @n: Number of bits of data.
248 * @data: Data bits to encode.
250 * Encodes the @n least significant bits of @data using Manchester (bi-phase)
251 * modulation with the timing characteristics described by @timings, writing up
252 * to @max raw IR events using the *@ev pointer.
254 * Returns: 0 on success.
255 * -ENOBUFS if there isn't enough space in the array to fit the
256 * full encoded data. In this case all @max events will have been
259 int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max,
260 const struct ir_raw_timings_manchester *timings,
261 unsigned int n, unsigned int data)
269 if (timings->leader) {
272 if (timings->pulse_space_start) {
273 init_ir_raw_event_duration((*ev)++, 1, timings->leader);
277 init_ir_raw_event_duration((*ev), 0, timings->leader);
279 init_ir_raw_event_duration((*ev), 1, timings->leader);
283 /* continue existing signal */
286 /* from here on *ev will point to the last event rather than the next */
289 need_pulse = !(data & i);
291 need_pulse = !need_pulse;
292 if (need_pulse == !!(*ev)->pulse) {
293 (*ev)->duration += timings->clock;
297 init_ir_raw_event_duration(++(*ev), need_pulse,
303 init_ir_raw_event_duration(++(*ev), !need_pulse,
308 if (timings->trailer_space) {
310 (*ev)->duration += timings->trailer_space;
314 init_ir_raw_event_duration(++(*ev), 0,
315 timings->trailer_space);
320 /* point to the next event rather than last event before returning */
324 EXPORT_SYMBOL(ir_raw_gen_manchester);
327 * ir_raw_gen_pd() - Encode data to raw events with pulse-distance modulation.
328 * @ev: Pointer to pointer to next free event. *@ev is incremented for
329 * each raw event filled.
330 * @max: Maximum number of raw events to fill.
331 * @timings: Pulse distance modulation timings.
332 * @n: Number of bits of data.
333 * @data: Data bits to encode.
335 * Encodes the @n least significant bits of @data using pulse-distance
336 * modulation with the timing characteristics described by @timings, writing up
337 * to @max raw IR events using the *@ev pointer.
339 * Returns: 0 on success.
340 * -ENOBUFS if there isn't enough space in the array to fit the
341 * full encoded data. In this case all @max events will have been
344 int ir_raw_gen_pd(struct ir_raw_event **ev, unsigned int max,
345 const struct ir_raw_timings_pd *timings,
346 unsigned int n, u64 data)
352 if (timings->header_pulse) {
353 ret = ir_raw_gen_pulse_space(ev, &max, timings->header_pulse,
354 timings->header_space);
359 if (timings->msb_first) {
360 for (i = n - 1; i >= 0; --i) {
361 space = timings->bit_space[(data >> i) & 1];
362 ret = ir_raw_gen_pulse_space(ev, &max,
369 for (i = 0; i < n; ++i, data >>= 1) {
370 space = timings->bit_space[data & 1];
371 ret = ir_raw_gen_pulse_space(ev, &max,
379 ret = ir_raw_gen_pulse_space(ev, &max, timings->trailer_pulse,
380 timings->trailer_space);
383 EXPORT_SYMBOL(ir_raw_gen_pd);
386 * ir_raw_gen_pl() - Encode data to raw events with pulse-length modulation.
387 * @ev: Pointer to pointer to next free event. *@ev is incremented for
388 * each raw event filled.
389 * @max: Maximum number of raw events to fill.
390 * @timings: Pulse distance modulation timings.
391 * @n: Number of bits of data.
392 * @data: Data bits to encode.
394 * Encodes the @n least significant bits of @data using space-distance
395 * modulation with the timing characteristics described by @timings, writing up
396 * to @max raw IR events using the *@ev pointer.
398 * Returns: 0 on success.
399 * -ENOBUFS if there isn't enough space in the array to fit the
400 * full encoded data. In this case all @max events will have been
403 int ir_raw_gen_pl(struct ir_raw_event **ev, unsigned int max,
404 const struct ir_raw_timings_pl *timings,
405 unsigned int n, u64 data)
414 init_ir_raw_event_duration((*ev)++, 1, timings->header_pulse);
416 if (timings->msb_first) {
417 for (i = n - 1; i >= 0; --i) {
420 init_ir_raw_event_duration((*ev)++, 0,
424 pulse = timings->bit_pulse[(data >> i) & 1];
425 init_ir_raw_event_duration((*ev)++, 1, pulse);
428 for (i = 0; i < n; ++i, data >>= 1) {
431 init_ir_raw_event_duration((*ev)++, 0,
435 pulse = timings->bit_pulse[data & 1];
436 init_ir_raw_event_duration((*ev)++, 1, pulse);
443 init_ir_raw_event_duration((*ev)++, 0, timings->trailer_space);
447 EXPORT_SYMBOL(ir_raw_gen_pl);
450 * ir_raw_encode_scancode() - Encode a scancode as raw events
452 * @protocol: protocol
453 * @scancode: scancode filter describing a single scancode
454 * @events: array of raw events to write into
455 * @max: max number of raw events
457 * Attempts to encode the scancode as raw events.
459 * Returns: The number of events written.
460 * -ENOBUFS if there isn't enough space in the array to fit the
461 * encoding. In this case all @max events will have been written.
462 * -EINVAL if the scancode is ambiguous or invalid, or if no
463 * compatible encoder was found.
465 int ir_raw_encode_scancode(enum rc_type protocol, u32 scancode,
466 struct ir_raw_event *events, unsigned int max)
468 struct ir_raw_handler *handler;
470 u64 mask = 1ULL << protocol;
472 mutex_lock(&ir_raw_handler_lock);
473 list_for_each_entry(handler, &ir_raw_handler_list, list) {
474 if (handler->protocols & mask && handler->encode) {
475 ret = handler->encode(protocol, scancode, events, max);
476 if (ret >= 0 || ret == -ENOBUFS)
480 mutex_unlock(&ir_raw_handler_lock);
484 EXPORT_SYMBOL(ir_raw_encode_scancode);
487 * Used to (un)register raw event clients
489 int ir_raw_event_register(struct rc_dev *dev)
492 struct ir_raw_handler *handler;
497 dev->raw = kzalloc(sizeof(*dev->raw), GFP_KERNEL);
502 dev->change_protocol = change_protocol;
503 INIT_KFIFO(dev->raw->kfifo);
506 * raw transmitters do not need any event registration
507 * because the event is coming from userspace
509 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
510 dev->raw->thread = kthread_run(ir_raw_event_thread, dev->raw,
513 if (IS_ERR(dev->raw->thread)) {
514 rc = PTR_ERR(dev->raw->thread);
519 mutex_lock(&ir_raw_handler_lock);
520 list_add_tail(&dev->raw->list, &ir_raw_client_list);
521 list_for_each_entry(handler, &ir_raw_handler_list, list)
522 if (handler->raw_register)
523 handler->raw_register(dev);
524 mutex_unlock(&ir_raw_handler_lock);
534 void ir_raw_event_unregister(struct rc_dev *dev)
536 struct ir_raw_handler *handler;
538 if (!dev || !dev->raw)
541 kthread_stop(dev->raw->thread);
543 mutex_lock(&ir_raw_handler_lock);
544 list_del(&dev->raw->list);
545 list_for_each_entry(handler, &ir_raw_handler_list, list)
546 if (handler->raw_unregister)
547 handler->raw_unregister(dev);
548 mutex_unlock(&ir_raw_handler_lock);
555 * Extension interface - used to register the IR decoders
558 int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
560 struct ir_raw_event_ctrl *raw;
562 mutex_lock(&ir_raw_handler_lock);
563 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
564 if (ir_raw_handler->raw_register)
565 list_for_each_entry(raw, &ir_raw_client_list, list)
566 ir_raw_handler->raw_register(raw->dev);
567 atomic64_or(ir_raw_handler->protocols, &available_protocols);
568 mutex_unlock(&ir_raw_handler_lock);
572 EXPORT_SYMBOL(ir_raw_handler_register);
574 void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
576 struct ir_raw_event_ctrl *raw;
577 u64 protocols = ir_raw_handler->protocols;
579 mutex_lock(&ir_raw_handler_lock);
580 list_del(&ir_raw_handler->list);
581 list_for_each_entry(raw, &ir_raw_client_list, list) {
582 ir_raw_disable_protocols(raw->dev, protocols);
583 if (ir_raw_handler->raw_unregister)
584 ir_raw_handler->raw_unregister(raw->dev);
586 atomic64_andnot(protocols, &available_protocols);
587 mutex_unlock(&ir_raw_handler_lock);
589 EXPORT_SYMBOL(ir_raw_handler_unregister);