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/kernel.h>
13 #include <linux/export.h>
14 #include <linux/device.h>
16 #include <linux/cdev.h>
17 #include <linux/slab.h>
18 #include <linux/poll.h>
19 #include <linux/sched/signal.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/iio-opaque.h>
24 #include "iio_core_trigger.h"
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/buffer_impl.h>
29 static const char * const iio_endian_prefix
[] = {
34 static bool iio_buffer_is_active(struct iio_buffer
*buf
)
36 return !list_empty(&buf
->buffer_list
);
39 static size_t iio_buffer_data_available(struct iio_buffer
*buf
)
41 return buf
->access
->data_available(buf
);
44 static int iio_buffer_flush_hwfifo(struct iio_dev
*indio_dev
,
45 struct iio_buffer
*buf
, size_t required
)
47 if (!indio_dev
->info
->hwfifo_flush_to_buffer
)
50 return indio_dev
->info
->hwfifo_flush_to_buffer(indio_dev
, required
);
53 static bool iio_buffer_ready(struct iio_dev
*indio_dev
, struct iio_buffer
*buf
,
54 size_t to_wait
, int to_flush
)
59 /* wakeup if the device was unregistered */
63 /* drain the buffer if it was disabled */
64 if (!iio_buffer_is_active(buf
)) {
65 to_wait
= min_t(size_t, to_wait
, 1);
69 avail
= iio_buffer_data_available(buf
);
71 if (avail
>= to_wait
) {
72 /* force a flush for non-blocking reads */
73 if (!to_wait
&& avail
< to_flush
)
74 iio_buffer_flush_hwfifo(indio_dev
, buf
,
80 flushed
= iio_buffer_flush_hwfifo(indio_dev
, buf
,
85 if (avail
+ flushed
>= to_wait
)
92 * iio_buffer_read_outer() - chrdev read for buffer access
93 * @filp: File structure pointer for the char device
94 * @buf: Destination buffer for iio buffer read
95 * @n: First n bytes to read
96 * @f_ps: Long offset provided by the user as a seek position
98 * This function relies on all buffer implementations having an
99 * iio_buffer as their first element.
101 * Return: negative values corresponding to error codes or ret != 0
102 * for ending the reading activity
104 ssize_t
iio_buffer_read_outer(struct file
*filp
, char __user
*buf
,
105 size_t n
, loff_t
*f_ps
)
107 struct iio_dev
*indio_dev
= filp
->private_data
;
108 struct iio_buffer
*rb
= indio_dev
->buffer
;
109 DEFINE_WAIT_FUNC(wait
, woken_wake_function
);
114 if (!indio_dev
->info
)
117 if (!rb
|| !rb
->access
->read
)
120 datum_size
= rb
->bytes_per_datum
;
123 * If datum_size is 0 there will never be anything to read from the
124 * buffer, so signal end of file now.
129 if (filp
->f_flags
& O_NONBLOCK
)
132 to_wait
= min_t(size_t, n
/ datum_size
, rb
->watermark
);
134 add_wait_queue(&rb
->pollq
, &wait
);
136 if (!indio_dev
->info
) {
141 if (!iio_buffer_ready(indio_dev
, rb
, to_wait
, n
/ datum_size
)) {
142 if (signal_pending(current
)) {
147 wait_woken(&wait
, TASK_INTERRUPTIBLE
,
148 MAX_SCHEDULE_TIMEOUT
);
152 ret
= rb
->access
->read(rb
, n
, buf
);
153 if (ret
== 0 && (filp
->f_flags
& O_NONBLOCK
))
156 remove_wait_queue(&rb
->pollq
, &wait
);
162 * iio_buffer_poll() - poll the buffer to find out if it has data
163 * @filp: File structure pointer for device access
164 * @wait: Poll table structure pointer for which the driver adds
167 * Return: (EPOLLIN | EPOLLRDNORM) if data is available for reading
168 * or 0 for other cases
170 __poll_t
iio_buffer_poll(struct file
*filp
,
171 struct poll_table_struct
*wait
)
173 struct iio_dev
*indio_dev
= filp
->private_data
;
174 struct iio_buffer
*rb
= indio_dev
->buffer
;
176 if (!indio_dev
->info
|| rb
== NULL
)
179 poll_wait(filp
, &rb
->pollq
, wait
);
180 if (iio_buffer_ready(indio_dev
, rb
, rb
->watermark
, 0))
181 return EPOLLIN
| EPOLLRDNORM
;
186 * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue
187 * @indio_dev: The IIO device
189 * Wakes up the event waitqueue used for poll(). Should usually
190 * be called when the device is unregistered.
192 void iio_buffer_wakeup_poll(struct iio_dev
*indio_dev
)
194 struct iio_buffer
*buffer
= indio_dev
->buffer
;
199 wake_up(&buffer
->pollq
);
202 void iio_buffer_init(struct iio_buffer
*buffer
)
204 INIT_LIST_HEAD(&buffer
->demux_list
);
205 INIT_LIST_HEAD(&buffer
->buffer_list
);
206 init_waitqueue_head(&buffer
->pollq
);
207 kref_init(&buffer
->ref
);
208 if (!buffer
->watermark
)
209 buffer
->watermark
= 1;
211 EXPORT_SYMBOL(iio_buffer_init
);
213 static ssize_t
iio_show_scan_index(struct device
*dev
,
214 struct device_attribute
*attr
,
217 return sprintf(buf
, "%u\n", to_iio_dev_attr(attr
)->c
->scan_index
);
220 static ssize_t
iio_show_fixed_type(struct device
*dev
,
221 struct device_attribute
*attr
,
224 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
225 u8 type
= this_attr
->c
->scan_type
.endianness
;
227 if (type
== IIO_CPU
) {
228 #ifdef __LITTLE_ENDIAN
234 if (this_attr
->c
->scan_type
.repeat
> 1)
235 return sprintf(buf
, "%s:%c%d/%dX%d>>%u\n",
236 iio_endian_prefix
[type
],
237 this_attr
->c
->scan_type
.sign
,
238 this_attr
->c
->scan_type
.realbits
,
239 this_attr
->c
->scan_type
.storagebits
,
240 this_attr
->c
->scan_type
.repeat
,
241 this_attr
->c
->scan_type
.shift
);
243 return sprintf(buf
, "%s:%c%d/%d>>%u\n",
244 iio_endian_prefix
[type
],
245 this_attr
->c
->scan_type
.sign
,
246 this_attr
->c
->scan_type
.realbits
,
247 this_attr
->c
->scan_type
.storagebits
,
248 this_attr
->c
->scan_type
.shift
);
251 static ssize_t
iio_scan_el_show(struct device
*dev
,
252 struct device_attribute
*attr
,
256 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
257 struct iio_buffer
*buffer
= indio_dev
->buffer
;
259 /* Ensure ret is 0 or 1. */
260 ret
= !!test_bit(to_iio_dev_attr(attr
)->address
,
263 return sprintf(buf
, "%d\n", ret
);
266 /* Note NULL used as error indicator as it doesn't make sense. */
267 static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks
,
268 unsigned int masklength
,
269 const unsigned long *mask
,
272 if (bitmap_empty(mask
, masklength
))
276 if (bitmap_equal(mask
, av_masks
, masklength
))
279 if (bitmap_subset(mask
, av_masks
, masklength
))
282 av_masks
+= BITS_TO_LONGS(masklength
);
287 static bool iio_validate_scan_mask(struct iio_dev
*indio_dev
,
288 const unsigned long *mask
)
290 if (!indio_dev
->setup_ops
->validate_scan_mask
)
293 return indio_dev
->setup_ops
->validate_scan_mask(indio_dev
, mask
);
297 * iio_scan_mask_set() - set particular bit in the scan mask
298 * @indio_dev: the iio device
299 * @buffer: the buffer whose scan mask we are interested in
300 * @bit: the bit to be set.
302 * Note that at this point we have no way of knowing what other
303 * buffers might request, hence this code only verifies that the
304 * individual buffers request is plausible.
306 static int iio_scan_mask_set(struct iio_dev
*indio_dev
,
307 struct iio_buffer
*buffer
, int bit
)
309 const unsigned long *mask
;
310 unsigned long *trialmask
;
312 trialmask
= bitmap_zalloc(indio_dev
->masklength
, GFP_KERNEL
);
313 if (trialmask
== NULL
)
315 if (!indio_dev
->masklength
) {
316 WARN(1, "Trying to set scanmask prior to registering buffer\n");
317 goto err_invalid_mask
;
319 bitmap_copy(trialmask
, buffer
->scan_mask
, indio_dev
->masklength
);
320 set_bit(bit
, trialmask
);
322 if (!iio_validate_scan_mask(indio_dev
, trialmask
))
323 goto err_invalid_mask
;
325 if (indio_dev
->available_scan_masks
) {
326 mask
= iio_scan_mask_match(indio_dev
->available_scan_masks
,
327 indio_dev
->masklength
,
330 goto err_invalid_mask
;
332 bitmap_copy(buffer
->scan_mask
, trialmask
, indio_dev
->masklength
);
334 bitmap_free(trialmask
);
339 bitmap_free(trialmask
);
343 static int iio_scan_mask_clear(struct iio_buffer
*buffer
, int bit
)
345 clear_bit(bit
, buffer
->scan_mask
);
349 static int iio_scan_mask_query(struct iio_dev
*indio_dev
,
350 struct iio_buffer
*buffer
, int bit
)
352 if (bit
> indio_dev
->masklength
)
355 if (!buffer
->scan_mask
)
358 /* Ensure return value is 0 or 1. */
359 return !!test_bit(bit
, buffer
->scan_mask
);
362 static ssize_t
iio_scan_el_store(struct device
*dev
,
363 struct device_attribute
*attr
,
369 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
370 struct iio_buffer
*buffer
= indio_dev
->buffer
;
371 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
373 ret
= strtobool(buf
, &state
);
376 mutex_lock(&indio_dev
->mlock
);
377 if (iio_buffer_is_active(buffer
)) {
381 ret
= iio_scan_mask_query(indio_dev
, buffer
, this_attr
->address
);
385 ret
= iio_scan_mask_clear(buffer
, this_attr
->address
);
388 } else if (state
&& !ret
) {
389 ret
= iio_scan_mask_set(indio_dev
, buffer
, this_attr
->address
);
395 mutex_unlock(&indio_dev
->mlock
);
397 return ret
< 0 ? ret
: len
;
401 static ssize_t
iio_scan_el_ts_show(struct device
*dev
,
402 struct device_attribute
*attr
,
405 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
406 struct iio_buffer
*buffer
= indio_dev
->buffer
;
408 return sprintf(buf
, "%d\n", buffer
->scan_timestamp
);
411 static ssize_t
iio_scan_el_ts_store(struct device
*dev
,
412 struct device_attribute
*attr
,
417 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
418 struct iio_buffer
*buffer
= indio_dev
->buffer
;
421 ret
= strtobool(buf
, &state
);
425 mutex_lock(&indio_dev
->mlock
);
426 if (iio_buffer_is_active(buffer
)) {
430 buffer
->scan_timestamp
= state
;
432 mutex_unlock(&indio_dev
->mlock
);
434 return ret
? ret
: len
;
437 static int iio_buffer_add_channel_sysfs(struct iio_dev
*indio_dev
,
438 struct iio_buffer
*buffer
,
439 const struct iio_chan_spec
*chan
)
441 int ret
, attrcount
= 0;
443 ret
= __iio_add_chan_devattr("index",
445 &iio_show_scan_index
,
450 &buffer
->scan_el_dev_attr_list
);
454 ret
= __iio_add_chan_devattr("type",
456 &iio_show_fixed_type
,
461 &buffer
->scan_el_dev_attr_list
);
465 if (chan
->type
!= IIO_TIMESTAMP
)
466 ret
= __iio_add_chan_devattr("en",
473 &buffer
->scan_el_dev_attr_list
);
475 ret
= __iio_add_chan_devattr("en",
477 &iio_scan_el_ts_show
,
478 &iio_scan_el_ts_store
,
482 &buffer
->scan_el_dev_attr_list
);
490 static ssize_t
iio_buffer_read_length(struct device
*dev
,
491 struct device_attribute
*attr
,
494 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
495 struct iio_buffer
*buffer
= indio_dev
->buffer
;
497 return sprintf(buf
, "%d\n", buffer
->length
);
500 static ssize_t
iio_buffer_write_length(struct device
*dev
,
501 struct device_attribute
*attr
,
502 const char *buf
, size_t len
)
504 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
505 struct iio_buffer
*buffer
= indio_dev
->buffer
;
509 ret
= kstrtouint(buf
, 10, &val
);
513 if (val
== buffer
->length
)
516 mutex_lock(&indio_dev
->mlock
);
517 if (iio_buffer_is_active(buffer
)) {
520 buffer
->access
->set_length(buffer
, val
);
525 if (buffer
->length
&& buffer
->length
< buffer
->watermark
)
526 buffer
->watermark
= buffer
->length
;
528 mutex_unlock(&indio_dev
->mlock
);
530 return ret
? ret
: len
;
533 static ssize_t
iio_buffer_show_enable(struct device
*dev
,
534 struct device_attribute
*attr
,
537 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
538 struct iio_buffer
*buffer
= indio_dev
->buffer
;
540 return sprintf(buf
, "%d\n", iio_buffer_is_active(buffer
));
543 static unsigned int iio_storage_bytes_for_si(struct iio_dev
*indio_dev
,
544 unsigned int scan_index
)
546 const struct iio_chan_spec
*ch
;
549 ch
= iio_find_channel_from_si(indio_dev
, scan_index
);
550 bytes
= ch
->scan_type
.storagebits
/ 8;
551 if (ch
->scan_type
.repeat
> 1)
552 bytes
*= ch
->scan_type
.repeat
;
556 static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev
*indio_dev
)
558 return iio_storage_bytes_for_si(indio_dev
,
559 indio_dev
->scan_index_timestamp
);
562 static int iio_compute_scan_bytes(struct iio_dev
*indio_dev
,
563 const unsigned long *mask
, bool timestamp
)
566 int length
, i
, largest
= 0;
568 /* How much space will the demuxed element take? */
569 for_each_set_bit(i
, mask
,
570 indio_dev
->masklength
) {
571 length
= iio_storage_bytes_for_si(indio_dev
, i
);
572 bytes
= ALIGN(bytes
, length
);
574 largest
= max(largest
, length
);
578 length
= iio_storage_bytes_for_timestamp(indio_dev
);
579 bytes
= ALIGN(bytes
, length
);
581 largest
= max(largest
, length
);
584 bytes
= ALIGN(bytes
, largest
);
588 static void iio_buffer_activate(struct iio_dev
*indio_dev
,
589 struct iio_buffer
*buffer
)
591 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
593 iio_buffer_get(buffer
);
594 list_add(&buffer
->buffer_list
, &iio_dev_opaque
->buffer_list
);
597 static void iio_buffer_deactivate(struct iio_buffer
*buffer
)
599 list_del_init(&buffer
->buffer_list
);
600 wake_up_interruptible(&buffer
->pollq
);
601 iio_buffer_put(buffer
);
604 static void iio_buffer_deactivate_all(struct iio_dev
*indio_dev
)
606 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
607 struct iio_buffer
*buffer
, *_buffer
;
609 list_for_each_entry_safe(buffer
, _buffer
,
610 &iio_dev_opaque
->buffer_list
, buffer_list
)
611 iio_buffer_deactivate(buffer
);
614 static int iio_buffer_enable(struct iio_buffer
*buffer
,
615 struct iio_dev
*indio_dev
)
617 if (!buffer
->access
->enable
)
619 return buffer
->access
->enable(buffer
, indio_dev
);
622 static int iio_buffer_disable(struct iio_buffer
*buffer
,
623 struct iio_dev
*indio_dev
)
625 if (!buffer
->access
->disable
)
627 return buffer
->access
->disable(buffer
, indio_dev
);
630 static void iio_buffer_update_bytes_per_datum(struct iio_dev
*indio_dev
,
631 struct iio_buffer
*buffer
)
635 if (!buffer
->access
->set_bytes_per_datum
)
638 bytes
= iio_compute_scan_bytes(indio_dev
, buffer
->scan_mask
,
639 buffer
->scan_timestamp
);
641 buffer
->access
->set_bytes_per_datum(buffer
, bytes
);
644 static int iio_buffer_request_update(struct iio_dev
*indio_dev
,
645 struct iio_buffer
*buffer
)
649 iio_buffer_update_bytes_per_datum(indio_dev
, buffer
);
650 if (buffer
->access
->request_update
) {
651 ret
= buffer
->access
->request_update(buffer
);
653 dev_dbg(&indio_dev
->dev
,
654 "Buffer not started: buffer parameter update failed (%d)\n",
663 static void iio_free_scan_mask(struct iio_dev
*indio_dev
,
664 const unsigned long *mask
)
666 /* If the mask is dynamically allocated free it, otherwise do nothing */
667 if (!indio_dev
->available_scan_masks
)
671 struct iio_device_config
{
673 unsigned int watermark
;
674 const unsigned long *scan_mask
;
675 unsigned int scan_bytes
;
679 static int iio_verify_update(struct iio_dev
*indio_dev
,
680 struct iio_buffer
*insert_buffer
, struct iio_buffer
*remove_buffer
,
681 struct iio_device_config
*config
)
683 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
684 unsigned long *compound_mask
;
685 const unsigned long *scan_mask
;
686 bool strict_scanmask
= false;
687 struct iio_buffer
*buffer
;
692 bitmap_empty(insert_buffer
->scan_mask
, indio_dev
->masklength
)) {
693 dev_dbg(&indio_dev
->dev
,
694 "At least one scan element must be enabled first\n");
698 memset(config
, 0, sizeof(*config
));
699 config
->watermark
= ~0;
702 * If there is just one buffer and we are removing it there is nothing
705 if (remove_buffer
&& !insert_buffer
&&
706 list_is_singular(&iio_dev_opaque
->buffer_list
))
709 modes
= indio_dev
->modes
;
711 list_for_each_entry(buffer
, &iio_dev_opaque
->buffer_list
, buffer_list
) {
712 if (buffer
== remove_buffer
)
714 modes
&= buffer
->access
->modes
;
715 config
->watermark
= min(config
->watermark
, buffer
->watermark
);
719 modes
&= insert_buffer
->access
->modes
;
720 config
->watermark
= min(config
->watermark
,
721 insert_buffer
->watermark
);
724 /* Definitely possible for devices to support both of these. */
725 if ((modes
& INDIO_BUFFER_TRIGGERED
) && indio_dev
->trig
) {
726 config
->mode
= INDIO_BUFFER_TRIGGERED
;
727 } else if (modes
& INDIO_BUFFER_HARDWARE
) {
729 * Keep things simple for now and only allow a single buffer to
730 * be connected in hardware mode.
732 if (insert_buffer
&& !list_empty(&iio_dev_opaque
->buffer_list
))
734 config
->mode
= INDIO_BUFFER_HARDWARE
;
735 strict_scanmask
= true;
736 } else if (modes
& INDIO_BUFFER_SOFTWARE
) {
737 config
->mode
= INDIO_BUFFER_SOFTWARE
;
739 /* Can only occur on first buffer */
740 if (indio_dev
->modes
& INDIO_BUFFER_TRIGGERED
)
741 dev_dbg(&indio_dev
->dev
, "Buffer not started: no trigger\n");
745 /* What scan mask do we actually have? */
746 compound_mask
= bitmap_zalloc(indio_dev
->masklength
, GFP_KERNEL
);
747 if (compound_mask
== NULL
)
750 scan_timestamp
= false;
752 list_for_each_entry(buffer
, &iio_dev_opaque
->buffer_list
, buffer_list
) {
753 if (buffer
== remove_buffer
)
755 bitmap_or(compound_mask
, compound_mask
, buffer
->scan_mask
,
756 indio_dev
->masklength
);
757 scan_timestamp
|= buffer
->scan_timestamp
;
761 bitmap_or(compound_mask
, compound_mask
,
762 insert_buffer
->scan_mask
, indio_dev
->masklength
);
763 scan_timestamp
|= insert_buffer
->scan_timestamp
;
766 if (indio_dev
->available_scan_masks
) {
767 scan_mask
= iio_scan_mask_match(indio_dev
->available_scan_masks
,
768 indio_dev
->masklength
,
771 bitmap_free(compound_mask
);
772 if (scan_mask
== NULL
)
775 scan_mask
= compound_mask
;
778 config
->scan_bytes
= iio_compute_scan_bytes(indio_dev
,
779 scan_mask
, scan_timestamp
);
780 config
->scan_mask
= scan_mask
;
781 config
->scan_timestamp
= scan_timestamp
;
787 * struct iio_demux_table - table describing demux memcpy ops
788 * @from: index to copy from
789 * @to: index to copy to
790 * @length: how many bytes to copy
791 * @l: list head used for management
793 struct iio_demux_table
{
800 static void iio_buffer_demux_free(struct iio_buffer
*buffer
)
802 struct iio_demux_table
*p
, *q
;
803 list_for_each_entry_safe(p
, q
, &buffer
->demux_list
, l
) {
809 static int iio_buffer_add_demux(struct iio_buffer
*buffer
,
810 struct iio_demux_table
**p
, unsigned int in_loc
, unsigned int out_loc
,
814 if (*p
&& (*p
)->from
+ (*p
)->length
== in_loc
&&
815 (*p
)->to
+ (*p
)->length
== out_loc
) {
816 (*p
)->length
+= length
;
818 *p
= kmalloc(sizeof(**p
), GFP_KERNEL
);
823 (*p
)->length
= length
;
824 list_add_tail(&(*p
)->l
, &buffer
->demux_list
);
830 static int iio_buffer_update_demux(struct iio_dev
*indio_dev
,
831 struct iio_buffer
*buffer
)
833 int ret
, in_ind
= -1, out_ind
, length
;
834 unsigned in_loc
= 0, out_loc
= 0;
835 struct iio_demux_table
*p
= NULL
;
837 /* Clear out any old demux */
838 iio_buffer_demux_free(buffer
);
839 kfree(buffer
->demux_bounce
);
840 buffer
->demux_bounce
= NULL
;
842 /* First work out which scan mode we will actually have */
843 if (bitmap_equal(indio_dev
->active_scan_mask
,
845 indio_dev
->masklength
))
848 /* Now we have the two masks, work from least sig and build up sizes */
849 for_each_set_bit(out_ind
,
851 indio_dev
->masklength
) {
852 in_ind
= find_next_bit(indio_dev
->active_scan_mask
,
853 indio_dev
->masklength
,
855 while (in_ind
!= out_ind
) {
856 length
= iio_storage_bytes_for_si(indio_dev
, in_ind
);
857 /* Make sure we are aligned */
858 in_loc
= roundup(in_loc
, length
) + length
;
859 in_ind
= find_next_bit(indio_dev
->active_scan_mask
,
860 indio_dev
->masklength
,
863 length
= iio_storage_bytes_for_si(indio_dev
, in_ind
);
864 out_loc
= roundup(out_loc
, length
);
865 in_loc
= roundup(in_loc
, length
);
866 ret
= iio_buffer_add_demux(buffer
, &p
, in_loc
, out_loc
, length
);
868 goto error_clear_mux_table
;
872 /* Relies on scan_timestamp being last */
873 if (buffer
->scan_timestamp
) {
874 length
= iio_storage_bytes_for_timestamp(indio_dev
);
875 out_loc
= roundup(out_loc
, length
);
876 in_loc
= roundup(in_loc
, length
);
877 ret
= iio_buffer_add_demux(buffer
, &p
, in_loc
, out_loc
, length
);
879 goto error_clear_mux_table
;
883 buffer
->demux_bounce
= kzalloc(out_loc
, GFP_KERNEL
);
884 if (buffer
->demux_bounce
== NULL
) {
886 goto error_clear_mux_table
;
890 error_clear_mux_table
:
891 iio_buffer_demux_free(buffer
);
896 static int iio_update_demux(struct iio_dev
*indio_dev
)
898 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
899 struct iio_buffer
*buffer
;
902 list_for_each_entry(buffer
, &iio_dev_opaque
->buffer_list
, buffer_list
) {
903 ret
= iio_buffer_update_demux(indio_dev
, buffer
);
905 goto error_clear_mux_table
;
909 error_clear_mux_table
:
910 list_for_each_entry(buffer
, &iio_dev_opaque
->buffer_list
, buffer_list
)
911 iio_buffer_demux_free(buffer
);
916 static int iio_enable_buffers(struct iio_dev
*indio_dev
,
917 struct iio_device_config
*config
)
919 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
920 struct iio_buffer
*buffer
;
923 indio_dev
->active_scan_mask
= config
->scan_mask
;
924 indio_dev
->scan_timestamp
= config
->scan_timestamp
;
925 indio_dev
->scan_bytes
= config
->scan_bytes
;
926 indio_dev
->currentmode
= config
->mode
;
928 iio_update_demux(indio_dev
);
931 if (indio_dev
->setup_ops
->preenable
) {
932 ret
= indio_dev
->setup_ops
->preenable(indio_dev
);
934 dev_dbg(&indio_dev
->dev
,
935 "Buffer not started: buffer preenable failed (%d)\n", ret
);
936 goto err_undo_config
;
940 if (indio_dev
->info
->update_scan_mode
) {
941 ret
= indio_dev
->info
942 ->update_scan_mode(indio_dev
,
943 indio_dev
->active_scan_mask
);
945 dev_dbg(&indio_dev
->dev
,
946 "Buffer not started: update scan mode failed (%d)\n",
948 goto err_run_postdisable
;
952 if (indio_dev
->info
->hwfifo_set_watermark
)
953 indio_dev
->info
->hwfifo_set_watermark(indio_dev
,
956 list_for_each_entry(buffer
, &iio_dev_opaque
->buffer_list
, buffer_list
) {
957 ret
= iio_buffer_enable(buffer
, indio_dev
);
959 goto err_disable_buffers
;
962 if (indio_dev
->currentmode
== INDIO_BUFFER_TRIGGERED
) {
963 ret
= iio_trigger_attach_poll_func(indio_dev
->trig
,
964 indio_dev
->pollfunc
);
966 goto err_disable_buffers
;
969 if (indio_dev
->setup_ops
->postenable
) {
970 ret
= indio_dev
->setup_ops
->postenable(indio_dev
);
972 dev_dbg(&indio_dev
->dev
,
973 "Buffer not started: postenable failed (%d)\n", ret
);
974 goto err_detach_pollfunc
;
981 if (indio_dev
->currentmode
== INDIO_BUFFER_TRIGGERED
) {
982 iio_trigger_detach_poll_func(indio_dev
->trig
,
983 indio_dev
->pollfunc
);
986 list_for_each_entry_continue_reverse(buffer
, &iio_dev_opaque
->buffer_list
,
988 iio_buffer_disable(buffer
, indio_dev
);
990 if (indio_dev
->setup_ops
->postdisable
)
991 indio_dev
->setup_ops
->postdisable(indio_dev
);
993 indio_dev
->currentmode
= INDIO_DIRECT_MODE
;
994 indio_dev
->active_scan_mask
= NULL
;
999 static int iio_disable_buffers(struct iio_dev
*indio_dev
)
1001 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
1002 struct iio_buffer
*buffer
;
1006 /* Wind down existing buffers - iff there are any */
1007 if (list_empty(&iio_dev_opaque
->buffer_list
))
1011 * If things go wrong at some step in disable we still need to continue
1012 * to perform the other steps, otherwise we leave the device in a
1013 * inconsistent state. We return the error code for the first error we
1017 if (indio_dev
->setup_ops
->predisable
) {
1018 ret2
= indio_dev
->setup_ops
->predisable(indio_dev
);
1023 if (indio_dev
->currentmode
== INDIO_BUFFER_TRIGGERED
) {
1024 iio_trigger_detach_poll_func(indio_dev
->trig
,
1025 indio_dev
->pollfunc
);
1028 list_for_each_entry(buffer
, &iio_dev_opaque
->buffer_list
, buffer_list
) {
1029 ret2
= iio_buffer_disable(buffer
, indio_dev
);
1034 if (indio_dev
->setup_ops
->postdisable
) {
1035 ret2
= indio_dev
->setup_ops
->postdisable(indio_dev
);
1040 iio_free_scan_mask(indio_dev
, indio_dev
->active_scan_mask
);
1041 indio_dev
->active_scan_mask
= NULL
;
1042 indio_dev
->currentmode
= INDIO_DIRECT_MODE
;
1047 static int __iio_update_buffers(struct iio_dev
*indio_dev
,
1048 struct iio_buffer
*insert_buffer
,
1049 struct iio_buffer
*remove_buffer
)
1051 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
1052 struct iio_device_config new_config
;
1055 ret
= iio_verify_update(indio_dev
, insert_buffer
, remove_buffer
,
1060 if (insert_buffer
) {
1061 ret
= iio_buffer_request_update(indio_dev
, insert_buffer
);
1063 goto err_free_config
;
1066 ret
= iio_disable_buffers(indio_dev
);
1068 goto err_deactivate_all
;
1071 iio_buffer_deactivate(remove_buffer
);
1073 iio_buffer_activate(indio_dev
, insert_buffer
);
1075 /* If no buffers in list, we are done */
1076 if (list_empty(&iio_dev_opaque
->buffer_list
))
1079 ret
= iio_enable_buffers(indio_dev
, &new_config
);
1081 goto err_deactivate_all
;
1087 * We've already verified that the config is valid earlier. If things go
1088 * wrong in either enable or disable the most likely reason is an IO
1089 * error from the device. In this case there is no good recovery
1090 * strategy. Just make sure to disable everything and leave the device
1091 * in a sane state. With a bit of luck the device might come back to
1092 * life again later and userspace can try again.
1094 iio_buffer_deactivate_all(indio_dev
);
1097 iio_free_scan_mask(indio_dev
, new_config
.scan_mask
);
1101 int iio_update_buffers(struct iio_dev
*indio_dev
,
1102 struct iio_buffer
*insert_buffer
,
1103 struct iio_buffer
*remove_buffer
)
1107 if (insert_buffer
== remove_buffer
)
1110 mutex_lock(&indio_dev
->info_exist_lock
);
1111 mutex_lock(&indio_dev
->mlock
);
1113 if (insert_buffer
&& iio_buffer_is_active(insert_buffer
))
1114 insert_buffer
= NULL
;
1116 if (remove_buffer
&& !iio_buffer_is_active(remove_buffer
))
1117 remove_buffer
= NULL
;
1119 if (!insert_buffer
&& !remove_buffer
) {
1124 if (indio_dev
->info
== NULL
) {
1129 ret
= __iio_update_buffers(indio_dev
, insert_buffer
, remove_buffer
);
1132 mutex_unlock(&indio_dev
->mlock
);
1133 mutex_unlock(&indio_dev
->info_exist_lock
);
1137 EXPORT_SYMBOL_GPL(iio_update_buffers
);
1139 void iio_disable_all_buffers(struct iio_dev
*indio_dev
)
1141 iio_disable_buffers(indio_dev
);
1142 iio_buffer_deactivate_all(indio_dev
);
1145 static ssize_t
iio_buffer_store_enable(struct device
*dev
,
1146 struct device_attribute
*attr
,
1151 bool requested_state
;
1152 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1153 struct iio_buffer
*buffer
= indio_dev
->buffer
;
1156 ret
= strtobool(buf
, &requested_state
);
1160 mutex_lock(&indio_dev
->mlock
);
1162 /* Find out if it is in the list */
1163 inlist
= iio_buffer_is_active(buffer
);
1164 /* Already in desired state */
1165 if (inlist
== requested_state
)
1168 if (requested_state
)
1169 ret
= __iio_update_buffers(indio_dev
, buffer
, NULL
);
1171 ret
= __iio_update_buffers(indio_dev
, NULL
, buffer
);
1174 mutex_unlock(&indio_dev
->mlock
);
1175 return (ret
< 0) ? ret
: len
;
1178 static const char * const iio_scan_elements_group_name
= "scan_elements";
1180 static ssize_t
iio_buffer_show_watermark(struct device
*dev
,
1181 struct device_attribute
*attr
,
1184 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1185 struct iio_buffer
*buffer
= indio_dev
->buffer
;
1187 return sprintf(buf
, "%u\n", buffer
->watermark
);
1190 static ssize_t
iio_buffer_store_watermark(struct device
*dev
,
1191 struct device_attribute
*attr
,
1195 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1196 struct iio_buffer
*buffer
= indio_dev
->buffer
;
1200 ret
= kstrtouint(buf
, 10, &val
);
1206 mutex_lock(&indio_dev
->mlock
);
1208 if (val
> buffer
->length
) {
1213 if (iio_buffer_is_active(buffer
)) {
1218 buffer
->watermark
= val
;
1220 mutex_unlock(&indio_dev
->mlock
);
1222 return ret
? ret
: len
;
1225 static ssize_t
iio_dma_show_data_available(struct device
*dev
,
1226 struct device_attribute
*attr
,
1229 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1230 struct iio_buffer
*buffer
= indio_dev
->buffer
;
1232 return sprintf(buf
, "%zu\n", iio_buffer_data_available(buffer
));
1235 static DEVICE_ATTR(length
, S_IRUGO
| S_IWUSR
, iio_buffer_read_length
,
1236 iio_buffer_write_length
);
1237 static struct device_attribute dev_attr_length_ro
= __ATTR(length
,
1238 S_IRUGO
, iio_buffer_read_length
, NULL
);
1239 static DEVICE_ATTR(enable
, S_IRUGO
| S_IWUSR
,
1240 iio_buffer_show_enable
, iio_buffer_store_enable
);
1241 static DEVICE_ATTR(watermark
, S_IRUGO
| S_IWUSR
,
1242 iio_buffer_show_watermark
, iio_buffer_store_watermark
);
1243 static struct device_attribute dev_attr_watermark_ro
= __ATTR(watermark
,
1244 S_IRUGO
, iio_buffer_show_watermark
, NULL
);
1245 static DEVICE_ATTR(data_available
, S_IRUGO
,
1246 iio_dma_show_data_available
, NULL
);
1248 static struct attribute
*iio_buffer_attrs
[] = {
1249 &dev_attr_length
.attr
,
1250 &dev_attr_enable
.attr
,
1251 &dev_attr_watermark
.attr
,
1252 &dev_attr_data_available
.attr
,
1255 static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer
*buffer
,
1256 struct iio_dev
*indio_dev
)
1258 struct iio_dev_attr
*p
;
1259 struct attribute
**attr
;
1260 int ret
, i
, attrn
, attrcount
;
1261 const struct iio_chan_spec
*channels
;
1264 if (buffer
->attrs
) {
1265 while (buffer
->attrs
[attrcount
] != NULL
)
1269 attr
= kcalloc(attrcount
+ ARRAY_SIZE(iio_buffer_attrs
) + 1,
1270 sizeof(struct attribute
*), GFP_KERNEL
);
1274 memcpy(attr
, iio_buffer_attrs
, sizeof(iio_buffer_attrs
));
1275 if (!buffer
->access
->set_length
)
1276 attr
[0] = &dev_attr_length_ro
.attr
;
1278 if (buffer
->access
->flags
& INDIO_BUFFER_FLAG_FIXED_WATERMARK
)
1279 attr
[2] = &dev_attr_watermark_ro
.attr
;
1282 memcpy(&attr
[ARRAY_SIZE(iio_buffer_attrs
)], buffer
->attrs
,
1283 sizeof(struct attribute
*) * attrcount
);
1285 attr
[attrcount
+ ARRAY_SIZE(iio_buffer_attrs
)] = NULL
;
1287 buffer
->buffer_group
.name
= "buffer";
1288 buffer
->buffer_group
.attrs
= attr
;
1290 indio_dev
->groups
[indio_dev
->groupcounter
++] = &buffer
->buffer_group
;
1293 INIT_LIST_HEAD(&buffer
->scan_el_dev_attr_list
);
1294 channels
= indio_dev
->channels
;
1297 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
1298 if (channels
[i
].scan_index
< 0)
1301 ret
= iio_buffer_add_channel_sysfs(indio_dev
, buffer
,
1304 goto error_cleanup_dynamic
;
1306 if (channels
[i
].type
== IIO_TIMESTAMP
)
1307 indio_dev
->scan_index_timestamp
=
1308 channels
[i
].scan_index
;
1310 if (indio_dev
->masklength
&& buffer
->scan_mask
== NULL
) {
1311 buffer
->scan_mask
= bitmap_zalloc(indio_dev
->masklength
,
1313 if (buffer
->scan_mask
== NULL
) {
1315 goto error_cleanup_dynamic
;
1320 buffer
->scan_el_group
.name
= iio_scan_elements_group_name
;
1322 buffer
->scan_el_group
.attrs
= kcalloc(attrcount
+ 1,
1323 sizeof(buffer
->scan_el_group
.attrs
[0]),
1325 if (buffer
->scan_el_group
.attrs
== NULL
) {
1327 goto error_free_scan_mask
;
1331 list_for_each_entry(p
, &buffer
->scan_el_dev_attr_list
, l
)
1332 buffer
->scan_el_group
.attrs
[attrn
++] = &p
->dev_attr
.attr
;
1333 indio_dev
->groups
[indio_dev
->groupcounter
++] = &buffer
->scan_el_group
;
1337 error_free_scan_mask
:
1338 bitmap_free(buffer
->scan_mask
);
1339 error_cleanup_dynamic
:
1340 iio_free_chan_devattr_list(&buffer
->scan_el_dev_attr_list
);
1341 kfree(buffer
->buffer_group
.attrs
);
1346 int iio_buffer_alloc_sysfs_and_mask(struct iio_dev
*indio_dev
)
1348 struct iio_buffer
*buffer
= indio_dev
->buffer
;
1349 const struct iio_chan_spec
*channels
;
1352 channels
= indio_dev
->channels
;
1354 int ml
= indio_dev
->masklength
;
1356 for (i
= 0; i
< indio_dev
->num_channels
; i
++)
1357 ml
= max(ml
, channels
[i
].scan_index
+ 1);
1358 indio_dev
->masklength
= ml
;
1364 return __iio_buffer_alloc_sysfs_and_mask(buffer
, indio_dev
);
1367 static void __iio_buffer_free_sysfs_and_mask(struct iio_buffer
*buffer
)
1369 bitmap_free(buffer
->scan_mask
);
1370 kfree(buffer
->buffer_group
.attrs
);
1371 kfree(buffer
->scan_el_group
.attrs
);
1372 iio_free_chan_devattr_list(&buffer
->scan_el_dev_attr_list
);
1375 void iio_buffer_free_sysfs_and_mask(struct iio_dev
*indio_dev
)
1377 struct iio_buffer
*buffer
= indio_dev
->buffer
;
1382 __iio_buffer_free_sysfs_and_mask(buffer
);
1386 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
1387 * @indio_dev: the iio device
1388 * @mask: scan mask to be checked
1390 * Return true if exactly one bit is set in the scan mask, false otherwise. It
1391 * can be used for devices where only one channel can be active for sampling at
1394 bool iio_validate_scan_mask_onehot(struct iio_dev
*indio_dev
,
1395 const unsigned long *mask
)
1397 return bitmap_weight(mask
, indio_dev
->masklength
) == 1;
1399 EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot
);
1401 static const void *iio_demux(struct iio_buffer
*buffer
,
1404 struct iio_demux_table
*t
;
1406 if (list_empty(&buffer
->demux_list
))
1408 list_for_each_entry(t
, &buffer
->demux_list
, l
)
1409 memcpy(buffer
->demux_bounce
+ t
->to
,
1410 datain
+ t
->from
, t
->length
);
1412 return buffer
->demux_bounce
;
1415 static int iio_push_to_buffer(struct iio_buffer
*buffer
, const void *data
)
1417 const void *dataout
= iio_demux(buffer
, data
);
1420 ret
= buffer
->access
->store_to(buffer
, dataout
);
1425 * We can't just test for watermark to decide if we wake the poll queue
1426 * because read may request less samples than the watermark.
1428 wake_up_interruptible_poll(&buffer
->pollq
, EPOLLIN
| EPOLLRDNORM
);
1433 * iio_push_to_buffers() - push to a registered buffer.
1434 * @indio_dev: iio_dev structure for device.
1437 int iio_push_to_buffers(struct iio_dev
*indio_dev
, const void *data
)
1439 struct iio_dev_opaque
*iio_dev_opaque
= to_iio_dev_opaque(indio_dev
);
1441 struct iio_buffer
*buf
;
1443 list_for_each_entry(buf
, &iio_dev_opaque
->buffer_list
, buffer_list
) {
1444 ret
= iio_push_to_buffer(buf
, data
);
1451 EXPORT_SYMBOL_GPL(iio_push_to_buffers
);
1454 * iio_buffer_release() - Free a buffer's resources
1455 * @ref: Pointer to the kref embedded in the iio_buffer struct
1457 * This function is called when the last reference to the buffer has been
1458 * dropped. It will typically free all resources allocated by the buffer. Do not
1459 * call this function manually, always use iio_buffer_put() when done using a
1462 static void iio_buffer_release(struct kref
*ref
)
1464 struct iio_buffer
*buffer
= container_of(ref
, struct iio_buffer
, ref
);
1466 buffer
->access
->release(buffer
);
1470 * iio_buffer_get() - Grab a reference to the buffer
1471 * @buffer: The buffer to grab a reference for, may be NULL
1473 * Returns the pointer to the buffer that was passed into the function.
1475 struct iio_buffer
*iio_buffer_get(struct iio_buffer
*buffer
)
1478 kref_get(&buffer
->ref
);
1482 EXPORT_SYMBOL_GPL(iio_buffer_get
);
1485 * iio_buffer_put() - Release the reference to the buffer
1486 * @buffer: The buffer to release the reference for, may be NULL
1488 void iio_buffer_put(struct iio_buffer
*buffer
)
1491 kref_put(&buffer
->ref
, iio_buffer_release
);
1493 EXPORT_SYMBOL_GPL(iio_buffer_put
);
1496 * iio_device_attach_buffer - Attach a buffer to a IIO device
1497 * @indio_dev: The device the buffer should be attached to
1498 * @buffer: The buffer to attach to the device
1500 * This function attaches a buffer to a IIO device. The buffer stays attached to
1501 * the device until the device is freed. The function should only be called at
1502 * most once per device.
1504 void iio_device_attach_buffer(struct iio_dev
*indio_dev
,
1505 struct iio_buffer
*buffer
)
1507 indio_dev
->buffer
= iio_buffer_get(buffer
);
1509 EXPORT_SYMBOL_GPL(iio_device_attach_buffer
);