1 // SPDX-License-Identifier: GPL-2.0
2 // rc-ir-raw.c - handle IR pulse/space events
4 // Copyright (C) 2010 by Mauro Carvalho Chehab
6 #include <linux/export.h>
7 #include <linux/kthread.h>
8 #include <linux/mutex.h>
9 #include <linux/kmod.h>
10 #include <linux/sched.h>
11 #include "rc-core-priv.h"
13 /* Used to keep track of IR raw clients, protected by ir_raw_handler_lock */
14 static LIST_HEAD(ir_raw_client_list);
16 /* Used to handle IR raw handler extensions */
17 DEFINE_MUTEX(ir_raw_handler_lock);
18 static LIST_HEAD(ir_raw_handler_list);
19 static atomic64_t available_protocols = ATOMIC64_INIT(0);
21 static int ir_raw_event_thread(void *data)
23 struct ir_raw_event ev;
24 struct ir_raw_handler *handler;
25 struct ir_raw_event_ctrl *raw = (struct ir_raw_event_ctrl *)data;
28 mutex_lock(&ir_raw_handler_lock);
29 while (kfifo_out(&raw->kfifo, &ev, 1)) {
30 list_for_each_entry(handler, &ir_raw_handler_list, list)
31 if (raw->dev->enabled_protocols &
32 handler->protocols || !handler->protocols)
33 handler->decode(raw->dev, ev);
34 ir_lirc_raw_event(raw->dev, ev);
37 mutex_unlock(&ir_raw_handler_lock);
39 set_current_state(TASK_INTERRUPTIBLE);
41 if (kthread_should_stop()) {
42 __set_current_state(TASK_RUNNING);
44 } else if (!kfifo_is_empty(&raw->kfifo))
45 set_current_state(TASK_RUNNING);
54 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
55 * @dev: the struct rc_dev device descriptor
56 * @ev: the struct ir_raw_event descriptor of the pulse/space
58 * This routine (which may be called from an interrupt context) stores a
59 * pulse/space duration for the raw ir decoding state machines. Pulses are
60 * signalled as positive values and spaces as negative values. A zero value
61 * will reset the decoding state machines.
63 int ir_raw_event_store(struct rc_dev *dev, struct ir_raw_event *ev)
68 dev_dbg(&dev->dev, "sample: (%05dus %s)\n",
69 TO_US(ev->duration), TO_STR(ev->pulse));
71 if (!kfifo_put(&dev->raw->kfifo, *ev)) {
72 dev_err(&dev->dev, "IR event FIFO is full!\n");
78 EXPORT_SYMBOL_GPL(ir_raw_event_store);
81 * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
82 * @dev: the struct rc_dev device descriptor
83 * @pulse: true for pulse, false for space
85 * This routine (which may be called from an interrupt context) is used to
86 * store the beginning of an ir pulse or space (or the start/end of ir
87 * reception) for the raw ir decoding state machines. This is used by
88 * hardware which does not provide durations directly but only interrupts
89 * (or similar events) on state change.
91 int ir_raw_event_store_edge(struct rc_dev *dev, bool pulse)
94 DEFINE_IR_RAW_EVENT(ev);
100 ev.duration = ktime_to_ns(ktime_sub(now, dev->raw->last_event));
103 return ir_raw_event_store_with_timeout(dev, &ev);
105 EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
108 * ir_raw_event_store_with_timeout() - pass a pulse/space duration to the raw
109 * ir decoders, schedule decoding and
111 * @dev: the struct rc_dev device descriptor
112 * @ev: the struct ir_raw_event descriptor of the pulse/space
114 * This routine (which may be called from an interrupt context) stores a
115 * pulse/space duration for the raw ir decoding state machines, schedules
116 * decoding and generates a timeout.
118 int ir_raw_event_store_with_timeout(struct rc_dev *dev, struct ir_raw_event *ev)
128 spin_lock(&dev->raw->edge_spinlock);
129 rc = ir_raw_event_store(dev, ev);
131 dev->raw->last_event = now;
133 /* timer could be set to timeout (125ms by default) */
134 if (!timer_pending(&dev->raw->edge_handle) ||
135 time_after(dev->raw->edge_handle.expires,
136 jiffies + msecs_to_jiffies(15))) {
137 mod_timer(&dev->raw->edge_handle,
138 jiffies + msecs_to_jiffies(15));
140 spin_unlock(&dev->raw->edge_spinlock);
144 EXPORT_SYMBOL_GPL(ir_raw_event_store_with_timeout);
147 * ir_raw_event_store_with_filter() - pass next pulse/space to decoders with some processing
148 * @dev: the struct rc_dev device descriptor
149 * @ev: the event that has occurred
151 * This routine (which may be called from an interrupt context) works
152 * in similar manner to ir_raw_event_store_edge.
153 * This routine is intended for devices with limited internal buffer
154 * It automerges samples of same type, and handles timeouts. Returns non-zero
155 * if the event was added, and zero if the event was ignored due to idle
158 int ir_raw_event_store_with_filter(struct rc_dev *dev, struct ir_raw_event *ev)
163 /* Ignore spaces in idle mode */
164 if (dev->idle && !ev->pulse)
167 ir_raw_event_set_idle(dev, false);
169 if (!dev->raw->this_ev.duration)
170 dev->raw->this_ev = *ev;
171 else if (ev->pulse == dev->raw->this_ev.pulse)
172 dev->raw->this_ev.duration += ev->duration;
174 ir_raw_event_store(dev, &dev->raw->this_ev);
175 dev->raw->this_ev = *ev;
178 /* Enter idle mode if nessesary */
179 if (!ev->pulse && dev->timeout &&
180 dev->raw->this_ev.duration >= dev->timeout)
181 ir_raw_event_set_idle(dev, true);
185 EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter);
188 * ir_raw_event_set_idle() - provide hint to rc-core when the device is idle or not
189 * @dev: the struct rc_dev device descriptor
190 * @idle: whether the device is idle or not
192 void ir_raw_event_set_idle(struct rc_dev *dev, bool idle)
197 dev_dbg(&dev->dev, "%s idle mode\n", idle ? "enter" : "leave");
200 dev->raw->this_ev.timeout = true;
201 ir_raw_event_store(dev, &dev->raw->this_ev);
202 init_ir_raw_event(&dev->raw->this_ev);
206 dev->s_idle(dev, idle);
210 EXPORT_SYMBOL_GPL(ir_raw_event_set_idle);
213 * ir_raw_event_handle() - schedules the decoding of stored ir data
214 * @dev: the struct rc_dev device descriptor
216 * This routine will tell rc-core to start decoding stored ir data.
218 void ir_raw_event_handle(struct rc_dev *dev)
220 if (!dev->raw || !dev->raw->thread)
223 wake_up_process(dev->raw->thread);
225 EXPORT_SYMBOL_GPL(ir_raw_event_handle);
227 /* used internally by the sysfs interface */
229 ir_raw_get_allowed_protocols(void)
231 return atomic64_read(&available_protocols);
234 static int change_protocol(struct rc_dev *dev, u64 *rc_proto)
236 /* the caller will update dev->enabled_protocols */
240 static void ir_raw_disable_protocols(struct rc_dev *dev, u64 protocols)
242 mutex_lock(&dev->lock);
243 dev->enabled_protocols &= ~protocols;
244 mutex_unlock(&dev->lock);
248 * ir_raw_gen_manchester() - Encode data with Manchester (bi-phase) modulation.
249 * @ev: Pointer to pointer to next free event. *@ev is incremented for
250 * each raw event filled.
251 * @max: Maximum number of raw events to fill.
252 * @timings: Manchester modulation timings.
253 * @n: Number of bits of data.
254 * @data: Data bits to encode.
256 * Encodes the @n least significant bits of @data using Manchester (bi-phase)
257 * modulation with the timing characteristics described by @timings, writing up
258 * to @max raw IR events using the *@ev pointer.
260 * Returns: 0 on success.
261 * -ENOBUFS if there isn't enough space in the array to fit the
262 * full encoded data. In this case all @max events will have been
265 int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max,
266 const struct ir_raw_timings_manchester *timings,
267 unsigned int n, u64 data)
275 if (timings->leader_pulse) {
278 init_ir_raw_event_duration((*ev), 1, timings->leader_pulse);
279 if (timings->leader_space) {
282 init_ir_raw_event_duration(++(*ev), 0,
283 timings->leader_space);
286 /* continue existing signal */
289 /* from here on *ev will point to the last event rather than the next */
292 need_pulse = !(data & i);
294 need_pulse = !need_pulse;
295 if (need_pulse == !!(*ev)->pulse) {
296 (*ev)->duration += timings->clock;
300 init_ir_raw_event_duration(++(*ev), need_pulse,
306 init_ir_raw_event_duration(++(*ev), !need_pulse,
311 if (timings->trailer_space) {
313 (*ev)->duration += timings->trailer_space;
317 init_ir_raw_event_duration(++(*ev), 0,
318 timings->trailer_space);
323 /* point to the next event rather than last event before returning */
327 EXPORT_SYMBOL(ir_raw_gen_manchester);
330 * ir_raw_gen_pd() - Encode data to raw events with pulse-distance modulation.
331 * @ev: Pointer to pointer to next free event. *@ev is incremented for
332 * each raw event filled.
333 * @max: Maximum number of raw events to fill.
334 * @timings: Pulse distance modulation timings.
335 * @n: Number of bits of data.
336 * @data: Data bits to encode.
338 * Encodes the @n least significant bits of @data using pulse-distance
339 * modulation with the timing characteristics described by @timings, writing up
340 * to @max raw IR events using the *@ev pointer.
342 * Returns: 0 on success.
343 * -ENOBUFS if there isn't enough space in the array to fit the
344 * full encoded data. In this case all @max events will have been
347 int ir_raw_gen_pd(struct ir_raw_event **ev, unsigned int max,
348 const struct ir_raw_timings_pd *timings,
349 unsigned int n, u64 data)
355 if (timings->header_pulse) {
356 ret = ir_raw_gen_pulse_space(ev, &max, timings->header_pulse,
357 timings->header_space);
362 if (timings->msb_first) {
363 for (i = n - 1; i >= 0; --i) {
364 space = timings->bit_space[(data >> i) & 1];
365 ret = ir_raw_gen_pulse_space(ev, &max,
372 for (i = 0; i < n; ++i, data >>= 1) {
373 space = timings->bit_space[data & 1];
374 ret = ir_raw_gen_pulse_space(ev, &max,
382 ret = ir_raw_gen_pulse_space(ev, &max, timings->trailer_pulse,
383 timings->trailer_space);
386 EXPORT_SYMBOL(ir_raw_gen_pd);
389 * ir_raw_gen_pl() - Encode data to raw events with pulse-length modulation.
390 * @ev: Pointer to pointer to next free event. *@ev is incremented for
391 * each raw event filled.
392 * @max: Maximum number of raw events to fill.
393 * @timings: Pulse distance modulation timings.
394 * @n: Number of bits of data.
395 * @data: Data bits to encode.
397 * Encodes the @n least significant bits of @data using space-distance
398 * modulation with the timing characteristics described by @timings, writing up
399 * to @max raw IR events using the *@ev pointer.
401 * Returns: 0 on success.
402 * -ENOBUFS if there isn't enough space in the array to fit the
403 * full encoded data. In this case all @max events will have been
406 int ir_raw_gen_pl(struct ir_raw_event **ev, unsigned int max,
407 const struct ir_raw_timings_pl *timings,
408 unsigned int n, u64 data)
417 init_ir_raw_event_duration((*ev)++, 1, timings->header_pulse);
419 if (timings->msb_first) {
420 for (i = n - 1; i >= 0; --i) {
423 init_ir_raw_event_duration((*ev)++, 0,
427 pulse = timings->bit_pulse[(data >> i) & 1];
428 init_ir_raw_event_duration((*ev)++, 1, pulse);
431 for (i = 0; i < n; ++i, data >>= 1) {
434 init_ir_raw_event_duration((*ev)++, 0,
438 pulse = timings->bit_pulse[data & 1];
439 init_ir_raw_event_duration((*ev)++, 1, pulse);
446 init_ir_raw_event_duration((*ev)++, 0, timings->trailer_space);
450 EXPORT_SYMBOL(ir_raw_gen_pl);
453 * ir_raw_encode_scancode() - Encode a scancode as raw events
455 * @protocol: protocol
456 * @scancode: scancode filter describing a single scancode
457 * @events: array of raw events to write into
458 * @max: max number of raw events
460 * Attempts to encode the scancode as raw events.
462 * Returns: The number of events written.
463 * -ENOBUFS if there isn't enough space in the array to fit the
464 * encoding. In this case all @max events will have been written.
465 * -EINVAL if the scancode is ambiguous or invalid, or if no
466 * compatible encoder was found.
468 int ir_raw_encode_scancode(enum rc_proto protocol, u32 scancode,
469 struct ir_raw_event *events, unsigned int max)
471 struct ir_raw_handler *handler;
473 u64 mask = 1ULL << protocol;
475 ir_raw_load_modules(&mask);
477 mutex_lock(&ir_raw_handler_lock);
478 list_for_each_entry(handler, &ir_raw_handler_list, list) {
479 if (handler->protocols & mask && handler->encode) {
480 ret = handler->encode(protocol, scancode, events, max);
481 if (ret >= 0 || ret == -ENOBUFS)
485 mutex_unlock(&ir_raw_handler_lock);
489 EXPORT_SYMBOL(ir_raw_encode_scancode);
492 * ir_raw_edge_handle() - Handle ir_raw_event_store_edge() processing
496 * This callback is armed by ir_raw_event_store_edge(). It does two things:
497 * first of all, rather than calling ir_raw_event_handle() for each
498 * edge and waking up the rc thread, 15 ms after the first edge
499 * ir_raw_event_handle() is called. Secondly, generate a timeout event
500 * no more IR is received after the rc_dev timeout.
502 static void ir_raw_edge_handle(struct timer_list *t)
504 struct ir_raw_event_ctrl *raw = from_timer(raw, t, edge_handle);
505 struct rc_dev *dev = raw->dev;
509 spin_lock_irqsave(&dev->raw->edge_spinlock, flags);
510 interval = ktime_sub(ktime_get(), dev->raw->last_event);
511 if (ktime_to_ns(interval) >= dev->timeout) {
512 DEFINE_IR_RAW_EVENT(ev);
515 ev.duration = ktime_to_ns(interval);
517 ir_raw_event_store(dev, &ev);
519 mod_timer(&dev->raw->edge_handle,
520 jiffies + nsecs_to_jiffies(dev->timeout -
521 ktime_to_ns(interval)));
523 spin_unlock_irqrestore(&dev->raw->edge_spinlock, flags);
525 ir_raw_event_handle(dev);
529 * ir_raw_encode_carrier() - Get carrier used for protocol
531 * @protocol: protocol
533 * Attempts to find the carrier for the specified protocol
535 * Returns: The carrier in Hz
536 * -EINVAL if the protocol is invalid, or if no
537 * compatible encoder was found.
539 int ir_raw_encode_carrier(enum rc_proto protocol)
541 struct ir_raw_handler *handler;
543 u64 mask = BIT_ULL(protocol);
545 mutex_lock(&ir_raw_handler_lock);
546 list_for_each_entry(handler, &ir_raw_handler_list, list) {
547 if (handler->protocols & mask && handler->encode) {
548 ret = handler->carrier;
552 mutex_unlock(&ir_raw_handler_lock);
556 EXPORT_SYMBOL(ir_raw_encode_carrier);
559 * Used to (un)register raw event clients
561 int ir_raw_event_prepare(struct rc_dev *dev)
566 dev->raw = kzalloc(sizeof(*dev->raw), GFP_KERNEL);
571 dev->change_protocol = change_protocol;
572 spin_lock_init(&dev->raw->edge_spinlock);
573 timer_setup(&dev->raw->edge_handle, ir_raw_edge_handle, 0);
574 INIT_KFIFO(dev->raw->kfifo);
579 int ir_raw_event_register(struct rc_dev *dev)
581 struct ir_raw_handler *handler;
582 struct task_struct *thread;
584 thread = kthread_run(ir_raw_event_thread, dev->raw, "rc%u", dev->minor);
586 return PTR_ERR(thread);
588 dev->raw->thread = thread;
590 mutex_lock(&ir_raw_handler_lock);
591 list_add_tail(&dev->raw->list, &ir_raw_client_list);
592 list_for_each_entry(handler, &ir_raw_handler_list, list)
593 if (handler->raw_register)
594 handler->raw_register(dev);
595 mutex_unlock(&ir_raw_handler_lock);
600 void ir_raw_event_free(struct rc_dev *dev)
609 void ir_raw_event_unregister(struct rc_dev *dev)
611 struct ir_raw_handler *handler;
613 if (!dev || !dev->raw)
616 kthread_stop(dev->raw->thread);
617 del_timer_sync(&dev->raw->edge_handle);
619 mutex_lock(&ir_raw_handler_lock);
620 list_del(&dev->raw->list);
621 list_for_each_entry(handler, &ir_raw_handler_list, list)
622 if (handler->raw_unregister)
623 handler->raw_unregister(dev);
627 ir_raw_event_free(dev);
630 * A user can be calling bpf(BPF_PROG_{QUERY|ATTACH|DETACH}), so
631 * ensure that the raw member is null on unlock; this is how
632 * "device gone" is checked.
634 mutex_unlock(&ir_raw_handler_lock);
638 * Extension interface - used to register the IR decoders
641 int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
643 struct ir_raw_event_ctrl *raw;
645 mutex_lock(&ir_raw_handler_lock);
646 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
647 if (ir_raw_handler->raw_register)
648 list_for_each_entry(raw, &ir_raw_client_list, list)
649 ir_raw_handler->raw_register(raw->dev);
650 atomic64_or(ir_raw_handler->protocols, &available_protocols);
651 mutex_unlock(&ir_raw_handler_lock);
655 EXPORT_SYMBOL(ir_raw_handler_register);
657 void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
659 struct ir_raw_event_ctrl *raw;
660 u64 protocols = ir_raw_handler->protocols;
662 mutex_lock(&ir_raw_handler_lock);
663 list_del(&ir_raw_handler->list);
664 list_for_each_entry(raw, &ir_raw_client_list, list) {
665 ir_raw_disable_protocols(raw->dev, protocols);
666 if (ir_raw_handler->raw_unregister)
667 ir_raw_handler->raw_unregister(raw->dev);
669 atomic64_andnot(protocols, &available_protocols);
670 mutex_unlock(&ir_raw_handler_lock);
672 EXPORT_SYMBOL(ir_raw_handler_unregister);