i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / staging / iio / kfifo_buf.c
blobe1e9c06cde4a0de776288f8f9cb345df3c3dc419
1 #include <linux/slab.h>
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/device.h>
5 #include <linux/workqueue.h>
6 #include <linux/kfifo.h>
7 #include <linux/mutex.h>
9 #include "kfifo_buf.h"
11 struct iio_kfifo {
12 struct iio_buffer buffer;
13 struct kfifo kf;
14 int update_needed;
17 #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
19 static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
20 int bytes_per_datum, int length)
22 if ((length == 0) || (bytes_per_datum == 0))
23 return -EINVAL;
25 __iio_update_buffer(&buf->buffer, bytes_per_datum, length);
26 return kfifo_alloc(&buf->kf, bytes_per_datum*length, GFP_KERNEL);
29 static int iio_request_update_kfifo(struct iio_buffer *r)
31 int ret = 0;
32 struct iio_kfifo *buf = iio_to_kfifo(r);
34 if (!buf->update_needed)
35 goto error_ret;
36 kfifo_free(&buf->kf);
37 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
38 buf->buffer.length);
39 error_ret:
40 return ret;
43 static int iio_get_length_kfifo(struct iio_buffer *r)
45 return r->length;
48 static IIO_BUFFER_ENABLE_ATTR;
49 static IIO_BUFFER_LENGTH_ATTR;
51 static struct attribute *iio_kfifo_attributes[] = {
52 &dev_attr_length.attr,
53 &dev_attr_enable.attr,
54 NULL,
57 static struct attribute_group iio_kfifo_attribute_group = {
58 .attrs = iio_kfifo_attributes,
59 .name = "buffer",
62 struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
64 struct iio_kfifo *kf;
66 kf = kzalloc(sizeof *kf, GFP_KERNEL);
67 if (!kf)
68 return NULL;
69 kf->update_needed = true;
70 iio_buffer_init(&kf->buffer);
71 kf->buffer.attrs = &iio_kfifo_attribute_group;
73 return &kf->buffer;
75 EXPORT_SYMBOL(iio_kfifo_allocate);
77 static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
79 return r->bytes_per_datum;
82 static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
84 struct iio_kfifo *kf = iio_to_kfifo(r);
85 kf->update_needed = true;
86 return 0;
89 static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
91 if (r->bytes_per_datum != bpd) {
92 r->bytes_per_datum = bpd;
93 iio_mark_update_needed_kfifo(r);
95 return 0;
98 static int iio_set_length_kfifo(struct iio_buffer *r, int length)
100 if (r->length != length) {
101 r->length = length;
102 iio_mark_update_needed_kfifo(r);
104 return 0;
107 void iio_kfifo_free(struct iio_buffer *r)
109 kfree(iio_to_kfifo(r));
111 EXPORT_SYMBOL(iio_kfifo_free);
113 static int iio_store_to_kfifo(struct iio_buffer *r,
114 u8 *data,
115 s64 timestamp)
117 int ret;
118 struct iio_kfifo *kf = iio_to_kfifo(r);
119 ret = kfifo_in(&kf->kf, data, r->bytes_per_datum);
120 if (ret != r->bytes_per_datum)
121 return -EBUSY;
122 return 0;
125 static int iio_read_first_n_kfifo(struct iio_buffer *r,
126 size_t n, char __user *buf)
128 int ret, copied;
129 struct iio_kfifo *kf = iio_to_kfifo(r);
131 if (n < r->bytes_per_datum)
132 return -EINVAL;
134 n = rounddown(n, r->bytes_per_datum);
135 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
137 return copied;
140 const struct iio_buffer_access_funcs kfifo_access_funcs = {
141 .store_to = &iio_store_to_kfifo,
142 .read_first_n = &iio_read_first_n_kfifo,
143 .request_update = &iio_request_update_kfifo,
144 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
145 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
146 .get_length = &iio_get_length_kfifo,
147 .set_length = &iio_set_length_kfifo,
149 EXPORT_SYMBOL(kfifo_access_funcs);
151 MODULE_LICENSE("GPL");