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>
12 struct iio_buffer buffer
;
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
);
26 return __kfifo_alloc((struct __kfifo
*)&buf
->kf
, length
,
27 bytes_per_datum
, GFP_KERNEL
);
30 static int iio_request_update_kfifo(struct iio_buffer
*r
)
33 struct iio_kfifo
*buf
= iio_to_kfifo(r
);
35 if (!buf
->update_needed
)
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
,
59 static struct attribute_group iio_kfifo_attribute_group
= {
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
)
71 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
72 kf
->update_needed
= true;
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
)
87 /* Avoid an invalid state */
90 if (r
->length
!= length
) {
92 iio_mark_update_needed_kfifo(r
);
97 static int iio_store_to_kfifo(struct iio_buffer
*r
,
101 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
102 ret
= kfifo_in(&kf
->kf
, data
, 1);
105 r
->stufftoread
= true;
106 wake_up_interruptible(&r
->pollq
);
111 static int iio_read_first_n_kfifo(struct iio_buffer
*r
,
112 size_t n
, char __user
*buf
)
115 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
117 if (n
< r
->bytes_per_datum
|| r
->bytes_per_datum
== 0)
120 ret
= kfifo_to_user(&kf
->kf
, buf
, n
, &copied
);
124 if (kfifo_is_empty(&kf
->kf
))
125 r
->stufftoread
= false;
126 /* verify it is still empty to avoid race */
127 if (!kfifo_is_empty(&kf
->kf
))
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
,
143 struct iio_buffer
*iio_kfifo_allocate(struct iio_dev
*indio_dev
)
145 struct iio_kfifo
*kf
;
147 kf
= kzalloc(sizeof *kf
, GFP_KERNEL
);
150 kf
->update_needed
= true;
151 iio_buffer_init(&kf
->buffer
);
152 kf
->buffer
.attrs
= &iio_kfifo_attribute_group
;
153 kf
->buffer
.access
= &kfifo_access_funcs
;
154 kf
->buffer
.length
= 2;
157 EXPORT_SYMBOL(iio_kfifo_allocate
);
159 void iio_kfifo_free(struct iio_buffer
*r
)
161 kfree(iio_to_kfifo(r
));
163 EXPORT_SYMBOL(iio_kfifo_free
);
165 MODULE_LICENSE("GPL");