blob: 36c39dcad850497c47da6e736dd76097568380ca [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>
23
Jonathan Cameron06458e22012-04-25 15:54:58 +010024#include <linux/iio/iio.h>
Jonathan Cameron14555b12011-09-21 11:15:57 +010025#include "iio_core.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010026#include <linux/iio/sysfs.h>
27#include <linux/iio/buffer.h>
Jonathan Cameron14555b12011-09-21 11:15:57 +010028
29static const char * const iio_endian_prefix[] = {
30 [IIO_BE] = "be",
31 [IIO_LE] = "le",
32};
33
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +010034static bool iio_buffer_is_active(struct iio_buffer *buf)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010035{
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +010036 return !list_empty(&buf->buffer_list);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010037}
38
Jonathan Cameron14555b12011-09-21 11:15:57 +010039/**
40 * iio_buffer_read_first_n_outer() - chrdev read for buffer access
41 *
42 * This function relies on all buffer implementations having an
43 * iio_buffer as their first element.
44 **/
45ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
46 size_t n, loff_t *f_ps)
47{
48 struct iio_dev *indio_dev = filp->private_data;
49 struct iio_buffer *rb = indio_dev->buffer;
50
Jonathan Cameron96e00f12011-10-26 17:27:45 +010051 if (!rb || !rb->access->read_first_n)
Jonathan Cameron14555b12011-09-21 11:15:57 +010052 return -EINVAL;
53 return rb->access->read_first_n(rb, n, buf);
54}
55
56/**
57 * iio_buffer_poll() - poll the buffer to find out if it has data
58 */
59unsigned int iio_buffer_poll(struct file *filp,
60 struct poll_table_struct *wait)
61{
62 struct iio_dev *indio_dev = filp->private_data;
63 struct iio_buffer *rb = indio_dev->buffer;
64
65 poll_wait(filp, &rb->pollq, wait);
66 if (rb->stufftoread)
67 return POLLIN | POLLRDNORM;
68 /* need a way of knowing if there may be enough data... */
69 return 0;
70}
71
Jonathan Cameronf79a9092011-12-05 22:18:29 +000072void iio_buffer_init(struct iio_buffer *buffer)
Jonathan Cameron14555b12011-09-21 11:15:57 +010073{
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +000074 INIT_LIST_HEAD(&buffer->demux_list);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +010075 INIT_LIST_HEAD(&buffer->buffer_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +010076 init_waitqueue_head(&buffer->pollq);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +010077 kref_init(&buffer->ref);
Jonathan Cameron14555b12011-09-21 11:15:57 +010078}
79EXPORT_SYMBOL(iio_buffer_init);
80
81static ssize_t iio_show_scan_index(struct device *dev,
82 struct device_attribute *attr,
83 char *buf)
84{
85 return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
86}
87
88static ssize_t iio_show_fixed_type(struct device *dev,
89 struct device_attribute *attr,
90 char *buf)
91{
92 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
93 u8 type = this_attr->c->scan_type.endianness;
94
95 if (type == IIO_CPU) {
Jonathan Cameron9d5d1152011-10-04 16:02:08 +010096#ifdef __LITTLE_ENDIAN
97 type = IIO_LE;
98#else
99 type = IIO_BE;
100#endif
Jonathan Cameron14555b12011-09-21 11:15:57 +0100101 }
102 return sprintf(buf, "%s:%c%d/%d>>%u\n",
103 iio_endian_prefix[type],
104 this_attr->c->scan_type.sign,
105 this_attr->c->scan_type.realbits,
106 this_attr->c->scan_type.storagebits,
107 this_attr->c->scan_type.shift);
108}
109
110static ssize_t iio_scan_el_show(struct device *dev,
111 struct device_attribute *attr,
112 char *buf)
113{
114 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200115 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100116
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000117 ret = test_bit(to_iio_dev_attr(attr)->address,
118 indio_dev->buffer->scan_mask);
119
Jonathan Cameron14555b12011-09-21 11:15:57 +0100120 return sprintf(buf, "%d\n", ret);
121}
122
123static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
124{
125 clear_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100126 return 0;
127}
128
129static ssize_t iio_scan_el_store(struct device *dev,
130 struct device_attribute *attr,
131 const char *buf,
132 size_t len)
133{
Jonathan Camerona714af22012-04-21 10:09:32 +0100134 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100135 bool state;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200136 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100137 struct iio_buffer *buffer = indio_dev->buffer;
138 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
139
Jonathan Camerona714af22012-04-21 10:09:32 +0100140 ret = strtobool(buf, &state);
141 if (ret < 0)
142 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100143 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100144 if (iio_buffer_is_active(indio_dev->buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100145 ret = -EBUSY;
146 goto error_ret;
147 }
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000148 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100149 if (ret < 0)
150 goto error_ret;
151 if (!state && ret) {
152 ret = iio_scan_mask_clear(buffer, this_attr->address);
153 if (ret)
154 goto error_ret;
155 } else if (state && !ret) {
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000156 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100157 if (ret)
158 goto error_ret;
159 }
160
161error_ret:
162 mutex_unlock(&indio_dev->mlock);
163
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100164 return ret < 0 ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100165
166}
167
168static ssize_t iio_scan_el_ts_show(struct device *dev,
169 struct device_attribute *attr,
170 char *buf)
171{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200172 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100173 return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100174}
175
176static ssize_t iio_scan_el_ts_store(struct device *dev,
177 struct device_attribute *attr,
178 const char *buf,
179 size_t len)
180{
Jonathan Camerona714af22012-04-21 10:09:32 +0100181 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200182 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100183 bool state;
184
Jonathan Camerona714af22012-04-21 10:09:32 +0100185 ret = strtobool(buf, &state);
186 if (ret < 0)
187 return ret;
188
Jonathan Cameron14555b12011-09-21 11:15:57 +0100189 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100190 if (iio_buffer_is_active(indio_dev->buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100191 ret = -EBUSY;
192 goto error_ret;
193 }
194 indio_dev->buffer->scan_timestamp = state;
195error_ret:
196 mutex_unlock(&indio_dev->mlock);
197
198 return ret ? ret : len;
199}
200
201static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
202 const struct iio_chan_spec *chan)
203{
204 int ret, attrcount = 0;
205 struct iio_buffer *buffer = indio_dev->buffer;
206
207 ret = __iio_add_chan_devattr("index",
208 chan,
209 &iio_show_scan_index,
210 NULL,
211 0,
Jonathan Cameron37044322013-09-08 14:57:00 +0100212 IIO_SEPARATE,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100213 &indio_dev->dev,
214 &buffer->scan_el_dev_attr_list);
215 if (ret)
216 goto error_ret;
217 attrcount++;
218 ret = __iio_add_chan_devattr("type",
219 chan,
220 &iio_show_fixed_type,
221 NULL,
222 0,
223 0,
224 &indio_dev->dev,
225 &buffer->scan_el_dev_attr_list);
226 if (ret)
227 goto error_ret;
228 attrcount++;
229 if (chan->type != IIO_TIMESTAMP)
230 ret = __iio_add_chan_devattr("en",
231 chan,
232 &iio_scan_el_show,
233 &iio_scan_el_store,
234 chan->scan_index,
235 0,
236 &indio_dev->dev,
237 &buffer->scan_el_dev_attr_list);
238 else
239 ret = __iio_add_chan_devattr("en",
240 chan,
241 &iio_scan_el_ts_show,
242 &iio_scan_el_ts_store,
243 chan->scan_index,
244 0,
245 &indio_dev->dev,
246 &buffer->scan_el_dev_attr_list);
Peter Meerwald95725882013-09-17 23:42:00 +0100247 if (ret)
248 goto error_ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100249 attrcount++;
250 ret = attrcount;
251error_ret:
252 return ret;
253}
254
255static void iio_buffer_remove_and_free_scan_dev_attr(struct iio_dev *indio_dev,
256 struct iio_dev_attr *p)
257{
258 kfree(p->dev_attr.attr.name);
259 kfree(p);
260}
261
262static void __iio_buffer_attr_cleanup(struct iio_dev *indio_dev)
263{
264 struct iio_dev_attr *p, *n;
265 struct iio_buffer *buffer = indio_dev->buffer;
266
267 list_for_each_entry_safe(p, n,
268 &buffer->scan_el_dev_attr_list, l)
269 iio_buffer_remove_and_free_scan_dev_attr(indio_dev, p);
270}
271
272static const char * const iio_scan_elements_group_name = "scan_elements";
273
274int iio_buffer_register(struct iio_dev *indio_dev,
275 const struct iio_chan_spec *channels,
276 int num_channels)
277{
278 struct iio_dev_attr *p;
279 struct attribute **attr;
280 struct iio_buffer *buffer = indio_dev->buffer;
281 int ret, i, attrn, attrcount, attrcount_orig = 0;
282
283 if (buffer->attrs)
284 indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs;
285
286 if (buffer->scan_el_attrs != NULL) {
287 attr = buffer->scan_el_attrs->attrs;
288 while (*attr++ != NULL)
289 attrcount_orig++;
290 }
291 attrcount = attrcount_orig;
292 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
293 if (channels) {
294 /* new magic */
295 for (i = 0; i < num_channels; i++) {
Lars-Peter Clausenf5b81dd2012-06-18 18:33:47 +0200296 if (channels[i].scan_index < 0)
297 continue;
298
Jonathan Cameron14555b12011-09-21 11:15:57 +0100299 /* Establish necessary mask length */
300 if (channels[i].scan_index >
301 (int)indio_dev->masklength - 1)
302 indio_dev->masklength
Lars-Peter Clausene1dc7be2012-07-02 14:52:56 +0200303 = channels[i].scan_index + 1;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100304
305 ret = iio_buffer_add_channel_sysfs(indio_dev,
306 &channels[i]);
307 if (ret < 0)
308 goto error_cleanup_dynamic;
309 attrcount += ret;
Jonathan Cameronbeb80602011-12-05 21:37:11 +0000310 if (channels[i].type == IIO_TIMESTAMP)
Jonathan Cameronf1264802012-04-21 10:09:34 +0100311 indio_dev->scan_index_timestamp =
Jonathan Cameronbeb80602011-12-05 21:37:11 +0000312 channels[i].scan_index;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100313 }
314 if (indio_dev->masklength && buffer->scan_mask == NULL) {
Thomas Meyerd83fb182011-11-29 22:08:00 +0100315 buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
316 sizeof(*buffer->scan_mask),
317 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100318 if (buffer->scan_mask == NULL) {
319 ret = -ENOMEM;
320 goto error_cleanup_dynamic;
321 }
322 }
323 }
324
325 buffer->scan_el_group.name = iio_scan_elements_group_name;
326
Thomas Meyerd83fb182011-11-29 22:08:00 +0100327 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
328 sizeof(buffer->scan_el_group.attrs[0]),
329 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100330 if (buffer->scan_el_group.attrs == NULL) {
331 ret = -ENOMEM;
332 goto error_free_scan_mask;
333 }
334 if (buffer->scan_el_attrs)
335 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
336 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
337 attrn = attrcount_orig;
338
339 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
340 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
341 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
342
343 return 0;
344
345error_free_scan_mask:
346 kfree(buffer->scan_mask);
347error_cleanup_dynamic:
348 __iio_buffer_attr_cleanup(indio_dev);
349
350 return ret;
351}
352EXPORT_SYMBOL(iio_buffer_register);
353
354void iio_buffer_unregister(struct iio_dev *indio_dev)
355{
356 kfree(indio_dev->buffer->scan_mask);
357 kfree(indio_dev->buffer->scan_el_group.attrs);
358 __iio_buffer_attr_cleanup(indio_dev);
359}
360EXPORT_SYMBOL(iio_buffer_unregister);
361
362ssize_t iio_buffer_read_length(struct device *dev,
363 struct device_attribute *attr,
364 char *buf)
365{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200366 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100367 struct iio_buffer *buffer = indio_dev->buffer;
368
369 if (buffer->access->get_length)
370 return sprintf(buf, "%d\n",
371 buffer->access->get_length(buffer));
372
373 return 0;
374}
375EXPORT_SYMBOL(iio_buffer_read_length);
376
377ssize_t iio_buffer_write_length(struct device *dev,
378 struct device_attribute *attr,
379 const char *buf,
380 size_t len)
381{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200382 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100383 struct iio_buffer *buffer = indio_dev->buffer;
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100384 unsigned int val;
385 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100386
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100387 ret = kstrtouint(buf, 10, &val);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100388 if (ret)
389 return ret;
390
391 if (buffer->access->get_length)
392 if (val == buffer->access->get_length(buffer))
393 return len;
394
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100395 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100396 if (iio_buffer_is_active(indio_dev->buffer)) {
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100397 ret = -EBUSY;
398 } else {
Lars-Peter Clausen869871b2011-12-19 15:23:48 +0100399 if (buffer->access->set_length)
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100400 buffer->access->set_length(buffer, val);
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100401 ret = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100402 }
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100403 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100404
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100405 return ret ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100406}
407EXPORT_SYMBOL(iio_buffer_write_length);
408
Jonathan Cameron14555b12011-09-21 11:15:57 +0100409ssize_t iio_buffer_show_enable(struct device *dev,
410 struct device_attribute *attr,
411 char *buf)
412{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200413 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100414 return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
Jonathan Cameron14555b12011-09-21 11:15:57 +0100415}
416EXPORT_SYMBOL(iio_buffer_show_enable);
417
Peter Meerwald95725882013-09-17 23:42:00 +0100418/* Note NULL used as error indicator as it doesn't make sense. */
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100419static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100420 unsigned int masklength,
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100421 const unsigned long *mask)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100422{
423 if (bitmap_empty(mask, masklength))
424 return NULL;
425 while (*av_masks) {
426 if (bitmap_subset(mask, av_masks, masklength))
427 return av_masks;
428 av_masks += BITS_TO_LONGS(masklength);
429 }
430 return NULL;
431}
432
Peter Meerwald183f4172013-09-18 22:10:00 +0100433static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
434 const unsigned long *mask, bool timestamp)
Jonathan Cameron959d2952011-12-05 21:37:13 +0000435{
Jonathan Cameron959d2952011-12-05 21:37:13 +0000436 const struct iio_chan_spec *ch;
437 unsigned bytes = 0;
438 int length, i;
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100439
440 /* How much space will the demuxed element take? */
441 for_each_set_bit(i, mask,
442 indio_dev->masklength) {
443 ch = iio_find_channel_from_si(indio_dev, i);
444 length = ch->scan_type.storagebits / 8;
445 bytes = ALIGN(bytes, length);
446 bytes += length;
447 }
448 if (timestamp) {
449 ch = iio_find_channel_from_si(indio_dev,
Jonathan Cameronf1264802012-04-21 10:09:34 +0100450 indio_dev->scan_index_timestamp);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100451 length = ch->scan_type.storagebits / 8;
452 bytes = ALIGN(bytes, length);
453 bytes += length;
454 }
455 return bytes;
456}
457
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100458static void iio_buffer_activate(struct iio_dev *indio_dev,
459 struct iio_buffer *buffer)
460{
461 iio_buffer_get(buffer);
462 list_add(&buffer->buffer_list, &indio_dev->buffer_list);
463}
464
465static void iio_buffer_deactivate(struct iio_buffer *buffer)
466{
467 list_del_init(&buffer->buffer_list);
468 iio_buffer_put(buffer);
469}
470
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100471void iio_disable_all_buffers(struct iio_dev *indio_dev)
472{
473 struct iio_buffer *buffer, *_buffer;
474
475 if (list_empty(&indio_dev->buffer_list))
476 return;
477
478 if (indio_dev->setup_ops->predisable)
479 indio_dev->setup_ops->predisable(indio_dev);
480
481 list_for_each_entry_safe(buffer, _buffer,
482 &indio_dev->buffer_list, buffer_list)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100483 iio_buffer_deactivate(buffer);
Lars-Peter Clausena87c82e2013-09-18 21:02:00 +0100484
485 indio_dev->currentmode = INDIO_DIRECT_MODE;
486 if (indio_dev->setup_ops->postdisable)
487 indio_dev->setup_ops->postdisable(indio_dev);
488}
489
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100490int iio_update_buffers(struct iio_dev *indio_dev,
491 struct iio_buffer *insert_buffer,
492 struct iio_buffer *remove_buffer)
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100493{
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100494 int ret;
495 int success = 0;
496 struct iio_buffer *buffer;
497 unsigned long *compound_mask;
498 const unsigned long *old_mask;
Jonathan Cameron959d2952011-12-05 21:37:13 +0000499
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100500 /* Wind down existing buffers - iff there are any */
501 if (!list_empty(&indio_dev->buffer_list)) {
502 if (indio_dev->setup_ops->predisable) {
503 ret = indio_dev->setup_ops->predisable(indio_dev);
504 if (ret)
505 goto error_ret;
506 }
507 indio_dev->currentmode = INDIO_DIRECT_MODE;
508 if (indio_dev->setup_ops->postdisable) {
509 ret = indio_dev->setup_ops->postdisable(indio_dev);
510 if (ret)
511 goto error_ret;
512 }
513 }
514 /* Keep a copy of current setup to allow roll back */
515 old_mask = indio_dev->active_scan_mask;
516 if (!indio_dev->available_scan_masks)
517 indio_dev->active_scan_mask = NULL;
518
519 if (remove_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100520 iio_buffer_deactivate(remove_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100521 if (insert_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100522 iio_buffer_activate(indio_dev, insert_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100523
524 /* If no buffers in list, we are done */
525 if (list_empty(&indio_dev->buffer_list)) {
526 indio_dev->currentmode = INDIO_DIRECT_MODE;
527 if (indio_dev->available_scan_masks == NULL)
528 kfree(old_mask);
529 return 0;
530 }
Jonathan Cameron959d2952011-12-05 21:37:13 +0000531
Peter Meerwald95725882013-09-17 23:42:00 +0100532 /* What scan mask do we actually have? */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100533 compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
534 sizeof(long), GFP_KERNEL);
535 if (compound_mask == NULL) {
536 if (indio_dev->available_scan_masks == NULL)
537 kfree(old_mask);
538 return -ENOMEM;
539 }
540 indio_dev->scan_timestamp = 0;
541
542 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
543 bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
544 indio_dev->masklength);
545 indio_dev->scan_timestamp |= buffer->scan_timestamp;
546 }
547 if (indio_dev->available_scan_masks) {
Jonathan Cameron959d2952011-12-05 21:37:13 +0000548 indio_dev->active_scan_mask =
549 iio_scan_mask_match(indio_dev->available_scan_masks,
550 indio_dev->masklength,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100551 compound_mask);
552 if (indio_dev->active_scan_mask == NULL) {
553 /*
554 * Roll back.
555 * Note can only occur when adding a buffer.
556 */
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100557 iio_buffer_deactivate(insert_buffer);
Peter Meerwaldd66e0452013-09-18 22:10:00 +0100558 if (old_mask) {
559 indio_dev->active_scan_mask = old_mask;
560 success = -EINVAL;
561 }
562 else {
563 kfree(compound_mask);
564 ret = -EINVAL;
565 goto error_ret;
566 }
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100567 }
568 } else {
569 indio_dev->active_scan_mask = compound_mask;
570 }
Lars-Peter Clausenaff1eb42012-06-15 18:08:59 +0200571
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000572 iio_update_demux(indio_dev);
573
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100574 /* Wind up again */
575 if (indio_dev->setup_ops->preenable) {
576 ret = indio_dev->setup_ops->preenable(indio_dev);
577 if (ret) {
578 printk(KERN_ERR
Michał Mirosławbec18892013-05-04 14:19:00 +0100579 "Buffer not started: buffer preenable failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100580 goto error_remove_inserted;
581 }
582 }
583 indio_dev->scan_bytes =
584 iio_compute_scan_bytes(indio_dev,
585 indio_dev->active_scan_mask,
586 indio_dev->scan_timestamp);
587 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
588 if (buffer->access->request_update) {
589 ret = buffer->access->request_update(buffer);
590 if (ret) {
591 printk(KERN_INFO
Michał Mirosławbec18892013-05-04 14:19:00 +0100592 "Buffer not started: buffer parameter update failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100593 goto error_run_postdisable;
594 }
595 }
596 if (indio_dev->info->update_scan_mode) {
597 ret = indio_dev->info
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000598 ->update_scan_mode(indio_dev,
599 indio_dev->active_scan_mask);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100600 if (ret < 0) {
Michał Mirosławbec18892013-05-04 14:19:00 +0100601 printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100602 goto error_run_postdisable;
603 }
604 }
Peter Meerwald95725882013-09-17 23:42:00 +0100605 /* Definitely possible for devices to support both of these. */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100606 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
607 if (!indio_dev->trig) {
608 printk(KERN_INFO "Buffer not started: no trigger\n");
609 ret = -EINVAL;
610 /* Can only occur on first buffer */
611 goto error_run_postdisable;
612 }
613 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
614 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) {
615 indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
Peter Meerwald95725882013-09-17 23:42:00 +0100616 } else { /* Should never be reached */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100617 ret = -EINVAL;
618 goto error_run_postdisable;
619 }
620
621 if (indio_dev->setup_ops->postenable) {
622 ret = indio_dev->setup_ops->postenable(indio_dev);
623 if (ret) {
624 printk(KERN_INFO
Michał Mirosławbec18892013-05-04 14:19:00 +0100625 "Buffer not started: postenable failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100626 indio_dev->currentmode = INDIO_DIRECT_MODE;
627 if (indio_dev->setup_ops->postdisable)
628 indio_dev->setup_ops->postdisable(indio_dev);
629 goto error_disable_all_buffers;
630 }
631 }
632
633 if (indio_dev->available_scan_masks)
634 kfree(compound_mask);
635 else
636 kfree(old_mask);
637
638 return success;
639
640error_disable_all_buffers:
641 indio_dev->currentmode = INDIO_DIRECT_MODE;
642error_run_postdisable:
643 if (indio_dev->setup_ops->postdisable)
644 indio_dev->setup_ops->postdisable(indio_dev);
645error_remove_inserted:
646
647 if (insert_buffer)
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100648 iio_buffer_deactivate(insert_buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100649 indio_dev->active_scan_mask = old_mask;
650 kfree(compound_mask);
651error_ret:
652
653 return ret;
654}
655EXPORT_SYMBOL_GPL(iio_update_buffers);
656
657ssize_t iio_buffer_store_enable(struct device *dev,
658 struct device_attribute *attr,
659 const char *buf,
660 size_t len)
661{
662 int ret;
663 bool requested_state;
664 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100665 bool inlist;
666
667 ret = strtobool(buf, &requested_state);
668 if (ret < 0)
669 return ret;
670
671 mutex_lock(&indio_dev->mlock);
672
673 /* Find out if it is in the list */
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100674 inlist = iio_buffer_is_active(indio_dev->buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100675 /* Already in desired state */
676 if (inlist == requested_state)
677 goto done;
678
679 if (requested_state)
680 ret = iio_update_buffers(indio_dev,
681 indio_dev->buffer, NULL);
682 else
683 ret = iio_update_buffers(indio_dev,
684 NULL, indio_dev->buffer);
685
686 if (ret < 0)
687 goto done;
688done:
689 mutex_unlock(&indio_dev->mlock);
690 return (ret < 0) ? ret : len;
691}
692EXPORT_SYMBOL(iio_buffer_store_enable);
693
694int iio_sw_buffer_preenable(struct iio_dev *indio_dev)
695{
696 struct iio_buffer *buffer;
697 unsigned bytes;
698 dev_dbg(&indio_dev->dev, "%s\n", __func__);
699
700 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
701 if (buffer->access->set_bytes_per_datum) {
702 bytes = iio_compute_scan_bytes(indio_dev,
703 buffer->scan_mask,
704 buffer->scan_timestamp);
705
706 buffer->access->set_bytes_per_datum(buffer, bytes);
707 }
Jonathan Cameron959d2952011-12-05 21:37:13 +0000708 return 0;
709}
710EXPORT_SYMBOL(iio_sw_buffer_preenable);
711
Jonathan Cameron14555b12011-09-21 11:15:57 +0100712/**
Lars-Peter Clausen81636632012-07-09 10:00:00 +0100713 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
714 * @indio_dev: the iio device
715 * @mask: scan mask to be checked
716 *
717 * Return true if exactly one bit is set in the scan mask, false otherwise. It
718 * can be used for devices where only one channel can be active for sampling at
719 * a time.
720 */
721bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
722 const unsigned long *mask)
723{
724 return bitmap_weight(mask, indio_dev->masklength) == 1;
725}
726EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
727
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100728static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
729 const unsigned long *mask)
730{
731 if (!indio_dev->setup_ops->validate_scan_mask)
732 return true;
733
734 return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
735}
736
Jonathan Cameron14555b12011-09-21 11:15:57 +0100737/**
738 * iio_scan_mask_set() - set particular bit in the scan mask
Peter Meerwald95725882013-09-17 23:42:00 +0100739 * @indio_dev: the iio device
Jonathan Cameron14555b12011-09-21 11:15:57 +0100740 * @buffer: the buffer whose scan mask we are interested in
741 * @bit: the bit to be set.
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100742 *
743 * Note that at this point we have no way of knowing what other
744 * buffers might request, hence this code only verifies that the
745 * individual buffers request is plausible.
746 */
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000747int iio_scan_mask_set(struct iio_dev *indio_dev,
748 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100749{
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100750 const unsigned long *mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100751 unsigned long *trialmask;
752
753 trialmask = kmalloc(sizeof(*trialmask)*
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100754 BITS_TO_LONGS(indio_dev->masklength),
Jonathan Cameron14555b12011-09-21 11:15:57 +0100755 GFP_KERNEL);
756
757 if (trialmask == NULL)
758 return -ENOMEM;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100759 if (!indio_dev->masklength) {
Peter Meerwald95725882013-09-17 23:42:00 +0100760 WARN_ON("Trying to set scanmask prior to registering buffer\n");
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100761 goto err_invalid_mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100762 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100763 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100764 set_bit(bit, trialmask);
765
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100766 if (!iio_validate_scan_mask(indio_dev, trialmask))
767 goto err_invalid_mask;
768
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100769 if (indio_dev->available_scan_masks) {
770 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
771 indio_dev->masklength,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100772 trialmask);
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100773 if (!mask)
774 goto err_invalid_mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100775 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100776 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100777
778 kfree(trialmask);
779
780 return 0;
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100781
782err_invalid_mask:
783 kfree(trialmask);
784 return -EINVAL;
785}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100786EXPORT_SYMBOL_GPL(iio_scan_mask_set);
787
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000788int iio_scan_mask_query(struct iio_dev *indio_dev,
789 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100790{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100791 if (bit > indio_dev->masklength)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100792 return -EINVAL;
793
794 if (!buffer->scan_mask)
795 return 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100796
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100797 return test_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100798};
799EXPORT_SYMBOL_GPL(iio_scan_mask_query);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000800
801/**
802 * struct iio_demux_table() - table describing demux memcpy ops
803 * @from: index to copy from
Peter Meerwald99698b42012-08-26 13:43:00 +0100804 * @to: index to copy to
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000805 * @length: how many bytes to copy
806 * @l: list head used for management
807 */
808struct iio_demux_table {
809 unsigned from;
810 unsigned to;
811 unsigned length;
812 struct list_head l;
813};
814
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100815static const void *iio_demux(struct iio_buffer *buffer,
816 const void *datain)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000817{
818 struct iio_demux_table *t;
819
820 if (list_empty(&buffer->demux_list))
821 return datain;
822 list_for_each_entry(t, &buffer->demux_list, l)
823 memcpy(buffer->demux_bounce + t->to,
824 datain + t->from, t->length);
825
826 return buffer->demux_bounce;
827}
828
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100829static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000830{
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100831 const void *dataout = iio_demux(buffer, data);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000832
Lars-Peter Clausence56ade2012-09-04 13:38:00 +0100833 return buffer->access->store_to(buffer, dataout);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000834}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000835
Jonathan Cameron842cd102012-04-21 10:09:45 +0100836static void iio_buffer_demux_free(struct iio_buffer *buffer)
837{
838 struct iio_demux_table *p, *q;
839 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
840 list_del(&p->l);
841 kfree(p);
842 }
843}
844
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100845
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100846int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100847{
848 int ret;
849 struct iio_buffer *buf;
850
851 list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
852 ret = iio_push_to_buffer(buf, data);
853 if (ret < 0)
854 return ret;
855 }
856
857 return 0;
858}
859EXPORT_SYMBOL_GPL(iio_push_to_buffers);
860
861static int iio_buffer_update_demux(struct iio_dev *indio_dev,
862 struct iio_buffer *buffer)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000863{
864 const struct iio_chan_spec *ch;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000865 int ret, in_ind = -1, out_ind, length;
866 unsigned in_loc = 0, out_loc = 0;
Jonathan Cameron842cd102012-04-21 10:09:45 +0100867 struct iio_demux_table *p;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000868
869 /* Clear out any old demux */
Jonathan Cameron842cd102012-04-21 10:09:45 +0100870 iio_buffer_demux_free(buffer);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000871 kfree(buffer->demux_bounce);
872 buffer->demux_bounce = NULL;
873
874 /* First work out which scan mode we will actually have */
875 if (bitmap_equal(indio_dev->active_scan_mask,
876 buffer->scan_mask,
877 indio_dev->masklength))
878 return 0;
879
880 /* Now we have the two masks, work from least sig and build up sizes */
881 for_each_set_bit(out_ind,
882 indio_dev->active_scan_mask,
883 indio_dev->masklength) {
884 in_ind = find_next_bit(indio_dev->active_scan_mask,
885 indio_dev->masklength,
886 in_ind + 1);
887 while (in_ind != out_ind) {
888 in_ind = find_next_bit(indio_dev->active_scan_mask,
889 indio_dev->masklength,
890 in_ind + 1);
891 ch = iio_find_channel_from_si(indio_dev, in_ind);
892 length = ch->scan_type.storagebits/8;
893 /* Make sure we are aligned */
894 in_loc += length;
895 if (in_loc % length)
896 in_loc += length - in_loc % length;
897 }
898 p = kmalloc(sizeof(*p), GFP_KERNEL);
899 if (p == NULL) {
900 ret = -ENOMEM;
901 goto error_clear_mux_table;
902 }
903 ch = iio_find_channel_from_si(indio_dev, in_ind);
904 length = ch->scan_type.storagebits/8;
905 if (out_loc % length)
906 out_loc += length - out_loc % length;
907 if (in_loc % length)
908 in_loc += length - in_loc % length;
909 p->from = in_loc;
910 p->to = out_loc;
911 p->length = length;
912 list_add_tail(&p->l, &buffer->demux_list);
913 out_loc += length;
914 in_loc += length;
915 }
916 /* Relies on scan_timestamp being last */
917 if (buffer->scan_timestamp) {
918 p = kmalloc(sizeof(*p), GFP_KERNEL);
919 if (p == NULL) {
920 ret = -ENOMEM;
921 goto error_clear_mux_table;
922 }
923 ch = iio_find_channel_from_si(indio_dev,
Jonathan Cameronf1264802012-04-21 10:09:34 +0100924 indio_dev->scan_index_timestamp);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000925 length = ch->scan_type.storagebits/8;
926 if (out_loc % length)
927 out_loc += length - out_loc % length;
928 if (in_loc % length)
929 in_loc += length - in_loc % length;
930 p->from = in_loc;
931 p->to = out_loc;
932 p->length = length;
933 list_add_tail(&p->l, &buffer->demux_list);
934 out_loc += length;
935 in_loc += length;
936 }
937 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
938 if (buffer->demux_bounce == NULL) {
939 ret = -ENOMEM;
940 goto error_clear_mux_table;
941 }
942 return 0;
943
944error_clear_mux_table:
Jonathan Cameron842cd102012-04-21 10:09:45 +0100945 iio_buffer_demux_free(buffer);
946
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000947 return ret;
948}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100949
950int iio_update_demux(struct iio_dev *indio_dev)
951{
952 struct iio_buffer *buffer;
953 int ret;
954
955 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
956 ret = iio_buffer_update_demux(indio_dev, buffer);
957 if (ret < 0)
958 goto error_clear_mux_table;
959 }
960 return 0;
961
962error_clear_mux_table:
963 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
964 iio_buffer_demux_free(buffer);
965
966 return ret;
967}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000968EXPORT_SYMBOL_GPL(iio_update_demux);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100969
970/**
971 * iio_buffer_release() - Free a buffer's resources
972 * @ref: Pointer to the kref embedded in the iio_buffer struct
973 *
974 * This function is called when the last reference to the buffer has been
975 * dropped. It will typically free all resources allocated by the buffer. Do not
976 * call this function manually, always use iio_buffer_put() when done using a
977 * buffer.
978 */
979static void iio_buffer_release(struct kref *ref)
980{
981 struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
982
983 buffer->access->release(buffer);
984}
985
986/**
987 * iio_buffer_get() - Grab a reference to the buffer
988 * @buffer: The buffer to grab a reference for, may be NULL
989 *
990 * Returns the pointer to the buffer that was passed into the function.
991 */
992struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
993{
994 if (buffer)
995 kref_get(&buffer->ref);
996
997 return buffer;
998}
999EXPORT_SYMBOL_GPL(iio_buffer_get);
1000
1001/**
1002 * iio_buffer_put() - Release the reference to the buffer
1003 * @buffer: The buffer to release the reference for, may be NULL
1004 */
1005void iio_buffer_put(struct iio_buffer *buffer)
1006{
1007 if (buffer)
1008 kref_put(&buffer->ref, iio_buffer_release);
1009}
1010EXPORT_SYMBOL_GPL(iio_buffer_put);