linux/drivers/iio/industrialio-buffer.c
<<
>>
Prefs
   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 *
   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>
  17#include <linux/export.h>
  18#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#include <linux/sched.h>
  24
  25#include <linux/iio/iio.h>
  26#include "iio_core.h"
  27#include <linux/iio/sysfs.h>
  28#include <linux/iio/buffer.h>
  29
  30static const char * const iio_endian_prefix[] = {
  31        [IIO_BE] = "be",
  32        [IIO_LE] = "le",
  33};
  34
  35static bool iio_buffer_is_active(struct iio_buffer *buf)
  36{
  37        return !list_empty(&buf->buffer_list);
  38}
  39
  40static size_t iio_buffer_data_available(struct iio_buffer *buf)
  41{
  42        return buf->access->data_available(buf);
  43}
  44
  45static int iio_buffer_flush_hwfifo(struct iio_dev *indio_dev,
  46                                   struct iio_buffer *buf, size_t required)
  47{
  48        if (!indio_dev->info->hwfifo_flush_to_buffer)
  49                return -ENODEV;
  50
  51        return indio_dev->info->hwfifo_flush_to_buffer(indio_dev, required);
  52}
  53
  54static bool iio_buffer_ready(struct iio_dev *indio_dev, struct iio_buffer *buf,
  55                             size_t to_wait, int to_flush)
  56{
  57        size_t avail;
  58        int flushed = 0;
  59
  60        /* wakeup if the device was unregistered */
  61        if (!indio_dev->info)
  62                return true;
  63
  64        /* drain the buffer if it was disabled */
  65        if (!iio_buffer_is_active(buf)) {
  66                to_wait = min_t(size_t, to_wait, 1);
  67                to_flush = 0;
  68        }
  69
  70        avail = iio_buffer_data_available(buf);
  71
  72        if (avail >= to_wait) {
  73                /* force a flush for non-blocking reads */
  74                if (!to_wait && avail < to_flush)
  75                        iio_buffer_flush_hwfifo(indio_dev, buf,
  76                                                to_flush - avail);
  77                return true;
  78        }
  79
  80        if (to_flush)
  81                flushed = iio_buffer_flush_hwfifo(indio_dev, buf,
  82                                                  to_wait - avail);
  83        if (flushed <= 0)
  84                return false;
  85
  86        if (avail + flushed >= to_wait)
  87                return true;
  88
  89        return false;
  90}
  91
  92/**
  93 * iio_buffer_read_first_n_outer() - chrdev read for buffer access
  94 * @filp:       File structure pointer for the char device
  95 * @buf:        Destination buffer for iio buffer read
  96 * @n:          First n bytes to read
  97 * @f_ps:       Long offset provided by the user as a seek position
  98 *
  99 * This function relies on all buffer implementations having an
 100 * iio_buffer as their first element.
 101 *
 102 * Return: negative values corresponding to error codes or ret != 0
 103 *         for ending the reading activity
 104 **/
 105ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
 106                                      size_t n, loff_t *f_ps)
 107{
 108        struct iio_dev *indio_dev = filp->private_data;
 109        struct iio_buffer *rb = indio_dev->buffer;
 110        DEFINE_WAIT_FUNC(wait, woken_wake_function);
 111        size_t datum_size;
 112        size_t to_wait;
 113        int ret = 0;
 114
 115        if (!indio_dev->info)
 116                return -ENODEV;
 117
 118        if (!rb || !rb->access->read_first_n)
 119                return -EINVAL;
 120
 121        datum_size = rb->bytes_per_datum;
 122
 123        /*
 124         * If datum_size is 0 there will never be anything to read from the
 125         * buffer, so signal end of file now.
 126         */
 127        if (!datum_size)
 128                return 0;
 129
 130        if (filp->f_flags & O_NONBLOCK)
 131                to_wait = 0;
 132        else
 133                to_wait = min_t(size_t, n / datum_size, rb->watermark);
 134
 135        add_wait_queue(&rb->pollq, &wait);
 136        do {
 137                if (!indio_dev->info) {
 138                        ret = -ENODEV;
 139                        break;
 140                }
 141
 142                if (!iio_buffer_ready(indio_dev, rb, to_wait, n / datum_size)) {
 143                        if (signal_pending(current)) {
 144                                ret = -ERESTARTSYS;
 145                                break;
 146                        }
 147
 148                        wait_woken(&wait, TASK_INTERRUPTIBLE,
 149                                   MAX_SCHEDULE_TIMEOUT);
 150                        continue;
 151                }
 152
 153                ret = rb->access->read_first_n(rb, n, buf);
 154                if (ret == 0 && (filp->f_flags & O_NONBLOCK))
 155                        ret = -EAGAIN;
 156        } while (ret == 0);
 157        remove_wait_queue(&rb->pollq, &wait);
 158
 159        return ret;
 160}
 161
 162/**
 163 * iio_buffer_poll() - poll the buffer to find out if it has data
 164 * @filp:       File structure pointer for device access
 165 * @wait:       Poll table structure pointer for which the driver adds
 166 *              a wait queue
 167 *
 168 * Return: (POLLIN | POLLRDNORM) if data is available for reading
 169 *         or 0 for other cases
 170 */
 171unsigned int iio_buffer_poll(struct file *filp,
 172                             struct poll_table_struct *wait)
 173{
 174        struct iio_dev *indio_dev = filp->private_data;
 175        struct iio_buffer *rb = indio_dev->buffer;
 176
 177        if (!indio_dev->info)
 178                return 0;
 179
 180        poll_wait(filp, &rb->pollq, wait);
 181        if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0))
 182                return POLLIN | POLLRDNORM;
 183        return 0;
 184}
 185
 186/**
 187 * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue
 188 * @indio_dev: The IIO device
 189 *
 190 * Wakes up the event waitqueue used for poll(). Should usually
 191 * be called when the device is unregistered.
 192 */
 193void iio_buffer_wakeup_poll(struct iio_dev *indio_dev)
 194{
 195        if (!indio_dev->buffer)
 196                return;
 197
 198        wake_up(&indio_dev->buffer->pollq);
 199}
 200
 201void iio_buffer_init(struct iio_buffer *buffer)
 202{
 203        INIT_LIST_HEAD(&buffer->demux_list);
 204        INIT_LIST_HEAD(&buffer->buffer_list);
 205        init_waitqueue_head(&buffer->pollq);
 206        kref_init(&buffer->ref);
 207        if (!buffer->watermark)
 208                buffer->watermark = 1;
 209}
 210EXPORT_SYMBOL(iio_buffer_init);
 211
 212/**
 213 * iio_buffer_set_attrs - Set buffer specific attributes
 214 * @buffer: The buffer for which we are setting attributes
 215 * @attrs: Pointer to a null terminated list of pointers to attributes
 216 */
 217void iio_buffer_set_attrs(struct iio_buffer *buffer,
 218                         const struct attribute **attrs)
 219{
 220        buffer->attrs = attrs;
 221}
 222EXPORT_SYMBOL_GPL(iio_buffer_set_attrs);
 223
 224static ssize_t iio_show_scan_index(struct device *dev,
 225                                   struct device_attribute *attr,
 226                                   char *buf)
 227{
 228        return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index);
 229}
 230
 231static ssize_t iio_show_fixed_type(struct device *dev,
 232                                   struct device_attribute *attr,
 233                                   char *buf)
 234{
 235        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 236        u8 type = this_attr->c->scan_type.endianness;
 237
 238        if (type == IIO_CPU) {
 239#ifdef __LITTLE_ENDIAN
 240                type = IIO_LE;
 241#else
 242                type = IIO_BE;
 243#endif
 244        }
 245        if (this_attr->c->scan_type.repeat > 1)
 246                return sprintf(buf, "%s:%c%d/%dX%d>>%u\n",
 247                       iio_endian_prefix[type],
 248                       this_attr->c->scan_type.sign,
 249                       this_attr->c->scan_type.realbits,
 250                       this_attr->c->scan_type.storagebits,
 251                       this_attr->c->scan_type.repeat,
 252                       this_attr->c->scan_type.shift);
 253                else
 254                        return sprintf(buf, "%s:%c%d/%d>>%u\n",
 255                       iio_endian_prefix[type],
 256                       this_attr->c->scan_type.sign,
 257                       this_attr->c->scan_type.realbits,
 258                       this_attr->c->scan_type.storagebits,
 259                       this_attr->c->scan_type.shift);
 260}
 261
 262static ssize_t iio_scan_el_show(struct device *dev,
 263                                struct device_attribute *attr,
 264                                char *buf)
 265{
 266        int ret;
 267        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 268
 269        /* Ensure ret is 0 or 1. */
 270        ret = !!test_bit(to_iio_dev_attr(attr)->address,
 271                       indio_dev->buffer->scan_mask);
 272
 273        return sprintf(buf, "%d\n", ret);
 274}
 275
 276/* Note NULL used as error indicator as it doesn't make sense. */
 277static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
 278                                          unsigned int masklength,
 279                                          const unsigned long *mask,
 280                                          bool strict)
 281{
 282        if (bitmap_empty(mask, masklength))
 283                return NULL;
 284        while (*av_masks) {
 285                if (strict) {
 286                        if (bitmap_equal(mask, av_masks, masklength))
 287                                return av_masks;
 288                } else {
 289                        if (bitmap_subset(mask, av_masks, masklength))
 290                                return av_masks;
 291                }
 292                av_masks += BITS_TO_LONGS(masklength);
 293        }
 294        return NULL;
 295}
 296
 297static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
 298        const unsigned long *mask)
 299{
 300        if (!indio_dev->setup_ops->validate_scan_mask)
 301                return true;
 302
 303        return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
 304}
 305
 306/**
 307 * iio_scan_mask_set() - set particular bit in the scan mask
 308 * @indio_dev: the iio device
 309 * @buffer: the buffer whose scan mask we are interested in
 310 * @bit: the bit to be set.
 311 *
 312 * Note that at this point we have no way of knowing what other
 313 * buffers might request, hence this code only verifies that the
 314 * individual buffers request is plausible.
 315 */
 316static int iio_scan_mask_set(struct iio_dev *indio_dev,
 317                      struct iio_buffer *buffer, int bit)
 318{
 319        const unsigned long *mask;
 320        unsigned long *trialmask;
 321
 322        trialmask = kmalloc_array(BITS_TO_LONGS(indio_dev->masklength),
 323                                  sizeof(*trialmask),
 324                                  GFP_KERNEL);
 325        if (trialmask == NULL)
 326                return -ENOMEM;
 327        if (!indio_dev->masklength) {
 328                WARN(1, "Trying to set scanmask prior to registering buffer\n");
 329                goto err_invalid_mask;
 330        }
 331        bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
 332        set_bit(bit, trialmask);
 333
 334        if (!iio_validate_scan_mask(indio_dev, trialmask))
 335                goto err_invalid_mask;
 336
 337        if (indio_dev->available_scan_masks) {
 338                mask = iio_scan_mask_match(indio_dev->available_scan_masks,
 339                                           indio_dev->masklength,
 340                                           trialmask, false);
 341                if (!mask)
 342                        goto err_invalid_mask;
 343        }
 344        bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
 345
 346        kfree(trialmask);
 347
 348        return 0;
 349
 350err_invalid_mask:
 351        kfree(trialmask);
 352        return -EINVAL;
 353}
 354
 355static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
 356{
 357        clear_bit(bit, buffer->scan_mask);
 358        return 0;
 359}
 360
 361static ssize_t iio_scan_el_store(struct device *dev,
 362                                 struct device_attribute *attr,
 363                                 const char *buf,
 364                                 size_t len)
 365{
 366        int ret;
 367        bool state;
 368        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 369        struct iio_buffer *buffer = indio_dev->buffer;
 370        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
 371
 372        ret = strtobool(buf, &state);
 373        if (ret < 0)
 374                return ret;
 375        mutex_lock(&indio_dev->mlock);
 376        if (iio_buffer_is_active(indio_dev->buffer)) {
 377                ret = -EBUSY;
 378                goto error_ret;
 379        }
 380        ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address);
 381        if (ret < 0)
 382                goto error_ret;
 383        if (!state && ret) {
 384                ret = iio_scan_mask_clear(buffer, this_attr->address);
 385                if (ret)
 386                        goto error_ret;
 387        } else if (state && !ret) {
 388                ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address);
 389                if (ret)
 390                        goto error_ret;
 391        }
 392
 393error_ret:
 394        mutex_unlock(&indio_dev->mlock);
 395
 396        return ret < 0 ? ret : len;
 397
 398}
 399
 400static ssize_t iio_scan_el_ts_show(struct device *dev,
 401                                   struct device_attribute *attr,
 402                                   char *buf)
 403{
 404        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 405        return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp);
 406}
 407
 408static ssize_t iio_scan_el_ts_store(struct device *dev,
 409                                    struct device_attribute *attr,
 410                                    const char *buf,
 411                                    size_t len)
 412{
 413        int ret;
 414        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 415        bool state;
 416
 417        ret = strtobool(buf, &state);
 418        if (ret < 0)
 419                return ret;
 420
 421        mutex_lock(&indio_dev->mlock);
 422        if (iio_buffer_is_active(indio_dev->buffer)) {
 423                ret = -EBUSY;
 424                goto error_ret;
 425        }
 426        indio_dev->buffer->scan_timestamp = state;
 427error_ret:
 428        mutex_unlock(&indio_dev->mlock);
 429
 430        return ret ? ret : len;
 431}
 432
 433static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
 434                                        const struct iio_chan_spec *chan)
 435{
 436        int ret, attrcount = 0;
 437        struct iio_buffer *buffer = indio_dev->buffer;
 438
 439        ret = __iio_add_chan_devattr("index",
 440                                     chan,
 441                                     &iio_show_scan_index,
 442                                     NULL,
 443                                     0,
 444                                     IIO_SEPARATE,
 445                                     &indio_dev->dev,
 446                                     &buffer->scan_el_dev_attr_list);
 447        if (ret)
 448                return ret;
 449        attrcount++;
 450        ret = __iio_add_chan_devattr("type",
 451                                     chan,
 452                                     &iio_show_fixed_type,
 453                                     NULL,
 454                                     0,
 455                                     0,
 456                                     &indio_dev->dev,
 457                                     &buffer->scan_el_dev_attr_list);
 458        if (ret)
 459                return ret;
 460        attrcount++;
 461        if (chan->type != IIO_TIMESTAMP)
 462                ret = __iio_add_chan_devattr("en",
 463                                             chan,
 464                                             &iio_scan_el_show,
 465                                             &iio_scan_el_store,
 466                                             chan->scan_index,
 467                                             0,
 468                                             &indio_dev->dev,
 469                                             &buffer->scan_el_dev_attr_list);
 470        else
 471                ret = __iio_add_chan_devattr("en",
 472                                             chan,
 473                                             &iio_scan_el_ts_show,
 474                                             &iio_scan_el_ts_store,
 475                                             chan->scan_index,
 476                                             0,
 477                                             &indio_dev->dev,
 478                                             &buffer->scan_el_dev_attr_list);
 479        if (ret)
 480                return ret;
 481        attrcount++;
 482        ret = attrcount;
 483        return ret;
 484}
 485
 486static ssize_t iio_buffer_read_length(struct device *dev,
 487                                      struct device_attribute *attr,
 488                                      char *buf)
 489{
 490        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 491        struct iio_buffer *buffer = indio_dev->buffer;
 492
 493        return sprintf(buf, "%d\n", buffer->length);
 494}
 495
 496static ssize_t iio_buffer_write_length(struct device *dev,
 497                                       struct device_attribute *attr,
 498                                       const char *buf, size_t len)
 499{
 500        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 501        struct iio_buffer *buffer = indio_dev->buffer;
 502        unsigned int val;
 503        int ret;
 504
 505        ret = kstrtouint(buf, 10, &val);
 506        if (ret)
 507                return ret;
 508
 509        if (val == buffer->length)
 510                return len;
 511
 512        mutex_lock(&indio_dev->mlock);
 513        if (iio_buffer_is_active(indio_dev->buffer)) {
 514                ret = -EBUSY;
 515        } else {
 516                buffer->access->set_length(buffer, val);
 517                ret = 0;
 518        }
 519        if (ret)
 520                goto out;
 521        if (buffer->length && buffer->length < buffer->watermark)
 522                buffer->watermark = buffer->length;
 523out:
 524        mutex_unlock(&indio_dev->mlock);
 525
 526        return ret ? ret : len;
 527}
 528
 529static ssize_t iio_buffer_show_enable(struct device *dev,
 530                                      struct device_attribute *attr,
 531                                      char *buf)
 532{
 533        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 534        return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
 535}
 536
 537static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev,
 538                                             unsigned int scan_index)
 539{
 540        const struct iio_chan_spec *ch;
 541        unsigned int bytes;
 542
 543        ch = iio_find_channel_from_si(indio_dev, scan_index);
 544        bytes = ch->scan_type.storagebits / 8;
 545        if (ch->scan_type.repeat > 1)
 546                bytes *= ch->scan_type.repeat;
 547        return bytes;
 548}
 549
 550static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev)
 551{
 552        return iio_storage_bytes_for_si(indio_dev,
 553                                        indio_dev->scan_index_timestamp);
 554}
 555
 556static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
 557                                const unsigned long *mask, bool timestamp)
 558{
 559        unsigned bytes = 0;
 560        int length, i;
 561
 562        /* How much space will the demuxed element take? */
 563        for_each_set_bit(i, mask,
 564                         indio_dev->masklength) {
 565                length = iio_storage_bytes_for_si(indio_dev, i);
 566                bytes = ALIGN(bytes, length);
 567                bytes += length;
 568        }
 569
 570        if (timestamp) {
 571                length = iio_storage_bytes_for_timestamp(indio_dev);
 572                bytes = ALIGN(bytes, length);
 573                bytes += length;
 574        }
 575        return bytes;
 576}
 577
 578static void iio_buffer_activate(struct iio_dev *indio_dev,
 579        struct iio_buffer *buffer)
 580{
 581        iio_buffer_get(buffer);
 582        list_add(&buffer->buffer_list, &indio_dev->buffer_list);
 583}
 584
 585static void iio_buffer_deactivate(struct iio_buffer *buffer)
 586{
 587        list_del_init(&buffer->buffer_list);
 588        wake_up_interruptible(&buffer->pollq);
 589        iio_buffer_put(buffer);
 590}
 591
 592static void iio_buffer_deactivate_all(struct iio_dev *indio_dev)
 593{
 594        struct iio_buffer *buffer, *_buffer;
 595
 596        list_for_each_entry_safe(buffer, _buffer,
 597                        &indio_dev->buffer_list, buffer_list)
 598                iio_buffer_deactivate(buffer);
 599}
 600
 601static int iio_buffer_enable(struct iio_buffer *buffer,
 602        struct iio_dev *indio_dev)
 603{
 604        if (!buffer->access->enable)
 605                return 0;
 606        return buffer->access->enable(buffer, indio_dev);
 607}
 608
 609static int iio_buffer_disable(struct iio_buffer *buffer,
 610        struct iio_dev *indio_dev)
 611{
 612        if (!buffer->access->disable)
 613                return 0;
 614        return buffer->access->disable(buffer, indio_dev);
 615}
 616
 617static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev,
 618        struct iio_buffer *buffer)
 619{
 620        unsigned int bytes;
 621
 622        if (!buffer->access->set_bytes_per_datum)
 623                return;
 624
 625        bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask,
 626                buffer->scan_timestamp);
 627
 628        buffer->access->set_bytes_per_datum(buffer, bytes);
 629}
 630
 631static int iio_buffer_request_update(struct iio_dev *indio_dev,
 632        struct iio_buffer *buffer)
 633{
 634        int ret;
 635
 636        iio_buffer_update_bytes_per_datum(indio_dev, buffer);
 637        if (buffer->access->request_update) {
 638                ret = buffer->access->request_update(buffer);
 639                if (ret) {
 640                        dev_dbg(&indio_dev->dev,
 641                               "Buffer not started: buffer parameter update failed (%d)\n",
 642                                ret);
 643                        return ret;
 644                }
 645        }
 646
 647        return 0;
 648}
 649
 650static void iio_free_scan_mask(struct iio_dev *indio_dev,
 651        const unsigned long *mask)
 652{
 653        /* If the mask is dynamically allocated free it, otherwise do nothing */
 654        if (!indio_dev->available_scan_masks)
 655                kfree(mask);
 656}
 657
 658struct iio_device_config {
 659        unsigned int mode;
 660        unsigned int watermark;
 661        const unsigned long *scan_mask;
 662        unsigned int scan_bytes;
 663        bool scan_timestamp;
 664};
 665
 666static int iio_verify_update(struct iio_dev *indio_dev,
 667        struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer,
 668        struct iio_device_config *config)
 669{
 670        unsigned long *compound_mask;
 671        const unsigned long *scan_mask;
 672        bool strict_scanmask = false;
 673        struct iio_buffer *buffer;
 674        bool scan_timestamp;
 675        unsigned int modes;
 676
 677        memset(config, 0, sizeof(*config));
 678        config->watermark = ~0;
 679
 680        /*
 681         * If there is just one buffer and we are removing it there is nothing
 682         * to verify.
 683         */
 684        if (remove_buffer && !insert_buffer &&
 685                list_is_singular(&indio_dev->buffer_list))
 686                        return 0;
 687
 688        modes = indio_dev->modes;
 689
 690        list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
 691                if (buffer == remove_buffer)
 692                        continue;
 693                modes &= buffer->access->modes;
 694                config->watermark = min(config->watermark, buffer->watermark);
 695        }
 696
 697        if (insert_buffer) {
 698                modes &= insert_buffer->access->modes;
 699                config->watermark = min(config->watermark,
 700                        insert_buffer->watermark);
 701        }
 702
 703        /* Definitely possible for devices to support both of these. */
 704        if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) {
 705                config->mode = INDIO_BUFFER_TRIGGERED;
 706        } else if (modes & INDIO_BUFFER_HARDWARE) {
 707                /*
 708                 * Keep things simple for now and only allow a single buffer to
 709                 * be connected in hardware mode.
 710                 */
 711                if (insert_buffer && !list_empty(&indio_dev->buffer_list))
 712                        return -EINVAL;
 713                config->mode = INDIO_BUFFER_HARDWARE;
 714                strict_scanmask = true;
 715        } else if (modes & INDIO_BUFFER_SOFTWARE) {
 716                config->mode = INDIO_BUFFER_SOFTWARE;
 717        } else {
 718                /* Can only occur on first buffer */
 719                if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
 720                        dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n");
 721                return -EINVAL;
 722        }
 723
 724        /* What scan mask do we actually have? */
 725        compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
 726                                sizeof(long), GFP_KERNEL);
 727        if (compound_mask == NULL)
 728                return -ENOMEM;
 729
 730        scan_timestamp = false;
 731
 732        list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
 733                if (buffer == remove_buffer)
 734                        continue;
 735                bitmap_or(compound_mask, compound_mask, buffer->scan_mask,
 736                          indio_dev->masklength);
 737                scan_timestamp |= buffer->scan_timestamp;
 738        }
 739
 740        if (insert_buffer) {
 741                bitmap_or(compound_mask, compound_mask,
 742                          insert_buffer->scan_mask, indio_dev->masklength);
 743                scan_timestamp |= insert_buffer->scan_timestamp;
 744        }
 745
 746        if (indio_dev->available_scan_masks) {
 747                scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks,
 748                                    indio_dev->masklength,
 749                                    compound_mask,
 750                                    strict_scanmask);
 751                kfree(compound_mask);
 752                if (scan_mask == NULL)
 753                        return -EINVAL;
 754        } else {
 755            scan_mask = compound_mask;
 756        }
 757
 758        config->scan_bytes = iio_compute_scan_bytes(indio_dev,
 759                                    scan_mask, scan_timestamp);
 760        config->scan_mask = scan_mask;
 761        config->scan_timestamp = scan_timestamp;
 762
 763        return 0;
 764}
 765
 766static int iio_enable_buffers(struct iio_dev *indio_dev,
 767        struct iio_device_config *config)
 768{
 769        struct iio_buffer *buffer;
 770        int ret;
 771
 772        indio_dev->active_scan_mask = config->scan_mask;
 773        indio_dev->scan_timestamp = config->scan_timestamp;
 774        indio_dev->scan_bytes = config->scan_bytes;
 775
 776        iio_update_demux(indio_dev);
 777
 778        /* Wind up again */
 779        if (indio_dev->setup_ops->preenable) {
 780                ret = indio_dev->setup_ops->preenable(indio_dev);
 781                if (ret) {
 782                        dev_dbg(&indio_dev->dev,
 783                               "Buffer not started: buffer preenable failed (%d)\n", ret);
 784                        goto err_undo_config;
 785                }
 786        }
 787
 788        if (indio_dev->info->update_scan_mode) {
 789                ret = indio_dev->info
 790                        ->update_scan_mode(indio_dev,
 791                                           indio_dev->active_scan_mask);
 792                if (ret < 0) {
 793                        dev_dbg(&indio_dev->dev,
 794                                "Buffer not started: update scan mode failed (%d)\n",
 795                                ret);
 796                        goto err_run_postdisable;
 797                }
 798        }
 799
 800        if (indio_dev->info->hwfifo_set_watermark)
 801                indio_dev->info->hwfifo_set_watermark(indio_dev,
 802                        config->watermark);
 803
 804        list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
 805                ret = iio_buffer_enable(buffer, indio_dev);
 806                if (ret)
 807                        goto err_disable_buffers;
 808        }
 809
 810        indio_dev->currentmode = config->mode;
 811
 812        if (indio_dev->setup_ops->postenable) {
 813                ret = indio_dev->setup_ops->postenable(indio_dev);
 814                if (ret) {
 815                        dev_dbg(&indio_dev->dev,
 816                               "Buffer not started: postenable failed (%d)\n", ret);
 817                        goto err_disable_buffers;
 818                }
 819        }
 820
 821        return 0;
 822
 823err_disable_buffers:
 824        list_for_each_entry_continue_reverse(buffer, &indio_dev->buffer_list,
 825                                             buffer_list)
 826                iio_buffer_disable(buffer, indio_dev);
 827err_run_postdisable:
 828        indio_dev->currentmode = INDIO_DIRECT_MODE;
 829        if (indio_dev->setup_ops->postdisable)
 830                indio_dev->setup_ops->postdisable(indio_dev);
 831err_undo_config:
 832        indio_dev->active_scan_mask = NULL;
 833
 834        return ret;
 835}
 836
 837static int iio_disable_buffers(struct iio_dev *indio_dev)
 838{
 839        struct iio_buffer *buffer;
 840        int ret = 0;
 841        int ret2;
 842
 843        /* Wind down existing buffers - iff there are any */
 844        if (list_empty(&indio_dev->buffer_list))
 845                return 0;
 846
 847        /*
 848         * If things go wrong at some step in disable we still need to continue
 849         * to perform the other steps, otherwise we leave the device in a
 850         * inconsistent state. We return the error code for the first error we
 851         * encountered.
 852         */
 853
 854        if (indio_dev->setup_ops->predisable) {
 855                ret2 = indio_dev->setup_ops->predisable(indio_dev);
 856                if (ret2 && !ret)
 857                        ret = ret2;
 858        }
 859
 860        list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
 861                ret2 = iio_buffer_disable(buffer, indio_dev);
 862                if (ret2 && !ret)
 863                        ret = ret2;
 864        }
 865
 866        indio_dev->currentmode = INDIO_DIRECT_MODE;
 867
 868        if (indio_dev->setup_ops->postdisable) {
 869                ret2 = indio_dev->setup_ops->postdisable(indio_dev);
 870                if (ret2 && !ret)
 871                        ret = ret2;
 872        }
 873
 874        iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask);
 875        indio_dev->active_scan_mask = NULL;
 876
 877        return ret;
 878}
 879
 880static int __iio_update_buffers(struct iio_dev *indio_dev,
 881                       struct iio_buffer *insert_buffer,
 882                       struct iio_buffer *remove_buffer)
 883{
 884        struct iio_device_config new_config;
 885        int ret;
 886
 887        ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer,
 888                &new_config);
 889        if (ret)
 890                return ret;
 891
 892        if (insert_buffer) {
 893                ret = iio_buffer_request_update(indio_dev, insert_buffer);
 894                if (ret)
 895                        goto err_free_config;
 896        }
 897
 898        ret = iio_disable_buffers(indio_dev);
 899        if (ret)
 900                goto err_deactivate_all;
 901
 902        if (remove_buffer)
 903                iio_buffer_deactivate(remove_buffer);
 904        if (insert_buffer)
 905                iio_buffer_activate(indio_dev, insert_buffer);
 906
 907        /* If no buffers in list, we are done */
 908        if (list_empty(&indio_dev->buffer_list))
 909                return 0;
 910
 911        ret = iio_enable_buffers(indio_dev, &new_config);
 912        if (ret)
 913                goto err_deactivate_all;
 914
 915        return 0;
 916
 917err_deactivate_all:
 918        /*
 919         * We've already verified that the config is valid earlier. If things go
 920         * wrong in either enable or disable the most likely reason is an IO
 921         * error from the device. In this case there is no good recovery
 922         * strategy. Just make sure to disable everything and leave the device
 923         * in a sane state.  With a bit of luck the device might come back to
 924         * life again later and userspace can try again.
 925         */
 926        iio_buffer_deactivate_all(indio_dev);
 927
 928err_free_config:
 929        iio_free_scan_mask(indio_dev, new_config.scan_mask);
 930        return ret;
 931}
 932
 933int iio_update_buffers(struct iio_dev *indio_dev,
 934                       struct iio_buffer *insert_buffer,
 935                       struct iio_buffer *remove_buffer)
 936{
 937        int ret;
 938
 939        if (insert_buffer == remove_buffer)
 940                return 0;
 941
 942        mutex_lock(&indio_dev->info_exist_lock);
 943        mutex_lock(&indio_dev->mlock);
 944
 945        if (insert_buffer && iio_buffer_is_active(insert_buffer))
 946                insert_buffer = NULL;
 947
 948        if (remove_buffer && !iio_buffer_is_active(remove_buffer))
 949                remove_buffer = NULL;
 950
 951        if (!insert_buffer && !remove_buffer) {
 952                ret = 0;
 953                goto out_unlock;
 954        }
 955
 956        if (indio_dev->info == NULL) {
 957                ret = -ENODEV;
 958                goto out_unlock;
 959        }
 960
 961        ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer);
 962
 963out_unlock:
 964        mutex_unlock(&indio_dev->mlock);
 965        mutex_unlock(&indio_dev->info_exist_lock);
 966
 967        return ret;
 968}
 969EXPORT_SYMBOL_GPL(iio_update_buffers);
 970
 971void iio_disable_all_buffers(struct iio_dev *indio_dev)
 972{
 973        iio_disable_buffers(indio_dev);
 974        iio_buffer_deactivate_all(indio_dev);
 975}
 976
 977static ssize_t iio_buffer_store_enable(struct device *dev,
 978                                       struct device_attribute *attr,
 979                                       const char *buf,
 980                                       size_t len)
 981{
 982        int ret;
 983        bool requested_state;
 984        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 985        bool inlist;
 986
 987        ret = strtobool(buf, &requested_state);
 988        if (ret < 0)
 989                return ret;
 990
 991        mutex_lock(&indio_dev->mlock);
 992
 993        /* Find out if it is in the list */
 994        inlist = iio_buffer_is_active(indio_dev->buffer);
 995        /* Already in desired state */
 996        if (inlist == requested_state)
 997                goto done;
 998
 999        if (requested_state)
1000                ret = __iio_update_buffers(indio_dev,
1001                                         indio_dev->buffer, NULL);
1002        else
1003                ret = __iio_update_buffers(indio_dev,
1004                                         NULL, indio_dev->buffer);
1005
1006done:
1007        mutex_unlock(&indio_dev->mlock);
1008        return (ret < 0) ? ret : len;
1009}
1010
1011static const char * const iio_scan_elements_group_name = "scan_elements";
1012
1013static ssize_t iio_buffer_show_watermark(struct device *dev,
1014                                         struct device_attribute *attr,
1015                                         char *buf)
1016{
1017        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1018        struct iio_buffer *buffer = indio_dev->buffer;
1019
1020        return sprintf(buf, "%u\n", buffer->watermark);
1021}
1022
1023static ssize_t iio_buffer_store_watermark(struct device *dev,
1024                                          struct device_attribute *attr,
1025                                          const char *buf,
1026                                          size_t len)
1027{
1028        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1029        struct iio_buffer *buffer = indio_dev->buffer;
1030        unsigned int val;
1031        int ret;
1032
1033        ret = kstrtouint(buf, 10, &val);
1034        if (ret)
1035                return ret;
1036        if (!val)
1037                return -EINVAL;
1038
1039        mutex_lock(&indio_dev->mlock);
1040
1041        if (val > buffer->length) {
1042                ret = -EINVAL;
1043                goto out;
1044        }
1045
1046        if (iio_buffer_is_active(indio_dev->buffer)) {
1047                ret = -EBUSY;
1048                goto out;
1049        }
1050
1051        buffer->watermark = val;
1052out:
1053        mutex_unlock(&indio_dev->mlock);
1054
1055        return ret ? ret : len;
1056}
1057
1058static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
1059                   iio_buffer_write_length);
1060static struct device_attribute dev_attr_length_ro = __ATTR(length,
1061        S_IRUGO, iio_buffer_read_length, NULL);
1062static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
1063                   iio_buffer_show_enable, iio_buffer_store_enable);
1064static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR,
1065                   iio_buffer_show_watermark, iio_buffer_store_watermark);
1066static struct device_attribute dev_attr_watermark_ro = __ATTR(watermark,
1067        S_IRUGO, iio_buffer_show_watermark, NULL);
1068
1069static struct attribute *iio_buffer_attrs[] = {
1070        &dev_attr_length.attr,
1071        &dev_attr_enable.attr,
1072        &dev_attr_watermark.attr,
1073};
1074
1075int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
1076{
1077        struct iio_dev_attr *p;
1078        struct attribute **attr;
1079        struct iio_buffer *buffer = indio_dev->buffer;
1080        int ret, i, attrn, attrcount, attrcount_orig = 0;
1081        const struct iio_chan_spec *channels;
1082
1083        channels = indio_dev->channels;
1084        if (channels) {
1085                int ml = indio_dev->masklength;
1086
1087                for (i = 0; i < indio_dev->num_channels; i++)
1088                        ml = max(ml, channels[i].scan_index + 1);
1089                indio_dev->masklength = ml;
1090        }
1091
1092        if (!buffer)
1093                return 0;
1094
1095        attrcount = 0;
1096        if (buffer->attrs) {
1097                while (buffer->attrs[attrcount] != NULL)
1098                        attrcount++;
1099        }
1100
1101        attr = kcalloc(attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1,
1102                       sizeof(struct attribute *), GFP_KERNEL);
1103        if (!attr)
1104                return -ENOMEM;
1105
1106        memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
1107        if (!buffer->access->set_length)
1108                attr[0] = &dev_attr_length_ro.attr;
1109
1110        if (buffer->access->flags & INDIO_BUFFER_FLAG_FIXED_WATERMARK)
1111                attr[2] = &dev_attr_watermark_ro.attr;
1112
1113        if (buffer->attrs)
1114                memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs,
1115                       sizeof(struct attribute *) * attrcount);
1116
1117        attr[attrcount + ARRAY_SIZE(iio_buffer_attrs)] = NULL;
1118
1119        buffer->buffer_group.name = "buffer";
1120        buffer->buffer_group.attrs = attr;
1121
1122        indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group;
1123
1124        if (buffer->scan_el_attrs != NULL) {
1125                attr = buffer->scan_el_attrs->attrs;
1126                while (*attr++ != NULL)
1127                        attrcount_orig++;
1128        }
1129        attrcount = attrcount_orig;
1130        INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
1131        channels = indio_dev->channels;
1132        if (channels) {
1133                /* new magic */
1134                for (i = 0; i < indio_dev->num_channels; i++) {
1135                        if (channels[i].scan_index < 0)
1136                                continue;
1137
1138                        ret = iio_buffer_add_channel_sysfs(indio_dev,
1139                                                         &channels[i]);
1140                        if (ret < 0)
1141                                goto error_cleanup_dynamic;
1142                        attrcount += ret;
1143                        if (channels[i].type == IIO_TIMESTAMP)
1144                                indio_dev->scan_index_timestamp =
1145                                        channels[i].scan_index;
1146                }
1147                if (indio_dev->masklength && buffer->scan_mask == NULL) {
1148                        buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
1149                                                    sizeof(*buffer->scan_mask),
1150                                                    GFP_KERNEL);
1151                        if (buffer->scan_mask == NULL) {
1152                                ret = -ENOMEM;
1153                                goto error_cleanup_dynamic;
1154                        }
1155                }
1156        }
1157
1158        buffer->scan_el_group.name = iio_scan_elements_group_name;
1159
1160        buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
1161                                              sizeof(buffer->scan_el_group.attrs[0]),
1162                                              GFP_KERNEL);
1163        if (buffer->scan_el_group.attrs == NULL) {
1164                ret = -ENOMEM;
1165                goto error_free_scan_mask;
1166        }
1167        if (buffer->scan_el_attrs)
1168                memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
1169                       sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
1170        attrn = attrcount_orig;
1171
1172        list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
1173                buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
1174        indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
1175
1176        return 0;
1177
1178error_free_scan_mask:
1179        kfree(buffer->scan_mask);
1180error_cleanup_dynamic:
1181        iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
1182        kfree(indio_dev->buffer->buffer_group.attrs);
1183
1184        return ret;
1185}
1186
1187void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
1188{
1189        if (!indio_dev->buffer)
1190                return;
1191
1192        kfree(indio_dev->buffer->scan_mask);
1193        kfree(indio_dev->buffer->buffer_group.attrs);
1194        kfree(indio_dev->buffer->scan_el_group.attrs);
1195        iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
1196}
1197
1198/**
1199 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
1200 * @indio_dev: the iio device
1201 * @mask: scan mask to be checked
1202 *
1203 * Return true if exactly one bit is set in the scan mask, false otherwise. It
1204 * can be used for devices where only one channel can be active for sampling at
1205 * a time.
1206 */
1207bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
1208        const unsigned long *mask)
1209{
1210        return bitmap_weight(mask, indio_dev->masklength) == 1;
1211}
1212EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
1213
1214int iio_scan_mask_query(struct iio_dev *indio_dev,
1215                        struct iio_buffer *buffer, int bit)
1216{
1217        if (bit > indio_dev->masklength)
1218                return -EINVAL;
1219
1220        if (!buffer->scan_mask)
1221                return 0;
1222
1223        /* Ensure return value is 0 or 1. */
1224        return !!test_bit(bit, buffer->scan_mask);
1225};
1226EXPORT_SYMBOL_GPL(iio_scan_mask_query);
1227
1228/**
1229 * struct iio_demux_table - table describing demux memcpy ops
1230 * @from:       index to copy from
1231 * @to:         index to copy to
1232 * @length:     how many bytes to copy
1233 * @l:          list head used for management
1234 */
1235struct iio_demux_table {
1236        unsigned from;
1237        unsigned to;
1238        unsigned length;
1239        struct list_head l;
1240};
1241
1242static const void *iio_demux(struct iio_buffer *buffer,
1243                                 const void *datain)
1244{
1245        struct iio_demux_table *t;
1246
1247        if (list_empty(&buffer->demux_list))
1248                return datain;
1249        list_for_each_entry(t, &buffer->demux_list, l)
1250                memcpy(buffer->demux_bounce + t->to,
1251                       datain + t->from, t->length);
1252
1253        return buffer->demux_bounce;
1254}
1255
1256static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
1257{
1258        const void *dataout = iio_demux(buffer, data);
1259        int ret;
1260
1261        ret = buffer->access->store_to(buffer, dataout);
1262        if (ret)
1263                return ret;
1264
1265        /*
1266         * We can't just test for watermark to decide if we wake the poll queue
1267         * because read may request less samples than the watermark.
1268         */
1269        wake_up_interruptible_poll(&buffer->pollq, POLLIN | POLLRDNORM);
1270        return 0;
1271}
1272
1273static void iio_buffer_demux_free(struct iio_buffer *buffer)
1274{
1275        struct iio_demux_table *p, *q;
1276        list_for_each_entry_safe(p, q, &buffer->demux_list, l) {
1277                list_del(&p->l);
1278                kfree(p);
1279        }
1280}
1281
1282
1283int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
1284{
1285        int ret;
1286        struct iio_buffer *buf;
1287
1288        list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) {
1289                ret = iio_push_to_buffer(buf, data);
1290                if (ret < 0)
1291                        return ret;
1292        }
1293
1294        return 0;
1295}
1296EXPORT_SYMBOL_GPL(iio_push_to_buffers);
1297
1298static int iio_buffer_add_demux(struct iio_buffer *buffer,
1299        struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc,
1300        unsigned int length)
1301{
1302
1303        if (*p && (*p)->from + (*p)->length == in_loc &&
1304                (*p)->to + (*p)->length == out_loc) {
1305                (*p)->length += length;
1306        } else {
1307                *p = kmalloc(sizeof(**p), GFP_KERNEL);
1308                if (*p == NULL)
1309                        return -ENOMEM;
1310                (*p)->from = in_loc;
1311                (*p)->to = out_loc;
1312                (*p)->length = length;
1313                list_add_tail(&(*p)->l, &buffer->demux_list);
1314        }
1315
1316        return 0;
1317}
1318
1319static int iio_buffer_update_demux(struct iio_dev *indio_dev,
1320                                   struct iio_buffer *buffer)
1321{
1322        int ret, in_ind = -1, out_ind, length;
1323        unsigned in_loc = 0, out_loc = 0;
1324        struct iio_demux_table *p = NULL;
1325
1326        /* Clear out any old demux */
1327        iio_buffer_demux_free(buffer);
1328        kfree(buffer->demux_bounce);
1329        buffer->demux_bounce = NULL;
1330
1331        /* First work out which scan mode we will actually have */
1332        if (bitmap_equal(indio_dev->active_scan_mask,
1333                         buffer->scan_mask,
1334                         indio_dev->masklength))
1335                return 0;
1336
1337        /* Now we have the two masks, work from least sig and build up sizes */
1338        for_each_set_bit(out_ind,
1339                         buffer->scan_mask,
1340                         indio_dev->masklength) {
1341                in_ind = find_next_bit(indio_dev->active_scan_mask,
1342                                       indio_dev->masklength,
1343                                       in_ind + 1);
1344                while (in_ind != out_ind) {
1345                        in_ind = find_next_bit(indio_dev->active_scan_mask,
1346                                               indio_dev->masklength,
1347                                               in_ind + 1);
1348                        length = iio_storage_bytes_for_si(indio_dev, in_ind);
1349                        /* Make sure we are aligned */
1350                        in_loc = roundup(in_loc, length) + length;
1351                }
1352                length = iio_storage_bytes_for_si(indio_dev, in_ind);
1353                out_loc = roundup(out_loc, length);
1354                in_loc = roundup(in_loc, length);
1355                ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
1356                if (ret)
1357                        goto error_clear_mux_table;
1358                out_loc += length;
1359                in_loc += length;
1360        }
1361        /* Relies on scan_timestamp being last */
1362        if (buffer->scan_timestamp) {
1363                length = iio_storage_bytes_for_timestamp(indio_dev);
1364                out_loc = roundup(out_loc, length);
1365                in_loc = roundup(in_loc, length);
1366                ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length);
1367                if (ret)
1368                        goto error_clear_mux_table;
1369                out_loc += length;
1370                in_loc += length;
1371        }
1372        buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL);
1373        if (buffer->demux_bounce == NULL) {
1374                ret = -ENOMEM;
1375                goto error_clear_mux_table;
1376        }
1377        return 0;
1378
1379error_clear_mux_table:
1380        iio_buffer_demux_free(buffer);
1381
1382        return ret;
1383}
1384
1385int iio_update_demux(struct iio_dev *indio_dev)
1386{
1387        struct iio_buffer *buffer;
1388        int ret;
1389
1390        list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) {
1391                ret = iio_buffer_update_demux(indio_dev, buffer);
1392                if (ret < 0)
1393                        goto error_clear_mux_table;
1394        }
1395        return 0;
1396
1397error_clear_mux_table:
1398        list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list)
1399                iio_buffer_demux_free(buffer);
1400
1401        return ret;
1402}
1403EXPORT_SYMBOL_GPL(iio_update_demux);
1404
1405/**
1406 * iio_buffer_release() - Free a buffer's resources
1407 * @ref: Pointer to the kref embedded in the iio_buffer struct
1408 *
1409 * This function is called when the last reference to the buffer has been
1410 * dropped. It will typically free all resources allocated by the buffer. Do not
1411 * call this function manually, always use iio_buffer_put() when done using a
1412 * buffer.
1413 */
1414static void iio_buffer_release(struct kref *ref)
1415{
1416        struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref);
1417
1418        buffer->access->release(buffer);
1419}
1420
1421/**
1422 * iio_buffer_get() - Grab a reference to the buffer
1423 * @buffer: The buffer to grab a reference for, may be NULL
1424 *
1425 * Returns the pointer to the buffer that was passed into the function.
1426 */
1427struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer)
1428{
1429        if (buffer)
1430                kref_get(&buffer->ref);
1431
1432        return buffer;
1433}
1434EXPORT_SYMBOL_GPL(iio_buffer_get);
1435
1436/**
1437 * iio_buffer_put() - Release the reference to the buffer
1438 * @buffer: The buffer to release the reference for, may be NULL
1439 */
1440void iio_buffer_put(struct iio_buffer *buffer)
1441{
1442        if (buffer)
1443                kref_put(&buffer->ref, iio_buffer_release);
1444}
1445EXPORT_SYMBOL_GPL(iio_buffer_put);
1446