blob: a4f6bb96d4f42505027f98392b208465ed04e9fa [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{
107 struct iio_dev *indio_dev = filp->private_data;
108 struct iio_buffer *rb = indio_dev->buffer;
Brian Norrisfcf68f32016-08-08 17:19:38 -0700109 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200110 size_t datum_size;
Octavian Purdilac6f67a12015-06-05 15:56:47 +0300111 size_t to_wait;
Colin Ian King5dba4b12016-09-05 15:39:06 +0100112 int ret = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100113
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +0100114 if (!indio_dev->info)
115 return -ENODEV;
116
Lars-Peter Clausenf6d40332019-12-11 12:43:00 +0200117 if (!rb || !rb->access->read)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100118 return -EINVAL;
Lars-Peter Clausenee551a12013-11-25 14:56:00 +0000119
Josselin Costanzi37d34552015-03-22 20:33:38 +0200120 datum_size = rb->bytes_per_datum;
Lars-Peter Clausenee551a12013-11-25 14:56:00 +0000121
Josselin Costanzi37d34552015-03-22 20:33:38 +0200122 /*
123 * If datum_size is 0 there will never be anything to read from the
124 * buffer, so signal end of file now.
125 */
126 if (!datum_size)
127 return 0;
128
Octavian Purdilac6f67a12015-06-05 15:56:47 +0300129 if (filp->f_flags & O_NONBLOCK)
130 to_wait = 0;
131 else
132 to_wait = min_t(size_t, n / datum_size, rb->watermark);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200133
Brian Norrisfcf68f32016-08-08 17:19:38 -0700134 add_wait_queue(&rb->pollq, &wait);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200135 do {
Brian Norrisfcf68f32016-08-08 17:19:38 -0700136 if (!indio_dev->info) {
137 ret = -ENODEV;
138 break;
139 }
Josselin Costanzi37d34552015-03-22 20:33:38 +0200140
Brian Norrisfcf68f32016-08-08 17:19:38 -0700141 if (!iio_buffer_ready(indio_dev, rb, to_wait, n / datum_size)) {
142 if (signal_pending(current)) {
143 ret = -ERESTARTSYS;
144 break;
145 }
146
147 wait_woken(&wait, TASK_INTERRUPTIBLE,
148 MAX_SCHEDULE_TIMEOUT);
149 continue;
150 }
Lars-Peter Clausenee551a12013-11-25 14:56:00 +0000151
Lars-Peter Clausenf6d40332019-12-11 12:43:00 +0200152 ret = rb->access->read(rb, n, buf);
Lars-Peter Clausenee551a12013-11-25 14:56:00 +0000153 if (ret == 0 && (filp->f_flags & O_NONBLOCK))
154 ret = -EAGAIN;
Colin Ian King5dba4b12016-09-05 15:39:06 +0100155 } while (ret == 0);
Brian Norrisfcf68f32016-08-08 17:19:38 -0700156 remove_wait_queue(&rb->pollq, &wait);
Lars-Peter Clausenee551a12013-11-25 14:56:00 +0000157
158 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100159}
160
161/**
162 * iio_buffer_poll() - poll the buffer to find out if it has data
Cristina Opriceana01236352015-07-24 16:18:09 +0300163 * @filp: File structure pointer for device access
164 * @wait: Poll table structure pointer for which the driver adds
165 * a wait queue
166 *
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800167 * Return: (EPOLLIN | EPOLLRDNORM) if data is available for reading
Cristina Opriceana01236352015-07-24 16:18:09 +0300168 * or 0 for other cases
Jonathan Cameron14555b12011-09-21 11:15:57 +0100169 */
Al Viroafc9a422017-07-03 06:39:46 -0400170__poll_t iio_buffer_poll(struct file *filp,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100171 struct poll_table_struct *wait)
172{
173 struct iio_dev *indio_dev = filp->private_data;
174 struct iio_buffer *rb = indio_dev->buffer;
175
Stefan Windfeldt-Prytz4cd140b2018-02-15 15:02:53 +0100176 if (!indio_dev->info || rb == NULL)
Cristina Opriceana1bdc0292015-08-03 13:37:40 +0300177 return 0;
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +0100178
Jonathan Cameron14555b12011-09-21 11:15:57 +0100179 poll_wait(filp, &rb->pollq, wait);
Octavian Purdilaf4f46732015-03-22 20:33:39 +0200180 if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800181 return EPOLLIN | EPOLLRDNORM;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100182 return 0;
183}
184
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100185/**
186 * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue
187 * @indio_dev: The IIO device
188 *
189 * Wakes up the event waitqueue used for poll(). Should usually
190 * be called when the device is unregistered.
191 */
192void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
193{
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300194 struct iio_buffer *buffer = indio_dev->buffer;
195
196 if (!buffer)
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100197 return;
198
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300199 wake_up(&buffer->pollq);
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100200}
201
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000202void iio_buffer_init(struct iio_buffer *buffer)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100203{
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000204 INIT_LIST_HEAD(&buffer->demux_list);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100205 INIT_LIST_HEAD(&buffer->buffer_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100206 init_waitqueue_head(&buffer->pollq);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100207 kref_init(&buffer->ref);
Lars-Peter Clausen4a605352015-10-13 18:10:25 +0200208 if (!buffer->watermark)
209 buffer->watermark = 1;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100210}
211EXPORT_SYMBOL(iio_buffer_init);
212
Jonathan Cameron9f466772017-01-02 19:28:26 +0000213/**
214 * iio_buffer_set_attrs - Set buffer specific attributes
215 * @buffer: The buffer for which we are setting attributes
216 * @attrs: Pointer to a null terminated list of pointers to attributes
217 */
218void iio_buffer_set_attrs(struct iio_buffer *buffer,
219 const struct attribute **attrs)
220{
221 buffer->attrs = attrs;
222}
223EXPORT_SYMBOL_GPL(iio_buffer_set_attrs);
224
Jonathan Cameron14555b12011-09-21 11:15:57 +0100225static ssize_t iio_show_scan_index(struct device *dev,
226 struct device_attribute *attr,
227 char *buf)
228{
229 return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
230}
231
232static ssize_t iio_show_fixed_type(struct device *dev,
233 struct device_attribute *attr,
234 char *buf)
235{
236 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
237 u8 type = this_attr->c->scan_type.endianness;
238
239 if (type == IIO_CPU) {
Jonathan Cameron9d5d1152011-10-04 16:02:08 +0100240#ifdef __LITTLE_ENDIAN
241 type = IIO_LE;
242#else
243 type = IIO_BE;
244#endif
Jonathan Cameron14555b12011-09-21 11:15:57 +0100245 }
Srinivas Pandruvada0ee85462014-04-29 00:51:00 +0100246 if (this_attr->c->scan_type.repeat > 1)
247 return sprintf(buf, "%s:%c%d/%dX%d>>%u\n",
248 iio_endian_prefix[type],
249 this_attr->c->scan_type.sign,
250 this_attr->c->scan_type.realbits,
251 this_attr->c->scan_type.storagebits,
252 this_attr->c->scan_type.repeat,
253 this_attr->c->scan_type.shift);
254 else
255 return sprintf(buf, "%s:%c%d/%d>>%u\n",
Jonathan Cameron14555b12011-09-21 11:15:57 +0100256 iio_endian_prefix[type],
257 this_attr->c->scan_type.sign,
258 this_attr->c->scan_type.realbits,
259 this_attr->c->scan_type.storagebits,
260 this_attr->c->scan_type.shift);
261}
262
263static ssize_t iio_scan_el_show(struct device *dev,
264 struct device_attribute *attr,
265 char *buf)
266{
267 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200268 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300269 struct iio_buffer *buffer = indio_dev->buffer;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100270
Alec Berg2076a202014-03-19 18:50:00 +0000271 /* Ensure ret is 0 or 1. */
272 ret = !!test_bit(to_iio_dev_attr(attr)->address,
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300273 buffer->scan_mask);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000274
Jonathan Cameron14555b12011-09-21 11:15:57 +0100275 return sprintf(buf, "%d\n", ret);
276}
277
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100278/* Note NULL used as error indicator as it doesn't make sense. */
279static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
280 unsigned int masklength,
Lars-Peter Clausen1e1ec282015-05-29 18:14:22 +0200281 const unsigned long *mask,
282 bool strict)
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100283{
284 if (bitmap_empty(mask, masklength))
285 return NULL;
286 while (*av_masks) {
Lars-Peter Clausen1e1ec282015-05-29 18:14:22 +0200287 if (strict) {
288 if (bitmap_equal(mask, av_masks, masklength))
289 return av_masks;
290 } else {
291 if (bitmap_subset(mask, av_masks, masklength))
292 return av_masks;
293 }
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100294 av_masks += BITS_TO_LONGS(masklength);
295 }
296 return NULL;
297}
298
299static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
300 const unsigned long *mask)
301{
302 if (!indio_dev->setup_ops->validate_scan_mask)
303 return true;
304
305 return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
306}
307
308/**
309 * iio_scan_mask_set() - set particular bit in the scan mask
310 * @indio_dev: the iio device
311 * @buffer: the buffer whose scan mask we are interested in
312 * @bit: the bit to be set.
313 *
314 * Note that at this point we have no way of knowing what other
315 * buffers might request, hence this code only verifies that the
316 * individual buffers request is plausible.
317 */
318static int iio_scan_mask_set(struct iio_dev *indio_dev,
319 struct iio_buffer *buffer, int bit)
320{
321 const unsigned long *mask;
322 unsigned long *trialmask;
323
Alexandru Ardeleanccd428e2020-03-21 10:59:56 +0200324 trialmask = bitmap_zalloc(indio_dev->masklength, GFP_KERNEL);
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100325 if (trialmask == NULL)
326 return -ENOMEM;
327 if (!indio_dev->masklength) {
Dan Carpenter231bfe52015-11-21 13:33:00 +0300328 WARN(1, "Trying to set scanmask prior to registering buffer\n");
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100329 goto err_invalid_mask;
330 }
331 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
332 set_bit(bit, trialmask);
333
334 if (!iio_validate_scan_mask(indio_dev, trialmask))
335 goto err_invalid_mask;
336
337 if (indio_dev->available_scan_masks) {
338 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
339 indio_dev->masklength,
Lars-Peter Clausen1e1ec282015-05-29 18:14:22 +0200340 trialmask, false);
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100341 if (!mask)
342 goto err_invalid_mask;
343 }
344 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
345
Andy Shevchenko38628282019-03-04 10:55:40 +0200346 bitmap_free(trialmask);
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100347
348 return 0;
349
350err_invalid_mask:
Andy Shevchenko38628282019-03-04 10:55:40 +0200351 bitmap_free(trialmask);
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100352 return -EINVAL;
353}
354
Jonathan Cameron14555b12011-09-21 11:15:57 +0100355static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
356{
357 clear_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100358 return 0;
359}
360
Jonathan Cameronc2bf8d52017-01-02 19:28:27 +0000361static int iio_scan_mask_query(struct iio_dev *indio_dev,
362 struct iio_buffer *buffer, int bit)
363{
364 if (bit > indio_dev->masklength)
365 return -EINVAL;
366
367 if (!buffer->scan_mask)
368 return 0;
369
370 /* Ensure return value is 0 or 1. */
371 return !!test_bit(bit, buffer->scan_mask);
372};
373
Jonathan Cameron14555b12011-09-21 11:15:57 +0100374static ssize_t iio_scan_el_store(struct device *dev,
375 struct device_attribute *attr,
376 const char *buf,
377 size_t len)
378{
Jonathan Camerona714af22012-04-21 10:09:32 +0100379 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100380 bool state;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200381 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100382 struct iio_buffer *buffer = indio_dev->buffer;
383 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
384
Jonathan Camerona714af22012-04-21 10:09:32 +0100385 ret = strtobool(buf, &state);
386 if (ret < 0)
387 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100388 mutex_lock(&indio_dev->mlock);
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300389 if (iio_buffer_is_active(buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100390 ret = -EBUSY;
391 goto error_ret;
392 }
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000393 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100394 if (ret < 0)
395 goto error_ret;
396 if (!state && ret) {
397 ret = iio_scan_mask_clear(buffer, this_attr->address);
398 if (ret)
399 goto error_ret;
400 } else if (state && !ret) {
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000401 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100402 if (ret)
403 goto error_ret;
404 }
405
406error_ret:
407 mutex_unlock(&indio_dev->mlock);
408
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100409 return ret < 0 ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100410
411}
412
413static ssize_t iio_scan_el_ts_show(struct device *dev,
414 struct device_attribute *attr,
415 char *buf)
416{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200417 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300418 struct iio_buffer *buffer = indio_dev->buffer;
419
420 return sprintf(buf, "%d\n", buffer->scan_timestamp);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100421}
422
423static ssize_t iio_scan_el_ts_store(struct device *dev,
424 struct device_attribute *attr,
425 const char *buf,
426 size_t len)
427{
Jonathan Camerona714af22012-04-21 10:09:32 +0100428 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200429 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300430 struct iio_buffer *buffer = indio_dev->buffer;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100431 bool state;
432
Jonathan Camerona714af22012-04-21 10:09:32 +0100433 ret = strtobool(buf, &state);
434 if (ret < 0)
435 return ret;
436
Jonathan Cameron14555b12011-09-21 11:15:57 +0100437 mutex_lock(&indio_dev->mlock);
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300438 if (iio_buffer_is_active(buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100439 ret = -EBUSY;
440 goto error_ret;
441 }
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300442 buffer->scan_timestamp = state;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100443error_ret:
444 mutex_unlock(&indio_dev->mlock);
445
446 return ret ? ret : len;
447}
448
449static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300450 struct iio_buffer *buffer,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100451 const struct iio_chan_spec *chan)
452{
453 int ret, attrcount = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100454
455 ret = __iio_add_chan_devattr("index",
456 chan,
457 &iio_show_scan_index,
458 NULL,
459 0,
Jonathan Cameron37044322013-09-08 14:57:00 +0100460 IIO_SEPARATE,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100461 &indio_dev->dev,
462 &buffer->scan_el_dev_attr_list);
463 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000464 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100465 attrcount++;
466 ret = __iio_add_chan_devattr("type",
467 chan,
468 &iio_show_fixed_type,
469 NULL,
470 0,
471 0,
472 &indio_dev->dev,
473 &buffer->scan_el_dev_attr_list);
474 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000475 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100476 attrcount++;
477 if (chan->type != IIO_TIMESTAMP)
478 ret = __iio_add_chan_devattr("en",
479 chan,
480 &iio_scan_el_show,
481 &iio_scan_el_store,
482 chan->scan_index,
483 0,
484 &indio_dev->dev,
485 &buffer->scan_el_dev_attr_list);
486 else
487 ret = __iio_add_chan_devattr("en",
488 chan,
489 &iio_scan_el_ts_show,
490 &iio_scan_el_ts_store,
491 chan->scan_index,
492 0,
493 &indio_dev->dev,
494 &buffer->scan_el_dev_attr_list);
Peter Meerwald95725882013-09-17 23:42:00 +0100495 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000496 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100497 attrcount++;
498 ret = attrcount;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100499 return ret;
500}
501
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100502static ssize_t iio_buffer_read_length(struct device *dev,
503 struct device_attribute *attr,
504 char *buf)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100505{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200506 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100507 struct iio_buffer *buffer = indio_dev->buffer;
508
Lars-Peter Clausen37495662014-11-26 18:55:17 +0100509 return sprintf(buf, "%d\n", buffer->length);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100510}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100511
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100512static ssize_t iio_buffer_write_length(struct device *dev,
513 struct device_attribute *attr,
514 const char *buf, size_t len)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100515{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200516 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100517 struct iio_buffer *buffer = indio_dev->buffer;
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100518 unsigned int val;
519 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100520
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100521 ret = kstrtouint(buf, 10, &val);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100522 if (ret)
523 return ret;
524
Lars-Peter Clausen37495662014-11-26 18:55:17 +0100525 if (val == buffer->length)
526 return len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100527
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100528 mutex_lock(&indio_dev->mlock);
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300529 if (iio_buffer_is_active(buffer)) {
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100530 ret = -EBUSY;
531 } else {
Lars-Peter Clausen8d92db22014-11-26 18:55:16 +0100532 buffer->access->set_length(buffer, val);
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100533 ret = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100534 }
Josselin Costanzi37d34552015-03-22 20:33:38 +0200535 if (ret)
536 goto out;
537 if (buffer->length && buffer->length < buffer->watermark)
538 buffer->watermark = buffer->length;
539out:
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100540 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100541
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100542 return ret ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100543}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100544
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100545static ssize_t iio_buffer_show_enable(struct device *dev,
546 struct device_attribute *attr,
547 char *buf)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100548{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200549 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +0300550 struct iio_buffer *buffer = indio_dev->buffer;
551
552 return sprintf(buf, "%d\n", iio_buffer_is_active(buffer));
Jonathan Cameron14555b12011-09-21 11:15:57 +0100553}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100554
Lars-Peter Clausen182b4902016-02-02 16:27:24 +0100555static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev,
556 unsigned int scan_index)
557{
558 const struct iio_chan_spec *ch;
559 unsigned int bytes;
560
561 ch = iio_find_channel_from_si(indio_dev, scan_index);
562 bytes = ch->scan_type.storagebits / 8;
563 if (ch->scan_type.repeat > 1)
564 bytes *= ch->scan_type.repeat;
565 return bytes;
566}
567
568static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev)
569{
570 return iio_storage_bytes_for_si(indio_dev,
571 indio_dev->scan_index_timestamp);
572}
573
Peter Meerwald183f4172013-09-18 22:10:00 +0100574static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
575 const unsigned long *mask, bool timestamp)
Jonathan Cameron959d2952011-12-05 21:37:13 +0000576{
Jonathan Cameron959d2952011-12-05 21:37:13 +0000577 unsigned bytes = 0;
Lars Möllendorf883f6162019-12-13 14:50:55 +0100578 int length, i, largest = 0;
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100579
580 /* How much space will the demuxed element take? */
581 for_each_set_bit(i, mask,
582 indio_dev->masklength) {
Lars-Peter Clausen182b4902016-02-02 16:27:24 +0100583 length = iio_storage_bytes_for_si(indio_dev, i);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100584 bytes = ALIGN(bytes, length);
585 bytes += length;
Lars Möllendorf883f6162019-12-13 14:50:55 +0100586 largest = max(largest, length);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100587 }
Lars-Peter Clausen182b4902016-02-02 16:27:24 +0100588
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100589 if (timestamp) {
Lars-Peter Clausen182b4902016-02-02 16:27:24 +0100590 length = iio_storage_bytes_for_timestamp(indio_dev);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100591 bytes = ALIGN(bytes, length);
592 bytes += length;
Lars Möllendorf883f6162019-12-13 14:50:55 +0100593 largest = max(largest, length);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100594 }
Lars Möllendorf883f6162019-12-13 14:50:55 +0100595
596 bytes = ALIGN(bytes, largest);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100597 return bytes;
598}
599
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100600static void iio_buffer_activate(struct iio_dev *indio_dev,
601 struct iio_buffer *buffer)
602{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300603 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
604
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100605 iio_buffer_get(buffer);
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300606 list_add(&buffer->buffer_list, &iio_dev_opaque->buffer_list);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100607}
608
609static void iio_buffer_deactivate(struct iio_buffer *buffer)
610{
611 list_del_init(&buffer->buffer_list);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200612 wake_up_interruptible(&buffer->pollq);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100613 iio_buffer_put(buffer);
614}
615
Lars-Peter Clausen12501862015-05-18 13:34:49 +0200616static void iio_buffer_deactivate_all(struct iio_dev *indio_dev)
617{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300618 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Lars-Peter Clausen12501862015-05-18 13:34:49 +0200619 struct iio_buffer *buffer, *_buffer;
620
621 list_for_each_entry_safe(buffer, _buffer,
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300622 &iio_dev_opaque->buffer_list, buffer_list)
Lars-Peter Clausen12501862015-05-18 13:34:49 +0200623 iio_buffer_deactivate(buffer);
624}
625
Lars-Peter Clausene18a2ad2015-10-13 18:10:27 +0200626static int iio_buffer_enable(struct iio_buffer *buffer,
627 struct iio_dev *indio_dev)
628{
629 if (!buffer->access->enable)
630 return 0;
631 return buffer->access->enable(buffer, indio_dev);
632}
633
634static int iio_buffer_disable(struct iio_buffer *buffer,
635 struct iio_dev *indio_dev)
636{
637 if (!buffer->access->disable)
638 return 0;
639 return buffer->access->disable(buffer, indio_dev);
640}
641
Lars-Peter Clausen8e050992013-10-14 17:49:00 +0100642static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
643 struct iio_buffer *buffer)
644{
645 unsigned int bytes;
646
647 if (!buffer->access->set_bytes_per_datum)
648 return;
649
650 bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
651 buffer->scan_timestamp);
652
653 buffer->access->set_bytes_per_datum(buffer, bytes);
654}
655
Lars-Peter Clausenfcc1b2f2015-05-13 16:04:46 +0200656static int iio_buffer_request_update(struct iio_dev *indio_dev,
657 struct iio_buffer *buffer)
658{
659 int ret;
660
661 iio_buffer_update_bytes_per_datum(indio_dev, buffer);
662 if (buffer->access->request_update) {
663 ret = buffer->access->request_update(buffer);
664 if (ret) {
665 dev_dbg(&indio_dev->dev,
666 "Buffer not started: buffer parameter update failed (%d)\n",
667 ret);
668 return ret;
669 }
670 }
671
672 return 0;
673}
674
Lars-Peter Clausen248be5a2015-05-13 16:04:45 +0200675static void iio_free_scan_mask(struct iio_dev *indio_dev,
676 const unsigned long *mask)
677{
678 /* If the mask is dynamically allocated free it, otherwise do nothing */
679 if (!indio_dev->available_scan_masks)
Andy Shevchenko38628282019-03-04 10:55:40 +0200680 bitmap_free(mask);
Lars-Peter Clausen248be5a2015-05-13 16:04:45 +0200681}
682
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200683struct iio_device_config {
684 unsigned int mode;
Lars-Peter Clausenf0566c02015-10-13 18:10:24 +0200685 unsigned int watermark;
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200686 const unsigned long *scan_mask;
687 unsigned int scan_bytes;
688 bool scan_timestamp;
689};
690
691static int iio_verify_update(struct iio_dev *indio_dev,
692 struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer,
693 struct iio_device_config *config)
694{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300695 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200696 unsigned long *compound_mask;
697 const unsigned long *scan_mask;
Lars-Peter Clausen1e1ec282015-05-29 18:14:22 +0200698 bool strict_scanmask = false;
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200699 struct iio_buffer *buffer;
700 bool scan_timestamp;
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200701 unsigned int modes;
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200702
Lars-Peter Clausenb7329242020-03-26 11:30:12 +0200703 if (insert_buffer &&
704 bitmap_empty(insert_buffer->scan_mask, indio_dev->masklength)) {
705 dev_dbg(&indio_dev->dev,
706 "At least one scan element must be enabled first\n");
707 return -EINVAL;
708 }
709
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200710 memset(config, 0, sizeof(*config));
Irina Tirdea1bef2c12016-03-24 11:09:45 +0200711 config->watermark = ~0;
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200712
713 /*
714 * If there is just one buffer and we are removing it there is nothing
715 * to verify.
716 */
717 if (remove_buffer && !insert_buffer &&
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300718 list_is_singular(&iio_dev_opaque->buffer_list))
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200719 return 0;
720
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200721 modes = indio_dev->modes;
722
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300723 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200724 if (buffer == remove_buffer)
725 continue;
726 modes &= buffer->access->modes;
Lars-Peter Clausenf0566c02015-10-13 18:10:24 +0200727 config->watermark = min(config->watermark, buffer->watermark);
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200728 }
729
Lars-Peter Clausenf0566c02015-10-13 18:10:24 +0200730 if (insert_buffer) {
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200731 modes &= insert_buffer->access->modes;
Lars-Peter Clausenf0566c02015-10-13 18:10:24 +0200732 config->watermark = min(config->watermark,
733 insert_buffer->watermark);
734 }
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200735
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200736 /* Definitely possible for devices to support both of these. */
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200737 if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) {
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200738 config->mode = INDIO_BUFFER_TRIGGERED;
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200739 } else if (modes & INDIO_BUFFER_HARDWARE) {
Lars-Peter Clausen1e1ec282015-05-29 18:14:22 +0200740 /*
741 * Keep things simple for now and only allow a single buffer to
742 * be connected in hardware mode.
743 */
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300744 if (insert_buffer && !list_empty(&iio_dev_opaque->buffer_list))
Lars-Peter Clausen1e1ec282015-05-29 18:14:22 +0200745 return -EINVAL;
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200746 config->mode = INDIO_BUFFER_HARDWARE;
Lars-Peter Clausen1e1ec282015-05-29 18:14:22 +0200747 strict_scanmask = true;
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200748 } else if (modes & INDIO_BUFFER_SOFTWARE) {
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200749 config->mode = INDIO_BUFFER_SOFTWARE;
750 } else {
751 /* Can only occur on first buffer */
752 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
753 dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n");
754 return -EINVAL;
755 }
756
757 /* What scan mask do we actually have? */
Andy Shevchenko38628282019-03-04 10:55:40 +0200758 compound_mask = bitmap_zalloc(indio_dev->masklength, GFP_KERNEL);
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200759 if (compound_mask == NULL)
760 return -ENOMEM;
761
762 scan_timestamp = false;
763
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300764 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200765 if (buffer == remove_buffer)
766 continue;
767 bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
768 indio_dev->masklength);
769 scan_timestamp |= buffer->scan_timestamp;
770 }
771
772 if (insert_buffer) {
773 bitmap_or(compound_mask, compound_mask,
774 insert_buffer->scan_mask, indio_dev->masklength);
775 scan_timestamp |= insert_buffer->scan_timestamp;
776 }
777
778 if (indio_dev->available_scan_masks) {
779 scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks,
780 indio_dev->masklength,
Lars-Peter Clausen1e1ec282015-05-29 18:14:22 +0200781 compound_mask,
782 strict_scanmask);
Andy Shevchenko38628282019-03-04 10:55:40 +0200783 bitmap_free(compound_mask);
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +0200784 if (scan_mask == NULL)
785 return -EINVAL;
786 } else {
787 scan_mask = compound_mask;
788 }
789
790 config->scan_bytes = iio_compute_scan_bytes(indio_dev,
791 scan_mask, scan_timestamp);
792 config->scan_mask = scan_mask;
793 config->scan_timestamp = scan_timestamp;
794
795 return 0;
796}
797
Jonathan Cameron78c99812017-01-02 19:28:24 +0000798/**
799 * struct iio_demux_table - table describing demux memcpy ops
800 * @from: index to copy from
801 * @to: index to copy to
802 * @length: how many bytes to copy
803 * @l: list head used for management
804 */
805struct iio_demux_table {
806 unsigned from;
807 unsigned to;
808 unsigned length;
809 struct list_head l;
810};
811
812static void iio_buffer_demux_free(struct iio_buffer *buffer)
813{
814 struct iio_demux_table *p, *q;
815 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
816 list_del(&p->l);
817 kfree(p);
818 }
819}
820
821static int iio_buffer_add_demux(struct iio_buffer *buffer,
822 struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc,
823 unsigned int length)
824{
825
826 if (*p && (*p)->from + (*p)->length == in_loc &&
827 (*p)->to + (*p)->length == out_loc) {
828 (*p)->length += length;
829 } else {
830 *p = kmalloc(sizeof(**p), GFP_KERNEL);
831 if (*p == NULL)
832 return -ENOMEM;
833 (*p)->from = in_loc;
834 (*p)->to = out_loc;
835 (*p)->length = length;
836 list_add_tail(&(*p)->l, &buffer->demux_list);
837 }
838
839 return 0;
840}
841
842static int iio_buffer_update_demux(struct iio_dev *indio_dev,
843 struct iio_buffer *buffer)
844{
845 int ret, in_ind = -1, out_ind, length;
846 unsigned in_loc = 0, out_loc = 0;
847 struct iio_demux_table *p = NULL;
848
849 /* Clear out any old demux */
850 iio_buffer_demux_free(buffer);
851 kfree(buffer->demux_bounce);
852 buffer->demux_bounce = NULL;
853
854 /* First work out which scan mode we will actually have */
855 if (bitmap_equal(indio_dev->active_scan_mask,
856 buffer->scan_mask,
857 indio_dev->masklength))
858 return 0;
859
860 /* Now we have the two masks, work from least sig and build up sizes */
861 for_each_set_bit(out_ind,
862 buffer->scan_mask,
863 indio_dev->masklength) {
864 in_ind = find_next_bit(indio_dev->active_scan_mask,
865 indio_dev->masklength,
866 in_ind + 1);
867 while (in_ind != out_ind) {
868 in_ind = find_next_bit(indio_dev->active_scan_mask,
869 indio_dev->masklength,
870 in_ind + 1);
871 length = iio_storage_bytes_for_si(indio_dev, in_ind);
872 /* Make sure we are aligned */
873 in_loc = roundup(in_loc, length) + length;
874 }
875 length = iio_storage_bytes_for_si(indio_dev, in_ind);
876 out_loc = roundup(out_loc, length);
877 in_loc = roundup(in_loc, length);
878 ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
879 if (ret)
880 goto error_clear_mux_table;
881 out_loc += length;
882 in_loc += length;
883 }
884 /* Relies on scan_timestamp being last */
885 if (buffer->scan_timestamp) {
886 length = iio_storage_bytes_for_timestamp(indio_dev);
887 out_loc = roundup(out_loc, length);
888 in_loc = roundup(in_loc, length);
889 ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
890 if (ret)
891 goto error_clear_mux_table;
892 out_loc += length;
893 in_loc += length;
894 }
895 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
896 if (buffer->demux_bounce == NULL) {
897 ret = -ENOMEM;
898 goto error_clear_mux_table;
899 }
900 return 0;
901
902error_clear_mux_table:
903 iio_buffer_demux_free(buffer);
904
905 return ret;
906}
907
908static int iio_update_demux(struct iio_dev *indio_dev)
909{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300910 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameron78c99812017-01-02 19:28:24 +0000911 struct iio_buffer *buffer;
912 int ret;
913
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300914 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
Jonathan Cameron78c99812017-01-02 19:28:24 +0000915 ret = iio_buffer_update_demux(indio_dev, buffer);
916 if (ret < 0)
917 goto error_clear_mux_table;
918 }
919 return 0;
920
921error_clear_mux_table:
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300922 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list)
Jonathan Cameron78c99812017-01-02 19:28:24 +0000923 iio_buffer_demux_free(buffer);
924
925 return ret;
926}
927
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +0200928static int iio_enable_buffers(struct iio_dev *indio_dev,
929 struct iio_device_config *config)
930{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300931 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Lars-Peter Clausene18a2ad2015-10-13 18:10:27 +0200932 struct iio_buffer *buffer;
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +0200933 int ret;
934
935 indio_dev->active_scan_mask = config->scan_mask;
936 indio_dev->scan_timestamp = config->scan_timestamp;
937 indio_dev->scan_bytes = config->scan_bytes;
Lars-Peter Clausen5cb1a542020-04-30 11:24:55 +0300938 indio_dev->currentmode = config->mode;
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +0200939
940 iio_update_demux(indio_dev);
941
942 /* Wind up again */
943 if (indio_dev->setup_ops->preenable) {
944 ret = indio_dev->setup_ops->preenable(indio_dev);
945 if (ret) {
946 dev_dbg(&indio_dev->dev,
947 "Buffer not started: buffer preenable failed (%d)\n", ret);
948 goto err_undo_config;
949 }
950 }
951
952 if (indio_dev->info->update_scan_mode) {
953 ret = indio_dev->info
954 ->update_scan_mode(indio_dev,
955 indio_dev->active_scan_mask);
956 if (ret < 0) {
957 dev_dbg(&indio_dev->dev,
958 "Buffer not started: update scan mode failed (%d)\n",
959 ret);
960 goto err_run_postdisable;
961 }
962 }
963
Lars-Peter Clausenf0566c02015-10-13 18:10:24 +0200964 if (indio_dev->info->hwfifo_set_watermark)
965 indio_dev->info->hwfifo_set_watermark(indio_dev,
966 config->watermark);
967
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300968 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
Lars-Peter Clausene18a2ad2015-10-13 18:10:27 +0200969 ret = iio_buffer_enable(buffer, indio_dev);
970 if (ret)
971 goto err_disable_buffers;
972 }
973
Lars-Peter Clausenf11d59d2020-05-25 14:38:53 +0300974 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
975 ret = iio_trigger_attach_poll_func(indio_dev->trig,
976 indio_dev->pollfunc);
977 if (ret)
978 goto err_disable_buffers;
979 }
980
Alexandru Ardelean62a30a22020-07-15 07:16:29 +0300981 if (indio_dev->setup_ops->postenable) {
982 ret = indio_dev->setup_ops->postenable(indio_dev);
983 if (ret) {
984 dev_dbg(&indio_dev->dev,
985 "Buffer not started: postenable failed (%d)\n", ret);
986 goto err_detach_pollfunc;
987 }
988 }
989
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +0200990 return 0;
991
Alexandru Ardelean62a30a22020-07-15 07:16:29 +0300992err_detach_pollfunc:
993 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
994 iio_trigger_detach_poll_func(indio_dev->trig,
995 indio_dev->pollfunc);
996 }
Lars-Peter Clausene18a2ad2015-10-13 18:10:27 +0200997err_disable_buffers:
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +0300998 list_for_each_entry_continue_reverse(buffer, &iio_dev_opaque->buffer_list,
Lars-Peter Clausene18a2ad2015-10-13 18:10:27 +0200999 buffer_list)
1000 iio_buffer_disable(buffer, indio_dev);
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001001err_run_postdisable:
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001002 if (indio_dev->setup_ops->postdisable)
1003 indio_dev->setup_ops->postdisable(indio_dev);
1004err_undo_config:
Lars-Peter Clausen5cb1a542020-04-30 11:24:55 +03001005 indio_dev->currentmode = INDIO_DIRECT_MODE;
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001006 indio_dev->active_scan_mask = NULL;
1007
1008 return ret;
1009}
1010
1011static int iio_disable_buffers(struct iio_dev *indio_dev)
1012{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001013 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Lars-Peter Clausene18a2ad2015-10-13 18:10:27 +02001014 struct iio_buffer *buffer;
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001015 int ret = 0;
1016 int ret2;
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001017
1018 /* Wind down existing buffers - iff there are any */
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001019 if (list_empty(&iio_dev_opaque->buffer_list))
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001020 return 0;
1021
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001022 /*
1023 * If things go wrong at some step in disable we still need to continue
1024 * to perform the other steps, otherwise we leave the device in a
1025 * inconsistent state. We return the error code for the first error we
1026 * encountered.
1027 */
1028
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001029 if (indio_dev->setup_ops->predisable) {
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001030 ret2 = indio_dev->setup_ops->predisable(indio_dev);
1031 if (ret2 && !ret)
1032 ret = ret2;
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001033 }
1034
Alexandru Ardelean62a30a22020-07-15 07:16:29 +03001035 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
1036 iio_trigger_detach_poll_func(indio_dev->trig,
1037 indio_dev->pollfunc);
1038 }
1039
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001040 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
Lars-Peter Clausene18a2ad2015-10-13 18:10:27 +02001041 ret2 = iio_buffer_disable(buffer, indio_dev);
1042 if (ret2 && !ret)
1043 ret = ret2;
1044 }
1045
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001046 if (indio_dev->setup_ops->postdisable) {
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001047 ret2 = indio_dev->setup_ops->postdisable(indio_dev);
1048 if (ret2 && !ret)
1049 ret = ret2;
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001050 }
1051
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001052 iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask);
1053 indio_dev->active_scan_mask = NULL;
Lars-Peter Clausen5cb1a542020-04-30 11:24:55 +03001054 indio_dev->currentmode = INDIO_DIRECT_MODE;
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001055
1056 return ret;
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001057}
1058
Lars-Peter Clausena9519452013-10-04 12:07:00 +01001059static int __iio_update_buffers(struct iio_dev *indio_dev,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001060 struct iio_buffer *insert_buffer,
1061 struct iio_buffer *remove_buffer)
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +01001062{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001063 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +02001064 struct iio_device_config new_config;
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001065 int ret;
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +02001066
1067 ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer,
1068 &new_config);
1069 if (ret)
1070 return ret;
Jonathan Cameron959d2952011-12-05 21:37:13 +00001071
Lars-Peter Clausenfcc1b2f2015-05-13 16:04:46 +02001072 if (insert_buffer) {
1073 ret = iio_buffer_request_update(indio_dev, insert_buffer);
1074 if (ret)
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +02001075 goto err_free_config;
Lars-Peter Clausenfcc1b2f2015-05-13 16:04:46 +02001076 }
1077
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001078 ret = iio_disable_buffers(indio_dev);
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001079 if (ret)
1080 goto err_deactivate_all;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001081
1082 if (remove_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +01001083 iio_buffer_deactivate(remove_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001084 if (insert_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +01001085 iio_buffer_activate(indio_dev, insert_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001086
1087 /* If no buffers in list, we are done */
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001088 if (list_empty(&iio_dev_opaque->buffer_list))
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001089 return 0;
Jonathan Cameron959d2952011-12-05 21:37:13 +00001090
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001091 ret = iio_enable_buffers(indio_dev, &new_config);
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001092 if (ret)
1093 goto err_deactivate_all;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001094
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +02001095 return 0;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001096
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001097err_deactivate_all:
1098 /*
1099 * We've already verified that the config is valid earlier. If things go
1100 * wrong in either enable or disable the most likely reason is an IO
1101 * error from the device. In this case there is no good recovery
1102 * strategy. Just make sure to disable everything and leave the device
1103 * in a sane state. With a bit of luck the device might come back to
1104 * life again later and userspace can try again.
1105 */
1106 iio_buffer_deactivate_all(indio_dev);
1107
Lars-Peter Clausen6e509c42015-05-18 13:34:47 +02001108err_free_config:
1109 iio_free_scan_mask(indio_dev, new_config.scan_mask);
1110 return ret;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001111}
Lars-Peter Clausena9519452013-10-04 12:07:00 +01001112
1113int iio_update_buffers(struct iio_dev *indio_dev,
1114 struct iio_buffer *insert_buffer,
1115 struct iio_buffer *remove_buffer)
1116{
1117 int ret;
1118
Lars-Peter Clausen3909fab2013-10-04 12:07:00 +01001119 if (insert_buffer == remove_buffer)
1120 return 0;
1121
Lars-Peter Clausena9519452013-10-04 12:07:00 +01001122 mutex_lock(&indio_dev->info_exist_lock);
1123 mutex_lock(&indio_dev->mlock);
1124
Lars-Peter Clausen3909fab2013-10-04 12:07:00 +01001125 if (insert_buffer && iio_buffer_is_active(insert_buffer))
1126 insert_buffer = NULL;
1127
1128 if (remove_buffer && !iio_buffer_is_active(remove_buffer))
1129 remove_buffer = NULL;
1130
1131 if (!insert_buffer && !remove_buffer) {
1132 ret = 0;
1133 goto out_unlock;
1134 }
1135
Lars-Peter Clausena9519452013-10-04 12:07:00 +01001136 if (indio_dev->info == NULL) {
1137 ret = -ENODEV;
1138 goto out_unlock;
1139 }
1140
1141 ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
1142
1143out_unlock:
1144 mutex_unlock(&indio_dev->mlock);
1145 mutex_unlock(&indio_dev->info_exist_lock);
1146
1147 return ret;
1148}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001149EXPORT_SYMBOL_GPL(iio_update_buffers);
1150
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001151void iio_disable_all_buffers(struct iio_dev *indio_dev)
1152{
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001153 iio_disable_buffers(indio_dev);
Lars-Peter Clausen12501862015-05-18 13:34:49 +02001154 iio_buffer_deactivate_all(indio_dev);
Lars-Peter Clausen623d74e2015-05-18 13:34:48 +02001155}
1156
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +01001157static ssize_t iio_buffer_store_enable(struct device *dev,
1158 struct device_attribute *attr,
1159 const char *buf,
1160 size_t len)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001161{
1162 int ret;
1163 bool requested_state;
1164 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +03001165 struct iio_buffer *buffer = indio_dev->buffer;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001166 bool inlist;
1167
1168 ret = strtobool(buf, &requested_state);
1169 if (ret < 0)
1170 return ret;
1171
1172 mutex_lock(&indio_dev->mlock);
1173
1174 /* Find out if it is in the list */
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +03001175 inlist = iio_buffer_is_active(buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001176 /* Already in desired state */
1177 if (inlist == requested_state)
1178 goto done;
1179
1180 if (requested_state)
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +03001181 ret = __iio_update_buffers(indio_dev, buffer, NULL);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001182 else
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +03001183 ret = __iio_update_buffers(indio_dev, NULL, buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001184
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001185done:
1186 mutex_unlock(&indio_dev->mlock);
1187 return (ret < 0) ? ret : len;
1188}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001189
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001190static const char * const iio_scan_elements_group_name = "scan_elements";
1191
Josselin Costanzi37d34552015-03-22 20:33:38 +02001192static ssize_t iio_buffer_show_watermark(struct device *dev,
1193 struct device_attribute *attr,
1194 char *buf)
1195{
1196 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1197 struct iio_buffer *buffer = indio_dev->buffer;
1198
1199 return sprintf(buf, "%u\n", buffer->watermark);
1200}
1201
1202static ssize_t iio_buffer_store_watermark(struct device *dev,
1203 struct device_attribute *attr,
1204 const char *buf,
1205 size_t len)
1206{
1207 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1208 struct iio_buffer *buffer = indio_dev->buffer;
1209 unsigned int val;
1210 int ret;
1211
1212 ret = kstrtouint(buf, 10, &val);
1213 if (ret)
1214 return ret;
1215 if (!val)
1216 return -EINVAL;
1217
1218 mutex_lock(&indio_dev->mlock);
1219
1220 if (val > buffer->length) {
1221 ret = -EINVAL;
1222 goto out;
1223 }
1224
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +03001225 if (iio_buffer_is_active(buffer)) {
Josselin Costanzi37d34552015-03-22 20:33:38 +02001226 ret = -EBUSY;
1227 goto out;
1228 }
1229
1230 buffer->watermark = val;
1231out:
1232 mutex_unlock(&indio_dev->mlock);
1233
1234 return ret ? ret : len;
1235}
1236
Matt Fornero350f6c72017-12-06 14:43:30 -05001237static ssize_t iio_dma_show_data_available(struct device *dev,
1238 struct device_attribute *attr,
1239 char *buf)
1240{
1241 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +03001242 struct iio_buffer *buffer = indio_dev->buffer;
Matt Fornero350f6c72017-12-06 14:43:30 -05001243
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +03001244 return sprintf(buf, "%zu\n", iio_buffer_data_available(buffer));
Matt Fornero350f6c72017-12-06 14:43:30 -05001245}
1246
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +01001247static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
1248 iio_buffer_write_length);
Lars-Peter Clausen8d92db22014-11-26 18:55:16 +01001249static struct device_attribute dev_attr_length_ro = __ATTR(length,
1250 S_IRUGO, iio_buffer_read_length, NULL);
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +01001251static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
1252 iio_buffer_show_enable, iio_buffer_store_enable);
Josselin Costanzi37d34552015-03-22 20:33:38 +02001253static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR,
1254 iio_buffer_show_watermark, iio_buffer_store_watermark);
Lars-Peter Clausenb4406552015-10-13 18:10:26 +02001255static struct device_attribute dev_attr_watermark_ro = __ATTR(watermark,
1256 S_IRUGO, iio_buffer_show_watermark, NULL);
Matt Fornero350f6c72017-12-06 14:43:30 -05001257static DEVICE_ATTR(data_available, S_IRUGO,
1258 iio_dma_show_data_available, NULL);
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +01001259
Octavian Purdila6da9b382015-01-31 02:00:00 +02001260static struct attribute *iio_buffer_attrs[] = {
1261 &dev_attr_length.attr,
1262 &dev_attr_enable.attr,
Josselin Costanzi37d34552015-03-22 20:33:38 +02001263 &dev_attr_watermark.attr,
Matt Fornero350f6c72017-12-06 14:43:30 -05001264 &dev_attr_data_available.attr,
Octavian Purdila6da9b382015-01-31 02:00:00 +02001265};
1266
Alexandru Ardeleane16e0a772020-09-17 15:59:51 +03001267static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
1268 struct iio_dev *indio_dev)
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001269{
1270 struct iio_dev_attr *p;
1271 struct attribute **attr;
Alexandru Ardelean96144d42020-05-11 15:53:22 +03001272 int ret, i, attrn, attrcount;
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001273 const struct iio_chan_spec *channels;
1274
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +01001275 attrcount = 0;
1276 if (buffer->attrs) {
1277 while (buffer->attrs[attrcount] != NULL)
1278 attrcount++;
1279 }
1280
Octavian Purdila6da9b382015-01-31 02:00:00 +02001281 attr = kcalloc(attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1,
1282 sizeof(struct attribute *), GFP_KERNEL);
1283 if (!attr)
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +01001284 return -ENOMEM;
1285
Octavian Purdila6da9b382015-01-31 02:00:00 +02001286 memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
1287 if (!buffer->access->set_length)
1288 attr[0] = &dev_attr_length_ro.attr;
1289
Lars-Peter Clausenb4406552015-10-13 18:10:26 +02001290 if (buffer->access->flags & INDIO_BUFFER_FLAG_FIXED_WATERMARK)
1291 attr[2] = &dev_attr_watermark_ro.attr;
1292
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +01001293 if (buffer->attrs)
Octavian Purdila6da9b382015-01-31 02:00:00 +02001294 memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs,
1295 sizeof(struct attribute *) * attrcount);
1296
1297 attr[attrcount + ARRAY_SIZE(iio_buffer_attrs)] = NULL;
1298
1299 buffer->buffer_group.name = "buffer";
1300 buffer->buffer_group.attrs = attr;
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +01001301
1302 indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group;
1303
Alexandru Ardelean96144d42020-05-11 15:53:22 +03001304 attrcount = 0;
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001305 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
1306 channels = indio_dev->channels;
1307 if (channels) {
1308 /* new magic */
1309 for (i = 0; i < indio_dev->num_channels; i++) {
1310 if (channels[i].scan_index < 0)
1311 continue;
1312
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +03001313 ret = iio_buffer_add_channel_sysfs(indio_dev, buffer,
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001314 &channels[i]);
1315 if (ret < 0)
1316 goto error_cleanup_dynamic;
1317 attrcount += ret;
1318 if (channels[i].type == IIO_TIMESTAMP)
1319 indio_dev->scan_index_timestamp =
1320 channels[i].scan_index;
1321 }
1322 if (indio_dev->masklength && buffer->scan_mask == NULL) {
Andy Shevchenko38628282019-03-04 10:55:40 +02001323 buffer->scan_mask = bitmap_zalloc(indio_dev->masklength,
1324 GFP_KERNEL);
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001325 if (buffer->scan_mask == NULL) {
1326 ret = -ENOMEM;
1327 goto error_cleanup_dynamic;
1328 }
1329 }
1330 }
1331
1332 buffer->scan_el_group.name = iio_scan_elements_group_name;
1333
1334 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
1335 sizeof(buffer->scan_el_group.attrs[0]),
1336 GFP_KERNEL);
1337 if (buffer->scan_el_group.attrs == NULL) {
1338 ret = -ENOMEM;
1339 goto error_free_scan_mask;
1340 }
Alexandru Ardelean96144d42020-05-11 15:53:22 +03001341 attrn = 0;
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001342
1343 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
1344 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
1345 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
1346
1347 return 0;
1348
1349error_free_scan_mask:
Andy Shevchenko38628282019-03-04 10:55:40 +02001350 bitmap_free(buffer->scan_mask);
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001351error_cleanup_dynamic:
1352 iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +03001353 kfree(buffer->buffer_group.attrs);
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001354
1355 return ret;
1356}
1357
Alexandru Ardeleane16e0a772020-09-17 15:59:51 +03001358int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
1359{
1360 struct iio_buffer *buffer = indio_dev->buffer;
1361 const struct iio_chan_spec *channels;
1362 int i;
1363
1364 channels = indio_dev->channels;
1365 if (channels) {
1366 int ml = indio_dev->masklength;
1367
1368 for (i = 0; i < indio_dev->num_channels; i++)
1369 ml = max(ml, channels[i].scan_index + 1);
1370 indio_dev->masklength = ml;
1371 }
1372
1373 if (!buffer)
1374 return 0;
1375
1376 return __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev);
1377}
1378
1379static void __iio_buffer_free_sysfs_and_mask(struct iio_buffer *buffer)
1380{
1381 bitmap_free(buffer->scan_mask);
1382 kfree(buffer->buffer_group.attrs);
1383 kfree(buffer->scan_el_group.attrs);
1384 iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
1385}
1386
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001387void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
1388{
Alexandru Ardeleanff3f7e042020-04-24 18:22:43 +03001389 struct iio_buffer *buffer = indio_dev->buffer;
1390
1391 if (!buffer)
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001392 return;
1393
Alexandru Ardeleane16e0a772020-09-17 15:59:51 +03001394 __iio_buffer_free_sysfs_and_mask(buffer);
Lars-Peter Clausend967cb62014-11-26 18:55:14 +01001395}
1396
Jonathan Cameron14555b12011-09-21 11:15:57 +01001397/**
Lars-Peter Clausen81636632012-07-09 10:00:00 +01001398 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
1399 * @indio_dev: the iio device
1400 * @mask: scan mask to be checked
1401 *
1402 * Return true if exactly one bit is set in the scan mask, false otherwise. It
1403 * can be used for devices where only one channel can be active for sampling at
1404 * a time.
1405 */
1406bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
1407 const unsigned long *mask)
1408{
1409 return bitmap_weight(mask, indio_dev->masklength) == 1;
1410}
1411EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
1412
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +01001413static const void *iio_demux(struct iio_buffer *buffer,
1414 const void *datain)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001415{
1416 struct iio_demux_table *t;
1417
1418 if (list_empty(&buffer->demux_list))
1419 return datain;
1420 list_for_each_entry(t, &buffer->demux_list, l)
1421 memcpy(buffer->demux_bounce + t->to,
1422 datain + t->from, t->length);
1423
1424 return buffer->demux_bounce;
1425}
1426
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +01001427static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001428{
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +01001429 const void *dataout = iio_demux(buffer, data);
Josselin Costanzi37d34552015-03-22 20:33:38 +02001430 int ret;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001431
Josselin Costanzi37d34552015-03-22 20:33:38 +02001432 ret = buffer->access->store_to(buffer, dataout);
1433 if (ret)
1434 return ret;
1435
1436 /*
1437 * We can't just test for watermark to decide if we wake the poll queue
1438 * because read may request less samples than the watermark.
1439 */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001440 wake_up_interruptible_poll(&buffer->pollq, EPOLLIN | EPOLLRDNORM);
Josselin Costanzi37d34552015-03-22 20:33:38 +02001441 return 0;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001442}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001443
Jonathan Cameron315a19e2017-01-02 19:28:28 +00001444/**
1445 * iio_push_to_buffers() - push to a registered buffer.
1446 * @indio_dev: iio_dev structure for device.
1447 * @data: Full scan.
1448 */
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +01001449int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001450{
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001451 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001452 int ret;
1453 struct iio_buffer *buf;
1454
Alexandru Ardelean6a8c6b22020-06-30 07:57:07 +03001455 list_for_each_entry(buf, &iio_dev_opaque->buffer_list, buffer_list) {
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001456 ret = iio_push_to_buffer(buf, data);
1457 if (ret < 0)
1458 return ret;
1459 }
1460
1461 return 0;
1462}
1463EXPORT_SYMBOL_GPL(iio_push_to_buffers);
1464
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +01001465/**
1466 * iio_buffer_release() - Free a buffer's resources
1467 * @ref: Pointer to the kref embedded in the iio_buffer struct
1468 *
1469 * This function is called when the last reference to the buffer has been
1470 * dropped. It will typically free all resources allocated by the buffer. Do not
1471 * call this function manually, always use iio_buffer_put() when done using a
1472 * buffer.
1473 */
1474static void iio_buffer_release(struct kref *ref)
1475{
1476 struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
1477
1478 buffer->access->release(buffer);
1479}
1480
1481/**
1482 * iio_buffer_get() - Grab a reference to the buffer
1483 * @buffer: The buffer to grab a reference for, may be NULL
1484 *
1485 * Returns the pointer to the buffer that was passed into the function.
1486 */
1487struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
1488{
1489 if (buffer)
1490 kref_get(&buffer->ref);
1491
1492 return buffer;
1493}
1494EXPORT_SYMBOL_GPL(iio_buffer_get);
1495
1496/**
1497 * iio_buffer_put() - Release the reference to the buffer
1498 * @buffer: The buffer to release the reference for, may be NULL
1499 */
1500void iio_buffer_put(struct iio_buffer *buffer)
1501{
1502 if (buffer)
1503 kref_put(&buffer->ref, iio_buffer_release);
1504}
1505EXPORT_SYMBOL_GPL(iio_buffer_put);
Jonathan Cameron2b827ad2017-01-02 19:28:32 +00001506
1507/**
1508 * iio_device_attach_buffer - Attach a buffer to a IIO device
1509 * @indio_dev: The device the buffer should be attached to
1510 * @buffer: The buffer to attach to the device
1511 *
1512 * This function attaches a buffer to a IIO device. The buffer stays attached to
1513 * the device until the device is freed. The function should only be called at
1514 * most once per device.
1515 */
1516void iio_device_attach_buffer(struct iio_dev *indio_dev,
1517 struct iio_buffer *buffer)
1518{
1519 indio_dev->buffer = iio_buffer_get(buffer);
1520}
1521EXPORT_SYMBOL_GPL(iio_device_attach_buffer);