]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/iio/buffer/kfifo_buf.c
treewide: Add SPDX license identifier for more missed files
[thirdparty/linux.git] / drivers / iio / buffer / kfifo_buf.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
b174baf4
JC
2#include <linux/slab.h>
3#include <linux/kernel.h>
4#include <linux/module.h>
5#include <linux/device.h>
6#include <linux/workqueue.h>
7#include <linux/kfifo.h>
8#include <linux/mutex.h>
19a5a8a5 9#include <linux/iio/iio.h>
8abd5ba5 10#include <linux/iio/buffer.h>
06458e27 11#include <linux/iio/kfifo_buf.h>
33dd94cb 12#include <linux/iio/buffer_impl.h>
7c388ec1 13#include <linux/sched.h>
623c334c 14#include <linux/poll.h>
b174baf4 15
a710cc77 16struct iio_kfifo {
14555b14 17 struct iio_buffer buffer;
a710cc77 18 struct kfifo kf;
0894d80d 19 struct mutex user_lock;
a710cc77 20 int update_needed;
a710cc77
JC
21};
22
14555b14 23#define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
5565a450 24
b174baf4 25static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
c043ec1c 26 size_t bytes_per_datum, unsigned int length)
b174baf4
JC
27{
28 if ((length == 0) || (bytes_per_datum == 0))
29 return -EINVAL;
30
3d13de4b
MK
31 /*
32 * Make sure we don't overflow an unsigned int after kfifo rounds up to
33 * the next power of 2.
34 */
35 if (roundup_pow_of_two(length) > UINT_MAX / bytes_per_datum)
36 return -EINVAL;
37
c559afbf
JC
38 return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
39 bytes_per_datum, GFP_KERNEL);
b174baf4
JC
40}
41
14555b14 42static int iio_request_update_kfifo(struct iio_buffer *r)
b174baf4
JC
43{
44 int ret = 0;
45 struct iio_kfifo *buf = iio_to_kfifo(r);
46
0894d80d 47 mutex_lock(&buf->user_lock);
5b78e513
LPC
48 if (buf->update_needed) {
49 kfifo_free(&buf->kf);
50 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
14555b14 51 buf->buffer.length);
e5f1efb9
GM
52 if (ret >= 0)
53 buf->update_needed = false;
5b78e513
LPC
54 } else {
55 kfifo_reset_out(&buf->kf);
56 }
0894d80d 57 mutex_unlock(&buf->user_lock);
5b78e513 58
b174baf4
JC
59 return ret;
60}
b174baf4 61
869871b5 62static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
b174baf4 63{
869871b5
LPC
64 struct iio_kfifo *kf = iio_to_kfifo(r);
65 kf->update_needed = true;
b174baf4
JC
66 return 0;
67}
b174baf4 68
869871b5 69static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
b174baf4 70{
869871b5
LPC
71 if (r->bytes_per_datum != bpd) {
72 r->bytes_per_datum = bpd;
73 iio_mark_update_needed_kfifo(r);
74 }
b174baf4
JC
75 return 0;
76}
b174baf4 77
c043ec1c 78static int iio_set_length_kfifo(struct iio_buffer *r, unsigned int length)
b174baf4 79{
7c388ec1
JC
80 /* Avoid an invalid state */
81 if (length < 2)
82 length = 2;
b174baf4
JC
83 if (r->length != length) {
84 r->length = length;
869871b5 85 iio_mark_update_needed_kfifo(r);
b174baf4
JC
86 }
87 return 0;
88}
b174baf4 89
14555b14 90static int iio_store_to_kfifo(struct iio_buffer *r,
5d65d920 91 const void *data)
b174baf4
JC
92{
93 int ret;
94 struct iio_kfifo *kf = iio_to_kfifo(r);
c559afbf
JC
95 ret = kfifo_in(&kf->kf, data, 1);
96 if (ret != 1)
b174baf4 97 return -EBUSY;
b174baf4
JC
98 return 0;
99}
b174baf4 100
14555b14 101static int iio_read_first_n_kfifo(struct iio_buffer *r,
b26a2188 102 size_t n, char __user *buf)
b174baf4
JC
103{
104 int ret, copied;
105 struct iio_kfifo *kf = iio_to_kfifo(r);
106
0894d80d
LPC
107 if (mutex_lock_interruptible(&kf->user_lock))
108 return -ERESTARTSYS;
8fe64955 109
0894d80d
LPC
110 if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
111 ret = -EINVAL;
112 else
113 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
0894d80d
LPC
114 mutex_unlock(&kf->user_lock);
115 if (ret < 0)
116 return ret;
117
b174baf4
JC
118 return copied;
119}
5565a450 120
37d34556 121static size_t iio_kfifo_buf_data_available(struct iio_buffer *r)
355c1a14
LPC
122{
123 struct iio_kfifo *kf = iio_to_kfifo(r);
37d34556 124 size_t samples;
355c1a14
LPC
125
126 mutex_lock(&kf->user_lock);
37d34556 127 samples = kfifo_len(&kf->kf);
355c1a14
LPC
128 mutex_unlock(&kf->user_lock);
129
37d34556 130 return samples;
355c1a14
LPC
131}
132
9e69c935
LPC
133static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
134{
f6c23f48
LPC
135 struct iio_kfifo *kf = iio_to_kfifo(buffer);
136
0894d80d 137 mutex_destroy(&kf->user_lock);
f6c23f48
LPC
138 kfifo_free(&kf->kf);
139 kfree(kf);
9e69c935
LPC
140}
141
7e632344 142static const struct iio_buffer_access_funcs kfifo_access_funcs = {
5565a450
JC
143 .store_to = &iio_store_to_kfifo,
144 .read_first_n = &iio_read_first_n_kfifo,
355c1a14 145 .data_available = iio_kfifo_buf_data_available,
5565a450 146 .request_update = &iio_request_update_kfifo,
5565a450 147 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
5565a450 148 .set_length = &iio_set_length_kfifo,
9e69c935 149 .release = &iio_kfifo_buffer_release,
225d59ad
LPC
150
151 .modes = INDIO_BUFFER_SOFTWARE | INDIO_BUFFER_TRIGGERED,
5565a450 152};
7e632344 153
7ab374a0 154struct iio_buffer *iio_kfifo_allocate(void)
7e632344
LPC
155{
156 struct iio_kfifo *kf;
157
7ab374a0 158 kf = kzalloc(sizeof(*kf), GFP_KERNEL);
7e632344
LPC
159 if (!kf)
160 return NULL;
7ab374a0 161
7e632344
LPC
162 kf->update_needed = true;
163 iio_buffer_init(&kf->buffer);
7e632344 164 kf->buffer.access = &kfifo_access_funcs;
7c388ec1 165 kf->buffer.length = 2;
0894d80d 166 mutex_init(&kf->user_lock);
7ab374a0 167
7e632344
LPC
168 return &kf->buffer;
169}
170EXPORT_SYMBOL(iio_kfifo_allocate);
171
172void iio_kfifo_free(struct iio_buffer *r)
173{
9e69c935 174 iio_buffer_put(r);
7e632344
LPC
175}
176EXPORT_SYMBOL(iio_kfifo_free);
5565a450 177
780103fe
KW
178static void devm_iio_kfifo_release(struct device *dev, void *res)
179{
180 iio_kfifo_free(*(struct iio_buffer **)res);
181}
182
183static int devm_iio_kfifo_match(struct device *dev, void *res, void *data)
184{
185 struct iio_buffer **r = res;
186
187 if (WARN_ON(!r || !*r))
188 return 0;
189
190 return *r == data;
191}
192
193/**
194 * devm_iio_fifo_allocate - Resource-managed iio_kfifo_allocate()
195 * @dev: Device to allocate kfifo buffer for
196 *
197 * RETURNS:
198 * Pointer to allocated iio_buffer on success, NULL on failure.
199 */
200struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
201{
202 struct iio_buffer **ptr, *r;
203
204 ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
205 if (!ptr)
206 return NULL;
207
208 r = iio_kfifo_allocate();
209 if (r) {
210 *ptr = r;
211 devres_add(dev, ptr);
212 } else {
213 devres_free(ptr);
214 }
215
216 return r;
217}
218EXPORT_SYMBOL(devm_iio_kfifo_allocate);
219
220/**
221 * devm_iio_fifo_free - Resource-managed iio_kfifo_free()
222 * @dev: Device the buffer belongs to
223 * @r: The buffer associated with the device
224 */
225void devm_iio_kfifo_free(struct device *dev, struct iio_buffer *r)
226{
227 WARN_ON(devres_release(dev, devm_iio_kfifo_release,
228 devm_iio_kfifo_match, r));
229}
230EXPORT_SYMBOL(devm_iio_kfifo_free);
231
b174baf4 232MODULE_LICENSE("GPL");