1 #include <linux/slab.h>
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/device.h>
9 #include <linux/sched.h>
17 #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
19 static inline int __iio_allocate_kfifo(
struct iio_kfifo *
buf,
20 int bytes_per_datum,
int length)
22 if ((length == 0) || (bytes_per_datum == 0))
25 __iio_update_buffer(&buf->
buffer, bytes_per_datum, length);
30 static int iio_request_update_kfifo(
struct iio_buffer *
r)
38 ret = __iio_allocate_kfifo(buf, buf->
buffer.bytes_per_datum,
40 r->stufftoread =
false;
45 static int iio_get_length_kfifo(
struct iio_buffer *r)
50 static IIO_BUFFER_ENABLE_ATTR;
51 static IIO_BUFFER_LENGTH_ATTR;
53 static struct attribute *iio_kfifo_attributes[] = {
54 &dev_attr_length.attr,
55 &dev_attr_enable.attr,
60 .attrs = iio_kfifo_attributes,
64 static int iio_get_bytes_per_datum_kfifo(
struct iio_buffer *r)
66 return r->bytes_per_datum;
69 static int iio_mark_update_needed_kfifo(
struct iio_buffer *r)
76 static int iio_set_bytes_per_datum_kfifo(
struct iio_buffer *r,
size_t bpd)
78 if (r->bytes_per_datum != bpd) {
79 r->bytes_per_datum = bpd;
80 iio_mark_update_needed_kfifo(r);
85 static int iio_set_length_kfifo(
struct iio_buffer *r,
int length)
90 if (r->length != length) {
92 iio_mark_update_needed_kfifo(r);
97 static int iio_store_to_kfifo(
struct iio_buffer *r,
105 r->stufftoread =
true;
111 static int iio_read_first_n_kfifo(
struct iio_buffer *r,
112 size_t n,
char __user *buf)
117 if (n < r->bytes_per_datum || r->bytes_per_datum == 0)
125 r->stufftoread =
false;
128 r->stufftoread =
true;
133 static const struct iio_buffer_access_funcs kfifo_access_funcs = {
134 .store_to = &iio_store_to_kfifo,
135 .read_first_n = &iio_read_first_n_kfifo,
136 .request_update = &iio_request_update_kfifo,
137 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
138 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
139 .get_length = &iio_get_length_kfifo,
140 .set_length = &iio_set_length_kfifo,
152 kf->
buffer.attrs = &iio_kfifo_attribute_group;
153 kf->
buffer.access = &kfifo_access_funcs;