^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kfifo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/iio/buffer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/iio/kfifo_buf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/iio/buffer_impl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct iio_kfifo {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct iio_buffer buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct kfifo kf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct mutex user_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int update_needed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) size_t bytes_per_datum, unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if ((length == 0) || (bytes_per_datum == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Make sure we don't overflow an unsigned int after kfifo rounds up to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * the next power of 2.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (roundup_pow_of_two(length) > UINT_MAX / bytes_per_datum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) bytes_per_datum, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int iio_request_update_kfifo(struct iio_buffer *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct iio_kfifo *buf = iio_to_kfifo(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) mutex_lock(&buf->user_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (buf->update_needed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) kfifo_free(&buf->kf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) buf->buffer.length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) buf->update_needed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) kfifo_reset_out(&buf->kf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) mutex_unlock(&buf->user_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct iio_kfifo *kf = iio_to_kfifo(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) kf->update_needed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (r->bytes_per_datum != bpd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) r->bytes_per_datum = bpd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) iio_mark_update_needed_kfifo(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static int iio_set_length_kfifo(struct iio_buffer *r, unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /* Avoid an invalid state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (length < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) length = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (r->length != length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) r->length = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) iio_mark_update_needed_kfifo(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static int iio_store_to_kfifo(struct iio_buffer *r,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) const void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct iio_kfifo *kf = iio_to_kfifo(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ret = kfifo_in(&kf->kf, data, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (ret != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int iio_read_kfifo(struct iio_buffer *r, size_t n, char __user *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int ret, copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct iio_kfifo *kf = iio_to_kfifo(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (mutex_lock_interruptible(&kf->user_lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) ret = kfifo_to_user(&kf->kf, buf, n, &copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) mutex_unlock(&kf->user_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static size_t iio_kfifo_buf_data_available(struct iio_buffer *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct iio_kfifo *kf = iio_to_kfifo(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) size_t samples;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) mutex_lock(&kf->user_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) samples = kfifo_len(&kf->kf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) mutex_unlock(&kf->user_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return samples;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct iio_kfifo *kf = iio_to_kfifo(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) mutex_destroy(&kf->user_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) kfifo_free(&kf->kf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) kfree(kf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static const struct iio_buffer_access_funcs kfifo_access_funcs = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .store_to = &iio_store_to_kfifo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .read = &iio_read_kfifo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .data_available = iio_kfifo_buf_data_available,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .request_update = &iio_request_update_kfifo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .set_length = &iio_set_length_kfifo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .release = &iio_kfifo_buffer_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) .modes = INDIO_BUFFER_SOFTWARE | INDIO_BUFFER_TRIGGERED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct iio_buffer *iio_kfifo_allocate(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct iio_kfifo *kf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) kf = kzalloc(sizeof(*kf), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (!kf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) kf->update_needed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) iio_buffer_init(&kf->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) kf->buffer.access = &kfifo_access_funcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) kf->buffer.length = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) mutex_init(&kf->user_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return &kf->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) EXPORT_SYMBOL(iio_kfifo_allocate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) void iio_kfifo_free(struct iio_buffer *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) iio_buffer_put(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) EXPORT_SYMBOL(iio_kfifo_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static void devm_iio_kfifo_release(struct device *dev, void *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) iio_kfifo_free(*(struct iio_buffer **)res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * devm_iio_fifo_allocate - Resource-managed iio_kfifo_allocate()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * @dev: Device to allocate kfifo buffer for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * RETURNS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * Pointer to allocated iio_buffer on success, NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct iio_buffer **ptr, *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (!ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) r = iio_kfifo_allocate();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) *ptr = r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) devres_add(dev, ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) devres_free(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) EXPORT_SYMBOL(devm_iio_kfifo_allocate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) MODULE_LICENSE("GPL");