blob: 389fe16872b4a38b874b72ef4981ffa887bf849b [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 Cameronf79a9092011-12-05 22:18:29 +000067void iio_buffer_init(struct iio_buffer *buffer)
Jonathan Cameron14555b12011-09-21 11:15:57 +010068{
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +000069 INIT_LIST_HEAD(&buffer->demux_list);
Jonathan Cameron14555b12011-09-21 11:15:57 +010070 init_waitqueue_head(&buffer->pollq);
71}
72EXPORT_SYMBOL(iio_buffer_init);
73
74static ssize_t iio_show_scan_index(struct device *dev,
75 struct device_attribute *attr,
76 char *buf)
77{
78 return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
79}
80
81static ssize_t iio_show_fixed_type(struct device *dev,
82 struct device_attribute *attr,
83 char *buf)
84{
85 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
86 u8 type = this_attr->c->scan_type.endianness;
87
88 if (type == IIO_CPU) {
Jonathan Cameron9d5d1152011-10-04 16:02:08 +010089#ifdef __LITTLE_ENDIAN
90 type = IIO_LE;
91#else
92 type = IIO_BE;
93#endif
Jonathan Cameron14555b12011-09-21 11:15:57 +010094 }
95 return sprintf(buf, "%s:%c%d/%d>>%u\n",
96 iio_endian_prefix[type],
97 this_attr->c->scan_type.sign,
98 this_attr->c->scan_type.realbits,
99 this_attr->c->scan_type.storagebits,
100 this_attr->c->scan_type.shift);
101}
102
103static ssize_t iio_scan_el_show(struct device *dev,
104 struct device_attribute *attr,
105 char *buf)
106{
107 int ret;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100108 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100109
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000110 ret = test_bit(to_iio_dev_attr(attr)->address,
111 indio_dev->buffer->scan_mask);
112
Jonathan Cameron14555b12011-09-21 11:15:57 +0100113 return sprintf(buf, "%d\n", ret);
114}
115
116static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
117{
118 clear_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100119 return 0;
120}
121
122static ssize_t iio_scan_el_store(struct device *dev,
123 struct device_attribute *attr,
124 const char *buf,
125 size_t len)
126{
Jonathan Camerona714af22012-04-21 10:09:32 +0100127 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100128 bool state;
129 struct iio_dev *indio_dev = dev_get_drvdata(dev);
130 struct iio_buffer *buffer = indio_dev->buffer;
131 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
132
Jonathan Camerona714af22012-04-21 10:09:32 +0100133 ret = strtobool(buf, &state);
134 if (ret < 0)
135 return ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100136 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen5fd62182011-12-19 15:23:43 +0100137 if (iio_buffer_enabled(indio_dev)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100138 ret = -EBUSY;
139 goto error_ret;
140 }
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000141 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100142 if (ret < 0)
143 goto error_ret;
144 if (!state && ret) {
145 ret = iio_scan_mask_clear(buffer, this_attr->address);
146 if (ret)
147 goto error_ret;
148 } else if (state && !ret) {
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000149 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100150 if (ret)
151 goto error_ret;
152 }
153
154error_ret:
155 mutex_unlock(&indio_dev->mlock);
156
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100157 return ret < 0 ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100158
159}
160
161static ssize_t iio_scan_el_ts_show(struct device *dev,
162 struct device_attribute *attr,
163 char *buf)
164{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100165 struct iio_dev *indio_dev = dev_get_drvdata(dev);
166 return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100167}
168
169static ssize_t iio_scan_el_ts_store(struct device *dev,
170 struct device_attribute *attr,
171 const char *buf,
172 size_t len)
173{
Jonathan Camerona714af22012-04-21 10:09:32 +0100174 int ret;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100175 struct iio_dev *indio_dev = dev_get_drvdata(dev);
176 bool state;
177
Jonathan Camerona714af22012-04-21 10:09:32 +0100178 ret = strtobool(buf, &state);
179 if (ret < 0)
180 return ret;
181
Jonathan Cameron14555b12011-09-21 11:15:57 +0100182 mutex_lock(&indio_dev->mlock);
Lars-Peter Clausen5fd62182011-12-19 15:23:43 +0100183 if (iio_buffer_enabled(indio_dev)) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100184 ret = -EBUSY;
185 goto error_ret;
186 }
187 indio_dev->buffer->scan_timestamp = state;
188error_ret:
189 mutex_unlock(&indio_dev->mlock);
190
191 return ret ? ret : len;
192}
193
194static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
195 const struct iio_chan_spec *chan)
196{
197 int ret, attrcount = 0;
198 struct iio_buffer *buffer = indio_dev->buffer;
199
200 ret = __iio_add_chan_devattr("index",
201 chan,
202 &iio_show_scan_index,
203 NULL,
204 0,
205 0,
206 &indio_dev->dev,
207 &buffer->scan_el_dev_attr_list);
208 if (ret)
209 goto error_ret;
210 attrcount++;
211 ret = __iio_add_chan_devattr("type",
212 chan,
213 &iio_show_fixed_type,
214 NULL,
215 0,
216 0,
217 &indio_dev->dev,
218 &buffer->scan_el_dev_attr_list);
219 if (ret)
220 goto error_ret;
221 attrcount++;
222 if (chan->type != IIO_TIMESTAMP)
223 ret = __iio_add_chan_devattr("en",
224 chan,
225 &iio_scan_el_show,
226 &iio_scan_el_store,
227 chan->scan_index,
228 0,
229 &indio_dev->dev,
230 &buffer->scan_el_dev_attr_list);
231 else
232 ret = __iio_add_chan_devattr("en",
233 chan,
234 &iio_scan_el_ts_show,
235 &iio_scan_el_ts_store,
236 chan->scan_index,
237 0,
238 &indio_dev->dev,
239 &buffer->scan_el_dev_attr_list);
240 attrcount++;
241 ret = attrcount;
242error_ret:
243 return ret;
244}
245
246static void iio_buffer_remove_and_free_scan_dev_attr(struct iio_dev *indio_dev,
247 struct iio_dev_attr *p)
248{
249 kfree(p->dev_attr.attr.name);
250 kfree(p);
251}
252
253static void __iio_buffer_attr_cleanup(struct iio_dev *indio_dev)
254{
255 struct iio_dev_attr *p, *n;
256 struct iio_buffer *buffer = indio_dev->buffer;
257
258 list_for_each_entry_safe(p, n,
259 &buffer->scan_el_dev_attr_list, l)
260 iio_buffer_remove_and_free_scan_dev_attr(indio_dev, p);
261}
262
263static const char * const iio_scan_elements_group_name = "scan_elements";
264
265int iio_buffer_register(struct iio_dev *indio_dev,
266 const struct iio_chan_spec *channels,
267 int num_channels)
268{
269 struct iio_dev_attr *p;
270 struct attribute **attr;
271 struct iio_buffer *buffer = indio_dev->buffer;
272 int ret, i, attrn, attrcount, attrcount_orig = 0;
273
274 if (buffer->attrs)
275 indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs;
276
277 if (buffer->scan_el_attrs != NULL) {
278 attr = buffer->scan_el_attrs->attrs;
279 while (*attr++ != NULL)
280 attrcount_orig++;
281 }
282 attrcount = attrcount_orig;
283 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
284 if (channels) {
285 /* new magic */
286 for (i = 0; i < num_channels; i++) {
287 /* Establish necessary mask length */
288 if (channels[i].scan_index >
289 (int)indio_dev->masklength - 1)
290 indio_dev->masklength
291 = indio_dev->channels[i].scan_index + 1;
292
293 ret = iio_buffer_add_channel_sysfs(indio_dev,
294 &channels[i]);
295 if (ret < 0)
296 goto error_cleanup_dynamic;
297 attrcount += ret;
Jonathan Cameronbeb80602011-12-05 21:37:11 +0000298 if (channels[i].type == IIO_TIMESTAMP)
299 buffer->scan_index_timestamp =
300 channels[i].scan_index;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100301 }
302 if (indio_dev->masklength && buffer->scan_mask == NULL) {
Thomas Meyerd83fb182011-11-29 22:08:00 +0100303 buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
304 sizeof(*buffer->scan_mask),
305 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100306 if (buffer->scan_mask == NULL) {
307 ret = -ENOMEM;
308 goto error_cleanup_dynamic;
309 }
310 }
311 }
312
313 buffer->scan_el_group.name = iio_scan_elements_group_name;
314
Thomas Meyerd83fb182011-11-29 22:08:00 +0100315 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
316 sizeof(buffer->scan_el_group.attrs[0]),
317 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100318 if (buffer->scan_el_group.attrs == NULL) {
319 ret = -ENOMEM;
320 goto error_free_scan_mask;
321 }
322 if (buffer->scan_el_attrs)
323 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
324 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
325 attrn = attrcount_orig;
326
327 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
328 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
329 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
330
331 return 0;
332
333error_free_scan_mask:
334 kfree(buffer->scan_mask);
335error_cleanup_dynamic:
336 __iio_buffer_attr_cleanup(indio_dev);
337
338 return ret;
339}
340EXPORT_SYMBOL(iio_buffer_register);
341
342void iio_buffer_unregister(struct iio_dev *indio_dev)
343{
344 kfree(indio_dev->buffer->scan_mask);
345 kfree(indio_dev->buffer->scan_el_group.attrs);
346 __iio_buffer_attr_cleanup(indio_dev);
347}
348EXPORT_SYMBOL(iio_buffer_unregister);
349
350ssize_t iio_buffer_read_length(struct device *dev,
351 struct device_attribute *attr,
352 char *buf)
353{
354 struct iio_dev *indio_dev = dev_get_drvdata(dev);
355 struct iio_buffer *buffer = indio_dev->buffer;
356
357 if (buffer->access->get_length)
358 return sprintf(buf, "%d\n",
359 buffer->access->get_length(buffer));
360
361 return 0;
362}
363EXPORT_SYMBOL(iio_buffer_read_length);
364
365ssize_t iio_buffer_write_length(struct device *dev,
366 struct device_attribute *attr,
367 const char *buf,
368 size_t len)
369{
370 int ret;
371 ulong val;
372 struct iio_dev *indio_dev = dev_get_drvdata(dev);
373 struct iio_buffer *buffer = indio_dev->buffer;
374
375 ret = strict_strtoul(buf, 10, &val);
376 if (ret)
377 return ret;
378
379 if (buffer->access->get_length)
380 if (val == buffer->access->get_length(buffer))
381 return len;
382
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100383 mutex_lock(&indio_dev->mlock);
384 if (iio_buffer_enabled(indio_dev)) {
385 ret = -EBUSY;
386 } else {
Lars-Peter Clausen869871b2011-12-19 15:23:48 +0100387 if (buffer->access->set_length)
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100388 buffer->access->set_length(buffer, val);
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100389 ret = 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100390 }
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100391 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100392
Lars-Peter Clausene38c79e2011-12-19 15:23:44 +0100393 return ret ? ret : len;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100394}
395EXPORT_SYMBOL(iio_buffer_write_length);
396
Jonathan Cameron14555b12011-09-21 11:15:57 +0100397ssize_t iio_buffer_store_enable(struct device *dev,
398 struct device_attribute *attr,
399 const char *buf,
400 size_t len)
401{
402 int ret;
403 bool requested_state, current_state;
404 int previous_mode;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100405 struct iio_dev *indio_dev = dev_get_drvdata(dev);
406 struct iio_buffer *buffer = indio_dev->buffer;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100407
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100408 mutex_lock(&indio_dev->mlock);
409 previous_mode = indio_dev->currentmode;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100410 requested_state = !(buf[0] == '0');
Lars-Peter Clausend4a68822011-12-19 15:23:42 +0100411 current_state = iio_buffer_enabled(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100412 if (current_state == requested_state) {
413 printk(KERN_INFO "iio-buffer, current state requested again\n");
414 goto done;
415 }
416 if (requested_state) {
Jonathan Cameron16122442011-12-05 22:18:14 +0000417 if (indio_dev->setup_ops->preenable) {
418 ret = indio_dev->setup_ops->preenable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100419 if (ret) {
420 printk(KERN_ERR
421 "Buffer not started:"
422 "buffer preenable failed\n");
423 goto error_ret;
424 }
425 }
426 if (buffer->access->request_update) {
427 ret = buffer->access->request_update(buffer);
428 if (ret) {
429 printk(KERN_INFO
430 "Buffer not started:"
431 "buffer parameter update failed\n");
432 goto error_ret;
433 }
434 }
Jonathan Cameron14555b12011-09-21 11:15:57 +0100435 /* Definitely possible for devices to support both of these.*/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100436 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
437 if (!indio_dev->trig) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100438 printk(KERN_INFO
439 "Buffer not started: no trigger\n");
440 ret = -EINVAL;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100441 goto error_ret;
442 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100443 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
444 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE)
445 indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100446 else { /* should never be reached */
447 ret = -EINVAL;
448 goto error_ret;
449 }
450
Jonathan Cameron16122442011-12-05 22:18:14 +0000451 if (indio_dev->setup_ops->postenable) {
452 ret = indio_dev->setup_ops->postenable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100453 if (ret) {
454 printk(KERN_INFO
455 "Buffer not started:"
456 "postenable failed\n");
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100457 indio_dev->currentmode = previous_mode;
Jonathan Cameron16122442011-12-05 22:18:14 +0000458 if (indio_dev->setup_ops->postdisable)
459 indio_dev->setup_ops->
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100460 postdisable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100461 goto error_ret;
462 }
463 }
464 } else {
Jonathan Cameron16122442011-12-05 22:18:14 +0000465 if (indio_dev->setup_ops->predisable) {
466 ret = indio_dev->setup_ops->predisable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100467 if (ret)
468 goto error_ret;
469 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100470 indio_dev->currentmode = INDIO_DIRECT_MODE;
Jonathan Cameron16122442011-12-05 22:18:14 +0000471 if (indio_dev->setup_ops->postdisable) {
472 ret = indio_dev->setup_ops->postdisable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100473 if (ret)
474 goto error_ret;
475 }
476 }
477done:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100478 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100479 return len;
480
481error_ret:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100482 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100483 return ret;
484}
485EXPORT_SYMBOL(iio_buffer_store_enable);
486
487ssize_t iio_buffer_show_enable(struct device *dev,
488 struct device_attribute *attr,
489 char *buf)
490{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100491 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Lars-Peter Clausend4a68822011-12-19 15:23:42 +0100492 return sprintf(buf, "%d\n", iio_buffer_enabled(indio_dev));
Jonathan Cameron14555b12011-09-21 11:15:57 +0100493}
494EXPORT_SYMBOL(iio_buffer_show_enable);
495
Jonathan Cameron14555b12011-09-21 11:15:57 +0100496/* note NULL used as error indicator as it doesn't make sense. */
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100497static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100498 unsigned int masklength,
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100499 const unsigned long *mask)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100500{
501 if (bitmap_empty(mask, masklength))
502 return NULL;
503 while (*av_masks) {
504 if (bitmap_subset(mask, av_masks, masklength))
505 return av_masks;
506 av_masks += BITS_TO_LONGS(masklength);
507 }
508 return NULL;
509}
510
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100511static int iio_compute_scan_bytes(struct iio_dev *indio_dev, const long *mask,
512 bool timestamp)
Jonathan Cameron959d2952011-12-05 21:37:13 +0000513{
Jonathan Cameron959d2952011-12-05 21:37:13 +0000514 const struct iio_chan_spec *ch;
515 unsigned bytes = 0;
516 int length, i;
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100517
518 /* How much space will the demuxed element take? */
519 for_each_set_bit(i, mask,
520 indio_dev->masklength) {
521 ch = iio_find_channel_from_si(indio_dev, i);
522 length = ch->scan_type.storagebits / 8;
523 bytes = ALIGN(bytes, length);
524 bytes += length;
525 }
526 if (timestamp) {
527 ch = iio_find_channel_from_si(indio_dev,
528 indio_dev
529 ->buffer->scan_index_timestamp);
530 length = ch->scan_type.storagebits / 8;
531 bytes = ALIGN(bytes, length);
532 bytes += length;
533 }
534 return bytes;
535}
536
537int iio_sw_buffer_preenable(struct iio_dev *indio_dev)
538{
539 struct iio_buffer *buffer = indio_dev->buffer;
540 unsigned bytes;
Jonathan Cameron959d2952011-12-05 21:37:13 +0000541 dev_dbg(&indio_dev->dev, "%s\n", __func__);
542
543 /* How much space will the demuxed element take? */
Jonathan Cameron6b3b58e2012-04-21 10:09:33 +0100544 bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
545 buffer->scan_timestamp);
Jonathan Cameron959d2952011-12-05 21:37:13 +0000546 buffer->access->set_bytes_per_datum(buffer, bytes);
547
548 /* What scan mask do we actually have ?*/
549 if (indio_dev->available_scan_masks)
550 indio_dev->active_scan_mask =
551 iio_scan_mask_match(indio_dev->available_scan_masks,
552 indio_dev->masklength,
553 buffer->scan_mask);
554 else
555 indio_dev->active_scan_mask = buffer->scan_mask;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000556 iio_update_demux(indio_dev);
557
558 if (indio_dev->info->update_scan_mode)
559 return indio_dev->info
560 ->update_scan_mode(indio_dev,
561 indio_dev->active_scan_mask);
Jonathan Cameron959d2952011-12-05 21:37:13 +0000562 return 0;
563}
564EXPORT_SYMBOL(iio_sw_buffer_preenable);
565
Jonathan Cameron14555b12011-09-21 11:15:57 +0100566/**
567 * iio_scan_mask_set() - set particular bit in the scan mask
568 * @buffer: the buffer whose scan mask we are interested in
569 * @bit: the bit to be set.
570 **/
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000571int iio_scan_mask_set(struct iio_dev *indio_dev,
572 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100573{
Michael Hennerichcd4361c2012-02-22 13:16:49 +0100574 const unsigned long *mask;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100575 unsigned long *trialmask;
576
577 trialmask = kmalloc(sizeof(*trialmask)*
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100578 BITS_TO_LONGS(indio_dev->masklength),
Jonathan Cameron14555b12011-09-21 11:15:57 +0100579 GFP_KERNEL);
580
581 if (trialmask == NULL)
582 return -ENOMEM;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100583 if (!indio_dev->masklength) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100584 WARN_ON("trying to set scanmask prior to registering buffer\n");
585 kfree(trialmask);
586 return -EINVAL;
587 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100588 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100589 set_bit(bit, trialmask);
590
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100591 if (indio_dev->available_scan_masks) {
592 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
593 indio_dev->masklength,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100594 trialmask);
595 if (!mask) {
596 kfree(trialmask);
597 return -EINVAL;
598 }
599 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100600 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100601
602 kfree(trialmask);
603
604 return 0;
605};
606EXPORT_SYMBOL_GPL(iio_scan_mask_set);
607
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000608int iio_scan_mask_query(struct iio_dev *indio_dev,
609 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100610{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100611 if (bit > indio_dev->masklength)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100612 return -EINVAL;
613
614 if (!buffer->scan_mask)
615 return 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100616
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100617 return test_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100618};
619EXPORT_SYMBOL_GPL(iio_scan_mask_query);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000620
621/**
622 * struct iio_demux_table() - table describing demux memcpy ops
623 * @from: index to copy from
624 * @to: index to copy to
625 * @length: how many bytes to copy
626 * @l: list head used for management
627 */
628struct iio_demux_table {
629 unsigned from;
630 unsigned to;
631 unsigned length;
632 struct list_head l;
633};
634
635static unsigned char *iio_demux(struct iio_buffer *buffer,
636 unsigned char *datain)
637{
638 struct iio_demux_table *t;
639
640 if (list_empty(&buffer->demux_list))
641 return datain;
642 list_for_each_entry(t, &buffer->demux_list, l)
643 memcpy(buffer->demux_bounce + t->to,
644 datain + t->from, t->length);
645
646 return buffer->demux_bounce;
647}
648
649int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data,
650 s64 timestamp)
651{
652 unsigned char *dataout = iio_demux(buffer, data);
653
654 return buffer->access->store_to(buffer, dataout, timestamp);
655}
656EXPORT_SYMBOL_GPL(iio_push_to_buffer);
657
658int iio_update_demux(struct iio_dev *indio_dev)
659{
660 const struct iio_chan_spec *ch;
661 struct iio_buffer *buffer = indio_dev->buffer;
662 int ret, in_ind = -1, out_ind, length;
663 unsigned in_loc = 0, out_loc = 0;
664 struct iio_demux_table *p, *q;
665
666 /* Clear out any old demux */
667 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
668 list_del(&p->l);
669 kfree(p);
670 }
671 kfree(buffer->demux_bounce);
672 buffer->demux_bounce = NULL;
673
674 /* First work out which scan mode we will actually have */
675 if (bitmap_equal(indio_dev->active_scan_mask,
676 buffer->scan_mask,
677 indio_dev->masklength))
678 return 0;
679
680 /* Now we have the two masks, work from least sig and build up sizes */
681 for_each_set_bit(out_ind,
682 indio_dev->active_scan_mask,
683 indio_dev->masklength) {
684 in_ind = find_next_bit(indio_dev->active_scan_mask,
685 indio_dev->masklength,
686 in_ind + 1);
687 while (in_ind != out_ind) {
688 in_ind = find_next_bit(indio_dev->active_scan_mask,
689 indio_dev->masklength,
690 in_ind + 1);
691 ch = iio_find_channel_from_si(indio_dev, in_ind);
692 length = ch->scan_type.storagebits/8;
693 /* Make sure we are aligned */
694 in_loc += length;
695 if (in_loc % length)
696 in_loc += length - in_loc % length;
697 }
698 p = kmalloc(sizeof(*p), GFP_KERNEL);
699 if (p == NULL) {
700 ret = -ENOMEM;
701 goto error_clear_mux_table;
702 }
703 ch = iio_find_channel_from_si(indio_dev, in_ind);
704 length = ch->scan_type.storagebits/8;
705 if (out_loc % length)
706 out_loc += length - out_loc % length;
707 if (in_loc % length)
708 in_loc += length - in_loc % length;
709 p->from = in_loc;
710 p->to = out_loc;
711 p->length = length;
712 list_add_tail(&p->l, &buffer->demux_list);
713 out_loc += length;
714 in_loc += length;
715 }
716 /* Relies on scan_timestamp being last */
717 if (buffer->scan_timestamp) {
718 p = kmalloc(sizeof(*p), GFP_KERNEL);
719 if (p == NULL) {
720 ret = -ENOMEM;
721 goto error_clear_mux_table;
722 }
723 ch = iio_find_channel_from_si(indio_dev,
724 buffer->scan_index_timestamp);
725 length = ch->scan_type.storagebits/8;
726 if (out_loc % length)
727 out_loc += length - out_loc % length;
728 if (in_loc % length)
729 in_loc += length - in_loc % length;
730 p->from = in_loc;
731 p->to = out_loc;
732 p->length = length;
733 list_add_tail(&p->l, &buffer->demux_list);
734 out_loc += length;
735 in_loc += length;
736 }
737 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
738 if (buffer->demux_bounce == NULL) {
739 ret = -ENOMEM;
740 goto error_clear_mux_table;
741 }
742 return 0;
743
744error_clear_mux_table:
745 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
746 list_del(&p->l);
747 kfree(p);
748 }
749 return ret;
750}
751EXPORT_SYMBOL_GPL(iio_update_demux);