iio: add watermark logic to iio read and poll
[deliverable/linux.git] / drivers / iio / industrialio-buffer.c
CommitLineData
7026ea4b
JC
1/* 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 *
14555b14 9 * Handling of buffer allocation / resizing.
7026ea4b
JC
10 *
11 *
12 * Things to look at here.
13 * - Better memory allocation techniques?
14 * - Alternative access techniques?
15 */
16#include <linux/kernel.h>
8e336a72 17#include <linux/export.h>
7026ea4b 18#include <linux/device.h>
7026ea4b 19#include <linux/fs.h>
7026ea4b 20#include <linux/cdev.h>
5a0e3ad6 21#include <linux/slab.h>
a7348347 22#include <linux/poll.h>
d2f0a48f 23#include <linux/sched.h>
7026ea4b 24
06458e27 25#include <linux/iio/iio.h>
df9c1c42 26#include "iio_core.h"
06458e27
JC
27#include <linux/iio/sysfs.h>
28#include <linux/iio/buffer.h>
7026ea4b 29
8310b86c
JC
30static const char * const iio_endian_prefix[] = {
31 [IIO_BE] = "be",
32 [IIO_LE] = "le",
33};
7026ea4b 34
705ee2c9 35static bool iio_buffer_is_active(struct iio_buffer *buf)
84b36ce5 36{
705ee2c9 37 return !list_empty(&buf->buffer_list);
84b36ce5
JC
38}
39
37d34556 40static size_t iio_buffer_data_available(struct iio_buffer *buf)
647cc7b9 41{
9dd4694d 42 return buf->access->data_available(buf);
647cc7b9
LPC
43}
44
37d34556
JC
45static bool iio_buffer_ready(struct iio_dev *indio_dev, struct iio_buffer *buf,
46 size_t to_wait)
47{
48 /* wakeup if the device was unregistered */
49 if (!indio_dev->info)
50 return true;
51
52 /* drain the buffer if it was disabled */
53 if (!iio_buffer_is_active(buf))
54 to_wait = min_t(size_t, to_wait, 1);
55
56 if (iio_buffer_data_available(buf) >= to_wait)
57 return true;
58
59 return false;
60}
61
7026ea4b 62/**
14555b14 63 * iio_buffer_read_first_n_outer() - chrdev read for buffer access
7026ea4b 64 *
14555b14
JC
65 * This function relies on all buffer implementations having an
66 * iio_buffer as their first element.
7026ea4b 67 **/
14555b14
JC
68ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
69 size_t n, loff_t *f_ps)
7026ea4b 70{
1aa04278 71 struct iio_dev *indio_dev = filp->private_data;
14555b14 72 struct iio_buffer *rb = indio_dev->buffer;
37d34556
JC
73 size_t datum_size;
74 size_t to_wait = 0;
ee551a10 75 int ret;
d5857d65 76
f18e7a06
LPC
77 if (!indio_dev->info)
78 return -ENODEV;
79
96e00f11 80 if (!rb || !rb->access->read_first_n)
7026ea4b 81 return -EINVAL;
ee551a10 82
37d34556
JC
83 datum_size = rb->bytes_per_datum;
84
85 /*
86 * If datum_size is 0 there will never be anything to read from the
87 * buffer, so signal end of file now.
88 */
89 if (!datum_size)
90 return 0;
91
92 if (!(filp->f_flags & O_NONBLOCK))
93 to_wait = min_t(size_t, n / datum_size, rb->watermark);
94
ee551a10 95 do {
37d34556
JC
96 ret = wait_event_interruptible(rb->pollq,
97 iio_buffer_ready(indio_dev, rb, to_wait));
98 if (ret)
99 return ret;
ee551a10 100
37d34556
JC
101 if (!indio_dev->info)
102 return -ENODEV;
ee551a10
LPC
103
104 ret = rb->access->read_first_n(rb, n, buf);
105 if (ret == 0 && (filp->f_flags & O_NONBLOCK))
106 ret = -EAGAIN;
107 } while (ret == 0);
108
109 return ret;
7026ea4b
JC
110}
111
a7348347 112/**
14555b14 113 * iio_buffer_poll() - poll the buffer to find out if it has data
a7348347 114 */
14555b14
JC
115unsigned int iio_buffer_poll(struct file *filp,
116 struct poll_table_struct *wait)
a7348347 117{
1aa04278 118 struct iio_dev *indio_dev = filp->private_data;
14555b14 119 struct iio_buffer *rb = indio_dev->buffer;
a7348347 120
f18e7a06
LPC
121 if (!indio_dev->info)
122 return -ENODEV;
123
a7348347 124 poll_wait(filp, &rb->pollq, wait);
37d34556 125 if (iio_buffer_ready(indio_dev, rb, rb->watermark))
a7348347 126 return POLLIN | POLLRDNORM;
8d213f24 127 return 0;
a7348347
JC
128}
129
d2f0a48f
LPC
130/**
131 * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue
132 * @indio_dev: The IIO device
133 *
134 * Wakes up the event waitqueue used for poll(). Should usually
135 * be called when the device is unregistered.
136 */
137void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
138{
139 if (!indio_dev->buffer)
140 return;
141
142 wake_up(&indio_dev->buffer->pollq);
143}
144
f79a9098 145void iio_buffer_init(struct iio_buffer *buffer)
7026ea4b 146{
5ada4ea9 147 INIT_LIST_HEAD(&buffer->demux_list);
705ee2c9 148 INIT_LIST_HEAD(&buffer->buffer_list);
14555b14 149 init_waitqueue_head(&buffer->pollq);
9e69c935 150 kref_init(&buffer->ref);
37d34556 151 buffer->watermark = 1;
7026ea4b 152}
14555b14 153EXPORT_SYMBOL(iio_buffer_init);
7026ea4b 154
1d892719 155static ssize_t iio_show_scan_index(struct device *dev,
8d213f24
JC
156 struct device_attribute *attr,
157 char *buf)
1d892719 158{
8d213f24 159 return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
1d892719
JC
160}
161
162static ssize_t iio_show_fixed_type(struct device *dev,
163 struct device_attribute *attr,
164 char *buf)
165{
166 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
8310b86c
JC
167 u8 type = this_attr->c->scan_type.endianness;
168
169 if (type == IIO_CPU) {
9d5d1153
JC
170#ifdef __LITTLE_ENDIAN
171 type = IIO_LE;
172#else
173 type = IIO_BE;
174#endif
8310b86c 175 }
0ee8546a
SP
176 if (this_attr->c->scan_type.repeat > 1)
177 return sprintf(buf, "%s:%c%d/%dX%d>>%u\n",
178 iio_endian_prefix[type],
179 this_attr->c->scan_type.sign,
180 this_attr->c->scan_type.realbits,
181 this_attr->c->scan_type.storagebits,
182 this_attr->c->scan_type.repeat,
183 this_attr->c->scan_type.shift);
184 else
185 return sprintf(buf, "%s:%c%d/%d>>%u\n",
8310b86c 186 iio_endian_prefix[type],
1d892719
JC
187 this_attr->c->scan_type.sign,
188 this_attr->c->scan_type.realbits,
189 this_attr->c->scan_type.storagebits,
190 this_attr->c->scan_type.shift);
191}
192
8d213f24
JC
193static ssize_t iio_scan_el_show(struct device *dev,
194 struct device_attribute *attr,
195 char *buf)
196{
197 int ret;
e53f5ac5 198 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
8d213f24 199
2076a20f
AB
200 /* Ensure ret is 0 or 1. */
201 ret = !!test_bit(to_iio_dev_attr(attr)->address,
5ada4ea9
JC
202 indio_dev->buffer->scan_mask);
203
8d213f24
JC
204 return sprintf(buf, "%d\n", ret);
205}
206
217a5cf0
LPC
207/* Note NULL used as error indicator as it doesn't make sense. */
208static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
209 unsigned int masklength,
210 const unsigned long *mask)
211{
212 if (bitmap_empty(mask, masklength))
213 return NULL;
214 while (*av_masks) {
215 if (bitmap_subset(mask, av_masks, masklength))
216 return av_masks;
217 av_masks += BITS_TO_LONGS(masklength);
218 }
219 return NULL;
220}
221
222static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
223 const unsigned long *mask)
224{
225 if (!indio_dev->setup_ops->validate_scan_mask)
226 return true;
227
228 return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
229}
230
231/**
232 * iio_scan_mask_set() - set particular bit in the scan mask
233 * @indio_dev: the iio device
234 * @buffer: the buffer whose scan mask we are interested in
235 * @bit: the bit to be set.
236 *
237 * Note that at this point we have no way of knowing what other
238 * buffers might request, hence this code only verifies that the
239 * individual buffers request is plausible.
240 */
241static int iio_scan_mask_set(struct iio_dev *indio_dev,
242 struct iio_buffer *buffer, int bit)
243{
244 const unsigned long *mask;
245 unsigned long *trialmask;
246
247 trialmask = kmalloc(sizeof(*trialmask)*
248 BITS_TO_LONGS(indio_dev->masklength),
249 GFP_KERNEL);
250
251 if (trialmask == NULL)
252 return -ENOMEM;
253 if (!indio_dev->masklength) {
254 WARN_ON("Trying to set scanmask prior to registering buffer\n");
255 goto err_invalid_mask;
256 }
257 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
258 set_bit(bit, trialmask);
259
260 if (!iio_validate_scan_mask(indio_dev, trialmask))
261 goto err_invalid_mask;
262
263 if (indio_dev->available_scan_masks) {
264 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
265 indio_dev->masklength,
266 trialmask);
267 if (!mask)
268 goto err_invalid_mask;
269 }
270 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
271
272 kfree(trialmask);
273
274 return 0;
275
276err_invalid_mask:
277 kfree(trialmask);
278 return -EINVAL;
279}
280
14555b14 281static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
8d213f24 282{
14555b14 283 clear_bit(bit, buffer->scan_mask);
8d213f24
JC
284 return 0;
285}
286
287static ssize_t iio_scan_el_store(struct device *dev,
288 struct device_attribute *attr,
289 const char *buf,
290 size_t len)
291{
a714af27 292 int ret;
8d213f24 293 bool state;
e53f5ac5 294 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
14555b14 295 struct iio_buffer *buffer = indio_dev->buffer;
8d213f24
JC
296 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
297
a714af27
JC
298 ret = strtobool(buf, &state);
299 if (ret < 0)
300 return ret;
8d213f24 301 mutex_lock(&indio_dev->mlock);
705ee2c9 302 if (iio_buffer_is_active(indio_dev->buffer)) {
8d213f24
JC
303 ret = -EBUSY;
304 goto error_ret;
305 }
f79a9098 306 ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
8d213f24
JC
307 if (ret < 0)
308 goto error_ret;
309 if (!state && ret) {
14555b14 310 ret = iio_scan_mask_clear(buffer, this_attr->address);
8d213f24
JC
311 if (ret)
312 goto error_ret;
313 } else if (state && !ret) {
f79a9098 314 ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
8d213f24
JC
315 if (ret)
316 goto error_ret;
317 }
318
319error_ret:
320 mutex_unlock(&indio_dev->mlock);
321
5a2a6e11 322 return ret < 0 ? ret : len;
8d213f24
JC
323
324}
325
326static ssize_t iio_scan_el_ts_show(struct device *dev,
327 struct device_attribute *attr,
328 char *buf)
329{
e53f5ac5 330 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
f8c6f4e9 331 return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
8d213f24
JC
332}
333
334static ssize_t iio_scan_el_ts_store(struct device *dev,
335 struct device_attribute *attr,
336 const char *buf,
337 size_t len)
338{
a714af27 339 int ret;
e53f5ac5 340 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
8d213f24 341 bool state;
1aa04278 342
a714af27
JC
343 ret = strtobool(buf, &state);
344 if (ret < 0)
345 return ret;
346
8d213f24 347 mutex_lock(&indio_dev->mlock);
705ee2c9 348 if (iio_buffer_is_active(indio_dev->buffer)) {
8d213f24
JC
349 ret = -EBUSY;
350 goto error_ret;
351 }
14555b14 352 indio_dev->buffer->scan_timestamp = state;
8d213f24
JC
353error_ret:
354 mutex_unlock(&indio_dev->mlock);
355
356 return ret ? ret : len;
357}
358
14555b14
JC
359static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
360 const struct iio_chan_spec *chan)
1d892719 361{
26d25ae3 362 int ret, attrcount = 0;
14555b14 363 struct iio_buffer *buffer = indio_dev->buffer;
1d892719 364
26d25ae3 365 ret = __iio_add_chan_devattr("index",
1d892719
JC
366 chan,
367 &iio_show_scan_index,
368 NULL,
369 0,
3704432f 370 IIO_SEPARATE,
1aa04278 371 &indio_dev->dev,
14555b14 372 &buffer->scan_el_dev_attr_list);
1d892719 373 if (ret)
92825ff9 374 return ret;
26d25ae3
JC
375 attrcount++;
376 ret = __iio_add_chan_devattr("type",
1d892719
JC
377 chan,
378 &iio_show_fixed_type,
379 NULL,
380 0,
381 0,
1aa04278 382 &indio_dev->dev,
14555b14 383 &buffer->scan_el_dev_attr_list);
1d892719 384 if (ret)
92825ff9 385 return ret;
26d25ae3 386 attrcount++;
a88b3ebc 387 if (chan->type != IIO_TIMESTAMP)
26d25ae3 388 ret = __iio_add_chan_devattr("en",
a88b3ebc
JC
389 chan,
390 &iio_scan_el_show,
391 &iio_scan_el_store,
392 chan->scan_index,
393 0,
1aa04278 394 &indio_dev->dev,
14555b14 395 &buffer->scan_el_dev_attr_list);
a88b3ebc 396 else
26d25ae3 397 ret = __iio_add_chan_devattr("en",
a88b3ebc
JC
398 chan,
399 &iio_scan_el_ts_show,
400 &iio_scan_el_ts_store,
401 chan->scan_index,
402 0,
1aa04278 403 &indio_dev->dev,
14555b14 404 &buffer->scan_el_dev_attr_list);
9572588c 405 if (ret)
92825ff9 406 return ret;
26d25ae3
JC
407 attrcount++;
408 ret = attrcount;
1d892719
JC
409 return ret;
410}
411
08e7e0ad
LPC
412static ssize_t iio_buffer_read_length(struct device *dev,
413 struct device_attribute *attr,
414 char *buf)
7026ea4b 415{
e53f5ac5 416 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
14555b14 417 struct iio_buffer *buffer = indio_dev->buffer;
7026ea4b 418
37495660 419 return sprintf(buf, "%d\n", buffer->length);
7026ea4b 420}
7026ea4b 421
08e7e0ad
LPC
422static ssize_t iio_buffer_write_length(struct device *dev,
423 struct device_attribute *attr,
424 const char *buf, size_t len)
7026ea4b 425{
e53f5ac5 426 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
14555b14 427 struct iio_buffer *buffer = indio_dev->buffer;
948ad205
LPC
428 unsigned int val;
429 int ret;
8d213f24 430
948ad205 431 ret = kstrtouint(buf, 10, &val);
7026ea4b
JC
432 if (ret)
433 return ret;
434
37495660
LPC
435 if (val == buffer->length)
436 return len;
7026ea4b 437
e38c79e0 438 mutex_lock(&indio_dev->mlock);
705ee2c9 439 if (iio_buffer_is_active(indio_dev->buffer)) {
e38c79e0
LPC
440 ret = -EBUSY;
441 } else {
8d92db28 442 buffer->access->set_length(buffer, val);
e38c79e0 443 ret = 0;
7026ea4b 444 }
37d34556
JC
445 if (ret)
446 goto out;
447 if (buffer->length && buffer->length < buffer->watermark)
448 buffer->watermark = buffer->length;
449out:
e38c79e0 450 mutex_unlock(&indio_dev->mlock);
7026ea4b 451
e38c79e0 452 return ret ? ret : len;
7026ea4b 453}
7026ea4b 454
08e7e0ad
LPC
455static ssize_t iio_buffer_show_enable(struct device *dev,
456 struct device_attribute *attr,
457 char *buf)
7026ea4b 458{
e53f5ac5 459 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
705ee2c9 460 return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
7026ea4b 461}
7026ea4b 462
183f4173
PM
463static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
464 const unsigned long *mask, bool timestamp)
959d2952 465{
959d2952
JC
466 const struct iio_chan_spec *ch;
467 unsigned bytes = 0;
468 int length, i;
959d2952
JC
469
470 /* How much space will the demuxed element take? */
6b3b58ed 471 for_each_set_bit(i, mask,
959d2952
JC
472 indio_dev->masklength) {
473 ch = iio_find_channel_from_si(indio_dev, i);
0ee8546a
SP
474 if (ch->scan_type.repeat > 1)
475 length = ch->scan_type.storagebits / 8 *
476 ch->scan_type.repeat;
477 else
478 length = ch->scan_type.storagebits / 8;
959d2952
JC
479 bytes = ALIGN(bytes, length);
480 bytes += length;
481 }
6b3b58ed 482 if (timestamp) {
959d2952 483 ch = iio_find_channel_from_si(indio_dev,
f1264809 484 indio_dev->scan_index_timestamp);
0ee8546a
SP
485 if (ch->scan_type.repeat > 1)
486 length = ch->scan_type.storagebits / 8 *
487 ch->scan_type.repeat;
488 else
489 length = ch->scan_type.storagebits / 8;
959d2952
JC
490 bytes = ALIGN(bytes, length);
491 bytes += length;
492 }
6b3b58ed
JC
493 return bytes;
494}
495
9e69c935
LPC
496static void iio_buffer_activate(struct iio_dev *indio_dev,
497 struct iio_buffer *buffer)
498{
499 iio_buffer_get(buffer);
500 list_add(&buffer->buffer_list, &indio_dev->buffer_list);
501}
502
503static void iio_buffer_deactivate(struct iio_buffer *buffer)
504{
505 list_del_init(&buffer->buffer_list);
37d34556 506 wake_up_interruptible(&buffer->pollq);
9e69c935
LPC
507 iio_buffer_put(buffer);
508}
509
a87c82e4
LPC
510void iio_disable_all_buffers(struct iio_dev *indio_dev)
511{
512 struct iio_buffer *buffer, *_buffer;
513
514 if (list_empty(&indio_dev->buffer_list))
515 return;
516
517 if (indio_dev->setup_ops->predisable)
518 indio_dev->setup_ops->predisable(indio_dev);
519
520 list_for_each_entry_safe(buffer, _buffer,
521 &indio_dev->buffer_list, buffer_list)
9e69c935 522 iio_buffer_deactivate(buffer);
a87c82e4
LPC
523
524 indio_dev->currentmode = INDIO_DIRECT_MODE;
525 if (indio_dev->setup_ops->postdisable)
526 indio_dev->setup_ops->postdisable(indio_dev);
e086ed76
LPC
527
528 if (indio_dev->available_scan_masks == NULL)
529 kfree(indio_dev->active_scan_mask);
a87c82e4
LPC
530}
531
8e050996
LPC
532static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
533 struct iio_buffer *buffer)
534{
535 unsigned int bytes;
536
537 if (!buffer->access->set_bytes_per_datum)
538 return;
539
540 bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
541 buffer->scan_timestamp);
542
543 buffer->access->set_bytes_per_datum(buffer, bytes);
544}
545
a9519456 546static int __iio_update_buffers(struct iio_dev *indio_dev,
84b36ce5
JC
547 struct iio_buffer *insert_buffer,
548 struct iio_buffer *remove_buffer)
6b3b58ed 549{
84b36ce5
JC
550 int ret;
551 int success = 0;
552 struct iio_buffer *buffer;
553 unsigned long *compound_mask;
554 const unsigned long *old_mask;
6b3b58ed 555
84b36ce5
JC
556 /* Wind down existing buffers - iff there are any */
557 if (!list_empty(&indio_dev->buffer_list)) {
558 if (indio_dev->setup_ops->predisable) {
559 ret = indio_dev->setup_ops->predisable(indio_dev);
560 if (ret)
92825ff9 561 return ret;
84b36ce5
JC
562 }
563 indio_dev->currentmode = INDIO_DIRECT_MODE;
564 if (indio_dev->setup_ops->postdisable) {
565 ret = indio_dev->setup_ops->postdisable(indio_dev);
566 if (ret)
92825ff9 567 return ret;
84b36ce5
JC
568 }
569 }
570 /* Keep a copy of current setup to allow roll back */
571 old_mask = indio_dev->active_scan_mask;
572 if (!indio_dev->available_scan_masks)
573 indio_dev->active_scan_mask = NULL;
574
575 if (remove_buffer)
9e69c935 576 iio_buffer_deactivate(remove_buffer);
84b36ce5 577 if (insert_buffer)
9e69c935 578 iio_buffer_activate(indio_dev, insert_buffer);
84b36ce5
JC
579
580 /* If no buffers in list, we are done */
581 if (list_empty(&indio_dev->buffer_list)) {
582 indio_dev->currentmode = INDIO_DIRECT_MODE;
583 if (indio_dev->available_scan_masks == NULL)
584 kfree(old_mask);
585 return 0;
586 }
959d2952 587
9572588c 588 /* What scan mask do we actually have? */
84b36ce5
JC
589 compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
590 sizeof(long), GFP_KERNEL);
591 if (compound_mask == NULL) {
592 if (indio_dev->available_scan_masks == NULL)
593 kfree(old_mask);
594 return -ENOMEM;
595 }
596 indio_dev->scan_timestamp = 0;
597
598 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
599 bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
600 indio_dev->masklength);
601 indio_dev->scan_timestamp |= buffer->scan_timestamp;
602 }
603 if (indio_dev->available_scan_masks) {
959d2952
JC
604 indio_dev->active_scan_mask =
605 iio_scan_mask_match(indio_dev->available_scan_masks,
606 indio_dev->masklength,
84b36ce5
JC
607 compound_mask);
608 if (indio_dev->active_scan_mask == NULL) {
609 /*
610 * Roll back.
611 * Note can only occur when adding a buffer.
612 */
9e69c935 613 iio_buffer_deactivate(insert_buffer);
d66e0452
PM
614 if (old_mask) {
615 indio_dev->active_scan_mask = old_mask;
616 success = -EINVAL;
617 }
618 else {
619 kfree(compound_mask);
620 ret = -EINVAL;
92825ff9 621 return ret;
d66e0452 622 }
84b36ce5
JC
623 }
624 } else {
625 indio_dev->active_scan_mask = compound_mask;
626 }
aff1eb4e 627
5ada4ea9
JC
628 iio_update_demux(indio_dev);
629
84b36ce5
JC
630 /* Wind up again */
631 if (indio_dev->setup_ops->preenable) {
632 ret = indio_dev->setup_ops->preenable(indio_dev);
633 if (ret) {
634 printk(KERN_ERR
bec1889d 635 "Buffer not started: buffer preenable failed (%d)\n", ret);
84b36ce5
JC
636 goto error_remove_inserted;
637 }
638 }
639 indio_dev->scan_bytes =
640 iio_compute_scan_bytes(indio_dev,
641 indio_dev->active_scan_mask,
642 indio_dev->scan_timestamp);
8e050996
LPC
643 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
644 iio_buffer_update_bytes_per_datum(indio_dev, buffer);
84b36ce5
JC
645 if (buffer->access->request_update) {
646 ret = buffer->access->request_update(buffer);
647 if (ret) {
648 printk(KERN_INFO
bec1889d 649 "Buffer not started: buffer parameter update failed (%d)\n", ret);
84b36ce5
JC
650 goto error_run_postdisable;
651 }
652 }
8e050996 653 }
84b36ce5
JC
654 if (indio_dev->info->update_scan_mode) {
655 ret = indio_dev->info
5ada4ea9
JC
656 ->update_scan_mode(indio_dev,
657 indio_dev->active_scan_mask);
84b36ce5 658 if (ret < 0) {
bec1889d 659 printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret);
84b36ce5
JC
660 goto error_run_postdisable;
661 }
662 }
9572588c 663 /* Definitely possible for devices to support both of these. */
84b36ce5
JC
664 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) {
665 if (!indio_dev->trig) {
666 printk(KERN_INFO "Buffer not started: no trigger\n");
667 ret = -EINVAL;
668 /* Can only occur on first buffer */
669 goto error_run_postdisable;
670 }
671 indio_dev->currentmode = INDIO_BUFFER_TRIGGERED;
672 } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) {
673 indio_dev->currentmode = INDIO_BUFFER_HARDWARE;
03af03ad
KW
674 } else if (indio_dev->modes & INDIO_BUFFER_SOFTWARE) {
675 indio_dev->currentmode = INDIO_BUFFER_SOFTWARE;
9572588c 676 } else { /* Should never be reached */
84b36ce5
JC
677 ret = -EINVAL;
678 goto error_run_postdisable;
679 }
680
681 if (indio_dev->setup_ops->postenable) {
682 ret = indio_dev->setup_ops->postenable(indio_dev);
683 if (ret) {
684 printk(KERN_INFO
bec1889d 685 "Buffer not started: postenable failed (%d)\n", ret);
84b36ce5
JC
686 indio_dev->currentmode = INDIO_DIRECT_MODE;
687 if (indio_dev->setup_ops->postdisable)
688 indio_dev->setup_ops->postdisable(indio_dev);
689 goto error_disable_all_buffers;
690 }
691 }
692
693 if (indio_dev->available_scan_masks)
694 kfree(compound_mask);
695 else
696 kfree(old_mask);
697
698 return success;
699
700error_disable_all_buffers:
701 indio_dev->currentmode = INDIO_DIRECT_MODE;
702error_run_postdisable:
703 if (indio_dev->setup_ops->postdisable)
704 indio_dev->setup_ops->postdisable(indio_dev);
705error_remove_inserted:
84b36ce5 706 if (insert_buffer)
9e69c935 707 iio_buffer_deactivate(insert_buffer);
84b36ce5
JC
708 indio_dev->active_scan_mask = old_mask;
709 kfree(compound_mask);
84b36ce5
JC
710 return ret;
711}
a9519456
LPC
712
713int iio_update_buffers(struct iio_dev *indio_dev,
714 struct iio_buffer *insert_buffer,
715 struct iio_buffer *remove_buffer)
716{
717 int ret;
718
3909fab5
LPC
719 if (insert_buffer == remove_buffer)
720 return 0;
721
a9519456
LPC
722 mutex_lock(&indio_dev->info_exist_lock);
723 mutex_lock(&indio_dev->mlock);
724
3909fab5
LPC
725 if (insert_buffer && iio_buffer_is_active(insert_buffer))
726 insert_buffer = NULL;
727
728 if (remove_buffer && !iio_buffer_is_active(remove_buffer))
729 remove_buffer = NULL;
730
731 if (!insert_buffer && !remove_buffer) {
732 ret = 0;
733 goto out_unlock;
734 }
735
a9519456
LPC
736 if (indio_dev->info == NULL) {
737 ret = -ENODEV;
738 goto out_unlock;
739 }
740
741 ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
742
743out_unlock:
744 mutex_unlock(&indio_dev->mlock);
745 mutex_unlock(&indio_dev->info_exist_lock);
746
747 return ret;
748}
84b36ce5
JC
749EXPORT_SYMBOL_GPL(iio_update_buffers);
750
08e7e0ad
LPC
751static ssize_t iio_buffer_store_enable(struct device *dev,
752 struct device_attribute *attr,
753 const char *buf,
754 size_t len)
84b36ce5
JC
755{
756 int ret;
757 bool requested_state;
758 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
84b36ce5
JC
759 bool inlist;
760
761 ret = strtobool(buf, &requested_state);
762 if (ret < 0)
763 return ret;
764
765 mutex_lock(&indio_dev->mlock);
766
767 /* Find out if it is in the list */
705ee2c9 768 inlist = iio_buffer_is_active(indio_dev->buffer);
84b36ce5
JC
769 /* Already in desired state */
770 if (inlist == requested_state)
771 goto done;
772
773 if (requested_state)
a9519456 774 ret = __iio_update_buffers(indio_dev,
84b36ce5
JC
775 indio_dev->buffer, NULL);
776 else
a9519456 777 ret = __iio_update_buffers(indio_dev,
84b36ce5
JC
778 NULL, indio_dev->buffer);
779
780 if (ret < 0)
781 goto done;
782done:
783 mutex_unlock(&indio_dev->mlock);
784 return (ret < 0) ? ret : len;
785}
84b36ce5 786
d967cb6b
LPC
787static const char * const iio_scan_elements_group_name = "scan_elements";
788
37d34556
JC
789static ssize_t iio_buffer_show_watermark(struct device *dev,
790 struct device_attribute *attr,
791 char *buf)
792{
793 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
794 struct iio_buffer *buffer = indio_dev->buffer;
795
796 return sprintf(buf, "%u\n", buffer->watermark);
797}
798
799static ssize_t iio_buffer_store_watermark(struct device *dev,
800 struct device_attribute *attr,
801 const char *buf,
802 size_t len)
803{
804 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
805 struct iio_buffer *buffer = indio_dev->buffer;
806 unsigned int val;
807 int ret;
808
809 ret = kstrtouint(buf, 10, &val);
810 if (ret)
811 return ret;
812 if (!val)
813 return -EINVAL;
814
815 mutex_lock(&indio_dev->mlock);
816
817 if (val > buffer->length) {
818 ret = -EINVAL;
819 goto out;
820 }
821
822 if (iio_buffer_is_active(indio_dev->buffer)) {
823 ret = -EBUSY;
824 goto out;
825 }
826
827 buffer->watermark = val;
828out:
829 mutex_unlock(&indio_dev->mlock);
830
831 return ret ? ret : len;
832}
833
08e7e0ad
LPC
834static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
835 iio_buffer_write_length);
8d92db28
LPC
836static struct device_attribute dev_attr_length_ro = __ATTR(length,
837 S_IRUGO, iio_buffer_read_length, NULL);
08e7e0ad
LPC
838static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
839 iio_buffer_show_enable, iio_buffer_store_enable);
37d34556
JC
840static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR,
841 iio_buffer_show_watermark, iio_buffer_store_watermark);
08e7e0ad 842
6da9b382
OP
843static struct attribute *iio_buffer_attrs[] = {
844 &dev_attr_length.attr,
845 &dev_attr_enable.attr,
37d34556 846 &dev_attr_watermark.attr,
6da9b382
OP
847};
848
d967cb6b
LPC
849int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
850{
851 struct iio_dev_attr *p;
852 struct attribute **attr;
853 struct iio_buffer *buffer = indio_dev->buffer;
854 int ret, i, attrn, attrcount, attrcount_orig = 0;
855 const struct iio_chan_spec *channels;
856
857 if (!buffer)
858 return 0;
859
08e7e0ad
LPC
860 attrcount = 0;
861 if (buffer->attrs) {
862 while (buffer->attrs[attrcount] != NULL)
863 attrcount++;
864 }
865
6da9b382
OP
866 attr = kcalloc(attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1,
867 sizeof(struct attribute *), GFP_KERNEL);
868 if (!attr)
08e7e0ad
LPC
869 return -ENOMEM;
870
6da9b382
OP
871 memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
872 if (!buffer->access->set_length)
873 attr[0] = &dev_attr_length_ro.attr;
874
08e7e0ad 875 if (buffer->attrs)
6da9b382
OP
876 memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs,
877 sizeof(struct attribute *) * attrcount);
878
879 attr[attrcount + ARRAY_SIZE(iio_buffer_attrs)] = NULL;
880
881 buffer->buffer_group.name = "buffer";
882 buffer->buffer_group.attrs = attr;
08e7e0ad
LPC
883
884 indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group;
885
d967cb6b
LPC
886 if (buffer->scan_el_attrs != NULL) {
887 attr = buffer->scan_el_attrs->attrs;
888 while (*attr++ != NULL)
889 attrcount_orig++;
890 }
891 attrcount = attrcount_orig;
892 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
893 channels = indio_dev->channels;
894 if (channels) {
895 /* new magic */
896 for (i = 0; i < indio_dev->num_channels; i++) {
897 if (channels[i].scan_index < 0)
898 continue;
899
900 /* Establish necessary mask length */
901 if (channels[i].scan_index >
902 (int)indio_dev->masklength - 1)
903 indio_dev->masklength
904 = channels[i].scan_index + 1;
905
906 ret = iio_buffer_add_channel_sysfs(indio_dev,
907 &channels[i]);
908 if (ret < 0)
909 goto error_cleanup_dynamic;
910 attrcount += ret;
911 if (channels[i].type == IIO_TIMESTAMP)
912 indio_dev->scan_index_timestamp =
913 channels[i].scan_index;
914 }
915 if (indio_dev->masklength && buffer->scan_mask == NULL) {
916 buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
917 sizeof(*buffer->scan_mask),
918 GFP_KERNEL);
919 if (buffer->scan_mask == NULL) {
920 ret = -ENOMEM;
921 goto error_cleanup_dynamic;
922 }
923 }
924 }
925
926 buffer->scan_el_group.name = iio_scan_elements_group_name;
927
928 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
929 sizeof(buffer->scan_el_group.attrs[0]),
930 GFP_KERNEL);
931 if (buffer->scan_el_group.attrs == NULL) {
932 ret = -ENOMEM;
933 goto error_free_scan_mask;
934 }
935 if (buffer->scan_el_attrs)
936 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
937 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
938 attrn = attrcount_orig;
939
940 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
941 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
942 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
943
944 return 0;
945
946error_free_scan_mask:
947 kfree(buffer->scan_mask);
948error_cleanup_dynamic:
949 iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
08e7e0ad 950 kfree(indio_dev->buffer->buffer_group.attrs);
d967cb6b
LPC
951
952 return ret;
953}
954
955void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
956{
957 if (!indio_dev->buffer)
958 return;
959
960 kfree(indio_dev->buffer->scan_mask);
08e7e0ad 961 kfree(indio_dev->buffer->buffer_group.attrs);
d967cb6b
LPC
962 kfree(indio_dev->buffer->scan_el_group.attrs);
963 iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
964}
965
81636632
LPC
966/**
967 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
968 * @indio_dev: the iio device
969 * @mask: scan mask to be checked
970 *
971 * Return true if exactly one bit is set in the scan mask, false otherwise. It
972 * can be used for devices where only one channel can be active for sampling at
973 * a time.
974 */
975bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
976 const unsigned long *mask)
977{
978 return bitmap_weight(mask, indio_dev->masklength) == 1;
979}
980EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
981
f79a9098
JC
982int iio_scan_mask_query(struct iio_dev *indio_dev,
983 struct iio_buffer *buffer, int bit)
32b5eeca 984{
f8c6f4e9 985 if (bit > indio_dev->masklength)
32b5eeca
JC
986 return -EINVAL;
987
14555b14 988 if (!buffer->scan_mask)
32b5eeca 989 return 0;
32b5eeca 990
2076a20f
AB
991 /* Ensure return value is 0 or 1. */
992 return !!test_bit(bit, buffer->scan_mask);
32b5eeca
JC
993};
994EXPORT_SYMBOL_GPL(iio_scan_mask_query);
5ada4ea9
JC
995
996/**
997 * struct iio_demux_table() - table describing demux memcpy ops
998 * @from: index to copy from
99698b45 999 * @to: index to copy to
5ada4ea9
JC
1000 * @length: how many bytes to copy
1001 * @l: list head used for management
1002 */
1003struct iio_demux_table {
1004 unsigned from;
1005 unsigned to;
1006 unsigned length;
1007 struct list_head l;
1008};
1009
5d65d920
LPC
1010static const void *iio_demux(struct iio_buffer *buffer,
1011 const void *datain)
5ada4ea9
JC
1012{
1013 struct iio_demux_table *t;
1014
1015 if (list_empty(&buffer->demux_list))
1016 return datain;
1017 list_for_each_entry(t, &buffer->demux_list, l)
1018 memcpy(buffer->demux_bounce + t->to,
1019 datain + t->from, t->length);
1020
1021 return buffer->demux_bounce;
1022}
1023
5d65d920 1024static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
5ada4ea9 1025{
5d65d920 1026 const void *dataout = iio_demux(buffer, data);
37d34556
JC
1027 int ret;
1028
1029 ret = buffer->access->store_to(buffer, dataout);
1030 if (ret)
1031 return ret;
5ada4ea9 1032
37d34556
JC
1033 /*
1034 * We can't just test for watermark to decide if we wake the poll queue
1035 * because read may request less samples than the watermark.
1036 */
1037 wake_up_interruptible_poll(&buffer->pollq, POLLIN | POLLRDNORM);
1038 return 0;
5ada4ea9 1039}
5ada4ea9 1040
842cd100
JC
1041static void iio_buffer_demux_free(struct iio_buffer *buffer)
1042{
1043 struct iio_demux_table *p, *q;
1044 list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
1045 list_del(&p->l);
1046 kfree(p);
1047 }
1048}
1049
84b36ce5 1050
5d65d920 1051int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
84b36ce5
JC
1052{
1053 int ret;
1054 struct iio_buffer *buf;
1055
1056 list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
1057 ret = iio_push_to_buffer(buf, data);
1058 if (ret < 0)
1059 return ret;
1060 }
1061
1062 return 0;
1063}
1064EXPORT_SYMBOL_GPL(iio_push_to_buffers);
1065
cbe88bcc
LPC
1066static int iio_buffer_add_demux(struct iio_buffer *buffer,
1067 struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc,
1068 unsigned int length)
1069{
1070
1071 if (*p && (*p)->from + (*p)->length == in_loc &&
1072 (*p)->to + (*p)->length == out_loc) {
1073 (*p)->length += length;
1074 } else {
7cdca178 1075 *p = kmalloc(sizeof(**p), GFP_KERNEL);
cbe88bcc
LPC
1076 if (*p == NULL)
1077 return -ENOMEM;
1078 (*p)->from = in_loc;
1079 (*p)->to = out_loc;
1080 (*p)->length = length;
1081 list_add_tail(&(*p)->l, &buffer->demux_list);
1082 }
1083
1084 return 0;
1085}
1086
84b36ce5
JC
1087static int iio_buffer_update_demux(struct iio_dev *indio_dev,
1088 struct iio_buffer *buffer)
5ada4ea9
JC
1089{
1090 const struct iio_chan_spec *ch;
5ada4ea9
JC
1091 int ret, in_ind = -1, out_ind, length;
1092 unsigned in_loc = 0, out_loc = 0;
cbe88bcc 1093 struct iio_demux_table *p = NULL;
5ada4ea9
JC
1094
1095 /* Clear out any old demux */
842cd100 1096 iio_buffer_demux_free(buffer);
5ada4ea9
JC
1097 kfree(buffer->demux_bounce);
1098 buffer->demux_bounce = NULL;
1099
1100 /* First work out which scan mode we will actually have */
1101 if (bitmap_equal(indio_dev->active_scan_mask,
1102 buffer->scan_mask,
1103 indio_dev->masklength))
1104 return 0;
1105
1106 /* Now we have the two masks, work from least sig and build up sizes */
1107 for_each_set_bit(out_ind,
61bd55ce 1108 buffer->scan_mask,
5ada4ea9
JC
1109 indio_dev->masklength) {
1110 in_ind = find_next_bit(indio_dev->active_scan_mask,
1111 indio_dev->masklength,
1112 in_ind + 1);
1113 while (in_ind != out_ind) {
1114 in_ind = find_next_bit(indio_dev->active_scan_mask,
1115 indio_dev->masklength,
1116 in_ind + 1);
1117 ch = iio_find_channel_from_si(indio_dev, in_ind);
0ee8546a
SP
1118 if (ch->scan_type.repeat > 1)
1119 length = ch->scan_type.storagebits / 8 *
1120 ch->scan_type.repeat;
1121 else
1122 length = ch->scan_type.storagebits / 8;
5ada4ea9 1123 /* Make sure we are aligned */
61072dbc 1124 in_loc = roundup(in_loc, length) + length;
5ada4ea9
JC
1125 }
1126 ch = iio_find_channel_from_si(indio_dev, in_ind);
0ee8546a
SP
1127 if (ch->scan_type.repeat > 1)
1128 length = ch->scan_type.storagebits / 8 *
1129 ch->scan_type.repeat;
1130 else
1131 length = ch->scan_type.storagebits / 8;
61072dbc
LPC
1132 out_loc = roundup(out_loc, length);
1133 in_loc = roundup(in_loc, length);
cbe88bcc
LPC
1134 ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
1135 if (ret)
1136 goto error_clear_mux_table;
5ada4ea9
JC
1137 out_loc += length;
1138 in_loc += length;
1139 }
1140 /* Relies on scan_timestamp being last */
1141 if (buffer->scan_timestamp) {
5ada4ea9 1142 ch = iio_find_channel_from_si(indio_dev,
f1264809 1143 indio_dev->scan_index_timestamp);
0ee8546a
SP
1144 if (ch->scan_type.repeat > 1)
1145 length = ch->scan_type.storagebits / 8 *
1146 ch->scan_type.repeat;
1147 else
1148 length = ch->scan_type.storagebits / 8;
61072dbc
LPC
1149 out_loc = roundup(out_loc, length);
1150 in_loc = roundup(in_loc, length);
cbe88bcc
LPC
1151 ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
1152 if (ret)
1153 goto error_clear_mux_table;
5ada4ea9
JC
1154 out_loc += length;
1155 in_loc += length;
1156 }
1157 buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
1158 if (buffer->demux_bounce == NULL) {
1159 ret = -ENOMEM;
1160 goto error_clear_mux_table;
1161 }
1162 return 0;
1163
1164error_clear_mux_table:
842cd100
JC
1165 iio_buffer_demux_free(buffer);
1166
5ada4ea9
JC
1167 return ret;
1168}
84b36ce5
JC
1169
1170int iio_update_demux(struct iio_dev *indio_dev)
1171{
1172 struct iio_buffer *buffer;
1173 int ret;
1174
1175 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
1176 ret = iio_buffer_update_demux(indio_dev, buffer);
1177 if (ret < 0)
1178 goto error_clear_mux_table;
1179 }
1180 return 0;
1181
1182error_clear_mux_table:
1183 list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
1184 iio_buffer_demux_free(buffer);
1185
1186 return ret;
1187}
5ada4ea9 1188EXPORT_SYMBOL_GPL(iio_update_demux);
9e69c935
LPC
1189
1190/**
1191 * iio_buffer_release() - Free a buffer's resources
1192 * @ref: Pointer to the kref embedded in the iio_buffer struct
1193 *
1194 * This function is called when the last reference to the buffer has been
1195 * dropped. It will typically free all resources allocated by the buffer. Do not
1196 * call this function manually, always use iio_buffer_put() when done using a
1197 * buffer.
1198 */
1199static void iio_buffer_release(struct kref *ref)
1200{
1201 struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
1202
1203 buffer->access->release(buffer);
1204}
1205
1206/**
1207 * iio_buffer_get() - Grab a reference to the buffer
1208 * @buffer: The buffer to grab a reference for, may be NULL
1209 *
1210 * Returns the pointer to the buffer that was passed into the function.
1211 */
1212struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
1213{
1214 if (buffer)
1215 kref_get(&buffer->ref);
1216
1217 return buffer;
1218}
1219EXPORT_SYMBOL_GPL(iio_buffer_get);
1220
1221/**
1222 * iio_buffer_put() - Release the reference to the buffer
1223 * @buffer: The buffer to release the reference for, may be NULL
1224 */
1225void iio_buffer_put(struct iio_buffer *buffer)
1226{
1227 if (buffer)
1228 kref_put(&buffer->ref, iio_buffer_release);
1229}
1230EXPORT_SYMBOL_GPL(iio_buffer_put);
This page took 0.587026 seconds and 5 git commands to generate.