blob: a923c78d5cb48f40b2b661933b329f4ddb420c64 [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>
Jonathan Cameronb174baf2011-02-11 13:09:10 +000010
Jonathan Camerona710cc72011-08-30 12:32:43 +010011struct iio_kfifo {
Jonathan Cameron14555b12011-09-21 11:15:57 +010012 struct iio_buffer buffer;
Jonathan Camerona710cc72011-08-30 12:32:43 +010013 struct kfifo kf;
Jonathan Camerona710cc72011-08-30 12:32:43 +010014 int update_needed;
Jonathan Camerona710cc72011-08-30 12:32:43 +010015};
16
Jonathan Cameron14555b12011-09-21 11:15:57 +010017#define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
Jonathan Cameron5565a452011-05-18 14:42:24 +010018
Jonathan Cameronb174baf2011-02-11 13:09:10 +000019static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
20 int bytes_per_datum, int length)
21{
22 if ((length == 0) || (bytes_per_datum == 0))
23 return -EINVAL;
24
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 Cameron7c388ec2012-06-30 13:52:00 +010039 r->stufftoread = false;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000040error_ret:
Jonathan Cameronb174baf2011-02-11 13:09:10 +000041 return ret;
42}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000043
Jonathan Cameron14555b12011-09-21 11:15:57 +010044static int iio_get_length_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000045{
46 return r->length;
47}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000048
Jonathan Cameron14555b12011-09-21 11:15:57 +010049static IIO_BUFFER_ENABLE_ATTR;
Jonathan Cameron14555b12011-09-21 11:15:57 +010050static IIO_BUFFER_LENGTH_ATTR;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000051
52static struct attribute *iio_kfifo_attributes[] = {
53 &dev_attr_length.attr,
Jonathan Cameronb174baf2011-02-11 13:09:10 +000054 &dev_attr_enable.attr,
55 NULL,
56};
57
58static struct attribute_group iio_kfifo_attribute_group = {
59 .attrs = iio_kfifo_attributes,
Jonathan Cameron1aa04272011-08-30 12:32:47 +010060 .name = "buffer",
Jonathan Cameronb174baf2011-02-11 13:09:10 +000061};
62
Jonathan Cameron14555b12011-09-21 11:15:57 +010063static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000064{
65 return r->bytes_per_datum;
66}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000067
Jonathan Cameron14555b12011-09-21 11:15:57 +010068static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000069{
70 struct iio_kfifo *kf = iio_to_kfifo(r);
71 kf->update_needed = true;
72 return 0;
73}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000074
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010075static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
76{
77 if (r->bytes_per_datum != bpd) {
78 r->bytes_per_datum = bpd;
79 iio_mark_update_needed_kfifo(r);
80 }
81 return 0;
82}
83
Jonathan Cameron14555b12011-09-21 11:15:57 +010084static int iio_set_length_kfifo(struct iio_buffer *r, int length)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000085{
Jonathan Cameron7c388ec2012-06-30 13:52:00 +010086 /* Avoid an invalid state */
87 if (length < 2)
88 length = 2;
Jonathan Cameronb174baf2011-02-11 13:09:10 +000089 if (r->length != length) {
90 r->length = length;
Lars-Peter Clausen869871b2011-12-19 15:23:48 +010091 iio_mark_update_needed_kfifo(r);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000092 }
93 return 0;
94}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000095
Jonathan Cameron14555b12011-09-21 11:15:57 +010096static int iio_store_to_kfifo(struct iio_buffer *r,
Lars-Peter Clausence56ade2012-09-04 13:38:00 +010097 u8 *data)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000098{
99 int ret;
100 struct iio_kfifo *kf = iio_to_kfifo(r);
Jonathan Cameronc559afb2012-06-30 13:52:00 +0100101 ret = kfifo_in(&kf->kf, data, 1);
102 if (ret != 1)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000103 return -EBUSY;
Jonathan Cameron7c388ec2012-06-30 13:52:00 +0100104 r->stufftoread = true;
105 wake_up_interruptible(&r->pollq);
Jonathan Cameronc559afb2012-06-30 13:52:00 +0100106
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000107 return 0;
108}
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000109
Jonathan Cameron14555b12011-09-21 11:15:57 +0100110static int iio_read_first_n_kfifo(struct iio_buffer *r,
Jonathan Cameronb26a2182011-05-18 14:41:02 +0100111 size_t n, char __user *buf)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000112{
113 int ret, copied;
114 struct iio_kfifo *kf = iio_to_kfifo(r);
115
Jonathan Cameron08ce9b42012-08-27 18:40:04 +0100116 if (n < r->bytes_per_datum || r->bytes_per_datum == 0)
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +0100117 return -EINVAL;
118
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +0100119 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
Jonathan Cameron08ce9b42012-08-27 18:40:04 +0100120 if (ret < 0)
121 return ret;
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000122
Jonathan Cameron7c388ec2012-06-30 13:52:00 +0100123 if (kfifo_is_empty(&kf->kf))
124 r->stufftoread = false;
125 /* verify it is still empty to avoid race */
126 if (!kfifo_is_empty(&kf->kf))
127 r->stufftoread = true;
128
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000129 return copied;
130}
Jonathan Cameron5565a452011-05-18 14:42:24 +0100131
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100132static const struct iio_buffer_access_funcs kfifo_access_funcs = {
Jonathan Cameron5565a452011-05-18 14:42:24 +0100133 .store_to = &iio_store_to_kfifo,
134 .read_first_n = &iio_read_first_n_kfifo,
Jonathan Cameron5565a452011-05-18 14:42:24 +0100135 .request_update = &iio_request_update_kfifo,
136 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
137 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
138 .get_length = &iio_get_length_kfifo,
139 .set_length = &iio_set_length_kfifo,
140};
Lars-Peter Clausen7e632342012-01-03 11:02:51 +0100141
142struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
143{
144 struct iio_kfifo *kf;
145
146 kf = kzalloc(sizeof *kf, GFP_KERNEL);
147 if (!kf)
148 return NULL;
149 kf->update_needed = true;
150 iio_buffer_init(&kf->buffer);
151 kf->buffer.attrs = &iio_kfifo_attribute_group;
152 kf->buffer.access = &kfifo_access_funcs;
Jonathan Cameron7c388ec2012-06-30 13:52:00 +0100153 kf->buffer.length = 2;
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{
160 kfree(iio_to_kfifo(r));
161}
162EXPORT_SYMBOL(iio_kfifo_free);
Jonathan Cameron5565a452011-05-18 14:42:24 +0100163
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000164MODULE_LICENSE("GPL");