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 4037d34556SJosselin Costanzi static size_t 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 45f4f4673bSOctavian Purdila static int iio_buffer_flush_hwfifo(struct iio_dev *indio_dev, 46f4f4673bSOctavian Purdila struct iio_buffer *buf, size_t required) 4737d34556SJosselin Costanzi { 48f4f4673bSOctavian Purdila if (!indio_dev->info->hwfifo_flush_to_buffer) 49f4f4673bSOctavian Purdila return -ENODEV; 50f4f4673bSOctavian Purdila 51f4f4673bSOctavian Purdila return indio_dev->info->hwfifo_flush_to_buffer(indio_dev, required); 52f4f4673bSOctavian Purdila } 53f4f4673bSOctavian Purdila 54f4f4673bSOctavian Purdila static bool iio_buffer_ready(struct iio_dev *indio_dev, struct iio_buffer *buf, 55f4f4673bSOctavian Purdila size_t to_wait, int to_flush) 56f4f4673bSOctavian Purdila { 57f4f4673bSOctavian Purdila size_t avail; 58f4f4673bSOctavian Purdila int flushed = 0; 59f4f4673bSOctavian Purdila 6037d34556SJosselin Costanzi /* wakeup if the device was unregistered */ 6137d34556SJosselin Costanzi if (!indio_dev->info) 6237d34556SJosselin Costanzi return true; 6337d34556SJosselin Costanzi 6437d34556SJosselin Costanzi /* drain the buffer if it was disabled */ 65f4f4673bSOctavian Purdila if (!iio_buffer_is_active(buf)) { 6637d34556SJosselin Costanzi to_wait = min_t(size_t, to_wait, 1); 67f4f4673bSOctavian Purdila to_flush = 0; 68f4f4673bSOctavian Purdila } 6937d34556SJosselin Costanzi 70f4f4673bSOctavian Purdila avail = iio_buffer_data_available(buf); 71f4f4673bSOctavian Purdila 72f4f4673bSOctavian Purdila if (avail >= to_wait) { 73f4f4673bSOctavian Purdila /* force a flush for non-blocking reads */ 74c6f67a1fSOctavian Purdila if (!to_wait && avail < to_flush) 75c6f67a1fSOctavian Purdila iio_buffer_flush_hwfifo(indio_dev, buf, 76c6f67a1fSOctavian Purdila to_flush - avail); 77f4f4673bSOctavian Purdila return true; 78f4f4673bSOctavian Purdila } 79f4f4673bSOctavian Purdila 80f4f4673bSOctavian Purdila if (to_flush) 81f4f4673bSOctavian Purdila flushed = iio_buffer_flush_hwfifo(indio_dev, buf, 82f4f4673bSOctavian Purdila to_wait - avail); 83f4f4673bSOctavian Purdila if (flushed <= 0) 84f4f4673bSOctavian Purdila return false; 85f4f4673bSOctavian Purdila 86f4f4673bSOctavian Purdila if (avail + flushed >= to_wait) 8737d34556SJosselin Costanzi return true; 8837d34556SJosselin Costanzi 8937d34556SJosselin Costanzi return false; 9037d34556SJosselin Costanzi } 9137d34556SJosselin Costanzi 92a980e046SJonathan Cameron /** 93a980e046SJonathan Cameron * iio_buffer_read_first_n_outer() - chrdev read for buffer access 940123635aSCristina Opriceana * @filp: File structure pointer for the char device 950123635aSCristina Opriceana * @buf: Destination buffer for iio buffer read 960123635aSCristina Opriceana * @n: First n bytes to read 970123635aSCristina Opriceana * @f_ps: Long offset provided by the user as a seek position 98a980e046SJonathan Cameron * 99a980e046SJonathan Cameron * This function relies on all buffer implementations having an 100a980e046SJonathan Cameron * iio_buffer as their first element. 1010123635aSCristina Opriceana * 1020123635aSCristina Opriceana * Return: negative values corresponding to error codes or ret != 0 1030123635aSCristina Opriceana * for ending the reading activity 104a980e046SJonathan Cameron **/ 105a980e046SJonathan Cameron ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf, 106a980e046SJonathan Cameron size_t n, loff_t *f_ps) 107a980e046SJonathan Cameron { 108a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 109a980e046SJonathan Cameron struct iio_buffer *rb = indio_dev->buffer; 11037d34556SJosselin Costanzi size_t datum_size; 111c6f67a1fSOctavian Purdila size_t to_wait; 112ee551a10SLars-Peter Clausen int ret; 113a980e046SJonathan Cameron 114f18e7a06SLars-Peter Clausen if (!indio_dev->info) 115f18e7a06SLars-Peter Clausen return -ENODEV; 116f18e7a06SLars-Peter Clausen 117a980e046SJonathan Cameron if (!rb || !rb->access->read_first_n) 118a980e046SJonathan Cameron return -EINVAL; 119ee551a10SLars-Peter Clausen 12037d34556SJosselin Costanzi datum_size = rb->bytes_per_datum; 121ee551a10SLars-Peter Clausen 12237d34556SJosselin Costanzi /* 12337d34556SJosselin Costanzi * If datum_size is 0 there will never be anything to read from the 12437d34556SJosselin Costanzi * buffer, so signal end of file now. 12537d34556SJosselin Costanzi */ 12637d34556SJosselin Costanzi if (!datum_size) 12737d34556SJosselin Costanzi return 0; 12837d34556SJosselin Costanzi 129c6f67a1fSOctavian Purdila if (filp->f_flags & O_NONBLOCK) 130c6f67a1fSOctavian Purdila to_wait = 0; 131c6f67a1fSOctavian Purdila else 132c6f67a1fSOctavian Purdila to_wait = min_t(size_t, n / datum_size, rb->watermark); 13337d34556SJosselin Costanzi 13437d34556SJosselin Costanzi do { 135ee551a10SLars-Peter Clausen ret = wait_event_interruptible(rb->pollq, 136c6f67a1fSOctavian Purdila iio_buffer_ready(indio_dev, rb, to_wait, n / datum_size)); 137ee551a10SLars-Peter Clausen if (ret) 138ee551a10SLars-Peter Clausen return ret; 13937d34556SJosselin Costanzi 14037d34556SJosselin Costanzi if (!indio_dev->info) 141ee551a10SLars-Peter Clausen return -ENODEV; 142ee551a10SLars-Peter Clausen 143ee551a10SLars-Peter Clausen ret = rb->access->read_first_n(rb, n, buf); 144ee551a10SLars-Peter Clausen if (ret == 0 && (filp->f_flags & O_NONBLOCK)) 145ee551a10SLars-Peter Clausen ret = -EAGAIN; 146ee551a10SLars-Peter Clausen } while (ret == 0); 147ee551a10SLars-Peter Clausen 148ee551a10SLars-Peter Clausen return ret; 149a980e046SJonathan Cameron } 150a980e046SJonathan Cameron 151a980e046SJonathan Cameron /** 152a980e046SJonathan Cameron * iio_buffer_poll() - poll the buffer to find out if it has data 1530123635aSCristina Opriceana * @filp: File structure pointer for device access 1540123635aSCristina Opriceana * @wait: Poll table structure pointer for which the driver adds 1550123635aSCristina Opriceana * a wait queue 1560123635aSCristina Opriceana * 1570123635aSCristina Opriceana * Return: (POLLIN | POLLRDNORM) if data is available for reading 1580123635aSCristina Opriceana * or 0 for other cases 159a980e046SJonathan Cameron */ 160a980e046SJonathan Cameron unsigned int iio_buffer_poll(struct file *filp, 161a980e046SJonathan Cameron struct poll_table_struct *wait) 162a980e046SJonathan Cameron { 163a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 164a980e046SJonathan Cameron struct iio_buffer *rb = indio_dev->buffer; 165a980e046SJonathan Cameron 166f18e7a06SLars-Peter Clausen if (!indio_dev->info) 1671bdc0293SCristina Opriceana return 0; 168f18e7a06SLars-Peter Clausen 169a980e046SJonathan Cameron poll_wait(filp, &rb->pollq, wait); 170f4f4673bSOctavian Purdila if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0)) 171a980e046SJonathan Cameron return POLLIN | POLLRDNORM; 172a980e046SJonathan Cameron return 0; 173a980e046SJonathan Cameron } 174a980e046SJonathan Cameron 175d2f0a48fSLars-Peter Clausen /** 176d2f0a48fSLars-Peter Clausen * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue 177d2f0a48fSLars-Peter Clausen * @indio_dev: The IIO device 178d2f0a48fSLars-Peter Clausen * 179d2f0a48fSLars-Peter Clausen * Wakes up the event waitqueue used for poll(). Should usually 180d2f0a48fSLars-Peter Clausen * be called when the device is unregistered. 181d2f0a48fSLars-Peter Clausen */ 182d2f0a48fSLars-Peter Clausen void iio_buffer_wakeup_poll(struct iio_dev *indio_dev) 183d2f0a48fSLars-Peter Clausen { 184d2f0a48fSLars-Peter Clausen if (!indio_dev->buffer) 185d2f0a48fSLars-Peter Clausen return; 186d2f0a48fSLars-Peter Clausen 187d2f0a48fSLars-Peter Clausen wake_up(&indio_dev->buffer->pollq); 188d2f0a48fSLars-Peter Clausen } 189d2f0a48fSLars-Peter Clausen 190a980e046SJonathan Cameron void iio_buffer_init(struct iio_buffer *buffer) 191a980e046SJonathan Cameron { 192a980e046SJonathan Cameron INIT_LIST_HEAD(&buffer->demux_list); 193705ee2c9SLars-Peter Clausen INIT_LIST_HEAD(&buffer->buffer_list); 194a980e046SJonathan Cameron init_waitqueue_head(&buffer->pollq); 1959e69c935SLars-Peter Clausen kref_init(&buffer->ref); 1964a605357SLars-Peter Clausen if (!buffer->watermark) 19737d34556SJosselin Costanzi buffer->watermark = 1; 198a980e046SJonathan Cameron } 199a980e046SJonathan Cameron EXPORT_SYMBOL(iio_buffer_init); 200a980e046SJonathan Cameron 201a980e046SJonathan Cameron static ssize_t iio_show_scan_index(struct device *dev, 202a980e046SJonathan Cameron struct device_attribute *attr, 203a980e046SJonathan Cameron char *buf) 204a980e046SJonathan Cameron { 205a980e046SJonathan Cameron return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index); 206a980e046SJonathan Cameron } 207a980e046SJonathan Cameron 208a980e046SJonathan Cameron static ssize_t iio_show_fixed_type(struct device *dev, 209a980e046SJonathan Cameron struct device_attribute *attr, 210a980e046SJonathan Cameron char *buf) 211a980e046SJonathan Cameron { 212a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 213a980e046SJonathan Cameron u8 type = this_attr->c->scan_type.endianness; 214a980e046SJonathan Cameron 215a980e046SJonathan Cameron if (type == IIO_CPU) { 216a980e046SJonathan Cameron #ifdef __LITTLE_ENDIAN 217a980e046SJonathan Cameron type = IIO_LE; 218a980e046SJonathan Cameron #else 219a980e046SJonathan Cameron type = IIO_BE; 220a980e046SJonathan Cameron #endif 221a980e046SJonathan Cameron } 2220ee8546aSSrinivas Pandruvada if (this_attr->c->scan_type.repeat > 1) 2230ee8546aSSrinivas Pandruvada return sprintf(buf, "%s:%c%d/%dX%d>>%u\n", 2240ee8546aSSrinivas Pandruvada iio_endian_prefix[type], 2250ee8546aSSrinivas Pandruvada this_attr->c->scan_type.sign, 2260ee8546aSSrinivas Pandruvada this_attr->c->scan_type.realbits, 2270ee8546aSSrinivas Pandruvada this_attr->c->scan_type.storagebits, 2280ee8546aSSrinivas Pandruvada this_attr->c->scan_type.repeat, 2290ee8546aSSrinivas Pandruvada this_attr->c->scan_type.shift); 2300ee8546aSSrinivas Pandruvada else 231a980e046SJonathan Cameron return sprintf(buf, "%s:%c%d/%d>>%u\n", 232a980e046SJonathan Cameron iio_endian_prefix[type], 233a980e046SJonathan Cameron this_attr->c->scan_type.sign, 234a980e046SJonathan Cameron this_attr->c->scan_type.realbits, 235a980e046SJonathan Cameron this_attr->c->scan_type.storagebits, 236a980e046SJonathan Cameron this_attr->c->scan_type.shift); 237a980e046SJonathan Cameron } 238a980e046SJonathan Cameron 239a980e046SJonathan Cameron static ssize_t iio_scan_el_show(struct device *dev, 240a980e046SJonathan Cameron struct device_attribute *attr, 241a980e046SJonathan Cameron char *buf) 242a980e046SJonathan Cameron { 243a980e046SJonathan Cameron int ret; 244e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 245a980e046SJonathan Cameron 2462076a20fSAlec Berg /* Ensure ret is 0 or 1. */ 2472076a20fSAlec Berg ret = !!test_bit(to_iio_dev_attr(attr)->address, 248a980e046SJonathan Cameron indio_dev->buffer->scan_mask); 249a980e046SJonathan Cameron 250a980e046SJonathan Cameron return sprintf(buf, "%d\n", ret); 251a980e046SJonathan Cameron } 252a980e046SJonathan Cameron 253217a5cf0SLars-Peter Clausen /* Note NULL used as error indicator as it doesn't make sense. */ 254217a5cf0SLars-Peter Clausen static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks, 255217a5cf0SLars-Peter Clausen unsigned int masklength, 2561e1ec286SLars-Peter Clausen const unsigned long *mask, 2571e1ec286SLars-Peter Clausen bool strict) 258217a5cf0SLars-Peter Clausen { 259217a5cf0SLars-Peter Clausen if (bitmap_empty(mask, masklength)) 260217a5cf0SLars-Peter Clausen return NULL; 261217a5cf0SLars-Peter Clausen while (*av_masks) { 2621e1ec286SLars-Peter Clausen if (strict) { 2631e1ec286SLars-Peter Clausen if (bitmap_equal(mask, av_masks, masklength)) 2641e1ec286SLars-Peter Clausen return av_masks; 2651e1ec286SLars-Peter Clausen } else { 266217a5cf0SLars-Peter Clausen if (bitmap_subset(mask, av_masks, masklength)) 267217a5cf0SLars-Peter Clausen return av_masks; 2681e1ec286SLars-Peter Clausen } 269217a5cf0SLars-Peter Clausen av_masks += BITS_TO_LONGS(masklength); 270217a5cf0SLars-Peter Clausen } 271217a5cf0SLars-Peter Clausen return NULL; 272217a5cf0SLars-Peter Clausen } 273217a5cf0SLars-Peter Clausen 274217a5cf0SLars-Peter Clausen static bool iio_validate_scan_mask(struct iio_dev *indio_dev, 275217a5cf0SLars-Peter Clausen const unsigned long *mask) 276217a5cf0SLars-Peter Clausen { 277217a5cf0SLars-Peter Clausen if (!indio_dev->setup_ops->validate_scan_mask) 278217a5cf0SLars-Peter Clausen return true; 279217a5cf0SLars-Peter Clausen 280217a5cf0SLars-Peter Clausen return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask); 281217a5cf0SLars-Peter Clausen } 282217a5cf0SLars-Peter Clausen 283217a5cf0SLars-Peter Clausen /** 284217a5cf0SLars-Peter Clausen * iio_scan_mask_set() - set particular bit in the scan mask 285217a5cf0SLars-Peter Clausen * @indio_dev: the iio device 286217a5cf0SLars-Peter Clausen * @buffer: the buffer whose scan mask we are interested in 287217a5cf0SLars-Peter Clausen * @bit: the bit to be set. 288217a5cf0SLars-Peter Clausen * 289217a5cf0SLars-Peter Clausen * Note that at this point we have no way of knowing what other 290217a5cf0SLars-Peter Clausen * buffers might request, hence this code only verifies that the 291217a5cf0SLars-Peter Clausen * individual buffers request is plausible. 292217a5cf0SLars-Peter Clausen */ 293217a5cf0SLars-Peter Clausen static int iio_scan_mask_set(struct iio_dev *indio_dev, 294217a5cf0SLars-Peter Clausen struct iio_buffer *buffer, int bit) 295217a5cf0SLars-Peter Clausen { 296217a5cf0SLars-Peter Clausen const unsigned long *mask; 297217a5cf0SLars-Peter Clausen unsigned long *trialmask; 298217a5cf0SLars-Peter Clausen 299217a5cf0SLars-Peter Clausen trialmask = kmalloc(sizeof(*trialmask)* 300217a5cf0SLars-Peter Clausen BITS_TO_LONGS(indio_dev->masklength), 301217a5cf0SLars-Peter Clausen GFP_KERNEL); 302217a5cf0SLars-Peter Clausen 303217a5cf0SLars-Peter Clausen if (trialmask == NULL) 304217a5cf0SLars-Peter Clausen return -ENOMEM; 305217a5cf0SLars-Peter Clausen if (!indio_dev->masklength) { 306231bfe53SDan Carpenter WARN(1, "Trying to set scanmask prior to registering buffer\n"); 307217a5cf0SLars-Peter Clausen goto err_invalid_mask; 308217a5cf0SLars-Peter Clausen } 309217a5cf0SLars-Peter Clausen bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength); 310217a5cf0SLars-Peter Clausen set_bit(bit, trialmask); 311217a5cf0SLars-Peter Clausen 312217a5cf0SLars-Peter Clausen if (!iio_validate_scan_mask(indio_dev, trialmask)) 313217a5cf0SLars-Peter Clausen goto err_invalid_mask; 314217a5cf0SLars-Peter Clausen 315217a5cf0SLars-Peter Clausen if (indio_dev->available_scan_masks) { 316217a5cf0SLars-Peter Clausen mask = iio_scan_mask_match(indio_dev->available_scan_masks, 317217a5cf0SLars-Peter Clausen indio_dev->masklength, 3181e1ec286SLars-Peter Clausen trialmask, false); 319217a5cf0SLars-Peter Clausen if (!mask) 320217a5cf0SLars-Peter Clausen goto err_invalid_mask; 321217a5cf0SLars-Peter Clausen } 322217a5cf0SLars-Peter Clausen bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength); 323217a5cf0SLars-Peter Clausen 324217a5cf0SLars-Peter Clausen kfree(trialmask); 325217a5cf0SLars-Peter Clausen 326217a5cf0SLars-Peter Clausen return 0; 327217a5cf0SLars-Peter Clausen 328217a5cf0SLars-Peter Clausen err_invalid_mask: 329217a5cf0SLars-Peter Clausen kfree(trialmask); 330217a5cf0SLars-Peter Clausen return -EINVAL; 331217a5cf0SLars-Peter Clausen } 332217a5cf0SLars-Peter Clausen 333a980e046SJonathan Cameron static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit) 334a980e046SJonathan Cameron { 335a980e046SJonathan Cameron clear_bit(bit, buffer->scan_mask); 336a980e046SJonathan Cameron return 0; 337a980e046SJonathan Cameron } 338a980e046SJonathan Cameron 339a980e046SJonathan Cameron static ssize_t iio_scan_el_store(struct device *dev, 340a980e046SJonathan Cameron struct device_attribute *attr, 341a980e046SJonathan Cameron const char *buf, 342a980e046SJonathan Cameron size_t len) 343a980e046SJonathan Cameron { 344a980e046SJonathan Cameron int ret; 345a980e046SJonathan Cameron bool state; 346e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 347a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 348a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 349a980e046SJonathan Cameron 350a980e046SJonathan Cameron ret = strtobool(buf, &state); 351a980e046SJonathan Cameron if (ret < 0) 352a980e046SJonathan Cameron return ret; 353a980e046SJonathan Cameron mutex_lock(&indio_dev->mlock); 354705ee2c9SLars-Peter Clausen if (iio_buffer_is_active(indio_dev->buffer)) { 355a980e046SJonathan Cameron ret = -EBUSY; 356a980e046SJonathan Cameron goto error_ret; 357a980e046SJonathan Cameron } 358a980e046SJonathan Cameron ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address); 359a980e046SJonathan Cameron if (ret < 0) 360a980e046SJonathan Cameron goto error_ret; 361a980e046SJonathan Cameron if (!state && ret) { 362a980e046SJonathan Cameron ret = iio_scan_mask_clear(buffer, this_attr->address); 363a980e046SJonathan Cameron if (ret) 364a980e046SJonathan Cameron goto error_ret; 365a980e046SJonathan Cameron } else if (state && !ret) { 366a980e046SJonathan Cameron ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address); 367a980e046SJonathan Cameron if (ret) 368a980e046SJonathan Cameron goto error_ret; 369a980e046SJonathan Cameron } 370a980e046SJonathan Cameron 371a980e046SJonathan Cameron error_ret: 372a980e046SJonathan Cameron mutex_unlock(&indio_dev->mlock); 373a980e046SJonathan Cameron 374a980e046SJonathan Cameron return ret < 0 ? ret : len; 375a980e046SJonathan Cameron 376a980e046SJonathan Cameron } 377a980e046SJonathan Cameron 378a980e046SJonathan Cameron static ssize_t iio_scan_el_ts_show(struct device *dev, 379a980e046SJonathan Cameron struct device_attribute *attr, 380a980e046SJonathan Cameron char *buf) 381a980e046SJonathan Cameron { 382e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 383a980e046SJonathan Cameron return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp); 384a980e046SJonathan Cameron } 385a980e046SJonathan Cameron 386a980e046SJonathan Cameron static ssize_t iio_scan_el_ts_store(struct device *dev, 387a980e046SJonathan Cameron struct device_attribute *attr, 388a980e046SJonathan Cameron const char *buf, 389a980e046SJonathan Cameron size_t len) 390a980e046SJonathan Cameron { 391a980e046SJonathan Cameron int ret; 392e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 393a980e046SJonathan Cameron bool state; 394a980e046SJonathan Cameron 395a980e046SJonathan Cameron ret = strtobool(buf, &state); 396a980e046SJonathan Cameron if (ret < 0) 397a980e046SJonathan Cameron return ret; 398a980e046SJonathan Cameron 399a980e046SJonathan Cameron mutex_lock(&indio_dev->mlock); 400705ee2c9SLars-Peter Clausen if (iio_buffer_is_active(indio_dev->buffer)) { 401a980e046SJonathan Cameron ret = -EBUSY; 402a980e046SJonathan Cameron goto error_ret; 403a980e046SJonathan Cameron } 404a980e046SJonathan Cameron indio_dev->buffer->scan_timestamp = state; 405a980e046SJonathan Cameron error_ret: 406a980e046SJonathan Cameron mutex_unlock(&indio_dev->mlock); 407a980e046SJonathan Cameron 408a980e046SJonathan Cameron return ret ? ret : len; 409a980e046SJonathan Cameron } 410a980e046SJonathan Cameron 411a980e046SJonathan Cameron static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev, 412a980e046SJonathan Cameron const struct iio_chan_spec *chan) 413a980e046SJonathan Cameron { 414a980e046SJonathan Cameron int ret, attrcount = 0; 415a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 416a980e046SJonathan Cameron 417a980e046SJonathan Cameron ret = __iio_add_chan_devattr("index", 418a980e046SJonathan Cameron chan, 419a980e046SJonathan Cameron &iio_show_scan_index, 420a980e046SJonathan Cameron NULL, 421a980e046SJonathan Cameron 0, 4223704432fSJonathan Cameron IIO_SEPARATE, 423a980e046SJonathan Cameron &indio_dev->dev, 424a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 425a980e046SJonathan Cameron if (ret) 42692825ff9SHartmut Knaack return ret; 427a980e046SJonathan Cameron attrcount++; 428a980e046SJonathan Cameron ret = __iio_add_chan_devattr("type", 429a980e046SJonathan Cameron chan, 430a980e046SJonathan Cameron &iio_show_fixed_type, 431a980e046SJonathan Cameron NULL, 432a980e046SJonathan Cameron 0, 433a980e046SJonathan Cameron 0, 434a980e046SJonathan Cameron &indio_dev->dev, 435a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 436a980e046SJonathan Cameron if (ret) 43792825ff9SHartmut Knaack return ret; 438a980e046SJonathan Cameron attrcount++; 439a980e046SJonathan Cameron if (chan->type != IIO_TIMESTAMP) 440a980e046SJonathan Cameron ret = __iio_add_chan_devattr("en", 441a980e046SJonathan Cameron chan, 442a980e046SJonathan Cameron &iio_scan_el_show, 443a980e046SJonathan Cameron &iio_scan_el_store, 444a980e046SJonathan Cameron chan->scan_index, 445a980e046SJonathan Cameron 0, 446a980e046SJonathan Cameron &indio_dev->dev, 447a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 448a980e046SJonathan Cameron else 449a980e046SJonathan Cameron ret = __iio_add_chan_devattr("en", 450a980e046SJonathan Cameron chan, 451a980e046SJonathan Cameron &iio_scan_el_ts_show, 452a980e046SJonathan Cameron &iio_scan_el_ts_store, 453a980e046SJonathan Cameron chan->scan_index, 454a980e046SJonathan Cameron 0, 455a980e046SJonathan Cameron &indio_dev->dev, 456a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 4579572588cSPeter Meerwald if (ret) 45892825ff9SHartmut Knaack return ret; 459a980e046SJonathan Cameron attrcount++; 460a980e046SJonathan Cameron ret = attrcount; 461a980e046SJonathan Cameron return ret; 462a980e046SJonathan Cameron } 463a980e046SJonathan Cameron 46408e7e0adSLars-Peter Clausen static ssize_t iio_buffer_read_length(struct device *dev, 465a980e046SJonathan Cameron struct device_attribute *attr, 466a980e046SJonathan Cameron char *buf) 467a980e046SJonathan Cameron { 468e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 469a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 470a980e046SJonathan Cameron 47137495660SLars-Peter Clausen return sprintf(buf, "%d\n", buffer->length); 472a980e046SJonathan Cameron } 473a980e046SJonathan Cameron 47408e7e0adSLars-Peter Clausen static ssize_t iio_buffer_write_length(struct device *dev, 475a980e046SJonathan Cameron struct device_attribute *attr, 47608e7e0adSLars-Peter Clausen const char *buf, size_t len) 477a980e046SJonathan Cameron { 478e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 479a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 480948ad205SLars-Peter Clausen unsigned int val; 481948ad205SLars-Peter Clausen int ret; 482a980e046SJonathan Cameron 483948ad205SLars-Peter Clausen ret = kstrtouint(buf, 10, &val); 484a980e046SJonathan Cameron if (ret) 485a980e046SJonathan Cameron return ret; 486a980e046SJonathan Cameron 48737495660SLars-Peter Clausen if (val == buffer->length) 488a980e046SJonathan Cameron return len; 489a980e046SJonathan Cameron 490a980e046SJonathan Cameron mutex_lock(&indio_dev->mlock); 491705ee2c9SLars-Peter Clausen if (iio_buffer_is_active(indio_dev->buffer)) { 492a980e046SJonathan Cameron ret = -EBUSY; 493a980e046SJonathan Cameron } else { 494a980e046SJonathan Cameron buffer->access->set_length(buffer, val); 495a980e046SJonathan Cameron ret = 0; 496a980e046SJonathan Cameron } 49737d34556SJosselin Costanzi if (ret) 49837d34556SJosselin Costanzi goto out; 49937d34556SJosselin Costanzi if (buffer->length && buffer->length < buffer->watermark) 50037d34556SJosselin Costanzi buffer->watermark = buffer->length; 50137d34556SJosselin Costanzi out: 502a980e046SJonathan Cameron mutex_unlock(&indio_dev->mlock); 503a980e046SJonathan Cameron 504a980e046SJonathan Cameron return ret ? ret : len; 505a980e046SJonathan Cameron } 506a980e046SJonathan Cameron 50708e7e0adSLars-Peter Clausen static ssize_t iio_buffer_show_enable(struct device *dev, 508a980e046SJonathan Cameron struct device_attribute *attr, 509a980e046SJonathan Cameron char *buf) 510a980e046SJonathan Cameron { 511e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 512705ee2c9SLars-Peter Clausen return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer)); 513a980e046SJonathan Cameron } 514a980e046SJonathan Cameron 515182b4905SLars-Peter Clausen static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev, 516182b4905SLars-Peter Clausen unsigned int scan_index) 517182b4905SLars-Peter Clausen { 518182b4905SLars-Peter Clausen const struct iio_chan_spec *ch; 519182b4905SLars-Peter Clausen unsigned int bytes; 520182b4905SLars-Peter Clausen 521182b4905SLars-Peter Clausen ch = iio_find_channel_from_si(indio_dev, scan_index); 522182b4905SLars-Peter Clausen bytes = ch->scan_type.storagebits / 8; 523182b4905SLars-Peter Clausen if (ch->scan_type.repeat > 1) 524182b4905SLars-Peter Clausen bytes *= ch->scan_type.repeat; 525182b4905SLars-Peter Clausen return bytes; 526182b4905SLars-Peter Clausen } 527182b4905SLars-Peter Clausen 528182b4905SLars-Peter Clausen static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev) 529182b4905SLars-Peter Clausen { 530182b4905SLars-Peter Clausen return iio_storage_bytes_for_si(indio_dev, 531182b4905SLars-Peter Clausen indio_dev->scan_index_timestamp); 532182b4905SLars-Peter Clausen } 533182b4905SLars-Peter Clausen 534183f4173SPeter Meerwald static int iio_compute_scan_bytes(struct iio_dev *indio_dev, 535183f4173SPeter Meerwald const unsigned long *mask, bool timestamp) 536a980e046SJonathan Cameron { 537a980e046SJonathan Cameron unsigned bytes = 0; 538a980e046SJonathan Cameron int length, i; 539a980e046SJonathan Cameron 540a980e046SJonathan Cameron /* How much space will the demuxed element take? */ 541a980e046SJonathan Cameron for_each_set_bit(i, mask, 542a980e046SJonathan Cameron indio_dev->masklength) { 543182b4905SLars-Peter Clausen length = iio_storage_bytes_for_si(indio_dev, i); 544a980e046SJonathan Cameron bytes = ALIGN(bytes, length); 545a980e046SJonathan Cameron bytes += length; 546a980e046SJonathan Cameron } 547182b4905SLars-Peter Clausen 548a980e046SJonathan Cameron if (timestamp) { 549182b4905SLars-Peter Clausen length = iio_storage_bytes_for_timestamp(indio_dev); 550a980e046SJonathan Cameron bytes = ALIGN(bytes, length); 551a980e046SJonathan Cameron bytes += length; 552a980e046SJonathan Cameron } 553a980e046SJonathan Cameron return bytes; 554a980e046SJonathan Cameron } 555a980e046SJonathan Cameron 5569e69c935SLars-Peter Clausen static void iio_buffer_activate(struct iio_dev *indio_dev, 5579e69c935SLars-Peter Clausen struct iio_buffer *buffer) 5589e69c935SLars-Peter Clausen { 5599e69c935SLars-Peter Clausen iio_buffer_get(buffer); 5609e69c935SLars-Peter Clausen list_add(&buffer->buffer_list, &indio_dev->buffer_list); 5619e69c935SLars-Peter Clausen } 5629e69c935SLars-Peter Clausen 5639e69c935SLars-Peter Clausen static void iio_buffer_deactivate(struct iio_buffer *buffer) 5649e69c935SLars-Peter Clausen { 5659e69c935SLars-Peter Clausen list_del_init(&buffer->buffer_list); 56637d34556SJosselin Costanzi wake_up_interruptible(&buffer->pollq); 5679e69c935SLars-Peter Clausen iio_buffer_put(buffer); 5689e69c935SLars-Peter Clausen } 5699e69c935SLars-Peter Clausen 5701250186aSLars-Peter Clausen static void iio_buffer_deactivate_all(struct iio_dev *indio_dev) 5711250186aSLars-Peter Clausen { 5721250186aSLars-Peter Clausen struct iio_buffer *buffer, *_buffer; 5731250186aSLars-Peter Clausen 5741250186aSLars-Peter Clausen list_for_each_entry_safe(buffer, _buffer, 5751250186aSLars-Peter Clausen &indio_dev->buffer_list, buffer_list) 5761250186aSLars-Peter Clausen iio_buffer_deactivate(buffer); 5771250186aSLars-Peter Clausen } 5781250186aSLars-Peter Clausen 579e18a2ad4SLars-Peter Clausen static int iio_buffer_enable(struct iio_buffer *buffer, 580e18a2ad4SLars-Peter Clausen struct iio_dev *indio_dev) 581e18a2ad4SLars-Peter Clausen { 582e18a2ad4SLars-Peter Clausen if (!buffer->access->enable) 583e18a2ad4SLars-Peter Clausen return 0; 584e18a2ad4SLars-Peter Clausen return buffer->access->enable(buffer, indio_dev); 585e18a2ad4SLars-Peter Clausen } 586e18a2ad4SLars-Peter Clausen 587e18a2ad4SLars-Peter Clausen static int iio_buffer_disable(struct iio_buffer *buffer, 588e18a2ad4SLars-Peter Clausen struct iio_dev *indio_dev) 589e18a2ad4SLars-Peter Clausen { 590e18a2ad4SLars-Peter Clausen if (!buffer->access->disable) 591e18a2ad4SLars-Peter Clausen return 0; 592e18a2ad4SLars-Peter Clausen return buffer->access->disable(buffer, indio_dev); 593e18a2ad4SLars-Peter Clausen } 594e18a2ad4SLars-Peter Clausen 5958e050996SLars-Peter Clausen static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev, 5968e050996SLars-Peter Clausen struct iio_buffer *buffer) 5978e050996SLars-Peter Clausen { 5988e050996SLars-Peter Clausen unsigned int bytes; 5998e050996SLars-Peter Clausen 6008e050996SLars-Peter Clausen if (!buffer->access->set_bytes_per_datum) 6018e050996SLars-Peter Clausen return; 6028e050996SLars-Peter Clausen 6038e050996SLars-Peter Clausen bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask, 6048e050996SLars-Peter Clausen buffer->scan_timestamp); 6058e050996SLars-Peter Clausen 6068e050996SLars-Peter Clausen buffer->access->set_bytes_per_datum(buffer, bytes); 6078e050996SLars-Peter Clausen } 6088e050996SLars-Peter Clausen 609fcc1b2f5SLars-Peter Clausen static int iio_buffer_request_update(struct iio_dev *indio_dev, 610fcc1b2f5SLars-Peter Clausen struct iio_buffer *buffer) 611fcc1b2f5SLars-Peter Clausen { 612fcc1b2f5SLars-Peter Clausen int ret; 613fcc1b2f5SLars-Peter Clausen 614fcc1b2f5SLars-Peter Clausen iio_buffer_update_bytes_per_datum(indio_dev, buffer); 615fcc1b2f5SLars-Peter Clausen if (buffer->access->request_update) { 616fcc1b2f5SLars-Peter Clausen ret = buffer->access->request_update(buffer); 617fcc1b2f5SLars-Peter Clausen if (ret) { 618fcc1b2f5SLars-Peter Clausen dev_dbg(&indio_dev->dev, 619fcc1b2f5SLars-Peter Clausen "Buffer not started: buffer parameter update failed (%d)\n", 620fcc1b2f5SLars-Peter Clausen ret); 621fcc1b2f5SLars-Peter Clausen return ret; 622fcc1b2f5SLars-Peter Clausen } 623fcc1b2f5SLars-Peter Clausen } 624fcc1b2f5SLars-Peter Clausen 625fcc1b2f5SLars-Peter Clausen return 0; 626fcc1b2f5SLars-Peter Clausen } 627fcc1b2f5SLars-Peter Clausen 628248be5aaSLars-Peter Clausen static void iio_free_scan_mask(struct iio_dev *indio_dev, 629248be5aaSLars-Peter Clausen const unsigned long *mask) 630248be5aaSLars-Peter Clausen { 631248be5aaSLars-Peter Clausen /* If the mask is dynamically allocated free it, otherwise do nothing */ 632248be5aaSLars-Peter Clausen if (!indio_dev->available_scan_masks) 633248be5aaSLars-Peter Clausen kfree(mask); 634248be5aaSLars-Peter Clausen } 635248be5aaSLars-Peter Clausen 6366e509c4dSLars-Peter Clausen struct iio_device_config { 6376e509c4dSLars-Peter Clausen unsigned int mode; 638f0566c0cSLars-Peter Clausen unsigned int watermark; 6396e509c4dSLars-Peter Clausen const unsigned long *scan_mask; 6406e509c4dSLars-Peter Clausen unsigned int scan_bytes; 6416e509c4dSLars-Peter Clausen bool scan_timestamp; 6426e509c4dSLars-Peter Clausen }; 6436e509c4dSLars-Peter Clausen 6446e509c4dSLars-Peter Clausen static int iio_verify_update(struct iio_dev *indio_dev, 6456e509c4dSLars-Peter Clausen struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer, 6466e509c4dSLars-Peter Clausen struct iio_device_config *config) 6476e509c4dSLars-Peter Clausen { 6486e509c4dSLars-Peter Clausen unsigned long *compound_mask; 6496e509c4dSLars-Peter Clausen const unsigned long *scan_mask; 6501e1ec286SLars-Peter Clausen bool strict_scanmask = false; 6516e509c4dSLars-Peter Clausen struct iio_buffer *buffer; 6526e509c4dSLars-Peter Clausen bool scan_timestamp; 653225d59adSLars-Peter Clausen unsigned int modes; 6546e509c4dSLars-Peter Clausen 6556e509c4dSLars-Peter Clausen memset(config, 0, sizeof(*config)); 656*1bef2c1dSIrina Tirdea config->watermark = ~0; 6576e509c4dSLars-Peter Clausen 6586e509c4dSLars-Peter Clausen /* 6596e509c4dSLars-Peter Clausen * If there is just one buffer and we are removing it there is nothing 6606e509c4dSLars-Peter Clausen * to verify. 6616e509c4dSLars-Peter Clausen */ 6626e509c4dSLars-Peter Clausen if (remove_buffer && !insert_buffer && 6636e509c4dSLars-Peter Clausen list_is_singular(&indio_dev->buffer_list)) 6646e509c4dSLars-Peter Clausen return 0; 6656e509c4dSLars-Peter Clausen 666225d59adSLars-Peter Clausen modes = indio_dev->modes; 667225d59adSLars-Peter Clausen 668225d59adSLars-Peter Clausen list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 669225d59adSLars-Peter Clausen if (buffer == remove_buffer) 670225d59adSLars-Peter Clausen continue; 671225d59adSLars-Peter Clausen modes &= buffer->access->modes; 672f0566c0cSLars-Peter Clausen config->watermark = min(config->watermark, buffer->watermark); 673225d59adSLars-Peter Clausen } 674225d59adSLars-Peter Clausen 675f0566c0cSLars-Peter Clausen if (insert_buffer) { 676225d59adSLars-Peter Clausen modes &= insert_buffer->access->modes; 677f0566c0cSLars-Peter Clausen config->watermark = min(config->watermark, 678f0566c0cSLars-Peter Clausen insert_buffer->watermark); 679f0566c0cSLars-Peter Clausen } 680225d59adSLars-Peter Clausen 6816e509c4dSLars-Peter Clausen /* Definitely possible for devices to support both of these. */ 682225d59adSLars-Peter Clausen if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) { 6836e509c4dSLars-Peter Clausen config->mode = INDIO_BUFFER_TRIGGERED; 684225d59adSLars-Peter Clausen } else if (modes & INDIO_BUFFER_HARDWARE) { 6851e1ec286SLars-Peter Clausen /* 6861e1ec286SLars-Peter Clausen * Keep things simple for now and only allow a single buffer to 6871e1ec286SLars-Peter Clausen * be connected in hardware mode. 6881e1ec286SLars-Peter Clausen */ 6891e1ec286SLars-Peter Clausen if (insert_buffer && !list_empty(&indio_dev->buffer_list)) 6901e1ec286SLars-Peter Clausen return -EINVAL; 6916e509c4dSLars-Peter Clausen config->mode = INDIO_BUFFER_HARDWARE; 6921e1ec286SLars-Peter Clausen strict_scanmask = true; 693225d59adSLars-Peter Clausen } else if (modes & INDIO_BUFFER_SOFTWARE) { 6946e509c4dSLars-Peter Clausen config->mode = INDIO_BUFFER_SOFTWARE; 6956e509c4dSLars-Peter Clausen } else { 6966e509c4dSLars-Peter Clausen /* Can only occur on first buffer */ 6976e509c4dSLars-Peter Clausen if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 6986e509c4dSLars-Peter Clausen dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n"); 6996e509c4dSLars-Peter Clausen return -EINVAL; 7006e509c4dSLars-Peter Clausen } 7016e509c4dSLars-Peter Clausen 7026e509c4dSLars-Peter Clausen /* What scan mask do we actually have? */ 7036e509c4dSLars-Peter Clausen compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), 7046e509c4dSLars-Peter Clausen sizeof(long), GFP_KERNEL); 7056e509c4dSLars-Peter Clausen if (compound_mask == NULL) 7066e509c4dSLars-Peter Clausen return -ENOMEM; 7076e509c4dSLars-Peter Clausen 7086e509c4dSLars-Peter Clausen scan_timestamp = false; 7096e509c4dSLars-Peter Clausen 7106e509c4dSLars-Peter Clausen list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 7116e509c4dSLars-Peter Clausen if (buffer == remove_buffer) 7126e509c4dSLars-Peter Clausen continue; 7136e509c4dSLars-Peter Clausen bitmap_or(compound_mask, compound_mask, buffer->scan_mask, 7146e509c4dSLars-Peter Clausen indio_dev->masklength); 7156e509c4dSLars-Peter Clausen scan_timestamp |= buffer->scan_timestamp; 7166e509c4dSLars-Peter Clausen } 7176e509c4dSLars-Peter Clausen 7186e509c4dSLars-Peter Clausen if (insert_buffer) { 7196e509c4dSLars-Peter Clausen bitmap_or(compound_mask, compound_mask, 7206e509c4dSLars-Peter Clausen insert_buffer->scan_mask, indio_dev->masklength); 7216e509c4dSLars-Peter Clausen scan_timestamp |= insert_buffer->scan_timestamp; 7226e509c4dSLars-Peter Clausen } 7236e509c4dSLars-Peter Clausen 7246e509c4dSLars-Peter Clausen if (indio_dev->available_scan_masks) { 7256e509c4dSLars-Peter Clausen scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks, 7266e509c4dSLars-Peter Clausen indio_dev->masklength, 7271e1ec286SLars-Peter Clausen compound_mask, 7281e1ec286SLars-Peter Clausen strict_scanmask); 7296e509c4dSLars-Peter Clausen kfree(compound_mask); 7306e509c4dSLars-Peter Clausen if (scan_mask == NULL) 7316e509c4dSLars-Peter Clausen return -EINVAL; 7326e509c4dSLars-Peter Clausen } else { 7336e509c4dSLars-Peter Clausen scan_mask = compound_mask; 7346e509c4dSLars-Peter Clausen } 7356e509c4dSLars-Peter Clausen 7366e509c4dSLars-Peter Clausen config->scan_bytes = iio_compute_scan_bytes(indio_dev, 7376e509c4dSLars-Peter Clausen scan_mask, scan_timestamp); 7386e509c4dSLars-Peter Clausen config->scan_mask = scan_mask; 7396e509c4dSLars-Peter Clausen config->scan_timestamp = scan_timestamp; 7406e509c4dSLars-Peter Clausen 7416e509c4dSLars-Peter Clausen return 0; 7426e509c4dSLars-Peter Clausen } 7436e509c4dSLars-Peter Clausen 744623d74e3SLars-Peter Clausen static int iio_enable_buffers(struct iio_dev *indio_dev, 745623d74e3SLars-Peter Clausen struct iio_device_config *config) 746623d74e3SLars-Peter Clausen { 747e18a2ad4SLars-Peter Clausen struct iio_buffer *buffer; 748623d74e3SLars-Peter Clausen int ret; 749623d74e3SLars-Peter Clausen 750623d74e3SLars-Peter Clausen indio_dev->active_scan_mask = config->scan_mask; 751623d74e3SLars-Peter Clausen indio_dev->scan_timestamp = config->scan_timestamp; 752623d74e3SLars-Peter Clausen indio_dev->scan_bytes = config->scan_bytes; 753623d74e3SLars-Peter Clausen 754623d74e3SLars-Peter Clausen iio_update_demux(indio_dev); 755623d74e3SLars-Peter Clausen 756623d74e3SLars-Peter Clausen /* Wind up again */ 757623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->preenable) { 758623d74e3SLars-Peter Clausen ret = indio_dev->setup_ops->preenable(indio_dev); 759623d74e3SLars-Peter Clausen if (ret) { 760623d74e3SLars-Peter Clausen dev_dbg(&indio_dev->dev, 761623d74e3SLars-Peter Clausen "Buffer not started: buffer preenable failed (%d)\n", ret); 762623d74e3SLars-Peter Clausen goto err_undo_config; 763623d74e3SLars-Peter Clausen } 764623d74e3SLars-Peter Clausen } 765623d74e3SLars-Peter Clausen 766623d74e3SLars-Peter Clausen if (indio_dev->info->update_scan_mode) { 767623d74e3SLars-Peter Clausen ret = indio_dev->info 768623d74e3SLars-Peter Clausen ->update_scan_mode(indio_dev, 769623d74e3SLars-Peter Clausen indio_dev->active_scan_mask); 770623d74e3SLars-Peter Clausen if (ret < 0) { 771623d74e3SLars-Peter Clausen dev_dbg(&indio_dev->dev, 772623d74e3SLars-Peter Clausen "Buffer not started: update scan mode failed (%d)\n", 773623d74e3SLars-Peter Clausen ret); 774623d74e3SLars-Peter Clausen goto err_run_postdisable; 775623d74e3SLars-Peter Clausen } 776623d74e3SLars-Peter Clausen } 777623d74e3SLars-Peter Clausen 778f0566c0cSLars-Peter Clausen if (indio_dev->info->hwfifo_set_watermark) 779f0566c0cSLars-Peter Clausen indio_dev->info->hwfifo_set_watermark(indio_dev, 780f0566c0cSLars-Peter Clausen config->watermark); 781f0566c0cSLars-Peter Clausen 782e18a2ad4SLars-Peter Clausen list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 783e18a2ad4SLars-Peter Clausen ret = iio_buffer_enable(buffer, indio_dev); 784e18a2ad4SLars-Peter Clausen if (ret) 785e18a2ad4SLars-Peter Clausen goto err_disable_buffers; 786e18a2ad4SLars-Peter Clausen } 787e18a2ad4SLars-Peter Clausen 788623d74e3SLars-Peter Clausen indio_dev->currentmode = config->mode; 789623d74e3SLars-Peter Clausen 790623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->postenable) { 791623d74e3SLars-Peter Clausen ret = indio_dev->setup_ops->postenable(indio_dev); 792623d74e3SLars-Peter Clausen if (ret) { 793623d74e3SLars-Peter Clausen dev_dbg(&indio_dev->dev, 794623d74e3SLars-Peter Clausen "Buffer not started: postenable failed (%d)\n", ret); 795e18a2ad4SLars-Peter Clausen goto err_disable_buffers; 796623d74e3SLars-Peter Clausen } 797623d74e3SLars-Peter Clausen } 798623d74e3SLars-Peter Clausen 799623d74e3SLars-Peter Clausen return 0; 800623d74e3SLars-Peter Clausen 801e18a2ad4SLars-Peter Clausen err_disable_buffers: 802e18a2ad4SLars-Peter Clausen list_for_each_entry_continue_reverse(buffer, &indio_dev->buffer_list, 803e18a2ad4SLars-Peter Clausen buffer_list) 804e18a2ad4SLars-Peter Clausen iio_buffer_disable(buffer, indio_dev); 805623d74e3SLars-Peter Clausen err_run_postdisable: 806623d74e3SLars-Peter Clausen indio_dev->currentmode = INDIO_DIRECT_MODE; 807623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->postdisable) 808623d74e3SLars-Peter Clausen indio_dev->setup_ops->postdisable(indio_dev); 809623d74e3SLars-Peter Clausen err_undo_config: 810623d74e3SLars-Peter Clausen indio_dev->active_scan_mask = NULL; 811623d74e3SLars-Peter Clausen 812623d74e3SLars-Peter Clausen return ret; 813623d74e3SLars-Peter Clausen } 814623d74e3SLars-Peter Clausen 815623d74e3SLars-Peter Clausen static int iio_disable_buffers(struct iio_dev *indio_dev) 816623d74e3SLars-Peter Clausen { 817e18a2ad4SLars-Peter Clausen struct iio_buffer *buffer; 8181250186aSLars-Peter Clausen int ret = 0; 8191250186aSLars-Peter Clausen int ret2; 820623d74e3SLars-Peter Clausen 821623d74e3SLars-Peter Clausen /* Wind down existing buffers - iff there are any */ 822623d74e3SLars-Peter Clausen if (list_empty(&indio_dev->buffer_list)) 823623d74e3SLars-Peter Clausen return 0; 824623d74e3SLars-Peter Clausen 8251250186aSLars-Peter Clausen /* 8261250186aSLars-Peter Clausen * If things go wrong at some step in disable we still need to continue 8271250186aSLars-Peter Clausen * to perform the other steps, otherwise we leave the device in a 8281250186aSLars-Peter Clausen * inconsistent state. We return the error code for the first error we 8291250186aSLars-Peter Clausen * encountered. 8301250186aSLars-Peter Clausen */ 8311250186aSLars-Peter Clausen 832623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->predisable) { 8331250186aSLars-Peter Clausen ret2 = indio_dev->setup_ops->predisable(indio_dev); 8341250186aSLars-Peter Clausen if (ret2 && !ret) 8351250186aSLars-Peter Clausen ret = ret2; 836623d74e3SLars-Peter Clausen } 837623d74e3SLars-Peter Clausen 838e18a2ad4SLars-Peter Clausen list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 839e18a2ad4SLars-Peter Clausen ret2 = iio_buffer_disable(buffer, indio_dev); 840e18a2ad4SLars-Peter Clausen if (ret2 && !ret) 841e18a2ad4SLars-Peter Clausen ret = ret2; 842e18a2ad4SLars-Peter Clausen } 843e18a2ad4SLars-Peter Clausen 844623d74e3SLars-Peter Clausen indio_dev->currentmode = INDIO_DIRECT_MODE; 845623d74e3SLars-Peter Clausen 846623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->postdisable) { 8471250186aSLars-Peter Clausen ret2 = indio_dev->setup_ops->postdisable(indio_dev); 8481250186aSLars-Peter Clausen if (ret2 && !ret) 8491250186aSLars-Peter Clausen ret = ret2; 850623d74e3SLars-Peter Clausen } 851623d74e3SLars-Peter Clausen 8521250186aSLars-Peter Clausen iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask); 8531250186aSLars-Peter Clausen indio_dev->active_scan_mask = NULL; 8541250186aSLars-Peter Clausen 8551250186aSLars-Peter Clausen return ret; 856623d74e3SLars-Peter Clausen } 857623d74e3SLars-Peter Clausen 858a9519456SLars-Peter Clausen static int __iio_update_buffers(struct iio_dev *indio_dev, 85984b36ce5SJonathan Cameron struct iio_buffer *insert_buffer, 86084b36ce5SJonathan Cameron struct iio_buffer *remove_buffer) 861a980e046SJonathan Cameron { 8626e509c4dSLars-Peter Clausen struct iio_device_config new_config; 8631250186aSLars-Peter Clausen int ret; 8646e509c4dSLars-Peter Clausen 8656e509c4dSLars-Peter Clausen ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer, 8666e509c4dSLars-Peter Clausen &new_config); 8676e509c4dSLars-Peter Clausen if (ret) 8686e509c4dSLars-Peter Clausen return ret; 869a980e046SJonathan Cameron 870fcc1b2f5SLars-Peter Clausen if (insert_buffer) { 871fcc1b2f5SLars-Peter Clausen ret = iio_buffer_request_update(indio_dev, insert_buffer); 872fcc1b2f5SLars-Peter Clausen if (ret) 8736e509c4dSLars-Peter Clausen goto err_free_config; 874fcc1b2f5SLars-Peter Clausen } 875fcc1b2f5SLars-Peter Clausen 876623d74e3SLars-Peter Clausen ret = iio_disable_buffers(indio_dev); 8771250186aSLars-Peter Clausen if (ret) 8781250186aSLars-Peter Clausen goto err_deactivate_all; 879623d74e3SLars-Peter Clausen 88084b36ce5SJonathan Cameron if (remove_buffer) 8819e69c935SLars-Peter Clausen iio_buffer_deactivate(remove_buffer); 88284b36ce5SJonathan Cameron if (insert_buffer) 8839e69c935SLars-Peter Clausen iio_buffer_activate(indio_dev, insert_buffer); 88484b36ce5SJonathan Cameron 88584b36ce5SJonathan Cameron /* If no buffers in list, we are done */ 8861250186aSLars-Peter Clausen if (list_empty(&indio_dev->buffer_list)) 88784b36ce5SJonathan Cameron return 0; 888a980e046SJonathan Cameron 889623d74e3SLars-Peter Clausen ret = iio_enable_buffers(indio_dev, &new_config); 8901250186aSLars-Peter Clausen if (ret) 8911250186aSLars-Peter Clausen goto err_deactivate_all; 892623d74e3SLars-Peter Clausen 893623d74e3SLars-Peter Clausen return 0; 8946e509c4dSLars-Peter Clausen 8951250186aSLars-Peter Clausen err_deactivate_all: 8961250186aSLars-Peter Clausen /* 8971250186aSLars-Peter Clausen * We've already verified that the config is valid earlier. If things go 8981250186aSLars-Peter Clausen * wrong in either enable or disable the most likely reason is an IO 8991250186aSLars-Peter Clausen * error from the device. In this case there is no good recovery 9001250186aSLars-Peter Clausen * strategy. Just make sure to disable everything and leave the device 9011250186aSLars-Peter Clausen * in a sane state. With a bit of luck the device might come back to 9021250186aSLars-Peter Clausen * life again later and userspace can try again. 9031250186aSLars-Peter Clausen */ 9041250186aSLars-Peter Clausen iio_buffer_deactivate_all(indio_dev); 9051250186aSLars-Peter Clausen 9066e509c4dSLars-Peter Clausen err_free_config: 9076e509c4dSLars-Peter Clausen iio_free_scan_mask(indio_dev, new_config.scan_mask); 9086e509c4dSLars-Peter Clausen return ret; 90984b36ce5SJonathan Cameron } 910a9519456SLars-Peter Clausen 911a9519456SLars-Peter Clausen int iio_update_buffers(struct iio_dev *indio_dev, 912a9519456SLars-Peter Clausen struct iio_buffer *insert_buffer, 913a9519456SLars-Peter Clausen struct iio_buffer *remove_buffer) 914a9519456SLars-Peter Clausen { 915a9519456SLars-Peter Clausen int ret; 916a9519456SLars-Peter Clausen 9173909fab5SLars-Peter Clausen if (insert_buffer == remove_buffer) 9183909fab5SLars-Peter Clausen return 0; 9193909fab5SLars-Peter Clausen 920a9519456SLars-Peter Clausen mutex_lock(&indio_dev->info_exist_lock); 921a9519456SLars-Peter Clausen mutex_lock(&indio_dev->mlock); 922a9519456SLars-Peter Clausen 9233909fab5SLars-Peter Clausen if (insert_buffer && iio_buffer_is_active(insert_buffer)) 9243909fab5SLars-Peter Clausen insert_buffer = NULL; 9253909fab5SLars-Peter Clausen 9263909fab5SLars-Peter Clausen if (remove_buffer && !iio_buffer_is_active(remove_buffer)) 9273909fab5SLars-Peter Clausen remove_buffer = NULL; 9283909fab5SLars-Peter Clausen 9293909fab5SLars-Peter Clausen if (!insert_buffer && !remove_buffer) { 9303909fab5SLars-Peter Clausen ret = 0; 9313909fab5SLars-Peter Clausen goto out_unlock; 9323909fab5SLars-Peter Clausen } 9333909fab5SLars-Peter Clausen 934a9519456SLars-Peter Clausen if (indio_dev->info == NULL) { 935a9519456SLars-Peter Clausen ret = -ENODEV; 936a9519456SLars-Peter Clausen goto out_unlock; 937a9519456SLars-Peter Clausen } 938a9519456SLars-Peter Clausen 939a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer); 940a9519456SLars-Peter Clausen 941a9519456SLars-Peter Clausen out_unlock: 942a9519456SLars-Peter Clausen mutex_unlock(&indio_dev->mlock); 943a9519456SLars-Peter Clausen mutex_unlock(&indio_dev->info_exist_lock); 944a9519456SLars-Peter Clausen 945a9519456SLars-Peter Clausen return ret; 946a9519456SLars-Peter Clausen } 94784b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_update_buffers); 94884b36ce5SJonathan Cameron 949623d74e3SLars-Peter Clausen void iio_disable_all_buffers(struct iio_dev *indio_dev) 950623d74e3SLars-Peter Clausen { 951623d74e3SLars-Peter Clausen iio_disable_buffers(indio_dev); 9521250186aSLars-Peter Clausen iio_buffer_deactivate_all(indio_dev); 953623d74e3SLars-Peter Clausen } 954623d74e3SLars-Peter Clausen 95508e7e0adSLars-Peter Clausen static ssize_t iio_buffer_store_enable(struct device *dev, 95684b36ce5SJonathan Cameron struct device_attribute *attr, 95784b36ce5SJonathan Cameron const char *buf, 95884b36ce5SJonathan Cameron size_t len) 95984b36ce5SJonathan Cameron { 96084b36ce5SJonathan Cameron int ret; 96184b36ce5SJonathan Cameron bool requested_state; 96284b36ce5SJonathan Cameron struct iio_dev *indio_dev = dev_to_iio_dev(dev); 96384b36ce5SJonathan Cameron bool inlist; 96484b36ce5SJonathan Cameron 96584b36ce5SJonathan Cameron ret = strtobool(buf, &requested_state); 96684b36ce5SJonathan Cameron if (ret < 0) 96784b36ce5SJonathan Cameron return ret; 96884b36ce5SJonathan Cameron 96984b36ce5SJonathan Cameron mutex_lock(&indio_dev->mlock); 97084b36ce5SJonathan Cameron 97184b36ce5SJonathan Cameron /* Find out if it is in the list */ 972705ee2c9SLars-Peter Clausen inlist = iio_buffer_is_active(indio_dev->buffer); 97384b36ce5SJonathan Cameron /* Already in desired state */ 97484b36ce5SJonathan Cameron if (inlist == requested_state) 97584b36ce5SJonathan Cameron goto done; 97684b36ce5SJonathan Cameron 97784b36ce5SJonathan Cameron if (requested_state) 978a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, 97984b36ce5SJonathan Cameron indio_dev->buffer, NULL); 98084b36ce5SJonathan Cameron else 981a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, 98284b36ce5SJonathan Cameron NULL, indio_dev->buffer); 98384b36ce5SJonathan Cameron 98484b36ce5SJonathan Cameron done: 98584b36ce5SJonathan Cameron mutex_unlock(&indio_dev->mlock); 98684b36ce5SJonathan Cameron return (ret < 0) ? ret : len; 98784b36ce5SJonathan Cameron } 98884b36ce5SJonathan Cameron 989d967cb6bSLars-Peter Clausen static const char * const iio_scan_elements_group_name = "scan_elements"; 990d967cb6bSLars-Peter Clausen 99137d34556SJosselin Costanzi static ssize_t iio_buffer_show_watermark(struct device *dev, 99237d34556SJosselin Costanzi struct device_attribute *attr, 99337d34556SJosselin Costanzi char *buf) 99437d34556SJosselin Costanzi { 99537d34556SJosselin Costanzi struct iio_dev *indio_dev = dev_to_iio_dev(dev); 99637d34556SJosselin Costanzi struct iio_buffer *buffer = indio_dev->buffer; 99737d34556SJosselin Costanzi 99837d34556SJosselin Costanzi return sprintf(buf, "%u\n", buffer->watermark); 99937d34556SJosselin Costanzi } 100037d34556SJosselin Costanzi 100137d34556SJosselin Costanzi static ssize_t iio_buffer_store_watermark(struct device *dev, 100237d34556SJosselin Costanzi struct device_attribute *attr, 100337d34556SJosselin Costanzi const char *buf, 100437d34556SJosselin Costanzi size_t len) 100537d34556SJosselin Costanzi { 100637d34556SJosselin Costanzi struct iio_dev *indio_dev = dev_to_iio_dev(dev); 100737d34556SJosselin Costanzi struct iio_buffer *buffer = indio_dev->buffer; 100837d34556SJosselin Costanzi unsigned int val; 100937d34556SJosselin Costanzi int ret; 101037d34556SJosselin Costanzi 101137d34556SJosselin Costanzi ret = kstrtouint(buf, 10, &val); 101237d34556SJosselin Costanzi if (ret) 101337d34556SJosselin Costanzi return ret; 101437d34556SJosselin Costanzi if (!val) 101537d34556SJosselin Costanzi return -EINVAL; 101637d34556SJosselin Costanzi 101737d34556SJosselin Costanzi mutex_lock(&indio_dev->mlock); 101837d34556SJosselin Costanzi 101937d34556SJosselin Costanzi if (val > buffer->length) { 102037d34556SJosselin Costanzi ret = -EINVAL; 102137d34556SJosselin Costanzi goto out; 102237d34556SJosselin Costanzi } 102337d34556SJosselin Costanzi 102437d34556SJosselin Costanzi if (iio_buffer_is_active(indio_dev->buffer)) { 102537d34556SJosselin Costanzi ret = -EBUSY; 102637d34556SJosselin Costanzi goto out; 102737d34556SJosselin Costanzi } 102837d34556SJosselin Costanzi 102937d34556SJosselin Costanzi buffer->watermark = val; 103037d34556SJosselin Costanzi out: 103137d34556SJosselin Costanzi mutex_unlock(&indio_dev->mlock); 103237d34556SJosselin Costanzi 103337d34556SJosselin Costanzi return ret ? ret : len; 103437d34556SJosselin Costanzi } 103537d34556SJosselin Costanzi 103608e7e0adSLars-Peter Clausen static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length, 103708e7e0adSLars-Peter Clausen iio_buffer_write_length); 10388d92db28SLars-Peter Clausen static struct device_attribute dev_attr_length_ro = __ATTR(length, 10398d92db28SLars-Peter Clausen S_IRUGO, iio_buffer_read_length, NULL); 104008e7e0adSLars-Peter Clausen static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, 104108e7e0adSLars-Peter Clausen iio_buffer_show_enable, iio_buffer_store_enable); 104237d34556SJosselin Costanzi static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR, 104337d34556SJosselin Costanzi iio_buffer_show_watermark, iio_buffer_store_watermark); 1044b440655bSLars-Peter Clausen static struct device_attribute dev_attr_watermark_ro = __ATTR(watermark, 1045b440655bSLars-Peter Clausen S_IRUGO, iio_buffer_show_watermark, NULL); 104608e7e0adSLars-Peter Clausen 10476da9b382SOctavian Purdila static struct attribute *iio_buffer_attrs[] = { 10486da9b382SOctavian Purdila &dev_attr_length.attr, 10496da9b382SOctavian Purdila &dev_attr_enable.attr, 105037d34556SJosselin Costanzi &dev_attr_watermark.attr, 10516da9b382SOctavian Purdila }; 10526da9b382SOctavian Purdila 1053d967cb6bSLars-Peter Clausen int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev) 1054d967cb6bSLars-Peter Clausen { 1055d967cb6bSLars-Peter Clausen struct iio_dev_attr *p; 1056d967cb6bSLars-Peter Clausen struct attribute **attr; 1057d967cb6bSLars-Peter Clausen struct iio_buffer *buffer = indio_dev->buffer; 1058d967cb6bSLars-Peter Clausen int ret, i, attrn, attrcount, attrcount_orig = 0; 1059d967cb6bSLars-Peter Clausen const struct iio_chan_spec *channels; 1060d967cb6bSLars-Peter Clausen 1061629bc023SLars-Peter Clausen channels = indio_dev->channels; 1062629bc023SLars-Peter Clausen if (channels) { 1063629bc023SLars-Peter Clausen int ml = indio_dev->masklength; 1064629bc023SLars-Peter Clausen 1065629bc023SLars-Peter Clausen for (i = 0; i < indio_dev->num_channels; i++) 1066629bc023SLars-Peter Clausen ml = max(ml, channels[i].scan_index + 1); 1067629bc023SLars-Peter Clausen indio_dev->masklength = ml; 1068629bc023SLars-Peter Clausen } 1069629bc023SLars-Peter Clausen 1070d967cb6bSLars-Peter Clausen if (!buffer) 1071d967cb6bSLars-Peter Clausen return 0; 1072d967cb6bSLars-Peter Clausen 107308e7e0adSLars-Peter Clausen attrcount = 0; 107408e7e0adSLars-Peter Clausen if (buffer->attrs) { 107508e7e0adSLars-Peter Clausen while (buffer->attrs[attrcount] != NULL) 107608e7e0adSLars-Peter Clausen attrcount++; 107708e7e0adSLars-Peter Clausen } 107808e7e0adSLars-Peter Clausen 10796da9b382SOctavian Purdila attr = kcalloc(attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1, 10806da9b382SOctavian Purdila sizeof(struct attribute *), GFP_KERNEL); 10816da9b382SOctavian Purdila if (!attr) 108208e7e0adSLars-Peter Clausen return -ENOMEM; 108308e7e0adSLars-Peter Clausen 10846da9b382SOctavian Purdila memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs)); 10856da9b382SOctavian Purdila if (!buffer->access->set_length) 10866da9b382SOctavian Purdila attr[0] = &dev_attr_length_ro.attr; 10876da9b382SOctavian Purdila 1088b440655bSLars-Peter Clausen if (buffer->access->flags & INDIO_BUFFER_FLAG_FIXED_WATERMARK) 1089b440655bSLars-Peter Clausen attr[2] = &dev_attr_watermark_ro.attr; 1090b440655bSLars-Peter Clausen 109108e7e0adSLars-Peter Clausen if (buffer->attrs) 10926da9b382SOctavian Purdila memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs, 10936da9b382SOctavian Purdila sizeof(struct attribute *) * attrcount); 10946da9b382SOctavian Purdila 10956da9b382SOctavian Purdila attr[attrcount + ARRAY_SIZE(iio_buffer_attrs)] = NULL; 10966da9b382SOctavian Purdila 10976da9b382SOctavian Purdila buffer->buffer_group.name = "buffer"; 10986da9b382SOctavian Purdila buffer->buffer_group.attrs = attr; 109908e7e0adSLars-Peter Clausen 110008e7e0adSLars-Peter Clausen indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group; 110108e7e0adSLars-Peter Clausen 1102d967cb6bSLars-Peter Clausen if (buffer->scan_el_attrs != NULL) { 1103d967cb6bSLars-Peter Clausen attr = buffer->scan_el_attrs->attrs; 1104d967cb6bSLars-Peter Clausen while (*attr++ != NULL) 1105d967cb6bSLars-Peter Clausen attrcount_orig++; 1106d967cb6bSLars-Peter Clausen } 1107d967cb6bSLars-Peter Clausen attrcount = attrcount_orig; 1108d967cb6bSLars-Peter Clausen INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list); 1109d967cb6bSLars-Peter Clausen channels = indio_dev->channels; 1110d967cb6bSLars-Peter Clausen if (channels) { 1111d967cb6bSLars-Peter Clausen /* new magic */ 1112d967cb6bSLars-Peter Clausen for (i = 0; i < indio_dev->num_channels; i++) { 1113d967cb6bSLars-Peter Clausen if (channels[i].scan_index < 0) 1114d967cb6bSLars-Peter Clausen continue; 1115d967cb6bSLars-Peter Clausen 1116d967cb6bSLars-Peter Clausen ret = iio_buffer_add_channel_sysfs(indio_dev, 1117d967cb6bSLars-Peter Clausen &channels[i]); 1118d967cb6bSLars-Peter Clausen if (ret < 0) 1119d967cb6bSLars-Peter Clausen goto error_cleanup_dynamic; 1120d967cb6bSLars-Peter Clausen attrcount += ret; 1121d967cb6bSLars-Peter Clausen if (channels[i].type == IIO_TIMESTAMP) 1122d967cb6bSLars-Peter Clausen indio_dev->scan_index_timestamp = 1123d967cb6bSLars-Peter Clausen channels[i].scan_index; 1124d967cb6bSLars-Peter Clausen } 1125d967cb6bSLars-Peter Clausen if (indio_dev->masklength && buffer->scan_mask == NULL) { 1126d967cb6bSLars-Peter Clausen buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), 1127d967cb6bSLars-Peter Clausen sizeof(*buffer->scan_mask), 1128d967cb6bSLars-Peter Clausen GFP_KERNEL); 1129d967cb6bSLars-Peter Clausen if (buffer->scan_mask == NULL) { 1130d967cb6bSLars-Peter Clausen ret = -ENOMEM; 1131d967cb6bSLars-Peter Clausen goto error_cleanup_dynamic; 1132d967cb6bSLars-Peter Clausen } 1133d967cb6bSLars-Peter Clausen } 1134d967cb6bSLars-Peter Clausen } 1135d967cb6bSLars-Peter Clausen 1136d967cb6bSLars-Peter Clausen buffer->scan_el_group.name = iio_scan_elements_group_name; 1137d967cb6bSLars-Peter Clausen 1138d967cb6bSLars-Peter Clausen buffer->scan_el_group.attrs = kcalloc(attrcount + 1, 1139d967cb6bSLars-Peter Clausen sizeof(buffer->scan_el_group.attrs[0]), 1140d967cb6bSLars-Peter Clausen GFP_KERNEL); 1141d967cb6bSLars-Peter Clausen if (buffer->scan_el_group.attrs == NULL) { 1142d967cb6bSLars-Peter Clausen ret = -ENOMEM; 1143d967cb6bSLars-Peter Clausen goto error_free_scan_mask; 1144d967cb6bSLars-Peter Clausen } 1145d967cb6bSLars-Peter Clausen if (buffer->scan_el_attrs) 1146d967cb6bSLars-Peter Clausen memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs, 1147d967cb6bSLars-Peter Clausen sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig); 1148d967cb6bSLars-Peter Clausen attrn = attrcount_orig; 1149d967cb6bSLars-Peter Clausen 1150d967cb6bSLars-Peter Clausen list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l) 1151d967cb6bSLars-Peter Clausen buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr; 1152d967cb6bSLars-Peter Clausen indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group; 1153d967cb6bSLars-Peter Clausen 1154d967cb6bSLars-Peter Clausen return 0; 1155d967cb6bSLars-Peter Clausen 1156d967cb6bSLars-Peter Clausen error_free_scan_mask: 1157d967cb6bSLars-Peter Clausen kfree(buffer->scan_mask); 1158d967cb6bSLars-Peter Clausen error_cleanup_dynamic: 1159d967cb6bSLars-Peter Clausen iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list); 116008e7e0adSLars-Peter Clausen kfree(indio_dev->buffer->buffer_group.attrs); 1161d967cb6bSLars-Peter Clausen 1162d967cb6bSLars-Peter Clausen return ret; 1163d967cb6bSLars-Peter Clausen } 1164d967cb6bSLars-Peter Clausen 1165d967cb6bSLars-Peter Clausen void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev) 1166d967cb6bSLars-Peter Clausen { 1167d967cb6bSLars-Peter Clausen if (!indio_dev->buffer) 1168d967cb6bSLars-Peter Clausen return; 1169d967cb6bSLars-Peter Clausen 1170d967cb6bSLars-Peter Clausen kfree(indio_dev->buffer->scan_mask); 117108e7e0adSLars-Peter Clausen kfree(indio_dev->buffer->buffer_group.attrs); 1172d967cb6bSLars-Peter Clausen kfree(indio_dev->buffer->scan_el_group.attrs); 1173d967cb6bSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list); 1174d967cb6bSLars-Peter Clausen } 1175d967cb6bSLars-Peter Clausen 1176a980e046SJonathan Cameron /** 117781636632SLars-Peter Clausen * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected 117881636632SLars-Peter Clausen * @indio_dev: the iio device 117981636632SLars-Peter Clausen * @mask: scan mask to be checked 118081636632SLars-Peter Clausen * 118181636632SLars-Peter Clausen * Return true if exactly one bit is set in the scan mask, false otherwise. It 118281636632SLars-Peter Clausen * can be used for devices where only one channel can be active for sampling at 118381636632SLars-Peter Clausen * a time. 118481636632SLars-Peter Clausen */ 118581636632SLars-Peter Clausen bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev, 118681636632SLars-Peter Clausen const unsigned long *mask) 118781636632SLars-Peter Clausen { 118881636632SLars-Peter Clausen return bitmap_weight(mask, indio_dev->masklength) == 1; 118981636632SLars-Peter Clausen } 119081636632SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot); 119181636632SLars-Peter Clausen 1192a980e046SJonathan Cameron int iio_scan_mask_query(struct iio_dev *indio_dev, 1193a980e046SJonathan Cameron struct iio_buffer *buffer, int bit) 1194a980e046SJonathan Cameron { 1195a980e046SJonathan Cameron if (bit > indio_dev->masklength) 1196a980e046SJonathan Cameron return -EINVAL; 1197a980e046SJonathan Cameron 1198a980e046SJonathan Cameron if (!buffer->scan_mask) 1199a980e046SJonathan Cameron return 0; 1200a980e046SJonathan Cameron 12012076a20fSAlec Berg /* Ensure return value is 0 or 1. */ 12022076a20fSAlec Berg return !!test_bit(bit, buffer->scan_mask); 1203a980e046SJonathan Cameron }; 1204a980e046SJonathan Cameron EXPORT_SYMBOL_GPL(iio_scan_mask_query); 1205a980e046SJonathan Cameron 1206a980e046SJonathan Cameron /** 12070123635aSCristina Opriceana * struct iio_demux_table - table describing demux memcpy ops 1208a980e046SJonathan Cameron * @from: index to copy from 1209a980e046SJonathan Cameron * @to: index to copy to 1210a980e046SJonathan Cameron * @length: how many bytes to copy 1211a980e046SJonathan Cameron * @l: list head used for management 1212a980e046SJonathan Cameron */ 1213a980e046SJonathan Cameron struct iio_demux_table { 1214a980e046SJonathan Cameron unsigned from; 1215a980e046SJonathan Cameron unsigned to; 1216a980e046SJonathan Cameron unsigned length; 1217a980e046SJonathan Cameron struct list_head l; 1218a980e046SJonathan Cameron }; 1219a980e046SJonathan Cameron 12205d65d920SLars-Peter Clausen static const void *iio_demux(struct iio_buffer *buffer, 12215d65d920SLars-Peter Clausen const void *datain) 1222a980e046SJonathan Cameron { 1223a980e046SJonathan Cameron struct iio_demux_table *t; 1224a980e046SJonathan Cameron 1225a980e046SJonathan Cameron if (list_empty(&buffer->demux_list)) 1226a980e046SJonathan Cameron return datain; 1227a980e046SJonathan Cameron list_for_each_entry(t, &buffer->demux_list, l) 1228a980e046SJonathan Cameron memcpy(buffer->demux_bounce + t->to, 1229a980e046SJonathan Cameron datain + t->from, t->length); 1230a980e046SJonathan Cameron 1231a980e046SJonathan Cameron return buffer->demux_bounce; 1232a980e046SJonathan Cameron } 1233a980e046SJonathan Cameron 12345d65d920SLars-Peter Clausen static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data) 1235a980e046SJonathan Cameron { 12365d65d920SLars-Peter Clausen const void *dataout = iio_demux(buffer, data); 123737d34556SJosselin Costanzi int ret; 1238a980e046SJonathan Cameron 123937d34556SJosselin Costanzi ret = buffer->access->store_to(buffer, dataout); 124037d34556SJosselin Costanzi if (ret) 124137d34556SJosselin Costanzi return ret; 124237d34556SJosselin Costanzi 124337d34556SJosselin Costanzi /* 124437d34556SJosselin Costanzi * We can't just test for watermark to decide if we wake the poll queue 124537d34556SJosselin Costanzi * because read may request less samples than the watermark. 124637d34556SJosselin Costanzi */ 124737d34556SJosselin Costanzi wake_up_interruptible_poll(&buffer->pollq, POLLIN | POLLRDNORM); 124837d34556SJosselin Costanzi return 0; 1249a980e046SJonathan Cameron } 1250a980e046SJonathan Cameron 1251a980e046SJonathan Cameron static void iio_buffer_demux_free(struct iio_buffer *buffer) 1252a980e046SJonathan Cameron { 1253a980e046SJonathan Cameron struct iio_demux_table *p, *q; 1254a980e046SJonathan Cameron list_for_each_entry_safe(p, q, &buffer->demux_list, l) { 1255a980e046SJonathan Cameron list_del(&p->l); 1256a980e046SJonathan Cameron kfree(p); 1257a980e046SJonathan Cameron } 1258a980e046SJonathan Cameron } 1259a980e046SJonathan Cameron 126084b36ce5SJonathan Cameron 12615d65d920SLars-Peter Clausen int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) 126284b36ce5SJonathan Cameron { 126384b36ce5SJonathan Cameron int ret; 126484b36ce5SJonathan Cameron struct iio_buffer *buf; 126584b36ce5SJonathan Cameron 126684b36ce5SJonathan Cameron list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) { 126784b36ce5SJonathan Cameron ret = iio_push_to_buffer(buf, data); 126884b36ce5SJonathan Cameron if (ret < 0) 126984b36ce5SJonathan Cameron return ret; 127084b36ce5SJonathan Cameron } 127184b36ce5SJonathan Cameron 127284b36ce5SJonathan Cameron return 0; 127384b36ce5SJonathan Cameron } 127484b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_push_to_buffers); 127584b36ce5SJonathan Cameron 1276cbe88bccSLars-Peter Clausen static int iio_buffer_add_demux(struct iio_buffer *buffer, 1277cbe88bccSLars-Peter Clausen struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc, 1278cbe88bccSLars-Peter Clausen unsigned int length) 1279cbe88bccSLars-Peter Clausen { 1280cbe88bccSLars-Peter Clausen 1281cbe88bccSLars-Peter Clausen if (*p && (*p)->from + (*p)->length == in_loc && 1282cbe88bccSLars-Peter Clausen (*p)->to + (*p)->length == out_loc) { 1283cbe88bccSLars-Peter Clausen (*p)->length += length; 1284cbe88bccSLars-Peter Clausen } else { 12857cdca178SJonathan Cameron *p = kmalloc(sizeof(**p), GFP_KERNEL); 1286cbe88bccSLars-Peter Clausen if (*p == NULL) 1287cbe88bccSLars-Peter Clausen return -ENOMEM; 1288cbe88bccSLars-Peter Clausen (*p)->from = in_loc; 1289cbe88bccSLars-Peter Clausen (*p)->to = out_loc; 1290cbe88bccSLars-Peter Clausen (*p)->length = length; 1291cbe88bccSLars-Peter Clausen list_add_tail(&(*p)->l, &buffer->demux_list); 1292cbe88bccSLars-Peter Clausen } 1293cbe88bccSLars-Peter Clausen 1294cbe88bccSLars-Peter Clausen return 0; 1295cbe88bccSLars-Peter Clausen } 1296cbe88bccSLars-Peter Clausen 129784b36ce5SJonathan Cameron static int iio_buffer_update_demux(struct iio_dev *indio_dev, 129884b36ce5SJonathan Cameron struct iio_buffer *buffer) 1299a980e046SJonathan Cameron { 1300a980e046SJonathan Cameron int ret, in_ind = -1, out_ind, length; 1301a980e046SJonathan Cameron unsigned in_loc = 0, out_loc = 0; 1302cbe88bccSLars-Peter Clausen struct iio_demux_table *p = NULL; 1303a980e046SJonathan Cameron 1304a980e046SJonathan Cameron /* Clear out any old demux */ 1305a980e046SJonathan Cameron iio_buffer_demux_free(buffer); 1306a980e046SJonathan Cameron kfree(buffer->demux_bounce); 1307a980e046SJonathan Cameron buffer->demux_bounce = NULL; 1308a980e046SJonathan Cameron 1309a980e046SJonathan Cameron /* First work out which scan mode we will actually have */ 1310a980e046SJonathan Cameron if (bitmap_equal(indio_dev->active_scan_mask, 1311a980e046SJonathan Cameron buffer->scan_mask, 1312a980e046SJonathan Cameron indio_dev->masklength)) 1313a980e046SJonathan Cameron return 0; 1314a980e046SJonathan Cameron 1315a980e046SJonathan Cameron /* Now we have the two masks, work from least sig and build up sizes */ 1316a980e046SJonathan Cameron for_each_set_bit(out_ind, 131761bd55ceSLars-Peter Clausen buffer->scan_mask, 1318a980e046SJonathan Cameron indio_dev->masklength) { 1319a980e046SJonathan Cameron in_ind = find_next_bit(indio_dev->active_scan_mask, 1320a980e046SJonathan Cameron indio_dev->masklength, 1321a980e046SJonathan Cameron in_ind + 1); 1322a980e046SJonathan Cameron while (in_ind != out_ind) { 1323a980e046SJonathan Cameron in_ind = find_next_bit(indio_dev->active_scan_mask, 1324a980e046SJonathan Cameron indio_dev->masklength, 1325a980e046SJonathan Cameron in_ind + 1); 1326182b4905SLars-Peter Clausen length = iio_storage_bytes_for_si(indio_dev, in_ind); 1327a980e046SJonathan Cameron /* Make sure we are aligned */ 132861072dbcSLars-Peter Clausen in_loc = roundup(in_loc, length) + length; 1329a980e046SJonathan Cameron } 1330182b4905SLars-Peter Clausen length = iio_storage_bytes_for_si(indio_dev, in_ind); 133161072dbcSLars-Peter Clausen out_loc = roundup(out_loc, length); 133261072dbcSLars-Peter Clausen in_loc = roundup(in_loc, length); 1333cbe88bccSLars-Peter Clausen ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); 1334cbe88bccSLars-Peter Clausen if (ret) 1335cbe88bccSLars-Peter Clausen goto error_clear_mux_table; 1336a980e046SJonathan Cameron out_loc += length; 1337a980e046SJonathan Cameron in_loc += length; 1338a980e046SJonathan Cameron } 1339a980e046SJonathan Cameron /* Relies on scan_timestamp being last */ 1340a980e046SJonathan Cameron if (buffer->scan_timestamp) { 1341182b4905SLars-Peter Clausen length = iio_storage_bytes_for_timestamp(indio_dev); 134261072dbcSLars-Peter Clausen out_loc = roundup(out_loc, length); 134361072dbcSLars-Peter Clausen in_loc = roundup(in_loc, length); 1344cbe88bccSLars-Peter Clausen ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); 1345cbe88bccSLars-Peter Clausen if (ret) 1346cbe88bccSLars-Peter Clausen goto error_clear_mux_table; 1347a980e046SJonathan Cameron out_loc += length; 1348a980e046SJonathan Cameron in_loc += length; 1349a980e046SJonathan Cameron } 1350a980e046SJonathan Cameron buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL); 1351a980e046SJonathan Cameron if (buffer->demux_bounce == NULL) { 1352a980e046SJonathan Cameron ret = -ENOMEM; 1353a980e046SJonathan Cameron goto error_clear_mux_table; 1354a980e046SJonathan Cameron } 1355a980e046SJonathan Cameron return 0; 1356a980e046SJonathan Cameron 1357a980e046SJonathan Cameron error_clear_mux_table: 1358a980e046SJonathan Cameron iio_buffer_demux_free(buffer); 1359a980e046SJonathan Cameron 1360a980e046SJonathan Cameron return ret; 1361a980e046SJonathan Cameron } 136284b36ce5SJonathan Cameron 136384b36ce5SJonathan Cameron int iio_update_demux(struct iio_dev *indio_dev) 136484b36ce5SJonathan Cameron { 136584b36ce5SJonathan Cameron struct iio_buffer *buffer; 136684b36ce5SJonathan Cameron int ret; 136784b36ce5SJonathan Cameron 136884b36ce5SJonathan Cameron list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 136984b36ce5SJonathan Cameron ret = iio_buffer_update_demux(indio_dev, buffer); 137084b36ce5SJonathan Cameron if (ret < 0) 137184b36ce5SJonathan Cameron goto error_clear_mux_table; 137284b36ce5SJonathan Cameron } 137384b36ce5SJonathan Cameron return 0; 137484b36ce5SJonathan Cameron 137584b36ce5SJonathan Cameron error_clear_mux_table: 137684b36ce5SJonathan Cameron list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) 137784b36ce5SJonathan Cameron iio_buffer_demux_free(buffer); 137884b36ce5SJonathan Cameron 137984b36ce5SJonathan Cameron return ret; 138084b36ce5SJonathan Cameron } 1381a980e046SJonathan Cameron EXPORT_SYMBOL_GPL(iio_update_demux); 13829e69c935SLars-Peter Clausen 13839e69c935SLars-Peter Clausen /** 13849e69c935SLars-Peter Clausen * iio_buffer_release() - Free a buffer's resources 13859e69c935SLars-Peter Clausen * @ref: Pointer to the kref embedded in the iio_buffer struct 13869e69c935SLars-Peter Clausen * 13879e69c935SLars-Peter Clausen * This function is called when the last reference to the buffer has been 13889e69c935SLars-Peter Clausen * dropped. It will typically free all resources allocated by the buffer. Do not 13899e69c935SLars-Peter Clausen * call this function manually, always use iio_buffer_put() when done using a 13909e69c935SLars-Peter Clausen * buffer. 13919e69c935SLars-Peter Clausen */ 13929e69c935SLars-Peter Clausen static void iio_buffer_release(struct kref *ref) 13939e69c935SLars-Peter Clausen { 13949e69c935SLars-Peter Clausen struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref); 13959e69c935SLars-Peter Clausen 13969e69c935SLars-Peter Clausen buffer->access->release(buffer); 13979e69c935SLars-Peter Clausen } 13989e69c935SLars-Peter Clausen 13999e69c935SLars-Peter Clausen /** 14009e69c935SLars-Peter Clausen * iio_buffer_get() - Grab a reference to the buffer 14019e69c935SLars-Peter Clausen * @buffer: The buffer to grab a reference for, may be NULL 14029e69c935SLars-Peter Clausen * 14039e69c935SLars-Peter Clausen * Returns the pointer to the buffer that was passed into the function. 14049e69c935SLars-Peter Clausen */ 14059e69c935SLars-Peter Clausen struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer) 14069e69c935SLars-Peter Clausen { 14079e69c935SLars-Peter Clausen if (buffer) 14089e69c935SLars-Peter Clausen kref_get(&buffer->ref); 14099e69c935SLars-Peter Clausen 14109e69c935SLars-Peter Clausen return buffer; 14119e69c935SLars-Peter Clausen } 14129e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_get); 14139e69c935SLars-Peter Clausen 14149e69c935SLars-Peter Clausen /** 14159e69c935SLars-Peter Clausen * iio_buffer_put() - Release the reference to the buffer 14169e69c935SLars-Peter Clausen * @buffer: The buffer to release the reference for, may be NULL 14179e69c935SLars-Peter Clausen */ 14189e69c935SLars-Peter Clausen void iio_buffer_put(struct iio_buffer *buffer) 14199e69c935SLars-Peter Clausen { 14209e69c935SLars-Peter Clausen if (buffer) 14219e69c935SLars-Peter Clausen kref_put(&buffer->ref, iio_buffer_release); 14229e69c935SLars-Peter Clausen } 14239e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_put); 1424