Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 2 | /* The industrial I/O core |
| 3 | * |
| 4 | * Copyright (c) 2008 Jonathan Cameron |
| 5 | * |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 6 | * Handling of buffer allocation / resizing. |
| 7 | * |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 8 | * Things to look at here. |
| 9 | * - Better memory allocation techniques? |
| 10 | * - Alternative access techniques? |
| 11 | */ |
| 12 | #include <linux/kernel.h> |
Paul Gortmaker | 8e336a7 | 2011-07-10 13:09:12 -0400 | [diff] [blame] | 13 | #include <linux/export.h> |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 14 | #include <linux/device.h> |
| 15 | #include <linux/fs.h> |
| 16 | #include <linux/cdev.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/poll.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 19 | #include <linux/sched/signal.h> |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 20 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 21 | #include <linux/iio/iio.h> |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 22 | #include <linux/iio/iio-opaque.h> |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 23 | #include "iio_core.h" |
Lars-Peter Clausen | f11d59d | 2020-05-25 14:38:53 +0300 | [diff] [blame] | 24 | #include "iio_core_trigger.h" |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 25 | #include <linux/iio/sysfs.h> |
| 26 | #include <linux/iio/buffer.h> |
Jonathan Cameron | 33dd94c | 2017-01-02 19:28:34 +0000 | [diff] [blame] | 27 | #include <linux/iio/buffer_impl.h> |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 28 | |
| 29 | static const char * const iio_endian_prefix[] = { |
| 30 | [IIO_BE] = "be", |
| 31 | [IIO_LE] = "le", |
| 32 | }; |
| 33 | |
Lars-Peter Clausen | 705ee2c | 2013-09-15 16:31:00 +0100 | [diff] [blame] | 34 | static bool iio_buffer_is_active(struct iio_buffer *buf) |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 35 | { |
Lars-Peter Clausen | 705ee2c | 2013-09-15 16:31:00 +0100 | [diff] [blame] | 36 | return !list_empty(&buf->buffer_list); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 39 | static size_t iio_buffer_data_available(struct iio_buffer *buf) |
Lars-Peter Clausen | 647cc7b | 2013-11-25 14:56:00 +0000 | [diff] [blame] | 40 | { |
Josselin Costanzi | 9dd4694 | 2014-06-27 17:20:00 +0100 | [diff] [blame] | 41 | return buf->access->data_available(buf); |
Lars-Peter Clausen | 647cc7b | 2013-11-25 14:56:00 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Octavian Purdila | f4f4673 | 2015-03-22 20:33:39 +0200 | [diff] [blame] | 44 | static int iio_buffer_flush_hwfifo(struct iio_dev *indio_dev, |
| 45 | struct iio_buffer *buf, size_t required) |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 46 | { |
Octavian Purdila | f4f4673 | 2015-03-22 20:33:39 +0200 | [diff] [blame] | 47 | if (!indio_dev->info->hwfifo_flush_to_buffer) |
| 48 | return -ENODEV; |
| 49 | |
| 50 | return indio_dev->info->hwfifo_flush_to_buffer(indio_dev, required); |
| 51 | } |
| 52 | |
| 53 | static bool iio_buffer_ready(struct iio_dev *indio_dev, struct iio_buffer *buf, |
| 54 | size_t to_wait, int to_flush) |
| 55 | { |
| 56 | size_t avail; |
| 57 | int flushed = 0; |
| 58 | |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 59 | /* wakeup if the device was unregistered */ |
| 60 | if (!indio_dev->info) |
| 61 | return true; |
| 62 | |
| 63 | /* drain the buffer if it was disabled */ |
Octavian Purdila | f4f4673 | 2015-03-22 20:33:39 +0200 | [diff] [blame] | 64 | if (!iio_buffer_is_active(buf)) { |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 65 | to_wait = min_t(size_t, to_wait, 1); |
Octavian Purdila | f4f4673 | 2015-03-22 20:33:39 +0200 | [diff] [blame] | 66 | to_flush = 0; |
| 67 | } |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 68 | |
Octavian Purdila | f4f4673 | 2015-03-22 20:33:39 +0200 | [diff] [blame] | 69 | avail = iio_buffer_data_available(buf); |
| 70 | |
| 71 | if (avail >= to_wait) { |
| 72 | /* force a flush for non-blocking reads */ |
Octavian Purdila | c6f67a1 | 2015-06-05 15:56:47 +0300 | [diff] [blame] | 73 | if (!to_wait && avail < to_flush) |
| 74 | iio_buffer_flush_hwfifo(indio_dev, buf, |
| 75 | to_flush - avail); |
Octavian Purdila | f4f4673 | 2015-03-22 20:33:39 +0200 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | |
| 79 | if (to_flush) |
| 80 | flushed = iio_buffer_flush_hwfifo(indio_dev, buf, |
| 81 | to_wait - avail); |
| 82 | if (flushed <= 0) |
| 83 | return false; |
| 84 | |
| 85 | if (avail + flushed >= to_wait) |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 86 | return true; |
| 87 | |
| 88 | return false; |
| 89 | } |
| 90 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 91 | /** |
Lars-Peter Clausen | f6d4033 | 2019-12-11 12:43:00 +0200 | [diff] [blame] | 92 | * iio_buffer_read_outer() - chrdev read for buffer access |
Cristina Opriceana | 0123635 | 2015-07-24 16:18:09 +0300 | [diff] [blame] | 93 | * @filp: File structure pointer for the char device |
| 94 | * @buf: Destination buffer for iio buffer read |
| 95 | * @n: First n bytes to read |
| 96 | * @f_ps: Long offset provided by the user as a seek position |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 97 | * |
| 98 | * This function relies on all buffer implementations having an |
| 99 | * iio_buffer as their first element. |
Cristina Opriceana | 0123635 | 2015-07-24 16:18:09 +0300 | [diff] [blame] | 100 | * |
| 101 | * Return: negative values corresponding to error codes or ret != 0 |
| 102 | * for ending the reading activity |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 103 | **/ |
Lars-Peter Clausen | f6d4033 | 2019-12-11 12:43:00 +0200 | [diff] [blame] | 104 | ssize_t iio_buffer_read_outer(struct file *filp, char __user *buf, |
| 105 | size_t n, loff_t *f_ps) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 106 | { |
| 107 | struct iio_dev *indio_dev = filp->private_data; |
| 108 | struct iio_buffer *rb = indio_dev->buffer; |
Brian Norris | fcf68f3 | 2016-08-08 17:19:38 -0700 | [diff] [blame] | 109 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 110 | size_t datum_size; |
Octavian Purdila | c6f67a1 | 2015-06-05 15:56:47 +0300 | [diff] [blame] | 111 | size_t to_wait; |
Colin Ian King | 5dba4b1 | 2016-09-05 15:39:06 +0100 | [diff] [blame] | 112 | int ret = 0; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 113 | |
Lars-Peter Clausen | f18e7a0 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 114 | if (!indio_dev->info) |
| 115 | return -ENODEV; |
| 116 | |
Lars-Peter Clausen | f6d4033 | 2019-12-11 12:43:00 +0200 | [diff] [blame] | 117 | if (!rb || !rb->access->read) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 118 | return -EINVAL; |
Lars-Peter Clausen | ee551a1 | 2013-11-25 14:56:00 +0000 | [diff] [blame] | 119 | |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 120 | datum_size = rb->bytes_per_datum; |
Lars-Peter Clausen | ee551a1 | 2013-11-25 14:56:00 +0000 | [diff] [blame] | 121 | |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 122 | /* |
| 123 | * If datum_size is 0 there will never be anything to read from the |
| 124 | * buffer, so signal end of file now. |
| 125 | */ |
| 126 | if (!datum_size) |
| 127 | return 0; |
| 128 | |
Octavian Purdila | c6f67a1 | 2015-06-05 15:56:47 +0300 | [diff] [blame] | 129 | if (filp->f_flags & O_NONBLOCK) |
| 130 | to_wait = 0; |
| 131 | else |
| 132 | to_wait = min_t(size_t, n / datum_size, rb->watermark); |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 133 | |
Brian Norris | fcf68f3 | 2016-08-08 17:19:38 -0700 | [diff] [blame] | 134 | add_wait_queue(&rb->pollq, &wait); |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 135 | do { |
Brian Norris | fcf68f3 | 2016-08-08 17:19:38 -0700 | [diff] [blame] | 136 | if (!indio_dev->info) { |
| 137 | ret = -ENODEV; |
| 138 | break; |
| 139 | } |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 140 | |
Brian Norris | fcf68f3 | 2016-08-08 17:19:38 -0700 | [diff] [blame] | 141 | if (!iio_buffer_ready(indio_dev, rb, to_wait, n / datum_size)) { |
| 142 | if (signal_pending(current)) { |
| 143 | ret = -ERESTARTSYS; |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | wait_woken(&wait, TASK_INTERRUPTIBLE, |
| 148 | MAX_SCHEDULE_TIMEOUT); |
| 149 | continue; |
| 150 | } |
Lars-Peter Clausen | ee551a1 | 2013-11-25 14:56:00 +0000 | [diff] [blame] | 151 | |
Lars-Peter Clausen | f6d4033 | 2019-12-11 12:43:00 +0200 | [diff] [blame] | 152 | ret = rb->access->read(rb, n, buf); |
Lars-Peter Clausen | ee551a1 | 2013-11-25 14:56:00 +0000 | [diff] [blame] | 153 | if (ret == 0 && (filp->f_flags & O_NONBLOCK)) |
| 154 | ret = -EAGAIN; |
Colin Ian King | 5dba4b1 | 2016-09-05 15:39:06 +0100 | [diff] [blame] | 155 | } while (ret == 0); |
Brian Norris | fcf68f3 | 2016-08-08 17:19:38 -0700 | [diff] [blame] | 156 | remove_wait_queue(&rb->pollq, &wait); |
Lars-Peter Clausen | ee551a1 | 2013-11-25 14:56:00 +0000 | [diff] [blame] | 157 | |
| 158 | return ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * iio_buffer_poll() - poll the buffer to find out if it has data |
Cristina Opriceana | 0123635 | 2015-07-24 16:18:09 +0300 | [diff] [blame] | 163 | * @filp: File structure pointer for device access |
| 164 | * @wait: Poll table structure pointer for which the driver adds |
| 165 | * a wait queue |
| 166 | * |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 167 | * Return: (EPOLLIN | EPOLLRDNORM) if data is available for reading |
Cristina Opriceana | 0123635 | 2015-07-24 16:18:09 +0300 | [diff] [blame] | 168 | * or 0 for other cases |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 169 | */ |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 170 | __poll_t iio_buffer_poll(struct file *filp, |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 171 | struct poll_table_struct *wait) |
| 172 | { |
| 173 | struct iio_dev *indio_dev = filp->private_data; |
| 174 | struct iio_buffer *rb = indio_dev->buffer; |
| 175 | |
Stefan Windfeldt-Prytz | 4cd140b | 2018-02-15 15:02:53 +0100 | [diff] [blame] | 176 | if (!indio_dev->info || rb == NULL) |
Cristina Opriceana | 1bdc029 | 2015-08-03 13:37:40 +0300 | [diff] [blame] | 177 | return 0; |
Lars-Peter Clausen | f18e7a0 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 178 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 179 | poll_wait(filp, &rb->pollq, wait); |
Octavian Purdila | f4f4673 | 2015-03-22 20:33:39 +0200 | [diff] [blame] | 180 | if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 181 | return EPOLLIN | EPOLLRDNORM; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 185 | /** |
| 186 | * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue |
| 187 | * @indio_dev: The IIO device |
| 188 | * |
| 189 | * Wakes up the event waitqueue used for poll(). Should usually |
| 190 | * be called when the device is unregistered. |
| 191 | */ |
| 192 | void iio_buffer_wakeup_poll(struct iio_dev *indio_dev) |
| 193 | { |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 194 | struct iio_buffer *buffer = indio_dev->buffer; |
| 195 | |
| 196 | if (!buffer) |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 197 | return; |
| 198 | |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 199 | wake_up(&buffer->pollq); |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame] | 202 | void iio_buffer_init(struct iio_buffer *buffer) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 203 | { |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 204 | INIT_LIST_HEAD(&buffer->demux_list); |
Lars-Peter Clausen | 705ee2c | 2013-09-15 16:31:00 +0100 | [diff] [blame] | 205 | INIT_LIST_HEAD(&buffer->buffer_list); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 206 | init_waitqueue_head(&buffer->pollq); |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 207 | kref_init(&buffer->ref); |
Lars-Peter Clausen | 4a60535 | 2015-10-13 18:10:25 +0200 | [diff] [blame] | 208 | if (!buffer->watermark) |
| 209 | buffer->watermark = 1; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 210 | } |
| 211 | EXPORT_SYMBOL(iio_buffer_init); |
| 212 | |
Jonathan Cameron | 9f46677 | 2017-01-02 19:28:26 +0000 | [diff] [blame] | 213 | /** |
| 214 | * iio_buffer_set_attrs - Set buffer specific attributes |
| 215 | * @buffer: The buffer for which we are setting attributes |
| 216 | * @attrs: Pointer to a null terminated list of pointers to attributes |
| 217 | */ |
| 218 | void iio_buffer_set_attrs(struct iio_buffer *buffer, |
| 219 | const struct attribute **attrs) |
| 220 | { |
| 221 | buffer->attrs = attrs; |
| 222 | } |
| 223 | EXPORT_SYMBOL_GPL(iio_buffer_set_attrs); |
| 224 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 225 | static ssize_t iio_show_scan_index(struct device *dev, |
| 226 | struct device_attribute *attr, |
| 227 | char *buf) |
| 228 | { |
| 229 | return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index); |
| 230 | } |
| 231 | |
| 232 | static ssize_t iio_show_fixed_type(struct device *dev, |
| 233 | struct device_attribute *attr, |
| 234 | char *buf) |
| 235 | { |
| 236 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 237 | u8 type = this_attr->c->scan_type.endianness; |
| 238 | |
| 239 | if (type == IIO_CPU) { |
Jonathan Cameron | 9d5d115 | 2011-10-04 16:02:08 +0100 | [diff] [blame] | 240 | #ifdef __LITTLE_ENDIAN |
| 241 | type = IIO_LE; |
| 242 | #else |
| 243 | type = IIO_BE; |
| 244 | #endif |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 245 | } |
Srinivas Pandruvada | 0ee8546 | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 246 | if (this_attr->c->scan_type.repeat > 1) |
| 247 | return sprintf(buf, "%s:%c%d/%dX%d>>%u\n", |
| 248 | iio_endian_prefix[type], |
| 249 | this_attr->c->scan_type.sign, |
| 250 | this_attr->c->scan_type.realbits, |
| 251 | this_attr->c->scan_type.storagebits, |
| 252 | this_attr->c->scan_type.repeat, |
| 253 | this_attr->c->scan_type.shift); |
| 254 | else |
| 255 | return sprintf(buf, "%s:%c%d/%d>>%u\n", |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 256 | iio_endian_prefix[type], |
| 257 | this_attr->c->scan_type.sign, |
| 258 | this_attr->c->scan_type.realbits, |
| 259 | this_attr->c->scan_type.storagebits, |
| 260 | this_attr->c->scan_type.shift); |
| 261 | } |
| 262 | |
| 263 | static ssize_t iio_scan_el_show(struct device *dev, |
| 264 | struct device_attribute *attr, |
| 265 | char *buf) |
| 266 | { |
| 267 | int ret; |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 268 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 269 | struct iio_buffer *buffer = indio_dev->buffer; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 270 | |
Alec Berg | 2076a20 | 2014-03-19 18:50:00 +0000 | [diff] [blame] | 271 | /* Ensure ret is 0 or 1. */ |
| 272 | ret = !!test_bit(to_iio_dev_attr(attr)->address, |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 273 | buffer->scan_mask); |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 274 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 275 | return sprintf(buf, "%d\n", ret); |
| 276 | } |
| 277 | |
Lars-Peter Clausen | 217a5cf | 2014-11-26 18:55:09 +0100 | [diff] [blame] | 278 | /* Note NULL used as error indicator as it doesn't make sense. */ |
| 279 | static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks, |
| 280 | unsigned int masklength, |
Lars-Peter Clausen | 1e1ec28 | 2015-05-29 18:14:22 +0200 | [diff] [blame] | 281 | const unsigned long *mask, |
| 282 | bool strict) |
Lars-Peter Clausen | 217a5cf | 2014-11-26 18:55:09 +0100 | [diff] [blame] | 283 | { |
| 284 | if (bitmap_empty(mask, masklength)) |
| 285 | return NULL; |
| 286 | while (*av_masks) { |
Lars-Peter Clausen | 1e1ec28 | 2015-05-29 18:14:22 +0200 | [diff] [blame] | 287 | if (strict) { |
| 288 | if (bitmap_equal(mask, av_masks, masklength)) |
| 289 | return av_masks; |
| 290 | } else { |
| 291 | if (bitmap_subset(mask, av_masks, masklength)) |
| 292 | return av_masks; |
| 293 | } |
Lars-Peter Clausen | 217a5cf | 2014-11-26 18:55:09 +0100 | [diff] [blame] | 294 | av_masks += BITS_TO_LONGS(masklength); |
| 295 | } |
| 296 | return NULL; |
| 297 | } |
| 298 | |
| 299 | static bool iio_validate_scan_mask(struct iio_dev *indio_dev, |
| 300 | const unsigned long *mask) |
| 301 | { |
| 302 | if (!indio_dev->setup_ops->validate_scan_mask) |
| 303 | return true; |
| 304 | |
| 305 | return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * iio_scan_mask_set() - set particular bit in the scan mask |
| 310 | * @indio_dev: the iio device |
| 311 | * @buffer: the buffer whose scan mask we are interested in |
| 312 | * @bit: the bit to be set. |
| 313 | * |
| 314 | * Note that at this point we have no way of knowing what other |
| 315 | * buffers might request, hence this code only verifies that the |
| 316 | * individual buffers request is plausible. |
| 317 | */ |
| 318 | static int iio_scan_mask_set(struct iio_dev *indio_dev, |
| 319 | struct iio_buffer *buffer, int bit) |
| 320 | { |
| 321 | const unsigned long *mask; |
| 322 | unsigned long *trialmask; |
| 323 | |
Alexandru Ardelean | ccd428e | 2020-03-21 10:59:56 +0200 | [diff] [blame] | 324 | trialmask = bitmap_zalloc(indio_dev->masklength, GFP_KERNEL); |
Lars-Peter Clausen | 217a5cf | 2014-11-26 18:55:09 +0100 | [diff] [blame] | 325 | if (trialmask == NULL) |
| 326 | return -ENOMEM; |
| 327 | if (!indio_dev->masklength) { |
Dan Carpenter | 231bfe5 | 2015-11-21 13:33:00 +0300 | [diff] [blame] | 328 | WARN(1, "Trying to set scanmask prior to registering buffer\n"); |
Lars-Peter Clausen | 217a5cf | 2014-11-26 18:55:09 +0100 | [diff] [blame] | 329 | goto err_invalid_mask; |
| 330 | } |
| 331 | bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength); |
| 332 | set_bit(bit, trialmask); |
| 333 | |
| 334 | if (!iio_validate_scan_mask(indio_dev, trialmask)) |
| 335 | goto err_invalid_mask; |
| 336 | |
| 337 | if (indio_dev->available_scan_masks) { |
| 338 | mask = iio_scan_mask_match(indio_dev->available_scan_masks, |
| 339 | indio_dev->masklength, |
Lars-Peter Clausen | 1e1ec28 | 2015-05-29 18:14:22 +0200 | [diff] [blame] | 340 | trialmask, false); |
Lars-Peter Clausen | 217a5cf | 2014-11-26 18:55:09 +0100 | [diff] [blame] | 341 | if (!mask) |
| 342 | goto err_invalid_mask; |
| 343 | } |
| 344 | bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength); |
| 345 | |
Andy Shevchenko | 3862828 | 2019-03-04 10:55:40 +0200 | [diff] [blame] | 346 | bitmap_free(trialmask); |
Lars-Peter Clausen | 217a5cf | 2014-11-26 18:55:09 +0100 | [diff] [blame] | 347 | |
| 348 | return 0; |
| 349 | |
| 350 | err_invalid_mask: |
Andy Shevchenko | 3862828 | 2019-03-04 10:55:40 +0200 | [diff] [blame] | 351 | bitmap_free(trialmask); |
Lars-Peter Clausen | 217a5cf | 2014-11-26 18:55:09 +0100 | [diff] [blame] | 352 | return -EINVAL; |
| 353 | } |
| 354 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 355 | static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit) |
| 356 | { |
| 357 | clear_bit(bit, buffer->scan_mask); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
Jonathan Cameron | c2bf8d5 | 2017-01-02 19:28:27 +0000 | [diff] [blame] | 361 | static int iio_scan_mask_query(struct iio_dev *indio_dev, |
| 362 | struct iio_buffer *buffer, int bit) |
| 363 | { |
| 364 | if (bit > indio_dev->masklength) |
| 365 | return -EINVAL; |
| 366 | |
| 367 | if (!buffer->scan_mask) |
| 368 | return 0; |
| 369 | |
| 370 | /* Ensure return value is 0 or 1. */ |
| 371 | return !!test_bit(bit, buffer->scan_mask); |
| 372 | }; |
| 373 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 374 | static ssize_t iio_scan_el_store(struct device *dev, |
| 375 | struct device_attribute *attr, |
| 376 | const char *buf, |
| 377 | size_t len) |
| 378 | { |
Jonathan Cameron | a714af2 | 2012-04-21 10:09:32 +0100 | [diff] [blame] | 379 | int ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 380 | bool state; |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 381 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 382 | struct iio_buffer *buffer = indio_dev->buffer; |
| 383 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 384 | |
Jonathan Cameron | a714af2 | 2012-04-21 10:09:32 +0100 | [diff] [blame] | 385 | ret = strtobool(buf, &state); |
| 386 | if (ret < 0) |
| 387 | return ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 388 | mutex_lock(&indio_dev->mlock); |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 389 | if (iio_buffer_is_active(buffer)) { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 390 | ret = -EBUSY; |
| 391 | goto error_ret; |
| 392 | } |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame] | 393 | ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 394 | if (ret < 0) |
| 395 | goto error_ret; |
| 396 | if (!state && ret) { |
| 397 | ret = iio_scan_mask_clear(buffer, this_attr->address); |
| 398 | if (ret) |
| 399 | goto error_ret; |
| 400 | } else if (state && !ret) { |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame] | 401 | ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 402 | if (ret) |
| 403 | goto error_ret; |
| 404 | } |
| 405 | |
| 406 | error_ret: |
| 407 | mutex_unlock(&indio_dev->mlock); |
| 408 | |
Lars-Peter Clausen | 5a2a6e1 | 2011-12-08 18:35:53 +0100 | [diff] [blame] | 409 | return ret < 0 ? ret : len; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 410 | |
| 411 | } |
| 412 | |
| 413 | static ssize_t iio_scan_el_ts_show(struct device *dev, |
| 414 | struct device_attribute *attr, |
| 415 | char *buf) |
| 416 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 417 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 418 | struct iio_buffer *buffer = indio_dev->buffer; |
| 419 | |
| 420 | return sprintf(buf, "%d\n", buffer->scan_timestamp); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static ssize_t iio_scan_el_ts_store(struct device *dev, |
| 424 | struct device_attribute *attr, |
| 425 | const char *buf, |
| 426 | size_t len) |
| 427 | { |
Jonathan Cameron | a714af2 | 2012-04-21 10:09:32 +0100 | [diff] [blame] | 428 | int ret; |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 429 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 430 | struct iio_buffer *buffer = indio_dev->buffer; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 431 | bool state; |
| 432 | |
Jonathan Cameron | a714af2 | 2012-04-21 10:09:32 +0100 | [diff] [blame] | 433 | ret = strtobool(buf, &state); |
| 434 | if (ret < 0) |
| 435 | return ret; |
| 436 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 437 | mutex_lock(&indio_dev->mlock); |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 438 | if (iio_buffer_is_active(buffer)) { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 439 | ret = -EBUSY; |
| 440 | goto error_ret; |
| 441 | } |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 442 | buffer->scan_timestamp = state; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 443 | error_ret: |
| 444 | mutex_unlock(&indio_dev->mlock); |
| 445 | |
| 446 | return ret ? ret : len; |
| 447 | } |
| 448 | |
| 449 | static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev, |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 450 | struct iio_buffer *buffer, |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 451 | const struct iio_chan_spec *chan) |
| 452 | { |
| 453 | int ret, attrcount = 0; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 454 | |
| 455 | ret = __iio_add_chan_devattr("index", |
| 456 | chan, |
| 457 | &iio_show_scan_index, |
| 458 | NULL, |
| 459 | 0, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 460 | IIO_SEPARATE, |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 461 | &indio_dev->dev, |
| 462 | &buffer->scan_el_dev_attr_list); |
| 463 | if (ret) |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 464 | return ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 465 | attrcount++; |
| 466 | ret = __iio_add_chan_devattr("type", |
| 467 | chan, |
| 468 | &iio_show_fixed_type, |
| 469 | NULL, |
| 470 | 0, |
| 471 | 0, |
| 472 | &indio_dev->dev, |
| 473 | &buffer->scan_el_dev_attr_list); |
| 474 | if (ret) |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 475 | return ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 476 | attrcount++; |
| 477 | if (chan->type != IIO_TIMESTAMP) |
| 478 | ret = __iio_add_chan_devattr("en", |
| 479 | chan, |
| 480 | &iio_scan_el_show, |
| 481 | &iio_scan_el_store, |
| 482 | chan->scan_index, |
| 483 | 0, |
| 484 | &indio_dev->dev, |
| 485 | &buffer->scan_el_dev_attr_list); |
| 486 | else |
| 487 | ret = __iio_add_chan_devattr("en", |
| 488 | chan, |
| 489 | &iio_scan_el_ts_show, |
| 490 | &iio_scan_el_ts_store, |
| 491 | chan->scan_index, |
| 492 | 0, |
| 493 | &indio_dev->dev, |
| 494 | &buffer->scan_el_dev_attr_list); |
Peter Meerwald | 9572588 | 2013-09-17 23:42:00 +0100 | [diff] [blame] | 495 | if (ret) |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 496 | return ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 497 | attrcount++; |
| 498 | ret = attrcount; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 499 | return ret; |
| 500 | } |
| 501 | |
Lars-Peter Clausen | 08e7e0a | 2014-11-26 18:55:15 +0100 | [diff] [blame] | 502 | static ssize_t iio_buffer_read_length(struct device *dev, |
| 503 | struct device_attribute *attr, |
| 504 | char *buf) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 505 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 506 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 507 | struct iio_buffer *buffer = indio_dev->buffer; |
| 508 | |
Lars-Peter Clausen | 3749566 | 2014-11-26 18:55:17 +0100 | [diff] [blame] | 509 | return sprintf(buf, "%d\n", buffer->length); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 510 | } |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 511 | |
Lars-Peter Clausen | 08e7e0a | 2014-11-26 18:55:15 +0100 | [diff] [blame] | 512 | static ssize_t iio_buffer_write_length(struct device *dev, |
| 513 | struct device_attribute *attr, |
| 514 | const char *buf, size_t len) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 515 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 516 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 517 | struct iio_buffer *buffer = indio_dev->buffer; |
Lars-Peter Clausen | 948ad20 | 2012-10-18 14:47:00 +0100 | [diff] [blame] | 518 | unsigned int val; |
| 519 | int ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 520 | |
Lars-Peter Clausen | 948ad20 | 2012-10-18 14:47:00 +0100 | [diff] [blame] | 521 | ret = kstrtouint(buf, 10, &val); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 522 | if (ret) |
| 523 | return ret; |
| 524 | |
Lars-Peter Clausen | 3749566 | 2014-11-26 18:55:17 +0100 | [diff] [blame] | 525 | if (val == buffer->length) |
| 526 | return len; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 527 | |
Lars-Peter Clausen | e38c79e | 2011-12-19 15:23:44 +0100 | [diff] [blame] | 528 | mutex_lock(&indio_dev->mlock); |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 529 | if (iio_buffer_is_active(buffer)) { |
Lars-Peter Clausen | e38c79e | 2011-12-19 15:23:44 +0100 | [diff] [blame] | 530 | ret = -EBUSY; |
| 531 | } else { |
Lars-Peter Clausen | 8d92db2 | 2014-11-26 18:55:16 +0100 | [diff] [blame] | 532 | buffer->access->set_length(buffer, val); |
Lars-Peter Clausen | e38c79e | 2011-12-19 15:23:44 +0100 | [diff] [blame] | 533 | ret = 0; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 534 | } |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 535 | if (ret) |
| 536 | goto out; |
| 537 | if (buffer->length && buffer->length < buffer->watermark) |
| 538 | buffer->watermark = buffer->length; |
| 539 | out: |
Lars-Peter Clausen | e38c79e | 2011-12-19 15:23:44 +0100 | [diff] [blame] | 540 | mutex_unlock(&indio_dev->mlock); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 541 | |
Lars-Peter Clausen | e38c79e | 2011-12-19 15:23:44 +0100 | [diff] [blame] | 542 | return ret ? ret : len; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 543 | } |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 544 | |
Lars-Peter Clausen | 08e7e0a | 2014-11-26 18:55:15 +0100 | [diff] [blame] | 545 | static ssize_t iio_buffer_show_enable(struct device *dev, |
| 546 | struct device_attribute *attr, |
| 547 | char *buf) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 548 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 549 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 550 | struct iio_buffer *buffer = indio_dev->buffer; |
| 551 | |
| 552 | return sprintf(buf, "%d\n", iio_buffer_is_active(buffer)); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 553 | } |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 554 | |
Lars-Peter Clausen | 182b490 | 2016-02-02 16:27:24 +0100 | [diff] [blame] | 555 | static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev, |
| 556 | unsigned int scan_index) |
| 557 | { |
| 558 | const struct iio_chan_spec *ch; |
| 559 | unsigned int bytes; |
| 560 | |
| 561 | ch = iio_find_channel_from_si(indio_dev, scan_index); |
| 562 | bytes = ch->scan_type.storagebits / 8; |
| 563 | if (ch->scan_type.repeat > 1) |
| 564 | bytes *= ch->scan_type.repeat; |
| 565 | return bytes; |
| 566 | } |
| 567 | |
| 568 | static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev) |
| 569 | { |
| 570 | return iio_storage_bytes_for_si(indio_dev, |
| 571 | indio_dev->scan_index_timestamp); |
| 572 | } |
| 573 | |
Peter Meerwald | 183f417 | 2013-09-18 22:10:00 +0100 | [diff] [blame] | 574 | static int iio_compute_scan_bytes(struct iio_dev *indio_dev, |
| 575 | const unsigned long *mask, bool timestamp) |
Jonathan Cameron | 959d295 | 2011-12-05 21:37:13 +0000 | [diff] [blame] | 576 | { |
Jonathan Cameron | 959d295 | 2011-12-05 21:37:13 +0000 | [diff] [blame] | 577 | unsigned bytes = 0; |
Lars Möllendorf | 883f616 | 2019-12-13 14:50:55 +0100 | [diff] [blame] | 578 | int length, i, largest = 0; |
Jonathan Cameron | 6b3b58e | 2012-04-21 10:09:33 +0100 | [diff] [blame] | 579 | |
| 580 | /* How much space will the demuxed element take? */ |
| 581 | for_each_set_bit(i, mask, |
| 582 | indio_dev->masklength) { |
Lars-Peter Clausen | 182b490 | 2016-02-02 16:27:24 +0100 | [diff] [blame] | 583 | length = iio_storage_bytes_for_si(indio_dev, i); |
Jonathan Cameron | 6b3b58e | 2012-04-21 10:09:33 +0100 | [diff] [blame] | 584 | bytes = ALIGN(bytes, length); |
| 585 | bytes += length; |
Lars Möllendorf | 883f616 | 2019-12-13 14:50:55 +0100 | [diff] [blame] | 586 | largest = max(largest, length); |
Jonathan Cameron | 6b3b58e | 2012-04-21 10:09:33 +0100 | [diff] [blame] | 587 | } |
Lars-Peter Clausen | 182b490 | 2016-02-02 16:27:24 +0100 | [diff] [blame] | 588 | |
Jonathan Cameron | 6b3b58e | 2012-04-21 10:09:33 +0100 | [diff] [blame] | 589 | if (timestamp) { |
Lars-Peter Clausen | 182b490 | 2016-02-02 16:27:24 +0100 | [diff] [blame] | 590 | length = iio_storage_bytes_for_timestamp(indio_dev); |
Jonathan Cameron | 6b3b58e | 2012-04-21 10:09:33 +0100 | [diff] [blame] | 591 | bytes = ALIGN(bytes, length); |
| 592 | bytes += length; |
Lars Möllendorf | 883f616 | 2019-12-13 14:50:55 +0100 | [diff] [blame] | 593 | largest = max(largest, length); |
Jonathan Cameron | 6b3b58e | 2012-04-21 10:09:33 +0100 | [diff] [blame] | 594 | } |
Lars Möllendorf | 883f616 | 2019-12-13 14:50:55 +0100 | [diff] [blame] | 595 | |
| 596 | bytes = ALIGN(bytes, largest); |
Jonathan Cameron | 6b3b58e | 2012-04-21 10:09:33 +0100 | [diff] [blame] | 597 | return bytes; |
| 598 | } |
| 599 | |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 600 | static void iio_buffer_activate(struct iio_dev *indio_dev, |
| 601 | struct iio_buffer *buffer) |
| 602 | { |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 603 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); |
| 604 | |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 605 | iio_buffer_get(buffer); |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 606 | list_add(&buffer->buffer_list, &iio_dev_opaque->buffer_list); |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | static void iio_buffer_deactivate(struct iio_buffer *buffer) |
| 610 | { |
| 611 | list_del_init(&buffer->buffer_list); |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 612 | wake_up_interruptible(&buffer->pollq); |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 613 | iio_buffer_put(buffer); |
| 614 | } |
| 615 | |
Lars-Peter Clausen | 1250186 | 2015-05-18 13:34:49 +0200 | [diff] [blame] | 616 | static void iio_buffer_deactivate_all(struct iio_dev *indio_dev) |
| 617 | { |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 618 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); |
Lars-Peter Clausen | 1250186 | 2015-05-18 13:34:49 +0200 | [diff] [blame] | 619 | struct iio_buffer *buffer, *_buffer; |
| 620 | |
| 621 | list_for_each_entry_safe(buffer, _buffer, |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 622 | &iio_dev_opaque->buffer_list, buffer_list) |
Lars-Peter Clausen | 1250186 | 2015-05-18 13:34:49 +0200 | [diff] [blame] | 623 | iio_buffer_deactivate(buffer); |
| 624 | } |
| 625 | |
Lars-Peter Clausen | e18a2ad | 2015-10-13 18:10:27 +0200 | [diff] [blame] | 626 | static int iio_buffer_enable(struct iio_buffer *buffer, |
| 627 | struct iio_dev *indio_dev) |
| 628 | { |
| 629 | if (!buffer->access->enable) |
| 630 | return 0; |
| 631 | return buffer->access->enable(buffer, indio_dev); |
| 632 | } |
| 633 | |
| 634 | static int iio_buffer_disable(struct iio_buffer *buffer, |
| 635 | struct iio_dev *indio_dev) |
| 636 | { |
| 637 | if (!buffer->access->disable) |
| 638 | return 0; |
| 639 | return buffer->access->disable(buffer, indio_dev); |
| 640 | } |
| 641 | |
Lars-Peter Clausen | 8e05099 | 2013-10-14 17:49:00 +0100 | [diff] [blame] | 642 | static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev, |
| 643 | struct iio_buffer *buffer) |
| 644 | { |
| 645 | unsigned int bytes; |
| 646 | |
| 647 | if (!buffer->access->set_bytes_per_datum) |
| 648 | return; |
| 649 | |
| 650 | bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask, |
| 651 | buffer->scan_timestamp); |
| 652 | |
| 653 | buffer->access->set_bytes_per_datum(buffer, bytes); |
| 654 | } |
| 655 | |
Lars-Peter Clausen | fcc1b2f | 2015-05-13 16:04:46 +0200 | [diff] [blame] | 656 | static int iio_buffer_request_update(struct iio_dev *indio_dev, |
| 657 | struct iio_buffer *buffer) |
| 658 | { |
| 659 | int ret; |
| 660 | |
| 661 | iio_buffer_update_bytes_per_datum(indio_dev, buffer); |
| 662 | if (buffer->access->request_update) { |
| 663 | ret = buffer->access->request_update(buffer); |
| 664 | if (ret) { |
| 665 | dev_dbg(&indio_dev->dev, |
| 666 | "Buffer not started: buffer parameter update failed (%d)\n", |
| 667 | ret); |
| 668 | return ret; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
Lars-Peter Clausen | 248be5a | 2015-05-13 16:04:45 +0200 | [diff] [blame] | 675 | static void iio_free_scan_mask(struct iio_dev *indio_dev, |
| 676 | const unsigned long *mask) |
| 677 | { |
| 678 | /* If the mask is dynamically allocated free it, otherwise do nothing */ |
| 679 | if (!indio_dev->available_scan_masks) |
Andy Shevchenko | 3862828 | 2019-03-04 10:55:40 +0200 | [diff] [blame] | 680 | bitmap_free(mask); |
Lars-Peter Clausen | 248be5a | 2015-05-13 16:04:45 +0200 | [diff] [blame] | 681 | } |
| 682 | |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 683 | struct iio_device_config { |
| 684 | unsigned int mode; |
Lars-Peter Clausen | f0566c0 | 2015-10-13 18:10:24 +0200 | [diff] [blame] | 685 | unsigned int watermark; |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 686 | const unsigned long *scan_mask; |
| 687 | unsigned int scan_bytes; |
| 688 | bool scan_timestamp; |
| 689 | }; |
| 690 | |
| 691 | static int iio_verify_update(struct iio_dev *indio_dev, |
| 692 | struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer, |
| 693 | struct iio_device_config *config) |
| 694 | { |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 695 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 696 | unsigned long *compound_mask; |
| 697 | const unsigned long *scan_mask; |
Lars-Peter Clausen | 1e1ec28 | 2015-05-29 18:14:22 +0200 | [diff] [blame] | 698 | bool strict_scanmask = false; |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 699 | struct iio_buffer *buffer; |
| 700 | bool scan_timestamp; |
Lars-Peter Clausen | 225d59a | 2015-05-29 18:14:21 +0200 | [diff] [blame] | 701 | unsigned int modes; |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 702 | |
Lars-Peter Clausen | b732924 | 2020-03-26 11:30:12 +0200 | [diff] [blame] | 703 | if (insert_buffer && |
| 704 | bitmap_empty(insert_buffer->scan_mask, indio_dev->masklength)) { |
| 705 | dev_dbg(&indio_dev->dev, |
| 706 | "At least one scan element must be enabled first\n"); |
| 707 | return -EINVAL; |
| 708 | } |
| 709 | |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 710 | memset(config, 0, sizeof(*config)); |
Irina Tirdea | 1bef2c1 | 2016-03-24 11:09:45 +0200 | [diff] [blame] | 711 | config->watermark = ~0; |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 712 | |
| 713 | /* |
| 714 | * If there is just one buffer and we are removing it there is nothing |
| 715 | * to verify. |
| 716 | */ |
| 717 | if (remove_buffer && !insert_buffer && |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 718 | list_is_singular(&iio_dev_opaque->buffer_list)) |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 719 | return 0; |
| 720 | |
Lars-Peter Clausen | 225d59a | 2015-05-29 18:14:21 +0200 | [diff] [blame] | 721 | modes = indio_dev->modes; |
| 722 | |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 723 | list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) { |
Lars-Peter Clausen | 225d59a | 2015-05-29 18:14:21 +0200 | [diff] [blame] | 724 | if (buffer == remove_buffer) |
| 725 | continue; |
| 726 | modes &= buffer->access->modes; |
Lars-Peter Clausen | f0566c0 | 2015-10-13 18:10:24 +0200 | [diff] [blame] | 727 | config->watermark = min(config->watermark, buffer->watermark); |
Lars-Peter Clausen | 225d59a | 2015-05-29 18:14:21 +0200 | [diff] [blame] | 728 | } |
| 729 | |
Lars-Peter Clausen | f0566c0 | 2015-10-13 18:10:24 +0200 | [diff] [blame] | 730 | if (insert_buffer) { |
Lars-Peter Clausen | 225d59a | 2015-05-29 18:14:21 +0200 | [diff] [blame] | 731 | modes &= insert_buffer->access->modes; |
Lars-Peter Clausen | f0566c0 | 2015-10-13 18:10:24 +0200 | [diff] [blame] | 732 | config->watermark = min(config->watermark, |
| 733 | insert_buffer->watermark); |
| 734 | } |
Lars-Peter Clausen | 225d59a | 2015-05-29 18:14:21 +0200 | [diff] [blame] | 735 | |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 736 | /* Definitely possible for devices to support both of these. */ |
Lars-Peter Clausen | 225d59a | 2015-05-29 18:14:21 +0200 | [diff] [blame] | 737 | if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) { |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 738 | config->mode = INDIO_BUFFER_TRIGGERED; |
Lars-Peter Clausen | 225d59a | 2015-05-29 18:14:21 +0200 | [diff] [blame] | 739 | } else if (modes & INDIO_BUFFER_HARDWARE) { |
Lars-Peter Clausen | 1e1ec28 | 2015-05-29 18:14:22 +0200 | [diff] [blame] | 740 | /* |
| 741 | * Keep things simple for now and only allow a single buffer to |
| 742 | * be connected in hardware mode. |
| 743 | */ |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 744 | if (insert_buffer && !list_empty(&iio_dev_opaque->buffer_list)) |
Lars-Peter Clausen | 1e1ec28 | 2015-05-29 18:14:22 +0200 | [diff] [blame] | 745 | return -EINVAL; |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 746 | config->mode = INDIO_BUFFER_HARDWARE; |
Lars-Peter Clausen | 1e1ec28 | 2015-05-29 18:14:22 +0200 | [diff] [blame] | 747 | strict_scanmask = true; |
Lars-Peter Clausen | 225d59a | 2015-05-29 18:14:21 +0200 | [diff] [blame] | 748 | } else if (modes & INDIO_BUFFER_SOFTWARE) { |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 749 | config->mode = INDIO_BUFFER_SOFTWARE; |
| 750 | } else { |
| 751 | /* Can only occur on first buffer */ |
| 752 | if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) |
| 753 | dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n"); |
| 754 | return -EINVAL; |
| 755 | } |
| 756 | |
| 757 | /* What scan mask do we actually have? */ |
Andy Shevchenko | 3862828 | 2019-03-04 10:55:40 +0200 | [diff] [blame] | 758 | compound_mask = bitmap_zalloc(indio_dev->masklength, GFP_KERNEL); |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 759 | if (compound_mask == NULL) |
| 760 | return -ENOMEM; |
| 761 | |
| 762 | scan_timestamp = false; |
| 763 | |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 764 | list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) { |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 765 | if (buffer == remove_buffer) |
| 766 | continue; |
| 767 | bitmap_or(compound_mask, compound_mask, buffer->scan_mask, |
| 768 | indio_dev->masklength); |
| 769 | scan_timestamp |= buffer->scan_timestamp; |
| 770 | } |
| 771 | |
| 772 | if (insert_buffer) { |
| 773 | bitmap_or(compound_mask, compound_mask, |
| 774 | insert_buffer->scan_mask, indio_dev->masklength); |
| 775 | scan_timestamp |= insert_buffer->scan_timestamp; |
| 776 | } |
| 777 | |
| 778 | if (indio_dev->available_scan_masks) { |
| 779 | scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks, |
| 780 | indio_dev->masklength, |
Lars-Peter Clausen | 1e1ec28 | 2015-05-29 18:14:22 +0200 | [diff] [blame] | 781 | compound_mask, |
| 782 | strict_scanmask); |
Andy Shevchenko | 3862828 | 2019-03-04 10:55:40 +0200 | [diff] [blame] | 783 | bitmap_free(compound_mask); |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 784 | if (scan_mask == NULL) |
| 785 | return -EINVAL; |
| 786 | } else { |
| 787 | scan_mask = compound_mask; |
| 788 | } |
| 789 | |
| 790 | config->scan_bytes = iio_compute_scan_bytes(indio_dev, |
| 791 | scan_mask, scan_timestamp); |
| 792 | config->scan_mask = scan_mask; |
| 793 | config->scan_timestamp = scan_timestamp; |
| 794 | |
| 795 | return 0; |
| 796 | } |
| 797 | |
Jonathan Cameron | 78c9981 | 2017-01-02 19:28:24 +0000 | [diff] [blame] | 798 | /** |
| 799 | * struct iio_demux_table - table describing demux memcpy ops |
| 800 | * @from: index to copy from |
| 801 | * @to: index to copy to |
| 802 | * @length: how many bytes to copy |
| 803 | * @l: list head used for management |
| 804 | */ |
| 805 | struct iio_demux_table { |
| 806 | unsigned from; |
| 807 | unsigned to; |
| 808 | unsigned length; |
| 809 | struct list_head l; |
| 810 | }; |
| 811 | |
| 812 | static void iio_buffer_demux_free(struct iio_buffer *buffer) |
| 813 | { |
| 814 | struct iio_demux_table *p, *q; |
| 815 | list_for_each_entry_safe(p, q, &buffer->demux_list, l) { |
| 816 | list_del(&p->l); |
| 817 | kfree(p); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | static int iio_buffer_add_demux(struct iio_buffer *buffer, |
| 822 | struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc, |
| 823 | unsigned int length) |
| 824 | { |
| 825 | |
| 826 | if (*p && (*p)->from + (*p)->length == in_loc && |
| 827 | (*p)->to + (*p)->length == out_loc) { |
| 828 | (*p)->length += length; |
| 829 | } else { |
| 830 | *p = kmalloc(sizeof(**p), GFP_KERNEL); |
| 831 | if (*p == NULL) |
| 832 | return -ENOMEM; |
| 833 | (*p)->from = in_loc; |
| 834 | (*p)->to = out_loc; |
| 835 | (*p)->length = length; |
| 836 | list_add_tail(&(*p)->l, &buffer->demux_list); |
| 837 | } |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | static int iio_buffer_update_demux(struct iio_dev *indio_dev, |
| 843 | struct iio_buffer *buffer) |
| 844 | { |
| 845 | int ret, in_ind = -1, out_ind, length; |
| 846 | unsigned in_loc = 0, out_loc = 0; |
| 847 | struct iio_demux_table *p = NULL; |
| 848 | |
| 849 | /* Clear out any old demux */ |
| 850 | iio_buffer_demux_free(buffer); |
| 851 | kfree(buffer->demux_bounce); |
| 852 | buffer->demux_bounce = NULL; |
| 853 | |
| 854 | /* First work out which scan mode we will actually have */ |
| 855 | if (bitmap_equal(indio_dev->active_scan_mask, |
| 856 | buffer->scan_mask, |
| 857 | indio_dev->masklength)) |
| 858 | return 0; |
| 859 | |
| 860 | /* Now we have the two masks, work from least sig and build up sizes */ |
| 861 | for_each_set_bit(out_ind, |
| 862 | buffer->scan_mask, |
| 863 | indio_dev->masklength) { |
| 864 | in_ind = find_next_bit(indio_dev->active_scan_mask, |
| 865 | indio_dev->masklength, |
| 866 | in_ind + 1); |
| 867 | while (in_ind != out_ind) { |
| 868 | in_ind = find_next_bit(indio_dev->active_scan_mask, |
| 869 | indio_dev->masklength, |
| 870 | in_ind + 1); |
| 871 | length = iio_storage_bytes_for_si(indio_dev, in_ind); |
| 872 | /* Make sure we are aligned */ |
| 873 | in_loc = roundup(in_loc, length) + length; |
| 874 | } |
| 875 | length = iio_storage_bytes_for_si(indio_dev, in_ind); |
| 876 | out_loc = roundup(out_loc, length); |
| 877 | in_loc = roundup(in_loc, length); |
| 878 | ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); |
| 879 | if (ret) |
| 880 | goto error_clear_mux_table; |
| 881 | out_loc += length; |
| 882 | in_loc += length; |
| 883 | } |
| 884 | /* Relies on scan_timestamp being last */ |
| 885 | if (buffer->scan_timestamp) { |
| 886 | length = iio_storage_bytes_for_timestamp(indio_dev); |
| 887 | out_loc = roundup(out_loc, length); |
| 888 | in_loc = roundup(in_loc, length); |
| 889 | ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); |
| 890 | if (ret) |
| 891 | goto error_clear_mux_table; |
| 892 | out_loc += length; |
| 893 | in_loc += length; |
| 894 | } |
| 895 | buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL); |
| 896 | if (buffer->demux_bounce == NULL) { |
| 897 | ret = -ENOMEM; |
| 898 | goto error_clear_mux_table; |
| 899 | } |
| 900 | return 0; |
| 901 | |
| 902 | error_clear_mux_table: |
| 903 | iio_buffer_demux_free(buffer); |
| 904 | |
| 905 | return ret; |
| 906 | } |
| 907 | |
| 908 | static int iio_update_demux(struct iio_dev *indio_dev) |
| 909 | { |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 910 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); |
Jonathan Cameron | 78c9981 | 2017-01-02 19:28:24 +0000 | [diff] [blame] | 911 | struct iio_buffer *buffer; |
| 912 | int ret; |
| 913 | |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 914 | list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) { |
Jonathan Cameron | 78c9981 | 2017-01-02 19:28:24 +0000 | [diff] [blame] | 915 | ret = iio_buffer_update_demux(indio_dev, buffer); |
| 916 | if (ret < 0) |
| 917 | goto error_clear_mux_table; |
| 918 | } |
| 919 | return 0; |
| 920 | |
| 921 | error_clear_mux_table: |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 922 | list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) |
Jonathan Cameron | 78c9981 | 2017-01-02 19:28:24 +0000 | [diff] [blame] | 923 | iio_buffer_demux_free(buffer); |
| 924 | |
| 925 | return ret; |
| 926 | } |
| 927 | |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 928 | static int iio_enable_buffers(struct iio_dev *indio_dev, |
| 929 | struct iio_device_config *config) |
| 930 | { |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 931 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); |
Lars-Peter Clausen | e18a2ad | 2015-10-13 18:10:27 +0200 | [diff] [blame] | 932 | struct iio_buffer *buffer; |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 933 | int ret; |
| 934 | |
| 935 | indio_dev->active_scan_mask = config->scan_mask; |
| 936 | indio_dev->scan_timestamp = config->scan_timestamp; |
| 937 | indio_dev->scan_bytes = config->scan_bytes; |
Lars-Peter Clausen | 5cb1a54 | 2020-04-30 11:24:55 +0300 | [diff] [blame] | 938 | indio_dev->currentmode = config->mode; |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 939 | |
| 940 | iio_update_demux(indio_dev); |
| 941 | |
| 942 | /* Wind up again */ |
| 943 | if (indio_dev->setup_ops->preenable) { |
| 944 | ret = indio_dev->setup_ops->preenable(indio_dev); |
| 945 | if (ret) { |
| 946 | dev_dbg(&indio_dev->dev, |
| 947 | "Buffer not started: buffer preenable failed (%d)\n", ret); |
| 948 | goto err_undo_config; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | if (indio_dev->info->update_scan_mode) { |
| 953 | ret = indio_dev->info |
| 954 | ->update_scan_mode(indio_dev, |
| 955 | indio_dev->active_scan_mask); |
| 956 | if (ret < 0) { |
| 957 | dev_dbg(&indio_dev->dev, |
| 958 | "Buffer not started: update scan mode failed (%d)\n", |
| 959 | ret); |
| 960 | goto err_run_postdisable; |
| 961 | } |
| 962 | } |
| 963 | |
Lars-Peter Clausen | f0566c0 | 2015-10-13 18:10:24 +0200 | [diff] [blame] | 964 | if (indio_dev->info->hwfifo_set_watermark) |
| 965 | indio_dev->info->hwfifo_set_watermark(indio_dev, |
| 966 | config->watermark); |
| 967 | |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 968 | list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) { |
Lars-Peter Clausen | e18a2ad | 2015-10-13 18:10:27 +0200 | [diff] [blame] | 969 | ret = iio_buffer_enable(buffer, indio_dev); |
| 970 | if (ret) |
| 971 | goto err_disable_buffers; |
| 972 | } |
| 973 | |
Lars-Peter Clausen | f11d59d | 2020-05-25 14:38:53 +0300 | [diff] [blame] | 974 | if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) { |
| 975 | ret = iio_trigger_attach_poll_func(indio_dev->trig, |
| 976 | indio_dev->pollfunc); |
| 977 | if (ret) |
| 978 | goto err_disable_buffers; |
| 979 | } |
| 980 | |
Alexandru Ardelean | 62a30a2 | 2020-07-15 07:16:29 +0300 | [diff] [blame] | 981 | if (indio_dev->setup_ops->postenable) { |
| 982 | ret = indio_dev->setup_ops->postenable(indio_dev); |
| 983 | if (ret) { |
| 984 | dev_dbg(&indio_dev->dev, |
| 985 | "Buffer not started: postenable failed (%d)\n", ret); |
| 986 | goto err_detach_pollfunc; |
| 987 | } |
| 988 | } |
| 989 | |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 990 | return 0; |
| 991 | |
Alexandru Ardelean | 62a30a2 | 2020-07-15 07:16:29 +0300 | [diff] [blame] | 992 | err_detach_pollfunc: |
| 993 | if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) { |
| 994 | iio_trigger_detach_poll_func(indio_dev->trig, |
| 995 | indio_dev->pollfunc); |
| 996 | } |
Lars-Peter Clausen | e18a2ad | 2015-10-13 18:10:27 +0200 | [diff] [blame] | 997 | err_disable_buffers: |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 998 | list_for_each_entry_continue_reverse(buffer, &iio_dev_opaque->buffer_list, |
Lars-Peter Clausen | e18a2ad | 2015-10-13 18:10:27 +0200 | [diff] [blame] | 999 | buffer_list) |
| 1000 | iio_buffer_disable(buffer, indio_dev); |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1001 | err_run_postdisable: |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1002 | if (indio_dev->setup_ops->postdisable) |
| 1003 | indio_dev->setup_ops->postdisable(indio_dev); |
| 1004 | err_undo_config: |
Lars-Peter Clausen | 5cb1a54 | 2020-04-30 11:24:55 +0300 | [diff] [blame] | 1005 | indio_dev->currentmode = INDIO_DIRECT_MODE; |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1006 | indio_dev->active_scan_mask = NULL; |
| 1007 | |
| 1008 | return ret; |
| 1009 | } |
| 1010 | |
| 1011 | static int iio_disable_buffers(struct iio_dev *indio_dev) |
| 1012 | { |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 1013 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); |
Lars-Peter Clausen | e18a2ad | 2015-10-13 18:10:27 +0200 | [diff] [blame] | 1014 | struct iio_buffer *buffer; |
Lars-Peter Clausen | 1250186 | 2015-05-18 13:34:49 +0200 | [diff] [blame] | 1015 | int ret = 0; |
| 1016 | int ret2; |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1017 | |
| 1018 | /* Wind down existing buffers - iff there are any */ |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 1019 | if (list_empty(&iio_dev_opaque->buffer_list)) |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1020 | return 0; |
| 1021 | |
Lars-Peter Clausen | 1250186 | 2015-05-18 13:34:49 +0200 | [diff] [blame] | 1022 | /* |
| 1023 | * If things go wrong at some step in disable we still need to continue |
| 1024 | * to perform the other steps, otherwise we leave the device in a |
| 1025 | * inconsistent state. We return the error code for the first error we |
| 1026 | * encountered. |
| 1027 | */ |
| 1028 | |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1029 | if (indio_dev->setup_ops->predisable) { |
Lars-Peter Clausen | 1250186 | 2015-05-18 13:34:49 +0200 | [diff] [blame] | 1030 | ret2 = indio_dev->setup_ops->predisable(indio_dev); |
| 1031 | if (ret2 && !ret) |
| 1032 | ret = ret2; |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1033 | } |
| 1034 | |
Alexandru Ardelean | 62a30a2 | 2020-07-15 07:16:29 +0300 | [diff] [blame] | 1035 | if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) { |
| 1036 | iio_trigger_detach_poll_func(indio_dev->trig, |
| 1037 | indio_dev->pollfunc); |
| 1038 | } |
| 1039 | |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 1040 | list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) { |
Lars-Peter Clausen | e18a2ad | 2015-10-13 18:10:27 +0200 | [diff] [blame] | 1041 | ret2 = iio_buffer_disable(buffer, indio_dev); |
| 1042 | if (ret2 && !ret) |
| 1043 | ret = ret2; |
| 1044 | } |
| 1045 | |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1046 | if (indio_dev->setup_ops->postdisable) { |
Lars-Peter Clausen | 1250186 | 2015-05-18 13:34:49 +0200 | [diff] [blame] | 1047 | ret2 = indio_dev->setup_ops->postdisable(indio_dev); |
| 1048 | if (ret2 && !ret) |
| 1049 | ret = ret2; |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1050 | } |
| 1051 | |
Lars-Peter Clausen | 1250186 | 2015-05-18 13:34:49 +0200 | [diff] [blame] | 1052 | iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask); |
| 1053 | indio_dev->active_scan_mask = NULL; |
Lars-Peter Clausen | 5cb1a54 | 2020-04-30 11:24:55 +0300 | [diff] [blame] | 1054 | indio_dev->currentmode = INDIO_DIRECT_MODE; |
Lars-Peter Clausen | 1250186 | 2015-05-18 13:34:49 +0200 | [diff] [blame] | 1055 | |
| 1056 | return ret; |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
Lars-Peter Clausen | a951945 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 1059 | static int __iio_update_buffers(struct iio_dev *indio_dev, |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1060 | struct iio_buffer *insert_buffer, |
| 1061 | struct iio_buffer *remove_buffer) |
Jonathan Cameron | 6b3b58e | 2012-04-21 10:09:33 +0100 | [diff] [blame] | 1062 | { |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 1063 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 1064 | struct iio_device_config new_config; |
Lars-Peter Clausen | 1250186 | 2015-05-18 13:34:49 +0200 | [diff] [blame] | 1065 | int ret; |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 1066 | |
| 1067 | ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer, |
| 1068 | &new_config); |
| 1069 | if (ret) |
| 1070 | return ret; |
Jonathan Cameron | 959d295 | 2011-12-05 21:37:13 +0000 | [diff] [blame] | 1071 | |
Lars-Peter Clausen | fcc1b2f | 2015-05-13 16:04:46 +0200 | [diff] [blame] | 1072 | if (insert_buffer) { |
| 1073 | ret = iio_buffer_request_update(indio_dev, insert_buffer); |
| 1074 | if (ret) |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 1075 | goto err_free_config; |
Lars-Peter Clausen | fcc1b2f | 2015-05-13 16:04:46 +0200 | [diff] [blame] | 1076 | } |
| 1077 | |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1078 | ret = iio_disable_buffers(indio_dev); |
Lars-Peter Clausen | 1250186 | 2015-05-18 13:34:49 +0200 | [diff] [blame] | 1079 | if (ret) |
| 1080 | goto err_deactivate_all; |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1081 | |
| 1082 | if (remove_buffer) |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 1083 | iio_buffer_deactivate(remove_buffer); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1084 | if (insert_buffer) |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 1085 | iio_buffer_activate(indio_dev, insert_buffer); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1086 | |
| 1087 | /* If no buffers in list, we are done */ |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 1088 | if (list_empty(&iio_dev_opaque->buffer_list)) |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1089 | return 0; |
Jonathan Cameron | 959d295 | 2011-12-05 21:37:13 +0000 | [diff] [blame] | 1090 | |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1091 | ret = iio_enable_buffers(indio_dev, &new_config); |
Lars-Peter Clausen | 1250186 | 2015-05-18 13:34:49 +0200 | [diff] [blame] | 1092 | if (ret) |
| 1093 | goto err_deactivate_all; |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1094 | |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 1095 | return 0; |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1096 | |
Lars-Peter Clausen | 1250186 | 2015-05-18 13:34:49 +0200 | [diff] [blame] | 1097 | err_deactivate_all: |
| 1098 | /* |
| 1099 | * We've already verified that the config is valid earlier. If things go |
| 1100 | * wrong in either enable or disable the most likely reason is an IO |
| 1101 | * error from the device. In this case there is no good recovery |
| 1102 | * strategy. Just make sure to disable everything and leave the device |
| 1103 | * in a sane state. With a bit of luck the device might come back to |
| 1104 | * life again later and userspace can try again. |
| 1105 | */ |
| 1106 | iio_buffer_deactivate_all(indio_dev); |
| 1107 | |
Lars-Peter Clausen | 6e509c4 | 2015-05-18 13:34:47 +0200 | [diff] [blame] | 1108 | err_free_config: |
| 1109 | iio_free_scan_mask(indio_dev, new_config.scan_mask); |
| 1110 | return ret; |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1111 | } |
Lars-Peter Clausen | a951945 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 1112 | |
| 1113 | int iio_update_buffers(struct iio_dev *indio_dev, |
| 1114 | struct iio_buffer *insert_buffer, |
| 1115 | struct iio_buffer *remove_buffer) |
| 1116 | { |
| 1117 | int ret; |
| 1118 | |
Lars-Peter Clausen | 3909fab | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 1119 | if (insert_buffer == remove_buffer) |
| 1120 | return 0; |
| 1121 | |
Lars-Peter Clausen | a951945 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 1122 | mutex_lock(&indio_dev->info_exist_lock); |
| 1123 | mutex_lock(&indio_dev->mlock); |
| 1124 | |
Lars-Peter Clausen | 3909fab | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 1125 | if (insert_buffer && iio_buffer_is_active(insert_buffer)) |
| 1126 | insert_buffer = NULL; |
| 1127 | |
| 1128 | if (remove_buffer && !iio_buffer_is_active(remove_buffer)) |
| 1129 | remove_buffer = NULL; |
| 1130 | |
| 1131 | if (!insert_buffer && !remove_buffer) { |
| 1132 | ret = 0; |
| 1133 | goto out_unlock; |
| 1134 | } |
| 1135 | |
Lars-Peter Clausen | a951945 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 1136 | if (indio_dev->info == NULL) { |
| 1137 | ret = -ENODEV; |
| 1138 | goto out_unlock; |
| 1139 | } |
| 1140 | |
| 1141 | ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer); |
| 1142 | |
| 1143 | out_unlock: |
| 1144 | mutex_unlock(&indio_dev->mlock); |
| 1145 | mutex_unlock(&indio_dev->info_exist_lock); |
| 1146 | |
| 1147 | return ret; |
| 1148 | } |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1149 | EXPORT_SYMBOL_GPL(iio_update_buffers); |
| 1150 | |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1151 | void iio_disable_all_buffers(struct iio_dev *indio_dev) |
| 1152 | { |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1153 | iio_disable_buffers(indio_dev); |
Lars-Peter Clausen | 1250186 | 2015-05-18 13:34:49 +0200 | [diff] [blame] | 1154 | iio_buffer_deactivate_all(indio_dev); |
Lars-Peter Clausen | 623d74e | 2015-05-18 13:34:48 +0200 | [diff] [blame] | 1155 | } |
| 1156 | |
Lars-Peter Clausen | 08e7e0a | 2014-11-26 18:55:15 +0100 | [diff] [blame] | 1157 | static ssize_t iio_buffer_store_enable(struct device *dev, |
| 1158 | struct device_attribute *attr, |
| 1159 | const char *buf, |
| 1160 | size_t len) |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1161 | { |
| 1162 | int ret; |
| 1163 | bool requested_state; |
| 1164 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 1165 | struct iio_buffer *buffer = indio_dev->buffer; |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1166 | bool inlist; |
| 1167 | |
| 1168 | ret = strtobool(buf, &requested_state); |
| 1169 | if (ret < 0) |
| 1170 | return ret; |
| 1171 | |
| 1172 | mutex_lock(&indio_dev->mlock); |
| 1173 | |
| 1174 | /* Find out if it is in the list */ |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 1175 | inlist = iio_buffer_is_active(buffer); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1176 | /* Already in desired state */ |
| 1177 | if (inlist == requested_state) |
| 1178 | goto done; |
| 1179 | |
| 1180 | if (requested_state) |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 1181 | ret = __iio_update_buffers(indio_dev, buffer, NULL); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1182 | else |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 1183 | ret = __iio_update_buffers(indio_dev, NULL, buffer); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1184 | |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1185 | done: |
| 1186 | mutex_unlock(&indio_dev->mlock); |
| 1187 | return (ret < 0) ? ret : len; |
| 1188 | } |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1189 | |
Lars-Peter Clausen | d967cb6 | 2014-11-26 18:55:14 +0100 | [diff] [blame] | 1190 | static const char * const iio_scan_elements_group_name = "scan_elements"; |
| 1191 | |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 1192 | static ssize_t iio_buffer_show_watermark(struct device *dev, |
| 1193 | struct device_attribute *attr, |
| 1194 | char *buf) |
| 1195 | { |
| 1196 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 1197 | struct iio_buffer *buffer = indio_dev->buffer; |
| 1198 | |
| 1199 | return sprintf(buf, "%u\n", buffer->watermark); |
| 1200 | } |
| 1201 | |
| 1202 | static ssize_t iio_buffer_store_watermark(struct device *dev, |
| 1203 | struct device_attribute *attr, |
| 1204 | const char *buf, |
| 1205 | size_t len) |
| 1206 | { |
| 1207 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 1208 | struct iio_buffer *buffer = indio_dev->buffer; |
| 1209 | unsigned int val; |
| 1210 | int ret; |
| 1211 | |
| 1212 | ret = kstrtouint(buf, 10, &val); |
| 1213 | if (ret) |
| 1214 | return ret; |
| 1215 | if (!val) |
| 1216 | return -EINVAL; |
| 1217 | |
| 1218 | mutex_lock(&indio_dev->mlock); |
| 1219 | |
| 1220 | if (val > buffer->length) { |
| 1221 | ret = -EINVAL; |
| 1222 | goto out; |
| 1223 | } |
| 1224 | |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 1225 | if (iio_buffer_is_active(buffer)) { |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 1226 | ret = -EBUSY; |
| 1227 | goto out; |
| 1228 | } |
| 1229 | |
| 1230 | buffer->watermark = val; |
| 1231 | out: |
| 1232 | mutex_unlock(&indio_dev->mlock); |
| 1233 | |
| 1234 | return ret ? ret : len; |
| 1235 | } |
| 1236 | |
Matt Fornero | 350f6c7 | 2017-12-06 14:43:30 -0500 | [diff] [blame] | 1237 | static ssize_t iio_dma_show_data_available(struct device *dev, |
| 1238 | struct device_attribute *attr, |
| 1239 | char *buf) |
| 1240 | { |
| 1241 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 1242 | struct iio_buffer *buffer = indio_dev->buffer; |
Matt Fornero | 350f6c7 | 2017-12-06 14:43:30 -0500 | [diff] [blame] | 1243 | |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 1244 | return sprintf(buf, "%zu\n", iio_buffer_data_available(buffer)); |
Matt Fornero | 350f6c7 | 2017-12-06 14:43:30 -0500 | [diff] [blame] | 1245 | } |
| 1246 | |
Lars-Peter Clausen | 08e7e0a | 2014-11-26 18:55:15 +0100 | [diff] [blame] | 1247 | static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length, |
| 1248 | iio_buffer_write_length); |
Lars-Peter Clausen | 8d92db2 | 2014-11-26 18:55:16 +0100 | [diff] [blame] | 1249 | static struct device_attribute dev_attr_length_ro = __ATTR(length, |
| 1250 | S_IRUGO, iio_buffer_read_length, NULL); |
Lars-Peter Clausen | 08e7e0a | 2014-11-26 18:55:15 +0100 | [diff] [blame] | 1251 | static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, |
| 1252 | iio_buffer_show_enable, iio_buffer_store_enable); |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 1253 | static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR, |
| 1254 | iio_buffer_show_watermark, iio_buffer_store_watermark); |
Lars-Peter Clausen | b440655 | 2015-10-13 18:10:26 +0200 | [diff] [blame] | 1255 | static struct device_attribute dev_attr_watermark_ro = __ATTR(watermark, |
| 1256 | S_IRUGO, iio_buffer_show_watermark, NULL); |
Matt Fornero | 350f6c7 | 2017-12-06 14:43:30 -0500 | [diff] [blame] | 1257 | static DEVICE_ATTR(data_available, S_IRUGO, |
| 1258 | iio_dma_show_data_available, NULL); |
Lars-Peter Clausen | 08e7e0a | 2014-11-26 18:55:15 +0100 | [diff] [blame] | 1259 | |
Octavian Purdila | 6da9b38 | 2015-01-31 02:00:00 +0200 | [diff] [blame] | 1260 | static struct attribute *iio_buffer_attrs[] = { |
| 1261 | &dev_attr_length.attr, |
| 1262 | &dev_attr_enable.attr, |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 1263 | &dev_attr_watermark.attr, |
Matt Fornero | 350f6c7 | 2017-12-06 14:43:30 -0500 | [diff] [blame] | 1264 | &dev_attr_data_available.attr, |
Octavian Purdila | 6da9b38 | 2015-01-31 02:00:00 +0200 | [diff] [blame] | 1265 | }; |
| 1266 | |
Alexandru Ardelean | e16e0a77 | 2020-09-17 15:59:51 +0300 | [diff] [blame] | 1267 | static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer, |
| 1268 | struct iio_dev *indio_dev) |
Lars-Peter Clausen | d967cb6 | 2014-11-26 18:55:14 +0100 | [diff] [blame] | 1269 | { |
| 1270 | struct iio_dev_attr *p; |
| 1271 | struct attribute **attr; |
Alexandru Ardelean | 96144d4 | 2020-05-11 15:53:22 +0300 | [diff] [blame] | 1272 | int ret, i, attrn, attrcount; |
Lars-Peter Clausen | d967cb6 | 2014-11-26 18:55:14 +0100 | [diff] [blame] | 1273 | const struct iio_chan_spec *channels; |
| 1274 | |
Lars-Peter Clausen | 08e7e0a | 2014-11-26 18:55:15 +0100 | [diff] [blame] | 1275 | attrcount = 0; |
| 1276 | if (buffer->attrs) { |
| 1277 | while (buffer->attrs[attrcount] != NULL) |
| 1278 | attrcount++; |
| 1279 | } |
| 1280 | |
Octavian Purdila | 6da9b38 | 2015-01-31 02:00:00 +0200 | [diff] [blame] | 1281 | attr = kcalloc(attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1, |
| 1282 | sizeof(struct attribute *), GFP_KERNEL); |
| 1283 | if (!attr) |
Lars-Peter Clausen | 08e7e0a | 2014-11-26 18:55:15 +0100 | [diff] [blame] | 1284 | return -ENOMEM; |
| 1285 | |
Octavian Purdila | 6da9b38 | 2015-01-31 02:00:00 +0200 | [diff] [blame] | 1286 | memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs)); |
| 1287 | if (!buffer->access->set_length) |
| 1288 | attr[0] = &dev_attr_length_ro.attr; |
| 1289 | |
Lars-Peter Clausen | b440655 | 2015-10-13 18:10:26 +0200 | [diff] [blame] | 1290 | if (buffer->access->flags & INDIO_BUFFER_FLAG_FIXED_WATERMARK) |
| 1291 | attr[2] = &dev_attr_watermark_ro.attr; |
| 1292 | |
Lars-Peter Clausen | 08e7e0a | 2014-11-26 18:55:15 +0100 | [diff] [blame] | 1293 | if (buffer->attrs) |
Octavian Purdila | 6da9b38 | 2015-01-31 02:00:00 +0200 | [diff] [blame] | 1294 | memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs, |
| 1295 | sizeof(struct attribute *) * attrcount); |
| 1296 | |
| 1297 | attr[attrcount + ARRAY_SIZE(iio_buffer_attrs)] = NULL; |
| 1298 | |
| 1299 | buffer->buffer_group.name = "buffer"; |
| 1300 | buffer->buffer_group.attrs = attr; |
Lars-Peter Clausen | 08e7e0a | 2014-11-26 18:55:15 +0100 | [diff] [blame] | 1301 | |
| 1302 | indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group; |
| 1303 | |
Alexandru Ardelean | 96144d4 | 2020-05-11 15:53:22 +0300 | [diff] [blame] | 1304 | attrcount = 0; |
Lars-Peter Clausen | d967cb6 | 2014-11-26 18:55:14 +0100 | [diff] [blame] | 1305 | INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list); |
| 1306 | channels = indio_dev->channels; |
| 1307 | if (channels) { |
| 1308 | /* new magic */ |
| 1309 | for (i = 0; i < indio_dev->num_channels; i++) { |
| 1310 | if (channels[i].scan_index < 0) |
| 1311 | continue; |
| 1312 | |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 1313 | ret = iio_buffer_add_channel_sysfs(indio_dev, buffer, |
Lars-Peter Clausen | d967cb6 | 2014-11-26 18:55:14 +0100 | [diff] [blame] | 1314 | &channels[i]); |
| 1315 | if (ret < 0) |
| 1316 | goto error_cleanup_dynamic; |
| 1317 | attrcount += ret; |
| 1318 | if (channels[i].type == IIO_TIMESTAMP) |
| 1319 | indio_dev->scan_index_timestamp = |
| 1320 | channels[i].scan_index; |
| 1321 | } |
| 1322 | if (indio_dev->masklength && buffer->scan_mask == NULL) { |
Andy Shevchenko | 3862828 | 2019-03-04 10:55:40 +0200 | [diff] [blame] | 1323 | buffer->scan_mask = bitmap_zalloc(indio_dev->masklength, |
| 1324 | GFP_KERNEL); |
Lars-Peter Clausen | d967cb6 | 2014-11-26 18:55:14 +0100 | [diff] [blame] | 1325 | if (buffer->scan_mask == NULL) { |
| 1326 | ret = -ENOMEM; |
| 1327 | goto error_cleanup_dynamic; |
| 1328 | } |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | buffer->scan_el_group.name = iio_scan_elements_group_name; |
| 1333 | |
| 1334 | buffer->scan_el_group.attrs = kcalloc(attrcount + 1, |
| 1335 | sizeof(buffer->scan_el_group.attrs[0]), |
| 1336 | GFP_KERNEL); |
| 1337 | if (buffer->scan_el_group.attrs == NULL) { |
| 1338 | ret = -ENOMEM; |
| 1339 | goto error_free_scan_mask; |
| 1340 | } |
Alexandru Ardelean | 96144d4 | 2020-05-11 15:53:22 +0300 | [diff] [blame] | 1341 | attrn = 0; |
Lars-Peter Clausen | d967cb6 | 2014-11-26 18:55:14 +0100 | [diff] [blame] | 1342 | |
| 1343 | list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l) |
| 1344 | buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr; |
| 1345 | indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group; |
| 1346 | |
| 1347 | return 0; |
| 1348 | |
| 1349 | error_free_scan_mask: |
Andy Shevchenko | 3862828 | 2019-03-04 10:55:40 +0200 | [diff] [blame] | 1350 | bitmap_free(buffer->scan_mask); |
Lars-Peter Clausen | d967cb6 | 2014-11-26 18:55:14 +0100 | [diff] [blame] | 1351 | error_cleanup_dynamic: |
| 1352 | iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list); |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 1353 | kfree(buffer->buffer_group.attrs); |
Lars-Peter Clausen | d967cb6 | 2014-11-26 18:55:14 +0100 | [diff] [blame] | 1354 | |
| 1355 | return ret; |
| 1356 | } |
| 1357 | |
Alexandru Ardelean | e16e0a77 | 2020-09-17 15:59:51 +0300 | [diff] [blame] | 1358 | int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev) |
| 1359 | { |
| 1360 | struct iio_buffer *buffer = indio_dev->buffer; |
| 1361 | const struct iio_chan_spec *channels; |
| 1362 | int i; |
| 1363 | |
| 1364 | channels = indio_dev->channels; |
| 1365 | if (channels) { |
| 1366 | int ml = indio_dev->masklength; |
| 1367 | |
| 1368 | for (i = 0; i < indio_dev->num_channels; i++) |
| 1369 | ml = max(ml, channels[i].scan_index + 1); |
| 1370 | indio_dev->masklength = ml; |
| 1371 | } |
| 1372 | |
| 1373 | if (!buffer) |
| 1374 | return 0; |
| 1375 | |
| 1376 | return __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev); |
| 1377 | } |
| 1378 | |
| 1379 | static void __iio_buffer_free_sysfs_and_mask(struct iio_buffer *buffer) |
| 1380 | { |
| 1381 | bitmap_free(buffer->scan_mask); |
| 1382 | kfree(buffer->buffer_group.attrs); |
| 1383 | kfree(buffer->scan_el_group.attrs); |
| 1384 | iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list); |
| 1385 | } |
| 1386 | |
Lars-Peter Clausen | d967cb6 | 2014-11-26 18:55:14 +0100 | [diff] [blame] | 1387 | void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev) |
| 1388 | { |
Alexandru Ardelean | ff3f7e04 | 2020-04-24 18:22:43 +0300 | [diff] [blame] | 1389 | struct iio_buffer *buffer = indio_dev->buffer; |
| 1390 | |
| 1391 | if (!buffer) |
Lars-Peter Clausen | d967cb6 | 2014-11-26 18:55:14 +0100 | [diff] [blame] | 1392 | return; |
| 1393 | |
Alexandru Ardelean | e16e0a77 | 2020-09-17 15:59:51 +0300 | [diff] [blame] | 1394 | __iio_buffer_free_sysfs_and_mask(buffer); |
Lars-Peter Clausen | d967cb6 | 2014-11-26 18:55:14 +0100 | [diff] [blame] | 1395 | } |
| 1396 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 1397 | /** |
Lars-Peter Clausen | 8163663 | 2012-07-09 10:00:00 +0100 | [diff] [blame] | 1398 | * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected |
| 1399 | * @indio_dev: the iio device |
| 1400 | * @mask: scan mask to be checked |
| 1401 | * |
| 1402 | * Return true if exactly one bit is set in the scan mask, false otherwise. It |
| 1403 | * can be used for devices where only one channel can be active for sampling at |
| 1404 | * a time. |
| 1405 | */ |
| 1406 | bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev, |
| 1407 | const unsigned long *mask) |
| 1408 | { |
| 1409 | return bitmap_weight(mask, indio_dev->masklength) == 1; |
| 1410 | } |
| 1411 | EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot); |
| 1412 | |
Lars-Peter Clausen | 5d65d92 | 2013-09-15 17:50:00 +0100 | [diff] [blame] | 1413 | static const void *iio_demux(struct iio_buffer *buffer, |
| 1414 | const void *datain) |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 1415 | { |
| 1416 | struct iio_demux_table *t; |
| 1417 | |
| 1418 | if (list_empty(&buffer->demux_list)) |
| 1419 | return datain; |
| 1420 | list_for_each_entry(t, &buffer->demux_list, l) |
| 1421 | memcpy(buffer->demux_bounce + t->to, |
| 1422 | datain + t->from, t->length); |
| 1423 | |
| 1424 | return buffer->demux_bounce; |
| 1425 | } |
| 1426 | |
Lars-Peter Clausen | 5d65d92 | 2013-09-15 17:50:00 +0100 | [diff] [blame] | 1427 | static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data) |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 1428 | { |
Lars-Peter Clausen | 5d65d92 | 2013-09-15 17:50:00 +0100 | [diff] [blame] | 1429 | const void *dataout = iio_demux(buffer, data); |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 1430 | int ret; |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 1431 | |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 1432 | ret = buffer->access->store_to(buffer, dataout); |
| 1433 | if (ret) |
| 1434 | return ret; |
| 1435 | |
| 1436 | /* |
| 1437 | * We can't just test for watermark to decide if we wake the poll queue |
| 1438 | * because read may request less samples than the watermark. |
| 1439 | */ |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1440 | wake_up_interruptible_poll(&buffer->pollq, EPOLLIN | EPOLLRDNORM); |
Josselin Costanzi | 37d3455 | 2015-03-22 20:33:38 +0200 | [diff] [blame] | 1441 | return 0; |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 1442 | } |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 1443 | |
Jonathan Cameron | 315a19e | 2017-01-02 19:28:28 +0000 | [diff] [blame] | 1444 | /** |
| 1445 | * iio_push_to_buffers() - push to a registered buffer. |
| 1446 | * @indio_dev: iio_dev structure for device. |
| 1447 | * @data: Full scan. |
| 1448 | */ |
Lars-Peter Clausen | 5d65d92 | 2013-09-15 17:50:00 +0100 | [diff] [blame] | 1449 | int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1450 | { |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 1451 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1452 | int ret; |
| 1453 | struct iio_buffer *buf; |
| 1454 | |
Alexandru Ardelean | 6a8c6b2 | 2020-06-30 07:57:07 +0300 | [diff] [blame] | 1455 | list_for_each_entry(buf, &iio_dev_opaque->buffer_list, buffer_list) { |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1456 | ret = iio_push_to_buffer(buf, data); |
| 1457 | if (ret < 0) |
| 1458 | return ret; |
| 1459 | } |
| 1460 | |
| 1461 | return 0; |
| 1462 | } |
| 1463 | EXPORT_SYMBOL_GPL(iio_push_to_buffers); |
| 1464 | |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 1465 | /** |
| 1466 | * iio_buffer_release() - Free a buffer's resources |
| 1467 | * @ref: Pointer to the kref embedded in the iio_buffer struct |
| 1468 | * |
| 1469 | * This function is called when the last reference to the buffer has been |
| 1470 | * dropped. It will typically free all resources allocated by the buffer. Do not |
| 1471 | * call this function manually, always use iio_buffer_put() when done using a |
| 1472 | * buffer. |
| 1473 | */ |
| 1474 | static void iio_buffer_release(struct kref *ref) |
| 1475 | { |
| 1476 | struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref); |
| 1477 | |
| 1478 | buffer->access->release(buffer); |
| 1479 | } |
| 1480 | |
| 1481 | /** |
| 1482 | * iio_buffer_get() - Grab a reference to the buffer |
| 1483 | * @buffer: The buffer to grab a reference for, may be NULL |
| 1484 | * |
| 1485 | * Returns the pointer to the buffer that was passed into the function. |
| 1486 | */ |
| 1487 | struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer) |
| 1488 | { |
| 1489 | if (buffer) |
| 1490 | kref_get(&buffer->ref); |
| 1491 | |
| 1492 | return buffer; |
| 1493 | } |
| 1494 | EXPORT_SYMBOL_GPL(iio_buffer_get); |
| 1495 | |
| 1496 | /** |
| 1497 | * iio_buffer_put() - Release the reference to the buffer |
| 1498 | * @buffer: The buffer to release the reference for, may be NULL |
| 1499 | */ |
| 1500 | void iio_buffer_put(struct iio_buffer *buffer) |
| 1501 | { |
| 1502 | if (buffer) |
| 1503 | kref_put(&buffer->ref, iio_buffer_release); |
| 1504 | } |
| 1505 | EXPORT_SYMBOL_GPL(iio_buffer_put); |
Jonathan Cameron | 2b827ad | 2017-01-02 19:28:32 +0000 | [diff] [blame] | 1506 | |
| 1507 | /** |
| 1508 | * iio_device_attach_buffer - Attach a buffer to a IIO device |
| 1509 | * @indio_dev: The device the buffer should be attached to |
| 1510 | * @buffer: The buffer to attach to the device |
| 1511 | * |
| 1512 | * This function attaches a buffer to a IIO device. The buffer stays attached to |
| 1513 | * the device until the device is freed. The function should only be called at |
| 1514 | * most once per device. |
| 1515 | */ |
| 1516 | void iio_device_attach_buffer(struct iio_dev *indio_dev, |
| 1517 | struct iio_buffer *buffer) |
| 1518 | { |
| 1519 | indio_dev->buffer = iio_buffer_get(buffer); |
| 1520 | } |
| 1521 | EXPORT_SYMBOL_GPL(iio_device_attach_buffer); |