Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / media / rc / rc-ir-raw.c
blob16e33d7eaaa2d8bb914e91d4807e57236718b1ff
1 // SPDX-License-Identifier: GPL-2.0
2 // rc-ir-raw.c - handle IR pulse/space events
3 //
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 = data;
26 struct rc_dev *dev = raw->dev;
28 while (1) {
29 mutex_lock(&ir_raw_handler_lock);
30 while (kfifo_out(&raw->kfifo, &ev, 1)) {
31 if (is_timing_event(ev)) {
32 if (ev.duration == 0)
33 dev_warn_once(&dev->dev, "nonsensical timing event of duration 0");
34 if (is_timing_event(raw->prev_ev) &&
35 !is_transition(&ev, &raw->prev_ev))
36 dev_warn_once(&dev->dev, "two consecutive events of type %s",
37 TO_STR(ev.pulse));
39 list_for_each_entry(handler, &ir_raw_handler_list, list)
40 if (dev->enabled_protocols &
41 handler->protocols || !handler->protocols)
42 handler->decode(dev, ev);
43 lirc_raw_event(dev, ev);
44 raw->prev_ev = ev;
46 mutex_unlock(&ir_raw_handler_lock);
48 set_current_state(TASK_INTERRUPTIBLE);
50 if (kthread_should_stop()) {
51 __set_current_state(TASK_RUNNING);
52 break;
53 } else if (!kfifo_is_empty(&raw->kfifo))
54 set_current_state(TASK_RUNNING);
56 schedule();
59 return 0;
62 /**
63 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
64 * @dev: the struct rc_dev device descriptor
65 * @ev: the struct ir_raw_event descriptor of the pulse/space
67 * This routine (which may be called from an interrupt context) stores a
68 * pulse/space duration for the raw ir decoding state machines. Pulses are
69 * signalled as positive values and spaces as negative values. A zero value
70 * will reset the decoding state machines.
72 int ir_raw_event_store(struct rc_dev *dev, struct ir_raw_event *ev)
74 if (!dev->raw)
75 return -EINVAL;
77 dev_dbg(&dev->dev, "sample: (%05dus %s)\n",
78 ev->duration, TO_STR(ev->pulse));
80 if (!kfifo_put(&dev->raw->kfifo, *ev)) {
81 dev_err(&dev->dev, "IR event FIFO is full!\n");
82 return -ENOSPC;
85 return 0;
87 EXPORT_SYMBOL_GPL(ir_raw_event_store);
89 /**
90 * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
91 * @dev: the struct rc_dev device descriptor
92 * @pulse: true for pulse, false for space
94 * This routine (which may be called from an interrupt context) is used to
95 * store the beginning of an ir pulse or space (or the start/end of ir
96 * reception) for the raw ir decoding state machines. This is used by
97 * hardware which does not provide durations directly but only interrupts
98 * (or similar events) on state change.
100 int ir_raw_event_store_edge(struct rc_dev *dev, bool pulse)
102 ktime_t now;
103 struct ir_raw_event ev = {};
105 if (!dev->raw)
106 return -EINVAL;
108 now = ktime_get();
109 ev.duration = ktime_to_us(ktime_sub(now, dev->raw->last_event));
110 ev.pulse = !pulse;
112 return ir_raw_event_store_with_timeout(dev, &ev);
114 EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
117 * ir_raw_event_store_with_timeout() - pass a pulse/space duration to the raw
118 * ir decoders, schedule decoding and
119 * timeout
120 * @dev: the struct rc_dev device descriptor
121 * @ev: the struct ir_raw_event descriptor of the pulse/space
123 * This routine (which may be called from an interrupt context) stores a
124 * pulse/space duration for the raw ir decoding state machines, schedules
125 * decoding and generates a timeout.
127 int ir_raw_event_store_with_timeout(struct rc_dev *dev, struct ir_raw_event *ev)
129 ktime_t now;
130 int rc = 0;
132 if (!dev->raw)
133 return -EINVAL;
135 now = ktime_get();
137 spin_lock(&dev->raw->edge_spinlock);
138 rc = ir_raw_event_store(dev, ev);
140 dev->raw->last_event = now;
142 /* timer could be set to timeout (125ms by default) */
143 if (!timer_pending(&dev->raw->edge_handle) ||
144 time_after(dev->raw->edge_handle.expires,
145 jiffies + msecs_to_jiffies(15))) {
146 mod_timer(&dev->raw->edge_handle,
147 jiffies + msecs_to_jiffies(15));
149 spin_unlock(&dev->raw->edge_spinlock);
151 return rc;
153 EXPORT_SYMBOL_GPL(ir_raw_event_store_with_timeout);
156 * ir_raw_event_store_with_filter() - pass next pulse/space to decoders with some processing
157 * @dev: the struct rc_dev device descriptor
158 * @ev: the event that has occurred
160 * This routine (which may be called from an interrupt context) works
161 * in similar manner to ir_raw_event_store_edge.
162 * This routine is intended for devices with limited internal buffer
163 * It automerges samples of same type, and handles timeouts. Returns non-zero
164 * if the event was added, and zero if the event was ignored due to idle
165 * processing.
167 int ir_raw_event_store_with_filter(struct rc_dev *dev, struct ir_raw_event *ev)
169 if (!dev->raw)
170 return -EINVAL;
172 /* Ignore spaces in idle mode */
173 if (dev->idle && !ev->pulse)
174 return 0;
175 else if (dev->idle)
176 ir_raw_event_set_idle(dev, false);
178 if (!dev->raw->this_ev.duration)
179 dev->raw->this_ev = *ev;
180 else if (ev->pulse == dev->raw->this_ev.pulse)
181 dev->raw->this_ev.duration += ev->duration;
182 else {
183 ir_raw_event_store(dev, &dev->raw->this_ev);
184 dev->raw->this_ev = *ev;
187 /* Enter idle mode if necessary */
188 if (!ev->pulse && dev->timeout &&
189 dev->raw->this_ev.duration >= dev->timeout)
190 ir_raw_event_set_idle(dev, true);
192 return 1;
194 EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter);
197 * ir_raw_event_set_idle() - provide hint to rc-core when the device is idle or not
198 * @dev: the struct rc_dev device descriptor
199 * @idle: whether the device is idle or not
201 void ir_raw_event_set_idle(struct rc_dev *dev, bool idle)
203 if (!dev->raw)
204 return;
206 dev_dbg(&dev->dev, "%s idle mode\n", idle ? "enter" : "leave");
208 if (idle) {
209 dev->raw->this_ev.timeout = true;
210 ir_raw_event_store(dev, &dev->raw->this_ev);
211 dev->raw->this_ev = (struct ir_raw_event) {};
214 if (dev->s_idle)
215 dev->s_idle(dev, idle);
217 dev->idle = idle;
219 EXPORT_SYMBOL_GPL(ir_raw_event_set_idle);
222 * ir_raw_event_handle() - schedules the decoding of stored ir data
223 * @dev: the struct rc_dev device descriptor
225 * This routine will tell rc-core to start decoding stored ir data.
227 void ir_raw_event_handle(struct rc_dev *dev)
229 if (!dev->raw || !dev->raw->thread)
230 return;
232 wake_up_process(dev->raw->thread);
234 EXPORT_SYMBOL_GPL(ir_raw_event_handle);
236 /* used internally by the sysfs interface */
238 ir_raw_get_allowed_protocols(void)
240 return atomic64_read(&available_protocols);
243 static int change_protocol(struct rc_dev *dev, u64 *rc_proto)
245 struct ir_raw_handler *handler;
246 u32 timeout = 0;
248 mutex_lock(&ir_raw_handler_lock);
249 list_for_each_entry(handler, &ir_raw_handler_list, list) {
250 if (!(dev->enabled_protocols & handler->protocols) &&
251 (*rc_proto & handler->protocols) && handler->raw_register)
252 handler->raw_register(dev);
254 if ((dev->enabled_protocols & handler->protocols) &&
255 !(*rc_proto & handler->protocols) &&
256 handler->raw_unregister)
257 handler->raw_unregister(dev);
259 mutex_unlock(&ir_raw_handler_lock);
261 if (!dev->max_timeout)
262 return 0;
264 mutex_lock(&ir_raw_handler_lock);
265 list_for_each_entry(handler, &ir_raw_handler_list, list) {
266 if (handler->protocols & *rc_proto) {
267 if (timeout < handler->min_timeout)
268 timeout = handler->min_timeout;
271 mutex_unlock(&ir_raw_handler_lock);
273 if (timeout == 0)
274 timeout = IR_DEFAULT_TIMEOUT;
275 else
276 timeout += MS_TO_US(10);
278 if (timeout < dev->min_timeout)
279 timeout = dev->min_timeout;
280 else if (timeout > dev->max_timeout)
281 timeout = dev->max_timeout;
283 if (dev->s_timeout)
284 dev->s_timeout(dev, timeout);
285 else
286 dev->timeout = timeout;
288 return 0;
291 static void ir_raw_disable_protocols(struct rc_dev *dev, u64 protocols)
293 mutex_lock(&dev->lock);
294 dev->enabled_protocols &= ~protocols;
295 mutex_unlock(&dev->lock);
299 * ir_raw_gen_manchester() - Encode data with Manchester (bi-phase) modulation.
300 * @ev: Pointer to pointer to next free event. *@ev is incremented for
301 * each raw event filled.
302 * @max: Maximum number of raw events to fill.
303 * @timings: Manchester modulation timings.
304 * @n: Number of bits of data.
305 * @data: Data bits to encode.
307 * Encodes the @n least significant bits of @data using Manchester (bi-phase)
308 * modulation with the timing characteristics described by @timings, writing up
309 * to @max raw IR events using the *@ev pointer.
311 * Returns: 0 on success.
312 * -ENOBUFS if there isn't enough space in the array to fit the
313 * full encoded data. In this case all @max events will have been
314 * written.
316 int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max,
317 const struct ir_raw_timings_manchester *timings,
318 unsigned int n, u64 data)
320 bool need_pulse;
321 u64 i;
322 int ret = -ENOBUFS;
324 i = BIT_ULL(n - 1);
326 if (timings->leader_pulse) {
327 if (!max--)
328 return ret;
329 init_ir_raw_event_duration((*ev), 1, timings->leader_pulse);
330 if (timings->leader_space) {
331 if (!max--)
332 return ret;
333 init_ir_raw_event_duration(++(*ev), 0,
334 timings->leader_space);
336 } else {
337 /* continue existing signal */
338 --(*ev);
340 /* from here on *ev will point to the last event rather than the next */
342 while (n && i > 0) {
343 need_pulse = !(data & i);
344 if (timings->invert)
345 need_pulse = !need_pulse;
346 if (need_pulse == !!(*ev)->pulse) {
347 (*ev)->duration += timings->clock;
348 } else {
349 if (!max--)
350 goto nobufs;
351 init_ir_raw_event_duration(++(*ev), need_pulse,
352 timings->clock);
355 if (!max--)
356 goto nobufs;
357 init_ir_raw_event_duration(++(*ev), !need_pulse,
358 timings->clock);
359 i >>= 1;
362 if (timings->trailer_space) {
363 if (!(*ev)->pulse)
364 (*ev)->duration += timings->trailer_space;
365 else if (!max--)
366 goto nobufs;
367 else
368 init_ir_raw_event_duration(++(*ev), 0,
369 timings->trailer_space);
372 ret = 0;
373 nobufs:
374 /* point to the next event rather than last event before returning */
375 ++(*ev);
376 return ret;
378 EXPORT_SYMBOL(ir_raw_gen_manchester);
381 * ir_raw_gen_pd() - Encode data to raw events with pulse-distance modulation.
382 * @ev: Pointer to pointer to next free event. *@ev is incremented for
383 * each raw event filled.
384 * @max: Maximum number of raw events to fill.
385 * @timings: Pulse distance modulation timings.
386 * @n: Number of bits of data.
387 * @data: Data bits to encode.
389 * Encodes the @n least significant bits of @data using pulse-distance
390 * modulation with the timing characteristics described by @timings, writing up
391 * to @max raw IR events using the *@ev pointer.
393 * Returns: 0 on success.
394 * -ENOBUFS if there isn't enough space in the array to fit the
395 * full encoded data. In this case all @max events will have been
396 * written.
398 int ir_raw_gen_pd(struct ir_raw_event **ev, unsigned int max,
399 const struct ir_raw_timings_pd *timings,
400 unsigned int n, u64 data)
402 int i;
403 int ret;
404 unsigned int space;
406 if (timings->header_pulse) {
407 ret = ir_raw_gen_pulse_space(ev, &max, timings->header_pulse,
408 timings->header_space);
409 if (ret)
410 return ret;
413 if (timings->msb_first) {
414 for (i = n - 1; i >= 0; --i) {
415 space = timings->bit_space[(data >> i) & 1];
416 ret = ir_raw_gen_pulse_space(ev, &max,
417 timings->bit_pulse,
418 space);
419 if (ret)
420 return ret;
422 } else {
423 for (i = 0; i < n; ++i, data >>= 1) {
424 space = timings->bit_space[data & 1];
425 ret = ir_raw_gen_pulse_space(ev, &max,
426 timings->bit_pulse,
427 space);
428 if (ret)
429 return ret;
433 ret = ir_raw_gen_pulse_space(ev, &max, timings->trailer_pulse,
434 timings->trailer_space);
435 return ret;
437 EXPORT_SYMBOL(ir_raw_gen_pd);
440 * ir_raw_gen_pl() - Encode data to raw events with pulse-length modulation.
441 * @ev: Pointer to pointer to next free event. *@ev is incremented for
442 * each raw event filled.
443 * @max: Maximum number of raw events to fill.
444 * @timings: Pulse distance modulation timings.
445 * @n: Number of bits of data.
446 * @data: Data bits to encode.
448 * Encodes the @n least significant bits of @data using space-distance
449 * modulation with the timing characteristics described by @timings, writing up
450 * to @max raw IR events using the *@ev pointer.
452 * Returns: 0 on success.
453 * -ENOBUFS if there isn't enough space in the array to fit the
454 * full encoded data. In this case all @max events will have been
455 * written.
457 int ir_raw_gen_pl(struct ir_raw_event **ev, unsigned int max,
458 const struct ir_raw_timings_pl *timings,
459 unsigned int n, u64 data)
461 int i;
462 int ret = -ENOBUFS;
463 unsigned int pulse;
465 if (!max--)
466 return ret;
468 init_ir_raw_event_duration((*ev)++, 1, timings->header_pulse);
470 if (timings->msb_first) {
471 for (i = n - 1; i >= 0; --i) {
472 if (!max--)
473 return ret;
474 init_ir_raw_event_duration((*ev)++, 0,
475 timings->bit_space);
476 if (!max--)
477 return ret;
478 pulse = timings->bit_pulse[(data >> i) & 1];
479 init_ir_raw_event_duration((*ev)++, 1, pulse);
481 } else {
482 for (i = 0; i < n; ++i, data >>= 1) {
483 if (!max--)
484 return ret;
485 init_ir_raw_event_duration((*ev)++, 0,
486 timings->bit_space);
487 if (!max--)
488 return ret;
489 pulse = timings->bit_pulse[data & 1];
490 init_ir_raw_event_duration((*ev)++, 1, pulse);
494 if (!max--)
495 return ret;
497 init_ir_raw_event_duration((*ev)++, 0, timings->trailer_space);
499 return 0;
501 EXPORT_SYMBOL(ir_raw_gen_pl);
504 * ir_raw_encode_scancode() - Encode a scancode as raw events
506 * @protocol: protocol
507 * @scancode: scancode filter describing a single scancode
508 * @events: array of raw events to write into
509 * @max: max number of raw events
511 * Attempts to encode the scancode as raw events.
513 * Returns: The number of events written.
514 * -ENOBUFS if there isn't enough space in the array to fit the
515 * encoding. In this case all @max events will have been written.
516 * -EINVAL if the scancode is ambiguous or invalid, or if no
517 * compatible encoder was found.
519 int ir_raw_encode_scancode(enum rc_proto protocol, u32 scancode,
520 struct ir_raw_event *events, unsigned int max)
522 struct ir_raw_handler *handler;
523 int ret = -EINVAL;
524 u64 mask = 1ULL << protocol;
526 ir_raw_load_modules(&mask);
528 mutex_lock(&ir_raw_handler_lock);
529 list_for_each_entry(handler, &ir_raw_handler_list, list) {
530 if (handler->protocols & mask && handler->encode) {
531 ret = handler->encode(protocol, scancode, events, max);
532 if (ret >= 0 || ret == -ENOBUFS)
533 break;
536 mutex_unlock(&ir_raw_handler_lock);
538 return ret;
540 EXPORT_SYMBOL(ir_raw_encode_scancode);
543 * ir_raw_edge_handle() - Handle ir_raw_event_store_edge() processing
545 * @t: timer_list
547 * This callback is armed by ir_raw_event_store_edge(). It does two things:
548 * first of all, rather than calling ir_raw_event_handle() for each
549 * edge and waking up the rc thread, 15 ms after the first edge
550 * ir_raw_event_handle() is called. Secondly, generate a timeout event
551 * no more IR is received after the rc_dev timeout.
553 static void ir_raw_edge_handle(struct timer_list *t)
555 struct ir_raw_event_ctrl *raw = from_timer(raw, t, edge_handle);
556 struct rc_dev *dev = raw->dev;
557 unsigned long flags;
558 ktime_t interval;
560 spin_lock_irqsave(&dev->raw->edge_spinlock, flags);
561 interval = ktime_sub(ktime_get(), dev->raw->last_event);
562 if (ktime_to_us(interval) >= dev->timeout) {
563 struct ir_raw_event ev = {
564 .timeout = true,
565 .duration = ktime_to_us(interval)
568 ir_raw_event_store(dev, &ev);
569 } else {
570 mod_timer(&dev->raw->edge_handle,
571 jiffies + usecs_to_jiffies(dev->timeout -
572 ktime_to_us(interval)));
574 spin_unlock_irqrestore(&dev->raw->edge_spinlock, flags);
576 ir_raw_event_handle(dev);
580 * ir_raw_encode_carrier() - Get carrier used for protocol
582 * @protocol: protocol
584 * Attempts to find the carrier for the specified protocol
586 * Returns: The carrier in Hz
587 * -EINVAL if the protocol is invalid, or if no
588 * compatible encoder was found.
590 int ir_raw_encode_carrier(enum rc_proto protocol)
592 struct ir_raw_handler *handler;
593 int ret = -EINVAL;
594 u64 mask = BIT_ULL(protocol);
596 mutex_lock(&ir_raw_handler_lock);
597 list_for_each_entry(handler, &ir_raw_handler_list, list) {
598 if (handler->protocols & mask && handler->encode) {
599 ret = handler->carrier;
600 break;
603 mutex_unlock(&ir_raw_handler_lock);
605 return ret;
607 EXPORT_SYMBOL(ir_raw_encode_carrier);
610 * Used to (un)register raw event clients
612 int ir_raw_event_prepare(struct rc_dev *dev)
614 if (!dev)
615 return -EINVAL;
617 dev->raw = kzalloc(sizeof(*dev->raw), GFP_KERNEL);
618 if (!dev->raw)
619 return -ENOMEM;
621 dev->raw->dev = dev;
622 dev->change_protocol = change_protocol;
623 dev->idle = true;
624 spin_lock_init(&dev->raw->edge_spinlock);
625 timer_setup(&dev->raw->edge_handle, ir_raw_edge_handle, 0);
626 INIT_KFIFO(dev->raw->kfifo);
628 return 0;
631 int ir_raw_event_register(struct rc_dev *dev)
633 struct task_struct *thread;
635 thread = kthread_run(ir_raw_event_thread, dev->raw, "rc%u", dev->minor);
636 if (IS_ERR(thread))
637 return PTR_ERR(thread);
639 dev->raw->thread = thread;
641 mutex_lock(&ir_raw_handler_lock);
642 list_add_tail(&dev->raw->list, &ir_raw_client_list);
643 mutex_unlock(&ir_raw_handler_lock);
645 return 0;
648 void ir_raw_event_free(struct rc_dev *dev)
650 if (!dev)
651 return;
653 kfree(dev->raw);
654 dev->raw = NULL;
657 void ir_raw_event_unregister(struct rc_dev *dev)
659 struct ir_raw_handler *handler;
661 if (!dev || !dev->raw)
662 return;
664 kthread_stop(dev->raw->thread);
665 del_timer_sync(&dev->raw->edge_handle);
667 mutex_lock(&ir_raw_handler_lock);
668 list_del(&dev->raw->list);
669 list_for_each_entry(handler, &ir_raw_handler_list, list)
670 if (handler->raw_unregister &&
671 (handler->protocols & dev->enabled_protocols))
672 handler->raw_unregister(dev);
674 lirc_bpf_free(dev);
676 ir_raw_event_free(dev);
679 * A user can be calling bpf(BPF_PROG_{QUERY|ATTACH|DETACH}), so
680 * ensure that the raw member is null on unlock; this is how
681 * "device gone" is checked.
683 mutex_unlock(&ir_raw_handler_lock);
687 * Extension interface - used to register the IR decoders
690 int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
692 mutex_lock(&ir_raw_handler_lock);
693 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
694 atomic64_or(ir_raw_handler->protocols, &available_protocols);
695 mutex_unlock(&ir_raw_handler_lock);
697 return 0;
699 EXPORT_SYMBOL(ir_raw_handler_register);
701 void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
703 struct ir_raw_event_ctrl *raw;
704 u64 protocols = ir_raw_handler->protocols;
706 mutex_lock(&ir_raw_handler_lock);
707 list_del(&ir_raw_handler->list);
708 list_for_each_entry(raw, &ir_raw_client_list, list) {
709 if (ir_raw_handler->raw_unregister &&
710 (raw->dev->enabled_protocols & protocols))
711 ir_raw_handler->raw_unregister(raw->dev);
712 ir_raw_disable_protocols(raw->dev, protocols);
714 atomic64_andnot(protocols, &available_protocols);
715 mutex_unlock(&ir_raw_handler_lock);
717 EXPORT_SYMBOL(ir_raw_handler_unregister);