blob: 6bf9d05f48411105b68d15a600a617ddea39c47e [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 Cameronb174baf2011-02-11 13:09:10 +00009
Jonathan Camerona710cc72011-08-30 12:32:43 +010010struct iio_kfifo {
Jonathan Cameron14555b12011-09-21 11:15:57 +010011 struct iio_buffer buffer;
Jonathan Camerona710cc72011-08-30 12:32:43 +010012 struct kfifo kf;
Jonathan Camerona710cc72011-08-30 12:32:43 +010013 int update_needed;
Jonathan Camerona710cc72011-08-30 12:32:43 +010014};
15
Jonathan Cameron14555b12011-09-21 11:15:57 +010016#define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
Jonathan Cameron5565a452011-05-18 14:42:24 +010017
Jonathan Cameronb174baf2011-02-11 13:09:10 +000018static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
19 int bytes_per_datum, int length)
20{
21 if ((length == 0) || (bytes_per_datum == 0))
22 return -EINVAL;
23
Jonathan Cameron14555b12011-09-21 11:15:57 +010024 __iio_update_buffer(&buf->buffer, bytes_per_datum, length);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000025 return kfifo_alloc(&buf->kf, bytes_per_datum*length, GFP_KERNEL);
26}
27
Jonathan Cameron14555b12011-09-21 11:15:57 +010028static int iio_request_update_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000029{
30 int ret = 0;
31 struct iio_kfifo *buf = iio_to_kfifo(r);
32
Jonathan Cameronb174baf2011-02-11 13:09:10 +000033 if (!buf->update_needed)
34 goto error_ret;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000035 kfifo_free(&buf->kf);
Jonathan Cameron14555b12011-09-21 11:15:57 +010036 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
37 buf->buffer.length);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000038error_ret:
Jonathan Cameronb174baf2011-02-11 13:09:10 +000039 return ret;
40}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000041
Jonathan Cameron14555b12011-09-21 11:15:57 +010042static int iio_get_length_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000043{
44 return r->length;
45}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000046
Jonathan Cameron14555b12011-09-21 11:15:57 +010047static IIO_BUFFER_ENABLE_ATTR;
Jonathan Cameron14555b12011-09-21 11:15:57 +010048static IIO_BUFFER_LENGTH_ATTR;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000049
50static struct attribute *iio_kfifo_attributes[] = {
51 &dev_attr_length.attr,
Jonathan Cameronb174baf2011-02-11 13:09:10 +000052 &dev_attr_enable.attr,
53 NULL,
54};
55
56static struct attribute_group iio_kfifo_attribute_group = {
57 .attrs = iio_kfifo_attributes,
Jonathan Cameron1aa04272011-08-30 12:32:47 +010058 .name = "buffer",
Jonathan Cameronb174baf2011-02-11 13:09:10 +000059};
60
Jonathan Cameron14555b12011-09-21 11:15:57 +010061static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000062{
63 return r->bytes_per_datum;
64}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000065
Jonathan Cameron14555b12011-09-21 11:15:57 +010066static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000067{
68 struct iio_kfifo *kf = iio_to_kfifo(r);
69 kf->update_needed = true;
70 return 0;
71}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000072
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010073static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
74{
75 if (r->bytes_per_datum != bpd) {
76 r->bytes_per_datum = bpd;
77 iio_mark_update_needed_kfifo(r);
78 }
79 return 0;
80}
81
Jonathan Cameron14555b12011-09-21 11:15:57 +010082static int iio_set_length_kfifo(struct iio_buffer *r, int length)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000083{
84 if (r->length != length) {
85 r->length = length;
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010086 iio_mark_update_needed_kfifo(r);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000087 }
88 return 0;
89}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000090
Jonathan Cameron14555b12011-09-21 11:15:57 +010091static int iio_store_to_kfifo(struct iio_buffer *r,
Jonathan Cameron5565a452011-05-18 14:42:24 +010092 u8 *data,
93 s64 timestamp)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000094{
95 int ret;
96 struct iio_kfifo *kf = iio_to_kfifo(r);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000097 ret = kfifo_in(&kf->kf, data, r->bytes_per_datum);
Jonathan Cameron4c3d1532011-10-26 17:27:40 +010098 if (ret != r->bytes_per_datum)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000099 return -EBUSY;
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000100 return 0;
101}
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000102
Jonathan Cameron14555b12011-09-21 11:15:57 +0100103static int iio_read_first_n_kfifo(struct iio_buffer *r,
Jonathan Cameronb26a2182011-05-18 14:41:02 +0100104 size_t n, char __user *buf)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000105{
106 int ret, copied;
107 struct iio_kfifo *kf = iio_to_kfifo(r);
108
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +0100109 if (n < r->bytes_per_datum)
110 return -EINVAL;
111
112 n = rounddown(n, r->bytes_per_datum);
113 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000114
115 return copied;
116}
Jonathan Cameron5565a452011-05-18 14:42:24 +0100117
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100118static const struct iio_buffer_access_funcs kfifo_access_funcs = {
Jonathan Cameron5565a452011-05-18 14:42:24 +0100119 .store_to = &iio_store_to_kfifo,
120 .read_first_n = &iio_read_first_n_kfifo,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100121 .request_update = &iio_request_update_kfifo,
122 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
123 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
124 .get_length = &iio_get_length_kfifo,
125 .set_length = &iio_set_length_kfifo,
126};
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100127
128struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
129{
130 struct iio_kfifo *kf;
131
132 kf = kzalloc(sizeof *kf, GFP_KERNEL);
133 if (!kf)
134 return NULL;
135 kf->update_needed = true;
136 iio_buffer_init(&kf->buffer);
137 kf->buffer.attrs = &iio_kfifo_attribute_group;
138 kf->buffer.access = &kfifo_access_funcs;
139
140 return &kf->buffer;
141}
142EXPORT_SYMBOL(iio_kfifo_allocate);
143
144void iio_kfifo_free(struct iio_buffer *r)
145{
146 kfree(iio_to_kfifo(r));
147}
148EXPORT_SYMBOL(iio_kfifo_free);
Jonathan Cameron5565a452011-05-18 14:42:24 +0100149
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000150MODULE_LICENSE("GPL");