blob: c5b999f0c51943f4ba79c066702cad96536310d4 [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
Jonathan Cameronc559afb2012-06-30 13:52:00 +010027 return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
28 bytes_per_datum, GFP_KERNEL);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000029}
30
Jonathan Cameron14555b12011-09-21 11:15:57 +010031static int iio_request_update_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000032{
33 int ret = 0;
34 struct iio_kfifo *buf = iio_to_kfifo(r);
35
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +010036 mutex_lock(&buf->user_lock);
Lars-Peter Clausen5b78e512013-10-15 09:30:00 +010037 if (buf->update_needed) {
38 kfifo_free(&buf->kf);
39 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
Jonathan Cameron14555b12011-09-21 11:15:57 +010040 buf->buffer.length);
Gabriele Mazzottae5f1efb2015-05-02 14:31:16 +020041 if (ret >= 0)
42 buf->update_needed = false;
Lars-Peter Clausen5b78e512013-10-15 09:30:00 +010043 } else {
44 kfifo_reset_out(&buf->kf);
45 }
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +010046 mutex_unlock(&buf->user_lock);
Lars-Peter Clausen5b78e512013-10-15 09:30:00 +010047
Jonathan Cameronb174baf2011-02-11 13:09:10 +000048 return ret;
49}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000050
Jonathan Cameron14555b12011-09-21 11:15:57 +010051static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000052{
53 struct iio_kfifo *kf = iio_to_kfifo(r);
54 kf->update_needed = true;
55 return 0;
56}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000057
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010058static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
59{
60 if (r->bytes_per_datum != bpd) {
61 r->bytes_per_datum = bpd;
62 iio_mark_update_needed_kfifo(r);
63 }
64 return 0;
65}
66
Jonathan Cameron14555b12011-09-21 11:15:57 +010067static int iio_set_length_kfifo(struct iio_buffer *r, int length)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000068{
Jonathan Cameron7c388ec2012-06-30 13:52:00 +010069 /* Avoid an invalid state */
70 if (length < 2)
71 length = 2;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000072 if (r->length != length) {
73 r->length = length;
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010074 iio_mark_update_needed_kfifo(r);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000075 }
76 return 0;
77}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000078
Jonathan Cameron14555b12011-09-21 11:15:57 +010079static int iio_store_to_kfifo(struct iio_buffer *r,
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +010080 const void *data)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000081{
82 int ret;
83 struct iio_kfifo *kf = iio_to_kfifo(r);
Jonathan Cameronc559afb2012-06-30 13:52:00 +010084 ret = kfifo_in(&kf->kf, data, 1);
85 if (ret != 1)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000086 return -EBUSY;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000087 return 0;
88}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000089
Jonathan Cameron14555b12011-09-21 11:15:57 +010090static int iio_read_first_n_kfifo(struct iio_buffer *r,
Jonathan Cameronb26a2182011-05-18 14:41:02 +010091 size_t n, char __user *buf)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000092{
93 int ret, copied;
94 struct iio_kfifo *kf = iio_to_kfifo(r);
95
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +010096 if (mutex_lock_interruptible(&kf->user_lock))
97 return -ERESTARTSYS;
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +010098
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +010099 if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
100 ret = -EINVAL;
101 else
102 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100103 mutex_unlock(&kf->user_lock);
104 if (ret < 0)
105 return ret;
106
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000107 return copied;
108}
Jonathan Cameron5565a452011-05-18 14:42:24 +0100109
Josselin Costanzi37d34552015-03-22 20:33:38 +0200110static size_t iio_kfifo_buf_data_available(struct iio_buffer *r)
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000111{
112 struct iio_kfifo *kf = iio_to_kfifo(r);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200113 size_t samples;
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000114
115 mutex_lock(&kf->user_lock);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200116 samples = kfifo_len(&kf->kf);
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000117 mutex_unlock(&kf->user_lock);
118
Josselin Costanzi37d34552015-03-22 20:33:38 +0200119 return samples;
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000120}
121
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100122static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
123{
Lars-Peter Clausenf6c23f42013-10-15 09:30:00 +0100124 struct iio_kfifo *kf = iio_to_kfifo(buffer);
125
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100126 mutex_destroy(&kf->user_lock);
Lars-Peter Clausenf6c23f42013-10-15 09:30:00 +0100127 kfifo_free(&kf->kf);
128 kfree(kf);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100129}
130
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100131static const struct iio_buffer_access_funcs kfifo_access_funcs = {
Jonathan Cameron5565a452011-05-18 14:42:24 +0100132 .store_to = &iio_store_to_kfifo,
133 .read_first_n = &iio_read_first_n_kfifo,
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000134 .data_available = iio_kfifo_buf_data_available,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100135 .request_update = &iio_request_update_kfifo,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100136 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100137 .set_length = &iio_set_length_kfifo,
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100138 .release = &iio_kfifo_buffer_release,
Lars-Peter Clausen225d59a2015-05-29 18:14:21 +0200139
140 .modes = INDIO_BUFFER_SOFTWARE | INDIO_BUFFER_TRIGGERED,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100141};
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100142
Karol Wrona7ab374a2014-12-19 18:39:24 +0100143struct iio_buffer *iio_kfifo_allocate(void)
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100144{
145 struct iio_kfifo *kf;
146
Karol Wrona7ab374a2014-12-19 18:39:24 +0100147 kf = kzalloc(sizeof(*kf), GFP_KERNEL);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100148 if (!kf)
149 return NULL;
Karol Wrona7ab374a2014-12-19 18:39:24 +0100150
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100151 kf->update_needed = true;
152 iio_buffer_init(&kf->buffer);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100153 kf->buffer.access = &kfifo_access_funcs;
Jonathan Cameron7c388ec2012-06-30 13:52:00 +0100154 kf->buffer.length = 2;
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100155 mutex_init(&kf->user_lock);
Karol Wrona7ab374a2014-12-19 18:39:24 +0100156
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100157 return &kf->buffer;
158}
159EXPORT_SYMBOL(iio_kfifo_allocate);
160
161void iio_kfifo_free(struct iio_buffer *r)
162{
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100163 iio_buffer_put(r);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100164}
165EXPORT_SYMBOL(iio_kfifo_free);
Jonathan Cameron5565a452011-05-18 14:42:24 +0100166
Karol Wrona780103f2014-12-19 18:39:25 +0100167static void devm_iio_kfifo_release(struct device *dev, void *res)
168{
169 iio_kfifo_free(*(struct iio_buffer **)res);
170}
171
172static int devm_iio_kfifo_match(struct device *dev, void *res, void *data)
173{
174 struct iio_buffer **r = res;
175
176 if (WARN_ON(!r || !*r))
177 return 0;
178
179 return *r == data;
180}
181
182/**
183 * devm_iio_fifo_allocate - Resource-managed iio_kfifo_allocate()
184 * @dev: Device to allocate kfifo buffer for
185 *
186 * RETURNS:
187 * Pointer to allocated iio_buffer on success, NULL on failure.
188 */
189struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
190{
191 struct iio_buffer **ptr, *r;
192
193 ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
194 if (!ptr)
195 return NULL;
196
197 r = iio_kfifo_allocate();
198 if (r) {
199 *ptr = r;
200 devres_add(dev, ptr);
201 } else {
202 devres_free(ptr);
203 }
204
205 return r;
206}
207EXPORT_SYMBOL(devm_iio_kfifo_allocate);
208
209/**
210 * devm_iio_fifo_free - Resource-managed iio_kfifo_free()
211 * @dev: Device the buffer belongs to
212 * @r: The buffer associated with the device
213 */
214void devm_iio_kfifo_free(struct device *dev, struct iio_buffer *r)
215{
216 WARN_ON(devres_release(dev, devm_iio_kfifo_release,
217 devm_iio_kfifo_match, r));
218}
219EXPORT_SYMBOL(devm_iio_kfifo_free);
220
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000221MODULE_LICENSE("GPL");