blob: e108f2a9d827fca1e37932f18b5232ca4e9b65ea [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
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000168 ret = test_bit(to_iio_dev_attr(attr)->address,
169 indio_dev->buffer->scan_mask);
170
Jonathan Cameron14555b12011-09-21 11:15:57 +0100171 return sprintf(buf, "%d\n", ret);
172}
173
174static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
175{
176 clear_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100177 return 0;
178}
179
180static ssize_t iio_scan_el_store(struct device *dev,
181 struct device_attribute *attr,
182 const char *buf,
183 size_t len)
184{
Jonathan Camerona714af22012-04-21 10:09:32 +0100185 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100186 bool state;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200187 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100188 struct iio_buffer *buffer = indio_dev->buffer;
189 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
190
Jonathan Camerona714af22012-04-21 10:09:32 +0100191 ret = strtobool(buf, &state);
192 if (ret < 0)
193 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100194 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100195 if (iio_buffer_is_active(indio_dev->buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100196 ret = -EBUSY;
197 goto error_ret;
198 }
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000199 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100200 if (ret < 0)
201 goto error_ret;
202 if (!state && ret) {
203 ret = iio_scan_mask_clear(buffer, this_attr->address);
204 if (ret)
205 goto error_ret;
206 } else if (state && !ret) {
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000207 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100208 if (ret)
209 goto error_ret;
210 }
211
212error_ret:
213 mutex_unlock(&indio_dev->mlock);
214
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100215 return ret < 0 ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100216
217}
218
219static ssize_t iio_scan_el_ts_show(struct device *dev,
220 struct device_attribute *attr,
221 char *buf)
222{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200223 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100224 return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100225}
226
227static ssize_t iio_scan_el_ts_store(struct device *dev,
228 struct device_attribute *attr,
229 const char *buf,
230 size_t len)
231{
Jonathan Camerona714af22012-04-21 10:09:32 +0100232 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200233 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100234 bool state;
235
Jonathan Camerona714af22012-04-21 10:09:32 +0100236 ret = strtobool(buf, &state);
237 if (ret < 0)
238 return ret;
239
Jonathan Cameron14555b12011-09-21 11:15:57 +0100240 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100241 if (iio_buffer_is_active(indio_dev->buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100242 ret = -EBUSY;
243 goto error_ret;
244 }
245 indio_dev->buffer->scan_timestamp = state;
246error_ret:
247 mutex_unlock(&indio_dev->mlock);
248
249 return ret ? ret : len;
250}
251
252static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
253 const struct iio_chan_spec *chan)
254{
255 int ret, attrcount = 0;
256 struct iio_buffer *buffer = indio_dev->buffer;
257
258 ret = __iio_add_chan_devattr("index",
259 chan,
260 &iio_show_scan_index,
261 NULL,
262 0,
Jonathan Cameron37044322013-09-08 14:57:00 +0100263 IIO_SEPARATE,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100264 &indio_dev->dev,
265 &buffer->scan_el_dev_attr_list);
266 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000267 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100268 attrcount++;
269 ret = __iio_add_chan_devattr("type",
270 chan,
271 &iio_show_fixed_type,
272 NULL,
273 0,
274 0,
275 &indio_dev->dev,
276 &buffer->scan_el_dev_attr_list);
277 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000278 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100279 attrcount++;
280 if (chan->type != IIO_TIMESTAMP)
281 ret = __iio_add_chan_devattr("en",
282 chan,
283 &iio_scan_el_show,
284 &iio_scan_el_store,
285 chan->scan_index,
286 0,
287 &indio_dev->dev,
288 &buffer->scan_el_dev_attr_list);
289 else
290 ret = __iio_add_chan_devattr("en",
291 chan,
292 &iio_scan_el_ts_show,
293 &iio_scan_el_ts_store,
294 chan->scan_index,
295 0,
296 &indio_dev->dev,
297 &buffer->scan_el_dev_attr_list);
Peter Meerwald95725882013-09-17 23:42:00 +0100298 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000299 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100300 attrcount++;
301 ret = attrcount;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100302 return ret;
303}
304
Jonathan Cameron14555b12011-09-21 11:15:57 +0100305static const char * const iio_scan_elements_group_name = "scan_elements";
306
307int iio_buffer_register(struct iio_dev *indio_dev,
308 const struct iio_chan_spec *channels,
309 int num_channels)
310{
311 struct iio_dev_attr *p;
312 struct attribute **attr;
313 struct iio_buffer *buffer = indio_dev->buffer;
314 int ret, i, attrn, attrcount, attrcount_orig = 0;
315
316 if (buffer->attrs)
317 indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs;
318
319 if (buffer->scan_el_attrs != NULL) {
320 attr = buffer->scan_el_attrs->attrs;
321 while (*attr++ != NULL)
322 attrcount_orig++;
323 }
324 attrcount = attrcount_orig;
325 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
326 if (channels) {
327 /* new magic */
328 for (i = 0; i < num_channels; i++) {
Lars-Peter Clausenf5b81dd2012-06-18 18:33:47 +0200329 if (channels[i].scan_index < 0)
330 continue;
331
Jonathan Cameron14555b12011-09-21 11:15:57 +0100332 /* Establish necessary mask length */
333 if (channels[i].scan_index >
334 (int)indio_dev->masklength - 1)
335 indio_dev->masklength
Lars-Peter Clausene1dc7be2012-07-02 14:52:56 +0200336 = channels[i].scan_index + 1;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100337
338 ret = iio_buffer_add_channel_sysfs(indio_dev,
339 &channels[i]);
340 if (ret < 0)
341 goto error_cleanup_dynamic;
342 attrcount += ret;
Jonathan Cameronbeb80602011-12-05 21:37:11 +0000343 if (channels[i].type == IIO_TIMESTAMP)
Jonathan Cameronf1264802012-04-21 10:09:34 +0100344 indio_dev->scan_index_timestamp =
Jonathan Cameronbeb80602011-12-05 21:37:11 +0000345 channels[i].scan_index;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100346 }
347 if (indio_dev->masklength && buffer->scan_mask == NULL) {
Thomas Meyerd83fb182011-11-29 22:08:00 +0100348 buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
349 sizeof(*buffer->scan_mask),
350 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100351 if (buffer->scan_mask == NULL) {
352 ret = -ENOMEM;
353 goto error_cleanup_dynamic;
354 }
355 }
356 }
357
358 buffer->scan_el_group.name = iio_scan_elements_group_name;
359
Thomas Meyerd83fb182011-11-29 22:08:00 +0100360 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
361 sizeof(buffer->scan_el_group.attrs[0]),
362 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100363 if (buffer->scan_el_group.attrs == NULL) {
364 ret = -ENOMEM;
365 goto error_free_scan_mask;
366 }
367 if (buffer->scan_el_attrs)
368 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
369 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
370 attrn = attrcount_orig;
371
372 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
373 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
374 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
375
376 return 0;
377
378error_free_scan_mask:
379 kfree(buffer->scan_mask);
380error_cleanup_dynamic:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100381 iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100382
383 return ret;
384}
385EXPORT_SYMBOL(iio_buffer_register);
386
387void iio_buffer_unregister(struct iio_dev *indio_dev)
388{
389 kfree(indio_dev->buffer->scan_mask);
390 kfree(indio_dev->buffer->scan_el_group.attrs);
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100391 iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100392}
393EXPORT_SYMBOL(iio_buffer_unregister);
394
395ssize_t iio_buffer_read_length(struct device *dev,
396 struct device_attribute *attr,
397 char *buf)
398{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200399 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100400 struct iio_buffer *buffer = indio_dev->buffer;
401
402 if (buffer->access->get_length)
403 return sprintf(buf, "%d\n",
404 buffer->access->get_length(buffer));
405
406 return 0;
407}
408EXPORT_SYMBOL(iio_buffer_read_length);
409
410ssize_t iio_buffer_write_length(struct device *dev,
411 struct device_attribute *attr,
412 const char *buf,
413 size_t len)
414{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200415 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100416 struct iio_buffer *buffer = indio_dev->buffer;
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100417 unsigned int val;
418 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100419
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100420 ret = kstrtouint(buf, 10, &val);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100421 if (ret)
422 return ret;
423
424 if (buffer->access->get_length)
425 if (val == buffer->access->get_length(buffer))
426 return len;
427
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100428 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100429 if (iio_buffer_is_active(indio_dev->buffer)) {
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100430 ret = -EBUSY;
431 } else {
Lars-Peter Clausen869871b2011-12-19 15:23:48 +0100432 if (buffer->access->set_length)
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100433 buffer->access->set_length(buffer, val);
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100434 ret = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100435 }
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100436 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100437
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100438 return ret ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100439}
440EXPORT_SYMBOL(iio_buffer_write_length);
441
Jonathan Cameron14555b12011-09-21 11:15:57 +0100442ssize_t iio_buffer_show_enable(struct device *dev,
443 struct device_attribute *attr,
444 char *buf)
445{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200446 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100447 return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
Jonathan Cameron14555b12011-09-21 11:15:57 +0100448}
449EXPORT_SYMBOL(iio_buffer_show_enable);
450
Peter Meerwald95725882013-09-17 23:42:00 +0100451/* Note NULL used as error indicator as it doesn't make sense. */
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100452static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100453 unsigned int masklength,
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100454 const unsigned long *mask)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100455{
456 if (bitmap_empty(mask, masklength))
457 return NULL;
458 while (*av_masks) {
459 if (bitmap_subset(mask, av_masks, masklength))
460 return av_masks;
461 av_masks += BITS_TO_LONGS(masklength);
462 }
463 return NULL;
464}
465
Peter Meerwald183f4172013-09-18 22:10:00 +0100466static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
467 const unsigned long *mask, bool timestamp)
Jonathan Cameron959d2952011-12-05 21:37:13 +0000468{
Jonathan Cameron959d2952011-12-05 21:37:13 +0000469 const struct iio_chan_spec *ch;
470 unsigned bytes = 0;
471 int length, i;
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100472
473 /* How much space will the demuxed element take? */
474 for_each_set_bit(i, mask,
475 indio_dev->masklength) {
476 ch = iio_find_channel_from_si(indio_dev, i);
477 length = ch->scan_type.storagebits / 8;
478 bytes = ALIGN(bytes, length);
479 bytes += length;
480 }
481 if (timestamp) {
482 ch = iio_find_channel_from_si(indio_dev,
Jonathan Cameronf1264802012-04-21 10:09:34 +0100483 indio_dev->scan_index_timestamp);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100484 length = ch->scan_type.storagebits / 8;
485 bytes = ALIGN(bytes, length);
486 bytes += length;
487 }
488 return bytes;
489}
490
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100491static void iio_buffer_activate(struct iio_dev *indio_dev,
492 struct iio_buffer *buffer)
493{
494 iio_buffer_get(buffer);
495 list_add(&buffer->buffer_list, &indio_dev->buffer_list);
496}
497
498static void iio_buffer_deactivate(struct iio_buffer *buffer)
499{
500 list_del_init(&buffer->buffer_list);
501 iio_buffer_put(buffer);
502}
503
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100504void iio_disable_all_buffers(struct iio_dev *indio_dev)
505{
506 struct iio_buffer *buffer, *_buffer;
507
508 if (list_empty(&indio_dev->buffer_list))
509 return;
510
511 if (indio_dev->setup_ops->predisable)
512 indio_dev->setup_ops->predisable(indio_dev);
513
514 list_for_each_entry_safe(buffer, _buffer,
515 &indio_dev->buffer_list, buffer_list)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100516 iio_buffer_deactivate(buffer);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100517
518 indio_dev->currentmode = INDIO_DIRECT_MODE;
519 if (indio_dev->setup_ops->postdisable)
520 indio_dev->setup_ops->postdisable(indio_dev);
Lars-Peter Clausene086ed72013-10-15 09:38:00 +0100521
522 if (indio_dev->available_scan_masks == NULL)
523 kfree(indio_dev->active_scan_mask);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100524}
525
Lars-Peter Clausen8e050992013-10-14 17:49:00 +0100526static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
527 struct iio_buffer *buffer)
528{
529 unsigned int bytes;
530
531 if (!buffer->access->set_bytes_per_datum)
532 return;
533
534 bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
535 buffer->scan_timestamp);
536
537 buffer->access->set_bytes_per_datum(buffer, bytes);
538}
539
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100540static int __iio_update_buffers(struct iio_dev *indio_dev,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100541 struct iio_buffer *insert_buffer,
542 struct iio_buffer *remove_buffer)
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100543{
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100544 int ret;
545 int success = 0;
546 struct iio_buffer *buffer;
547 unsigned long *compound_mask;
548 const unsigned long *old_mask;
Jonathan Cameron959d2952011-12-05 21:37:13 +0000549
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100550 /* Wind down existing buffers - iff there are any */
551 if (!list_empty(&indio_dev->buffer_list)) {
552 if (indio_dev->setup_ops->predisable) {
553 ret = indio_dev->setup_ops->predisable(indio_dev);
554 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000555 return ret;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100556 }
557 indio_dev->currentmode = INDIO_DIRECT_MODE;
558 if (indio_dev->setup_ops->postdisable) {
559 ret = indio_dev->setup_ops->postdisable(indio_dev);
560 if (ret)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000561 return ret;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100562 }
563 }
564 /* Keep a copy of current setup to allow roll back */
565 old_mask = indio_dev->active_scan_mask;
566 if (!indio_dev->available_scan_masks)
567 indio_dev->active_scan_mask = NULL;
568
569 if (remove_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100570 iio_buffer_deactivate(remove_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100571 if (insert_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100572 iio_buffer_activate(indio_dev, insert_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100573
574 /* If no buffers in list, we are done */
575 if (list_empty(&indio_dev->buffer_list)) {
576 indio_dev->currentmode = INDIO_DIRECT_MODE;
577 if (indio_dev->available_scan_masks == NULL)
578 kfree(old_mask);
579 return 0;
580 }
Jonathan Cameron959d2952011-12-05 21:37:13 +0000581
Peter Meerwald95725882013-09-17 23:42:00 +0100582 /* What scan mask do we actually have? */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100583 compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
584 sizeof(long), GFP_KERNEL);
585 if (compound_mask == NULL) {
586 if (indio_dev->available_scan_masks == NULL)
587 kfree(old_mask);
588 return -ENOMEM;
589 }
590 indio_dev->scan_timestamp = 0;
591
592 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
593 bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
594 indio_dev->masklength);
595 indio_dev->scan_timestamp |= buffer->scan_timestamp;
596 }
597 if (indio_dev->available_scan_masks) {
Jonathan Cameron959d2952011-12-05 21:37:13 +0000598 indio_dev->active_scan_mask =
599 iio_scan_mask_match(indio_dev->available_scan_masks,
600 indio_dev->masklength,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100601 compound_mask);
602 if (indio_dev->active_scan_mask == NULL) {
603 /*
604 * Roll back.
605 * Note can only occur when adding a buffer.
606 */
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100607 iio_buffer_deactivate(insert_buffer);
Peter Meerwaldd66e0452013-09-18 22:10:00 +0100608 if (old_mask) {
609 indio_dev->active_scan_mask = old_mask;
610 success = -EINVAL;
611 }
612 else {
613 kfree(compound_mask);
614 ret = -EINVAL;
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000615 return ret;
Peter Meerwaldd66e0452013-09-18 22:10:00 +0100616 }
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100617 }
618 } else {
619 indio_dev->active_scan_mask = compound_mask;
620 }
Lars-Peter Clausenaff1eb42012-06-15 18:08:59 +0200621
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000622 iio_update_demux(indio_dev);
623
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100624 /* Wind up again */
625 if (indio_dev->setup_ops->preenable) {
626 ret = indio_dev->setup_ops->preenable(indio_dev);
627 if (ret) {
628 printk(KERN_ERR
Michał Mirosławbec18892013-05-04 14:19:00 +0100629 "Buffer not started: buffer preenable failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100630 goto error_remove_inserted;
631 }
632 }
633 indio_dev->scan_bytes =
634 iio_compute_scan_bytes(indio_dev,
635 indio_dev->active_scan_mask,
636 indio_dev->scan_timestamp);
Lars-Peter Clausen8e050992013-10-14 17:49:00 +0100637 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
638 iio_buffer_update_bytes_per_datum(indio_dev, buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100639 if (buffer->access->request_update) {
640 ret = buffer->access->request_update(buffer);
641 if (ret) {
642 printk(KERN_INFO
Michał Mirosławbec18892013-05-04 14:19:00 +0100643 "Buffer not started: buffer parameter update failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100644 goto error_run_postdisable;
645 }
646 }
Lars-Peter Clausen8e050992013-10-14 17:49:00 +0100647 }
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100648 if (indio_dev->info->update_scan_mode) {
649 ret = indio_dev->info
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000650 ->update_scan_mode(indio_dev,
651 indio_dev->active_scan_mask);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100652 if (ret < 0) {
Michał Mirosławbec18892013-05-04 14:19:00 +0100653 printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100654 goto error_run_postdisable;
655 }
656 }
Peter Meerwald95725882013-09-17 23:42:00 +0100657 /* Definitely possible for devices to support both of these. */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100658 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
659 if (!indio_dev->trig) {
660 printk(KERN_INFO "Buffer not started: no trigger\n");
661 ret = -EINVAL;
662 /* Can only occur on first buffer */
663 goto error_run_postdisable;
664 }
665 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
666 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) {
667 indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
Peter Meerwald95725882013-09-17 23:42:00 +0100668 } else { /* Should never be reached */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100669 ret = -EINVAL;
670 goto error_run_postdisable;
671 }
672
673 if (indio_dev->setup_ops->postenable) {
674 ret = indio_dev->setup_ops->postenable(indio_dev);
675 if (ret) {
676 printk(KERN_INFO
Michał Mirosławbec18892013-05-04 14:19:00 +0100677 "Buffer not started: postenable failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100678 indio_dev->currentmode = INDIO_DIRECT_MODE;
679 if (indio_dev->setup_ops->postdisable)
680 indio_dev->setup_ops->postdisable(indio_dev);
681 goto error_disable_all_buffers;
682 }
683 }
684
685 if (indio_dev->available_scan_masks)
686 kfree(compound_mask);
687 else
688 kfree(old_mask);
689
690 return success;
691
692error_disable_all_buffers:
693 indio_dev->currentmode = INDIO_DIRECT_MODE;
694error_run_postdisable:
695 if (indio_dev->setup_ops->postdisable)
696 indio_dev->setup_ops->postdisable(indio_dev);
697error_remove_inserted:
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100698 if (insert_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100699 iio_buffer_deactivate(insert_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100700 indio_dev->active_scan_mask = old_mask;
701 kfree(compound_mask);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100702 return ret;
703}
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100704
705int iio_update_buffers(struct iio_dev *indio_dev,
706 struct iio_buffer *insert_buffer,
707 struct iio_buffer *remove_buffer)
708{
709 int ret;
710
Lars-Peter Clausen3909fab2013-10-04 12:07:00 +0100711 if (insert_buffer == remove_buffer)
712 return 0;
713
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100714 mutex_lock(&indio_dev->info_exist_lock);
715 mutex_lock(&indio_dev->mlock);
716
Lars-Peter Clausen3909fab2013-10-04 12:07:00 +0100717 if (insert_buffer && iio_buffer_is_active(insert_buffer))
718 insert_buffer = NULL;
719
720 if (remove_buffer && !iio_buffer_is_active(remove_buffer))
721 remove_buffer = NULL;
722
723 if (!insert_buffer && !remove_buffer) {
724 ret = 0;
725 goto out_unlock;
726 }
727
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100728 if (indio_dev->info == NULL) {
729 ret = -ENODEV;
730 goto out_unlock;
731 }
732
733 ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
734
735out_unlock:
736 mutex_unlock(&indio_dev->mlock);
737 mutex_unlock(&indio_dev->info_exist_lock);
738
739 return ret;
740}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100741EXPORT_SYMBOL_GPL(iio_update_buffers);
742
743ssize_t iio_buffer_store_enable(struct device *dev,
744 struct device_attribute *attr,
745 const char *buf,
746 size_t len)
747{
748 int ret;
749 bool requested_state;
750 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100751 bool inlist;
752
753 ret = strtobool(buf, &requested_state);
754 if (ret < 0)
755 return ret;
756
757 mutex_lock(&indio_dev->mlock);
758
759 /* Find out if it is in the list */
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100760 inlist = iio_buffer_is_active(indio_dev->buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100761 /* Already in desired state */
762 if (inlist == requested_state)
763 goto done;
764
765 if (requested_state)
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100766 ret = __iio_update_buffers(indio_dev,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100767 indio_dev->buffer, NULL);
768 else
Lars-Peter Clausena9519452013-10-04 12:07:00 +0100769 ret = __iio_update_buffers(indio_dev,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100770 NULL, indio_dev->buffer);
771
772 if (ret < 0)
773 goto done;
774done:
775 mutex_unlock(&indio_dev->mlock);
776 return (ret < 0) ? ret : len;
777}
778EXPORT_SYMBOL(iio_buffer_store_enable);
779
Jonathan Cameron14555b12011-09-21 11:15:57 +0100780/**
Lars-Peter Clausen81636632012-07-09 10:00:00 +0100781 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
782 * @indio_dev: the iio device
783 * @mask: scan mask to be checked
784 *
785 * Return true if exactly one bit is set in the scan mask, false otherwise. It
786 * can be used for devices where only one channel can be active for sampling at
787 * a time.
788 */
789bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
790 const unsigned long *mask)
791{
792 return bitmap_weight(mask, indio_dev->masklength) == 1;
793}
794EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
795
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100796static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
797 const unsigned long *mask)
798{
799 if (!indio_dev->setup_ops->validate_scan_mask)
800 return true;
801
802 return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
803}
804
Jonathan Cameron14555b12011-09-21 11:15:57 +0100805/**
806 * iio_scan_mask_set() - set particular bit in the scan mask
Peter Meerwald95725882013-09-17 23:42:00 +0100807 * @indio_dev: the iio device
Jonathan Cameron14555b12011-09-21 11:15:57 +0100808 * @buffer: the buffer whose scan mask we are interested in
809 * @bit: the bit to be set.
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100810 *
811 * Note that at this point we have no way of knowing what other
812 * buffers might request, hence this code only verifies that the
813 * individual buffers request is plausible.
814 */
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000815int iio_scan_mask_set(struct iio_dev *indio_dev,
816 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100817{
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100818 const unsigned long *mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100819 unsigned long *trialmask;
820
821 trialmask = kmalloc(sizeof(*trialmask)*
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100822 BITS_TO_LONGS(indio_dev->masklength),
Jonathan Cameron14555b12011-09-21 11:15:57 +0100823 GFP_KERNEL);
824
825 if (trialmask == NULL)
826 return -ENOMEM;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100827 if (!indio_dev->masklength) {
Peter Meerwald95725882013-09-17 23:42:00 +0100828 WARN_ON("Trying to set scanmask prior to registering buffer\n");
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100829 goto err_invalid_mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100830 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100831 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100832 set_bit(bit, trialmask);
833
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100834 if (!iio_validate_scan_mask(indio_dev, trialmask))
835 goto err_invalid_mask;
836
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100837 if (indio_dev->available_scan_masks) {
838 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
839 indio_dev->masklength,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100840 trialmask);
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100841 if (!mask)
842 goto err_invalid_mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100843 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100844 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100845
846 kfree(trialmask);
847
848 return 0;
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100849
850err_invalid_mask:
851 kfree(trialmask);
852 return -EINVAL;
853}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100854EXPORT_SYMBOL_GPL(iio_scan_mask_set);
855
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000856int iio_scan_mask_query(struct iio_dev *indio_dev,
857 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100858{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100859 if (bit > indio_dev->masklength)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100860 return -EINVAL;
861
862 if (!buffer->scan_mask)
863 return 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100864
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100865 return test_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100866};
867EXPORT_SYMBOL_GPL(iio_scan_mask_query);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000868
869/**
870 * struct iio_demux_table() - table describing demux memcpy ops
871 * @from: index to copy from
Peter Meerwald99698b42012-08-26 13:43:00 +0100872 * @to: index to copy to
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000873 * @length: how many bytes to copy
874 * @l: list head used for management
875 */
876struct iio_demux_table {
877 unsigned from;
878 unsigned to;
879 unsigned length;
880 struct list_head l;
881};
882
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100883static const void *iio_demux(struct iio_buffer *buffer,
884 const void *datain)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000885{
886 struct iio_demux_table *t;
887
888 if (list_empty(&buffer->demux_list))
889 return datain;
890 list_for_each_entry(t, &buffer->demux_list, l)
891 memcpy(buffer->demux_bounce + t->to,
892 datain + t->from, t->length);
893
894 return buffer->demux_bounce;
895}
896
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100897static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000898{
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100899 const void *dataout = iio_demux(buffer, data);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000900
Lars-Peter Clausence56ade2012-09-04 13:38:00 +0100901 return buffer->access->store_to(buffer, dataout);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000902}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000903
Jonathan Cameron842cd102012-04-21 10:09:45 +0100904static void iio_buffer_demux_free(struct iio_buffer *buffer)
905{
906 struct iio_demux_table *p, *q;
907 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
908 list_del(&p->l);
909 kfree(p);
910 }
911}
912
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100913
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100914int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100915{
916 int ret;
917 struct iio_buffer *buf;
918
919 list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
920 ret = iio_push_to_buffer(buf, data);
921 if (ret < 0)
922 return ret;
923 }
924
925 return 0;
926}
927EXPORT_SYMBOL_GPL(iio_push_to_buffers);
928
929static int iio_buffer_update_demux(struct iio_dev *indio_dev,
930 struct iio_buffer *buffer)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000931{
932 const struct iio_chan_spec *ch;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000933 int ret, in_ind = -1, out_ind, length;
934 unsigned in_loc = 0, out_loc = 0;
Jonathan Cameron842cd102012-04-21 10:09:45 +0100935 struct iio_demux_table *p;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000936
937 /* Clear out any old demux */
Jonathan Cameron842cd102012-04-21 10:09:45 +0100938 iio_buffer_demux_free(buffer);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000939 kfree(buffer->demux_bounce);
940 buffer->demux_bounce = NULL;
941
942 /* First work out which scan mode we will actually have */
943 if (bitmap_equal(indio_dev->active_scan_mask,
944 buffer->scan_mask,
945 indio_dev->masklength))
946 return 0;
947
948 /* Now we have the two masks, work from least sig and build up sizes */
949 for_each_set_bit(out_ind,
950 indio_dev->active_scan_mask,
951 indio_dev->masklength) {
952 in_ind = find_next_bit(indio_dev->active_scan_mask,
953 indio_dev->masklength,
954 in_ind + 1);
955 while (in_ind != out_ind) {
956 in_ind = find_next_bit(indio_dev->active_scan_mask,
957 indio_dev->masklength,
958 in_ind + 1);
959 ch = iio_find_channel_from_si(indio_dev, in_ind);
960 length = ch->scan_type.storagebits/8;
961 /* Make sure we are aligned */
962 in_loc += length;
963 if (in_loc % length)
964 in_loc += length - in_loc % length;
965 }
966 p = kmalloc(sizeof(*p), GFP_KERNEL);
967 if (p == NULL) {
968 ret = -ENOMEM;
969 goto error_clear_mux_table;
970 }
971 ch = iio_find_channel_from_si(indio_dev, in_ind);
972 length = ch->scan_type.storagebits/8;
973 if (out_loc % length)
974 out_loc += length - out_loc % length;
975 if (in_loc % length)
976 in_loc += length - in_loc % length;
977 p->from = in_loc;
978 p->to = out_loc;
979 p->length = length;
980 list_add_tail(&p->l, &buffer->demux_list);
981 out_loc += length;
982 in_loc += length;
983 }
984 /* Relies on scan_timestamp being last */
985 if (buffer->scan_timestamp) {
986 p = kmalloc(sizeof(*p), GFP_KERNEL);
987 if (p == NULL) {
988 ret = -ENOMEM;
989 goto error_clear_mux_table;
990 }
991 ch = iio_find_channel_from_si(indio_dev,
Jonathan Cameronf1264802012-04-21 10:09:34 +0100992 indio_dev->scan_index_timestamp);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000993 length = ch->scan_type.storagebits/8;
994 if (out_loc % length)
995 out_loc += length - out_loc % length;
996 if (in_loc % length)
997 in_loc += length - in_loc % length;
998 p->from = in_loc;
999 p->to = out_loc;
1000 p->length = length;
1001 list_add_tail(&p->l, &buffer->demux_list);
1002 out_loc += length;
1003 in_loc += length;
1004 }
1005 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
1006 if (buffer->demux_bounce == NULL) {
1007 ret = -ENOMEM;
1008 goto error_clear_mux_table;
1009 }
1010 return 0;
1011
1012error_clear_mux_table:
Jonathan Cameron842cd102012-04-21 10:09:45 +01001013 iio_buffer_demux_free(buffer);
1014
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001015 return ret;
1016}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +01001017
1018int iio_update_demux(struct iio_dev *indio_dev)
1019{
1020 struct iio_buffer *buffer;
1021 int ret;
1022
1023 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
1024 ret = iio_buffer_update_demux(indio_dev, buffer);
1025 if (ret < 0)
1026 goto error_clear_mux_table;
1027 }
1028 return 0;
1029
1030error_clear_mux_table:
1031 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
1032 iio_buffer_demux_free(buffer);
1033
1034 return ret;
1035}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +00001036EXPORT_SYMBOL_GPL(iio_update_demux);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +01001037
1038/**
1039 * iio_buffer_release() - Free a buffer's resources
1040 * @ref: Pointer to the kref embedded in the iio_buffer struct
1041 *
1042 * This function is called when the last reference to the buffer has been
1043 * dropped. It will typically free all resources allocated by the buffer. Do not
1044 * call this function manually, always use iio_buffer_put() when done using a
1045 * buffer.
1046 */
1047static void iio_buffer_release(struct kref *ref)
1048{
1049 struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
1050
1051 buffer->access->release(buffer);
1052}
1053
1054/**
1055 * iio_buffer_get() - Grab a reference to the buffer
1056 * @buffer: The buffer to grab a reference for, may be NULL
1057 *
1058 * Returns the pointer to the buffer that was passed into the function.
1059 */
1060struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
1061{
1062 if (buffer)
1063 kref_get(&buffer->ref);
1064
1065 return buffer;
1066}
1067EXPORT_SYMBOL_GPL(iio_buffer_get);
1068
1069/**
1070 * iio_buffer_put() - Release the reference to the buffer
1071 * @buffer: The buffer to release the reference for, may be NULL
1072 */
1073void iio_buffer_put(struct iio_buffer *buffer)
1074{
1075 if (buffer)
1076 kref_put(&buffer->ref, iio_buffer_release);
1077}
1078EXPORT_SYMBOL_GPL(iio_buffer_put);