blob: 047fe757ab97d6075bc920511a187dde78450b75 [file] [log] [blame]
Jonathan Cameronb174baf2011-02-11 13:09:10 +00001#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>
Jonathan Cameron19a5a8a2017-01-02 19:28:31 +00008#include <linux/iio/iio.h>
Jonathan Cameron8abd5ba2017-01-02 19:28:30 +00009#include <linux/iio/buffer.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010010#include <linux/iio/kfifo_buf.h>
Jonathan Cameron33dd94c2017-01-02 19:28:34 +000011#include <linux/iio/buffer_impl.h>
Jonathan Cameron7c388ec2012-06-30 13:52:00 +010012#include <linux/sched.h>
Lars-Peter Clausen623c3342013-09-15 16:31:00 +010013#include <linux/poll.h>
Jonathan Cameronb174baf2011-02-11 13:09:10 +000014
Jonathan Camerona710cc72011-08-30 12:32:43 +010015struct iio_kfifo {
Jonathan Cameron14555b12011-09-21 11:15:57 +010016 struct iio_buffer buffer;
Jonathan Camerona710cc72011-08-30 12:32:43 +010017 struct kfifo kf;
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +010018 struct mutex user_lock;
Jonathan Camerona710cc72011-08-30 12:32:43 +010019 int update_needed;
Jonathan Camerona710cc72011-08-30 12:32:43 +010020};
21
Jonathan Cameron14555b12011-09-21 11:15:57 +010022#define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
Jonathan Cameron5565a452011-05-18 14:42:24 +010023
Jonathan Cameronb174baf2011-02-11 13:09:10 +000024static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
25 int bytes_per_datum, int length)
26{
27 if ((length == 0) || (bytes_per_datum == 0))
28 return -EINVAL;
29
Jonathan Cameronc559afb2012-06-30 13:52:00 +010030 return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
31 bytes_per_datum, GFP_KERNEL);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000032}
33
Jonathan Cameron14555b12011-09-21 11:15:57 +010034static int iio_request_update_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000035{
36 int ret = 0;
37 struct iio_kfifo *buf = iio_to_kfifo(r);
38
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +010039 mutex_lock(&buf->user_lock);
Lars-Peter Clausen5b78e512013-10-15 09:30:00 +010040 if (buf->update_needed) {
41 kfifo_free(&buf->kf);
42 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
Jonathan Cameron14555b12011-09-21 11:15:57 +010043 buf->buffer.length);
Gabriele Mazzottae5f1efb2015-05-02 14:31:16 +020044 if (ret >= 0)
45 buf->update_needed = false;
Lars-Peter Clausen5b78e512013-10-15 09:30:00 +010046 } else {
47 kfifo_reset_out(&buf->kf);
48 }
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +010049 mutex_unlock(&buf->user_lock);
Lars-Peter Clausen5b78e512013-10-15 09:30:00 +010050
Jonathan Cameronb174baf2011-02-11 13:09:10 +000051 return ret;
52}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000053
Jonathan Cameron14555b12011-09-21 11:15:57 +010054static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000055{
56 struct iio_kfifo *kf = iio_to_kfifo(r);
57 kf->update_needed = true;
58 return 0;
59}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000060
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010061static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
62{
63 if (r->bytes_per_datum != bpd) {
64 r->bytes_per_datum = bpd;
65 iio_mark_update_needed_kfifo(r);
66 }
67 return 0;
68}
69
Jonathan Cameron14555b12011-09-21 11:15:57 +010070static int iio_set_length_kfifo(struct iio_buffer *r, int length)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000071{
Jonathan Cameron7c388ec2012-06-30 13:52:00 +010072 /* Avoid an invalid state */
73 if (length < 2)
74 length = 2;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000075 if (r->length != length) {
76 r->length = length;
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010077 iio_mark_update_needed_kfifo(r);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000078 }
79 return 0;
80}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000081
Jonathan Cameron14555b12011-09-21 11:15:57 +010082static int iio_store_to_kfifo(struct iio_buffer *r,
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +010083 const void *data)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000084{
85 int ret;
86 struct iio_kfifo *kf = iio_to_kfifo(r);
Jonathan Cameronc559afb2012-06-30 13:52:00 +010087 ret = kfifo_in(&kf->kf, data, 1);
88 if (ret != 1)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000089 return -EBUSY;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000090 return 0;
91}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000092
Jonathan Cameron14555b12011-09-21 11:15:57 +010093static int iio_read_first_n_kfifo(struct iio_buffer *r,
Jonathan Cameronb26a2182011-05-18 14:41:02 +010094 size_t n, char __user *buf)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000095{
96 int ret, copied;
97 struct iio_kfifo *kf = iio_to_kfifo(r);
98
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +010099 if (mutex_lock_interruptible(&kf->user_lock))
100 return -ERESTARTSYS;
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +0100101
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100102 if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
103 ret = -EINVAL;
104 else
105 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100106 mutex_unlock(&kf->user_lock);
107 if (ret < 0)
108 return ret;
109
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000110 return copied;
111}
Jonathan Cameron5565a452011-05-18 14:42:24 +0100112
Josselin Costanzi37d34552015-03-22 20:33:38 +0200113static size_t iio_kfifo_buf_data_available(struct iio_buffer *r)
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000114{
115 struct iio_kfifo *kf = iio_to_kfifo(r);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200116 size_t samples;
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000117
118 mutex_lock(&kf->user_lock);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200119 samples = kfifo_len(&kf->kf);
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000120 mutex_unlock(&kf->user_lock);
121
Josselin Costanzi37d34552015-03-22 20:33:38 +0200122 return samples;
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000123}
124
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100125static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
126{
Lars-Peter Clausenf6c23f42013-10-15 09:30:00 +0100127 struct iio_kfifo *kf = iio_to_kfifo(buffer);
128
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100129 mutex_destroy(&kf->user_lock);
Lars-Peter Clausenf6c23f42013-10-15 09:30:00 +0100130 kfifo_free(&kf->kf);
131 kfree(kf);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100132}
133
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100134static const struct iio_buffer_access_funcs kfifo_access_funcs = {
Jonathan Cameron5565a452011-05-18 14:42:24 +0100135 .store_to = &iio_store_to_kfifo,
136 .read_first_n = &iio_read_first_n_kfifo,
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000137 .data_available = iio_kfifo_buf_data_available,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100138 .request_update = &iio_request_update_kfifo,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100139 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100140 .set_length = &iio_set_length_kfifo,
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100141 .release = &iio_kfifo_buffer_release,
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200142
143 .modes = INDIO_BUFFER_SOFTWARE | INDIO_BUFFER_TRIGGERED,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100144};
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100145
Karol Wrona7ab374a2014-12-19 18:39:24 +0100146struct iio_buffer *iio_kfifo_allocate(void)
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100147{
148 struct iio_kfifo *kf;
149
Karol Wrona7ab374a2014-12-19 18:39:24 +0100150 kf = kzalloc(sizeof(*kf), GFP_KERNEL);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100151 if (!kf)
152 return NULL;
Karol Wrona7ab374a2014-12-19 18:39:24 +0100153
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100154 kf->update_needed = true;
155 iio_buffer_init(&kf->buffer);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100156 kf->buffer.access = &kfifo_access_funcs;
Jonathan Cameron7c388ec2012-06-30 13:52:00 +0100157 kf->buffer.length = 2;
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100158 mutex_init(&kf->user_lock);
Karol Wrona7ab374a2014-12-19 18:39:24 +0100159
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100160 return &kf->buffer;
161}
162EXPORT_SYMBOL(iio_kfifo_allocate);
163
164void iio_kfifo_free(struct iio_buffer *r)
165{
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100166 iio_buffer_put(r);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100167}
168EXPORT_SYMBOL(iio_kfifo_free);
Jonathan Cameron5565a452011-05-18 14:42:24 +0100169
Karol Wrona780103f2014-12-19 18:39:25 +0100170static void devm_iio_kfifo_release(struct device *dev, void *res)
171{
172 iio_kfifo_free(*(struct iio_buffer **)res);
173}
174
175static int devm_iio_kfifo_match(struct device *dev, void *res, void *data)
176{
177 struct iio_buffer **r = res;
178
179 if (WARN_ON(!r || !*r))
180 return 0;
181
182 return *r == data;
183}
184
185/**
186 * devm_iio_fifo_allocate - Resource-managed iio_kfifo_allocate()
187 * @dev: Device to allocate kfifo buffer for
188 *
189 * RETURNS:
190 * Pointer to allocated iio_buffer on success, NULL on failure.
191 */
192struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
193{
194 struct iio_buffer **ptr, *r;
195
196 ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
197 if (!ptr)
198 return NULL;
199
200 r = iio_kfifo_allocate();
201 if (r) {
202 *ptr = r;
203 devres_add(dev, ptr);
204 } else {
205 devres_free(ptr);
206 }
207
208 return r;
209}
210EXPORT_SYMBOL(devm_iio_kfifo_allocate);
211
212/**
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
216 */
217void devm_iio_kfifo_free(struct device *dev, struct iio_buffer *r)
218{
219 WARN_ON(devres_release(dev, devm_iio_kfifo_release,
220 devm_iio_kfifo_match, r));
221}
222EXPORT_SYMBOL(devm_iio_kfifo_free);
223
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000224MODULE_LICENSE("GPL");