1a980e046SJonathan Cameron /* The industrial I/O core 2a980e046SJonathan Cameron * 3a980e046SJonathan Cameron * Copyright (c) 2008 Jonathan Cameron 4a980e046SJonathan Cameron * 5a980e046SJonathan Cameron * This program is free software; you can redistribute it and/or modify it 6a980e046SJonathan Cameron * under the terms of the GNU General Public License version 2 as published by 7a980e046SJonathan Cameron * the Free Software Foundation. 8a980e046SJonathan Cameron * 9a980e046SJonathan Cameron * Handling of buffer allocation / resizing. 10a980e046SJonathan Cameron * 11a980e046SJonathan Cameron * 12a980e046SJonathan Cameron * Things to look at here. 13a980e046SJonathan Cameron * - Better memory allocation techniques? 14a980e046SJonathan Cameron * - Alternative access techniques? 15a980e046SJonathan Cameron */ 16a980e046SJonathan Cameron #include <linux/kernel.h> 17a980e046SJonathan Cameron #include <linux/export.h> 18a980e046SJonathan Cameron #include <linux/device.h> 19a980e046SJonathan Cameron #include <linux/fs.h> 20a980e046SJonathan Cameron #include <linux/cdev.h> 21a980e046SJonathan Cameron #include <linux/slab.h> 22a980e046SJonathan Cameron #include <linux/poll.h> 23d2f0a48fSLars-Peter Clausen #include <linux/sched.h> 24a980e046SJonathan Cameron 25a980e046SJonathan Cameron #include <linux/iio/iio.h> 26a980e046SJonathan Cameron #include "iio_core.h" 27a980e046SJonathan Cameron #include <linux/iio/sysfs.h> 28a980e046SJonathan Cameron #include <linux/iio/buffer.h> 29a980e046SJonathan Cameron 30a980e046SJonathan Cameron static const char * const iio_endian_prefix[] = { 31a980e046SJonathan Cameron [IIO_BE] = "be", 32a980e046SJonathan Cameron [IIO_LE] = "le", 33a980e046SJonathan Cameron }; 34a980e046SJonathan Cameron 35705ee2c9SLars-Peter Clausen static bool iio_buffer_is_active(struct iio_buffer *buf) 3684b36ce5SJonathan Cameron { 37705ee2c9SLars-Peter Clausen return !list_empty(&buf->buffer_list); 3884b36ce5SJonathan Cameron } 3984b36ce5SJonathan Cameron 40647cc7b9SLars-Peter Clausen static bool iio_buffer_data_available(struct iio_buffer *buf) 41647cc7b9SLars-Peter Clausen { 42647cc7b9SLars-Peter Clausen return buf->access->data_available(buf); 43647cc7b9SLars-Peter Clausen } 44647cc7b9SLars-Peter Clausen 45a980e046SJonathan Cameron /** 46a980e046SJonathan Cameron * iio_buffer_read_first_n_outer() - chrdev read for buffer access 47a980e046SJonathan Cameron * 48a980e046SJonathan Cameron * This function relies on all buffer implementations having an 49a980e046SJonathan Cameron * iio_buffer as their first element. 50a980e046SJonathan Cameron **/ 51a980e046SJonathan Cameron ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf, 52a980e046SJonathan Cameron size_t n, loff_t *f_ps) 53a980e046SJonathan Cameron { 54a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 55a980e046SJonathan Cameron struct iio_buffer *rb = indio_dev->buffer; 56ee551a10SLars-Peter Clausen int ret; 57a980e046SJonathan Cameron 58f18e7a06SLars-Peter Clausen if (!indio_dev->info) 59f18e7a06SLars-Peter Clausen return -ENODEV; 60f18e7a06SLars-Peter Clausen 61a980e046SJonathan Cameron if (!rb || !rb->access->read_first_n) 62a980e046SJonathan Cameron return -EINVAL; 63ee551a10SLars-Peter Clausen 64ee551a10SLars-Peter Clausen do { 65ee551a10SLars-Peter Clausen if (!iio_buffer_data_available(rb)) { 66ee551a10SLars-Peter Clausen if (filp->f_flags & O_NONBLOCK) 67ee551a10SLars-Peter Clausen return -EAGAIN; 68ee551a10SLars-Peter Clausen 69ee551a10SLars-Peter Clausen ret = wait_event_interruptible(rb->pollq, 70ee551a10SLars-Peter Clausen iio_buffer_data_available(rb) || 71ee551a10SLars-Peter Clausen indio_dev->info == NULL); 72ee551a10SLars-Peter Clausen if (ret) 73ee551a10SLars-Peter Clausen return ret; 74ee551a10SLars-Peter Clausen if (indio_dev->info == NULL) 75ee551a10SLars-Peter Clausen return -ENODEV; 76ee551a10SLars-Peter Clausen } 77ee551a10SLars-Peter Clausen 78ee551a10SLars-Peter Clausen ret = rb->access->read_first_n(rb, n, buf); 79ee551a10SLars-Peter Clausen if (ret == 0 && (filp->f_flags & O_NONBLOCK)) 80ee551a10SLars-Peter Clausen ret = -EAGAIN; 81ee551a10SLars-Peter Clausen } while (ret == 0); 82ee551a10SLars-Peter Clausen 83ee551a10SLars-Peter Clausen return ret; 84a980e046SJonathan Cameron } 85a980e046SJonathan Cameron 86a980e046SJonathan Cameron /** 87a980e046SJonathan Cameron * iio_buffer_poll() - poll the buffer to find out if it has data 88a980e046SJonathan Cameron */ 89a980e046SJonathan Cameron unsigned int iio_buffer_poll(struct file *filp, 90a980e046SJonathan Cameron struct poll_table_struct *wait) 91a980e046SJonathan Cameron { 92a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 93a980e046SJonathan Cameron struct iio_buffer *rb = indio_dev->buffer; 94a980e046SJonathan Cameron 95f18e7a06SLars-Peter Clausen if (!indio_dev->info) 96f18e7a06SLars-Peter Clausen return -ENODEV; 97f18e7a06SLars-Peter Clausen 98a980e046SJonathan Cameron poll_wait(filp, &rb->pollq, wait); 99647cc7b9SLars-Peter Clausen if (iio_buffer_data_available(rb)) 100a980e046SJonathan Cameron return POLLIN | POLLRDNORM; 101a980e046SJonathan Cameron /* need a way of knowing if there may be enough data... */ 102a980e046SJonathan Cameron return 0; 103a980e046SJonathan Cameron } 104a980e046SJonathan Cameron 105d2f0a48fSLars-Peter Clausen /** 106d2f0a48fSLars-Peter Clausen * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue 107d2f0a48fSLars-Peter Clausen * @indio_dev: The IIO device 108d2f0a48fSLars-Peter Clausen * 109d2f0a48fSLars-Peter Clausen * Wakes up the event waitqueue used for poll(). Should usually 110d2f0a48fSLars-Peter Clausen * be called when the device is unregistered. 111d2f0a48fSLars-Peter Clausen */ 112d2f0a48fSLars-Peter Clausen void iio_buffer_wakeup_poll(struct iio_dev *indio_dev) 113d2f0a48fSLars-Peter Clausen { 114d2f0a48fSLars-Peter Clausen if (!indio_dev->buffer) 115d2f0a48fSLars-Peter Clausen return; 116d2f0a48fSLars-Peter Clausen 117d2f0a48fSLars-Peter Clausen wake_up(&indio_dev->buffer->pollq); 118d2f0a48fSLars-Peter Clausen } 119d2f0a48fSLars-Peter Clausen 120a980e046SJonathan Cameron void iio_buffer_init(struct iio_buffer *buffer) 121a980e046SJonathan Cameron { 122a980e046SJonathan Cameron INIT_LIST_HEAD(&buffer->demux_list); 123705ee2c9SLars-Peter Clausen INIT_LIST_HEAD(&buffer->buffer_list); 124a980e046SJonathan Cameron init_waitqueue_head(&buffer->pollq); 1259e69c935SLars-Peter Clausen kref_init(&buffer->ref); 126a980e046SJonathan Cameron } 127a980e046SJonathan Cameron EXPORT_SYMBOL(iio_buffer_init); 128a980e046SJonathan Cameron 129a980e046SJonathan Cameron static ssize_t iio_show_scan_index(struct device *dev, 130a980e046SJonathan Cameron struct device_attribute *attr, 131a980e046SJonathan Cameron char *buf) 132a980e046SJonathan Cameron { 133a980e046SJonathan Cameron return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index); 134a980e046SJonathan Cameron } 135a980e046SJonathan Cameron 136a980e046SJonathan Cameron static ssize_t iio_show_fixed_type(struct device *dev, 137a980e046SJonathan Cameron struct device_attribute *attr, 138a980e046SJonathan Cameron char *buf) 139a980e046SJonathan Cameron { 140a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 141a980e046SJonathan Cameron u8 type = this_attr->c->scan_type.endianness; 142a980e046SJonathan Cameron 143a980e046SJonathan Cameron if (type == IIO_CPU) { 144a980e046SJonathan Cameron #ifdef __LITTLE_ENDIAN 145a980e046SJonathan Cameron type = IIO_LE; 146a980e046SJonathan Cameron #else 147a980e046SJonathan Cameron type = IIO_BE; 148a980e046SJonathan Cameron #endif 149a980e046SJonathan Cameron } 1500ee8546aSSrinivas Pandruvada if (this_attr->c->scan_type.repeat > 1) 1510ee8546aSSrinivas Pandruvada return sprintf(buf, "%s:%c%d/%dX%d>>%u\n", 1520ee8546aSSrinivas Pandruvada iio_endian_prefix[type], 1530ee8546aSSrinivas Pandruvada this_attr->c->scan_type.sign, 1540ee8546aSSrinivas Pandruvada this_attr->c->scan_type.realbits, 1550ee8546aSSrinivas Pandruvada this_attr->c->scan_type.storagebits, 1560ee8546aSSrinivas Pandruvada this_attr->c->scan_type.repeat, 1570ee8546aSSrinivas Pandruvada this_attr->c->scan_type.shift); 1580ee8546aSSrinivas Pandruvada else 159a980e046SJonathan Cameron return sprintf(buf, "%s:%c%d/%d>>%u\n", 160a980e046SJonathan Cameron iio_endian_prefix[type], 161a980e046SJonathan Cameron this_attr->c->scan_type.sign, 162a980e046SJonathan Cameron this_attr->c->scan_type.realbits, 163a980e046SJonathan Cameron this_attr->c->scan_type.storagebits, 164a980e046SJonathan Cameron this_attr->c->scan_type.shift); 165a980e046SJonathan Cameron } 166a980e046SJonathan Cameron 167a980e046SJonathan Cameron static ssize_t iio_scan_el_show(struct device *dev, 168a980e046SJonathan Cameron struct device_attribute *attr, 169a980e046SJonathan Cameron char *buf) 170a980e046SJonathan Cameron { 171a980e046SJonathan Cameron int ret; 172e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 173a980e046SJonathan Cameron 1742076a20fSAlec Berg /* Ensure ret is 0 or 1. */ 1752076a20fSAlec Berg ret = !!test_bit(to_iio_dev_attr(attr)->address, 176a980e046SJonathan Cameron indio_dev->buffer->scan_mask); 177a980e046SJonathan Cameron 178a980e046SJonathan Cameron return sprintf(buf, "%d\n", ret); 179a980e046SJonathan Cameron } 180a980e046SJonathan Cameron 181217a5cf0SLars-Peter Clausen /* Note NULL used as error indicator as it doesn't make sense. */ 182217a5cf0SLars-Peter Clausen static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks, 183217a5cf0SLars-Peter Clausen unsigned int masklength, 184217a5cf0SLars-Peter Clausen const unsigned long *mask) 185217a5cf0SLars-Peter Clausen { 186217a5cf0SLars-Peter Clausen if (bitmap_empty(mask, masklength)) 187217a5cf0SLars-Peter Clausen return NULL; 188217a5cf0SLars-Peter Clausen while (*av_masks) { 189217a5cf0SLars-Peter Clausen if (bitmap_subset(mask, av_masks, masklength)) 190217a5cf0SLars-Peter Clausen return av_masks; 191217a5cf0SLars-Peter Clausen av_masks += BITS_TO_LONGS(masklength); 192217a5cf0SLars-Peter Clausen } 193217a5cf0SLars-Peter Clausen return NULL; 194217a5cf0SLars-Peter Clausen } 195217a5cf0SLars-Peter Clausen 196217a5cf0SLars-Peter Clausen static bool iio_validate_scan_mask(struct iio_dev *indio_dev, 197217a5cf0SLars-Peter Clausen const unsigned long *mask) 198217a5cf0SLars-Peter Clausen { 199217a5cf0SLars-Peter Clausen if (!indio_dev->setup_ops->validate_scan_mask) 200217a5cf0SLars-Peter Clausen return true; 201217a5cf0SLars-Peter Clausen 202217a5cf0SLars-Peter Clausen return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask); 203217a5cf0SLars-Peter Clausen } 204217a5cf0SLars-Peter Clausen 205217a5cf0SLars-Peter Clausen /** 206217a5cf0SLars-Peter Clausen * iio_scan_mask_set() - set particular bit in the scan mask 207217a5cf0SLars-Peter Clausen * @indio_dev: the iio device 208217a5cf0SLars-Peter Clausen * @buffer: the buffer whose scan mask we are interested in 209217a5cf0SLars-Peter Clausen * @bit: the bit to be set. 210217a5cf0SLars-Peter Clausen * 211217a5cf0SLars-Peter Clausen * Note that at this point we have no way of knowing what other 212217a5cf0SLars-Peter Clausen * buffers might request, hence this code only verifies that the 213217a5cf0SLars-Peter Clausen * individual buffers request is plausible. 214217a5cf0SLars-Peter Clausen */ 215217a5cf0SLars-Peter Clausen static int iio_scan_mask_set(struct iio_dev *indio_dev, 216217a5cf0SLars-Peter Clausen struct iio_buffer *buffer, int bit) 217217a5cf0SLars-Peter Clausen { 218217a5cf0SLars-Peter Clausen const unsigned long *mask; 219217a5cf0SLars-Peter Clausen unsigned long *trialmask; 220217a5cf0SLars-Peter Clausen 221217a5cf0SLars-Peter Clausen trialmask = kmalloc(sizeof(*trialmask)* 222217a5cf0SLars-Peter Clausen BITS_TO_LONGS(indio_dev->masklength), 223217a5cf0SLars-Peter Clausen GFP_KERNEL); 224217a5cf0SLars-Peter Clausen 225217a5cf0SLars-Peter Clausen if (trialmask == NULL) 226217a5cf0SLars-Peter Clausen return -ENOMEM; 227217a5cf0SLars-Peter Clausen if (!indio_dev->masklength) { 228217a5cf0SLars-Peter Clausen WARN_ON("Trying to set scanmask prior to registering buffer\n"); 229217a5cf0SLars-Peter Clausen goto err_invalid_mask; 230217a5cf0SLars-Peter Clausen } 231217a5cf0SLars-Peter Clausen bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength); 232217a5cf0SLars-Peter Clausen set_bit(bit, trialmask); 233217a5cf0SLars-Peter Clausen 234217a5cf0SLars-Peter Clausen if (!iio_validate_scan_mask(indio_dev, trialmask)) 235217a5cf0SLars-Peter Clausen goto err_invalid_mask; 236217a5cf0SLars-Peter Clausen 237217a5cf0SLars-Peter Clausen if (indio_dev->available_scan_masks) { 238217a5cf0SLars-Peter Clausen mask = iio_scan_mask_match(indio_dev->available_scan_masks, 239217a5cf0SLars-Peter Clausen indio_dev->masklength, 240217a5cf0SLars-Peter Clausen trialmask); 241217a5cf0SLars-Peter Clausen if (!mask) 242217a5cf0SLars-Peter Clausen goto err_invalid_mask; 243217a5cf0SLars-Peter Clausen } 244217a5cf0SLars-Peter Clausen bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength); 245217a5cf0SLars-Peter Clausen 246217a5cf0SLars-Peter Clausen kfree(trialmask); 247217a5cf0SLars-Peter Clausen 248217a5cf0SLars-Peter Clausen return 0; 249217a5cf0SLars-Peter Clausen 250217a5cf0SLars-Peter Clausen err_invalid_mask: 251217a5cf0SLars-Peter Clausen kfree(trialmask); 252217a5cf0SLars-Peter Clausen return -EINVAL; 253217a5cf0SLars-Peter Clausen } 254217a5cf0SLars-Peter Clausen 255a980e046SJonathan Cameron static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit) 256a980e046SJonathan Cameron { 257a980e046SJonathan Cameron clear_bit(bit, buffer->scan_mask); 258a980e046SJonathan Cameron return 0; 259a980e046SJonathan Cameron } 260a980e046SJonathan Cameron 261a980e046SJonathan Cameron static ssize_t iio_scan_el_store(struct device *dev, 262a980e046SJonathan Cameron struct device_attribute *attr, 263a980e046SJonathan Cameron const char *buf, 264a980e046SJonathan Cameron size_t len) 265a980e046SJonathan Cameron { 266a980e046SJonathan Cameron int ret; 267a980e046SJonathan Cameron bool state; 268e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 269a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 270a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 271a980e046SJonathan Cameron 272a980e046SJonathan Cameron ret = strtobool(buf, &state); 273a980e046SJonathan Cameron if (ret < 0) 274a980e046SJonathan Cameron return ret; 275a980e046SJonathan Cameron mutex_lock(&indio_dev->mlock); 276705ee2c9SLars-Peter Clausen if (iio_buffer_is_active(indio_dev->buffer)) { 277a980e046SJonathan Cameron ret = -EBUSY; 278a980e046SJonathan Cameron goto error_ret; 279a980e046SJonathan Cameron } 280a980e046SJonathan Cameron ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address); 281a980e046SJonathan Cameron if (ret < 0) 282a980e046SJonathan Cameron goto error_ret; 283a980e046SJonathan Cameron if (!state && ret) { 284a980e046SJonathan Cameron ret = iio_scan_mask_clear(buffer, this_attr->address); 285a980e046SJonathan Cameron if (ret) 286a980e046SJonathan Cameron goto error_ret; 287a980e046SJonathan Cameron } else if (state && !ret) { 288a980e046SJonathan Cameron ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address); 289a980e046SJonathan Cameron if (ret) 290a980e046SJonathan Cameron goto error_ret; 291a980e046SJonathan Cameron } 292a980e046SJonathan Cameron 293a980e046SJonathan Cameron error_ret: 294a980e046SJonathan Cameron mutex_unlock(&indio_dev->mlock); 295a980e046SJonathan Cameron 296a980e046SJonathan Cameron return ret < 0 ? ret : len; 297a980e046SJonathan Cameron 298a980e046SJonathan Cameron } 299a980e046SJonathan Cameron 300a980e046SJonathan Cameron static ssize_t iio_scan_el_ts_show(struct device *dev, 301a980e046SJonathan Cameron struct device_attribute *attr, 302a980e046SJonathan Cameron char *buf) 303a980e046SJonathan Cameron { 304e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 305a980e046SJonathan Cameron return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp); 306a980e046SJonathan Cameron } 307a980e046SJonathan Cameron 308a980e046SJonathan Cameron static ssize_t iio_scan_el_ts_store(struct device *dev, 309a980e046SJonathan Cameron struct device_attribute *attr, 310a980e046SJonathan Cameron const char *buf, 311a980e046SJonathan Cameron size_t len) 312a980e046SJonathan Cameron { 313a980e046SJonathan Cameron int ret; 314e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 315a980e046SJonathan Cameron bool state; 316a980e046SJonathan Cameron 317a980e046SJonathan Cameron ret = strtobool(buf, &state); 318a980e046SJonathan Cameron if (ret < 0) 319a980e046SJonathan Cameron return ret; 320a980e046SJonathan Cameron 321a980e046SJonathan Cameron mutex_lock(&indio_dev->mlock); 322705ee2c9SLars-Peter Clausen if (iio_buffer_is_active(indio_dev->buffer)) { 323a980e046SJonathan Cameron ret = -EBUSY; 324a980e046SJonathan Cameron goto error_ret; 325a980e046SJonathan Cameron } 326a980e046SJonathan Cameron indio_dev->buffer->scan_timestamp = state; 327a980e046SJonathan Cameron error_ret: 328a980e046SJonathan Cameron mutex_unlock(&indio_dev->mlock); 329a980e046SJonathan Cameron 330a980e046SJonathan Cameron return ret ? ret : len; 331a980e046SJonathan Cameron } 332a980e046SJonathan Cameron 333a980e046SJonathan Cameron static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev, 334a980e046SJonathan Cameron const struct iio_chan_spec *chan) 335a980e046SJonathan Cameron { 336a980e046SJonathan Cameron int ret, attrcount = 0; 337a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 338a980e046SJonathan Cameron 339a980e046SJonathan Cameron ret = __iio_add_chan_devattr("index", 340a980e046SJonathan Cameron chan, 341a980e046SJonathan Cameron &iio_show_scan_index, 342a980e046SJonathan Cameron NULL, 343a980e046SJonathan Cameron 0, 3443704432fSJonathan Cameron IIO_SEPARATE, 345a980e046SJonathan Cameron &indio_dev->dev, 346a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 347a980e046SJonathan Cameron if (ret) 34892825ff9SHartmut Knaack return ret; 349a980e046SJonathan Cameron attrcount++; 350a980e046SJonathan Cameron ret = __iio_add_chan_devattr("type", 351a980e046SJonathan Cameron chan, 352a980e046SJonathan Cameron &iio_show_fixed_type, 353a980e046SJonathan Cameron NULL, 354a980e046SJonathan Cameron 0, 355a980e046SJonathan Cameron 0, 356a980e046SJonathan Cameron &indio_dev->dev, 357a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 358a980e046SJonathan Cameron if (ret) 35992825ff9SHartmut Knaack return ret; 360a980e046SJonathan Cameron attrcount++; 361a980e046SJonathan Cameron if (chan->type != IIO_TIMESTAMP) 362a980e046SJonathan Cameron ret = __iio_add_chan_devattr("en", 363a980e046SJonathan Cameron chan, 364a980e046SJonathan Cameron &iio_scan_el_show, 365a980e046SJonathan Cameron &iio_scan_el_store, 366a980e046SJonathan Cameron chan->scan_index, 367a980e046SJonathan Cameron 0, 368a980e046SJonathan Cameron &indio_dev->dev, 369a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 370a980e046SJonathan Cameron else 371a980e046SJonathan Cameron ret = __iio_add_chan_devattr("en", 372a980e046SJonathan Cameron chan, 373a980e046SJonathan Cameron &iio_scan_el_ts_show, 374a980e046SJonathan Cameron &iio_scan_el_ts_store, 375a980e046SJonathan Cameron chan->scan_index, 376a980e046SJonathan Cameron 0, 377a980e046SJonathan Cameron &indio_dev->dev, 378a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 3799572588cSPeter Meerwald if (ret) 38092825ff9SHartmut Knaack return ret; 381a980e046SJonathan Cameron attrcount++; 382a980e046SJonathan Cameron ret = attrcount; 383a980e046SJonathan Cameron return ret; 384a980e046SJonathan Cameron } 385a980e046SJonathan Cameron 38608e7e0adSLars-Peter Clausen static ssize_t iio_buffer_read_length(struct device *dev, 387a980e046SJonathan Cameron struct device_attribute *attr, 388a980e046SJonathan Cameron char *buf) 389a980e046SJonathan Cameron { 390e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 391a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 392a980e046SJonathan Cameron 393*37495660SLars-Peter Clausen return sprintf(buf, "%d\n", buffer->length); 394a980e046SJonathan Cameron } 395a980e046SJonathan Cameron 39608e7e0adSLars-Peter Clausen static ssize_t iio_buffer_write_length(struct device *dev, 397a980e046SJonathan Cameron struct device_attribute *attr, 39808e7e0adSLars-Peter Clausen const char *buf, size_t len) 399a980e046SJonathan Cameron { 400e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 401a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 402948ad205SLars-Peter Clausen unsigned int val; 403948ad205SLars-Peter Clausen int ret; 404a980e046SJonathan Cameron 405948ad205SLars-Peter Clausen ret = kstrtouint(buf, 10, &val); 406a980e046SJonathan Cameron if (ret) 407a980e046SJonathan Cameron return ret; 408a980e046SJonathan Cameron 409*37495660SLars-Peter Clausen if (val == buffer->length) 410a980e046SJonathan Cameron return len; 411a980e046SJonathan Cameron 412a980e046SJonathan Cameron mutex_lock(&indio_dev->mlock); 413705ee2c9SLars-Peter Clausen if (iio_buffer_is_active(indio_dev->buffer)) { 414a980e046SJonathan Cameron ret = -EBUSY; 415a980e046SJonathan Cameron } else { 416a980e046SJonathan Cameron buffer->access->set_length(buffer, val); 417a980e046SJonathan Cameron ret = 0; 418a980e046SJonathan Cameron } 419a980e046SJonathan Cameron mutex_unlock(&indio_dev->mlock); 420a980e046SJonathan Cameron 421a980e046SJonathan Cameron return ret ? ret : len; 422a980e046SJonathan Cameron } 423a980e046SJonathan Cameron 42408e7e0adSLars-Peter Clausen static ssize_t iio_buffer_show_enable(struct device *dev, 425a980e046SJonathan Cameron struct device_attribute *attr, 426a980e046SJonathan Cameron char *buf) 427a980e046SJonathan Cameron { 428e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 429705ee2c9SLars-Peter Clausen return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer)); 430a980e046SJonathan Cameron } 431a980e046SJonathan Cameron 432183f4173SPeter Meerwald static int iio_compute_scan_bytes(struct iio_dev *indio_dev, 433183f4173SPeter Meerwald const unsigned long *mask, bool timestamp) 434a980e046SJonathan Cameron { 435a980e046SJonathan Cameron const struct iio_chan_spec *ch; 436a980e046SJonathan Cameron unsigned bytes = 0; 437a980e046SJonathan Cameron int length, i; 438a980e046SJonathan Cameron 439a980e046SJonathan Cameron /* How much space will the demuxed element take? */ 440a980e046SJonathan Cameron for_each_set_bit(i, mask, 441a980e046SJonathan Cameron indio_dev->masklength) { 442a980e046SJonathan Cameron ch = iio_find_channel_from_si(indio_dev, i); 4430ee8546aSSrinivas Pandruvada if (ch->scan_type.repeat > 1) 4440ee8546aSSrinivas Pandruvada length = ch->scan_type.storagebits / 8 * 4450ee8546aSSrinivas Pandruvada ch->scan_type.repeat; 4460ee8546aSSrinivas Pandruvada else 447a980e046SJonathan Cameron length = ch->scan_type.storagebits / 8; 448a980e046SJonathan Cameron bytes = ALIGN(bytes, length); 449a980e046SJonathan Cameron bytes += length; 450a980e046SJonathan Cameron } 451a980e046SJonathan Cameron if (timestamp) { 452a980e046SJonathan Cameron ch = iio_find_channel_from_si(indio_dev, 453a980e046SJonathan Cameron indio_dev->scan_index_timestamp); 4540ee8546aSSrinivas Pandruvada if (ch->scan_type.repeat > 1) 4550ee8546aSSrinivas Pandruvada length = ch->scan_type.storagebits / 8 * 4560ee8546aSSrinivas Pandruvada ch->scan_type.repeat; 4570ee8546aSSrinivas Pandruvada else 458a980e046SJonathan Cameron length = ch->scan_type.storagebits / 8; 459a980e046SJonathan Cameron bytes = ALIGN(bytes, length); 460a980e046SJonathan Cameron bytes += length; 461a980e046SJonathan Cameron } 462a980e046SJonathan Cameron return bytes; 463a980e046SJonathan Cameron } 464a980e046SJonathan Cameron 4659e69c935SLars-Peter Clausen static void iio_buffer_activate(struct iio_dev *indio_dev, 4669e69c935SLars-Peter Clausen struct iio_buffer *buffer) 4679e69c935SLars-Peter Clausen { 4689e69c935SLars-Peter Clausen iio_buffer_get(buffer); 4699e69c935SLars-Peter Clausen list_add(&buffer->buffer_list, &indio_dev->buffer_list); 4709e69c935SLars-Peter Clausen } 4719e69c935SLars-Peter Clausen 4729e69c935SLars-Peter Clausen static void iio_buffer_deactivate(struct iio_buffer *buffer) 4739e69c935SLars-Peter Clausen { 4749e69c935SLars-Peter Clausen list_del_init(&buffer->buffer_list); 4759e69c935SLars-Peter Clausen iio_buffer_put(buffer); 4769e69c935SLars-Peter Clausen } 4779e69c935SLars-Peter Clausen 478a87c82e4SLars-Peter Clausen void iio_disable_all_buffers(struct iio_dev *indio_dev) 479a87c82e4SLars-Peter Clausen { 480a87c82e4SLars-Peter Clausen struct iio_buffer *buffer, *_buffer; 481a87c82e4SLars-Peter Clausen 482a87c82e4SLars-Peter Clausen if (list_empty(&indio_dev->buffer_list)) 483a87c82e4SLars-Peter Clausen return; 484a87c82e4SLars-Peter Clausen 485a87c82e4SLars-Peter Clausen if (indio_dev->setup_ops->predisable) 486a87c82e4SLars-Peter Clausen indio_dev->setup_ops->predisable(indio_dev); 487a87c82e4SLars-Peter Clausen 488a87c82e4SLars-Peter Clausen list_for_each_entry_safe(buffer, _buffer, 489a87c82e4SLars-Peter Clausen &indio_dev->buffer_list, buffer_list) 4909e69c935SLars-Peter Clausen iio_buffer_deactivate(buffer); 491a87c82e4SLars-Peter Clausen 492a87c82e4SLars-Peter Clausen indio_dev->currentmode = INDIO_DIRECT_MODE; 493a87c82e4SLars-Peter Clausen if (indio_dev->setup_ops->postdisable) 494a87c82e4SLars-Peter Clausen indio_dev->setup_ops->postdisable(indio_dev); 495e086ed76SLars-Peter Clausen 496e086ed76SLars-Peter Clausen if (indio_dev->available_scan_masks == NULL) 497e086ed76SLars-Peter Clausen kfree(indio_dev->active_scan_mask); 498a87c82e4SLars-Peter Clausen } 499a87c82e4SLars-Peter Clausen 5008e050996SLars-Peter Clausen static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev, 5018e050996SLars-Peter Clausen struct iio_buffer *buffer) 5028e050996SLars-Peter Clausen { 5038e050996SLars-Peter Clausen unsigned int bytes; 5048e050996SLars-Peter Clausen 5058e050996SLars-Peter Clausen if (!buffer->access->set_bytes_per_datum) 5068e050996SLars-Peter Clausen return; 5078e050996SLars-Peter Clausen 5088e050996SLars-Peter Clausen bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask, 5098e050996SLars-Peter Clausen buffer->scan_timestamp); 5108e050996SLars-Peter Clausen 5118e050996SLars-Peter Clausen buffer->access->set_bytes_per_datum(buffer, bytes); 5128e050996SLars-Peter Clausen } 5138e050996SLars-Peter Clausen 514a9519456SLars-Peter Clausen static int __iio_update_buffers(struct iio_dev *indio_dev, 51584b36ce5SJonathan Cameron struct iio_buffer *insert_buffer, 51684b36ce5SJonathan Cameron struct iio_buffer *remove_buffer) 517a980e046SJonathan Cameron { 51884b36ce5SJonathan Cameron int ret; 51984b36ce5SJonathan Cameron int success = 0; 52084b36ce5SJonathan Cameron struct iio_buffer *buffer; 52184b36ce5SJonathan Cameron unsigned long *compound_mask; 52284b36ce5SJonathan Cameron const unsigned long *old_mask; 523a980e046SJonathan Cameron 52484b36ce5SJonathan Cameron /* Wind down existing buffers - iff there are any */ 52584b36ce5SJonathan Cameron if (!list_empty(&indio_dev->buffer_list)) { 52684b36ce5SJonathan Cameron if (indio_dev->setup_ops->predisable) { 52784b36ce5SJonathan Cameron ret = indio_dev->setup_ops->predisable(indio_dev); 52884b36ce5SJonathan Cameron if (ret) 52992825ff9SHartmut Knaack return ret; 53084b36ce5SJonathan Cameron } 53184b36ce5SJonathan Cameron indio_dev->currentmode = INDIO_DIRECT_MODE; 53284b36ce5SJonathan Cameron if (indio_dev->setup_ops->postdisable) { 53384b36ce5SJonathan Cameron ret = indio_dev->setup_ops->postdisable(indio_dev); 53484b36ce5SJonathan Cameron if (ret) 53592825ff9SHartmut Knaack return ret; 53684b36ce5SJonathan Cameron } 53784b36ce5SJonathan Cameron } 53884b36ce5SJonathan Cameron /* Keep a copy of current setup to allow roll back */ 53984b36ce5SJonathan Cameron old_mask = indio_dev->active_scan_mask; 54084b36ce5SJonathan Cameron if (!indio_dev->available_scan_masks) 54184b36ce5SJonathan Cameron indio_dev->active_scan_mask = NULL; 54284b36ce5SJonathan Cameron 54384b36ce5SJonathan Cameron if (remove_buffer) 5449e69c935SLars-Peter Clausen iio_buffer_deactivate(remove_buffer); 54584b36ce5SJonathan Cameron if (insert_buffer) 5469e69c935SLars-Peter Clausen iio_buffer_activate(indio_dev, insert_buffer); 54784b36ce5SJonathan Cameron 54884b36ce5SJonathan Cameron /* If no buffers in list, we are done */ 54984b36ce5SJonathan Cameron if (list_empty(&indio_dev->buffer_list)) { 55084b36ce5SJonathan Cameron indio_dev->currentmode = INDIO_DIRECT_MODE; 55184b36ce5SJonathan Cameron if (indio_dev->available_scan_masks == NULL) 55284b36ce5SJonathan Cameron kfree(old_mask); 55384b36ce5SJonathan Cameron return 0; 55484b36ce5SJonathan Cameron } 555a980e046SJonathan Cameron 556a980e046SJonathan Cameron /* What scan mask do we actually have? */ 55784b36ce5SJonathan Cameron compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), 55884b36ce5SJonathan Cameron sizeof(long), GFP_KERNEL); 55984b36ce5SJonathan Cameron if (compound_mask == NULL) { 56084b36ce5SJonathan Cameron if (indio_dev->available_scan_masks == NULL) 56184b36ce5SJonathan Cameron kfree(old_mask); 56284b36ce5SJonathan Cameron return -ENOMEM; 56384b36ce5SJonathan Cameron } 56484b36ce5SJonathan Cameron indio_dev->scan_timestamp = 0; 56584b36ce5SJonathan Cameron 56684b36ce5SJonathan Cameron list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 56784b36ce5SJonathan Cameron bitmap_or(compound_mask, compound_mask, buffer->scan_mask, 56884b36ce5SJonathan Cameron indio_dev->masklength); 56984b36ce5SJonathan Cameron indio_dev->scan_timestamp |= buffer->scan_timestamp; 57084b36ce5SJonathan Cameron } 57184b36ce5SJonathan Cameron if (indio_dev->available_scan_masks) { 572a980e046SJonathan Cameron indio_dev->active_scan_mask = 573a980e046SJonathan Cameron iio_scan_mask_match(indio_dev->available_scan_masks, 574a980e046SJonathan Cameron indio_dev->masklength, 57584b36ce5SJonathan Cameron compound_mask); 57684b36ce5SJonathan Cameron if (indio_dev->active_scan_mask == NULL) { 57784b36ce5SJonathan Cameron /* 57884b36ce5SJonathan Cameron * Roll back. 57984b36ce5SJonathan Cameron * Note can only occur when adding a buffer. 58084b36ce5SJonathan Cameron */ 5819e69c935SLars-Peter Clausen iio_buffer_deactivate(insert_buffer); 582d66e0452SPeter Meerwald if (old_mask) { 58384b36ce5SJonathan Cameron indio_dev->active_scan_mask = old_mask; 58484b36ce5SJonathan Cameron success = -EINVAL; 58584b36ce5SJonathan Cameron } 586d66e0452SPeter Meerwald else { 587d66e0452SPeter Meerwald kfree(compound_mask); 588d66e0452SPeter Meerwald ret = -EINVAL; 58992825ff9SHartmut Knaack return ret; 590d66e0452SPeter Meerwald } 591d66e0452SPeter Meerwald } 59284b36ce5SJonathan Cameron } else { 59384b36ce5SJonathan Cameron indio_dev->active_scan_mask = compound_mask; 59484b36ce5SJonathan Cameron } 595aff1eb4eSLars-Peter Clausen 596a980e046SJonathan Cameron iio_update_demux(indio_dev); 597a980e046SJonathan Cameron 59884b36ce5SJonathan Cameron /* Wind up again */ 59984b36ce5SJonathan Cameron if (indio_dev->setup_ops->preenable) { 60084b36ce5SJonathan Cameron ret = indio_dev->setup_ops->preenable(indio_dev); 60184b36ce5SJonathan Cameron if (ret) { 60284b36ce5SJonathan Cameron printk(KERN_ERR 603bec1889dSMichał Mirosław "Buffer not started: buffer preenable failed (%d)\n", ret); 60484b36ce5SJonathan Cameron goto error_remove_inserted; 60584b36ce5SJonathan Cameron } 60684b36ce5SJonathan Cameron } 60784b36ce5SJonathan Cameron indio_dev->scan_bytes = 60884b36ce5SJonathan Cameron iio_compute_scan_bytes(indio_dev, 60984b36ce5SJonathan Cameron indio_dev->active_scan_mask, 61084b36ce5SJonathan Cameron indio_dev->scan_timestamp); 6118e050996SLars-Peter Clausen list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 6128e050996SLars-Peter Clausen iio_buffer_update_bytes_per_datum(indio_dev, buffer); 61384b36ce5SJonathan Cameron if (buffer->access->request_update) { 61484b36ce5SJonathan Cameron ret = buffer->access->request_update(buffer); 61584b36ce5SJonathan Cameron if (ret) { 61684b36ce5SJonathan Cameron printk(KERN_INFO 617bec1889dSMichał Mirosław "Buffer not started: buffer parameter update failed (%d)\n", ret); 61884b36ce5SJonathan Cameron goto error_run_postdisable; 61984b36ce5SJonathan Cameron } 62084b36ce5SJonathan Cameron } 6218e050996SLars-Peter Clausen } 62284b36ce5SJonathan Cameron if (indio_dev->info->update_scan_mode) { 62384b36ce5SJonathan Cameron ret = indio_dev->info 624a980e046SJonathan Cameron ->update_scan_mode(indio_dev, 625a980e046SJonathan Cameron indio_dev->active_scan_mask); 62684b36ce5SJonathan Cameron if (ret < 0) { 627bec1889dSMichał Mirosław printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret); 62884b36ce5SJonathan Cameron goto error_run_postdisable; 62984b36ce5SJonathan Cameron } 63084b36ce5SJonathan Cameron } 63184b36ce5SJonathan Cameron /* Definitely possible for devices to support both of these. */ 63284b36ce5SJonathan Cameron if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) { 63384b36ce5SJonathan Cameron if (!indio_dev->trig) { 63484b36ce5SJonathan Cameron printk(KERN_INFO "Buffer not started: no trigger\n"); 63584b36ce5SJonathan Cameron ret = -EINVAL; 63684b36ce5SJonathan Cameron /* Can only occur on first buffer */ 63784b36ce5SJonathan Cameron goto error_run_postdisable; 63884b36ce5SJonathan Cameron } 63984b36ce5SJonathan Cameron indio_dev->currentmode = INDIO_BUFFER_TRIGGERED; 64084b36ce5SJonathan Cameron } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) { 64184b36ce5SJonathan Cameron indio_dev->currentmode = INDIO_BUFFER_HARDWARE; 6429572588cSPeter Meerwald } else { /* Should never be reached */ 64384b36ce5SJonathan Cameron ret = -EINVAL; 64484b36ce5SJonathan Cameron goto error_run_postdisable; 64584b36ce5SJonathan Cameron } 64684b36ce5SJonathan Cameron 64784b36ce5SJonathan Cameron if (indio_dev->setup_ops->postenable) { 64884b36ce5SJonathan Cameron ret = indio_dev->setup_ops->postenable(indio_dev); 64984b36ce5SJonathan Cameron if (ret) { 65084b36ce5SJonathan Cameron printk(KERN_INFO 651bec1889dSMichał Mirosław "Buffer not started: postenable failed (%d)\n", ret); 65284b36ce5SJonathan Cameron indio_dev->currentmode = INDIO_DIRECT_MODE; 65384b36ce5SJonathan Cameron if (indio_dev->setup_ops->postdisable) 65484b36ce5SJonathan Cameron indio_dev->setup_ops->postdisable(indio_dev); 65584b36ce5SJonathan Cameron goto error_disable_all_buffers; 65684b36ce5SJonathan Cameron } 65784b36ce5SJonathan Cameron } 65884b36ce5SJonathan Cameron 65984b36ce5SJonathan Cameron if (indio_dev->available_scan_masks) 66084b36ce5SJonathan Cameron kfree(compound_mask); 66184b36ce5SJonathan Cameron else 66284b36ce5SJonathan Cameron kfree(old_mask); 66384b36ce5SJonathan Cameron 66484b36ce5SJonathan Cameron return success; 66584b36ce5SJonathan Cameron 66684b36ce5SJonathan Cameron error_disable_all_buffers: 66784b36ce5SJonathan Cameron indio_dev->currentmode = INDIO_DIRECT_MODE; 66884b36ce5SJonathan Cameron error_run_postdisable: 66984b36ce5SJonathan Cameron if (indio_dev->setup_ops->postdisable) 67084b36ce5SJonathan Cameron indio_dev->setup_ops->postdisable(indio_dev); 67184b36ce5SJonathan Cameron error_remove_inserted: 67284b36ce5SJonathan Cameron if (insert_buffer) 6739e69c935SLars-Peter Clausen iio_buffer_deactivate(insert_buffer); 67484b36ce5SJonathan Cameron indio_dev->active_scan_mask = old_mask; 67584b36ce5SJonathan Cameron kfree(compound_mask); 67684b36ce5SJonathan Cameron return ret; 67784b36ce5SJonathan Cameron } 678a9519456SLars-Peter Clausen 679a9519456SLars-Peter Clausen int iio_update_buffers(struct iio_dev *indio_dev, 680a9519456SLars-Peter Clausen struct iio_buffer *insert_buffer, 681a9519456SLars-Peter Clausen struct iio_buffer *remove_buffer) 682a9519456SLars-Peter Clausen { 683a9519456SLars-Peter Clausen int ret; 684a9519456SLars-Peter Clausen 6853909fab5SLars-Peter Clausen if (insert_buffer == remove_buffer) 6863909fab5SLars-Peter Clausen return 0; 6873909fab5SLars-Peter Clausen 688a9519456SLars-Peter Clausen mutex_lock(&indio_dev->info_exist_lock); 689a9519456SLars-Peter Clausen mutex_lock(&indio_dev->mlock); 690a9519456SLars-Peter Clausen 6913909fab5SLars-Peter Clausen if (insert_buffer && iio_buffer_is_active(insert_buffer)) 6923909fab5SLars-Peter Clausen insert_buffer = NULL; 6933909fab5SLars-Peter Clausen 6943909fab5SLars-Peter Clausen if (remove_buffer && !iio_buffer_is_active(remove_buffer)) 6953909fab5SLars-Peter Clausen remove_buffer = NULL; 6963909fab5SLars-Peter Clausen 6973909fab5SLars-Peter Clausen if (!insert_buffer && !remove_buffer) { 6983909fab5SLars-Peter Clausen ret = 0; 6993909fab5SLars-Peter Clausen goto out_unlock; 7003909fab5SLars-Peter Clausen } 7013909fab5SLars-Peter Clausen 702a9519456SLars-Peter Clausen if (indio_dev->info == NULL) { 703a9519456SLars-Peter Clausen ret = -ENODEV; 704a9519456SLars-Peter Clausen goto out_unlock; 705a9519456SLars-Peter Clausen } 706a9519456SLars-Peter Clausen 707a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer); 708a9519456SLars-Peter Clausen 709a9519456SLars-Peter Clausen out_unlock: 710a9519456SLars-Peter Clausen mutex_unlock(&indio_dev->mlock); 711a9519456SLars-Peter Clausen mutex_unlock(&indio_dev->info_exist_lock); 712a9519456SLars-Peter Clausen 713a9519456SLars-Peter Clausen return ret; 714a9519456SLars-Peter Clausen } 71584b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_update_buffers); 71684b36ce5SJonathan Cameron 71708e7e0adSLars-Peter Clausen static ssize_t iio_buffer_store_enable(struct device *dev, 71884b36ce5SJonathan Cameron struct device_attribute *attr, 71984b36ce5SJonathan Cameron const char *buf, 72084b36ce5SJonathan Cameron size_t len) 72184b36ce5SJonathan Cameron { 72284b36ce5SJonathan Cameron int ret; 72384b36ce5SJonathan Cameron bool requested_state; 72484b36ce5SJonathan Cameron struct iio_dev *indio_dev = dev_to_iio_dev(dev); 72584b36ce5SJonathan Cameron bool inlist; 72684b36ce5SJonathan Cameron 72784b36ce5SJonathan Cameron ret = strtobool(buf, &requested_state); 72884b36ce5SJonathan Cameron if (ret < 0) 72984b36ce5SJonathan Cameron return ret; 73084b36ce5SJonathan Cameron 73184b36ce5SJonathan Cameron mutex_lock(&indio_dev->mlock); 73284b36ce5SJonathan Cameron 73384b36ce5SJonathan Cameron /* Find out if it is in the list */ 734705ee2c9SLars-Peter Clausen inlist = iio_buffer_is_active(indio_dev->buffer); 73584b36ce5SJonathan Cameron /* Already in desired state */ 73684b36ce5SJonathan Cameron if (inlist == requested_state) 73784b36ce5SJonathan Cameron goto done; 73884b36ce5SJonathan Cameron 73984b36ce5SJonathan Cameron if (requested_state) 740a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, 74184b36ce5SJonathan Cameron indio_dev->buffer, NULL); 74284b36ce5SJonathan Cameron else 743a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, 74484b36ce5SJonathan Cameron NULL, indio_dev->buffer); 74584b36ce5SJonathan Cameron 74684b36ce5SJonathan Cameron if (ret < 0) 74784b36ce5SJonathan Cameron goto done; 74884b36ce5SJonathan Cameron done: 74984b36ce5SJonathan Cameron mutex_unlock(&indio_dev->mlock); 75084b36ce5SJonathan Cameron return (ret < 0) ? ret : len; 75184b36ce5SJonathan Cameron } 75284b36ce5SJonathan Cameron 753d967cb6bSLars-Peter Clausen static const char * const iio_scan_elements_group_name = "scan_elements"; 754d967cb6bSLars-Peter Clausen 75508e7e0adSLars-Peter Clausen static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length, 75608e7e0adSLars-Peter Clausen iio_buffer_write_length); 7578d92db28SLars-Peter Clausen static struct device_attribute dev_attr_length_ro = __ATTR(length, 7588d92db28SLars-Peter Clausen S_IRUGO, iio_buffer_read_length, NULL); 75908e7e0adSLars-Peter Clausen static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, 76008e7e0adSLars-Peter Clausen iio_buffer_show_enable, iio_buffer_store_enable); 76108e7e0adSLars-Peter Clausen 762d967cb6bSLars-Peter Clausen int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev) 763d967cb6bSLars-Peter Clausen { 764d967cb6bSLars-Peter Clausen struct iio_dev_attr *p; 765d967cb6bSLars-Peter Clausen struct attribute **attr; 766d967cb6bSLars-Peter Clausen struct iio_buffer *buffer = indio_dev->buffer; 767d967cb6bSLars-Peter Clausen int ret, i, attrn, attrcount, attrcount_orig = 0; 768d967cb6bSLars-Peter Clausen const struct iio_chan_spec *channels; 769d967cb6bSLars-Peter Clausen 770d967cb6bSLars-Peter Clausen if (!buffer) 771d967cb6bSLars-Peter Clausen return 0; 772d967cb6bSLars-Peter Clausen 77308e7e0adSLars-Peter Clausen attrcount = 0; 77408e7e0adSLars-Peter Clausen if (buffer->attrs) { 77508e7e0adSLars-Peter Clausen while (buffer->attrs[attrcount] != NULL) 77608e7e0adSLars-Peter Clausen attrcount++; 77708e7e0adSLars-Peter Clausen } 77808e7e0adSLars-Peter Clausen 77908e7e0adSLars-Peter Clausen buffer->buffer_group.name = "buffer"; 78008e7e0adSLars-Peter Clausen buffer->buffer_group.attrs = kcalloc(attrcount + 3, 78108e7e0adSLars-Peter Clausen sizeof(*buffer->buffer_group.attrs), GFP_KERNEL); 78208e7e0adSLars-Peter Clausen if (!buffer->buffer_group.attrs) 78308e7e0adSLars-Peter Clausen return -ENOMEM; 78408e7e0adSLars-Peter Clausen 7858d92db28SLars-Peter Clausen if (buffer->access->set_length) 78608e7e0adSLars-Peter Clausen buffer->buffer_group.attrs[0] = &dev_attr_length.attr; 7878d92db28SLars-Peter Clausen else 7888d92db28SLars-Peter Clausen buffer->buffer_group.attrs[0] = &dev_attr_length_ro.attr; 78908e7e0adSLars-Peter Clausen buffer->buffer_group.attrs[1] = &dev_attr_enable.attr; 79008e7e0adSLars-Peter Clausen if (buffer->attrs) 79108e7e0adSLars-Peter Clausen memcpy(&buffer->buffer_group.attrs[2], buffer->attrs, 79208e7e0adSLars-Peter Clausen sizeof(*&buffer->buffer_group.attrs) * (attrcount - 2)); 79308e7e0adSLars-Peter Clausen buffer->buffer_group.attrs[attrcount+2] = NULL; 79408e7e0adSLars-Peter Clausen 79508e7e0adSLars-Peter Clausen indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group; 79608e7e0adSLars-Peter Clausen 797d967cb6bSLars-Peter Clausen if (buffer->scan_el_attrs != NULL) { 798d967cb6bSLars-Peter Clausen attr = buffer->scan_el_attrs->attrs; 799d967cb6bSLars-Peter Clausen while (*attr++ != NULL) 800d967cb6bSLars-Peter Clausen attrcount_orig++; 801d967cb6bSLars-Peter Clausen } 802d967cb6bSLars-Peter Clausen attrcount = attrcount_orig; 803d967cb6bSLars-Peter Clausen INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list); 804d967cb6bSLars-Peter Clausen channels = indio_dev->channels; 805d967cb6bSLars-Peter Clausen if (channels) { 806d967cb6bSLars-Peter Clausen /* new magic */ 807d967cb6bSLars-Peter Clausen for (i = 0; i < indio_dev->num_channels; i++) { 808d967cb6bSLars-Peter Clausen if (channels[i].scan_index < 0) 809d967cb6bSLars-Peter Clausen continue; 810d967cb6bSLars-Peter Clausen 811d967cb6bSLars-Peter Clausen /* Establish necessary mask length */ 812d967cb6bSLars-Peter Clausen if (channels[i].scan_index > 813d967cb6bSLars-Peter Clausen (int)indio_dev->masklength - 1) 814d967cb6bSLars-Peter Clausen indio_dev->masklength 815d967cb6bSLars-Peter Clausen = channels[i].scan_index + 1; 816d967cb6bSLars-Peter Clausen 817d967cb6bSLars-Peter Clausen ret = iio_buffer_add_channel_sysfs(indio_dev, 818d967cb6bSLars-Peter Clausen &channels[i]); 819d967cb6bSLars-Peter Clausen if (ret < 0) 820d967cb6bSLars-Peter Clausen goto error_cleanup_dynamic; 821d967cb6bSLars-Peter Clausen attrcount += ret; 822d967cb6bSLars-Peter Clausen if (channels[i].type == IIO_TIMESTAMP) 823d967cb6bSLars-Peter Clausen indio_dev->scan_index_timestamp = 824d967cb6bSLars-Peter Clausen channels[i].scan_index; 825d967cb6bSLars-Peter Clausen } 826d967cb6bSLars-Peter Clausen if (indio_dev->masklength && buffer->scan_mask == NULL) { 827d967cb6bSLars-Peter Clausen buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), 828d967cb6bSLars-Peter Clausen sizeof(*buffer->scan_mask), 829d967cb6bSLars-Peter Clausen GFP_KERNEL); 830d967cb6bSLars-Peter Clausen if (buffer->scan_mask == NULL) { 831d967cb6bSLars-Peter Clausen ret = -ENOMEM; 832d967cb6bSLars-Peter Clausen goto error_cleanup_dynamic; 833d967cb6bSLars-Peter Clausen } 834d967cb6bSLars-Peter Clausen } 835d967cb6bSLars-Peter Clausen } 836d967cb6bSLars-Peter Clausen 837d967cb6bSLars-Peter Clausen buffer->scan_el_group.name = iio_scan_elements_group_name; 838d967cb6bSLars-Peter Clausen 839d967cb6bSLars-Peter Clausen buffer->scan_el_group.attrs = kcalloc(attrcount + 1, 840d967cb6bSLars-Peter Clausen sizeof(buffer->scan_el_group.attrs[0]), 841d967cb6bSLars-Peter Clausen GFP_KERNEL); 842d967cb6bSLars-Peter Clausen if (buffer->scan_el_group.attrs == NULL) { 843d967cb6bSLars-Peter Clausen ret = -ENOMEM; 844d967cb6bSLars-Peter Clausen goto error_free_scan_mask; 845d967cb6bSLars-Peter Clausen } 846d967cb6bSLars-Peter Clausen if (buffer->scan_el_attrs) 847d967cb6bSLars-Peter Clausen memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs, 848d967cb6bSLars-Peter Clausen sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig); 849d967cb6bSLars-Peter Clausen attrn = attrcount_orig; 850d967cb6bSLars-Peter Clausen 851d967cb6bSLars-Peter Clausen list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l) 852d967cb6bSLars-Peter Clausen buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr; 853d967cb6bSLars-Peter Clausen indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group; 854d967cb6bSLars-Peter Clausen 855d967cb6bSLars-Peter Clausen return 0; 856d967cb6bSLars-Peter Clausen 857d967cb6bSLars-Peter Clausen error_free_scan_mask: 858d967cb6bSLars-Peter Clausen kfree(buffer->scan_mask); 859d967cb6bSLars-Peter Clausen error_cleanup_dynamic: 860d967cb6bSLars-Peter Clausen iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list); 86108e7e0adSLars-Peter Clausen kfree(indio_dev->buffer->buffer_group.attrs); 862d967cb6bSLars-Peter Clausen 863d967cb6bSLars-Peter Clausen return ret; 864d967cb6bSLars-Peter Clausen } 865d967cb6bSLars-Peter Clausen 866d967cb6bSLars-Peter Clausen void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev) 867d967cb6bSLars-Peter Clausen { 868d967cb6bSLars-Peter Clausen if (!indio_dev->buffer) 869d967cb6bSLars-Peter Clausen return; 870d967cb6bSLars-Peter Clausen 871d967cb6bSLars-Peter Clausen kfree(indio_dev->buffer->scan_mask); 87208e7e0adSLars-Peter Clausen kfree(indio_dev->buffer->buffer_group.attrs); 873d967cb6bSLars-Peter Clausen kfree(indio_dev->buffer->scan_el_group.attrs); 874d967cb6bSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list); 875d967cb6bSLars-Peter Clausen } 876d967cb6bSLars-Peter Clausen 877a980e046SJonathan Cameron /** 87881636632SLars-Peter Clausen * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected 87981636632SLars-Peter Clausen * @indio_dev: the iio device 88081636632SLars-Peter Clausen * @mask: scan mask to be checked 88181636632SLars-Peter Clausen * 88281636632SLars-Peter Clausen * Return true if exactly one bit is set in the scan mask, false otherwise. It 88381636632SLars-Peter Clausen * can be used for devices where only one channel can be active for sampling at 88481636632SLars-Peter Clausen * a time. 88581636632SLars-Peter Clausen */ 88681636632SLars-Peter Clausen bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev, 88781636632SLars-Peter Clausen const unsigned long *mask) 88881636632SLars-Peter Clausen { 88981636632SLars-Peter Clausen return bitmap_weight(mask, indio_dev->masklength) == 1; 89081636632SLars-Peter Clausen } 89181636632SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot); 89281636632SLars-Peter Clausen 893a980e046SJonathan Cameron int iio_scan_mask_query(struct iio_dev *indio_dev, 894a980e046SJonathan Cameron struct iio_buffer *buffer, int bit) 895a980e046SJonathan Cameron { 896a980e046SJonathan Cameron if (bit > indio_dev->masklength) 897a980e046SJonathan Cameron return -EINVAL; 898a980e046SJonathan Cameron 899a980e046SJonathan Cameron if (!buffer->scan_mask) 900a980e046SJonathan Cameron return 0; 901a980e046SJonathan Cameron 9022076a20fSAlec Berg /* Ensure return value is 0 or 1. */ 9032076a20fSAlec Berg return !!test_bit(bit, buffer->scan_mask); 904a980e046SJonathan Cameron }; 905a980e046SJonathan Cameron EXPORT_SYMBOL_GPL(iio_scan_mask_query); 906a980e046SJonathan Cameron 907a980e046SJonathan Cameron /** 908a980e046SJonathan Cameron * struct iio_demux_table() - table describing demux memcpy ops 909a980e046SJonathan Cameron * @from: index to copy from 910a980e046SJonathan Cameron * @to: index to copy to 911a980e046SJonathan Cameron * @length: how many bytes to copy 912a980e046SJonathan Cameron * @l: list head used for management 913a980e046SJonathan Cameron */ 914a980e046SJonathan Cameron struct iio_demux_table { 915a980e046SJonathan Cameron unsigned from; 916a980e046SJonathan Cameron unsigned to; 917a980e046SJonathan Cameron unsigned length; 918a980e046SJonathan Cameron struct list_head l; 919a980e046SJonathan Cameron }; 920a980e046SJonathan Cameron 9215d65d920SLars-Peter Clausen static const void *iio_demux(struct iio_buffer *buffer, 9225d65d920SLars-Peter Clausen const void *datain) 923a980e046SJonathan Cameron { 924a980e046SJonathan Cameron struct iio_demux_table *t; 925a980e046SJonathan Cameron 926a980e046SJonathan Cameron if (list_empty(&buffer->demux_list)) 927a980e046SJonathan Cameron return datain; 928a980e046SJonathan Cameron list_for_each_entry(t, &buffer->demux_list, l) 929a980e046SJonathan Cameron memcpy(buffer->demux_bounce + t->to, 930a980e046SJonathan Cameron datain + t->from, t->length); 931a980e046SJonathan Cameron 932a980e046SJonathan Cameron return buffer->demux_bounce; 933a980e046SJonathan Cameron } 934a980e046SJonathan Cameron 9355d65d920SLars-Peter Clausen static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data) 936a980e046SJonathan Cameron { 9375d65d920SLars-Peter Clausen const void *dataout = iio_demux(buffer, data); 938a980e046SJonathan Cameron 939ce56ade6SLars-Peter Clausen return buffer->access->store_to(buffer, dataout); 940a980e046SJonathan Cameron } 941a980e046SJonathan Cameron 942a980e046SJonathan Cameron static void iio_buffer_demux_free(struct iio_buffer *buffer) 943a980e046SJonathan Cameron { 944a980e046SJonathan Cameron struct iio_demux_table *p, *q; 945a980e046SJonathan Cameron list_for_each_entry_safe(p, q, &buffer->demux_list, l) { 946a980e046SJonathan Cameron list_del(&p->l); 947a980e046SJonathan Cameron kfree(p); 948a980e046SJonathan Cameron } 949a980e046SJonathan Cameron } 950a980e046SJonathan Cameron 95184b36ce5SJonathan Cameron 9525d65d920SLars-Peter Clausen int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) 95384b36ce5SJonathan Cameron { 95484b36ce5SJonathan Cameron int ret; 95584b36ce5SJonathan Cameron struct iio_buffer *buf; 95684b36ce5SJonathan Cameron 95784b36ce5SJonathan Cameron list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) { 95884b36ce5SJonathan Cameron ret = iio_push_to_buffer(buf, data); 95984b36ce5SJonathan Cameron if (ret < 0) 96084b36ce5SJonathan Cameron return ret; 96184b36ce5SJonathan Cameron } 96284b36ce5SJonathan Cameron 96384b36ce5SJonathan Cameron return 0; 96484b36ce5SJonathan Cameron } 96584b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_push_to_buffers); 96684b36ce5SJonathan Cameron 967cbe88bccSLars-Peter Clausen static int iio_buffer_add_demux(struct iio_buffer *buffer, 968cbe88bccSLars-Peter Clausen struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc, 969cbe88bccSLars-Peter Clausen unsigned int length) 970cbe88bccSLars-Peter Clausen { 971cbe88bccSLars-Peter Clausen 972cbe88bccSLars-Peter Clausen if (*p && (*p)->from + (*p)->length == in_loc && 973cbe88bccSLars-Peter Clausen (*p)->to + (*p)->length == out_loc) { 974cbe88bccSLars-Peter Clausen (*p)->length += length; 975cbe88bccSLars-Peter Clausen } else { 9767cdca178SJonathan Cameron *p = kmalloc(sizeof(**p), GFP_KERNEL); 977cbe88bccSLars-Peter Clausen if (*p == NULL) 978cbe88bccSLars-Peter Clausen return -ENOMEM; 979cbe88bccSLars-Peter Clausen (*p)->from = in_loc; 980cbe88bccSLars-Peter Clausen (*p)->to = out_loc; 981cbe88bccSLars-Peter Clausen (*p)->length = length; 982cbe88bccSLars-Peter Clausen list_add_tail(&(*p)->l, &buffer->demux_list); 983cbe88bccSLars-Peter Clausen } 984cbe88bccSLars-Peter Clausen 985cbe88bccSLars-Peter Clausen return 0; 986cbe88bccSLars-Peter Clausen } 987cbe88bccSLars-Peter Clausen 98884b36ce5SJonathan Cameron static int iio_buffer_update_demux(struct iio_dev *indio_dev, 98984b36ce5SJonathan Cameron struct iio_buffer *buffer) 990a980e046SJonathan Cameron { 991a980e046SJonathan Cameron const struct iio_chan_spec *ch; 992a980e046SJonathan Cameron int ret, in_ind = -1, out_ind, length; 993a980e046SJonathan Cameron unsigned in_loc = 0, out_loc = 0; 994cbe88bccSLars-Peter Clausen struct iio_demux_table *p = NULL; 995a980e046SJonathan Cameron 996a980e046SJonathan Cameron /* Clear out any old demux */ 997a980e046SJonathan Cameron iio_buffer_demux_free(buffer); 998a980e046SJonathan Cameron kfree(buffer->demux_bounce); 999a980e046SJonathan Cameron buffer->demux_bounce = NULL; 1000a980e046SJonathan Cameron 1001a980e046SJonathan Cameron /* First work out which scan mode we will actually have */ 1002a980e046SJonathan Cameron if (bitmap_equal(indio_dev->active_scan_mask, 1003a980e046SJonathan Cameron buffer->scan_mask, 1004a980e046SJonathan Cameron indio_dev->masklength)) 1005a980e046SJonathan Cameron return 0; 1006a980e046SJonathan Cameron 1007a980e046SJonathan Cameron /* Now we have the two masks, work from least sig and build up sizes */ 1008a980e046SJonathan Cameron for_each_set_bit(out_ind, 100961bd55ceSLars-Peter Clausen buffer->scan_mask, 1010a980e046SJonathan Cameron indio_dev->masklength) { 1011a980e046SJonathan Cameron in_ind = find_next_bit(indio_dev->active_scan_mask, 1012a980e046SJonathan Cameron indio_dev->masklength, 1013a980e046SJonathan Cameron in_ind + 1); 1014a980e046SJonathan Cameron while (in_ind != out_ind) { 1015a980e046SJonathan Cameron in_ind = find_next_bit(indio_dev->active_scan_mask, 1016a980e046SJonathan Cameron indio_dev->masklength, 1017a980e046SJonathan Cameron in_ind + 1); 1018a980e046SJonathan Cameron ch = iio_find_channel_from_si(indio_dev, in_ind); 10190ee8546aSSrinivas Pandruvada if (ch->scan_type.repeat > 1) 10200ee8546aSSrinivas Pandruvada length = ch->scan_type.storagebits / 8 * 10210ee8546aSSrinivas Pandruvada ch->scan_type.repeat; 10220ee8546aSSrinivas Pandruvada else 1023a980e046SJonathan Cameron length = ch->scan_type.storagebits / 8; 1024a980e046SJonathan Cameron /* Make sure we are aligned */ 102561072dbcSLars-Peter Clausen in_loc = roundup(in_loc, length) + length; 1026a980e046SJonathan Cameron } 1027a980e046SJonathan Cameron ch = iio_find_channel_from_si(indio_dev, in_ind); 10280ee8546aSSrinivas Pandruvada if (ch->scan_type.repeat > 1) 10290ee8546aSSrinivas Pandruvada length = ch->scan_type.storagebits / 8 * 10300ee8546aSSrinivas Pandruvada ch->scan_type.repeat; 10310ee8546aSSrinivas Pandruvada else 1032a980e046SJonathan Cameron length = ch->scan_type.storagebits / 8; 103361072dbcSLars-Peter Clausen out_loc = roundup(out_loc, length); 103461072dbcSLars-Peter Clausen in_loc = roundup(in_loc, length); 1035cbe88bccSLars-Peter Clausen ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); 1036cbe88bccSLars-Peter Clausen if (ret) 1037cbe88bccSLars-Peter Clausen goto error_clear_mux_table; 1038a980e046SJonathan Cameron out_loc += length; 1039a980e046SJonathan Cameron in_loc += length; 1040a980e046SJonathan Cameron } 1041a980e046SJonathan Cameron /* Relies on scan_timestamp being last */ 1042a980e046SJonathan Cameron if (buffer->scan_timestamp) { 1043a980e046SJonathan Cameron ch = iio_find_channel_from_si(indio_dev, 1044a980e046SJonathan Cameron indio_dev->scan_index_timestamp); 10450ee8546aSSrinivas Pandruvada if (ch->scan_type.repeat > 1) 10460ee8546aSSrinivas Pandruvada length = ch->scan_type.storagebits / 8 * 10470ee8546aSSrinivas Pandruvada ch->scan_type.repeat; 10480ee8546aSSrinivas Pandruvada else 1049a980e046SJonathan Cameron length = ch->scan_type.storagebits / 8; 105061072dbcSLars-Peter Clausen out_loc = roundup(out_loc, length); 105161072dbcSLars-Peter Clausen in_loc = roundup(in_loc, length); 1052cbe88bccSLars-Peter Clausen ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); 1053cbe88bccSLars-Peter Clausen if (ret) 1054cbe88bccSLars-Peter Clausen goto error_clear_mux_table; 1055a980e046SJonathan Cameron out_loc += length; 1056a980e046SJonathan Cameron in_loc += length; 1057a980e046SJonathan Cameron } 1058a980e046SJonathan Cameron buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL); 1059a980e046SJonathan Cameron if (buffer->demux_bounce == NULL) { 1060a980e046SJonathan Cameron ret = -ENOMEM; 1061a980e046SJonathan Cameron goto error_clear_mux_table; 1062a980e046SJonathan Cameron } 1063a980e046SJonathan Cameron return 0; 1064a980e046SJonathan Cameron 1065a980e046SJonathan Cameron error_clear_mux_table: 1066a980e046SJonathan Cameron iio_buffer_demux_free(buffer); 1067a980e046SJonathan Cameron 1068a980e046SJonathan Cameron return ret; 1069a980e046SJonathan Cameron } 107084b36ce5SJonathan Cameron 107184b36ce5SJonathan Cameron int iio_update_demux(struct iio_dev *indio_dev) 107284b36ce5SJonathan Cameron { 107384b36ce5SJonathan Cameron struct iio_buffer *buffer; 107484b36ce5SJonathan Cameron int ret; 107584b36ce5SJonathan Cameron 107684b36ce5SJonathan Cameron list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 107784b36ce5SJonathan Cameron ret = iio_buffer_update_demux(indio_dev, buffer); 107884b36ce5SJonathan Cameron if (ret < 0) 107984b36ce5SJonathan Cameron goto error_clear_mux_table; 108084b36ce5SJonathan Cameron } 108184b36ce5SJonathan Cameron return 0; 108284b36ce5SJonathan Cameron 108384b36ce5SJonathan Cameron error_clear_mux_table: 108484b36ce5SJonathan Cameron list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) 108584b36ce5SJonathan Cameron iio_buffer_demux_free(buffer); 108684b36ce5SJonathan Cameron 108784b36ce5SJonathan Cameron return ret; 108884b36ce5SJonathan Cameron } 1089a980e046SJonathan Cameron EXPORT_SYMBOL_GPL(iio_update_demux); 10909e69c935SLars-Peter Clausen 10919e69c935SLars-Peter Clausen /** 10929e69c935SLars-Peter Clausen * iio_buffer_release() - Free a buffer's resources 10939e69c935SLars-Peter Clausen * @ref: Pointer to the kref embedded in the iio_buffer struct 10949e69c935SLars-Peter Clausen * 10959e69c935SLars-Peter Clausen * This function is called when the last reference to the buffer has been 10969e69c935SLars-Peter Clausen * dropped. It will typically free all resources allocated by the buffer. Do not 10979e69c935SLars-Peter Clausen * call this function manually, always use iio_buffer_put() when done using a 10989e69c935SLars-Peter Clausen * buffer. 10999e69c935SLars-Peter Clausen */ 11009e69c935SLars-Peter Clausen static void iio_buffer_release(struct kref *ref) 11019e69c935SLars-Peter Clausen { 11029e69c935SLars-Peter Clausen struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref); 11039e69c935SLars-Peter Clausen 11049e69c935SLars-Peter Clausen buffer->access->release(buffer); 11059e69c935SLars-Peter Clausen } 11069e69c935SLars-Peter Clausen 11079e69c935SLars-Peter Clausen /** 11089e69c935SLars-Peter Clausen * iio_buffer_get() - Grab a reference to the buffer 11099e69c935SLars-Peter Clausen * @buffer: The buffer to grab a reference for, may be NULL 11109e69c935SLars-Peter Clausen * 11119e69c935SLars-Peter Clausen * Returns the pointer to the buffer that was passed into the function. 11129e69c935SLars-Peter Clausen */ 11139e69c935SLars-Peter Clausen struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer) 11149e69c935SLars-Peter Clausen { 11159e69c935SLars-Peter Clausen if (buffer) 11169e69c935SLars-Peter Clausen kref_get(&buffer->ref); 11179e69c935SLars-Peter Clausen 11189e69c935SLars-Peter Clausen return buffer; 11199e69c935SLars-Peter Clausen } 11209e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_get); 11219e69c935SLars-Peter Clausen 11229e69c935SLars-Peter Clausen /** 11239e69c935SLars-Peter Clausen * iio_buffer_put() - Release the reference to the buffer 11249e69c935SLars-Peter Clausen * @buffer: The buffer to release the reference for, may be NULL 11259e69c935SLars-Peter Clausen */ 11269e69c935SLars-Peter Clausen void iio_buffer_put(struct iio_buffer *buffer) 11279e69c935SLars-Peter Clausen { 11289e69c935SLars-Peter Clausen if (buffer) 11299e69c935SLars-Peter Clausen kref_put(&buffer->ref, iio_buffer_release); 11309e69c935SLars-Peter Clausen } 11319e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_put); 1132