drivers/firmware/dmi_scan.c: fetch dmi version from SMBIOS if it exists
[linux/fpc-iii.git] / drivers / iio / kfifo_buf.c
blob5bc5c860e9ca5fe1426cf1189b3a581aad78f3ba
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>
8 #include <linux/iio/kfifo_buf.h>
9 #include <linux/sched.h>
11 struct iio_kfifo {
12 struct iio_buffer buffer;
13 struct kfifo kf;
14 int update_needed;
17 #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
19 static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
20 int bytes_per_datum, int length)
22 if ((length == 0) || (bytes_per_datum == 0))
23 return -EINVAL;
25 __iio_update_buffer(&buf->buffer, bytes_per_datum, length);
26 return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
27 bytes_per_datum, GFP_KERNEL);
30 static int iio_request_update_kfifo(struct iio_buffer *r)
32 int ret = 0;
33 struct iio_kfifo *buf = iio_to_kfifo(r);
35 if (!buf->update_needed)
36 goto error_ret;
37 kfifo_free(&buf->kf);
38 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
39 buf->buffer.length);
40 r->stufftoread = false;
41 error_ret:
42 return ret;
45 static int iio_get_length_kfifo(struct iio_buffer *r)
47 return r->length;
50 static IIO_BUFFER_ENABLE_ATTR;
51 static IIO_BUFFER_LENGTH_ATTR;
53 static struct attribute *iio_kfifo_attributes[] = {
54 &dev_attr_length.attr,
55 &dev_attr_enable.attr,
56 NULL,
59 static struct attribute_group iio_kfifo_attribute_group = {
60 .attrs = iio_kfifo_attributes,
61 .name = "buffer",
64 static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
66 return r->bytes_per_datum;
69 static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
71 struct iio_kfifo *kf = iio_to_kfifo(r);
72 kf->update_needed = true;
73 return 0;
76 static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
78 if (r->bytes_per_datum != bpd) {
79 r->bytes_per_datum = bpd;
80 iio_mark_update_needed_kfifo(r);
82 return 0;
85 static int iio_set_length_kfifo(struct iio_buffer *r, int length)
87 /* Avoid an invalid state */
88 if (length < 2)
89 length = 2;
90 if (r->length != length) {
91 r->length = length;
92 iio_mark_update_needed_kfifo(r);
94 return 0;
97 static int iio_store_to_kfifo(struct iio_buffer *r,
98 u8 *data)
100 int ret;
101 struct iio_kfifo *kf = iio_to_kfifo(r);
102 ret = kfifo_in(&kf->kf, data, 1);
103 if (ret != 1)
104 return -EBUSY;
105 r->stufftoread = true;
106 wake_up_interruptible(&r->pollq);
108 return 0;
111 static int iio_read_first_n_kfifo(struct iio_buffer *r,
112 size_t n, char __user *buf)
114 int ret, copied;
115 struct iio_kfifo *kf = iio_to_kfifo(r);
117 if (n < r->bytes_per_datum || r->bytes_per_datum == 0)
118 return -EINVAL;
120 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
121 if (ret < 0)
122 return ret;
124 if (kfifo_is_empty(&kf->kf))
125 r->stufftoread = false;
126 /* verify it is still empty to avoid race */
127 if (!kfifo_is_empty(&kf->kf))
128 r->stufftoread = true;
130 return copied;
133 static const struct iio_buffer_access_funcs kfifo_access_funcs = {
134 .store_to = &iio_store_to_kfifo,
135 .read_first_n = &iio_read_first_n_kfifo,
136 .request_update = &iio_request_update_kfifo,
137 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
138 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
139 .get_length = &iio_get_length_kfifo,
140 .set_length = &iio_set_length_kfifo,
143 struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
145 struct iio_kfifo *kf;
147 kf = kzalloc(sizeof *kf, GFP_KERNEL);
148 if (!kf)
149 return NULL;
150 kf->update_needed = true;
151 iio_buffer_init(&kf->buffer);
152 kf->buffer.attrs = &iio_kfifo_attribute_group;
153 kf->buffer.access = &kfifo_access_funcs;
154 kf->buffer.length = 2;
155 return &kf->buffer;
157 EXPORT_SYMBOL(iio_kfifo_allocate);
159 void iio_kfifo_free(struct iio_buffer *r)
161 kfree(iio_to_kfifo(r));
163 EXPORT_SYMBOL(iio_kfifo_free);
165 MODULE_LICENSE("GPL");