blob: 4f5a641832f4a005d127a11e663497671af4c8fe [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 Cameronf8c6f4e2011-10-06 17:14:35 +010088void iio_buffer_init(struct iio_buffer *buffer, struct iio_dev *indio_dev)
Jonathan Cameron14555b12011-09-21 11:15:57 +010089{
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +000090 INIT_LIST_HEAD(&buffer->demux_list);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +010091 buffer->indio_dev = indio_dev;
Jonathan Cameron14555b12011-09-21 11:15:57 +010092 init_waitqueue_head(&buffer->pollq);
93}
94EXPORT_SYMBOL(iio_buffer_init);
95
96static ssize_t iio_show_scan_index(struct device *dev,
97 struct device_attribute *attr,
98 char *buf)
99{
100 return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
101}
102
103static ssize_t iio_show_fixed_type(struct device *dev,
104 struct device_attribute *attr,
105 char *buf)
106{
107 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
108 u8 type = this_attr->c->scan_type.endianness;
109
110 if (type == IIO_CPU) {
Jonathan Cameron9d5d1152011-10-04 16:02:08 +0100111#ifdef __LITTLE_ENDIAN
112 type = IIO_LE;
113#else
114 type = IIO_BE;
115#endif
Jonathan Cameron14555b12011-09-21 11:15:57 +0100116 }
117 return sprintf(buf, "%s:%c%d/%d>>%u\n",
118 iio_endian_prefix[type],
119 this_attr->c->scan_type.sign,
120 this_attr->c->scan_type.realbits,
121 this_attr->c->scan_type.storagebits,
122 this_attr->c->scan_type.shift);
123}
124
125static ssize_t iio_scan_el_show(struct device *dev,
126 struct device_attribute *attr,
127 char *buf)
128{
129 int ret;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100130 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100131
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000132 ret = test_bit(to_iio_dev_attr(attr)->address,
133 indio_dev->buffer->scan_mask);
134
Jonathan Cameron14555b12011-09-21 11:15:57 +0100135 return sprintf(buf, "%d\n", ret);
136}
137
138static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
139{
140 clear_bit(bit, buffer->scan_mask);
141 buffer->scan_count--;
142 return 0;
143}
144
145static ssize_t iio_scan_el_store(struct device *dev,
146 struct device_attribute *attr,
147 const char *buf,
148 size_t len)
149{
150 int ret = 0;
151 bool state;
152 struct iio_dev *indio_dev = dev_get_drvdata(dev);
153 struct iio_buffer *buffer = indio_dev->buffer;
154 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
155
156 state = !(buf[0] == '0');
157 mutex_lock(&indio_dev->mlock);
158 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
159 ret = -EBUSY;
160 goto error_ret;
161 }
162 ret = iio_scan_mask_query(buffer, this_attr->address);
163 if (ret < 0)
164 goto error_ret;
165 if (!state && ret) {
166 ret = iio_scan_mask_clear(buffer, this_attr->address);
167 if (ret)
168 goto error_ret;
169 } else if (state && !ret) {
170 ret = iio_scan_mask_set(buffer, this_attr->address);
171 if (ret)
172 goto error_ret;
173 }
174
175error_ret:
176 mutex_unlock(&indio_dev->mlock);
177
178 return ret ? ret : len;
179
180}
181
182static ssize_t iio_scan_el_ts_show(struct device *dev,
183 struct device_attribute *attr,
184 char *buf)
185{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100186 struct iio_dev *indio_dev = dev_get_drvdata(dev);
187 return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100188}
189
190static ssize_t iio_scan_el_ts_store(struct device *dev,
191 struct device_attribute *attr,
192 const char *buf,
193 size_t len)
194{
195 int ret = 0;
196 struct iio_dev *indio_dev = dev_get_drvdata(dev);
197 bool state;
198
199 state = !(buf[0] == '0');
200 mutex_lock(&indio_dev->mlock);
201 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
202 ret = -EBUSY;
203 goto error_ret;
204 }
205 indio_dev->buffer->scan_timestamp = state;
206error_ret:
207 mutex_unlock(&indio_dev->mlock);
208
209 return ret ? ret : len;
210}
211
212static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
213 const struct iio_chan_spec *chan)
214{
215 int ret, attrcount = 0;
216 struct iio_buffer *buffer = indio_dev->buffer;
217
218 ret = __iio_add_chan_devattr("index",
219 chan,
220 &iio_show_scan_index,
221 NULL,
222 0,
223 0,
224 &indio_dev->dev,
225 &buffer->scan_el_dev_attr_list);
226 if (ret)
227 goto error_ret;
228 attrcount++;
229 ret = __iio_add_chan_devattr("type",
230 chan,
231 &iio_show_fixed_type,
232 NULL,
233 0,
234 0,
235 &indio_dev->dev,
236 &buffer->scan_el_dev_attr_list);
237 if (ret)
238 goto error_ret;
239 attrcount++;
240 if (chan->type != IIO_TIMESTAMP)
241 ret = __iio_add_chan_devattr("en",
242 chan,
243 &iio_scan_el_show,
244 &iio_scan_el_store,
245 chan->scan_index,
246 0,
247 &indio_dev->dev,
248 &buffer->scan_el_dev_attr_list);
249 else
250 ret = __iio_add_chan_devattr("en",
251 chan,
252 &iio_scan_el_ts_show,
253 &iio_scan_el_ts_store,
254 chan->scan_index,
255 0,
256 &indio_dev->dev,
257 &buffer->scan_el_dev_attr_list);
258 attrcount++;
259 ret = attrcount;
260error_ret:
261 return ret;
262}
263
264static void iio_buffer_remove_and_free_scan_dev_attr(struct iio_dev *indio_dev,
265 struct iio_dev_attr *p)
266{
267 kfree(p->dev_attr.attr.name);
268 kfree(p);
269}
270
271static void __iio_buffer_attr_cleanup(struct iio_dev *indio_dev)
272{
273 struct iio_dev_attr *p, *n;
274 struct iio_buffer *buffer = indio_dev->buffer;
275
276 list_for_each_entry_safe(p, n,
277 &buffer->scan_el_dev_attr_list, l)
278 iio_buffer_remove_and_free_scan_dev_attr(indio_dev, p);
279}
280
281static const char * const iio_scan_elements_group_name = "scan_elements";
282
283int iio_buffer_register(struct iio_dev *indio_dev,
284 const struct iio_chan_spec *channels,
285 int num_channels)
286{
287 struct iio_dev_attr *p;
288 struct attribute **attr;
289 struct iio_buffer *buffer = indio_dev->buffer;
290 int ret, i, attrn, attrcount, attrcount_orig = 0;
291
292 if (buffer->attrs)
293 indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs;
294
295 if (buffer->scan_el_attrs != NULL) {
296 attr = buffer->scan_el_attrs->attrs;
297 while (*attr++ != NULL)
298 attrcount_orig++;
299 }
300 attrcount = attrcount_orig;
301 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
302 if (channels) {
303 /* new magic */
304 for (i = 0; i < num_channels; i++) {
305 /* Establish necessary mask length */
306 if (channels[i].scan_index >
307 (int)indio_dev->masklength - 1)
308 indio_dev->masklength
309 = indio_dev->channels[i].scan_index + 1;
310
311 ret = iio_buffer_add_channel_sysfs(indio_dev,
312 &channels[i]);
313 if (ret < 0)
314 goto error_cleanup_dynamic;
315 attrcount += ret;
Jonathan Cameronbeb80602011-12-05 21:37:11 +0000316 if (channels[i].type == IIO_TIMESTAMP)
317 buffer->scan_index_timestamp =
318 channels[i].scan_index;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100319 }
320 if (indio_dev->masklength && buffer->scan_mask == NULL) {
Thomas Meyerd83fb182011-11-29 22:08:00 +0100321 buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
322 sizeof(*buffer->scan_mask),
323 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100324 if (buffer->scan_mask == NULL) {
325 ret = -ENOMEM;
326 goto error_cleanup_dynamic;
327 }
328 }
329 }
330
331 buffer->scan_el_group.name = iio_scan_elements_group_name;
332
Thomas Meyerd83fb182011-11-29 22:08:00 +0100333 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
334 sizeof(buffer->scan_el_group.attrs[0]),
335 GFP_KERNEL);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100336 if (buffer->scan_el_group.attrs == NULL) {
337 ret = -ENOMEM;
338 goto error_free_scan_mask;
339 }
340 if (buffer->scan_el_attrs)
341 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
342 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
343 attrn = attrcount_orig;
344
345 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
346 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
347 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
348
349 return 0;
350
351error_free_scan_mask:
352 kfree(buffer->scan_mask);
353error_cleanup_dynamic:
354 __iio_buffer_attr_cleanup(indio_dev);
355
356 return ret;
357}
358EXPORT_SYMBOL(iio_buffer_register);
359
360void iio_buffer_unregister(struct iio_dev *indio_dev)
361{
362 kfree(indio_dev->buffer->scan_mask);
363 kfree(indio_dev->buffer->scan_el_group.attrs);
364 __iio_buffer_attr_cleanup(indio_dev);
365}
366EXPORT_SYMBOL(iio_buffer_unregister);
367
368ssize_t iio_buffer_read_length(struct device *dev,
369 struct device_attribute *attr,
370 char *buf)
371{
372 struct iio_dev *indio_dev = dev_get_drvdata(dev);
373 struct iio_buffer *buffer = indio_dev->buffer;
374
375 if (buffer->access->get_length)
376 return sprintf(buf, "%d\n",
377 buffer->access->get_length(buffer));
378
379 return 0;
380}
381EXPORT_SYMBOL(iio_buffer_read_length);
382
383ssize_t iio_buffer_write_length(struct device *dev,
384 struct device_attribute *attr,
385 const char *buf,
386 size_t len)
387{
388 int ret;
389 ulong val;
390 struct iio_dev *indio_dev = dev_get_drvdata(dev);
391 struct iio_buffer *buffer = indio_dev->buffer;
392
393 ret = strict_strtoul(buf, 10, &val);
394 if (ret)
395 return ret;
396
397 if (buffer->access->get_length)
398 if (val == buffer->access->get_length(buffer))
399 return len;
400
401 if (buffer->access->set_length) {
402 buffer->access->set_length(buffer, val);
403 if (buffer->access->mark_param_change)
404 buffer->access->mark_param_change(buffer);
405 }
406
407 return len;
408}
409EXPORT_SYMBOL(iio_buffer_write_length);
410
Jonathan Cameron14555b12011-09-21 11:15:57 +0100411ssize_t iio_buffer_store_enable(struct device *dev,
412 struct device_attribute *attr,
413 const char *buf,
414 size_t len)
415{
416 int ret;
417 bool requested_state, current_state;
418 int previous_mode;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100419 struct iio_dev *indio_dev = dev_get_drvdata(dev);
420 struct iio_buffer *buffer = indio_dev->buffer;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100421
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100422 mutex_lock(&indio_dev->mlock);
423 previous_mode = indio_dev->currentmode;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100424 requested_state = !(buf[0] == '0');
425 current_state = !!(previous_mode & INDIO_ALL_BUFFER_MODES);
426 if (current_state == requested_state) {
427 printk(KERN_INFO "iio-buffer, current state requested again\n");
428 goto done;
429 }
430 if (requested_state) {
Jonathan Cameron16122442011-12-05 22:18:14 +0000431 if (indio_dev->setup_ops->preenable) {
432 ret = indio_dev->setup_ops->preenable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100433 if (ret) {
434 printk(KERN_ERR
435 "Buffer not started:"
436 "buffer preenable failed\n");
437 goto error_ret;
438 }
439 }
440 if (buffer->access->request_update) {
441 ret = buffer->access->request_update(buffer);
442 if (ret) {
443 printk(KERN_INFO
444 "Buffer not started:"
445 "buffer parameter update failed\n");
446 goto error_ret;
447 }
448 }
449 if (buffer->access->mark_in_use)
450 buffer->access->mark_in_use(buffer);
451 /* Definitely possible for devices to support both of these.*/
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100452 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
453 if (!indio_dev->trig) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100454 printk(KERN_INFO
455 "Buffer not started: no trigger\n");
456 ret = -EINVAL;
457 if (buffer->access->unmark_in_use)
458 buffer->access->unmark_in_use(buffer);
459 goto error_ret;
460 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100461 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
462 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE)
463 indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100464 else { /* should never be reached */
465 ret = -EINVAL;
466 goto error_ret;
467 }
468
Jonathan Cameron16122442011-12-05 22:18:14 +0000469 if (indio_dev->setup_ops->postenable) {
470 ret = indio_dev->setup_ops->postenable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100471 if (ret) {
472 printk(KERN_INFO
473 "Buffer not started:"
474 "postenable failed\n");
475 if (buffer->access->unmark_in_use)
476 buffer->access->unmark_in_use(buffer);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100477 indio_dev->currentmode = previous_mode;
Jonathan Cameron16122442011-12-05 22:18:14 +0000478 if (indio_dev->setup_ops->postdisable)
479 indio_dev->setup_ops->
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100480 postdisable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100481 goto error_ret;
482 }
483 }
484 } else {
Jonathan Cameron16122442011-12-05 22:18:14 +0000485 if (indio_dev->setup_ops->predisable) {
486 ret = indio_dev->setup_ops->predisable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100487 if (ret)
488 goto error_ret;
489 }
490 if (buffer->access->unmark_in_use)
491 buffer->access->unmark_in_use(buffer);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100492 indio_dev->currentmode = INDIO_DIRECT_MODE;
Jonathan Cameron16122442011-12-05 22:18:14 +0000493 if (indio_dev->setup_ops->postdisable) {
494 ret = indio_dev->setup_ops->postdisable(indio_dev);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100495 if (ret)
496 goto error_ret;
497 }
498 }
499done:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100500 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100501 return len;
502
503error_ret:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100504 mutex_unlock(&indio_dev->mlock);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100505 return ret;
506}
507EXPORT_SYMBOL(iio_buffer_store_enable);
508
509ssize_t iio_buffer_show_enable(struct device *dev,
510 struct device_attribute *attr,
511 char *buf)
512{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100513 struct iio_dev *indio_dev = dev_get_drvdata(dev);
514 return sprintf(buf, "%d\n", !!(indio_dev->currentmode
Jonathan Cameron14555b12011-09-21 11:15:57 +0100515 & INDIO_ALL_BUFFER_MODES));
516}
517EXPORT_SYMBOL(iio_buffer_show_enable);
518
Jonathan Cameron14555b12011-09-21 11:15:57 +0100519/* note NULL used as error indicator as it doesn't make sense. */
520static unsigned long *iio_scan_mask_match(unsigned long *av_masks,
521 unsigned int masklength,
522 unsigned long *mask)
523{
524 if (bitmap_empty(mask, masklength))
525 return NULL;
526 while (*av_masks) {
527 if (bitmap_subset(mask, av_masks, masklength))
528 return av_masks;
529 av_masks += BITS_TO_LONGS(masklength);
530 }
531 return NULL;
532}
533
Jonathan Cameron959d2952011-12-05 21:37:13 +0000534int iio_sw_buffer_preenable(struct iio_dev *indio_dev)
535{
536 struct iio_buffer *buffer = indio_dev->buffer;
537 const struct iio_chan_spec *ch;
538 unsigned bytes = 0;
539 int length, i;
540 dev_dbg(&indio_dev->dev, "%s\n", __func__);
541
542 /* How much space will the demuxed element take? */
543 for_each_set_bit(i, buffer->scan_mask,
544 indio_dev->masklength) {
545 ch = iio_find_channel_from_si(indio_dev, i);
546 length = ch->scan_type.storagebits/8;
547 bytes = ALIGN(bytes, length);
548 bytes += length;
549 }
550 if (buffer->scan_timestamp) {
551 ch = iio_find_channel_from_si(indio_dev,
552 buffer->scan_index_timestamp);
553 length = ch->scan_type.storagebits/8;
554 bytes = ALIGN(bytes, length);
555 bytes += length;
556 }
557 buffer->access->set_bytes_per_datum(buffer, bytes);
558
559 /* What scan mask do we actually have ?*/
560 if (indio_dev->available_scan_masks)
561 indio_dev->active_scan_mask =
562 iio_scan_mask_match(indio_dev->available_scan_masks,
563 indio_dev->masklength,
564 buffer->scan_mask);
565 else
566 indio_dev->active_scan_mask = buffer->scan_mask;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000567 iio_update_demux(indio_dev);
568
569 if (indio_dev->info->update_scan_mode)
570 return indio_dev->info
571 ->update_scan_mode(indio_dev,
572 indio_dev->active_scan_mask);
Jonathan Cameron959d2952011-12-05 21:37:13 +0000573 return 0;
574}
575EXPORT_SYMBOL(iio_sw_buffer_preenable);
576
Jonathan Cameron14555b12011-09-21 11:15:57 +0100577/**
578 * iio_scan_mask_set() - set particular bit in the scan mask
579 * @buffer: the buffer whose scan mask we are interested in
580 * @bit: the bit to be set.
581 **/
582int iio_scan_mask_set(struct iio_buffer *buffer, int bit)
583{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100584 struct iio_dev *indio_dev = buffer->indio_dev;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100585 unsigned long *mask;
586 unsigned long *trialmask;
587
588 trialmask = kmalloc(sizeof(*trialmask)*
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100589 BITS_TO_LONGS(indio_dev->masklength),
Jonathan Cameron14555b12011-09-21 11:15:57 +0100590 GFP_KERNEL);
591
592 if (trialmask == NULL)
593 return -ENOMEM;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100594 if (!indio_dev->masklength) {
Jonathan Cameron14555b12011-09-21 11:15:57 +0100595 WARN_ON("trying to set scanmask prior to registering buffer\n");
596 kfree(trialmask);
597 return -EINVAL;
598 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100599 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100600 set_bit(bit, trialmask);
601
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100602 if (indio_dev->available_scan_masks) {
603 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
604 indio_dev->masklength,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100605 trialmask);
606 if (!mask) {
607 kfree(trialmask);
608 return -EINVAL;
609 }
610 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100611 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
Jonathan Cameron14555b12011-09-21 11:15:57 +0100612 buffer->scan_count++;
613
614 kfree(trialmask);
615
616 return 0;
617};
618EXPORT_SYMBOL_GPL(iio_scan_mask_set);
619
620int iio_scan_mask_query(struct iio_buffer *buffer, int bit)
621{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100622 struct iio_dev *indio_dev = buffer->indio_dev;
Jonathan Cameron14555b12011-09-21 11:15:57 +0100623 long *mask;
624
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100625 if (bit > indio_dev->masklength)
Jonathan Cameron14555b12011-09-21 11:15:57 +0100626 return -EINVAL;
627
628 if (!buffer->scan_mask)
629 return 0;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100630 if (indio_dev->available_scan_masks)
631 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
632 indio_dev->masklength,
Jonathan Cameron14555b12011-09-21 11:15:57 +0100633 buffer->scan_mask);
634 else
635 mask = buffer->scan_mask;
636 if (!mask)
637 return 0;
638
639 return test_bit(bit, mask);
640};
641EXPORT_SYMBOL_GPL(iio_scan_mask_query);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000642
643/**
644 * struct iio_demux_table() - table describing demux memcpy ops
645 * @from: index to copy from
646 * @to: index to copy to
647 * @length: how many bytes to copy
648 * @l: list head used for management
649 */
650struct iio_demux_table {
651 unsigned from;
652 unsigned to;
653 unsigned length;
654 struct list_head l;
655};
656
657static unsigned char *iio_demux(struct iio_buffer *buffer,
658 unsigned char *datain)
659{
660 struct iio_demux_table *t;
661
662 if (list_empty(&buffer->demux_list))
663 return datain;
664 list_for_each_entry(t, &buffer->demux_list, l)
665 memcpy(buffer->demux_bounce + t->to,
666 datain + t->from, t->length);
667
668 return buffer->demux_bounce;
669}
670
671int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data,
672 s64 timestamp)
673{
674 unsigned char *dataout = iio_demux(buffer, data);
675
676 return buffer->access->store_to(buffer, dataout, timestamp);
677}
678EXPORT_SYMBOL_GPL(iio_push_to_buffer);
679
680int iio_update_demux(struct iio_dev *indio_dev)
681{
682 const struct iio_chan_spec *ch;
683 struct iio_buffer *buffer = indio_dev->buffer;
684 int ret, in_ind = -1, out_ind, length;
685 unsigned in_loc = 0, out_loc = 0;
686 struct iio_demux_table *p, *q;
687
688 /* Clear out any old demux */
689 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
690 list_del(&p->l);
691 kfree(p);
692 }
693 kfree(buffer->demux_bounce);
694 buffer->demux_bounce = NULL;
695
696 /* First work out which scan mode we will actually have */
697 if (bitmap_equal(indio_dev->active_scan_mask,
698 buffer->scan_mask,
699 indio_dev->masklength))
700 return 0;
701
702 /* Now we have the two masks, work from least sig and build up sizes */
703 for_each_set_bit(out_ind,
704 indio_dev->active_scan_mask,
705 indio_dev->masklength) {
706 in_ind = find_next_bit(indio_dev->active_scan_mask,
707 indio_dev->masklength,
708 in_ind + 1);
709 while (in_ind != out_ind) {
710 in_ind = find_next_bit(indio_dev->active_scan_mask,
711 indio_dev->masklength,
712 in_ind + 1);
713 ch = iio_find_channel_from_si(indio_dev, in_ind);
714 length = ch->scan_type.storagebits/8;
715 /* Make sure we are aligned */
716 in_loc += length;
717 if (in_loc % length)
718 in_loc += length - in_loc % length;
719 }
720 p = kmalloc(sizeof(*p), GFP_KERNEL);
721 if (p == NULL) {
722 ret = -ENOMEM;
723 goto error_clear_mux_table;
724 }
725 ch = iio_find_channel_from_si(indio_dev, in_ind);
726 length = ch->scan_type.storagebits/8;
727 if (out_loc % length)
728 out_loc += length - out_loc % length;
729 if (in_loc % length)
730 in_loc += length - in_loc % length;
731 p->from = in_loc;
732 p->to = out_loc;
733 p->length = length;
734 list_add_tail(&p->l, &buffer->demux_list);
735 out_loc += length;
736 in_loc += length;
737 }
738 /* Relies on scan_timestamp being last */
739 if (buffer->scan_timestamp) {
740 p = kmalloc(sizeof(*p), GFP_KERNEL);
741 if (p == NULL) {
742 ret = -ENOMEM;
743 goto error_clear_mux_table;
744 }
745 ch = iio_find_channel_from_si(indio_dev,
746 buffer->scan_index_timestamp);
747 length = ch->scan_type.storagebits/8;
748 if (out_loc % length)
749 out_loc += length - out_loc % length;
750 if (in_loc % length)
751 in_loc += length - in_loc % length;
752 p->from = in_loc;
753 p->to = out_loc;
754 p->length = length;
755 list_add_tail(&p->l, &buffer->demux_list);
756 out_loc += length;
757 in_loc += length;
758 }
759 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
760 if (buffer->demux_bounce == NULL) {
761 ret = -ENOMEM;
762 goto error_clear_mux_table;
763 }
764 return 0;
765
766error_clear_mux_table:
767 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
768 list_del(&p->l);
769 kfree(p);
770 }
771 return ret;
772}
773EXPORT_SYMBOL_GPL(iio_update_demux);