xref: /openbmc/linux/drivers/iio/industrialio-buffer.c (revision 604faf9a2ecd1addcc0c10a47e5aaef3c4d4fd6b)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a980e046SJonathan Cameron /* The industrial I/O core
3a980e046SJonathan Cameron  *
4a980e046SJonathan Cameron  * Copyright (c) 2008 Jonathan Cameron
5a980e046SJonathan Cameron  *
6a980e046SJonathan Cameron  * Handling of buffer allocation / resizing.
7a980e046SJonathan Cameron  *
8a980e046SJonathan Cameron  * Things to look at here.
9a980e046SJonathan Cameron  * - Better memory allocation techniques?
10a980e046SJonathan Cameron  * - Alternative access techniques?
11a980e046SJonathan Cameron  */
12f73f7f4dSAlexandru Ardelean #include <linux/anon_inodes.h>
13a980e046SJonathan Cameron #include <linux/kernel.h>
14a980e046SJonathan Cameron #include <linux/export.h>
15a980e046SJonathan Cameron #include <linux/device.h>
16f73f7f4dSAlexandru Ardelean #include <linux/file.h>
17a980e046SJonathan Cameron #include <linux/fs.h>
18a980e046SJonathan Cameron #include <linux/cdev.h>
19a980e046SJonathan Cameron #include <linux/slab.h>
20a980e046SJonathan Cameron #include <linux/poll.h>
21174cd4b1SIngo Molnar #include <linux/sched/signal.h>
22a980e046SJonathan Cameron 
23a980e046SJonathan Cameron #include <linux/iio/iio.h>
246a8c6b26SAlexandru Ardelean #include <linux/iio/iio-opaque.h>
25a980e046SJonathan Cameron #include "iio_core.h"
26f11d59d8SLars-Peter Clausen #include "iio_core_trigger.h"
27a980e046SJonathan Cameron #include <linux/iio/sysfs.h>
28a980e046SJonathan Cameron #include <linux/iio/buffer.h>
2933dd94cbSJonathan Cameron #include <linux/iio/buffer_impl.h>
30a980e046SJonathan Cameron 
31a980e046SJonathan Cameron static const char * const iio_endian_prefix[] = {
32a980e046SJonathan Cameron 	[IIO_BE] = "be",
33a980e046SJonathan Cameron 	[IIO_LE] = "le",
34a980e046SJonathan Cameron };
35a980e046SJonathan Cameron 
36705ee2c9SLars-Peter Clausen static bool iio_buffer_is_active(struct iio_buffer *buf)
3784b36ce5SJonathan Cameron {
38705ee2c9SLars-Peter Clausen 	return !list_empty(&buf->buffer_list);
3984b36ce5SJonathan Cameron }
4084b36ce5SJonathan Cameron 
4137d34556SJosselin Costanzi static size_t iio_buffer_data_available(struct iio_buffer *buf)
42647cc7b9SLars-Peter Clausen {
43647cc7b9SLars-Peter Clausen 	return buf->access->data_available(buf);
44647cc7b9SLars-Peter Clausen }
45647cc7b9SLars-Peter Clausen 
46f4f4673bSOctavian Purdila static int iio_buffer_flush_hwfifo(struct iio_dev *indio_dev,
47f4f4673bSOctavian Purdila 				   struct iio_buffer *buf, size_t required)
4837d34556SJosselin Costanzi {
49f4f4673bSOctavian Purdila 	if (!indio_dev->info->hwfifo_flush_to_buffer)
50f4f4673bSOctavian Purdila 		return -ENODEV;
51f4f4673bSOctavian Purdila 
52f4f4673bSOctavian Purdila 	return indio_dev->info->hwfifo_flush_to_buffer(indio_dev, required);
53f4f4673bSOctavian Purdila }
54f4f4673bSOctavian Purdila 
55f4f4673bSOctavian Purdila static bool iio_buffer_ready(struct iio_dev *indio_dev, struct iio_buffer *buf,
56f4f4673bSOctavian Purdila 			     size_t to_wait, int to_flush)
57f4f4673bSOctavian Purdila {
58f4f4673bSOctavian Purdila 	size_t avail;
59f4f4673bSOctavian Purdila 	int flushed = 0;
60f4f4673bSOctavian Purdila 
6137d34556SJosselin Costanzi 	/* wakeup if the device was unregistered */
6237d34556SJosselin Costanzi 	if (!indio_dev->info)
6337d34556SJosselin Costanzi 		return true;
6437d34556SJosselin Costanzi 
6537d34556SJosselin Costanzi 	/* drain the buffer if it was disabled */
66f4f4673bSOctavian Purdila 	if (!iio_buffer_is_active(buf)) {
6737d34556SJosselin Costanzi 		to_wait = min_t(size_t, to_wait, 1);
68f4f4673bSOctavian Purdila 		to_flush = 0;
69f4f4673bSOctavian Purdila 	}
7037d34556SJosselin Costanzi 
71f4f4673bSOctavian Purdila 	avail = iio_buffer_data_available(buf);
72f4f4673bSOctavian Purdila 
73f4f4673bSOctavian Purdila 	if (avail >= to_wait) {
74f4f4673bSOctavian Purdila 		/* force a flush for non-blocking reads */
75c6f67a1fSOctavian Purdila 		if (!to_wait && avail < to_flush)
76c6f67a1fSOctavian Purdila 			iio_buffer_flush_hwfifo(indio_dev, buf,
77c6f67a1fSOctavian Purdila 						to_flush - avail);
78f4f4673bSOctavian Purdila 		return true;
79f4f4673bSOctavian Purdila 	}
80f4f4673bSOctavian Purdila 
81f4f4673bSOctavian Purdila 	if (to_flush)
82f4f4673bSOctavian Purdila 		flushed = iio_buffer_flush_hwfifo(indio_dev, buf,
83f4f4673bSOctavian Purdila 						  to_wait - avail);
84f4f4673bSOctavian Purdila 	if (flushed <= 0)
85f4f4673bSOctavian Purdila 		return false;
86f4f4673bSOctavian Purdila 
87f4f4673bSOctavian Purdila 	if (avail + flushed >= to_wait)
8837d34556SJosselin Costanzi 		return true;
8937d34556SJosselin Costanzi 
9037d34556SJosselin Costanzi 	return false;
9137d34556SJosselin Costanzi }
9237d34556SJosselin Costanzi 
93a980e046SJonathan Cameron /**
94f73f7f4dSAlexandru Ardelean  * iio_buffer_read() - chrdev read for buffer access
950123635aSCristina Opriceana  * @filp:	File structure pointer for the char device
960123635aSCristina Opriceana  * @buf:	Destination buffer for iio buffer read
970123635aSCristina Opriceana  * @n:		First n bytes to read
980123635aSCristina Opriceana  * @f_ps:	Long offset provided by the user as a seek position
99a980e046SJonathan Cameron  *
100a980e046SJonathan Cameron  * This function relies on all buffer implementations having an
101a980e046SJonathan Cameron  * iio_buffer as their first element.
1020123635aSCristina Opriceana  *
1030123635aSCristina Opriceana  * Return: negative values corresponding to error codes or ret != 0
1040123635aSCristina Opriceana  *	   for ending the reading activity
105a980e046SJonathan Cameron  **/
106f73f7f4dSAlexandru Ardelean static ssize_t iio_buffer_read(struct file *filp, char __user *buf,
107a980e046SJonathan Cameron 			       size_t n, loff_t *f_ps)
108a980e046SJonathan Cameron {
109be24dcb1SAlexandru Ardelean 	struct iio_dev_buffer_pair *ib = filp->private_data;
110be24dcb1SAlexandru Ardelean 	struct iio_buffer *rb = ib->buffer;
111be24dcb1SAlexandru Ardelean 	struct iio_dev *indio_dev = ib->indio_dev;
112fcf68f3cSBrian Norris 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
11337d34556SJosselin Costanzi 	size_t datum_size;
114c6f67a1fSOctavian Purdila 	size_t to_wait;
1155dba4b14SColin Ian King 	int ret = 0;
116a980e046SJonathan Cameron 
117f18e7a06SLars-Peter Clausen 	if (!indio_dev->info)
118f18e7a06SLars-Peter Clausen 		return -ENODEV;
119f18e7a06SLars-Peter Clausen 
120f6d4033dSLars-Peter Clausen 	if (!rb || !rb->access->read)
121a980e046SJonathan Cameron 		return -EINVAL;
122ee551a10SLars-Peter Clausen 
12337d34556SJosselin Costanzi 	datum_size = rb->bytes_per_datum;
124ee551a10SLars-Peter Clausen 
12537d34556SJosselin Costanzi 	/*
12637d34556SJosselin Costanzi 	 * If datum_size is 0 there will never be anything to read from the
12737d34556SJosselin Costanzi 	 * buffer, so signal end of file now.
12837d34556SJosselin Costanzi 	 */
12937d34556SJosselin Costanzi 	if (!datum_size)
13037d34556SJosselin Costanzi 		return 0;
13137d34556SJosselin Costanzi 
132c6f67a1fSOctavian Purdila 	if (filp->f_flags & O_NONBLOCK)
133c6f67a1fSOctavian Purdila 		to_wait = 0;
134c6f67a1fSOctavian Purdila 	else
135c6f67a1fSOctavian Purdila 		to_wait = min_t(size_t, n / datum_size, rb->watermark);
13637d34556SJosselin Costanzi 
137fcf68f3cSBrian Norris 	add_wait_queue(&rb->pollq, &wait);
13837d34556SJosselin Costanzi 	do {
139fcf68f3cSBrian Norris 		if (!indio_dev->info) {
140fcf68f3cSBrian Norris 			ret = -ENODEV;
141fcf68f3cSBrian Norris 			break;
142fcf68f3cSBrian Norris 		}
14337d34556SJosselin Costanzi 
144fcf68f3cSBrian Norris 		if (!iio_buffer_ready(indio_dev, rb, to_wait, n / datum_size)) {
145fcf68f3cSBrian Norris 			if (signal_pending(current)) {
146fcf68f3cSBrian Norris 				ret = -ERESTARTSYS;
147fcf68f3cSBrian Norris 				break;
148fcf68f3cSBrian Norris 			}
149fcf68f3cSBrian Norris 
150fcf68f3cSBrian Norris 			wait_woken(&wait, TASK_INTERRUPTIBLE,
151fcf68f3cSBrian Norris 				   MAX_SCHEDULE_TIMEOUT);
152fcf68f3cSBrian Norris 			continue;
153fcf68f3cSBrian Norris 		}
154ee551a10SLars-Peter Clausen 
155f6d4033dSLars-Peter Clausen 		ret = rb->access->read(rb, n, buf);
156ee551a10SLars-Peter Clausen 		if (ret == 0 && (filp->f_flags & O_NONBLOCK))
157ee551a10SLars-Peter Clausen 			ret = -EAGAIN;
158ee551a10SLars-Peter Clausen 	} while (ret == 0);
159fcf68f3cSBrian Norris 	remove_wait_queue(&rb->pollq, &wait);
160ee551a10SLars-Peter Clausen 
161ee551a10SLars-Peter Clausen 	return ret;
162a980e046SJonathan Cameron }
163a980e046SJonathan Cameron 
164a980e046SJonathan Cameron /**
165a980e046SJonathan Cameron  * iio_buffer_poll() - poll the buffer to find out if it has data
1660123635aSCristina Opriceana  * @filp:	File structure pointer for device access
1670123635aSCristina Opriceana  * @wait:	Poll table structure pointer for which the driver adds
1680123635aSCristina Opriceana  *		a wait queue
1690123635aSCristina Opriceana  *
170a9a08845SLinus Torvalds  * Return: (EPOLLIN | EPOLLRDNORM) if data is available for reading
1710123635aSCristina Opriceana  *	   or 0 for other cases
172a980e046SJonathan Cameron  */
173f73f7f4dSAlexandru Ardelean static __poll_t iio_buffer_poll(struct file *filp,
174a980e046SJonathan Cameron 				struct poll_table_struct *wait)
175a980e046SJonathan Cameron {
176be24dcb1SAlexandru Ardelean 	struct iio_dev_buffer_pair *ib = filp->private_data;
177be24dcb1SAlexandru Ardelean 	struct iio_buffer *rb = ib->buffer;
178be24dcb1SAlexandru Ardelean 	struct iio_dev *indio_dev = ib->indio_dev;
179a980e046SJonathan Cameron 
1804cd140bdSStefan Windfeldt-Prytz 	if (!indio_dev->info || rb == NULL)
1811bdc0293SCristina Opriceana 		return 0;
182f18e7a06SLars-Peter Clausen 
183a980e046SJonathan Cameron 	poll_wait(filp, &rb->pollq, wait);
184f4f4673bSOctavian Purdila 	if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0))
185a9a08845SLinus Torvalds 		return EPOLLIN | EPOLLRDNORM;
186a980e046SJonathan Cameron 	return 0;
187a980e046SJonathan Cameron }
188a980e046SJonathan Cameron 
189f73f7f4dSAlexandru Ardelean ssize_t iio_buffer_read_wrapper(struct file *filp, char __user *buf,
190f73f7f4dSAlexandru Ardelean 				size_t n, loff_t *f_ps)
191f73f7f4dSAlexandru Ardelean {
192f73f7f4dSAlexandru Ardelean 	struct iio_dev_buffer_pair *ib = filp->private_data;
193f73f7f4dSAlexandru Ardelean 	struct iio_buffer *rb = ib->buffer;
194f73f7f4dSAlexandru Ardelean 
195f73f7f4dSAlexandru Ardelean 	/* check if buffer was opened through new API */
196f73f7f4dSAlexandru Ardelean 	if (test_bit(IIO_BUSY_BIT_POS, &rb->flags))
197f73f7f4dSAlexandru Ardelean 		return -EBUSY;
198f73f7f4dSAlexandru Ardelean 
199f73f7f4dSAlexandru Ardelean 	return iio_buffer_read(filp, buf, n, f_ps);
200f73f7f4dSAlexandru Ardelean }
201f73f7f4dSAlexandru Ardelean 
202f73f7f4dSAlexandru Ardelean __poll_t iio_buffer_poll_wrapper(struct file *filp,
203f73f7f4dSAlexandru Ardelean 				 struct poll_table_struct *wait)
204f73f7f4dSAlexandru Ardelean {
205f73f7f4dSAlexandru Ardelean 	struct iio_dev_buffer_pair *ib = filp->private_data;
206f73f7f4dSAlexandru Ardelean 	struct iio_buffer *rb = ib->buffer;
207f73f7f4dSAlexandru Ardelean 
208f73f7f4dSAlexandru Ardelean 	/* check if buffer was opened through new API */
209f73f7f4dSAlexandru Ardelean 	if (test_bit(IIO_BUSY_BIT_POS, &rb->flags))
210f73f7f4dSAlexandru Ardelean 		return 0;
211f73f7f4dSAlexandru Ardelean 
212f73f7f4dSAlexandru Ardelean 	return iio_buffer_poll(filp, wait);
213f73f7f4dSAlexandru Ardelean }
214f73f7f4dSAlexandru Ardelean 
215d2f0a48fSLars-Peter Clausen /**
216d2f0a48fSLars-Peter Clausen  * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue
217d2f0a48fSLars-Peter Clausen  * @indio_dev: The IIO device
218d2f0a48fSLars-Peter Clausen  *
219d2f0a48fSLars-Peter Clausen  * Wakes up the event waitqueue used for poll(). Should usually
220d2f0a48fSLars-Peter Clausen  * be called when the device is unregistered.
221d2f0a48fSLars-Peter Clausen  */
222d2f0a48fSLars-Peter Clausen void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
223d2f0a48fSLars-Peter Clausen {
224ee708e6bSAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
225ee708e6bSAlexandru Ardelean 	struct iio_buffer *buffer;
226ee708e6bSAlexandru Ardelean 	unsigned int i;
227ff3f7e04SAlexandru Ardelean 
228ee708e6bSAlexandru Ardelean 	for (i = 0; i < iio_dev_opaque->attached_buffers_cnt; i++) {
229ee708e6bSAlexandru Ardelean 		buffer = iio_dev_opaque->attached_buffers[i];
230ff3f7e04SAlexandru Ardelean 		wake_up(&buffer->pollq);
231d2f0a48fSLars-Peter Clausen 	}
232ee708e6bSAlexandru Ardelean }
233d2f0a48fSLars-Peter Clausen 
234a980e046SJonathan Cameron void iio_buffer_init(struct iio_buffer *buffer)
235a980e046SJonathan Cameron {
236a980e046SJonathan Cameron 	INIT_LIST_HEAD(&buffer->demux_list);
237705ee2c9SLars-Peter Clausen 	INIT_LIST_HEAD(&buffer->buffer_list);
238a980e046SJonathan Cameron 	init_waitqueue_head(&buffer->pollq);
2399e69c935SLars-Peter Clausen 	kref_init(&buffer->ref);
2404a605357SLars-Peter Clausen 	if (!buffer->watermark)
24137d34556SJosselin Costanzi 		buffer->watermark = 1;
242a980e046SJonathan Cameron }
243a980e046SJonathan Cameron EXPORT_SYMBOL(iio_buffer_init);
244a980e046SJonathan Cameron 
245218bc53dSAlexandru Ardelean void iio_device_detach_buffers(struct iio_dev *indio_dev)
246ee708e6bSAlexandru Ardelean {
247ee708e6bSAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
248ee708e6bSAlexandru Ardelean 	struct iio_buffer *buffer;
249ee708e6bSAlexandru Ardelean 	unsigned int i;
250ee708e6bSAlexandru Ardelean 
251ee708e6bSAlexandru Ardelean 	for (i = 0; i < iio_dev_opaque->attached_buffers_cnt; i++) {
252ee708e6bSAlexandru Ardelean 		buffer = iio_dev_opaque->attached_buffers[i];
253ee708e6bSAlexandru Ardelean 		iio_buffer_put(buffer);
254ee708e6bSAlexandru Ardelean 	}
255218bc53dSAlexandru Ardelean 
256218bc53dSAlexandru Ardelean 	kfree(iio_dev_opaque->attached_buffers);
257ee708e6bSAlexandru Ardelean }
258ee708e6bSAlexandru Ardelean 
259a980e046SJonathan Cameron static ssize_t iio_show_scan_index(struct device *dev,
260a980e046SJonathan Cameron 				   struct device_attribute *attr,
261a980e046SJonathan Cameron 				   char *buf)
262a980e046SJonathan Cameron {
26383ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
264a980e046SJonathan Cameron }
265a980e046SJonathan Cameron 
266a980e046SJonathan Cameron static ssize_t iio_show_fixed_type(struct device *dev,
267a980e046SJonathan Cameron 				   struct device_attribute *attr,
268a980e046SJonathan Cameron 				   char *buf)
269a980e046SJonathan Cameron {
270a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
271a980e046SJonathan Cameron 	u8 type = this_attr->c->scan_type.endianness;
272a980e046SJonathan Cameron 
273a980e046SJonathan Cameron 	if (type == IIO_CPU) {
274a980e046SJonathan Cameron #ifdef __LITTLE_ENDIAN
275a980e046SJonathan Cameron 		type = IIO_LE;
276a980e046SJonathan Cameron #else
277a980e046SJonathan Cameron 		type = IIO_BE;
278a980e046SJonathan Cameron #endif
279a980e046SJonathan Cameron 	}
2800ee8546aSSrinivas Pandruvada 	if (this_attr->c->scan_type.repeat > 1)
28183ca56b6SLars-Peter Clausen 		return sysfs_emit(buf, "%s:%c%d/%dX%d>>%u\n",
2820ee8546aSSrinivas Pandruvada 		       iio_endian_prefix[type],
2830ee8546aSSrinivas Pandruvada 		       this_attr->c->scan_type.sign,
2840ee8546aSSrinivas Pandruvada 		       this_attr->c->scan_type.realbits,
2850ee8546aSSrinivas Pandruvada 		       this_attr->c->scan_type.storagebits,
2860ee8546aSSrinivas Pandruvada 		       this_attr->c->scan_type.repeat,
2870ee8546aSSrinivas Pandruvada 		       this_attr->c->scan_type.shift);
2880ee8546aSSrinivas Pandruvada 	else
28983ca56b6SLars-Peter Clausen 		return sysfs_emit(buf, "%s:%c%d/%d>>%u\n",
290a980e046SJonathan Cameron 		       iio_endian_prefix[type],
291a980e046SJonathan Cameron 		       this_attr->c->scan_type.sign,
292a980e046SJonathan Cameron 		       this_attr->c->scan_type.realbits,
293a980e046SJonathan Cameron 		       this_attr->c->scan_type.storagebits,
294a980e046SJonathan Cameron 		       this_attr->c->scan_type.shift);
295a980e046SJonathan Cameron }
296a980e046SJonathan Cameron 
297a980e046SJonathan Cameron static ssize_t iio_scan_el_show(struct device *dev,
298a980e046SJonathan Cameron 				struct device_attribute *attr,
299a980e046SJonathan Cameron 				char *buf)
300a980e046SJonathan Cameron {
301a980e046SJonathan Cameron 	int ret;
30215097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
303a980e046SJonathan Cameron 
3042076a20fSAlec Berg 	/* Ensure ret is 0 or 1. */
3052076a20fSAlec Berg 	ret = !!test_bit(to_iio_dev_attr(attr)->address,
306ff3f7e04SAlexandru Ardelean 		       buffer->scan_mask);
307a980e046SJonathan Cameron 
30883ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%d\n", ret);
309a980e046SJonathan Cameron }
310a980e046SJonathan Cameron 
311217a5cf0SLars-Peter Clausen /* Note NULL used as error indicator as it doesn't make sense. */
312217a5cf0SLars-Peter Clausen static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
313217a5cf0SLars-Peter Clausen 					  unsigned int masklength,
3141e1ec286SLars-Peter Clausen 					  const unsigned long *mask,
3151e1ec286SLars-Peter Clausen 					  bool strict)
316217a5cf0SLars-Peter Clausen {
317217a5cf0SLars-Peter Clausen 	if (bitmap_empty(mask, masklength))
318217a5cf0SLars-Peter Clausen 		return NULL;
319217a5cf0SLars-Peter Clausen 	while (*av_masks) {
3201e1ec286SLars-Peter Clausen 		if (strict) {
3211e1ec286SLars-Peter Clausen 			if (bitmap_equal(mask, av_masks, masklength))
3221e1ec286SLars-Peter Clausen 				return av_masks;
3231e1ec286SLars-Peter Clausen 		} else {
324217a5cf0SLars-Peter Clausen 			if (bitmap_subset(mask, av_masks, masklength))
325217a5cf0SLars-Peter Clausen 				return av_masks;
3261e1ec286SLars-Peter Clausen 		}
327217a5cf0SLars-Peter Clausen 		av_masks += BITS_TO_LONGS(masklength);
328217a5cf0SLars-Peter Clausen 	}
329217a5cf0SLars-Peter Clausen 	return NULL;
330217a5cf0SLars-Peter Clausen }
331217a5cf0SLars-Peter Clausen 
332217a5cf0SLars-Peter Clausen static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
333217a5cf0SLars-Peter Clausen 	const unsigned long *mask)
334217a5cf0SLars-Peter Clausen {
335217a5cf0SLars-Peter Clausen 	if (!indio_dev->setup_ops->validate_scan_mask)
336217a5cf0SLars-Peter Clausen 		return true;
337217a5cf0SLars-Peter Clausen 
338217a5cf0SLars-Peter Clausen 	return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
339217a5cf0SLars-Peter Clausen }
340217a5cf0SLars-Peter Clausen 
341217a5cf0SLars-Peter Clausen /**
342217a5cf0SLars-Peter Clausen  * iio_scan_mask_set() - set particular bit in the scan mask
343217a5cf0SLars-Peter Clausen  * @indio_dev: the iio device
344217a5cf0SLars-Peter Clausen  * @buffer: the buffer whose scan mask we are interested in
345217a5cf0SLars-Peter Clausen  * @bit: the bit to be set.
346217a5cf0SLars-Peter Clausen  *
347217a5cf0SLars-Peter Clausen  * Note that at this point we have no way of knowing what other
348217a5cf0SLars-Peter Clausen  * buffers might request, hence this code only verifies that the
349217a5cf0SLars-Peter Clausen  * individual buffers request is plausible.
350217a5cf0SLars-Peter Clausen  */
351217a5cf0SLars-Peter Clausen static int iio_scan_mask_set(struct iio_dev *indio_dev,
352217a5cf0SLars-Peter Clausen 		      struct iio_buffer *buffer, int bit)
353217a5cf0SLars-Peter Clausen {
354217a5cf0SLars-Peter Clausen 	const unsigned long *mask;
355217a5cf0SLars-Peter Clausen 	unsigned long *trialmask;
356217a5cf0SLars-Peter Clausen 
357d21fed06SChristophe JAILLET 	if (!indio_dev->masklength) {
358d21fed06SChristophe JAILLET 		WARN(1, "Trying to set scanmask prior to registering buffer\n");
359d21fed06SChristophe JAILLET 		return -EINVAL;
360d21fed06SChristophe JAILLET 	}
361d21fed06SChristophe JAILLET 
36245851650SChristophe JAILLET 	trialmask = bitmap_alloc(indio_dev->masklength, GFP_KERNEL);
36345851650SChristophe JAILLET 	if (!trialmask)
364217a5cf0SLars-Peter Clausen 		return -ENOMEM;
365217a5cf0SLars-Peter Clausen 	bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
366217a5cf0SLars-Peter Clausen 	set_bit(bit, trialmask);
367217a5cf0SLars-Peter Clausen 
368217a5cf0SLars-Peter Clausen 	if (!iio_validate_scan_mask(indio_dev, trialmask))
369217a5cf0SLars-Peter Clausen 		goto err_invalid_mask;
370217a5cf0SLars-Peter Clausen 
371217a5cf0SLars-Peter Clausen 	if (indio_dev->available_scan_masks) {
372217a5cf0SLars-Peter Clausen 		mask = iio_scan_mask_match(indio_dev->available_scan_masks,
373217a5cf0SLars-Peter Clausen 					   indio_dev->masklength,
3741e1ec286SLars-Peter Clausen 					   trialmask, false);
375217a5cf0SLars-Peter Clausen 		if (!mask)
376217a5cf0SLars-Peter Clausen 			goto err_invalid_mask;
377217a5cf0SLars-Peter Clausen 	}
378217a5cf0SLars-Peter Clausen 	bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
379217a5cf0SLars-Peter Clausen 
3803862828aSAndy Shevchenko 	bitmap_free(trialmask);
381217a5cf0SLars-Peter Clausen 
382217a5cf0SLars-Peter Clausen 	return 0;
383217a5cf0SLars-Peter Clausen 
384217a5cf0SLars-Peter Clausen err_invalid_mask:
3853862828aSAndy Shevchenko 	bitmap_free(trialmask);
386217a5cf0SLars-Peter Clausen 	return -EINVAL;
387217a5cf0SLars-Peter Clausen }
388217a5cf0SLars-Peter Clausen 
389a980e046SJonathan Cameron static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
390a980e046SJonathan Cameron {
391a980e046SJonathan Cameron 	clear_bit(bit, buffer->scan_mask);
392a980e046SJonathan Cameron 	return 0;
393a980e046SJonathan Cameron }
394a980e046SJonathan Cameron 
395c2bf8d5fSJonathan Cameron static int iio_scan_mask_query(struct iio_dev *indio_dev,
396c2bf8d5fSJonathan Cameron 			       struct iio_buffer *buffer, int bit)
397c2bf8d5fSJonathan Cameron {
398c2bf8d5fSJonathan Cameron 	if (bit > indio_dev->masklength)
399c2bf8d5fSJonathan Cameron 		return -EINVAL;
400c2bf8d5fSJonathan Cameron 
401c2bf8d5fSJonathan Cameron 	if (!buffer->scan_mask)
402c2bf8d5fSJonathan Cameron 		return 0;
403c2bf8d5fSJonathan Cameron 
404c2bf8d5fSJonathan Cameron 	/* Ensure return value is 0 or 1. */
405c2bf8d5fSJonathan Cameron 	return !!test_bit(bit, buffer->scan_mask);
406c2bf8d5fSJonathan Cameron };
407c2bf8d5fSJonathan Cameron 
408a980e046SJonathan Cameron static ssize_t iio_scan_el_store(struct device *dev,
409a980e046SJonathan Cameron 				 struct device_attribute *attr,
410a980e046SJonathan Cameron 				 const char *buf,
411a980e046SJonathan Cameron 				 size_t len)
412a980e046SJonathan Cameron {
413a980e046SJonathan Cameron 	int ret;
414a980e046SJonathan Cameron 	bool state;
415e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
416a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
41715097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = this_attr->buffer;
418a980e046SJonathan Cameron 
419a980e046SJonathan Cameron 	ret = strtobool(buf, &state);
420a980e046SJonathan Cameron 	if (ret < 0)
421a980e046SJonathan Cameron 		return ret;
422a980e046SJonathan Cameron 	mutex_lock(&indio_dev->mlock);
423ff3f7e04SAlexandru Ardelean 	if (iio_buffer_is_active(buffer)) {
424a980e046SJonathan Cameron 		ret = -EBUSY;
425a980e046SJonathan Cameron 		goto error_ret;
426a980e046SJonathan Cameron 	}
427a980e046SJonathan Cameron 	ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
428a980e046SJonathan Cameron 	if (ret < 0)
429a980e046SJonathan Cameron 		goto error_ret;
430a980e046SJonathan Cameron 	if (!state && ret) {
431a980e046SJonathan Cameron 		ret = iio_scan_mask_clear(buffer, this_attr->address);
432a980e046SJonathan Cameron 		if (ret)
433a980e046SJonathan Cameron 			goto error_ret;
434a980e046SJonathan Cameron 	} else if (state && !ret) {
435a980e046SJonathan Cameron 		ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
436a980e046SJonathan Cameron 		if (ret)
437a980e046SJonathan Cameron 			goto error_ret;
438a980e046SJonathan Cameron 	}
439a980e046SJonathan Cameron 
440a980e046SJonathan Cameron error_ret:
441a980e046SJonathan Cameron 	mutex_unlock(&indio_dev->mlock);
442a980e046SJonathan Cameron 
443a980e046SJonathan Cameron 	return ret < 0 ? ret : len;
444a980e046SJonathan Cameron 
445a980e046SJonathan Cameron }
446a980e046SJonathan Cameron 
447a980e046SJonathan Cameron static ssize_t iio_scan_el_ts_show(struct device *dev,
448a980e046SJonathan Cameron 				   struct device_attribute *attr,
449a980e046SJonathan Cameron 				   char *buf)
450a980e046SJonathan Cameron {
45115097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
452ff3f7e04SAlexandru Ardelean 
45383ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%d\n", buffer->scan_timestamp);
454a980e046SJonathan Cameron }
455a980e046SJonathan Cameron 
456a980e046SJonathan Cameron static ssize_t iio_scan_el_ts_store(struct device *dev,
457a980e046SJonathan Cameron 				    struct device_attribute *attr,
458a980e046SJonathan Cameron 				    const char *buf,
459a980e046SJonathan Cameron 				    size_t len)
460a980e046SJonathan Cameron {
461a980e046SJonathan Cameron 	int ret;
462e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
46315097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
464a980e046SJonathan Cameron 	bool state;
465a980e046SJonathan Cameron 
466a980e046SJonathan Cameron 	ret = strtobool(buf, &state);
467a980e046SJonathan Cameron 	if (ret < 0)
468a980e046SJonathan Cameron 		return ret;
469a980e046SJonathan Cameron 
470a980e046SJonathan Cameron 	mutex_lock(&indio_dev->mlock);
471ff3f7e04SAlexandru Ardelean 	if (iio_buffer_is_active(buffer)) {
472a980e046SJonathan Cameron 		ret = -EBUSY;
473a980e046SJonathan Cameron 		goto error_ret;
474a980e046SJonathan Cameron 	}
475ff3f7e04SAlexandru Ardelean 	buffer->scan_timestamp = state;
476a980e046SJonathan Cameron error_ret:
477a980e046SJonathan Cameron 	mutex_unlock(&indio_dev->mlock);
478a980e046SJonathan Cameron 
479a980e046SJonathan Cameron 	return ret ? ret : len;
480a980e046SJonathan Cameron }
481a980e046SJonathan Cameron 
482a980e046SJonathan Cameron static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
483ff3f7e04SAlexandru Ardelean 					struct iio_buffer *buffer,
484a980e046SJonathan Cameron 					const struct iio_chan_spec *chan)
485a980e046SJonathan Cameron {
486a980e046SJonathan Cameron 	int ret, attrcount = 0;
487a980e046SJonathan Cameron 
488a980e046SJonathan Cameron 	ret = __iio_add_chan_devattr("index",
489a980e046SJonathan Cameron 				     chan,
490a980e046SJonathan Cameron 				     &iio_show_scan_index,
491a980e046SJonathan Cameron 				     NULL,
492a980e046SJonathan Cameron 				     0,
4933704432fSJonathan Cameron 				     IIO_SEPARATE,
494a980e046SJonathan Cameron 				     &indio_dev->dev,
4953e3d11b2SAlexandru Ardelean 				     buffer,
49615097c7aSAlexandru Ardelean 				     &buffer->buffer_attr_list);
497a980e046SJonathan Cameron 	if (ret)
49892825ff9SHartmut Knaack 		return ret;
499a980e046SJonathan Cameron 	attrcount++;
500a980e046SJonathan Cameron 	ret = __iio_add_chan_devattr("type",
501a980e046SJonathan Cameron 				     chan,
502a980e046SJonathan Cameron 				     &iio_show_fixed_type,
503a980e046SJonathan Cameron 				     NULL,
504a980e046SJonathan Cameron 				     0,
505a980e046SJonathan Cameron 				     0,
506a980e046SJonathan Cameron 				     &indio_dev->dev,
5073e3d11b2SAlexandru Ardelean 				     buffer,
50815097c7aSAlexandru Ardelean 				     &buffer->buffer_attr_list);
509a980e046SJonathan Cameron 	if (ret)
51092825ff9SHartmut Knaack 		return ret;
511a980e046SJonathan Cameron 	attrcount++;
512a980e046SJonathan Cameron 	if (chan->type != IIO_TIMESTAMP)
513a980e046SJonathan Cameron 		ret = __iio_add_chan_devattr("en",
514a980e046SJonathan Cameron 					     chan,
515a980e046SJonathan Cameron 					     &iio_scan_el_show,
516a980e046SJonathan Cameron 					     &iio_scan_el_store,
517a980e046SJonathan Cameron 					     chan->scan_index,
518a980e046SJonathan Cameron 					     0,
519a980e046SJonathan Cameron 					     &indio_dev->dev,
5203e3d11b2SAlexandru Ardelean 					     buffer,
52115097c7aSAlexandru Ardelean 					     &buffer->buffer_attr_list);
522a980e046SJonathan Cameron 	else
523a980e046SJonathan Cameron 		ret = __iio_add_chan_devattr("en",
524a980e046SJonathan Cameron 					     chan,
525a980e046SJonathan Cameron 					     &iio_scan_el_ts_show,
526a980e046SJonathan Cameron 					     &iio_scan_el_ts_store,
527a980e046SJonathan Cameron 					     chan->scan_index,
528a980e046SJonathan Cameron 					     0,
529a980e046SJonathan Cameron 					     &indio_dev->dev,
5303e3d11b2SAlexandru Ardelean 					     buffer,
53115097c7aSAlexandru Ardelean 					     &buffer->buffer_attr_list);
5329572588cSPeter Meerwald 	if (ret)
53392825ff9SHartmut Knaack 		return ret;
534a980e046SJonathan Cameron 	attrcount++;
535a980e046SJonathan Cameron 	ret = attrcount;
536a980e046SJonathan Cameron 	return ret;
537a980e046SJonathan Cameron }
538a980e046SJonathan Cameron 
53908e7e0adSLars-Peter Clausen static ssize_t iio_buffer_read_length(struct device *dev,
540a980e046SJonathan Cameron 				      struct device_attribute *attr,
541a980e046SJonathan Cameron 				      char *buf)
542a980e046SJonathan Cameron {
54315097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
544a980e046SJonathan Cameron 
54583ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%d\n", buffer->length);
546a980e046SJonathan Cameron }
547a980e046SJonathan Cameron 
54808e7e0adSLars-Peter Clausen static ssize_t iio_buffer_write_length(struct device *dev,
549a980e046SJonathan Cameron 				       struct device_attribute *attr,
55008e7e0adSLars-Peter Clausen 				       const char *buf, size_t len)
551a980e046SJonathan Cameron {
552e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
55315097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
554948ad205SLars-Peter Clausen 	unsigned int val;
555948ad205SLars-Peter Clausen 	int ret;
556a980e046SJonathan Cameron 
557948ad205SLars-Peter Clausen 	ret = kstrtouint(buf, 10, &val);
558a980e046SJonathan Cameron 	if (ret)
559a980e046SJonathan Cameron 		return ret;
560a980e046SJonathan Cameron 
56137495660SLars-Peter Clausen 	if (val == buffer->length)
562a980e046SJonathan Cameron 		return len;
563a980e046SJonathan Cameron 
564a980e046SJonathan Cameron 	mutex_lock(&indio_dev->mlock);
565ff3f7e04SAlexandru Ardelean 	if (iio_buffer_is_active(buffer)) {
566a980e046SJonathan Cameron 		ret = -EBUSY;
567a980e046SJonathan Cameron 	} else {
568a980e046SJonathan Cameron 		buffer->access->set_length(buffer, val);
569a980e046SJonathan Cameron 		ret = 0;
570a980e046SJonathan Cameron 	}
57137d34556SJosselin Costanzi 	if (ret)
57237d34556SJosselin Costanzi 		goto out;
57337d34556SJosselin Costanzi 	if (buffer->length && buffer->length < buffer->watermark)
57437d34556SJosselin Costanzi 		buffer->watermark = buffer->length;
57537d34556SJosselin Costanzi out:
576a980e046SJonathan Cameron 	mutex_unlock(&indio_dev->mlock);
577a980e046SJonathan Cameron 
578a980e046SJonathan Cameron 	return ret ? ret : len;
579a980e046SJonathan Cameron }
580a980e046SJonathan Cameron 
58108e7e0adSLars-Peter Clausen static ssize_t iio_buffer_show_enable(struct device *dev,
582a980e046SJonathan Cameron 				      struct device_attribute *attr,
583a980e046SJonathan Cameron 				      char *buf)
584a980e046SJonathan Cameron {
58515097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
586ff3f7e04SAlexandru Ardelean 
58783ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%d\n", iio_buffer_is_active(buffer));
588a980e046SJonathan Cameron }
589a980e046SJonathan Cameron 
590182b4905SLars-Peter Clausen static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev,
591182b4905SLars-Peter Clausen 					     unsigned int scan_index)
592182b4905SLars-Peter Clausen {
593182b4905SLars-Peter Clausen 	const struct iio_chan_spec *ch;
594182b4905SLars-Peter Clausen 	unsigned int bytes;
595182b4905SLars-Peter Clausen 
596182b4905SLars-Peter Clausen 	ch = iio_find_channel_from_si(indio_dev, scan_index);
597182b4905SLars-Peter Clausen 	bytes = ch->scan_type.storagebits / 8;
598182b4905SLars-Peter Clausen 	if (ch->scan_type.repeat > 1)
599182b4905SLars-Peter Clausen 		bytes *= ch->scan_type.repeat;
600182b4905SLars-Peter Clausen 	return bytes;
601182b4905SLars-Peter Clausen }
602182b4905SLars-Peter Clausen 
603182b4905SLars-Peter Clausen static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev)
604182b4905SLars-Peter Clausen {
60562f4f36cSJonathan Cameron 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
60662f4f36cSJonathan Cameron 
607182b4905SLars-Peter Clausen 	return iio_storage_bytes_for_si(indio_dev,
60862f4f36cSJonathan Cameron 					iio_dev_opaque->scan_index_timestamp);
609182b4905SLars-Peter Clausen }
610182b4905SLars-Peter Clausen 
611183f4173SPeter Meerwald static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
612183f4173SPeter Meerwald 				const unsigned long *mask, bool timestamp)
613a980e046SJonathan Cameron {
614a980e046SJonathan Cameron 	unsigned bytes = 0;
615883f6165SLars Möllendorf 	int length, i, largest = 0;
616a980e046SJonathan Cameron 
617a980e046SJonathan Cameron 	/* How much space will the demuxed element take? */
618a980e046SJonathan Cameron 	for_each_set_bit(i, mask,
619a980e046SJonathan Cameron 			 indio_dev->masklength) {
620182b4905SLars-Peter Clausen 		length = iio_storage_bytes_for_si(indio_dev, i);
621a980e046SJonathan Cameron 		bytes = ALIGN(bytes, length);
622a980e046SJonathan Cameron 		bytes += length;
623883f6165SLars Möllendorf 		largest = max(largest, length);
624a980e046SJonathan Cameron 	}
625182b4905SLars-Peter Clausen 
626a980e046SJonathan Cameron 	if (timestamp) {
627182b4905SLars-Peter Clausen 		length = iio_storage_bytes_for_timestamp(indio_dev);
628a980e046SJonathan Cameron 		bytes = ALIGN(bytes, length);
629a980e046SJonathan Cameron 		bytes += length;
630883f6165SLars Möllendorf 		largest = max(largest, length);
631a980e046SJonathan Cameron 	}
632883f6165SLars Möllendorf 
633883f6165SLars Möllendorf 	bytes = ALIGN(bytes, largest);
634a980e046SJonathan Cameron 	return bytes;
635a980e046SJonathan Cameron }
636a980e046SJonathan Cameron 
6379e69c935SLars-Peter Clausen static void iio_buffer_activate(struct iio_dev *indio_dev,
6389e69c935SLars-Peter Clausen 	struct iio_buffer *buffer)
6399e69c935SLars-Peter Clausen {
6406a8c6b26SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
6416a8c6b26SAlexandru Ardelean 
6429e69c935SLars-Peter Clausen 	iio_buffer_get(buffer);
6436a8c6b26SAlexandru Ardelean 	list_add(&buffer->buffer_list, &iio_dev_opaque->buffer_list);
6449e69c935SLars-Peter Clausen }
6459e69c935SLars-Peter Clausen 
6469e69c935SLars-Peter Clausen static void iio_buffer_deactivate(struct iio_buffer *buffer)
6479e69c935SLars-Peter Clausen {
6489e69c935SLars-Peter Clausen 	list_del_init(&buffer->buffer_list);
64937d34556SJosselin Costanzi 	wake_up_interruptible(&buffer->pollq);
6509e69c935SLars-Peter Clausen 	iio_buffer_put(buffer);
6519e69c935SLars-Peter Clausen }
6529e69c935SLars-Peter Clausen 
6531250186aSLars-Peter Clausen static void iio_buffer_deactivate_all(struct iio_dev *indio_dev)
6541250186aSLars-Peter Clausen {
6556a8c6b26SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
6561250186aSLars-Peter Clausen 	struct iio_buffer *buffer, *_buffer;
6571250186aSLars-Peter Clausen 
6581250186aSLars-Peter Clausen 	list_for_each_entry_safe(buffer, _buffer,
6596a8c6b26SAlexandru Ardelean 			&iio_dev_opaque->buffer_list, buffer_list)
6601250186aSLars-Peter Clausen 		iio_buffer_deactivate(buffer);
6611250186aSLars-Peter Clausen }
6621250186aSLars-Peter Clausen 
663e18a2ad4SLars-Peter Clausen static int iio_buffer_enable(struct iio_buffer *buffer,
664e18a2ad4SLars-Peter Clausen 	struct iio_dev *indio_dev)
665e18a2ad4SLars-Peter Clausen {
666e18a2ad4SLars-Peter Clausen 	if (!buffer->access->enable)
667e18a2ad4SLars-Peter Clausen 		return 0;
668e18a2ad4SLars-Peter Clausen 	return buffer->access->enable(buffer, indio_dev);
669e18a2ad4SLars-Peter Clausen }
670e18a2ad4SLars-Peter Clausen 
671e18a2ad4SLars-Peter Clausen static int iio_buffer_disable(struct iio_buffer *buffer,
672e18a2ad4SLars-Peter Clausen 	struct iio_dev *indio_dev)
673e18a2ad4SLars-Peter Clausen {
674e18a2ad4SLars-Peter Clausen 	if (!buffer->access->disable)
675e18a2ad4SLars-Peter Clausen 		return 0;
676e18a2ad4SLars-Peter Clausen 	return buffer->access->disable(buffer, indio_dev);
677e18a2ad4SLars-Peter Clausen }
678e18a2ad4SLars-Peter Clausen 
6798e050996SLars-Peter Clausen static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
6808e050996SLars-Peter Clausen 	struct iio_buffer *buffer)
6818e050996SLars-Peter Clausen {
6828e050996SLars-Peter Clausen 	unsigned int bytes;
6838e050996SLars-Peter Clausen 
6848e050996SLars-Peter Clausen 	if (!buffer->access->set_bytes_per_datum)
6858e050996SLars-Peter Clausen 		return;
6868e050996SLars-Peter Clausen 
6878e050996SLars-Peter Clausen 	bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
6888e050996SLars-Peter Clausen 		buffer->scan_timestamp);
6898e050996SLars-Peter Clausen 
6908e050996SLars-Peter Clausen 	buffer->access->set_bytes_per_datum(buffer, bytes);
6918e050996SLars-Peter Clausen }
6928e050996SLars-Peter Clausen 
693fcc1b2f5SLars-Peter Clausen static int iio_buffer_request_update(struct iio_dev *indio_dev,
694fcc1b2f5SLars-Peter Clausen 	struct iio_buffer *buffer)
695fcc1b2f5SLars-Peter Clausen {
696fcc1b2f5SLars-Peter Clausen 	int ret;
697fcc1b2f5SLars-Peter Clausen 
698fcc1b2f5SLars-Peter Clausen 	iio_buffer_update_bytes_per_datum(indio_dev, buffer);
699fcc1b2f5SLars-Peter Clausen 	if (buffer->access->request_update) {
700fcc1b2f5SLars-Peter Clausen 		ret = buffer->access->request_update(buffer);
701fcc1b2f5SLars-Peter Clausen 		if (ret) {
702fcc1b2f5SLars-Peter Clausen 			dev_dbg(&indio_dev->dev,
703fcc1b2f5SLars-Peter Clausen 			       "Buffer not started: buffer parameter update failed (%d)\n",
704fcc1b2f5SLars-Peter Clausen 				ret);
705fcc1b2f5SLars-Peter Clausen 			return ret;
706fcc1b2f5SLars-Peter Clausen 		}
707fcc1b2f5SLars-Peter Clausen 	}
708fcc1b2f5SLars-Peter Clausen 
709fcc1b2f5SLars-Peter Clausen 	return 0;
710fcc1b2f5SLars-Peter Clausen }
711fcc1b2f5SLars-Peter Clausen 
712248be5aaSLars-Peter Clausen static void iio_free_scan_mask(struct iio_dev *indio_dev,
713248be5aaSLars-Peter Clausen 	const unsigned long *mask)
714248be5aaSLars-Peter Clausen {
715248be5aaSLars-Peter Clausen 	/* If the mask is dynamically allocated free it, otherwise do nothing */
716248be5aaSLars-Peter Clausen 	if (!indio_dev->available_scan_masks)
7173862828aSAndy Shevchenko 		bitmap_free(mask);
718248be5aaSLars-Peter Clausen }
719248be5aaSLars-Peter Clausen 
7206e509c4dSLars-Peter Clausen struct iio_device_config {
7216e509c4dSLars-Peter Clausen 	unsigned int mode;
722f0566c0cSLars-Peter Clausen 	unsigned int watermark;
7236e509c4dSLars-Peter Clausen 	const unsigned long *scan_mask;
7246e509c4dSLars-Peter Clausen 	unsigned int scan_bytes;
7256e509c4dSLars-Peter Clausen 	bool scan_timestamp;
7266e509c4dSLars-Peter Clausen };
7276e509c4dSLars-Peter Clausen 
7286e509c4dSLars-Peter Clausen static int iio_verify_update(struct iio_dev *indio_dev,
7296e509c4dSLars-Peter Clausen 	struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer,
7306e509c4dSLars-Peter Clausen 	struct iio_device_config *config)
7316e509c4dSLars-Peter Clausen {
7326a8c6b26SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
7336e509c4dSLars-Peter Clausen 	unsigned long *compound_mask;
7346e509c4dSLars-Peter Clausen 	const unsigned long *scan_mask;
7351e1ec286SLars-Peter Clausen 	bool strict_scanmask = false;
7366e509c4dSLars-Peter Clausen 	struct iio_buffer *buffer;
7376e509c4dSLars-Peter Clausen 	bool scan_timestamp;
738225d59adSLars-Peter Clausen 	unsigned int modes;
7396e509c4dSLars-Peter Clausen 
740b7329249SLars-Peter Clausen 	if (insert_buffer &&
741b7329249SLars-Peter Clausen 	    bitmap_empty(insert_buffer->scan_mask, indio_dev->masklength)) {
742b7329249SLars-Peter Clausen 		dev_dbg(&indio_dev->dev,
743b7329249SLars-Peter Clausen 			"At least one scan element must be enabled first\n");
744b7329249SLars-Peter Clausen 		return -EINVAL;
745b7329249SLars-Peter Clausen 	}
746b7329249SLars-Peter Clausen 
7476e509c4dSLars-Peter Clausen 	memset(config, 0, sizeof(*config));
7481bef2c1dSIrina Tirdea 	config->watermark = ~0;
7496e509c4dSLars-Peter Clausen 
7506e509c4dSLars-Peter Clausen 	/*
7516e509c4dSLars-Peter Clausen 	 * If there is just one buffer and we are removing it there is nothing
7526e509c4dSLars-Peter Clausen 	 * to verify.
7536e509c4dSLars-Peter Clausen 	 */
7546e509c4dSLars-Peter Clausen 	if (remove_buffer && !insert_buffer &&
7556a8c6b26SAlexandru Ardelean 		list_is_singular(&iio_dev_opaque->buffer_list))
7566e509c4dSLars-Peter Clausen 			return 0;
7576e509c4dSLars-Peter Clausen 
758225d59adSLars-Peter Clausen 	modes = indio_dev->modes;
759225d59adSLars-Peter Clausen 
7606a8c6b26SAlexandru Ardelean 	list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
761225d59adSLars-Peter Clausen 		if (buffer == remove_buffer)
762225d59adSLars-Peter Clausen 			continue;
763225d59adSLars-Peter Clausen 		modes &= buffer->access->modes;
764f0566c0cSLars-Peter Clausen 		config->watermark = min(config->watermark, buffer->watermark);
765225d59adSLars-Peter Clausen 	}
766225d59adSLars-Peter Clausen 
767f0566c0cSLars-Peter Clausen 	if (insert_buffer) {
768225d59adSLars-Peter Clausen 		modes &= insert_buffer->access->modes;
769f0566c0cSLars-Peter Clausen 		config->watermark = min(config->watermark,
770f0566c0cSLars-Peter Clausen 			insert_buffer->watermark);
771f0566c0cSLars-Peter Clausen 	}
772225d59adSLars-Peter Clausen 
7736e509c4dSLars-Peter Clausen 	/* Definitely possible for devices to support both of these. */
774225d59adSLars-Peter Clausen 	if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) {
7756e509c4dSLars-Peter Clausen 		config->mode = INDIO_BUFFER_TRIGGERED;
776225d59adSLars-Peter Clausen 	} else if (modes & INDIO_BUFFER_HARDWARE) {
7771e1ec286SLars-Peter Clausen 		/*
7781e1ec286SLars-Peter Clausen 		 * Keep things simple for now and only allow a single buffer to
7791e1ec286SLars-Peter Clausen 		 * be connected in hardware mode.
7801e1ec286SLars-Peter Clausen 		 */
7816a8c6b26SAlexandru Ardelean 		if (insert_buffer && !list_empty(&iio_dev_opaque->buffer_list))
7821e1ec286SLars-Peter Clausen 			return -EINVAL;
7836e509c4dSLars-Peter Clausen 		config->mode = INDIO_BUFFER_HARDWARE;
7841e1ec286SLars-Peter Clausen 		strict_scanmask = true;
785225d59adSLars-Peter Clausen 	} else if (modes & INDIO_BUFFER_SOFTWARE) {
7866e509c4dSLars-Peter Clausen 		config->mode = INDIO_BUFFER_SOFTWARE;
7876e509c4dSLars-Peter Clausen 	} else {
7886e509c4dSLars-Peter Clausen 		/* Can only occur on first buffer */
7896e509c4dSLars-Peter Clausen 		if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
7906e509c4dSLars-Peter Clausen 			dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n");
7916e509c4dSLars-Peter Clausen 		return -EINVAL;
7926e509c4dSLars-Peter Clausen 	}
7936e509c4dSLars-Peter Clausen 
7946e509c4dSLars-Peter Clausen 	/* What scan mask do we actually have? */
7953862828aSAndy Shevchenko 	compound_mask = bitmap_zalloc(indio_dev->masklength, GFP_KERNEL);
7966e509c4dSLars-Peter Clausen 	if (compound_mask == NULL)
7976e509c4dSLars-Peter Clausen 		return -ENOMEM;
7986e509c4dSLars-Peter Clausen 
7996e509c4dSLars-Peter Clausen 	scan_timestamp = false;
8006e509c4dSLars-Peter Clausen 
8016a8c6b26SAlexandru Ardelean 	list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
8026e509c4dSLars-Peter Clausen 		if (buffer == remove_buffer)
8036e509c4dSLars-Peter Clausen 			continue;
8046e509c4dSLars-Peter Clausen 		bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
8056e509c4dSLars-Peter Clausen 			  indio_dev->masklength);
8066e509c4dSLars-Peter Clausen 		scan_timestamp |= buffer->scan_timestamp;
8076e509c4dSLars-Peter Clausen 	}
8086e509c4dSLars-Peter Clausen 
8096e509c4dSLars-Peter Clausen 	if (insert_buffer) {
8106e509c4dSLars-Peter Clausen 		bitmap_or(compound_mask, compound_mask,
8116e509c4dSLars-Peter Clausen 			  insert_buffer->scan_mask, indio_dev->masklength);
8126e509c4dSLars-Peter Clausen 		scan_timestamp |= insert_buffer->scan_timestamp;
8136e509c4dSLars-Peter Clausen 	}
8146e509c4dSLars-Peter Clausen 
8156e509c4dSLars-Peter Clausen 	if (indio_dev->available_scan_masks) {
8166e509c4dSLars-Peter Clausen 		scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks,
8176e509c4dSLars-Peter Clausen 				    indio_dev->masklength,
8181e1ec286SLars-Peter Clausen 				    compound_mask,
8191e1ec286SLars-Peter Clausen 				    strict_scanmask);
8203862828aSAndy Shevchenko 		bitmap_free(compound_mask);
8216e509c4dSLars-Peter Clausen 		if (scan_mask == NULL)
8226e509c4dSLars-Peter Clausen 			return -EINVAL;
8236e509c4dSLars-Peter Clausen 	} else {
8246e509c4dSLars-Peter Clausen 	    scan_mask = compound_mask;
8256e509c4dSLars-Peter Clausen 	}
8266e509c4dSLars-Peter Clausen 
8276e509c4dSLars-Peter Clausen 	config->scan_bytes = iio_compute_scan_bytes(indio_dev,
8286e509c4dSLars-Peter Clausen 				    scan_mask, scan_timestamp);
8296e509c4dSLars-Peter Clausen 	config->scan_mask = scan_mask;
8306e509c4dSLars-Peter Clausen 	config->scan_timestamp = scan_timestamp;
8316e509c4dSLars-Peter Clausen 
8326e509c4dSLars-Peter Clausen 	return 0;
8336e509c4dSLars-Peter Clausen }
8346e509c4dSLars-Peter Clausen 
83578c9981fSJonathan Cameron /**
83678c9981fSJonathan Cameron  * struct iio_demux_table - table describing demux memcpy ops
83778c9981fSJonathan Cameron  * @from:	index to copy from
83878c9981fSJonathan Cameron  * @to:		index to copy to
83978c9981fSJonathan Cameron  * @length:	how many bytes to copy
84078c9981fSJonathan Cameron  * @l:		list head used for management
84178c9981fSJonathan Cameron  */
84278c9981fSJonathan Cameron struct iio_demux_table {
84378c9981fSJonathan Cameron 	unsigned from;
84478c9981fSJonathan Cameron 	unsigned to;
84578c9981fSJonathan Cameron 	unsigned length;
84678c9981fSJonathan Cameron 	struct list_head l;
84778c9981fSJonathan Cameron };
84878c9981fSJonathan Cameron 
84978c9981fSJonathan Cameron static void iio_buffer_demux_free(struct iio_buffer *buffer)
85078c9981fSJonathan Cameron {
85178c9981fSJonathan Cameron 	struct iio_demux_table *p, *q;
85278c9981fSJonathan Cameron 	list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
85378c9981fSJonathan Cameron 		list_del(&p->l);
85478c9981fSJonathan Cameron 		kfree(p);
85578c9981fSJonathan Cameron 	}
85678c9981fSJonathan Cameron }
85778c9981fSJonathan Cameron 
85878c9981fSJonathan Cameron static int iio_buffer_add_demux(struct iio_buffer *buffer,
85978c9981fSJonathan Cameron 	struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc,
86078c9981fSJonathan Cameron 	unsigned int length)
86178c9981fSJonathan Cameron {
86278c9981fSJonathan Cameron 
86378c9981fSJonathan Cameron 	if (*p && (*p)->from + (*p)->length == in_loc &&
86478c9981fSJonathan Cameron 		(*p)->to + (*p)->length == out_loc) {
86578c9981fSJonathan Cameron 		(*p)->length += length;
86678c9981fSJonathan Cameron 	} else {
86778c9981fSJonathan Cameron 		*p = kmalloc(sizeof(**p), GFP_KERNEL);
86878c9981fSJonathan Cameron 		if (*p == NULL)
86978c9981fSJonathan Cameron 			return -ENOMEM;
87078c9981fSJonathan Cameron 		(*p)->from = in_loc;
87178c9981fSJonathan Cameron 		(*p)->to = out_loc;
87278c9981fSJonathan Cameron 		(*p)->length = length;
87378c9981fSJonathan Cameron 		list_add_tail(&(*p)->l, &buffer->demux_list);
87478c9981fSJonathan Cameron 	}
87578c9981fSJonathan Cameron 
87678c9981fSJonathan Cameron 	return 0;
87778c9981fSJonathan Cameron }
87878c9981fSJonathan Cameron 
87978c9981fSJonathan Cameron static int iio_buffer_update_demux(struct iio_dev *indio_dev,
88078c9981fSJonathan Cameron 				   struct iio_buffer *buffer)
88178c9981fSJonathan Cameron {
88278c9981fSJonathan Cameron 	int ret, in_ind = -1, out_ind, length;
88378c9981fSJonathan Cameron 	unsigned in_loc = 0, out_loc = 0;
88478c9981fSJonathan Cameron 	struct iio_demux_table *p = NULL;
88578c9981fSJonathan Cameron 
88678c9981fSJonathan Cameron 	/* Clear out any old demux */
88778c9981fSJonathan Cameron 	iio_buffer_demux_free(buffer);
88878c9981fSJonathan Cameron 	kfree(buffer->demux_bounce);
88978c9981fSJonathan Cameron 	buffer->demux_bounce = NULL;
89078c9981fSJonathan Cameron 
89178c9981fSJonathan Cameron 	/* First work out which scan mode we will actually have */
89278c9981fSJonathan Cameron 	if (bitmap_equal(indio_dev->active_scan_mask,
89378c9981fSJonathan Cameron 			 buffer->scan_mask,
89478c9981fSJonathan Cameron 			 indio_dev->masklength))
89578c9981fSJonathan Cameron 		return 0;
89678c9981fSJonathan Cameron 
89778c9981fSJonathan Cameron 	/* Now we have the two masks, work from least sig and build up sizes */
89878c9981fSJonathan Cameron 	for_each_set_bit(out_ind,
89978c9981fSJonathan Cameron 			 buffer->scan_mask,
90078c9981fSJonathan Cameron 			 indio_dev->masklength) {
90178c9981fSJonathan Cameron 		in_ind = find_next_bit(indio_dev->active_scan_mask,
90278c9981fSJonathan Cameron 				       indio_dev->masklength,
90378c9981fSJonathan Cameron 				       in_ind + 1);
90478c9981fSJonathan Cameron 		while (in_ind != out_ind) {
90578c9981fSJonathan Cameron 			length = iio_storage_bytes_for_si(indio_dev, in_ind);
90678c9981fSJonathan Cameron 			/* Make sure we are aligned */
90778c9981fSJonathan Cameron 			in_loc = roundup(in_loc, length) + length;
90819ef7b70SNuno Sá 			in_ind = find_next_bit(indio_dev->active_scan_mask,
90919ef7b70SNuno Sá 					       indio_dev->masklength,
91019ef7b70SNuno Sá 					       in_ind + 1);
91178c9981fSJonathan Cameron 		}
91278c9981fSJonathan Cameron 		length = iio_storage_bytes_for_si(indio_dev, in_ind);
91378c9981fSJonathan Cameron 		out_loc = roundup(out_loc, length);
91478c9981fSJonathan Cameron 		in_loc = roundup(in_loc, length);
91578c9981fSJonathan Cameron 		ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
91678c9981fSJonathan Cameron 		if (ret)
91778c9981fSJonathan Cameron 			goto error_clear_mux_table;
91878c9981fSJonathan Cameron 		out_loc += length;
91978c9981fSJonathan Cameron 		in_loc += length;
92078c9981fSJonathan Cameron 	}
92178c9981fSJonathan Cameron 	/* Relies on scan_timestamp being last */
92278c9981fSJonathan Cameron 	if (buffer->scan_timestamp) {
92378c9981fSJonathan Cameron 		length = iio_storage_bytes_for_timestamp(indio_dev);
92478c9981fSJonathan Cameron 		out_loc = roundup(out_loc, length);
92578c9981fSJonathan Cameron 		in_loc = roundup(in_loc, length);
92678c9981fSJonathan Cameron 		ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
92778c9981fSJonathan Cameron 		if (ret)
92878c9981fSJonathan Cameron 			goto error_clear_mux_table;
92978c9981fSJonathan Cameron 		out_loc += length;
93078c9981fSJonathan Cameron 	}
93178c9981fSJonathan Cameron 	buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
93278c9981fSJonathan Cameron 	if (buffer->demux_bounce == NULL) {
93378c9981fSJonathan Cameron 		ret = -ENOMEM;
93478c9981fSJonathan Cameron 		goto error_clear_mux_table;
93578c9981fSJonathan Cameron 	}
93678c9981fSJonathan Cameron 	return 0;
93778c9981fSJonathan Cameron 
93878c9981fSJonathan Cameron error_clear_mux_table:
93978c9981fSJonathan Cameron 	iio_buffer_demux_free(buffer);
94078c9981fSJonathan Cameron 
94178c9981fSJonathan Cameron 	return ret;
94278c9981fSJonathan Cameron }
94378c9981fSJonathan Cameron 
94478c9981fSJonathan Cameron static int iio_update_demux(struct iio_dev *indio_dev)
94578c9981fSJonathan Cameron {
9466a8c6b26SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
94778c9981fSJonathan Cameron 	struct iio_buffer *buffer;
94878c9981fSJonathan Cameron 	int ret;
94978c9981fSJonathan Cameron 
9506a8c6b26SAlexandru Ardelean 	list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
95178c9981fSJonathan Cameron 		ret = iio_buffer_update_demux(indio_dev, buffer);
95278c9981fSJonathan Cameron 		if (ret < 0)
95378c9981fSJonathan Cameron 			goto error_clear_mux_table;
95478c9981fSJonathan Cameron 	}
95578c9981fSJonathan Cameron 	return 0;
95678c9981fSJonathan Cameron 
95778c9981fSJonathan Cameron error_clear_mux_table:
9586a8c6b26SAlexandru Ardelean 	list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list)
95978c9981fSJonathan Cameron 		iio_buffer_demux_free(buffer);
96078c9981fSJonathan Cameron 
96178c9981fSJonathan Cameron 	return ret;
96278c9981fSJonathan Cameron }
96378c9981fSJonathan Cameron 
964623d74e3SLars-Peter Clausen static int iio_enable_buffers(struct iio_dev *indio_dev,
965623d74e3SLars-Peter Clausen 	struct iio_device_config *config)
966623d74e3SLars-Peter Clausen {
9676a8c6b26SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
968e18a2ad4SLars-Peter Clausen 	struct iio_buffer *buffer;
969623d74e3SLars-Peter Clausen 	int ret;
970623d74e3SLars-Peter Clausen 
971623d74e3SLars-Peter Clausen 	indio_dev->active_scan_mask = config->scan_mask;
972623d74e3SLars-Peter Clausen 	indio_dev->scan_timestamp = config->scan_timestamp;
973623d74e3SLars-Peter Clausen 	indio_dev->scan_bytes = config->scan_bytes;
9745cb1a548SLars-Peter Clausen 	indio_dev->currentmode = config->mode;
975623d74e3SLars-Peter Clausen 
976623d74e3SLars-Peter Clausen 	iio_update_demux(indio_dev);
977623d74e3SLars-Peter Clausen 
978623d74e3SLars-Peter Clausen 	/* Wind up again */
979623d74e3SLars-Peter Clausen 	if (indio_dev->setup_ops->preenable) {
980623d74e3SLars-Peter Clausen 		ret = indio_dev->setup_ops->preenable(indio_dev);
981623d74e3SLars-Peter Clausen 		if (ret) {
982623d74e3SLars-Peter Clausen 			dev_dbg(&indio_dev->dev,
983623d74e3SLars-Peter Clausen 			       "Buffer not started: buffer preenable failed (%d)\n", ret);
984623d74e3SLars-Peter Clausen 			goto err_undo_config;
985623d74e3SLars-Peter Clausen 		}
986623d74e3SLars-Peter Clausen 	}
987623d74e3SLars-Peter Clausen 
988623d74e3SLars-Peter Clausen 	if (indio_dev->info->update_scan_mode) {
989623d74e3SLars-Peter Clausen 		ret = indio_dev->info
990623d74e3SLars-Peter Clausen 			->update_scan_mode(indio_dev,
991623d74e3SLars-Peter Clausen 					   indio_dev->active_scan_mask);
992623d74e3SLars-Peter Clausen 		if (ret < 0) {
993623d74e3SLars-Peter Clausen 			dev_dbg(&indio_dev->dev,
994623d74e3SLars-Peter Clausen 				"Buffer not started: update scan mode failed (%d)\n",
995623d74e3SLars-Peter Clausen 				ret);
996623d74e3SLars-Peter Clausen 			goto err_run_postdisable;
997623d74e3SLars-Peter Clausen 		}
998623d74e3SLars-Peter Clausen 	}
999623d74e3SLars-Peter Clausen 
1000f0566c0cSLars-Peter Clausen 	if (indio_dev->info->hwfifo_set_watermark)
1001f0566c0cSLars-Peter Clausen 		indio_dev->info->hwfifo_set_watermark(indio_dev,
1002f0566c0cSLars-Peter Clausen 			config->watermark);
1003f0566c0cSLars-Peter Clausen 
10046a8c6b26SAlexandru Ardelean 	list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
1005e18a2ad4SLars-Peter Clausen 		ret = iio_buffer_enable(buffer, indio_dev);
1006e18a2ad4SLars-Peter Clausen 		if (ret)
1007e18a2ad4SLars-Peter Clausen 			goto err_disable_buffers;
1008e18a2ad4SLars-Peter Clausen 	}
1009e18a2ad4SLars-Peter Clausen 
1010f11d59d8SLars-Peter Clausen 	if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
1011f11d59d8SLars-Peter Clausen 		ret = iio_trigger_attach_poll_func(indio_dev->trig,
1012f11d59d8SLars-Peter Clausen 						   indio_dev->pollfunc);
1013f11d59d8SLars-Peter Clausen 		if (ret)
1014f11d59d8SLars-Peter Clausen 			goto err_disable_buffers;
1015f11d59d8SLars-Peter Clausen 	}
1016f11d59d8SLars-Peter Clausen 
101762a30a29SAlexandru Ardelean 	if (indio_dev->setup_ops->postenable) {
101862a30a29SAlexandru Ardelean 		ret = indio_dev->setup_ops->postenable(indio_dev);
101962a30a29SAlexandru Ardelean 		if (ret) {
102062a30a29SAlexandru Ardelean 			dev_dbg(&indio_dev->dev,
102162a30a29SAlexandru Ardelean 			       "Buffer not started: postenable failed (%d)\n", ret);
102262a30a29SAlexandru Ardelean 			goto err_detach_pollfunc;
102362a30a29SAlexandru Ardelean 		}
102462a30a29SAlexandru Ardelean 	}
102562a30a29SAlexandru Ardelean 
1026623d74e3SLars-Peter Clausen 	return 0;
1027623d74e3SLars-Peter Clausen 
102862a30a29SAlexandru Ardelean err_detach_pollfunc:
102962a30a29SAlexandru Ardelean 	if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
103062a30a29SAlexandru Ardelean 		iio_trigger_detach_poll_func(indio_dev->trig,
103162a30a29SAlexandru Ardelean 					     indio_dev->pollfunc);
103262a30a29SAlexandru Ardelean 	}
1033e18a2ad4SLars-Peter Clausen err_disable_buffers:
10346a8c6b26SAlexandru Ardelean 	list_for_each_entry_continue_reverse(buffer, &iio_dev_opaque->buffer_list,
1035e18a2ad4SLars-Peter Clausen 					     buffer_list)
1036e18a2ad4SLars-Peter Clausen 		iio_buffer_disable(buffer, indio_dev);
1037623d74e3SLars-Peter Clausen err_run_postdisable:
1038623d74e3SLars-Peter Clausen 	if (indio_dev->setup_ops->postdisable)
1039623d74e3SLars-Peter Clausen 		indio_dev->setup_ops->postdisable(indio_dev);
1040623d74e3SLars-Peter Clausen err_undo_config:
10415cb1a548SLars-Peter Clausen 	indio_dev->currentmode = INDIO_DIRECT_MODE;
1042623d74e3SLars-Peter Clausen 	indio_dev->active_scan_mask = NULL;
1043623d74e3SLars-Peter Clausen 
1044623d74e3SLars-Peter Clausen 	return ret;
1045623d74e3SLars-Peter Clausen }
1046623d74e3SLars-Peter Clausen 
1047623d74e3SLars-Peter Clausen static int iio_disable_buffers(struct iio_dev *indio_dev)
1048623d74e3SLars-Peter Clausen {
10496a8c6b26SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1050e18a2ad4SLars-Peter Clausen 	struct iio_buffer *buffer;
10511250186aSLars-Peter Clausen 	int ret = 0;
10521250186aSLars-Peter Clausen 	int ret2;
1053623d74e3SLars-Peter Clausen 
1054623d74e3SLars-Peter Clausen 	/* Wind down existing buffers - iff there are any */
10556a8c6b26SAlexandru Ardelean 	if (list_empty(&iio_dev_opaque->buffer_list))
1056623d74e3SLars-Peter Clausen 		return 0;
1057623d74e3SLars-Peter Clausen 
10581250186aSLars-Peter Clausen 	/*
10591250186aSLars-Peter Clausen 	 * If things go wrong at some step in disable we still need to continue
10601250186aSLars-Peter Clausen 	 * to perform the other steps, otherwise we leave the device in a
10611250186aSLars-Peter Clausen 	 * inconsistent state. We return the error code for the first error we
10621250186aSLars-Peter Clausen 	 * encountered.
10631250186aSLars-Peter Clausen 	 */
10641250186aSLars-Peter Clausen 
1065623d74e3SLars-Peter Clausen 	if (indio_dev->setup_ops->predisable) {
10661250186aSLars-Peter Clausen 		ret2 = indio_dev->setup_ops->predisable(indio_dev);
10671250186aSLars-Peter Clausen 		if (ret2 && !ret)
10681250186aSLars-Peter Clausen 			ret = ret2;
1069623d74e3SLars-Peter Clausen 	}
1070623d74e3SLars-Peter Clausen 
107162a30a29SAlexandru Ardelean 	if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
107262a30a29SAlexandru Ardelean 		iio_trigger_detach_poll_func(indio_dev->trig,
107362a30a29SAlexandru Ardelean 					     indio_dev->pollfunc);
107462a30a29SAlexandru Ardelean 	}
107562a30a29SAlexandru Ardelean 
10766a8c6b26SAlexandru Ardelean 	list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
1077e18a2ad4SLars-Peter Clausen 		ret2 = iio_buffer_disable(buffer, indio_dev);
1078e18a2ad4SLars-Peter Clausen 		if (ret2 && !ret)
1079e18a2ad4SLars-Peter Clausen 			ret = ret2;
1080e18a2ad4SLars-Peter Clausen 	}
1081e18a2ad4SLars-Peter Clausen 
1082623d74e3SLars-Peter Clausen 	if (indio_dev->setup_ops->postdisable) {
10831250186aSLars-Peter Clausen 		ret2 = indio_dev->setup_ops->postdisable(indio_dev);
10841250186aSLars-Peter Clausen 		if (ret2 && !ret)
10851250186aSLars-Peter Clausen 			ret = ret2;
1086623d74e3SLars-Peter Clausen 	}
1087623d74e3SLars-Peter Clausen 
10881250186aSLars-Peter Clausen 	iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask);
10891250186aSLars-Peter Clausen 	indio_dev->active_scan_mask = NULL;
10905cb1a548SLars-Peter Clausen 	indio_dev->currentmode = INDIO_DIRECT_MODE;
10911250186aSLars-Peter Clausen 
10921250186aSLars-Peter Clausen 	return ret;
1093623d74e3SLars-Peter Clausen }
1094623d74e3SLars-Peter Clausen 
1095a9519456SLars-Peter Clausen static int __iio_update_buffers(struct iio_dev *indio_dev,
109684b36ce5SJonathan Cameron 		       struct iio_buffer *insert_buffer,
109784b36ce5SJonathan Cameron 		       struct iio_buffer *remove_buffer)
1098a980e046SJonathan Cameron {
10996a8c6b26SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
11006e509c4dSLars-Peter Clausen 	struct iio_device_config new_config;
11011250186aSLars-Peter Clausen 	int ret;
11026e509c4dSLars-Peter Clausen 
11036e509c4dSLars-Peter Clausen 	ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer,
11046e509c4dSLars-Peter Clausen 		&new_config);
11056e509c4dSLars-Peter Clausen 	if (ret)
11066e509c4dSLars-Peter Clausen 		return ret;
1107a980e046SJonathan Cameron 
1108fcc1b2f5SLars-Peter Clausen 	if (insert_buffer) {
1109fcc1b2f5SLars-Peter Clausen 		ret = iio_buffer_request_update(indio_dev, insert_buffer);
1110fcc1b2f5SLars-Peter Clausen 		if (ret)
11116e509c4dSLars-Peter Clausen 			goto err_free_config;
1112fcc1b2f5SLars-Peter Clausen 	}
1113fcc1b2f5SLars-Peter Clausen 
1114623d74e3SLars-Peter Clausen 	ret = iio_disable_buffers(indio_dev);
11151250186aSLars-Peter Clausen 	if (ret)
11161250186aSLars-Peter Clausen 		goto err_deactivate_all;
1117623d74e3SLars-Peter Clausen 
111884b36ce5SJonathan Cameron 	if (remove_buffer)
11199e69c935SLars-Peter Clausen 		iio_buffer_deactivate(remove_buffer);
112084b36ce5SJonathan Cameron 	if (insert_buffer)
11219e69c935SLars-Peter Clausen 		iio_buffer_activate(indio_dev, insert_buffer);
112284b36ce5SJonathan Cameron 
112384b36ce5SJonathan Cameron 	/* If no buffers in list, we are done */
11246a8c6b26SAlexandru Ardelean 	if (list_empty(&iio_dev_opaque->buffer_list))
112584b36ce5SJonathan Cameron 		return 0;
1126a980e046SJonathan Cameron 
1127623d74e3SLars-Peter Clausen 	ret = iio_enable_buffers(indio_dev, &new_config);
11281250186aSLars-Peter Clausen 	if (ret)
11291250186aSLars-Peter Clausen 		goto err_deactivate_all;
1130623d74e3SLars-Peter Clausen 
1131623d74e3SLars-Peter Clausen 	return 0;
11326e509c4dSLars-Peter Clausen 
11331250186aSLars-Peter Clausen err_deactivate_all:
11341250186aSLars-Peter Clausen 	/*
11351250186aSLars-Peter Clausen 	 * We've already verified that the config is valid earlier. If things go
11361250186aSLars-Peter Clausen 	 * wrong in either enable or disable the most likely reason is an IO
11371250186aSLars-Peter Clausen 	 * error from the device. In this case there is no good recovery
11381250186aSLars-Peter Clausen 	 * strategy. Just make sure to disable everything and leave the device
11391250186aSLars-Peter Clausen 	 * in a sane state.  With a bit of luck the device might come back to
11401250186aSLars-Peter Clausen 	 * life again later and userspace can try again.
11411250186aSLars-Peter Clausen 	 */
11421250186aSLars-Peter Clausen 	iio_buffer_deactivate_all(indio_dev);
11431250186aSLars-Peter Clausen 
11446e509c4dSLars-Peter Clausen err_free_config:
11456e509c4dSLars-Peter Clausen 	iio_free_scan_mask(indio_dev, new_config.scan_mask);
11466e509c4dSLars-Peter Clausen 	return ret;
114784b36ce5SJonathan Cameron }
1148a9519456SLars-Peter Clausen 
1149a9519456SLars-Peter Clausen int iio_update_buffers(struct iio_dev *indio_dev,
1150a9519456SLars-Peter Clausen 		       struct iio_buffer *insert_buffer,
1151a9519456SLars-Peter Clausen 		       struct iio_buffer *remove_buffer)
1152a9519456SLars-Peter Clausen {
1153b804e2b7SJonathan Cameron 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1154a9519456SLars-Peter Clausen 	int ret;
1155a9519456SLars-Peter Clausen 
11563909fab5SLars-Peter Clausen 	if (insert_buffer == remove_buffer)
11573909fab5SLars-Peter Clausen 		return 0;
11583909fab5SLars-Peter Clausen 
1159b804e2b7SJonathan Cameron 	mutex_lock(&iio_dev_opaque->info_exist_lock);
1160a9519456SLars-Peter Clausen 	mutex_lock(&indio_dev->mlock);
1161a9519456SLars-Peter Clausen 
11623909fab5SLars-Peter Clausen 	if (insert_buffer && iio_buffer_is_active(insert_buffer))
11633909fab5SLars-Peter Clausen 		insert_buffer = NULL;
11643909fab5SLars-Peter Clausen 
11653909fab5SLars-Peter Clausen 	if (remove_buffer && !iio_buffer_is_active(remove_buffer))
11663909fab5SLars-Peter Clausen 		remove_buffer = NULL;
11673909fab5SLars-Peter Clausen 
11683909fab5SLars-Peter Clausen 	if (!insert_buffer && !remove_buffer) {
11693909fab5SLars-Peter Clausen 		ret = 0;
11703909fab5SLars-Peter Clausen 		goto out_unlock;
11713909fab5SLars-Peter Clausen 	}
11723909fab5SLars-Peter Clausen 
1173a9519456SLars-Peter Clausen 	if (indio_dev->info == NULL) {
1174a9519456SLars-Peter Clausen 		ret = -ENODEV;
1175a9519456SLars-Peter Clausen 		goto out_unlock;
1176a9519456SLars-Peter Clausen 	}
1177a9519456SLars-Peter Clausen 
1178a9519456SLars-Peter Clausen 	ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
1179a9519456SLars-Peter Clausen 
1180a9519456SLars-Peter Clausen out_unlock:
1181a9519456SLars-Peter Clausen 	mutex_unlock(&indio_dev->mlock);
1182b804e2b7SJonathan Cameron 	mutex_unlock(&iio_dev_opaque->info_exist_lock);
1183a9519456SLars-Peter Clausen 
1184a9519456SLars-Peter Clausen 	return ret;
1185a9519456SLars-Peter Clausen }
118684b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_update_buffers);
118784b36ce5SJonathan Cameron 
1188623d74e3SLars-Peter Clausen void iio_disable_all_buffers(struct iio_dev *indio_dev)
1189623d74e3SLars-Peter Clausen {
1190623d74e3SLars-Peter Clausen 	iio_disable_buffers(indio_dev);
11911250186aSLars-Peter Clausen 	iio_buffer_deactivate_all(indio_dev);
1192623d74e3SLars-Peter Clausen }
1193623d74e3SLars-Peter Clausen 
119408e7e0adSLars-Peter Clausen static ssize_t iio_buffer_store_enable(struct device *dev,
119584b36ce5SJonathan Cameron 				       struct device_attribute *attr,
119684b36ce5SJonathan Cameron 				       const char *buf,
119784b36ce5SJonathan Cameron 				       size_t len)
119884b36ce5SJonathan Cameron {
119984b36ce5SJonathan Cameron 	int ret;
120084b36ce5SJonathan Cameron 	bool requested_state;
120184b36ce5SJonathan Cameron 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
120215097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
120384b36ce5SJonathan Cameron 	bool inlist;
120484b36ce5SJonathan Cameron 
120584b36ce5SJonathan Cameron 	ret = strtobool(buf, &requested_state);
120684b36ce5SJonathan Cameron 	if (ret < 0)
120784b36ce5SJonathan Cameron 		return ret;
120884b36ce5SJonathan Cameron 
120984b36ce5SJonathan Cameron 	mutex_lock(&indio_dev->mlock);
121084b36ce5SJonathan Cameron 
121184b36ce5SJonathan Cameron 	/* Find out if it is in the list */
1212ff3f7e04SAlexandru Ardelean 	inlist = iio_buffer_is_active(buffer);
121384b36ce5SJonathan Cameron 	/* Already in desired state */
121484b36ce5SJonathan Cameron 	if (inlist == requested_state)
121584b36ce5SJonathan Cameron 		goto done;
121684b36ce5SJonathan Cameron 
121784b36ce5SJonathan Cameron 	if (requested_state)
1218ff3f7e04SAlexandru Ardelean 		ret = __iio_update_buffers(indio_dev, buffer, NULL);
121984b36ce5SJonathan Cameron 	else
1220ff3f7e04SAlexandru Ardelean 		ret = __iio_update_buffers(indio_dev, NULL, buffer);
122184b36ce5SJonathan Cameron 
122284b36ce5SJonathan Cameron done:
122384b36ce5SJonathan Cameron 	mutex_unlock(&indio_dev->mlock);
122484b36ce5SJonathan Cameron 	return (ret < 0) ? ret : len;
122584b36ce5SJonathan Cameron }
122684b36ce5SJonathan Cameron 
122737d34556SJosselin Costanzi static ssize_t iio_buffer_show_watermark(struct device *dev,
122837d34556SJosselin Costanzi 					 struct device_attribute *attr,
122937d34556SJosselin Costanzi 					 char *buf)
123037d34556SJosselin Costanzi {
123115097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
123237d34556SJosselin Costanzi 
123383ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%u\n", buffer->watermark);
123437d34556SJosselin Costanzi }
123537d34556SJosselin Costanzi 
123637d34556SJosselin Costanzi static ssize_t iio_buffer_store_watermark(struct device *dev,
123737d34556SJosselin Costanzi 					  struct device_attribute *attr,
123837d34556SJosselin Costanzi 					  const char *buf,
123937d34556SJosselin Costanzi 					  size_t len)
124037d34556SJosselin Costanzi {
124137d34556SJosselin Costanzi 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
124215097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
124337d34556SJosselin Costanzi 	unsigned int val;
124437d34556SJosselin Costanzi 	int ret;
124537d34556SJosselin Costanzi 
124637d34556SJosselin Costanzi 	ret = kstrtouint(buf, 10, &val);
124737d34556SJosselin Costanzi 	if (ret)
124837d34556SJosselin Costanzi 		return ret;
124937d34556SJosselin Costanzi 	if (!val)
125037d34556SJosselin Costanzi 		return -EINVAL;
125137d34556SJosselin Costanzi 
125237d34556SJosselin Costanzi 	mutex_lock(&indio_dev->mlock);
125337d34556SJosselin Costanzi 
125437d34556SJosselin Costanzi 	if (val > buffer->length) {
125537d34556SJosselin Costanzi 		ret = -EINVAL;
125637d34556SJosselin Costanzi 		goto out;
125737d34556SJosselin Costanzi 	}
125837d34556SJosselin Costanzi 
1259ff3f7e04SAlexandru Ardelean 	if (iio_buffer_is_active(buffer)) {
126037d34556SJosselin Costanzi 		ret = -EBUSY;
126137d34556SJosselin Costanzi 		goto out;
126237d34556SJosselin Costanzi 	}
126337d34556SJosselin Costanzi 
126437d34556SJosselin Costanzi 	buffer->watermark = val;
126537d34556SJosselin Costanzi out:
126637d34556SJosselin Costanzi 	mutex_unlock(&indio_dev->mlock);
126737d34556SJosselin Costanzi 
126837d34556SJosselin Costanzi 	return ret ? ret : len;
126937d34556SJosselin Costanzi }
127037d34556SJosselin Costanzi 
1271350f6c75SMatt Fornero static ssize_t iio_dma_show_data_available(struct device *dev,
1272350f6c75SMatt Fornero 						struct device_attribute *attr,
1273350f6c75SMatt Fornero 						char *buf)
1274350f6c75SMatt Fornero {
127515097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
1276350f6c75SMatt Fornero 
127783ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%zu\n", iio_buffer_data_available(buffer));
1278350f6c75SMatt Fornero }
1279350f6c75SMatt Fornero 
128008e7e0adSLars-Peter Clausen static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
128108e7e0adSLars-Peter Clausen 		   iio_buffer_write_length);
12828d92db28SLars-Peter Clausen static struct device_attribute dev_attr_length_ro = __ATTR(length,
12838d92db28SLars-Peter Clausen 	S_IRUGO, iio_buffer_read_length, NULL);
128408e7e0adSLars-Peter Clausen static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
128508e7e0adSLars-Peter Clausen 		   iio_buffer_show_enable, iio_buffer_store_enable);
128637d34556SJosselin Costanzi static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR,
128737d34556SJosselin Costanzi 		   iio_buffer_show_watermark, iio_buffer_store_watermark);
1288b440655bSLars-Peter Clausen static struct device_attribute dev_attr_watermark_ro = __ATTR(watermark,
1289b440655bSLars-Peter Clausen 	S_IRUGO, iio_buffer_show_watermark, NULL);
1290350f6c75SMatt Fornero static DEVICE_ATTR(data_available, S_IRUGO,
1291350f6c75SMatt Fornero 		iio_dma_show_data_available, NULL);
129208e7e0adSLars-Peter Clausen 
12936da9b382SOctavian Purdila static struct attribute *iio_buffer_attrs[] = {
12946da9b382SOctavian Purdila 	&dev_attr_length.attr,
12956da9b382SOctavian Purdila 	&dev_attr_enable.attr,
129637d34556SJosselin Costanzi 	&dev_attr_watermark.attr,
1297350f6c75SMatt Fornero 	&dev_attr_data_available.attr,
12986da9b382SOctavian Purdila };
12996da9b382SOctavian Purdila 
130015097c7aSAlexandru Ardelean #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
130115097c7aSAlexandru Ardelean 
130215097c7aSAlexandru Ardelean static struct attribute *iio_buffer_wrap_attr(struct iio_buffer *buffer,
130315097c7aSAlexandru Ardelean 					      struct attribute *attr)
130415097c7aSAlexandru Ardelean {
130515097c7aSAlexandru Ardelean 	struct device_attribute *dattr = to_dev_attr(attr);
130615097c7aSAlexandru Ardelean 	struct iio_dev_attr *iio_attr;
130715097c7aSAlexandru Ardelean 
130815097c7aSAlexandru Ardelean 	iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
130915097c7aSAlexandru Ardelean 	if (!iio_attr)
131015097c7aSAlexandru Ardelean 		return NULL;
131115097c7aSAlexandru Ardelean 
131215097c7aSAlexandru Ardelean 	iio_attr->buffer = buffer;
131315097c7aSAlexandru Ardelean 	memcpy(&iio_attr->dev_attr, dattr, sizeof(iio_attr->dev_attr));
131415097c7aSAlexandru Ardelean 	iio_attr->dev_attr.attr.name = kstrdup_const(attr->name, GFP_KERNEL);
13152c0ad3f0SYang Yingliang 	if (!iio_attr->dev_attr.attr.name) {
13162c0ad3f0SYang Yingliang 		kfree(iio_attr);
13172c0ad3f0SYang Yingliang 		return NULL;
13182c0ad3f0SYang Yingliang 	}
13192c0ad3f0SYang Yingliang 
1320ca3e7d52SAlexandru Ardelean 	sysfs_attr_init(&iio_attr->dev_attr.attr);
132115097c7aSAlexandru Ardelean 
132215097c7aSAlexandru Ardelean 	list_add(&iio_attr->l, &buffer->buffer_attr_list);
132315097c7aSAlexandru Ardelean 
132415097c7aSAlexandru Ardelean 	return &iio_attr->dev_attr.attr;
132515097c7aSAlexandru Ardelean }
132615097c7aSAlexandru Ardelean 
1327d9a62574SAlexandru Ardelean static int iio_buffer_register_legacy_sysfs_groups(struct iio_dev *indio_dev,
1328d9a62574SAlexandru Ardelean 						   struct attribute **buffer_attrs,
1329d9a62574SAlexandru Ardelean 						   int buffer_attrcount,
1330d9a62574SAlexandru Ardelean 						   int scan_el_attrcount)
1331d9a62574SAlexandru Ardelean {
1332d9a62574SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1333d9a62574SAlexandru Ardelean 	struct attribute_group *group;
1334d9a62574SAlexandru Ardelean 	struct attribute **attrs;
1335d9a62574SAlexandru Ardelean 	int ret;
1336d9a62574SAlexandru Ardelean 
1337d9a62574SAlexandru Ardelean 	attrs = kcalloc(buffer_attrcount + 1, sizeof(*attrs), GFP_KERNEL);
1338d9a62574SAlexandru Ardelean 	if (!attrs)
1339d9a62574SAlexandru Ardelean 		return -ENOMEM;
1340d9a62574SAlexandru Ardelean 
1341d9a62574SAlexandru Ardelean 	memcpy(attrs, buffer_attrs, buffer_attrcount * sizeof(*attrs));
1342d9a62574SAlexandru Ardelean 
1343d9a62574SAlexandru Ardelean 	group = &iio_dev_opaque->legacy_buffer_group;
1344d9a62574SAlexandru Ardelean 	group->attrs = attrs;
1345d9a62574SAlexandru Ardelean 	group->name = "buffer";
1346d9a62574SAlexandru Ardelean 
1347d9a62574SAlexandru Ardelean 	ret = iio_device_register_sysfs_group(indio_dev, group);
1348d9a62574SAlexandru Ardelean 	if (ret)
1349d9a62574SAlexandru Ardelean 		goto error_free_buffer_attrs;
1350d9a62574SAlexandru Ardelean 
1351d9a62574SAlexandru Ardelean 	attrs = kcalloc(scan_el_attrcount + 1, sizeof(*attrs), GFP_KERNEL);
1352d9a62574SAlexandru Ardelean 	if (!attrs) {
1353d9a62574SAlexandru Ardelean 		ret = -ENOMEM;
1354d9a62574SAlexandru Ardelean 		goto error_free_buffer_attrs;
1355d9a62574SAlexandru Ardelean 	}
1356d9a62574SAlexandru Ardelean 
1357d9a62574SAlexandru Ardelean 	memcpy(attrs, &buffer_attrs[buffer_attrcount],
1358d9a62574SAlexandru Ardelean 	       scan_el_attrcount * sizeof(*attrs));
1359d9a62574SAlexandru Ardelean 
1360d9a62574SAlexandru Ardelean 	group = &iio_dev_opaque->legacy_scan_el_group;
1361d9a62574SAlexandru Ardelean 	group->attrs = attrs;
1362d9a62574SAlexandru Ardelean 	group->name = "scan_elements";
1363d9a62574SAlexandru Ardelean 
1364d9a62574SAlexandru Ardelean 	ret = iio_device_register_sysfs_group(indio_dev, group);
1365d9a62574SAlexandru Ardelean 	if (ret)
1366d9a62574SAlexandru Ardelean 		goto error_free_scan_el_attrs;
1367d9a62574SAlexandru Ardelean 
1368d9a62574SAlexandru Ardelean 	return 0;
1369d9a62574SAlexandru Ardelean 
1370d9a62574SAlexandru Ardelean error_free_scan_el_attrs:
1371d9a62574SAlexandru Ardelean 	kfree(iio_dev_opaque->legacy_scan_el_group.attrs);
1372*604faf9aSYang Yingliang error_free_buffer_attrs:
1373*604faf9aSYang Yingliang 	kfree(iio_dev_opaque->legacy_buffer_group.attrs);
1374d9a62574SAlexandru Ardelean 
1375d9a62574SAlexandru Ardelean 	return ret;
1376d9a62574SAlexandru Ardelean }
1377d9a62574SAlexandru Ardelean 
1378d9a62574SAlexandru Ardelean static void iio_buffer_unregister_legacy_sysfs_groups(struct iio_dev *indio_dev)
1379d9a62574SAlexandru Ardelean {
1380d9a62574SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1381d9a62574SAlexandru Ardelean 
1382d9a62574SAlexandru Ardelean 	kfree(iio_dev_opaque->legacy_buffer_group.attrs);
1383d9a62574SAlexandru Ardelean 	kfree(iio_dev_opaque->legacy_scan_el_group.attrs);
1384d9a62574SAlexandru Ardelean }
1385d9a62574SAlexandru Ardelean 
1386f73f7f4dSAlexandru Ardelean static int iio_buffer_chrdev_release(struct inode *inode, struct file *filep)
1387f73f7f4dSAlexandru Ardelean {
1388f73f7f4dSAlexandru Ardelean 	struct iio_dev_buffer_pair *ib = filep->private_data;
1389f73f7f4dSAlexandru Ardelean 	struct iio_dev *indio_dev = ib->indio_dev;
1390f73f7f4dSAlexandru Ardelean 	struct iio_buffer *buffer = ib->buffer;
1391f73f7f4dSAlexandru Ardelean 
1392f73f7f4dSAlexandru Ardelean 	wake_up(&buffer->pollq);
1393f73f7f4dSAlexandru Ardelean 
1394f73f7f4dSAlexandru Ardelean 	kfree(ib);
1395f73f7f4dSAlexandru Ardelean 	clear_bit(IIO_BUSY_BIT_POS, &buffer->flags);
1396f73f7f4dSAlexandru Ardelean 	iio_device_put(indio_dev);
1397f73f7f4dSAlexandru Ardelean 
1398f73f7f4dSAlexandru Ardelean 	return 0;
1399f73f7f4dSAlexandru Ardelean }
1400f73f7f4dSAlexandru Ardelean 
1401f73f7f4dSAlexandru Ardelean static const struct file_operations iio_buffer_chrdev_fileops = {
1402f73f7f4dSAlexandru Ardelean 	.owner = THIS_MODULE,
1403f73f7f4dSAlexandru Ardelean 	.llseek = noop_llseek,
1404f73f7f4dSAlexandru Ardelean 	.read = iio_buffer_read,
1405f73f7f4dSAlexandru Ardelean 	.poll = iio_buffer_poll,
1406f73f7f4dSAlexandru Ardelean 	.release = iio_buffer_chrdev_release,
1407f73f7f4dSAlexandru Ardelean };
1408f73f7f4dSAlexandru Ardelean 
1409f73f7f4dSAlexandru Ardelean static long iio_device_buffer_getfd(struct iio_dev *indio_dev, unsigned long arg)
1410f73f7f4dSAlexandru Ardelean {
1411f73f7f4dSAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1412f73f7f4dSAlexandru Ardelean 	int __user *ival = (int __user *)arg;
1413f73f7f4dSAlexandru Ardelean 	struct iio_dev_buffer_pair *ib;
1414f73f7f4dSAlexandru Ardelean 	struct iio_buffer *buffer;
1415f73f7f4dSAlexandru Ardelean 	int fd, idx, ret;
1416f73f7f4dSAlexandru Ardelean 
1417f73f7f4dSAlexandru Ardelean 	if (copy_from_user(&idx, ival, sizeof(idx)))
1418f73f7f4dSAlexandru Ardelean 		return -EFAULT;
1419f73f7f4dSAlexandru Ardelean 
1420f73f7f4dSAlexandru Ardelean 	if (idx >= iio_dev_opaque->attached_buffers_cnt)
1421f73f7f4dSAlexandru Ardelean 		return -ENODEV;
1422f73f7f4dSAlexandru Ardelean 
1423f73f7f4dSAlexandru Ardelean 	iio_device_get(indio_dev);
1424f73f7f4dSAlexandru Ardelean 
1425f73f7f4dSAlexandru Ardelean 	buffer = iio_dev_opaque->attached_buffers[idx];
1426f73f7f4dSAlexandru Ardelean 
1427f73f7f4dSAlexandru Ardelean 	if (test_and_set_bit(IIO_BUSY_BIT_POS, &buffer->flags)) {
1428f73f7f4dSAlexandru Ardelean 		ret = -EBUSY;
1429f73f7f4dSAlexandru Ardelean 		goto error_iio_dev_put;
1430f73f7f4dSAlexandru Ardelean 	}
1431f73f7f4dSAlexandru Ardelean 
1432f73f7f4dSAlexandru Ardelean 	ib = kzalloc(sizeof(*ib), GFP_KERNEL);
1433f73f7f4dSAlexandru Ardelean 	if (!ib) {
1434f73f7f4dSAlexandru Ardelean 		ret = -ENOMEM;
1435f73f7f4dSAlexandru Ardelean 		goto error_clear_busy_bit;
1436f73f7f4dSAlexandru Ardelean 	}
1437f73f7f4dSAlexandru Ardelean 
1438f73f7f4dSAlexandru Ardelean 	ib->indio_dev = indio_dev;
1439f73f7f4dSAlexandru Ardelean 	ib->buffer = buffer;
1440f73f7f4dSAlexandru Ardelean 
1441f73f7f4dSAlexandru Ardelean 	fd = anon_inode_getfd("iio:buffer", &iio_buffer_chrdev_fileops,
1442f73f7f4dSAlexandru Ardelean 			      ib, O_RDWR | O_CLOEXEC);
1443f73f7f4dSAlexandru Ardelean 	if (fd < 0) {
1444f73f7f4dSAlexandru Ardelean 		ret = fd;
1445f73f7f4dSAlexandru Ardelean 		goto error_free_ib;
1446f73f7f4dSAlexandru Ardelean 	}
1447f73f7f4dSAlexandru Ardelean 
1448f73f7f4dSAlexandru Ardelean 	if (copy_to_user(ival, &fd, sizeof(fd))) {
1449f73f7f4dSAlexandru Ardelean 		put_unused_fd(fd);
1450f73f7f4dSAlexandru Ardelean 		ret = -EFAULT;
1451f73f7f4dSAlexandru Ardelean 		goto error_free_ib;
1452f73f7f4dSAlexandru Ardelean 	}
1453f73f7f4dSAlexandru Ardelean 
14544c822244SAlexandru Ardelean 	return 0;
1455f73f7f4dSAlexandru Ardelean 
1456f73f7f4dSAlexandru Ardelean error_free_ib:
1457f73f7f4dSAlexandru Ardelean 	kfree(ib);
1458f73f7f4dSAlexandru Ardelean error_clear_busy_bit:
1459f73f7f4dSAlexandru Ardelean 	clear_bit(IIO_BUSY_BIT_POS, &buffer->flags);
1460f73f7f4dSAlexandru Ardelean error_iio_dev_put:
1461f73f7f4dSAlexandru Ardelean 	iio_device_put(indio_dev);
1462f73f7f4dSAlexandru Ardelean 	return ret;
1463f73f7f4dSAlexandru Ardelean }
1464f73f7f4dSAlexandru Ardelean 
1465f73f7f4dSAlexandru Ardelean static long iio_device_buffer_ioctl(struct iio_dev *indio_dev, struct file *filp,
1466f73f7f4dSAlexandru Ardelean 				    unsigned int cmd, unsigned long arg)
1467f73f7f4dSAlexandru Ardelean {
1468f73f7f4dSAlexandru Ardelean 	switch (cmd) {
1469f73f7f4dSAlexandru Ardelean 	case IIO_BUFFER_GET_FD_IOCTL:
1470f73f7f4dSAlexandru Ardelean 		return iio_device_buffer_getfd(indio_dev, arg);
1471f73f7f4dSAlexandru Ardelean 	default:
1472f73f7f4dSAlexandru Ardelean 		return IIO_IOCTL_UNHANDLED;
1473f73f7f4dSAlexandru Ardelean 	}
1474f73f7f4dSAlexandru Ardelean }
1475f73f7f4dSAlexandru Ardelean 
1476e16e0a77SAlexandru Ardelean static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
1477d9a62574SAlexandru Ardelean 					     struct iio_dev *indio_dev,
1478d9a62574SAlexandru Ardelean 					     int index)
1479d967cb6bSLars-Peter Clausen {
148062f4f36cSJonathan Cameron 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1481d967cb6bSLars-Peter Clausen 	struct iio_dev_attr *p;
1482d967cb6bSLars-Peter Clausen 	struct attribute **attr;
1483e2b4d7acSAlexandru Ardelean 	int ret, i, attrn, scan_el_attrcount, buffer_attrcount;
1484d967cb6bSLars-Peter Clausen 	const struct iio_chan_spec *channels;
1485d967cb6bSLars-Peter Clausen 
1486e2b4d7acSAlexandru Ardelean 	buffer_attrcount = 0;
148708e7e0adSLars-Peter Clausen 	if (buffer->attrs) {
1488e2b4d7acSAlexandru Ardelean 		while (buffer->attrs[buffer_attrcount] != NULL)
1489e2b4d7acSAlexandru Ardelean 			buffer_attrcount++;
149008e7e0adSLars-Peter Clausen 	}
149108e7e0adSLars-Peter Clausen 
1492e2b4d7acSAlexandru Ardelean 	scan_el_attrcount = 0;
149315097c7aSAlexandru Ardelean 	INIT_LIST_HEAD(&buffer->buffer_attr_list);
1494d967cb6bSLars-Peter Clausen 	channels = indio_dev->channels;
1495d967cb6bSLars-Peter Clausen 	if (channels) {
1496d967cb6bSLars-Peter Clausen 		/* new magic */
1497d967cb6bSLars-Peter Clausen 		for (i = 0; i < indio_dev->num_channels; i++) {
1498d967cb6bSLars-Peter Clausen 			if (channels[i].scan_index < 0)
1499d967cb6bSLars-Peter Clausen 				continue;
1500d967cb6bSLars-Peter Clausen 
1501ff3f7e04SAlexandru Ardelean 			ret = iio_buffer_add_channel_sysfs(indio_dev, buffer,
1502d967cb6bSLars-Peter Clausen 							 &channels[i]);
1503d967cb6bSLars-Peter Clausen 			if (ret < 0)
1504d967cb6bSLars-Peter Clausen 				goto error_cleanup_dynamic;
1505e2b4d7acSAlexandru Ardelean 			scan_el_attrcount += ret;
1506d967cb6bSLars-Peter Clausen 			if (channels[i].type == IIO_TIMESTAMP)
150762f4f36cSJonathan Cameron 				iio_dev_opaque->scan_index_timestamp =
1508d967cb6bSLars-Peter Clausen 					channels[i].scan_index;
1509d967cb6bSLars-Peter Clausen 		}
1510d967cb6bSLars-Peter Clausen 		if (indio_dev->masklength && buffer->scan_mask == NULL) {
15113862828aSAndy Shevchenko 			buffer->scan_mask = bitmap_zalloc(indio_dev->masklength,
1512d967cb6bSLars-Peter Clausen 							  GFP_KERNEL);
1513d967cb6bSLars-Peter Clausen 			if (buffer->scan_mask == NULL) {
1514d967cb6bSLars-Peter Clausen 				ret = -ENOMEM;
1515d967cb6bSLars-Peter Clausen 				goto error_cleanup_dynamic;
1516d967cb6bSLars-Peter Clausen 			}
1517d967cb6bSLars-Peter Clausen 		}
1518d967cb6bSLars-Peter Clausen 	}
1519d967cb6bSLars-Peter Clausen 
1520d9a62574SAlexandru Ardelean 	attrn = buffer_attrcount + scan_el_attrcount + ARRAY_SIZE(iio_buffer_attrs);
1521d9a62574SAlexandru Ardelean 	attr = kcalloc(attrn + 1, sizeof(* attr), GFP_KERNEL);
1522e2b4d7acSAlexandru Ardelean 	if (!attr) {
1523e2b4d7acSAlexandru Ardelean 		ret = -ENOMEM;
1524e2b4d7acSAlexandru Ardelean 		goto error_free_scan_mask;
1525e2b4d7acSAlexandru Ardelean 	}
1526e2b4d7acSAlexandru Ardelean 
1527e2b4d7acSAlexandru Ardelean 	memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
1528e2b4d7acSAlexandru Ardelean 	if (!buffer->access->set_length)
1529e2b4d7acSAlexandru Ardelean 		attr[0] = &dev_attr_length_ro.attr;
1530e2b4d7acSAlexandru Ardelean 
1531e2b4d7acSAlexandru Ardelean 	if (buffer->access->flags & INDIO_BUFFER_FLAG_FIXED_WATERMARK)
1532e2b4d7acSAlexandru Ardelean 		attr[2] = &dev_attr_watermark_ro.attr;
1533e2b4d7acSAlexandru Ardelean 
1534e2b4d7acSAlexandru Ardelean 	if (buffer->attrs)
1535e2b4d7acSAlexandru Ardelean 		memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs,
1536e2b4d7acSAlexandru Ardelean 		       sizeof(struct attribute *) * buffer_attrcount);
1537e2b4d7acSAlexandru Ardelean 
1538e2b4d7acSAlexandru Ardelean 	buffer_attrcount += ARRAY_SIZE(iio_buffer_attrs);
15399a2ff800SYang Yingliang 	buffer->buffer_group.attrs = attr;
1540e2b4d7acSAlexandru Ardelean 
154115097c7aSAlexandru Ardelean 	for (i = 0; i < buffer_attrcount; i++) {
154215097c7aSAlexandru Ardelean 		struct attribute *wrapped;
1543d9a62574SAlexandru Ardelean 
154415097c7aSAlexandru Ardelean 		wrapped = iio_buffer_wrap_attr(buffer, attr[i]);
154515097c7aSAlexandru Ardelean 		if (!wrapped) {
154615097c7aSAlexandru Ardelean 			ret = -ENOMEM;
15479a2ff800SYang Yingliang 			goto error_free_buffer_attrs;
154815097c7aSAlexandru Ardelean 		}
154915097c7aSAlexandru Ardelean 		attr[i] = wrapped;
155015097c7aSAlexandru Ardelean 	}
155115097c7aSAlexandru Ardelean 
155215097c7aSAlexandru Ardelean 	attrn = 0;
155315097c7aSAlexandru Ardelean 	list_for_each_entry(p, &buffer->buffer_attr_list, l)
1554d9a62574SAlexandru Ardelean 		attr[attrn++] = &p->dev_attr.attr;
1555d9a62574SAlexandru Ardelean 
1556d9a62574SAlexandru Ardelean 	buffer->buffer_group.name = kasprintf(GFP_KERNEL, "buffer%d", index);
1557d9a62574SAlexandru Ardelean 	if (!buffer->buffer_group.name) {
1558d9a62574SAlexandru Ardelean 		ret = -ENOMEM;
1559d9a62574SAlexandru Ardelean 		goto error_free_buffer_attrs;
1560d9a62574SAlexandru Ardelean 	}
1561d9a62574SAlexandru Ardelean 
1562e2b4d7acSAlexandru Ardelean 	ret = iio_device_register_sysfs_group(indio_dev, &buffer->buffer_group);
1563e2b4d7acSAlexandru Ardelean 	if (ret)
1564d9a62574SAlexandru Ardelean 		goto error_free_buffer_attr_group_name;
1565e2b4d7acSAlexandru Ardelean 
1566d9a62574SAlexandru Ardelean 	/* we only need to register the legacy groups for the first buffer */
1567d9a62574SAlexandru Ardelean 	if (index > 0)
1568d9a62574SAlexandru Ardelean 		return 0;
1569d967cb6bSLars-Peter Clausen 
1570d9a62574SAlexandru Ardelean 	ret = iio_buffer_register_legacy_sysfs_groups(indio_dev, attr,
1571d9a62574SAlexandru Ardelean 						      buffer_attrcount,
1572d9a62574SAlexandru Ardelean 						      scan_el_attrcount);
157332f17172SAlexandru Ardelean 	if (ret)
1574d9a62574SAlexandru Ardelean 		goto error_free_buffer_attr_group_name;
1575d967cb6bSLars-Peter Clausen 
1576d967cb6bSLars-Peter Clausen 	return 0;
1577d967cb6bSLars-Peter Clausen 
1578d9a62574SAlexandru Ardelean error_free_buffer_attr_group_name:
1579d9a62574SAlexandru Ardelean 	kfree(buffer->buffer_group.name);
1580e2b4d7acSAlexandru Ardelean error_free_buffer_attrs:
1581e2b4d7acSAlexandru Ardelean 	kfree(buffer->buffer_group.attrs);
1582d967cb6bSLars-Peter Clausen error_free_scan_mask:
15833862828aSAndy Shevchenko 	bitmap_free(buffer->scan_mask);
1584d967cb6bSLars-Peter Clausen error_cleanup_dynamic:
158515097c7aSAlexandru Ardelean 	iio_free_chan_devattr_list(&buffer->buffer_attr_list);
1586d967cb6bSLars-Peter Clausen 
1587d967cb6bSLars-Peter Clausen 	return ret;
1588d967cb6bSLars-Peter Clausen }
1589d967cb6bSLars-Peter Clausen 
15900224af85SAlexandru Ardelean static void __iio_buffer_free_sysfs_and_mask(struct iio_buffer *buffer)
15910224af85SAlexandru Ardelean {
15920224af85SAlexandru Ardelean 	bitmap_free(buffer->scan_mask);
15930224af85SAlexandru Ardelean 	kfree(buffer->buffer_group.name);
15940224af85SAlexandru Ardelean 	kfree(buffer->buffer_group.attrs);
15950224af85SAlexandru Ardelean 	iio_free_chan_devattr_list(&buffer->buffer_attr_list);
15960224af85SAlexandru Ardelean }
15970224af85SAlexandru Ardelean 
1598ee708e6bSAlexandru Ardelean int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
1599e16e0a77SAlexandru Ardelean {
1600ee708e6bSAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1601e16e0a77SAlexandru Ardelean 	const struct iio_chan_spec *channels;
1602ee708e6bSAlexandru Ardelean 	struct iio_buffer *buffer;
1603ee708e6bSAlexandru Ardelean 	int unwind_idx;
1604ee708e6bSAlexandru Ardelean 	int ret, i;
1605f73f7f4dSAlexandru Ardelean 	size_t sz;
1606e16e0a77SAlexandru Ardelean 
1607e16e0a77SAlexandru Ardelean 	channels = indio_dev->channels;
1608e16e0a77SAlexandru Ardelean 	if (channels) {
1609e16e0a77SAlexandru Ardelean 		int ml = indio_dev->masklength;
1610e16e0a77SAlexandru Ardelean 
1611e16e0a77SAlexandru Ardelean 		for (i = 0; i < indio_dev->num_channels; i++)
1612e16e0a77SAlexandru Ardelean 			ml = max(ml, channels[i].scan_index + 1);
1613e16e0a77SAlexandru Ardelean 		indio_dev->masklength = ml;
1614e16e0a77SAlexandru Ardelean 	}
1615e16e0a77SAlexandru Ardelean 
1616ee708e6bSAlexandru Ardelean 	if (!iio_dev_opaque->attached_buffers_cnt)
1617e16e0a77SAlexandru Ardelean 		return 0;
1618e16e0a77SAlexandru Ardelean 
1619ee708e6bSAlexandru Ardelean 	for (i = 0; i < iio_dev_opaque->attached_buffers_cnt; i++) {
1620ee708e6bSAlexandru Ardelean 		buffer = iio_dev_opaque->attached_buffers[i];
1621ee708e6bSAlexandru Ardelean 		ret = __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev, i);
1622ee708e6bSAlexandru Ardelean 		if (ret) {
162309776d93SYang Yingliang 			unwind_idx = i - 1;
1624ee708e6bSAlexandru Ardelean 			goto error_unwind_sysfs_and_mask;
1625ee708e6bSAlexandru Ardelean 		}
1626e16e0a77SAlexandru Ardelean 	}
1627f73f7f4dSAlexandru Ardelean 	unwind_idx = iio_dev_opaque->attached_buffers_cnt - 1;
1628f73f7f4dSAlexandru Ardelean 
1629f73f7f4dSAlexandru Ardelean 	sz = sizeof(*(iio_dev_opaque->buffer_ioctl_handler));
1630f73f7f4dSAlexandru Ardelean 	iio_dev_opaque->buffer_ioctl_handler = kzalloc(sz, GFP_KERNEL);
1631f73f7f4dSAlexandru Ardelean 	if (!iio_dev_opaque->buffer_ioctl_handler) {
1632f73f7f4dSAlexandru Ardelean 		ret = -ENOMEM;
1633f73f7f4dSAlexandru Ardelean 		goto error_unwind_sysfs_and_mask;
1634f73f7f4dSAlexandru Ardelean 	}
1635f73f7f4dSAlexandru Ardelean 
1636f73f7f4dSAlexandru Ardelean 	iio_dev_opaque->buffer_ioctl_handler->ioctl = iio_device_buffer_ioctl;
1637f73f7f4dSAlexandru Ardelean 	iio_device_ioctl_handler_register(indio_dev,
1638f73f7f4dSAlexandru Ardelean 					  iio_dev_opaque->buffer_ioctl_handler);
1639e16e0a77SAlexandru Ardelean 
1640ee708e6bSAlexandru Ardelean 	return 0;
1641ff3f7e04SAlexandru Ardelean 
1642ee708e6bSAlexandru Ardelean error_unwind_sysfs_and_mask:
1643ee708e6bSAlexandru Ardelean 	for (; unwind_idx >= 0; unwind_idx--) {
1644ee708e6bSAlexandru Ardelean 		buffer = iio_dev_opaque->attached_buffers[unwind_idx];
1645ee708e6bSAlexandru Ardelean 		__iio_buffer_free_sysfs_and_mask(buffer);
1646ee708e6bSAlexandru Ardelean 	}
1647ee708e6bSAlexandru Ardelean 	return ret;
1648ee708e6bSAlexandru Ardelean }
1649ee708e6bSAlexandru Ardelean 
1650ee708e6bSAlexandru Ardelean void iio_buffers_free_sysfs_and_mask(struct iio_dev *indio_dev)
1651ee708e6bSAlexandru Ardelean {
1652ee708e6bSAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1653ee708e6bSAlexandru Ardelean 	struct iio_buffer *buffer;
1654ee708e6bSAlexandru Ardelean 	int i;
1655ee708e6bSAlexandru Ardelean 
1656ee708e6bSAlexandru Ardelean 	if (!iio_dev_opaque->attached_buffers_cnt)
1657d967cb6bSLars-Peter Clausen 		return;
1658d967cb6bSLars-Peter Clausen 
1659f73f7f4dSAlexandru Ardelean 	iio_device_ioctl_handler_unregister(iio_dev_opaque->buffer_ioctl_handler);
1660f73f7f4dSAlexandru Ardelean 	kfree(iio_dev_opaque->buffer_ioctl_handler);
1661f73f7f4dSAlexandru Ardelean 
1662d9a62574SAlexandru Ardelean 	iio_buffer_unregister_legacy_sysfs_groups(indio_dev);
1663d9a62574SAlexandru Ardelean 
1664ee708e6bSAlexandru Ardelean 	for (i = iio_dev_opaque->attached_buffers_cnt - 1; i >= 0; i--) {
1665ee708e6bSAlexandru Ardelean 		buffer = iio_dev_opaque->attached_buffers[i];
1666e16e0a77SAlexandru Ardelean 		__iio_buffer_free_sysfs_and_mask(buffer);
1667d967cb6bSLars-Peter Clausen 	}
1668ee708e6bSAlexandru Ardelean }
1669ee708e6bSAlexandru Ardelean 
1670a980e046SJonathan Cameron /**
167181636632SLars-Peter Clausen  * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
167281636632SLars-Peter Clausen  * @indio_dev: the iio device
167381636632SLars-Peter Clausen  * @mask: scan mask to be checked
167481636632SLars-Peter Clausen  *
167581636632SLars-Peter Clausen  * Return true if exactly one bit is set in the scan mask, false otherwise. It
167681636632SLars-Peter Clausen  * can be used for devices where only one channel can be active for sampling at
167781636632SLars-Peter Clausen  * a time.
167881636632SLars-Peter Clausen  */
167981636632SLars-Peter Clausen bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
168081636632SLars-Peter Clausen 	const unsigned long *mask)
168181636632SLars-Peter Clausen {
168281636632SLars-Peter Clausen 	return bitmap_weight(mask, indio_dev->masklength) == 1;
168381636632SLars-Peter Clausen }
168481636632SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
168581636632SLars-Peter Clausen 
16865d65d920SLars-Peter Clausen static const void *iio_demux(struct iio_buffer *buffer,
16875d65d920SLars-Peter Clausen 				 const void *datain)
1688a980e046SJonathan Cameron {
1689a980e046SJonathan Cameron 	struct iio_demux_table *t;
1690a980e046SJonathan Cameron 
1691a980e046SJonathan Cameron 	if (list_empty(&buffer->demux_list))
1692a980e046SJonathan Cameron 		return datain;
1693a980e046SJonathan Cameron 	list_for_each_entry(t, &buffer->demux_list, l)
1694a980e046SJonathan Cameron 		memcpy(buffer->demux_bounce + t->to,
1695a980e046SJonathan Cameron 		       datain + t->from, t->length);
1696a980e046SJonathan Cameron 
1697a980e046SJonathan Cameron 	return buffer->demux_bounce;
1698a980e046SJonathan Cameron }
1699a980e046SJonathan Cameron 
17005d65d920SLars-Peter Clausen static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
1701a980e046SJonathan Cameron {
17025d65d920SLars-Peter Clausen 	const void *dataout = iio_demux(buffer, data);
170337d34556SJosselin Costanzi 	int ret;
1704a980e046SJonathan Cameron 
170537d34556SJosselin Costanzi 	ret = buffer->access->store_to(buffer, dataout);
170637d34556SJosselin Costanzi 	if (ret)
170737d34556SJosselin Costanzi 		return ret;
170837d34556SJosselin Costanzi 
170937d34556SJosselin Costanzi 	/*
171037d34556SJosselin Costanzi 	 * We can't just test for watermark to decide if we wake the poll queue
171137d34556SJosselin Costanzi 	 * because read may request less samples than the watermark.
171237d34556SJosselin Costanzi 	 */
1713a9a08845SLinus Torvalds 	wake_up_interruptible_poll(&buffer->pollq, EPOLLIN | EPOLLRDNORM);
171437d34556SJosselin Costanzi 	return 0;
1715a980e046SJonathan Cameron }
1716a980e046SJonathan Cameron 
1717315a19ecSJonathan Cameron /**
1718315a19ecSJonathan Cameron  * iio_push_to_buffers() - push to a registered buffer.
1719315a19ecSJonathan Cameron  * @indio_dev:		iio_dev structure for device.
1720315a19ecSJonathan Cameron  * @data:		Full scan.
1721315a19ecSJonathan Cameron  */
17225d65d920SLars-Peter Clausen int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
172384b36ce5SJonathan Cameron {
17246a8c6b26SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
172584b36ce5SJonathan Cameron 	int ret;
172684b36ce5SJonathan Cameron 	struct iio_buffer *buf;
172784b36ce5SJonathan Cameron 
17286a8c6b26SAlexandru Ardelean 	list_for_each_entry(buf, &iio_dev_opaque->buffer_list, buffer_list) {
172984b36ce5SJonathan Cameron 		ret = iio_push_to_buffer(buf, data);
173084b36ce5SJonathan Cameron 		if (ret < 0)
173184b36ce5SJonathan Cameron 			return ret;
173284b36ce5SJonathan Cameron 	}
173384b36ce5SJonathan Cameron 
173484b36ce5SJonathan Cameron 	return 0;
173584b36ce5SJonathan Cameron }
173684b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_push_to_buffers);
173784b36ce5SJonathan Cameron 
17389e69c935SLars-Peter Clausen /**
17399e69c935SLars-Peter Clausen  * iio_buffer_release() - Free a buffer's resources
17409e69c935SLars-Peter Clausen  * @ref: Pointer to the kref embedded in the iio_buffer struct
17419e69c935SLars-Peter Clausen  *
17429e69c935SLars-Peter Clausen  * This function is called when the last reference to the buffer has been
17439e69c935SLars-Peter Clausen  * dropped. It will typically free all resources allocated by the buffer. Do not
17449e69c935SLars-Peter Clausen  * call this function manually, always use iio_buffer_put() when done using a
17459e69c935SLars-Peter Clausen  * buffer.
17469e69c935SLars-Peter Clausen  */
17479e69c935SLars-Peter Clausen static void iio_buffer_release(struct kref *ref)
17489e69c935SLars-Peter Clausen {
17499e69c935SLars-Peter Clausen 	struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
17509e69c935SLars-Peter Clausen 
17519e69c935SLars-Peter Clausen 	buffer->access->release(buffer);
17529e69c935SLars-Peter Clausen }
17539e69c935SLars-Peter Clausen 
17549e69c935SLars-Peter Clausen /**
17559e69c935SLars-Peter Clausen  * iio_buffer_get() - Grab a reference to the buffer
17569e69c935SLars-Peter Clausen  * @buffer: The buffer to grab a reference for, may be NULL
17579e69c935SLars-Peter Clausen  *
17589e69c935SLars-Peter Clausen  * Returns the pointer to the buffer that was passed into the function.
17599e69c935SLars-Peter Clausen  */
17609e69c935SLars-Peter Clausen struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
17619e69c935SLars-Peter Clausen {
17629e69c935SLars-Peter Clausen 	if (buffer)
17639e69c935SLars-Peter Clausen 		kref_get(&buffer->ref);
17649e69c935SLars-Peter Clausen 
17659e69c935SLars-Peter Clausen 	return buffer;
17669e69c935SLars-Peter Clausen }
17679e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_get);
17689e69c935SLars-Peter Clausen 
17699e69c935SLars-Peter Clausen /**
17709e69c935SLars-Peter Clausen  * iio_buffer_put() - Release the reference to the buffer
17719e69c935SLars-Peter Clausen  * @buffer: The buffer to release the reference for, may be NULL
17729e69c935SLars-Peter Clausen  */
17739e69c935SLars-Peter Clausen void iio_buffer_put(struct iio_buffer *buffer)
17749e69c935SLars-Peter Clausen {
17759e69c935SLars-Peter Clausen 	if (buffer)
17769e69c935SLars-Peter Clausen 		kref_put(&buffer->ref, iio_buffer_release);
17779e69c935SLars-Peter Clausen }
17789e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_put);
17792b827ad5SJonathan Cameron 
17802b827ad5SJonathan Cameron /**
17812b827ad5SJonathan Cameron  * iio_device_attach_buffer - Attach a buffer to a IIO device
17822b827ad5SJonathan Cameron  * @indio_dev: The device the buffer should be attached to
17832b827ad5SJonathan Cameron  * @buffer: The buffer to attach to the device
17842b827ad5SJonathan Cameron  *
1785ee708e6bSAlexandru Ardelean  * Return 0 if successful, negative if error.
1786ee708e6bSAlexandru Ardelean  *
17872b827ad5SJonathan Cameron  * This function attaches a buffer to a IIO device. The buffer stays attached to
1788ee708e6bSAlexandru Ardelean  * the device until the device is freed. For legacy reasons, the first attached
1789ee708e6bSAlexandru Ardelean  * buffer will also be assigned to 'indio_dev->buffer'.
1790218bc53dSAlexandru Ardelean  * The array allocated here, will be free'd via the iio_device_detach_buffers()
1791218bc53dSAlexandru Ardelean  * call which is handled by the iio_device_free().
17922b827ad5SJonathan Cameron  */
1793ee708e6bSAlexandru Ardelean int iio_device_attach_buffer(struct iio_dev *indio_dev,
17942b827ad5SJonathan Cameron 			     struct iio_buffer *buffer)
17952b827ad5SJonathan Cameron {
1796ee708e6bSAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1797ee708e6bSAlexandru Ardelean 	struct iio_buffer **new, **old = iio_dev_opaque->attached_buffers;
1798ee708e6bSAlexandru Ardelean 	unsigned int cnt = iio_dev_opaque->attached_buffers_cnt;
1799ee708e6bSAlexandru Ardelean 
1800ee708e6bSAlexandru Ardelean 	cnt++;
1801ee708e6bSAlexandru Ardelean 
1802ee708e6bSAlexandru Ardelean 	new = krealloc(old, sizeof(*new) * cnt, GFP_KERNEL);
1803ee708e6bSAlexandru Ardelean 	if (!new)
1804ee708e6bSAlexandru Ardelean 		return -ENOMEM;
1805ee708e6bSAlexandru Ardelean 	iio_dev_opaque->attached_buffers = new;
1806ee708e6bSAlexandru Ardelean 
1807ee708e6bSAlexandru Ardelean 	buffer = iio_buffer_get(buffer);
1808ee708e6bSAlexandru Ardelean 
1809ee708e6bSAlexandru Ardelean 	/* first buffer is legacy; attach it to the IIO device directly */
1810ee708e6bSAlexandru Ardelean 	if (!indio_dev->buffer)
1811ee708e6bSAlexandru Ardelean 		indio_dev->buffer = buffer;
1812ee708e6bSAlexandru Ardelean 
1813ee708e6bSAlexandru Ardelean 	iio_dev_opaque->attached_buffers[cnt - 1] = buffer;
1814ee708e6bSAlexandru Ardelean 	iio_dev_opaque->attached_buffers_cnt = cnt;
1815ee708e6bSAlexandru Ardelean 
1816ee708e6bSAlexandru Ardelean 	return 0;
18172b827ad5SJonathan Cameron }
18182b827ad5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_device_attach_buffer);
1819