Linux-libre 4.9.123-gnu
[librecmc/linux-libre.git] / drivers / iio / buffer / kfifo_buf.c
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>
11
12 struct iio_kfifo {
13         struct iio_buffer buffer;
14         struct kfifo kf;
15         struct mutex user_lock;
16         int update_needed;
17 };
18
19 #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
20
21 static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
22                         size_t bytes_per_datum, unsigned int length)
23 {
24         if ((length == 0) || (bytes_per_datum == 0))
25                 return -EINVAL;
26
27         /*
28          * Make sure we don't overflow an unsigned int after kfifo rounds up to
29          * the next power of 2.
30          */
31         if (roundup_pow_of_two(length) > UINT_MAX / bytes_per_datum)
32                 return -EINVAL;
33
34         return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
35                              bytes_per_datum, GFP_KERNEL);
36 }
37
38 static int iio_request_update_kfifo(struct iio_buffer *r)
39 {
40         int ret = 0;
41         struct iio_kfifo *buf = iio_to_kfifo(r);
42
43         mutex_lock(&buf->user_lock);
44         if (buf->update_needed) {
45                 kfifo_free(&buf->kf);
46                 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
47                                    buf->buffer.length);
48                 if (ret >= 0)
49                         buf->update_needed = false;
50         } else {
51                 kfifo_reset_out(&buf->kf);
52         }
53         mutex_unlock(&buf->user_lock);
54
55         return ret;
56 }
57
58 static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
59 {
60         struct iio_kfifo *kf = iio_to_kfifo(r);
61         kf->update_needed = true;
62         return 0;
63 }
64
65 static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
66 {
67         if (r->bytes_per_datum != bpd) {
68                 r->bytes_per_datum = bpd;
69                 iio_mark_update_needed_kfifo(r);
70         }
71         return 0;
72 }
73
74 static int iio_set_length_kfifo(struct iio_buffer *r, unsigned int length)
75 {
76         /* Avoid an invalid state */
77         if (length < 2)
78                 length = 2;
79         if (r->length != length) {
80                 r->length = length;
81                 iio_mark_update_needed_kfifo(r);
82         }
83         return 0;
84 }
85
86 static int iio_store_to_kfifo(struct iio_buffer *r,
87                               const void *data)
88 {
89         int ret;
90         struct iio_kfifo *kf = iio_to_kfifo(r);
91         ret = kfifo_in(&kf->kf, data, 1);
92         if (ret != 1)
93                 return -EBUSY;
94         return 0;
95 }
96
97 static int iio_read_first_n_kfifo(struct iio_buffer *r,
98                            size_t n, char __user *buf)
99 {
100         int ret, copied;
101         struct iio_kfifo *kf = iio_to_kfifo(r);
102
103         if (mutex_lock_interruptible(&kf->user_lock))
104                 return -ERESTARTSYS;
105
106         if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
107                 ret = -EINVAL;
108         else
109                 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
110         mutex_unlock(&kf->user_lock);
111         if (ret < 0)
112                 return ret;
113
114         return copied;
115 }
116
117 static size_t iio_kfifo_buf_data_available(struct iio_buffer *r)
118 {
119         struct iio_kfifo *kf = iio_to_kfifo(r);
120         size_t samples;
121
122         mutex_lock(&kf->user_lock);
123         samples = kfifo_len(&kf->kf);
124         mutex_unlock(&kf->user_lock);
125
126         return samples;
127 }
128
129 static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
130 {
131         struct iio_kfifo *kf = iio_to_kfifo(buffer);
132
133         mutex_destroy(&kf->user_lock);
134         kfifo_free(&kf->kf);
135         kfree(kf);
136 }
137
138 static const struct iio_buffer_access_funcs kfifo_access_funcs = {
139         .store_to = &iio_store_to_kfifo,
140         .read_first_n = &iio_read_first_n_kfifo,
141         .data_available = iio_kfifo_buf_data_available,
142         .request_update = &iio_request_update_kfifo,
143         .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
144         .set_length = &iio_set_length_kfifo,
145         .release = &iio_kfifo_buffer_release,
146
147         .modes = INDIO_BUFFER_SOFTWARE | INDIO_BUFFER_TRIGGERED,
148 };
149
150 struct iio_buffer *iio_kfifo_allocate(void)
151 {
152         struct iio_kfifo *kf;
153
154         kf = kzalloc(sizeof(*kf), GFP_KERNEL);
155         if (!kf)
156                 return NULL;
157
158         kf->update_needed = true;
159         iio_buffer_init(&kf->buffer);
160         kf->buffer.access = &kfifo_access_funcs;
161         kf->buffer.length = 2;
162         mutex_init(&kf->user_lock);
163
164         return &kf->buffer;
165 }
166 EXPORT_SYMBOL(iio_kfifo_allocate);
167
168 void iio_kfifo_free(struct iio_buffer *r)
169 {
170         iio_buffer_put(r);
171 }
172 EXPORT_SYMBOL(iio_kfifo_free);
173
174 static void devm_iio_kfifo_release(struct device *dev, void *res)
175 {
176         iio_kfifo_free(*(struct iio_buffer **)res);
177 }
178
179 static int devm_iio_kfifo_match(struct device *dev, void *res, void *data)
180 {
181         struct iio_buffer **r = res;
182
183         if (WARN_ON(!r || !*r))
184                 return 0;
185
186         return *r == data;
187 }
188
189 /**
190  * devm_iio_fifo_allocate - Resource-managed iio_kfifo_allocate()
191  * @dev:                Device to allocate kfifo buffer for
192  *
193  * RETURNS:
194  * Pointer to allocated iio_buffer on success, NULL on failure.
195  */
196 struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
197 {
198         struct iio_buffer **ptr, *r;
199
200         ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
201         if (!ptr)
202                 return NULL;
203
204         r = iio_kfifo_allocate();
205         if (r) {
206                 *ptr = r;
207                 devres_add(dev, ptr);
208         } else {
209                 devres_free(ptr);
210         }
211
212         return r;
213 }
214 EXPORT_SYMBOL(devm_iio_kfifo_allocate);
215
216 /**
217  * devm_iio_fifo_free - Resource-managed iio_kfifo_free()
218  * @dev:                Device the buffer belongs to
219  * @r:                  The buffer associated with the device
220  */
221 void devm_iio_kfifo_free(struct device *dev, struct iio_buffer *r)
222 {
223         WARN_ON(devres_release(dev, devm_iio_kfifo_release,
224                                devm_iio_kfifo_match, r));
225 }
226 EXPORT_SYMBOL(devm_iio_kfifo_free);
227
228 MODULE_LICENSE("GPL");