blob: 4ca4c0a09923567e9804c55b4afe7c81d89c2599 [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
Lars-Peter Clausen647cc7b2013-11-25 14:56:00 +000040static bool iio_buffer_data_available(struct iio_buffer *buf)
41{
Josselin Costanzi9dd46942014-06-27 17:20:00 +010042 return buf->access->data_available(buf);
Lars-Peter Clausen647cc7b2013-11-25 14:56:00 +000043}
44
Jonathan Cameron14555b12011-09-21 11:15:57 +010045/**
46 * iio_buffer_read_first_n_outer() - chrdev read for buffer access
47 *
48 * This function relies on all buffer implementations having an
49 * iio_buffer as their first element.
50 **/
51ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
52 size_t n, loff_t *f_ps)
53{
54 struct iio_dev *indio_dev = filp->private_data;
55 struct iio_buffer *rb = indio_dev->buffer;
Lars-Peter Clausenee551a12013-11-25 14:56:00 +000056 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +010057
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +010058 if (!indio_dev->info)
59 return -ENODEV;
60
Jonathan Cameron96e00f12011-10-26 17:27:45 +010061 if (!rb || !rb->access->read_first_n)
Jonathan Cameron14555b12011-09-21 11:15:57 +010062 return -EINVAL;
Lars-Peter Clausenee551a12013-11-25 14:56:00 +000063
64 do {
65 if (!iio_buffer_data_available(rb)) {
66 if (filp->f_flags & O_NONBLOCK)
67 return -EAGAIN;
68
69 ret = wait_event_interruptible(rb->pollq,
70 iio_buffer_data_available(rb) ||
71 indio_dev->info == NULL);
72 if (ret)
73 return ret;
74 if (indio_dev->info == NULL)
75 return -ENODEV;
76 }
77
78 ret = rb->access->read_first_n(rb, n, buf);
79 if (ret == 0 && (filp->f_flags & O_NONBLOCK))
80 ret = -EAGAIN;
81 } while (ret == 0);
82
83 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +010084}
85
86/**
87 * iio_buffer_poll() - poll the buffer to find out if it has data
88 */
89unsigned int iio_buffer_poll(struct file *filp,
90 struct poll_table_struct *wait)
91{
92 struct iio_dev *indio_dev = filp->private_data;
93 struct iio_buffer *rb = indio_dev->buffer;
94
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +010095 if (!indio_dev->info)
96 return -ENODEV;
97
Jonathan Cameron14555b12011-09-21 11:15:57 +010098 poll_wait(filp, &rb->pollq, wait);
Lars-Peter Clausen647cc7b2013-11-25 14:56:00 +000099 if (iio_buffer_data_available(rb))
Jonathan Cameron14555b12011-09-21 11:15:57 +0100100 return POLLIN | POLLRDNORM;
101 /* need a way of knowing if there may be enough data... */
102 return 0;
103}
104
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100105/**
106 * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue
107 * @indio_dev: The IIO device
108 *
109 * Wakes up the event waitqueue used for poll(). Should usually
110 * be called when the device is unregistered.
111 */
112void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
113{
114 if (!indio_dev->buffer)
115 return;
116
117 wake_up(&indio_dev->buffer->pollq);
118}
119
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000120void iio_buffer_init(struct iio_buffer *buffer)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100121{
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000122 INIT_LIST_HEAD(&buffer->demux_list);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100123 INIT_LIST_HEAD(&buffer->buffer_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100124 init_waitqueue_head(&buffer->pollq);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100125 kref_init(&buffer->ref);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100126}
127EXPORT_SYMBOL(iio_buffer_init);
128
129static ssize_t iio_show_scan_index(struct device *dev,
130 struct device_attribute *attr,
131 char *buf)
132{
133 return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
134}
135
136static ssize_t iio_show_fixed_type(struct device *dev,
137 struct device_attribute *attr,
138 char *buf)
139{
140 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
141 u8 type = this_attr->c->scan_type.endianness;
142
143 if (type == IIO_CPU) {
Jonathan Cameron9d5d1152011-10-04 16:02:08 +0100144#ifdef __LITTLE_ENDIAN
145 type = IIO_LE;
146#else
147 type = IIO_BE;
148#endif
Jonathan Cameron14555b12011-09-21 11:15:57 +0100149 }
Srinivas Pandruvada0ee85462014-04-29 00:51:00 +0100150 if (this_attr->c->scan_type.repeat > 1)
151 return sprintf(buf, "%s:%c%d/%dX%d>>%u\n",
152 iio_endian_prefix[type],
153 this_attr->c->scan_type.sign,
154 this_attr->c->scan_type.realbits,
155 this_attr->c->scan_type.storagebits,
156 this_attr->c->scan_type.repeat,
157 this_attr->c->scan_type.shift);
158 else
159 return sprintf(buf, "%s:%c%d/%d>>%u\n",
Jonathan Cameron14555b12011-09-21 11:15:57 +0100160 iio_endian_prefix[type],
161 this_attr->c->scan_type.sign,
162 this_attr->c->scan_type.realbits,
163 this_attr->c->scan_type.storagebits,
164 this_attr->c->scan_type.shift);
165}
166
167static ssize_t iio_scan_el_show(struct device *dev,
168 struct device_attribute *attr,
169 char *buf)
170{
171 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200172 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100173
Alec Berg2076a202014-03-19 18:50:00 +0000174 /* Ensure ret is 0 or 1. */
175 ret = !!test_bit(to_iio_dev_attr(attr)->address,
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000176 indio_dev->buffer->scan_mask);
177
Jonathan Cameron14555b12011-09-21 11:15:57 +0100178 return sprintf(buf, "%d\n", ret);
179}
180
Lars-Peter Clausen217a5cf2014-11-26 18:55:09 +0100181/* Note NULL used as error indicator as it doesn't make sense. */
182static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
183 unsigned int masklength,
184 const unsigned long *mask)
185{
186 if (bitmap_empty(mask, masklength))
187 return NULL;
188 while (*av_masks) {
189 if (bitmap_subset(mask, av_masks, masklength))
190 return av_masks;
191 av_masks += BITS_TO_LONGS(masklength);
192 }
193 return NULL;
194}
195
196static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
197 const unsigned long *mask)
198{
199 if (!indio_dev->setup_ops->validate_scan_mask)
200 return true;
201
202 return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
203}
204
205/**
206 * iio_scan_mask_set() - set particular bit in the scan mask
207 * @indio_dev: the iio device
208 * @buffer: the buffer whose scan mask we are interested in
209 * @bit: the bit to be set.
210 *
211 * Note that at this point we have no way of knowing what other
212 * buffers might request, hence this code only verifies that the
213 * individual buffers request is plausible.
214 */
215static int iio_scan_mask_set(struct iio_dev *indio_dev,
216 struct iio_buffer *buffer, int bit)
217{
218 const unsigned long *mask;
219 unsigned long *trialmask;
220
221 trialmask = kmalloc(sizeof(*trialmask)*
222 BITS_TO_LONGS(indio_dev->masklength),
223 GFP_KERNEL);
224
225 if (trialmask == NULL)
226 return -ENOMEM;
227 if (!indio_dev->masklength) {
228 WARN_ON("Trying to set scanmask prior to registering buffer\n");
229 goto err_invalid_mask;
230 }
231 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
232 set_bit(bit, trialmask);
233
234 if (!iio_validate_scan_mask(indio_dev, trialmask))
235 goto err_invalid_mask;
236
237 if (indio_dev->available_scan_masks) {
238 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
239 indio_dev->masklength,
240 trialmask);
241 if (!mask)
242 goto err_invalid_mask;
243 }
244 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
245
246 kfree(trialmask);
247
248 return 0;
249
250err_invalid_mask:
251 kfree(trialmask);
252 return -EINVAL;
253}
254
Jonathan Cameron14555b12011-09-21 11:15:57 +0100255static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
256{
257 clear_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100258 return 0;
259}
260
261static ssize_t iio_scan_el_store(struct device *dev,
262 struct device_attribute *attr,
263 const char *buf,
264 size_t len)
265{
Jonathan Camerona714af22012-04-21 10:09:32 +0100266 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100267 bool state;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200268 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100269 struct iio_buffer *buffer = indio_dev->buffer;
270 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
271
Jonathan Camerona714af22012-04-21 10:09:32 +0100272 ret = strtobool(buf, &state);
273 if (ret < 0)
274 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100275 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100276 if (iio_buffer_is_active(indio_dev->buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100277 ret = -EBUSY;
278 goto error_ret;
279 }
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000280 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100281 if (ret < 0)
282 goto error_ret;
283 if (!state && ret) {
284 ret = iio_scan_mask_clear(buffer, this_attr->address);
285 if (ret)
286 goto error_ret;
287 } else if (state && !ret) {
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000288 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100289 if (ret)
290 goto error_ret;
291 }
292
293error_ret:
294 mutex_unlock(&indio_dev->mlock);
295
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100296 return ret < 0 ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100297
298}
299
300static ssize_t iio_scan_el_ts_show(struct device *dev,
301 struct device_attribute *attr,
302 char *buf)
303{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200304 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100305 return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100306}
307
308static ssize_t iio_scan_el_ts_store(struct device *dev,
309 struct device_attribute *attr,
310 const char *buf,
311 size_t len)
312{
Jonathan Camerona714af22012-04-21 10:09:32 +0100313 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200314 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100315 bool state;
316
Jonathan Camerona714af22012-04-21 10:09:32 +0100317 ret = strtobool(buf, &state);
318 if (ret < 0)
319 return ret;
320
Jonathan Cameron14555b12011-09-21 11:15:57 +0100321 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100322 if (iio_buffer_is_active(indio_dev->buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100323 ret = -EBUSY;
324 goto error_ret;
325 }
326 indio_dev->buffer->scan_timestamp = state;
327error_ret:
328 mutex_unlock(&indio_dev->mlock);
329
330 return ret ? ret : len;
331}
332
333static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
334 const struct iio_chan_spec *chan)
335{
336 int ret, attrcount = 0;
337 struct iio_buffer *buffer = indio_dev->buffer;
338
339 ret = __iio_add_chan_devattr("index",
340 chan,
341 &iio_show_scan_index,
342 NULL,
343 0,
Jonathan Cameron37044322013-09-08 14:57:00 +0100344 IIO_SEPARATE,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100345 &indio_dev->dev,
346 &buffer->scan_el_dev_attr_list);
347 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000348 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100349 attrcount++;
350 ret = __iio_add_chan_devattr("type",
351 chan,
352 &iio_show_fixed_type,
353 NULL,
354 0,
355 0,
356 &indio_dev->dev,
357 &buffer->scan_el_dev_attr_list);
358 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000359 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100360 attrcount++;
361 if (chan->type != IIO_TIMESTAMP)
362 ret = __iio_add_chan_devattr("en",
363 chan,
364 &iio_scan_el_show,
365 &iio_scan_el_store,
366 chan->scan_index,
367 0,
368 &indio_dev->dev,
369 &buffer->scan_el_dev_attr_list);
370 else
371 ret = __iio_add_chan_devattr("en",
372 chan,
373 &iio_scan_el_ts_show,
374 &iio_scan_el_ts_store,
375 chan->scan_index,
376 0,
377 &indio_dev->dev,
378 &buffer->scan_el_dev_attr_list);
Peter Meerwald95725882013-09-17 23:42:00 +0100379 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000380 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100381 attrcount++;
382 ret = attrcount;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100383 return ret;
384}
385
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100386static ssize_t iio_buffer_read_length(struct device *dev,
387 struct device_attribute *attr,
388 char *buf)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100389{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200390 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100391 struct iio_buffer *buffer = indio_dev->buffer;
392
393 if (buffer->access->get_length)
394 return sprintf(buf, "%d\n",
395 buffer->access->get_length(buffer));
396
397 return 0;
398}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100399
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100400static ssize_t iio_buffer_write_length(struct device *dev,
401 struct device_attribute *attr,
402 const char *buf, size_t len)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100403{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200404 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100405 struct iio_buffer *buffer = indio_dev->buffer;
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100406 unsigned int val;
407 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100408
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100409 ret = kstrtouint(buf, 10, &val);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100410 if (ret)
411 return ret;
412
413 if (buffer->access->get_length)
414 if (val == buffer->access->get_length(buffer))
415 return len;
416
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100417 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100418 if (iio_buffer_is_active(indio_dev->buffer)) {
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100419 ret = -EBUSY;
420 } else {
Lars-Peter Clausen8d92db22014-11-26 18:55:16 +0100421 buffer->access->set_length(buffer, val);
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100422 ret = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100423 }
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100424 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100425
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100426 return ret ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100427}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100428
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100429static ssize_t iio_buffer_show_enable(struct device *dev,
430 struct device_attribute *attr,
431 char *buf)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100432{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200433 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100434 return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
Jonathan Cameron14555b12011-09-21 11:15:57 +0100435}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100436
Peter Meerwald183f4172013-09-18 22:10:00 +0100437static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
438 const unsigned long *mask, bool timestamp)
Jonathan Cameron959d2952011-12-05 21:37:13 +0000439{
Jonathan Cameron959d2952011-12-05 21:37:13 +0000440 const struct iio_chan_spec *ch;
441 unsigned bytes = 0;
442 int length, i;
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100443
444 /* How much space will the demuxed element take? */
445 for_each_set_bit(i, mask,
446 indio_dev->masklength) {
447 ch = iio_find_channel_from_si(indio_dev, i);
Srinivas Pandruvada0ee85462014-04-29 00:51:00 +0100448 if (ch->scan_type.repeat > 1)
449 length = ch->scan_type.storagebits / 8 *
450 ch->scan_type.repeat;
451 else
452 length = ch->scan_type.storagebits / 8;
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100453 bytes = ALIGN(bytes, length);
454 bytes += length;
455 }
456 if (timestamp) {
457 ch = iio_find_channel_from_si(indio_dev,
Jonathan Cameronf1264802012-04-21 10:09:34 +0100458 indio_dev->scan_index_timestamp);
Srinivas Pandruvada0ee85462014-04-29 00:51:00 +0100459 if (ch->scan_type.repeat > 1)
460 length = ch->scan_type.storagebits / 8 *
461 ch->scan_type.repeat;
462 else
463 length = ch->scan_type.storagebits / 8;
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100464 bytes = ALIGN(bytes, length);
465 bytes += length;
466 }
467 return bytes;
468}
469
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100470static void iio_buffer_activate(struct iio_dev *indio_dev,
471 struct iio_buffer *buffer)
472{
473 iio_buffer_get(buffer);
474 list_add(&buffer->buffer_list, &indio_dev->buffer_list);
475}
476
477static void iio_buffer_deactivate(struct iio_buffer *buffer)
478{
479 list_del_init(&buffer->buffer_list);
480 iio_buffer_put(buffer);
481}
482
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100483void iio_disable_all_buffers(struct iio_dev *indio_dev)
484{
485 struct iio_buffer *buffer, *_buffer;
486
487 if (list_empty(&indio_dev->buffer_list))
488 return;
489
490 if (indio_dev->setup_ops->predisable)
491 indio_dev->setup_ops->predisable(indio_dev);
492
493 list_for_each_entry_safe(buffer, _buffer,
494 &indio_dev->buffer_list, buffer_list)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100495 iio_buffer_deactivate(buffer);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100496
497 indio_dev->currentmode = INDIO_DIRECT_MODE;
498 if (indio_dev->setup_ops->postdisable)
499 indio_dev->setup_ops->postdisable(indio_dev);
Lars-Peter Clausene086ed72013-10-15 09:38:00 +0100500
501 if (indio_dev->available_scan_masks == NULL)
502 kfree(indio_dev->active_scan_mask);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100503}
504
Lars-Peter Clausen8e050992013-10-14 17:49:00 +0100505static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
506 struct iio_buffer *buffer)
507{
508 unsigned int bytes;
509
510 if (!buffer->access->set_bytes_per_datum)
511 return;
512
513 bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
514 buffer->scan_timestamp);
515
516 buffer->access->set_bytes_per_datum(buffer, bytes);
517}
518
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100519static int __iio_update_buffers(struct iio_dev *indio_dev,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100520 struct iio_buffer *insert_buffer,
521 struct iio_buffer *remove_buffer)
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100522{
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100523 int ret;
524 int success = 0;
525 struct iio_buffer *buffer;
526 unsigned long *compound_mask;
527 const unsigned long *old_mask;
Jonathan Cameron959d2952011-12-05 21:37:13 +0000528
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100529 /* Wind down existing buffers - iff there are any */
530 if (!list_empty(&indio_dev->buffer_list)) {
531 if (indio_dev->setup_ops->predisable) {
532 ret = indio_dev->setup_ops->predisable(indio_dev);
533 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000534 return ret;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100535 }
536 indio_dev->currentmode = INDIO_DIRECT_MODE;
537 if (indio_dev->setup_ops->postdisable) {
538 ret = indio_dev->setup_ops->postdisable(indio_dev);
539 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000540 return ret;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100541 }
542 }
543 /* Keep a copy of current setup to allow roll back */
544 old_mask = indio_dev->active_scan_mask;
545 if (!indio_dev->available_scan_masks)
546 indio_dev->active_scan_mask = NULL;
547
548 if (remove_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100549 iio_buffer_deactivate(remove_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100550 if (insert_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100551 iio_buffer_activate(indio_dev, insert_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100552
553 /* If no buffers in list, we are done */
554 if (list_empty(&indio_dev->buffer_list)) {
555 indio_dev->currentmode = INDIO_DIRECT_MODE;
556 if (indio_dev->available_scan_masks == NULL)
557 kfree(old_mask);
558 return 0;
559 }
Jonathan Cameron959d2952011-12-05 21:37:13 +0000560
Peter Meerwald95725882013-09-17 23:42:00 +0100561 /* What scan mask do we actually have? */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100562 compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
563 sizeof(long), GFP_KERNEL);
564 if (compound_mask == NULL) {
565 if (indio_dev->available_scan_masks == NULL)
566 kfree(old_mask);
567 return -ENOMEM;
568 }
569 indio_dev->scan_timestamp = 0;
570
571 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
572 bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
573 indio_dev->masklength);
574 indio_dev->scan_timestamp |= buffer->scan_timestamp;
575 }
576 if (indio_dev->available_scan_masks) {
Jonathan Cameron959d2952011-12-05 21:37:13 +0000577 indio_dev->active_scan_mask =
578 iio_scan_mask_match(indio_dev->available_scan_masks,
579 indio_dev->masklength,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100580 compound_mask);
581 if (indio_dev->active_scan_mask == NULL) {
582 /*
583 * Roll back.
584 * Note can only occur when adding a buffer.
585 */
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100586 iio_buffer_deactivate(insert_buffer);
Peter Meerwaldd66e0452013-09-18 22:10:00 +0100587 if (old_mask) {
588 indio_dev->active_scan_mask = old_mask;
589 success = -EINVAL;
590 }
591 else {
592 kfree(compound_mask);
593 ret = -EINVAL;
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000594 return ret;
Peter Meerwaldd66e0452013-09-18 22:10:00 +0100595 }
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100596 }
597 } else {
598 indio_dev->active_scan_mask = compound_mask;
599 }
Lars-Peter Clausenaff1eb42012-06-15 18:08:59 +0200600
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000601 iio_update_demux(indio_dev);
602
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100603 /* Wind up again */
604 if (indio_dev->setup_ops->preenable) {
605 ret = indio_dev->setup_ops->preenable(indio_dev);
606 if (ret) {
607 printk(KERN_ERR
Michał Mirosławbec18892013-05-04 14:19:00 +0100608 "Buffer not started: buffer preenable failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100609 goto error_remove_inserted;
610 }
611 }
612 indio_dev->scan_bytes =
613 iio_compute_scan_bytes(indio_dev,
614 indio_dev->active_scan_mask,
615 indio_dev->scan_timestamp);
Lars-Peter Clausen8e050992013-10-14 17:49:00 +0100616 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
617 iio_buffer_update_bytes_per_datum(indio_dev, buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100618 if (buffer->access->request_update) {
619 ret = buffer->access->request_update(buffer);
620 if (ret) {
621 printk(KERN_INFO
Michał Mirosławbec18892013-05-04 14:19:00 +0100622 "Buffer not started: buffer parameter update failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100623 goto error_run_postdisable;
624 }
625 }
Lars-Peter Clausen8e050992013-10-14 17:49:00 +0100626 }
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100627 if (indio_dev->info->update_scan_mode) {
628 ret = indio_dev->info
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000629 ->update_scan_mode(indio_dev,
630 indio_dev->active_scan_mask);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100631 if (ret < 0) {
Michał Mirosławbec18892013-05-04 14:19:00 +0100632 printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100633 goto error_run_postdisable;
634 }
635 }
Peter Meerwald95725882013-09-17 23:42:00 +0100636 /* Definitely possible for devices to support both of these. */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100637 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
638 if (!indio_dev->trig) {
639 printk(KERN_INFO "Buffer not started: no trigger\n");
640 ret = -EINVAL;
641 /* Can only occur on first buffer */
642 goto error_run_postdisable;
643 }
644 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
645 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) {
646 indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
Peter Meerwald95725882013-09-17 23:42:00 +0100647 } else { /* Should never be reached */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100648 ret = -EINVAL;
649 goto error_run_postdisable;
650 }
651
652 if (indio_dev->setup_ops->postenable) {
653 ret = indio_dev->setup_ops->postenable(indio_dev);
654 if (ret) {
655 printk(KERN_INFO
Michał Mirosławbec18892013-05-04 14:19:00 +0100656 "Buffer not started: postenable failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100657 indio_dev->currentmode = INDIO_DIRECT_MODE;
658 if (indio_dev->setup_ops->postdisable)
659 indio_dev->setup_ops->postdisable(indio_dev);
660 goto error_disable_all_buffers;
661 }
662 }
663
664 if (indio_dev->available_scan_masks)
665 kfree(compound_mask);
666 else
667 kfree(old_mask);
668
669 return success;
670
671error_disable_all_buffers:
672 indio_dev->currentmode = INDIO_DIRECT_MODE;
673error_run_postdisable:
674 if (indio_dev->setup_ops->postdisable)
675 indio_dev->setup_ops->postdisable(indio_dev);
676error_remove_inserted:
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100677 if (insert_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100678 iio_buffer_deactivate(insert_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100679 indio_dev->active_scan_mask = old_mask;
680 kfree(compound_mask);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100681 return ret;
682}
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100683
684int iio_update_buffers(struct iio_dev *indio_dev,
685 struct iio_buffer *insert_buffer,
686 struct iio_buffer *remove_buffer)
687{
688 int ret;
689
Lars-Peter Clausen3909fab2013-10-04 12:07:00 +0100690 if (insert_buffer == remove_buffer)
691 return 0;
692
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100693 mutex_lock(&indio_dev->info_exist_lock);
694 mutex_lock(&indio_dev->mlock);
695
Lars-Peter Clausen3909fab2013-10-04 12:07:00 +0100696 if (insert_buffer && iio_buffer_is_active(insert_buffer))
697 insert_buffer = NULL;
698
699 if (remove_buffer && !iio_buffer_is_active(remove_buffer))
700 remove_buffer = NULL;
701
702 if (!insert_buffer && !remove_buffer) {
703 ret = 0;
704 goto out_unlock;
705 }
706
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100707 if (indio_dev->info == NULL) {
708 ret = -ENODEV;
709 goto out_unlock;
710 }
711
712 ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
713
714out_unlock:
715 mutex_unlock(&indio_dev->mlock);
716 mutex_unlock(&indio_dev->info_exist_lock);
717
718 return ret;
719}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100720EXPORT_SYMBOL_GPL(iio_update_buffers);
721
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100722static ssize_t iio_buffer_store_enable(struct device *dev,
723 struct device_attribute *attr,
724 const char *buf,
725 size_t len)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100726{
727 int ret;
728 bool requested_state;
729 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100730 bool inlist;
731
732 ret = strtobool(buf, &requested_state);
733 if (ret < 0)
734 return ret;
735
736 mutex_lock(&indio_dev->mlock);
737
738 /* Find out if it is in the list */
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100739 inlist = iio_buffer_is_active(indio_dev->buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100740 /* Already in desired state */
741 if (inlist == requested_state)
742 goto done;
743
744 if (requested_state)
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100745 ret = __iio_update_buffers(indio_dev,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100746 indio_dev->buffer, NULL);
747 else
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100748 ret = __iio_update_buffers(indio_dev,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100749 NULL, indio_dev->buffer);
750
751 if (ret < 0)
752 goto done;
753done:
754 mutex_unlock(&indio_dev->mlock);
755 return (ret < 0) ? ret : len;
756}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100757
Lars-Peter Clausend967cb62014-11-26 18:55:14 +0100758static const char * const iio_scan_elements_group_name = "scan_elements";
759
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100760static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
761 iio_buffer_write_length);
Lars-Peter Clausen8d92db22014-11-26 18:55:16 +0100762static struct device_attribute dev_attr_length_ro = __ATTR(length,
763 S_IRUGO, iio_buffer_read_length, NULL);
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100764static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
765 iio_buffer_show_enable, iio_buffer_store_enable);
766
Lars-Peter Clausend967cb62014-11-26 18:55:14 +0100767int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
768{
769 struct iio_dev_attr *p;
770 struct attribute **attr;
771 struct iio_buffer *buffer = indio_dev->buffer;
772 int ret, i, attrn, attrcount, attrcount_orig = 0;
773 const struct iio_chan_spec *channels;
774
775 if (!buffer)
776 return 0;
777
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100778 attrcount = 0;
779 if (buffer->attrs) {
780 while (buffer->attrs[attrcount] != NULL)
781 attrcount++;
782 }
783
784 buffer->buffer_group.name = "buffer";
785 buffer->buffer_group.attrs = kcalloc(attrcount + 3,
786 sizeof(*buffer->buffer_group.attrs), GFP_KERNEL);
787 if (!buffer->buffer_group.attrs)
788 return -ENOMEM;
789
Lars-Peter Clausen8d92db22014-11-26 18:55:16 +0100790 if (buffer->access->set_length)
791 buffer->buffer_group.attrs[0] = &dev_attr_length.attr;
792 else
793 buffer->buffer_group.attrs[0] = &dev_attr_length_ro.attr;
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100794 buffer->buffer_group.attrs[1] = &dev_attr_enable.attr;
795 if (buffer->attrs)
796 memcpy(&buffer->buffer_group.attrs[2], buffer->attrs,
797 sizeof(*&buffer->buffer_group.attrs) * (attrcount - 2));
798 buffer->buffer_group.attrs[attrcount+2] = NULL;
799
800 indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group;
801
Lars-Peter Clausend967cb62014-11-26 18:55:14 +0100802 if (buffer->scan_el_attrs != NULL) {
803 attr = buffer->scan_el_attrs->attrs;
804 while (*attr++ != NULL)
805 attrcount_orig++;
806 }
807 attrcount = attrcount_orig;
808 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
809 channels = indio_dev->channels;
810 if (channels) {
811 /* new magic */
812 for (i = 0; i < indio_dev->num_channels; i++) {
813 if (channels[i].scan_index < 0)
814 continue;
815
816 /* Establish necessary mask length */
817 if (channels[i].scan_index >
818 (int)indio_dev->masklength - 1)
819 indio_dev->masklength
820 = channels[i].scan_index + 1;
821
822 ret = iio_buffer_add_channel_sysfs(indio_dev,
823 &channels[i]);
824 if (ret < 0)
825 goto error_cleanup_dynamic;
826 attrcount += ret;
827 if (channels[i].type == IIO_TIMESTAMP)
828 indio_dev->scan_index_timestamp =
829 channels[i].scan_index;
830 }
831 if (indio_dev->masklength && buffer->scan_mask == NULL) {
832 buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
833 sizeof(*buffer->scan_mask),
834 GFP_KERNEL);
835 if (buffer->scan_mask == NULL) {
836 ret = -ENOMEM;
837 goto error_cleanup_dynamic;
838 }
839 }
840 }
841
842 buffer->scan_el_group.name = iio_scan_elements_group_name;
843
844 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
845 sizeof(buffer->scan_el_group.attrs[0]),
846 GFP_KERNEL);
847 if (buffer->scan_el_group.attrs == NULL) {
848 ret = -ENOMEM;
849 goto error_free_scan_mask;
850 }
851 if (buffer->scan_el_attrs)
852 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
853 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
854 attrn = attrcount_orig;
855
856 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
857 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
858 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
859
860 return 0;
861
862error_free_scan_mask:
863 kfree(buffer->scan_mask);
864error_cleanup_dynamic:
865 iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100866 kfree(indio_dev->buffer->buffer_group.attrs);
Lars-Peter Clausend967cb62014-11-26 18:55:14 +0100867
868 return ret;
869}
870
871void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
872{
873 if (!indio_dev->buffer)
874 return;
875
876 kfree(indio_dev->buffer->scan_mask);
Lars-Peter Clausen08e7e0a2014-11-26 18:55:15 +0100877 kfree(indio_dev->buffer->buffer_group.attrs);
Lars-Peter Clausend967cb62014-11-26 18:55:14 +0100878 kfree(indio_dev->buffer->scan_el_group.attrs);
879 iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
880}
881
Jonathan Cameron14555b12011-09-21 11:15:57 +0100882/**
Lars-Peter Clausen81636632012-07-09 10:00:00 +0100883 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
884 * @indio_dev: the iio device
885 * @mask: scan mask to be checked
886 *
887 * Return true if exactly one bit is set in the scan mask, false otherwise. It
888 * can be used for devices where only one channel can be active for sampling at
889 * a time.
890 */
891bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
892 const unsigned long *mask)
893{
894 return bitmap_weight(mask, indio_dev->masklength) == 1;
895}
896EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
897
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000898int iio_scan_mask_query(struct iio_dev *indio_dev,
899 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100900{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100901 if (bit > indio_dev->masklength)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100902 return -EINVAL;
903
904 if (!buffer->scan_mask)
905 return 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100906
Alec Berg2076a202014-03-19 18:50:00 +0000907 /* Ensure return value is 0 or 1. */
908 return !!test_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100909};
910EXPORT_SYMBOL_GPL(iio_scan_mask_query);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000911
912/**
913 * struct iio_demux_table() - table describing demux memcpy ops
914 * @from: index to copy from
Peter Meerwald99698b42012-08-26 13:43:00 +0100915 * @to: index to copy to
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000916 * @length: how many bytes to copy
917 * @l: list head used for management
918 */
919struct iio_demux_table {
920 unsigned from;
921 unsigned to;
922 unsigned length;
923 struct list_head l;
924};
925
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100926static const void *iio_demux(struct iio_buffer *buffer,
927 const void *datain)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000928{
929 struct iio_demux_table *t;
930
931 if (list_empty(&buffer->demux_list))
932 return datain;
933 list_for_each_entry(t, &buffer->demux_list, l)
934 memcpy(buffer->demux_bounce + t->to,
935 datain + t->from, t->length);
936
937 return buffer->demux_bounce;
938}
939
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100940static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000941{
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100942 const void *dataout = iio_demux(buffer, data);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000943
Lars-Peter Clausence56ade2012-09-04 13:38:00 +0100944 return buffer->access->store_to(buffer, dataout);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000945}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000946
Jonathan Cameron842cd102012-04-21 10:09:45 +0100947static void iio_buffer_demux_free(struct iio_buffer *buffer)
948{
949 struct iio_demux_table *p, *q;
950 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
951 list_del(&p->l);
952 kfree(p);
953 }
954}
955
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100956
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100957int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100958{
959 int ret;
960 struct iio_buffer *buf;
961
962 list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
963 ret = iio_push_to_buffer(buf, data);
964 if (ret < 0)
965 return ret;
966 }
967
968 return 0;
969}
970EXPORT_SYMBOL_GPL(iio_push_to_buffers);
971
Lars-Peter Clausencbe88bc2014-07-17 16:59:00 +0100972static int iio_buffer_add_demux(struct iio_buffer *buffer,
973 struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc,
974 unsigned int length)
975{
976
977 if (*p && (*p)->from + (*p)->length == in_loc &&
978 (*p)->to + (*p)->length == out_loc) {
979 (*p)->length += length;
980 } else {
Jonathan Cameron7cdca1782014-08-08 09:43:00 +0100981 *p = kmalloc(sizeof(**p), GFP_KERNEL);
Lars-Peter Clausencbe88bc2014-07-17 16:59:00 +0100982 if (*p == NULL)
983 return -ENOMEM;
984 (*p)->from = in_loc;
985 (*p)->to = out_loc;
986 (*p)->length = length;
987 list_add_tail(&(*p)->l, &buffer->demux_list);
988 }
989
990 return 0;
991}
992
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100993static int iio_buffer_update_demux(struct iio_dev *indio_dev,
994 struct iio_buffer *buffer)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000995{
996 const struct iio_chan_spec *ch;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000997 int ret, in_ind = -1, out_ind, length;
998 unsigned in_loc = 0, out_loc = 0;
Lars-Peter Clausencbe88bc2014-07-17 16:59:00 +0100999 struct iio_demux_table *p = NULL;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001000
1001 /* Clear out any old demux */
Jonathan Cameron842cd102012-04-21 10:09:45 +01001002 iio_buffer_demux_free(buffer);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001003 kfree(buffer->demux_bounce);
1004 buffer->demux_bounce = NULL;
1005
1006 /* First work out which scan mode we will actually have */
1007 if (bitmap_equal(indio_dev->active_scan_mask,
1008 buffer->scan_mask,
1009 indio_dev->masklength))
1010 return 0;
1011
1012 /* Now we have the two masks, work from least sig and build up sizes */
1013 for_each_set_bit(out_ind,
Lars-Peter Clausen61bd55c2014-07-17 16:59:00 +01001014 buffer->scan_mask,
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001015 indio_dev->masklength) {
1016 in_ind = find_next_bit(indio_dev->active_scan_mask,
1017 indio_dev->masklength,
1018 in_ind + 1);
1019 while (in_ind != out_ind) {
1020 in_ind = find_next_bit(indio_dev->active_scan_mask,
1021 indio_dev->masklength,
1022 in_ind + 1);
1023 ch = iio_find_channel_from_si(indio_dev, in_ind);
Srinivas Pandruvada0ee85462014-04-29 00:51:00 +01001024 if (ch->scan_type.repeat > 1)
1025 length = ch->scan_type.storagebits / 8 *
1026 ch->scan_type.repeat;
1027 else
1028 length = ch->scan_type.storagebits / 8;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001029 /* Make sure we are aligned */
Lars-Peter Clausen61072db2014-07-17 16:59:00 +01001030 in_loc = roundup(in_loc, length) + length;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001031 }
1032 ch = iio_find_channel_from_si(indio_dev, in_ind);
Srinivas Pandruvada0ee85462014-04-29 00:51:00 +01001033 if (ch->scan_type.repeat > 1)
1034 length = ch->scan_type.storagebits / 8 *
1035 ch->scan_type.repeat;
1036 else
1037 length = ch->scan_type.storagebits / 8;
Lars-Peter Clausen61072db2014-07-17 16:59:00 +01001038 out_loc = roundup(out_loc, length);
1039 in_loc = roundup(in_loc, length);
Lars-Peter Clausencbe88bc2014-07-17 16:59:00 +01001040 ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
1041 if (ret)
1042 goto error_clear_mux_table;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001043 out_loc += length;
1044 in_loc += length;
1045 }
1046 /* Relies on scan_timestamp being last */
1047 if (buffer->scan_timestamp) {
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001048 ch = iio_find_channel_from_si(indio_dev,
Jonathan Cameronf1264802012-04-21 10:09:34 +01001049 indio_dev->scan_index_timestamp);
Srinivas Pandruvada0ee85462014-04-29 00:51:00 +01001050 if (ch->scan_type.repeat > 1)
1051 length = ch->scan_type.storagebits / 8 *
1052 ch->scan_type.repeat;
1053 else
1054 length = ch->scan_type.storagebits / 8;
Lars-Peter Clausen61072db2014-07-17 16:59:00 +01001055 out_loc = roundup(out_loc, length);
1056 in_loc = roundup(in_loc, length);
Lars-Peter Clausencbe88bc2014-07-17 16:59:00 +01001057 ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
1058 if (ret)
1059 goto error_clear_mux_table;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001060 out_loc += length;
1061 in_loc += length;
1062 }
1063 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
1064 if (buffer->demux_bounce == NULL) {
1065 ret = -ENOMEM;
1066 goto error_clear_mux_table;
1067 }
1068 return 0;
1069
1070error_clear_mux_table:
Jonathan Cameron842cd102012-04-21 10:09:45 +01001071 iio_buffer_demux_free(buffer);
1072
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001073 return ret;
1074}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001075
1076int iio_update_demux(struct iio_dev *indio_dev)
1077{
1078 struct iio_buffer *buffer;
1079 int ret;
1080
1081 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
1082 ret = iio_buffer_update_demux(indio_dev, buffer);
1083 if (ret < 0)
1084 goto error_clear_mux_table;
1085 }
1086 return 0;
1087
1088error_clear_mux_table:
1089 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
1090 iio_buffer_demux_free(buffer);
1091
1092 return ret;
1093}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001094EXPORT_SYMBOL_GPL(iio_update_demux);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +01001095
1096/**
1097 * iio_buffer_release() - Free a buffer's resources
1098 * @ref: Pointer to the kref embedded in the iio_buffer struct
1099 *
1100 * This function is called when the last reference to the buffer has been
1101 * dropped. It will typically free all resources allocated by the buffer. Do not
1102 * call this function manually, always use iio_buffer_put() when done using a
1103 * buffer.
1104 */
1105static void iio_buffer_release(struct kref *ref)
1106{
1107 struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
1108
1109 buffer->access->release(buffer);
1110}
1111
1112/**
1113 * iio_buffer_get() - Grab a reference to the buffer
1114 * @buffer: The buffer to grab a reference for, may be NULL
1115 *
1116 * Returns the pointer to the buffer that was passed into the function.
1117 */
1118struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
1119{
1120 if (buffer)
1121 kref_get(&buffer->ref);
1122
1123 return buffer;
1124}
1125EXPORT_SYMBOL_GPL(iio_buffer_get);
1126
1127/**
1128 * iio_buffer_put() - Release the reference to the buffer
1129 * @buffer: The buffer to release the reference for, may be NULL
1130 */
1131void iio_buffer_put(struct iio_buffer *buffer)
1132{
1133 if (buffer)
1134 kref_put(&buffer->ref, iio_buffer_release);
1135}
1136EXPORT_SYMBOL_GPL(iio_buffer_put);