Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: kfifo-buffer: Add output buffer support

Add output buffer support to the kfifo buffer implementation.

The implementation is straight forward and mostly just wraps the kfifo
API to provide the required operations.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
Signed-off-by: Mihail Chindris <mihail.chindris@analog.com>
Link: https://lore.kernel.org/r/20211007080035.2531-3-mihail.chindris@analog.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Lars-Peter Clausen and committed by
Jonathan Cameron
1546d671 9eeee3b0

+50
+50
drivers/iio/buffer/kfifo_buf.c
··· 138 138 kfree(kf); 139 139 } 140 140 141 + static size_t iio_kfifo_buf_space_available(struct iio_buffer *r) 142 + { 143 + struct iio_kfifo *kf = iio_to_kfifo(r); 144 + size_t avail; 145 + 146 + mutex_lock(&kf->user_lock); 147 + avail = kfifo_avail(&kf->kf); 148 + mutex_unlock(&kf->user_lock); 149 + 150 + return avail; 151 + } 152 + 153 + static int iio_kfifo_remove_from(struct iio_buffer *r, void *data) 154 + { 155 + int ret; 156 + struct iio_kfifo *kf = iio_to_kfifo(r); 157 + 158 + if (kfifo_size(&kf->kf) < 1) 159 + return -EBUSY; 160 + 161 + ret = kfifo_out(&kf->kf, data, 1); 162 + if (ret != 1) 163 + return -EBUSY; 164 + 165 + wake_up_interruptible_poll(&r->pollq, EPOLLOUT | EPOLLWRNORM); 166 + 167 + return 0; 168 + } 169 + 170 + static int iio_kfifo_write(struct iio_buffer *r, size_t n, 171 + const char __user *buf) 172 + { 173 + struct iio_kfifo *kf = iio_to_kfifo(r); 174 + int ret, copied; 175 + 176 + mutex_lock(&kf->user_lock); 177 + if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf)) 178 + ret = -EINVAL; 179 + else 180 + ret = kfifo_from_user(&kf->kf, buf, n, &copied); 181 + mutex_unlock(&kf->user_lock); 182 + if (ret) 183 + return ret; 184 + 185 + return copied; 186 + } 187 + 141 188 static const struct iio_buffer_access_funcs kfifo_access_funcs = { 142 189 .store_to = &iio_store_to_kfifo, 143 190 .read = &iio_read_kfifo, 144 191 .data_available = iio_kfifo_buf_data_available, 192 + .remove_from = &iio_kfifo_remove_from, 193 + .write = &iio_kfifo_write, 194 + .space_available = &iio_kfifo_buf_space_available, 145 195 .request_update = &iio_request_update_kfifo, 146 196 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo, 147 197 .set_length = &iio_set_length_kfifo,