blob: 7ea2edbf76140dab44f1fe360d78c047120989af [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);
77}
78EXPORT_SYMBOL(iio_buffer_init);
79
80static ssize_t iio_show_scan_index(struct device *dev,
81 struct device_attribute *attr,
82 char *buf)
83{
84 return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
85}
86
87static ssize_t iio_show_fixed_type(struct device *dev,
88 struct device_attribute *attr,
89 char *buf)
90{
91 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
92 u8 type = this_attr->c->scan_type.endianness;
93
94 if (type == IIO_CPU) {
Jonathan Cameron9d5d1152011-10-04 16:02:08 +010095#ifdef __LITTLE_ENDIAN
96 type = IIO_LE;
97#else
98 type = IIO_BE;
99#endif
Jonathan Cameron14555b12011-09-21 11:15:57 +0100100 }
101 return sprintf(buf, "%s:%c%d/%d>>%u\n",
102 iio_endian_prefix[type],
103 this_attr->c->scan_type.sign,
104 this_attr->c->scan_type.realbits,
105 this_attr->c->scan_type.storagebits,
106 this_attr->c->scan_type.shift);
107}
108
109static ssize_t iio_scan_el_show(struct device *dev,
110 struct device_attribute *attr,
111 char *buf)
112{
113 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200114 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100115
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000116 ret = test_bit(to_iio_dev_attr(attr)->address,
117 indio_dev->buffer->scan_mask);
118
Jonathan Cameron14555b12011-09-21 11:15:57 +0100119 return sprintf(buf, "%d\n", ret);
120}
121
122static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
123{
124 clear_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100125 return 0;
126}
127
128static ssize_t iio_scan_el_store(struct device *dev,
129 struct device_attribute *attr,
130 const char *buf,
131 size_t len)
132{
Jonathan Camerona714af22012-04-21 10:09:32 +0100133 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100134 bool state;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200135 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100136 struct iio_buffer *buffer = indio_dev->buffer;
137 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
138
Jonathan Camerona714af22012-04-21 10:09:32 +0100139 ret = strtobool(buf, &state);
140 if (ret < 0)
141 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100142 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100143 if (iio_buffer_is_active(indio_dev->buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100144 ret = -EBUSY;
145 goto error_ret;
146 }
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000147 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100148 if (ret < 0)
149 goto error_ret;
150 if (!state && ret) {
151 ret = iio_scan_mask_clear(buffer, this_attr->address);
152 if (ret)
153 goto error_ret;
154 } else if (state && !ret) {
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000155 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100156 if (ret)
157 goto error_ret;
158 }
159
160error_ret:
161 mutex_unlock(&indio_dev->mlock);
162
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100163 return ret < 0 ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100164
165}
166
167static ssize_t iio_scan_el_ts_show(struct device *dev,
168 struct device_attribute *attr,
169 char *buf)
170{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200171 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100172 return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100173}
174
175static ssize_t iio_scan_el_ts_store(struct device *dev,
176 struct device_attribute *attr,
177 const char *buf,
178 size_t len)
179{
Jonathan Camerona714af22012-04-21 10:09:32 +0100180 int ret;
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200181 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100182 bool state;
183
Jonathan Camerona714af22012-04-21 10:09:32 +0100184 ret = strtobool(buf, &state);
185 if (ret < 0)
186 return ret;
187
Jonathan Cameron14555b12011-09-21 11:15:57 +0100188 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100189 if (iio_buffer_is_active(indio_dev->buffer)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100190 ret = -EBUSY;
191 goto error_ret;
192 }
193 indio_dev->buffer->scan_timestamp = state;
194error_ret:
195 mutex_unlock(&indio_dev->mlock);
196
197 return ret ? ret : len;
198}
199
200static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
201 const struct iio_chan_spec *chan)
202{
203 int ret, attrcount = 0;
204 struct iio_buffer *buffer = indio_dev->buffer;
205
206 ret = __iio_add_chan_devattr("index",
207 chan,
208 &iio_show_scan_index,
209 NULL,
210 0,
Jonathan Cameron37044322013-09-08 14:57:00 +0100211 IIO_SEPARATE,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100212 &indio_dev->dev,
213 &buffer->scan_el_dev_attr_list);
214 if (ret)
215 goto error_ret;
216 attrcount++;
217 ret = __iio_add_chan_devattr("type",
218 chan,
219 &iio_show_fixed_type,
220 NULL,
221 0,
222 0,
223 &indio_dev->dev,
224 &buffer->scan_el_dev_attr_list);
225 if (ret)
226 goto error_ret;
227 attrcount++;
228 if (chan->type != IIO_TIMESTAMP)
229 ret = __iio_add_chan_devattr("en",
230 chan,
231 &iio_scan_el_show,
232 &iio_scan_el_store,
233 chan->scan_index,
234 0,
235 &indio_dev->dev,
236 &buffer->scan_el_dev_attr_list);
237 else
238 ret = __iio_add_chan_devattr("en",
239 chan,
240 &iio_scan_el_ts_show,
241 &iio_scan_el_ts_store,
242 chan->scan_index,
243 0,
244 &indio_dev->dev,
245 &buffer->scan_el_dev_attr_list);
246 attrcount++;
247 ret = attrcount;
248error_ret:
249 return ret;
250}
251
252static void iio_buffer_remove_and_free_scan_dev_attr(struct iio_dev *indio_dev,
253 struct iio_dev_attr *p)
254{
255 kfree(p->dev_attr.attr.name);
256 kfree(p);
257}
258
259static void __iio_buffer_attr_cleanup(struct iio_dev *indio_dev)
260{
261 struct iio_dev_attr *p, *n;
262 struct iio_buffer *buffer = indio_dev->buffer;
263
264 list_for_each_entry_safe(p, n,
265 &buffer->scan_el_dev_attr_list, l)
266 iio_buffer_remove_and_free_scan_dev_attr(indio_dev, p);
267}
268
269static const char * const iio_scan_elements_group_name = "scan_elements";
270
271int iio_buffer_register(struct iio_dev *indio_dev,
272 const struct iio_chan_spec *channels,
273 int num_channels)
274{
275 struct iio_dev_attr *p;
276 struct attribute **attr;
277 struct iio_buffer *buffer = indio_dev->buffer;
278 int ret, i, attrn, attrcount, attrcount_orig = 0;
279
280 if (buffer->attrs)
281 indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs;
282
283 if (buffer->scan_el_attrs != NULL) {
284 attr = buffer->scan_el_attrs->attrs;
285 while (*attr++ != NULL)
286 attrcount_orig++;
287 }
288 attrcount = attrcount_orig;
289 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
290 if (channels) {
291 /* new magic */
292 for (i = 0; i < num_channels; i++) {
Lars-Peter Clausenf5b81dd2012-06-18 18:33:47 +0200293 if (channels[i].scan_index < 0)
294 continue;
295
Jonathan Cameron14555b12011-09-21 11:15:57 +0100296 /* Establish necessary mask length */
297 if (channels[i].scan_index >
298 (int)indio_dev->masklength - 1)
299 indio_dev->masklength
Lars-Peter Clausene1dc7be2012-07-02 14:52:56 +0200300 = channels[i].scan_index + 1;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100301
302 ret = iio_buffer_add_channel_sysfs(indio_dev,
303 &channels[i]);
304 if (ret < 0)
305 goto error_cleanup_dynamic;
306 attrcount += ret;
Jonathan Cameronbeb80602011-12-05 21:37:11 +0000307 if (channels[i].type == IIO_TIMESTAMP)
Jonathan Cameronf1264802012-04-21 10:09:34 +0100308 indio_dev->scan_index_timestamp =
Jonathan Cameronbeb80602011-12-05 21:37:11 +0000309 channels[i].scan_index;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100310 }
311 if (indio_dev->masklength && buffer->scan_mask == NULL) {
Thomas Meyerd83fb182011-11-29 22:08:00 +0100312 buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
313 sizeof(*buffer->scan_mask),
314 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100315 if (buffer->scan_mask == NULL) {
316 ret = -ENOMEM;
317 goto error_cleanup_dynamic;
318 }
319 }
320 }
321
322 buffer->scan_el_group.name = iio_scan_elements_group_name;
323
Thomas Meyerd83fb182011-11-29 22:08:00 +0100324 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
325 sizeof(buffer->scan_el_group.attrs[0]),
326 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100327 if (buffer->scan_el_group.attrs == NULL) {
328 ret = -ENOMEM;
329 goto error_free_scan_mask;
330 }
331 if (buffer->scan_el_attrs)
332 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
333 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
334 attrn = attrcount_orig;
335
336 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
337 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
338 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
339
340 return 0;
341
342error_free_scan_mask:
343 kfree(buffer->scan_mask);
344error_cleanup_dynamic:
345 __iio_buffer_attr_cleanup(indio_dev);
346
347 return ret;
348}
349EXPORT_SYMBOL(iio_buffer_register);
350
351void iio_buffer_unregister(struct iio_dev *indio_dev)
352{
353 kfree(indio_dev->buffer->scan_mask);
354 kfree(indio_dev->buffer->scan_el_group.attrs);
355 __iio_buffer_attr_cleanup(indio_dev);
356}
357EXPORT_SYMBOL(iio_buffer_unregister);
358
359ssize_t iio_buffer_read_length(struct device *dev,
360 struct device_attribute *attr,
361 char *buf)
362{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200363 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100364 struct iio_buffer *buffer = indio_dev->buffer;
365
366 if (buffer->access->get_length)
367 return sprintf(buf, "%d\n",
368 buffer->access->get_length(buffer));
369
370 return 0;
371}
372EXPORT_SYMBOL(iio_buffer_read_length);
373
374ssize_t iio_buffer_write_length(struct device *dev,
375 struct device_attribute *attr,
376 const char *buf,
377 size_t len)
378{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200379 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100380 struct iio_buffer *buffer = indio_dev->buffer;
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100381 unsigned int val;
382 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100383
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100384 ret = kstrtouint(buf, 10, &val);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100385 if (ret)
386 return ret;
387
388 if (buffer->access->get_length)
389 if (val == buffer->access->get_length(buffer))
390 return len;
391
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100392 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100393 if (iio_buffer_is_active(indio_dev->buffer)) {
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100394 ret = -EBUSY;
395 } else {
Lars-Peter Clausen869871b2011-12-19 15:23:48 +0100396 if (buffer->access->set_length)
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100397 buffer->access->set_length(buffer, val);
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100398 ret = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100399 }
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100400 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100401
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100402 return ret ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100403}
404EXPORT_SYMBOL(iio_buffer_write_length);
405
Jonathan Cameron14555b12011-09-21 11:15:57 +0100406ssize_t iio_buffer_show_enable(struct device *dev,
407 struct device_attribute *attr,
408 char *buf)
409{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200410 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100411 return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
Jonathan Cameron14555b12011-09-21 11:15:57 +0100412}
413EXPORT_SYMBOL(iio_buffer_show_enable);
414
Jonathan Cameron14555b12011-09-21 11:15:57 +0100415/* note NULL used as error indicator as it doesn't make sense. */
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100416static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100417 unsigned int masklength,
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100418 const unsigned long *mask)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100419{
420 if (bitmap_empty(mask, masklength))
421 return NULL;
422 while (*av_masks) {
423 if (bitmap_subset(mask, av_masks, masklength))
424 return av_masks;
425 av_masks += BITS_TO_LONGS(masklength);
426 }
427 return NULL;
428}
429
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100430static int iio_compute_scan_bytes(struct iio_dev *indio_dev, const long *mask,
431 bool timestamp)
Jonathan Cameron959d2952011-12-05 21:37:13 +0000432{
Jonathan Cameron959d2952011-12-05 21:37:13 +0000433 const struct iio_chan_spec *ch;
434 unsigned bytes = 0;
435 int length, i;
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100436
437 /* How much space will the demuxed element take? */
438 for_each_set_bit(i, mask,
439 indio_dev->masklength) {
440 ch = iio_find_channel_from_si(indio_dev, i);
441 length = ch->scan_type.storagebits / 8;
442 bytes = ALIGN(bytes, length);
443 bytes += length;
444 }
445 if (timestamp) {
446 ch = iio_find_channel_from_si(indio_dev,
Jonathan Cameronf1264802012-04-21 10:09:34 +0100447 indio_dev->scan_index_timestamp);
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100448 length = ch->scan_type.storagebits / 8;
449 bytes = ALIGN(bytes, length);
450 bytes += length;
451 }
452 return bytes;
453}
454
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100455int iio_update_buffers(struct iio_dev *indio_dev,
456 struct iio_buffer *insert_buffer,
457 struct iio_buffer *remove_buffer)
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100458{
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100459 int ret;
460 int success = 0;
461 struct iio_buffer *buffer;
462 unsigned long *compound_mask;
463 const unsigned long *old_mask;
Jonathan Cameron959d2952011-12-05 21:37:13 +0000464
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100465 /* Wind down existing buffers - iff there are any */
466 if (!list_empty(&indio_dev->buffer_list)) {
467 if (indio_dev->setup_ops->predisable) {
468 ret = indio_dev->setup_ops->predisable(indio_dev);
469 if (ret)
470 goto error_ret;
471 }
472 indio_dev->currentmode = INDIO_DIRECT_MODE;
473 if (indio_dev->setup_ops->postdisable) {
474 ret = indio_dev->setup_ops->postdisable(indio_dev);
475 if (ret)
476 goto error_ret;
477 }
478 }
479 /* Keep a copy of current setup to allow roll back */
480 old_mask = indio_dev->active_scan_mask;
481 if (!indio_dev->available_scan_masks)
482 indio_dev->active_scan_mask = NULL;
483
484 if (remove_buffer)
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100485 list_del_init(&remove_buffer->buffer_list);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100486 if (insert_buffer)
487 list_add(&insert_buffer->buffer_list, &indio_dev->buffer_list);
488
489 /* If no buffers in list, we are done */
490 if (list_empty(&indio_dev->buffer_list)) {
491 indio_dev->currentmode = INDIO_DIRECT_MODE;
492 if (indio_dev->available_scan_masks == NULL)
493 kfree(old_mask);
494 return 0;
495 }
Jonathan Cameron959d2952011-12-05 21:37:13 +0000496
497 /* What scan mask do we actually have ?*/
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100498 compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
499 sizeof(long), GFP_KERNEL);
500 if (compound_mask == NULL) {
501 if (indio_dev->available_scan_masks == NULL)
502 kfree(old_mask);
503 return -ENOMEM;
504 }
505 indio_dev->scan_timestamp = 0;
506
507 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
508 bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
509 indio_dev->masklength);
510 indio_dev->scan_timestamp |= buffer->scan_timestamp;
511 }
512 if (indio_dev->available_scan_masks) {
Jonathan Cameron959d2952011-12-05 21:37:13 +0000513 indio_dev->active_scan_mask =
514 iio_scan_mask_match(indio_dev->available_scan_masks,
515 indio_dev->masklength,
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100516 compound_mask);
517 if (indio_dev->active_scan_mask == NULL) {
518 /*
519 * Roll back.
520 * Note can only occur when adding a buffer.
521 */
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100522 list_del_init(&insert_buffer->buffer_list);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100523 indio_dev->active_scan_mask = old_mask;
524 success = -EINVAL;
525 }
526 } else {
527 indio_dev->active_scan_mask = compound_mask;
528 }
Lars-Peter Clausenaff1eb42012-06-15 18:08:59 +0200529
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000530 iio_update_demux(indio_dev);
531
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100532 /* Wind up again */
533 if (indio_dev->setup_ops->preenable) {
534 ret = indio_dev->setup_ops->preenable(indio_dev);
535 if (ret) {
536 printk(KERN_ERR
Michał Mirosławbec18892013-05-04 14:19:00 +0100537 "Buffer not started: buffer preenable failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100538 goto error_remove_inserted;
539 }
540 }
541 indio_dev->scan_bytes =
542 iio_compute_scan_bytes(indio_dev,
543 indio_dev->active_scan_mask,
544 indio_dev->scan_timestamp);
545 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
546 if (buffer->access->request_update) {
547 ret = buffer->access->request_update(buffer);
548 if (ret) {
549 printk(KERN_INFO
Michał Mirosławbec18892013-05-04 14:19:00 +0100550 "Buffer not started: buffer parameter update failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100551 goto error_run_postdisable;
552 }
553 }
554 if (indio_dev->info->update_scan_mode) {
555 ret = indio_dev->info
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000556 ->update_scan_mode(indio_dev,
557 indio_dev->active_scan_mask);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100558 if (ret < 0) {
Michał Mirosławbec18892013-05-04 14:19:00 +0100559 printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100560 goto error_run_postdisable;
561 }
562 }
563 /* Definitely possible for devices to support both of these.*/
564 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
565 if (!indio_dev->trig) {
566 printk(KERN_INFO "Buffer not started: no trigger\n");
567 ret = -EINVAL;
568 /* Can only occur on first buffer */
569 goto error_run_postdisable;
570 }
571 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
572 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) {
573 indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
574 } else { /* should never be reached */
575 ret = -EINVAL;
576 goto error_run_postdisable;
577 }
578
579 if (indio_dev->setup_ops->postenable) {
580 ret = indio_dev->setup_ops->postenable(indio_dev);
581 if (ret) {
582 printk(KERN_INFO
Michał Mirosławbec18892013-05-04 14:19:00 +0100583 "Buffer not started: postenable failed (%d)\n", ret);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100584 indio_dev->currentmode = INDIO_DIRECT_MODE;
585 if (indio_dev->setup_ops->postdisable)
586 indio_dev->setup_ops->postdisable(indio_dev);
587 goto error_disable_all_buffers;
588 }
589 }
590
591 if (indio_dev->available_scan_masks)
592 kfree(compound_mask);
593 else
594 kfree(old_mask);
595
596 return success;
597
598error_disable_all_buffers:
599 indio_dev->currentmode = INDIO_DIRECT_MODE;
600error_run_postdisable:
601 if (indio_dev->setup_ops->postdisable)
602 indio_dev->setup_ops->postdisable(indio_dev);
603error_remove_inserted:
604
605 if (insert_buffer)
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100606 list_del_init(&insert_buffer->buffer_list);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100607 indio_dev->active_scan_mask = old_mask;
608 kfree(compound_mask);
609error_ret:
610
611 return ret;
612}
613EXPORT_SYMBOL_GPL(iio_update_buffers);
614
615ssize_t iio_buffer_store_enable(struct device *dev,
616 struct device_attribute *attr,
617 const char *buf,
618 size_t len)
619{
620 int ret;
621 bool requested_state;
622 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100623 bool inlist;
624
625 ret = strtobool(buf, &requested_state);
626 if (ret < 0)
627 return ret;
628
629 mutex_lock(&indio_dev->mlock);
630
631 /* Find out if it is in the list */
Lars-Peter Clausen705ee2c2013-09-15 16:31:00 +0100632 inlist = iio_buffer_is_active(indio_dev->buffer);
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100633 /* Already in desired state */
634 if (inlist == requested_state)
635 goto done;
636
637 if (requested_state)
638 ret = iio_update_buffers(indio_dev,
639 indio_dev->buffer, NULL);
640 else
641 ret = iio_update_buffers(indio_dev,
642 NULL, indio_dev->buffer);
643
644 if (ret < 0)
645 goto done;
646done:
647 mutex_unlock(&indio_dev->mlock);
648 return (ret < 0) ? ret : len;
649}
650EXPORT_SYMBOL(iio_buffer_store_enable);
651
652int iio_sw_buffer_preenable(struct iio_dev *indio_dev)
653{
654 struct iio_buffer *buffer;
655 unsigned bytes;
656 dev_dbg(&indio_dev->dev, "%s\n", __func__);
657
658 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
659 if (buffer->access->set_bytes_per_datum) {
660 bytes = iio_compute_scan_bytes(indio_dev,
661 buffer->scan_mask,
662 buffer->scan_timestamp);
663
664 buffer->access->set_bytes_per_datum(buffer, bytes);
665 }
Jonathan Cameron959d2952011-12-05 21:37:13 +0000666 return 0;
667}
668EXPORT_SYMBOL(iio_sw_buffer_preenable);
669
Jonathan Cameron14555b12011-09-21 11:15:57 +0100670/**
Lars-Peter Clausen81636632012-07-09 10:00:00 +0100671 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
672 * @indio_dev: the iio device
673 * @mask: scan mask to be checked
674 *
675 * Return true if exactly one bit is set in the scan mask, false otherwise. It
676 * can be used for devices where only one channel can be active for sampling at
677 * a time.
678 */
679bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
680 const unsigned long *mask)
681{
682 return bitmap_weight(mask, indio_dev->masklength) == 1;
683}
684EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
685
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100686static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
687 const unsigned long *mask)
688{
689 if (!indio_dev->setup_ops->validate_scan_mask)
690 return true;
691
692 return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
693}
694
Jonathan Cameron14555b12011-09-21 11:15:57 +0100695/**
696 * iio_scan_mask_set() - set particular bit in the scan mask
697 * @buffer: the buffer whose scan mask we are interested in
698 * @bit: the bit to be set.
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100699 *
700 * Note that at this point we have no way of knowing what other
701 * buffers might request, hence this code only verifies that the
702 * individual buffers request is plausible.
703 */
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000704int iio_scan_mask_set(struct iio_dev *indio_dev,
705 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100706{
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100707 const unsigned long *mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100708 unsigned long *trialmask;
709
710 trialmask = kmalloc(sizeof(*trialmask)*
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100711 BITS_TO_LONGS(indio_dev->masklength),
Jonathan Cameron14555b12011-09-21 11:15:57 +0100712 GFP_KERNEL);
713
714 if (trialmask == NULL)
715 return -ENOMEM;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100716 if (!indio_dev->masklength) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100717 WARN_ON("trying to set scanmask prior to registering buffer\n");
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100718 goto err_invalid_mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100719 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100720 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100721 set_bit(bit, trialmask);
722
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100723 if (!iio_validate_scan_mask(indio_dev, trialmask))
724 goto err_invalid_mask;
725
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100726 if (indio_dev->available_scan_masks) {
727 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
728 indio_dev->masklength,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100729 trialmask);
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100730 if (!mask)
731 goto err_invalid_mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100732 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100733 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100734
735 kfree(trialmask);
736
737 return 0;
Lars-Peter Clausen939546d2012-07-09 10:00:00 +0100738
739err_invalid_mask:
740 kfree(trialmask);
741 return -EINVAL;
742}
Jonathan Cameron14555b12011-09-21 11:15:57 +0100743EXPORT_SYMBOL_GPL(iio_scan_mask_set);
744
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000745int iio_scan_mask_query(struct iio_dev *indio_dev,
746 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100747{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100748 if (bit > indio_dev->masklength)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100749 return -EINVAL;
750
751 if (!buffer->scan_mask)
752 return 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100753
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100754 return test_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100755};
756EXPORT_SYMBOL_GPL(iio_scan_mask_query);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000757
758/**
759 * struct iio_demux_table() - table describing demux memcpy ops
760 * @from: index to copy from
Peter Meerwald99698b42012-08-26 13:43:00 +0100761 * @to: index to copy to
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000762 * @length: how many bytes to copy
763 * @l: list head used for management
764 */
765struct iio_demux_table {
766 unsigned from;
767 unsigned to;
768 unsigned length;
769 struct list_head l;
770};
771
772static unsigned char *iio_demux(struct iio_buffer *buffer,
773 unsigned char *datain)
774{
775 struct iio_demux_table *t;
776
777 if (list_empty(&buffer->demux_list))
778 return datain;
779 list_for_each_entry(t, &buffer->demux_list, l)
780 memcpy(buffer->demux_bounce + t->to,
781 datain + t->from, t->length);
782
783 return buffer->demux_bounce;
784}
785
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100786static int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000787{
788 unsigned char *dataout = iio_demux(buffer, data);
789
Lars-Peter Clausence56ade2012-09-04 13:38:00 +0100790 return buffer->access->store_to(buffer, dataout);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000791}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000792
Jonathan Cameron842cd102012-04-21 10:09:45 +0100793static void iio_buffer_demux_free(struct iio_buffer *buffer)
794{
795 struct iio_demux_table *p, *q;
796 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
797 list_del(&p->l);
798 kfree(p);
799 }
800}
801
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100802
803int iio_push_to_buffers(struct iio_dev *indio_dev, unsigned char *data)
804{
805 int ret;
806 struct iio_buffer *buf;
807
808 list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
809 ret = iio_push_to_buffer(buf, data);
810 if (ret < 0)
811 return ret;
812 }
813
814 return 0;
815}
816EXPORT_SYMBOL_GPL(iio_push_to_buffers);
817
818static int iio_buffer_update_demux(struct iio_dev *indio_dev,
819 struct iio_buffer *buffer)
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000820{
821 const struct iio_chan_spec *ch;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000822 int ret, in_ind = -1, out_ind, length;
823 unsigned in_loc = 0, out_loc = 0;
Jonathan Cameron842cd102012-04-21 10:09:45 +0100824 struct iio_demux_table *p;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000825
826 /* Clear out any old demux */
Jonathan Cameron842cd102012-04-21 10:09:45 +0100827 iio_buffer_demux_free(buffer);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000828 kfree(buffer->demux_bounce);
829 buffer->demux_bounce = NULL;
830
831 /* First work out which scan mode we will actually have */
832 if (bitmap_equal(indio_dev->active_scan_mask,
833 buffer->scan_mask,
834 indio_dev->masklength))
835 return 0;
836
837 /* Now we have the two masks, work from least sig and build up sizes */
838 for_each_set_bit(out_ind,
839 indio_dev->active_scan_mask,
840 indio_dev->masklength) {
841 in_ind = find_next_bit(indio_dev->active_scan_mask,
842 indio_dev->masklength,
843 in_ind + 1);
844 while (in_ind != out_ind) {
845 in_ind = find_next_bit(indio_dev->active_scan_mask,
846 indio_dev->masklength,
847 in_ind + 1);
848 ch = iio_find_channel_from_si(indio_dev, in_ind);
849 length = ch->scan_type.storagebits/8;
850 /* Make sure we are aligned */
851 in_loc += length;
852 if (in_loc % length)
853 in_loc += length - in_loc % length;
854 }
855 p = kmalloc(sizeof(*p), GFP_KERNEL);
856 if (p == NULL) {
857 ret = -ENOMEM;
858 goto error_clear_mux_table;
859 }
860 ch = iio_find_channel_from_si(indio_dev, in_ind);
861 length = ch->scan_type.storagebits/8;
862 if (out_loc % length)
863 out_loc += length - out_loc % length;
864 if (in_loc % length)
865 in_loc += length - in_loc % length;
866 p->from = in_loc;
867 p->to = out_loc;
868 p->length = length;
869 list_add_tail(&p->l, &buffer->demux_list);
870 out_loc += length;
871 in_loc += length;
872 }
873 /* Relies on scan_timestamp being last */
874 if (buffer->scan_timestamp) {
875 p = kmalloc(sizeof(*p), GFP_KERNEL);
876 if (p == NULL) {
877 ret = -ENOMEM;
878 goto error_clear_mux_table;
879 }
880 ch = iio_find_channel_from_si(indio_dev,
Jonathan Cameronf1264802012-04-21 10:09:34 +0100881 indio_dev->scan_index_timestamp);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000882 length = ch->scan_type.storagebits/8;
883 if (out_loc % length)
884 out_loc += length - out_loc % length;
885 if (in_loc % length)
886 in_loc += length - in_loc % length;
887 p->from = in_loc;
888 p->to = out_loc;
889 p->length = length;
890 list_add_tail(&p->l, &buffer->demux_list);
891 out_loc += length;
892 in_loc += length;
893 }
894 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
895 if (buffer->demux_bounce == NULL) {
896 ret = -ENOMEM;
897 goto error_clear_mux_table;
898 }
899 return 0;
900
901error_clear_mux_table:
Jonathan Cameron842cd102012-04-21 10:09:45 +0100902 iio_buffer_demux_free(buffer);
903
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000904 return ret;
905}
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100906
907int iio_update_demux(struct iio_dev *indio_dev)
908{
909 struct iio_buffer *buffer;
910 int ret;
911
912 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
913 ret = iio_buffer_update_demux(indio_dev, buffer);
914 if (ret < 0)
915 goto error_clear_mux_table;
916 }
917 return 0;
918
919error_clear_mux_table:
920 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
921 iio_buffer_demux_free(buffer);
922
923 return ret;
924}
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000925EXPORT_SYMBOL_GPL(iio_update_demux);