David Härdeman | 4924a31 | 2014-04-03 20:34:28 -0300 | [diff] [blame] | 1 | /* rc-ir-raw.c - handle IR pulse/space events |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 2 | * |
Mauro Carvalho Chehab | 37e59f8 | 2014-02-07 08:03:07 -0200 | [diff] [blame] | 3 | * Copyright (C) 2010 by Mauro Carvalho Chehab |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
Paul Gortmaker | 35a2463 | 2011-08-01 15:26:38 -0400 | [diff] [blame] | 15 | #include <linux/export.h> |
Maxim Levitsky | 0d2cb1d | 2010-07-31 11:59:17 -0300 | [diff] [blame] | 16 | #include <linux/kthread.h> |
Maxim Levitsky | 45a568f | 2010-07-31 11:59:16 -0300 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Stephen Rothwell | dff65de | 2011-07-29 15:34:32 +1000 | [diff] [blame] | 18 | #include <linux/kmod.h> |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 19 | #include <linux/sched.h> |
Mauro Carvalho Chehab | f62de67 | 2010-11-09 23:09:57 -0300 | [diff] [blame] | 20 | #include "rc-core-priv.h" |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 21 | |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 22 | /* Used to keep track of IR raw clients, protected by ir_raw_handler_lock */ |
| 23 | static LIST_HEAD(ir_raw_client_list); |
| 24 | |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 25 | /* Used to handle IR raw handler extensions */ |
Maxim Levitsky | 45a568f | 2010-07-31 11:59:16 -0300 | [diff] [blame] | 26 | static DEFINE_MUTEX(ir_raw_handler_lock); |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 27 | static LIST_HEAD(ir_raw_handler_list); |
Heiner Kallweit | 37e90a2 | 2016-09-27 16:48:47 -0300 | [diff] [blame] | 28 | static atomic64_t available_protocols = ATOMIC64_INIT(0); |
Mauro Carvalho Chehab | 93c312f | 2010-03-25 21:13:43 -0300 | [diff] [blame] | 29 | |
Maxim Levitsky | 0d2cb1d | 2010-07-31 11:59:17 -0300 | [diff] [blame] | 30 | static int ir_raw_event_thread(void *data) |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 31 | { |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 32 | struct ir_raw_event ev; |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 33 | struct ir_raw_handler *handler; |
Maxim Levitsky | 0d2cb1d | 2010-07-31 11:59:17 -0300 | [diff] [blame] | 34 | struct ir_raw_event_ctrl *raw = (struct ir_raw_event_ctrl *)data; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 35 | |
Heiner Kallweit | 74d47d7 | 2016-08-02 02:44:07 -0300 | [diff] [blame] | 36 | while (1) { |
Maxim Levitsky | c6ef1e7 | 2010-09-06 18:26:06 -0300 | [diff] [blame] | 37 | mutex_lock(&ir_raw_handler_lock); |
Heiner Kallweit | 74d47d7 | 2016-08-02 02:44:07 -0300 | [diff] [blame] | 38 | while (kfifo_out(&raw->kfifo, &ev, 1)) { |
| 39 | list_for_each_entry(handler, &ir_raw_handler_list, list) |
| 40 | if (raw->dev->enabled_protocols & |
| 41 | handler->protocols || !handler->protocols) |
| 42 | handler->decode(raw->dev, ev); |
| 43 | raw->prev_ev = ev; |
| 44 | } |
Maxim Levitsky | c6ef1e7 | 2010-09-06 18:26:06 -0300 | [diff] [blame] | 45 | mutex_unlock(&ir_raw_handler_lock); |
Heiner Kallweit | 74d47d7 | 2016-08-02 02:44:07 -0300 | [diff] [blame] | 46 | |
| 47 | set_current_state(TASK_INTERRUPTIBLE); |
| 48 | |
| 49 | if (kthread_should_stop()) { |
| 50 | __set_current_state(TASK_RUNNING); |
| 51 | break; |
| 52 | } else if (!kfifo_is_empty(&raw->kfifo)) |
| 53 | set_current_state(TASK_RUNNING); |
| 54 | |
| 55 | schedule(); |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 56 | } |
Maxim Levitsky | 0d2cb1d | 2010-07-31 11:59:17 -0300 | [diff] [blame] | 57 | |
| 58 | return 0; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 59 | } |
| 60 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 61 | /** |
| 62 | * ir_raw_event_store() - pass a pulse/space duration to the raw ir decoders |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 63 | * @dev: the struct rc_dev device descriptor |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 64 | * @ev: the struct ir_raw_event descriptor of the pulse/space |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 65 | * |
| 66 | * This routine (which may be called from an interrupt context) stores a |
| 67 | * pulse/space duration for the raw ir decoding state machines. Pulses are |
| 68 | * signalled as positive values and spaces as negative values. A zero value |
| 69 | * will reset the decoding state machines. |
| 70 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 71 | int ir_raw_event_store(struct rc_dev *dev, struct ir_raw_event *ev) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 72 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 73 | if (!dev->raw) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 74 | return -EINVAL; |
| 75 | |
Mauro Carvalho Chehab | 74c4792 | 2010-10-20 11:56:50 -0300 | [diff] [blame] | 76 | IR_dprintk(2, "sample: (%05dus %s)\n", |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 77 | TO_US(ev->duration), TO_STR(ev->pulse)); |
Maxim Levitsky | 510fcb7 | 2010-07-31 11:59:15 -0300 | [diff] [blame] | 78 | |
Heiner Kallweit | 464254e | 2015-11-27 20:02:38 -0200 | [diff] [blame] | 79 | if (!kfifo_put(&dev->raw->kfifo, *ev)) { |
| 80 | dev_err(&dev->dev, "IR event FIFO is full!\n"); |
| 81 | return -ENOSPC; |
| 82 | } |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 83 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 84 | return 0; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 85 | } |
| 86 | EXPORT_SYMBOL_GPL(ir_raw_event_store); |
| 87 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 88 | /** |
| 89 | * ir_raw_event_store_edge() - notify raw ir decoders of the start of a pulse/space |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 90 | * @dev: the struct rc_dev device descriptor |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 91 | * @type: the type of the event that has occurred |
| 92 | * |
| 93 | * This routine (which may be called from an interrupt context) is used to |
| 94 | * store the beginning of an ir pulse or space (or the start/end of ir |
| 95 | * reception) for the raw ir decoding state machines. This is used by |
| 96 | * hardware which does not provide durations directly but only interrupts |
| 97 | * (or similar events) on state change. |
| 98 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 99 | int ir_raw_event_store_edge(struct rc_dev *dev, enum raw_event_type type) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 100 | { |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 101 | ktime_t now; |
| 102 | s64 delta; /* ns */ |
Mauro Carvalho Chehab | 8358783 | 2011-01-20 18:16:50 -0300 | [diff] [blame] | 103 | DEFINE_IR_RAW_EVENT(ev); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 104 | int rc = 0; |
Jarod Wilson | 3f5c4c7 | 2011-06-16 16:18:37 -0300 | [diff] [blame] | 105 | int delay; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 106 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 107 | if (!dev->raw) |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 108 | return -EINVAL; |
| 109 | |
| 110 | now = ktime_get(); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 111 | delta = ktime_to_ns(ktime_sub(now, dev->raw->last_event)); |
Jarod Wilson | 3f5c4c7 | 2011-06-16 16:18:37 -0300 | [diff] [blame] | 112 | delay = MS_TO_NS(dev->input_dev->rep[REP_DELAY]); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 113 | |
| 114 | /* Check for a long duration since last event or if we're |
| 115 | * being called for the first time, note that delta can't |
| 116 | * possibly be negative. |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 117 | */ |
Jarod Wilson | 3f5c4c7 | 2011-06-16 16:18:37 -0300 | [diff] [blame] | 118 | if (delta > delay || !dev->raw->last_type) |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 119 | type |= IR_START_EVENT; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 120 | else |
| 121 | ev.duration = delta; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 122 | |
| 123 | if (type & IR_START_EVENT) |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 124 | ir_raw_event_reset(dev); |
| 125 | else if (dev->raw->last_type & IR_SPACE) { |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 126 | ev.pulse = false; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 127 | rc = ir_raw_event_store(dev, &ev); |
| 128 | } else if (dev->raw->last_type & IR_PULSE) { |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 129 | ev.pulse = true; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 130 | rc = ir_raw_event_store(dev, &ev); |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 131 | } else |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 132 | return 0; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 133 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 134 | dev->raw->last_event = now; |
| 135 | dev->raw->last_type = type; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 136 | return rc; |
| 137 | } |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 138 | EXPORT_SYMBOL_GPL(ir_raw_event_store_edge); |
| 139 | |
| 140 | /** |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 141 | * ir_raw_event_store_with_filter() - pass next pulse/space to decoders with some processing |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 142 | * @dev: the struct rc_dev device descriptor |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 143 | * @type: the type of the event that has occurred |
| 144 | * |
| 145 | * This routine (which may be called from an interrupt context) works |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 146 | * in similar manner to ir_raw_event_store_edge. |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 147 | * This routine is intended for devices with limited internal buffer |
Sean Young | b83bfd1 | 2012-08-13 08:59:47 -0300 | [diff] [blame] | 148 | * It automerges samples of same type, and handles timeouts. Returns non-zero |
| 149 | * if the event was added, and zero if the event was ignored due to idle |
| 150 | * processing. |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 151 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 152 | int ir_raw_event_store_with_filter(struct rc_dev *dev, struct ir_raw_event *ev) |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 153 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 154 | if (!dev->raw) |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 155 | return -EINVAL; |
| 156 | |
| 157 | /* Ignore spaces in idle mode */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 158 | if (dev->idle && !ev->pulse) |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 159 | return 0; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 160 | else if (dev->idle) |
| 161 | ir_raw_event_set_idle(dev, false); |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 162 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 163 | if (!dev->raw->this_ev.duration) |
| 164 | dev->raw->this_ev = *ev; |
| 165 | else if (ev->pulse == dev->raw->this_ev.pulse) |
| 166 | dev->raw->this_ev.duration += ev->duration; |
| 167 | else { |
| 168 | ir_raw_event_store(dev, &dev->raw->this_ev); |
| 169 | dev->raw->this_ev = *ev; |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* Enter idle mode if nessesary */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 173 | if (!ev->pulse && dev->timeout && |
| 174 | dev->raw->this_ev.duration >= dev->timeout) |
| 175 | ir_raw_event_set_idle(dev, true); |
| 176 | |
Sean Young | b83bfd1 | 2012-08-13 08:59:47 -0300 | [diff] [blame] | 177 | return 1; |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 178 | } |
| 179 | EXPORT_SYMBOL_GPL(ir_raw_event_store_with_filter); |
| 180 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 181 | /** |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 182 | * ir_raw_event_set_idle() - provide hint to rc-core when the device is idle or not |
| 183 | * @dev: the struct rc_dev device descriptor |
| 184 | * @idle: whether the device is idle or not |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 185 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 186 | void ir_raw_event_set_idle(struct rc_dev *dev, bool idle) |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 187 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 188 | if (!dev->raw) |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 189 | return; |
| 190 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 191 | IR_dprintk(2, "%s idle mode\n", idle ? "enter" : "leave"); |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 192 | |
| 193 | if (idle) { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 194 | dev->raw->this_ev.timeout = true; |
| 195 | ir_raw_event_store(dev, &dev->raw->this_ev); |
| 196 | init_ir_raw_event(&dev->raw->this_ev); |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 197 | } |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 198 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 199 | if (dev->s_idle) |
| 200 | dev->s_idle(dev, idle); |
| 201 | |
| 202 | dev->idle = idle; |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 203 | } |
| 204 | EXPORT_SYMBOL_GPL(ir_raw_event_set_idle); |
| 205 | |
| 206 | /** |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 207 | * ir_raw_event_handle() - schedules the decoding of stored ir data |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 208 | * @dev: the struct rc_dev device descriptor |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 209 | * |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 210 | * This routine will tell rc-core to start decoding stored ir data. |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 211 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 212 | void ir_raw_event_handle(struct rc_dev *dev) |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 213 | { |
Sean Young | 963761a | 2017-05-24 06:24:51 -0300 | [diff] [blame] | 214 | if (!dev->raw || !dev->raw->thread) |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 215 | return; |
| 216 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 217 | wake_up_process(dev->raw->thread); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 218 | } |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 219 | EXPORT_SYMBOL_GPL(ir_raw_event_handle); |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 220 | |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 221 | /* used internally by the sysfs interface */ |
| 222 | u64 |
Randy Dunlap | 2dbd61b | 2011-01-09 00:53:53 -0300 | [diff] [blame] | 223 | ir_raw_get_allowed_protocols(void) |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 224 | { |
Heiner Kallweit | 37e90a2 | 2016-09-27 16:48:47 -0300 | [diff] [blame] | 225 | return atomic64_read(&available_protocols); |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 226 | } |
| 227 | |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 228 | static int change_protocol(struct rc_dev *dev, u64 *rc_type) |
| 229 | { |
| 230 | /* the caller will update dev->enabled_protocols */ |
| 231 | return 0; |
| 232 | } |
| 233 | |
Heiner Kallweit | 93cffffc | 2015-11-16 17:51:56 -0200 | [diff] [blame] | 234 | static void ir_raw_disable_protocols(struct rc_dev *dev, u64 protocols) |
| 235 | { |
| 236 | mutex_lock(&dev->lock); |
| 237 | dev->enabled_protocols &= ~protocols; |
Heiner Kallweit | 93cffffc | 2015-11-16 17:51:56 -0200 | [diff] [blame] | 238 | mutex_unlock(&dev->lock); |
| 239 | } |
| 240 | |
James Hogan | 3875233d | 2015-03-31 14:48:06 -0300 | [diff] [blame] | 241 | /** |
Antti Seppälä | 844a4f4 | 2015-03-31 14:48:07 -0300 | [diff] [blame] | 242 | * ir_raw_gen_manchester() - Encode data with Manchester (bi-phase) modulation. |
| 243 | * @ev: Pointer to pointer to next free event. *@ev is incremented for |
| 244 | * each raw event filled. |
| 245 | * @max: Maximum number of raw events to fill. |
| 246 | * @timings: Manchester modulation timings. |
| 247 | * @n: Number of bits of data. |
| 248 | * @data: Data bits to encode. |
| 249 | * |
| 250 | * Encodes the @n least significant bits of @data using Manchester (bi-phase) |
| 251 | * modulation with the timing characteristics described by @timings, writing up |
| 252 | * to @max raw IR events using the *@ev pointer. |
| 253 | * |
| 254 | * Returns: 0 on success. |
| 255 | * -ENOBUFS if there isn't enough space in the array to fit the |
| 256 | * full encoded data. In this case all @max events will have been |
| 257 | * written. |
| 258 | */ |
| 259 | int ir_raw_gen_manchester(struct ir_raw_event **ev, unsigned int max, |
| 260 | const struct ir_raw_timings_manchester *timings, |
Sean Young | b73bc16 | 2017-02-11 20:33:38 -0200 | [diff] [blame] | 261 | unsigned int n, u64 data) |
Antti Seppälä | 844a4f4 | 2015-03-31 14:48:07 -0300 | [diff] [blame] | 262 | { |
| 263 | bool need_pulse; |
Sean Young | b73bc16 | 2017-02-11 20:33:38 -0200 | [diff] [blame] | 264 | u64 i; |
Antti Seppälä | 844a4f4 | 2015-03-31 14:48:07 -0300 | [diff] [blame] | 265 | int ret = -ENOBUFS; |
| 266 | |
Sean Young | b73bc16 | 2017-02-11 20:33:38 -0200 | [diff] [blame] | 267 | i = BIT_ULL(n - 1); |
Antti Seppälä | 844a4f4 | 2015-03-31 14:48:07 -0300 | [diff] [blame] | 268 | |
| 269 | if (timings->leader) { |
| 270 | if (!max--) |
| 271 | return ret; |
| 272 | if (timings->pulse_space_start) { |
| 273 | init_ir_raw_event_duration((*ev)++, 1, timings->leader); |
| 274 | |
| 275 | if (!max--) |
| 276 | return ret; |
| 277 | init_ir_raw_event_duration((*ev), 0, timings->leader); |
| 278 | } else { |
| 279 | init_ir_raw_event_duration((*ev), 1, timings->leader); |
| 280 | } |
| 281 | i >>= 1; |
| 282 | } else { |
| 283 | /* continue existing signal */ |
| 284 | --(*ev); |
| 285 | } |
| 286 | /* from here on *ev will point to the last event rather than the next */ |
| 287 | |
| 288 | while (n && i > 0) { |
| 289 | need_pulse = !(data & i); |
| 290 | if (timings->invert) |
| 291 | need_pulse = !need_pulse; |
| 292 | if (need_pulse == !!(*ev)->pulse) { |
| 293 | (*ev)->duration += timings->clock; |
| 294 | } else { |
| 295 | if (!max--) |
| 296 | goto nobufs; |
| 297 | init_ir_raw_event_duration(++(*ev), need_pulse, |
| 298 | timings->clock); |
| 299 | } |
| 300 | |
| 301 | if (!max--) |
| 302 | goto nobufs; |
| 303 | init_ir_raw_event_duration(++(*ev), !need_pulse, |
| 304 | timings->clock); |
| 305 | i >>= 1; |
| 306 | } |
| 307 | |
| 308 | if (timings->trailer_space) { |
| 309 | if (!(*ev)->pulse) |
| 310 | (*ev)->duration += timings->trailer_space; |
| 311 | else if (!max--) |
| 312 | goto nobufs; |
| 313 | else |
| 314 | init_ir_raw_event_duration(++(*ev), 0, |
| 315 | timings->trailer_space); |
| 316 | } |
| 317 | |
| 318 | ret = 0; |
| 319 | nobufs: |
| 320 | /* point to the next event rather than last event before returning */ |
| 321 | ++(*ev); |
| 322 | return ret; |
| 323 | } |
| 324 | EXPORT_SYMBOL(ir_raw_gen_manchester); |
| 325 | |
| 326 | /** |
James Hogan | caec098 | 2014-03-14 20:04:12 -0300 | [diff] [blame] | 327 | * ir_raw_gen_pd() - Encode data to raw events with pulse-distance modulation. |
| 328 | * @ev: Pointer to pointer to next free event. *@ev is incremented for |
| 329 | * each raw event filled. |
| 330 | * @max: Maximum number of raw events to fill. |
| 331 | * @timings: Pulse distance modulation timings. |
| 332 | * @n: Number of bits of data. |
| 333 | * @data: Data bits to encode. |
| 334 | * |
| 335 | * Encodes the @n least significant bits of @data using pulse-distance |
| 336 | * modulation with the timing characteristics described by @timings, writing up |
| 337 | * to @max raw IR events using the *@ev pointer. |
| 338 | * |
| 339 | * Returns: 0 on success. |
| 340 | * -ENOBUFS if there isn't enough space in the array to fit the |
| 341 | * full encoded data. In this case all @max events will have been |
| 342 | * written. |
| 343 | */ |
| 344 | int ir_raw_gen_pd(struct ir_raw_event **ev, unsigned int max, |
| 345 | const struct ir_raw_timings_pd *timings, |
| 346 | unsigned int n, u64 data) |
| 347 | { |
| 348 | int i; |
| 349 | int ret; |
| 350 | unsigned int space; |
| 351 | |
| 352 | if (timings->header_pulse) { |
| 353 | ret = ir_raw_gen_pulse_space(ev, &max, timings->header_pulse, |
| 354 | timings->header_space); |
| 355 | if (ret) |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | if (timings->msb_first) { |
| 360 | for (i = n - 1; i >= 0; --i) { |
| 361 | space = timings->bit_space[(data >> i) & 1]; |
| 362 | ret = ir_raw_gen_pulse_space(ev, &max, |
| 363 | timings->bit_pulse, |
| 364 | space); |
| 365 | if (ret) |
| 366 | return ret; |
| 367 | } |
| 368 | } else { |
| 369 | for (i = 0; i < n; ++i, data >>= 1) { |
| 370 | space = timings->bit_space[data & 1]; |
| 371 | ret = ir_raw_gen_pulse_space(ev, &max, |
| 372 | timings->bit_pulse, |
| 373 | space); |
| 374 | if (ret) |
| 375 | return ret; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | ret = ir_raw_gen_pulse_space(ev, &max, timings->trailer_pulse, |
| 380 | timings->trailer_space); |
| 381 | return ret; |
| 382 | } |
| 383 | EXPORT_SYMBOL(ir_raw_gen_pd); |
| 384 | |
| 385 | /** |
Sean Young | 103293b | 2016-12-06 18:33:57 -0200 | [diff] [blame] | 386 | * ir_raw_gen_pl() - Encode data to raw events with pulse-length modulation. |
| 387 | * @ev: Pointer to pointer to next free event. *@ev is incremented for |
| 388 | * each raw event filled. |
| 389 | * @max: Maximum number of raw events to fill. |
| 390 | * @timings: Pulse distance modulation timings. |
| 391 | * @n: Number of bits of data. |
| 392 | * @data: Data bits to encode. |
| 393 | * |
| 394 | * Encodes the @n least significant bits of @data using space-distance |
| 395 | * modulation with the timing characteristics described by @timings, writing up |
| 396 | * to @max raw IR events using the *@ev pointer. |
| 397 | * |
| 398 | * Returns: 0 on success. |
| 399 | * -ENOBUFS if there isn't enough space in the array to fit the |
| 400 | * full encoded data. In this case all @max events will have been |
| 401 | * written. |
| 402 | */ |
| 403 | int ir_raw_gen_pl(struct ir_raw_event **ev, unsigned int max, |
| 404 | const struct ir_raw_timings_pl *timings, |
| 405 | unsigned int n, u64 data) |
| 406 | { |
| 407 | int i; |
| 408 | int ret = -ENOBUFS; |
| 409 | unsigned int pulse; |
| 410 | |
| 411 | if (!max--) |
| 412 | return ret; |
| 413 | |
| 414 | init_ir_raw_event_duration((*ev)++, 1, timings->header_pulse); |
| 415 | |
| 416 | if (timings->msb_first) { |
| 417 | for (i = n - 1; i >= 0; --i) { |
| 418 | if (!max--) |
| 419 | return ret; |
| 420 | init_ir_raw_event_duration((*ev)++, 0, |
| 421 | timings->bit_space); |
| 422 | if (!max--) |
| 423 | return ret; |
| 424 | pulse = timings->bit_pulse[(data >> i) & 1]; |
| 425 | init_ir_raw_event_duration((*ev)++, 1, pulse); |
| 426 | } |
| 427 | } else { |
| 428 | for (i = 0; i < n; ++i, data >>= 1) { |
| 429 | if (!max--) |
| 430 | return ret; |
| 431 | init_ir_raw_event_duration((*ev)++, 0, |
| 432 | timings->bit_space); |
| 433 | if (!max--) |
| 434 | return ret; |
| 435 | pulse = timings->bit_pulse[data & 1]; |
| 436 | init_ir_raw_event_duration((*ev)++, 1, pulse); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | if (!max--) |
| 441 | return ret; |
| 442 | |
| 443 | init_ir_raw_event_duration((*ev)++, 0, timings->trailer_space); |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | EXPORT_SYMBOL(ir_raw_gen_pl); |
| 448 | |
| 449 | /** |
James Hogan | 3875233d | 2015-03-31 14:48:06 -0300 | [diff] [blame] | 450 | * ir_raw_encode_scancode() - Encode a scancode as raw events |
| 451 | * |
| 452 | * @protocol: protocol |
| 453 | * @scancode: scancode filter describing a single scancode |
| 454 | * @events: array of raw events to write into |
| 455 | * @max: max number of raw events |
| 456 | * |
| 457 | * Attempts to encode the scancode as raw events. |
| 458 | * |
| 459 | * Returns: The number of events written. |
| 460 | * -ENOBUFS if there isn't enough space in the array to fit the |
| 461 | * encoding. In this case all @max events will have been written. |
| 462 | * -EINVAL if the scancode is ambiguous or invalid, or if no |
| 463 | * compatible encoder was found. |
| 464 | */ |
| 465 | int ir_raw_encode_scancode(enum rc_type protocol, u32 scancode, |
| 466 | struct ir_raw_event *events, unsigned int max) |
| 467 | { |
| 468 | struct ir_raw_handler *handler; |
| 469 | int ret = -EINVAL; |
| 470 | u64 mask = 1ULL << protocol; |
| 471 | |
| 472 | mutex_lock(&ir_raw_handler_lock); |
| 473 | list_for_each_entry(handler, &ir_raw_handler_list, list) { |
| 474 | if (handler->protocols & mask && handler->encode) { |
| 475 | ret = handler->encode(protocol, scancode, events, max); |
| 476 | if (ret >= 0 || ret == -ENOBUFS) |
| 477 | break; |
| 478 | } |
| 479 | } |
| 480 | mutex_unlock(&ir_raw_handler_lock); |
| 481 | |
| 482 | return ret; |
| 483 | } |
| 484 | EXPORT_SYMBOL(ir_raw_encode_scancode); |
| 485 | |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 486 | /* |
| 487 | * Used to (un)register raw event clients |
| 488 | */ |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 489 | int ir_raw_event_prepare(struct rc_dev *dev) |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 490 | { |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 491 | static bool raw_init; /* 'false' default value, raw decoders loaded? */ |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 492 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 493 | if (!dev) |
| 494 | return -EINVAL; |
| 495 | |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 496 | if (!raw_init) { |
| 497 | request_module("ir-lirc-codec"); |
| 498 | raw_init = true; |
| 499 | } |
| 500 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 501 | dev->raw = kzalloc(sizeof(*dev->raw), GFP_KERNEL); |
| 502 | if (!dev->raw) |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 503 | return -ENOMEM; |
| 504 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 505 | dev->raw->dev = dev; |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 506 | dev->change_protocol = change_protocol; |
Heiner Kallweit | 464254e | 2015-11-27 20:02:38 -0200 | [diff] [blame] | 507 | INIT_KFIFO(dev->raw->kfifo); |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 508 | |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | int ir_raw_event_register(struct rc_dev *dev) |
| 513 | { |
| 514 | struct ir_raw_handler *handler; |
| 515 | struct task_struct *thread; |
| 516 | |
Andi Shyti | d508367 | 2016-12-16 04:12:16 -0200 | [diff] [blame] | 517 | /* |
| 518 | * raw transmitters do not need any event registration |
| 519 | * because the event is coming from userspace |
| 520 | */ |
| 521 | if (dev->driver_type != RC_DRIVER_IR_RAW_TX) { |
Sean Young | 963761a | 2017-05-24 06:24:51 -0300 | [diff] [blame] | 522 | thread = kthread_run(ir_raw_event_thread, dev->raw, "rc%u", |
| 523 | dev->minor); |
Maxim Levitsky | 0d2cb1d | 2010-07-31 11:59:17 -0300 | [diff] [blame] | 524 | |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 525 | if (IS_ERR(thread)) |
| 526 | return PTR_ERR(thread); |
Sean Young | 963761a | 2017-05-24 06:24:51 -0300 | [diff] [blame] | 527 | |
| 528 | dev->raw->thread = thread; |
Maxim Levitsky | 0d2cb1d | 2010-07-31 11:59:17 -0300 | [diff] [blame] | 529 | } |
| 530 | |
Maxim Levitsky | 45a568f | 2010-07-31 11:59:16 -0300 | [diff] [blame] | 531 | mutex_lock(&ir_raw_handler_lock); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 532 | list_add_tail(&dev->raw->list, &ir_raw_client_list); |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 533 | list_for_each_entry(handler, &ir_raw_handler_list, list) |
| 534 | if (handler->raw_register) |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 535 | handler->raw_register(dev); |
Maxim Levitsky | 45a568f | 2010-07-31 11:59:16 -0300 | [diff] [blame] | 536 | mutex_unlock(&ir_raw_handler_lock); |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 537 | |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 538 | return 0; |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 539 | } |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 540 | |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 541 | void ir_raw_event_free(struct rc_dev *dev) |
| 542 | { |
| 543 | if (!dev) |
| 544 | return; |
| 545 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 546 | kfree(dev->raw); |
| 547 | dev->raw = NULL; |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 548 | } |
| 549 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 550 | void ir_raw_event_unregister(struct rc_dev *dev) |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 551 | { |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 552 | struct ir_raw_handler *handler; |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 553 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 554 | if (!dev || !dev->raw) |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 555 | return; |
| 556 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 557 | kthread_stop(dev->raw->thread); |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 558 | |
Maxim Levitsky | 45a568f | 2010-07-31 11:59:16 -0300 | [diff] [blame] | 559 | mutex_lock(&ir_raw_handler_lock); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 560 | list_del(&dev->raw->list); |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 561 | list_for_each_entry(handler, &ir_raw_handler_list, list) |
| 562 | if (handler->raw_unregister) |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 563 | handler->raw_unregister(dev); |
Maxim Levitsky | 45a568f | 2010-07-31 11:59:16 -0300 | [diff] [blame] | 564 | mutex_unlock(&ir_raw_handler_lock); |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 565 | |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 566 | ir_raw_event_free(dev); |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 567 | } |
| 568 | |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 569 | /* |
| 570 | * Extension interface - used to register the IR decoders |
| 571 | */ |
| 572 | |
| 573 | int ir_raw_handler_register(struct ir_raw_handler *ir_raw_handler) |
| 574 | { |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 575 | struct ir_raw_event_ctrl *raw; |
| 576 | |
Maxim Levitsky | 45a568f | 2010-07-31 11:59:16 -0300 | [diff] [blame] | 577 | mutex_lock(&ir_raw_handler_lock); |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 578 | list_add_tail(&ir_raw_handler->list, &ir_raw_handler_list); |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 579 | if (ir_raw_handler->raw_register) |
| 580 | list_for_each_entry(raw, &ir_raw_client_list, list) |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 581 | ir_raw_handler->raw_register(raw->dev); |
Heiner Kallweit | 37e90a2 | 2016-09-27 16:48:47 -0300 | [diff] [blame] | 582 | atomic64_or(ir_raw_handler->protocols, &available_protocols); |
Maxim Levitsky | 45a568f | 2010-07-31 11:59:16 -0300 | [diff] [blame] | 583 | mutex_unlock(&ir_raw_handler_lock); |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 584 | |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 585 | return 0; |
| 586 | } |
| 587 | EXPORT_SYMBOL(ir_raw_handler_register); |
| 588 | |
| 589 | void ir_raw_handler_unregister(struct ir_raw_handler *ir_raw_handler) |
| 590 | { |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 591 | struct ir_raw_event_ctrl *raw; |
Heiner Kallweit | 93cffffc | 2015-11-16 17:51:56 -0200 | [diff] [blame] | 592 | u64 protocols = ir_raw_handler->protocols; |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 593 | |
Maxim Levitsky | 45a568f | 2010-07-31 11:59:16 -0300 | [diff] [blame] | 594 | mutex_lock(&ir_raw_handler_lock); |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 595 | list_del(&ir_raw_handler->list); |
Heiner Kallweit | 93cffffc | 2015-11-16 17:51:56 -0200 | [diff] [blame] | 596 | list_for_each_entry(raw, &ir_raw_client_list, list) { |
| 597 | ir_raw_disable_protocols(raw->dev, protocols); |
| 598 | if (ir_raw_handler->raw_unregister) |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 599 | ir_raw_handler->raw_unregister(raw->dev); |
Heiner Kallweit | 93cffffc | 2015-11-16 17:51:56 -0200 | [diff] [blame] | 600 | } |
Heiner Kallweit | 37e90a2 | 2016-09-27 16:48:47 -0300 | [diff] [blame] | 601 | atomic64_andnot(protocols, &available_protocols); |
Maxim Levitsky | 45a568f | 2010-07-31 11:59:16 -0300 | [diff] [blame] | 602 | mutex_unlock(&ir_raw_handler_lock); |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 603 | } |
| 604 | EXPORT_SYMBOL(ir_raw_handler_unregister); |