blob: 7ef9b13262a806779137e8930694c1496256b663 [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 Cameron06458e22012-04-25 15:54:58 +01008#include <linux/iio/kfifo_buf.h>
Jonathan Cameron7c388ec2012-06-30 13:52:00 +01009#include <linux/sched.h>
Lars-Peter Clausen623c3342013-09-15 16:31:00 +010010#include <linux/poll.h>
Jonathan Cameronb174baf2011-02-11 13:09:10 +000011
Jonathan Camerona710cc72011-08-30 12:32:43 +010012struct iio_kfifo {
Jonathan Cameron14555b12011-09-21 11:15:57 +010013 struct iio_buffer buffer;
Jonathan Camerona710cc72011-08-30 12:32:43 +010014 struct kfifo kf;
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +010015 struct mutex user_lock;
Jonathan Camerona710cc72011-08-30 12:32:43 +010016 int update_needed;
Jonathan Camerona710cc72011-08-30 12:32:43 +010017};
18
Jonathan Cameron14555b12011-09-21 11:15:57 +010019#define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
Jonathan Cameron5565a452011-05-18 14:42:24 +010020
Jonathan Cameronb174baf2011-02-11 13:09:10 +000021static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
22 int bytes_per_datum, int length)
23{
24 if ((length == 0) || (bytes_per_datum == 0))
25 return -EINVAL;
26
Martin Kelly8978f152018-03-26 14:27:52 -070027 /*
28 * Make sure we don't overflow an unsigned int after kfifo rounds up to
29 * the next power of 2.
30 */
31 if (roundup_pow_of_two(length) > UINT_MAX / bytes_per_datum)
32 return -EINVAL;
33
Jonathan Cameronc559afb2012-06-30 13:52:00 +010034 return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
35 bytes_per_datum, GFP_KERNEL);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000036}
37
Jonathan Cameron14555b12011-09-21 11:15:57 +010038static int iio_request_update_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000039{
40 int ret = 0;
41 struct iio_kfifo *buf = iio_to_kfifo(r);
42
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +010043 mutex_lock(&buf->user_lock);
Lars-Peter Clausen5b78e512013-10-15 09:30:00 +010044 if (buf->update_needed) {
45 kfifo_free(&buf->kf);
46 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
Jonathan Cameron14555b12011-09-21 11:15:57 +010047 buf->buffer.length);
Gabriele Mazzottae5f1efb2015-05-02 14:31:16 +020048 if (ret >= 0)
49 buf->update_needed = false;
Lars-Peter Clausen5b78e512013-10-15 09:30:00 +010050 } else {
51 kfifo_reset_out(&buf->kf);
52 }
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +010053 mutex_unlock(&buf->user_lock);
Lars-Peter Clausen5b78e512013-10-15 09:30:00 +010054
Jonathan Cameronb174baf2011-02-11 13:09:10 +000055 return ret;
56}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000057
Jonathan Cameron14555b12011-09-21 11:15:57 +010058static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000059{
60 struct iio_kfifo *kf = iio_to_kfifo(r);
61 kf->update_needed = true;
62 return 0;
63}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000064
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010065static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
66{
67 if (r->bytes_per_datum != bpd) {
68 r->bytes_per_datum = bpd;
69 iio_mark_update_needed_kfifo(r);
70 }
71 return 0;
72}
73
Jonathan Cameron14555b12011-09-21 11:15:57 +010074static int iio_set_length_kfifo(struct iio_buffer *r, int length)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000075{
Jonathan Cameron7c388ec2012-06-30 13:52:00 +010076 /* Avoid an invalid state */
77 if (length < 2)
78 length = 2;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000079 if (r->length != length) {
80 r->length = length;
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010081 iio_mark_update_needed_kfifo(r);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000082 }
83 return 0;
84}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000085
Jonathan Cameron14555b12011-09-21 11:15:57 +010086static int iio_store_to_kfifo(struct iio_buffer *r,
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +010087 const void *data)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000088{
89 int ret;
90 struct iio_kfifo *kf = iio_to_kfifo(r);
Jonathan Cameronc559afb2012-06-30 13:52:00 +010091 ret = kfifo_in(&kf->kf, data, 1);
92 if (ret != 1)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000093 return -EBUSY;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000094 return 0;
95}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000096
Jonathan Cameron14555b12011-09-21 11:15:57 +010097static int iio_read_first_n_kfifo(struct iio_buffer *r,
Jonathan Cameronb26a2182011-05-18 14:41:02 +010098 size_t n, char __user *buf)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000099{
100 int ret, copied;
101 struct iio_kfifo *kf = iio_to_kfifo(r);
102
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100103 if (mutex_lock_interruptible(&kf->user_lock))
104 return -ERESTARTSYS;
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +0100105
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100106 if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
107 ret = -EINVAL;
108 else
109 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100110 mutex_unlock(&kf->user_lock);
111 if (ret < 0)
112 return ret;
113
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000114 return copied;
115}
Jonathan Cameron5565a452011-05-18 14:42:24 +0100116
Josselin Costanzi37d34552015-03-22 20:33:38 +0200117static size_t iio_kfifo_buf_data_available(struct iio_buffer *r)
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000118{
119 struct iio_kfifo *kf = iio_to_kfifo(r);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200120 size_t samples;
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000121
122 mutex_lock(&kf->user_lock);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200123 samples = kfifo_len(&kf->kf);
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000124 mutex_unlock(&kf->user_lock);
125
Josselin Costanzi37d34552015-03-22 20:33:38 +0200126 return samples;
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000127}
128
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100129static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
130{
Lars-Peter Clausenf6c23f42013-10-15 09:30:00 +0100131 struct iio_kfifo *kf = iio_to_kfifo(buffer);
132
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100133 mutex_destroy(&kf->user_lock);
Lars-Peter Clausenf6c23f42013-10-15 09:30:00 +0100134 kfifo_free(&kf->kf);
135 kfree(kf);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100136}
137
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100138static const struct iio_buffer_access_funcs kfifo_access_funcs = {
Jonathan Cameron5565a452011-05-18 14:42:24 +0100139 .store_to = &iio_store_to_kfifo,
140 .read_first_n = &iio_read_first_n_kfifo,
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000141 .data_available = iio_kfifo_buf_data_available,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100142 .request_update = &iio_request_update_kfifo,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100143 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100144 .set_length = &iio_set_length_kfifo,
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100145 .release = &iio_kfifo_buffer_release,
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200146
147 .modes = INDIO_BUFFER_SOFTWARE | INDIO_BUFFER_TRIGGERED,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100148};
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100149
Karol Wrona7ab374a2014-12-19 18:39:24 +0100150struct iio_buffer *iio_kfifo_allocate(void)
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100151{
152 struct iio_kfifo *kf;
153
Karol Wrona7ab374a2014-12-19 18:39:24 +0100154 kf = kzalloc(sizeof(*kf), GFP_KERNEL);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100155 if (!kf)
156 return NULL;
Karol Wrona7ab374a2014-12-19 18:39:24 +0100157
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100158 kf->update_needed = true;
159 iio_buffer_init(&kf->buffer);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100160 kf->buffer.access = &kfifo_access_funcs;
Jonathan Cameron7c388ec2012-06-30 13:52:00 +0100161 kf->buffer.length = 2;
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100162 mutex_init(&kf->user_lock);
Karol Wrona7ab374a2014-12-19 18:39:24 +0100163
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100164 return &kf->buffer;
165}
166EXPORT_SYMBOL(iio_kfifo_allocate);
167
168void iio_kfifo_free(struct iio_buffer *r)
169{
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100170 iio_buffer_put(r);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100171}
172EXPORT_SYMBOL(iio_kfifo_free);
Jonathan Cameron5565a452011-05-18 14:42:24 +0100173
Karol Wrona780103f2014-12-19 18:39:25 +0100174static void devm_iio_kfifo_release(struct device *dev, void *res)
175{
176 iio_kfifo_free(*(struct iio_buffer **)res);
177}
178
179static int devm_iio_kfifo_match(struct device *dev, void *res, void *data)
180{
181 struct iio_buffer **r = res;
182
183 if (WARN_ON(!r || !*r))
184 return 0;
185
186 return *r == data;
187}
188
189/**
190 * devm_iio_fifo_allocate - Resource-managed iio_kfifo_allocate()
191 * @dev: Device to allocate kfifo buffer for
192 *
193 * RETURNS:
194 * Pointer to allocated iio_buffer on success, NULL on failure.
195 */
196struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
197{
198 struct iio_buffer **ptr, *r;
199
200 ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
201 if (!ptr)
202 return NULL;
203
204 r = iio_kfifo_allocate();
205 if (r) {
206 *ptr = r;
207 devres_add(dev, ptr);
208 } else {
209 devres_free(ptr);
210 }
211
212 return r;
213}
214EXPORT_SYMBOL(devm_iio_kfifo_allocate);
215
216/**
217 * devm_iio_fifo_free - Resource-managed iio_kfifo_free()
218 * @dev: Device the buffer belongs to
219 * @r: The buffer associated with the device
220 */
221void devm_iio_kfifo_free(struct device *dev, struct iio_buffer *r)
222{
223 WARN_ON(devres_release(dev, devm_iio_kfifo_release,
224 devm_iio_kfifo_match, r));
225}
226EXPORT_SYMBOL(devm_iio_kfifo_free);
227
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000228MODULE_LICENSE("GPL");