Lines Matching +full:reset +full:- +full:duration +full:- +full:ms
1 // SPDX-License-Identifier: GPL-2.0
2 // rc-ir-raw.c - handle IR pulse/space events
11 #include "rc-core-priv.h"
26 struct rc_dev *dev = raw->dev; in ir_raw_event_thread()
30 while (kfifo_out(&raw->kfifo, &ev, 1)) { in ir_raw_event_thread()
32 if (ev.duration == 0) in ir_raw_event_thread()
33 dev_warn_once(&dev->dev, "nonsensical timing event of duration 0"); in ir_raw_event_thread()
34 if (is_timing_event(raw->prev_ev) && in ir_raw_event_thread()
35 !is_transition(&ev, &raw->prev_ev)) in ir_raw_event_thread()
36 dev_warn_once(&dev->dev, "two consecutive events of type %s", in ir_raw_event_thread()
40 if (dev->enabled_protocols & in ir_raw_event_thread()
41 handler->protocols || !handler->protocols) in ir_raw_event_thread()
42 handler->decode(dev, ev); in ir_raw_event_thread()
44 raw->prev_ev = ev; in ir_raw_event_thread()
53 } else if (!kfifo_is_empty(&raw->kfifo)) in ir_raw_event_thread()
63 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
68 * pulse/space duration for the raw ir decoding state machines. Pulses are
70 * will reset the decoding state machines.
74 if (!dev->raw) in ir_raw_event_store()
75 return -EINVAL; in ir_raw_event_store()
77 dev_dbg(&dev->dev, "sample: (%05dus %s)\n", in ir_raw_event_store()
78 ev->duration, TO_STR(ev->pulse)); in ir_raw_event_store()
80 if (!kfifo_put(&dev->raw->kfifo, *ev)) { in ir_raw_event_store()
81 dev_err(&dev->dev, "IR event FIFO is full!\n"); in ir_raw_event_store()
82 return -ENOSPC; in ir_raw_event_store()
90 * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
105 if (!dev->raw) in ir_raw_event_store_edge()
106 return -EINVAL; in ir_raw_event_store_edge()
109 ev.duration = ktime_to_us(ktime_sub(now, dev->raw->last_event)); in ir_raw_event_store_edge()
117 * ir_raw_event_store_with_timeout() - pass a pulse/space duration to the raw
124 * pulse/space duration for the raw ir decoding state machines, schedules
132 if (!dev->raw) in ir_raw_event_store_with_timeout()
133 return -EINVAL; in ir_raw_event_store_with_timeout()
137 spin_lock(&dev->raw->edge_spinlock); in ir_raw_event_store_with_timeout()
140 dev->raw->last_event = now; in ir_raw_event_store_with_timeout()
142 /* timer could be set to timeout (125ms by default) */ in ir_raw_event_store_with_timeout()
143 if (!timer_pending(&dev->raw->edge_handle) || in ir_raw_event_store_with_timeout()
144 time_after(dev->raw->edge_handle.expires, in ir_raw_event_store_with_timeout()
146 mod_timer(&dev->raw->edge_handle, in ir_raw_event_store_with_timeout()
149 spin_unlock(&dev->raw->edge_spinlock); in ir_raw_event_store_with_timeout()
156 * ir_raw_event_store_with_filter() - pass next pulse/space to decoders with some processing
163 * It automerges samples of same type, and handles timeouts. Returns non-zero
169 if (!dev->raw) in ir_raw_event_store_with_filter()
170 return -EINVAL; in ir_raw_event_store_with_filter()
173 if (dev->idle && !ev->pulse) in ir_raw_event_store_with_filter()
175 else if (dev->idle) in ir_raw_event_store_with_filter()
178 if (!dev->raw->this_ev.duration) in ir_raw_event_store_with_filter()
179 dev->raw->this_ev = *ev; in ir_raw_event_store_with_filter()
180 else if (ev->pulse == dev->raw->this_ev.pulse) in ir_raw_event_store_with_filter()
181 dev->raw->this_ev.duration += ev->duration; in ir_raw_event_store_with_filter()
183 ir_raw_event_store(dev, &dev->raw->this_ev); in ir_raw_event_store_with_filter()
184 dev->raw->this_ev = *ev; in ir_raw_event_store_with_filter()
188 if (!ev->pulse && dev->timeout && in ir_raw_event_store_with_filter()
189 dev->raw->this_ev.duration >= dev->timeout) in ir_raw_event_store_with_filter()
197 * ir_raw_event_set_idle() - provide hint to rc-core when the device is idle or not
203 if (!dev->raw) in ir_raw_event_set_idle()
206 dev_dbg(&dev->dev, "%s idle mode\n", idle ? "enter" : "leave"); in ir_raw_event_set_idle()
209 dev->raw->this_ev.timeout = true; in ir_raw_event_set_idle()
210 ir_raw_event_store(dev, &dev->raw->this_ev); in ir_raw_event_set_idle()
211 dev->raw->this_ev = (struct ir_raw_event) {}; in ir_raw_event_set_idle()
214 if (dev->s_idle) in ir_raw_event_set_idle()
215 dev->s_idle(dev, idle); in ir_raw_event_set_idle()
217 dev->idle = idle; in ir_raw_event_set_idle()
222 * ir_raw_event_handle() - schedules the decoding of stored ir data
225 * This routine will tell rc-core to start decoding stored ir data.
229 if (!dev->raw || !dev->raw->thread) in ir_raw_event_handle()
232 wake_up_process(dev->raw->thread); in ir_raw_event_handle()
250 if (!(dev->enabled_protocols & handler->protocols) && in change_protocol()
251 (*rc_proto & handler->protocols) && handler->raw_register) in change_protocol()
252 handler->raw_register(dev); in change_protocol()
254 if ((dev->enabled_protocols & handler->protocols) && in change_protocol()
255 !(*rc_proto & handler->protocols) && in change_protocol()
256 handler->raw_unregister) in change_protocol()
257 handler->raw_unregister(dev); in change_protocol()
261 if (!dev->max_timeout) in change_protocol()
266 if (handler->protocols & *rc_proto) { in change_protocol()
267 if (timeout < handler->min_timeout) in change_protocol()
268 timeout = handler->min_timeout; in change_protocol()
278 if (timeout < dev->min_timeout) in change_protocol()
279 timeout = dev->min_timeout; in change_protocol()
280 else if (timeout > dev->max_timeout) in change_protocol()
281 timeout = dev->max_timeout; in change_protocol()
283 if (dev->s_timeout) in change_protocol()
284 dev->s_timeout(dev, timeout); in change_protocol()
286 dev->timeout = timeout; in change_protocol()
293 mutex_lock(&dev->lock); in ir_raw_disable_protocols()
294 dev->enabled_protocols &= ~protocols; in ir_raw_disable_protocols()
295 mutex_unlock(&dev->lock); in ir_raw_disable_protocols()
299 * ir_raw_gen_manchester() - Encode data with Manchester (bi-phase) modulation.
307 * Encodes the @n least significant bits of @data using Manchester (bi-phase)
312 * -ENOBUFS if there isn't enough space in the array to fit the
322 int ret = -ENOBUFS; in ir_raw_gen_manchester()
324 i = BIT_ULL(n - 1); in ir_raw_gen_manchester()
326 if (timings->leader_pulse) { in ir_raw_gen_manchester()
327 if (!max--) in ir_raw_gen_manchester()
329 init_ir_raw_event_duration((*ev), 1, timings->leader_pulse); in ir_raw_gen_manchester()
330 if (timings->leader_space) { in ir_raw_gen_manchester()
331 if (!max--) in ir_raw_gen_manchester()
334 timings->leader_space); in ir_raw_gen_manchester()
338 --(*ev); in ir_raw_gen_manchester()
344 if (timings->invert) in ir_raw_gen_manchester()
346 if (need_pulse == !!(*ev)->pulse) { in ir_raw_gen_manchester()
347 (*ev)->duration += timings->clock; in ir_raw_gen_manchester()
349 if (!max--) in ir_raw_gen_manchester()
352 timings->clock); in ir_raw_gen_manchester()
355 if (!max--) in ir_raw_gen_manchester()
358 timings->clock); in ir_raw_gen_manchester()
362 if (timings->trailer_space) { in ir_raw_gen_manchester()
363 if (!(*ev)->pulse) in ir_raw_gen_manchester()
364 (*ev)->duration += timings->trailer_space; in ir_raw_gen_manchester()
365 else if (!max--) in ir_raw_gen_manchester()
369 timings->trailer_space); in ir_raw_gen_manchester()
381 * ir_raw_gen_pd() - Encode data to raw events with pulse-distance modulation.
389 * Encodes the @n least significant bits of @data using pulse-distance
394 * -ENOBUFS if there isn't enough space in the array to fit the
406 if (timings->header_pulse) { in ir_raw_gen_pd()
407 ret = ir_raw_gen_pulse_space(ev, &max, timings->header_pulse, in ir_raw_gen_pd()
408 timings->header_space); in ir_raw_gen_pd()
413 if (timings->msb_first) { in ir_raw_gen_pd()
414 for (i = n - 1; i >= 0; --i) { in ir_raw_gen_pd()
415 space = timings->bit_space[(data >> i) & 1]; in ir_raw_gen_pd()
417 timings->bit_pulse, in ir_raw_gen_pd()
424 space = timings->bit_space[data & 1]; in ir_raw_gen_pd()
426 timings->bit_pulse, in ir_raw_gen_pd()
433 ret = ir_raw_gen_pulse_space(ev, &max, timings->trailer_pulse, in ir_raw_gen_pd()
434 timings->trailer_space); in ir_raw_gen_pd()
440 * ir_raw_gen_pl() - Encode data to raw events with pulse-length modulation.
448 * Encodes the @n least significant bits of @data using space-distance
453 * -ENOBUFS if there isn't enough space in the array to fit the
462 int ret = -ENOBUFS; in ir_raw_gen_pl()
465 if (!max--) in ir_raw_gen_pl()
468 init_ir_raw_event_duration((*ev)++, 1, timings->header_pulse); in ir_raw_gen_pl()
470 if (timings->msb_first) { in ir_raw_gen_pl()
471 for (i = n - 1; i >= 0; --i) { in ir_raw_gen_pl()
472 if (!max--) in ir_raw_gen_pl()
475 timings->bit_space); in ir_raw_gen_pl()
476 if (!max--) in ir_raw_gen_pl()
478 pulse = timings->bit_pulse[(data >> i) & 1]; in ir_raw_gen_pl()
483 if (!max--) in ir_raw_gen_pl()
486 timings->bit_space); in ir_raw_gen_pl()
487 if (!max--) in ir_raw_gen_pl()
489 pulse = timings->bit_pulse[data & 1]; in ir_raw_gen_pl()
494 if (!max--) in ir_raw_gen_pl()
497 init_ir_raw_event_duration((*ev)++, 0, timings->trailer_space); in ir_raw_gen_pl()
504 * ir_raw_encode_scancode() - Encode a scancode as raw events
514 * -ENOBUFS if there isn't enough space in the array to fit the
516 * -EINVAL if the scancode is ambiguous or invalid, or if no
523 int ret = -EINVAL; in ir_raw_encode_scancode()
530 if (handler->protocols & mask && handler->encode) { in ir_raw_encode_scancode()
531 ret = handler->encode(protocol, scancode, events, max); in ir_raw_encode_scancode()
532 if (ret >= 0 || ret == -ENOBUFS) in ir_raw_encode_scancode()
543 * ir_raw_edge_handle() - Handle ir_raw_event_store_edge() processing
549 * edge and waking up the rc thread, 15 ms after the first edge
556 struct rc_dev *dev = raw->dev; in ir_raw_edge_handle()
560 spin_lock_irqsave(&dev->raw->edge_spinlock, flags); in ir_raw_edge_handle()
561 interval = ktime_sub(ktime_get(), dev->raw->last_event); in ir_raw_edge_handle()
562 if (ktime_to_us(interval) >= dev->timeout) { in ir_raw_edge_handle()
565 .duration = ktime_to_us(interval) in ir_raw_edge_handle()
570 mod_timer(&dev->raw->edge_handle, in ir_raw_edge_handle()
571 jiffies + usecs_to_jiffies(dev->timeout - in ir_raw_edge_handle()
574 spin_unlock_irqrestore(&dev->raw->edge_spinlock, flags); in ir_raw_edge_handle()
580 * ir_raw_encode_carrier() - Get carrier used for protocol
587 * -EINVAL if the protocol is invalid, or if no
593 int ret = -EINVAL; in ir_raw_encode_carrier()
598 if (handler->protocols & mask && handler->encode) { in ir_raw_encode_carrier()
599 ret = handler->carrier; in ir_raw_encode_carrier()
615 return -EINVAL; in ir_raw_event_prepare()
617 dev->raw = kzalloc(sizeof(*dev->raw), GFP_KERNEL); in ir_raw_event_prepare()
618 if (!dev->raw) in ir_raw_event_prepare()
619 return -ENOMEM; in ir_raw_event_prepare()
621 dev->raw->dev = dev; in ir_raw_event_prepare()
622 dev->change_protocol = change_protocol; in ir_raw_event_prepare()
623 dev->idle = true; in ir_raw_event_prepare()
624 spin_lock_init(&dev->raw->edge_spinlock); in ir_raw_event_prepare()
625 timer_setup(&dev->raw->edge_handle, ir_raw_edge_handle, 0); in ir_raw_event_prepare()
626 INIT_KFIFO(dev->raw->kfifo); in ir_raw_event_prepare()
635 thread = kthread_run(ir_raw_event_thread, dev->raw, "rc%u", dev->minor); in ir_raw_event_register()
639 dev->raw->thread = thread; in ir_raw_event_register()
642 list_add_tail(&dev->raw->list, &ir_raw_client_list); in ir_raw_event_register()
653 kfree(dev->raw); in ir_raw_event_free()
654 dev->raw = NULL; in ir_raw_event_free()
661 if (!dev || !dev->raw) in ir_raw_event_unregister()
664 kthread_stop(dev->raw->thread); in ir_raw_event_unregister()
665 del_timer_sync(&dev->raw->edge_handle); in ir_raw_event_unregister()
668 list_del(&dev->raw->list); in ir_raw_event_unregister()
670 if (handler->raw_unregister && in ir_raw_event_unregister()
671 (handler->protocols & dev->enabled_protocols)) in ir_raw_event_unregister()
672 handler->raw_unregister(dev); in ir_raw_event_unregister()
687 * Extension interface - used to register the IR decoders
693 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list); in ir_raw_handler_register()
694 atomic64_or(ir_raw_handler->protocols, &available_protocols); in ir_raw_handler_register()
704 u64 protocols = ir_raw_handler->protocols; in ir_raw_handler_unregister()
707 list_del(&ir_raw_handler->list); in ir_raw_handler_unregister()
709 if (ir_raw_handler->raw_unregister && in ir_raw_handler_unregister()
710 (raw->dev->enabled_protocols & protocols)) in ir_raw_handler_unregister()
711 ir_raw_handler->raw_unregister(raw->dev); in ir_raw_handler_unregister()
712 ir_raw_disable_protocols(raw->dev, protocols); in ir_raw_handler_unregister()