blob: 95c6fc81c2c78f2d57598995753e342302c3cb3a [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 }
Jonathan Cameron7c388ec2012-06-30 13:52:00 +010045 r->stufftoread = false;
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_get_length_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000052{
53 return r->length;
54}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000055
Jonathan Cameron14555b12011-09-21 11:15:57 +010056static IIO_BUFFER_ENABLE_ATTR;
Jonathan Cameron14555b12011-09-21 11:15:57 +010057static IIO_BUFFER_LENGTH_ATTR;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000058
59static struct attribute *iio_kfifo_attributes[] = {
60 &dev_attr_length.attr,
Jonathan Cameronb174baf2011-02-11 13:09:10 +000061 &dev_attr_enable.attr,
62 NULL,
63};
64
65static struct attribute_group iio_kfifo_attribute_group = {
66 .attrs = iio_kfifo_attributes,
Jonathan Cameron1aa04272011-08-30 12:32:47 +010067 .name = "buffer",
Jonathan Cameronb174baf2011-02-11 13:09:10 +000068};
69
Jonathan Cameron14555b12011-09-21 11:15:57 +010070static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000071{
72 return r->bytes_per_datum;
73}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000074
Jonathan Cameron14555b12011-09-21 11:15:57 +010075static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000076{
77 struct iio_kfifo *kf = iio_to_kfifo(r);
78 kf->update_needed = true;
79 return 0;
80}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000081
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010082static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
83{
84 if (r->bytes_per_datum != bpd) {
85 r->bytes_per_datum = bpd;
86 iio_mark_update_needed_kfifo(r);
87 }
88 return 0;
89}
90
Jonathan Cameron14555b12011-09-21 11:15:57 +010091static int iio_set_length_kfifo(struct iio_buffer *r, int length)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000092{
Jonathan Cameron7c388ec2012-06-30 13:52:00 +010093 /* Avoid an invalid state */
94 if (length < 2)
95 length = 2;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000096 if (r->length != length) {
97 r->length = length;
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010098 iio_mark_update_needed_kfifo(r);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000099 }
100 return 0;
101}
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000102
Jonathan Cameron14555b12011-09-21 11:15:57 +0100103static int iio_store_to_kfifo(struct iio_buffer *r,
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100104 const void *data)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000105{
106 int ret;
107 struct iio_kfifo *kf = iio_to_kfifo(r);
Jonathan Cameronc559afb2012-06-30 13:52:00 +0100108 ret = kfifo_in(&kf->kf, data, 1);
109 if (ret != 1)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000110 return -EBUSY;
Jonathan Cameron7c388ec2012-06-30 13:52:00 +0100111 r->stufftoread = true;
Lars-Peter Clausen623c3342013-09-15 16:31:00 +0100112 wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM);
Jonathan Cameronc559afb2012-06-30 13:52:00 +0100113
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000114 return 0;
115}
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000116
Jonathan Cameron14555b12011-09-21 11:15:57 +0100117static int iio_read_first_n_kfifo(struct iio_buffer *r,
Jonathan Cameronb26a2182011-05-18 14:41:02 +0100118 size_t n, char __user *buf)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000119{
120 int ret, copied;
121 struct iio_kfifo *kf = iio_to_kfifo(r);
122
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100123 if (mutex_lock_interruptible(&kf->user_lock))
124 return -ERESTARTSYS;
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +0100125
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100126 if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
127 ret = -EINVAL;
128 else
129 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000130
Jonathan Cameron7c388ec2012-06-30 13:52:00 +0100131 if (kfifo_is_empty(&kf->kf))
132 r->stufftoread = false;
133 /* verify it is still empty to avoid race */
134 if (!kfifo_is_empty(&kf->kf))
135 r->stufftoread = true;
136
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100137 mutex_unlock(&kf->user_lock);
138 if (ret < 0)
139 return ret;
140
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000141 return copied;
142}
Jonathan Cameron5565a452011-05-18 14:42:24 +0100143
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100144static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
145{
Lars-Peter Clausenf6c23f42013-10-15 09:30:00 +0100146 struct iio_kfifo *kf = iio_to_kfifo(buffer);
147
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100148 mutex_destroy(&kf->user_lock);
Lars-Peter Clausenf6c23f42013-10-15 09:30:00 +0100149 kfifo_free(&kf->kf);
150 kfree(kf);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100151}
152
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100153static const struct iio_buffer_access_funcs kfifo_access_funcs = {
Jonathan Cameron5565a452011-05-18 14:42:24 +0100154 .store_to = &iio_store_to_kfifo,
155 .read_first_n = &iio_read_first_n_kfifo,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100156 .request_update = &iio_request_update_kfifo,
157 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
158 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
159 .get_length = &iio_get_length_kfifo,
160 .set_length = &iio_set_length_kfifo,
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100161 .release = &iio_kfifo_buffer_release,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100162};
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100163
164struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
165{
166 struct iio_kfifo *kf;
167
168 kf = kzalloc(sizeof *kf, GFP_KERNEL);
169 if (!kf)
170 return NULL;
171 kf->update_needed = true;
172 iio_buffer_init(&kf->buffer);
173 kf->buffer.attrs = &iio_kfifo_attribute_group;
174 kf->buffer.access = &kfifo_access_funcs;
Jonathan Cameron7c388ec2012-06-30 13:52:00 +0100175 kf->buffer.length = 2;
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100176 mutex_init(&kf->user_lock);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100177 return &kf->buffer;
178}
179EXPORT_SYMBOL(iio_kfifo_allocate);
180
181void iio_kfifo_free(struct iio_buffer *r)
182{
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100183 iio_buffer_put(r);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100184}
185EXPORT_SYMBOL(iio_kfifo_free);
Jonathan Cameron5565a452011-05-18 14:42:24 +0100186
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000187MODULE_LICENSE("GPL");