Merge branch 'drm-nouveau-fixes-3.9' of git://anongit.freedesktop.org/git/nouveau...
[linux-2.6/libata-dev.git] / drivers / iio / kfifo_buf.c
bloba923c78d5cb48f40b2b661933b329f4ddb420c64
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>
8 #include <linux/iio/kfifo_buf.h>
9 #include <linux/sched.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 return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
26 bytes_per_datum, 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 r->stufftoread = false;
40 error_ret:
41 return ret;
44 static int iio_get_length_kfifo(struct iio_buffer *r)
46 return r->length;
49 static IIO_BUFFER_ENABLE_ATTR;
50 static IIO_BUFFER_LENGTH_ATTR;
52 static struct attribute *iio_kfifo_attributes[] = {
53 &dev_attr_length.attr,
54 &dev_attr_enable.attr,
55 NULL,
58 static struct attribute_group iio_kfifo_attribute_group = {
59 .attrs = iio_kfifo_attributes,
60 .name = "buffer",
63 static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
65 return r->bytes_per_datum;
68 static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
70 struct iio_kfifo *kf = iio_to_kfifo(r);
71 kf->update_needed = true;
72 return 0;
75 static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
77 if (r->bytes_per_datum != bpd) {
78 r->bytes_per_datum = bpd;
79 iio_mark_update_needed_kfifo(r);
81 return 0;
84 static int iio_set_length_kfifo(struct iio_buffer *r, int length)
86 /* Avoid an invalid state */
87 if (length < 2)
88 length = 2;
89 if (r->length != length) {
90 r->length = length;
91 iio_mark_update_needed_kfifo(r);
93 return 0;
96 static int iio_store_to_kfifo(struct iio_buffer *r,
97 u8 *data)
99 int ret;
100 struct iio_kfifo *kf = iio_to_kfifo(r);
101 ret = kfifo_in(&kf->kf, data, 1);
102 if (ret != 1)
103 return -EBUSY;
104 r->stufftoread = true;
105 wake_up_interruptible(&r->pollq);
107 return 0;
110 static int iio_read_first_n_kfifo(struct iio_buffer *r,
111 size_t n, char __user *buf)
113 int ret, copied;
114 struct iio_kfifo *kf = iio_to_kfifo(r);
116 if (n < r->bytes_per_datum || r->bytes_per_datum == 0)
117 return -EINVAL;
119 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
120 if (ret < 0)
121 return ret;
123 if (kfifo_is_empty(&kf->kf))
124 r->stufftoread = false;
125 /* verify it is still empty to avoid race */
126 if (!kfifo_is_empty(&kf->kf))
127 r->stufftoread = true;
129 return copied;
132 static const struct iio_buffer_access_funcs kfifo_access_funcs = {
133 .store_to = &iio_store_to_kfifo,
134 .read_first_n = &iio_read_first_n_kfifo,
135 .request_update = &iio_request_update_kfifo,
136 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
137 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
138 .get_length = &iio_get_length_kfifo,
139 .set_length = &iio_set_length_kfifo,
142 struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
144 struct iio_kfifo *kf;
146 kf = kzalloc(sizeof *kf, GFP_KERNEL);
147 if (!kf)
148 return NULL;
149 kf->update_needed = true;
150 iio_buffer_init(&kf->buffer);
151 kf->buffer.attrs = &iio_kfifo_attribute_group;
152 kf->buffer.access = &kfifo_access_funcs;
153 kf->buffer.length = 2;
154 return &kf->buffer;
156 EXPORT_SYMBOL(iio_kfifo_allocate);
158 void iio_kfifo_free(struct iio_buffer *r)
160 kfree(iio_to_kfifo(r));
162 EXPORT_SYMBOL(iio_kfifo_free);
164 MODULE_LICENSE("GPL");