blob: 232a60fe57d25f9a93b16e4a66c6e850d02f07fb [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jonathan Cameron14555b12011-09-21 11:15:57 +01002/* The industrial I/O core
3 *
4 * Copyright (c) 2008 Jonathan Cameron
5 *
Jonathan Cameron14555b12011-09-21 11:15:57 +01006 * Handling of buffer allocation / resizing.
7 *
Jonathan Cameron14555b12011-09-21 11:15:57 +01008 * Things to look at here.
9 * - Better memory allocation techniques?
10 * - Alternative access techniques?
11 */
12#include <linux/kernel.h>
Paul Gortmaker8e336a72011-07-10 13:09:12 -040013#include <linux/export.h>
Jonathan Cameron14555b12011-09-21 11:15:57 +010014#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 Molnar174cd4b2017-02-02 19:15:33 +010019#include <linux/sched/signal.h>
Jonathan Cameron14555b12011-09-21 11:15:57 +010020
Jonathan Cameron06458e22012-04-25 15:54:58 +010021#include <linux/iio/iio.h>
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +030022#include <linux/iio/iio-opaque.h>
Jonathan Cameron14555b12011-09-21 11:15:57 +010023#include "iio_core.h"
Lars-Peter Clausenf11d59d2020-05-25 14:38:53 +030024#include "iio_core_trigger.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010025#include <linux/iio/sysfs.h>
26#include <linux/iio/buffer.h>
Jonathan Cameron33dd94c2017-01-02 19:28:34 +000027#include <linux/iio/buffer_impl.h>
Jonathan Cameron14555b12011-09-21 11:15:57 +010028
29static const char * const iio_endian_prefix[] = {
30 [IIO_BE] = "be",
31 [IIO_LE] = "le",
32};
33
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +010034static bool iio_buffer_is_active(struct iio_buffer *buf)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010035{
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +010036 return !list_empty(&buf->buffer_list);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010037}
38
Josselin Costanzi37d34552015-03-22 20:33:38 +020039static size_t iio_buffer_data_available(struct iio_buffer *buf)
Lars-Peter Clausen647cc7b2013-11-25 14:56:00 +000040{
Josselin Costanzi9dd46942014-06-27 17:20:00 +010041 return buf->access->data_available(buf);
Lars-Peter Clausen647cc7b2013-11-25 14:56:00 +000042}
43
Octavian Purdilaf4f46732015-03-22 20:33:39 +020044static int iio_buffer_flush_hwfifo(struct iio_dev *indio_dev,
45 struct iio_buffer *buf, size_t required)
Josselin Costanzi37d34552015-03-22 20:33:38 +020046{
Octavian Purdilaf4f46732015-03-22 20:33:39 +020047 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
53static 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 Costanzi37d34552015-03-22 20:33:38 +020059 /* wakeup if the device was unregistered */
60 if (!indio_dev->info)
61 return true;
62
63 /* drain the buffer if it was disabled */
Octavian Purdilaf4f46732015-03-22 20:33:39 +020064 if (!iio_buffer_is_active(buf)) {
Josselin Costanzi37d34552015-03-22 20:33:38 +020065 to_wait = min_t(size_t, to_wait, 1);
Octavian Purdilaf4f46732015-03-22 20:33:39 +020066 to_flush = 0;
67 }
Josselin Costanzi37d34552015-03-22 20:33:38 +020068
Octavian Purdilaf4f46732015-03-22 20:33:39 +020069 avail = iio_buffer_data_available(buf);
70
71 if (avail >= to_wait) {
72 /* force a flush for non-blocking reads */
Octavian Purdilac6f67a12015-06-05 15:56:47 +030073 if (!to_wait && avail < to_flush)
74 iio_buffer_flush_hwfifo(indio_dev, buf,
75 to_flush - avail);
Octavian Purdilaf4f46732015-03-22 20:33:39 +020076 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 Costanzi37d34552015-03-22 20:33:38 +020086 return true;
87
88 return false;
89}
90
Jonathan Cameron14555b12011-09-21 11:15:57 +010091/**
Lars-Peter Clausenf6d40332019-12-11 12:43:00 +020092 * iio_buffer_read_outer() - chrdev read for buffer access
Cristina Opriceana01236352015-07-24 16:18:09 +030093 * @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 Cameron14555b12011-09-21 11:15:57 +010097 *
98 * This function relies on all buffer implementations having an
99 * iio_buffer as their first element.
Cristina Opriceana01236352015-07-24 16:18:09 +0300100 *
101 * Return: negative values corresponding to error codes or ret != 0
102 * for ending the reading activity
Jonathan Cameron14555b12011-09-21 11:15:57 +0100103 **/
Lars-Peter Clausenf6d40332019-12-11 12:43:00 +0200104ssize_t iio_buffer_read_outer(struct file *filp, char __user *buf,
105 size_t n, loff_t *f_ps)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100106{
Alexandru Ardeleanbe24dcb2021-02-15 12:40:35 +0200107 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 Norrisfcf68f32016-08-08 17:19:38 -0700110 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200111 size_t datum_size;
Octavian Purdilac6f67a12015-06-05 15:56:47 +0300112 size_t to_wait;
Colin Ian King5dba4b12016-09-05 15:39:06 +0100113 int ret = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100114
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +0100115 if (!indio_dev->info)
116 return -ENODEV;
117
Lars-Peter Clausenf6d40332019-12-11 12:43:00 +0200118 if (!rb || !rb->access->read)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100119 return -EINVAL;
Lars-Peter Clausenee551a12013-11-25 14:56:00 +0000120
Josselin Costanzi37d34552015-03-22 20:33:38 +0200121 datum_size = rb->bytes_per_datum;
Lars-Peter Clausenee551a12013-11-25 14:56:00 +0000122
Josselin Costanzi37d34552015-03-22 20:33:38 +0200123 /*
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 Purdilac6f67a12015-06-05 15:56:47 +0300130 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 Costanzi37d34552015-03-22 20:33:38 +0200134
Brian Norrisfcf68f32016-08-08 17:19:38 -0700135 add_wait_queue(&rb->pollq, &wait);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200136 do {
Brian Norrisfcf68f32016-08-08 17:19:38 -0700137 if (!indio_dev->info) {
138 ret = -ENODEV;
139 break;
140 }
Josselin Costanzi37d34552015-03-22 20:33:38 +0200141
Brian Norrisfcf68f32016-08-08 17:19:38 -0700142 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 Clausenee551a12013-11-25 14:56:00 +0000152
Lars-Peter Clausenf6d40332019-12-11 12:43:00 +0200153 ret = rb->access->read(rb, n, buf);
Lars-Peter Clausenee551a12013-11-25 14:56:00 +0000154 if (ret == 0 && (filp->f_flags & O_NONBLOCK))
155 ret = -EAGAIN;
Colin Ian King5dba4b12016-09-05 15:39:06 +0100156 } while (ret == 0);
Brian Norrisfcf68f32016-08-08 17:19:38 -0700157 remove_wait_queue(&rb->pollq, &wait);
Lars-Peter Clausenee551a12013-11-25 14:56:00 +0000158
159 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100160}
161
162/**
163 * iio_buffer_poll() - poll the buffer to find out if it has data
Cristina Opriceana01236352015-07-24 16:18:09 +0300164 * @filp: File structure pointer for device access
165 * @wait: Poll table structure pointer for which the driver adds
166 * a wait queue
167 *
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800168 * Return: (EPOLLIN | EPOLLRDNORM) if data is available for reading
Cristina Opriceana01236352015-07-24 16:18:09 +0300169 * or 0 for other cases
Jonathan Cameron14555b12011-09-21 11:15:57 +0100170 */
Al Viroafc9a422017-07-03 06:39:46 -0400171__poll_t iio_buffer_poll(struct file *filp,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100172 struct poll_table_struct *wait)
173{
Alexandru Ardeleanbe24dcb2021-02-15 12:40:35 +0200174 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 Cameron14555b12011-09-21 11:15:57 +0100177
Stefan Windfeldt-Prytz4cd140b2018-02-15 15:02:53 +0100178 if (!indio_dev->info || rb == NULL)
Cristina Opriceana1bdc0292015-08-03 13:37:40 +0300179 return 0;
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +0100180
Jonathan Cameron14555b12011-09-21 11:15:57 +0100181 poll_wait(filp, &rb->pollq, wait);
Octavian Purdilaf4f46732015-03-22 20:33:39 +0200182 if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800183 return EPOLLIN | EPOLLRDNORM;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100184 return 0;
185}
186
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100187/**
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 */
194void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
195{
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300196 struct iio_buffer *buffer = indio_dev->buffer;
197
198 if (!buffer)
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100199 return;
200
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300201 wake_up(&buffer->pollq);
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100202}
203
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000204void iio_buffer_init(struct iio_buffer *buffer)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100205{
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000206 INIT_LIST_HEAD(&buffer->demux_list);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100207 INIT_LIST_HEAD(&buffer->buffer_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100208 init_waitqueue_head(&buffer->pollq);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100209 kref_init(&buffer->ref);
Lars-Peter Clausen4a605352015-10-13 18:10:25 +0200210 if (!buffer->watermark)
211 buffer->watermark = 1;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100212}
213EXPORT_SYMBOL(iio_buffer_init);
214
215static 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
222static 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 Cameron9d5d1152011-10-04 16:02:08 +0100230#ifdef __LITTLE_ENDIAN
231 type = IIO_LE;
232#else
233 type = IIO_BE;
234#endif
Jonathan Cameron14555b12011-09-21 11:15:57 +0100235 }
Srinivas Pandruvada0ee85462014-04-29 00:51:00 +0100236 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 Cameron14555b12011-09-21 11:15:57 +0100246 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
253static ssize_t iio_scan_el_show(struct device *dev,
254 struct device_attribute *attr,
255 char *buf)
256{
257 int ret;
Alexandru Ardelean15097c72021-02-15 12:40:33 +0200258 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100259
Alec Berg2076a202014-03-19 18:50:00 +0000260 /* Ensure ret is 0 or 1. */
261 ret = !!test_bit(to_iio_dev_attr(attr)->address,
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300262 buffer->scan_mask);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000263
Jonathan Cameron14555b12011-09-21 11:15:57 +0100264 return sprintf(buf, "%d\n", ret);
265}
266
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100267/* Note NULL used as error indicator as it doesn't make sense. */
268static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
269 unsigned int masklength,
Lars-Peter Clausen1e1ec282015-05-29 18:14:22 +0200270 const unsigned long *mask,
271 bool strict)
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100272{
273 if (bitmap_empty(mask, masklength))
274 return NULL;
275 while (*av_masks) {
Lars-Peter Clausen1e1ec282015-05-29 18:14:22 +0200276 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 Clausen217a5cf2014-11-26 18:55:09 +0100283 av_masks += BITS_TO_LONGS(masklength);
284 }
285 return NULL;
286}
287
288static 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 */
307static 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 Ardeleanccd428e2020-03-21 10:59:56 +0200313 trialmask = bitmap_zalloc(indio_dev->masklength, GFP_KERNEL);
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100314 if (trialmask == NULL)
315 return -ENOMEM;
316 if (!indio_dev->masklength) {
Dan Carpenter231bfe52015-11-21 13:33:00 +0300317 WARN(1, "Trying to set scanmask prior to registering buffer\n");
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100318 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 Clausen1e1ec282015-05-29 18:14:22 +0200329 trialmask, false);
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100330 if (!mask)
331 goto err_invalid_mask;
332 }
333 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
334
Andy Shevchenko38628282019-03-04 10:55:40 +0200335 bitmap_free(trialmask);
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100336
337 return 0;
338
339err_invalid_mask:
Andy Shevchenko38628282019-03-04 10:55:40 +0200340 bitmap_free(trialmask);
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100341 return -EINVAL;
342}
343
Jonathan Cameron14555b12011-09-21 11:15:57 +0100344static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
345{
346 clear_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100347 return 0;
348}
349
Jonathan Cameronc2bf8d52017-01-02 19:28:27 +0000350static 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 Cameron14555b12011-09-21 11:15:57 +0100363static ssize_t iio_scan_el_store(struct device *dev,
364 struct device_attribute *attr,
365 const char *buf,
366 size_t len)
367{
Jonathan Camerona714af22012-04-21 10:09:32 +0100368 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100369 bool state;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200370 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100371 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Alexandru Ardelean15097c72021-02-15 12:40:33 +0200372 struct iio_buffer *buffer = this_attr->buffer;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100373
Jonathan Camerona714af22012-04-21 10:09:32 +0100374 ret = strtobool(buf, &state);
375 if (ret < 0)
376 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100377 mutex_lock(&indio_dev->mlock);
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300378 if (iio_buffer_is_active(buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100379 ret = -EBUSY;
380 goto error_ret;
381 }
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000382 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100383 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 Cameronf79a9092011-12-05 22:18:29 +0000390 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100391 if (ret)
392 goto error_ret;
393 }
394
395error_ret:
396 mutex_unlock(&indio_dev->mlock);
397
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100398 return ret < 0 ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100399
400}
401
402static ssize_t iio_scan_el_ts_show(struct device *dev,
403 struct device_attribute *attr,
404 char *buf)
405{
Alexandru Ardelean15097c72021-02-15 12:40:33 +0200406 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300407
408 return sprintf(buf, "%d\n", buffer->scan_timestamp);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100409}
410
411static 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 Camerona714af22012-04-21 10:09:32 +0100416 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200417 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Alexandru Ardelean15097c72021-02-15 12:40:33 +0200418 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100419 bool state;
420
Jonathan Camerona714af22012-04-21 10:09:32 +0100421 ret = strtobool(buf, &state);
422 if (ret < 0)
423 return ret;
424
Jonathan Cameron14555b12011-09-21 11:15:57 +0100425 mutex_lock(&indio_dev->mlock);
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300426 if (iio_buffer_is_active(buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100427 ret = -EBUSY;
428 goto error_ret;
429 }
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300430 buffer->scan_timestamp = state;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100431error_ret:
432 mutex_unlock(&indio_dev->mlock);
433
434 return ret ? ret : len;
435}
436
437static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300438 struct iio_buffer *buffer,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100439 const struct iio_chan_spec *chan)
440{
441 int ret, attrcount = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100442
443 ret = __iio_add_chan_devattr("index",
444 chan,
445 &iio_show_scan_index,
446 NULL,
447 0,
Jonathan Cameron37044322013-09-08 14:57:00 +0100448 IIO_SEPARATE,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100449 &indio_dev->dev,
Alexandru Ardelean3e3d11b2021-02-15 12:40:32 +0200450 buffer,
Alexandru Ardelean15097c72021-02-15 12:40:33 +0200451 &buffer->buffer_attr_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100452 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000453 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100454 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 Ardelean3e3d11b2021-02-15 12:40:32 +0200462 buffer,
Alexandru Ardelean15097c72021-02-15 12:40:33 +0200463 &buffer->buffer_attr_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100464 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000465 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100466 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 Ardelean3e3d11b2021-02-15 12:40:32 +0200475 buffer,
Alexandru Ardelean15097c72021-02-15 12:40:33 +0200476 &buffer->buffer_attr_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100477 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 Ardelean3e3d11b2021-02-15 12:40:32 +0200485 buffer,
Alexandru Ardelean15097c72021-02-15 12:40:33 +0200486 &buffer->buffer_attr_list);
Peter Meerwald95725882013-09-17 23:42:00 +0100487 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000488 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100489 attrcount++;
490 ret = attrcount;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100491 return ret;
492}
493
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100494static ssize_t iio_buffer_read_length(struct device *dev,
495 struct device_attribute *attr,
496 char *buf)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100497{
Alexandru Ardelean15097c72021-02-15 12:40:33 +0200498 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100499
Lars-Peter Clausen37495662014-11-26 18:55:17 +0100500 return sprintf(buf, "%d\n", buffer->length);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100501}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100502
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100503static ssize_t iio_buffer_write_length(struct device *dev,
504 struct device_attribute *attr,
505 const char *buf, size_t len)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100506{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200507 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Alexandru Ardelean15097c72021-02-15 12:40:33 +0200508 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100509 unsigned int val;
510 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100511
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100512 ret = kstrtouint(buf, 10, &val);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100513 if (ret)
514 return ret;
515
Lars-Peter Clausen37495662014-11-26 18:55:17 +0100516 if (val == buffer->length)
517 return len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100518
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100519 mutex_lock(&indio_dev->mlock);
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300520 if (iio_buffer_is_active(buffer)) {
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100521 ret = -EBUSY;
522 } else {
Lars-Peter Clausen8d92db22014-11-26 18:55:16 +0100523 buffer->access->set_length(buffer, val);
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100524 ret = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100525 }
Josselin Costanzi37d34552015-03-22 20:33:38 +0200526 if (ret)
527 goto out;
528 if (buffer->length && buffer->length < buffer->watermark)
529 buffer->watermark = buffer->length;
530out:
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100531 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100532
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100533 return ret ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100534}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100535
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100536static ssize_t iio_buffer_show_enable(struct device *dev,
537 struct device_attribute *attr,
538 char *buf)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100539{
Alexandru Ardelean15097c72021-02-15 12:40:33 +0200540 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300541
542 return sprintf(buf, "%d\n", iio_buffer_is_active(buffer));
Jonathan Cameron14555b12011-09-21 11:15:57 +0100543}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100544
Lars-Peter Clausen182b4902016-02-02 16:27:24 +0100545static 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
558static 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 Meerwald183f4172013-09-18 22:10:00 +0100564static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
565 const unsigned long *mask, bool timestamp)
Jonathan Cameron959d2952011-12-05 21:37:13 +0000566{
Jonathan Cameron959d2952011-12-05 21:37:13 +0000567 unsigned bytes = 0;
Lars Möllendorf883f6162019-12-13 14:50:55 +0100568 int length, i, largest = 0;
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100569
570 /* How much space will the demuxed element take? */
571 for_each_set_bit(i, mask,
572 indio_dev->masklength) {
Lars-Peter Clausen182b4902016-02-02 16:27:24 +0100573 length = iio_storage_bytes_for_si(indio_dev, i);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100574 bytes = ALIGN(bytes, length);
575 bytes += length;
Lars Möllendorf883f6162019-12-13 14:50:55 +0100576 largest = max(largest, length);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100577 }
Lars-Peter Clausen182b4902016-02-02 16:27:24 +0100578
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100579 if (timestamp) {
Lars-Peter Clausen182b4902016-02-02 16:27:24 +0100580 length = iio_storage_bytes_for_timestamp(indio_dev);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100581 bytes = ALIGN(bytes, length);
582 bytes += length;
Lars Möllendorf883f6162019-12-13 14:50:55 +0100583 largest = max(largest, length);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100584 }
Lars Möllendorf883f6162019-12-13 14:50:55 +0100585
586 bytes = ALIGN(bytes, largest);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100587 return bytes;
588}
589
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100590static void iio_buffer_activate(struct iio_dev *indio_dev,
591 struct iio_buffer *buffer)
592{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300593 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
594
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100595 iio_buffer_get(buffer);
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300596 list_add(&buffer->buffer_list, &iio_dev_opaque->buffer_list);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100597}
598
599static void iio_buffer_deactivate(struct iio_buffer *buffer)
600{
601 list_del_init(&buffer->buffer_list);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200602 wake_up_interruptible(&buffer->pollq);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100603 iio_buffer_put(buffer);
604}
605
Lars-Peter Clausen12501862015-05-18 13:34:49 +0200606static void iio_buffer_deactivate_all(struct iio_dev *indio_dev)
607{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300608 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Lars-Peter Clausen12501862015-05-18 13:34:49 +0200609 struct iio_buffer *buffer, *_buffer;
610
611 list_for_each_entry_safe(buffer, _buffer,
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300612 &iio_dev_opaque->buffer_list, buffer_list)
Lars-Peter Clausen12501862015-05-18 13:34:49 +0200613 iio_buffer_deactivate(buffer);
614}
615
Lars-Peter Clausene18a2ad2015-10-13 18:10:27 +0200616static 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
624static 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 Clausen8e050992013-10-14 17:49:00 +0100632static 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 Clausenfcc1b2f2015-05-13 16:04:46 +0200646static 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 Clausen248be5a2015-05-13 16:04:45 +0200665static 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 Shevchenko38628282019-03-04 10:55:40 +0200670 bitmap_free(mask);
Lars-Peter Clausen248be5a2015-05-13 16:04:45 +0200671}
672
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200673struct iio_device_config {
674 unsigned int mode;
Lars-Peter Clausenf0566c02015-10-13 18:10:24 +0200675 unsigned int watermark;
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200676 const unsigned long *scan_mask;
677 unsigned int scan_bytes;
678 bool scan_timestamp;
679};
680
681static 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 Ardelean6a8c6b22020-06-30 07:57:07 +0300685 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200686 unsigned long *compound_mask;
687 const unsigned long *scan_mask;
Lars-Peter Clausen1e1ec282015-05-29 18:14:22 +0200688 bool strict_scanmask = false;
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200689 struct iio_buffer *buffer;
690 bool scan_timestamp;
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200691 unsigned int modes;
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200692
Lars-Peter Clausenb7329242020-03-26 11:30:12 +0200693 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 Clausen6e509c42015-05-18 13:34:47 +0200700 memset(config, 0, sizeof(*config));
Irina Tirdea1bef2c12016-03-24 11:09:45 +0200701 config->watermark = ~0;
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200702
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 Ardelean6a8c6b22020-06-30 07:57:07 +0300708 list_is_singular(&iio_dev_opaque->buffer_list))
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200709 return 0;
710
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200711 modes = indio_dev->modes;
712
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300713 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200714 if (buffer == remove_buffer)
715 continue;
716 modes &= buffer->access->modes;
Lars-Peter Clausenf0566c02015-10-13 18:10:24 +0200717 config->watermark = min(config->watermark, buffer->watermark);
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200718 }
719
Lars-Peter Clausenf0566c02015-10-13 18:10:24 +0200720 if (insert_buffer) {
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200721 modes &= insert_buffer->access->modes;
Lars-Peter Clausenf0566c02015-10-13 18:10:24 +0200722 config->watermark = min(config->watermark,
723 insert_buffer->watermark);
724 }
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200725
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200726 /* Definitely possible for devices to support both of these. */
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200727 if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) {
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200728 config->mode = INDIO_BUFFER_TRIGGERED;
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200729 } else if (modes & INDIO_BUFFER_HARDWARE) {
Lars-Peter Clausen1e1ec282015-05-29 18:14:22 +0200730 /*
731 * Keep things simple for now and only allow a single buffer to
732 * be connected in hardware mode.
733 */
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300734 if (insert_buffer && !list_empty(&iio_dev_opaque->buffer_list))
Lars-Peter Clausen1e1ec282015-05-29 18:14:22 +0200735 return -EINVAL;
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200736 config->mode = INDIO_BUFFER_HARDWARE;
Lars-Peter Clausen1e1ec282015-05-29 18:14:22 +0200737 strict_scanmask = true;
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200738 } else if (modes & INDIO_BUFFER_SOFTWARE) {
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200739 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 Shevchenko38628282019-03-04 10:55:40 +0200748 compound_mask = bitmap_zalloc(indio_dev->masklength, GFP_KERNEL);
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200749 if (compound_mask == NULL)
750 return -ENOMEM;
751
752 scan_timestamp = false;
753
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300754 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200755 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 Clausen1e1ec282015-05-29 18:14:22 +0200771 compound_mask,
772 strict_scanmask);
Andy Shevchenko38628282019-03-04 10:55:40 +0200773 bitmap_free(compound_mask);
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200774 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 Cameron78c99812017-01-02 19:28:24 +0000788/**
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 */
795struct iio_demux_table {
796 unsigned from;
797 unsigned to;
798 unsigned length;
799 struct list_head l;
800};
801
802static 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
811static 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
832static 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 Cameron78c99812017-01-02 19:28:24 +0000858 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á19ef7b72020-11-12 15:43:22 +0100861 in_ind = find_next_bit(indio_dev->active_scan_mask,
862 indio_dev->masklength,
863 in_ind + 1);
Jonathan Cameron78c99812017-01-02 19:28:24 +0000864 }
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
892error_clear_mux_table:
893 iio_buffer_demux_free(buffer);
894
895 return ret;
896}
897
898static int iio_update_demux(struct iio_dev *indio_dev)
899{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300900 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameron78c99812017-01-02 19:28:24 +0000901 struct iio_buffer *buffer;
902 int ret;
903
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300904 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
Jonathan Cameron78c99812017-01-02 19:28:24 +0000905 ret = iio_buffer_update_demux(indio_dev, buffer);
906 if (ret < 0)
907 goto error_clear_mux_table;
908 }
909 return 0;
910
911error_clear_mux_table:
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300912 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list)
Jonathan Cameron78c99812017-01-02 19:28:24 +0000913 iio_buffer_demux_free(buffer);
914
915 return ret;
916}
917
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +0200918static int iio_enable_buffers(struct iio_dev *indio_dev,
919 struct iio_device_config *config)
920{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300921 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Lars-Peter Clausene18a2ad2015-10-13 18:10:27 +0200922 struct iio_buffer *buffer;
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +0200923 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 Clausen5cb1a542020-04-30 11:24:55 +0300928 indio_dev->currentmode = config->mode;
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +0200929
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 Clausenf0566c02015-10-13 18:10:24 +0200954 if (indio_dev->info->hwfifo_set_watermark)
955 indio_dev->info->hwfifo_set_watermark(indio_dev,
956 config->watermark);
957
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300958 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
Lars-Peter Clausene18a2ad2015-10-13 18:10:27 +0200959 ret = iio_buffer_enable(buffer, indio_dev);
960 if (ret)
961 goto err_disable_buffers;
962 }
963
Lars-Peter Clausenf11d59d2020-05-25 14:38:53 +0300964 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 Ardelean62a30a22020-07-15 07:16:29 +0300971 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 Clausen623d74e2015-05-18 13:34:48 +0200980 return 0;
981
Alexandru Ardelean62a30a22020-07-15 07:16:29 +0300982err_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 Clausene18a2ad2015-10-13 18:10:27 +0200987err_disable_buffers:
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300988 list_for_each_entry_continue_reverse(buffer, &iio_dev_opaque->buffer_list,
Lars-Peter Clausene18a2ad2015-10-13 18:10:27 +0200989 buffer_list)
990 iio_buffer_disable(buffer, indio_dev);
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +0200991err_run_postdisable:
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +0200992 if (indio_dev->setup_ops->postdisable)
993 indio_dev->setup_ops->postdisable(indio_dev);
994err_undo_config:
Lars-Peter Clausen5cb1a542020-04-30 11:24:55 +0300995 indio_dev->currentmode = INDIO_DIRECT_MODE;
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +0200996 indio_dev->active_scan_mask = NULL;
997
998 return ret;
999}
1000
1001static int iio_disable_buffers(struct iio_dev *indio_dev)
1002{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001003 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Lars-Peter Clausene18a2ad2015-10-13 18:10:27 +02001004 struct iio_buffer *buffer;
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001005 int ret = 0;
1006 int ret2;
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001007
1008 /* Wind down existing buffers - iff there are any */
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001009 if (list_empty(&iio_dev_opaque->buffer_list))
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001010 return 0;
1011
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001012 /*
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 Clausen623d74e2015-05-18 13:34:48 +02001019 if (indio_dev->setup_ops->predisable) {
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001020 ret2 = indio_dev->setup_ops->predisable(indio_dev);
1021 if (ret2 && !ret)
1022 ret = ret2;
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001023 }
1024
Alexandru Ardelean62a30a22020-07-15 07:16:29 +03001025 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
1026 iio_trigger_detach_poll_func(indio_dev->trig,
1027 indio_dev->pollfunc);
1028 }
1029
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001030 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
Lars-Peter Clausene18a2ad2015-10-13 18:10:27 +02001031 ret2 = iio_buffer_disable(buffer, indio_dev);
1032 if (ret2 && !ret)
1033 ret = ret2;
1034 }
1035
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001036 if (indio_dev->setup_ops->postdisable) {
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001037 ret2 = indio_dev->setup_ops->postdisable(indio_dev);
1038 if (ret2 && !ret)
1039 ret = ret2;
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001040 }
1041
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001042 iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask);
1043 indio_dev->active_scan_mask = NULL;
Lars-Peter Clausen5cb1a542020-04-30 11:24:55 +03001044 indio_dev->currentmode = INDIO_DIRECT_MODE;
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001045
1046 return ret;
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001047}
1048
Lars-Peter Clausena9519452013-10-04 12:07:00 +01001049static int __iio_update_buffers(struct iio_dev *indio_dev,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001050 struct iio_buffer *insert_buffer,
1051 struct iio_buffer *remove_buffer)
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +01001052{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001053 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +02001054 struct iio_device_config new_config;
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001055 int ret;
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +02001056
1057 ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer,
1058 &new_config);
1059 if (ret)
1060 return ret;
Jonathan Cameron959d2952011-12-05 21:37:13 +00001061
Lars-Peter Clausenfcc1b2f2015-05-13 16:04:46 +02001062 if (insert_buffer) {
1063 ret = iio_buffer_request_update(indio_dev, insert_buffer);
1064 if (ret)
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +02001065 goto err_free_config;
Lars-Peter Clausenfcc1b2f2015-05-13 16:04:46 +02001066 }
1067
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001068 ret = iio_disable_buffers(indio_dev);
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001069 if (ret)
1070 goto err_deactivate_all;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001071
1072 if (remove_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +01001073 iio_buffer_deactivate(remove_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001074 if (insert_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +01001075 iio_buffer_activate(indio_dev, insert_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001076
1077 /* If no buffers in list, we are done */
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001078 if (list_empty(&iio_dev_opaque->buffer_list))
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001079 return 0;
Jonathan Cameron959d2952011-12-05 21:37:13 +00001080
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001081 ret = iio_enable_buffers(indio_dev, &new_config);
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001082 if (ret)
1083 goto err_deactivate_all;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001084
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +02001085 return 0;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001086
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001087err_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 Clausen6e509c42015-05-18 13:34:47 +02001098err_free_config:
1099 iio_free_scan_mask(indio_dev, new_config.scan_mask);
1100 return ret;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001101}
Lars-Peter Clausena9519452013-10-04 12:07:00 +01001102
1103int 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 Clausen3909fab2013-10-04 12:07:00 +01001109 if (insert_buffer == remove_buffer)
1110 return 0;
1111
Lars-Peter Clausena9519452013-10-04 12:07:00 +01001112 mutex_lock(&indio_dev->info_exist_lock);
1113 mutex_lock(&indio_dev->mlock);
1114
Lars-Peter Clausen3909fab2013-10-04 12:07:00 +01001115 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 Clausena9519452013-10-04 12:07:00 +01001126 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
1133out_unlock:
1134 mutex_unlock(&indio_dev->mlock);
1135 mutex_unlock(&indio_dev->info_exist_lock);
1136
1137 return ret;
1138}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001139EXPORT_SYMBOL_GPL(iio_update_buffers);
1140
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001141void iio_disable_all_buffers(struct iio_dev *indio_dev)
1142{
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001143 iio_disable_buffers(indio_dev);
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001144 iio_buffer_deactivate_all(indio_dev);
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001145}
1146
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +01001147static ssize_t iio_buffer_store_enable(struct device *dev,
1148 struct device_attribute *attr,
1149 const char *buf,
1150 size_t len)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001151{
1152 int ret;
1153 bool requested_state;
1154 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Alexandru Ardelean15097c72021-02-15 12:40:33 +02001155 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001156 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 Ardeleanff3f7e042020-04-24 18:22:43 +03001165 inlist = iio_buffer_is_active(buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001166 /* Already in desired state */
1167 if (inlist == requested_state)
1168 goto done;
1169
1170 if (requested_state)
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +03001171 ret = __iio_update_buffers(indio_dev, buffer, NULL);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001172 else
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +03001173 ret = __iio_update_buffers(indio_dev, NULL, buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001174
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001175done:
1176 mutex_unlock(&indio_dev->mlock);
1177 return (ret < 0) ? ret : len;
1178}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001179
Josselin Costanzi37d34552015-03-22 20:33:38 +02001180static ssize_t iio_buffer_show_watermark(struct device *dev,
1181 struct device_attribute *attr,
1182 char *buf)
1183{
Alexandru Ardelean15097c72021-02-15 12:40:33 +02001184 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
Josselin Costanzi37d34552015-03-22 20:33:38 +02001185
1186 return sprintf(buf, "%u\n", buffer->watermark);
1187}
1188
1189static 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 Ardelean15097c72021-02-15 12:40:33 +02001195 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
Josselin Costanzi37d34552015-03-22 20:33:38 +02001196 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 Ardeleanff3f7e042020-04-24 18:22:43 +03001212 if (iio_buffer_is_active(buffer)) {
Josselin Costanzi37d34552015-03-22 20:33:38 +02001213 ret = -EBUSY;
1214 goto out;
1215 }
1216
1217 buffer->watermark = val;
1218out:
1219 mutex_unlock(&indio_dev->mlock);
1220
1221 return ret ? ret : len;
1222}
1223
Matt Fornero350f6c72017-12-06 14:43:30 -05001224static ssize_t iio_dma_show_data_available(struct device *dev,
1225 struct device_attribute *attr,
1226 char *buf)
1227{
Alexandru Ardelean15097c72021-02-15 12:40:33 +02001228 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
Matt Fornero350f6c72017-12-06 14:43:30 -05001229
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +03001230 return sprintf(buf, "%zu\n", iio_buffer_data_available(buffer));
Matt Fornero350f6c72017-12-06 14:43:30 -05001231}
1232
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +01001233static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
1234 iio_buffer_write_length);
Lars-Peter Clausen8d92db22014-11-26 18:55:16 +01001235static struct device_attribute dev_attr_length_ro = __ATTR(length,
1236 S_IRUGO, iio_buffer_read_length, NULL);
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +01001237static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
1238 iio_buffer_show_enable, iio_buffer_store_enable);
Josselin Costanzi37d34552015-03-22 20:33:38 +02001239static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR,
1240 iio_buffer_show_watermark, iio_buffer_store_watermark);
Lars-Peter Clausenb4406552015-10-13 18:10:26 +02001241static struct device_attribute dev_attr_watermark_ro = __ATTR(watermark,
1242 S_IRUGO, iio_buffer_show_watermark, NULL);
Matt Fornero350f6c72017-12-06 14:43:30 -05001243static DEVICE_ATTR(data_available, S_IRUGO,
1244 iio_dma_show_data_available, NULL);
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +01001245
Octavian Purdila6da9b382015-01-31 02:00:00 +02001246static struct attribute *iio_buffer_attrs[] = {
1247 &dev_attr_length.attr,
1248 &dev_attr_enable.attr,
Josselin Costanzi37d34552015-03-22 20:33:38 +02001249 &dev_attr_watermark.attr,
Matt Fornero350f6c72017-12-06 14:43:30 -05001250 &dev_attr_data_available.attr,
Octavian Purdila6da9b382015-01-31 02:00:00 +02001251};
1252
Alexandru Ardelean15097c72021-02-15 12:40:33 +02001253#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
1254
1255static 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 Ardeleand9a62572021-02-15 12:40:31 +02001274static 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
1317error_free_buffer_attrs:
1318 kfree(iio_dev_opaque->legacy_buffer_group.attrs);
1319error_free_scan_el_attrs:
1320 kfree(iio_dev_opaque->legacy_scan_el_group.attrs);
1321
1322 return ret;
1323}
1324
1325static 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 Ardeleane16e0a772020-09-17 15:59:51 +03001333static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
Alexandru Ardeleand9a62572021-02-15 12:40:31 +02001334 struct iio_dev *indio_dev,
1335 int index)
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001336{
1337 struct iio_dev_attr *p;
1338 struct attribute **attr;
Alexandru Ardeleane2b4d7ac2021-02-15 12:40:30 +02001339 int ret, i, attrn, scan_el_attrcount, buffer_attrcount;
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001340 const struct iio_chan_spec *channels;
1341
Alexandru Ardeleane2b4d7ac2021-02-15 12:40:30 +02001342 buffer_attrcount = 0;
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +01001343 if (buffer->attrs) {
Alexandru Ardeleane2b4d7ac2021-02-15 12:40:30 +02001344 while (buffer->attrs[buffer_attrcount] != NULL)
1345 buffer_attrcount++;
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +01001346 }
1347
Alexandru Ardeleane2b4d7ac2021-02-15 12:40:30 +02001348 scan_el_attrcount = 0;
Alexandru Ardelean15097c72021-02-15 12:40:33 +02001349 INIT_LIST_HEAD(&buffer->buffer_attr_list);
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001350 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 Ardeleanff3f7e042020-04-24 18:22:43 +03001357 ret = iio_buffer_add_channel_sysfs(indio_dev, buffer,
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001358 &channels[i]);
1359 if (ret < 0)
1360 goto error_cleanup_dynamic;
Alexandru Ardeleane2b4d7ac2021-02-15 12:40:30 +02001361 scan_el_attrcount += ret;
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001362 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 Shevchenko38628282019-03-04 10:55:40 +02001367 buffer->scan_mask = bitmap_zalloc(indio_dev->masklength,
1368 GFP_KERNEL);
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001369 if (buffer->scan_mask == NULL) {
1370 ret = -ENOMEM;
1371 goto error_cleanup_dynamic;
1372 }
1373 }
1374 }
1375
Alexandru Ardeleand9a62572021-02-15 12:40:31 +02001376 attrn = buffer_attrcount + scan_el_attrcount + ARRAY_SIZE(iio_buffer_attrs);
1377 attr = kcalloc(attrn + 1, sizeof(* attr), GFP_KERNEL);
Alexandru Ardeleane2b4d7ac2021-02-15 12:40:30 +02001378 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 Ardeleane2b4d7ac2021-02-15 12:40:30 +02001395
Alexandru Ardelean15097c72021-02-15 12:40:33 +02001396 for (i = 0; i < buffer_attrcount; i++) {
1397 struct attribute *wrapped;
Alexandru Ardeleand9a62572021-02-15 12:40:31 +02001398
Alexandru Ardelean15097c72021-02-15 12:40:33 +02001399 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 Ardeleand9a62572021-02-15 12:40:31 +02001409 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 Ardeleane2b4d7ac2021-02-15 12:40:30 +02001417 buffer->buffer_group.attrs = attr;
1418
1419 ret = iio_device_register_sysfs_group(indio_dev, &buffer->buffer_group);
1420 if (ret)
Alexandru Ardeleand9a62572021-02-15 12:40:31 +02001421 goto error_free_buffer_attr_group_name;
Alexandru Ardeleane2b4d7ac2021-02-15 12:40:30 +02001422
Alexandru Ardeleand9a62572021-02-15 12:40:31 +02001423 /* we only need to register the legacy groups for the first buffer */
1424 if (index > 0)
1425 return 0;
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001426
Alexandru Ardeleand9a62572021-02-15 12:40:31 +02001427 ret = iio_buffer_register_legacy_sysfs_groups(indio_dev, attr,
1428 buffer_attrcount,
1429 scan_el_attrcount);
Alexandru Ardelean32f17172021-02-15 12:40:29 +02001430 if (ret)
Alexandru Ardeleand9a62572021-02-15 12:40:31 +02001431 goto error_free_buffer_attr_group_name;
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001432
1433 return 0;
1434
Alexandru Ardeleand9a62572021-02-15 12:40:31 +02001435error_free_buffer_attr_group_name:
1436 kfree(buffer->buffer_group.name);
Alexandru Ardeleane2b4d7ac2021-02-15 12:40:30 +02001437error_free_buffer_attrs:
1438 kfree(buffer->buffer_group.attrs);
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001439error_free_scan_mask:
Andy Shevchenko38628282019-03-04 10:55:40 +02001440 bitmap_free(buffer->scan_mask);
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001441error_cleanup_dynamic:
Alexandru Ardelean15097c72021-02-15 12:40:33 +02001442 iio_free_chan_devattr_list(&buffer->buffer_attr_list);
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001443
1444 return ret;
1445}
1446
Alexandru Ardeleane16e0a772020-09-17 15:59:51 +03001447int 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 Ardeleand9a62572021-02-15 12:40:31 +02001465 return __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev, 0);
Alexandru Ardeleane16e0a772020-09-17 15:59:51 +03001466}
1467
1468static void __iio_buffer_free_sysfs_and_mask(struct iio_buffer *buffer)
1469{
1470 bitmap_free(buffer->scan_mask);
Alexandru Ardeleand9a62572021-02-15 12:40:31 +02001471 kfree(buffer->buffer_group.name);
Alexandru Ardeleane16e0a772020-09-17 15:59:51 +03001472 kfree(buffer->buffer_group.attrs);
Alexandru Ardelean15097c72021-02-15 12:40:33 +02001473 iio_free_chan_devattr_list(&buffer->buffer_attr_list);
Alexandru Ardeleane16e0a772020-09-17 15:59:51 +03001474}
1475
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001476void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
1477{
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +03001478 struct iio_buffer *buffer = indio_dev->buffer;
1479
1480 if (!buffer)
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001481 return;
1482
Alexandru Ardeleand9a62572021-02-15 12:40:31 +02001483 iio_buffer_unregister_legacy_sysfs_groups(indio_dev);
1484
Alexandru Ardeleane16e0a772020-09-17 15:59:51 +03001485 __iio_buffer_free_sysfs_and_mask(buffer);
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001486}
1487
Jonathan Cameron14555b12011-09-21 11:15:57 +01001488/**
Lars-Peter Clausen81636632012-07-09 10:00:00 +01001489 * 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 */
1497bool 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}
1502EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
1503
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +01001504static const void *iio_demux(struct iio_buffer *buffer,
1505 const void *datain)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001506{
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 Clausen5d65d922013-09-15 17:50:00 +01001518static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001519{
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +01001520 const void *dataout = iio_demux(buffer, data);
Josselin Costanzi37d34552015-03-22 20:33:38 +02001521 int ret;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001522
Josselin Costanzi37d34552015-03-22 20:33:38 +02001523 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 Torvaldsa9a08842018-02-11 14:34:03 -08001531 wake_up_interruptible_poll(&buffer->pollq, EPOLLIN | EPOLLRDNORM);
Josselin Costanzi37d34552015-03-22 20:33:38 +02001532 return 0;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001533}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001534
Jonathan Cameron315a19e2017-01-02 19:28:28 +00001535/**
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 Clausen5d65d922013-09-15 17:50:00 +01001540int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001541{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001542 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001543 int ret;
1544 struct iio_buffer *buf;
1545
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001546 list_for_each_entry(buf, &iio_dev_opaque->buffer_list, buffer_list) {
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001547 ret = iio_push_to_buffer(buf, data);
1548 if (ret < 0)
1549 return ret;
1550 }
1551
1552 return 0;
1553}
1554EXPORT_SYMBOL_GPL(iio_push_to_buffers);
1555
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +01001556/**
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 */
1565static 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 */
1578struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
1579{
1580 if (buffer)
1581 kref_get(&buffer->ref);
1582
1583 return buffer;
1584}
1585EXPORT_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 */
1591void iio_buffer_put(struct iio_buffer *buffer)
1592{
1593 if (buffer)
1594 kref_put(&buffer->ref, iio_buffer_release);
1595}
1596EXPORT_SYMBOL_GPL(iio_buffer_put);
Jonathan Cameron2b827ad2017-01-02 19:28:32 +00001597
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 */
1607void iio_device_attach_buffer(struct iio_dev *indio_dev,
1608 struct iio_buffer *buffer)
1609{
1610 indio_dev->buffer = iio_buffer_get(buffer);
1611}
1612EXPORT_SYMBOL_GPL(iio_device_attach_buffer);