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>
11 static inline int __iio_allocate_kfifo(struct iio_kfifo
*buf
,
12 int bytes_per_datum
, int length
)
14 if ((length
== 0) || (bytes_per_datum
== 0))
17 __iio_update_ring_buffer(&buf
->ring
, bytes_per_datum
, length
);
18 return kfifo_alloc(&buf
->kf
, bytes_per_datum
*length
, GFP_KERNEL
);
21 int iio_request_update_kfifo(struct iio_ring_buffer
*r
)
24 struct iio_kfifo
*buf
= iio_to_kfifo(r
);
26 mutex_lock(&buf
->use_lock
);
27 if (!buf
->update_needed
)
34 ret
= __iio_allocate_kfifo(buf
, buf
->ring
.bytes_per_datum
,
37 mutex_unlock(&buf
->use_lock
);
40 EXPORT_SYMBOL(iio_request_update_kfifo
);
42 void iio_mark_kfifo_in_use(struct iio_ring_buffer
*r
)
44 struct iio_kfifo
*buf
= iio_to_kfifo(r
);
45 mutex_lock(&buf
->use_lock
);
47 mutex_unlock(&buf
->use_lock
);
49 EXPORT_SYMBOL(iio_mark_kfifo_in_use
);
51 void iio_unmark_kfifo_in_use(struct iio_ring_buffer
*r
)
53 struct iio_kfifo
*buf
= iio_to_kfifo(r
);
54 mutex_lock(&buf
->use_lock
);
56 mutex_unlock(&buf
->use_lock
);
58 EXPORT_SYMBOL(iio_unmark_kfifo_in_use
);
60 int iio_get_length_kfifo(struct iio_ring_buffer
*r
)
64 EXPORT_SYMBOL(iio_get_length_kfifo
);
66 static inline void __iio_init_kfifo(struct iio_kfifo
*kf
)
68 mutex_init(&kf
->use_lock
);
71 static IIO_RING_ENABLE_ATTR
;
72 static IIO_RING_BYTES_PER_DATUM_ATTR
;
73 static IIO_RING_LENGTH_ATTR
;
75 static struct attribute
*iio_kfifo_attributes
[] = {
76 &dev_attr_length
.attr
,
77 &dev_attr_bytes_per_datum
.attr
,
78 &dev_attr_enable
.attr
,
82 static struct attribute_group iio_kfifo_attribute_group
= {
83 .attrs
= iio_kfifo_attributes
,
86 static const struct attribute_group
*iio_kfifo_attribute_groups
[] = {
87 &iio_kfifo_attribute_group
,
91 static void iio_kfifo_release(struct device
*dev
)
93 struct iio_ring_buffer
*r
= to_iio_ring_buffer(dev
);
94 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
99 static struct device_type iio_kfifo_type
= {
100 .release
= iio_kfifo_release
,
101 .groups
= iio_kfifo_attribute_groups
,
104 struct iio_ring_buffer
*iio_kfifo_allocate(struct iio_dev
*indio_dev
)
106 struct iio_kfifo
*kf
;
108 kf
= kzalloc(sizeof *kf
, GFP_KERNEL
);
111 iio_ring_buffer_init(&kf
->ring
, indio_dev
);
112 __iio_init_kfifo(kf
);
113 kf
->ring
.dev
.type
= &iio_kfifo_type
;
114 device_initialize(&kf
->ring
.dev
);
115 kf
->ring
.dev
.parent
= &indio_dev
->dev
;
116 kf
->ring
.dev
.bus
= &iio_bus_type
;
117 dev_set_drvdata(&kf
->ring
.dev
, (void *)&(kf
->ring
));
121 EXPORT_SYMBOL(iio_kfifo_allocate
);
123 int iio_get_bytes_per_datum_kfifo(struct iio_ring_buffer
*r
)
125 return r
->bytes_per_datum
;
127 EXPORT_SYMBOL(iio_get_bytes_per_datum_kfifo
);
129 int iio_set_bytes_per_datum_kfifo(struct iio_ring_buffer
*r
, size_t bpd
)
131 if (r
->bytes_per_datum
!= bpd
) {
132 r
->bytes_per_datum
= bpd
;
133 if (r
->access
.mark_param_change
)
134 r
->access
.mark_param_change(r
);
138 EXPORT_SYMBOL(iio_set_bytes_per_datum_kfifo
);
140 int iio_mark_update_needed_kfifo(struct iio_ring_buffer
*r
)
142 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
143 kf
->update_needed
= true;
146 EXPORT_SYMBOL(iio_mark_update_needed_kfifo
);
148 int iio_set_length_kfifo(struct iio_ring_buffer
*r
, int length
)
150 if (r
->length
!= length
) {
152 if (r
->access
.mark_param_change
)
153 r
->access
.mark_param_change(r
);
157 EXPORT_SYMBOL(iio_set_length_kfifo
);
159 void iio_kfifo_free(struct iio_ring_buffer
*r
)
162 iio_put_ring_buffer(r
);
164 EXPORT_SYMBOL(iio_kfifo_free
);
166 int iio_store_to_kfifo(struct iio_ring_buffer
*r
, u8
*data
, s64 timestamp
)
169 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
170 u8
*datal
= kmalloc(r
->bytes_per_datum
, GFP_KERNEL
);
171 memcpy(datal
, data
, r
->bytes_per_datum
- sizeof(timestamp
));
172 memcpy(datal
+ r
->bytes_per_datum
- sizeof(timestamp
),
173 ×tamp
, sizeof(timestamp
));
174 ret
= kfifo_in(&kf
->kf
, data
, r
->bytes_per_datum
);
175 if (ret
!= r
->bytes_per_datum
) {
182 EXPORT_SYMBOL(iio_store_to_kfifo
);
184 int iio_read_first_n_kfifo(struct iio_ring_buffer
*r
,
185 size_t n
, char __user
*buf
)
188 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
190 ret
= kfifo_to_user(&kf
->kf
, buf
, r
->bytes_per_datum
*n
, &copied
);
194 EXPORT_SYMBOL(iio_read_first_n_kfifo
);
195 MODULE_LICENSE("GPL");