blob: 7134e8ada09ac5a08cf3c6382533cb6eaa89afa6 [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_get_length_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000051{
52 return r->length;
53}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000054
Jonathan Cameron14555b12011-09-21 11:15:57 +010055static IIO_BUFFER_ENABLE_ATTR;
Jonathan Cameron14555b12011-09-21 11:15:57 +010056static IIO_BUFFER_LENGTH_ATTR;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000057
58static struct attribute *iio_kfifo_attributes[] = {
59 &dev_attr_length.attr,
Jonathan Cameronb174baf2011-02-11 13:09:10 +000060 &dev_attr_enable.attr,
61 NULL,
62};
63
64static struct attribute_group iio_kfifo_attribute_group = {
65 .attrs = iio_kfifo_attributes,
Jonathan Cameron1aa04272011-08-30 12:32:47 +010066 .name = "buffer",
Jonathan Cameronb174baf2011-02-11 13:09:10 +000067};
68
Jonathan Cameron14555b12011-09-21 11:15:57 +010069static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000070{
71 return r->bytes_per_datum;
72}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000073
Jonathan Cameron14555b12011-09-21 11:15:57 +010074static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000075{
76 struct iio_kfifo *kf = iio_to_kfifo(r);
77 kf->update_needed = true;
78 return 0;
79}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000080
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010081static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
82{
83 if (r->bytes_per_datum != bpd) {
84 r->bytes_per_datum = bpd;
85 iio_mark_update_needed_kfifo(r);
86 }
87 return 0;
88}
89
Jonathan Cameron14555b12011-09-21 11:15:57 +010090static int iio_set_length_kfifo(struct iio_buffer *r, int length)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000091{
Jonathan Cameron7c388ec2012-06-30 13:52:00 +010092 /* Avoid an invalid state */
93 if (length < 2)
94 length = 2;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000095 if (r->length != length) {
96 r->length = length;
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010097 iio_mark_update_needed_kfifo(r);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000098 }
99 return 0;
100}
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000101
Jonathan Cameron14555b12011-09-21 11:15:57 +0100102static int iio_store_to_kfifo(struct iio_buffer *r,
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100103 const void *data)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000104{
105 int ret;
106 struct iio_kfifo *kf = iio_to_kfifo(r);
Jonathan Cameronc559afb2012-06-30 13:52:00 +0100107 ret = kfifo_in(&kf->kf, data, 1);
108 if (ret != 1)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000109 return -EBUSY;
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000110
Lars-Peter Clausen623c3342013-09-15 16:31:00 +0100111 wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM);
Jonathan Cameronc559afb2012-06-30 13:52:00 +0100112
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000113 return 0;
114}
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000115
Jonathan Cameron14555b12011-09-21 11:15:57 +0100116static int iio_read_first_n_kfifo(struct iio_buffer *r,
Jonathan Cameronb26a2182011-05-18 14:41:02 +0100117 size_t n, char __user *buf)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000118{
119 int ret, copied;
120 struct iio_kfifo *kf = iio_to_kfifo(r);
121
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100122 if (mutex_lock_interruptible(&kf->user_lock))
123 return -ERESTARTSYS;
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +0100124
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100125 if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
126 ret = -EINVAL;
127 else
128 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100129 mutex_unlock(&kf->user_lock);
130 if (ret < 0)
131 return ret;
132
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000133 return copied;
134}
Jonathan Cameron5565a452011-05-18 14:42:24 +0100135
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000136static bool iio_kfifo_buf_data_available(struct iio_buffer *r)
137{
138 struct iio_kfifo *kf = iio_to_kfifo(r);
139 bool empty;
140
141 mutex_lock(&kf->user_lock);
142 empty = kfifo_is_empty(&kf->kf);
143 mutex_unlock(&kf->user_lock);
144
145 return !empty;
146}
147
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100148static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
149{
Lars-Peter Clausenf6c23f42013-10-15 09:30:00 +0100150 struct iio_kfifo *kf = iio_to_kfifo(buffer);
151
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100152 mutex_destroy(&kf->user_lock);
Lars-Peter Clausenf6c23f42013-10-15 09:30:00 +0100153 kfifo_free(&kf->kf);
154 kfree(kf);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100155}
156
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100157static const struct iio_buffer_access_funcs kfifo_access_funcs = {
Jonathan Cameron5565a452011-05-18 14:42:24 +0100158 .store_to = &iio_store_to_kfifo,
159 .read_first_n = &iio_read_first_n_kfifo,
Lars-Peter Clausen355c1a12013-11-25 14:56:00 +0000160 .data_available = iio_kfifo_buf_data_available,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100161 .request_update = &iio_request_update_kfifo,
162 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
163 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
164 .get_length = &iio_get_length_kfifo,
165 .set_length = &iio_set_length_kfifo,
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100166 .release = &iio_kfifo_buffer_release,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100167};
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100168
169struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
170{
171 struct iio_kfifo *kf;
172
173 kf = kzalloc(sizeof *kf, GFP_KERNEL);
174 if (!kf)
175 return NULL;
176 kf->update_needed = true;
177 iio_buffer_init(&kf->buffer);
178 kf->buffer.attrs = &iio_kfifo_attribute_group;
179 kf->buffer.access = &kfifo_access_funcs;
Jonathan Cameron7c388ec2012-06-30 13:52:00 +0100180 kf->buffer.length = 2;
Lars-Peter Clausen0894d80d2013-10-15 09:30:00 +0100181 mutex_init(&kf->user_lock);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100182 return &kf->buffer;
183}
184EXPORT_SYMBOL(iio_kfifo_allocate);
185
186void iio_kfifo_free(struct iio_buffer *r)
187{
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100188 iio_buffer_put(r);
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100189}
190EXPORT_SYMBOL(iio_kfifo_free);
Jonathan Cameron5565a452011-05-18 14:42:24 +0100191
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000192MODULE_LICENSE("GPL");