iio: Add data_available callback for buffers
[linux-2.6-block.git] / drivers / iio / kfifo_buf.c
CommitLineData
b174baf4
JC
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>
06458e27 8#include <linux/iio/kfifo_buf.h>
7c388ec1 9#include <linux/sched.h>
623c334c 10#include <linux/poll.h>
b174baf4 11
a710cc77 12struct iio_kfifo {
14555b14 13 struct iio_buffer buffer;
a710cc77 14 struct kfifo kf;
0894d80d 15 struct mutex user_lock;
a710cc77 16 int update_needed;
a710cc77
JC
17};
18
14555b14 19#define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
5565a450 20
b174baf4
JC
21static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
22 int bytes_per_datum, int length)
23{
24 if ((length == 0) || (bytes_per_datum == 0))
25 return -EINVAL;
26
c559afbf
JC
27 return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
28 bytes_per_datum, GFP_KERNEL);
b174baf4
JC
29}
30
14555b14 31static int iio_request_update_kfifo(struct iio_buffer *r)
b174baf4
JC
32{
33 int ret = 0;
34 struct iio_kfifo *buf = iio_to_kfifo(r);
35
0894d80d 36 mutex_lock(&buf->user_lock);
5b78e513
LPC
37 if (buf->update_needed) {
38 kfifo_free(&buf->kf);
39 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
14555b14 40 buf->buffer.length);
cb6fbfa1 41 buf->update_needed = false;
5b78e513
LPC
42 } else {
43 kfifo_reset_out(&buf->kf);
44 }
7c388ec1 45 r->stufftoread = false;
0894d80d 46 mutex_unlock(&buf->user_lock);
5b78e513 47
b174baf4
JC
48 return ret;
49}
b174baf4 50
14555b14 51static int iio_get_length_kfifo(struct iio_buffer *r)
b174baf4
JC
52{
53 return r->length;
54}
b174baf4 55
14555b14 56static IIO_BUFFER_ENABLE_ATTR;
14555b14 57static IIO_BUFFER_LENGTH_ATTR;
b174baf4
JC
58
59static struct attribute *iio_kfifo_attributes[] = {
60 &dev_attr_length.attr,
b174baf4
JC
61 &dev_attr_enable.attr,
62 NULL,
63};
64
65static struct attribute_group iio_kfifo_attribute_group = {
66 .attrs = iio_kfifo_attributes,
1aa04278 67 .name = "buffer",
b174baf4
JC
68};
69
14555b14 70static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
b174baf4
JC
71{
72 return r->bytes_per_datum;
73}
b174baf4 74
869871b5 75static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
b174baf4 76{
869871b5
LPC
77 struct iio_kfifo *kf = iio_to_kfifo(r);
78 kf->update_needed = true;
b174baf4
JC
79 return 0;
80}
b174baf4 81
869871b5 82static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
b174baf4 83{
869871b5
LPC
84 if (r->bytes_per_datum != bpd) {
85 r->bytes_per_datum = bpd;
86 iio_mark_update_needed_kfifo(r);
87 }
b174baf4
JC
88 return 0;
89}
b174baf4 90
14555b14 91static int iio_set_length_kfifo(struct iio_buffer *r, int length)
b174baf4 92{
7c388ec1
JC
93 /* Avoid an invalid state */
94 if (length < 2)
95 length = 2;
b174baf4
JC
96 if (r->length != length) {
97 r->length = length;
869871b5 98 iio_mark_update_needed_kfifo(r);
b174baf4
JC
99 }
100 return 0;
101}
b174baf4 102
14555b14 103static int iio_store_to_kfifo(struct iio_buffer *r,
5d65d920 104 const void *data)
b174baf4
JC
105{
106 int ret;
107 struct iio_kfifo *kf = iio_to_kfifo(r);
c559afbf
JC
108 ret = kfifo_in(&kf->kf, data, 1);
109 if (ret != 1)
b174baf4 110 return -EBUSY;
7c388ec1 111 r->stufftoread = true;
623c334c 112 wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM);
c559afbf 113
b174baf4
JC
114 return 0;
115}
b174baf4 116
14555b14 117static int iio_read_first_n_kfifo(struct iio_buffer *r,
b26a2188 118 size_t n, char __user *buf)
b174baf4
JC
119{
120 int ret, copied;
121 struct iio_kfifo *kf = iio_to_kfifo(r);
122
0894d80d
LPC
123 if (mutex_lock_interruptible(&kf->user_lock))
124 return -ERESTARTSYS;
8fe64955 125
0894d80d
LPC
126 if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
127 ret = -EINVAL;
128 else
129 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
b174baf4 130
7c388ec1
JC
131 if (kfifo_is_empty(&kf->kf))
132 r->stufftoread = false;
133 /* verify it is still empty to avoid race */
134 if (!kfifo_is_empty(&kf->kf))
135 r->stufftoread = true;
136
0894d80d
LPC
137 mutex_unlock(&kf->user_lock);
138 if (ret < 0)
139 return ret;
140
b174baf4
JC
141 return copied;
142}
5565a450 143
9e69c935
LPC
144static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
145{
f6c23f48
LPC
146 struct iio_kfifo *kf = iio_to_kfifo(buffer);
147
0894d80d 148 mutex_destroy(&kf->user_lock);
f6c23f48
LPC
149 kfifo_free(&kf->kf);
150 kfree(kf);
9e69c935
LPC
151}
152
7e632344 153static const struct iio_buffer_access_funcs kfifo_access_funcs = {
5565a450
JC
154 .store_to = &iio_store_to_kfifo,
155 .read_first_n = &iio_read_first_n_kfifo,
5565a450
JC
156 .request_update = &iio_request_update_kfifo,
157 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
158 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
159 .get_length = &iio_get_length_kfifo,
160 .set_length = &iio_set_length_kfifo,
9e69c935 161 .release = &iio_kfifo_buffer_release,
5565a450 162};
7e632344
LPC
163
164struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
165{
166 struct iio_kfifo *kf;
167
168 kf = kzalloc(sizeof *kf, GFP_KERNEL);
169 if (!kf)
170 return NULL;
171 kf->update_needed = true;
172 iio_buffer_init(&kf->buffer);
173 kf->buffer.attrs = &iio_kfifo_attribute_group;
174 kf->buffer.access = &kfifo_access_funcs;
7c388ec1 175 kf->buffer.length = 2;
0894d80d 176 mutex_init(&kf->user_lock);
7e632344
LPC
177 return &kf->buffer;
178}
179EXPORT_SYMBOL(iio_kfifo_allocate);
180
181void iio_kfifo_free(struct iio_buffer *r)
182{
9e69c935 183 iio_buffer_put(r);
7e632344
LPC
184}
185EXPORT_SYMBOL(iio_kfifo_free);
5565a450 186
b174baf4 187MODULE_LICENSE("GPL");