1 // SPDX-License-Identifier: GPL-2.0-only
2 /* The industrial I/O core
4 * Copyright (c) 2008 Jonathan Cameron
6 * Handling of buffer allocation / resizing.
8 * Things to look at here.
9 * - Better memory allocation techniques?
10 * - Alternative access techniques?
12 #include <linux/anon_inodes.h>
13 #include <linux/kernel.h>
14 #include <linux/export.h>
15 #include <linux/device.h>
16 #include <linux/file.h>
18 #include <linux/cdev.h>
19 #include <linux/slab.h>
20 #include <linux/poll.h>
21 #include <linux/sched/signal.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/iio-opaque.h>
26 #include "iio_core_trigger.h"
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/buffer_impl.h>
31 static const char * const iio_endian_prefix[] = {
36 static bool iio_buffer_is_active(struct iio_buffer *buf)
38 return !list_empty(&buf->buffer_list);
41 static size_t iio_buffer_data_available(struct iio_buffer *buf)
43 return buf->access->data_available(buf);
46 static int iio_buffer_flush_hwfifo(struct iio_dev *indio_dev,
47 struct iio_buffer *buf, size_t required)
49 if (!indio_dev->info->hwfifo_flush_to_buffer)
52 return indio_dev->info->hwfifo_flush_to_buffer(indio_dev, required);
55 static bool iio_buffer_ready(struct iio_dev *indio_dev, struct iio_buffer *buf,
56 size_t to_wait, int to_flush)
61 /* wakeup if the device was unregistered */
65 /* drain the buffer if it was disabled */
66 if (!iio_buffer_is_active(buf)) {
67 to_wait = min_t(size_t, to_wait, 1);
71 avail = iio_buffer_data_available(buf);
73 if (avail >= to_wait) {
74 /* force a flush for non-blocking reads */
75 if (!to_wait && avail < to_flush)
76 iio_buffer_flush_hwfifo(indio_dev, buf,
82 flushed = iio_buffer_flush_hwfifo(indio_dev, buf,
87 if (avail + flushed >= to_wait)
94 * iio_buffer_read() - chrdev read for buffer access
95 * @filp: File structure pointer for the char device
96 * @buf: Destination buffer for iio buffer read
97 * @n: First n bytes to read
98 * @f_ps: Long offset provided by the user as a seek position
100 * This function relies on all buffer implementations having an
101 * iio_buffer as their first element.
103 * Return: negative values corresponding to error codes or ret != 0
104 * for ending the reading activity
106 static ssize_t iio_buffer_read(struct file *filp, char __user *buf,
107 size_t n, loff_t *f_ps)
109 struct iio_dev_buffer_pair *ib = filp->private_data;
110 struct iio_buffer *rb = ib->buffer;
111 struct iio_dev *indio_dev = ib->indio_dev;
112 DEFINE_WAIT_FUNC(wait, woken_wake_function);
117 if (!indio_dev->info)
120 if (!rb || !rb->access->read)
123 if (rb->direction != IIO_BUFFER_DIRECTION_IN)
126 datum_size = rb->bytes_per_datum;
129 * If datum_size is 0 there will never be anything to read from the
130 * buffer, so signal end of file now.
135 if (filp->f_flags & O_NONBLOCK)
138 to_wait = min_t(size_t, n / datum_size, rb->watermark);
140 add_wait_queue(&rb->pollq, &wait);
142 if (!indio_dev->info) {
147 if (!iio_buffer_ready(indio_dev, rb, to_wait, n / datum_size)) {
148 if (signal_pending(current)) {
153 wait_woken(&wait, TASK_INTERRUPTIBLE,
154 MAX_SCHEDULE_TIMEOUT);
158 ret = rb->access->read(rb, n, buf);
159 if (ret == 0 && (filp->f_flags & O_NONBLOCK))
162 remove_wait_queue(&rb->pollq, &wait);
167 static size_t iio_buffer_space_available(struct iio_buffer *buf)
169 if (buf->access->space_available)
170 return buf->access->space_available(buf);
175 static ssize_t iio_buffer_write(struct file *filp, const char __user *buf,
176 size_t n, loff_t *f_ps)
178 struct iio_dev_buffer_pair *ib = filp->private_data;
179 struct iio_buffer *rb = ib->buffer;
180 struct iio_dev *indio_dev = ib->indio_dev;
181 DEFINE_WAIT_FUNC(wait, woken_wake_function);
185 if (!indio_dev->info)
188 if (!rb || !rb->access->write)
191 if (rb->direction != IIO_BUFFER_DIRECTION_OUT)
195 add_wait_queue(&rb->pollq, &wait);
197 if (indio_dev->info == NULL)
200 if (!iio_buffer_space_available(rb)) {
201 if (signal_pending(current)) {
206 if (filp->f_flags & O_NONBLOCK) {
212 wait_woken(&wait, TASK_INTERRUPTIBLE,
213 MAX_SCHEDULE_TIMEOUT);
217 ret = rb->access->write(rb, n - written, buf + written);
223 } while (written != n);
224 remove_wait_queue(&rb->pollq, &wait);
226 return ret < 0 ? ret : written;
230 * iio_buffer_poll() - poll the buffer to find out if it has data
231 * @filp: File structure pointer for device access
232 * @wait: Poll table structure pointer for which the driver adds
235 * Return: (EPOLLIN | EPOLLRDNORM) if data is available for reading
236 * or 0 for other cases
238 static __poll_t iio_buffer_poll(struct file *filp,
239 struct poll_table_struct *wait)
241 struct iio_dev_buffer_pair *ib = filp->private_data;
242 struct iio_buffer *rb = ib->buffer;
243 struct iio_dev *indio_dev = ib->indio_dev;
245 if (!indio_dev->info || rb == NULL)
248 poll_wait(filp, &rb->pollq, wait);
250 switch (rb->direction) {
251 case IIO_BUFFER_DIRECTION_IN:
252 if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0))
253 return EPOLLIN | EPOLLRDNORM;
255 case IIO_BUFFER_DIRECTION_OUT:
256 if (iio_buffer_space_available(rb))
257 return EPOLLOUT | EPOLLWRNORM;
264 ssize_t iio_buffer_read_wrapper(struct file *filp, char __user *buf,
265 size_t n, loff_t *f_ps)
267 struct iio_dev_buffer_pair *ib = filp->private_data;
268 struct iio_buffer *rb = ib->buffer;
270 /* check if buffer was opened through new API */
271 if (test_bit(IIO_BUSY_BIT_POS, &rb->flags))
274 return iio_buffer_read(filp, buf, n, f_ps);
277 ssize_t iio_buffer_write_wrapper(struct file *filp, const char __user *buf,
278 size_t n, loff_t *f_ps)
280 struct iio_dev_buffer_pair *ib = filp->private_data;
281 struct iio_buffer *rb = ib->buffer;
283 /* check if buffer was opened through new API */
284 if (test_bit(IIO_BUSY_BIT_POS, &rb->flags))
287 return iio_buffer_write(filp, buf, n, f_ps);
290 __poll_t iio_buffer_poll_wrapper(struct file *filp,
291 struct poll_table_struct *wait)
293 struct iio_dev_buffer_pair *ib = filp->private_data;
294 struct iio_buffer *rb = ib->buffer;
296 /* check if buffer was opened through new API */
297 if (test_bit(IIO_BUSY_BIT_POS, &rb->flags))
300 return iio_buffer_poll(filp, wait);
304 * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue
305 * @indio_dev: The IIO device
307 * Wakes up the event waitqueue used for poll(). Should usually
308 * be called when the device is unregistered.
310 void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
312 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
313 struct iio_buffer *buffer;
316 for (i = 0; i < iio_dev_opaque->attached_buffers_cnt; i++) {
317 buffer = iio_dev_opaque->attached_buffers[i];
318 wake_up(&buffer->pollq);
322 int iio_pop_from_buffer(struct iio_buffer *buffer, void *data)
324 if (!buffer || !buffer->access || !buffer->access->remove_from)
327 return buffer->access->remove_from(buffer, data);
329 EXPORT_SYMBOL_GPL(iio_pop_from_buffer);
331 void iio_buffer_init(struct iio_buffer *buffer)
333 INIT_LIST_HEAD(&buffer->demux_list);
334 INIT_LIST_HEAD(&buffer->buffer_list);
335 init_waitqueue_head(&buffer->pollq);
336 kref_init(&buffer->ref);
337 if (!buffer->watermark)
338 buffer->watermark = 1;
340 EXPORT_SYMBOL(iio_buffer_init);
342 void iio_device_detach_buffers(struct iio_dev *indio_dev)
344 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
345 struct iio_buffer *buffer;
348 for (i = 0; i < iio_dev_opaque->attached_buffers_cnt; i++) {
349 buffer = iio_dev_opaque->attached_buffers[i];
350 iio_buffer_put(buffer);
353 kfree(iio_dev_opaque->attached_buffers);
356 static ssize_t iio_show_scan_index(struct device *dev,
357 struct device_attribute *attr,
360 return sysfs_emit(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
363 static ssize_t iio_show_fixed_type(struct device *dev,
364 struct device_attribute *attr,
367 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
368 u8 type = this_attr->c->scan_type.endianness;
370 if (type == IIO_CPU) {
371 #ifdef __LITTLE_ENDIAN
377 if (this_attr->c->scan_type.repeat > 1)
378 return sysfs_emit(buf, "%s:%c%d/%dX%d>>%u\n",
379 iio_endian_prefix[type],
380 this_attr->c->scan_type.sign,
381 this_attr->c->scan_type.realbits,
382 this_attr->c->scan_type.storagebits,
383 this_attr->c->scan_type.repeat,
384 this_attr->c->scan_type.shift);
386 return sysfs_emit(buf, "%s:%c%d/%d>>%u\n",
387 iio_endian_prefix[type],
388 this_attr->c->scan_type.sign,
389 this_attr->c->scan_type.realbits,
390 this_attr->c->scan_type.storagebits,
391 this_attr->c->scan_type.shift);
394 static ssize_t iio_scan_el_show(struct device *dev,
395 struct device_attribute *attr,
399 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
401 /* Ensure ret is 0 or 1. */
402 ret = !!test_bit(to_iio_dev_attr(attr)->address,
405 return sysfs_emit(buf, "%d\n", ret);
408 /* Note NULL used as error indicator as it doesn't make sense. */
409 static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
410 unsigned int masklength,
411 const unsigned long *mask,
414 if (bitmap_empty(mask, masklength))
418 if (bitmap_equal(mask, av_masks, masklength))
421 if (bitmap_subset(mask, av_masks, masklength))
424 av_masks += BITS_TO_LONGS(masklength);
429 static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
430 const unsigned long *mask)
432 if (!indio_dev->setup_ops->validate_scan_mask)
435 return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
439 * iio_scan_mask_set() - set particular bit in the scan mask
440 * @indio_dev: the iio device
441 * @buffer: the buffer whose scan mask we are interested in
442 * @bit: the bit to be set.
444 * Note that at this point we have no way of knowing what other
445 * buffers might request, hence this code only verifies that the
446 * individual buffers request is plausible.
448 static int iio_scan_mask_set(struct iio_dev *indio_dev,
449 struct iio_buffer *buffer, int bit)
451 const unsigned long *mask;
452 unsigned long *trialmask;
454 if (!indio_dev->masklength) {
455 WARN(1, "Trying to set scanmask prior to registering buffer\n");
459 trialmask = bitmap_alloc(indio_dev->masklength, GFP_KERNEL);
462 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
463 set_bit(bit, trialmask);
465 if (!iio_validate_scan_mask(indio_dev, trialmask))
466 goto err_invalid_mask;
468 if (indio_dev->available_scan_masks) {
469 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
470 indio_dev->masklength,
473 goto err_invalid_mask;
475 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
477 bitmap_free(trialmask);
482 bitmap_free(trialmask);
486 static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
488 clear_bit(bit, buffer->scan_mask);
492 static int iio_scan_mask_query(struct iio_dev *indio_dev,
493 struct iio_buffer *buffer, int bit)
495 if (bit > indio_dev->masklength)
498 if (!buffer->scan_mask)
501 /* Ensure return value is 0 or 1. */
502 return !!test_bit(bit, buffer->scan_mask);
505 static ssize_t iio_scan_el_store(struct device *dev,
506 struct device_attribute *attr,
512 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
513 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
514 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
515 struct iio_buffer *buffer = this_attr->buffer;
517 ret = kstrtobool(buf, &state);
520 mutex_lock(&iio_dev_opaque->mlock);
521 if (iio_buffer_is_active(buffer)) {
525 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
529 ret = iio_scan_mask_clear(buffer, this_attr->address);
532 } else if (state && !ret) {
533 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
539 mutex_unlock(&iio_dev_opaque->mlock);
541 return ret < 0 ? ret : len;
545 static ssize_t iio_scan_el_ts_show(struct device *dev,
546 struct device_attribute *attr,
549 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
551 return sysfs_emit(buf, "%d\n", buffer->scan_timestamp);
554 static ssize_t iio_scan_el_ts_store(struct device *dev,
555 struct device_attribute *attr,
560 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
561 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
562 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
565 ret = kstrtobool(buf, &state);
569 mutex_lock(&iio_dev_opaque->mlock);
570 if (iio_buffer_is_active(buffer)) {
574 buffer->scan_timestamp = state;
576 mutex_unlock(&iio_dev_opaque->mlock);
578 return ret ? ret : len;
581 static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
582 struct iio_buffer *buffer,
583 const struct iio_chan_spec *chan)
585 int ret, attrcount = 0;
587 ret = __iio_add_chan_devattr("index",
589 &iio_show_scan_index,
595 &buffer->buffer_attr_list);
599 ret = __iio_add_chan_devattr("type",
601 &iio_show_fixed_type,
607 &buffer->buffer_attr_list);
611 if (chan->type != IIO_TIMESTAMP)
612 ret = __iio_add_chan_devattr("en",
620 &buffer->buffer_attr_list);
622 ret = __iio_add_chan_devattr("en",
624 &iio_scan_el_ts_show,
625 &iio_scan_el_ts_store,
630 &buffer->buffer_attr_list);
638 static ssize_t length_show(struct device *dev, struct device_attribute *attr,
641 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
643 return sysfs_emit(buf, "%d\n", buffer->length);
646 static ssize_t length_store(struct device *dev, struct device_attribute *attr,
647 const char *buf, size_t len)
649 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
650 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
651 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
655 ret = kstrtouint(buf, 10, &val);
659 if (val == buffer->length)
662 mutex_lock(&iio_dev_opaque->mlock);
663 if (iio_buffer_is_active(buffer)) {
666 buffer->access->set_length(buffer, val);
671 if (buffer->length && buffer->length < buffer->watermark)
672 buffer->watermark = buffer->length;
674 mutex_unlock(&iio_dev_opaque->mlock);
676 return ret ? ret : len;
679 static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
682 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
684 return sysfs_emit(buf, "%d\n", iio_buffer_is_active(buffer));
687 static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev,
688 unsigned int scan_index)
690 const struct iio_chan_spec *ch;
693 ch = iio_find_channel_from_si(indio_dev, scan_index);
694 bytes = ch->scan_type.storagebits / 8;
695 if (ch->scan_type.repeat > 1)
696 bytes *= ch->scan_type.repeat;
700 static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev)
702 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
704 return iio_storage_bytes_for_si(indio_dev,
705 iio_dev_opaque->scan_index_timestamp);
708 static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
709 const unsigned long *mask, bool timestamp)
711 unsigned int bytes = 0;
712 int length, i, largest = 0;
714 /* How much space will the demuxed element take? */
715 for_each_set_bit(i, mask,
716 indio_dev->masklength) {
717 length = iio_storage_bytes_for_si(indio_dev, i);
718 bytes = ALIGN(bytes, length);
720 largest = max(largest, length);
724 length = iio_storage_bytes_for_timestamp(indio_dev);
725 bytes = ALIGN(bytes, length);
727 largest = max(largest, length);
730 bytes = ALIGN(bytes, largest);
734 static void iio_buffer_activate(struct iio_dev *indio_dev,
735 struct iio_buffer *buffer)
737 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
739 iio_buffer_get(buffer);
740 list_add(&buffer->buffer_list, &iio_dev_opaque->buffer_list);
743 static void iio_buffer_deactivate(struct iio_buffer *buffer)
745 list_del_init(&buffer->buffer_list);
746 wake_up_interruptible(&buffer->pollq);
747 iio_buffer_put(buffer);
750 static void iio_buffer_deactivate_all(struct iio_dev *indio_dev)
752 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
753 struct iio_buffer *buffer, *_buffer;
755 list_for_each_entry_safe(buffer, _buffer,
756 &iio_dev_opaque->buffer_list, buffer_list)
757 iio_buffer_deactivate(buffer);
760 static int iio_buffer_enable(struct iio_buffer *buffer,
761 struct iio_dev *indio_dev)
763 if (!buffer->access->enable)
765 return buffer->access->enable(buffer, indio_dev);
768 static int iio_buffer_disable(struct iio_buffer *buffer,
769 struct iio_dev *indio_dev)
771 if (!buffer->access->disable)
773 return buffer->access->disable(buffer, indio_dev);
776 static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
777 struct iio_buffer *buffer)
781 if (!buffer->access->set_bytes_per_datum)
784 bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
785 buffer->scan_timestamp);
787 buffer->access->set_bytes_per_datum(buffer, bytes);
790 static int iio_buffer_request_update(struct iio_dev *indio_dev,
791 struct iio_buffer *buffer)
795 iio_buffer_update_bytes_per_datum(indio_dev, buffer);
796 if (buffer->access->request_update) {
797 ret = buffer->access->request_update(buffer);
799 dev_dbg(&indio_dev->dev,
800 "Buffer not started: buffer parameter update failed (%d)\n",
809 static void iio_free_scan_mask(struct iio_dev *indio_dev,
810 const unsigned long *mask)
812 /* If the mask is dynamically allocated free it, otherwise do nothing */
813 if (!indio_dev->available_scan_masks)
817 struct iio_device_config {
819 unsigned int watermark;
820 const unsigned long *scan_mask;
821 unsigned int scan_bytes;
825 static int iio_verify_update(struct iio_dev *indio_dev,
826 struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer,
827 struct iio_device_config *config)
829 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
830 unsigned long *compound_mask;
831 const unsigned long *scan_mask;
832 bool strict_scanmask = false;
833 struct iio_buffer *buffer;
838 bitmap_empty(insert_buffer->scan_mask, indio_dev->masklength)) {
839 dev_dbg(&indio_dev->dev,
840 "At least one scan element must be enabled first\n");
844 memset(config, 0, sizeof(*config));
845 config->watermark = ~0;
848 * If there is just one buffer and we are removing it there is nothing
851 if (remove_buffer && !insert_buffer &&
852 list_is_singular(&iio_dev_opaque->buffer_list))
855 modes = indio_dev->modes;
857 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
858 if (buffer == remove_buffer)
860 modes &= buffer->access->modes;
861 config->watermark = min(config->watermark, buffer->watermark);
865 modes &= insert_buffer->access->modes;
866 config->watermark = min(config->watermark,
867 insert_buffer->watermark);
870 /* Definitely possible for devices to support both of these. */
871 if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) {
872 config->mode = INDIO_BUFFER_TRIGGERED;
873 } else if (modes & INDIO_BUFFER_HARDWARE) {
875 * Keep things simple for now and only allow a single buffer to
876 * be connected in hardware mode.
878 if (insert_buffer && !list_empty(&iio_dev_opaque->buffer_list))
880 config->mode = INDIO_BUFFER_HARDWARE;
881 strict_scanmask = true;
882 } else if (modes & INDIO_BUFFER_SOFTWARE) {
883 config->mode = INDIO_BUFFER_SOFTWARE;
885 /* Can only occur on first buffer */
886 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
887 dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n");
891 /* What scan mask do we actually have? */
892 compound_mask = bitmap_zalloc(indio_dev->masklength, GFP_KERNEL);
893 if (compound_mask == NULL)
896 scan_timestamp = false;
898 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
899 if (buffer == remove_buffer)
901 bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
902 indio_dev->masklength);
903 scan_timestamp |= buffer->scan_timestamp;
907 bitmap_or(compound_mask, compound_mask,
908 insert_buffer->scan_mask, indio_dev->masklength);
909 scan_timestamp |= insert_buffer->scan_timestamp;
912 if (indio_dev->available_scan_masks) {
913 scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks,
914 indio_dev->masklength,
917 bitmap_free(compound_mask);
918 if (scan_mask == NULL)
921 scan_mask = compound_mask;
924 config->scan_bytes = iio_compute_scan_bytes(indio_dev,
925 scan_mask, scan_timestamp);
926 config->scan_mask = scan_mask;
927 config->scan_timestamp = scan_timestamp;
933 * struct iio_demux_table - table describing demux memcpy ops
934 * @from: index to copy from
935 * @to: index to copy to
936 * @length: how many bytes to copy
937 * @l: list head used for management
939 struct iio_demux_table {
946 static void iio_buffer_demux_free(struct iio_buffer *buffer)
948 struct iio_demux_table *p, *q;
950 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
956 static int iio_buffer_add_demux(struct iio_buffer *buffer,
957 struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc,
961 if (*p && (*p)->from + (*p)->length == in_loc &&
962 (*p)->to + (*p)->length == out_loc) {
963 (*p)->length += length;
965 *p = kmalloc(sizeof(**p), GFP_KERNEL);
970 (*p)->length = length;
971 list_add_tail(&(*p)->l, &buffer->demux_list);
977 static int iio_buffer_update_demux(struct iio_dev *indio_dev,
978 struct iio_buffer *buffer)
980 int ret, in_ind = -1, out_ind, length;
981 unsigned int in_loc = 0, out_loc = 0;
982 struct iio_demux_table *p = NULL;
984 /* Clear out any old demux */
985 iio_buffer_demux_free(buffer);
986 kfree(buffer->demux_bounce);
987 buffer->demux_bounce = NULL;
989 /* First work out which scan mode we will actually have */
990 if (bitmap_equal(indio_dev->active_scan_mask,
992 indio_dev->masklength))
995 /* Now we have the two masks, work from least sig and build up sizes */
996 for_each_set_bit(out_ind,
998 indio_dev->masklength) {
999 in_ind = find_next_bit(indio_dev->active_scan_mask,
1000 indio_dev->masklength,
1002 while (in_ind != out_ind) {
1003 length = iio_storage_bytes_for_si(indio_dev, in_ind);
1004 /* Make sure we are aligned */
1005 in_loc = roundup(in_loc, length) + length;
1006 in_ind = find_next_bit(indio_dev->active_scan_mask,
1007 indio_dev->masklength,
1010 length = iio_storage_bytes_for_si(indio_dev, in_ind);
1011 out_loc = roundup(out_loc, length);
1012 in_loc = roundup(in_loc, length);
1013 ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
1015 goto error_clear_mux_table;
1019 /* Relies on scan_timestamp being last */
1020 if (buffer->scan_timestamp) {
1021 length = iio_storage_bytes_for_timestamp(indio_dev);
1022 out_loc = roundup(out_loc, length);
1023 in_loc = roundup(in_loc, length);
1024 ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
1026 goto error_clear_mux_table;
1029 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
1030 if (buffer->demux_bounce == NULL) {
1032 goto error_clear_mux_table;
1036 error_clear_mux_table:
1037 iio_buffer_demux_free(buffer);
1042 static int iio_update_demux(struct iio_dev *indio_dev)
1044 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1045 struct iio_buffer *buffer;
1048 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
1049 ret = iio_buffer_update_demux(indio_dev, buffer);
1051 goto error_clear_mux_table;
1055 error_clear_mux_table:
1056 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list)
1057 iio_buffer_demux_free(buffer);
1062 static int iio_enable_buffers(struct iio_dev *indio_dev,
1063 struct iio_device_config *config)
1065 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1066 struct iio_buffer *buffer, *tmp = NULL;
1069 indio_dev->active_scan_mask = config->scan_mask;
1070 indio_dev->scan_timestamp = config->scan_timestamp;
1071 indio_dev->scan_bytes = config->scan_bytes;
1072 iio_dev_opaque->currentmode = config->mode;
1074 iio_update_demux(indio_dev);
1077 if (indio_dev->setup_ops->preenable) {
1078 ret = indio_dev->setup_ops->preenable(indio_dev);
1080 dev_dbg(&indio_dev->dev,
1081 "Buffer not started: buffer preenable failed (%d)\n", ret);
1082 goto err_undo_config;
1086 if (indio_dev->info->update_scan_mode) {
1087 ret = indio_dev->info
1088 ->update_scan_mode(indio_dev,
1089 indio_dev->active_scan_mask);
1091 dev_dbg(&indio_dev->dev,
1092 "Buffer not started: update scan mode failed (%d)\n",
1094 goto err_run_postdisable;
1098 if (indio_dev->info->hwfifo_set_watermark)
1099 indio_dev->info->hwfifo_set_watermark(indio_dev,
1102 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
1103 ret = iio_buffer_enable(buffer, indio_dev);
1106 goto err_disable_buffers;
1110 if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) {
1111 ret = iio_trigger_attach_poll_func(indio_dev->trig,
1112 indio_dev->pollfunc);
1114 goto err_disable_buffers;
1117 if (indio_dev->setup_ops->postenable) {
1118 ret = indio_dev->setup_ops->postenable(indio_dev);
1120 dev_dbg(&indio_dev->dev,
1121 "Buffer not started: postenable failed (%d)\n", ret);
1122 goto err_detach_pollfunc;
1128 err_detach_pollfunc:
1129 if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) {
1130 iio_trigger_detach_poll_func(indio_dev->trig,
1131 indio_dev->pollfunc);
1133 err_disable_buffers:
1134 buffer = list_prepare_entry(tmp, &iio_dev_opaque->buffer_list, buffer_list);
1135 list_for_each_entry_continue_reverse(buffer, &iio_dev_opaque->buffer_list,
1137 iio_buffer_disable(buffer, indio_dev);
1138 err_run_postdisable:
1139 if (indio_dev->setup_ops->postdisable)
1140 indio_dev->setup_ops->postdisable(indio_dev);
1142 iio_dev_opaque->currentmode = INDIO_DIRECT_MODE;
1143 indio_dev->active_scan_mask = NULL;
1148 static int iio_disable_buffers(struct iio_dev *indio_dev)
1150 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1151 struct iio_buffer *buffer;
1155 /* Wind down existing buffers - iff there are any */
1156 if (list_empty(&iio_dev_opaque->buffer_list))
1160 * If things go wrong at some step in disable we still need to continue
1161 * to perform the other steps, otherwise we leave the device in a
1162 * inconsistent state. We return the error code for the first error we
1166 if (indio_dev->setup_ops->predisable) {
1167 ret2 = indio_dev->setup_ops->predisable(indio_dev);
1172 if (iio_dev_opaque->currentmode == INDIO_BUFFER_TRIGGERED) {
1173 iio_trigger_detach_poll_func(indio_dev->trig,
1174 indio_dev->pollfunc);
1177 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
1178 ret2 = iio_buffer_disable(buffer, indio_dev);
1183 if (indio_dev->setup_ops->postdisable) {
1184 ret2 = indio_dev->setup_ops->postdisable(indio_dev);
1189 iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask);
1190 indio_dev->active_scan_mask = NULL;
1191 iio_dev_opaque->currentmode = INDIO_DIRECT_MODE;
1196 static int __iio_update_buffers(struct iio_dev *indio_dev,
1197 struct iio_buffer *insert_buffer,
1198 struct iio_buffer *remove_buffer)
1200 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1201 struct iio_device_config new_config;
1204 ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer,
1209 if (insert_buffer) {
1210 ret = iio_buffer_request_update(indio_dev, insert_buffer);
1212 goto err_free_config;
1215 ret = iio_disable_buffers(indio_dev);
1217 goto err_deactivate_all;
1220 iio_buffer_deactivate(remove_buffer);
1222 iio_buffer_activate(indio_dev, insert_buffer);
1224 /* If no buffers in list, we are done */
1225 if (list_empty(&iio_dev_opaque->buffer_list))
1228 ret = iio_enable_buffers(indio_dev, &new_config);
1230 goto err_deactivate_all;
1236 * We've already verified that the config is valid earlier. If things go
1237 * wrong in either enable or disable the most likely reason is an IO
1238 * error from the device. In this case there is no good recovery
1239 * strategy. Just make sure to disable everything and leave the device
1240 * in a sane state. With a bit of luck the device might come back to
1241 * life again later and userspace can try again.
1243 iio_buffer_deactivate_all(indio_dev);
1246 iio_free_scan_mask(indio_dev, new_config.scan_mask);
1250 int iio_update_buffers(struct iio_dev *indio_dev,
1251 struct iio_buffer *insert_buffer,
1252 struct iio_buffer *remove_buffer)
1254 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1257 if (insert_buffer == remove_buffer)
1260 if (insert_buffer &&
1261 (insert_buffer->direction == IIO_BUFFER_DIRECTION_OUT))
1264 mutex_lock(&iio_dev_opaque->info_exist_lock);
1265 mutex_lock(&iio_dev_opaque->mlock);
1267 if (insert_buffer && iio_buffer_is_active(insert_buffer))
1268 insert_buffer = NULL;
1270 if (remove_buffer && !iio_buffer_is_active(remove_buffer))
1271 remove_buffer = NULL;
1273 if (!insert_buffer && !remove_buffer) {
1278 if (indio_dev->info == NULL) {
1283 ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
1286 mutex_unlock(&iio_dev_opaque->mlock);
1287 mutex_unlock(&iio_dev_opaque->info_exist_lock);
1291 EXPORT_SYMBOL_GPL(iio_update_buffers);
1293 void iio_disable_all_buffers(struct iio_dev *indio_dev)
1295 iio_disable_buffers(indio_dev);
1296 iio_buffer_deactivate_all(indio_dev);
1299 static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
1300 const char *buf, size_t len)
1303 bool requested_state;
1304 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1305 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1306 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
1309 ret = kstrtobool(buf, &requested_state);
1313 mutex_lock(&iio_dev_opaque->mlock);
1315 /* Find out if it is in the list */
1316 inlist = iio_buffer_is_active(buffer);
1317 /* Already in desired state */
1318 if (inlist == requested_state)
1321 if (requested_state)
1322 ret = __iio_update_buffers(indio_dev, buffer, NULL);
1324 ret = __iio_update_buffers(indio_dev, NULL, buffer);
1327 mutex_unlock(&iio_dev_opaque->mlock);
1328 return (ret < 0) ? ret : len;
1331 static ssize_t watermark_show(struct device *dev, struct device_attribute *attr,
1334 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
1336 return sysfs_emit(buf, "%u\n", buffer->watermark);
1339 static ssize_t watermark_store(struct device *dev,
1340 struct device_attribute *attr,
1341 const char *buf, size_t len)
1343 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1344 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1345 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
1349 ret = kstrtouint(buf, 10, &val);
1355 mutex_lock(&iio_dev_opaque->mlock);
1357 if (val > buffer->length) {
1362 if (iio_buffer_is_active(buffer)) {
1367 buffer->watermark = val;
1369 mutex_unlock(&iio_dev_opaque->mlock);
1371 return ret ? ret : len;
1374 static ssize_t data_available_show(struct device *dev,
1375 struct device_attribute *attr, char *buf)
1377 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
1379 return sysfs_emit(buf, "%zu\n", iio_buffer_data_available(buffer));
1382 static ssize_t direction_show(struct device *dev,
1383 struct device_attribute *attr,
1386 struct iio_buffer *buffer = to_iio_dev_attr(attr)->buffer;
1388 switch (buffer->direction) {
1389 case IIO_BUFFER_DIRECTION_IN:
1390 return sysfs_emit(buf, "in\n");
1391 case IIO_BUFFER_DIRECTION_OUT:
1392 return sysfs_emit(buf, "out\n");
1398 static DEVICE_ATTR_RW(length);
1399 static struct device_attribute dev_attr_length_ro = __ATTR_RO(length);
1400 static DEVICE_ATTR_RW(enable);
1401 static DEVICE_ATTR_RW(watermark);
1402 static struct device_attribute dev_attr_watermark_ro = __ATTR_RO(watermark);
1403 static DEVICE_ATTR_RO(data_available);
1404 static DEVICE_ATTR_RO(direction);
1407 * When adding new attributes here, put the at the end, at least until
1408 * the code that handles the length/length_ro & watermark/watermark_ro
1409 * assignments gets cleaned up. Otherwise these can create some weird
1410 * duplicate attributes errors under some setups.
1412 static struct attribute *iio_buffer_attrs[] = {
1413 &dev_attr_length.attr,
1414 &dev_attr_enable.attr,
1415 &dev_attr_watermark.attr,
1416 &dev_attr_data_available.attr,
1417 &dev_attr_direction.attr,
1420 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
1422 static struct attribute *iio_buffer_wrap_attr(struct iio_buffer *buffer,
1423 struct attribute *attr)
1425 struct device_attribute *dattr = to_dev_attr(attr);
1426 struct iio_dev_attr *iio_attr;
1428 iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
1432 iio_attr->buffer = buffer;
1433 memcpy(&iio_attr->dev_attr, dattr, sizeof(iio_attr->dev_attr));
1434 iio_attr->dev_attr.attr.name = kstrdup_const(attr->name, GFP_KERNEL);
1435 if (!iio_attr->dev_attr.attr.name) {
1440 sysfs_attr_init(&iio_attr->dev_attr.attr);
1442 list_add(&iio_attr->l, &buffer->buffer_attr_list);
1444 return &iio_attr->dev_attr.attr;
1447 static int iio_buffer_register_legacy_sysfs_groups(struct iio_dev *indio_dev,
1448 struct attribute **buffer_attrs,
1449 int buffer_attrcount,
1450 int scan_el_attrcount)
1452 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1453 struct attribute_group *group;
1454 struct attribute **attrs;
1457 attrs = kcalloc(buffer_attrcount + 1, sizeof(*attrs), GFP_KERNEL);
1461 memcpy(attrs, buffer_attrs, buffer_attrcount * sizeof(*attrs));
1463 group = &iio_dev_opaque->legacy_buffer_group;
1464 group->attrs = attrs;
1465 group->name = "buffer";
1467 ret = iio_device_register_sysfs_group(indio_dev, group);
1469 goto error_free_buffer_attrs;
1471 attrs = kcalloc(scan_el_attrcount + 1, sizeof(*attrs), GFP_KERNEL);
1474 goto error_free_buffer_attrs;
1477 memcpy(attrs, &buffer_attrs[buffer_attrcount],
1478 scan_el_attrcount * sizeof(*attrs));
1480 group = &iio_dev_opaque->legacy_scan_el_group;
1481 group->attrs = attrs;
1482 group->name = "scan_elements";
1484 ret = iio_device_register_sysfs_group(indio_dev, group);
1486 goto error_free_scan_el_attrs;
1490 error_free_scan_el_attrs:
1491 kfree(iio_dev_opaque->legacy_scan_el_group.attrs);
1492 error_free_buffer_attrs:
1493 kfree(iio_dev_opaque->legacy_buffer_group.attrs);
1498 static void iio_buffer_unregister_legacy_sysfs_groups(struct iio_dev *indio_dev)
1500 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1502 kfree(iio_dev_opaque->legacy_buffer_group.attrs);
1503 kfree(iio_dev_opaque->legacy_scan_el_group.attrs);
1506 static int iio_buffer_chrdev_release(struct inode *inode, struct file *filep)
1508 struct iio_dev_buffer_pair *ib = filep->private_data;
1509 struct iio_dev *indio_dev = ib->indio_dev;
1510 struct iio_buffer *buffer = ib->buffer;
1512 wake_up(&buffer->pollq);
1515 clear_bit(IIO_BUSY_BIT_POS, &buffer->flags);
1516 iio_device_put(indio_dev);
1521 static const struct file_operations iio_buffer_chrdev_fileops = {
1522 .owner = THIS_MODULE,
1523 .llseek = noop_llseek,
1524 .read = iio_buffer_read,
1525 .write = iio_buffer_write,
1526 .poll = iio_buffer_poll,
1527 .release = iio_buffer_chrdev_release,
1530 static long iio_device_buffer_getfd(struct iio_dev *indio_dev, unsigned long arg)
1532 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1533 int __user *ival = (int __user *)arg;
1534 struct iio_dev_buffer_pair *ib;
1535 struct iio_buffer *buffer;
1538 if (copy_from_user(&idx, ival, sizeof(idx)))
1541 if (idx >= iio_dev_opaque->attached_buffers_cnt)
1544 iio_device_get(indio_dev);
1546 buffer = iio_dev_opaque->attached_buffers[idx];
1548 if (test_and_set_bit(IIO_BUSY_BIT_POS, &buffer->flags)) {
1550 goto error_iio_dev_put;
1553 ib = kzalloc(sizeof(*ib), GFP_KERNEL);
1556 goto error_clear_busy_bit;
1559 ib->indio_dev = indio_dev;
1560 ib->buffer = buffer;
1562 fd = anon_inode_getfd("iio:buffer", &iio_buffer_chrdev_fileops,
1563 ib, O_RDWR | O_CLOEXEC);
1569 if (copy_to_user(ival, &fd, sizeof(fd))) {
1571 * "Leak" the fd, as there's not much we can do about this
1572 * anyway. 'fd' might have been closed already, as
1573 * anon_inode_getfd() called fd_install() on it, which made
1574 * it reachable by userland.
1576 * Instead of allowing a malicious user to play tricks with
1577 * us, rely on the process exit path to do any necessary
1578 * cleanup, as in releasing the file, if still needed.
1587 error_clear_busy_bit:
1588 clear_bit(IIO_BUSY_BIT_POS, &buffer->flags);
1590 iio_device_put(indio_dev);
1594 static long iio_device_buffer_ioctl(struct iio_dev *indio_dev, struct file *filp,
1595 unsigned int cmd, unsigned long arg)
1598 case IIO_BUFFER_GET_FD_IOCTL:
1599 return iio_device_buffer_getfd(indio_dev, arg);
1601 return IIO_IOCTL_UNHANDLED;
1605 static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
1606 struct iio_dev *indio_dev,
1609 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1610 struct iio_dev_attr *p;
1611 const struct iio_dev_attr *id_attr;
1612 struct attribute **attr;
1613 int ret, i, attrn, scan_el_attrcount, buffer_attrcount;
1614 const struct iio_chan_spec *channels;
1616 buffer_attrcount = 0;
1617 if (buffer->attrs) {
1618 while (buffer->attrs[buffer_attrcount] != NULL)
1621 buffer_attrcount += ARRAY_SIZE(iio_buffer_attrs);
1623 scan_el_attrcount = 0;
1624 INIT_LIST_HEAD(&buffer->buffer_attr_list);
1625 channels = indio_dev->channels;
1628 for (i = 0; i < indio_dev->num_channels; i++) {
1629 if (channels[i].scan_index < 0)
1632 /* Verify that sample bits fit into storage */
1633 if (channels[i].scan_type.storagebits <
1634 channels[i].scan_type.realbits +
1635 channels[i].scan_type.shift) {
1636 dev_err(&indio_dev->dev,
1637 "Channel %d storagebits (%d) < shifted realbits (%d + %d)\n",
1638 i, channels[i].scan_type.storagebits,
1639 channels[i].scan_type.realbits,
1640 channels[i].scan_type.shift);
1642 goto error_cleanup_dynamic;
1645 ret = iio_buffer_add_channel_sysfs(indio_dev, buffer,
1648 goto error_cleanup_dynamic;
1649 scan_el_attrcount += ret;
1650 if (channels[i].type == IIO_TIMESTAMP)
1651 iio_dev_opaque->scan_index_timestamp =
1652 channels[i].scan_index;
1654 if (indio_dev->masklength && buffer->scan_mask == NULL) {
1655 buffer->scan_mask = bitmap_zalloc(indio_dev->masklength,
1657 if (buffer->scan_mask == NULL) {
1659 goto error_cleanup_dynamic;
1664 attrn = buffer_attrcount + scan_el_attrcount;
1665 attr = kcalloc(attrn + 1, sizeof(*attr), GFP_KERNEL);
1668 goto error_free_scan_mask;
1671 memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
1672 if (!buffer->access->set_length)
1673 attr[0] = &dev_attr_length_ro.attr;
1675 if (buffer->access->flags & INDIO_BUFFER_FLAG_FIXED_WATERMARK)
1676 attr[2] = &dev_attr_watermark_ro.attr;
1679 for (i = 0, id_attr = buffer->attrs[i];
1680 (id_attr = buffer->attrs[i]); i++)
1681 attr[ARRAY_SIZE(iio_buffer_attrs) + i] =
1682 (struct attribute *)&id_attr->dev_attr.attr;
1684 buffer->buffer_group.attrs = attr;
1686 for (i = 0; i < buffer_attrcount; i++) {
1687 struct attribute *wrapped;
1689 wrapped = iio_buffer_wrap_attr(buffer, attr[i]);
1692 goto error_free_buffer_attrs;
1698 list_for_each_entry(p, &buffer->buffer_attr_list, l)
1699 attr[attrn++] = &p->dev_attr.attr;
1701 buffer->buffer_group.name = kasprintf(GFP_KERNEL, "buffer%d", index);
1702 if (!buffer->buffer_group.name) {
1704 goto error_free_buffer_attrs;
1707 ret = iio_device_register_sysfs_group(indio_dev, &buffer->buffer_group);
1709 goto error_free_buffer_attr_group_name;
1711 /* we only need to register the legacy groups for the first buffer */
1715 ret = iio_buffer_register_legacy_sysfs_groups(indio_dev, attr,
1719 goto error_free_buffer_attr_group_name;
1723 error_free_buffer_attr_group_name:
1724 kfree(buffer->buffer_group.name);
1725 error_free_buffer_attrs:
1726 kfree(buffer->buffer_group.attrs);
1727 error_free_scan_mask:
1728 bitmap_free(buffer->scan_mask);
1729 error_cleanup_dynamic:
1730 iio_free_chan_devattr_list(&buffer->buffer_attr_list);
1735 static void __iio_buffer_free_sysfs_and_mask(struct iio_buffer *buffer,
1736 struct iio_dev *indio_dev,
1740 iio_buffer_unregister_legacy_sysfs_groups(indio_dev);
1741 bitmap_free(buffer->scan_mask);
1742 kfree(buffer->buffer_group.name);
1743 kfree(buffer->buffer_group.attrs);
1744 iio_free_chan_devattr_list(&buffer->buffer_attr_list);
1747 int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
1749 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1750 const struct iio_chan_spec *channels;
1751 struct iio_buffer *buffer;
1755 channels = indio_dev->channels;
1757 int ml = indio_dev->masklength;
1759 for (i = 0; i < indio_dev->num_channels; i++)
1760 ml = max(ml, channels[i].scan_index + 1);
1761 indio_dev->masklength = ml;
1764 if (!iio_dev_opaque->attached_buffers_cnt)
1767 for (idx = 0; idx < iio_dev_opaque->attached_buffers_cnt; idx++) {
1768 buffer = iio_dev_opaque->attached_buffers[idx];
1769 ret = __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev, idx);
1771 goto error_unwind_sysfs_and_mask;
1774 sz = sizeof(*(iio_dev_opaque->buffer_ioctl_handler));
1775 iio_dev_opaque->buffer_ioctl_handler = kzalloc(sz, GFP_KERNEL);
1776 if (!iio_dev_opaque->buffer_ioctl_handler) {
1778 goto error_unwind_sysfs_and_mask;
1781 iio_dev_opaque->buffer_ioctl_handler->ioctl = iio_device_buffer_ioctl;
1782 iio_device_ioctl_handler_register(indio_dev,
1783 iio_dev_opaque->buffer_ioctl_handler);
1787 error_unwind_sysfs_and_mask:
1789 buffer = iio_dev_opaque->attached_buffers[idx];
1790 __iio_buffer_free_sysfs_and_mask(buffer, indio_dev, idx);
1795 void iio_buffers_free_sysfs_and_mask(struct iio_dev *indio_dev)
1797 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1798 struct iio_buffer *buffer;
1801 if (!iio_dev_opaque->attached_buffers_cnt)
1804 iio_device_ioctl_handler_unregister(iio_dev_opaque->buffer_ioctl_handler);
1805 kfree(iio_dev_opaque->buffer_ioctl_handler);
1807 for (i = iio_dev_opaque->attached_buffers_cnt - 1; i >= 0; i--) {
1808 buffer = iio_dev_opaque->attached_buffers[i];
1809 __iio_buffer_free_sysfs_and_mask(buffer, indio_dev, i);
1814 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
1815 * @indio_dev: the iio device
1816 * @mask: scan mask to be checked
1818 * Return true if exactly one bit is set in the scan mask, false otherwise. It
1819 * can be used for devices where only one channel can be active for sampling at
1822 bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
1823 const unsigned long *mask)
1825 return bitmap_weight(mask, indio_dev->masklength) == 1;
1827 EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
1829 static const void *iio_demux(struct iio_buffer *buffer,
1832 struct iio_demux_table *t;
1834 if (list_empty(&buffer->demux_list))
1836 list_for_each_entry(t, &buffer->demux_list, l)
1837 memcpy(buffer->demux_bounce + t->to,
1838 datain + t->from, t->length);
1840 return buffer->demux_bounce;
1843 static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
1845 const void *dataout = iio_demux(buffer, data);
1848 ret = buffer->access->store_to(buffer, dataout);
1853 * We can't just test for watermark to decide if we wake the poll queue
1854 * because read may request less samples than the watermark.
1856 wake_up_interruptible_poll(&buffer->pollq, EPOLLIN | EPOLLRDNORM);
1861 * iio_push_to_buffers() - push to a registered buffer.
1862 * @indio_dev: iio_dev structure for device.
1865 int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
1867 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1869 struct iio_buffer *buf;
1871 list_for_each_entry(buf, &iio_dev_opaque->buffer_list, buffer_list) {
1872 ret = iio_push_to_buffer(buf, data);
1879 EXPORT_SYMBOL_GPL(iio_push_to_buffers);
1882 * iio_push_to_buffers_with_ts_unaligned() - push to registered buffer,
1883 * no alignment or space requirements.
1884 * @indio_dev: iio_dev structure for device.
1885 * @data: channel data excluding the timestamp.
1886 * @data_sz: size of data.
1887 * @timestamp: timestamp for the sample data.
1889 * This special variant of iio_push_to_buffers_with_timestamp() does
1890 * not require space for the timestamp, or 8 byte alignment of data.
1891 * It does however require an allocation on first call and additional
1892 * copies on all calls, so should be avoided if possible.
1894 int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev,
1899 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1902 * Conservative estimate - we can always safely copy the minimum
1903 * of either the data provided or the length of the destination buffer.
1904 * This relaxed limit allows the calling drivers to be lax about
1905 * tracking the size of the data they are pushing, at the cost of
1906 * unnecessary copying of padding.
1908 data_sz = min_t(size_t, indio_dev->scan_bytes, data_sz);
1909 if (iio_dev_opaque->bounce_buffer_size != indio_dev->scan_bytes) {
1912 bb = devm_krealloc(&indio_dev->dev,
1913 iio_dev_opaque->bounce_buffer,
1914 indio_dev->scan_bytes, GFP_KERNEL);
1917 iio_dev_opaque->bounce_buffer = bb;
1918 iio_dev_opaque->bounce_buffer_size = indio_dev->scan_bytes;
1920 memcpy(iio_dev_opaque->bounce_buffer, data, data_sz);
1921 return iio_push_to_buffers_with_timestamp(indio_dev,
1922 iio_dev_opaque->bounce_buffer,
1925 EXPORT_SYMBOL_GPL(iio_push_to_buffers_with_ts_unaligned);
1928 * iio_buffer_release() - Free a buffer's resources
1929 * @ref: Pointer to the kref embedded in the iio_buffer struct
1931 * This function is called when the last reference to the buffer has been
1932 * dropped. It will typically free all resources allocated by the buffer. Do not
1933 * call this function manually, always use iio_buffer_put() when done using a
1936 static void iio_buffer_release(struct kref *ref)
1938 struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
1940 buffer->access->release(buffer);
1944 * iio_buffer_get() - Grab a reference to the buffer
1945 * @buffer: The buffer to grab a reference for, may be NULL
1947 * Returns the pointer to the buffer that was passed into the function.
1949 struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
1952 kref_get(&buffer->ref);
1956 EXPORT_SYMBOL_GPL(iio_buffer_get);
1959 * iio_buffer_put() - Release the reference to the buffer
1960 * @buffer: The buffer to release the reference for, may be NULL
1962 void iio_buffer_put(struct iio_buffer *buffer)
1965 kref_put(&buffer->ref, iio_buffer_release);
1967 EXPORT_SYMBOL_GPL(iio_buffer_put);
1970 * iio_device_attach_buffer - Attach a buffer to a IIO device
1971 * @indio_dev: The device the buffer should be attached to
1972 * @buffer: The buffer to attach to the device
1974 * Return 0 if successful, negative if error.
1976 * This function attaches a buffer to a IIO device. The buffer stays attached to
1977 * the device until the device is freed. For legacy reasons, the first attached
1978 * buffer will also be assigned to 'indio_dev->buffer'.
1979 * The array allocated here, will be free'd via the iio_device_detach_buffers()
1980 * call which is handled by the iio_device_free().
1982 int iio_device_attach_buffer(struct iio_dev *indio_dev,
1983 struct iio_buffer *buffer)
1985 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
1986 struct iio_buffer **new, **old = iio_dev_opaque->attached_buffers;
1987 unsigned int cnt = iio_dev_opaque->attached_buffers_cnt;
1991 new = krealloc(old, sizeof(*new) * cnt, GFP_KERNEL);
1994 iio_dev_opaque->attached_buffers = new;
1996 buffer = iio_buffer_get(buffer);
1998 /* first buffer is legacy; attach it to the IIO device directly */
1999 if (!indio_dev->buffer)
2000 indio_dev->buffer = buffer;
2002 iio_dev_opaque->attached_buffers[cnt - 1] = buffer;
2003 iio_dev_opaque->attached_buffers_cnt = cnt;
2007 EXPORT_SYMBOL_GPL(iio_device_attach_buffer);