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