]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - drivers/iio/kfifo_buf.c
Merge branch 'next' of git://git.infradead.org/users/vkoul/slave-dma
[karo-tx-linux.git] / drivers / iio / kfifo_buf.c
index 6bf9d05f48411105b68d15a600a617ddea39c47e..5bc5c860e9ca5fe1426cf1189b3a581aad78f3ba 100644 (file)
@@ -6,6 +6,7 @@
 #include <linux/kfifo.h>
 #include <linux/mutex.h>
 #include <linux/iio/kfifo_buf.h>
+#include <linux/sched.h>
 
 struct iio_kfifo {
        struct iio_buffer buffer;
@@ -22,7 +23,8 @@ static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
                return -EINVAL;
 
        __iio_update_buffer(&buf->buffer, bytes_per_datum, length);
-       return kfifo_alloc(&buf->kf, bytes_per_datum*length, GFP_KERNEL);
+       return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
+                            bytes_per_datum, GFP_KERNEL);
 }
 
 static int iio_request_update_kfifo(struct iio_buffer *r)
@@ -35,6 +37,7 @@ static int iio_request_update_kfifo(struct iio_buffer *r)
        kfifo_free(&buf->kf);
        ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
                                   buf->buffer.length);
+       r->stufftoread = false;
 error_ret:
        return ret;
 }
@@ -81,6 +84,9 @@ static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
 
 static int iio_set_length_kfifo(struct iio_buffer *r, int length)
 {
+       /* Avoid an invalid state */
+       if (length < 2)
+               length = 2;
        if (r->length != length) {
                r->length = length;
                iio_mark_update_needed_kfifo(r);
@@ -89,14 +95,16 @@ static int iio_set_length_kfifo(struct iio_buffer *r, int length)
 }
 
 static int iio_store_to_kfifo(struct iio_buffer *r,
-                             u8 *data,
-                             s64 timestamp)
+                             u8 *data)
 {
        int ret;
        struct iio_kfifo *kf = iio_to_kfifo(r);
-       ret = kfifo_in(&kf->kf, data, r->bytes_per_datum);
-       if (ret != r->bytes_per_datum)
+       ret = kfifo_in(&kf->kf, data, 1);
+       if (ret != 1)
                return -EBUSY;
+       r->stufftoread = true;
+       wake_up_interruptible(&r->pollq);
+
        return 0;
 }
 
@@ -106,11 +114,18 @@ static int iio_read_first_n_kfifo(struct iio_buffer *r,
        int ret, copied;
        struct iio_kfifo *kf = iio_to_kfifo(r);
 
-       if (n < r->bytes_per_datum)
+       if (n < r->bytes_per_datum || r->bytes_per_datum == 0)
                return -EINVAL;
 
-       n = rounddown(n, r->bytes_per_datum);
        ret = kfifo_to_user(&kf->kf, buf, n, &copied);
+       if (ret < 0)
+               return ret;
+
+       if (kfifo_is_empty(&kf->kf))
+               r->stufftoread = false;
+       /* verify it is still empty to avoid race */
+       if (!kfifo_is_empty(&kf->kf))
+               r->stufftoread = true;
 
        return copied;
 }
@@ -136,7 +151,7 @@ struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
        iio_buffer_init(&kf->buffer);
        kf->buffer.attrs = &iio_kfifo_attribute_group;
        kf->buffer.access = &kfifo_access_funcs;
-
+       kf->buffer.length = 2;
        return &kf->buffer;
 }
 EXPORT_SYMBOL(iio_kfifo_allocate);