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/iio.h>
9 #include <linux/iio/buffer.h>
10 #include <linux/iio/kfifo_buf.h>
11 #include <linux/iio/buffer_impl.h>
12 #include <linux/sched.h>
13 #include <linux/poll.h>
16 struct iio_buffer buffer
;
18 struct mutex user_lock
;
22 #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
24 static inline int __iio_allocate_kfifo(struct iio_kfifo
*buf
,
25 int bytes_per_datum
, int length
)
27 if ((length
== 0) || (bytes_per_datum
== 0))
30 return __kfifo_alloc((struct __kfifo
*)&buf
->kf
, length
,
31 bytes_per_datum
, GFP_KERNEL
);
34 static int iio_request_update_kfifo(struct iio_buffer
*r
)
37 struct iio_kfifo
*buf
= iio_to_kfifo(r
);
39 mutex_lock(&buf
->user_lock
);
40 if (buf
->update_needed
) {
42 ret
= __iio_allocate_kfifo(buf
, buf
->buffer
.bytes_per_datum
,
45 buf
->update_needed
= false;
47 kfifo_reset_out(&buf
->kf
);
49 mutex_unlock(&buf
->user_lock
);
54 static int iio_mark_update_needed_kfifo(struct iio_buffer
*r
)
56 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
57 kf
->update_needed
= true;
61 static int iio_set_bytes_per_datum_kfifo(struct iio_buffer
*r
, size_t bpd
)
63 if (r
->bytes_per_datum
!= bpd
) {
64 r
->bytes_per_datum
= bpd
;
65 iio_mark_update_needed_kfifo(r
);
70 static int iio_set_length_kfifo(struct iio_buffer
*r
, int length
)
72 /* Avoid an invalid state */
75 if (r
->length
!= length
) {
77 iio_mark_update_needed_kfifo(r
);
82 static int iio_store_to_kfifo(struct iio_buffer
*r
,
86 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
87 ret
= kfifo_in(&kf
->kf
, data
, 1);
93 static int iio_read_first_n_kfifo(struct iio_buffer
*r
,
94 size_t n
, char __user
*buf
)
97 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
99 if (mutex_lock_interruptible(&kf
->user_lock
))
102 if (!kfifo_initialized(&kf
->kf
) || n
< kfifo_esize(&kf
->kf
))
105 ret
= kfifo_to_user(&kf
->kf
, buf
, n
, &copied
);
106 mutex_unlock(&kf
->user_lock
);
113 static size_t iio_kfifo_buf_data_available(struct iio_buffer
*r
)
115 struct iio_kfifo
*kf
= iio_to_kfifo(r
);
118 mutex_lock(&kf
->user_lock
);
119 samples
= kfifo_len(&kf
->kf
);
120 mutex_unlock(&kf
->user_lock
);
125 static void iio_kfifo_buffer_release(struct iio_buffer
*buffer
)
127 struct iio_kfifo
*kf
= iio_to_kfifo(buffer
);
129 mutex_destroy(&kf
->user_lock
);
134 static const struct iio_buffer_access_funcs kfifo_access_funcs
= {
135 .store_to
= &iio_store_to_kfifo
,
136 .read_first_n
= &iio_read_first_n_kfifo
,
137 .data_available
= iio_kfifo_buf_data_available
,
138 .request_update
= &iio_request_update_kfifo
,
139 .set_bytes_per_datum
= &iio_set_bytes_per_datum_kfifo
,
140 .set_length
= &iio_set_length_kfifo
,
141 .release
= &iio_kfifo_buffer_release
,
143 .modes
= INDIO_BUFFER_SOFTWARE
| INDIO_BUFFER_TRIGGERED
,
146 struct iio_buffer
*iio_kfifo_allocate(void)
148 struct iio_kfifo
*kf
;
150 kf
= kzalloc(sizeof(*kf
), GFP_KERNEL
);
154 kf
->update_needed
= true;
155 iio_buffer_init(&kf
->buffer
);
156 kf
->buffer
.access
= &kfifo_access_funcs
;
157 kf
->buffer
.length
= 2;
158 mutex_init(&kf
->user_lock
);
162 EXPORT_SYMBOL(iio_kfifo_allocate
);
164 void iio_kfifo_free(struct iio_buffer
*r
)
168 EXPORT_SYMBOL(iio_kfifo_free
);
170 static void devm_iio_kfifo_release(struct device
*dev
, void *res
)
172 iio_kfifo_free(*(struct iio_buffer
**)res
);
175 static int devm_iio_kfifo_match(struct device
*dev
, void *res
, void *data
)
177 struct iio_buffer
**r
= res
;
179 if (WARN_ON(!r
|| !*r
))
186 * devm_iio_fifo_allocate - Resource-managed iio_kfifo_allocate()
187 * @dev: Device to allocate kfifo buffer for
190 * Pointer to allocated iio_buffer on success, NULL on failure.
192 struct iio_buffer
*devm_iio_kfifo_allocate(struct device
*dev
)
194 struct iio_buffer
**ptr
, *r
;
196 ptr
= devres_alloc(devm_iio_kfifo_release
, sizeof(*ptr
), GFP_KERNEL
);
200 r
= iio_kfifo_allocate();
203 devres_add(dev
, ptr
);
210 EXPORT_SYMBOL(devm_iio_kfifo_allocate
);
213 * devm_iio_fifo_free - Resource-managed iio_kfifo_free()
214 * @dev: Device the buffer belongs to
215 * @r: The buffer associated with the device
217 void devm_iio_kfifo_free(struct device
*dev
, struct iio_buffer
*r
)
219 WARN_ON(devres_release(dev
, devm_iio_kfifo_release
,
220 devm_iio_kfifo_match
, r
));
222 EXPORT_SYMBOL(devm_iio_kfifo_free
);
224 MODULE_LICENSE("GPL");