iio: adis16400: Use != channel indices for the two voltage channels
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / iio / kfifo_buf.c
CommitLineData
b174baf4
JC
1#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>
06458e27 8#include <linux/iio/kfifo_buf.h>
7c388ec1 9#include <linux/sched.h>
b174baf4 10
a710cc77 11struct iio_kfifo {
14555b14 12 struct iio_buffer buffer;
a710cc77 13 struct kfifo kf;
a710cc77 14 int update_needed;
a710cc77
JC
15};
16
14555b14 17#define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
5565a450 18
b174baf4
JC
19static 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
c559afbf
JC
25 return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
26 bytes_per_datum, GFP_KERNEL);
b174baf4
JC
27}
28
14555b14 29static int iio_request_update_kfifo(struct iio_buffer *r)
b174baf4
JC
30{
31 int ret = 0;
32 struct iio_kfifo *buf = iio_to_kfifo(r);
33
b174baf4
JC
34 if (!buf->update_needed)
35 goto error_ret;
b174baf4 36 kfifo_free(&buf->kf);
14555b14
JC
37 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
38 buf->buffer.length);
7c388ec1 39 r->stufftoread = false;
b174baf4 40error_ret:
b174baf4
JC
41 return ret;
42}
b174baf4 43
14555b14 44static int iio_get_length_kfifo(struct iio_buffer *r)
b174baf4
JC
45{
46 return r->length;
47}
b174baf4 48
14555b14 49static IIO_BUFFER_ENABLE_ATTR;
14555b14 50static IIO_BUFFER_LENGTH_ATTR;
b174baf4
JC
51
52static struct attribute *iio_kfifo_attributes[] = {
53 &dev_attr_length.attr,
b174baf4
JC
54 &dev_attr_enable.attr,
55 NULL,
56};
57
58static struct attribute_group iio_kfifo_attribute_group = {
59 .attrs = iio_kfifo_attributes,
1aa04278 60 .name = "buffer",
b174baf4
JC
61};
62
14555b14 63static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
b174baf4
JC
64{
65 return r->bytes_per_datum;
66}
b174baf4 67
869871b5 68static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
b174baf4 69{
869871b5
LPC
70 struct iio_kfifo *kf = iio_to_kfifo(r);
71 kf->update_needed = true;
b174baf4
JC
72 return 0;
73}
b174baf4 74
869871b5 75static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
b174baf4 76{
869871b5
LPC
77 if (r->bytes_per_datum != bpd) {
78 r->bytes_per_datum = bpd;
79 iio_mark_update_needed_kfifo(r);
80 }
b174baf4
JC
81 return 0;
82}
b174baf4 83
14555b14 84static int iio_set_length_kfifo(struct iio_buffer *r, int length)
b174baf4 85{
7c388ec1
JC
86 /* Avoid an invalid state */
87 if (length < 2)
88 length = 2;
b174baf4
JC
89 if (r->length != length) {
90 r->length = length;
869871b5 91 iio_mark_update_needed_kfifo(r);
b174baf4
JC
92 }
93 return 0;
94}
b174baf4 95
14555b14 96static int iio_store_to_kfifo(struct iio_buffer *r,
ce56ade6 97 u8 *data)
b174baf4
JC
98{
99 int ret;
100 struct iio_kfifo *kf = iio_to_kfifo(r);
c559afbf
JC
101 ret = kfifo_in(&kf->kf, data, 1);
102 if (ret != 1)
b174baf4 103 return -EBUSY;
7c388ec1
JC
104 r->stufftoread = true;
105 wake_up_interruptible(&r->pollq);
c559afbf 106
b174baf4
JC
107 return 0;
108}
b174baf4 109
14555b14 110static int iio_read_first_n_kfifo(struct iio_buffer *r,
b26a2188 111 size_t n, char __user *buf)
b174baf4
JC
112{
113 int ret, copied;
114 struct iio_kfifo *kf = iio_to_kfifo(r);
115
08ce9b44 116 if (n < r->bytes_per_datum || r->bytes_per_datum == 0)
8fe64955
LPC
117 return -EINVAL;
118
8fe64955 119 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
08ce9b44
JC
120 if (ret < 0)
121 return ret;
b174baf4 122
7c388ec1
JC
123 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
b174baf4
JC
129 return copied;
130}
5565a450 131
7e632344 132static const struct iio_buffer_access_funcs kfifo_access_funcs = {
5565a450
JC
133 .store_to = &iio_store_to_kfifo,
134 .read_first_n = &iio_read_first_n_kfifo,
5565a450
JC
135 .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};
7e632344
LPC
141
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;
7c388ec1 153 kf->buffer.length = 2;
7e632344
LPC
154 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);
5565a450 163
b174baf4 164MODULE_LICENSE("GPL");