static void iio_kfifo_buffer_release(struct iio_buffer *buffer) { struct iio_kfifo *kf = iio_to_kfifo(buffer); mutex_destroy(&kf->user_lock); kfifo_free(&kf->kf); kfree(kf); }
static bool iio_kfifo_buf_space_available(struct iio_buffer *r) { struct iio_kfifo *kf = iio_to_kfifo(r); bool full; mutex_lock(&kf->user_lock); full = kfifo_is_full(&kf->kf); mutex_unlock(&kf->user_lock); return !full; }
static bool iio_kfifo_buf_data_available(struct iio_buffer *r) { struct iio_kfifo *kf = iio_to_kfifo(r); bool empty; mutex_lock(&kf->user_lock); empty = kfifo_is_empty(&kf->kf); mutex_unlock(&kf->user_lock); return !empty; }
static int iio_store_to_kfifo(struct iio_buffer *r, u8 *data, s64 timestamp) { int ret; struct iio_kfifo *kf = iio_to_kfifo(r); ret = kfifo_in(&kf->kf, data, r->bytes_per_datum); if (ret != r->bytes_per_datum) return -EBUSY; return 0; }
static int iio_store_to_kfifo(struct iio_buffer *r, u8 *data, s64 timestamp) { int ret; struct iio_kfifo *kf = iio_to_kfifo(r); ret = kfifo_in(&kf->kf, data, r->bytes_per_datum); if (ret != r->bytes_per_datum) return -EBUSY; r->stufftoread = true; wake_up_interruptible(&r->pollq); return 0; }
static int iio_store_to_kfifo(struct iio_buffer *r, const void *data) { int ret; struct iio_kfifo *kf = iio_to_kfifo(r); ret = kfifo_in(&kf->kf, data, 1); if (ret != 1) return -EBUSY; wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM); return 0; }
static int iio_request_update_kfifo(struct iio_buffer *r) { int ret = 0; struct iio_kfifo *buf = iio_to_kfifo(r); if (!buf->update_needed) goto error_ret; kfifo_free(&buf->kf); ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum, buf->buffer.length); error_ret: return ret; }
static int iio_read_first_n_kfifo(struct iio_buffer *r, size_t n, char __user *buf) { int ret, copied; struct iio_kfifo *kf = iio_to_kfifo(r); if (n < r->bytes_per_datum) return -EINVAL; n = rounddown(n, r->bytes_per_datum); ret = kfifo_to_user(&kf->kf, buf, n, &copied); return copied; }
static int iio_kfifo_remove_from(struct iio_buffer *r, void *data) { int ret; struct iio_kfifo *kf = iio_to_kfifo(r); if (kfifo_size(&kf->kf) < r->bytes_per_datum) return -EBUSY; ret = kfifo_out(&kf->kf, data, r->bytes_per_datum); if (ret != r->bytes_per_datum) return -EBUSY; wake_up_interruptible_poll(&r->pollq, POLLOUT | POLLWRNORM); return 0; }
static int iio_kfifo_write(struct iio_buffer *r, size_t n, const char __user *buf) { struct iio_kfifo *kf = iio_to_kfifo(r); int ret, copied; mutex_lock(&kf->user_lock); if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf)) ret = -EINVAL; else ret = kfifo_from_user(&kf->kf, buf, n, &copied); mutex_unlock(&kf->user_lock); if (ret) return ret; return copied; }
static int iio_read_kfifo(struct iio_buffer *r, size_t n, char __user *buf) { int ret, copied; struct iio_kfifo *kf = iio_to_kfifo(r); if (mutex_lock_interruptible(&kf->user_lock)) return -ERESTARTSYS; if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf)) ret = -EINVAL; else ret = kfifo_to_user(&kf->kf, buf, n, &copied); mutex_unlock(&kf->user_lock); if (ret < 0) return ret; return copied; }
static int iio_request_update_kfifo(struct iio_buffer *r) { int ret = 0; struct iio_kfifo *buf = iio_to_kfifo(r); mutex_lock(&buf->user_lock); if (buf->update_needed) { kfifo_free(&buf->kf); ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum, buf->buffer.length); buf->update_needed = false; } else { kfifo_reset_out(&buf->kf); } mutex_unlock(&buf->user_lock); return ret; }
static int iio_read_first_n_kfifo(struct iio_buffer *r, size_t n, char __user *buf) { int ret, copied; struct iio_kfifo *kf = iio_to_kfifo(r); if (n < r->bytes_per_datum) return -EINVAL; n = rounddown(n, r->bytes_per_datum); ret = kfifo_to_user(&kf->kf, buf, n, &copied); if (kfifo_is_empty(&kf->kf)) r->stufftoread = false; /* verify it is still empty to avoid race */ if (!kfifo_is_empty(&kf->kf)) r->stufftoread = true; return copied; }
static int iio_mark_update_needed_kfifo(struct iio_buffer *r) { struct iio_kfifo *kf = iio_to_kfifo(r); kf->update_needed = true; return 0; }
void iio_kfifo_free(struct iio_buffer *r) { kfree(iio_to_kfifo(r)); }