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