Merge tag 'boards-for-3.17' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[deliverable/linux.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 }
0894d80d 45 mutex_unlock(&buf->user_lock);
5b78e513 46
b174baf4
JC
47 return ret;
48}
b174baf4 49
14555b14 50static int iio_get_length_kfifo(struct iio_buffer *r)
b174baf4
JC
51{
52 return r->length;
53}
b174baf4 54
14555b14 55static IIO_BUFFER_ENABLE_ATTR;
14555b14 56static IIO_BUFFER_LENGTH_ATTR;
b174baf4
JC
57
58static struct attribute *iio_kfifo_attributes[] = {
59 &dev_attr_length.attr,
b174baf4
JC
60 &dev_attr_enable.attr,
61 NULL,
62};
63
64static struct attribute_group iio_kfifo_attribute_group = {
65 .attrs = iio_kfifo_attributes,
1aa04278 66 .name = "buffer",
b174baf4
JC
67};
68
14555b14 69static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
b174baf4
JC
70{
71 return r->bytes_per_datum;
72}
b174baf4 73
869871b5 74static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
b174baf4 75{
869871b5
LPC
76 struct iio_kfifo *kf = iio_to_kfifo(r);
77 kf->update_needed = true;
b174baf4
JC
78 return 0;
79}
b174baf4 80
869871b5 81static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
b174baf4 82{
869871b5
LPC
83 if (r->bytes_per_datum != bpd) {
84 r->bytes_per_datum = bpd;
85 iio_mark_update_needed_kfifo(r);
86 }
b174baf4
JC
87 return 0;
88}
b174baf4 89
14555b14 90static int iio_set_length_kfifo(struct iio_buffer *r, int length)
b174baf4 91{
7c388ec1
JC
92 /* Avoid an invalid state */
93 if (length < 2)
94 length = 2;
b174baf4
JC
95 if (r->length != length) {
96 r->length = length;
869871b5 97 iio_mark_update_needed_kfifo(r);
b174baf4
JC
98 }
99 return 0;
100}
b174baf4 101
14555b14 102static int iio_store_to_kfifo(struct iio_buffer *r,
5d65d920 103 const void *data)
b174baf4
JC
104{
105 int ret;
106 struct iio_kfifo *kf = iio_to_kfifo(r);
c559afbf
JC
107 ret = kfifo_in(&kf->kf, data, 1);
108 if (ret != 1)
b174baf4 109 return -EBUSY;
355c1a14 110
623c334c 111 wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM);
c559afbf 112
b174baf4
JC
113 return 0;
114}
b174baf4 115
14555b14 116static int iio_read_first_n_kfifo(struct iio_buffer *r,
b26a2188 117 size_t n, char __user *buf)
b174baf4
JC
118{
119 int ret, copied;
120 struct iio_kfifo *kf = iio_to_kfifo(r);
121
0894d80d
LPC
122 if (mutex_lock_interruptible(&kf->user_lock))
123 return -ERESTARTSYS;
8fe64955 124
0894d80d
LPC
125 if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
126 ret = -EINVAL;
127 else
128 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
0894d80d
LPC
129 mutex_unlock(&kf->user_lock);
130 if (ret < 0)
131 return ret;
132
b174baf4
JC
133 return copied;
134}
5565a450 135
355c1a14
LPC
136static bool iio_kfifo_buf_data_available(struct iio_buffer *r)
137{
138 struct iio_kfifo *kf = iio_to_kfifo(r);
139 bool empty;
140
141 mutex_lock(&kf->user_lock);
142 empty = kfifo_is_empty(&kf->kf);
143 mutex_unlock(&kf->user_lock);
144
145 return !empty;
146}
147
9e69c935
LPC
148static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
149{
f6c23f48
LPC
150 struct iio_kfifo *kf = iio_to_kfifo(buffer);
151
0894d80d 152 mutex_destroy(&kf->user_lock);
f6c23f48
LPC
153 kfifo_free(&kf->kf);
154 kfree(kf);
9e69c935
LPC
155}
156
7e632344 157static const struct iio_buffer_access_funcs kfifo_access_funcs = {
5565a450
JC
158 .store_to = &iio_store_to_kfifo,
159 .read_first_n = &iio_read_first_n_kfifo,
355c1a14 160 .data_available = iio_kfifo_buf_data_available,
5565a450
JC
161 .request_update = &iio_request_update_kfifo,
162 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
163 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
164 .get_length = &iio_get_length_kfifo,
165 .set_length = &iio_set_length_kfifo,
9e69c935 166 .release = &iio_kfifo_buffer_release,
5565a450 167};
7e632344
LPC
168
169struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
170{
171 struct iio_kfifo *kf;
172
173 kf = kzalloc(sizeof *kf, GFP_KERNEL);
174 if (!kf)
175 return NULL;
176 kf->update_needed = true;
177 iio_buffer_init(&kf->buffer);
178 kf->buffer.attrs = &iio_kfifo_attribute_group;
179 kf->buffer.access = &kfifo_access_funcs;
7c388ec1 180 kf->buffer.length = 2;
0894d80d 181 mutex_init(&kf->user_lock);
7e632344
LPC
182 return &kf->buffer;
183}
184EXPORT_SYMBOL(iio_kfifo_allocate);
185
186void iio_kfifo_free(struct iio_buffer *r)
187{
9e69c935 188 iio_buffer_put(r);
7e632344
LPC
189}
190EXPORT_SYMBOL(iio_kfifo_free);
5565a450 191
b174baf4 192MODULE_LICENSE("GPL");
This page took 0.291673 seconds and 5 git commands to generate.