toshiba_acpi: Add ECO mode led support
[linux/fpc-iii.git] / drivers / iio / kfifo_buf.c
blob7134e8ada09ac5a08cf3c6382533cb6eaa89afa6
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>
10 #include <linux/poll.h>
12 struct iio_kfifo {
13 struct iio_buffer buffer;
14 struct kfifo kf;
15 struct mutex user_lock;
16 int update_needed;
19 #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
21 static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
22 int bytes_per_datum, int length)
24 if ((length == 0) || (bytes_per_datum == 0))
25 return -EINVAL;
27 return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
28 bytes_per_datum, GFP_KERNEL);
31 static int iio_request_update_kfifo(struct iio_buffer *r)
33 int ret = 0;
34 struct iio_kfifo *buf = iio_to_kfifo(r);
36 mutex_lock(&buf->user_lock);
37 if (buf->update_needed) {
38 kfifo_free(&buf->kf);
39 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
40 buf->buffer.length);
41 buf->update_needed = false;
42 } else {
43 kfifo_reset_out(&buf->kf);
45 mutex_unlock(&buf->user_lock);
47 return ret;
50 static int iio_get_length_kfifo(struct iio_buffer *r)
52 return r->length;
55 static IIO_BUFFER_ENABLE_ATTR;
56 static IIO_BUFFER_LENGTH_ATTR;
58 static struct attribute *iio_kfifo_attributes[] = {
59 &dev_attr_length.attr,
60 &dev_attr_enable.attr,
61 NULL,
64 static struct attribute_group iio_kfifo_attribute_group = {
65 .attrs = iio_kfifo_attributes,
66 .name = "buffer",
69 static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
71 return r->bytes_per_datum;
74 static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
76 struct iio_kfifo *kf = iio_to_kfifo(r);
77 kf->update_needed = true;
78 return 0;
81 static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
83 if (r->bytes_per_datum != bpd) {
84 r->bytes_per_datum = bpd;
85 iio_mark_update_needed_kfifo(r);
87 return 0;
90 static int iio_set_length_kfifo(struct iio_buffer *r, int length)
92 /* Avoid an invalid state */
93 if (length < 2)
94 length = 2;
95 if (r->length != length) {
96 r->length = length;
97 iio_mark_update_needed_kfifo(r);
99 return 0;
102 static int iio_store_to_kfifo(struct iio_buffer *r,
103 const void *data)
105 int ret;
106 struct iio_kfifo *kf = iio_to_kfifo(r);
107 ret = kfifo_in(&kf->kf, data, 1);
108 if (ret != 1)
109 return -EBUSY;
111 wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM);
113 return 0;
116 static int iio_read_first_n_kfifo(struct iio_buffer *r,
117 size_t n, char __user *buf)
119 int ret, copied;
120 struct iio_kfifo *kf = iio_to_kfifo(r);
122 if (mutex_lock_interruptible(&kf->user_lock))
123 return -ERESTARTSYS;
125 if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
126 ret = -EINVAL;
127 else
128 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
129 mutex_unlock(&kf->user_lock);
130 if (ret < 0)
131 return ret;
133 return copied;
136 static bool iio_kfifo_buf_data_available(struct iio_buffer *r)
138 struct iio_kfifo *kf = iio_to_kfifo(r);
139 bool empty;
141 mutex_lock(&kf->user_lock);
142 empty = kfifo_is_empty(&kf->kf);
143 mutex_unlock(&kf->user_lock);
145 return !empty;
148 static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
150 struct iio_kfifo *kf = iio_to_kfifo(buffer);
152 mutex_destroy(&kf->user_lock);
153 kfifo_free(&kf->kf);
154 kfree(kf);
157 static const struct iio_buffer_access_funcs kfifo_access_funcs = {
158 .store_to = &iio_store_to_kfifo,
159 .read_first_n = &iio_read_first_n_kfifo,
160 .data_available = iio_kfifo_buf_data_available,
161 .request_update = &iio_request_update_kfifo,
162 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
163 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
164 .get_length = &iio_get_length_kfifo,
165 .set_length = &iio_set_length_kfifo,
166 .release = &iio_kfifo_buffer_release,
169 struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
171 struct iio_kfifo *kf;
173 kf = kzalloc(sizeof *kf, GFP_KERNEL);
174 if (!kf)
175 return NULL;
176 kf->update_needed = true;
177 iio_buffer_init(&kf->buffer);
178 kf->buffer.attrs = &iio_kfifo_attribute_group;
179 kf->buffer.access = &kfifo_access_funcs;
180 kf->buffer.length = 2;
181 mutex_init(&kf->user_lock);
182 return &kf->buffer;
184 EXPORT_SYMBOL(iio_kfifo_allocate);
186 void iio_kfifo_free(struct iio_buffer *r)
188 iio_buffer_put(r);
190 EXPORT_SYMBOL(iio_kfifo_free);
192 MODULE_LICENSE("GPL");