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 return __kfifo_alloc((struct __kfifo
*)&buf
->kf
, length
,
26 bytes_per_datum
, GFP_KERNEL
);
29 static int iio_request_update_kfifo(struct iio_buffer
*r
)
32 struct iio_kfifo
*buf
= iio_to_kfifo(r
);
34 if (!buf
->update_needed
)
37 ret
= __iio_allocate_kfifo(buf
, buf
->buffer
.bytes_per_datum
,
39 r
->stufftoread
= false;
44 static int iio_get_length_kfifo(struct iio_buffer
*r
)
49 static IIO_BUFFER_ENABLE_ATTR
;
50 static IIO_BUFFER_LENGTH_ATTR
;
52 static struct attribute
*iio_kfifo_attributes
[] = {
53 &dev_attr_length
.attr
,
54 &dev_attr_enable
.attr
,
58 static struct attribute_group iio_kfifo_attribute_group
= {
59 .attrs
= iio_kfifo_attributes
,
63 static int iio_get_bytes_per_datum_kfifo(struct iio_buffer
*r
)
65 return r
->bytes_per_datum
;
68 static int iio_mark_update_needed_kfifo(struct iio_buffer
*r
)
70 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
71 kf
->update_needed
= true;
75 static int iio_set_bytes_per_datum_kfifo(struct iio_buffer
*r
, size_t bpd
)
77 if (r
->bytes_per_datum
!= bpd
) {
78 r
->bytes_per_datum
= bpd
;
79 iio_mark_update_needed_kfifo(r
);
84 static int iio_set_length_kfifo(struct iio_buffer
*r
, int length
)
86 /* Avoid an invalid state */
89 if (r
->length
!= length
) {
91 iio_mark_update_needed_kfifo(r
);
96 static int iio_store_to_kfifo(struct iio_buffer
*r
,
100 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
101 ret
= kfifo_in(&kf
->kf
, data
, 1);
104 r
->stufftoread
= true;
105 wake_up_interruptible(&r
->pollq
);
110 static int iio_read_first_n_kfifo(struct iio_buffer
*r
,
111 size_t n
, char __user
*buf
)
114 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
116 if (n
< r
->bytes_per_datum
|| r
->bytes_per_datum
== 0)
119 ret
= kfifo_to_user(&kf
->kf
, buf
, n
, &copied
);
123 if (kfifo_is_empty(&kf
->kf
))
124 r
->stufftoread
= false;
125 /* verify it is still empty to avoid race */
126 if (!kfifo_is_empty(&kf
->kf
))
127 r
->stufftoread
= true;
132 static const struct iio_buffer_access_funcs kfifo_access_funcs
= {
133 .store_to
= &iio_store_to_kfifo
,
134 .read_first_n
= &iio_read_first_n_kfifo
,
135 .request_update
= &iio_request_update_kfifo
,
136 .get_bytes_per_datum
= &iio_get_bytes_per_datum_kfifo
,
137 .set_bytes_per_datum
= &iio_set_bytes_per_datum_kfifo
,
138 .get_length
= &iio_get_length_kfifo
,
139 .set_length
= &iio_set_length_kfifo
,
142 struct iio_buffer
*iio_kfifo_allocate(struct iio_dev
*indio_dev
)
144 struct iio_kfifo
*kf
;
146 kf
= kzalloc(sizeof *kf
, GFP_KERNEL
);
149 kf
->update_needed
= true;
150 iio_buffer_init(&kf
->buffer
);
151 kf
->buffer
.attrs
= &iio_kfifo_attribute_group
;
152 kf
->buffer
.access
= &kfifo_access_funcs
;
153 kf
->buffer
.length
= 2;
156 EXPORT_SYMBOL(iio_kfifo_allocate
);
158 void iio_kfifo_free(struct iio_buffer
*r
)
160 kfree(iio_to_kfifo(r
));
162 EXPORT_SYMBOL(iio_kfifo_free
);
164 MODULE_LICENSE("GPL");