blob: 8a6d28ce21b2f5102ac3ddae7e9f1ad43b50e85f [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 Cameronc559afb2012-06-30 13:52:00 +010025 return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
26 bytes_per_datum, GFP_KERNEL);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000027}
28
Jonathan Cameron14555b12011-09-21 11:15:57 +010029static int iio_request_update_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000030{
31 int ret = 0;
32 struct iio_kfifo *buf = iio_to_kfifo(r);
33
Jonathan Cameronb174baf2011-02-11 13:09:10 +000034 if (!buf->update_needed)
35 goto error_ret;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000036 kfifo_free(&buf->kf);
Jonathan Cameron14555b12011-09-21 11:15:57 +010037 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
38 buf->buffer.length);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000039error_ret:
Jonathan Cameronb174baf2011-02-11 13:09:10 +000040 return ret;
41}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000042
Jonathan Cameron14555b12011-09-21 11:15:57 +010043static int iio_get_length_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000044{
45 return r->length;
46}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000047
Jonathan Cameron14555b12011-09-21 11:15:57 +010048static IIO_BUFFER_ENABLE_ATTR;
Jonathan Cameron14555b12011-09-21 11:15:57 +010049static IIO_BUFFER_LENGTH_ATTR;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000050
51static struct attribute *iio_kfifo_attributes[] = {
52 &dev_attr_length.attr,
Jonathan Cameronb174baf2011-02-11 13:09:10 +000053 &dev_attr_enable.attr,
54 NULL,
55};
56
57static struct attribute_group iio_kfifo_attribute_group = {
58 .attrs = iio_kfifo_attributes,
Jonathan Cameron1aa04272011-08-30 12:32:47 +010059 .name = "buffer",
Jonathan Cameronb174baf2011-02-11 13:09:10 +000060};
61
Jonathan Cameron14555b12011-09-21 11:15:57 +010062static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000063{
64 return r->bytes_per_datum;
65}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000066
Jonathan Cameron14555b12011-09-21 11:15:57 +010067static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000068{
69 struct iio_kfifo *kf = iio_to_kfifo(r);
70 kf->update_needed = true;
71 return 0;
72}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000073
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010074static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
75{
76 if (r->bytes_per_datum != bpd) {
77 r->bytes_per_datum = bpd;
78 iio_mark_update_needed_kfifo(r);
79 }
80 return 0;
81}
82
Jonathan Cameron14555b12011-09-21 11:15:57 +010083static int iio_set_length_kfifo(struct iio_buffer *r, int length)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000084{
85 if (r->length != length) {
86 r->length = length;
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010087 iio_mark_update_needed_kfifo(r);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000088 }
89 return 0;
90}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000091
Jonathan Cameron14555b12011-09-21 11:15:57 +010092static int iio_store_to_kfifo(struct iio_buffer *r,
Jonathan Cameron5565a452011-05-18 14:42:24 +010093 u8 *data,
94 s64 timestamp)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000095{
96 int ret;
97 struct iio_kfifo *kf = iio_to_kfifo(r);
Jonathan Cameronc559afb2012-06-30 13:52:00 +010098 ret = kfifo_in(&kf->kf, data, 1);
99 if (ret != 1)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000100 return -EBUSY;
Jonathan Cameronc559afb2012-06-30 13:52:00 +0100101
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000102 return 0;
103}
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000104
Jonathan Cameron14555b12011-09-21 11:15:57 +0100105static int iio_read_first_n_kfifo(struct iio_buffer *r,
Jonathan Cameronb26a2182011-05-18 14:41:02 +0100106 size_t n, char __user *buf)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000107{
108 int ret, copied;
109 struct iio_kfifo *kf = iio_to_kfifo(r);
110
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +0100111 if (n < r->bytes_per_datum)
112 return -EINVAL;
113
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +0100114 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000115
116 return copied;
117}
Jonathan Cameron5565a452011-05-18 14:42:24 +0100118
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100119static const struct iio_buffer_access_funcs kfifo_access_funcs = {
Jonathan Cameron5565a452011-05-18 14:42:24 +0100120 .store_to = &iio_store_to_kfifo,
121 .read_first_n = &iio_read_first_n_kfifo,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100122 .request_update = &iio_request_update_kfifo,
123 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
124 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
125 .get_length = &iio_get_length_kfifo,
126 .set_length = &iio_set_length_kfifo,
127};
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100128
129struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
130{
131 struct iio_kfifo *kf;
132
133 kf = kzalloc(sizeof *kf, GFP_KERNEL);
134 if (!kf)
135 return NULL;
136 kf->update_needed = true;
137 iio_buffer_init(&kf->buffer);
138 kf->buffer.attrs = &iio_kfifo_attribute_group;
139 kf->buffer.access = &kfifo_access_funcs;
140
141 return &kf->buffer;
142}
143EXPORT_SYMBOL(iio_kfifo_allocate);
144
145void iio_kfifo_free(struct iio_buffer *r)
146{
147 kfree(iio_to_kfifo(r));
148}
149EXPORT_SYMBOL(iio_kfifo_free);
Jonathan Cameron5565a452011-05-18 14:42:24 +0100150
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000151MODULE_LICENSE("GPL");