Lines Matching +full:timeout +full:- +full:idle +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()
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()
38 if (raw->prev_ev.reset && ev.pulse == 0) in ir_raw_event_thread()
39 dev_warn_once(&dev->dev, "timing event after reset should be pulse"); in ir_raw_event_thread()
42 if (dev->enabled_protocols & in ir_raw_event_thread()
43 handler->protocols || !handler->protocols) in ir_raw_event_thread()
44 handler->decode(dev, ev); in ir_raw_event_thread()
46 raw->prev_ev = ev; in ir_raw_event_thread()
55 } else if (!kfifo_is_empty(&raw->kfifo)) in ir_raw_event_thread()
65 * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders
76 if (!dev->raw) in ir_raw_event_store()
77 return -EINVAL; in ir_raw_event_store()
79 dev_dbg(&dev->dev, "sample: (%05dus %s)\n", in ir_raw_event_store()
80 ev->duration, TO_STR(ev->pulse)); in ir_raw_event_store()
82 if (!kfifo_put(&dev->raw->kfifo, *ev)) { in ir_raw_event_store()
83 dev_err(&dev->dev, "IR event FIFO is full!\n"); in ir_raw_event_store()
84 return -ENOSPC; in ir_raw_event_store()
92 * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space
107 if (!dev->raw) in ir_raw_event_store_edge()
108 return -EINVAL; in ir_raw_event_store_edge()
111 ev.duration = ktime_to_us(ktime_sub(now, dev->raw->last_event)); in ir_raw_event_store_edge()
119 * ir_raw_event_store_with_timeout() - pass a pulse/space duration to the raw
121 * timeout
127 * decoding and generates a timeout.
134 if (!dev->raw) in ir_raw_event_store_with_timeout()
135 return -EINVAL; in ir_raw_event_store_with_timeout()
139 spin_lock(&dev->raw->edge_spinlock); in ir_raw_event_store_with_timeout()
142 dev->raw->last_event = now; in ir_raw_event_store_with_timeout()
144 /* timer could be set to timeout (125ms by default) */ in ir_raw_event_store_with_timeout()
145 if (!timer_pending(&dev->raw->edge_handle) || in ir_raw_event_store_with_timeout()
146 time_after(dev->raw->edge_handle.expires, in ir_raw_event_store_with_timeout()
148 mod_timer(&dev->raw->edge_handle, in ir_raw_event_store_with_timeout()
151 spin_unlock(&dev->raw->edge_spinlock); in ir_raw_event_store_with_timeout()
158 * ir_raw_event_store_with_filter() - pass next pulse/space to decoders with some processing
165 * It automerges samples of same type, and handles timeouts. Returns non-zero
166 * if the event was added, and zero if the event was ignored due to idle
171 if (!dev->raw) in ir_raw_event_store_with_filter()
172 return -EINVAL; in ir_raw_event_store_with_filter()
174 /* Ignore spaces in idle mode */ in ir_raw_event_store_with_filter()
175 if (dev->idle && !ev->pulse) in ir_raw_event_store_with_filter()
177 else if (dev->idle) in ir_raw_event_store_with_filter()
180 if (!dev->raw->this_ev.duration) in ir_raw_event_store_with_filter()
181 dev->raw->this_ev = *ev; in ir_raw_event_store_with_filter()
182 else if (ev->pulse == dev->raw->this_ev.pulse) in ir_raw_event_store_with_filter()
183 dev->raw->this_ev.duration += ev->duration; in ir_raw_event_store_with_filter()
185 ir_raw_event_store(dev, &dev->raw->this_ev); in ir_raw_event_store_with_filter()
186 dev->raw->this_ev = *ev; in ir_raw_event_store_with_filter()
189 /* Enter idle mode if necessary */ in ir_raw_event_store_with_filter()
190 if (!ev->pulse && dev->timeout && in ir_raw_event_store_with_filter()
191 dev->raw->this_ev.duration >= dev->timeout) in ir_raw_event_store_with_filter()
199 * ir_raw_event_set_idle() - provide hint to rc-core when the device is idle or not
201 * @idle: whether the device is idle or not
203 void ir_raw_event_set_idle(struct rc_dev *dev, bool idle) in ir_raw_event_set_idle() argument
205 if (!dev->raw) in ir_raw_event_set_idle()
208 dev_dbg(&dev->dev, "%s idle mode\n", idle ? "enter" : "leave"); in ir_raw_event_set_idle()
210 if (idle) { in ir_raw_event_set_idle()
211 dev->raw->this_ev.timeout = true; in ir_raw_event_set_idle()
212 ir_raw_event_store(dev, &dev->raw->this_ev); in ir_raw_event_set_idle()
213 dev->raw->this_ev = (struct ir_raw_event) {}; in ir_raw_event_set_idle()
216 if (dev->s_idle) in ir_raw_event_set_idle()
217 dev->s_idle(dev, idle); in ir_raw_event_set_idle()
219 dev->idle = idle; in ir_raw_event_set_idle()
224 * ir_raw_event_handle() - schedules the decoding of stored ir data
227 * This routine will tell rc-core to start decoding stored ir data.
231 if (!dev->raw || !dev->raw->thread) in ir_raw_event_handle()
234 wake_up_process(dev->raw->thread); in ir_raw_event_handle()
248 u32 timeout = 0; in change_protocol() local
252 if (!(dev->enabled_protocols & handler->protocols) && in change_protocol()
253 (*rc_proto & handler->protocols) && handler->raw_register) in change_protocol()
254 handler->raw_register(dev); in change_protocol()
256 if ((dev->enabled_protocols & handler->protocols) && in change_protocol()
257 !(*rc_proto & handler->protocols) && in change_protocol()
258 handler->raw_unregister) in change_protocol()
259 handler->raw_unregister(dev); in change_protocol()
263 if (!dev->max_timeout) in change_protocol()
268 if (handler->protocols & *rc_proto) { in change_protocol()
269 if (timeout < handler->min_timeout) in change_protocol()
270 timeout = handler->min_timeout; in change_protocol()
275 if (timeout == 0) in change_protocol()
276 timeout = IR_DEFAULT_TIMEOUT; in change_protocol()
278 timeout += MS_TO_US(10); in change_protocol()
280 if (timeout < dev->min_timeout) in change_protocol()
281 timeout = dev->min_timeout; in change_protocol()
282 else if (timeout > dev->max_timeout) in change_protocol()
283 timeout = dev->max_timeout; in change_protocol()
285 if (dev->s_timeout) in change_protocol()
286 dev->s_timeout(dev, timeout); in change_protocol()
288 dev->timeout = timeout; in change_protocol()
295 mutex_lock(&dev->lock); in ir_raw_disable_protocols()
296 dev->enabled_protocols &= ~protocols; in ir_raw_disable_protocols()
297 mutex_unlock(&dev->lock); in ir_raw_disable_protocols()
301 * ir_raw_gen_manchester() - Encode data with Manchester (bi-phase) modulation.
309 * Encodes the @n least significant bits of @data using Manchester (bi-phase)
314 * -ENOBUFS if there isn't enough space in the array to fit the
324 int ret = -ENOBUFS; in ir_raw_gen_manchester()
326 i = BIT_ULL(n - 1); in ir_raw_gen_manchester()
328 if (timings->leader_pulse) { in ir_raw_gen_manchester()
329 if (!max--) in ir_raw_gen_manchester()
331 init_ir_raw_event_duration((*ev), 1, timings->leader_pulse); in ir_raw_gen_manchester()
332 if (timings->leader_space) { in ir_raw_gen_manchester()
333 if (!max--) in ir_raw_gen_manchester()
336 timings->leader_space); in ir_raw_gen_manchester()
340 --(*ev); in ir_raw_gen_manchester()
346 if (timings->invert) in ir_raw_gen_manchester()
348 if (need_pulse == !!(*ev)->pulse) { in ir_raw_gen_manchester()
349 (*ev)->duration += timings->clock; in ir_raw_gen_manchester()
351 if (!max--) in ir_raw_gen_manchester()
354 timings->clock); in ir_raw_gen_manchester()
357 if (!max--) in ir_raw_gen_manchester()
360 timings->clock); in ir_raw_gen_manchester()
364 if (timings->trailer_space) { in ir_raw_gen_manchester()
365 if (!(*ev)->pulse) in ir_raw_gen_manchester()
366 (*ev)->duration += timings->trailer_space; in ir_raw_gen_manchester()
367 else if (!max--) in ir_raw_gen_manchester()
371 timings->trailer_space); in ir_raw_gen_manchester()
383 * ir_raw_gen_pd() - Encode data to raw events with pulse-distance modulation.
391 * Encodes the @n least significant bits of @data using pulse-distance
396 * -ENOBUFS if there isn't enough space in the array to fit the
408 if (timings->header_pulse) { in ir_raw_gen_pd()
409 ret = ir_raw_gen_pulse_space(ev, &max, timings->header_pulse, in ir_raw_gen_pd()
410 timings->header_space); in ir_raw_gen_pd()
415 if (timings->msb_first) { in ir_raw_gen_pd()
416 for (i = n - 1; i >= 0; --i) { in ir_raw_gen_pd()
417 space = timings->bit_space[(data >> i) & 1]; in ir_raw_gen_pd()
419 timings->bit_pulse, in ir_raw_gen_pd()
426 space = timings->bit_space[data & 1]; in ir_raw_gen_pd()
428 timings->bit_pulse, in ir_raw_gen_pd()
435 ret = ir_raw_gen_pulse_space(ev, &max, timings->trailer_pulse, in ir_raw_gen_pd()
436 timings->trailer_space); in ir_raw_gen_pd()
442 * ir_raw_gen_pl() - Encode data to raw events with pulse-length modulation.
450 * Encodes the @n least significant bits of @data using space-distance
455 * -ENOBUFS if there isn't enough space in the array to fit the
464 int ret = -ENOBUFS; in ir_raw_gen_pl()
467 if (!max--) in ir_raw_gen_pl()
470 init_ir_raw_event_duration((*ev)++, 1, timings->header_pulse); in ir_raw_gen_pl()
472 if (timings->msb_first) { in ir_raw_gen_pl()
473 for (i = n - 1; i >= 0; --i) { in ir_raw_gen_pl()
474 if (!max--) in ir_raw_gen_pl()
477 timings->bit_space); in ir_raw_gen_pl()
478 if (!max--) in ir_raw_gen_pl()
480 pulse = timings->bit_pulse[(data >> i) & 1]; in ir_raw_gen_pl()
485 if (!max--) in ir_raw_gen_pl()
488 timings->bit_space); in ir_raw_gen_pl()
489 if (!max--) in ir_raw_gen_pl()
491 pulse = timings->bit_pulse[data & 1]; in ir_raw_gen_pl()
496 if (!max--) in ir_raw_gen_pl()
499 init_ir_raw_event_duration((*ev)++, 0, timings->trailer_space); in ir_raw_gen_pl()
506 * ir_raw_encode_scancode() - Encode a scancode as raw events
516 * -ENOBUFS if there isn't enough space in the array to fit the
518 * -EINVAL if the scancode is ambiguous or invalid, or if no
525 int ret = -EINVAL; in ir_raw_encode_scancode()
532 if (handler->protocols & mask && handler->encode) { in ir_raw_encode_scancode()
533 ret = handler->encode(protocol, scancode, events, max); in ir_raw_encode_scancode()
534 if (ret >= 0 || ret == -ENOBUFS) in ir_raw_encode_scancode()
545 * ir_raw_edge_handle() - Handle ir_raw_event_store_edge() processing
551 * edge and waking up the rc thread, 15 ms after the first edge
552 * ir_raw_event_handle() is called. Secondly, generate a timeout event
553 * no more IR is received after the rc_dev timeout.
558 struct rc_dev *dev = raw->dev; in ir_raw_edge_handle()
562 spin_lock_irqsave(&dev->raw->edge_spinlock, flags); in ir_raw_edge_handle()
563 interval = ktime_sub(ktime_get(), dev->raw->last_event); in ir_raw_edge_handle()
564 if (ktime_to_us(interval) >= dev->timeout) { in ir_raw_edge_handle()
566 .timeout = true, in ir_raw_edge_handle()
572 mod_timer(&dev->raw->edge_handle, in ir_raw_edge_handle()
573 jiffies + usecs_to_jiffies(dev->timeout - in ir_raw_edge_handle()
576 spin_unlock_irqrestore(&dev->raw->edge_spinlock, flags); in ir_raw_edge_handle()
582 * ir_raw_encode_carrier() - Get carrier used for protocol
589 * -EINVAL if the protocol is invalid, or if no
595 int ret = -EINVAL; in ir_raw_encode_carrier()
600 if (handler->protocols & mask && handler->encode) { in ir_raw_encode_carrier()
601 ret = handler->carrier; in ir_raw_encode_carrier()
617 return -EINVAL; in ir_raw_event_prepare()
619 dev->raw = kzalloc(sizeof(*dev->raw), GFP_KERNEL); in ir_raw_event_prepare()
620 if (!dev->raw) in ir_raw_event_prepare()
621 return -ENOMEM; in ir_raw_event_prepare()
623 dev->raw->dev = dev; in ir_raw_event_prepare()
624 dev->change_protocol = change_protocol; in ir_raw_event_prepare()
625 dev->idle = true; in ir_raw_event_prepare()
626 spin_lock_init(&dev->raw->edge_spinlock); in ir_raw_event_prepare()
627 timer_setup(&dev->raw->edge_handle, ir_raw_edge_handle, 0); in ir_raw_event_prepare()
628 INIT_KFIFO(dev->raw->kfifo); in ir_raw_event_prepare()
637 thread = kthread_run(ir_raw_event_thread, dev->raw, "rc%u", dev->minor); in ir_raw_event_register()
641 dev->raw->thread = thread; in ir_raw_event_register()
644 list_add_tail(&dev->raw->list, &ir_raw_client_list); in ir_raw_event_register()
655 kfree(dev->raw); in ir_raw_event_free()
656 dev->raw = NULL; in ir_raw_event_free()
663 if (!dev || !dev->raw) in ir_raw_event_unregister()
666 kthread_stop(dev->raw->thread); in ir_raw_event_unregister()
667 del_timer_sync(&dev->raw->edge_handle); in ir_raw_event_unregister()
670 list_del(&dev->raw->list); in ir_raw_event_unregister()
672 if (handler->raw_unregister && in ir_raw_event_unregister()
673 (handler->protocols & dev->enabled_protocols)) in ir_raw_event_unregister()
674 handler->raw_unregister(dev); in ir_raw_event_unregister()
689 * Extension interface - used to register the IR decoders
695 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list); in ir_raw_handler_register()
696 atomic64_or(ir_raw_handler->protocols, &available_protocols); in ir_raw_handler_register()
706 u64 protocols = ir_raw_handler->protocols; in ir_raw_handler_unregister()
709 list_del(&ir_raw_handler->list); in ir_raw_handler_unregister()
711 if (ir_raw_handler->raw_unregister && in ir_raw_handler_unregister()
712 (raw->dev->enabled_protocols & protocols)) in ir_raw_handler_unregister()
713 ir_raw_handler->raw_unregister(raw->dev); in ir_raw_handler_unregister()
714 ir_raw_disable_protocols(raw->dev, protocols); in ir_raw_handler_unregister()