Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / rc / rc-ir-raw.c
blob18504870b9f05b9bbb086ac776a519c560139387
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 static 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;
27 while (1) {
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);
35 raw->prev_ev = 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);
43 break;
44 } else if (!kfifo_is_empty(&raw->kfifo))
45 set_current_state(TASK_RUNNING);
47 schedule();
50 return 0;
53 /**
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)
65 if (!dev->raw)
66 return -EINVAL;
68 IR_dprintk(2, "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");
73 return -ENOSPC;
76 return 0;
78 EXPORT_SYMBOL_GPL(ir_raw_event_store);
80 /**
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)
93 ktime_t now;
94 DEFINE_IR_RAW_EVENT(ev);
95 int rc = 0;
97 if (!dev->raw)
98 return -EINVAL;
100 now = ktime_get();
101 ev.duration = ktime_to_ns(ktime_sub(now, dev->raw->last_event));
102 ev.pulse = !pulse;
104 rc = ir_raw_event_store(dev, &ev);
106 dev->raw->last_event = now;
108 /* timer could be set to timeout (125ms by default) */
109 if (!timer_pending(&dev->raw->edge_handle) ||
110 time_after(dev->raw->edge_handle.expires,
111 jiffies + msecs_to_jiffies(15))) {
112 mod_timer(&dev->raw->edge_handle,
113 jiffies + msecs_to_jiffies(15));
116 return rc;
118 EXPORT_SYMBOL_GPL(ir_raw_event_store_edge);
121 * ir_raw_event_store_with_filter() - pass next pulse/space to decoders with some processing
122 * @dev: the struct rc_dev device descriptor
123 * @ev: the event that has occurred
125 * This routine (which may be called from an interrupt context) works
126 * in similar manner to ir_raw_event_store_edge.
127 * This routine is intended for devices with limited internal buffer
128 * It automerges samples of same type, and handles timeouts. Returns non-zero
129 * if the event was added, and zero if the event was ignored due to idle
130 * processing.
132 int ir_raw_event_store_with_filter(struct rc_dev *dev, struct ir_raw_event *ev)
134 if (!dev->raw)
135 return -EINVAL;
137 /* Ignore spaces in idle mode */
138 if (dev->idle && !ev->pulse)
139 return 0;
140 else if (dev->idle)
141 ir_raw_event_set_idle(dev, false);
143 if (!dev->raw->this_ev.duration)
144 dev->raw->this_ev = *ev;
145 else if (ev->pulse == dev->raw->this_ev.pulse)
146 dev->raw->this_ev.duration += ev->duration;
147 else {
148 ir_raw_event_store(dev, &dev->raw->this_ev);
149 dev->raw->this_ev = *ev;
152 /* Enter idle mode if nessesary */
153 if (!ev->pulse && dev->timeout &&
154 dev->raw->this_ev.duration >= dev->timeout)
155 ir_raw_event_set_idle(dev, true);
157 return 1;
159 EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter);
162 * ir_raw_event_set_idle() - provide hint to rc-core when the device is idle or not
163 * @dev: the struct rc_dev device descriptor
164 * @idle: whether the device is idle or not
166 void ir_raw_event_set_idle(struct rc_dev *dev, bool idle)
168 if (!dev->raw)
169 return;
171 IR_dprintk(2, "%s idle mode\n", idle ? "enter" : "leave");
173 if (idle) {
174 dev->raw->this_ev.timeout = true;
175 ir_raw_event_store(dev, &dev->raw->this_ev);
176 init_ir_raw_event(&dev->raw->this_ev);
179 if (dev->s_idle)
180 dev->s_idle(dev, idle);
182 dev->idle = idle;
184 EXPORT_SYMBOL_GPL(ir_raw_event_set_idle);
187 * ir_raw_event_handle() - schedules the decoding of stored ir data
188 * @dev: the struct rc_dev device descriptor
190 * This routine will tell rc-core to start decoding stored ir data.
192 void ir_raw_event_handle(struct rc_dev *dev)
194 if (!dev->raw || !dev->raw->thread)
195 return;
197 wake_up_process(dev->raw->thread);
199 EXPORT_SYMBOL_GPL(ir_raw_event_handle);
201 /* used internally by the sysfs interface */
203 ir_raw_get_allowed_protocols(void)
205 return atomic64_read(&available_protocols);
208 static int change_protocol(struct rc_dev *dev, u64 *rc_proto)
210 /* the caller will update dev->enabled_protocols */
211 return 0;
214 static void ir_raw_disable_protocols(struct rc_dev *dev, u64 protocols)
216 mutex_lock(&dev->lock);
217 dev->enabled_protocols &= ~protocols;
218 mutex_unlock(&dev->lock);
222 * ir_raw_gen_manchester() - Encode data with Manchester (bi-phase) modulation.
223 * @ev: Pointer to pointer to next free event. *@ev is incremented for
224 * each raw event filled.
225 * @max: Maximum number of raw events to fill.
226 * @timings: Manchester modulation timings.
227 * @n: Number of bits of data.
228 * @data: Data bits to encode.
230 * Encodes the @n least significant bits of @data using Manchester (bi-phase)
231 * modulation with the timing characteristics described by @timings, writing up
232 * to @max raw IR events using the *@ev pointer.
234 * Returns: 0 on success.
235 * -ENOBUFS if there isn't enough space in the array to fit the
236 * full encoded data. In this case all @max events will have been
237 * written.
239 int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max,
240 const struct ir_raw_timings_manchester *timings,
241 unsigned int n, u64 data)
243 bool need_pulse;
244 u64 i;
245 int ret = -ENOBUFS;
247 i = BIT_ULL(n - 1);
249 if (timings->leader_pulse) {
250 if (!max--)
251 return ret;
252 init_ir_raw_event_duration((*ev), 1, timings->leader_pulse);
253 if (timings->leader_space) {
254 if (!max--)
255 return ret;
256 init_ir_raw_event_duration(++(*ev), 0,
257 timings->leader_space);
259 } else {
260 /* continue existing signal */
261 --(*ev);
263 /* from here on *ev will point to the last event rather than the next */
265 while (n && i > 0) {
266 need_pulse = !(data & i);
267 if (timings->invert)
268 need_pulse = !need_pulse;
269 if (need_pulse == !!(*ev)->pulse) {
270 (*ev)->duration += timings->clock;
271 } else {
272 if (!max--)
273 goto nobufs;
274 init_ir_raw_event_duration(++(*ev), need_pulse,
275 timings->clock);
278 if (!max--)
279 goto nobufs;
280 init_ir_raw_event_duration(++(*ev), !need_pulse,
281 timings->clock);
282 i >>= 1;
285 if (timings->trailer_space) {
286 if (!(*ev)->pulse)
287 (*ev)->duration += timings->trailer_space;
288 else if (!max--)
289 goto nobufs;
290 else
291 init_ir_raw_event_duration(++(*ev), 0,
292 timings->trailer_space);
295 ret = 0;
296 nobufs:
297 /* point to the next event rather than last event before returning */
298 ++(*ev);
299 return ret;
301 EXPORT_SYMBOL(ir_raw_gen_manchester);
304 * ir_raw_gen_pd() - Encode data to raw events with pulse-distance modulation.
305 * @ev: Pointer to pointer to next free event. *@ev is incremented for
306 * each raw event filled.
307 * @max: Maximum number of raw events to fill.
308 * @timings: Pulse distance modulation timings.
309 * @n: Number of bits of data.
310 * @data: Data bits to encode.
312 * Encodes the @n least significant bits of @data using pulse-distance
313 * modulation with the timing characteristics described by @timings, writing up
314 * to @max raw IR events using the *@ev pointer.
316 * Returns: 0 on success.
317 * -ENOBUFS if there isn't enough space in the array to fit the
318 * full encoded data. In this case all @max events will have been
319 * written.
321 int ir_raw_gen_pd(struct ir_raw_event **ev, unsigned int max,
322 const struct ir_raw_timings_pd *timings,
323 unsigned int n, u64 data)
325 int i;
326 int ret;
327 unsigned int space;
329 if (timings->header_pulse) {
330 ret = ir_raw_gen_pulse_space(ev, &max, timings->header_pulse,
331 timings->header_space);
332 if (ret)
333 return ret;
336 if (timings->msb_first) {
337 for (i = n - 1; i >= 0; --i) {
338 space = timings->bit_space[(data >> i) & 1];
339 ret = ir_raw_gen_pulse_space(ev, &max,
340 timings->bit_pulse,
341 space);
342 if (ret)
343 return ret;
345 } else {
346 for (i = 0; i < n; ++i, data >>= 1) {
347 space = timings->bit_space[data & 1];
348 ret = ir_raw_gen_pulse_space(ev, &max,
349 timings->bit_pulse,
350 space);
351 if (ret)
352 return ret;
356 ret = ir_raw_gen_pulse_space(ev, &max, timings->trailer_pulse,
357 timings->trailer_space);
358 return ret;
360 EXPORT_SYMBOL(ir_raw_gen_pd);
363 * ir_raw_gen_pl() - Encode data to raw events with pulse-length modulation.
364 * @ev: Pointer to pointer to next free event. *@ev is incremented for
365 * each raw event filled.
366 * @max: Maximum number of raw events to fill.
367 * @timings: Pulse distance modulation timings.
368 * @n: Number of bits of data.
369 * @data: Data bits to encode.
371 * Encodes the @n least significant bits of @data using space-distance
372 * modulation with the timing characteristics described by @timings, writing up
373 * to @max raw IR events using the *@ev pointer.
375 * Returns: 0 on success.
376 * -ENOBUFS if there isn't enough space in the array to fit the
377 * full encoded data. In this case all @max events will have been
378 * written.
380 int ir_raw_gen_pl(struct ir_raw_event **ev, unsigned int max,
381 const struct ir_raw_timings_pl *timings,
382 unsigned int n, u64 data)
384 int i;
385 int ret = -ENOBUFS;
386 unsigned int pulse;
388 if (!max--)
389 return ret;
391 init_ir_raw_event_duration((*ev)++, 1, timings->header_pulse);
393 if (timings->msb_first) {
394 for (i = n - 1; i >= 0; --i) {
395 if (!max--)
396 return ret;
397 init_ir_raw_event_duration((*ev)++, 0,
398 timings->bit_space);
399 if (!max--)
400 return ret;
401 pulse = timings->bit_pulse[(data >> i) & 1];
402 init_ir_raw_event_duration((*ev)++, 1, pulse);
404 } else {
405 for (i = 0; i < n; ++i, data >>= 1) {
406 if (!max--)
407 return ret;
408 init_ir_raw_event_duration((*ev)++, 0,
409 timings->bit_space);
410 if (!max--)
411 return ret;
412 pulse = timings->bit_pulse[data & 1];
413 init_ir_raw_event_duration((*ev)++, 1, pulse);
417 if (!max--)
418 return ret;
420 init_ir_raw_event_duration((*ev)++, 0, timings->trailer_space);
422 return 0;
424 EXPORT_SYMBOL(ir_raw_gen_pl);
427 * ir_raw_encode_scancode() - Encode a scancode as raw events
429 * @protocol: protocol
430 * @scancode: scancode filter describing a single scancode
431 * @events: array of raw events to write into
432 * @max: max number of raw events
434 * Attempts to encode the scancode as raw events.
436 * Returns: The number of events written.
437 * -ENOBUFS if there isn't enough space in the array to fit the
438 * encoding. In this case all @max events will have been written.
439 * -EINVAL if the scancode is ambiguous or invalid, or if no
440 * compatible encoder was found.
442 int ir_raw_encode_scancode(enum rc_proto protocol, u32 scancode,
443 struct ir_raw_event *events, unsigned int max)
445 struct ir_raw_handler *handler;
446 int ret = -EINVAL;
447 u64 mask = 1ULL << protocol;
449 ir_raw_load_modules(&mask);
451 mutex_lock(&ir_raw_handler_lock);
452 list_for_each_entry(handler, &ir_raw_handler_list, list) {
453 if (handler->protocols & mask && handler->encode) {
454 ret = handler->encode(protocol, scancode, events, max);
455 if (ret >= 0 || ret == -ENOBUFS)
456 break;
459 mutex_unlock(&ir_raw_handler_lock);
461 return ret;
463 EXPORT_SYMBOL(ir_raw_encode_scancode);
465 static void edge_handle(struct timer_list *t)
467 struct ir_raw_event_ctrl *raw = from_timer(raw, t, edge_handle);
468 struct rc_dev *dev = raw->dev;
469 ktime_t interval = ktime_sub(ktime_get(), dev->raw->last_event);
471 if (ktime_to_ns(interval) >= dev->timeout) {
472 DEFINE_IR_RAW_EVENT(ev);
474 ev.timeout = true;
475 ev.duration = ktime_to_ns(interval);
477 ir_raw_event_store(dev, &ev);
478 } else {
479 mod_timer(&dev->raw->edge_handle,
480 jiffies + nsecs_to_jiffies(dev->timeout -
481 ktime_to_ns(interval)));
484 ir_raw_event_handle(dev);
488 * ir_raw_encode_carrier() - Get carrier used for protocol
490 * @protocol: protocol
492 * Attempts to find the carrier for the specified protocol
494 * Returns: The carrier in Hz
495 * -EINVAL if the protocol is invalid, or if no
496 * compatible encoder was found.
498 int ir_raw_encode_carrier(enum rc_proto protocol)
500 struct ir_raw_handler *handler;
501 int ret = -EINVAL;
502 u64 mask = BIT_ULL(protocol);
504 mutex_lock(&ir_raw_handler_lock);
505 list_for_each_entry(handler, &ir_raw_handler_list, list) {
506 if (handler->protocols & mask && handler->encode) {
507 ret = handler->carrier;
508 break;
511 mutex_unlock(&ir_raw_handler_lock);
513 return ret;
515 EXPORT_SYMBOL(ir_raw_encode_carrier);
518 * Used to (un)register raw event clients
520 int ir_raw_event_prepare(struct rc_dev *dev)
522 if (!dev)
523 return -EINVAL;
525 dev->raw = kzalloc(sizeof(*dev->raw), GFP_KERNEL);
526 if (!dev->raw)
527 return -ENOMEM;
529 dev->raw->dev = dev;
530 dev->change_protocol = change_protocol;
531 timer_setup(&dev->raw->edge_handle, edge_handle, 0);
532 INIT_KFIFO(dev->raw->kfifo);
534 return 0;
537 int ir_raw_event_register(struct rc_dev *dev)
539 struct ir_raw_handler *handler;
540 struct task_struct *thread;
542 thread = kthread_run(ir_raw_event_thread, dev->raw, "rc%u", dev->minor);
543 if (IS_ERR(thread))
544 return PTR_ERR(thread);
546 dev->raw->thread = thread;
548 mutex_lock(&ir_raw_handler_lock);
549 list_add_tail(&dev->raw->list, &ir_raw_client_list);
550 list_for_each_entry(handler, &ir_raw_handler_list, list)
551 if (handler->raw_register)
552 handler->raw_register(dev);
553 mutex_unlock(&ir_raw_handler_lock);
555 return 0;
558 void ir_raw_event_free(struct rc_dev *dev)
560 if (!dev)
561 return;
563 kfree(dev->raw);
564 dev->raw = NULL;
567 void ir_raw_event_unregister(struct rc_dev *dev)
569 struct ir_raw_handler *handler;
571 if (!dev || !dev->raw)
572 return;
574 kthread_stop(dev->raw->thread);
575 del_timer_sync(&dev->raw->edge_handle);
577 mutex_lock(&ir_raw_handler_lock);
578 list_del(&dev->raw->list);
579 list_for_each_entry(handler, &ir_raw_handler_list, list)
580 if (handler->raw_unregister)
581 handler->raw_unregister(dev);
582 mutex_unlock(&ir_raw_handler_lock);
584 ir_raw_event_free(dev);
588 * Extension interface - used to register the IR decoders
591 int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler)
593 struct ir_raw_event_ctrl *raw;
595 mutex_lock(&ir_raw_handler_lock);
596 list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list);
597 if (ir_raw_handler->raw_register)
598 list_for_each_entry(raw, &ir_raw_client_list, list)
599 ir_raw_handler->raw_register(raw->dev);
600 atomic64_or(ir_raw_handler->protocols, &available_protocols);
601 mutex_unlock(&ir_raw_handler_lock);
603 return 0;
605 EXPORT_SYMBOL(ir_raw_handler_register);
607 void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler)
609 struct ir_raw_event_ctrl *raw;
610 u64 protocols = ir_raw_handler->protocols;
612 mutex_lock(&ir_raw_handler_lock);
613 list_del(&ir_raw_handler->list);
614 list_for_each_entry(raw, &ir_raw_client_list, list) {
615 ir_raw_disable_protocols(raw->dev, protocols);
616 if (ir_raw_handler->raw_unregister)
617 ir_raw_handler->raw_unregister(raw->dev);
619 atomic64_andnot(protocols, &available_protocols);
620 mutex_unlock(&ir_raw_handler_lock);
622 EXPORT_SYMBOL(ir_raw_handler_unregister);