Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / iio / industrialio-buffer.c
blob2f7426a2f47ca571a73851a51ba0c22bb6334996
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>
15 #include <linux/fs.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>
23 #include "iio_core.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[] = {
30 [IIO_BE] = "be",
31 [IIO_LE] = "le",
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)
48 return -ENODEV;
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)
56 size_t avail;
57 int flushed = 0;
59 /* wakeup if the device was unregistered */
60 if (!indio_dev->info)
61 return true;
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);
66 to_flush = 0;
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,
75 to_flush - avail);
76 return true;
79 if (to_flush)
80 flushed = iio_buffer_flush_hwfifo(indio_dev, buf,
81 to_wait - avail);
82 if (flushed <= 0)
83 return false;
85 if (avail + flushed >= to_wait)
86 return true;
88 return false;
91 /**
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);
110 size_t datum_size;
111 size_t to_wait;
112 int ret = 0;
114 if (!indio_dev->info)
115 return -ENODEV;
117 if (!rb || !rb->access->read)
118 return -EINVAL;
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.
126 if (!datum_size)
127 return 0;
129 if (filp->f_flags & O_NONBLOCK)
130 to_wait = 0;
131 else
132 to_wait = min_t(size_t, n / datum_size, rb->watermark);
134 add_wait_queue(&rb->pollq, &wait);
135 do {
136 if (!indio_dev->info) {
137 ret = -ENODEV;
138 break;
141 if (!iio_buffer_ready(indio_dev, rb, to_wait, n / datum_size)) {
142 if (signal_pending(current)) {
143 ret = -ERESTARTSYS;
144 break;
147 wait_woken(&wait, TASK_INTERRUPTIBLE,
148 MAX_SCHEDULE_TIMEOUT);
149 continue;
152 ret = rb->access->read(rb, n, buf);
153 if (ret == 0 && (filp->f_flags & O_NONBLOCK))
154 ret = -EAGAIN;
155 } while (ret == 0);
156 remove_wait_queue(&rb->pollq, &wait);
158 return ret;
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
165 * a wait queue
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)
177 return 0;
179 poll_wait(filp, &rb->pollq, wait);
180 if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0))
181 return EPOLLIN | EPOLLRDNORM;
182 return 0;
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;
196 if (!buffer)
197 return;
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,
215 char *buf)
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,
222 char *buf)
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
229 type = IIO_LE;
230 #else
231 type = IIO_BE;
232 #endif
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);
242 else
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,
253 char *buf)
255 int ret;
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,
261 buffer->scan_mask);
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,
270 bool strict)
272 if (bitmap_empty(mask, masklength))
273 return NULL;
274 while (*av_masks) {
275 if (strict) {
276 if (bitmap_equal(mask, av_masks, masklength))
277 return av_masks;
278 } else {
279 if (bitmap_subset(mask, av_masks, masklength))
280 return av_masks;
282 av_masks += BITS_TO_LONGS(masklength);
284 return NULL;
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)
291 return true;
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)
314 return -ENOMEM;
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,
328 trialmask, false);
329 if (!mask)
330 goto err_invalid_mask;
332 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
334 bitmap_free(trialmask);
336 return 0;
338 err_invalid_mask:
339 bitmap_free(trialmask);
340 return -EINVAL;
343 static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
345 clear_bit(bit, buffer->scan_mask);
346 return 0;
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)
353 return -EINVAL;
355 if (!buffer->scan_mask)
356 return 0;
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,
364 const char *buf,
365 size_t len)
367 int ret;
368 bool state;
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);
374 if (ret < 0)
375 return ret;
376 mutex_lock(&indio_dev->mlock);
377 if (iio_buffer_is_active(buffer)) {
378 ret = -EBUSY;
379 goto error_ret;
381 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
382 if (ret < 0)
383 goto error_ret;
384 if (!state && ret) {
385 ret = iio_scan_mask_clear(buffer, this_attr->address);
386 if (ret)
387 goto error_ret;
388 } else if (state && !ret) {
389 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
390 if (ret)
391 goto error_ret;
394 error_ret:
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,
403 char *buf)
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,
413 const char *buf,
414 size_t len)
416 int ret;
417 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
418 struct iio_buffer *buffer = indio_dev->buffer;
419 bool state;
421 ret = strtobool(buf, &state);
422 if (ret < 0)
423 return ret;
425 mutex_lock(&indio_dev->mlock);
426 if (iio_buffer_is_active(buffer)) {
427 ret = -EBUSY;
428 goto error_ret;
430 buffer->scan_timestamp = state;
431 error_ret:
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",
444 chan,
445 &iio_show_scan_index,
446 NULL,
448 IIO_SEPARATE,
449 &indio_dev->dev,
450 &buffer->scan_el_dev_attr_list);
451 if (ret)
452 return ret;
453 attrcount++;
454 ret = __iio_add_chan_devattr("type",
455 chan,
456 &iio_show_fixed_type,
457 NULL,
460 &indio_dev->dev,
461 &buffer->scan_el_dev_attr_list);
462 if (ret)
463 return ret;
464 attrcount++;
465 if (chan->type != IIO_TIMESTAMP)
466 ret = __iio_add_chan_devattr("en",
467 chan,
468 &iio_scan_el_show,
469 &iio_scan_el_store,
470 chan->scan_index,
472 &indio_dev->dev,
473 &buffer->scan_el_dev_attr_list);
474 else
475 ret = __iio_add_chan_devattr("en",
476 chan,
477 &iio_scan_el_ts_show,
478 &iio_scan_el_ts_store,
479 chan->scan_index,
481 &indio_dev->dev,
482 &buffer->scan_el_dev_attr_list);
483 if (ret)
484 return ret;
485 attrcount++;
486 ret = attrcount;
487 return ret;
490 static ssize_t iio_buffer_read_length(struct device *dev,
491 struct device_attribute *attr,
492 char *buf)
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;
506 unsigned int val;
507 int ret;
509 ret = kstrtouint(buf, 10, &val);
510 if (ret)
511 return ret;
513 if (val == buffer->length)
514 return len;
516 mutex_lock(&indio_dev->mlock);
517 if (iio_buffer_is_active(buffer)) {
518 ret = -EBUSY;
519 } else {
520 buffer->access->set_length(buffer, val);
521 ret = 0;
523 if (ret)
524 goto out;
525 if (buffer->length && buffer->length < buffer->watermark)
526 buffer->watermark = buffer->length;
527 out:
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,
535 char *buf)
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;
547 unsigned int bytes;
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;
553 return bytes;
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)
565 unsigned bytes = 0;
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);
573 bytes += length;
574 largest = max(largest, length);
577 if (timestamp) {
578 length = iio_storage_bytes_for_timestamp(indio_dev);
579 bytes = ALIGN(bytes, length);
580 bytes += length;
581 largest = max(largest, length);
584 bytes = ALIGN(bytes, largest);
585 return bytes;
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)
618 return 0;
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)
626 return 0;
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)
633 unsigned int bytes;
635 if (!buffer->access->set_bytes_per_datum)
636 return;
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)
647 int ret;
649 iio_buffer_update_bytes_per_datum(indio_dev, buffer);
650 if (buffer->access->request_update) {
651 ret = buffer->access->request_update(buffer);
652 if (ret) {
653 dev_dbg(&indio_dev->dev,
654 "Buffer not started: buffer parameter update failed (%d)\n",
655 ret);
656 return ret;
660 return 0;
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)
668 bitmap_free(mask);
671 struct iio_device_config {
672 unsigned int mode;
673 unsigned int watermark;
674 const unsigned long *scan_mask;
675 unsigned int scan_bytes;
676 bool scan_timestamp;
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;
688 bool scan_timestamp;
689 unsigned int modes;
691 if (insert_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");
695 return -EINVAL;
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
703 * to verify.
705 if (remove_buffer && !insert_buffer &&
706 list_is_singular(&iio_dev_opaque->buffer_list))
707 return 0;
709 modes = indio_dev->modes;
711 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
712 if (buffer == remove_buffer)
713 continue;
714 modes &= buffer->access->modes;
715 config->watermark = min(config->watermark, buffer->watermark);
718 if (insert_buffer) {
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))
733 return -EINVAL;
734 config->mode = INDIO_BUFFER_HARDWARE;
735 strict_scanmask = true;
736 } else if (modes & INDIO_BUFFER_SOFTWARE) {
737 config->mode = INDIO_BUFFER_SOFTWARE;
738 } else {
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");
742 return -EINVAL;
745 /* What scan mask do we actually have? */
746 compound_mask = bitmap_zalloc(indio_dev->masklength, GFP_KERNEL);
747 if (compound_mask == NULL)
748 return -ENOMEM;
750 scan_timestamp = false;
752 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
753 if (buffer == remove_buffer)
754 continue;
755 bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
756 indio_dev->masklength);
757 scan_timestamp |= buffer->scan_timestamp;
760 if (insert_buffer) {
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,
769 compound_mask,
770 strict_scanmask);
771 bitmap_free(compound_mask);
772 if (scan_mask == NULL)
773 return -EINVAL;
774 } else {
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;
783 return 0;
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 {
794 unsigned from;
795 unsigned to;
796 unsigned length;
797 struct list_head l;
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) {
804 list_del(&p->l);
805 kfree(p);
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,
811 unsigned int length)
814 if (*p && (*p)->from + (*p)->length == in_loc &&
815 (*p)->to + (*p)->length == out_loc) {
816 (*p)->length += length;
817 } else {
818 *p = kmalloc(sizeof(**p), GFP_KERNEL);
819 if (*p == NULL)
820 return -ENOMEM;
821 (*p)->from = in_loc;
822 (*p)->to = out_loc;
823 (*p)->length = length;
824 list_add_tail(&(*p)->l, &buffer->demux_list);
827 return 0;
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,
844 buffer->scan_mask,
845 indio_dev->masklength))
846 return 0;
848 /* Now we have the two masks, work from least sig and build up sizes */
849 for_each_set_bit(out_ind,
850 buffer->scan_mask,
851 indio_dev->masklength) {
852 in_ind = find_next_bit(indio_dev->active_scan_mask,
853 indio_dev->masklength,
854 in_ind + 1);
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,
861 in_ind + 1);
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);
867 if (ret)
868 goto error_clear_mux_table;
869 out_loc += length;
870 in_loc += length;
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);
878 if (ret)
879 goto error_clear_mux_table;
880 out_loc += length;
881 in_loc += length;
883 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
884 if (buffer->demux_bounce == NULL) {
885 ret = -ENOMEM;
886 goto error_clear_mux_table;
888 return 0;
890 error_clear_mux_table:
891 iio_buffer_demux_free(buffer);
893 return ret;
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;
900 int ret;
902 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
903 ret = iio_buffer_update_demux(indio_dev, buffer);
904 if (ret < 0)
905 goto error_clear_mux_table;
907 return 0;
909 error_clear_mux_table:
910 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list)
911 iio_buffer_demux_free(buffer);
913 return ret;
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;
921 int ret;
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);
930 /* Wind up again */
931 if (indio_dev->setup_ops->preenable) {
932 ret = indio_dev->setup_ops->preenable(indio_dev);
933 if (ret) {
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);
944 if (ret < 0) {
945 dev_dbg(&indio_dev->dev,
946 "Buffer not started: update scan mode failed (%d)\n",
947 ret);
948 goto err_run_postdisable;
952 if (indio_dev->info->hwfifo_set_watermark)
953 indio_dev->info->hwfifo_set_watermark(indio_dev,
954 config->watermark);
956 list_for_each_entry(buffer, &iio_dev_opaque->buffer_list, buffer_list) {
957 ret = iio_buffer_enable(buffer, indio_dev);
958 if (ret)
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);
965 if (ret)
966 goto err_disable_buffers;
969 if (indio_dev->setup_ops->postenable) {
970 ret = indio_dev->setup_ops->postenable(indio_dev);
971 if (ret) {
972 dev_dbg(&indio_dev->dev,
973 "Buffer not started: postenable failed (%d)\n", ret);
974 goto err_detach_pollfunc;
978 return 0;
980 err_detach_pollfunc:
981 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
982 iio_trigger_detach_poll_func(indio_dev->trig,
983 indio_dev->pollfunc);
985 err_disable_buffers:
986 list_for_each_entry_continue_reverse(buffer, &iio_dev_opaque->buffer_list,
987 buffer_list)
988 iio_buffer_disable(buffer, indio_dev);
989 err_run_postdisable:
990 if (indio_dev->setup_ops->postdisable)
991 indio_dev->setup_ops->postdisable(indio_dev);
992 err_undo_config:
993 indio_dev->currentmode = INDIO_DIRECT_MODE;
994 indio_dev->active_scan_mask = NULL;
996 return ret;
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;
1003 int ret = 0;
1004 int ret2;
1006 /* Wind down existing buffers - iff there are any */
1007 if (list_empty(&iio_dev_opaque->buffer_list))
1008 return 0;
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
1014 * encountered.
1017 if (indio_dev->setup_ops->predisable) {
1018 ret2 = indio_dev->setup_ops->predisable(indio_dev);
1019 if (ret2 && !ret)
1020 ret = ret2;
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);
1030 if (ret2 && !ret)
1031 ret = ret2;
1034 if (indio_dev->setup_ops->postdisable) {
1035 ret2 = indio_dev->setup_ops->postdisable(indio_dev);
1036 if (ret2 && !ret)
1037 ret = ret2;
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;
1044 return ret;
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;
1053 int ret;
1055 ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer,
1056 &new_config);
1057 if (ret)
1058 return ret;
1060 if (insert_buffer) {
1061 ret = iio_buffer_request_update(indio_dev, insert_buffer);
1062 if (ret)
1063 goto err_free_config;
1066 ret = iio_disable_buffers(indio_dev);
1067 if (ret)
1068 goto err_deactivate_all;
1070 if (remove_buffer)
1071 iio_buffer_deactivate(remove_buffer);
1072 if (insert_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))
1077 return 0;
1079 ret = iio_enable_buffers(indio_dev, &new_config);
1080 if (ret)
1081 goto err_deactivate_all;
1083 return 0;
1085 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);
1096 err_free_config:
1097 iio_free_scan_mask(indio_dev, new_config.scan_mask);
1098 return ret;
1101 int iio_update_buffers(struct iio_dev *indio_dev,
1102 struct iio_buffer *insert_buffer,
1103 struct iio_buffer *remove_buffer)
1105 int ret;
1107 if (insert_buffer == remove_buffer)
1108 return 0;
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) {
1120 ret = 0;
1121 goto out_unlock;
1124 if (indio_dev->info == NULL) {
1125 ret = -ENODEV;
1126 goto out_unlock;
1129 ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
1131 out_unlock:
1132 mutex_unlock(&indio_dev->mlock);
1133 mutex_unlock(&indio_dev->info_exist_lock);
1135 return ret;
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,
1147 const char *buf,
1148 size_t len)
1150 int ret;
1151 bool requested_state;
1152 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1153 struct iio_buffer *buffer = indio_dev->buffer;
1154 bool inlist;
1156 ret = strtobool(buf, &requested_state);
1157 if (ret < 0)
1158 return ret;
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)
1166 goto done;
1168 if (requested_state)
1169 ret = __iio_update_buffers(indio_dev, buffer, NULL);
1170 else
1171 ret = __iio_update_buffers(indio_dev, NULL, buffer);
1173 done:
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,
1182 char *buf)
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,
1192 const char *buf,
1193 size_t len)
1195 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1196 struct iio_buffer *buffer = indio_dev->buffer;
1197 unsigned int val;
1198 int ret;
1200 ret = kstrtouint(buf, 10, &val);
1201 if (ret)
1202 return ret;
1203 if (!val)
1204 return -EINVAL;
1206 mutex_lock(&indio_dev->mlock);
1208 if (val > buffer->length) {
1209 ret = -EINVAL;
1210 goto out;
1213 if (iio_buffer_is_active(buffer)) {
1214 ret = -EBUSY;
1215 goto out;
1218 buffer->watermark = val;
1219 out:
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,
1227 char *buf)
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;
1263 attrcount = 0;
1264 if (buffer->attrs) {
1265 while (buffer->attrs[attrcount] != NULL)
1266 attrcount++;
1269 attr = kcalloc(attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1,
1270 sizeof(struct attribute *), GFP_KERNEL);
1271 if (!attr)
1272 return -ENOMEM;
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;
1281 if (buffer->attrs)
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;
1292 attrcount = 0;
1293 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
1294 channels = indio_dev->channels;
1295 if (channels) {
1296 /* new magic */
1297 for (i = 0; i < indio_dev->num_channels; i++) {
1298 if (channels[i].scan_index < 0)
1299 continue;
1301 ret = iio_buffer_add_channel_sysfs(indio_dev, buffer,
1302 &channels[i]);
1303 if (ret < 0)
1304 goto error_cleanup_dynamic;
1305 attrcount += ret;
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,
1312 GFP_KERNEL);
1313 if (buffer->scan_mask == NULL) {
1314 ret = -ENOMEM;
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]),
1324 GFP_KERNEL);
1325 if (buffer->scan_el_group.attrs == NULL) {
1326 ret = -ENOMEM;
1327 goto error_free_scan_mask;
1329 attrn = 0;
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;
1335 return 0;
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);
1343 return ret;
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;
1350 int i;
1352 channels = indio_dev->channels;
1353 if (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;
1361 if (!buffer)
1362 return 0;
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;
1379 if (!buffer)
1380 return;
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
1392 * a time.
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,
1402 const void *datain)
1404 struct iio_demux_table *t;
1406 if (list_empty(&buffer->demux_list))
1407 return datain;
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);
1418 int ret;
1420 ret = buffer->access->store_to(buffer, dataout);
1421 if (ret)
1422 return ret;
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);
1429 return 0;
1433 * iio_push_to_buffers() - push to a registered buffer.
1434 * @indio_dev: iio_dev structure for device.
1435 * @data: Full scan.
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);
1440 int ret;
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);
1445 if (ret < 0)
1446 return ret;
1449 return 0;
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
1460 * buffer.
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)
1477 if (buffer)
1478 kref_get(&buffer->ref);
1480 return buffer;
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)
1490 if (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);