linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iio: make blocking read wait for data
@ 2014-06-06 14:30 Josselin Costanzi
  2014-06-06 14:38 ` Lars-Peter Clausen
  0 siblings, 1 reply; 10+ messages in thread
From: Josselin Costanzi @ 2014-06-06 14:30 UTC (permalink / raw)
  To: linux-iio; +Cc: Josselin Costanzi

Currently the IIO buffer blocking read only wait until at least one
data element is available.
This patch makes the reader sleep until enough data is collected
before returning to userspace.
This commit also fix a bug where data is lost if an error happens
after some data is already read.

Signed-off-by: Josselin Costanzi <josselin.costanzi@mobile-devices.fr>
---
 drivers/iio/industrialio-buffer.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index e472cff..ce8a1df 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -56,6 +56,7 @@ ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
 {
 	struct iio_dev *indio_dev = filp->private_data;
 	struct iio_buffer *rb = indio_dev->buffer;
+	size_t count = 0;
 	int ret;
 
 	if (!indio_dev->info)
@@ -66,24 +67,31 @@ ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
 
 	do {
 		if (!iio_buffer_data_available(rb)) {
-			if (filp->f_flags & O_NONBLOCK)
-				return -EAGAIN;
+			if (filp->f_flags & O_NONBLOCK) {
+				ret = -EAGAIN;
+				break;
+			}
 
 			ret = wait_event_interruptible(rb->pollq,
 					iio_buffer_data_available(rb) ||
 					indio_dev->info == NULL);
 			if (ret)
-				return ret;
-			if (indio_dev->info == NULL)
-				return -ENODEV;
+				break;
+			if (indio_dev->info == NULL) {
+				ret = -ENODEV;
+				break;
+			}
 		}
 
-		ret = rb->access->read_first_n(rb, n, buf);
-		if (ret == 0 && (filp->f_flags & O_NONBLOCK))
-			ret = -EAGAIN;
-	 } while (ret == 0);
+		ret = rb->access->read_first_n(rb, n, buf + count);
+		if (ret < 0)
+			break;
 
-	return ret;
+		n -= ret;
+		count += ret;
+	 } while (n > 0);
+
+	return count ? count : ret;
 }
 
 /**
-- 
1.9.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2014-06-10 15:23 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-06 14:30 [PATCH] iio: make blocking read wait for data Josselin Costanzi
2014-06-06 14:38 ` Lars-Peter Clausen
2014-06-06 15:12   ` Josselin Costanzi
2014-06-06 15:40     ` Lars-Peter Clausen
2014-06-06 17:15       ` Josselin Costanzi
2014-06-07 10:18         ` Jonathan Cameron
2014-06-09 19:57           ` Lars-Peter Clausen
2014-06-09 20:12             ` Jonathan Cameron
2014-06-10 10:55               ` Josselin Costanzi
2014-06-10 15:23                 ` Srinivas Pandruvada

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).