1 /* The industrial I/O core
3 * Copyright (c) 2008 Jonathan Cameron
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * Handling of buffer allocation / resizing.
12 * Things to look at here.
13 * - Better memory allocation techniques?
14 * - Alternative access techniques?
16 #include <linux/kernel.h>
17 #include <linux/export.h>
18 #include <linux/device.h>
20 #include <linux/cdev.h>
21 #include <linux/slab.h>
22 #include <linux/poll.h>
23 #include <linux/sched.h>
25 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/buffer.h>
30 static const char * const iio_endian_prefix[] = {
35 static bool iio_buffer_is_active(struct iio_buffer *buf)
37 return !list_empty(&buf->buffer_list);
40 static bool iio_buffer_data_available(struct iio_buffer *buf)
42 return buf->access->data_available(buf);
46 * iio_buffer_read_first_n_outer() - chrdev read for buffer access
48 * This function relies on all buffer implementations having an
49 * iio_buffer as their first element.
51 ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
52 size_t n, loff_t *f_ps)
54 struct iio_dev *indio_dev = filp->private_data;
55 struct iio_buffer *rb = indio_dev->buffer;
61 if (!rb || !rb->access->read_first_n)
65 if (!iio_buffer_data_available(rb)) {
66 if (filp->f_flags & O_NONBLOCK)
69 ret = wait_event_interruptible(rb->pollq,
70 iio_buffer_data_available(rb) ||
71 indio_dev->info == NULL);
74 if (indio_dev->info == NULL)
78 ret = rb->access->read_first_n(rb, n, buf);
79 if (ret == 0 && (filp->f_flags & O_NONBLOCK))
87 * iio_buffer_poll() - poll the buffer to find out if it has data
89 unsigned int iio_buffer_poll(struct file *filp,
90 struct poll_table_struct *wait)
92 struct iio_dev *indio_dev = filp->private_data;
93 struct iio_buffer *rb = indio_dev->buffer;
98 poll_wait(filp, &rb->pollq, wait);
99 if (iio_buffer_data_available(rb))
100 return POLLIN | POLLRDNORM;
101 /* need a way of knowing if there may be enough data... */
106 * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue
107 * @indio_dev: The IIO device
109 * Wakes up the event waitqueue used for poll(). Should usually
110 * be called when the device is unregistered.
112 void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
114 if (!indio_dev->buffer)
117 wake_up(&indio_dev->buffer->pollq);
120 void iio_buffer_init(struct iio_buffer *buffer)
122 INIT_LIST_HEAD(&buffer->demux_list);
123 INIT_LIST_HEAD(&buffer->buffer_list);
124 init_waitqueue_head(&buffer->pollq);
125 kref_init(&buffer->ref);
127 EXPORT_SYMBOL(iio_buffer_init);
129 static ssize_t iio_show_scan_index(struct device *dev,
130 struct device_attribute *attr,
133 return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
136 static ssize_t iio_show_fixed_type(struct device *dev,
137 struct device_attribute *attr,
140 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
141 u8 type = this_attr->c->scan_type.endianness;
143 if (type == IIO_CPU) {
144 #ifdef __LITTLE_ENDIAN
150 if (this_attr->c->scan_type.repeat > 1)
151 return sprintf(buf, "%s:%c%d/%dX%d>>%u\n",
152 iio_endian_prefix[type],
153 this_attr->c->scan_type.sign,
154 this_attr->c->scan_type.realbits,
155 this_attr->c->scan_type.storagebits,
156 this_attr->c->scan_type.repeat,
157 this_attr->c->scan_type.shift);
159 return sprintf(buf, "%s:%c%d/%d>>%u\n",
160 iio_endian_prefix[type],
161 this_attr->c->scan_type.sign,
162 this_attr->c->scan_type.realbits,
163 this_attr->c->scan_type.storagebits,
164 this_attr->c->scan_type.shift);
167 static ssize_t iio_scan_el_show(struct device *dev,
168 struct device_attribute *attr,
172 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
174 /* Ensure ret is 0 or 1. */
175 ret = !!test_bit(to_iio_dev_attr(attr)->address,
176 indio_dev->buffer->scan_mask);
178 return sprintf(buf, "%d\n", ret);
181 /* Note NULL used as error indicator as it doesn't make sense. */
182 static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
183 unsigned int masklength,
184 const unsigned long *mask)
186 if (bitmap_empty(mask, masklength))
189 if (bitmap_subset(mask, av_masks, masklength))
191 av_masks += BITS_TO_LONGS(masklength);
196 static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
197 const unsigned long *mask)
199 if (!indio_dev->setup_ops->validate_scan_mask)
202 return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
206 * iio_scan_mask_set() - set particular bit in the scan mask
207 * @indio_dev: the iio device
208 * @buffer: the buffer whose scan mask we are interested in
209 * @bit: the bit to be set.
211 * Note that at this point we have no way of knowing what other
212 * buffers might request, hence this code only verifies that the
213 * individual buffers request is plausible.
215 static int iio_scan_mask_set(struct iio_dev *indio_dev,
216 struct iio_buffer *buffer, int bit)
218 const unsigned long *mask;
219 unsigned long *trialmask;
221 trialmask = kmalloc(sizeof(*trialmask)*
222 BITS_TO_LONGS(indio_dev->masklength),
225 if (trialmask == NULL)
227 if (!indio_dev->masklength) {
228 WARN_ON("Trying to set scanmask prior to registering buffer\n");
229 goto err_invalid_mask;
231 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
232 set_bit(bit, trialmask);
234 if (!iio_validate_scan_mask(indio_dev, trialmask))
235 goto err_invalid_mask;
237 if (indio_dev->available_scan_masks) {
238 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
239 indio_dev->masklength,
242 goto err_invalid_mask;
244 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
255 static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
257 clear_bit(bit, buffer->scan_mask);
261 static ssize_t iio_scan_el_store(struct device *dev,
262 struct device_attribute *attr,
268 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
269 struct iio_buffer *buffer = indio_dev->buffer;
270 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
272 ret = strtobool(buf, &state);
275 mutex_lock(&indio_dev->mlock);
276 if (iio_buffer_is_active(indio_dev->buffer)) {
280 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
284 ret = iio_scan_mask_clear(buffer, this_attr->address);
287 } else if (state && !ret) {
288 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
294 mutex_unlock(&indio_dev->mlock);
296 return ret < 0 ? ret : len;
300 static ssize_t iio_scan_el_ts_show(struct device *dev,
301 struct device_attribute *attr,
304 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
305 return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
308 static ssize_t iio_scan_el_ts_store(struct device *dev,
309 struct device_attribute *attr,
314 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
317 ret = strtobool(buf, &state);
321 mutex_lock(&indio_dev->mlock);
322 if (iio_buffer_is_active(indio_dev->buffer)) {
326 indio_dev->buffer->scan_timestamp = state;
328 mutex_unlock(&indio_dev->mlock);
330 return ret ? ret : len;
333 static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
334 const struct iio_chan_spec *chan)
336 int ret, attrcount = 0;
337 struct iio_buffer *buffer = indio_dev->buffer;
339 ret = __iio_add_chan_devattr("index",
341 &iio_show_scan_index,
346 &buffer->scan_el_dev_attr_list);
350 ret = __iio_add_chan_devattr("type",
352 &iio_show_fixed_type,
357 &buffer->scan_el_dev_attr_list);
361 if (chan->type != IIO_TIMESTAMP)
362 ret = __iio_add_chan_devattr("en",
369 &buffer->scan_el_dev_attr_list);
371 ret = __iio_add_chan_devattr("en",
373 &iio_scan_el_ts_show,
374 &iio_scan_el_ts_store,
378 &buffer->scan_el_dev_attr_list);
386 static const char * const iio_scan_elements_group_name = "scan_elements";
388 int iio_buffer_register(struct iio_dev *indio_dev,
389 const struct iio_chan_spec *channels,
392 struct iio_dev_attr *p;
393 struct attribute **attr;
394 struct iio_buffer *buffer = indio_dev->buffer;
395 int ret, i, attrn, attrcount, attrcount_orig = 0;
398 indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs;
400 if (buffer->scan_el_attrs != NULL) {
401 attr = buffer->scan_el_attrs->attrs;
402 while (*attr++ != NULL)
405 attrcount = attrcount_orig;
406 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
409 for (i = 0; i < num_channels; i++) {
410 if (channels[i].scan_index < 0)
413 /* Establish necessary mask length */
414 if (channels[i].scan_index >
415 (int)indio_dev->masklength - 1)
416 indio_dev->masklength
417 = channels[i].scan_index + 1;
419 ret = iio_buffer_add_channel_sysfs(indio_dev,
422 goto error_cleanup_dynamic;
424 if (channels[i].type == IIO_TIMESTAMP)
425 indio_dev->scan_index_timestamp =
426 channels[i].scan_index;
428 if (indio_dev->masklength && buffer->scan_mask == NULL) {
429 buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
430 sizeof(*buffer->scan_mask),
432 if (buffer->scan_mask == NULL) {
434 goto error_cleanup_dynamic;
439 buffer->scan_el_group.name = iio_scan_elements_group_name;
441 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
442 sizeof(buffer->scan_el_group.attrs[0]),
444 if (buffer->scan_el_group.attrs == NULL) {
446 goto error_free_scan_mask;
448 if (buffer->scan_el_attrs)
449 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
450 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
451 attrn = attrcount_orig;
453 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
454 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
455 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
459 error_free_scan_mask:
460 kfree(buffer->scan_mask);
461 error_cleanup_dynamic:
462 iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
466 EXPORT_SYMBOL(iio_buffer_register);
468 void iio_buffer_unregister(struct iio_dev *indio_dev)
470 kfree(indio_dev->buffer->scan_mask);
471 kfree(indio_dev->buffer->scan_el_group.attrs);
472 iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
474 EXPORT_SYMBOL(iio_buffer_unregister);
476 ssize_t iio_buffer_read_length(struct device *dev,
477 struct device_attribute *attr,
480 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
481 struct iio_buffer *buffer = indio_dev->buffer;
483 if (buffer->access->get_length)
484 return sprintf(buf, "%d\n",
485 buffer->access->get_length(buffer));
489 EXPORT_SYMBOL(iio_buffer_read_length);
491 ssize_t iio_buffer_write_length(struct device *dev,
492 struct device_attribute *attr,
496 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
497 struct iio_buffer *buffer = indio_dev->buffer;
501 ret = kstrtouint(buf, 10, &val);
505 if (buffer->access->get_length)
506 if (val == buffer->access->get_length(buffer))
509 mutex_lock(&indio_dev->mlock);
510 if (iio_buffer_is_active(indio_dev->buffer)) {
513 if (buffer->access->set_length)
514 buffer->access->set_length(buffer, val);
517 mutex_unlock(&indio_dev->mlock);
519 return ret ? ret : len;
521 EXPORT_SYMBOL(iio_buffer_write_length);
523 ssize_t iio_buffer_show_enable(struct device *dev,
524 struct device_attribute *attr,
527 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
528 return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
530 EXPORT_SYMBOL(iio_buffer_show_enable);
532 static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
533 const unsigned long *mask, bool timestamp)
535 const struct iio_chan_spec *ch;
539 /* How much space will the demuxed element take? */
540 for_each_set_bit(i, mask,
541 indio_dev->masklength) {
542 ch = iio_find_channel_from_si(indio_dev, i);
543 if (ch->scan_type.repeat > 1)
544 length = ch->scan_type.storagebits / 8 *
545 ch->scan_type.repeat;
547 length = ch->scan_type.storagebits / 8;
548 bytes = ALIGN(bytes, length);
552 ch = iio_find_channel_from_si(indio_dev,
553 indio_dev->scan_index_timestamp);
554 if (ch->scan_type.repeat > 1)
555 length = ch->scan_type.storagebits / 8 *
556 ch->scan_type.repeat;
558 length = ch->scan_type.storagebits / 8;
559 bytes = ALIGN(bytes, length);
565 static void iio_buffer_activate(struct iio_dev *indio_dev,
566 struct iio_buffer *buffer)
568 iio_buffer_get(buffer);
569 list_add(&buffer->buffer_list, &indio_dev->buffer_list);
572 static void iio_buffer_deactivate(struct iio_buffer *buffer)
574 list_del_init(&buffer->buffer_list);
575 iio_buffer_put(buffer);
578 void iio_disable_all_buffers(struct iio_dev *indio_dev)
580 struct iio_buffer *buffer, *_buffer;
582 if (list_empty(&indio_dev->buffer_list))
585 if (indio_dev->setup_ops->predisable)
586 indio_dev->setup_ops->predisable(indio_dev);
588 list_for_each_entry_safe(buffer, _buffer,
589 &indio_dev->buffer_list, buffer_list)
590 iio_buffer_deactivate(buffer);
592 indio_dev->currentmode = INDIO_DIRECT_MODE;
593 if (indio_dev->setup_ops->postdisable)
594 indio_dev->setup_ops->postdisable(indio_dev);
596 if (indio_dev->available_scan_masks == NULL)
597 kfree(indio_dev->active_scan_mask);
600 static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
601 struct iio_buffer *buffer)
605 if (!buffer->access->set_bytes_per_datum)
608 bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
609 buffer->scan_timestamp);
611 buffer->access->set_bytes_per_datum(buffer, bytes);
614 static int __iio_update_buffers(struct iio_dev *indio_dev,
615 struct iio_buffer *insert_buffer,
616 struct iio_buffer *remove_buffer)
620 struct iio_buffer *buffer;
621 unsigned long *compound_mask;
622 const unsigned long *old_mask;
624 /* Wind down existing buffers - iff there are any */
625 if (!list_empty(&indio_dev->buffer_list)) {
626 if (indio_dev->setup_ops->predisable) {
627 ret = indio_dev->setup_ops->predisable(indio_dev);
631 indio_dev->currentmode = INDIO_DIRECT_MODE;
632 if (indio_dev->setup_ops->postdisable) {
633 ret = indio_dev->setup_ops->postdisable(indio_dev);
638 /* Keep a copy of current setup to allow roll back */
639 old_mask = indio_dev->active_scan_mask;
640 if (!indio_dev->available_scan_masks)
641 indio_dev->active_scan_mask = NULL;
644 iio_buffer_deactivate(remove_buffer);
646 iio_buffer_activate(indio_dev, insert_buffer);
648 /* If no buffers in list, we are done */
649 if (list_empty(&indio_dev->buffer_list)) {
650 indio_dev->currentmode = INDIO_DIRECT_MODE;
651 if (indio_dev->available_scan_masks == NULL)
656 /* What scan mask do we actually have? */
657 compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
658 sizeof(long), GFP_KERNEL);
659 if (compound_mask == NULL) {
660 if (indio_dev->available_scan_masks == NULL)
664 indio_dev->scan_timestamp = 0;
666 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
667 bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
668 indio_dev->masklength);
669 indio_dev->scan_timestamp |= buffer->scan_timestamp;
671 if (indio_dev->available_scan_masks) {
672 indio_dev->active_scan_mask =
673 iio_scan_mask_match(indio_dev->available_scan_masks,
674 indio_dev->masklength,
676 if (indio_dev->active_scan_mask == NULL) {
679 * Note can only occur when adding a buffer.
681 iio_buffer_deactivate(insert_buffer);
683 indio_dev->active_scan_mask = old_mask;
687 kfree(compound_mask);
693 indio_dev->active_scan_mask = compound_mask;
696 iio_update_demux(indio_dev);
699 if (indio_dev->setup_ops->preenable) {
700 ret = indio_dev->setup_ops->preenable(indio_dev);
703 "Buffer not started: buffer preenable failed (%d)\n", ret);
704 goto error_remove_inserted;
707 indio_dev->scan_bytes =
708 iio_compute_scan_bytes(indio_dev,
709 indio_dev->active_scan_mask,
710 indio_dev->scan_timestamp);
711 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
712 iio_buffer_update_bytes_per_datum(indio_dev, buffer);
713 if (buffer->access->request_update) {
714 ret = buffer->access->request_update(buffer);
717 "Buffer not started: buffer parameter update failed (%d)\n", ret);
718 goto error_run_postdisable;
722 if (indio_dev->info->update_scan_mode) {
723 ret = indio_dev->info
724 ->update_scan_mode(indio_dev,
725 indio_dev->active_scan_mask);
727 printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret);
728 goto error_run_postdisable;
731 /* Definitely possible for devices to support both of these. */
732 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
733 if (!indio_dev->trig) {
734 printk(KERN_INFO "Buffer not started: no trigger\n");
736 /* Can only occur on first buffer */
737 goto error_run_postdisable;
739 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
740 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) {
741 indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
742 } else { /* Should never be reached */
744 goto error_run_postdisable;
747 if (indio_dev->setup_ops->postenable) {
748 ret = indio_dev->setup_ops->postenable(indio_dev);
751 "Buffer not started: postenable failed (%d)\n", ret);
752 indio_dev->currentmode = INDIO_DIRECT_MODE;
753 if (indio_dev->setup_ops->postdisable)
754 indio_dev->setup_ops->postdisable(indio_dev);
755 goto error_disable_all_buffers;
759 if (indio_dev->available_scan_masks)
760 kfree(compound_mask);
766 error_disable_all_buffers:
767 indio_dev->currentmode = INDIO_DIRECT_MODE;
768 error_run_postdisable:
769 if (indio_dev->setup_ops->postdisable)
770 indio_dev->setup_ops->postdisable(indio_dev);
771 error_remove_inserted:
773 iio_buffer_deactivate(insert_buffer);
774 indio_dev->active_scan_mask = old_mask;
775 kfree(compound_mask);
779 int iio_update_buffers(struct iio_dev *indio_dev,
780 struct iio_buffer *insert_buffer,
781 struct iio_buffer *remove_buffer)
785 if (insert_buffer == remove_buffer)
788 mutex_lock(&indio_dev->info_exist_lock);
789 mutex_lock(&indio_dev->mlock);
791 if (insert_buffer && iio_buffer_is_active(insert_buffer))
792 insert_buffer = NULL;
794 if (remove_buffer && !iio_buffer_is_active(remove_buffer))
795 remove_buffer = NULL;
797 if (!insert_buffer && !remove_buffer) {
802 if (indio_dev->info == NULL) {
807 ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
810 mutex_unlock(&indio_dev->mlock);
811 mutex_unlock(&indio_dev->info_exist_lock);
815 EXPORT_SYMBOL_GPL(iio_update_buffers);
817 ssize_t iio_buffer_store_enable(struct device *dev,
818 struct device_attribute *attr,
823 bool requested_state;
824 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
827 ret = strtobool(buf, &requested_state);
831 mutex_lock(&indio_dev->mlock);
833 /* Find out if it is in the list */
834 inlist = iio_buffer_is_active(indio_dev->buffer);
835 /* Already in desired state */
836 if (inlist == requested_state)
840 ret = __iio_update_buffers(indio_dev,
841 indio_dev->buffer, NULL);
843 ret = __iio_update_buffers(indio_dev,
844 NULL, indio_dev->buffer);
849 mutex_unlock(&indio_dev->mlock);
850 return (ret < 0) ? ret : len;
852 EXPORT_SYMBOL(iio_buffer_store_enable);
855 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
856 * @indio_dev: the iio device
857 * @mask: scan mask to be checked
859 * Return true if exactly one bit is set in the scan mask, false otherwise. It
860 * can be used for devices where only one channel can be active for sampling at
863 bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
864 const unsigned long *mask)
866 return bitmap_weight(mask, indio_dev->masklength) == 1;
868 EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
870 int iio_scan_mask_query(struct iio_dev *indio_dev,
871 struct iio_buffer *buffer, int bit)
873 if (bit > indio_dev->masklength)
876 if (!buffer->scan_mask)
879 /* Ensure return value is 0 or 1. */
880 return !!test_bit(bit, buffer->scan_mask);
882 EXPORT_SYMBOL_GPL(iio_scan_mask_query);
885 * struct iio_demux_table() - table describing demux memcpy ops
886 * @from: index to copy from
887 * @to: index to copy to
888 * @length: how many bytes to copy
889 * @l: list head used for management
891 struct iio_demux_table {
898 static const void *iio_demux(struct iio_buffer *buffer,
901 struct iio_demux_table *t;
903 if (list_empty(&buffer->demux_list))
905 list_for_each_entry(t, &buffer->demux_list, l)
906 memcpy(buffer->demux_bounce + t->to,
907 datain + t->from, t->length);
909 return buffer->demux_bounce;
912 static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
914 const void *dataout = iio_demux(buffer, data);
916 return buffer->access->store_to(buffer, dataout);
919 static void iio_buffer_demux_free(struct iio_buffer *buffer)
921 struct iio_demux_table *p, *q;
922 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
929 int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
932 struct iio_buffer *buf;
934 list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
935 ret = iio_push_to_buffer(buf, data);
942 EXPORT_SYMBOL_GPL(iio_push_to_buffers);
944 static int iio_buffer_add_demux(struct iio_buffer *buffer,
945 struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc,
949 if (*p && (*p)->from + (*p)->length == in_loc &&
950 (*p)->to + (*p)->length == out_loc) {
951 (*p)->length += length;
953 *p = kmalloc(sizeof(**p), GFP_KERNEL);
958 (*p)->length = length;
959 list_add_tail(&(*p)->l, &buffer->demux_list);
965 static int iio_buffer_update_demux(struct iio_dev *indio_dev,
966 struct iio_buffer *buffer)
968 const struct iio_chan_spec *ch;
969 int ret, in_ind = -1, out_ind, length;
970 unsigned in_loc = 0, out_loc = 0;
971 struct iio_demux_table *p = NULL;
973 /* Clear out any old demux */
974 iio_buffer_demux_free(buffer);
975 kfree(buffer->demux_bounce);
976 buffer->demux_bounce = NULL;
978 /* First work out which scan mode we will actually have */
979 if (bitmap_equal(indio_dev->active_scan_mask,
981 indio_dev->masklength))
984 /* Now we have the two masks, work from least sig and build up sizes */
985 for_each_set_bit(out_ind,
987 indio_dev->masklength) {
988 in_ind = find_next_bit(indio_dev->active_scan_mask,
989 indio_dev->masklength,
991 while (in_ind != out_ind) {
992 in_ind = find_next_bit(indio_dev->active_scan_mask,
993 indio_dev->masklength,
995 ch = iio_find_channel_from_si(indio_dev, in_ind);
996 if (ch->scan_type.repeat > 1)
997 length = ch->scan_type.storagebits / 8 *
998 ch->scan_type.repeat;
1000 length = ch->scan_type.storagebits / 8;
1001 /* Make sure we are aligned */
1002 in_loc = roundup(in_loc, length) + length;
1004 ch = iio_find_channel_from_si(indio_dev, in_ind);
1005 if (ch->scan_type.repeat > 1)
1006 length = ch->scan_type.storagebits / 8 *
1007 ch->scan_type.repeat;
1009 length = ch->scan_type.storagebits / 8;
1010 out_loc = roundup(out_loc, length);
1011 in_loc = roundup(in_loc, length);
1012 ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
1014 goto error_clear_mux_table;
1018 /* Relies on scan_timestamp being last */
1019 if (buffer->scan_timestamp) {
1020 ch = iio_find_channel_from_si(indio_dev,
1021 indio_dev->scan_index_timestamp);
1022 if (ch->scan_type.repeat > 1)
1023 length = ch->scan_type.storagebits / 8 *
1024 ch->scan_type.repeat;
1026 length = ch->scan_type.storagebits / 8;
1027 out_loc = roundup(out_loc, length);
1028 in_loc = roundup(in_loc, length);
1029 ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
1031 goto error_clear_mux_table;
1035 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
1036 if (buffer->demux_bounce == NULL) {
1038 goto error_clear_mux_table;
1042 error_clear_mux_table:
1043 iio_buffer_demux_free(buffer);
1048 int iio_update_demux(struct iio_dev *indio_dev)
1050 struct iio_buffer *buffer;
1053 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
1054 ret = iio_buffer_update_demux(indio_dev, buffer);
1056 goto error_clear_mux_table;
1060 error_clear_mux_table:
1061 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
1062 iio_buffer_demux_free(buffer);
1066 EXPORT_SYMBOL_GPL(iio_update_demux);
1069 * iio_buffer_release() - Free a buffer's resources
1070 * @ref: Pointer to the kref embedded in the iio_buffer struct
1072 * This function is called when the last reference to the buffer has been
1073 * dropped. It will typically free all resources allocated by the buffer. Do not
1074 * call this function manually, always use iio_buffer_put() when done using a
1077 static void iio_buffer_release(struct kref *ref)
1079 struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
1081 buffer->access->release(buffer);
1085 * iio_buffer_get() - Grab a reference to the buffer
1086 * @buffer: The buffer to grab a reference for, may be NULL
1088 * Returns the pointer to the buffer that was passed into the function.
1090 struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
1093 kref_get(&buffer->ref);
1097 EXPORT_SYMBOL_GPL(iio_buffer_get);
1100 * iio_buffer_put() - Release the reference to the buffer
1101 * @buffer: The buffer to release the reference for, may be NULL
1103 void iio_buffer_put(struct iio_buffer *buffer)
1106 kref_put(&buffer->ref, iio_buffer_release);
1108 EXPORT_SYMBOL_GPL(iio_buffer_put);