blob: a24b2e005eb394d7c6704e66645416f0960c4eab [file] [log] [blame]
Jonathan Cameron14555b12011-09-21 11:15:57 +01001/* The industrial I/O core
2 *
3 * Copyright (c) 2008 Jonathan Cameron
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * Handling of buffer allocation / resizing.
10 *
11 *
12 * Things to look at here.
13 * - Better memory allocation techniques?
14 * - Alternative access techniques?
15 */
16#include <linux/kernel.h>
Paul Gortmaker8e336a72011-07-10 13:09:12 -040017#include <linux/export.h>
Jonathan Cameron14555b12011-09-21 11:15:57 +010018#include <linux/device.h>
19#include <linux/fs.h>
20#include <linux/cdev.h>
21#include <linux/slab.h>
22#include <linux/poll.h>
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +010023#include <linux/sched.h>
Jonathan Cameron14555b12011-09-21 11:15:57 +010024
Jonathan Cameron06458e22012-04-25 15:54:58 +010025#include <linux/iio/iio.h>
Jonathan Cameron14555b12011-09-21 11:15:57 +010026#include "iio_core.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010027#include <linux/iio/sysfs.h>
28#include <linux/iio/buffer.h>
Jonathan Cameron14555b12011-09-21 11:15:57 +010029
30static const char * const iio_endian_prefix[] = {
31 [IIO_BE] = "be",
32 [IIO_LE] = "le",
33};
34
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +010035static bool iio_buffer_is_active(struct iio_buffer *buf)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010036{
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +010037 return !list_empty(&buf->buffer_list);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010038}
39
Josselin Costanzi37d34552015-03-22 20:33:38 +020040static size_t iio_buffer_data_available(struct iio_buffer *buf)
Lars-Peter Clausen647cc7b2013-11-25 14:56:00 +000041{
Josselin Costanzi9dd46942014-06-27 17:20:00 +010042 return buf->access->data_available(buf);
Lars-Peter Clausen647cc7b2013-11-25 14:56:00 +000043}
44
Josselin Costanzi37d34552015-03-22 20:33:38 +020045static bool iio_buffer_ready(struct iio_dev *indio_dev, struct iio_buffer *buf,
46 size_t to_wait)
47{
48 /* wakeup if the device was unregistered */
49 if (!indio_dev->info)
50 return true;
51
52 /* drain the buffer if it was disabled */
53 if (!iio_buffer_is_active(buf))
54 to_wait = min_t(size_t, to_wait, 1);
55
56 if (iio_buffer_data_available(buf) >= to_wait)
57 return true;
58
59 return false;
60}
61
Jonathan Cameron14555b12011-09-21 11:15:57 +010062/**
63 * iio_buffer_read_first_n_outer() - chrdev read for buffer access
64 *
65 * This function relies on all buffer implementations having an
66 * iio_buffer as their first element.
67 **/
68ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
69 size_t n, loff_t *f_ps)
70{
71 struct iio_dev *indio_dev = filp->private_data;
72 struct iio_buffer *rb = indio_dev->buffer;
Josselin Costanzi37d34552015-03-22 20:33:38 +020073 size_t datum_size;
74 size_t to_wait = 0;
Lars-Peter Clausenee551a12013-11-25 14:56:00 +000075 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +010076
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +010077 if (!indio_dev->info)
78 return -ENODEV;
79
Jonathan Cameron96e00f12011-10-26 17:27:45 +010080 if (!rb || !rb->access->read_first_n)
Jonathan Cameron14555b12011-09-21 11:15:57 +010081 return -EINVAL;
Lars-Peter Clausenee551a12013-11-25 14:56:00 +000082
Josselin Costanzi37d34552015-03-22 20:33:38 +020083 datum_size = rb->bytes_per_datum;
Lars-Peter Clausenee551a12013-11-25 14:56:00 +000084
Josselin Costanzi37d34552015-03-22 20:33:38 +020085 /*
86 * If datum_size is 0 there will never be anything to read from the
87 * buffer, so signal end of file now.
88 */
89 if (!datum_size)
90 return 0;
91
92 if (!(filp->f_flags & O_NONBLOCK))
93 to_wait = min_t(size_t, n / datum_size, rb->watermark);
94
95 do {
96 ret = wait_event_interruptible(rb->pollq,
97 iio_buffer_ready(indio_dev, rb, to_wait));
98 if (ret)
99 return ret;
100
101 if (!indio_dev->info)
102 return -ENODEV;
Lars-Peter Clausenee551a12013-11-25 14:56:00 +0000103
104 ret = rb->access->read_first_n(rb, n, buf);
105 if (ret == 0 && (filp->f_flags & O_NONBLOCK))
106 ret = -EAGAIN;
107 } while (ret == 0);
108
109 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100110}
111
112/**
113 * iio_buffer_poll() - poll the buffer to find out if it has data
114 */
115unsigned int iio_buffer_poll(struct file *filp,
116 struct poll_table_struct *wait)
117{
118 struct iio_dev *indio_dev = filp->private_data;
119 struct iio_buffer *rb = indio_dev->buffer;
120
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +0100121 if (!indio_dev->info)
122 return -ENODEV;
123
Jonathan Cameron14555b12011-09-21 11:15:57 +0100124 poll_wait(filp, &rb->pollq, wait);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200125 if (iio_buffer_ready(indio_dev, rb, rb->watermark))
Jonathan Cameron14555b12011-09-21 11:15:57 +0100126 return POLLIN | POLLRDNORM;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100127 return 0;
128}
129
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100130/**
131 * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue
132 * @indio_dev: The IIO device
133 *
134 * Wakes up the event waitqueue used for poll(). Should usually
135 * be called when the device is unregistered.
136 */
137void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
138{
139 if (!indio_dev->buffer)
140 return;
141
142 wake_up(&indio_dev->buffer->pollq);
143}
144
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000145void iio_buffer_init(struct iio_buffer *buffer)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100146{
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000147 INIT_LIST_HEAD(&buffer->demux_list);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100148 INIT_LIST_HEAD(&buffer->buffer_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100149 init_waitqueue_head(&buffer->pollq);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100150 kref_init(&buffer->ref);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200151 buffer->watermark = 1;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100152}
153EXPORT_SYMBOL(iio_buffer_init);
154
155static ssize_t iio_show_scan_index(struct device *dev,
156 struct device_attribute *attr,
157 char *buf)
158{
159 return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
160}
161
162static ssize_t iio_show_fixed_type(struct device *dev,
163 struct device_attribute *attr,
164 char *buf)
165{
166 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
167 u8 type = this_attr->c->scan_type.endianness;
168
169 if (type == IIO_CPU) {
Jonathan Cameron9d5d1152011-10-04 16:02:08 +0100170#ifdef __LITTLE_ENDIAN
171 type = IIO_LE;
172#else
173 type = IIO_BE;
174#endif
Jonathan Cameron14555b12011-09-21 11:15:57 +0100175 }
Srinivas Pandruvada0ee85462014-04-29 00:51:00 +0100176 if (this_attr->c->scan_type.repeat > 1)
177 return sprintf(buf, "%s:%c%d/%dX%d>>%u\n",
178 iio_endian_prefix[type],
179 this_attr->c->scan_type.sign,
180 this_attr->c->scan_type.realbits,
181 this_attr->c->scan_type.storagebits,
182 this_attr->c->scan_type.repeat,
183 this_attr->c->scan_type.shift);
184 else
185 return sprintf(buf, "%s:%c%d/%d>>%u\n",
Jonathan Cameron14555b12011-09-21 11:15:57 +0100186 iio_endian_prefix[type],
187 this_attr->c->scan_type.sign,
188 this_attr->c->scan_type.realbits,
189 this_attr->c->scan_type.storagebits,
190 this_attr->c->scan_type.shift);
191}
192
193static ssize_t iio_scan_el_show(struct device *dev,
194 struct device_attribute *attr,
195 char *buf)
196{
197 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200198 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100199
Alec Berg2076a202014-03-19 18:50:00 +0000200 /* Ensure ret is 0 or 1. */
201 ret = !!test_bit(to_iio_dev_attr(attr)->address,
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000202 indio_dev->buffer->scan_mask);
203
Jonathan Cameron14555b12011-09-21 11:15:57 +0100204 return sprintf(buf, "%d\n", ret);
205}
206
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100207/* Note NULL used as error indicator as it doesn't make sense. */
208static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
209 unsigned int masklength,
210 const unsigned long *mask)
211{
212 if (bitmap_empty(mask, masklength))
213 return NULL;
214 while (*av_masks) {
215 if (bitmap_subset(mask, av_masks, masklength))
216 return av_masks;
217 av_masks += BITS_TO_LONGS(masklength);
218 }
219 return NULL;
220}
221
222static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
223 const unsigned long *mask)
224{
225 if (!indio_dev->setup_ops->validate_scan_mask)
226 return true;
227
228 return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
229}
230
231/**
232 * iio_scan_mask_set() - set particular bit in the scan mask
233 * @indio_dev: the iio device
234 * @buffer: the buffer whose scan mask we are interested in
235 * @bit: the bit to be set.
236 *
237 * Note that at this point we have no way of knowing what other
238 * buffers might request, hence this code only verifies that the
239 * individual buffers request is plausible.
240 */
241static int iio_scan_mask_set(struct iio_dev *indio_dev,
242 struct iio_buffer *buffer, int bit)
243{
244 const unsigned long *mask;
245 unsigned long *trialmask;
246
247 trialmask = kmalloc(sizeof(*trialmask)*
248 BITS_TO_LONGS(indio_dev->masklength),
249 GFP_KERNEL);
250
251 if (trialmask == NULL)
252 return -ENOMEM;
253 if (!indio_dev->masklength) {
254 WARN_ON("Trying to set scanmask prior to registering buffer\n");
255 goto err_invalid_mask;
256 }
257 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
258 set_bit(bit, trialmask);
259
260 if (!iio_validate_scan_mask(indio_dev, trialmask))
261 goto err_invalid_mask;
262
263 if (indio_dev->available_scan_masks) {
264 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
265 indio_dev->masklength,
266 trialmask);
267 if (!mask)
268 goto err_invalid_mask;
269 }
270 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
271
272 kfree(trialmask);
273
274 return 0;
275
276err_invalid_mask:
277 kfree(trialmask);
278 return -EINVAL;
279}
280
Jonathan Cameron14555b12011-09-21 11:15:57 +0100281static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
282{
283 clear_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100284 return 0;
285}
286
287static ssize_t iio_scan_el_store(struct device *dev,
288 struct device_attribute *attr,
289 const char *buf,
290 size_t len)
291{
Jonathan Camerona714af22012-04-21 10:09:32 +0100292 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100293 bool state;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200294 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100295 struct iio_buffer *buffer = indio_dev->buffer;
296 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
297
Jonathan Camerona714af22012-04-21 10:09:32 +0100298 ret = strtobool(buf, &state);
299 if (ret < 0)
300 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100301 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100302 if (iio_buffer_is_active(indio_dev->buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100303 ret = -EBUSY;
304 goto error_ret;
305 }
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000306 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100307 if (ret < 0)
308 goto error_ret;
309 if (!state && ret) {
310 ret = iio_scan_mask_clear(buffer, this_attr->address);
311 if (ret)
312 goto error_ret;
313 } else if (state && !ret) {
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000314 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100315 if (ret)
316 goto error_ret;
317 }
318
319error_ret:
320 mutex_unlock(&indio_dev->mlock);
321
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100322 return ret < 0 ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100323
324}
325
326static ssize_t iio_scan_el_ts_show(struct device *dev,
327 struct device_attribute *attr,
328 char *buf)
329{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200330 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100331 return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100332}
333
334static ssize_t iio_scan_el_ts_store(struct device *dev,
335 struct device_attribute *attr,
336 const char *buf,
337 size_t len)
338{
Jonathan Camerona714af22012-04-21 10:09:32 +0100339 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200340 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100341 bool state;
342
Jonathan Camerona714af22012-04-21 10:09:32 +0100343 ret = strtobool(buf, &state);
344 if (ret < 0)
345 return ret;
346
Jonathan Cameron14555b12011-09-21 11:15:57 +0100347 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100348 if (iio_buffer_is_active(indio_dev->buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100349 ret = -EBUSY;
350 goto error_ret;
351 }
352 indio_dev->buffer->scan_timestamp = state;
353error_ret:
354 mutex_unlock(&indio_dev->mlock);
355
356 return ret ? ret : len;
357}
358
359static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
360 const struct iio_chan_spec *chan)
361{
362 int ret, attrcount = 0;
363 struct iio_buffer *buffer = indio_dev->buffer;
364
365 ret = __iio_add_chan_devattr("index",
366 chan,
367 &iio_show_scan_index,
368 NULL,
369 0,
Jonathan Cameron37044322013-09-08 14:57:00 +0100370 IIO_SEPARATE,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100371 &indio_dev->dev,
372 &buffer->scan_el_dev_attr_list);
373 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000374 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100375 attrcount++;
376 ret = __iio_add_chan_devattr("type",
377 chan,
378 &iio_show_fixed_type,
379 NULL,
380 0,
381 0,
382 &indio_dev->dev,
383 &buffer->scan_el_dev_attr_list);
384 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000385 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100386 attrcount++;
387 if (chan->type != IIO_TIMESTAMP)
388 ret = __iio_add_chan_devattr("en",
389 chan,
390 &iio_scan_el_show,
391 &iio_scan_el_store,
392 chan->scan_index,
393 0,
394 &indio_dev->dev,
395 &buffer->scan_el_dev_attr_list);
396 else
397 ret = __iio_add_chan_devattr("en",
398 chan,
399 &iio_scan_el_ts_show,
400 &iio_scan_el_ts_store,
401 chan->scan_index,
402 0,
403 &indio_dev->dev,
404 &buffer->scan_el_dev_attr_list);
Peter Meerwald95725882013-09-17 23:42:00 +0100405 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000406 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100407 attrcount++;
408 ret = attrcount;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100409 return ret;
410}
411
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100412static ssize_t iio_buffer_read_length(struct device *dev,
413 struct device_attribute *attr,
414 char *buf)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100415{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200416 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100417 struct iio_buffer *buffer = indio_dev->buffer;
418
Lars-Peter Clausen37495662014-11-26 18:55:17 +0100419 return sprintf(buf, "%d\n", buffer->length);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100420}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100421
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100422static ssize_t iio_buffer_write_length(struct device *dev,
423 struct device_attribute *attr,
424 const char *buf, size_t len)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100425{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200426 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100427 struct iio_buffer *buffer = indio_dev->buffer;
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100428 unsigned int val;
429 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100430
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100431 ret = kstrtouint(buf, 10, &val);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100432 if (ret)
433 return ret;
434
Lars-Peter Clausen37495662014-11-26 18:55:17 +0100435 if (val == buffer->length)
436 return len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100437
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100438 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100439 if (iio_buffer_is_active(indio_dev->buffer)) {
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100440 ret = -EBUSY;
441 } else {
Lars-Peter Clausen8d92db22014-11-26 18:55:16 +0100442 buffer->access->set_length(buffer, val);
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100443 ret = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100444 }
Josselin Costanzi37d34552015-03-22 20:33:38 +0200445 if (ret)
446 goto out;
447 if (buffer->length && buffer->length < buffer->watermark)
448 buffer->watermark = buffer->length;
449out:
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100450 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100451
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100452 return ret ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100453}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100454
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100455static ssize_t iio_buffer_show_enable(struct device *dev,
456 struct device_attribute *attr,
457 char *buf)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100458{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200459 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100460 return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
Jonathan Cameron14555b12011-09-21 11:15:57 +0100461}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100462
Peter Meerwald183f4172013-09-18 22:10:00 +0100463static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
464 const unsigned long *mask, bool timestamp)
Jonathan Cameron959d2952011-12-05 21:37:13 +0000465{
Jonathan Cameron959d2952011-12-05 21:37:13 +0000466 const struct iio_chan_spec *ch;
467 unsigned bytes = 0;
468 int length, i;
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100469
470 /* How much space will the demuxed element take? */
471 for_each_set_bit(i, mask,
472 indio_dev->masklength) {
473 ch = iio_find_channel_from_si(indio_dev, i);
Srinivas Pandruvada0ee85462014-04-29 00:51:00 +0100474 if (ch->scan_type.repeat > 1)
475 length = ch->scan_type.storagebits / 8 *
476 ch->scan_type.repeat;
477 else
478 length = ch->scan_type.storagebits / 8;
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100479 bytes = ALIGN(bytes, length);
480 bytes += length;
481 }
482 if (timestamp) {
483 ch = iio_find_channel_from_si(indio_dev,
Jonathan Cameronf1264802012-04-21 10:09:34 +0100484 indio_dev->scan_index_timestamp);
Srinivas Pandruvada0ee85462014-04-29 00:51:00 +0100485 if (ch->scan_type.repeat > 1)
486 length = ch->scan_type.storagebits / 8 *
487 ch->scan_type.repeat;
488 else
489 length = ch->scan_type.storagebits / 8;
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100490 bytes = ALIGN(bytes, length);
491 bytes += length;
492 }
493 return bytes;
494}
495
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100496static void iio_buffer_activate(struct iio_dev *indio_dev,
497 struct iio_buffer *buffer)
498{
499 iio_buffer_get(buffer);
500 list_add(&buffer->buffer_list, &indio_dev->buffer_list);
501}
502
503static void iio_buffer_deactivate(struct iio_buffer *buffer)
504{
505 list_del_init(&buffer->buffer_list);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200506 wake_up_interruptible(&buffer->pollq);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100507 iio_buffer_put(buffer);
508}
509
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100510void iio_disable_all_buffers(struct iio_dev *indio_dev)
511{
512 struct iio_buffer *buffer, *_buffer;
513
514 if (list_empty(&indio_dev->buffer_list))
515 return;
516
517 if (indio_dev->setup_ops->predisable)
518 indio_dev->setup_ops->predisable(indio_dev);
519
520 list_for_each_entry_safe(buffer, _buffer,
521 &indio_dev->buffer_list, buffer_list)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100522 iio_buffer_deactivate(buffer);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100523
524 indio_dev->currentmode = INDIO_DIRECT_MODE;
525 if (indio_dev->setup_ops->postdisable)
526 indio_dev->setup_ops->postdisable(indio_dev);
Lars-Peter Clausene086ed72013-10-15 09:38:00 +0100527
528 if (indio_dev->available_scan_masks == NULL)
529 kfree(indio_dev->active_scan_mask);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100530}
531
Lars-Peter Clausen8e050992013-10-14 17:49:00 +0100532static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
533 struct iio_buffer *buffer)
534{
535 unsigned int bytes;
536
537 if (!buffer->access->set_bytes_per_datum)
538 return;
539
540 bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
541 buffer->scan_timestamp);
542
543 buffer->access->set_bytes_per_datum(buffer, bytes);
544}
545
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100546static int __iio_update_buffers(struct iio_dev *indio_dev,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100547 struct iio_buffer *insert_buffer,
548 struct iio_buffer *remove_buffer)
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100549{
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100550 int ret;
551 int success = 0;
552 struct iio_buffer *buffer;
553 unsigned long *compound_mask;
554 const unsigned long *old_mask;
Jonathan Cameron959d2952011-12-05 21:37:13 +0000555
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100556 /* Wind down existing buffers - iff there are any */
557 if (!list_empty(&indio_dev->buffer_list)) {
558 if (indio_dev->setup_ops->predisable) {
559 ret = indio_dev->setup_ops->predisable(indio_dev);
560 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000561 return ret;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100562 }
563 indio_dev->currentmode = INDIO_DIRECT_MODE;
564 if (indio_dev->setup_ops->postdisable) {
565 ret = indio_dev->setup_ops->postdisable(indio_dev);
566 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000567 return ret;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100568 }
569 }
570 /* Keep a copy of current setup to allow roll back */
571 old_mask = indio_dev->active_scan_mask;
572 if (!indio_dev->available_scan_masks)
573 indio_dev->active_scan_mask = NULL;
574
575 if (remove_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100576 iio_buffer_deactivate(remove_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100577 if (insert_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100578 iio_buffer_activate(indio_dev, insert_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100579
580 /* If no buffers in list, we are done */
581 if (list_empty(&indio_dev->buffer_list)) {
582 indio_dev->currentmode = INDIO_DIRECT_MODE;
583 if (indio_dev->available_scan_masks == NULL)
584 kfree(old_mask);
585 return 0;
586 }
Jonathan Cameron959d2952011-12-05 21:37:13 +0000587
Peter Meerwald95725882013-09-17 23:42:00 +0100588 /* What scan mask do we actually have? */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100589 compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
590 sizeof(long), GFP_KERNEL);
591 if (compound_mask == NULL) {
592 if (indio_dev->available_scan_masks == NULL)
593 kfree(old_mask);
594 return -ENOMEM;
595 }
596 indio_dev->scan_timestamp = 0;
597
598 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
599 bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
600 indio_dev->masklength);
601 indio_dev->scan_timestamp |= buffer->scan_timestamp;
602 }
603 if (indio_dev->available_scan_masks) {
Jonathan Cameron959d2952011-12-05 21:37:13 +0000604 indio_dev->active_scan_mask =
605 iio_scan_mask_match(indio_dev->available_scan_masks,
606 indio_dev->masklength,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100607 compound_mask);
608 if (indio_dev->active_scan_mask == NULL) {
609 /*
610 * Roll back.
611 * Note can only occur when adding a buffer.
612 */
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100613 iio_buffer_deactivate(insert_buffer);
Peter Meerwaldd66e0452013-09-18 22:10:00 +0100614 if (old_mask) {
615 indio_dev->active_scan_mask = old_mask;
616 success = -EINVAL;
617 }
618 else {
619 kfree(compound_mask);
620 ret = -EINVAL;
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000621 return ret;
Peter Meerwaldd66e0452013-09-18 22:10:00 +0100622 }
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100623 }
624 } else {
625 indio_dev->active_scan_mask = compound_mask;
626 }
Lars-Peter Clausenaff1eb42012-06-15 18:08:59 +0200627
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000628 iio_update_demux(indio_dev);
629
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100630 /* Wind up again */
631 if (indio_dev->setup_ops->preenable) {
632 ret = indio_dev->setup_ops->preenable(indio_dev);
633 if (ret) {
634 printk(KERN_ERR
Michał Mirosławbec18892013-05-04 14:19:00 +0100635 "Buffer not started: buffer preenable failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100636 goto error_remove_inserted;
637 }
638 }
639 indio_dev->scan_bytes =
640 iio_compute_scan_bytes(indio_dev,
641 indio_dev->active_scan_mask,
642 indio_dev->scan_timestamp);
Lars-Peter Clausen8e050992013-10-14 17:49:00 +0100643 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
644 iio_buffer_update_bytes_per_datum(indio_dev, buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100645 if (buffer->access->request_update) {
646 ret = buffer->access->request_update(buffer);
647 if (ret) {
648 printk(KERN_INFO
Michał Mirosławbec18892013-05-04 14:19:00 +0100649 "Buffer not started: buffer parameter update failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100650 goto error_run_postdisable;
651 }
652 }
Lars-Peter Clausen8e050992013-10-14 17:49:00 +0100653 }
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100654 if (indio_dev->info->update_scan_mode) {
655 ret = indio_dev->info
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000656 ->update_scan_mode(indio_dev,
657 indio_dev->active_scan_mask);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100658 if (ret < 0) {
Michał Mirosławbec18892013-05-04 14:19:00 +0100659 printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100660 goto error_run_postdisable;
661 }
662 }
Peter Meerwald95725882013-09-17 23:42:00 +0100663 /* Definitely possible for devices to support both of these. */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100664 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
665 if (!indio_dev->trig) {
666 printk(KERN_INFO "Buffer not started: no trigger\n");
667 ret = -EINVAL;
668 /* Can only occur on first buffer */
669 goto error_run_postdisable;
670 }
671 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
672 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) {
673 indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
Karol Wrona03af03a2015-01-07 19:36:11 +0100674 } else if (indio_dev->modes & INDIO_BUFFER_SOFTWARE) {
675 indio_dev->currentmode = INDIO_BUFFER_SOFTWARE;
Peter Meerwald95725882013-09-17 23:42:00 +0100676 } else { /* Should never be reached */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100677 ret = -EINVAL;
678 goto error_run_postdisable;
679 }
680
681 if (indio_dev->setup_ops->postenable) {
682 ret = indio_dev->setup_ops->postenable(indio_dev);
683 if (ret) {
684 printk(KERN_INFO
Michał Mirosławbec18892013-05-04 14:19:00 +0100685 "Buffer not started: postenable failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100686 indio_dev->currentmode = INDIO_DIRECT_MODE;
687 if (indio_dev->setup_ops->postdisable)
688 indio_dev->setup_ops->postdisable(indio_dev);
689 goto error_disable_all_buffers;
690 }
691 }
692
693 if (indio_dev->available_scan_masks)
694 kfree(compound_mask);
695 else
696 kfree(old_mask);
697
698 return success;
699
700error_disable_all_buffers:
701 indio_dev->currentmode = INDIO_DIRECT_MODE;
702error_run_postdisable:
703 if (indio_dev->setup_ops->postdisable)
704 indio_dev->setup_ops->postdisable(indio_dev);
705error_remove_inserted:
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100706 if (insert_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100707 iio_buffer_deactivate(insert_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100708 indio_dev->active_scan_mask = old_mask;
709 kfree(compound_mask);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100710 return ret;
711}
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100712
713int iio_update_buffers(struct iio_dev *indio_dev,
714 struct iio_buffer *insert_buffer,
715 struct iio_buffer *remove_buffer)
716{
717 int ret;
718
Lars-Peter Clausen3909fab2013-10-04 12:07:00 +0100719 if (insert_buffer == remove_buffer)
720 return 0;
721
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100722 mutex_lock(&indio_dev->info_exist_lock);
723 mutex_lock(&indio_dev->mlock);
724
Lars-Peter Clausen3909fab2013-10-04 12:07:00 +0100725 if (insert_buffer && iio_buffer_is_active(insert_buffer))
726 insert_buffer = NULL;
727
728 if (remove_buffer && !iio_buffer_is_active(remove_buffer))
729 remove_buffer = NULL;
730
731 if (!insert_buffer && !remove_buffer) {
732 ret = 0;
733 goto out_unlock;
734 }
735
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100736 if (indio_dev->info == NULL) {
737 ret = -ENODEV;
738 goto out_unlock;
739 }
740
741 ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
742
743out_unlock:
744 mutex_unlock(&indio_dev->mlock);
745 mutex_unlock(&indio_dev->info_exist_lock);
746
747 return ret;
748}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100749EXPORT_SYMBOL_GPL(iio_update_buffers);
750
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100751static ssize_t iio_buffer_store_enable(struct device *dev,
752 struct device_attribute *attr,
753 const char *buf,
754 size_t len)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100755{
756 int ret;
757 bool requested_state;
758 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100759 bool inlist;
760
761 ret = strtobool(buf, &requested_state);
762 if (ret < 0)
763 return ret;
764
765 mutex_lock(&indio_dev->mlock);
766
767 /* Find out if it is in the list */
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100768 inlist = iio_buffer_is_active(indio_dev->buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100769 /* Already in desired state */
770 if (inlist == requested_state)
771 goto done;
772
773 if (requested_state)
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100774 ret = __iio_update_buffers(indio_dev,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100775 indio_dev->buffer, NULL);
776 else
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100777 ret = __iio_update_buffers(indio_dev,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100778 NULL, indio_dev->buffer);
779
780 if (ret < 0)
781 goto done;
782done:
783 mutex_unlock(&indio_dev->mlock);
784 return (ret < 0) ? ret : len;
785}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100786
Lars-Peter Clausend967cb62014-11-26 18:55:14 +0100787static const char * const iio_scan_elements_group_name = "scan_elements";
788
Josselin Costanzi37d34552015-03-22 20:33:38 +0200789static ssize_t iio_buffer_show_watermark(struct device *dev,
790 struct device_attribute *attr,
791 char *buf)
792{
793 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
794 struct iio_buffer *buffer = indio_dev->buffer;
795
796 return sprintf(buf, "%u\n", buffer->watermark);
797}
798
799static ssize_t iio_buffer_store_watermark(struct device *dev,
800 struct device_attribute *attr,
801 const char *buf,
802 size_t len)
803{
804 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
805 struct iio_buffer *buffer = indio_dev->buffer;
806 unsigned int val;
807 int ret;
808
809 ret = kstrtouint(buf, 10, &val);
810 if (ret)
811 return ret;
812 if (!val)
813 return -EINVAL;
814
815 mutex_lock(&indio_dev->mlock);
816
817 if (val > buffer->length) {
818 ret = -EINVAL;
819 goto out;
820 }
821
822 if (iio_buffer_is_active(indio_dev->buffer)) {
823 ret = -EBUSY;
824 goto out;
825 }
826
827 buffer->watermark = val;
828out:
829 mutex_unlock(&indio_dev->mlock);
830
831 return ret ? ret : len;
832}
833
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100834static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
835 iio_buffer_write_length);
Lars-Peter Clausen8d92db22014-11-26 18:55:16 +0100836static struct device_attribute dev_attr_length_ro = __ATTR(length,
837 S_IRUGO, iio_buffer_read_length, NULL);
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100838static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
839 iio_buffer_show_enable, iio_buffer_store_enable);
Josselin Costanzi37d34552015-03-22 20:33:38 +0200840static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR,
841 iio_buffer_show_watermark, iio_buffer_store_watermark);
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100842
Octavian Purdila6da9b382015-01-31 02:00:00 +0200843static struct attribute *iio_buffer_attrs[] = {
844 &dev_attr_length.attr,
845 &dev_attr_enable.attr,
Josselin Costanzi37d34552015-03-22 20:33:38 +0200846 &dev_attr_watermark.attr,
Octavian Purdila6da9b382015-01-31 02:00:00 +0200847};
848
Lars-Peter Clausend967cb62014-11-26 18:55:14 +0100849int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
850{
851 struct iio_dev_attr *p;
852 struct attribute **attr;
853 struct iio_buffer *buffer = indio_dev->buffer;
854 int ret, i, attrn, attrcount, attrcount_orig = 0;
855 const struct iio_chan_spec *channels;
856
857 if (!buffer)
858 return 0;
859
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100860 attrcount = 0;
861 if (buffer->attrs) {
862 while (buffer->attrs[attrcount] != NULL)
863 attrcount++;
864 }
865
Octavian Purdila6da9b382015-01-31 02:00:00 +0200866 attr = kcalloc(attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1,
867 sizeof(struct attribute *), GFP_KERNEL);
868 if (!attr)
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100869 return -ENOMEM;
870
Octavian Purdila6da9b382015-01-31 02:00:00 +0200871 memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
872 if (!buffer->access->set_length)
873 attr[0] = &dev_attr_length_ro.attr;
874
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100875 if (buffer->attrs)
Octavian Purdila6da9b382015-01-31 02:00:00 +0200876 memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs,
877 sizeof(struct attribute *) * attrcount);
878
879 attr[attrcount + ARRAY_SIZE(iio_buffer_attrs)] = NULL;
880
881 buffer->buffer_group.name = "buffer";
882 buffer->buffer_group.attrs = attr;
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100883
884 indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group;
885
Lars-Peter Clausend967cb62014-11-26 18:55:14 +0100886 if (buffer->scan_el_attrs != NULL) {
887 attr = buffer->scan_el_attrs->attrs;
888 while (*attr++ != NULL)
889 attrcount_orig++;
890 }
891 attrcount = attrcount_orig;
892 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
893 channels = indio_dev->channels;
894 if (channels) {
895 /* new magic */
896 for (i = 0; i < indio_dev->num_channels; i++) {
897 if (channels[i].scan_index < 0)
898 continue;
899
900 /* Establish necessary mask length */
901 if (channels[i].scan_index >
902 (int)indio_dev->masklength - 1)
903 indio_dev->masklength
904 = channels[i].scan_index + 1;
905
906 ret = iio_buffer_add_channel_sysfs(indio_dev,
907 &channels[i]);
908 if (ret < 0)
909 goto error_cleanup_dynamic;
910 attrcount += ret;
911 if (channels[i].type == IIO_TIMESTAMP)
912 indio_dev->scan_index_timestamp =
913 channels[i].scan_index;
914 }
915 if (indio_dev->masklength && buffer->scan_mask == NULL) {
916 buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
917 sizeof(*buffer->scan_mask),
918 GFP_KERNEL);
919 if (buffer->scan_mask == NULL) {
920 ret = -ENOMEM;
921 goto error_cleanup_dynamic;
922 }
923 }
924 }
925
926 buffer->scan_el_group.name = iio_scan_elements_group_name;
927
928 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
929 sizeof(buffer->scan_el_group.attrs[0]),
930 GFP_KERNEL);
931 if (buffer->scan_el_group.attrs == NULL) {
932 ret = -ENOMEM;
933 goto error_free_scan_mask;
934 }
935 if (buffer->scan_el_attrs)
936 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
937 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
938 attrn = attrcount_orig;
939
940 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
941 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
942 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
943
944 return 0;
945
946error_free_scan_mask:
947 kfree(buffer->scan_mask);
948error_cleanup_dynamic:
949 iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100950 kfree(indio_dev->buffer->buffer_group.attrs);
Lars-Peter Clausend967cb62014-11-26 18:55:14 +0100951
952 return ret;
953}
954
955void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
956{
957 if (!indio_dev->buffer)
958 return;
959
960 kfree(indio_dev->buffer->scan_mask);
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100961 kfree(indio_dev->buffer->buffer_group.attrs);
Lars-Peter Clausend967cb62014-11-26 18:55:14 +0100962 kfree(indio_dev->buffer->scan_el_group.attrs);
963 iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
964}
965
Jonathan Cameron14555b12011-09-21 11:15:57 +0100966/**
Lars-Peter Clausen81636632012-07-09 10:00:00 +0100967 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
968 * @indio_dev: the iio device
969 * @mask: scan mask to be checked
970 *
971 * Return true if exactly one bit is set in the scan mask, false otherwise. It
972 * can be used for devices where only one channel can be active for sampling at
973 * a time.
974 */
975bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
976 const unsigned long *mask)
977{
978 return bitmap_weight(mask, indio_dev->masklength) == 1;
979}
980EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
981
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000982int iio_scan_mask_query(struct iio_dev *indio_dev,
983 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100984{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100985 if (bit > indio_dev->masklength)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100986 return -EINVAL;
987
988 if (!buffer->scan_mask)
989 return 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100990
Alec Berg2076a202014-03-19 18:50:00 +0000991 /* Ensure return value is 0 or 1. */
992 return !!test_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100993};
994EXPORT_SYMBOL_GPL(iio_scan_mask_query);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000995
996/**
997 * struct iio_demux_table() - table describing demux memcpy ops
998 * @from: index to copy from
Peter Meerwald99698b42012-08-26 13:43:00 +0100999 * @to: index to copy to
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001000 * @length: how many bytes to copy
1001 * @l: list head used for management
1002 */
1003struct iio_demux_table {
1004 unsigned from;
1005 unsigned to;
1006 unsigned length;
1007 struct list_head l;
1008};
1009
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +01001010static const void *iio_demux(struct iio_buffer *buffer,
1011 const void *datain)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001012{
1013 struct iio_demux_table *t;
1014
1015 if (list_empty(&buffer->demux_list))
1016 return datain;
1017 list_for_each_entry(t, &buffer->demux_list, l)
1018 memcpy(buffer->demux_bounce + t->to,
1019 datain + t->from, t->length);
1020
1021 return buffer->demux_bounce;
1022}
1023
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +01001024static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001025{
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +01001026 const void *dataout = iio_demux(buffer, data);
Josselin Costanzi37d34552015-03-22 20:33:38 +02001027 int ret;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001028
Josselin Costanzi37d34552015-03-22 20:33:38 +02001029 ret = buffer->access->store_to(buffer, dataout);
1030 if (ret)
1031 return ret;
1032
1033 /*
1034 * We can't just test for watermark to decide if we wake the poll queue
1035 * because read may request less samples than the watermark.
1036 */
1037 wake_up_interruptible_poll(&buffer->pollq, POLLIN | POLLRDNORM);
1038 return 0;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001039}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001040
Jonathan Cameron842cd102012-04-21 10:09:45 +01001041static void iio_buffer_demux_free(struct iio_buffer *buffer)
1042{
1043 struct iio_demux_table *p, *q;
1044 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
1045 list_del(&p->l);
1046 kfree(p);
1047 }
1048}
1049
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001050
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +01001051int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001052{
1053 int ret;
1054 struct iio_buffer *buf;
1055
1056 list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
1057 ret = iio_push_to_buffer(buf, data);
1058 if (ret < 0)
1059 return ret;
1060 }
1061
1062 return 0;
1063}
1064EXPORT_SYMBOL_GPL(iio_push_to_buffers);
1065
Lars-Peter Clausencbe88bc2014-07-17 16:59:00 +01001066static int iio_buffer_add_demux(struct iio_buffer *buffer,
1067 struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc,
1068 unsigned int length)
1069{
1070
1071 if (*p && (*p)->from + (*p)->length == in_loc &&
1072 (*p)->to + (*p)->length == out_loc) {
1073 (*p)->length += length;
1074 } else {
Jonathan Cameron7cdca1782014-08-08 09:43:00 +01001075 *p = kmalloc(sizeof(**p), GFP_KERNEL);
Lars-Peter Clausencbe88bc2014-07-17 16:59:00 +01001076 if (*p == NULL)
1077 return -ENOMEM;
1078 (*p)->from = in_loc;
1079 (*p)->to = out_loc;
1080 (*p)->length = length;
1081 list_add_tail(&(*p)->l, &buffer->demux_list);
1082 }
1083
1084 return 0;
1085}
1086
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001087static int iio_buffer_update_demux(struct iio_dev *indio_dev,
1088 struct iio_buffer *buffer)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001089{
1090 const struct iio_chan_spec *ch;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001091 int ret, in_ind = -1, out_ind, length;
1092 unsigned in_loc = 0, out_loc = 0;
Lars-Peter Clausencbe88bc2014-07-17 16:59:00 +01001093 struct iio_demux_table *p = NULL;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001094
1095 /* Clear out any old demux */
Jonathan Cameron842cd102012-04-21 10:09:45 +01001096 iio_buffer_demux_free(buffer);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001097 kfree(buffer->demux_bounce);
1098 buffer->demux_bounce = NULL;
1099
1100 /* First work out which scan mode we will actually have */
1101 if (bitmap_equal(indio_dev->active_scan_mask,
1102 buffer->scan_mask,
1103 indio_dev->masklength))
1104 return 0;
1105
1106 /* Now we have the two masks, work from least sig and build up sizes */
1107 for_each_set_bit(out_ind,
Lars-Peter Clausen61bd55c2014-07-17 16:59:00 +01001108 buffer->scan_mask,
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001109 indio_dev->masklength) {
1110 in_ind = find_next_bit(indio_dev->active_scan_mask,
1111 indio_dev->masklength,
1112 in_ind + 1);
1113 while (in_ind != out_ind) {
1114 in_ind = find_next_bit(indio_dev->active_scan_mask,
1115 indio_dev->masklength,
1116 in_ind + 1);
1117 ch = iio_find_channel_from_si(indio_dev, in_ind);
Srinivas Pandruvada0ee85462014-04-29 00:51:00 +01001118 if (ch->scan_type.repeat > 1)
1119 length = ch->scan_type.storagebits / 8 *
1120 ch->scan_type.repeat;
1121 else
1122 length = ch->scan_type.storagebits / 8;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001123 /* Make sure we are aligned */
Lars-Peter Clausen61072db2014-07-17 16:59:00 +01001124 in_loc = roundup(in_loc, length) + length;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001125 }
1126 ch = iio_find_channel_from_si(indio_dev, in_ind);
Srinivas Pandruvada0ee85462014-04-29 00:51:00 +01001127 if (ch->scan_type.repeat > 1)
1128 length = ch->scan_type.storagebits / 8 *
1129 ch->scan_type.repeat;
1130 else
1131 length = ch->scan_type.storagebits / 8;
Lars-Peter Clausen61072db2014-07-17 16:59:00 +01001132 out_loc = roundup(out_loc, length);
1133 in_loc = roundup(in_loc, length);
Lars-Peter Clausencbe88bc2014-07-17 16:59:00 +01001134 ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
1135 if (ret)
1136 goto error_clear_mux_table;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001137 out_loc += length;
1138 in_loc += length;
1139 }
1140 /* Relies on scan_timestamp being last */
1141 if (buffer->scan_timestamp) {
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001142 ch = iio_find_channel_from_si(indio_dev,
Jonathan Cameronf1264802012-04-21 10:09:34 +01001143 indio_dev->scan_index_timestamp);
Srinivas Pandruvada0ee85462014-04-29 00:51:00 +01001144 if (ch->scan_type.repeat > 1)
1145 length = ch->scan_type.storagebits / 8 *
1146 ch->scan_type.repeat;
1147 else
1148 length = ch->scan_type.storagebits / 8;
Lars-Peter Clausen61072db2014-07-17 16:59:00 +01001149 out_loc = roundup(out_loc, length);
1150 in_loc = roundup(in_loc, length);
Lars-Peter Clausencbe88bc2014-07-17 16:59:00 +01001151 ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
1152 if (ret)
1153 goto error_clear_mux_table;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001154 out_loc += length;
1155 in_loc += length;
1156 }
1157 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
1158 if (buffer->demux_bounce == NULL) {
1159 ret = -ENOMEM;
1160 goto error_clear_mux_table;
1161 }
1162 return 0;
1163
1164error_clear_mux_table:
Jonathan Cameron842cd102012-04-21 10:09:45 +01001165 iio_buffer_demux_free(buffer);
1166
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001167 return ret;
1168}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001169
1170int iio_update_demux(struct iio_dev *indio_dev)
1171{
1172 struct iio_buffer *buffer;
1173 int ret;
1174
1175 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
1176 ret = iio_buffer_update_demux(indio_dev, buffer);
1177 if (ret < 0)
1178 goto error_clear_mux_table;
1179 }
1180 return 0;
1181
1182error_clear_mux_table:
1183 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
1184 iio_buffer_demux_free(buffer);
1185
1186 return ret;
1187}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001188EXPORT_SYMBOL_GPL(iio_update_demux);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +01001189
1190/**
1191 * iio_buffer_release() - Free a buffer's resources
1192 * @ref: Pointer to the kref embedded in the iio_buffer struct
1193 *
1194 * This function is called when the last reference to the buffer has been
1195 * dropped. It will typically free all resources allocated by the buffer. Do not
1196 * call this function manually, always use iio_buffer_put() when done using a
1197 * buffer.
1198 */
1199static void iio_buffer_release(struct kref *ref)
1200{
1201 struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
1202
1203 buffer->access->release(buffer);
1204}
1205
1206/**
1207 * iio_buffer_get() - Grab a reference to the buffer
1208 * @buffer: The buffer to grab a reference for, may be NULL
1209 *
1210 * Returns the pointer to the buffer that was passed into the function.
1211 */
1212struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
1213{
1214 if (buffer)
1215 kref_get(&buffer->ref);
1216
1217 return buffer;
1218}
1219EXPORT_SYMBOL_GPL(iio_buffer_get);
1220
1221/**
1222 * iio_buffer_put() - Release the reference to the buffer
1223 * @buffer: The buffer to release the reference for, may be NULL
1224 */
1225void iio_buffer_put(struct iio_buffer *buffer)
1226{
1227 if (buffer)
1228 kref_put(&buffer->ref, iio_buffer_release);
1229}
1230EXPORT_SYMBOL_GPL(iio_buffer_put);