xref: /openbmc/linux/drivers/iio/industrialio-buffer.c (revision 057ac1acdfc4743f066fcefe359385cad00549eb)
1a980e046SJonathan Cameron /* The industrial I/O core
2a980e046SJonathan Cameron  *
3a980e046SJonathan Cameron  * Copyright (c) 2008 Jonathan Cameron
4a980e046SJonathan Cameron  *
5a980e046SJonathan Cameron  * This program is free software; you can redistribute it and/or modify it
6a980e046SJonathan Cameron  * under the terms of the GNU General Public License version 2 as published by
7a980e046SJonathan Cameron  * the Free Software Foundation.
8a980e046SJonathan Cameron  *
9a980e046SJonathan Cameron  * Handling of buffer allocation / resizing.
10a980e046SJonathan Cameron  *
11a980e046SJonathan Cameron  *
12a980e046SJonathan Cameron  * Things to look at here.
13a980e046SJonathan Cameron  * - Better memory allocation techniques?
14a980e046SJonathan Cameron  * - Alternative access techniques?
15a980e046SJonathan Cameron  */
16a980e046SJonathan Cameron #include <linux/kernel.h>
17a980e046SJonathan Cameron #include <linux/export.h>
18a980e046SJonathan Cameron #include <linux/device.h>
19a980e046SJonathan Cameron #include <linux/fs.h>
20a980e046SJonathan Cameron #include <linux/cdev.h>
21a980e046SJonathan Cameron #include <linux/slab.h>
22a980e046SJonathan Cameron #include <linux/poll.h>
23d2f0a48fSLars-Peter Clausen #include <linux/sched.h>
24a980e046SJonathan Cameron 
25a980e046SJonathan Cameron #include <linux/iio/iio.h>
26a980e046SJonathan Cameron #include "iio_core.h"
27a980e046SJonathan Cameron #include <linux/iio/sysfs.h>
28a980e046SJonathan Cameron #include <linux/iio/buffer.h>
29a980e046SJonathan Cameron 
30a980e046SJonathan Cameron static const char * const iio_endian_prefix[] = {
31a980e046SJonathan Cameron 	[IIO_BE] = "be",
32a980e046SJonathan Cameron 	[IIO_LE] = "le",
33a980e046SJonathan Cameron };
34a980e046SJonathan Cameron 
35705ee2c9SLars-Peter Clausen static bool iio_buffer_is_active(struct iio_buffer *buf)
3684b36ce5SJonathan Cameron {
37705ee2c9SLars-Peter Clausen 	return !list_empty(&buf->buffer_list);
3884b36ce5SJonathan Cameron }
3984b36ce5SJonathan Cameron 
4037d34556SJosselin Costanzi static size_t iio_buffer_data_available(struct iio_buffer *buf)
41647cc7b9SLars-Peter Clausen {
42647cc7b9SLars-Peter Clausen 	return buf->access->data_available(buf);
43647cc7b9SLars-Peter Clausen }
44647cc7b9SLars-Peter Clausen 
45f4f4673bSOctavian Purdila static int iio_buffer_flush_hwfifo(struct iio_dev *indio_dev,
46f4f4673bSOctavian Purdila 				   struct iio_buffer *buf, size_t required)
4737d34556SJosselin Costanzi {
48f4f4673bSOctavian Purdila 	if (!indio_dev->info->hwfifo_flush_to_buffer)
49f4f4673bSOctavian Purdila 		return -ENODEV;
50f4f4673bSOctavian Purdila 
51f4f4673bSOctavian Purdila 	return indio_dev->info->hwfifo_flush_to_buffer(indio_dev, required);
52f4f4673bSOctavian Purdila }
53f4f4673bSOctavian Purdila 
54f4f4673bSOctavian Purdila static bool iio_buffer_ready(struct iio_dev *indio_dev, struct iio_buffer *buf,
55f4f4673bSOctavian Purdila 			     size_t to_wait, int to_flush)
56f4f4673bSOctavian Purdila {
57f4f4673bSOctavian Purdila 	size_t avail;
58f4f4673bSOctavian Purdila 	int flushed = 0;
59f4f4673bSOctavian Purdila 
6037d34556SJosselin Costanzi 	/* wakeup if the device was unregistered */
6137d34556SJosselin Costanzi 	if (!indio_dev->info)
6237d34556SJosselin Costanzi 		return true;
6337d34556SJosselin Costanzi 
6437d34556SJosselin Costanzi 	/* drain the buffer if it was disabled */
65f4f4673bSOctavian Purdila 	if (!iio_buffer_is_active(buf)) {
6637d34556SJosselin Costanzi 		to_wait = min_t(size_t, to_wait, 1);
67f4f4673bSOctavian Purdila 		to_flush = 0;
68f4f4673bSOctavian Purdila 	}
6937d34556SJosselin Costanzi 
70f4f4673bSOctavian Purdila 	avail = iio_buffer_data_available(buf);
71f4f4673bSOctavian Purdila 
72f4f4673bSOctavian Purdila 	if (avail >= to_wait) {
73f4f4673bSOctavian Purdila 		/* force a flush for non-blocking reads */
74c6f67a1fSOctavian Purdila 		if (!to_wait && avail < to_flush)
75c6f67a1fSOctavian Purdila 			iio_buffer_flush_hwfifo(indio_dev, buf,
76c6f67a1fSOctavian Purdila 						to_flush - avail);
77f4f4673bSOctavian Purdila 		return true;
78f4f4673bSOctavian Purdila 	}
79f4f4673bSOctavian Purdila 
80f4f4673bSOctavian Purdila 	if (to_flush)
81f4f4673bSOctavian Purdila 		flushed = iio_buffer_flush_hwfifo(indio_dev, buf,
82f4f4673bSOctavian Purdila 						  to_wait - avail);
83f4f4673bSOctavian Purdila 	if (flushed <= 0)
84f4f4673bSOctavian Purdila 		return false;
85f4f4673bSOctavian Purdila 
86f4f4673bSOctavian Purdila 	if (avail + flushed >= to_wait)
8737d34556SJosselin Costanzi 		return true;
8837d34556SJosselin Costanzi 
8937d34556SJosselin Costanzi 	return false;
9037d34556SJosselin Costanzi }
9137d34556SJosselin Costanzi 
92a980e046SJonathan Cameron /**
93a980e046SJonathan Cameron  * iio_buffer_read_first_n_outer() - chrdev read for buffer access
940123635aSCristina Opriceana  * @filp:	File structure pointer for the char device
950123635aSCristina Opriceana  * @buf:	Destination buffer for iio buffer read
960123635aSCristina Opriceana  * @n:		First n bytes to read
970123635aSCristina Opriceana  * @f_ps:	Long offset provided by the user as a seek position
98a980e046SJonathan Cameron  *
99a980e046SJonathan Cameron  * This function relies on all buffer implementations having an
100a980e046SJonathan Cameron  * iio_buffer as their first element.
1010123635aSCristina Opriceana  *
1020123635aSCristina Opriceana  * Return: negative values corresponding to error codes or ret != 0
1030123635aSCristina Opriceana  *	   for ending the reading activity
104a980e046SJonathan Cameron  **/
105a980e046SJonathan Cameron ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
106a980e046SJonathan Cameron 				      size_t n, loff_t *f_ps)
107a980e046SJonathan Cameron {
108a980e046SJonathan Cameron 	struct iio_dev *indio_dev = filp->private_data;
109a980e046SJonathan Cameron 	struct iio_buffer *rb = indio_dev->buffer;
110fcf68f3cSBrian Norris 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
11137d34556SJosselin Costanzi 	size_t datum_size;
112c6f67a1fSOctavian Purdila 	size_t to_wait;
1135dba4b14SColin Ian King 	int ret = 0;
114a980e046SJonathan Cameron 
115f18e7a06SLars-Peter Clausen 	if (!indio_dev->info)
116f18e7a06SLars-Peter Clausen 		return -ENODEV;
117f18e7a06SLars-Peter Clausen 
118a980e046SJonathan Cameron 	if (!rb || !rb->access->read_first_n)
119a980e046SJonathan Cameron 		return -EINVAL;
120ee551a10SLars-Peter Clausen 
12137d34556SJosselin Costanzi 	datum_size = rb->bytes_per_datum;
122ee551a10SLars-Peter Clausen 
12337d34556SJosselin Costanzi 	/*
12437d34556SJosselin Costanzi 	 * If datum_size is 0 there will never be anything to read from the
12537d34556SJosselin Costanzi 	 * buffer, so signal end of file now.
12637d34556SJosselin Costanzi 	 */
12737d34556SJosselin Costanzi 	if (!datum_size)
12837d34556SJosselin Costanzi 		return 0;
12937d34556SJosselin Costanzi 
130c6f67a1fSOctavian Purdila 	if (filp->f_flags & O_NONBLOCK)
131c6f67a1fSOctavian Purdila 		to_wait = 0;
132c6f67a1fSOctavian Purdila 	else
133c6f67a1fSOctavian Purdila 		to_wait = min_t(size_t, n / datum_size, rb->watermark);
13437d34556SJosselin Costanzi 
135fcf68f3cSBrian Norris 	add_wait_queue(&rb->pollq, &wait);
13637d34556SJosselin Costanzi 	do {
137fcf68f3cSBrian Norris 		if (!indio_dev->info) {
138fcf68f3cSBrian Norris 			ret = -ENODEV;
139fcf68f3cSBrian Norris 			break;
140fcf68f3cSBrian Norris 		}
14137d34556SJosselin Costanzi 
142fcf68f3cSBrian Norris 		if (!iio_buffer_ready(indio_dev, rb, to_wait, n / datum_size)) {
143fcf68f3cSBrian Norris 			if (signal_pending(current)) {
144fcf68f3cSBrian Norris 				ret = -ERESTARTSYS;
145fcf68f3cSBrian Norris 				break;
146fcf68f3cSBrian Norris 			}
147fcf68f3cSBrian Norris 
148fcf68f3cSBrian Norris 			wait_woken(&wait, TASK_INTERRUPTIBLE,
149fcf68f3cSBrian Norris 				   MAX_SCHEDULE_TIMEOUT);
150fcf68f3cSBrian Norris 			continue;
151fcf68f3cSBrian Norris 		}
152ee551a10SLars-Peter Clausen 
153ee551a10SLars-Peter Clausen 		ret = rb->access->read_first_n(rb, n, buf);
154ee551a10SLars-Peter Clausen 		if (ret == 0 && (filp->f_flags & O_NONBLOCK))
155ee551a10SLars-Peter Clausen 			ret = -EAGAIN;
156ee551a10SLars-Peter Clausen 	} while (ret == 0);
157fcf68f3cSBrian Norris 	remove_wait_queue(&rb->pollq, &wait);
158ee551a10SLars-Peter Clausen 
159ee551a10SLars-Peter Clausen 	return ret;
160a980e046SJonathan Cameron }
161a980e046SJonathan Cameron 
162a980e046SJonathan Cameron /**
163a980e046SJonathan Cameron  * iio_buffer_poll() - poll the buffer to find out if it has data
1640123635aSCristina Opriceana  * @filp:	File structure pointer for device access
1650123635aSCristina Opriceana  * @wait:	Poll table structure pointer for which the driver adds
1660123635aSCristina Opriceana  *		a wait queue
1670123635aSCristina Opriceana  *
1680123635aSCristina Opriceana  * Return: (POLLIN | POLLRDNORM) if data is available for reading
1690123635aSCristina Opriceana  *	   or 0 for other cases
170a980e046SJonathan Cameron  */
171a980e046SJonathan Cameron unsigned int iio_buffer_poll(struct file *filp,
172a980e046SJonathan Cameron 			     struct poll_table_struct *wait)
173a980e046SJonathan Cameron {
174a980e046SJonathan Cameron 	struct iio_dev *indio_dev = filp->private_data;
175a980e046SJonathan Cameron 	struct iio_buffer *rb = indio_dev->buffer;
176a980e046SJonathan Cameron 
177f18e7a06SLars-Peter Clausen 	if (!indio_dev->info)
1781bdc0293SCristina Opriceana 		return 0;
179f18e7a06SLars-Peter Clausen 
180a980e046SJonathan Cameron 	poll_wait(filp, &rb->pollq, wait);
181f4f4673bSOctavian Purdila 	if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0))
182a980e046SJonathan Cameron 		return POLLIN | POLLRDNORM;
183a980e046SJonathan Cameron 	return 0;
184a980e046SJonathan Cameron }
185a980e046SJonathan Cameron 
186d2f0a48fSLars-Peter Clausen /**
187d2f0a48fSLars-Peter Clausen  * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue
188d2f0a48fSLars-Peter Clausen  * @indio_dev: The IIO device
189d2f0a48fSLars-Peter Clausen  *
190d2f0a48fSLars-Peter Clausen  * Wakes up the event waitqueue used for poll(). Should usually
191d2f0a48fSLars-Peter Clausen  * be called when the device is unregistered.
192d2f0a48fSLars-Peter Clausen  */
193d2f0a48fSLars-Peter Clausen void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
194d2f0a48fSLars-Peter Clausen {
195d2f0a48fSLars-Peter Clausen 	if (!indio_dev->buffer)
196d2f0a48fSLars-Peter Clausen 		return;
197d2f0a48fSLars-Peter Clausen 
198d2f0a48fSLars-Peter Clausen 	wake_up(&indio_dev->buffer->pollq);
199d2f0a48fSLars-Peter Clausen }
200d2f0a48fSLars-Peter Clausen 
201a980e046SJonathan Cameron void iio_buffer_init(struct iio_buffer *buffer)
202a980e046SJonathan Cameron {
203a980e046SJonathan Cameron 	INIT_LIST_HEAD(&buffer->demux_list);
204705ee2c9SLars-Peter Clausen 	INIT_LIST_HEAD(&buffer->buffer_list);
205a980e046SJonathan Cameron 	init_waitqueue_head(&buffer->pollq);
2069e69c935SLars-Peter Clausen 	kref_init(&buffer->ref);
2074a605357SLars-Peter Clausen 	if (!buffer->watermark)
20837d34556SJosselin Costanzi 		buffer->watermark = 1;
209a980e046SJonathan Cameron }
210a980e046SJonathan Cameron EXPORT_SYMBOL(iio_buffer_init);
211a980e046SJonathan Cameron 
212a980e046SJonathan Cameron static ssize_t iio_show_scan_index(struct device *dev,
213a980e046SJonathan Cameron 				   struct device_attribute *attr,
214a980e046SJonathan Cameron 				   char *buf)
215a980e046SJonathan Cameron {
216a980e046SJonathan Cameron 	return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
217a980e046SJonathan Cameron }
218a980e046SJonathan Cameron 
219a980e046SJonathan Cameron static ssize_t iio_show_fixed_type(struct device *dev,
220a980e046SJonathan Cameron 				   struct device_attribute *attr,
221a980e046SJonathan Cameron 				   char *buf)
222a980e046SJonathan Cameron {
223a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
224a980e046SJonathan Cameron 	u8 type = this_attr->c->scan_type.endianness;
225a980e046SJonathan Cameron 
226a980e046SJonathan Cameron 	if (type == IIO_CPU) {
227a980e046SJonathan Cameron #ifdef __LITTLE_ENDIAN
228a980e046SJonathan Cameron 		type = IIO_LE;
229a980e046SJonathan Cameron #else
230a980e046SJonathan Cameron 		type = IIO_BE;
231a980e046SJonathan Cameron #endif
232a980e046SJonathan Cameron 	}
2330ee8546aSSrinivas Pandruvada 	if (this_attr->c->scan_type.repeat > 1)
2340ee8546aSSrinivas Pandruvada 		return sprintf(buf, "%s:%c%d/%dX%d>>%u\n",
2350ee8546aSSrinivas Pandruvada 		       iio_endian_prefix[type],
2360ee8546aSSrinivas Pandruvada 		       this_attr->c->scan_type.sign,
2370ee8546aSSrinivas Pandruvada 		       this_attr->c->scan_type.realbits,
2380ee8546aSSrinivas Pandruvada 		       this_attr->c->scan_type.storagebits,
2390ee8546aSSrinivas Pandruvada 		       this_attr->c->scan_type.repeat,
2400ee8546aSSrinivas Pandruvada 		       this_attr->c->scan_type.shift);
2410ee8546aSSrinivas Pandruvada 		else
242a980e046SJonathan Cameron 			return sprintf(buf, "%s:%c%d/%d>>%u\n",
243a980e046SJonathan Cameron 		       iio_endian_prefix[type],
244a980e046SJonathan Cameron 		       this_attr->c->scan_type.sign,
245a980e046SJonathan Cameron 		       this_attr->c->scan_type.realbits,
246a980e046SJonathan Cameron 		       this_attr->c->scan_type.storagebits,
247a980e046SJonathan Cameron 		       this_attr->c->scan_type.shift);
248a980e046SJonathan Cameron }
249a980e046SJonathan Cameron 
250a980e046SJonathan Cameron static ssize_t iio_scan_el_show(struct device *dev,
251a980e046SJonathan Cameron 				struct device_attribute *attr,
252a980e046SJonathan Cameron 				char *buf)
253a980e046SJonathan Cameron {
254a980e046SJonathan Cameron 	int ret;
255e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
256a980e046SJonathan Cameron 
2572076a20fSAlec Berg 	/* Ensure ret is 0 or 1. */
2582076a20fSAlec Berg 	ret = !!test_bit(to_iio_dev_attr(attr)->address,
259a980e046SJonathan Cameron 		       indio_dev->buffer->scan_mask);
260a980e046SJonathan Cameron 
261a980e046SJonathan Cameron 	return sprintf(buf, "%d\n", ret);
262a980e046SJonathan Cameron }
263a980e046SJonathan Cameron 
264217a5cf0SLars-Peter Clausen /* Note NULL used as error indicator as it doesn't make sense. */
265217a5cf0SLars-Peter Clausen static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
266217a5cf0SLars-Peter Clausen 					  unsigned int masklength,
2671e1ec286SLars-Peter Clausen 					  const unsigned long *mask,
2681e1ec286SLars-Peter Clausen 					  bool strict)
269217a5cf0SLars-Peter Clausen {
270217a5cf0SLars-Peter Clausen 	if (bitmap_empty(mask, masklength))
271217a5cf0SLars-Peter Clausen 		return NULL;
272217a5cf0SLars-Peter Clausen 	while (*av_masks) {
2731e1ec286SLars-Peter Clausen 		if (strict) {
2741e1ec286SLars-Peter Clausen 			if (bitmap_equal(mask, av_masks, masklength))
2751e1ec286SLars-Peter Clausen 				return av_masks;
2761e1ec286SLars-Peter Clausen 		} else {
277217a5cf0SLars-Peter Clausen 			if (bitmap_subset(mask, av_masks, masklength))
278217a5cf0SLars-Peter Clausen 				return av_masks;
2791e1ec286SLars-Peter Clausen 		}
280217a5cf0SLars-Peter Clausen 		av_masks += BITS_TO_LONGS(masklength);
281217a5cf0SLars-Peter Clausen 	}
282217a5cf0SLars-Peter Clausen 	return NULL;
283217a5cf0SLars-Peter Clausen }
284217a5cf0SLars-Peter Clausen 
285217a5cf0SLars-Peter Clausen static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
286217a5cf0SLars-Peter Clausen 	const unsigned long *mask)
287217a5cf0SLars-Peter Clausen {
288217a5cf0SLars-Peter Clausen 	if (!indio_dev->setup_ops->validate_scan_mask)
289217a5cf0SLars-Peter Clausen 		return true;
290217a5cf0SLars-Peter Clausen 
291217a5cf0SLars-Peter Clausen 	return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
292217a5cf0SLars-Peter Clausen }
293217a5cf0SLars-Peter Clausen 
294217a5cf0SLars-Peter Clausen /**
295217a5cf0SLars-Peter Clausen  * iio_scan_mask_set() - set particular bit in the scan mask
296217a5cf0SLars-Peter Clausen  * @indio_dev: the iio device
297217a5cf0SLars-Peter Clausen  * @buffer: the buffer whose scan mask we are interested in
298217a5cf0SLars-Peter Clausen  * @bit: the bit to be set.
299217a5cf0SLars-Peter Clausen  *
300217a5cf0SLars-Peter Clausen  * Note that at this point we have no way of knowing what other
301217a5cf0SLars-Peter Clausen  * buffers might request, hence this code only verifies that the
302217a5cf0SLars-Peter Clausen  * individual buffers request is plausible.
303217a5cf0SLars-Peter Clausen  */
304217a5cf0SLars-Peter Clausen static int iio_scan_mask_set(struct iio_dev *indio_dev,
305217a5cf0SLars-Peter Clausen 		      struct iio_buffer *buffer, int bit)
306217a5cf0SLars-Peter Clausen {
307217a5cf0SLars-Peter Clausen 	const unsigned long *mask;
308217a5cf0SLars-Peter Clausen 	unsigned long *trialmask;
309217a5cf0SLars-Peter Clausen 
310*057ac1acSMarkus Elfring 	trialmask = kmalloc_array(BITS_TO_LONGS(indio_dev->masklength),
311*057ac1acSMarkus Elfring 				  sizeof(*trialmask),
312217a5cf0SLars-Peter Clausen 				  GFP_KERNEL);
313217a5cf0SLars-Peter Clausen 	if (trialmask == NULL)
314217a5cf0SLars-Peter Clausen 		return -ENOMEM;
315217a5cf0SLars-Peter Clausen 	if (!indio_dev->masklength) {
316231bfe53SDan Carpenter 		WARN(1, "Trying to set scanmask prior to registering buffer\n");
317217a5cf0SLars-Peter Clausen 		goto err_invalid_mask;
318217a5cf0SLars-Peter Clausen 	}
319217a5cf0SLars-Peter Clausen 	bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
320217a5cf0SLars-Peter Clausen 	set_bit(bit, trialmask);
321217a5cf0SLars-Peter Clausen 
322217a5cf0SLars-Peter Clausen 	if (!iio_validate_scan_mask(indio_dev, trialmask))
323217a5cf0SLars-Peter Clausen 		goto err_invalid_mask;
324217a5cf0SLars-Peter Clausen 
325217a5cf0SLars-Peter Clausen 	if (indio_dev->available_scan_masks) {
326217a5cf0SLars-Peter Clausen 		mask = iio_scan_mask_match(indio_dev->available_scan_masks,
327217a5cf0SLars-Peter Clausen 					   indio_dev->masklength,
3281e1ec286SLars-Peter Clausen 					   trialmask, false);
329217a5cf0SLars-Peter Clausen 		if (!mask)
330217a5cf0SLars-Peter Clausen 			goto err_invalid_mask;
331217a5cf0SLars-Peter Clausen 	}
332217a5cf0SLars-Peter Clausen 	bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
333217a5cf0SLars-Peter Clausen 
334217a5cf0SLars-Peter Clausen 	kfree(trialmask);
335217a5cf0SLars-Peter Clausen 
336217a5cf0SLars-Peter Clausen 	return 0;
337217a5cf0SLars-Peter Clausen 
338217a5cf0SLars-Peter Clausen err_invalid_mask:
339217a5cf0SLars-Peter Clausen 	kfree(trialmask);
340217a5cf0SLars-Peter Clausen 	return -EINVAL;
341217a5cf0SLars-Peter Clausen }
342217a5cf0SLars-Peter Clausen 
343a980e046SJonathan Cameron static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
344a980e046SJonathan Cameron {
345a980e046SJonathan Cameron 	clear_bit(bit, buffer->scan_mask);
346a980e046SJonathan Cameron 	return 0;
347a980e046SJonathan Cameron }
348a980e046SJonathan Cameron 
349a980e046SJonathan Cameron static ssize_t iio_scan_el_store(struct device *dev,
350a980e046SJonathan Cameron 				 struct device_attribute *attr,
351a980e046SJonathan Cameron 				 const char *buf,
352a980e046SJonathan Cameron 				 size_t len)
353a980e046SJonathan Cameron {
354a980e046SJonathan Cameron 	int ret;
355a980e046SJonathan Cameron 	bool state;
356e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
357a980e046SJonathan Cameron 	struct iio_buffer *buffer = indio_dev->buffer;
358a980e046SJonathan Cameron 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
359a980e046SJonathan Cameron 
360a980e046SJonathan Cameron 	ret = strtobool(buf, &state);
361a980e046SJonathan Cameron 	if (ret < 0)
362a980e046SJonathan Cameron 		return ret;
363a980e046SJonathan Cameron 	mutex_lock(&indio_dev->mlock);
364705ee2c9SLars-Peter Clausen 	if (iio_buffer_is_active(indio_dev->buffer)) {
365a980e046SJonathan Cameron 		ret = -EBUSY;
366a980e046SJonathan Cameron 		goto error_ret;
367a980e046SJonathan Cameron 	}
368a980e046SJonathan Cameron 	ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
369a980e046SJonathan Cameron 	if (ret < 0)
370a980e046SJonathan Cameron 		goto error_ret;
371a980e046SJonathan Cameron 	if (!state && ret) {
372a980e046SJonathan Cameron 		ret = iio_scan_mask_clear(buffer, this_attr->address);
373a980e046SJonathan Cameron 		if (ret)
374a980e046SJonathan Cameron 			goto error_ret;
375a980e046SJonathan Cameron 	} else if (state && !ret) {
376a980e046SJonathan Cameron 		ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
377a980e046SJonathan Cameron 		if (ret)
378a980e046SJonathan Cameron 			goto error_ret;
379a980e046SJonathan Cameron 	}
380a980e046SJonathan Cameron 
381a980e046SJonathan Cameron error_ret:
382a980e046SJonathan Cameron 	mutex_unlock(&indio_dev->mlock);
383a980e046SJonathan Cameron 
384a980e046SJonathan Cameron 	return ret < 0 ? ret : len;
385a980e046SJonathan Cameron 
386a980e046SJonathan Cameron }
387a980e046SJonathan Cameron 
388a980e046SJonathan Cameron static ssize_t iio_scan_el_ts_show(struct device *dev,
389a980e046SJonathan Cameron 				   struct device_attribute *attr,
390a980e046SJonathan Cameron 				   char *buf)
391a980e046SJonathan Cameron {
392e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
393a980e046SJonathan Cameron 	return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
394a980e046SJonathan Cameron }
395a980e046SJonathan Cameron 
396a980e046SJonathan Cameron static ssize_t iio_scan_el_ts_store(struct device *dev,
397a980e046SJonathan Cameron 				    struct device_attribute *attr,
398a980e046SJonathan Cameron 				    const char *buf,
399a980e046SJonathan Cameron 				    size_t len)
400a980e046SJonathan Cameron {
401a980e046SJonathan Cameron 	int ret;
402e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
403a980e046SJonathan Cameron 	bool state;
404a980e046SJonathan Cameron 
405a980e046SJonathan Cameron 	ret = strtobool(buf, &state);
406a980e046SJonathan Cameron 	if (ret < 0)
407a980e046SJonathan Cameron 		return ret;
408a980e046SJonathan Cameron 
409a980e046SJonathan Cameron 	mutex_lock(&indio_dev->mlock);
410705ee2c9SLars-Peter Clausen 	if (iio_buffer_is_active(indio_dev->buffer)) {
411a980e046SJonathan Cameron 		ret = -EBUSY;
412a980e046SJonathan Cameron 		goto error_ret;
413a980e046SJonathan Cameron 	}
414a980e046SJonathan Cameron 	indio_dev->buffer->scan_timestamp = state;
415a980e046SJonathan Cameron error_ret:
416a980e046SJonathan Cameron 	mutex_unlock(&indio_dev->mlock);
417a980e046SJonathan Cameron 
418a980e046SJonathan Cameron 	return ret ? ret : len;
419a980e046SJonathan Cameron }
420a980e046SJonathan Cameron 
421a980e046SJonathan Cameron static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
422a980e046SJonathan Cameron 					const struct iio_chan_spec *chan)
423a980e046SJonathan Cameron {
424a980e046SJonathan Cameron 	int ret, attrcount = 0;
425a980e046SJonathan Cameron 	struct iio_buffer *buffer = indio_dev->buffer;
426a980e046SJonathan Cameron 
427a980e046SJonathan Cameron 	ret = __iio_add_chan_devattr("index",
428a980e046SJonathan Cameron 				     chan,
429a980e046SJonathan Cameron 				     &iio_show_scan_index,
430a980e046SJonathan Cameron 				     NULL,
431a980e046SJonathan Cameron 				     0,
4323704432fSJonathan Cameron 				     IIO_SEPARATE,
433a980e046SJonathan Cameron 				     &indio_dev->dev,
434a980e046SJonathan Cameron 				     &buffer->scan_el_dev_attr_list);
435a980e046SJonathan Cameron 	if (ret)
43692825ff9SHartmut Knaack 		return ret;
437a980e046SJonathan Cameron 	attrcount++;
438a980e046SJonathan Cameron 	ret = __iio_add_chan_devattr("type",
439a980e046SJonathan Cameron 				     chan,
440a980e046SJonathan Cameron 				     &iio_show_fixed_type,
441a980e046SJonathan Cameron 				     NULL,
442a980e046SJonathan Cameron 				     0,
443a980e046SJonathan Cameron 				     0,
444a980e046SJonathan Cameron 				     &indio_dev->dev,
445a980e046SJonathan Cameron 				     &buffer->scan_el_dev_attr_list);
446a980e046SJonathan Cameron 	if (ret)
44792825ff9SHartmut Knaack 		return ret;
448a980e046SJonathan Cameron 	attrcount++;
449a980e046SJonathan Cameron 	if (chan->type != IIO_TIMESTAMP)
450a980e046SJonathan Cameron 		ret = __iio_add_chan_devattr("en",
451a980e046SJonathan Cameron 					     chan,
452a980e046SJonathan Cameron 					     &iio_scan_el_show,
453a980e046SJonathan Cameron 					     &iio_scan_el_store,
454a980e046SJonathan Cameron 					     chan->scan_index,
455a980e046SJonathan Cameron 					     0,
456a980e046SJonathan Cameron 					     &indio_dev->dev,
457a980e046SJonathan Cameron 					     &buffer->scan_el_dev_attr_list);
458a980e046SJonathan Cameron 	else
459a980e046SJonathan Cameron 		ret = __iio_add_chan_devattr("en",
460a980e046SJonathan Cameron 					     chan,
461a980e046SJonathan Cameron 					     &iio_scan_el_ts_show,
462a980e046SJonathan Cameron 					     &iio_scan_el_ts_store,
463a980e046SJonathan Cameron 					     chan->scan_index,
464a980e046SJonathan Cameron 					     0,
465a980e046SJonathan Cameron 					     &indio_dev->dev,
466a980e046SJonathan Cameron 					     &buffer->scan_el_dev_attr_list);
4679572588cSPeter Meerwald 	if (ret)
46892825ff9SHartmut Knaack 		return ret;
469a980e046SJonathan Cameron 	attrcount++;
470a980e046SJonathan Cameron 	ret = attrcount;
471a980e046SJonathan Cameron 	return ret;
472a980e046SJonathan Cameron }
473a980e046SJonathan Cameron 
47408e7e0adSLars-Peter Clausen static ssize_t iio_buffer_read_length(struct device *dev,
475a980e046SJonathan Cameron 				      struct device_attribute *attr,
476a980e046SJonathan Cameron 				      char *buf)
477a980e046SJonathan Cameron {
478e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
479a980e046SJonathan Cameron 	struct iio_buffer *buffer = indio_dev->buffer;
480a980e046SJonathan Cameron 
48137495660SLars-Peter Clausen 	return sprintf(buf, "%d\n", buffer->length);
482a980e046SJonathan Cameron }
483a980e046SJonathan Cameron 
48408e7e0adSLars-Peter Clausen static ssize_t iio_buffer_write_length(struct device *dev,
485a980e046SJonathan Cameron 				       struct device_attribute *attr,
48608e7e0adSLars-Peter Clausen 				       const char *buf, size_t len)
487a980e046SJonathan Cameron {
488e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
489a980e046SJonathan Cameron 	struct iio_buffer *buffer = indio_dev->buffer;
490948ad205SLars-Peter Clausen 	unsigned int val;
491948ad205SLars-Peter Clausen 	int ret;
492a980e046SJonathan Cameron 
493948ad205SLars-Peter Clausen 	ret = kstrtouint(buf, 10, &val);
494a980e046SJonathan Cameron 	if (ret)
495a980e046SJonathan Cameron 		return ret;
496a980e046SJonathan Cameron 
49737495660SLars-Peter Clausen 	if (val == buffer->length)
498a980e046SJonathan Cameron 		return len;
499a980e046SJonathan Cameron 
500a980e046SJonathan Cameron 	mutex_lock(&indio_dev->mlock);
501705ee2c9SLars-Peter Clausen 	if (iio_buffer_is_active(indio_dev->buffer)) {
502a980e046SJonathan Cameron 		ret = -EBUSY;
503a980e046SJonathan Cameron 	} else {
504a980e046SJonathan Cameron 		buffer->access->set_length(buffer, val);
505a980e046SJonathan Cameron 		ret = 0;
506a980e046SJonathan Cameron 	}
50737d34556SJosselin Costanzi 	if (ret)
50837d34556SJosselin Costanzi 		goto out;
50937d34556SJosselin Costanzi 	if (buffer->length && buffer->length < buffer->watermark)
51037d34556SJosselin Costanzi 		buffer->watermark = buffer->length;
51137d34556SJosselin Costanzi out:
512a980e046SJonathan Cameron 	mutex_unlock(&indio_dev->mlock);
513a980e046SJonathan Cameron 
514a980e046SJonathan Cameron 	return ret ? ret : len;
515a980e046SJonathan Cameron }
516a980e046SJonathan Cameron 
51708e7e0adSLars-Peter Clausen static ssize_t iio_buffer_show_enable(struct device *dev,
518a980e046SJonathan Cameron 				      struct device_attribute *attr,
519a980e046SJonathan Cameron 				      char *buf)
520a980e046SJonathan Cameron {
521e53f5ac5SLars-Peter Clausen 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
522705ee2c9SLars-Peter Clausen 	return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
523a980e046SJonathan Cameron }
524a980e046SJonathan Cameron 
525182b4905SLars-Peter Clausen static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev,
526182b4905SLars-Peter Clausen 					     unsigned int scan_index)
527182b4905SLars-Peter Clausen {
528182b4905SLars-Peter Clausen 	const struct iio_chan_spec *ch;
529182b4905SLars-Peter Clausen 	unsigned int bytes;
530182b4905SLars-Peter Clausen 
531182b4905SLars-Peter Clausen 	ch = iio_find_channel_from_si(indio_dev, scan_index);
532182b4905SLars-Peter Clausen 	bytes = ch->scan_type.storagebits / 8;
533182b4905SLars-Peter Clausen 	if (ch->scan_type.repeat > 1)
534182b4905SLars-Peter Clausen 		bytes *= ch->scan_type.repeat;
535182b4905SLars-Peter Clausen 	return bytes;
536182b4905SLars-Peter Clausen }
537182b4905SLars-Peter Clausen 
538182b4905SLars-Peter Clausen static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev)
539182b4905SLars-Peter Clausen {
540182b4905SLars-Peter Clausen 	return iio_storage_bytes_for_si(indio_dev,
541182b4905SLars-Peter Clausen 					indio_dev->scan_index_timestamp);
542182b4905SLars-Peter Clausen }
543182b4905SLars-Peter Clausen 
544183f4173SPeter Meerwald static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
545183f4173SPeter Meerwald 				const unsigned long *mask, bool timestamp)
546a980e046SJonathan Cameron {
547a980e046SJonathan Cameron 	unsigned bytes = 0;
548a980e046SJonathan Cameron 	int length, i;
549a980e046SJonathan Cameron 
550a980e046SJonathan Cameron 	/* How much space will the demuxed element take? */
551a980e046SJonathan Cameron 	for_each_set_bit(i, mask,
552a980e046SJonathan Cameron 			 indio_dev->masklength) {
553182b4905SLars-Peter Clausen 		length = iio_storage_bytes_for_si(indio_dev, i);
554a980e046SJonathan Cameron 		bytes = ALIGN(bytes, length);
555a980e046SJonathan Cameron 		bytes += length;
556a980e046SJonathan Cameron 	}
557182b4905SLars-Peter Clausen 
558a980e046SJonathan Cameron 	if (timestamp) {
559182b4905SLars-Peter Clausen 		length = iio_storage_bytes_for_timestamp(indio_dev);
560a980e046SJonathan Cameron 		bytes = ALIGN(bytes, length);
561a980e046SJonathan Cameron 		bytes += length;
562a980e046SJonathan Cameron 	}
563a980e046SJonathan Cameron 	return bytes;
564a980e046SJonathan Cameron }
565a980e046SJonathan Cameron 
5669e69c935SLars-Peter Clausen static void iio_buffer_activate(struct iio_dev *indio_dev,
5679e69c935SLars-Peter Clausen 	struct iio_buffer *buffer)
5689e69c935SLars-Peter Clausen {
5699e69c935SLars-Peter Clausen 	iio_buffer_get(buffer);
5709e69c935SLars-Peter Clausen 	list_add(&buffer->buffer_list, &indio_dev->buffer_list);
5719e69c935SLars-Peter Clausen }
5729e69c935SLars-Peter Clausen 
5739e69c935SLars-Peter Clausen static void iio_buffer_deactivate(struct iio_buffer *buffer)
5749e69c935SLars-Peter Clausen {
5759e69c935SLars-Peter Clausen 	list_del_init(&buffer->buffer_list);
57637d34556SJosselin Costanzi 	wake_up_interruptible(&buffer->pollq);
5779e69c935SLars-Peter Clausen 	iio_buffer_put(buffer);
5789e69c935SLars-Peter Clausen }
5799e69c935SLars-Peter Clausen 
5801250186aSLars-Peter Clausen static void iio_buffer_deactivate_all(struct iio_dev *indio_dev)
5811250186aSLars-Peter Clausen {
5821250186aSLars-Peter Clausen 	struct iio_buffer *buffer, *_buffer;
5831250186aSLars-Peter Clausen 
5841250186aSLars-Peter Clausen 	list_for_each_entry_safe(buffer, _buffer,
5851250186aSLars-Peter Clausen 			&indio_dev->buffer_list, buffer_list)
5861250186aSLars-Peter Clausen 		iio_buffer_deactivate(buffer);
5871250186aSLars-Peter Clausen }
5881250186aSLars-Peter Clausen 
589e18a2ad4SLars-Peter Clausen static int iio_buffer_enable(struct iio_buffer *buffer,
590e18a2ad4SLars-Peter Clausen 	struct iio_dev *indio_dev)
591e18a2ad4SLars-Peter Clausen {
592e18a2ad4SLars-Peter Clausen 	if (!buffer->access->enable)
593e18a2ad4SLars-Peter Clausen 		return 0;
594e18a2ad4SLars-Peter Clausen 	return buffer->access->enable(buffer, indio_dev);
595e18a2ad4SLars-Peter Clausen }
596e18a2ad4SLars-Peter Clausen 
597e18a2ad4SLars-Peter Clausen static int iio_buffer_disable(struct iio_buffer *buffer,
598e18a2ad4SLars-Peter Clausen 	struct iio_dev *indio_dev)
599e18a2ad4SLars-Peter Clausen {
600e18a2ad4SLars-Peter Clausen 	if (!buffer->access->disable)
601e18a2ad4SLars-Peter Clausen 		return 0;
602e18a2ad4SLars-Peter Clausen 	return buffer->access->disable(buffer, indio_dev);
603e18a2ad4SLars-Peter Clausen }
604e18a2ad4SLars-Peter Clausen 
6058e050996SLars-Peter Clausen static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
6068e050996SLars-Peter Clausen 	struct iio_buffer *buffer)
6078e050996SLars-Peter Clausen {
6088e050996SLars-Peter Clausen 	unsigned int bytes;
6098e050996SLars-Peter Clausen 
6108e050996SLars-Peter Clausen 	if (!buffer->access->set_bytes_per_datum)
6118e050996SLars-Peter Clausen 		return;
6128e050996SLars-Peter Clausen 
6138e050996SLars-Peter Clausen 	bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
6148e050996SLars-Peter Clausen 		buffer->scan_timestamp);
6158e050996SLars-Peter Clausen 
6168e050996SLars-Peter Clausen 	buffer->access->set_bytes_per_datum(buffer, bytes);
6178e050996SLars-Peter Clausen }
6188e050996SLars-Peter Clausen 
619fcc1b2f5SLars-Peter Clausen static int iio_buffer_request_update(struct iio_dev *indio_dev,
620fcc1b2f5SLars-Peter Clausen 	struct iio_buffer *buffer)
621fcc1b2f5SLars-Peter Clausen {
622fcc1b2f5SLars-Peter Clausen 	int ret;
623fcc1b2f5SLars-Peter Clausen 
624fcc1b2f5SLars-Peter Clausen 	iio_buffer_update_bytes_per_datum(indio_dev, buffer);
625fcc1b2f5SLars-Peter Clausen 	if (buffer->access->request_update) {
626fcc1b2f5SLars-Peter Clausen 		ret = buffer->access->request_update(buffer);
627fcc1b2f5SLars-Peter Clausen 		if (ret) {
628fcc1b2f5SLars-Peter Clausen 			dev_dbg(&indio_dev->dev,
629fcc1b2f5SLars-Peter Clausen 			       "Buffer not started: buffer parameter update failed (%d)\n",
630fcc1b2f5SLars-Peter Clausen 				ret);
631fcc1b2f5SLars-Peter Clausen 			return ret;
632fcc1b2f5SLars-Peter Clausen 		}
633fcc1b2f5SLars-Peter Clausen 	}
634fcc1b2f5SLars-Peter Clausen 
635fcc1b2f5SLars-Peter Clausen 	return 0;
636fcc1b2f5SLars-Peter Clausen }
637fcc1b2f5SLars-Peter Clausen 
638248be5aaSLars-Peter Clausen static void iio_free_scan_mask(struct iio_dev *indio_dev,
639248be5aaSLars-Peter Clausen 	const unsigned long *mask)
640248be5aaSLars-Peter Clausen {
641248be5aaSLars-Peter Clausen 	/* If the mask is dynamically allocated free it, otherwise do nothing */
642248be5aaSLars-Peter Clausen 	if (!indio_dev->available_scan_masks)
643248be5aaSLars-Peter Clausen 		kfree(mask);
644248be5aaSLars-Peter Clausen }
645248be5aaSLars-Peter Clausen 
6466e509c4dSLars-Peter Clausen struct iio_device_config {
6476e509c4dSLars-Peter Clausen 	unsigned int mode;
648f0566c0cSLars-Peter Clausen 	unsigned int watermark;
6496e509c4dSLars-Peter Clausen 	const unsigned long *scan_mask;
6506e509c4dSLars-Peter Clausen 	unsigned int scan_bytes;
6516e509c4dSLars-Peter Clausen 	bool scan_timestamp;
6526e509c4dSLars-Peter Clausen };
6536e509c4dSLars-Peter Clausen 
6546e509c4dSLars-Peter Clausen static int iio_verify_update(struct iio_dev *indio_dev,
6556e509c4dSLars-Peter Clausen 	struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer,
6566e509c4dSLars-Peter Clausen 	struct iio_device_config *config)
6576e509c4dSLars-Peter Clausen {
6586e509c4dSLars-Peter Clausen 	unsigned long *compound_mask;
6596e509c4dSLars-Peter Clausen 	const unsigned long *scan_mask;
6601e1ec286SLars-Peter Clausen 	bool strict_scanmask = false;
6616e509c4dSLars-Peter Clausen 	struct iio_buffer *buffer;
6626e509c4dSLars-Peter Clausen 	bool scan_timestamp;
663225d59adSLars-Peter Clausen 	unsigned int modes;
6646e509c4dSLars-Peter Clausen 
6656e509c4dSLars-Peter Clausen 	memset(config, 0, sizeof(*config));
6661bef2c1dSIrina Tirdea 	config->watermark = ~0;
6676e509c4dSLars-Peter Clausen 
6686e509c4dSLars-Peter Clausen 	/*
6696e509c4dSLars-Peter Clausen 	 * If there is just one buffer and we are removing it there is nothing
6706e509c4dSLars-Peter Clausen 	 * to verify.
6716e509c4dSLars-Peter Clausen 	 */
6726e509c4dSLars-Peter Clausen 	if (remove_buffer && !insert_buffer &&
6736e509c4dSLars-Peter Clausen 		list_is_singular(&indio_dev->buffer_list))
6746e509c4dSLars-Peter Clausen 			return 0;
6756e509c4dSLars-Peter Clausen 
676225d59adSLars-Peter Clausen 	modes = indio_dev->modes;
677225d59adSLars-Peter Clausen 
678225d59adSLars-Peter Clausen 	list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
679225d59adSLars-Peter Clausen 		if (buffer == remove_buffer)
680225d59adSLars-Peter Clausen 			continue;
681225d59adSLars-Peter Clausen 		modes &= buffer->access->modes;
682f0566c0cSLars-Peter Clausen 		config->watermark = min(config->watermark, buffer->watermark);
683225d59adSLars-Peter Clausen 	}
684225d59adSLars-Peter Clausen 
685f0566c0cSLars-Peter Clausen 	if (insert_buffer) {
686225d59adSLars-Peter Clausen 		modes &= insert_buffer->access->modes;
687f0566c0cSLars-Peter Clausen 		config->watermark = min(config->watermark,
688f0566c0cSLars-Peter Clausen 			insert_buffer->watermark);
689f0566c0cSLars-Peter Clausen 	}
690225d59adSLars-Peter Clausen 
6916e509c4dSLars-Peter Clausen 	/* Definitely possible for devices to support both of these. */
692225d59adSLars-Peter Clausen 	if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) {
6936e509c4dSLars-Peter Clausen 		config->mode = INDIO_BUFFER_TRIGGERED;
694225d59adSLars-Peter Clausen 	} else if (modes & INDIO_BUFFER_HARDWARE) {
6951e1ec286SLars-Peter Clausen 		/*
6961e1ec286SLars-Peter Clausen 		 * Keep things simple for now and only allow a single buffer to
6971e1ec286SLars-Peter Clausen 		 * be connected in hardware mode.
6981e1ec286SLars-Peter Clausen 		 */
6991e1ec286SLars-Peter Clausen 		if (insert_buffer && !list_empty(&indio_dev->buffer_list))
7001e1ec286SLars-Peter Clausen 			return -EINVAL;
7016e509c4dSLars-Peter Clausen 		config->mode = INDIO_BUFFER_HARDWARE;
7021e1ec286SLars-Peter Clausen 		strict_scanmask = true;
703225d59adSLars-Peter Clausen 	} else if (modes & INDIO_BUFFER_SOFTWARE) {
7046e509c4dSLars-Peter Clausen 		config->mode = INDIO_BUFFER_SOFTWARE;
7056e509c4dSLars-Peter Clausen 	} else {
7066e509c4dSLars-Peter Clausen 		/* Can only occur on first buffer */
7076e509c4dSLars-Peter Clausen 		if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
7086e509c4dSLars-Peter Clausen 			dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n");
7096e509c4dSLars-Peter Clausen 		return -EINVAL;
7106e509c4dSLars-Peter Clausen 	}
7116e509c4dSLars-Peter Clausen 
7126e509c4dSLars-Peter Clausen 	/* What scan mask do we actually have? */
7136e509c4dSLars-Peter Clausen 	compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
7146e509c4dSLars-Peter Clausen 				sizeof(long), GFP_KERNEL);
7156e509c4dSLars-Peter Clausen 	if (compound_mask == NULL)
7166e509c4dSLars-Peter Clausen 		return -ENOMEM;
7176e509c4dSLars-Peter Clausen 
7186e509c4dSLars-Peter Clausen 	scan_timestamp = false;
7196e509c4dSLars-Peter Clausen 
7206e509c4dSLars-Peter Clausen 	list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
7216e509c4dSLars-Peter Clausen 		if (buffer == remove_buffer)
7226e509c4dSLars-Peter Clausen 			continue;
7236e509c4dSLars-Peter Clausen 		bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
7246e509c4dSLars-Peter Clausen 			  indio_dev->masklength);
7256e509c4dSLars-Peter Clausen 		scan_timestamp |= buffer->scan_timestamp;
7266e509c4dSLars-Peter Clausen 	}
7276e509c4dSLars-Peter Clausen 
7286e509c4dSLars-Peter Clausen 	if (insert_buffer) {
7296e509c4dSLars-Peter Clausen 		bitmap_or(compound_mask, compound_mask,
7306e509c4dSLars-Peter Clausen 			  insert_buffer->scan_mask, indio_dev->masklength);
7316e509c4dSLars-Peter Clausen 		scan_timestamp |= insert_buffer->scan_timestamp;
7326e509c4dSLars-Peter Clausen 	}
7336e509c4dSLars-Peter Clausen 
7346e509c4dSLars-Peter Clausen 	if (indio_dev->available_scan_masks) {
7356e509c4dSLars-Peter Clausen 		scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks,
7366e509c4dSLars-Peter Clausen 				    indio_dev->masklength,
7371e1ec286SLars-Peter Clausen 				    compound_mask,
7381e1ec286SLars-Peter Clausen 				    strict_scanmask);
7396e509c4dSLars-Peter Clausen 		kfree(compound_mask);
7406e509c4dSLars-Peter Clausen 		if (scan_mask == NULL)
7416e509c4dSLars-Peter Clausen 			return -EINVAL;
7426e509c4dSLars-Peter Clausen 	} else {
7436e509c4dSLars-Peter Clausen 	    scan_mask = compound_mask;
7446e509c4dSLars-Peter Clausen 	}
7456e509c4dSLars-Peter Clausen 
7466e509c4dSLars-Peter Clausen 	config->scan_bytes = iio_compute_scan_bytes(indio_dev,
7476e509c4dSLars-Peter Clausen 				    scan_mask, scan_timestamp);
7486e509c4dSLars-Peter Clausen 	config->scan_mask = scan_mask;
7496e509c4dSLars-Peter Clausen 	config->scan_timestamp = scan_timestamp;
7506e509c4dSLars-Peter Clausen 
7516e509c4dSLars-Peter Clausen 	return 0;
7526e509c4dSLars-Peter Clausen }
7536e509c4dSLars-Peter Clausen 
754623d74e3SLars-Peter Clausen static int iio_enable_buffers(struct iio_dev *indio_dev,
755623d74e3SLars-Peter Clausen 	struct iio_device_config *config)
756623d74e3SLars-Peter Clausen {
757e18a2ad4SLars-Peter Clausen 	struct iio_buffer *buffer;
758623d74e3SLars-Peter Clausen 	int ret;
759623d74e3SLars-Peter Clausen 
760623d74e3SLars-Peter Clausen 	indio_dev->active_scan_mask = config->scan_mask;
761623d74e3SLars-Peter Clausen 	indio_dev->scan_timestamp = config->scan_timestamp;
762623d74e3SLars-Peter Clausen 	indio_dev->scan_bytes = config->scan_bytes;
763623d74e3SLars-Peter Clausen 
764623d74e3SLars-Peter Clausen 	iio_update_demux(indio_dev);
765623d74e3SLars-Peter Clausen 
766623d74e3SLars-Peter Clausen 	/* Wind up again */
767623d74e3SLars-Peter Clausen 	if (indio_dev->setup_ops->preenable) {
768623d74e3SLars-Peter Clausen 		ret = indio_dev->setup_ops->preenable(indio_dev);
769623d74e3SLars-Peter Clausen 		if (ret) {
770623d74e3SLars-Peter Clausen 			dev_dbg(&indio_dev->dev,
771623d74e3SLars-Peter Clausen 			       "Buffer not started: buffer preenable failed (%d)\n", ret);
772623d74e3SLars-Peter Clausen 			goto err_undo_config;
773623d74e3SLars-Peter Clausen 		}
774623d74e3SLars-Peter Clausen 	}
775623d74e3SLars-Peter Clausen 
776623d74e3SLars-Peter Clausen 	if (indio_dev->info->update_scan_mode) {
777623d74e3SLars-Peter Clausen 		ret = indio_dev->info
778623d74e3SLars-Peter Clausen 			->update_scan_mode(indio_dev,
779623d74e3SLars-Peter Clausen 					   indio_dev->active_scan_mask);
780623d74e3SLars-Peter Clausen 		if (ret < 0) {
781623d74e3SLars-Peter Clausen 			dev_dbg(&indio_dev->dev,
782623d74e3SLars-Peter Clausen 				"Buffer not started: update scan mode failed (%d)\n",
783623d74e3SLars-Peter Clausen 				ret);
784623d74e3SLars-Peter Clausen 			goto err_run_postdisable;
785623d74e3SLars-Peter Clausen 		}
786623d74e3SLars-Peter Clausen 	}
787623d74e3SLars-Peter Clausen 
788f0566c0cSLars-Peter Clausen 	if (indio_dev->info->hwfifo_set_watermark)
789f0566c0cSLars-Peter Clausen 		indio_dev->info->hwfifo_set_watermark(indio_dev,
790f0566c0cSLars-Peter Clausen 			config->watermark);
791f0566c0cSLars-Peter Clausen 
792e18a2ad4SLars-Peter Clausen 	list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
793e18a2ad4SLars-Peter Clausen 		ret = iio_buffer_enable(buffer, indio_dev);
794e18a2ad4SLars-Peter Clausen 		if (ret)
795e18a2ad4SLars-Peter Clausen 			goto err_disable_buffers;
796e18a2ad4SLars-Peter Clausen 	}
797e18a2ad4SLars-Peter Clausen 
798623d74e3SLars-Peter Clausen 	indio_dev->currentmode = config->mode;
799623d74e3SLars-Peter Clausen 
800623d74e3SLars-Peter Clausen 	if (indio_dev->setup_ops->postenable) {
801623d74e3SLars-Peter Clausen 		ret = indio_dev->setup_ops->postenable(indio_dev);
802623d74e3SLars-Peter Clausen 		if (ret) {
803623d74e3SLars-Peter Clausen 			dev_dbg(&indio_dev->dev,
804623d74e3SLars-Peter Clausen 			       "Buffer not started: postenable failed (%d)\n", ret);
805e18a2ad4SLars-Peter Clausen 			goto err_disable_buffers;
806623d74e3SLars-Peter Clausen 		}
807623d74e3SLars-Peter Clausen 	}
808623d74e3SLars-Peter Clausen 
809623d74e3SLars-Peter Clausen 	return 0;
810623d74e3SLars-Peter Clausen 
811e18a2ad4SLars-Peter Clausen err_disable_buffers:
812e18a2ad4SLars-Peter Clausen 	list_for_each_entry_continue_reverse(buffer, &indio_dev->buffer_list,
813e18a2ad4SLars-Peter Clausen 					     buffer_list)
814e18a2ad4SLars-Peter Clausen 		iio_buffer_disable(buffer, indio_dev);
815623d74e3SLars-Peter Clausen err_run_postdisable:
816623d74e3SLars-Peter Clausen 	indio_dev->currentmode = INDIO_DIRECT_MODE;
817623d74e3SLars-Peter Clausen 	if (indio_dev->setup_ops->postdisable)
818623d74e3SLars-Peter Clausen 		indio_dev->setup_ops->postdisable(indio_dev);
819623d74e3SLars-Peter Clausen err_undo_config:
820623d74e3SLars-Peter Clausen 	indio_dev->active_scan_mask = NULL;
821623d74e3SLars-Peter Clausen 
822623d74e3SLars-Peter Clausen 	return ret;
823623d74e3SLars-Peter Clausen }
824623d74e3SLars-Peter Clausen 
825623d74e3SLars-Peter Clausen static int iio_disable_buffers(struct iio_dev *indio_dev)
826623d74e3SLars-Peter Clausen {
827e18a2ad4SLars-Peter Clausen 	struct iio_buffer *buffer;
8281250186aSLars-Peter Clausen 	int ret = 0;
8291250186aSLars-Peter Clausen 	int ret2;
830623d74e3SLars-Peter Clausen 
831623d74e3SLars-Peter Clausen 	/* Wind down existing buffers - iff there are any */
832623d74e3SLars-Peter Clausen 	if (list_empty(&indio_dev->buffer_list))
833623d74e3SLars-Peter Clausen 		return 0;
834623d74e3SLars-Peter Clausen 
8351250186aSLars-Peter Clausen 	/*
8361250186aSLars-Peter Clausen 	 * If things go wrong at some step in disable we still need to continue
8371250186aSLars-Peter Clausen 	 * to perform the other steps, otherwise we leave the device in a
8381250186aSLars-Peter Clausen 	 * inconsistent state. We return the error code for the first error we
8391250186aSLars-Peter Clausen 	 * encountered.
8401250186aSLars-Peter Clausen 	 */
8411250186aSLars-Peter Clausen 
842623d74e3SLars-Peter Clausen 	if (indio_dev->setup_ops->predisable) {
8431250186aSLars-Peter Clausen 		ret2 = indio_dev->setup_ops->predisable(indio_dev);
8441250186aSLars-Peter Clausen 		if (ret2 && !ret)
8451250186aSLars-Peter Clausen 			ret = ret2;
846623d74e3SLars-Peter Clausen 	}
847623d74e3SLars-Peter Clausen 
848e18a2ad4SLars-Peter Clausen 	list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
849e18a2ad4SLars-Peter Clausen 		ret2 = iio_buffer_disable(buffer, indio_dev);
850e18a2ad4SLars-Peter Clausen 		if (ret2 && !ret)
851e18a2ad4SLars-Peter Clausen 			ret = ret2;
852e18a2ad4SLars-Peter Clausen 	}
853e18a2ad4SLars-Peter Clausen 
854623d74e3SLars-Peter Clausen 	indio_dev->currentmode = INDIO_DIRECT_MODE;
855623d74e3SLars-Peter Clausen 
856623d74e3SLars-Peter Clausen 	if (indio_dev->setup_ops->postdisable) {
8571250186aSLars-Peter Clausen 		ret2 = indio_dev->setup_ops->postdisable(indio_dev);
8581250186aSLars-Peter Clausen 		if (ret2 && !ret)
8591250186aSLars-Peter Clausen 			ret = ret2;
860623d74e3SLars-Peter Clausen 	}
861623d74e3SLars-Peter Clausen 
8621250186aSLars-Peter Clausen 	iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask);
8631250186aSLars-Peter Clausen 	indio_dev->active_scan_mask = NULL;
8641250186aSLars-Peter Clausen 
8651250186aSLars-Peter Clausen 	return ret;
866623d74e3SLars-Peter Clausen }
867623d74e3SLars-Peter Clausen 
868a9519456SLars-Peter Clausen static int __iio_update_buffers(struct iio_dev *indio_dev,
86984b36ce5SJonathan Cameron 		       struct iio_buffer *insert_buffer,
87084b36ce5SJonathan Cameron 		       struct iio_buffer *remove_buffer)
871a980e046SJonathan Cameron {
8726e509c4dSLars-Peter Clausen 	struct iio_device_config new_config;
8731250186aSLars-Peter Clausen 	int ret;
8746e509c4dSLars-Peter Clausen 
8756e509c4dSLars-Peter Clausen 	ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer,
8766e509c4dSLars-Peter Clausen 		&new_config);
8776e509c4dSLars-Peter Clausen 	if (ret)
8786e509c4dSLars-Peter Clausen 		return ret;
879a980e046SJonathan Cameron 
880fcc1b2f5SLars-Peter Clausen 	if (insert_buffer) {
881fcc1b2f5SLars-Peter Clausen 		ret = iio_buffer_request_update(indio_dev, insert_buffer);
882fcc1b2f5SLars-Peter Clausen 		if (ret)
8836e509c4dSLars-Peter Clausen 			goto err_free_config;
884fcc1b2f5SLars-Peter Clausen 	}
885fcc1b2f5SLars-Peter Clausen 
886623d74e3SLars-Peter Clausen 	ret = iio_disable_buffers(indio_dev);
8871250186aSLars-Peter Clausen 	if (ret)
8881250186aSLars-Peter Clausen 		goto err_deactivate_all;
889623d74e3SLars-Peter Clausen 
89084b36ce5SJonathan Cameron 	if (remove_buffer)
8919e69c935SLars-Peter Clausen 		iio_buffer_deactivate(remove_buffer);
89284b36ce5SJonathan Cameron 	if (insert_buffer)
8939e69c935SLars-Peter Clausen 		iio_buffer_activate(indio_dev, insert_buffer);
89484b36ce5SJonathan Cameron 
89584b36ce5SJonathan Cameron 	/* If no buffers in list, we are done */
8961250186aSLars-Peter Clausen 	if (list_empty(&indio_dev->buffer_list))
89784b36ce5SJonathan Cameron 		return 0;
898a980e046SJonathan Cameron 
899623d74e3SLars-Peter Clausen 	ret = iio_enable_buffers(indio_dev, &new_config);
9001250186aSLars-Peter Clausen 	if (ret)
9011250186aSLars-Peter Clausen 		goto err_deactivate_all;
902623d74e3SLars-Peter Clausen 
903623d74e3SLars-Peter Clausen 	return 0;
9046e509c4dSLars-Peter Clausen 
9051250186aSLars-Peter Clausen err_deactivate_all:
9061250186aSLars-Peter Clausen 	/*
9071250186aSLars-Peter Clausen 	 * We've already verified that the config is valid earlier. If things go
9081250186aSLars-Peter Clausen 	 * wrong in either enable or disable the most likely reason is an IO
9091250186aSLars-Peter Clausen 	 * error from the device. In this case there is no good recovery
9101250186aSLars-Peter Clausen 	 * strategy. Just make sure to disable everything and leave the device
9111250186aSLars-Peter Clausen 	 * in a sane state.  With a bit of luck the device might come back to
9121250186aSLars-Peter Clausen 	 * life again later and userspace can try again.
9131250186aSLars-Peter Clausen 	 */
9141250186aSLars-Peter Clausen 	iio_buffer_deactivate_all(indio_dev);
9151250186aSLars-Peter Clausen 
9166e509c4dSLars-Peter Clausen err_free_config:
9176e509c4dSLars-Peter Clausen 	iio_free_scan_mask(indio_dev, new_config.scan_mask);
9186e509c4dSLars-Peter Clausen 	return ret;
91984b36ce5SJonathan Cameron }
920a9519456SLars-Peter Clausen 
921a9519456SLars-Peter Clausen int iio_update_buffers(struct iio_dev *indio_dev,
922a9519456SLars-Peter Clausen 		       struct iio_buffer *insert_buffer,
923a9519456SLars-Peter Clausen 		       struct iio_buffer *remove_buffer)
924a9519456SLars-Peter Clausen {
925a9519456SLars-Peter Clausen 	int ret;
926a9519456SLars-Peter Clausen 
9273909fab5SLars-Peter Clausen 	if (insert_buffer == remove_buffer)
9283909fab5SLars-Peter Clausen 		return 0;
9293909fab5SLars-Peter Clausen 
930a9519456SLars-Peter Clausen 	mutex_lock(&indio_dev->info_exist_lock);
931a9519456SLars-Peter Clausen 	mutex_lock(&indio_dev->mlock);
932a9519456SLars-Peter Clausen 
9333909fab5SLars-Peter Clausen 	if (insert_buffer && iio_buffer_is_active(insert_buffer))
9343909fab5SLars-Peter Clausen 		insert_buffer = NULL;
9353909fab5SLars-Peter Clausen 
9363909fab5SLars-Peter Clausen 	if (remove_buffer && !iio_buffer_is_active(remove_buffer))
9373909fab5SLars-Peter Clausen 		remove_buffer = NULL;
9383909fab5SLars-Peter Clausen 
9393909fab5SLars-Peter Clausen 	if (!insert_buffer && !remove_buffer) {
9403909fab5SLars-Peter Clausen 		ret = 0;
9413909fab5SLars-Peter Clausen 		goto out_unlock;
9423909fab5SLars-Peter Clausen 	}
9433909fab5SLars-Peter Clausen 
944a9519456SLars-Peter Clausen 	if (indio_dev->info == NULL) {
945a9519456SLars-Peter Clausen 		ret = -ENODEV;
946a9519456SLars-Peter Clausen 		goto out_unlock;
947a9519456SLars-Peter Clausen 	}
948a9519456SLars-Peter Clausen 
949a9519456SLars-Peter Clausen 	ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
950a9519456SLars-Peter Clausen 
951a9519456SLars-Peter Clausen out_unlock:
952a9519456SLars-Peter Clausen 	mutex_unlock(&indio_dev->mlock);
953a9519456SLars-Peter Clausen 	mutex_unlock(&indio_dev->info_exist_lock);
954a9519456SLars-Peter Clausen 
955a9519456SLars-Peter Clausen 	return ret;
956a9519456SLars-Peter Clausen }
95784b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_update_buffers);
95884b36ce5SJonathan Cameron 
959623d74e3SLars-Peter Clausen void iio_disable_all_buffers(struct iio_dev *indio_dev)
960623d74e3SLars-Peter Clausen {
961623d74e3SLars-Peter Clausen 	iio_disable_buffers(indio_dev);
9621250186aSLars-Peter Clausen 	iio_buffer_deactivate_all(indio_dev);
963623d74e3SLars-Peter Clausen }
964623d74e3SLars-Peter Clausen 
96508e7e0adSLars-Peter Clausen static ssize_t iio_buffer_store_enable(struct device *dev,
96684b36ce5SJonathan Cameron 				       struct device_attribute *attr,
96784b36ce5SJonathan Cameron 				       const char *buf,
96884b36ce5SJonathan Cameron 				       size_t len)
96984b36ce5SJonathan Cameron {
97084b36ce5SJonathan Cameron 	int ret;
97184b36ce5SJonathan Cameron 	bool requested_state;
97284b36ce5SJonathan Cameron 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
97384b36ce5SJonathan Cameron 	bool inlist;
97484b36ce5SJonathan Cameron 
97584b36ce5SJonathan Cameron 	ret = strtobool(buf, &requested_state);
97684b36ce5SJonathan Cameron 	if (ret < 0)
97784b36ce5SJonathan Cameron 		return ret;
97884b36ce5SJonathan Cameron 
97984b36ce5SJonathan Cameron 	mutex_lock(&indio_dev->mlock);
98084b36ce5SJonathan Cameron 
98184b36ce5SJonathan Cameron 	/* Find out if it is in the list */
982705ee2c9SLars-Peter Clausen 	inlist = iio_buffer_is_active(indio_dev->buffer);
98384b36ce5SJonathan Cameron 	/* Already in desired state */
98484b36ce5SJonathan Cameron 	if (inlist == requested_state)
98584b36ce5SJonathan Cameron 		goto done;
98684b36ce5SJonathan Cameron 
98784b36ce5SJonathan Cameron 	if (requested_state)
988a9519456SLars-Peter Clausen 		ret = __iio_update_buffers(indio_dev,
98984b36ce5SJonathan Cameron 					 indio_dev->buffer, NULL);
99084b36ce5SJonathan Cameron 	else
991a9519456SLars-Peter Clausen 		ret = __iio_update_buffers(indio_dev,
99284b36ce5SJonathan Cameron 					 NULL, indio_dev->buffer);
99384b36ce5SJonathan Cameron 
99484b36ce5SJonathan Cameron done:
99584b36ce5SJonathan Cameron 	mutex_unlock(&indio_dev->mlock);
99684b36ce5SJonathan Cameron 	return (ret < 0) ? ret : len;
99784b36ce5SJonathan Cameron }
99884b36ce5SJonathan Cameron 
999d967cb6bSLars-Peter Clausen static const char * const iio_scan_elements_group_name = "scan_elements";
1000d967cb6bSLars-Peter Clausen 
100137d34556SJosselin Costanzi static ssize_t iio_buffer_show_watermark(struct device *dev,
100237d34556SJosselin Costanzi 					 struct device_attribute *attr,
100337d34556SJosselin Costanzi 					 char *buf)
100437d34556SJosselin Costanzi {
100537d34556SJosselin Costanzi 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
100637d34556SJosselin Costanzi 	struct iio_buffer *buffer = indio_dev->buffer;
100737d34556SJosselin Costanzi 
100837d34556SJosselin Costanzi 	return sprintf(buf, "%u\n", buffer->watermark);
100937d34556SJosselin Costanzi }
101037d34556SJosselin Costanzi 
101137d34556SJosselin Costanzi static ssize_t iio_buffer_store_watermark(struct device *dev,
101237d34556SJosselin Costanzi 					  struct device_attribute *attr,
101337d34556SJosselin Costanzi 					  const char *buf,
101437d34556SJosselin Costanzi 					  size_t len)
101537d34556SJosselin Costanzi {
101637d34556SJosselin Costanzi 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
101737d34556SJosselin Costanzi 	struct iio_buffer *buffer = indio_dev->buffer;
101837d34556SJosselin Costanzi 	unsigned int val;
101937d34556SJosselin Costanzi 	int ret;
102037d34556SJosselin Costanzi 
102137d34556SJosselin Costanzi 	ret = kstrtouint(buf, 10, &val);
102237d34556SJosselin Costanzi 	if (ret)
102337d34556SJosselin Costanzi 		return ret;
102437d34556SJosselin Costanzi 	if (!val)
102537d34556SJosselin Costanzi 		return -EINVAL;
102637d34556SJosselin Costanzi 
102737d34556SJosselin Costanzi 	mutex_lock(&indio_dev->mlock);
102837d34556SJosselin Costanzi 
102937d34556SJosselin Costanzi 	if (val > buffer->length) {
103037d34556SJosselin Costanzi 		ret = -EINVAL;
103137d34556SJosselin Costanzi 		goto out;
103237d34556SJosselin Costanzi 	}
103337d34556SJosselin Costanzi 
103437d34556SJosselin Costanzi 	if (iio_buffer_is_active(indio_dev->buffer)) {
103537d34556SJosselin Costanzi 		ret = -EBUSY;
103637d34556SJosselin Costanzi 		goto out;
103737d34556SJosselin Costanzi 	}
103837d34556SJosselin Costanzi 
103937d34556SJosselin Costanzi 	buffer->watermark = val;
104037d34556SJosselin Costanzi out:
104137d34556SJosselin Costanzi 	mutex_unlock(&indio_dev->mlock);
104237d34556SJosselin Costanzi 
104337d34556SJosselin Costanzi 	return ret ? ret : len;
104437d34556SJosselin Costanzi }
104537d34556SJosselin Costanzi 
104608e7e0adSLars-Peter Clausen static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
104708e7e0adSLars-Peter Clausen 		   iio_buffer_write_length);
10488d92db28SLars-Peter Clausen static struct device_attribute dev_attr_length_ro = __ATTR(length,
10498d92db28SLars-Peter Clausen 	S_IRUGO, iio_buffer_read_length, NULL);
105008e7e0adSLars-Peter Clausen static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
105108e7e0adSLars-Peter Clausen 		   iio_buffer_show_enable, iio_buffer_store_enable);
105237d34556SJosselin Costanzi static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR,
105337d34556SJosselin Costanzi 		   iio_buffer_show_watermark, iio_buffer_store_watermark);
1054b440655bSLars-Peter Clausen static struct device_attribute dev_attr_watermark_ro = __ATTR(watermark,
1055b440655bSLars-Peter Clausen 	S_IRUGO, iio_buffer_show_watermark, NULL);
105608e7e0adSLars-Peter Clausen 
10576da9b382SOctavian Purdila static struct attribute *iio_buffer_attrs[] = {
10586da9b382SOctavian Purdila 	&dev_attr_length.attr,
10596da9b382SOctavian Purdila 	&dev_attr_enable.attr,
106037d34556SJosselin Costanzi 	&dev_attr_watermark.attr,
10616da9b382SOctavian Purdila };
10626da9b382SOctavian Purdila 
1063d967cb6bSLars-Peter Clausen int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
1064d967cb6bSLars-Peter Clausen {
1065d967cb6bSLars-Peter Clausen 	struct iio_dev_attr *p;
1066d967cb6bSLars-Peter Clausen 	struct attribute **attr;
1067d967cb6bSLars-Peter Clausen 	struct iio_buffer *buffer = indio_dev->buffer;
1068d967cb6bSLars-Peter Clausen 	int ret, i, attrn, attrcount, attrcount_orig = 0;
1069d967cb6bSLars-Peter Clausen 	const struct iio_chan_spec *channels;
1070d967cb6bSLars-Peter Clausen 
1071629bc023SLars-Peter Clausen 	channels = indio_dev->channels;
1072629bc023SLars-Peter Clausen 	if (channels) {
1073629bc023SLars-Peter Clausen 		int ml = indio_dev->masklength;
1074629bc023SLars-Peter Clausen 
1075629bc023SLars-Peter Clausen 		for (i = 0; i < indio_dev->num_channels; i++)
1076629bc023SLars-Peter Clausen 			ml = max(ml, channels[i].scan_index + 1);
1077629bc023SLars-Peter Clausen 		indio_dev->masklength = ml;
1078629bc023SLars-Peter Clausen 	}
1079629bc023SLars-Peter Clausen 
1080d967cb6bSLars-Peter Clausen 	if (!buffer)
1081d967cb6bSLars-Peter Clausen 		return 0;
1082d967cb6bSLars-Peter Clausen 
108308e7e0adSLars-Peter Clausen 	attrcount = 0;
108408e7e0adSLars-Peter Clausen 	if (buffer->attrs) {
108508e7e0adSLars-Peter Clausen 		while (buffer->attrs[attrcount] != NULL)
108608e7e0adSLars-Peter Clausen 			attrcount++;
108708e7e0adSLars-Peter Clausen 	}
108808e7e0adSLars-Peter Clausen 
10896da9b382SOctavian Purdila 	attr = kcalloc(attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1,
10906da9b382SOctavian Purdila 		       sizeof(struct attribute *), GFP_KERNEL);
10916da9b382SOctavian Purdila 	if (!attr)
109208e7e0adSLars-Peter Clausen 		return -ENOMEM;
109308e7e0adSLars-Peter Clausen 
10946da9b382SOctavian Purdila 	memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
10956da9b382SOctavian Purdila 	if (!buffer->access->set_length)
10966da9b382SOctavian Purdila 		attr[0] = &dev_attr_length_ro.attr;
10976da9b382SOctavian Purdila 
1098b440655bSLars-Peter Clausen 	if (buffer->access->flags & INDIO_BUFFER_FLAG_FIXED_WATERMARK)
1099b440655bSLars-Peter Clausen 		attr[2] = &dev_attr_watermark_ro.attr;
1100b440655bSLars-Peter Clausen 
110108e7e0adSLars-Peter Clausen 	if (buffer->attrs)
11026da9b382SOctavian Purdila 		memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs,
11036da9b382SOctavian Purdila 		       sizeof(struct attribute *) * attrcount);
11046da9b382SOctavian Purdila 
11056da9b382SOctavian Purdila 	attr[attrcount + ARRAY_SIZE(iio_buffer_attrs)] = NULL;
11066da9b382SOctavian Purdila 
11076da9b382SOctavian Purdila 	buffer->buffer_group.name = "buffer";
11086da9b382SOctavian Purdila 	buffer->buffer_group.attrs = attr;
110908e7e0adSLars-Peter Clausen 
111008e7e0adSLars-Peter Clausen 	indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group;
111108e7e0adSLars-Peter Clausen 
1112d967cb6bSLars-Peter Clausen 	if (buffer->scan_el_attrs != NULL) {
1113d967cb6bSLars-Peter Clausen 		attr = buffer->scan_el_attrs->attrs;
1114d967cb6bSLars-Peter Clausen 		while (*attr++ != NULL)
1115d967cb6bSLars-Peter Clausen 			attrcount_orig++;
1116d967cb6bSLars-Peter Clausen 	}
1117d967cb6bSLars-Peter Clausen 	attrcount = attrcount_orig;
1118d967cb6bSLars-Peter Clausen 	INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
1119d967cb6bSLars-Peter Clausen 	channels = indio_dev->channels;
1120d967cb6bSLars-Peter Clausen 	if (channels) {
1121d967cb6bSLars-Peter Clausen 		/* new magic */
1122d967cb6bSLars-Peter Clausen 		for (i = 0; i < indio_dev->num_channels; i++) {
1123d967cb6bSLars-Peter Clausen 			if (channels[i].scan_index < 0)
1124d967cb6bSLars-Peter Clausen 				continue;
1125d967cb6bSLars-Peter Clausen 
1126d967cb6bSLars-Peter Clausen 			ret = iio_buffer_add_channel_sysfs(indio_dev,
1127d967cb6bSLars-Peter Clausen 							 &channels[i]);
1128d967cb6bSLars-Peter Clausen 			if (ret < 0)
1129d967cb6bSLars-Peter Clausen 				goto error_cleanup_dynamic;
1130d967cb6bSLars-Peter Clausen 			attrcount += ret;
1131d967cb6bSLars-Peter Clausen 			if (channels[i].type == IIO_TIMESTAMP)
1132d967cb6bSLars-Peter Clausen 				indio_dev->scan_index_timestamp =
1133d967cb6bSLars-Peter Clausen 					channels[i].scan_index;
1134d967cb6bSLars-Peter Clausen 		}
1135d967cb6bSLars-Peter Clausen 		if (indio_dev->masklength && buffer->scan_mask == NULL) {
1136d967cb6bSLars-Peter Clausen 			buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
1137d967cb6bSLars-Peter Clausen 						    sizeof(*buffer->scan_mask),
1138d967cb6bSLars-Peter Clausen 						    GFP_KERNEL);
1139d967cb6bSLars-Peter Clausen 			if (buffer->scan_mask == NULL) {
1140d967cb6bSLars-Peter Clausen 				ret = -ENOMEM;
1141d967cb6bSLars-Peter Clausen 				goto error_cleanup_dynamic;
1142d967cb6bSLars-Peter Clausen 			}
1143d967cb6bSLars-Peter Clausen 		}
1144d967cb6bSLars-Peter Clausen 	}
1145d967cb6bSLars-Peter Clausen 
1146d967cb6bSLars-Peter Clausen 	buffer->scan_el_group.name = iio_scan_elements_group_name;
1147d967cb6bSLars-Peter Clausen 
1148d967cb6bSLars-Peter Clausen 	buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
1149d967cb6bSLars-Peter Clausen 					      sizeof(buffer->scan_el_group.attrs[0]),
1150d967cb6bSLars-Peter Clausen 					      GFP_KERNEL);
1151d967cb6bSLars-Peter Clausen 	if (buffer->scan_el_group.attrs == NULL) {
1152d967cb6bSLars-Peter Clausen 		ret = -ENOMEM;
1153d967cb6bSLars-Peter Clausen 		goto error_free_scan_mask;
1154d967cb6bSLars-Peter Clausen 	}
1155d967cb6bSLars-Peter Clausen 	if (buffer->scan_el_attrs)
1156d967cb6bSLars-Peter Clausen 		memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
1157d967cb6bSLars-Peter Clausen 		       sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
1158d967cb6bSLars-Peter Clausen 	attrn = attrcount_orig;
1159d967cb6bSLars-Peter Clausen 
1160d967cb6bSLars-Peter Clausen 	list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
1161d967cb6bSLars-Peter Clausen 		buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
1162d967cb6bSLars-Peter Clausen 	indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
1163d967cb6bSLars-Peter Clausen 
1164d967cb6bSLars-Peter Clausen 	return 0;
1165d967cb6bSLars-Peter Clausen 
1166d967cb6bSLars-Peter Clausen error_free_scan_mask:
1167d967cb6bSLars-Peter Clausen 	kfree(buffer->scan_mask);
1168d967cb6bSLars-Peter Clausen error_cleanup_dynamic:
1169d967cb6bSLars-Peter Clausen 	iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
117008e7e0adSLars-Peter Clausen 	kfree(indio_dev->buffer->buffer_group.attrs);
1171d967cb6bSLars-Peter Clausen 
1172d967cb6bSLars-Peter Clausen 	return ret;
1173d967cb6bSLars-Peter Clausen }
1174d967cb6bSLars-Peter Clausen 
1175d967cb6bSLars-Peter Clausen void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
1176d967cb6bSLars-Peter Clausen {
1177d967cb6bSLars-Peter Clausen 	if (!indio_dev->buffer)
1178d967cb6bSLars-Peter Clausen 		return;
1179d967cb6bSLars-Peter Clausen 
1180d967cb6bSLars-Peter Clausen 	kfree(indio_dev->buffer->scan_mask);
118108e7e0adSLars-Peter Clausen 	kfree(indio_dev->buffer->buffer_group.attrs);
1182d967cb6bSLars-Peter Clausen 	kfree(indio_dev->buffer->scan_el_group.attrs);
1183d967cb6bSLars-Peter Clausen 	iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
1184d967cb6bSLars-Peter Clausen }
1185d967cb6bSLars-Peter Clausen 
1186a980e046SJonathan Cameron /**
118781636632SLars-Peter Clausen  * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
118881636632SLars-Peter Clausen  * @indio_dev: the iio device
118981636632SLars-Peter Clausen  * @mask: scan mask to be checked
119081636632SLars-Peter Clausen  *
119181636632SLars-Peter Clausen  * Return true if exactly one bit is set in the scan mask, false otherwise. It
119281636632SLars-Peter Clausen  * can be used for devices where only one channel can be active for sampling at
119381636632SLars-Peter Clausen  * a time.
119481636632SLars-Peter Clausen  */
119581636632SLars-Peter Clausen bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
119681636632SLars-Peter Clausen 	const unsigned long *mask)
119781636632SLars-Peter Clausen {
119881636632SLars-Peter Clausen 	return bitmap_weight(mask, indio_dev->masklength) == 1;
119981636632SLars-Peter Clausen }
120081636632SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
120181636632SLars-Peter Clausen 
1202a980e046SJonathan Cameron int iio_scan_mask_query(struct iio_dev *indio_dev,
1203a980e046SJonathan Cameron 			struct iio_buffer *buffer, int bit)
1204a980e046SJonathan Cameron {
1205a980e046SJonathan Cameron 	if (bit > indio_dev->masklength)
1206a980e046SJonathan Cameron 		return -EINVAL;
1207a980e046SJonathan Cameron 
1208a980e046SJonathan Cameron 	if (!buffer->scan_mask)
1209a980e046SJonathan Cameron 		return 0;
1210a980e046SJonathan Cameron 
12112076a20fSAlec Berg 	/* Ensure return value is 0 or 1. */
12122076a20fSAlec Berg 	return !!test_bit(bit, buffer->scan_mask);
1213a980e046SJonathan Cameron };
1214a980e046SJonathan Cameron EXPORT_SYMBOL_GPL(iio_scan_mask_query);
1215a980e046SJonathan Cameron 
1216a980e046SJonathan Cameron /**
12170123635aSCristina Opriceana  * struct iio_demux_table - table describing demux memcpy ops
1218a980e046SJonathan Cameron  * @from:	index to copy from
1219a980e046SJonathan Cameron  * @to:		index to copy to
1220a980e046SJonathan Cameron  * @length:	how many bytes to copy
1221a980e046SJonathan Cameron  * @l:		list head used for management
1222a980e046SJonathan Cameron  */
1223a980e046SJonathan Cameron struct iio_demux_table {
1224a980e046SJonathan Cameron 	unsigned from;
1225a980e046SJonathan Cameron 	unsigned to;
1226a980e046SJonathan Cameron 	unsigned length;
1227a980e046SJonathan Cameron 	struct list_head l;
1228a980e046SJonathan Cameron };
1229a980e046SJonathan Cameron 
12305d65d920SLars-Peter Clausen static const void *iio_demux(struct iio_buffer *buffer,
12315d65d920SLars-Peter Clausen 				 const void *datain)
1232a980e046SJonathan Cameron {
1233a980e046SJonathan Cameron 	struct iio_demux_table *t;
1234a980e046SJonathan Cameron 
1235a980e046SJonathan Cameron 	if (list_empty(&buffer->demux_list))
1236a980e046SJonathan Cameron 		return datain;
1237a980e046SJonathan Cameron 	list_for_each_entry(t, &buffer->demux_list, l)
1238a980e046SJonathan Cameron 		memcpy(buffer->demux_bounce + t->to,
1239a980e046SJonathan Cameron 		       datain + t->from, t->length);
1240a980e046SJonathan Cameron 
1241a980e046SJonathan Cameron 	return buffer->demux_bounce;
1242a980e046SJonathan Cameron }
1243a980e046SJonathan Cameron 
12445d65d920SLars-Peter Clausen static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
1245a980e046SJonathan Cameron {
12465d65d920SLars-Peter Clausen 	const void *dataout = iio_demux(buffer, data);
124737d34556SJosselin Costanzi 	int ret;
1248a980e046SJonathan Cameron 
124937d34556SJosselin Costanzi 	ret = buffer->access->store_to(buffer, dataout);
125037d34556SJosselin Costanzi 	if (ret)
125137d34556SJosselin Costanzi 		return ret;
125237d34556SJosselin Costanzi 
125337d34556SJosselin Costanzi 	/*
125437d34556SJosselin Costanzi 	 * We can't just test for watermark to decide if we wake the poll queue
125537d34556SJosselin Costanzi 	 * because read may request less samples than the watermark.
125637d34556SJosselin Costanzi 	 */
125737d34556SJosselin Costanzi 	wake_up_interruptible_poll(&buffer->pollq, POLLIN | POLLRDNORM);
125837d34556SJosselin Costanzi 	return 0;
1259a980e046SJonathan Cameron }
1260a980e046SJonathan Cameron 
1261a980e046SJonathan Cameron static void iio_buffer_demux_free(struct iio_buffer *buffer)
1262a980e046SJonathan Cameron {
1263a980e046SJonathan Cameron 	struct iio_demux_table *p, *q;
1264a980e046SJonathan Cameron 	list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
1265a980e046SJonathan Cameron 		list_del(&p->l);
1266a980e046SJonathan Cameron 		kfree(p);
1267a980e046SJonathan Cameron 	}
1268a980e046SJonathan Cameron }
1269a980e046SJonathan Cameron 
127084b36ce5SJonathan Cameron 
12715d65d920SLars-Peter Clausen int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
127284b36ce5SJonathan Cameron {
127384b36ce5SJonathan Cameron 	int ret;
127484b36ce5SJonathan Cameron 	struct iio_buffer *buf;
127584b36ce5SJonathan Cameron 
127684b36ce5SJonathan Cameron 	list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
127784b36ce5SJonathan Cameron 		ret = iio_push_to_buffer(buf, data);
127884b36ce5SJonathan Cameron 		if (ret < 0)
127984b36ce5SJonathan Cameron 			return ret;
128084b36ce5SJonathan Cameron 	}
128184b36ce5SJonathan Cameron 
128284b36ce5SJonathan Cameron 	return 0;
128384b36ce5SJonathan Cameron }
128484b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_push_to_buffers);
128584b36ce5SJonathan Cameron 
1286cbe88bccSLars-Peter Clausen static int iio_buffer_add_demux(struct iio_buffer *buffer,
1287cbe88bccSLars-Peter Clausen 	struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc,
1288cbe88bccSLars-Peter Clausen 	unsigned int length)
1289cbe88bccSLars-Peter Clausen {
1290cbe88bccSLars-Peter Clausen 
1291cbe88bccSLars-Peter Clausen 	if (*p && (*p)->from + (*p)->length == in_loc &&
1292cbe88bccSLars-Peter Clausen 		(*p)->to + (*p)->length == out_loc) {
1293cbe88bccSLars-Peter Clausen 		(*p)->length += length;
1294cbe88bccSLars-Peter Clausen 	} else {
12957cdca178SJonathan Cameron 		*p = kmalloc(sizeof(**p), GFP_KERNEL);
1296cbe88bccSLars-Peter Clausen 		if (*p == NULL)
1297cbe88bccSLars-Peter Clausen 			return -ENOMEM;
1298cbe88bccSLars-Peter Clausen 		(*p)->from = in_loc;
1299cbe88bccSLars-Peter Clausen 		(*p)->to = out_loc;
1300cbe88bccSLars-Peter Clausen 		(*p)->length = length;
1301cbe88bccSLars-Peter Clausen 		list_add_tail(&(*p)->l, &buffer->demux_list);
1302cbe88bccSLars-Peter Clausen 	}
1303cbe88bccSLars-Peter Clausen 
1304cbe88bccSLars-Peter Clausen 	return 0;
1305cbe88bccSLars-Peter Clausen }
1306cbe88bccSLars-Peter Clausen 
130784b36ce5SJonathan Cameron static int iio_buffer_update_demux(struct iio_dev *indio_dev,
130884b36ce5SJonathan Cameron 				   struct iio_buffer *buffer)
1309a980e046SJonathan Cameron {
1310a980e046SJonathan Cameron 	int ret, in_ind = -1, out_ind, length;
1311a980e046SJonathan Cameron 	unsigned in_loc = 0, out_loc = 0;
1312cbe88bccSLars-Peter Clausen 	struct iio_demux_table *p = NULL;
1313a980e046SJonathan Cameron 
1314a980e046SJonathan Cameron 	/* Clear out any old demux */
1315a980e046SJonathan Cameron 	iio_buffer_demux_free(buffer);
1316a980e046SJonathan Cameron 	kfree(buffer->demux_bounce);
1317a980e046SJonathan Cameron 	buffer->demux_bounce = NULL;
1318a980e046SJonathan Cameron 
1319a980e046SJonathan Cameron 	/* First work out which scan mode we will actually have */
1320a980e046SJonathan Cameron 	if (bitmap_equal(indio_dev->active_scan_mask,
1321a980e046SJonathan Cameron 			 buffer->scan_mask,
1322a980e046SJonathan Cameron 			 indio_dev->masklength))
1323a980e046SJonathan Cameron 		return 0;
1324a980e046SJonathan Cameron 
1325a980e046SJonathan Cameron 	/* Now we have the two masks, work from least sig and build up sizes */
1326a980e046SJonathan Cameron 	for_each_set_bit(out_ind,
132761bd55ceSLars-Peter Clausen 			 buffer->scan_mask,
1328a980e046SJonathan Cameron 			 indio_dev->masklength) {
1329a980e046SJonathan Cameron 		in_ind = find_next_bit(indio_dev->active_scan_mask,
1330a980e046SJonathan Cameron 				       indio_dev->masklength,
1331a980e046SJonathan Cameron 				       in_ind + 1);
1332a980e046SJonathan Cameron 		while (in_ind != out_ind) {
1333a980e046SJonathan Cameron 			in_ind = find_next_bit(indio_dev->active_scan_mask,
1334a980e046SJonathan Cameron 					       indio_dev->masklength,
1335a980e046SJonathan Cameron 					       in_ind + 1);
1336182b4905SLars-Peter Clausen 			length = iio_storage_bytes_for_si(indio_dev, in_ind);
1337a980e046SJonathan Cameron 			/* Make sure we are aligned */
133861072dbcSLars-Peter Clausen 			in_loc = roundup(in_loc, length) + length;
1339a980e046SJonathan Cameron 		}
1340182b4905SLars-Peter Clausen 		length = iio_storage_bytes_for_si(indio_dev, in_ind);
134161072dbcSLars-Peter Clausen 		out_loc = roundup(out_loc, length);
134261072dbcSLars-Peter Clausen 		in_loc = roundup(in_loc, length);
1343cbe88bccSLars-Peter Clausen 		ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
1344cbe88bccSLars-Peter Clausen 		if (ret)
1345cbe88bccSLars-Peter Clausen 			goto error_clear_mux_table;
1346a980e046SJonathan Cameron 		out_loc += length;
1347a980e046SJonathan Cameron 		in_loc += length;
1348a980e046SJonathan Cameron 	}
1349a980e046SJonathan Cameron 	/* Relies on scan_timestamp being last */
1350a980e046SJonathan Cameron 	if (buffer->scan_timestamp) {
1351182b4905SLars-Peter Clausen 		length = iio_storage_bytes_for_timestamp(indio_dev);
135261072dbcSLars-Peter Clausen 		out_loc = roundup(out_loc, length);
135361072dbcSLars-Peter Clausen 		in_loc = roundup(in_loc, length);
1354cbe88bccSLars-Peter Clausen 		ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
1355cbe88bccSLars-Peter Clausen 		if (ret)
1356cbe88bccSLars-Peter Clausen 			goto error_clear_mux_table;
1357a980e046SJonathan Cameron 		out_loc += length;
1358a980e046SJonathan Cameron 		in_loc += length;
1359a980e046SJonathan Cameron 	}
1360a980e046SJonathan Cameron 	buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
1361a980e046SJonathan Cameron 	if (buffer->demux_bounce == NULL) {
1362a980e046SJonathan Cameron 		ret = -ENOMEM;
1363a980e046SJonathan Cameron 		goto error_clear_mux_table;
1364a980e046SJonathan Cameron 	}
1365a980e046SJonathan Cameron 	return 0;
1366a980e046SJonathan Cameron 
1367a980e046SJonathan Cameron error_clear_mux_table:
1368a980e046SJonathan Cameron 	iio_buffer_demux_free(buffer);
1369a980e046SJonathan Cameron 
1370a980e046SJonathan Cameron 	return ret;
1371a980e046SJonathan Cameron }
137284b36ce5SJonathan Cameron 
137384b36ce5SJonathan Cameron int iio_update_demux(struct iio_dev *indio_dev)
137484b36ce5SJonathan Cameron {
137584b36ce5SJonathan Cameron 	struct iio_buffer *buffer;
137684b36ce5SJonathan Cameron 	int ret;
137784b36ce5SJonathan Cameron 
137884b36ce5SJonathan Cameron 	list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
137984b36ce5SJonathan Cameron 		ret = iio_buffer_update_demux(indio_dev, buffer);
138084b36ce5SJonathan Cameron 		if (ret < 0)
138184b36ce5SJonathan Cameron 			goto error_clear_mux_table;
138284b36ce5SJonathan Cameron 	}
138384b36ce5SJonathan Cameron 	return 0;
138484b36ce5SJonathan Cameron 
138584b36ce5SJonathan Cameron error_clear_mux_table:
138684b36ce5SJonathan Cameron 	list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
138784b36ce5SJonathan Cameron 		iio_buffer_demux_free(buffer);
138884b36ce5SJonathan Cameron 
138984b36ce5SJonathan Cameron 	return ret;
139084b36ce5SJonathan Cameron }
1391a980e046SJonathan Cameron EXPORT_SYMBOL_GPL(iio_update_demux);
13929e69c935SLars-Peter Clausen 
13939e69c935SLars-Peter Clausen /**
13949e69c935SLars-Peter Clausen  * iio_buffer_release() - Free a buffer's resources
13959e69c935SLars-Peter Clausen  * @ref: Pointer to the kref embedded in the iio_buffer struct
13969e69c935SLars-Peter Clausen  *
13979e69c935SLars-Peter Clausen  * This function is called when the last reference to the buffer has been
13989e69c935SLars-Peter Clausen  * dropped. It will typically free all resources allocated by the buffer. Do not
13999e69c935SLars-Peter Clausen  * call this function manually, always use iio_buffer_put() when done using a
14009e69c935SLars-Peter Clausen  * buffer.
14019e69c935SLars-Peter Clausen  */
14029e69c935SLars-Peter Clausen static void iio_buffer_release(struct kref *ref)
14039e69c935SLars-Peter Clausen {
14049e69c935SLars-Peter Clausen 	struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
14059e69c935SLars-Peter Clausen 
14069e69c935SLars-Peter Clausen 	buffer->access->release(buffer);
14079e69c935SLars-Peter Clausen }
14089e69c935SLars-Peter Clausen 
14099e69c935SLars-Peter Clausen /**
14109e69c935SLars-Peter Clausen  * iio_buffer_get() - Grab a reference to the buffer
14119e69c935SLars-Peter Clausen  * @buffer: The buffer to grab a reference for, may be NULL
14129e69c935SLars-Peter Clausen  *
14139e69c935SLars-Peter Clausen  * Returns the pointer to the buffer that was passed into the function.
14149e69c935SLars-Peter Clausen  */
14159e69c935SLars-Peter Clausen struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
14169e69c935SLars-Peter Clausen {
14179e69c935SLars-Peter Clausen 	if (buffer)
14189e69c935SLars-Peter Clausen 		kref_get(&buffer->ref);
14199e69c935SLars-Peter Clausen 
14209e69c935SLars-Peter Clausen 	return buffer;
14219e69c935SLars-Peter Clausen }
14229e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_get);
14239e69c935SLars-Peter Clausen 
14249e69c935SLars-Peter Clausen /**
14259e69c935SLars-Peter Clausen  * iio_buffer_put() - Release the reference to the buffer
14269e69c935SLars-Peter Clausen  * @buffer: The buffer to release the reference for, may be NULL
14279e69c935SLars-Peter Clausen  */
14289e69c935SLars-Peter Clausen void iio_buffer_put(struct iio_buffer *buffer)
14299e69c935SLars-Peter Clausen {
14309e69c935SLars-Peter Clausen 	if (buffer)
14319e69c935SLars-Peter Clausen 		kref_put(&buffer->ref, iio_buffer_release);
14329e69c935SLars-Peter Clausen }
14339e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_put);
1434