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>
8 #include <linux/iio/kfifo_buf.h>
9 #include <linux/sched.h>
10 #include <linux/poll.h>
13 struct iio_buffer buffer
;
15 struct mutex user_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 return __kfifo_alloc((struct __kfifo
*)&buf
->kf
, length
,
28 bytes_per_datum
, 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
->user_lock
);
37 if (buf
->update_needed
) {
39 ret
= __iio_allocate_kfifo(buf
, buf
->buffer
.bytes_per_datum
,
41 buf
->update_needed
= false;
43 kfifo_reset_out(&buf
->kf
);
45 r
->stufftoread
= false;
46 mutex_unlock(&buf
->user_lock
);
51 static int iio_get_length_kfifo(struct iio_buffer
*r
)
56 static IIO_BUFFER_ENABLE_ATTR
;
57 static IIO_BUFFER_LENGTH_ATTR
;
59 static struct attribute
*iio_kfifo_attributes
[] = {
60 &dev_attr_length
.attr
,
61 &dev_attr_enable
.attr
,
65 static struct attribute_group iio_kfifo_attribute_group
= {
66 .attrs
= iio_kfifo_attributes
,
70 static int iio_get_bytes_per_datum_kfifo(struct iio_buffer
*r
)
72 return r
->bytes_per_datum
;
75 static int iio_mark_update_needed_kfifo(struct iio_buffer
*r
)
77 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
78 kf
->update_needed
= true;
82 static int iio_set_bytes_per_datum_kfifo(struct iio_buffer
*r
, size_t bpd
)
84 if (r
->bytes_per_datum
!= bpd
) {
85 r
->bytes_per_datum
= bpd
;
86 iio_mark_update_needed_kfifo(r
);
91 static int iio_set_length_kfifo(struct iio_buffer
*r
, int length
)
93 /* Avoid an invalid state */
96 if (r
->length
!= length
) {
98 iio_mark_update_needed_kfifo(r
);
103 static int iio_store_to_kfifo(struct iio_buffer
*r
,
107 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
108 ret
= kfifo_in(&kf
->kf
, data
, 1);
111 r
->stufftoread
= true;
112 wake_up_interruptible_poll(&r
->pollq
, POLLIN
| POLLRDNORM
);
117 static int iio_read_first_n_kfifo(struct iio_buffer
*r
,
118 size_t n
, char __user
*buf
)
121 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
123 if (mutex_lock_interruptible(&kf
->user_lock
))
126 if (!kfifo_initialized(&kf
->kf
) || n
< kfifo_esize(&kf
->kf
))
129 ret
= kfifo_to_user(&kf
->kf
, buf
, n
, &copied
);
131 if (kfifo_is_empty(&kf
->kf
))
132 r
->stufftoread
= false;
133 /* verify it is still empty to avoid race */
134 if (!kfifo_is_empty(&kf
->kf
))
135 r
->stufftoread
= true;
137 mutex_unlock(&kf
->user_lock
);
144 static void iio_kfifo_buffer_release(struct iio_buffer
*buffer
)
146 struct iio_kfifo
*kf
= iio_to_kfifo(buffer
);
148 mutex_destroy(&kf
->user_lock
);
153 static const struct iio_buffer_access_funcs kfifo_access_funcs
= {
154 .store_to
= &iio_store_to_kfifo
,
155 .read_first_n
= &iio_read_first_n_kfifo
,
156 .request_update
= &iio_request_update_kfifo
,
157 .get_bytes_per_datum
= &iio_get_bytes_per_datum_kfifo
,
158 .set_bytes_per_datum
= &iio_set_bytes_per_datum_kfifo
,
159 .get_length
= &iio_get_length_kfifo
,
160 .set_length
= &iio_set_length_kfifo
,
161 .release
= &iio_kfifo_buffer_release
,
164 struct iio_buffer
*iio_kfifo_allocate(struct iio_dev
*indio_dev
)
166 struct iio_kfifo
*kf
;
168 kf
= kzalloc(sizeof *kf
, GFP_KERNEL
);
171 kf
->update_needed
= true;
172 iio_buffer_init(&kf
->buffer
);
173 kf
->buffer
.attrs
= &iio_kfifo_attribute_group
;
174 kf
->buffer
.access
= &kfifo_access_funcs
;
175 kf
->buffer
.length
= 2;
176 mutex_init(&kf
->user_lock
);
179 EXPORT_SYMBOL(iio_kfifo_allocate
);
181 void iio_kfifo_free(struct iio_buffer
*r
)
185 EXPORT_SYMBOL(iio_kfifo_free
);
187 MODULE_LICENSE("GPL");