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 386*08e7e0adSLars-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 393a980e046SJonathan Cameron if (buffer->access->get_length) 394a980e046SJonathan Cameron return sprintf(buf, "%d\n", 395a980e046SJonathan Cameron buffer->access->get_length(buffer)); 396a980e046SJonathan Cameron 397a980e046SJonathan Cameron return 0; 398a980e046SJonathan Cameron } 399a980e046SJonathan Cameron 400*08e7e0adSLars-Peter Clausen static ssize_t iio_buffer_write_length(struct device *dev, 401a980e046SJonathan Cameron struct device_attribute *attr, 402*08e7e0adSLars-Peter Clausen const char *buf, size_t len) 403a980e046SJonathan Cameron { 404e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 405a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 406948ad205SLars-Peter Clausen unsigned int val; 407948ad205SLars-Peter Clausen int ret; 408a980e046SJonathan Cameron 409948ad205SLars-Peter Clausen ret = kstrtouint(buf, 10, &val); 410a980e046SJonathan Cameron if (ret) 411a980e046SJonathan Cameron return ret; 412a980e046SJonathan Cameron 413a980e046SJonathan Cameron if (buffer->access->get_length) 414a980e046SJonathan Cameron if (val == buffer->access->get_length(buffer)) 415a980e046SJonathan Cameron return len; 416a980e046SJonathan Cameron 417a980e046SJonathan Cameron mutex_lock(&indio_dev->mlock); 418705ee2c9SLars-Peter Clausen if (iio_buffer_is_active(indio_dev->buffer)) { 419a980e046SJonathan Cameron ret = -EBUSY; 420a980e046SJonathan Cameron } else { 421a980e046SJonathan Cameron if (buffer->access->set_length) 422a980e046SJonathan Cameron buffer->access->set_length(buffer, val); 423a980e046SJonathan Cameron ret = 0; 424a980e046SJonathan Cameron } 425a980e046SJonathan Cameron mutex_unlock(&indio_dev->mlock); 426a980e046SJonathan Cameron 427a980e046SJonathan Cameron return ret ? ret : len; 428a980e046SJonathan Cameron } 429a980e046SJonathan Cameron 430*08e7e0adSLars-Peter Clausen static ssize_t iio_buffer_show_enable(struct device *dev, 431a980e046SJonathan Cameron struct device_attribute *attr, 432a980e046SJonathan Cameron char *buf) 433a980e046SJonathan Cameron { 434e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 435705ee2c9SLars-Peter Clausen return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer)); 436a980e046SJonathan Cameron } 437a980e046SJonathan Cameron 438183f4173SPeter Meerwald static int iio_compute_scan_bytes(struct iio_dev *indio_dev, 439183f4173SPeter Meerwald const unsigned long *mask, bool timestamp) 440a980e046SJonathan Cameron { 441a980e046SJonathan Cameron const struct iio_chan_spec *ch; 442a980e046SJonathan Cameron unsigned bytes = 0; 443a980e046SJonathan Cameron int length, i; 444a980e046SJonathan Cameron 445a980e046SJonathan Cameron /* How much space will the demuxed element take? */ 446a980e046SJonathan Cameron for_each_set_bit(i, mask, 447a980e046SJonathan Cameron indio_dev->masklength) { 448a980e046SJonathan Cameron ch = iio_find_channel_from_si(indio_dev, i); 4490ee8546aSSrinivas Pandruvada if (ch->scan_type.repeat > 1) 4500ee8546aSSrinivas Pandruvada length = ch->scan_type.storagebits / 8 * 4510ee8546aSSrinivas Pandruvada ch->scan_type.repeat; 4520ee8546aSSrinivas Pandruvada else 453a980e046SJonathan Cameron length = ch->scan_type.storagebits / 8; 454a980e046SJonathan Cameron bytes = ALIGN(bytes, length); 455a980e046SJonathan Cameron bytes += length; 456a980e046SJonathan Cameron } 457a980e046SJonathan Cameron if (timestamp) { 458a980e046SJonathan Cameron ch = iio_find_channel_from_si(indio_dev, 459a980e046SJonathan Cameron indio_dev->scan_index_timestamp); 4600ee8546aSSrinivas Pandruvada if (ch->scan_type.repeat > 1) 4610ee8546aSSrinivas Pandruvada length = ch->scan_type.storagebits / 8 * 4620ee8546aSSrinivas Pandruvada ch->scan_type.repeat; 4630ee8546aSSrinivas Pandruvada else 464a980e046SJonathan Cameron length = ch->scan_type.storagebits / 8; 465a980e046SJonathan Cameron bytes = ALIGN(bytes, length); 466a980e046SJonathan Cameron bytes += length; 467a980e046SJonathan Cameron } 468a980e046SJonathan Cameron return bytes; 469a980e046SJonathan Cameron } 470a980e046SJonathan Cameron 4719e69c935SLars-Peter Clausen static void iio_buffer_activate(struct iio_dev *indio_dev, 4729e69c935SLars-Peter Clausen struct iio_buffer *buffer) 4739e69c935SLars-Peter Clausen { 4749e69c935SLars-Peter Clausen iio_buffer_get(buffer); 4759e69c935SLars-Peter Clausen list_add(&buffer->buffer_list, &indio_dev->buffer_list); 4769e69c935SLars-Peter Clausen } 4779e69c935SLars-Peter Clausen 4789e69c935SLars-Peter Clausen static void iio_buffer_deactivate(struct iio_buffer *buffer) 4799e69c935SLars-Peter Clausen { 4809e69c935SLars-Peter Clausen list_del_init(&buffer->buffer_list); 4819e69c935SLars-Peter Clausen iio_buffer_put(buffer); 4829e69c935SLars-Peter Clausen } 4839e69c935SLars-Peter Clausen 484a87c82e4SLars-Peter Clausen void iio_disable_all_buffers(struct iio_dev *indio_dev) 485a87c82e4SLars-Peter Clausen { 486a87c82e4SLars-Peter Clausen struct iio_buffer *buffer, *_buffer; 487a87c82e4SLars-Peter Clausen 488a87c82e4SLars-Peter Clausen if (list_empty(&indio_dev->buffer_list)) 489a87c82e4SLars-Peter Clausen return; 490a87c82e4SLars-Peter Clausen 491a87c82e4SLars-Peter Clausen if (indio_dev->setup_ops->predisable) 492a87c82e4SLars-Peter Clausen indio_dev->setup_ops->predisable(indio_dev); 493a87c82e4SLars-Peter Clausen 494a87c82e4SLars-Peter Clausen list_for_each_entry_safe(buffer, _buffer, 495a87c82e4SLars-Peter Clausen &indio_dev->buffer_list, buffer_list) 4969e69c935SLars-Peter Clausen iio_buffer_deactivate(buffer); 497a87c82e4SLars-Peter Clausen 498a87c82e4SLars-Peter Clausen indio_dev->currentmode = INDIO_DIRECT_MODE; 499a87c82e4SLars-Peter Clausen if (indio_dev->setup_ops->postdisable) 500a87c82e4SLars-Peter Clausen indio_dev->setup_ops->postdisable(indio_dev); 501e086ed76SLars-Peter Clausen 502e086ed76SLars-Peter Clausen if (indio_dev->available_scan_masks == NULL) 503e086ed76SLars-Peter Clausen kfree(indio_dev->active_scan_mask); 504a87c82e4SLars-Peter Clausen } 505a87c82e4SLars-Peter Clausen 5068e050996SLars-Peter Clausen static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev, 5078e050996SLars-Peter Clausen struct iio_buffer *buffer) 5088e050996SLars-Peter Clausen { 5098e050996SLars-Peter Clausen unsigned int bytes; 5108e050996SLars-Peter Clausen 5118e050996SLars-Peter Clausen if (!buffer->access->set_bytes_per_datum) 5128e050996SLars-Peter Clausen return; 5138e050996SLars-Peter Clausen 5148e050996SLars-Peter Clausen bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask, 5158e050996SLars-Peter Clausen buffer->scan_timestamp); 5168e050996SLars-Peter Clausen 5178e050996SLars-Peter Clausen buffer->access->set_bytes_per_datum(buffer, bytes); 5188e050996SLars-Peter Clausen } 5198e050996SLars-Peter Clausen 520a9519456SLars-Peter Clausen static int __iio_update_buffers(struct iio_dev *indio_dev, 52184b36ce5SJonathan Cameron struct iio_buffer *insert_buffer, 52284b36ce5SJonathan Cameron struct iio_buffer *remove_buffer) 523a980e046SJonathan Cameron { 52484b36ce5SJonathan Cameron int ret; 52584b36ce5SJonathan Cameron int success = 0; 52684b36ce5SJonathan Cameron struct iio_buffer *buffer; 52784b36ce5SJonathan Cameron unsigned long *compound_mask; 52884b36ce5SJonathan Cameron const unsigned long *old_mask; 529a980e046SJonathan Cameron 53084b36ce5SJonathan Cameron /* Wind down existing buffers - iff there are any */ 53184b36ce5SJonathan Cameron if (!list_empty(&indio_dev->buffer_list)) { 53284b36ce5SJonathan Cameron if (indio_dev->setup_ops->predisable) { 53384b36ce5SJonathan Cameron ret = indio_dev->setup_ops->predisable(indio_dev); 53484b36ce5SJonathan Cameron if (ret) 53592825ff9SHartmut Knaack return ret; 53684b36ce5SJonathan Cameron } 53784b36ce5SJonathan Cameron indio_dev->currentmode = INDIO_DIRECT_MODE; 53884b36ce5SJonathan Cameron if (indio_dev->setup_ops->postdisable) { 53984b36ce5SJonathan Cameron ret = indio_dev->setup_ops->postdisable(indio_dev); 54084b36ce5SJonathan Cameron if (ret) 54192825ff9SHartmut Knaack return ret; 54284b36ce5SJonathan Cameron } 54384b36ce5SJonathan Cameron } 54484b36ce5SJonathan Cameron /* Keep a copy of current setup to allow roll back */ 54584b36ce5SJonathan Cameron old_mask = indio_dev->active_scan_mask; 54684b36ce5SJonathan Cameron if (!indio_dev->available_scan_masks) 54784b36ce5SJonathan Cameron indio_dev->active_scan_mask = NULL; 54884b36ce5SJonathan Cameron 54984b36ce5SJonathan Cameron if (remove_buffer) 5509e69c935SLars-Peter Clausen iio_buffer_deactivate(remove_buffer); 55184b36ce5SJonathan Cameron if (insert_buffer) 5529e69c935SLars-Peter Clausen iio_buffer_activate(indio_dev, insert_buffer); 55384b36ce5SJonathan Cameron 55484b36ce5SJonathan Cameron /* If no buffers in list, we are done */ 55584b36ce5SJonathan Cameron if (list_empty(&indio_dev->buffer_list)) { 55684b36ce5SJonathan Cameron indio_dev->currentmode = INDIO_DIRECT_MODE; 55784b36ce5SJonathan Cameron if (indio_dev->available_scan_masks == NULL) 55884b36ce5SJonathan Cameron kfree(old_mask); 55984b36ce5SJonathan Cameron return 0; 56084b36ce5SJonathan Cameron } 561a980e046SJonathan Cameron 562a980e046SJonathan Cameron /* What scan mask do we actually have? */ 56384b36ce5SJonathan Cameron compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), 56484b36ce5SJonathan Cameron sizeof(long), GFP_KERNEL); 56584b36ce5SJonathan Cameron if (compound_mask == NULL) { 56684b36ce5SJonathan Cameron if (indio_dev->available_scan_masks == NULL) 56784b36ce5SJonathan Cameron kfree(old_mask); 56884b36ce5SJonathan Cameron return -ENOMEM; 56984b36ce5SJonathan Cameron } 57084b36ce5SJonathan Cameron indio_dev->scan_timestamp = 0; 57184b36ce5SJonathan Cameron 57284b36ce5SJonathan Cameron list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 57384b36ce5SJonathan Cameron bitmap_or(compound_mask, compound_mask, buffer->scan_mask, 57484b36ce5SJonathan Cameron indio_dev->masklength); 57584b36ce5SJonathan Cameron indio_dev->scan_timestamp |= buffer->scan_timestamp; 57684b36ce5SJonathan Cameron } 57784b36ce5SJonathan Cameron if (indio_dev->available_scan_masks) { 578a980e046SJonathan Cameron indio_dev->active_scan_mask = 579a980e046SJonathan Cameron iio_scan_mask_match(indio_dev->available_scan_masks, 580a980e046SJonathan Cameron indio_dev->masklength, 58184b36ce5SJonathan Cameron compound_mask); 58284b36ce5SJonathan Cameron if (indio_dev->active_scan_mask == NULL) { 58384b36ce5SJonathan Cameron /* 58484b36ce5SJonathan Cameron * Roll back. 58584b36ce5SJonathan Cameron * Note can only occur when adding a buffer. 58684b36ce5SJonathan Cameron */ 5879e69c935SLars-Peter Clausen iio_buffer_deactivate(insert_buffer); 588d66e0452SPeter Meerwald if (old_mask) { 58984b36ce5SJonathan Cameron indio_dev->active_scan_mask = old_mask; 59084b36ce5SJonathan Cameron success = -EINVAL; 59184b36ce5SJonathan Cameron } 592d66e0452SPeter Meerwald else { 593d66e0452SPeter Meerwald kfree(compound_mask); 594d66e0452SPeter Meerwald ret = -EINVAL; 59592825ff9SHartmut Knaack return ret; 596d66e0452SPeter Meerwald } 597d66e0452SPeter Meerwald } 59884b36ce5SJonathan Cameron } else { 59984b36ce5SJonathan Cameron indio_dev->active_scan_mask = compound_mask; 60084b36ce5SJonathan Cameron } 601aff1eb4eSLars-Peter Clausen 602a980e046SJonathan Cameron iio_update_demux(indio_dev); 603a980e046SJonathan Cameron 60484b36ce5SJonathan Cameron /* Wind up again */ 60584b36ce5SJonathan Cameron if (indio_dev->setup_ops->preenable) { 60684b36ce5SJonathan Cameron ret = indio_dev->setup_ops->preenable(indio_dev); 60784b36ce5SJonathan Cameron if (ret) { 60884b36ce5SJonathan Cameron printk(KERN_ERR 609bec1889dSMichał Mirosław "Buffer not started: buffer preenable failed (%d)\n", ret); 61084b36ce5SJonathan Cameron goto error_remove_inserted; 61184b36ce5SJonathan Cameron } 61284b36ce5SJonathan Cameron } 61384b36ce5SJonathan Cameron indio_dev->scan_bytes = 61484b36ce5SJonathan Cameron iio_compute_scan_bytes(indio_dev, 61584b36ce5SJonathan Cameron indio_dev->active_scan_mask, 61684b36ce5SJonathan Cameron indio_dev->scan_timestamp); 6178e050996SLars-Peter Clausen list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 6188e050996SLars-Peter Clausen iio_buffer_update_bytes_per_datum(indio_dev, buffer); 61984b36ce5SJonathan Cameron if (buffer->access->request_update) { 62084b36ce5SJonathan Cameron ret = buffer->access->request_update(buffer); 62184b36ce5SJonathan Cameron if (ret) { 62284b36ce5SJonathan Cameron printk(KERN_INFO 623bec1889dSMichał Mirosław "Buffer not started: buffer parameter update failed (%d)\n", ret); 62484b36ce5SJonathan Cameron goto error_run_postdisable; 62584b36ce5SJonathan Cameron } 62684b36ce5SJonathan Cameron } 6278e050996SLars-Peter Clausen } 62884b36ce5SJonathan Cameron if (indio_dev->info->update_scan_mode) { 62984b36ce5SJonathan Cameron ret = indio_dev->info 630a980e046SJonathan Cameron ->update_scan_mode(indio_dev, 631a980e046SJonathan Cameron indio_dev->active_scan_mask); 63284b36ce5SJonathan Cameron if (ret < 0) { 633bec1889dSMichał Mirosław printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret); 63484b36ce5SJonathan Cameron goto error_run_postdisable; 63584b36ce5SJonathan Cameron } 63684b36ce5SJonathan Cameron } 63784b36ce5SJonathan Cameron /* Definitely possible for devices to support both of these. */ 63884b36ce5SJonathan Cameron if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) { 63984b36ce5SJonathan Cameron if (!indio_dev->trig) { 64084b36ce5SJonathan Cameron printk(KERN_INFO "Buffer not started: no trigger\n"); 64184b36ce5SJonathan Cameron ret = -EINVAL; 64284b36ce5SJonathan Cameron /* Can only occur on first buffer */ 64384b36ce5SJonathan Cameron goto error_run_postdisable; 64484b36ce5SJonathan Cameron } 64584b36ce5SJonathan Cameron indio_dev->currentmode = INDIO_BUFFER_TRIGGERED; 64684b36ce5SJonathan Cameron } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) { 64784b36ce5SJonathan Cameron indio_dev->currentmode = INDIO_BUFFER_HARDWARE; 6489572588cSPeter Meerwald } else { /* Should never be reached */ 64984b36ce5SJonathan Cameron ret = -EINVAL; 65084b36ce5SJonathan Cameron goto error_run_postdisable; 65184b36ce5SJonathan Cameron } 65284b36ce5SJonathan Cameron 65384b36ce5SJonathan Cameron if (indio_dev->setup_ops->postenable) { 65484b36ce5SJonathan Cameron ret = indio_dev->setup_ops->postenable(indio_dev); 65584b36ce5SJonathan Cameron if (ret) { 65684b36ce5SJonathan Cameron printk(KERN_INFO 657bec1889dSMichał Mirosław "Buffer not started: postenable failed (%d)\n", ret); 65884b36ce5SJonathan Cameron indio_dev->currentmode = INDIO_DIRECT_MODE; 65984b36ce5SJonathan Cameron if (indio_dev->setup_ops->postdisable) 66084b36ce5SJonathan Cameron indio_dev->setup_ops->postdisable(indio_dev); 66184b36ce5SJonathan Cameron goto error_disable_all_buffers; 66284b36ce5SJonathan Cameron } 66384b36ce5SJonathan Cameron } 66484b36ce5SJonathan Cameron 66584b36ce5SJonathan Cameron if (indio_dev->available_scan_masks) 66684b36ce5SJonathan Cameron kfree(compound_mask); 66784b36ce5SJonathan Cameron else 66884b36ce5SJonathan Cameron kfree(old_mask); 66984b36ce5SJonathan Cameron 67084b36ce5SJonathan Cameron return success; 67184b36ce5SJonathan Cameron 67284b36ce5SJonathan Cameron error_disable_all_buffers: 67384b36ce5SJonathan Cameron indio_dev->currentmode = INDIO_DIRECT_MODE; 67484b36ce5SJonathan Cameron error_run_postdisable: 67584b36ce5SJonathan Cameron if (indio_dev->setup_ops->postdisable) 67684b36ce5SJonathan Cameron indio_dev->setup_ops->postdisable(indio_dev); 67784b36ce5SJonathan Cameron error_remove_inserted: 67884b36ce5SJonathan Cameron if (insert_buffer) 6799e69c935SLars-Peter Clausen iio_buffer_deactivate(insert_buffer); 68084b36ce5SJonathan Cameron indio_dev->active_scan_mask = old_mask; 68184b36ce5SJonathan Cameron kfree(compound_mask); 68284b36ce5SJonathan Cameron return ret; 68384b36ce5SJonathan Cameron } 684a9519456SLars-Peter Clausen 685a9519456SLars-Peter Clausen int iio_update_buffers(struct iio_dev *indio_dev, 686a9519456SLars-Peter Clausen struct iio_buffer *insert_buffer, 687a9519456SLars-Peter Clausen struct iio_buffer *remove_buffer) 688a9519456SLars-Peter Clausen { 689a9519456SLars-Peter Clausen int ret; 690a9519456SLars-Peter Clausen 6913909fab5SLars-Peter Clausen if (insert_buffer == remove_buffer) 6923909fab5SLars-Peter Clausen return 0; 6933909fab5SLars-Peter Clausen 694a9519456SLars-Peter Clausen mutex_lock(&indio_dev->info_exist_lock); 695a9519456SLars-Peter Clausen mutex_lock(&indio_dev->mlock); 696a9519456SLars-Peter Clausen 6973909fab5SLars-Peter Clausen if (insert_buffer && iio_buffer_is_active(insert_buffer)) 6983909fab5SLars-Peter Clausen insert_buffer = NULL; 6993909fab5SLars-Peter Clausen 7003909fab5SLars-Peter Clausen if (remove_buffer && !iio_buffer_is_active(remove_buffer)) 7013909fab5SLars-Peter Clausen remove_buffer = NULL; 7023909fab5SLars-Peter Clausen 7033909fab5SLars-Peter Clausen if (!insert_buffer && !remove_buffer) { 7043909fab5SLars-Peter Clausen ret = 0; 7053909fab5SLars-Peter Clausen goto out_unlock; 7063909fab5SLars-Peter Clausen } 7073909fab5SLars-Peter Clausen 708a9519456SLars-Peter Clausen if (indio_dev->info == NULL) { 709a9519456SLars-Peter Clausen ret = -ENODEV; 710a9519456SLars-Peter Clausen goto out_unlock; 711a9519456SLars-Peter Clausen } 712a9519456SLars-Peter Clausen 713a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer); 714a9519456SLars-Peter Clausen 715a9519456SLars-Peter Clausen out_unlock: 716a9519456SLars-Peter Clausen mutex_unlock(&indio_dev->mlock); 717a9519456SLars-Peter Clausen mutex_unlock(&indio_dev->info_exist_lock); 718a9519456SLars-Peter Clausen 719a9519456SLars-Peter Clausen return ret; 720a9519456SLars-Peter Clausen } 72184b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_update_buffers); 72284b36ce5SJonathan Cameron 723*08e7e0adSLars-Peter Clausen static ssize_t iio_buffer_store_enable(struct device *dev, 72484b36ce5SJonathan Cameron struct device_attribute *attr, 72584b36ce5SJonathan Cameron const char *buf, 72684b36ce5SJonathan Cameron size_t len) 72784b36ce5SJonathan Cameron { 72884b36ce5SJonathan Cameron int ret; 72984b36ce5SJonathan Cameron bool requested_state; 73084b36ce5SJonathan Cameron struct iio_dev *indio_dev = dev_to_iio_dev(dev); 73184b36ce5SJonathan Cameron bool inlist; 73284b36ce5SJonathan Cameron 73384b36ce5SJonathan Cameron ret = strtobool(buf, &requested_state); 73484b36ce5SJonathan Cameron if (ret < 0) 73584b36ce5SJonathan Cameron return ret; 73684b36ce5SJonathan Cameron 73784b36ce5SJonathan Cameron mutex_lock(&indio_dev->mlock); 73884b36ce5SJonathan Cameron 73984b36ce5SJonathan Cameron /* Find out if it is in the list */ 740705ee2c9SLars-Peter Clausen inlist = iio_buffer_is_active(indio_dev->buffer); 74184b36ce5SJonathan Cameron /* Already in desired state */ 74284b36ce5SJonathan Cameron if (inlist == requested_state) 74384b36ce5SJonathan Cameron goto done; 74484b36ce5SJonathan Cameron 74584b36ce5SJonathan Cameron if (requested_state) 746a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, 74784b36ce5SJonathan Cameron indio_dev->buffer, NULL); 74884b36ce5SJonathan Cameron else 749a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, 75084b36ce5SJonathan Cameron NULL, indio_dev->buffer); 75184b36ce5SJonathan Cameron 75284b36ce5SJonathan Cameron if (ret < 0) 75384b36ce5SJonathan Cameron goto done; 75484b36ce5SJonathan Cameron done: 75584b36ce5SJonathan Cameron mutex_unlock(&indio_dev->mlock); 75684b36ce5SJonathan Cameron return (ret < 0) ? ret : len; 75784b36ce5SJonathan Cameron } 75884b36ce5SJonathan Cameron 759d967cb6bSLars-Peter Clausen static const char * const iio_scan_elements_group_name = "scan_elements"; 760d967cb6bSLars-Peter Clausen 761*08e7e0adSLars-Peter Clausen static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length, 762*08e7e0adSLars-Peter Clausen iio_buffer_write_length); 763*08e7e0adSLars-Peter Clausen static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, 764*08e7e0adSLars-Peter Clausen iio_buffer_show_enable, iio_buffer_store_enable); 765*08e7e0adSLars-Peter Clausen 766d967cb6bSLars-Peter Clausen int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev) 767d967cb6bSLars-Peter Clausen { 768d967cb6bSLars-Peter Clausen struct iio_dev_attr *p; 769d967cb6bSLars-Peter Clausen struct attribute **attr; 770d967cb6bSLars-Peter Clausen struct iio_buffer *buffer = indio_dev->buffer; 771d967cb6bSLars-Peter Clausen int ret, i, attrn, attrcount, attrcount_orig = 0; 772d967cb6bSLars-Peter Clausen const struct iio_chan_spec *channels; 773d967cb6bSLars-Peter Clausen 774d967cb6bSLars-Peter Clausen if (!buffer) 775d967cb6bSLars-Peter Clausen return 0; 776d967cb6bSLars-Peter Clausen 777*08e7e0adSLars-Peter Clausen attrcount = 0; 778*08e7e0adSLars-Peter Clausen if (buffer->attrs) { 779*08e7e0adSLars-Peter Clausen while (buffer->attrs[attrcount] != NULL) 780*08e7e0adSLars-Peter Clausen attrcount++; 781*08e7e0adSLars-Peter Clausen } 782*08e7e0adSLars-Peter Clausen 783*08e7e0adSLars-Peter Clausen buffer->buffer_group.name = "buffer"; 784*08e7e0adSLars-Peter Clausen buffer->buffer_group.attrs = kcalloc(attrcount + 3, 785*08e7e0adSLars-Peter Clausen sizeof(*buffer->buffer_group.attrs), GFP_KERNEL); 786*08e7e0adSLars-Peter Clausen if (!buffer->buffer_group.attrs) 787*08e7e0adSLars-Peter Clausen return -ENOMEM; 788*08e7e0adSLars-Peter Clausen 789*08e7e0adSLars-Peter Clausen buffer->buffer_group.attrs[0] = &dev_attr_length.attr; 790*08e7e0adSLars-Peter Clausen buffer->buffer_group.attrs[1] = &dev_attr_enable.attr; 791*08e7e0adSLars-Peter Clausen if (buffer->attrs) 792*08e7e0adSLars-Peter Clausen memcpy(&buffer->buffer_group.attrs[2], buffer->attrs, 793*08e7e0adSLars-Peter Clausen sizeof(*&buffer->buffer_group.attrs) * (attrcount - 2)); 794*08e7e0adSLars-Peter Clausen buffer->buffer_group.attrs[attrcount+2] = NULL; 795*08e7e0adSLars-Peter Clausen 796*08e7e0adSLars-Peter Clausen indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group; 797*08e7e0adSLars-Peter Clausen 798d967cb6bSLars-Peter Clausen if (buffer->scan_el_attrs != NULL) { 799d967cb6bSLars-Peter Clausen attr = buffer->scan_el_attrs->attrs; 800d967cb6bSLars-Peter Clausen while (*attr++ != NULL) 801d967cb6bSLars-Peter Clausen attrcount_orig++; 802d967cb6bSLars-Peter Clausen } 803d967cb6bSLars-Peter Clausen attrcount = attrcount_orig; 804d967cb6bSLars-Peter Clausen INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list); 805d967cb6bSLars-Peter Clausen channels = indio_dev->channels; 806d967cb6bSLars-Peter Clausen if (channels) { 807d967cb6bSLars-Peter Clausen /* new magic */ 808d967cb6bSLars-Peter Clausen for (i = 0; i < indio_dev->num_channels; i++) { 809d967cb6bSLars-Peter Clausen if (channels[i].scan_index < 0) 810d967cb6bSLars-Peter Clausen continue; 811d967cb6bSLars-Peter Clausen 812d967cb6bSLars-Peter Clausen /* Establish necessary mask length */ 813d967cb6bSLars-Peter Clausen if (channels[i].scan_index > 814d967cb6bSLars-Peter Clausen (int)indio_dev->masklength - 1) 815d967cb6bSLars-Peter Clausen indio_dev->masklength 816d967cb6bSLars-Peter Clausen = channels[i].scan_index + 1; 817d967cb6bSLars-Peter Clausen 818d967cb6bSLars-Peter Clausen ret = iio_buffer_add_channel_sysfs(indio_dev, 819d967cb6bSLars-Peter Clausen &channels[i]); 820d967cb6bSLars-Peter Clausen if (ret < 0) 821d967cb6bSLars-Peter Clausen goto error_cleanup_dynamic; 822d967cb6bSLars-Peter Clausen attrcount += ret; 823d967cb6bSLars-Peter Clausen if (channels[i].type == IIO_TIMESTAMP) 824d967cb6bSLars-Peter Clausen indio_dev->scan_index_timestamp = 825d967cb6bSLars-Peter Clausen channels[i].scan_index; 826d967cb6bSLars-Peter Clausen } 827d967cb6bSLars-Peter Clausen if (indio_dev->masklength && buffer->scan_mask == NULL) { 828d967cb6bSLars-Peter Clausen buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), 829d967cb6bSLars-Peter Clausen sizeof(*buffer->scan_mask), 830d967cb6bSLars-Peter Clausen GFP_KERNEL); 831d967cb6bSLars-Peter Clausen if (buffer->scan_mask == NULL) { 832d967cb6bSLars-Peter Clausen ret = -ENOMEM; 833d967cb6bSLars-Peter Clausen goto error_cleanup_dynamic; 834d967cb6bSLars-Peter Clausen } 835d967cb6bSLars-Peter Clausen } 836d967cb6bSLars-Peter Clausen } 837d967cb6bSLars-Peter Clausen 838d967cb6bSLars-Peter Clausen buffer->scan_el_group.name = iio_scan_elements_group_name; 839d967cb6bSLars-Peter Clausen 840d967cb6bSLars-Peter Clausen buffer->scan_el_group.attrs = kcalloc(attrcount + 1, 841d967cb6bSLars-Peter Clausen sizeof(buffer->scan_el_group.attrs[0]), 842d967cb6bSLars-Peter Clausen GFP_KERNEL); 843d967cb6bSLars-Peter Clausen if (buffer->scan_el_group.attrs == NULL) { 844d967cb6bSLars-Peter Clausen ret = -ENOMEM; 845d967cb6bSLars-Peter Clausen goto error_free_scan_mask; 846d967cb6bSLars-Peter Clausen } 847d967cb6bSLars-Peter Clausen if (buffer->scan_el_attrs) 848d967cb6bSLars-Peter Clausen memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs, 849d967cb6bSLars-Peter Clausen sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig); 850d967cb6bSLars-Peter Clausen attrn = attrcount_orig; 851d967cb6bSLars-Peter Clausen 852d967cb6bSLars-Peter Clausen list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l) 853d967cb6bSLars-Peter Clausen buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr; 854d967cb6bSLars-Peter Clausen indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group; 855d967cb6bSLars-Peter Clausen 856d967cb6bSLars-Peter Clausen return 0; 857d967cb6bSLars-Peter Clausen 858d967cb6bSLars-Peter Clausen error_free_scan_mask: 859d967cb6bSLars-Peter Clausen kfree(buffer->scan_mask); 860d967cb6bSLars-Peter Clausen error_cleanup_dynamic: 861d967cb6bSLars-Peter Clausen iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list); 862*08e7e0adSLars-Peter Clausen kfree(indio_dev->buffer->buffer_group.attrs); 863d967cb6bSLars-Peter Clausen 864d967cb6bSLars-Peter Clausen return ret; 865d967cb6bSLars-Peter Clausen } 866d967cb6bSLars-Peter Clausen 867d967cb6bSLars-Peter Clausen void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev) 868d967cb6bSLars-Peter Clausen { 869d967cb6bSLars-Peter Clausen if (!indio_dev->buffer) 870d967cb6bSLars-Peter Clausen return; 871d967cb6bSLars-Peter Clausen 872d967cb6bSLars-Peter Clausen kfree(indio_dev->buffer->scan_mask); 873*08e7e0adSLars-Peter Clausen kfree(indio_dev->buffer->buffer_group.attrs); 874d967cb6bSLars-Peter Clausen kfree(indio_dev->buffer->scan_el_group.attrs); 875d967cb6bSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list); 876d967cb6bSLars-Peter Clausen } 877d967cb6bSLars-Peter Clausen 878a980e046SJonathan Cameron /** 87981636632SLars-Peter Clausen * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected 88081636632SLars-Peter Clausen * @indio_dev: the iio device 88181636632SLars-Peter Clausen * @mask: scan mask to be checked 88281636632SLars-Peter Clausen * 88381636632SLars-Peter Clausen * Return true if exactly one bit is set in the scan mask, false otherwise. It 88481636632SLars-Peter Clausen * can be used for devices where only one channel can be active for sampling at 88581636632SLars-Peter Clausen * a time. 88681636632SLars-Peter Clausen */ 88781636632SLars-Peter Clausen bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev, 88881636632SLars-Peter Clausen const unsigned long *mask) 88981636632SLars-Peter Clausen { 89081636632SLars-Peter Clausen return bitmap_weight(mask, indio_dev->masklength) == 1; 89181636632SLars-Peter Clausen } 89281636632SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot); 89381636632SLars-Peter Clausen 894a980e046SJonathan Cameron int iio_scan_mask_query(struct iio_dev *indio_dev, 895a980e046SJonathan Cameron struct iio_buffer *buffer, int bit) 896a980e046SJonathan Cameron { 897a980e046SJonathan Cameron if (bit > indio_dev->masklength) 898a980e046SJonathan Cameron return -EINVAL; 899a980e046SJonathan Cameron 900a980e046SJonathan Cameron if (!buffer->scan_mask) 901a980e046SJonathan Cameron return 0; 902a980e046SJonathan Cameron 9032076a20fSAlec Berg /* Ensure return value is 0 or 1. */ 9042076a20fSAlec Berg return !!test_bit(bit, buffer->scan_mask); 905a980e046SJonathan Cameron }; 906a980e046SJonathan Cameron EXPORT_SYMBOL_GPL(iio_scan_mask_query); 907a980e046SJonathan Cameron 908a980e046SJonathan Cameron /** 909a980e046SJonathan Cameron * struct iio_demux_table() - table describing demux memcpy ops 910a980e046SJonathan Cameron * @from: index to copy from 911a980e046SJonathan Cameron * @to: index to copy to 912a980e046SJonathan Cameron * @length: how many bytes to copy 913a980e046SJonathan Cameron * @l: list head used for management 914a980e046SJonathan Cameron */ 915a980e046SJonathan Cameron struct iio_demux_table { 916a980e046SJonathan Cameron unsigned from; 917a980e046SJonathan Cameron unsigned to; 918a980e046SJonathan Cameron unsigned length; 919a980e046SJonathan Cameron struct list_head l; 920a980e046SJonathan Cameron }; 921a980e046SJonathan Cameron 9225d65d920SLars-Peter Clausen static const void *iio_demux(struct iio_buffer *buffer, 9235d65d920SLars-Peter Clausen const void *datain) 924a980e046SJonathan Cameron { 925a980e046SJonathan Cameron struct iio_demux_table *t; 926a980e046SJonathan Cameron 927a980e046SJonathan Cameron if (list_empty(&buffer->demux_list)) 928a980e046SJonathan Cameron return datain; 929a980e046SJonathan Cameron list_for_each_entry(t, &buffer->demux_list, l) 930a980e046SJonathan Cameron memcpy(buffer->demux_bounce + t->to, 931a980e046SJonathan Cameron datain + t->from, t->length); 932a980e046SJonathan Cameron 933a980e046SJonathan Cameron return buffer->demux_bounce; 934a980e046SJonathan Cameron } 935a980e046SJonathan Cameron 9365d65d920SLars-Peter Clausen static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data) 937a980e046SJonathan Cameron { 9385d65d920SLars-Peter Clausen const void *dataout = iio_demux(buffer, data); 939a980e046SJonathan Cameron 940ce56ade6SLars-Peter Clausen return buffer->access->store_to(buffer, dataout); 941a980e046SJonathan Cameron } 942a980e046SJonathan Cameron 943a980e046SJonathan Cameron static void iio_buffer_demux_free(struct iio_buffer *buffer) 944a980e046SJonathan Cameron { 945a980e046SJonathan Cameron struct iio_demux_table *p, *q; 946a980e046SJonathan Cameron list_for_each_entry_safe(p, q, &buffer->demux_list, l) { 947a980e046SJonathan Cameron list_del(&p->l); 948a980e046SJonathan Cameron kfree(p); 949a980e046SJonathan Cameron } 950a980e046SJonathan Cameron } 951a980e046SJonathan Cameron 95284b36ce5SJonathan Cameron 9535d65d920SLars-Peter Clausen int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) 95484b36ce5SJonathan Cameron { 95584b36ce5SJonathan Cameron int ret; 95684b36ce5SJonathan Cameron struct iio_buffer *buf; 95784b36ce5SJonathan Cameron 95884b36ce5SJonathan Cameron list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) { 95984b36ce5SJonathan Cameron ret = iio_push_to_buffer(buf, data); 96084b36ce5SJonathan Cameron if (ret < 0) 96184b36ce5SJonathan Cameron return ret; 96284b36ce5SJonathan Cameron } 96384b36ce5SJonathan Cameron 96484b36ce5SJonathan Cameron return 0; 96584b36ce5SJonathan Cameron } 96684b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_push_to_buffers); 96784b36ce5SJonathan Cameron 968cbe88bccSLars-Peter Clausen static int iio_buffer_add_demux(struct iio_buffer *buffer, 969cbe88bccSLars-Peter Clausen struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc, 970cbe88bccSLars-Peter Clausen unsigned int length) 971cbe88bccSLars-Peter Clausen { 972cbe88bccSLars-Peter Clausen 973cbe88bccSLars-Peter Clausen if (*p && (*p)->from + (*p)->length == in_loc && 974cbe88bccSLars-Peter Clausen (*p)->to + (*p)->length == out_loc) { 975cbe88bccSLars-Peter Clausen (*p)->length += length; 976cbe88bccSLars-Peter Clausen } else { 9777cdca178SJonathan Cameron *p = kmalloc(sizeof(**p), GFP_KERNEL); 978cbe88bccSLars-Peter Clausen if (*p == NULL) 979cbe88bccSLars-Peter Clausen return -ENOMEM; 980cbe88bccSLars-Peter Clausen (*p)->from = in_loc; 981cbe88bccSLars-Peter Clausen (*p)->to = out_loc; 982cbe88bccSLars-Peter Clausen (*p)->length = length; 983cbe88bccSLars-Peter Clausen list_add_tail(&(*p)->l, &buffer->demux_list); 984cbe88bccSLars-Peter Clausen } 985cbe88bccSLars-Peter Clausen 986cbe88bccSLars-Peter Clausen return 0; 987cbe88bccSLars-Peter Clausen } 988cbe88bccSLars-Peter Clausen 98984b36ce5SJonathan Cameron static int iio_buffer_update_demux(struct iio_dev *indio_dev, 99084b36ce5SJonathan Cameron struct iio_buffer *buffer) 991a980e046SJonathan Cameron { 992a980e046SJonathan Cameron const struct iio_chan_spec *ch; 993a980e046SJonathan Cameron int ret, in_ind = -1, out_ind, length; 994a980e046SJonathan Cameron unsigned in_loc = 0, out_loc = 0; 995cbe88bccSLars-Peter Clausen struct iio_demux_table *p = NULL; 996a980e046SJonathan Cameron 997a980e046SJonathan Cameron /* Clear out any old demux */ 998a980e046SJonathan Cameron iio_buffer_demux_free(buffer); 999a980e046SJonathan Cameron kfree(buffer->demux_bounce); 1000a980e046SJonathan Cameron buffer->demux_bounce = NULL; 1001a980e046SJonathan Cameron 1002a980e046SJonathan Cameron /* First work out which scan mode we will actually have */ 1003a980e046SJonathan Cameron if (bitmap_equal(indio_dev->active_scan_mask, 1004a980e046SJonathan Cameron buffer->scan_mask, 1005a980e046SJonathan Cameron indio_dev->masklength)) 1006a980e046SJonathan Cameron return 0; 1007a980e046SJonathan Cameron 1008a980e046SJonathan Cameron /* Now we have the two masks, work from least sig and build up sizes */ 1009a980e046SJonathan Cameron for_each_set_bit(out_ind, 101061bd55ceSLars-Peter Clausen buffer->scan_mask, 1011a980e046SJonathan Cameron indio_dev->masklength) { 1012a980e046SJonathan Cameron in_ind = find_next_bit(indio_dev->active_scan_mask, 1013a980e046SJonathan Cameron indio_dev->masklength, 1014a980e046SJonathan Cameron in_ind + 1); 1015a980e046SJonathan Cameron while (in_ind != out_ind) { 1016a980e046SJonathan Cameron in_ind = find_next_bit(indio_dev->active_scan_mask, 1017a980e046SJonathan Cameron indio_dev->masklength, 1018a980e046SJonathan Cameron in_ind + 1); 1019a980e046SJonathan Cameron ch = iio_find_channel_from_si(indio_dev, in_ind); 10200ee8546aSSrinivas Pandruvada if (ch->scan_type.repeat > 1) 10210ee8546aSSrinivas Pandruvada length = ch->scan_type.storagebits / 8 * 10220ee8546aSSrinivas Pandruvada ch->scan_type.repeat; 10230ee8546aSSrinivas Pandruvada else 1024a980e046SJonathan Cameron length = ch->scan_type.storagebits / 8; 1025a980e046SJonathan Cameron /* Make sure we are aligned */ 102661072dbcSLars-Peter Clausen in_loc = roundup(in_loc, length) + length; 1027a980e046SJonathan Cameron } 1028a980e046SJonathan Cameron ch = iio_find_channel_from_si(indio_dev, in_ind); 10290ee8546aSSrinivas Pandruvada if (ch->scan_type.repeat > 1) 10300ee8546aSSrinivas Pandruvada length = ch->scan_type.storagebits / 8 * 10310ee8546aSSrinivas Pandruvada ch->scan_type.repeat; 10320ee8546aSSrinivas Pandruvada else 1033a980e046SJonathan Cameron length = ch->scan_type.storagebits / 8; 103461072dbcSLars-Peter Clausen out_loc = roundup(out_loc, length); 103561072dbcSLars-Peter Clausen in_loc = roundup(in_loc, length); 1036cbe88bccSLars-Peter Clausen ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); 1037cbe88bccSLars-Peter Clausen if (ret) 1038cbe88bccSLars-Peter Clausen goto error_clear_mux_table; 1039a980e046SJonathan Cameron out_loc += length; 1040a980e046SJonathan Cameron in_loc += length; 1041a980e046SJonathan Cameron } 1042a980e046SJonathan Cameron /* Relies on scan_timestamp being last */ 1043a980e046SJonathan Cameron if (buffer->scan_timestamp) { 1044a980e046SJonathan Cameron ch = iio_find_channel_from_si(indio_dev, 1045a980e046SJonathan Cameron indio_dev->scan_index_timestamp); 10460ee8546aSSrinivas Pandruvada if (ch->scan_type.repeat > 1) 10470ee8546aSSrinivas Pandruvada length = ch->scan_type.storagebits / 8 * 10480ee8546aSSrinivas Pandruvada ch->scan_type.repeat; 10490ee8546aSSrinivas Pandruvada else 1050a980e046SJonathan Cameron length = ch->scan_type.storagebits / 8; 105161072dbcSLars-Peter Clausen out_loc = roundup(out_loc, length); 105261072dbcSLars-Peter Clausen in_loc = roundup(in_loc, length); 1053cbe88bccSLars-Peter Clausen ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); 1054cbe88bccSLars-Peter Clausen if (ret) 1055cbe88bccSLars-Peter Clausen goto error_clear_mux_table; 1056a980e046SJonathan Cameron out_loc += length; 1057a980e046SJonathan Cameron in_loc += length; 1058a980e046SJonathan Cameron } 1059a980e046SJonathan Cameron buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL); 1060a980e046SJonathan Cameron if (buffer->demux_bounce == NULL) { 1061a980e046SJonathan Cameron ret = -ENOMEM; 1062a980e046SJonathan Cameron goto error_clear_mux_table; 1063a980e046SJonathan Cameron } 1064a980e046SJonathan Cameron return 0; 1065a980e046SJonathan Cameron 1066a980e046SJonathan Cameron error_clear_mux_table: 1067a980e046SJonathan Cameron iio_buffer_demux_free(buffer); 1068a980e046SJonathan Cameron 1069a980e046SJonathan Cameron return ret; 1070a980e046SJonathan Cameron } 107184b36ce5SJonathan Cameron 107284b36ce5SJonathan Cameron int iio_update_demux(struct iio_dev *indio_dev) 107384b36ce5SJonathan Cameron { 107484b36ce5SJonathan Cameron struct iio_buffer *buffer; 107584b36ce5SJonathan Cameron int ret; 107684b36ce5SJonathan Cameron 107784b36ce5SJonathan Cameron list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 107884b36ce5SJonathan Cameron ret = iio_buffer_update_demux(indio_dev, buffer); 107984b36ce5SJonathan Cameron if (ret < 0) 108084b36ce5SJonathan Cameron goto error_clear_mux_table; 108184b36ce5SJonathan Cameron } 108284b36ce5SJonathan Cameron return 0; 108384b36ce5SJonathan Cameron 108484b36ce5SJonathan Cameron error_clear_mux_table: 108584b36ce5SJonathan Cameron list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) 108684b36ce5SJonathan Cameron iio_buffer_demux_free(buffer); 108784b36ce5SJonathan Cameron 108884b36ce5SJonathan Cameron return ret; 108984b36ce5SJonathan Cameron } 1090a980e046SJonathan Cameron EXPORT_SYMBOL_GPL(iio_update_demux); 10919e69c935SLars-Peter Clausen 10929e69c935SLars-Peter Clausen /** 10939e69c935SLars-Peter Clausen * iio_buffer_release() - Free a buffer's resources 10949e69c935SLars-Peter Clausen * @ref: Pointer to the kref embedded in the iio_buffer struct 10959e69c935SLars-Peter Clausen * 10969e69c935SLars-Peter Clausen * This function is called when the last reference to the buffer has been 10979e69c935SLars-Peter Clausen * dropped. It will typically free all resources allocated by the buffer. Do not 10989e69c935SLars-Peter Clausen * call this function manually, always use iio_buffer_put() when done using a 10999e69c935SLars-Peter Clausen * buffer. 11009e69c935SLars-Peter Clausen */ 11019e69c935SLars-Peter Clausen static void iio_buffer_release(struct kref *ref) 11029e69c935SLars-Peter Clausen { 11039e69c935SLars-Peter Clausen struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref); 11049e69c935SLars-Peter Clausen 11059e69c935SLars-Peter Clausen buffer->access->release(buffer); 11069e69c935SLars-Peter Clausen } 11079e69c935SLars-Peter Clausen 11089e69c935SLars-Peter Clausen /** 11099e69c935SLars-Peter Clausen * iio_buffer_get() - Grab a reference to the buffer 11109e69c935SLars-Peter Clausen * @buffer: The buffer to grab a reference for, may be NULL 11119e69c935SLars-Peter Clausen * 11129e69c935SLars-Peter Clausen * Returns the pointer to the buffer that was passed into the function. 11139e69c935SLars-Peter Clausen */ 11149e69c935SLars-Peter Clausen struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer) 11159e69c935SLars-Peter Clausen { 11169e69c935SLars-Peter Clausen if (buffer) 11179e69c935SLars-Peter Clausen kref_get(&buffer->ref); 11189e69c935SLars-Peter Clausen 11199e69c935SLars-Peter Clausen return buffer; 11209e69c935SLars-Peter Clausen } 11219e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_get); 11229e69c935SLars-Peter Clausen 11239e69c935SLars-Peter Clausen /** 11249e69c935SLars-Peter Clausen * iio_buffer_put() - Release the reference to the buffer 11259e69c935SLars-Peter Clausen * @buffer: The buffer to release the reference for, may be NULL 11269e69c935SLars-Peter Clausen */ 11279e69c935SLars-Peter Clausen void iio_buffer_put(struct iio_buffer *buffer) 11289e69c935SLars-Peter Clausen { 11299e69c935SLars-Peter Clausen if (buffer) 11309e69c935SLars-Peter Clausen kref_put(&buffer->ref, iio_buffer_release); 11319e69c935SLars-Peter Clausen } 11329e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_put); 1133