blob: 59472890cb2a280786b77db12cb63eb9f9bbd79c [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
24#include "iio.h"
25#include "iio_core.h"
26#include "sysfs.h"
Jonathan Cameronaf5046a2011-10-26 17:41:32 +010027#include "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
34/**
35 * iio_buffer_read_first_n_outer() - chrdev read for buffer access
36 *
37 * This function relies on all buffer implementations having an
38 * iio_buffer as their first element.
39 **/
40ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
41 size_t n, loff_t *f_ps)
42{
43 struct iio_dev *indio_dev = filp->private_data;
44 struct iio_buffer *rb = indio_dev->buffer;
45
Jonathan Cameron96e00f12011-10-26 17:27:45 +010046 if (!rb || !rb->access->read_first_n)
Jonathan Cameron14555b12011-09-21 11:15:57 +010047 return -EINVAL;
48 return rb->access->read_first_n(rb, n, buf);
49}
50
51/**
52 * iio_buffer_poll() - poll the buffer to find out if it has data
53 */
54unsigned int iio_buffer_poll(struct file *filp,
55 struct poll_table_struct *wait)
56{
57 struct iio_dev *indio_dev = filp->private_data;
58 struct iio_buffer *rb = indio_dev->buffer;
59
60 poll_wait(filp, &rb->pollq, wait);
61 if (rb->stufftoread)
62 return POLLIN | POLLRDNORM;
63 /* need a way of knowing if there may be enough data... */
64 return 0;
65}
66
Jonathan Cameron30eb82f2011-09-21 11:16:02 +010067int iio_chrdev_buffer_open(struct iio_dev *indio_dev)
Jonathan Cameron14555b12011-09-21 11:15:57 +010068{
69 struct iio_buffer *rb = indio_dev->buffer;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +010070 if (!rb)
Jonathan Cameron96e00f12011-10-26 17:27:45 +010071 return 0;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +010072 if (rb->access->mark_in_use)
Jonathan Cameron14555b12011-09-21 11:15:57 +010073 rb->access->mark_in_use(rb);
Jonathan Cameron30eb82f2011-09-21 11:16:02 +010074 return 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +010075}
76
77void iio_chrdev_buffer_release(struct iio_dev *indio_dev)
78{
79 struct iio_buffer *rb = indio_dev->buffer;
80
Jonathan Cameron96e00f12011-10-26 17:27:45 +010081 if (!rb)
82 return;
Jonathan Cameron14555b12011-09-21 11:15:57 +010083 clear_bit(IIO_BUSY_BIT_POS, &rb->flags);
84 if (rb->access->unmark_in_use)
85 rb->access->unmark_in_use(rb);
Jonathan Cameron14555b12011-09-21 11:15:57 +010086}
87
Jonathan Cameronf79a9092011-12-05 22:18:29 +000088void iio_buffer_init(struct iio_buffer *buffer)
Jonathan Cameron14555b12011-09-21 11:15:57 +010089{
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +000090 INIT_LIST_HEAD(&buffer->demux_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +010091 init_waitqueue_head(&buffer->pollq);
92}
93EXPORT_SYMBOL(iio_buffer_init);
94
95static ssize_t iio_show_scan_index(struct device *dev,
96 struct device_attribute *attr,
97 char *buf)
98{
99 return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
100}
101
102static ssize_t iio_show_fixed_type(struct device *dev,
103 struct device_attribute *attr,
104 char *buf)
105{
106 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
107 u8 type = this_attr->c->scan_type.endianness;
108
109 if (type == IIO_CPU) {
Jonathan Cameron9d5d1152011-10-04 16:02:08 +0100110#ifdef __LITTLE_ENDIAN
111 type = IIO_LE;
112#else
113 type = IIO_BE;
114#endif
Jonathan Cameron14555b12011-09-21 11:15:57 +0100115 }
116 return sprintf(buf, "%s:%c%d/%d>>%u\n",
117 iio_endian_prefix[type],
118 this_attr->c->scan_type.sign,
119 this_attr->c->scan_type.realbits,
120 this_attr->c->scan_type.storagebits,
121 this_attr->c->scan_type.shift);
122}
123
124static ssize_t iio_scan_el_show(struct device *dev,
125 struct device_attribute *attr,
126 char *buf)
127{
128 int ret;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100129 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100130
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000131 ret = test_bit(to_iio_dev_attr(attr)->address,
132 indio_dev->buffer->scan_mask);
133
Jonathan Cameron14555b12011-09-21 11:15:57 +0100134 return sprintf(buf, "%d\n", ret);
135}
136
137static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
138{
139 clear_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100140 return 0;
141}
142
143static ssize_t iio_scan_el_store(struct device *dev,
144 struct device_attribute *attr,
145 const char *buf,
146 size_t len)
147{
148 int ret = 0;
149 bool state;
150 struct iio_dev *indio_dev = dev_get_drvdata(dev);
151 struct iio_buffer *buffer = indio_dev->buffer;
152 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
153
154 state = !(buf[0] == '0');
155 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen5fd62182011-12-19 15:23:43 +0100156 if (iio_buffer_enabled(indio_dev)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100157 ret = -EBUSY;
158 goto error_ret;
159 }
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000160 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100161 if (ret < 0)
162 goto error_ret;
163 if (!state && ret) {
164 ret = iio_scan_mask_clear(buffer, this_attr->address);
165 if (ret)
166 goto error_ret;
167 } else if (state && !ret) {
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000168 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100169 if (ret)
170 goto error_ret;
171 }
172
173error_ret:
174 mutex_unlock(&indio_dev->mlock);
175
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100176 return ret < 0 ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100177
178}
179
180static ssize_t iio_scan_el_ts_show(struct device *dev,
181 struct device_attribute *attr,
182 char *buf)
183{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100184 struct iio_dev *indio_dev = dev_get_drvdata(dev);
185 return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100186}
187
188static ssize_t iio_scan_el_ts_store(struct device *dev,
189 struct device_attribute *attr,
190 const char *buf,
191 size_t len)
192{
193 int ret = 0;
194 struct iio_dev *indio_dev = dev_get_drvdata(dev);
195 bool state;
196
197 state = !(buf[0] == '0');
198 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen5fd62182011-12-19 15:23:43 +0100199 if (iio_buffer_enabled(indio_dev)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100200 ret = -EBUSY;
201 goto error_ret;
202 }
203 indio_dev->buffer->scan_timestamp = state;
204error_ret:
205 mutex_unlock(&indio_dev->mlock);
206
207 return ret ? ret : len;
208}
209
210static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
211 const struct iio_chan_spec *chan)
212{
213 int ret, attrcount = 0;
214 struct iio_buffer *buffer = indio_dev->buffer;
215
216 ret = __iio_add_chan_devattr("index",
217 chan,
218 &iio_show_scan_index,
219 NULL,
220 0,
221 0,
222 &indio_dev->dev,
223 &buffer->scan_el_dev_attr_list);
224 if (ret)
225 goto error_ret;
226 attrcount++;
227 ret = __iio_add_chan_devattr("type",
228 chan,
229 &iio_show_fixed_type,
230 NULL,
231 0,
232 0,
233 &indio_dev->dev,
234 &buffer->scan_el_dev_attr_list);
235 if (ret)
236 goto error_ret;
237 attrcount++;
238 if (chan->type != IIO_TIMESTAMP)
239 ret = __iio_add_chan_devattr("en",
240 chan,
241 &iio_scan_el_show,
242 &iio_scan_el_store,
243 chan->scan_index,
244 0,
245 &indio_dev->dev,
246 &buffer->scan_el_dev_attr_list);
247 else
248 ret = __iio_add_chan_devattr("en",
249 chan,
250 &iio_scan_el_ts_show,
251 &iio_scan_el_ts_store,
252 chan->scan_index,
253 0,
254 &indio_dev->dev,
255 &buffer->scan_el_dev_attr_list);
256 attrcount++;
257 ret = attrcount;
258error_ret:
259 return ret;
260}
261
262static void iio_buffer_remove_and_free_scan_dev_attr(struct iio_dev *indio_dev,
263 struct iio_dev_attr *p)
264{
265 kfree(p->dev_attr.attr.name);
266 kfree(p);
267}
268
269static void __iio_buffer_attr_cleanup(struct iio_dev *indio_dev)
270{
271 struct iio_dev_attr *p, *n;
272 struct iio_buffer *buffer = indio_dev->buffer;
273
274 list_for_each_entry_safe(p, n,
275 &buffer->scan_el_dev_attr_list, l)
276 iio_buffer_remove_and_free_scan_dev_attr(indio_dev, p);
277}
278
279static const char * const iio_scan_elements_group_name = "scan_elements";
280
281int iio_buffer_register(struct iio_dev *indio_dev,
282 const struct iio_chan_spec *channels,
283 int num_channels)
284{
285 struct iio_dev_attr *p;
286 struct attribute **attr;
287 struct iio_buffer *buffer = indio_dev->buffer;
288 int ret, i, attrn, attrcount, attrcount_orig = 0;
289
290 if (buffer->attrs)
291 indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs;
292
293 if (buffer->scan_el_attrs != NULL) {
294 attr = buffer->scan_el_attrs->attrs;
295 while (*attr++ != NULL)
296 attrcount_orig++;
297 }
298 attrcount = attrcount_orig;
299 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
300 if (channels) {
301 /* new magic */
302 for (i = 0; i < num_channels; i++) {
303 /* Establish necessary mask length */
304 if (channels[i].scan_index >
305 (int)indio_dev->masklength - 1)
306 indio_dev->masklength
307 = indio_dev->channels[i].scan_index + 1;
308
309 ret = iio_buffer_add_channel_sysfs(indio_dev,
310 &channels[i]);
311 if (ret < 0)
312 goto error_cleanup_dynamic;
313 attrcount += ret;
Jonathan Cameronbeb80602011-12-05 21:37:11 +0000314 if (channels[i].type == IIO_TIMESTAMP)
315 buffer->scan_index_timestamp =
316 channels[i].scan_index;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100317 }
318 if (indio_dev->masklength && buffer->scan_mask == NULL) {
Thomas Meyerd83fb182011-11-29 22:08:00 +0100319 buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
320 sizeof(*buffer->scan_mask),
321 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100322 if (buffer->scan_mask == NULL) {
323 ret = -ENOMEM;
324 goto error_cleanup_dynamic;
325 }
326 }
327 }
328
329 buffer->scan_el_group.name = iio_scan_elements_group_name;
330
Thomas Meyerd83fb182011-11-29 22:08:00 +0100331 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
332 sizeof(buffer->scan_el_group.attrs[0]),
333 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100334 if (buffer->scan_el_group.attrs == NULL) {
335 ret = -ENOMEM;
336 goto error_free_scan_mask;
337 }
338 if (buffer->scan_el_attrs)
339 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
340 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
341 attrn = attrcount_orig;
342
343 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
344 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
345 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
346
347 return 0;
348
349error_free_scan_mask:
350 kfree(buffer->scan_mask);
351error_cleanup_dynamic:
352 __iio_buffer_attr_cleanup(indio_dev);
353
354 return ret;
355}
356EXPORT_SYMBOL(iio_buffer_register);
357
358void iio_buffer_unregister(struct iio_dev *indio_dev)
359{
360 kfree(indio_dev->buffer->scan_mask);
361 kfree(indio_dev->buffer->scan_el_group.attrs);
362 __iio_buffer_attr_cleanup(indio_dev);
363}
364EXPORT_SYMBOL(iio_buffer_unregister);
365
366ssize_t iio_buffer_read_length(struct device *dev,
367 struct device_attribute *attr,
368 char *buf)
369{
370 struct iio_dev *indio_dev = dev_get_drvdata(dev);
371 struct iio_buffer *buffer = indio_dev->buffer;
372
373 if (buffer->access->get_length)
374 return sprintf(buf, "%d\n",
375 buffer->access->get_length(buffer));
376
377 return 0;
378}
379EXPORT_SYMBOL(iio_buffer_read_length);
380
381ssize_t iio_buffer_write_length(struct device *dev,
382 struct device_attribute *attr,
383 const char *buf,
384 size_t len)
385{
386 int ret;
387 ulong val;
388 struct iio_dev *indio_dev = dev_get_drvdata(dev);
389 struct iio_buffer *buffer = indio_dev->buffer;
390
391 ret = strict_strtoul(buf, 10, &val);
392 if (ret)
393 return ret;
394
395 if (buffer->access->get_length)
396 if (val == buffer->access->get_length(buffer))
397 return len;
398
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100399 mutex_lock(&indio_dev->mlock);
400 if (iio_buffer_enabled(indio_dev)) {
401 ret = -EBUSY;
402 } else {
403 if (buffer->access->set_length) {
404 buffer->access->set_length(buffer, val);
405 if (buffer->access->mark_param_change)
406 buffer->access->mark_param_change(buffer);
407 }
408 ret = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100409 }
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100410 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100411
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100412 return ret ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100413}
414EXPORT_SYMBOL(iio_buffer_write_length);
415
Jonathan Cameron14555b12011-09-21 11:15:57 +0100416ssize_t iio_buffer_store_enable(struct device *dev,
417 struct device_attribute *attr,
418 const char *buf,
419 size_t len)
420{
421 int ret;
422 bool requested_state, current_state;
423 int previous_mode;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100424 struct iio_dev *indio_dev = dev_get_drvdata(dev);
425 struct iio_buffer *buffer = indio_dev->buffer;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100426
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100427 mutex_lock(&indio_dev->mlock);
428 previous_mode = indio_dev->currentmode;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100429 requested_state = !(buf[0] == '0');
Lars-Peter Clausend4a68822011-12-19 15:23:42 +0100430 current_state = iio_buffer_enabled(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100431 if (current_state == requested_state) {
432 printk(KERN_INFO "iio-buffer, current state requested again\n");
433 goto done;
434 }
435 if (requested_state) {
Jonathan Cameron16122442011-12-05 22:18:14 +0000436 if (indio_dev->setup_ops->preenable) {
437 ret = indio_dev->setup_ops->preenable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100438 if (ret) {
439 printk(KERN_ERR
440 "Buffer not started:"
441 "buffer preenable failed\n");
442 goto error_ret;
443 }
444 }
445 if (buffer->access->request_update) {
446 ret = buffer->access->request_update(buffer);
447 if (ret) {
448 printk(KERN_INFO
449 "Buffer not started:"
450 "buffer parameter update failed\n");
451 goto error_ret;
452 }
453 }
454 if (buffer->access->mark_in_use)
455 buffer->access->mark_in_use(buffer);
456 /* Definitely possible for devices to support both of these.*/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100457 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
458 if (!indio_dev->trig) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100459 printk(KERN_INFO
460 "Buffer not started: no trigger\n");
461 ret = -EINVAL;
462 if (buffer->access->unmark_in_use)
463 buffer->access->unmark_in_use(buffer);
464 goto error_ret;
465 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100466 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
467 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE)
468 indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100469 else { /* should never be reached */
470 ret = -EINVAL;
471 goto error_ret;
472 }
473
Jonathan Cameron16122442011-12-05 22:18:14 +0000474 if (indio_dev->setup_ops->postenable) {
475 ret = indio_dev->setup_ops->postenable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100476 if (ret) {
477 printk(KERN_INFO
478 "Buffer not started:"
479 "postenable failed\n");
480 if (buffer->access->unmark_in_use)
481 buffer->access->unmark_in_use(buffer);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100482 indio_dev->currentmode = previous_mode;
Jonathan Cameron16122442011-12-05 22:18:14 +0000483 if (indio_dev->setup_ops->postdisable)
484 indio_dev->setup_ops->
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100485 postdisable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100486 goto error_ret;
487 }
488 }
489 } else {
Jonathan Cameron16122442011-12-05 22:18:14 +0000490 if (indio_dev->setup_ops->predisable) {
491 ret = indio_dev->setup_ops->predisable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100492 if (ret)
493 goto error_ret;
494 }
495 if (buffer->access->unmark_in_use)
496 buffer->access->unmark_in_use(buffer);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100497 indio_dev->currentmode = INDIO_DIRECT_MODE;
Jonathan Cameron16122442011-12-05 22:18:14 +0000498 if (indio_dev->setup_ops->postdisable) {
499 ret = indio_dev->setup_ops->postdisable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100500 if (ret)
501 goto error_ret;
502 }
503 }
504done:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100505 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100506 return len;
507
508error_ret:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100509 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100510 return ret;
511}
512EXPORT_SYMBOL(iio_buffer_store_enable);
513
514ssize_t iio_buffer_show_enable(struct device *dev,
515 struct device_attribute *attr,
516 char *buf)
517{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100518 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Lars-Peter Clausend4a68822011-12-19 15:23:42 +0100519 return sprintf(buf, "%d\n", iio_buffer_enabled(indio_dev));
Jonathan Cameron14555b12011-09-21 11:15:57 +0100520}
521EXPORT_SYMBOL(iio_buffer_show_enable);
522
Jonathan Cameron14555b12011-09-21 11:15:57 +0100523/* note NULL used as error indicator as it doesn't make sense. */
524static unsigned long *iio_scan_mask_match(unsigned long *av_masks,
525 unsigned int masklength,
526 unsigned long *mask)
527{
528 if (bitmap_empty(mask, masklength))
529 return NULL;
530 while (*av_masks) {
531 if (bitmap_subset(mask, av_masks, masklength))
532 return av_masks;
533 av_masks += BITS_TO_LONGS(masklength);
534 }
535 return NULL;
536}
537
Jonathan Cameron959d2952011-12-05 21:37:13 +0000538int iio_sw_buffer_preenable(struct iio_dev *indio_dev)
539{
540 struct iio_buffer *buffer = indio_dev->buffer;
541 const struct iio_chan_spec *ch;
542 unsigned bytes = 0;
543 int length, i;
544 dev_dbg(&indio_dev->dev, "%s\n", __func__);
545
546 /* How much space will the demuxed element take? */
547 for_each_set_bit(i, buffer->scan_mask,
548 indio_dev->masklength) {
549 ch = iio_find_channel_from_si(indio_dev, i);
550 length = ch->scan_type.storagebits/8;
551 bytes = ALIGN(bytes, length);
552 bytes += length;
553 }
554 if (buffer->scan_timestamp) {
555 ch = iio_find_channel_from_si(indio_dev,
556 buffer->scan_index_timestamp);
557 length = ch->scan_type.storagebits/8;
558 bytes = ALIGN(bytes, length);
559 bytes += length;
560 }
561 buffer->access->set_bytes_per_datum(buffer, bytes);
562
563 /* What scan mask do we actually have ?*/
564 if (indio_dev->available_scan_masks)
565 indio_dev->active_scan_mask =
566 iio_scan_mask_match(indio_dev->available_scan_masks,
567 indio_dev->masklength,
568 buffer->scan_mask);
569 else
570 indio_dev->active_scan_mask = buffer->scan_mask;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000571 iio_update_demux(indio_dev);
572
573 if (indio_dev->info->update_scan_mode)
574 return indio_dev->info
575 ->update_scan_mode(indio_dev,
576 indio_dev->active_scan_mask);
Jonathan Cameron959d2952011-12-05 21:37:13 +0000577 return 0;
578}
579EXPORT_SYMBOL(iio_sw_buffer_preenable);
580
Jonathan Cameron14555b12011-09-21 11:15:57 +0100581/**
582 * iio_scan_mask_set() - set particular bit in the scan mask
583 * @buffer: the buffer whose scan mask we are interested in
584 * @bit: the bit to be set.
585 **/
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000586int iio_scan_mask_set(struct iio_dev *indio_dev,
587 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100588{
Jonathan Cameron14555b12011-09-21 11:15:57 +0100589 unsigned long *mask;
590 unsigned long *trialmask;
591
592 trialmask = kmalloc(sizeof(*trialmask)*
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100593 BITS_TO_LONGS(indio_dev->masklength),
Jonathan Cameron14555b12011-09-21 11:15:57 +0100594 GFP_KERNEL);
595
596 if (trialmask == NULL)
597 return -ENOMEM;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100598 if (!indio_dev->masklength) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100599 WARN_ON("trying to set scanmask prior to registering buffer\n");
600 kfree(trialmask);
601 return -EINVAL;
602 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100603 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100604 set_bit(bit, trialmask);
605
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100606 if (indio_dev->available_scan_masks) {
607 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
608 indio_dev->masklength,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100609 trialmask);
610 if (!mask) {
611 kfree(trialmask);
612 return -EINVAL;
613 }
614 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100615 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100616
617 kfree(trialmask);
618
619 return 0;
620};
621EXPORT_SYMBOL_GPL(iio_scan_mask_set);
622
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000623int iio_scan_mask_query(struct iio_dev *indio_dev,
624 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100625{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100626 if (bit > indio_dev->masklength)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100627 return -EINVAL;
628
629 if (!buffer->scan_mask)
630 return 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100631
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100632 return test_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100633};
634EXPORT_SYMBOL_GPL(iio_scan_mask_query);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000635
636/**
637 * struct iio_demux_table() - table describing demux memcpy ops
638 * @from: index to copy from
639 * @to: index to copy to
640 * @length: how many bytes to copy
641 * @l: list head used for management
642 */
643struct iio_demux_table {
644 unsigned from;
645 unsigned to;
646 unsigned length;
647 struct list_head l;
648};
649
650static unsigned char *iio_demux(struct iio_buffer *buffer,
651 unsigned char *datain)
652{
653 struct iio_demux_table *t;
654
655 if (list_empty(&buffer->demux_list))
656 return datain;
657 list_for_each_entry(t, &buffer->demux_list, l)
658 memcpy(buffer->demux_bounce + t->to,
659 datain + t->from, t->length);
660
661 return buffer->demux_bounce;
662}
663
664int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data,
665 s64 timestamp)
666{
667 unsigned char *dataout = iio_demux(buffer, data);
668
669 return buffer->access->store_to(buffer, dataout, timestamp);
670}
671EXPORT_SYMBOL_GPL(iio_push_to_buffer);
672
673int iio_update_demux(struct iio_dev *indio_dev)
674{
675 const struct iio_chan_spec *ch;
676 struct iio_buffer *buffer = indio_dev->buffer;
677 int ret, in_ind = -1, out_ind, length;
678 unsigned in_loc = 0, out_loc = 0;
679 struct iio_demux_table *p, *q;
680
681 /* Clear out any old demux */
682 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
683 list_del(&p->l);
684 kfree(p);
685 }
686 kfree(buffer->demux_bounce);
687 buffer->demux_bounce = NULL;
688
689 /* First work out which scan mode we will actually have */
690 if (bitmap_equal(indio_dev->active_scan_mask,
691 buffer->scan_mask,
692 indio_dev->masklength))
693 return 0;
694
695 /* Now we have the two masks, work from least sig and build up sizes */
696 for_each_set_bit(out_ind,
697 indio_dev->active_scan_mask,
698 indio_dev->masklength) {
699 in_ind = find_next_bit(indio_dev->active_scan_mask,
700 indio_dev->masklength,
701 in_ind + 1);
702 while (in_ind != out_ind) {
703 in_ind = find_next_bit(indio_dev->active_scan_mask,
704 indio_dev->masklength,
705 in_ind + 1);
706 ch = iio_find_channel_from_si(indio_dev, in_ind);
707 length = ch->scan_type.storagebits/8;
708 /* Make sure we are aligned */
709 in_loc += length;
710 if (in_loc % length)
711 in_loc += length - in_loc % length;
712 }
713 p = kmalloc(sizeof(*p), GFP_KERNEL);
714 if (p == NULL) {
715 ret = -ENOMEM;
716 goto error_clear_mux_table;
717 }
718 ch = iio_find_channel_from_si(indio_dev, in_ind);
719 length = ch->scan_type.storagebits/8;
720 if (out_loc % length)
721 out_loc += length - out_loc % length;
722 if (in_loc % length)
723 in_loc += length - in_loc % length;
724 p->from = in_loc;
725 p->to = out_loc;
726 p->length = length;
727 list_add_tail(&p->l, &buffer->demux_list);
728 out_loc += length;
729 in_loc += length;
730 }
731 /* Relies on scan_timestamp being last */
732 if (buffer->scan_timestamp) {
733 p = kmalloc(sizeof(*p), GFP_KERNEL);
734 if (p == NULL) {
735 ret = -ENOMEM;
736 goto error_clear_mux_table;
737 }
738 ch = iio_find_channel_from_si(indio_dev,
739 buffer->scan_index_timestamp);
740 length = ch->scan_type.storagebits/8;
741 if (out_loc % length)
742 out_loc += length - out_loc % length;
743 if (in_loc % length)
744 in_loc += length - in_loc % length;
745 p->from = in_loc;
746 p->to = out_loc;
747 p->length = length;
748 list_add_tail(&p->l, &buffer->demux_list);
749 out_loc += length;
750 in_loc += length;
751 }
752 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
753 if (buffer->demux_bounce == NULL) {
754 ret = -ENOMEM;
755 goto error_clear_mux_table;
756 }
757 return 0;
758
759error_clear_mux_table:
760 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
761 list_del(&p->l);
762 kfree(p);
763 }
764 return ret;
765}
766EXPORT_SYMBOL_GPL(iio_update_demux);