1 #include <linux/slab.h>
2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/device.h>
5 #include <linux/workqueue.h>
6 #include <linux/kfifo.h>
7 #include <linux/mutex.h>
12 struct iio_buffer buffer;
16 struct mutex use_lock;
19 #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
21 static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
22 int bytes_per_datum, int length)
24 if ((length == 0) || (bytes_per_datum == 0))
27 __iio_update_buffer(&buf->buffer, bytes_per_datum, length);
28 return kfifo_alloc(&buf->kf, bytes_per_datum*length, GFP_KERNEL);
31 static int iio_request_update_kfifo(struct iio_buffer *r)
34 struct iio_kfifo *buf = iio_to_kfifo(r);
36 mutex_lock(&buf->use_lock);
37 if (!buf->update_needed)
44 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
47 mutex_unlock(&buf->use_lock);
51 static void iio_mark_kfifo_in_use(struct iio_buffer *r)
53 struct iio_kfifo *buf = iio_to_kfifo(r);
54 mutex_lock(&buf->use_lock);
56 mutex_unlock(&buf->use_lock);
59 static void iio_unmark_kfifo_in_use(struct iio_buffer *r)
61 struct iio_kfifo *buf = iio_to_kfifo(r);
62 mutex_lock(&buf->use_lock);
64 mutex_unlock(&buf->use_lock);
67 static int iio_get_length_kfifo(struct iio_buffer *r)
72 static inline void __iio_init_kfifo(struct iio_kfifo *kf)
74 mutex_init(&kf->use_lock);
77 static IIO_BUFFER_ENABLE_ATTR;
78 static IIO_BUFFER_LENGTH_ATTR;
80 static struct attribute *iio_kfifo_attributes[] = {
81 &dev_attr_length.attr,
82 &dev_attr_enable.attr,
86 static struct attribute_group iio_kfifo_attribute_group = {
87 .attrs = iio_kfifo_attributes,
91 struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
95 kf = kzalloc(sizeof *kf, GFP_KERNEL);
98 kf->update_needed = true;
99 iio_buffer_init(&kf->buffer);
100 kf->buffer.attrs = &iio_kfifo_attribute_group;
101 __iio_init_kfifo(kf);
105 EXPORT_SYMBOL(iio_kfifo_allocate);
107 static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
109 return r->bytes_per_datum;
112 static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
114 struct iio_kfifo *kf = iio_to_kfifo(r);
115 kf->update_needed = true;
119 static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
121 if (r->bytes_per_datum != bpd) {
122 r->bytes_per_datum = bpd;
123 iio_mark_update_needed_kfifo(r);
128 static int iio_set_length_kfifo(struct iio_buffer *r, int length)
130 if (r->length != length) {
132 iio_mark_update_needed_kfifo(r);
137 void iio_kfifo_free(struct iio_buffer *r)
139 kfree(iio_to_kfifo(r));
141 EXPORT_SYMBOL(iio_kfifo_free);
143 static int iio_store_to_kfifo(struct iio_buffer *r,
148 struct iio_kfifo *kf = iio_to_kfifo(r);
149 ret = kfifo_in(&kf->kf, data, r->bytes_per_datum);
150 if (ret != r->bytes_per_datum)
155 static int iio_read_first_n_kfifo(struct iio_buffer *r,
156 size_t n, char __user *buf)
159 struct iio_kfifo *kf = iio_to_kfifo(r);
161 if (n < r->bytes_per_datum)
164 n = rounddown(n, r->bytes_per_datum);
165 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
170 const struct iio_buffer_access_funcs kfifo_access_funcs = {
171 .mark_in_use = &iio_mark_kfifo_in_use,
172 .unmark_in_use = &iio_unmark_kfifo_in_use,
173 .store_to = &iio_store_to_kfifo,
174 .read_first_n = &iio_read_first_n_kfifo,
175 .request_update = &iio_request_update_kfifo,
176 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
177 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
178 .get_length = &iio_get_length_kfifo,
179 .set_length = &iio_set_length_kfifo,
181 EXPORT_SYMBOL(kfifo_access_funcs);
183 MODULE_LICENSE("GPL");