blob: 3c9516b2a670727de8efa9be762fa00096c8b9f2 [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>
8
9#include "kfifo_buf.h"
10
Jonathan Camerona710cc72011-08-30 12:32:43 +010011struct iio_kfifo {
12 struct iio_ring_buffer ring;
13 struct kfifo kf;
14 int use_count;
15 int update_needed;
16 struct mutex use_lock;
17};
18
Jonathan Cameron5565a452011-05-18 14:42:24 +010019#define iio_to_kfifo(r) container_of(r, struct iio_kfifo, ring)
20
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
27 __iio_update_ring_buffer(&buf->ring, bytes_per_datum, length);
28 return kfifo_alloc(&buf->kf, bytes_per_datum*length, GFP_KERNEL);
29}
30
Jonathan Cameron5565a452011-05-18 14:42:24 +010031static int iio_request_update_kfifo(struct iio_ring_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000032{
33 int ret = 0;
34 struct iio_kfifo *buf = iio_to_kfifo(r);
35
36 mutex_lock(&buf->use_lock);
37 if (!buf->update_needed)
38 goto error_ret;
39 if (buf->use_count) {
40 ret = -EAGAIN;
41 goto error_ret;
42 }
43 kfifo_free(&buf->kf);
44 ret = __iio_allocate_kfifo(buf, buf->ring.bytes_per_datum,
45 buf->ring.length);
46error_ret:
47 mutex_unlock(&buf->use_lock);
48 return ret;
49}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000050
Jonathan Cameron5565a452011-05-18 14:42:24 +010051static void iio_mark_kfifo_in_use(struct iio_ring_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000052{
53 struct iio_kfifo *buf = iio_to_kfifo(r);
54 mutex_lock(&buf->use_lock);
55 buf->use_count++;
56 mutex_unlock(&buf->use_lock);
57}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000058
Jonathan Cameron5565a452011-05-18 14:42:24 +010059static void iio_unmark_kfifo_in_use(struct iio_ring_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000060{
61 struct iio_kfifo *buf = iio_to_kfifo(r);
62 mutex_lock(&buf->use_lock);
63 buf->use_count--;
64 mutex_unlock(&buf->use_lock);
65}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000066
Jonathan Cameron5565a452011-05-18 14:42:24 +010067static int iio_get_length_kfifo(struct iio_ring_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +000068{
69 return r->length;
70}
Jonathan Cameronb174baf2011-02-11 13:09:10 +000071
72static inline void __iio_init_kfifo(struct iio_kfifo *kf)
73{
74 mutex_init(&kf->use_lock);
75}
76
77static IIO_RING_ENABLE_ATTR;
78static IIO_RING_BYTES_PER_DATUM_ATTR;
79static IIO_RING_LENGTH_ATTR;
80
81static struct attribute *iio_kfifo_attributes[] = {
82 &dev_attr_length.attr,
83 &dev_attr_bytes_per_datum.attr,
84 &dev_attr_enable.attr,
85 NULL,
86};
87
88static struct attribute_group iio_kfifo_attribute_group = {
89 .attrs = iio_kfifo_attributes,
90};
91
92static const struct attribute_group *iio_kfifo_attribute_groups[] = {
93 &iio_kfifo_attribute_group,
94 NULL
95};
96
97static void iio_kfifo_release(struct device *dev)
98{
99 struct iio_ring_buffer *r = to_iio_ring_buffer(dev);
100 struct iio_kfifo *kf = iio_to_kfifo(r);
101 kfifo_free(&kf->kf);
102 kfree(kf);
103}
104
105static struct device_type iio_kfifo_type = {
106 .release = iio_kfifo_release,
107 .groups = iio_kfifo_attribute_groups,
108};
109
110struct iio_ring_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
111{
112 struct iio_kfifo *kf;
113
114 kf = kzalloc(sizeof *kf, GFP_KERNEL);
115 if (!kf)
116 return NULL;
Jonathan Cameron5565a452011-05-18 14:42:24 +0100117 kf->update_needed = true;
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000118 iio_ring_buffer_init(&kf->ring, indio_dev);
119 __iio_init_kfifo(kf);
120 kf->ring.dev.type = &iio_kfifo_type;
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000121 kf->ring.dev.parent = &indio_dev->dev;
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000122 dev_set_drvdata(&kf->ring.dev, (void *)&(kf->ring));
123
124 return &kf->ring;
125}
126EXPORT_SYMBOL(iio_kfifo_allocate);
127
Jonathan Cameron5565a452011-05-18 14:42:24 +0100128static int iio_get_bytes_per_datum_kfifo(struct iio_ring_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000129{
130 return r->bytes_per_datum;
131}
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000132
Jonathan Cameron5565a452011-05-18 14:42:24 +0100133static int iio_set_bytes_per_datum_kfifo(struct iio_ring_buffer *r, size_t bpd)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000134{
135 if (r->bytes_per_datum != bpd) {
136 r->bytes_per_datum = bpd;
Jonathan Cameron5565a452011-05-18 14:42:24 +0100137 if (r->access->mark_param_change)
138 r->access->mark_param_change(r);
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000139 }
140 return 0;
141}
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000142
Jonathan Cameron5565a452011-05-18 14:42:24 +0100143static int iio_mark_update_needed_kfifo(struct iio_ring_buffer *r)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000144{
145 struct iio_kfifo *kf = iio_to_kfifo(r);
146 kf->update_needed = true;
147 return 0;
148}
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000149
Jonathan Cameron5565a452011-05-18 14:42:24 +0100150static int iio_set_length_kfifo(struct iio_ring_buffer *r, int length)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000151{
152 if (r->length != length) {
153 r->length = length;
Jonathan Cameron5565a452011-05-18 14:42:24 +0100154 if (r->access->mark_param_change)
155 r->access->mark_param_change(r);
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000156 }
157 return 0;
158}
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000159
160void iio_kfifo_free(struct iio_ring_buffer *r)
161{
162 if (r)
163 iio_put_ring_buffer(r);
164}
165EXPORT_SYMBOL(iio_kfifo_free);
166
Jonathan Cameron5565a452011-05-18 14:42:24 +0100167static int iio_store_to_kfifo(struct iio_ring_buffer *r,
168 u8 *data,
169 s64 timestamp)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000170{
171 int ret;
172 struct iio_kfifo *kf = iio_to_kfifo(r);
173 u8 *datal = kmalloc(r->bytes_per_datum, GFP_KERNEL);
174 memcpy(datal, data, r->bytes_per_datum - sizeof(timestamp));
175 memcpy(datal + r->bytes_per_datum - sizeof(timestamp),
176 &timestamp, sizeof(timestamp));
177 ret = kfifo_in(&kf->kf, data, r->bytes_per_datum);
178 if (ret != r->bytes_per_datum) {
179 kfree(datal);
180 return -EBUSY;
181 }
182 kfree(datal);
183 return 0;
184}
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000185
Jonathan Cameron5565a452011-05-18 14:42:24 +0100186static int iio_read_first_n_kfifo(struct iio_ring_buffer *r,
Jonathan Cameronb26a2182011-05-18 14:41:02 +0100187 size_t n, char __user *buf)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000188{
189 int ret, copied;
190 struct iio_kfifo *kf = iio_to_kfifo(r);
191
Jonathan Cameronb4281732011-04-15 18:55:55 +0100192 ret = kfifo_to_user(&kf->kf, buf, r->bytes_per_datum*n, &copied);
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000193
194 return copied;
195}
Jonathan Cameron5565a452011-05-18 14:42:24 +0100196
197const struct iio_ring_access_funcs kfifo_access_funcs = {
198 .mark_in_use = &iio_mark_kfifo_in_use,
199 .unmark_in_use = &iio_unmark_kfifo_in_use,
200 .store_to = &iio_store_to_kfifo,
201 .read_first_n = &iio_read_first_n_kfifo,
202 .mark_param_change = &iio_mark_update_needed_kfifo,
203 .request_update = &iio_request_update_kfifo,
204 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
205 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
206 .get_length = &iio_get_length_kfifo,
207 .set_length = &iio_set_length_kfifo,
208};
209EXPORT_SYMBOL(kfifo_access_funcs);
210
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000211MODULE_LICENSE("GPL");