1 // SPDX-License-Identifier: GPL-2.0-only
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>
9 #include <linux/iio/iio.h>
10 #include <linux/iio/buffer.h>
11 #include <linux/iio/kfifo_buf.h>
12 #include <linux/iio/buffer_impl.h>
13 #include <linux/sched.h>
14 #include <linux/poll.h>
17 struct iio_buffer buffer
;
19 struct mutex user_lock
;
23 #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
25 static inline int __iio_allocate_kfifo(struct iio_kfifo
*buf
,
26 size_t bytes_per_datum
, unsigned int length
)
28 if ((length
== 0) || (bytes_per_datum
== 0))
32 * Make sure we don't overflow an unsigned int after kfifo rounds up to
33 * the next power of 2.
35 if (roundup_pow_of_two(length
) > UINT_MAX
/ bytes_per_datum
)
38 return __kfifo_alloc((struct __kfifo
*)&buf
->kf
, length
,
39 bytes_per_datum
, GFP_KERNEL
);
42 static int iio_request_update_kfifo(struct iio_buffer
*r
)
45 struct iio_kfifo
*buf
= iio_to_kfifo(r
);
47 mutex_lock(&buf
->user_lock
);
48 if (buf
->update_needed
) {
50 ret
= __iio_allocate_kfifo(buf
, buf
->buffer
.bytes_per_datum
,
53 buf
->update_needed
= false;
55 kfifo_reset_out(&buf
->kf
);
57 mutex_unlock(&buf
->user_lock
);
62 static int iio_mark_update_needed_kfifo(struct iio_buffer
*r
)
64 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
65 kf
->update_needed
= true;
69 static int iio_set_bytes_per_datum_kfifo(struct iio_buffer
*r
, size_t bpd
)
71 if (r
->bytes_per_datum
!= bpd
) {
72 r
->bytes_per_datum
= bpd
;
73 iio_mark_update_needed_kfifo(r
);
78 static int iio_set_length_kfifo(struct iio_buffer
*r
, unsigned int length
)
80 /* Avoid an invalid state */
83 if (r
->length
!= length
) {
85 iio_mark_update_needed_kfifo(r
);
90 static int iio_store_to_kfifo(struct iio_buffer
*r
,
94 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
95 ret
= kfifo_in(&kf
->kf
, data
, 1);
101 static int iio_read_first_n_kfifo(struct iio_buffer
*r
,
102 size_t n
, char __user
*buf
)
105 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
107 if (mutex_lock_interruptible(&kf
->user_lock
))
110 if (!kfifo_initialized(&kf
->kf
) || n
< kfifo_esize(&kf
->kf
))
113 ret
= kfifo_to_user(&kf
->kf
, buf
, n
, &copied
);
114 mutex_unlock(&kf
->user_lock
);
121 static size_t iio_kfifo_buf_data_available(struct iio_buffer
*r
)
123 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
126 mutex_lock(&kf
->user_lock
);
127 samples
= kfifo_len(&kf
->kf
);
128 mutex_unlock(&kf
->user_lock
);
133 static void iio_kfifo_buffer_release(struct iio_buffer
*buffer
)
135 struct iio_kfifo
*kf
= iio_to_kfifo(buffer
);
137 mutex_destroy(&kf
->user_lock
);
142 static const struct iio_buffer_access_funcs kfifo_access_funcs
= {
143 .store_to
= &iio_store_to_kfifo
,
144 .read_first_n
= &iio_read_first_n_kfifo
,
145 .data_available
= iio_kfifo_buf_data_available
,
146 .request_update
= &iio_request_update_kfifo
,
147 .set_bytes_per_datum
= &iio_set_bytes_per_datum_kfifo
,
148 .set_length
= &iio_set_length_kfifo
,
149 .release
= &iio_kfifo_buffer_release
,
151 .modes
= INDIO_BUFFER_SOFTWARE
| INDIO_BUFFER_TRIGGERED
,
154 struct iio_buffer
*iio_kfifo_allocate(void)
156 struct iio_kfifo
*kf
;
158 kf
= kzalloc(sizeof(*kf
), GFP_KERNEL
);
162 kf
->update_needed
= true;
163 iio_buffer_init(&kf
->buffer
);
164 kf
->buffer
.access
= &kfifo_access_funcs
;
165 kf
->buffer
.length
= 2;
166 mutex_init(&kf
->user_lock
);
170 EXPORT_SYMBOL(iio_kfifo_allocate
);
172 void iio_kfifo_free(struct iio_buffer
*r
)
176 EXPORT_SYMBOL(iio_kfifo_free
);
178 static void devm_iio_kfifo_release(struct device
*dev
, void *res
)
180 iio_kfifo_free(*(struct iio_buffer
**)res
);
183 static int devm_iio_kfifo_match(struct device
*dev
, void *res
, void *data
)
185 struct iio_buffer
**r
= res
;
187 if (WARN_ON(!r
|| !*r
))
194 * devm_iio_fifo_allocate - Resource-managed iio_kfifo_allocate()
195 * @dev: Device to allocate kfifo buffer for
198 * Pointer to allocated iio_buffer on success, NULL on failure.
200 struct iio_buffer
*devm_iio_kfifo_allocate(struct device
*dev
)
202 struct iio_buffer
**ptr
, *r
;
204 ptr
= devres_alloc(devm_iio_kfifo_release
, sizeof(*ptr
), GFP_KERNEL
);
208 r
= iio_kfifo_allocate();
211 devres_add(dev
, ptr
);
218 EXPORT_SYMBOL(devm_iio_kfifo_allocate
);
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
225 void devm_iio_kfifo_free(struct device
*dev
, struct iio_buffer
*r
)
227 WARN_ON(devres_release(dev
, devm_iio_kfifo_release
,
228 devm_iio_kfifo_match
, r
));
230 EXPORT_SYMBOL(devm_iio_kfifo_free
);
232 MODULE_LICENSE("GPL");