blob: fe25042f056a9844e8662fd19b0f0f02c85b3e6e [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{
42 if (buf->access->data_available)
43 return buf->access->data_available(buf);
44
45 return buf->stufftoread;
46}
47
Jonathan Cameron14555b12011-09-21 11:15:57 +010048/**
49 * iio_buffer_read_first_n_outer() - chrdev read for buffer access
50 *
51 * This function relies on all buffer implementations having an
52 * iio_buffer as their first element.
53 **/
54ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
55 size_t n, loff_t *f_ps)
56{
57 struct iio_dev *indio_dev = filp->private_data;
58 struct iio_buffer *rb = indio_dev->buffer;
Lars-Peter Clausenee551a12013-11-25 14:56:00 +000059 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +010060
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +010061 if (!indio_dev->info)
62 return -ENODEV;
63
Jonathan Cameron96e00f12011-10-26 17:27:45 +010064 if (!rb || !rb->access->read_first_n)
Jonathan Cameron14555b12011-09-21 11:15:57 +010065 return -EINVAL;
Lars-Peter Clausenee551a12013-11-25 14:56:00 +000066
67 do {
68 if (!iio_buffer_data_available(rb)) {
69 if (filp->f_flags & O_NONBLOCK)
70 return -EAGAIN;
71
72 ret = wait_event_interruptible(rb->pollq,
73 iio_buffer_data_available(rb) ||
74 indio_dev->info == NULL);
75 if (ret)
76 return ret;
77 if (indio_dev->info == NULL)
78 return -ENODEV;
79 }
80
81 ret = rb->access->read_first_n(rb, n, buf);
82 if (ret == 0 && (filp->f_flags & O_NONBLOCK))
83 ret = -EAGAIN;
84 } while (ret == 0);
85
86 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +010087}
88
89/**
90 * iio_buffer_poll() - poll the buffer to find out if it has data
91 */
92unsigned int iio_buffer_poll(struct file *filp,
93 struct poll_table_struct *wait)
94{
95 struct iio_dev *indio_dev = filp->private_data;
96 struct iio_buffer *rb = indio_dev->buffer;
97
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +010098 if (!indio_dev->info)
99 return -ENODEV;
100
Jonathan Cameron14555b12011-09-21 11:15:57 +0100101 poll_wait(filp, &rb->pollq, wait);
Lars-Peter Clausen647cc7b2013-11-25 14:56:00 +0000102 if (iio_buffer_data_available(rb))
Jonathan Cameron14555b12011-09-21 11:15:57 +0100103 return POLLIN | POLLRDNORM;
104 /* need a way of knowing if there may be enough data... */
105 return 0;
106}
107
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100108/**
109 * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue
110 * @indio_dev: The IIO device
111 *
112 * Wakes up the event waitqueue used for poll(). Should usually
113 * be called when the device is unregistered.
114 */
115void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
116{
117 if (!indio_dev->buffer)
118 return;
119
120 wake_up(&indio_dev->buffer->pollq);
121}
122
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000123void iio_buffer_init(struct iio_buffer *buffer)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100124{
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000125 INIT_LIST_HEAD(&buffer->demux_list);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100126 INIT_LIST_HEAD(&buffer->buffer_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100127 init_waitqueue_head(&buffer->pollq);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100128 kref_init(&buffer->ref);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100129}
130EXPORT_SYMBOL(iio_buffer_init);
131
132static ssize_t iio_show_scan_index(struct device *dev,
133 struct device_attribute *attr,
134 char *buf)
135{
136 return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
137}
138
139static ssize_t iio_show_fixed_type(struct device *dev,
140 struct device_attribute *attr,
141 char *buf)
142{
143 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
144 u8 type = this_attr->c->scan_type.endianness;
145
146 if (type == IIO_CPU) {
Jonathan Cameron9d5d1152011-10-04 16:02:08 +0100147#ifdef __LITTLE_ENDIAN
148 type = IIO_LE;
149#else
150 type = IIO_BE;
151#endif
Jonathan Cameron14555b12011-09-21 11:15:57 +0100152 }
153 return sprintf(buf, "%s:%c%d/%d>>%u\n",
154 iio_endian_prefix[type],
155 this_attr->c->scan_type.sign,
156 this_attr->c->scan_type.realbits,
157 this_attr->c->scan_type.storagebits,
158 this_attr->c->scan_type.shift);
159}
160
161static ssize_t iio_scan_el_show(struct device *dev,
162 struct device_attribute *attr,
163 char *buf)
164{
165 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200166 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100167
Alec Berg2076a202014-03-19 18:50:00 +0000168 /* Ensure ret is 0 or 1. */
169 ret = !!test_bit(to_iio_dev_attr(attr)->address,
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000170 indio_dev->buffer->scan_mask);
171
Jonathan Cameron14555b12011-09-21 11:15:57 +0100172 return sprintf(buf, "%d\n", ret);
173}
174
175static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
176{
177 clear_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100178 return 0;
179}
180
181static ssize_t iio_scan_el_store(struct device *dev,
182 struct device_attribute *attr,
183 const char *buf,
184 size_t len)
185{
Jonathan Camerona714af22012-04-21 10:09:32 +0100186 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100187 bool state;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200188 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100189 struct iio_buffer *buffer = indio_dev->buffer;
190 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
191
Jonathan Camerona714af22012-04-21 10:09:32 +0100192 ret = strtobool(buf, &state);
193 if (ret < 0)
194 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100195 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100196 if (iio_buffer_is_active(indio_dev->buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100197 ret = -EBUSY;
198 goto error_ret;
199 }
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000200 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100201 if (ret < 0)
202 goto error_ret;
203 if (!state && ret) {
204 ret = iio_scan_mask_clear(buffer, this_attr->address);
205 if (ret)
206 goto error_ret;
207 } else if (state && !ret) {
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000208 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100209 if (ret)
210 goto error_ret;
211 }
212
213error_ret:
214 mutex_unlock(&indio_dev->mlock);
215
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100216 return ret < 0 ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100217
218}
219
220static ssize_t iio_scan_el_ts_show(struct device *dev,
221 struct device_attribute *attr,
222 char *buf)
223{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200224 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100225 return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100226}
227
228static ssize_t iio_scan_el_ts_store(struct device *dev,
229 struct device_attribute *attr,
230 const char *buf,
231 size_t len)
232{
Jonathan Camerona714af22012-04-21 10:09:32 +0100233 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200234 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100235 bool state;
236
Jonathan Camerona714af22012-04-21 10:09:32 +0100237 ret = strtobool(buf, &state);
238 if (ret < 0)
239 return ret;
240
Jonathan Cameron14555b12011-09-21 11:15:57 +0100241 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100242 if (iio_buffer_is_active(indio_dev->buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100243 ret = -EBUSY;
244 goto error_ret;
245 }
246 indio_dev->buffer->scan_timestamp = state;
247error_ret:
248 mutex_unlock(&indio_dev->mlock);
249
250 return ret ? ret : len;
251}
252
253static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
254 const struct iio_chan_spec *chan)
255{
256 int ret, attrcount = 0;
257 struct iio_buffer *buffer = indio_dev->buffer;
258
259 ret = __iio_add_chan_devattr("index",
260 chan,
261 &iio_show_scan_index,
262 NULL,
263 0,
Jonathan Cameron37044322013-09-08 14:57:00 +0100264 IIO_SEPARATE,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100265 &indio_dev->dev,
266 &buffer->scan_el_dev_attr_list);
267 if (ret)
268 goto error_ret;
269 attrcount++;
270 ret = __iio_add_chan_devattr("type",
271 chan,
272 &iio_show_fixed_type,
273 NULL,
274 0,
275 0,
276 &indio_dev->dev,
277 &buffer->scan_el_dev_attr_list);
278 if (ret)
279 goto error_ret;
280 attrcount++;
281 if (chan->type != IIO_TIMESTAMP)
282 ret = __iio_add_chan_devattr("en",
283 chan,
284 &iio_scan_el_show,
285 &iio_scan_el_store,
286 chan->scan_index,
287 0,
288 &indio_dev->dev,
289 &buffer->scan_el_dev_attr_list);
290 else
291 ret = __iio_add_chan_devattr("en",
292 chan,
293 &iio_scan_el_ts_show,
294 &iio_scan_el_ts_store,
295 chan->scan_index,
296 0,
297 &indio_dev->dev,
298 &buffer->scan_el_dev_attr_list);
Peter Meerwald95725882013-09-17 23:42:00 +0100299 if (ret)
300 goto error_ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100301 attrcount++;
302 ret = attrcount;
303error_ret:
304 return ret;
305}
306
Jonathan Cameron14555b12011-09-21 11:15:57 +0100307static const char * const iio_scan_elements_group_name = "scan_elements";
308
309int iio_buffer_register(struct iio_dev *indio_dev,
310 const struct iio_chan_spec *channels,
311 int num_channels)
312{
313 struct iio_dev_attr *p;
314 struct attribute **attr;
315 struct iio_buffer *buffer = indio_dev->buffer;
316 int ret, i, attrn, attrcount, attrcount_orig = 0;
317
318 if (buffer->attrs)
319 indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs;
320
321 if (buffer->scan_el_attrs != NULL) {
322 attr = buffer->scan_el_attrs->attrs;
323 while (*attr++ != NULL)
324 attrcount_orig++;
325 }
326 attrcount = attrcount_orig;
327 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
328 if (channels) {
329 /* new magic */
330 for (i = 0; i < num_channels; i++) {
Lars-Peter Clausenf5b81dd2012-06-18 18:33:47 +0200331 if (channels[i].scan_index < 0)
332 continue;
333
Jonathan Cameron14555b12011-09-21 11:15:57 +0100334 /* Establish necessary mask length */
335 if (channels[i].scan_index >
336 (int)indio_dev->masklength - 1)
337 indio_dev->masklength
Lars-Peter Clausene1dc7be2012-07-02 14:52:56 +0200338 = channels[i].scan_index + 1;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100339
340 ret = iio_buffer_add_channel_sysfs(indio_dev,
341 &channels[i]);
342 if (ret < 0)
343 goto error_cleanup_dynamic;
344 attrcount += ret;
Jonathan Cameronbeb80602011-12-05 21:37:11 +0000345 if (channels[i].type == IIO_TIMESTAMP)
Jonathan Cameronf1264802012-04-21 10:09:34 +0100346 indio_dev->scan_index_timestamp =
Jonathan Cameronbeb80602011-12-05 21:37:11 +0000347 channels[i].scan_index;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100348 }
349 if (indio_dev->masklength && buffer->scan_mask == NULL) {
Thomas Meyerd83fb182011-11-29 22:08:00 +0100350 buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
351 sizeof(*buffer->scan_mask),
352 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100353 if (buffer->scan_mask == NULL) {
354 ret = -ENOMEM;
355 goto error_cleanup_dynamic;
356 }
357 }
358 }
359
360 buffer->scan_el_group.name = iio_scan_elements_group_name;
361
Thomas Meyerd83fb182011-11-29 22:08:00 +0100362 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
363 sizeof(buffer->scan_el_group.attrs[0]),
364 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100365 if (buffer->scan_el_group.attrs == NULL) {
366 ret = -ENOMEM;
367 goto error_free_scan_mask;
368 }
369 if (buffer->scan_el_attrs)
370 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
371 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
372 attrn = attrcount_orig;
373
374 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
375 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
376 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
377
378 return 0;
379
380error_free_scan_mask:
381 kfree(buffer->scan_mask);
382error_cleanup_dynamic:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100383 iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100384
385 return ret;
386}
387EXPORT_SYMBOL(iio_buffer_register);
388
389void iio_buffer_unregister(struct iio_dev *indio_dev)
390{
391 kfree(indio_dev->buffer->scan_mask);
392 kfree(indio_dev->buffer->scan_el_group.attrs);
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100393 iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100394}
395EXPORT_SYMBOL(iio_buffer_unregister);
396
397ssize_t iio_buffer_read_length(struct device *dev,
398 struct device_attribute *attr,
399 char *buf)
400{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200401 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100402 struct iio_buffer *buffer = indio_dev->buffer;
403
404 if (buffer->access->get_length)
405 return sprintf(buf, "%d\n",
406 buffer->access->get_length(buffer));
407
408 return 0;
409}
410EXPORT_SYMBOL(iio_buffer_read_length);
411
412ssize_t iio_buffer_write_length(struct device *dev,
413 struct device_attribute *attr,
414 const char *buf,
415 size_t len)
416{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200417 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100418 struct iio_buffer *buffer = indio_dev->buffer;
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100419 unsigned int val;
420 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100421
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100422 ret = kstrtouint(buf, 10, &val);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100423 if (ret)
424 return ret;
425
426 if (buffer->access->get_length)
427 if (val == buffer->access->get_length(buffer))
428 return len;
429
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100430 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100431 if (iio_buffer_is_active(indio_dev->buffer)) {
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100432 ret = -EBUSY;
433 } else {
Lars-Peter Clausen869871b2011-12-19 15:23:48 +0100434 if (buffer->access->set_length)
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100435 buffer->access->set_length(buffer, val);
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100436 ret = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100437 }
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100438 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100439
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100440 return ret ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100441}
442EXPORT_SYMBOL(iio_buffer_write_length);
443
Jonathan Cameron14555b12011-09-21 11:15:57 +0100444ssize_t iio_buffer_show_enable(struct device *dev,
445 struct device_attribute *attr,
446 char *buf)
447{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200448 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100449 return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
Jonathan Cameron14555b12011-09-21 11:15:57 +0100450}
451EXPORT_SYMBOL(iio_buffer_show_enable);
452
Peter Meerwald95725882013-09-17 23:42:00 +0100453/* Note NULL used as error indicator as it doesn't make sense. */
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100454static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100455 unsigned int masklength,
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100456 const unsigned long *mask)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100457{
458 if (bitmap_empty(mask, masklength))
459 return NULL;
460 while (*av_masks) {
461 if (bitmap_subset(mask, av_masks, masklength))
462 return av_masks;
463 av_masks += BITS_TO_LONGS(masklength);
464 }
465 return NULL;
466}
467
Peter Meerwald183f4172013-09-18 22:10:00 +0100468static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
469 const unsigned long *mask, bool timestamp)
Jonathan Cameron959d2952011-12-05 21:37:13 +0000470{
Jonathan Cameron959d2952011-12-05 21:37:13 +0000471 const struct iio_chan_spec *ch;
472 unsigned bytes = 0;
473 int length, i;
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100474
475 /* How much space will the demuxed element take? */
476 for_each_set_bit(i, mask,
477 indio_dev->masklength) {
478 ch = iio_find_channel_from_si(indio_dev, i);
479 length = ch->scan_type.storagebits / 8;
480 bytes = ALIGN(bytes, length);
481 bytes += length;
482 }
483 if (timestamp) {
484 ch = iio_find_channel_from_si(indio_dev,
Jonathan Cameronf1264802012-04-21 10:09:34 +0100485 indio_dev->scan_index_timestamp);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100486 length = ch->scan_type.storagebits / 8;
487 bytes = ALIGN(bytes, length);
488 bytes += length;
489 }
490 return bytes;
491}
492
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100493static void iio_buffer_activate(struct iio_dev *indio_dev,
494 struct iio_buffer *buffer)
495{
496 iio_buffer_get(buffer);
497 list_add(&buffer->buffer_list, &indio_dev->buffer_list);
498}
499
500static void iio_buffer_deactivate(struct iio_buffer *buffer)
501{
502 list_del_init(&buffer->buffer_list);
503 iio_buffer_put(buffer);
504}
505
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100506void iio_disable_all_buffers(struct iio_dev *indio_dev)
507{
508 struct iio_buffer *buffer, *_buffer;
509
510 if (list_empty(&indio_dev->buffer_list))
511 return;
512
513 if (indio_dev->setup_ops->predisable)
514 indio_dev->setup_ops->predisable(indio_dev);
515
516 list_for_each_entry_safe(buffer, _buffer,
517 &indio_dev->buffer_list, buffer_list)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100518 iio_buffer_deactivate(buffer);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100519
520 indio_dev->currentmode = INDIO_DIRECT_MODE;
521 if (indio_dev->setup_ops->postdisable)
522 indio_dev->setup_ops->postdisable(indio_dev);
Lars-Peter Clausene086ed72013-10-15 09:38:00 +0100523
524 if (indio_dev->available_scan_masks == NULL)
525 kfree(indio_dev->active_scan_mask);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100526}
527
Lars-Peter Clausen8e050992013-10-14 17:49:00 +0100528static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
529 struct iio_buffer *buffer)
530{
531 unsigned int bytes;
532
533 if (!buffer->access->set_bytes_per_datum)
534 return;
535
536 bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
537 buffer->scan_timestamp);
538
539 buffer->access->set_bytes_per_datum(buffer, bytes);
540}
541
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100542static int __iio_update_buffers(struct iio_dev *indio_dev,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100543 struct iio_buffer *insert_buffer,
544 struct iio_buffer *remove_buffer)
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100545{
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100546 int ret;
547 int success = 0;
548 struct iio_buffer *buffer;
549 unsigned long *compound_mask;
550 const unsigned long *old_mask;
Jonathan Cameron959d2952011-12-05 21:37:13 +0000551
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100552 /* Wind down existing buffers - iff there are any */
553 if (!list_empty(&indio_dev->buffer_list)) {
554 if (indio_dev->setup_ops->predisable) {
555 ret = indio_dev->setup_ops->predisable(indio_dev);
556 if (ret)
557 goto error_ret;
558 }
559 indio_dev->currentmode = INDIO_DIRECT_MODE;
560 if (indio_dev->setup_ops->postdisable) {
561 ret = indio_dev->setup_ops->postdisable(indio_dev);
562 if (ret)
563 goto error_ret;
564 }
565 }
566 /* Keep a copy of current setup to allow roll back */
567 old_mask = indio_dev->active_scan_mask;
568 if (!indio_dev->available_scan_masks)
569 indio_dev->active_scan_mask = NULL;
570
571 if (remove_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100572 iio_buffer_deactivate(remove_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100573 if (insert_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100574 iio_buffer_activate(indio_dev, insert_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100575
576 /* If no buffers in list, we are done */
577 if (list_empty(&indio_dev->buffer_list)) {
578 indio_dev->currentmode = INDIO_DIRECT_MODE;
579 if (indio_dev->available_scan_masks == NULL)
580 kfree(old_mask);
581 return 0;
582 }
Jonathan Cameron959d2952011-12-05 21:37:13 +0000583
Peter Meerwald95725882013-09-17 23:42:00 +0100584 /* What scan mask do we actually have? */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100585 compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
586 sizeof(long), GFP_KERNEL);
587 if (compound_mask == NULL) {
588 if (indio_dev->available_scan_masks == NULL)
589 kfree(old_mask);
590 return -ENOMEM;
591 }
592 indio_dev->scan_timestamp = 0;
593
594 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
595 bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
596 indio_dev->masklength);
597 indio_dev->scan_timestamp |= buffer->scan_timestamp;
598 }
599 if (indio_dev->available_scan_masks) {
Jonathan Cameron959d2952011-12-05 21:37:13 +0000600 indio_dev->active_scan_mask =
601 iio_scan_mask_match(indio_dev->available_scan_masks,
602 indio_dev->masklength,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100603 compound_mask);
604 if (indio_dev->active_scan_mask == NULL) {
605 /*
606 * Roll back.
607 * Note can only occur when adding a buffer.
608 */
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100609 iio_buffer_deactivate(insert_buffer);
Peter Meerwaldd66e0452013-09-18 22:10:00 +0100610 if (old_mask) {
611 indio_dev->active_scan_mask = old_mask;
612 success = -EINVAL;
613 }
614 else {
615 kfree(compound_mask);
616 ret = -EINVAL;
617 goto error_ret;
618 }
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100619 }
620 } else {
621 indio_dev->active_scan_mask = compound_mask;
622 }
Lars-Peter Clausenaff1eb42012-06-15 18:08:59 +0200623
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000624 iio_update_demux(indio_dev);
625
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100626 /* Wind up again */
627 if (indio_dev->setup_ops->preenable) {
628 ret = indio_dev->setup_ops->preenable(indio_dev);
629 if (ret) {
630 printk(KERN_ERR
Michał Mirosławbec18892013-05-04 14:19:00 +0100631 "Buffer not started: buffer preenable failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100632 goto error_remove_inserted;
633 }
634 }
635 indio_dev->scan_bytes =
636 iio_compute_scan_bytes(indio_dev,
637 indio_dev->active_scan_mask,
638 indio_dev->scan_timestamp);
Lars-Peter Clausen8e050992013-10-14 17:49:00 +0100639 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
640 iio_buffer_update_bytes_per_datum(indio_dev, buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100641 if (buffer->access->request_update) {
642 ret = buffer->access->request_update(buffer);
643 if (ret) {
644 printk(KERN_INFO
Michał Mirosławbec18892013-05-04 14:19:00 +0100645 "Buffer not started: buffer parameter update failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100646 goto error_run_postdisable;
647 }
648 }
Lars-Peter Clausen8e050992013-10-14 17:49:00 +0100649 }
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100650 if (indio_dev->info->update_scan_mode) {
651 ret = indio_dev->info
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000652 ->update_scan_mode(indio_dev,
653 indio_dev->active_scan_mask);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100654 if (ret < 0) {
Michał Mirosławbec18892013-05-04 14:19:00 +0100655 printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100656 goto error_run_postdisable;
657 }
658 }
Peter Meerwald95725882013-09-17 23:42:00 +0100659 /* Definitely possible for devices to support both of these. */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100660 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
661 if (!indio_dev->trig) {
662 printk(KERN_INFO "Buffer not started: no trigger\n");
663 ret = -EINVAL;
664 /* Can only occur on first buffer */
665 goto error_run_postdisable;
666 }
667 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
668 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) {
669 indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
Peter Meerwald95725882013-09-17 23:42:00 +0100670 } else { /* Should never be reached */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100671 ret = -EINVAL;
672 goto error_run_postdisable;
673 }
674
675 if (indio_dev->setup_ops->postenable) {
676 ret = indio_dev->setup_ops->postenable(indio_dev);
677 if (ret) {
678 printk(KERN_INFO
Michał Mirosławbec18892013-05-04 14:19:00 +0100679 "Buffer not started: postenable failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100680 indio_dev->currentmode = INDIO_DIRECT_MODE;
681 if (indio_dev->setup_ops->postdisable)
682 indio_dev->setup_ops->postdisable(indio_dev);
683 goto error_disable_all_buffers;
684 }
685 }
686
687 if (indio_dev->available_scan_masks)
688 kfree(compound_mask);
689 else
690 kfree(old_mask);
691
692 return success;
693
694error_disable_all_buffers:
695 indio_dev->currentmode = INDIO_DIRECT_MODE;
696error_run_postdisable:
697 if (indio_dev->setup_ops->postdisable)
698 indio_dev->setup_ops->postdisable(indio_dev);
699error_remove_inserted:
700
701 if (insert_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100702 iio_buffer_deactivate(insert_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100703 indio_dev->active_scan_mask = old_mask;
704 kfree(compound_mask);
705error_ret:
706
707 return ret;
708}
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100709
710int iio_update_buffers(struct iio_dev *indio_dev,
711 struct iio_buffer *insert_buffer,
712 struct iio_buffer *remove_buffer)
713{
714 int ret;
715
Lars-Peter Clausen3909fab2013-10-04 12:07:00 +0100716 if (insert_buffer == remove_buffer)
717 return 0;
718
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100719 mutex_lock(&indio_dev->info_exist_lock);
720 mutex_lock(&indio_dev->mlock);
721
Lars-Peter Clausen3909fab2013-10-04 12:07:00 +0100722 if (insert_buffer && iio_buffer_is_active(insert_buffer))
723 insert_buffer = NULL;
724
725 if (remove_buffer && !iio_buffer_is_active(remove_buffer))
726 remove_buffer = NULL;
727
728 if (!insert_buffer && !remove_buffer) {
729 ret = 0;
730 goto out_unlock;
731 }
732
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100733 if (indio_dev->info == NULL) {
734 ret = -ENODEV;
735 goto out_unlock;
736 }
737
738 ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
739
740out_unlock:
741 mutex_unlock(&indio_dev->mlock);
742 mutex_unlock(&indio_dev->info_exist_lock);
743
744 return ret;
745}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100746EXPORT_SYMBOL_GPL(iio_update_buffers);
747
748ssize_t iio_buffer_store_enable(struct device *dev,
749 struct device_attribute *attr,
750 const char *buf,
751 size_t len)
752{
753 int ret;
754 bool requested_state;
755 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100756 bool inlist;
757
758 ret = strtobool(buf, &requested_state);
759 if (ret < 0)
760 return ret;
761
762 mutex_lock(&indio_dev->mlock);
763
764 /* Find out if it is in the list */
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100765 inlist = iio_buffer_is_active(indio_dev->buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100766 /* Already in desired state */
767 if (inlist == requested_state)
768 goto done;
769
770 if (requested_state)
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100771 ret = __iio_update_buffers(indio_dev,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100772 indio_dev->buffer, NULL);
773 else
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100774 ret = __iio_update_buffers(indio_dev,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100775 NULL, indio_dev->buffer);
776
777 if (ret < 0)
778 goto done;
779done:
780 mutex_unlock(&indio_dev->mlock);
781 return (ret < 0) ? ret : len;
782}
783EXPORT_SYMBOL(iio_buffer_store_enable);
784
Jonathan Cameron14555b12011-09-21 11:15:57 +0100785/**
Lars-Peter Clausen81636632012-07-09 10:00:00 +0100786 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
787 * @indio_dev: the iio device
788 * @mask: scan mask to be checked
789 *
790 * Return true if exactly one bit is set in the scan mask, false otherwise. It
791 * can be used for devices where only one channel can be active for sampling at
792 * a time.
793 */
794bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
795 const unsigned long *mask)
796{
797 return bitmap_weight(mask, indio_dev->masklength) == 1;
798}
799EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
800
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100801static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
802 const unsigned long *mask)
803{
804 if (!indio_dev->setup_ops->validate_scan_mask)
805 return true;
806
807 return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
808}
809
Jonathan Cameron14555b12011-09-21 11:15:57 +0100810/**
811 * iio_scan_mask_set() - set particular bit in the scan mask
Peter Meerwald95725882013-09-17 23:42:00 +0100812 * @indio_dev: the iio device
Jonathan Cameron14555b12011-09-21 11:15:57 +0100813 * @buffer: the buffer whose scan mask we are interested in
814 * @bit: the bit to be set.
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100815 *
816 * Note that at this point we have no way of knowing what other
817 * buffers might request, hence this code only verifies that the
818 * individual buffers request is plausible.
819 */
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000820int iio_scan_mask_set(struct iio_dev *indio_dev,
821 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100822{
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100823 const unsigned long *mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100824 unsigned long *trialmask;
825
826 trialmask = kmalloc(sizeof(*trialmask)*
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100827 BITS_TO_LONGS(indio_dev->masklength),
Jonathan Cameron14555b12011-09-21 11:15:57 +0100828 GFP_KERNEL);
829
830 if (trialmask == NULL)
831 return -ENOMEM;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100832 if (!indio_dev->masklength) {
Peter Meerwald95725882013-09-17 23:42:00 +0100833 WARN_ON("Trying to set scanmask prior to registering buffer\n");
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100834 goto err_invalid_mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100835 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100836 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100837 set_bit(bit, trialmask);
838
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100839 if (!iio_validate_scan_mask(indio_dev, trialmask))
840 goto err_invalid_mask;
841
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100842 if (indio_dev->available_scan_masks) {
843 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
844 indio_dev->masklength,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100845 trialmask);
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100846 if (!mask)
847 goto err_invalid_mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100848 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100849 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100850
851 kfree(trialmask);
852
853 return 0;
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100854
855err_invalid_mask:
856 kfree(trialmask);
857 return -EINVAL;
858}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100859EXPORT_SYMBOL_GPL(iio_scan_mask_set);
860
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000861int iio_scan_mask_query(struct iio_dev *indio_dev,
862 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100863{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100864 if (bit > indio_dev->masklength)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100865 return -EINVAL;
866
867 if (!buffer->scan_mask)
868 return 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100869
Alec Berg2076a202014-03-19 18:50:00 +0000870 /* Ensure return value is 0 or 1. */
871 return !!test_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100872};
873EXPORT_SYMBOL_GPL(iio_scan_mask_query);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000874
875/**
876 * struct iio_demux_table() - table describing demux memcpy ops
877 * @from: index to copy from
Peter Meerwald99698b42012-08-26 13:43:00 +0100878 * @to: index to copy to
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000879 * @length: how many bytes to copy
880 * @l: list head used for management
881 */
882struct iio_demux_table {
883 unsigned from;
884 unsigned to;
885 unsigned length;
886 struct list_head l;
887};
888
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100889static const void *iio_demux(struct iio_buffer *buffer,
890 const void *datain)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000891{
892 struct iio_demux_table *t;
893
894 if (list_empty(&buffer->demux_list))
895 return datain;
896 list_for_each_entry(t, &buffer->demux_list, l)
897 memcpy(buffer->demux_bounce + t->to,
898 datain + t->from, t->length);
899
900 return buffer->demux_bounce;
901}
902
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100903static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000904{
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100905 const void *dataout = iio_demux(buffer, data);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000906
Lars-Peter Clausence56ade2012-09-04 13:38:00 +0100907 return buffer->access->store_to(buffer, dataout);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000908}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000909
Jonathan Cameron842cd102012-04-21 10:09:45 +0100910static void iio_buffer_demux_free(struct iio_buffer *buffer)
911{
912 struct iio_demux_table *p, *q;
913 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
914 list_del(&p->l);
915 kfree(p);
916 }
917}
918
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100919
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100920int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100921{
922 int ret;
923 struct iio_buffer *buf;
924
925 list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
926 ret = iio_push_to_buffer(buf, data);
927 if (ret < 0)
928 return ret;
929 }
930
931 return 0;
932}
933EXPORT_SYMBOL_GPL(iio_push_to_buffers);
934
935static int iio_buffer_update_demux(struct iio_dev *indio_dev,
936 struct iio_buffer *buffer)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000937{
938 const struct iio_chan_spec *ch;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000939 int ret, in_ind = -1, out_ind, length;
940 unsigned in_loc = 0, out_loc = 0;
Jonathan Cameron842cd102012-04-21 10:09:45 +0100941 struct iio_demux_table *p;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000942
943 /* Clear out any old demux */
Jonathan Cameron842cd102012-04-21 10:09:45 +0100944 iio_buffer_demux_free(buffer);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000945 kfree(buffer->demux_bounce);
946 buffer->demux_bounce = NULL;
947
948 /* First work out which scan mode we will actually have */
949 if (bitmap_equal(indio_dev->active_scan_mask,
950 buffer->scan_mask,
951 indio_dev->masklength))
952 return 0;
953
954 /* Now we have the two masks, work from least sig and build up sizes */
955 for_each_set_bit(out_ind,
956 indio_dev->active_scan_mask,
957 indio_dev->masklength) {
958 in_ind = find_next_bit(indio_dev->active_scan_mask,
959 indio_dev->masklength,
960 in_ind + 1);
961 while (in_ind != out_ind) {
962 in_ind = find_next_bit(indio_dev->active_scan_mask,
963 indio_dev->masklength,
964 in_ind + 1);
965 ch = iio_find_channel_from_si(indio_dev, in_ind);
966 length = ch->scan_type.storagebits/8;
967 /* Make sure we are aligned */
968 in_loc += length;
969 if (in_loc % length)
970 in_loc += length - in_loc % length;
971 }
972 p = kmalloc(sizeof(*p), GFP_KERNEL);
973 if (p == NULL) {
974 ret = -ENOMEM;
975 goto error_clear_mux_table;
976 }
977 ch = iio_find_channel_from_si(indio_dev, in_ind);
978 length = ch->scan_type.storagebits/8;
979 if (out_loc % length)
980 out_loc += length - out_loc % length;
981 if (in_loc % length)
982 in_loc += length - in_loc % length;
983 p->from = in_loc;
984 p->to = out_loc;
985 p->length = length;
986 list_add_tail(&p->l, &buffer->demux_list);
987 out_loc += length;
988 in_loc += length;
989 }
990 /* Relies on scan_timestamp being last */
991 if (buffer->scan_timestamp) {
992 p = kmalloc(sizeof(*p), GFP_KERNEL);
993 if (p == NULL) {
994 ret = -ENOMEM;
995 goto error_clear_mux_table;
996 }
997 ch = iio_find_channel_from_si(indio_dev,
Jonathan Cameronf1264802012-04-21 10:09:34 +0100998 indio_dev->scan_index_timestamp);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000999 length = ch->scan_type.storagebits/8;
1000 if (out_loc % length)
1001 out_loc += length - out_loc % length;
1002 if (in_loc % length)
1003 in_loc += length - in_loc % length;
1004 p->from = in_loc;
1005 p->to = out_loc;
1006 p->length = length;
1007 list_add_tail(&p->l, &buffer->demux_list);
1008 out_loc += length;
1009 in_loc += length;
1010 }
1011 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
1012 if (buffer->demux_bounce == NULL) {
1013 ret = -ENOMEM;
1014 goto error_clear_mux_table;
1015 }
1016 return 0;
1017
1018error_clear_mux_table:
Jonathan Cameron842cd102012-04-21 10:09:45 +01001019 iio_buffer_demux_free(buffer);
1020
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001021 return ret;
1022}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001023
1024int iio_update_demux(struct iio_dev *indio_dev)
1025{
1026 struct iio_buffer *buffer;
1027 int ret;
1028
1029 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
1030 ret = iio_buffer_update_demux(indio_dev, buffer);
1031 if (ret < 0)
1032 goto error_clear_mux_table;
1033 }
1034 return 0;
1035
1036error_clear_mux_table:
1037 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
1038 iio_buffer_demux_free(buffer);
1039
1040 return ret;
1041}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001042EXPORT_SYMBOL_GPL(iio_update_demux);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +01001043
1044/**
1045 * iio_buffer_release() - Free a buffer's resources
1046 * @ref: Pointer to the kref embedded in the iio_buffer struct
1047 *
1048 * This function is called when the last reference to the buffer has been
1049 * dropped. It will typically free all resources allocated by the buffer. Do not
1050 * call this function manually, always use iio_buffer_put() when done using a
1051 * buffer.
1052 */
1053static void iio_buffer_release(struct kref *ref)
1054{
1055 struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
1056
1057 buffer->access->release(buffer);
1058}
1059
1060/**
1061 * iio_buffer_get() - Grab a reference to the buffer
1062 * @buffer: The buffer to grab a reference for, may be NULL
1063 *
1064 * Returns the pointer to the buffer that was passed into the function.
1065 */
1066struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
1067{
1068 if (buffer)
1069 kref_get(&buffer->ref);
1070
1071 return buffer;
1072}
1073EXPORT_SYMBOL_GPL(iio_buffer_get);
1074
1075/**
1076 * iio_buffer_put() - Release the reference to the buffer
1077 * @buffer: The buffer to release the reference for, may be NULL
1078 */
1079void iio_buffer_put(struct iio_buffer *buffer)
1080{
1081 if (buffer)
1082 kref_put(&buffer->ref, iio_buffer_release);
1083}
1084EXPORT_SYMBOL_GPL(iio_buffer_put);