blob: 9f3bd59c0e72bd3a0919d3b6d49a27b8750e1caa [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 {
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 Cameron14555b12011-09-21 11:15:57 +010025 __iio_update_buffer(&buf->buffer, bytes_per_datum, length);
Jonathan Cameronb174baf2011-02-11 13:09:10 +000026 return kfifo_alloc(&buf->kf, bytes_per_datum*length, GFP_KERNEL);
27}
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 Cameronb174baf2011-02-11 13:09:10 +000098 ret = kfifo_in(&kf->kf, data, r->bytes_per_datum);
Jonathan Cameron4c3d1532011-10-26 17:27:40 +010099 if (ret != r->bytes_per_datum)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000100 return -EBUSY;
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000101 return 0;
102}
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000103
Jonathan Cameron14555b12011-09-21 11:15:57 +0100104static int iio_read_first_n_kfifo(struct iio_buffer *r,
Jonathan Cameronb26a2182011-05-18 14:41:02 +0100105 size_t n, char __user *buf)
Jonathan Cameronb174baf2011-02-11 13:09:10 +0000106{
107 int ret, copied;
108 struct iio_kfifo *kf = iio_to_kfifo(r);
109
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +0100110 if (n < r->bytes_per_datum)
111 return -EINVAL;
112
113 n = rounddown(n, r->bytes_per_datum);
114 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");