blob: 747b9013a6661e5f57264621d0c5476b08e59886 [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
399 if (buffer->access->set_length) {
400 buffer->access->set_length(buffer, val);
401 if (buffer->access->mark_param_change)
402 buffer->access->mark_param_change(buffer);
403 }
404
405 return len;
406}
407EXPORT_SYMBOL(iio_buffer_write_length);
408
Jonathan Cameron14555b12011-09-21 11:15:57 +0100409ssize_t iio_buffer_store_enable(struct device *dev,
410 struct device_attribute *attr,
411 const char *buf,
412 size_t len)
413{
414 int ret;
415 bool requested_state, current_state;
416 int previous_mode;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100417 struct iio_dev *indio_dev = dev_get_drvdata(dev);
418 struct iio_buffer *buffer = indio_dev->buffer;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100419
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100420 mutex_lock(&indio_dev->mlock);
421 previous_mode = indio_dev->currentmode;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100422 requested_state = !(buf[0] == '0');
Lars-Peter Clausend4a68822011-12-19 15:23:42 +0100423 current_state = iio_buffer_enabled(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100424 if (current_state == requested_state) {
425 printk(KERN_INFO "iio-buffer, current state requested again\n");
426 goto done;
427 }
428 if (requested_state) {
Jonathan Cameron16122442011-12-05 22:18:14 +0000429 if (indio_dev->setup_ops->preenable) {
430 ret = indio_dev->setup_ops->preenable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100431 if (ret) {
432 printk(KERN_ERR
433 "Buffer not started:"
434 "buffer preenable failed\n");
435 goto error_ret;
436 }
437 }
438 if (buffer->access->request_update) {
439 ret = buffer->access->request_update(buffer);
440 if (ret) {
441 printk(KERN_INFO
442 "Buffer not started:"
443 "buffer parameter update failed\n");
444 goto error_ret;
445 }
446 }
447 if (buffer->access->mark_in_use)
448 buffer->access->mark_in_use(buffer);
449 /* Definitely possible for devices to support both of these.*/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100450 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
451 if (!indio_dev->trig) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100452 printk(KERN_INFO
453 "Buffer not started: no trigger\n");
454 ret = -EINVAL;
455 if (buffer->access->unmark_in_use)
456 buffer->access->unmark_in_use(buffer);
457 goto error_ret;
458 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100459 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
460 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE)
461 indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100462 else { /* should never be reached */
463 ret = -EINVAL;
464 goto error_ret;
465 }
466
Jonathan Cameron16122442011-12-05 22:18:14 +0000467 if (indio_dev->setup_ops->postenable) {
468 ret = indio_dev->setup_ops->postenable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100469 if (ret) {
470 printk(KERN_INFO
471 "Buffer not started:"
472 "postenable failed\n");
473 if (buffer->access->unmark_in_use)
474 buffer->access->unmark_in_use(buffer);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100475 indio_dev->currentmode = previous_mode;
Jonathan Cameron16122442011-12-05 22:18:14 +0000476 if (indio_dev->setup_ops->postdisable)
477 indio_dev->setup_ops->
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100478 postdisable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100479 goto error_ret;
480 }
481 }
482 } else {
Jonathan Cameron16122442011-12-05 22:18:14 +0000483 if (indio_dev->setup_ops->predisable) {
484 ret = indio_dev->setup_ops->predisable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100485 if (ret)
486 goto error_ret;
487 }
488 if (buffer->access->unmark_in_use)
489 buffer->access->unmark_in_use(buffer);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100490 indio_dev->currentmode = INDIO_DIRECT_MODE;
Jonathan Cameron16122442011-12-05 22:18:14 +0000491 if (indio_dev->setup_ops->postdisable) {
492 ret = indio_dev->setup_ops->postdisable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100493 if (ret)
494 goto error_ret;
495 }
496 }
497done:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100498 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100499 return len;
500
501error_ret:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100502 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100503 return ret;
504}
505EXPORT_SYMBOL(iio_buffer_store_enable);
506
507ssize_t iio_buffer_show_enable(struct device *dev,
508 struct device_attribute *attr,
509 char *buf)
510{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100511 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Lars-Peter Clausend4a68822011-12-19 15:23:42 +0100512 return sprintf(buf, "%d\n", iio_buffer_enabled(indio_dev));
Jonathan Cameron14555b12011-09-21 11:15:57 +0100513}
514EXPORT_SYMBOL(iio_buffer_show_enable);
515
Jonathan Cameron14555b12011-09-21 11:15:57 +0100516/* note NULL used as error indicator as it doesn't make sense. */
517static unsigned long *iio_scan_mask_match(unsigned long *av_masks,
518 unsigned int masklength,
519 unsigned long *mask)
520{
521 if (bitmap_empty(mask, masklength))
522 return NULL;
523 while (*av_masks) {
524 if (bitmap_subset(mask, av_masks, masklength))
525 return av_masks;
526 av_masks += BITS_TO_LONGS(masklength);
527 }
528 return NULL;
529}
530
Jonathan Cameron959d2952011-12-05 21:37:13 +0000531int iio_sw_buffer_preenable(struct iio_dev *indio_dev)
532{
533 struct iio_buffer *buffer = indio_dev->buffer;
534 const struct iio_chan_spec *ch;
535 unsigned bytes = 0;
536 int length, i;
537 dev_dbg(&indio_dev->dev, "%s\n", __func__);
538
539 /* How much space will the demuxed element take? */
540 for_each_set_bit(i, buffer->scan_mask,
541 indio_dev->masklength) {
542 ch = iio_find_channel_from_si(indio_dev, i);
543 length = ch->scan_type.storagebits/8;
544 bytes = ALIGN(bytes, length);
545 bytes += length;
546 }
547 if (buffer->scan_timestamp) {
548 ch = iio_find_channel_from_si(indio_dev,
549 buffer->scan_index_timestamp);
550 length = ch->scan_type.storagebits/8;
551 bytes = ALIGN(bytes, length);
552 bytes += length;
553 }
554 buffer->access->set_bytes_per_datum(buffer, bytes);
555
556 /* What scan mask do we actually have ?*/
557 if (indio_dev->available_scan_masks)
558 indio_dev->active_scan_mask =
559 iio_scan_mask_match(indio_dev->available_scan_masks,
560 indio_dev->masklength,
561 buffer->scan_mask);
562 else
563 indio_dev->active_scan_mask = buffer->scan_mask;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000564 iio_update_demux(indio_dev);
565
566 if (indio_dev->info->update_scan_mode)
567 return indio_dev->info
568 ->update_scan_mode(indio_dev,
569 indio_dev->active_scan_mask);
Jonathan Cameron959d2952011-12-05 21:37:13 +0000570 return 0;
571}
572EXPORT_SYMBOL(iio_sw_buffer_preenable);
573
Jonathan Cameron14555b12011-09-21 11:15:57 +0100574/**
575 * iio_scan_mask_set() - set particular bit in the scan mask
576 * @buffer: the buffer whose scan mask we are interested in
577 * @bit: the bit to be set.
578 **/
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000579int iio_scan_mask_set(struct iio_dev *indio_dev,
580 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100581{
Jonathan Cameron14555b12011-09-21 11:15:57 +0100582 unsigned long *mask;
583 unsigned long *trialmask;
584
585 trialmask = kmalloc(sizeof(*trialmask)*
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100586 BITS_TO_LONGS(indio_dev->masklength),
Jonathan Cameron14555b12011-09-21 11:15:57 +0100587 GFP_KERNEL);
588
589 if (trialmask == NULL)
590 return -ENOMEM;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100591 if (!indio_dev->masklength) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100592 WARN_ON("trying to set scanmask prior to registering buffer\n");
593 kfree(trialmask);
594 return -EINVAL;
595 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100596 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100597 set_bit(bit, trialmask);
598
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100599 if (indio_dev->available_scan_masks) {
600 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
601 indio_dev->masklength,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100602 trialmask);
603 if (!mask) {
604 kfree(trialmask);
605 return -EINVAL;
606 }
607 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100608 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100609
610 kfree(trialmask);
611
612 return 0;
613};
614EXPORT_SYMBOL_GPL(iio_scan_mask_set);
615
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000616int iio_scan_mask_query(struct iio_dev *indio_dev,
617 struct iio_buffer *buffer, int bit)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100618{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100619 if (bit > indio_dev->masklength)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100620 return -EINVAL;
621
622 if (!buffer->scan_mask)
623 return 0;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100624
Lars-Peter Clausen5a2a6e12011-12-08 18:35:53 +0100625 return test_bit(bit, buffer->scan_mask);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100626};
627EXPORT_SYMBOL_GPL(iio_scan_mask_query);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000628
629/**
630 * struct iio_demux_table() - table describing demux memcpy ops
631 * @from: index to copy from
632 * @to: index to copy to
633 * @length: how many bytes to copy
634 * @l: list head used for management
635 */
636struct iio_demux_table {
637 unsigned from;
638 unsigned to;
639 unsigned length;
640 struct list_head l;
641};
642
643static unsigned char *iio_demux(struct iio_buffer *buffer,
644 unsigned char *datain)
645{
646 struct iio_demux_table *t;
647
648 if (list_empty(&buffer->demux_list))
649 return datain;
650 list_for_each_entry(t, &buffer->demux_list, l)
651 memcpy(buffer->demux_bounce + t->to,
652 datain + t->from, t->length);
653
654 return buffer->demux_bounce;
655}
656
657int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data,
658 s64 timestamp)
659{
660 unsigned char *dataout = iio_demux(buffer, data);
661
662 return buffer->access->store_to(buffer, dataout, timestamp);
663}
664EXPORT_SYMBOL_GPL(iio_push_to_buffer);
665
666int iio_update_demux(struct iio_dev *indio_dev)
667{
668 const struct iio_chan_spec *ch;
669 struct iio_buffer *buffer = indio_dev->buffer;
670 int ret, in_ind = -1, out_ind, length;
671 unsigned in_loc = 0, out_loc = 0;
672 struct iio_demux_table *p, *q;
673
674 /* Clear out any old demux */
675 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
676 list_del(&p->l);
677 kfree(p);
678 }
679 kfree(buffer->demux_bounce);
680 buffer->demux_bounce = NULL;
681
682 /* First work out which scan mode we will actually have */
683 if (bitmap_equal(indio_dev->active_scan_mask,
684 buffer->scan_mask,
685 indio_dev->masklength))
686 return 0;
687
688 /* Now we have the two masks, work from least sig and build up sizes */
689 for_each_set_bit(out_ind,
690 indio_dev->active_scan_mask,
691 indio_dev->masklength) {
692 in_ind = find_next_bit(indio_dev->active_scan_mask,
693 indio_dev->masklength,
694 in_ind + 1);
695 while (in_ind != out_ind) {
696 in_ind = find_next_bit(indio_dev->active_scan_mask,
697 indio_dev->masklength,
698 in_ind + 1);
699 ch = iio_find_channel_from_si(indio_dev, in_ind);
700 length = ch->scan_type.storagebits/8;
701 /* Make sure we are aligned */
702 in_loc += length;
703 if (in_loc % length)
704 in_loc += length - in_loc % length;
705 }
706 p = kmalloc(sizeof(*p), GFP_KERNEL);
707 if (p == NULL) {
708 ret = -ENOMEM;
709 goto error_clear_mux_table;
710 }
711 ch = iio_find_channel_from_si(indio_dev, in_ind);
712 length = ch->scan_type.storagebits/8;
713 if (out_loc % length)
714 out_loc += length - out_loc % length;
715 if (in_loc % length)
716 in_loc += length - in_loc % length;
717 p->from = in_loc;
718 p->to = out_loc;
719 p->length = length;
720 list_add_tail(&p->l, &buffer->demux_list);
721 out_loc += length;
722 in_loc += length;
723 }
724 /* Relies on scan_timestamp being last */
725 if (buffer->scan_timestamp) {
726 p = kmalloc(sizeof(*p), GFP_KERNEL);
727 if (p == NULL) {
728 ret = -ENOMEM;
729 goto error_clear_mux_table;
730 }
731 ch = iio_find_channel_from_si(indio_dev,
732 buffer->scan_index_timestamp);
733 length = ch->scan_type.storagebits/8;
734 if (out_loc % length)
735 out_loc += length - out_loc % length;
736 if (in_loc % length)
737 in_loc += length - in_loc % length;
738 p->from = in_loc;
739 p->to = out_loc;
740 p->length = length;
741 list_add_tail(&p->l, &buffer->demux_list);
742 out_loc += length;
743 in_loc += length;
744 }
745 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
746 if (buffer->demux_bounce == NULL) {
747 ret = -ENOMEM;
748 goto error_clear_mux_table;
749 }
750 return 0;
751
752error_clear_mux_table:
753 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
754 list_del(&p->l);
755 kfree(p);
756 }
757 return ret;
758}
759EXPORT_SYMBOL_GPL(iio_update_demux);