blob: 847ca561afe014e83707d04eaef02054f9b2bbfa [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);
Lars-Peter Clausencb6fbfa2013-10-15 09:30:00 +010041 buf->update_needed = false;
Lars-Peter Clausen5b78e512013-10-15 09:30:00 +010042 } else {
43 kfifo_reset_out(&buf->kf);
44 }
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +010045 mutex_unlock(&buf->user_lock);
Lars-Peter Clausen5b78e512013-10-15 09:30:00 +010046
Jonathan Cameronb174baf2011-02-11 13:09:10 +000047 return ret;
48}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000049
Jonathan Cameron14555b12011-09-21 11:15:57 +010050static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000051{
52 struct iio_kfifo *kf = iio_to_kfifo(r);
53 kf->update_needed = true;
54 return 0;
55}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000056
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010057static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
58{
59 if (r->bytes_per_datum != bpd) {
60 r->bytes_per_datum = bpd;
61 iio_mark_update_needed_kfifo(r);
62 }
63 return 0;
64}
65
Jonathan Cameron14555b12011-09-21 11:15:57 +010066static int iio_set_length_kfifo(struct iio_buffer *r, int length)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000067{
Jonathan Cameron7c388ec2012-06-30 13:52:00 +010068 /* Avoid an invalid state */
69 if (length < 2)
70 length = 2;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000071 if (r->length != length) {
72 r->length = length;
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010073 iio_mark_update_needed_kfifo(r);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000074 }
75 return 0;
76}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000077
Jonathan Cameron14555b12011-09-21 11:15:57 +010078static int iio_store_to_kfifo(struct iio_buffer *r,
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +010079 const void *data)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000080{
81 int ret;
82 struct iio_kfifo *kf = iio_to_kfifo(r);
Jonathan Cameronc559afb2012-06-30 13:52:00 +010083 ret = kfifo_in(&kf->kf, data, 1);
84 if (ret != 1)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000085 return -EBUSY;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000086 return 0;
87}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000088
Jonathan Cameron14555b12011-09-21 11:15:57 +010089static int iio_read_first_n_kfifo(struct iio_buffer *r,
Jonathan Cameronb26a2182011-05-18 14:41:02 +010090 size_t n, char __user *buf)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000091{
92 int ret, copied;
93 struct iio_kfifo *kf = iio_to_kfifo(r);
94
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +010095 if (mutex_lock_interruptible(&kf->user_lock))
96 return -ERESTARTSYS;
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +010097
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +010098 if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
99 ret = -EINVAL;
100 else
101 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100102 mutex_unlock(&kf->user_lock);
103 if (ret < 0)
104 return ret;
105
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000106 return copied;
107}
Jonathan Cameron5565a452011-05-18 14:42:24 +0100108
Josselin Costanzi37d34552015-03-22 20:33:38 +0200109static size_t iio_kfifo_buf_data_available(struct iio_buffer *r)
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000110{
111 struct iio_kfifo *kf = iio_to_kfifo(r);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200112 size_t samples;
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000113
114 mutex_lock(&kf->user_lock);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200115 samples = kfifo_len(&kf->kf);
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000116 mutex_unlock(&kf->user_lock);
117
Josselin Costanzi37d34552015-03-22 20:33:38 +0200118 return samples;
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000119}
120
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100121static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
122{
Lars-Peter Clausenf6c23f42013-10-15 09:30:00 +0100123 struct iio_kfifo *kf = iio_to_kfifo(buffer);
124
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100125 mutex_destroy(&kf->user_lock);
Lars-Peter Clausenf6c23f42013-10-15 09:30:00 +0100126 kfifo_free(&kf->kf);
127 kfree(kf);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100128}
129
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100130static const struct iio_buffer_access_funcs kfifo_access_funcs = {
Jonathan Cameron5565a452011-05-18 14:42:24 +0100131 .store_to = &iio_store_to_kfifo,
132 .read_first_n = &iio_read_first_n_kfifo,
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000133 .data_available = iio_kfifo_buf_data_available,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100134 .request_update = &iio_request_update_kfifo,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100135 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100136 .set_length = &iio_set_length_kfifo,
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100137 .release = &iio_kfifo_buffer_release,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100138};
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100139
Karol Wrona7ab374a2014-12-19 18:39:24 +0100140struct iio_buffer *iio_kfifo_allocate(void)
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100141{
142 struct iio_kfifo *kf;
143
Karol Wrona7ab374a2014-12-19 18:39:24 +0100144 kf = kzalloc(sizeof(*kf), GFP_KERNEL);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100145 if (!kf)
146 return NULL;
Karol Wrona7ab374a2014-12-19 18:39:24 +0100147
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100148 kf->update_needed = true;
149 iio_buffer_init(&kf->buffer);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100150 kf->buffer.access = &kfifo_access_funcs;
Jonathan Cameron7c388ec2012-06-30 13:52:00 +0100151 kf->buffer.length = 2;
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100152 mutex_init(&kf->user_lock);
Karol Wrona7ab374a2014-12-19 18:39:24 +0100153
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100154 return &kf->buffer;
155}
156EXPORT_SYMBOL(iio_kfifo_allocate);
157
158void iio_kfifo_free(struct iio_buffer *r)
159{
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100160 iio_buffer_put(r);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100161}
162EXPORT_SYMBOL(iio_kfifo_free);
Jonathan Cameron5565a452011-05-18 14:42:24 +0100163
Karol Wrona780103f2014-12-19 18:39:25 +0100164static void devm_iio_kfifo_release(struct device *dev, void *res)
165{
166 iio_kfifo_free(*(struct iio_buffer **)res);
167}
168
169static int devm_iio_kfifo_match(struct device *dev, void *res, void *data)
170{
171 struct iio_buffer **r = res;
172
173 if (WARN_ON(!r || !*r))
174 return 0;
175
176 return *r == data;
177}
178
179/**
180 * devm_iio_fifo_allocate - Resource-managed iio_kfifo_allocate()
181 * @dev: Device to allocate kfifo buffer for
182 *
183 * RETURNS:
184 * Pointer to allocated iio_buffer on success, NULL on failure.
185 */
186struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
187{
188 struct iio_buffer **ptr, *r;
189
190 ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
191 if (!ptr)
192 return NULL;
193
194 r = iio_kfifo_allocate();
195 if (r) {
196 *ptr = r;
197 devres_add(dev, ptr);
198 } else {
199 devres_free(ptr);
200 }
201
202 return r;
203}
204EXPORT_SYMBOL(devm_iio_kfifo_allocate);
205
206/**
207 * devm_iio_fifo_free - Resource-managed iio_kfifo_free()
208 * @dev: Device the buffer belongs to
209 * @r: The buffer associated with the device
210 */
211void devm_iio_kfifo_free(struct device *dev, struct iio_buffer *r)
212{
213 WARN_ON(devres_release(dev, devm_iio_kfifo_release,
214 devm_iio_kfifo_match, r));
215}
216EXPORT_SYMBOL(devm_iio_kfifo_free);
217
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000218MODULE_LICENSE("GPL");