fib6: install fib6 ops in the last step
[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
869871b5 50static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
b174baf4 51{
869871b5
LPC
52 struct iio_kfifo *kf = iio_to_kfifo(r);
53 kf->update_needed = true;
b174baf4
JC
54 return 0;
55}
b174baf4 56
869871b5 57static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
b174baf4 58{
869871b5
LPC
59 if (r->bytes_per_datum != bpd) {
60 r->bytes_per_datum = bpd;
61 iio_mark_update_needed_kfifo(r);
62 }
b174baf4
JC
63 return 0;
64}
b174baf4 65
14555b14 66static int iio_set_length_kfifo(struct iio_buffer *r, int length)
b174baf4 67{
7c388ec1
JC
68 /* Avoid an invalid state */
69 if (length < 2)
70 length = 2;
b174baf4
JC
71 if (r->length != length) {
72 r->length = length;
869871b5 73 iio_mark_update_needed_kfifo(r);
b174baf4
JC
74 }
75 return 0;
76}
b174baf4 77
14555b14 78static int iio_store_to_kfifo(struct iio_buffer *r,
5d65d920 79 const void *data)
b174baf4
JC
80{
81 int ret;
82 struct iio_kfifo *kf = iio_to_kfifo(r);
c559afbf
JC
83 ret = kfifo_in(&kf->kf, data, 1);
84 if (ret != 1)
b174baf4 85 return -EBUSY;
355c1a14 86
623c334c 87 wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM);
c559afbf 88
b174baf4
JC
89 return 0;
90}
b174baf4 91
14555b14 92static int iio_read_first_n_kfifo(struct iio_buffer *r,
b26a2188 93 size_t n, char __user *buf)
b174baf4
JC
94{
95 int ret, copied;
96 struct iio_kfifo *kf = iio_to_kfifo(r);
97
0894d80d
LPC
98 if (mutex_lock_interruptible(&kf->user_lock))
99 return -ERESTARTSYS;
8fe64955 100
0894d80d
LPC
101 if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
102 ret = -EINVAL;
103 else
104 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
0894d80d
LPC
105 mutex_unlock(&kf->user_lock);
106 if (ret < 0)
107 return ret;
108
b174baf4
JC
109 return copied;
110}
5565a450 111
355c1a14
LPC
112static bool iio_kfifo_buf_data_available(struct iio_buffer *r)
113{
114 struct iio_kfifo *kf = iio_to_kfifo(r);
115 bool empty;
116
117 mutex_lock(&kf->user_lock);
118 empty = kfifo_is_empty(&kf->kf);
119 mutex_unlock(&kf->user_lock);
120
121 return !empty;
122}
123
9e69c935
LPC
124static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
125{
f6c23f48
LPC
126 struct iio_kfifo *kf = iio_to_kfifo(buffer);
127
0894d80d 128 mutex_destroy(&kf->user_lock);
f6c23f48
LPC
129 kfifo_free(&kf->kf);
130 kfree(kf);
9e69c935
LPC
131}
132
7e632344 133static const struct iio_buffer_access_funcs kfifo_access_funcs = {
5565a450
JC
134 .store_to = &iio_store_to_kfifo,
135 .read_first_n = &iio_read_first_n_kfifo,
355c1a14 136 .data_available = iio_kfifo_buf_data_available,
5565a450 137 .request_update = &iio_request_update_kfifo,
5565a450 138 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
5565a450 139 .set_length = &iio_set_length_kfifo,
9e69c935 140 .release = &iio_kfifo_buffer_release,
5565a450 141};
7e632344 142
7ab374a0 143struct iio_buffer *iio_kfifo_allocate(void)
7e632344
LPC
144{
145 struct iio_kfifo *kf;
146
7ab374a0 147 kf = kzalloc(sizeof(*kf), GFP_KERNEL);
7e632344
LPC
148 if (!kf)
149 return NULL;
7ab374a0 150
7e632344
LPC
151 kf->update_needed = true;
152 iio_buffer_init(&kf->buffer);
7e632344 153 kf->buffer.access = &kfifo_access_funcs;
7c388ec1 154 kf->buffer.length = 2;
0894d80d 155 mutex_init(&kf->user_lock);
7ab374a0 156
7e632344
LPC
157 return &kf->buffer;
158}
159EXPORT_SYMBOL(iio_kfifo_allocate);
160
161void iio_kfifo_free(struct iio_buffer *r)
162{
9e69c935 163 iio_buffer_put(r);
7e632344
LPC
164}
165EXPORT_SYMBOL(iio_kfifo_free);
5565a450 166
780103fe
KW
167static void devm_iio_kfifo_release(struct device *dev, void *res)
168{
169 iio_kfifo_free(*(struct iio_buffer **)res);
170}
171
172static int devm_iio_kfifo_match(struct device *dev, void *res, void *data)
173{
174 struct iio_buffer **r = res;
175
176 if (WARN_ON(!r || !*r))
177 return 0;
178
179 return *r == data;
180}
181
182/**
183 * devm_iio_fifo_allocate - Resource-managed iio_kfifo_allocate()
184 * @dev: Device to allocate kfifo buffer for
185 *
186 * RETURNS:
187 * Pointer to allocated iio_buffer on success, NULL on failure.
188 */
189struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
190{
191 struct iio_buffer **ptr, *r;
192
193 ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
194 if (!ptr)
195 return NULL;
196
197 r = iio_kfifo_allocate();
198 if (r) {
199 *ptr = r;
200 devres_add(dev, ptr);
201 } else {
202 devres_free(ptr);
203 }
204
205 return r;
206}
207EXPORT_SYMBOL(devm_iio_kfifo_allocate);
208
209/**
210 * devm_iio_fifo_free - Resource-managed iio_kfifo_free()
211 * @dev: Device the buffer belongs to
212 * @r: The buffer associated with the device
213 */
214void devm_iio_kfifo_free(struct device *dev, struct iio_buffer *r)
215{
216 WARN_ON(devres_release(dev, devm_iio_kfifo_release,
217 devm_iio_kfifo_match, r));
218}
219EXPORT_SYMBOL(devm_iio_kfifo_free);
220
b174baf4 221MODULE_LICENSE("GPL");
This page took 0.443746 seconds and 5 git commands to generate.