xref: /openbmc/linux/drivers/iio/industrialio-buffer.c (revision 62f4f36cdfcdbb961bbbeab15e6595dd391d2205)
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 
357ccd428e4SAlexandru Ardelean 	trialmask = bitmap_zalloc(indio_dev->masklength, GFP_KERNEL);
358217a5cf0SLars-Peter Clausen 	if (trialmask == NULL)
359217a5cf0SLars-Peter Clausen 		return -ENOMEM;
360217a5cf0SLars-Peter Clausen 	if (!indio_dev->masklength) {
361231bfe53SDan Carpenter 		WARN(1, "Trying to set scanmask prior to registering buffer\n");
362217a5cf0SLars-Peter Clausen 		goto err_invalid_mask;
363217a5cf0SLars-Peter Clausen 	}
364217a5cf0SLars-Peter Clausen 	bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
365217a5cf0SLars-Peter Clausen 	set_bit(bit, trialmask);
366217a5cf0SLars-Peter Clausen 
367217a5cf0SLars-Peter Clausen 	if (!iio_validate_scan_mask(indio_dev, trialmask))
368217a5cf0SLars-Peter Clausen 		goto err_invalid_mask;
369217a5cf0SLars-Peter Clausen 
370217a5cf0SLars-Peter Clausen 	if (indio_dev->available_scan_masks) {
371217a5cf0SLars-Peter Clausen 		mask = iio_scan_mask_match(indio_dev->available_scan_masks,
372217a5cf0SLars-Peter Clausen 					   indio_dev->masklength,
3731e1ec286SLars-Peter Clausen 					   trialmask, false);
374217a5cf0SLars-Peter Clausen 		if (!mask)
375217a5cf0SLars-Peter Clausen 			goto err_invalid_mask;
376217a5cf0SLars-Peter Clausen 	}
377217a5cf0SLars-Peter Clausen 	bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
378217a5cf0SLars-Peter Clausen 
3793862828aSAndy Shevchenko 	bitmap_free(trialmask);
380217a5cf0SLars-Peter Clausen 
381217a5cf0SLars-Peter Clausen 	return 0;
382217a5cf0SLars-Peter Clausen 
383217a5cf0SLars-Peter Clausen err_invalid_mask:
3843862828aSAndy Shevchenko 	bitmap_free(trialmask);
385217a5cf0SLars-Peter Clausen 	return -EINVAL;
386217a5cf0SLars-Peter Clausen }
387217a5cf0SLars-Peter Clausen 
388a980e046SJonathan Cameron static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
389a980e046SJonathan Cameron {
390a980e046SJonathan Cameron 	clear_bit(bit, buffer->scan_mask);
391a980e046SJonathan Cameron 	return 0;
392a980e046SJonathan Cameron }
393a980e046SJonathan Cameron 
394c2bf8d5fSJonathan Cameron static int iio_scan_mask_query(struct iio_dev *indio_dev,
395c2bf8d5fSJonathan Cameron 			       struct iio_buffer *buffer, int bit)
396c2bf8d5fSJonathan Cameron {
397c2bf8d5fSJonathan Cameron 	if (bit > indio_dev->masklength)
398c2bf8d5fSJonathan Cameron 		return -EINVAL;
399c2bf8d5fSJonathan Cameron 
400c2bf8d5fSJonathan Cameron 	if (!buffer->scan_mask)
401c2bf8d5fSJonathan Cameron 		return 0;
402c2bf8d5fSJonathan Cameron 
403c2bf8d5fSJonathan Cameron 	/* Ensure return value is 0 or 1. */
404c2bf8d5fSJonathan Cameron 	return !!test_bit(bit, buffer->scan_mask);
405c2bf8d5fSJonathan Cameron };
406c2bf8d5fSJonathan Cameron 
407a980e046SJonathan Cameron static ssize_t iio_scan_el_store(struct device *dev,
408a980e046SJonathan Cameron 				 struct device_attribute *attr,
409a980e046SJonathan Cameron 				 const char *buf,
410a980e046SJonathan Cameron 				 size_t len)
411a980e046SJonathan Cameron {
412a980e046SJonathan Cameron 	int ret;
413a980e046SJonathan Cameron 	bool state;
414e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
415a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
41615097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = this_attr->buffer;
417a980e046SJonathan Cameron 
418a980e046SJonathan Cameron 	ret = strtobool(buf, &state);
419a980e046SJonathan Cameron 	if (ret < 0)
420a980e046SJonathan Cameron 		return ret;
421a980e046SJonathan Cameron 	mutex_lock(&indio_dev->mlock);
422ff3f7e04SAlexandru Ardelean 	if (iio_buffer_is_active(buffer)) {
423a980e046SJonathan Cameron 		ret = -EBUSY;
424a980e046SJonathan Cameron 		goto error_ret;
425a980e046SJonathan Cameron 	}
426a980e046SJonathan Cameron 	ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
427a980e046SJonathan Cameron 	if (ret < 0)
428a980e046SJonathan Cameron 		goto error_ret;
429a980e046SJonathan Cameron 	if (!state && ret) {
430a980e046SJonathan Cameron 		ret = iio_scan_mask_clear(buffer, this_attr->address);
431a980e046SJonathan Cameron 		if (ret)
432a980e046SJonathan Cameron 			goto error_ret;
433a980e046SJonathan Cameron 	} else if (state && !ret) {
434a980e046SJonathan Cameron 		ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
435a980e046SJonathan Cameron 		if (ret)
436a980e046SJonathan Cameron 			goto error_ret;
437a980e046SJonathan Cameron 	}
438a980e046SJonathan Cameron 
439a980e046SJonathan Cameron error_ret:
440a980e046SJonathan Cameron 	mutex_unlock(&indio_dev->mlock);
441a980e046SJonathan Cameron 
442a980e046SJonathan Cameron 	return ret < 0 ? ret : len;
443a980e046SJonathan Cameron 
444a980e046SJonathan Cameron }
445a980e046SJonathan Cameron 
446a980e046SJonathan Cameron static ssize_t iio_scan_el_ts_show(struct device *dev,
447a980e046SJonathan Cameron 				   struct device_attribute *attr,
448a980e046SJonathan Cameron 				   char *buf)
449a980e046SJonathan Cameron {
45015097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
451ff3f7e04SAlexandru Ardelean 
45283ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%d\n", buffer->scan_timestamp);
453a980e046SJonathan Cameron }
454a980e046SJonathan Cameron 
455a980e046SJonathan Cameron static ssize_t iio_scan_el_ts_store(struct device *dev,
456a980e046SJonathan Cameron 				    struct device_attribute *attr,
457a980e046SJonathan Cameron 				    const char *buf,
458a980e046SJonathan Cameron 				    size_t len)
459a980e046SJonathan Cameron {
460a980e046SJonathan Cameron 	int ret;
461e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
46215097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
463a980e046SJonathan Cameron 	bool state;
464a980e046SJonathan Cameron 
465a980e046SJonathan Cameron 	ret = strtobool(buf, &state);
466a980e046SJonathan Cameron 	if (ret < 0)
467a980e046SJonathan Cameron 		return ret;
468a980e046SJonathan Cameron 
469a980e046SJonathan Cameron 	mutex_lock(&indio_dev->mlock);
470ff3f7e04SAlexandru Ardelean 	if (iio_buffer_is_active(buffer)) {
471a980e046SJonathan Cameron 		ret = -EBUSY;
472a980e046SJonathan Cameron 		goto error_ret;
473a980e046SJonathan Cameron 	}
474ff3f7e04SAlexandru Ardelean 	buffer->scan_timestamp = state;
475a980e046SJonathan Cameron error_ret:
476a980e046SJonathan Cameron 	mutex_unlock(&indio_dev->mlock);
477a980e046SJonathan Cameron 
478a980e046SJonathan Cameron 	return ret ? ret : len;
479a980e046SJonathan Cameron }
480a980e046SJonathan Cameron 
481a980e046SJonathan Cameron static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
482ff3f7e04SAlexandru Ardelean 					struct iio_buffer *buffer,
483a980e046SJonathan Cameron 					const struct iio_chan_spec *chan)
484a980e046SJonathan Cameron {
485a980e046SJonathan Cameron 	int ret, attrcount = 0;
486a980e046SJonathan Cameron 
487a980e046SJonathan Cameron 	ret = __iio_add_chan_devattr("index",
488a980e046SJonathan Cameron 				     chan,
489a980e046SJonathan Cameron 				     &iio_show_scan_index,
490a980e046SJonathan Cameron 				     NULL,
491a980e046SJonathan Cameron 				     0,
4923704432fSJonathan Cameron 				     IIO_SEPARATE,
493a980e046SJonathan Cameron 				     &indio_dev->dev,
4943e3d11b2SAlexandru Ardelean 				     buffer,
49515097c7aSAlexandru Ardelean 				     &buffer->buffer_attr_list);
496a980e046SJonathan Cameron 	if (ret)
49792825ff9SHartmut Knaack 		return ret;
498a980e046SJonathan Cameron 	attrcount++;
499a980e046SJonathan Cameron 	ret = __iio_add_chan_devattr("type",
500a980e046SJonathan Cameron 				     chan,
501a980e046SJonathan Cameron 				     &iio_show_fixed_type,
502a980e046SJonathan Cameron 				     NULL,
503a980e046SJonathan Cameron 				     0,
504a980e046SJonathan Cameron 				     0,
505a980e046SJonathan Cameron 				     &indio_dev->dev,
5063e3d11b2SAlexandru Ardelean 				     buffer,
50715097c7aSAlexandru Ardelean 				     &buffer->buffer_attr_list);
508a980e046SJonathan Cameron 	if (ret)
50992825ff9SHartmut Knaack 		return ret;
510a980e046SJonathan Cameron 	attrcount++;
511a980e046SJonathan Cameron 	if (chan->type != IIO_TIMESTAMP)
512a980e046SJonathan Cameron 		ret = __iio_add_chan_devattr("en",
513a980e046SJonathan Cameron 					     chan,
514a980e046SJonathan Cameron 					     &iio_scan_el_show,
515a980e046SJonathan Cameron 					     &iio_scan_el_store,
516a980e046SJonathan Cameron 					     chan->scan_index,
517a980e046SJonathan Cameron 					     0,
518a980e046SJonathan Cameron 					     &indio_dev->dev,
5193e3d11b2SAlexandru Ardelean 					     buffer,
52015097c7aSAlexandru Ardelean 					     &buffer->buffer_attr_list);
521a980e046SJonathan Cameron 	else
522a980e046SJonathan Cameron 		ret = __iio_add_chan_devattr("en",
523a980e046SJonathan Cameron 					     chan,
524a980e046SJonathan Cameron 					     &iio_scan_el_ts_show,
525a980e046SJonathan Cameron 					     &iio_scan_el_ts_store,
526a980e046SJonathan Cameron 					     chan->scan_index,
527a980e046SJonathan Cameron 					     0,
528a980e046SJonathan Cameron 					     &indio_dev->dev,
5293e3d11b2SAlexandru Ardelean 					     buffer,
53015097c7aSAlexandru Ardelean 					     &buffer->buffer_attr_list);
5319572588cSPeter Meerwald 	if (ret)
53292825ff9SHartmut Knaack 		return ret;
533a980e046SJonathan Cameron 	attrcount++;
534a980e046SJonathan Cameron 	ret = attrcount;
535a980e046SJonathan Cameron 	return ret;
536a980e046SJonathan Cameron }
537a980e046SJonathan Cameron 
53808e7e0adSLars-Peter Clausen static ssize_t iio_buffer_read_length(struct device *dev,
539a980e046SJonathan Cameron 				      struct device_attribute *attr,
540a980e046SJonathan Cameron 				      char *buf)
541a980e046SJonathan Cameron {
54215097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
543a980e046SJonathan Cameron 
54483ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%d\n", buffer->length);
545a980e046SJonathan Cameron }
546a980e046SJonathan Cameron 
54708e7e0adSLars-Peter Clausen static ssize_t iio_buffer_write_length(struct device *dev,
548a980e046SJonathan Cameron 				       struct device_attribute *attr,
54908e7e0adSLars-Peter Clausen 				       const char *buf, size_t len)
550a980e046SJonathan Cameron {
551e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
55215097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
553948ad205SLars-Peter Clausen 	unsigned int val;
554948ad205SLars-Peter Clausen 	int ret;
555a980e046SJonathan Cameron 
556948ad205SLars-Peter Clausen 	ret = kstrtouint(buf, 10, &val);
557a980e046SJonathan Cameron 	if (ret)
558a980e046SJonathan Cameron 		return ret;
559a980e046SJonathan Cameron 
56037495660SLars-Peter Clausen 	if (val == buffer->length)
561a980e046SJonathan Cameron 		return len;
562a980e046SJonathan Cameron 
563a980e046SJonathan Cameron 	mutex_lock(&indio_dev->mlock);
564ff3f7e04SAlexandru Ardelean 	if (iio_buffer_is_active(buffer)) {
565a980e046SJonathan Cameron 		ret = -EBUSY;
566a980e046SJonathan Cameron 	} else {
567a980e046SJonathan Cameron 		buffer->access->set_length(buffer, val);
568a980e046SJonathan Cameron 		ret = 0;
569a980e046SJonathan Cameron 	}
57037d34556SJosselin Costanzi 	if (ret)
57137d34556SJosselin Costanzi 		goto out;
57237d34556SJosselin Costanzi 	if (buffer->length && buffer->length < buffer->watermark)
57337d34556SJosselin Costanzi 		buffer->watermark = buffer->length;
57437d34556SJosselin Costanzi out:
575a980e046SJonathan Cameron 	mutex_unlock(&indio_dev->mlock);
576a980e046SJonathan Cameron 
577a980e046SJonathan Cameron 	return ret ? ret : len;
578a980e046SJonathan Cameron }
579a980e046SJonathan Cameron 
58008e7e0adSLars-Peter Clausen static ssize_t iio_buffer_show_enable(struct device *dev,
581a980e046SJonathan Cameron 				      struct device_attribute *attr,
582a980e046SJonathan Cameron 				      char *buf)
583a980e046SJonathan Cameron {
58415097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
585ff3f7e04SAlexandru Ardelean 
58683ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%d\n", iio_buffer_is_active(buffer));
587a980e046SJonathan Cameron }
588a980e046SJonathan Cameron 
589182b4905SLars-Peter Clausen static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev,
590182b4905SLars-Peter Clausen 					     unsigned int scan_index)
591182b4905SLars-Peter Clausen {
592182b4905SLars-Peter Clausen 	const struct iio_chan_spec *ch;
593182b4905SLars-Peter Clausen 	unsigned int bytes;
594182b4905SLars-Peter Clausen 
595182b4905SLars-Peter Clausen 	ch = iio_find_channel_from_si(indio_dev, scan_index);
596182b4905SLars-Peter Clausen 	bytes = ch->scan_type.storagebits / 8;
597182b4905SLars-Peter Clausen 	if (ch->scan_type.repeat > 1)
598182b4905SLars-Peter Clausen 		bytes *= ch->scan_type.repeat;
599182b4905SLars-Peter Clausen 	return bytes;
600182b4905SLars-Peter Clausen }
601182b4905SLars-Peter Clausen 
602182b4905SLars-Peter Clausen static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev)
603182b4905SLars-Peter Clausen {
604*62f4f36cSJonathan Cameron 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
605*62f4f36cSJonathan Cameron 
606182b4905SLars-Peter Clausen 	return iio_storage_bytes_for_si(indio_dev,
607*62f4f36cSJonathan Cameron 					iio_dev_opaque->scan_index_timestamp);
608182b4905SLars-Peter Clausen }
609182b4905SLars-Peter Clausen 
610183f4173SPeter Meerwald static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
611183f4173SPeter Meerwald 				const unsigned long *mask, bool timestamp)
612a980e046SJonathan Cameron {
613a980e046SJonathan Cameron 	unsigned bytes = 0;
614883f6165SLars Möllendorf 	int length, i, largest = 0;
615a980e046SJonathan Cameron 
616a980e046SJonathan Cameron 	/* How much space will the demuxed element take? */
617a980e046SJonathan Cameron 	for_each_set_bit(i, mask,
618a980e046SJonathan Cameron 			 indio_dev->masklength) {
619182b4905SLars-Peter Clausen 		length = iio_storage_bytes_for_si(indio_dev, i);
620a980e046SJonathan Cameron 		bytes = ALIGN(bytes, length);
621a980e046SJonathan Cameron 		bytes += length;
622883f6165SLars Möllendorf 		largest = max(largest, length);
623a980e046SJonathan Cameron 	}
624182b4905SLars-Peter Clausen 
625a980e046SJonathan Cameron 	if (timestamp) {
626182b4905SLars-Peter Clausen 		length = iio_storage_bytes_for_timestamp(indio_dev);
627a980e046SJonathan Cameron 		bytes = ALIGN(bytes, length);
628a980e046SJonathan Cameron 		bytes += length;
629883f6165SLars Möllendorf 		largest = max(largest, length);
630a980e046SJonathan Cameron 	}
631883f6165SLars Möllendorf 
632883f6165SLars Möllendorf 	bytes = ALIGN(bytes, largest);
633a980e046SJonathan Cameron 	return bytes;
634a980e046SJonathan Cameron }
635a980e046SJonathan Cameron 
6369e69c935SLars-Peter Clausen static void iio_buffer_activate(struct iio_dev *indio_dev,
6379e69c935SLars-Peter Clausen 	struct iio_buffer *buffer)
6389e69c935SLars-Peter Clausen {
6396a8c6b26SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
6406a8c6b26SAlexandru Ardelean 
6419e69c935SLars-Peter Clausen 	iio_buffer_get(buffer);
6426a8c6b26SAlexandru Ardelean 	list_add(&buffer->buffer_list, &iio_dev_opaque->buffer_list);
6439e69c935SLars-Peter Clausen }
6449e69c935SLars-Peter Clausen 
6459e69c935SLars-Peter Clausen static void iio_buffer_deactivate(struct iio_buffer *buffer)
6469e69c935SLars-Peter Clausen {
6479e69c935SLars-Peter Clausen 	list_del_init(&buffer->buffer_list);
64837d34556SJosselin Costanzi 	wake_up_interruptible(&buffer->pollq);
6499e69c935SLars-Peter Clausen 	iio_buffer_put(buffer);
6509e69c935SLars-Peter Clausen }
6519e69c935SLars-Peter Clausen 
6521250186aSLars-Peter Clausen static void iio_buffer_deactivate_all(struct iio_dev *indio_dev)
6531250186aSLars-Peter Clausen {
6546a8c6b26SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
6551250186aSLars-Peter Clausen 	struct iio_buffer *buffer, *_buffer;
6561250186aSLars-Peter Clausen 
6571250186aSLars-Peter Clausen 	list_for_each_entry_safe(buffer, _buffer,
6586a8c6b26SAlexandru Ardelean 			&iio_dev_opaque->buffer_list, buffer_list)
6591250186aSLars-Peter Clausen 		iio_buffer_deactivate(buffer);
6601250186aSLars-Peter Clausen }
6611250186aSLars-Peter Clausen 
662e18a2ad4SLars-Peter Clausen static int iio_buffer_enable(struct iio_buffer *buffer,
663e18a2ad4SLars-Peter Clausen 	struct iio_dev *indio_dev)
664e18a2ad4SLars-Peter Clausen {
665e18a2ad4SLars-Peter Clausen 	if (!buffer->access->enable)
666e18a2ad4SLars-Peter Clausen 		return 0;
667e18a2ad4SLars-Peter Clausen 	return buffer->access->enable(buffer, indio_dev);
668e18a2ad4SLars-Peter Clausen }
669e18a2ad4SLars-Peter Clausen 
670e18a2ad4SLars-Peter Clausen static int iio_buffer_disable(struct iio_buffer *buffer,
671e18a2ad4SLars-Peter Clausen 	struct iio_dev *indio_dev)
672e18a2ad4SLars-Peter Clausen {
673e18a2ad4SLars-Peter Clausen 	if (!buffer->access->disable)
674e18a2ad4SLars-Peter Clausen 		return 0;
675e18a2ad4SLars-Peter Clausen 	return buffer->access->disable(buffer, indio_dev);
676e18a2ad4SLars-Peter Clausen }
677e18a2ad4SLars-Peter Clausen 
6788e050996SLars-Peter Clausen static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
6798e050996SLars-Peter Clausen 	struct iio_buffer *buffer)
6808e050996SLars-Peter Clausen {
6818e050996SLars-Peter Clausen 	unsigned int bytes;
6828e050996SLars-Peter Clausen 
6838e050996SLars-Peter Clausen 	if (!buffer->access->set_bytes_per_datum)
6848e050996SLars-Peter Clausen 		return;
6858e050996SLars-Peter Clausen 
6868e050996SLars-Peter Clausen 	bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
6878e050996SLars-Peter Clausen 		buffer->scan_timestamp);
6888e050996SLars-Peter Clausen 
6898e050996SLars-Peter Clausen 	buffer->access->set_bytes_per_datum(buffer, bytes);
6908e050996SLars-Peter Clausen }
6918e050996SLars-Peter Clausen 
692fcc1b2f5SLars-Peter Clausen static int iio_buffer_request_update(struct iio_dev *indio_dev,
693fcc1b2f5SLars-Peter Clausen 	struct iio_buffer *buffer)
694fcc1b2f5SLars-Peter Clausen {
695fcc1b2f5SLars-Peter Clausen 	int ret;
696fcc1b2f5SLars-Peter Clausen 
697fcc1b2f5SLars-Peter Clausen 	iio_buffer_update_bytes_per_datum(indio_dev, buffer);
698fcc1b2f5SLars-Peter Clausen 	if (buffer->access->request_update) {
699fcc1b2f5SLars-Peter Clausen 		ret = buffer->access->request_update(buffer);
700fcc1b2f5SLars-Peter Clausen 		if (ret) {
701fcc1b2f5SLars-Peter Clausen 			dev_dbg(&indio_dev->dev,
702fcc1b2f5SLars-Peter Clausen 			       "Buffer not started: buffer parameter update failed (%d)\n",
703fcc1b2f5SLars-Peter Clausen 				ret);
704fcc1b2f5SLars-Peter Clausen 			return ret;
705fcc1b2f5SLars-Peter Clausen 		}
706fcc1b2f5SLars-Peter Clausen 	}
707fcc1b2f5SLars-Peter Clausen 
708fcc1b2f5SLars-Peter Clausen 	return 0;
709fcc1b2f5SLars-Peter Clausen }
710fcc1b2f5SLars-Peter Clausen 
711248be5aaSLars-Peter Clausen static void iio_free_scan_mask(struct iio_dev *indio_dev,
712248be5aaSLars-Peter Clausen 	const unsigned long *mask)
713248be5aaSLars-Peter Clausen {
714248be5aaSLars-Peter Clausen 	/* If the mask is dynamically allocated free it, otherwise do nothing */
715248be5aaSLars-Peter Clausen 	if (!indio_dev->available_scan_masks)
7163862828aSAndy Shevchenko 		bitmap_free(mask);
717248be5aaSLars-Peter Clausen }
718248be5aaSLars-Peter Clausen 
7196e509c4dSLars-Peter Clausen struct iio_device_config {
7206e509c4dSLars-Peter Clausen 	unsigned int mode;
721f0566c0cSLars-Peter Clausen 	unsigned int watermark;
7226e509c4dSLars-Peter Clausen 	const unsigned long *scan_mask;
7236e509c4dSLars-Peter Clausen 	unsigned int scan_bytes;
7246e509c4dSLars-Peter Clausen 	bool scan_timestamp;
7256e509c4dSLars-Peter Clausen };
7266e509c4dSLars-Peter Clausen 
7276e509c4dSLars-Peter Clausen static int iio_verify_update(struct iio_dev *indio_dev,
7286e509c4dSLars-Peter Clausen 	struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer,
7296e509c4dSLars-Peter Clausen 	struct iio_device_config *config)
7306e509c4dSLars-Peter Clausen {
7316a8c6b26SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
7326e509c4dSLars-Peter Clausen 	unsigned long *compound_mask;
7336e509c4dSLars-Peter Clausen 	const unsigned long *scan_mask;
7341e1ec286SLars-Peter Clausen 	bool strict_scanmask = false;
7356e509c4dSLars-Peter Clausen 	struct iio_buffer *buffer;
7366e509c4dSLars-Peter Clausen 	bool scan_timestamp;
737225d59adSLars-Peter Clausen 	unsigned int modes;
7386e509c4dSLars-Peter Clausen 
739b7329249SLars-Peter Clausen 	if (insert_buffer &&
740b7329249SLars-Peter Clausen 	    bitmap_empty(insert_buffer->scan_mask, indio_dev->masklength)) {
741b7329249SLars-Peter Clausen 		dev_dbg(&indio_dev->dev,
742b7329249SLars-Peter Clausen 			"At least one scan element must be enabled first\n");
743b7329249SLars-Peter Clausen 		return -EINVAL;
744b7329249SLars-Peter Clausen 	}
745b7329249SLars-Peter Clausen 
7466e509c4dSLars-Peter Clausen 	memset(config, 0, sizeof(*config));
7471bef2c1dSIrina Tirdea 	config->watermark = ~0;
7486e509c4dSLars-Peter Clausen 
7496e509c4dSLars-Peter Clausen 	/*
7506e509c4dSLars-Peter Clausen 	 * If there is just one buffer and we are removing it there is nothing
7516e509c4dSLars-Peter Clausen 	 * to verify.
7526e509c4dSLars-Peter Clausen 	 */
7536e509c4dSLars-Peter Clausen 	if (remove_buffer && !insert_buffer &&
7546a8c6b26SAlexandru Ardelean 		list_is_singular(&iio_dev_opaque->buffer_list))
7556e509c4dSLars-Peter Clausen 			return 0;
7566e509c4dSLars-Peter Clausen 
757225d59adSLars-Peter Clausen 	modes = indio_dev->modes;
758225d59adSLars-Peter Clausen 
7596a8c6b26SAlexandru Ardelean 	list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
760225d59adSLars-Peter Clausen 		if (buffer == remove_buffer)
761225d59adSLars-Peter Clausen 			continue;
762225d59adSLars-Peter Clausen 		modes &= buffer->access->modes;
763f0566c0cSLars-Peter Clausen 		config->watermark = min(config->watermark, buffer->watermark);
764225d59adSLars-Peter Clausen 	}
765225d59adSLars-Peter Clausen 
766f0566c0cSLars-Peter Clausen 	if (insert_buffer) {
767225d59adSLars-Peter Clausen 		modes &= insert_buffer->access->modes;
768f0566c0cSLars-Peter Clausen 		config->watermark = min(config->watermark,
769f0566c0cSLars-Peter Clausen 			insert_buffer->watermark);
770f0566c0cSLars-Peter Clausen 	}
771225d59adSLars-Peter Clausen 
7726e509c4dSLars-Peter Clausen 	/* Definitely possible for devices to support both of these. */
773225d59adSLars-Peter Clausen 	if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) {
7746e509c4dSLars-Peter Clausen 		config->mode = INDIO_BUFFER_TRIGGERED;
775225d59adSLars-Peter Clausen 	} else if (modes & INDIO_BUFFER_HARDWARE) {
7761e1ec286SLars-Peter Clausen 		/*
7771e1ec286SLars-Peter Clausen 		 * Keep things simple for now and only allow a single buffer to
7781e1ec286SLars-Peter Clausen 		 * be connected in hardware mode.
7791e1ec286SLars-Peter Clausen 		 */
7806a8c6b26SAlexandru Ardelean 		if (insert_buffer && !list_empty(&iio_dev_opaque->buffer_list))
7811e1ec286SLars-Peter Clausen 			return -EINVAL;
7826e509c4dSLars-Peter Clausen 		config->mode = INDIO_BUFFER_HARDWARE;
7831e1ec286SLars-Peter Clausen 		strict_scanmask = true;
784225d59adSLars-Peter Clausen 	} else if (modes & INDIO_BUFFER_SOFTWARE) {
7856e509c4dSLars-Peter Clausen 		config->mode = INDIO_BUFFER_SOFTWARE;
7866e509c4dSLars-Peter Clausen 	} else {
7876e509c4dSLars-Peter Clausen 		/* Can only occur on first buffer */
7886e509c4dSLars-Peter Clausen 		if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
7896e509c4dSLars-Peter Clausen 			dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n");
7906e509c4dSLars-Peter Clausen 		return -EINVAL;
7916e509c4dSLars-Peter Clausen 	}
7926e509c4dSLars-Peter Clausen 
7936e509c4dSLars-Peter Clausen 	/* What scan mask do we actually have? */
7943862828aSAndy Shevchenko 	compound_mask = bitmap_zalloc(indio_dev->masklength, GFP_KERNEL);
7956e509c4dSLars-Peter Clausen 	if (compound_mask == NULL)
7966e509c4dSLars-Peter Clausen 		return -ENOMEM;
7976e509c4dSLars-Peter Clausen 
7986e509c4dSLars-Peter Clausen 	scan_timestamp = false;
7996e509c4dSLars-Peter Clausen 
8006a8c6b26SAlexandru Ardelean 	list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
8016e509c4dSLars-Peter Clausen 		if (buffer == remove_buffer)
8026e509c4dSLars-Peter Clausen 			continue;
8036e509c4dSLars-Peter Clausen 		bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
8046e509c4dSLars-Peter Clausen 			  indio_dev->masklength);
8056e509c4dSLars-Peter Clausen 		scan_timestamp |= buffer->scan_timestamp;
8066e509c4dSLars-Peter Clausen 	}
8076e509c4dSLars-Peter Clausen 
8086e509c4dSLars-Peter Clausen 	if (insert_buffer) {
8096e509c4dSLars-Peter Clausen 		bitmap_or(compound_mask, compound_mask,
8106e509c4dSLars-Peter Clausen 			  insert_buffer->scan_mask, indio_dev->masklength);
8116e509c4dSLars-Peter Clausen 		scan_timestamp |= insert_buffer->scan_timestamp;
8126e509c4dSLars-Peter Clausen 	}
8136e509c4dSLars-Peter Clausen 
8146e509c4dSLars-Peter Clausen 	if (indio_dev->available_scan_masks) {
8156e509c4dSLars-Peter Clausen 		scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks,
8166e509c4dSLars-Peter Clausen 				    indio_dev->masklength,
8171e1ec286SLars-Peter Clausen 				    compound_mask,
8181e1ec286SLars-Peter Clausen 				    strict_scanmask);
8193862828aSAndy Shevchenko 		bitmap_free(compound_mask);
8206e509c4dSLars-Peter Clausen 		if (scan_mask == NULL)
8216e509c4dSLars-Peter Clausen 			return -EINVAL;
8226e509c4dSLars-Peter Clausen 	} else {
8236e509c4dSLars-Peter Clausen 	    scan_mask = compound_mask;
8246e509c4dSLars-Peter Clausen 	}
8256e509c4dSLars-Peter Clausen 
8266e509c4dSLars-Peter Clausen 	config->scan_bytes = iio_compute_scan_bytes(indio_dev,
8276e509c4dSLars-Peter Clausen 				    scan_mask, scan_timestamp);
8286e509c4dSLars-Peter Clausen 	config->scan_mask = scan_mask;
8296e509c4dSLars-Peter Clausen 	config->scan_timestamp = scan_timestamp;
8306e509c4dSLars-Peter Clausen 
8316e509c4dSLars-Peter Clausen 	return 0;
8326e509c4dSLars-Peter Clausen }
8336e509c4dSLars-Peter Clausen 
83478c9981fSJonathan Cameron /**
83578c9981fSJonathan Cameron  * struct iio_demux_table - table describing demux memcpy ops
83678c9981fSJonathan Cameron  * @from:	index to copy from
83778c9981fSJonathan Cameron  * @to:		index to copy to
83878c9981fSJonathan Cameron  * @length:	how many bytes to copy
83978c9981fSJonathan Cameron  * @l:		list head used for management
84078c9981fSJonathan Cameron  */
84178c9981fSJonathan Cameron struct iio_demux_table {
84278c9981fSJonathan Cameron 	unsigned from;
84378c9981fSJonathan Cameron 	unsigned to;
84478c9981fSJonathan Cameron 	unsigned length;
84578c9981fSJonathan Cameron 	struct list_head l;
84678c9981fSJonathan Cameron };
84778c9981fSJonathan Cameron 
84878c9981fSJonathan Cameron static void iio_buffer_demux_free(struct iio_buffer *buffer)
84978c9981fSJonathan Cameron {
85078c9981fSJonathan Cameron 	struct iio_demux_table *p, *q;
85178c9981fSJonathan Cameron 	list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
85278c9981fSJonathan Cameron 		list_del(&p->l);
85378c9981fSJonathan Cameron 		kfree(p);
85478c9981fSJonathan Cameron 	}
85578c9981fSJonathan Cameron }
85678c9981fSJonathan Cameron 
85778c9981fSJonathan Cameron static int iio_buffer_add_demux(struct iio_buffer *buffer,
85878c9981fSJonathan Cameron 	struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc,
85978c9981fSJonathan Cameron 	unsigned int length)
86078c9981fSJonathan Cameron {
86178c9981fSJonathan Cameron 
86278c9981fSJonathan Cameron 	if (*p && (*p)->from + (*p)->length == in_loc &&
86378c9981fSJonathan Cameron 		(*p)->to + (*p)->length == out_loc) {
86478c9981fSJonathan Cameron 		(*p)->length += length;
86578c9981fSJonathan Cameron 	} else {
86678c9981fSJonathan Cameron 		*p = kmalloc(sizeof(**p), GFP_KERNEL);
86778c9981fSJonathan Cameron 		if (*p == NULL)
86878c9981fSJonathan Cameron 			return -ENOMEM;
86978c9981fSJonathan Cameron 		(*p)->from = in_loc;
87078c9981fSJonathan Cameron 		(*p)->to = out_loc;
87178c9981fSJonathan Cameron 		(*p)->length = length;
87278c9981fSJonathan Cameron 		list_add_tail(&(*p)->l, &buffer->demux_list);
87378c9981fSJonathan Cameron 	}
87478c9981fSJonathan Cameron 
87578c9981fSJonathan Cameron 	return 0;
87678c9981fSJonathan Cameron }
87778c9981fSJonathan Cameron 
87878c9981fSJonathan Cameron static int iio_buffer_update_demux(struct iio_dev *indio_dev,
87978c9981fSJonathan Cameron 				   struct iio_buffer *buffer)
88078c9981fSJonathan Cameron {
88178c9981fSJonathan Cameron 	int ret, in_ind = -1, out_ind, length;
88278c9981fSJonathan Cameron 	unsigned in_loc = 0, out_loc = 0;
88378c9981fSJonathan Cameron 	struct iio_demux_table *p = NULL;
88478c9981fSJonathan Cameron 
88578c9981fSJonathan Cameron 	/* Clear out any old demux */
88678c9981fSJonathan Cameron 	iio_buffer_demux_free(buffer);
88778c9981fSJonathan Cameron 	kfree(buffer->demux_bounce);
88878c9981fSJonathan Cameron 	buffer->demux_bounce = NULL;
88978c9981fSJonathan Cameron 
89078c9981fSJonathan Cameron 	/* First work out which scan mode we will actually have */
89178c9981fSJonathan Cameron 	if (bitmap_equal(indio_dev->active_scan_mask,
89278c9981fSJonathan Cameron 			 buffer->scan_mask,
89378c9981fSJonathan Cameron 			 indio_dev->masklength))
89478c9981fSJonathan Cameron 		return 0;
89578c9981fSJonathan Cameron 
89678c9981fSJonathan Cameron 	/* Now we have the two masks, work from least sig and build up sizes */
89778c9981fSJonathan Cameron 	for_each_set_bit(out_ind,
89878c9981fSJonathan Cameron 			 buffer->scan_mask,
89978c9981fSJonathan Cameron 			 indio_dev->masklength) {
90078c9981fSJonathan Cameron 		in_ind = find_next_bit(indio_dev->active_scan_mask,
90178c9981fSJonathan Cameron 				       indio_dev->masklength,
90278c9981fSJonathan Cameron 				       in_ind + 1);
90378c9981fSJonathan Cameron 		while (in_ind != out_ind) {
90478c9981fSJonathan Cameron 			length = iio_storage_bytes_for_si(indio_dev, in_ind);
90578c9981fSJonathan Cameron 			/* Make sure we are aligned */
90678c9981fSJonathan Cameron 			in_loc = roundup(in_loc, length) + length;
90719ef7b70SNuno Sá 			in_ind = find_next_bit(indio_dev->active_scan_mask,
90819ef7b70SNuno Sá 					       indio_dev->masklength,
90919ef7b70SNuno Sá 					       in_ind + 1);
91078c9981fSJonathan Cameron 		}
91178c9981fSJonathan Cameron 		length = iio_storage_bytes_for_si(indio_dev, in_ind);
91278c9981fSJonathan Cameron 		out_loc = roundup(out_loc, length);
91378c9981fSJonathan Cameron 		in_loc = roundup(in_loc, length);
91478c9981fSJonathan Cameron 		ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
91578c9981fSJonathan Cameron 		if (ret)
91678c9981fSJonathan Cameron 			goto error_clear_mux_table;
91778c9981fSJonathan Cameron 		out_loc += length;
91878c9981fSJonathan Cameron 		in_loc += length;
91978c9981fSJonathan Cameron 	}
92078c9981fSJonathan Cameron 	/* Relies on scan_timestamp being last */
92178c9981fSJonathan Cameron 	if (buffer->scan_timestamp) {
92278c9981fSJonathan Cameron 		length = iio_storage_bytes_for_timestamp(indio_dev);
92378c9981fSJonathan Cameron 		out_loc = roundup(out_loc, length);
92478c9981fSJonathan Cameron 		in_loc = roundup(in_loc, length);
92578c9981fSJonathan Cameron 		ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
92678c9981fSJonathan Cameron 		if (ret)
92778c9981fSJonathan Cameron 			goto error_clear_mux_table;
92878c9981fSJonathan Cameron 		out_loc += length;
92978c9981fSJonathan Cameron 		in_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 {
1153a9519456SLars-Peter Clausen 	int ret;
1154a9519456SLars-Peter Clausen 
11553909fab5SLars-Peter Clausen 	if (insert_buffer == remove_buffer)
11563909fab5SLars-Peter Clausen 		return 0;
11573909fab5SLars-Peter Clausen 
1158a9519456SLars-Peter Clausen 	mutex_lock(&indio_dev->info_exist_lock);
1159a9519456SLars-Peter Clausen 	mutex_lock(&indio_dev->mlock);
1160a9519456SLars-Peter Clausen 
11613909fab5SLars-Peter Clausen 	if (insert_buffer && iio_buffer_is_active(insert_buffer))
11623909fab5SLars-Peter Clausen 		insert_buffer = NULL;
11633909fab5SLars-Peter Clausen 
11643909fab5SLars-Peter Clausen 	if (remove_buffer && !iio_buffer_is_active(remove_buffer))
11653909fab5SLars-Peter Clausen 		remove_buffer = NULL;
11663909fab5SLars-Peter Clausen 
11673909fab5SLars-Peter Clausen 	if (!insert_buffer && !remove_buffer) {
11683909fab5SLars-Peter Clausen 		ret = 0;
11693909fab5SLars-Peter Clausen 		goto out_unlock;
11703909fab5SLars-Peter Clausen 	}
11713909fab5SLars-Peter Clausen 
1172a9519456SLars-Peter Clausen 	if (indio_dev->info == NULL) {
1173a9519456SLars-Peter Clausen 		ret = -ENODEV;
1174a9519456SLars-Peter Clausen 		goto out_unlock;
1175a9519456SLars-Peter Clausen 	}
1176a9519456SLars-Peter Clausen 
1177a9519456SLars-Peter Clausen 	ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
1178a9519456SLars-Peter Clausen 
1179a9519456SLars-Peter Clausen out_unlock:
1180a9519456SLars-Peter Clausen 	mutex_unlock(&indio_dev->mlock);
1181a9519456SLars-Peter Clausen 	mutex_unlock(&indio_dev->info_exist_lock);
1182a9519456SLars-Peter Clausen 
1183a9519456SLars-Peter Clausen 	return ret;
1184a9519456SLars-Peter Clausen }
118584b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_update_buffers);
118684b36ce5SJonathan Cameron 
1187623d74e3SLars-Peter Clausen void iio_disable_all_buffers(struct iio_dev *indio_dev)
1188623d74e3SLars-Peter Clausen {
1189623d74e3SLars-Peter Clausen 	iio_disable_buffers(indio_dev);
11901250186aSLars-Peter Clausen 	iio_buffer_deactivate_all(indio_dev);
1191623d74e3SLars-Peter Clausen }
1192623d74e3SLars-Peter Clausen 
119308e7e0adSLars-Peter Clausen static ssize_t iio_buffer_store_enable(struct device *dev,
119484b36ce5SJonathan Cameron 				       struct device_attribute *attr,
119584b36ce5SJonathan Cameron 				       const char *buf,
119684b36ce5SJonathan Cameron 				       size_t len)
119784b36ce5SJonathan Cameron {
119884b36ce5SJonathan Cameron 	int ret;
119984b36ce5SJonathan Cameron 	bool requested_state;
120084b36ce5SJonathan Cameron 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
120115097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
120284b36ce5SJonathan Cameron 	bool inlist;
120384b36ce5SJonathan Cameron 
120484b36ce5SJonathan Cameron 	ret = strtobool(buf, &requested_state);
120584b36ce5SJonathan Cameron 	if (ret < 0)
120684b36ce5SJonathan Cameron 		return ret;
120784b36ce5SJonathan Cameron 
120884b36ce5SJonathan Cameron 	mutex_lock(&indio_dev->mlock);
120984b36ce5SJonathan Cameron 
121084b36ce5SJonathan Cameron 	/* Find out if it is in the list */
1211ff3f7e04SAlexandru Ardelean 	inlist = iio_buffer_is_active(buffer);
121284b36ce5SJonathan Cameron 	/* Already in desired state */
121384b36ce5SJonathan Cameron 	if (inlist == requested_state)
121484b36ce5SJonathan Cameron 		goto done;
121584b36ce5SJonathan Cameron 
121684b36ce5SJonathan Cameron 	if (requested_state)
1217ff3f7e04SAlexandru Ardelean 		ret = __iio_update_buffers(indio_dev, buffer, NULL);
121884b36ce5SJonathan Cameron 	else
1219ff3f7e04SAlexandru Ardelean 		ret = __iio_update_buffers(indio_dev, NULL, buffer);
122084b36ce5SJonathan Cameron 
122184b36ce5SJonathan Cameron done:
122284b36ce5SJonathan Cameron 	mutex_unlock(&indio_dev->mlock);
122384b36ce5SJonathan Cameron 	return (ret < 0) ? ret : len;
122484b36ce5SJonathan Cameron }
122584b36ce5SJonathan Cameron 
122637d34556SJosselin Costanzi static ssize_t iio_buffer_show_watermark(struct device *dev,
122737d34556SJosselin Costanzi 					 struct device_attribute *attr,
122837d34556SJosselin Costanzi 					 char *buf)
122937d34556SJosselin Costanzi {
123015097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
123137d34556SJosselin Costanzi 
123283ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%u\n", buffer->watermark);
123337d34556SJosselin Costanzi }
123437d34556SJosselin Costanzi 
123537d34556SJosselin Costanzi static ssize_t iio_buffer_store_watermark(struct device *dev,
123637d34556SJosselin Costanzi 					  struct device_attribute *attr,
123737d34556SJosselin Costanzi 					  const char *buf,
123837d34556SJosselin Costanzi 					  size_t len)
123937d34556SJosselin Costanzi {
124037d34556SJosselin Costanzi 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
124115097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
124237d34556SJosselin Costanzi 	unsigned int val;
124337d34556SJosselin Costanzi 	int ret;
124437d34556SJosselin Costanzi 
124537d34556SJosselin Costanzi 	ret = kstrtouint(buf, 10, &val);
124637d34556SJosselin Costanzi 	if (ret)
124737d34556SJosselin Costanzi 		return ret;
124837d34556SJosselin Costanzi 	if (!val)
124937d34556SJosselin Costanzi 		return -EINVAL;
125037d34556SJosselin Costanzi 
125137d34556SJosselin Costanzi 	mutex_lock(&indio_dev->mlock);
125237d34556SJosselin Costanzi 
125337d34556SJosselin Costanzi 	if (val > buffer->length) {
125437d34556SJosselin Costanzi 		ret = -EINVAL;
125537d34556SJosselin Costanzi 		goto out;
125637d34556SJosselin Costanzi 	}
125737d34556SJosselin Costanzi 
1258ff3f7e04SAlexandru Ardelean 	if (iio_buffer_is_active(buffer)) {
125937d34556SJosselin Costanzi 		ret = -EBUSY;
126037d34556SJosselin Costanzi 		goto out;
126137d34556SJosselin Costanzi 	}
126237d34556SJosselin Costanzi 
126337d34556SJosselin Costanzi 	buffer->watermark = val;
126437d34556SJosselin Costanzi out:
126537d34556SJosselin Costanzi 	mutex_unlock(&indio_dev->mlock);
126637d34556SJosselin Costanzi 
126737d34556SJosselin Costanzi 	return ret ? ret : len;
126837d34556SJosselin Costanzi }
126937d34556SJosselin Costanzi 
1270350f6c75SMatt Fornero static ssize_t iio_dma_show_data_available(struct device *dev,
1271350f6c75SMatt Fornero 						struct device_attribute *attr,
1272350f6c75SMatt Fornero 						char *buf)
1273350f6c75SMatt Fornero {
127415097c7aSAlexandru Ardelean 	struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
1275350f6c75SMatt Fornero 
127683ca56b6SLars-Peter Clausen 	return sysfs_emit(buf, "%zu\n", iio_buffer_data_available(buffer));
1277350f6c75SMatt Fornero }
1278350f6c75SMatt Fornero 
127908e7e0adSLars-Peter Clausen static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
128008e7e0adSLars-Peter Clausen 		   iio_buffer_write_length);
12818d92db28SLars-Peter Clausen static struct device_attribute dev_attr_length_ro = __ATTR(length,
12828d92db28SLars-Peter Clausen 	S_IRUGO, iio_buffer_read_length, NULL);
128308e7e0adSLars-Peter Clausen static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
128408e7e0adSLars-Peter Clausen 		   iio_buffer_show_enable, iio_buffer_store_enable);
128537d34556SJosselin Costanzi static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR,
128637d34556SJosselin Costanzi 		   iio_buffer_show_watermark, iio_buffer_store_watermark);
1287b440655bSLars-Peter Clausen static struct device_attribute dev_attr_watermark_ro = __ATTR(watermark,
1288b440655bSLars-Peter Clausen 	S_IRUGO, iio_buffer_show_watermark, NULL);
1289350f6c75SMatt Fornero static DEVICE_ATTR(data_available, S_IRUGO,
1290350f6c75SMatt Fornero 		iio_dma_show_data_available, NULL);
129108e7e0adSLars-Peter Clausen 
12926da9b382SOctavian Purdila static struct attribute *iio_buffer_attrs[] = {
12936da9b382SOctavian Purdila 	&dev_attr_length.attr,
12946da9b382SOctavian Purdila 	&dev_attr_enable.attr,
129537d34556SJosselin Costanzi 	&dev_attr_watermark.attr,
1296350f6c75SMatt Fornero 	&dev_attr_data_available.attr,
12976da9b382SOctavian Purdila };
12986da9b382SOctavian Purdila 
129915097c7aSAlexandru Ardelean #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
130015097c7aSAlexandru Ardelean 
130115097c7aSAlexandru Ardelean static struct attribute *iio_buffer_wrap_attr(struct iio_buffer *buffer,
130215097c7aSAlexandru Ardelean 					      struct attribute *attr)
130315097c7aSAlexandru Ardelean {
130415097c7aSAlexandru Ardelean 	struct device_attribute *dattr = to_dev_attr(attr);
130515097c7aSAlexandru Ardelean 	struct iio_dev_attr *iio_attr;
130615097c7aSAlexandru Ardelean 
130715097c7aSAlexandru Ardelean 	iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
130815097c7aSAlexandru Ardelean 	if (!iio_attr)
130915097c7aSAlexandru Ardelean 		return NULL;
131015097c7aSAlexandru Ardelean 
131115097c7aSAlexandru Ardelean 	iio_attr->buffer = buffer;
131215097c7aSAlexandru Ardelean 	memcpy(&iio_attr->dev_attr, dattr, sizeof(iio_attr->dev_attr));
131315097c7aSAlexandru Ardelean 	iio_attr->dev_attr.attr.name = kstrdup_const(attr->name, GFP_KERNEL);
1314ca3e7d52SAlexandru Ardelean 	sysfs_attr_init(&iio_attr->dev_attr.attr);
131515097c7aSAlexandru Ardelean 
131615097c7aSAlexandru Ardelean 	list_add(&iio_attr->l, &buffer->buffer_attr_list);
131715097c7aSAlexandru Ardelean 
131815097c7aSAlexandru Ardelean 	return &iio_attr->dev_attr.attr;
131915097c7aSAlexandru Ardelean }
132015097c7aSAlexandru Ardelean 
1321d9a62574SAlexandru Ardelean static int iio_buffer_register_legacy_sysfs_groups(struct iio_dev *indio_dev,
1322d9a62574SAlexandru Ardelean 						   struct attribute **buffer_attrs,
1323d9a62574SAlexandru Ardelean 						   int buffer_attrcount,
1324d9a62574SAlexandru Ardelean 						   int scan_el_attrcount)
1325d9a62574SAlexandru Ardelean {
1326d9a62574SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1327d9a62574SAlexandru Ardelean 	struct attribute_group *group;
1328d9a62574SAlexandru Ardelean 	struct attribute **attrs;
1329d9a62574SAlexandru Ardelean 	int ret;
1330d9a62574SAlexandru Ardelean 
1331d9a62574SAlexandru Ardelean 	attrs = kcalloc(buffer_attrcount + 1, sizeof(*attrs), GFP_KERNEL);
1332d9a62574SAlexandru Ardelean 	if (!attrs)
1333d9a62574SAlexandru Ardelean 		return -ENOMEM;
1334d9a62574SAlexandru Ardelean 
1335d9a62574SAlexandru Ardelean 	memcpy(attrs, buffer_attrs, buffer_attrcount * sizeof(*attrs));
1336d9a62574SAlexandru Ardelean 
1337d9a62574SAlexandru Ardelean 	group = &iio_dev_opaque->legacy_buffer_group;
1338d9a62574SAlexandru Ardelean 	group->attrs = attrs;
1339d9a62574SAlexandru Ardelean 	group->name = "buffer";
1340d9a62574SAlexandru Ardelean 
1341d9a62574SAlexandru Ardelean 	ret = iio_device_register_sysfs_group(indio_dev, group);
1342d9a62574SAlexandru Ardelean 	if (ret)
1343d9a62574SAlexandru Ardelean 		goto error_free_buffer_attrs;
1344d9a62574SAlexandru Ardelean 
1345d9a62574SAlexandru Ardelean 	attrs = kcalloc(scan_el_attrcount + 1, sizeof(*attrs), GFP_KERNEL);
1346d9a62574SAlexandru Ardelean 	if (!attrs) {
1347d9a62574SAlexandru Ardelean 		ret = -ENOMEM;
1348d9a62574SAlexandru Ardelean 		goto error_free_buffer_attrs;
1349d9a62574SAlexandru Ardelean 	}
1350d9a62574SAlexandru Ardelean 
1351d9a62574SAlexandru Ardelean 	memcpy(attrs, &buffer_attrs[buffer_attrcount],
1352d9a62574SAlexandru Ardelean 	       scan_el_attrcount * sizeof(*attrs));
1353d9a62574SAlexandru Ardelean 
1354d9a62574SAlexandru Ardelean 	group = &iio_dev_opaque->legacy_scan_el_group;
1355d9a62574SAlexandru Ardelean 	group->attrs = attrs;
1356d9a62574SAlexandru Ardelean 	group->name = "scan_elements";
1357d9a62574SAlexandru Ardelean 
1358d9a62574SAlexandru Ardelean 	ret = iio_device_register_sysfs_group(indio_dev, group);
1359d9a62574SAlexandru Ardelean 	if (ret)
1360d9a62574SAlexandru Ardelean 		goto error_free_scan_el_attrs;
1361d9a62574SAlexandru Ardelean 
1362d9a62574SAlexandru Ardelean 	return 0;
1363d9a62574SAlexandru Ardelean 
1364d9a62574SAlexandru Ardelean error_free_buffer_attrs:
1365d9a62574SAlexandru Ardelean 	kfree(iio_dev_opaque->legacy_buffer_group.attrs);
1366d9a62574SAlexandru Ardelean error_free_scan_el_attrs:
1367d9a62574SAlexandru Ardelean 	kfree(iio_dev_opaque->legacy_scan_el_group.attrs);
1368d9a62574SAlexandru Ardelean 
1369d9a62574SAlexandru Ardelean 	return ret;
1370d9a62574SAlexandru Ardelean }
1371d9a62574SAlexandru Ardelean 
1372d9a62574SAlexandru Ardelean static void iio_buffer_unregister_legacy_sysfs_groups(struct iio_dev *indio_dev)
1373d9a62574SAlexandru Ardelean {
1374d9a62574SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1375d9a62574SAlexandru Ardelean 
1376d9a62574SAlexandru Ardelean 	kfree(iio_dev_opaque->legacy_buffer_group.attrs);
1377d9a62574SAlexandru Ardelean 	kfree(iio_dev_opaque->legacy_scan_el_group.attrs);
1378d9a62574SAlexandru Ardelean }
1379d9a62574SAlexandru Ardelean 
1380f73f7f4dSAlexandru Ardelean static int iio_buffer_chrdev_release(struct inode *inode, struct file *filep)
1381f73f7f4dSAlexandru Ardelean {
1382f73f7f4dSAlexandru Ardelean 	struct iio_dev_buffer_pair *ib = filep->private_data;
1383f73f7f4dSAlexandru Ardelean 	struct iio_dev *indio_dev = ib->indio_dev;
1384f73f7f4dSAlexandru Ardelean 	struct iio_buffer *buffer = ib->buffer;
1385f73f7f4dSAlexandru Ardelean 
1386f73f7f4dSAlexandru Ardelean 	wake_up(&buffer->pollq);
1387f73f7f4dSAlexandru Ardelean 
1388f73f7f4dSAlexandru Ardelean 	kfree(ib);
1389f73f7f4dSAlexandru Ardelean 	clear_bit(IIO_BUSY_BIT_POS, &buffer->flags);
1390f73f7f4dSAlexandru Ardelean 	iio_device_put(indio_dev);
1391f73f7f4dSAlexandru Ardelean 
1392f73f7f4dSAlexandru Ardelean 	return 0;
1393f73f7f4dSAlexandru Ardelean }
1394f73f7f4dSAlexandru Ardelean 
1395f73f7f4dSAlexandru Ardelean static const struct file_operations iio_buffer_chrdev_fileops = {
1396f73f7f4dSAlexandru Ardelean 	.owner = THIS_MODULE,
1397f73f7f4dSAlexandru Ardelean 	.llseek = noop_llseek,
1398f73f7f4dSAlexandru Ardelean 	.read = iio_buffer_read,
1399f73f7f4dSAlexandru Ardelean 	.poll = iio_buffer_poll,
1400f73f7f4dSAlexandru Ardelean 	.release = iio_buffer_chrdev_release,
1401f73f7f4dSAlexandru Ardelean };
1402f73f7f4dSAlexandru Ardelean 
1403f73f7f4dSAlexandru Ardelean static long iio_device_buffer_getfd(struct iio_dev *indio_dev, unsigned long arg)
1404f73f7f4dSAlexandru Ardelean {
1405f73f7f4dSAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1406f73f7f4dSAlexandru Ardelean 	int __user *ival = (int __user *)arg;
1407f73f7f4dSAlexandru Ardelean 	struct iio_dev_buffer_pair *ib;
1408f73f7f4dSAlexandru Ardelean 	struct iio_buffer *buffer;
1409f73f7f4dSAlexandru Ardelean 	int fd, idx, ret;
1410f73f7f4dSAlexandru Ardelean 
1411f73f7f4dSAlexandru Ardelean 	if (copy_from_user(&idx, ival, sizeof(idx)))
1412f73f7f4dSAlexandru Ardelean 		return -EFAULT;
1413f73f7f4dSAlexandru Ardelean 
1414f73f7f4dSAlexandru Ardelean 	if (idx >= iio_dev_opaque->attached_buffers_cnt)
1415f73f7f4dSAlexandru Ardelean 		return -ENODEV;
1416f73f7f4dSAlexandru Ardelean 
1417f73f7f4dSAlexandru Ardelean 	iio_device_get(indio_dev);
1418f73f7f4dSAlexandru Ardelean 
1419f73f7f4dSAlexandru Ardelean 	buffer = iio_dev_opaque->attached_buffers[idx];
1420f73f7f4dSAlexandru Ardelean 
1421f73f7f4dSAlexandru Ardelean 	if (test_and_set_bit(IIO_BUSY_BIT_POS, &buffer->flags)) {
1422f73f7f4dSAlexandru Ardelean 		ret = -EBUSY;
1423f73f7f4dSAlexandru Ardelean 		goto error_iio_dev_put;
1424f73f7f4dSAlexandru Ardelean 	}
1425f73f7f4dSAlexandru Ardelean 
1426f73f7f4dSAlexandru Ardelean 	ib = kzalloc(sizeof(*ib), GFP_KERNEL);
1427f73f7f4dSAlexandru Ardelean 	if (!ib) {
1428f73f7f4dSAlexandru Ardelean 		ret = -ENOMEM;
1429f73f7f4dSAlexandru Ardelean 		goto error_clear_busy_bit;
1430f73f7f4dSAlexandru Ardelean 	}
1431f73f7f4dSAlexandru Ardelean 
1432f73f7f4dSAlexandru Ardelean 	ib->indio_dev = indio_dev;
1433f73f7f4dSAlexandru Ardelean 	ib->buffer = buffer;
1434f73f7f4dSAlexandru Ardelean 
1435f73f7f4dSAlexandru Ardelean 	fd = anon_inode_getfd("iio:buffer", &iio_buffer_chrdev_fileops,
1436f73f7f4dSAlexandru Ardelean 			      ib, O_RDWR | O_CLOEXEC);
1437f73f7f4dSAlexandru Ardelean 	if (fd < 0) {
1438f73f7f4dSAlexandru Ardelean 		ret = fd;
1439f73f7f4dSAlexandru Ardelean 		goto error_free_ib;
1440f73f7f4dSAlexandru Ardelean 	}
1441f73f7f4dSAlexandru Ardelean 
1442f73f7f4dSAlexandru Ardelean 	if (copy_to_user(ival, &fd, sizeof(fd))) {
1443f73f7f4dSAlexandru Ardelean 		put_unused_fd(fd);
1444f73f7f4dSAlexandru Ardelean 		ret = -EFAULT;
1445f73f7f4dSAlexandru Ardelean 		goto error_free_ib;
1446f73f7f4dSAlexandru Ardelean 	}
1447f73f7f4dSAlexandru Ardelean 
14484c822244SAlexandru Ardelean 	return 0;
1449f73f7f4dSAlexandru Ardelean 
1450f73f7f4dSAlexandru Ardelean error_free_ib:
1451f73f7f4dSAlexandru Ardelean 	kfree(ib);
1452f73f7f4dSAlexandru Ardelean error_clear_busy_bit:
1453f73f7f4dSAlexandru Ardelean 	clear_bit(IIO_BUSY_BIT_POS, &buffer->flags);
1454f73f7f4dSAlexandru Ardelean error_iio_dev_put:
1455f73f7f4dSAlexandru Ardelean 	iio_device_put(indio_dev);
1456f73f7f4dSAlexandru Ardelean 	return ret;
1457f73f7f4dSAlexandru Ardelean }
1458f73f7f4dSAlexandru Ardelean 
1459f73f7f4dSAlexandru Ardelean static long iio_device_buffer_ioctl(struct iio_dev *indio_dev, struct file *filp,
1460f73f7f4dSAlexandru Ardelean 				    unsigned int cmd, unsigned long arg)
1461f73f7f4dSAlexandru Ardelean {
1462f73f7f4dSAlexandru Ardelean 	switch (cmd) {
1463f73f7f4dSAlexandru Ardelean 	case IIO_BUFFER_GET_FD_IOCTL:
1464f73f7f4dSAlexandru Ardelean 		return iio_device_buffer_getfd(indio_dev, arg);
1465f73f7f4dSAlexandru Ardelean 	default:
1466f73f7f4dSAlexandru Ardelean 		return IIO_IOCTL_UNHANDLED;
1467f73f7f4dSAlexandru Ardelean 	}
1468f73f7f4dSAlexandru Ardelean }
1469f73f7f4dSAlexandru Ardelean 
1470e16e0a77SAlexandru Ardelean static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
1471d9a62574SAlexandru Ardelean 					     struct iio_dev *indio_dev,
1472d9a62574SAlexandru Ardelean 					     int index)
1473d967cb6bSLars-Peter Clausen {
1474*62f4f36cSJonathan Cameron 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1475d967cb6bSLars-Peter Clausen 	struct iio_dev_attr *p;
1476d967cb6bSLars-Peter Clausen 	struct attribute **attr;
1477e2b4d7acSAlexandru Ardelean 	int ret, i, attrn, scan_el_attrcount, buffer_attrcount;
1478d967cb6bSLars-Peter Clausen 	const struct iio_chan_spec *channels;
1479d967cb6bSLars-Peter Clausen 
1480e2b4d7acSAlexandru Ardelean 	buffer_attrcount = 0;
148108e7e0adSLars-Peter Clausen 	if (buffer->attrs) {
1482e2b4d7acSAlexandru Ardelean 		while (buffer->attrs[buffer_attrcount] != NULL)
1483e2b4d7acSAlexandru Ardelean 			buffer_attrcount++;
148408e7e0adSLars-Peter Clausen 	}
148508e7e0adSLars-Peter Clausen 
1486e2b4d7acSAlexandru Ardelean 	scan_el_attrcount = 0;
148715097c7aSAlexandru Ardelean 	INIT_LIST_HEAD(&buffer->buffer_attr_list);
1488d967cb6bSLars-Peter Clausen 	channels = indio_dev->channels;
1489d967cb6bSLars-Peter Clausen 	if (channels) {
1490d967cb6bSLars-Peter Clausen 		/* new magic */
1491d967cb6bSLars-Peter Clausen 		for (i = 0; i < indio_dev->num_channels; i++) {
1492d967cb6bSLars-Peter Clausen 			if (channels[i].scan_index < 0)
1493d967cb6bSLars-Peter Clausen 				continue;
1494d967cb6bSLars-Peter Clausen 
1495ff3f7e04SAlexandru Ardelean 			ret = iio_buffer_add_channel_sysfs(indio_dev, buffer,
1496d967cb6bSLars-Peter Clausen 							 &channels[i]);
1497d967cb6bSLars-Peter Clausen 			if (ret < 0)
1498d967cb6bSLars-Peter Clausen 				goto error_cleanup_dynamic;
1499e2b4d7acSAlexandru Ardelean 			scan_el_attrcount += ret;
1500d967cb6bSLars-Peter Clausen 			if (channels[i].type == IIO_TIMESTAMP)
1501*62f4f36cSJonathan Cameron 				iio_dev_opaque->scan_index_timestamp =
1502d967cb6bSLars-Peter Clausen 					channels[i].scan_index;
1503d967cb6bSLars-Peter Clausen 		}
1504d967cb6bSLars-Peter Clausen 		if (indio_dev->masklength && buffer->scan_mask == NULL) {
15053862828aSAndy Shevchenko 			buffer->scan_mask = bitmap_zalloc(indio_dev->masklength,
1506d967cb6bSLars-Peter Clausen 							  GFP_KERNEL);
1507d967cb6bSLars-Peter Clausen 			if (buffer->scan_mask == NULL) {
1508d967cb6bSLars-Peter Clausen 				ret = -ENOMEM;
1509d967cb6bSLars-Peter Clausen 				goto error_cleanup_dynamic;
1510d967cb6bSLars-Peter Clausen 			}
1511d967cb6bSLars-Peter Clausen 		}
1512d967cb6bSLars-Peter Clausen 	}
1513d967cb6bSLars-Peter Clausen 
1514d9a62574SAlexandru Ardelean 	attrn = buffer_attrcount + scan_el_attrcount + ARRAY_SIZE(iio_buffer_attrs);
1515d9a62574SAlexandru Ardelean 	attr = kcalloc(attrn + 1, sizeof(* attr), GFP_KERNEL);
1516e2b4d7acSAlexandru Ardelean 	if (!attr) {
1517e2b4d7acSAlexandru Ardelean 		ret = -ENOMEM;
1518e2b4d7acSAlexandru Ardelean 		goto error_free_scan_mask;
1519e2b4d7acSAlexandru Ardelean 	}
1520e2b4d7acSAlexandru Ardelean 
1521e2b4d7acSAlexandru Ardelean 	memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
1522e2b4d7acSAlexandru Ardelean 	if (!buffer->access->set_length)
1523e2b4d7acSAlexandru Ardelean 		attr[0] = &dev_attr_length_ro.attr;
1524e2b4d7acSAlexandru Ardelean 
1525e2b4d7acSAlexandru Ardelean 	if (buffer->access->flags & INDIO_BUFFER_FLAG_FIXED_WATERMARK)
1526e2b4d7acSAlexandru Ardelean 		attr[2] = &dev_attr_watermark_ro.attr;
1527e2b4d7acSAlexandru Ardelean 
1528e2b4d7acSAlexandru Ardelean 	if (buffer->attrs)
1529e2b4d7acSAlexandru Ardelean 		memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs,
1530e2b4d7acSAlexandru Ardelean 		       sizeof(struct attribute *) * buffer_attrcount);
1531e2b4d7acSAlexandru Ardelean 
1532e2b4d7acSAlexandru Ardelean 	buffer_attrcount += ARRAY_SIZE(iio_buffer_attrs);
1533e2b4d7acSAlexandru Ardelean 
153415097c7aSAlexandru Ardelean 	for (i = 0; i < buffer_attrcount; i++) {
153515097c7aSAlexandru Ardelean 		struct attribute *wrapped;
1536d9a62574SAlexandru Ardelean 
153715097c7aSAlexandru Ardelean 		wrapped = iio_buffer_wrap_attr(buffer, attr[i]);
153815097c7aSAlexandru Ardelean 		if (!wrapped) {
153915097c7aSAlexandru Ardelean 			ret = -ENOMEM;
154015097c7aSAlexandru Ardelean 			goto error_free_scan_mask;
154115097c7aSAlexandru Ardelean 		}
154215097c7aSAlexandru Ardelean 		attr[i] = wrapped;
154315097c7aSAlexandru Ardelean 	}
154415097c7aSAlexandru Ardelean 
154515097c7aSAlexandru Ardelean 	attrn = 0;
154615097c7aSAlexandru Ardelean 	list_for_each_entry(p, &buffer->buffer_attr_list, l)
1547d9a62574SAlexandru Ardelean 		attr[attrn++] = &p->dev_attr.attr;
1548d9a62574SAlexandru Ardelean 
1549d9a62574SAlexandru Ardelean 	buffer->buffer_group.name = kasprintf(GFP_KERNEL, "buffer%d", index);
1550d9a62574SAlexandru Ardelean 	if (!buffer->buffer_group.name) {
1551d9a62574SAlexandru Ardelean 		ret = -ENOMEM;
1552d9a62574SAlexandru Ardelean 		goto error_free_buffer_attrs;
1553d9a62574SAlexandru Ardelean 	}
1554d9a62574SAlexandru Ardelean 
1555e2b4d7acSAlexandru Ardelean 	buffer->buffer_group.attrs = attr;
1556e2b4d7acSAlexandru Ardelean 
1557e2b4d7acSAlexandru Ardelean 	ret = iio_device_register_sysfs_group(indio_dev, &buffer->buffer_group);
1558e2b4d7acSAlexandru Ardelean 	if (ret)
1559d9a62574SAlexandru Ardelean 		goto error_free_buffer_attr_group_name;
1560e2b4d7acSAlexandru Ardelean 
1561d9a62574SAlexandru Ardelean 	/* we only need to register the legacy groups for the first buffer */
1562d9a62574SAlexandru Ardelean 	if (index > 0)
1563d9a62574SAlexandru Ardelean 		return 0;
1564d967cb6bSLars-Peter Clausen 
1565d9a62574SAlexandru Ardelean 	ret = iio_buffer_register_legacy_sysfs_groups(indio_dev, attr,
1566d9a62574SAlexandru Ardelean 						      buffer_attrcount,
1567d9a62574SAlexandru Ardelean 						      scan_el_attrcount);
156832f17172SAlexandru Ardelean 	if (ret)
1569d9a62574SAlexandru Ardelean 		goto error_free_buffer_attr_group_name;
1570d967cb6bSLars-Peter Clausen 
1571d967cb6bSLars-Peter Clausen 	return 0;
1572d967cb6bSLars-Peter Clausen 
1573d9a62574SAlexandru Ardelean error_free_buffer_attr_group_name:
1574d9a62574SAlexandru Ardelean 	kfree(buffer->buffer_group.name);
1575e2b4d7acSAlexandru Ardelean error_free_buffer_attrs:
1576e2b4d7acSAlexandru Ardelean 	kfree(buffer->buffer_group.attrs);
1577d967cb6bSLars-Peter Clausen error_free_scan_mask:
15783862828aSAndy Shevchenko 	bitmap_free(buffer->scan_mask);
1579d967cb6bSLars-Peter Clausen error_cleanup_dynamic:
158015097c7aSAlexandru Ardelean 	iio_free_chan_devattr_list(&buffer->buffer_attr_list);
1581d967cb6bSLars-Peter Clausen 
1582d967cb6bSLars-Peter Clausen 	return ret;
1583d967cb6bSLars-Peter Clausen }
1584d967cb6bSLars-Peter Clausen 
15850224af85SAlexandru Ardelean static void __iio_buffer_free_sysfs_and_mask(struct iio_buffer *buffer)
15860224af85SAlexandru Ardelean {
15870224af85SAlexandru Ardelean 	bitmap_free(buffer->scan_mask);
15880224af85SAlexandru Ardelean 	kfree(buffer->buffer_group.name);
15890224af85SAlexandru Ardelean 	kfree(buffer->buffer_group.attrs);
15900224af85SAlexandru Ardelean 	iio_free_chan_devattr_list(&buffer->buffer_attr_list);
15910224af85SAlexandru Ardelean }
15920224af85SAlexandru Ardelean 
1593ee708e6bSAlexandru Ardelean int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
1594e16e0a77SAlexandru Ardelean {
1595ee708e6bSAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1596e16e0a77SAlexandru Ardelean 	const struct iio_chan_spec *channels;
1597ee708e6bSAlexandru Ardelean 	struct iio_buffer *buffer;
1598ee708e6bSAlexandru Ardelean 	int unwind_idx;
1599ee708e6bSAlexandru Ardelean 	int ret, i;
1600f73f7f4dSAlexandru Ardelean 	size_t sz;
1601e16e0a77SAlexandru Ardelean 
1602e16e0a77SAlexandru Ardelean 	channels = indio_dev->channels;
1603e16e0a77SAlexandru Ardelean 	if (channels) {
1604e16e0a77SAlexandru Ardelean 		int ml = indio_dev->masklength;
1605e16e0a77SAlexandru Ardelean 
1606e16e0a77SAlexandru Ardelean 		for (i = 0; i < indio_dev->num_channels; i++)
1607e16e0a77SAlexandru Ardelean 			ml = max(ml, channels[i].scan_index + 1);
1608e16e0a77SAlexandru Ardelean 		indio_dev->masklength = ml;
1609e16e0a77SAlexandru Ardelean 	}
1610e16e0a77SAlexandru Ardelean 
1611ee708e6bSAlexandru Ardelean 	if (!iio_dev_opaque->attached_buffers_cnt)
1612e16e0a77SAlexandru Ardelean 		return 0;
1613e16e0a77SAlexandru Ardelean 
1614ee708e6bSAlexandru Ardelean 	for (i = 0; i < iio_dev_opaque->attached_buffers_cnt; i++) {
1615ee708e6bSAlexandru Ardelean 		buffer = iio_dev_opaque->attached_buffers[i];
1616ee708e6bSAlexandru Ardelean 		ret = __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev, i);
1617ee708e6bSAlexandru Ardelean 		if (ret) {
1618ee708e6bSAlexandru Ardelean 			unwind_idx = i;
1619ee708e6bSAlexandru Ardelean 			goto error_unwind_sysfs_and_mask;
1620ee708e6bSAlexandru Ardelean 		}
1621e16e0a77SAlexandru Ardelean 	}
1622f73f7f4dSAlexandru Ardelean 	unwind_idx = iio_dev_opaque->attached_buffers_cnt - 1;
1623f73f7f4dSAlexandru Ardelean 
1624f73f7f4dSAlexandru Ardelean 	sz = sizeof(*(iio_dev_opaque->buffer_ioctl_handler));
1625f73f7f4dSAlexandru Ardelean 	iio_dev_opaque->buffer_ioctl_handler = kzalloc(sz, GFP_KERNEL);
1626f73f7f4dSAlexandru Ardelean 	if (!iio_dev_opaque->buffer_ioctl_handler) {
1627f73f7f4dSAlexandru Ardelean 		ret = -ENOMEM;
1628f73f7f4dSAlexandru Ardelean 		goto error_unwind_sysfs_and_mask;
1629f73f7f4dSAlexandru Ardelean 	}
1630f73f7f4dSAlexandru Ardelean 
1631f73f7f4dSAlexandru Ardelean 	iio_dev_opaque->buffer_ioctl_handler->ioctl = iio_device_buffer_ioctl;
1632f73f7f4dSAlexandru Ardelean 	iio_device_ioctl_handler_register(indio_dev,
1633f73f7f4dSAlexandru Ardelean 					  iio_dev_opaque->buffer_ioctl_handler);
1634e16e0a77SAlexandru Ardelean 
1635ee708e6bSAlexandru Ardelean 	return 0;
1636ff3f7e04SAlexandru Ardelean 
1637ee708e6bSAlexandru Ardelean error_unwind_sysfs_and_mask:
1638ee708e6bSAlexandru Ardelean 	for (; unwind_idx >= 0; unwind_idx--) {
1639ee708e6bSAlexandru Ardelean 		buffer = iio_dev_opaque->attached_buffers[unwind_idx];
1640ee708e6bSAlexandru Ardelean 		__iio_buffer_free_sysfs_and_mask(buffer);
1641ee708e6bSAlexandru Ardelean 	}
1642ee708e6bSAlexandru Ardelean 	return ret;
1643ee708e6bSAlexandru Ardelean }
1644ee708e6bSAlexandru Ardelean 
1645ee708e6bSAlexandru Ardelean void iio_buffers_free_sysfs_and_mask(struct iio_dev *indio_dev)
1646ee708e6bSAlexandru Ardelean {
1647ee708e6bSAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1648ee708e6bSAlexandru Ardelean 	struct iio_buffer *buffer;
1649ee708e6bSAlexandru Ardelean 	int i;
1650ee708e6bSAlexandru Ardelean 
1651ee708e6bSAlexandru Ardelean 	if (!iio_dev_opaque->attached_buffers_cnt)
1652d967cb6bSLars-Peter Clausen 		return;
1653d967cb6bSLars-Peter Clausen 
1654f73f7f4dSAlexandru Ardelean 	iio_device_ioctl_handler_unregister(iio_dev_opaque->buffer_ioctl_handler);
1655f73f7f4dSAlexandru Ardelean 	kfree(iio_dev_opaque->buffer_ioctl_handler);
1656f73f7f4dSAlexandru Ardelean 
1657d9a62574SAlexandru Ardelean 	iio_buffer_unregister_legacy_sysfs_groups(indio_dev);
1658d9a62574SAlexandru Ardelean 
1659ee708e6bSAlexandru Ardelean 	for (i = iio_dev_opaque->attached_buffers_cnt - 1; i >= 0; i--) {
1660ee708e6bSAlexandru Ardelean 		buffer = iio_dev_opaque->attached_buffers[i];
1661e16e0a77SAlexandru Ardelean 		__iio_buffer_free_sysfs_and_mask(buffer);
1662d967cb6bSLars-Peter Clausen 	}
1663ee708e6bSAlexandru Ardelean }
1664ee708e6bSAlexandru Ardelean 
1665a980e046SJonathan Cameron /**
166681636632SLars-Peter Clausen  * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
166781636632SLars-Peter Clausen  * @indio_dev: the iio device
166881636632SLars-Peter Clausen  * @mask: scan mask to be checked
166981636632SLars-Peter Clausen  *
167081636632SLars-Peter Clausen  * Return true if exactly one bit is set in the scan mask, false otherwise. It
167181636632SLars-Peter Clausen  * can be used for devices where only one channel can be active for sampling at
167281636632SLars-Peter Clausen  * a time.
167381636632SLars-Peter Clausen  */
167481636632SLars-Peter Clausen bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
167581636632SLars-Peter Clausen 	const unsigned long *mask)
167681636632SLars-Peter Clausen {
167781636632SLars-Peter Clausen 	return bitmap_weight(mask, indio_dev->masklength) == 1;
167881636632SLars-Peter Clausen }
167981636632SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
168081636632SLars-Peter Clausen 
16815d65d920SLars-Peter Clausen static const void *iio_demux(struct iio_buffer *buffer,
16825d65d920SLars-Peter Clausen 				 const void *datain)
1683a980e046SJonathan Cameron {
1684a980e046SJonathan Cameron 	struct iio_demux_table *t;
1685a980e046SJonathan Cameron 
1686a980e046SJonathan Cameron 	if (list_empty(&buffer->demux_list))
1687a980e046SJonathan Cameron 		return datain;
1688a980e046SJonathan Cameron 	list_for_each_entry(t, &buffer->demux_list, l)
1689a980e046SJonathan Cameron 		memcpy(buffer->demux_bounce + t->to,
1690a980e046SJonathan Cameron 		       datain + t->from, t->length);
1691a980e046SJonathan Cameron 
1692a980e046SJonathan Cameron 	return buffer->demux_bounce;
1693a980e046SJonathan Cameron }
1694a980e046SJonathan Cameron 
16955d65d920SLars-Peter Clausen static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
1696a980e046SJonathan Cameron {
16975d65d920SLars-Peter Clausen 	const void *dataout = iio_demux(buffer, data);
169837d34556SJosselin Costanzi 	int ret;
1699a980e046SJonathan Cameron 
170037d34556SJosselin Costanzi 	ret = buffer->access->store_to(buffer, dataout);
170137d34556SJosselin Costanzi 	if (ret)
170237d34556SJosselin Costanzi 		return ret;
170337d34556SJosselin Costanzi 
170437d34556SJosselin Costanzi 	/*
170537d34556SJosselin Costanzi 	 * We can't just test for watermark to decide if we wake the poll queue
170637d34556SJosselin Costanzi 	 * because read may request less samples than the watermark.
170737d34556SJosselin Costanzi 	 */
1708a9a08845SLinus Torvalds 	wake_up_interruptible_poll(&buffer->pollq, EPOLLIN | EPOLLRDNORM);
170937d34556SJosselin Costanzi 	return 0;
1710a980e046SJonathan Cameron }
1711a980e046SJonathan Cameron 
1712315a19ecSJonathan Cameron /**
1713315a19ecSJonathan Cameron  * iio_push_to_buffers() - push to a registered buffer.
1714315a19ecSJonathan Cameron  * @indio_dev:		iio_dev structure for device.
1715315a19ecSJonathan Cameron  * @data:		Full scan.
1716315a19ecSJonathan Cameron  */
17175d65d920SLars-Peter Clausen int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
171884b36ce5SJonathan Cameron {
17196a8c6b26SAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
172084b36ce5SJonathan Cameron 	int ret;
172184b36ce5SJonathan Cameron 	struct iio_buffer *buf;
172284b36ce5SJonathan Cameron 
17236a8c6b26SAlexandru Ardelean 	list_for_each_entry(buf, &iio_dev_opaque->buffer_list, buffer_list) {
172484b36ce5SJonathan Cameron 		ret = iio_push_to_buffer(buf, data);
172584b36ce5SJonathan Cameron 		if (ret < 0)
172684b36ce5SJonathan Cameron 			return ret;
172784b36ce5SJonathan Cameron 	}
172884b36ce5SJonathan Cameron 
172984b36ce5SJonathan Cameron 	return 0;
173084b36ce5SJonathan Cameron }
173184b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_push_to_buffers);
173284b36ce5SJonathan Cameron 
17339e69c935SLars-Peter Clausen /**
17349e69c935SLars-Peter Clausen  * iio_buffer_release() - Free a buffer's resources
17359e69c935SLars-Peter Clausen  * @ref: Pointer to the kref embedded in the iio_buffer struct
17369e69c935SLars-Peter Clausen  *
17379e69c935SLars-Peter Clausen  * This function is called when the last reference to the buffer has been
17389e69c935SLars-Peter Clausen  * dropped. It will typically free all resources allocated by the buffer. Do not
17399e69c935SLars-Peter Clausen  * call this function manually, always use iio_buffer_put() when done using a
17409e69c935SLars-Peter Clausen  * buffer.
17419e69c935SLars-Peter Clausen  */
17429e69c935SLars-Peter Clausen static void iio_buffer_release(struct kref *ref)
17439e69c935SLars-Peter Clausen {
17449e69c935SLars-Peter Clausen 	struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
17459e69c935SLars-Peter Clausen 
17469e69c935SLars-Peter Clausen 	buffer->access->release(buffer);
17479e69c935SLars-Peter Clausen }
17489e69c935SLars-Peter Clausen 
17499e69c935SLars-Peter Clausen /**
17509e69c935SLars-Peter Clausen  * iio_buffer_get() - Grab a reference to the buffer
17519e69c935SLars-Peter Clausen  * @buffer: The buffer to grab a reference for, may be NULL
17529e69c935SLars-Peter Clausen  *
17539e69c935SLars-Peter Clausen  * Returns the pointer to the buffer that was passed into the function.
17549e69c935SLars-Peter Clausen  */
17559e69c935SLars-Peter Clausen struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
17569e69c935SLars-Peter Clausen {
17579e69c935SLars-Peter Clausen 	if (buffer)
17589e69c935SLars-Peter Clausen 		kref_get(&buffer->ref);
17599e69c935SLars-Peter Clausen 
17609e69c935SLars-Peter Clausen 	return buffer;
17619e69c935SLars-Peter Clausen }
17629e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_get);
17639e69c935SLars-Peter Clausen 
17649e69c935SLars-Peter Clausen /**
17659e69c935SLars-Peter Clausen  * iio_buffer_put() - Release the reference to the buffer
17669e69c935SLars-Peter Clausen  * @buffer: The buffer to release the reference for, may be NULL
17679e69c935SLars-Peter Clausen  */
17689e69c935SLars-Peter Clausen void iio_buffer_put(struct iio_buffer *buffer)
17699e69c935SLars-Peter Clausen {
17709e69c935SLars-Peter Clausen 	if (buffer)
17719e69c935SLars-Peter Clausen 		kref_put(&buffer->ref, iio_buffer_release);
17729e69c935SLars-Peter Clausen }
17739e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_put);
17742b827ad5SJonathan Cameron 
17752b827ad5SJonathan Cameron /**
17762b827ad5SJonathan Cameron  * iio_device_attach_buffer - Attach a buffer to a IIO device
17772b827ad5SJonathan Cameron  * @indio_dev: The device the buffer should be attached to
17782b827ad5SJonathan Cameron  * @buffer: The buffer to attach to the device
17792b827ad5SJonathan Cameron  *
1780ee708e6bSAlexandru Ardelean  * Return 0 if successful, negative if error.
1781ee708e6bSAlexandru Ardelean  *
17822b827ad5SJonathan Cameron  * This function attaches a buffer to a IIO device. The buffer stays attached to
1783ee708e6bSAlexandru Ardelean  * the device until the device is freed. For legacy reasons, the first attached
1784ee708e6bSAlexandru Ardelean  * buffer will also be assigned to 'indio_dev->buffer'.
1785218bc53dSAlexandru Ardelean  * The array allocated here, will be free'd via the iio_device_detach_buffers()
1786218bc53dSAlexandru Ardelean  * call which is handled by the iio_device_free().
17872b827ad5SJonathan Cameron  */
1788ee708e6bSAlexandru Ardelean int iio_device_attach_buffer(struct iio_dev *indio_dev,
17892b827ad5SJonathan Cameron 			     struct iio_buffer *buffer)
17902b827ad5SJonathan Cameron {
1791ee708e6bSAlexandru Ardelean 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1792ee708e6bSAlexandru Ardelean 	struct iio_buffer **new, **old = iio_dev_opaque->attached_buffers;
1793ee708e6bSAlexandru Ardelean 	unsigned int cnt = iio_dev_opaque->attached_buffers_cnt;
1794ee708e6bSAlexandru Ardelean 
1795ee708e6bSAlexandru Ardelean 	cnt++;
1796ee708e6bSAlexandru Ardelean 
1797ee708e6bSAlexandru Ardelean 	new = krealloc(old, sizeof(*new) * cnt, GFP_KERNEL);
1798ee708e6bSAlexandru Ardelean 	if (!new)
1799ee708e6bSAlexandru Ardelean 		return -ENOMEM;
1800ee708e6bSAlexandru Ardelean 	iio_dev_opaque->attached_buffers = new;
1801ee708e6bSAlexandru Ardelean 
1802ee708e6bSAlexandru Ardelean 	buffer = iio_buffer_get(buffer);
1803ee708e6bSAlexandru Ardelean 
1804ee708e6bSAlexandru Ardelean 	/* first buffer is legacy; attach it to the IIO device directly */
1805ee708e6bSAlexandru Ardelean 	if (!indio_dev->buffer)
1806ee708e6bSAlexandru Ardelean 		indio_dev->buffer = buffer;
1807ee708e6bSAlexandru Ardelean 
1808ee708e6bSAlexandru Ardelean 	iio_dev_opaque->attached_buffers[cnt - 1] = buffer;
1809ee708e6bSAlexandru Ardelean 	iio_dev_opaque->attached_buffers_cnt = cnt;
1810ee708e6bSAlexandru Ardelean 
1811ee708e6bSAlexandru Ardelean 	return 0;
18122b827ad5SJonathan Cameron }
18132b827ad5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_device_attach_buffer);
1814