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