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; 110fcf68f3cSBrian Norris DEFINE_WAIT_FUNC(wait, woken_wake_function); 11137d34556SJosselin Costanzi size_t datum_size; 112c6f67a1fSOctavian Purdila size_t to_wait; 113*5dba4b14SColin Ian King int ret = 0; 114a980e046SJonathan Cameron 115f18e7a06SLars-Peter Clausen if (!indio_dev->info) 116f18e7a06SLars-Peter Clausen return -ENODEV; 117f18e7a06SLars-Peter Clausen 118a980e046SJonathan Cameron if (!rb || !rb->access->read_first_n) 119a980e046SJonathan Cameron return -EINVAL; 120ee551a10SLars-Peter Clausen 12137d34556SJosselin Costanzi datum_size = rb->bytes_per_datum; 122ee551a10SLars-Peter Clausen 12337d34556SJosselin Costanzi /* 12437d34556SJosselin Costanzi * If datum_size is 0 there will never be anything to read from the 12537d34556SJosselin Costanzi * buffer, so signal end of file now. 12637d34556SJosselin Costanzi */ 12737d34556SJosselin Costanzi if (!datum_size) 12837d34556SJosselin Costanzi return 0; 12937d34556SJosselin Costanzi 130c6f67a1fSOctavian Purdila if (filp->f_flags & O_NONBLOCK) 131c6f67a1fSOctavian Purdila to_wait = 0; 132c6f67a1fSOctavian Purdila else 133c6f67a1fSOctavian Purdila to_wait = min_t(size_t, n / datum_size, rb->watermark); 13437d34556SJosselin Costanzi 135fcf68f3cSBrian Norris add_wait_queue(&rb->pollq, &wait); 13637d34556SJosselin Costanzi do { 137fcf68f3cSBrian Norris if (!indio_dev->info) { 138fcf68f3cSBrian Norris ret = -ENODEV; 139fcf68f3cSBrian Norris break; 140fcf68f3cSBrian Norris } 14137d34556SJosselin Costanzi 142fcf68f3cSBrian Norris if (!iio_buffer_ready(indio_dev, rb, to_wait, n / datum_size)) { 143fcf68f3cSBrian Norris if (signal_pending(current)) { 144fcf68f3cSBrian Norris ret = -ERESTARTSYS; 145fcf68f3cSBrian Norris break; 146fcf68f3cSBrian Norris } 147fcf68f3cSBrian Norris 148fcf68f3cSBrian Norris wait_woken(&wait, TASK_INTERRUPTIBLE, 149fcf68f3cSBrian Norris MAX_SCHEDULE_TIMEOUT); 150fcf68f3cSBrian Norris continue; 151fcf68f3cSBrian Norris } 152ee551a10SLars-Peter Clausen 153ee551a10SLars-Peter Clausen ret = rb->access->read_first_n(rb, n, buf); 154ee551a10SLars-Peter Clausen if (ret == 0 && (filp->f_flags & O_NONBLOCK)) 155ee551a10SLars-Peter Clausen ret = -EAGAIN; 156ee551a10SLars-Peter Clausen } while (ret == 0); 157fcf68f3cSBrian Norris remove_wait_queue(&rb->pollq, &wait); 158ee551a10SLars-Peter Clausen 159ee551a10SLars-Peter Clausen return ret; 160a980e046SJonathan Cameron } 161a980e046SJonathan Cameron 162a980e046SJonathan Cameron /** 163a980e046SJonathan Cameron * iio_buffer_poll() - poll the buffer to find out if it has data 1640123635aSCristina Opriceana * @filp: File structure pointer for device access 1650123635aSCristina Opriceana * @wait: Poll table structure pointer for which the driver adds 1660123635aSCristina Opriceana * a wait queue 1670123635aSCristina Opriceana * 1680123635aSCristina Opriceana * Return: (POLLIN | POLLRDNORM) if data is available for reading 1690123635aSCristina Opriceana * or 0 for other cases 170a980e046SJonathan Cameron */ 171a980e046SJonathan Cameron unsigned int iio_buffer_poll(struct file *filp, 172a980e046SJonathan Cameron struct poll_table_struct *wait) 173a980e046SJonathan Cameron { 174a980e046SJonathan Cameron struct iio_dev *indio_dev = filp->private_data; 175a980e046SJonathan Cameron struct iio_buffer *rb = indio_dev->buffer; 176a980e046SJonathan Cameron 177f18e7a06SLars-Peter Clausen if (!indio_dev->info) 1781bdc0293SCristina Opriceana return 0; 179f18e7a06SLars-Peter Clausen 180a980e046SJonathan Cameron poll_wait(filp, &rb->pollq, wait); 181f4f4673bSOctavian Purdila if (iio_buffer_ready(indio_dev, rb, rb->watermark, 0)) 182a980e046SJonathan Cameron return POLLIN | POLLRDNORM; 183a980e046SJonathan Cameron return 0; 184a980e046SJonathan Cameron } 185a980e046SJonathan Cameron 186d2f0a48fSLars-Peter Clausen /** 187d2f0a48fSLars-Peter Clausen * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue 188d2f0a48fSLars-Peter Clausen * @indio_dev: The IIO device 189d2f0a48fSLars-Peter Clausen * 190d2f0a48fSLars-Peter Clausen * Wakes up the event waitqueue used for poll(). Should usually 191d2f0a48fSLars-Peter Clausen * be called when the device is unregistered. 192d2f0a48fSLars-Peter Clausen */ 193d2f0a48fSLars-Peter Clausen void iio_buffer_wakeup_poll(struct iio_dev *indio_dev) 194d2f0a48fSLars-Peter Clausen { 195d2f0a48fSLars-Peter Clausen if (!indio_dev->buffer) 196d2f0a48fSLars-Peter Clausen return; 197d2f0a48fSLars-Peter Clausen 198d2f0a48fSLars-Peter Clausen wake_up(&indio_dev->buffer->pollq); 199d2f0a48fSLars-Peter Clausen } 200d2f0a48fSLars-Peter Clausen 201a980e046SJonathan Cameron void iio_buffer_init(struct iio_buffer *buffer) 202a980e046SJonathan Cameron { 203a980e046SJonathan Cameron INIT_LIST_HEAD(&buffer->demux_list); 204705ee2c9SLars-Peter Clausen INIT_LIST_HEAD(&buffer->buffer_list); 205a980e046SJonathan Cameron init_waitqueue_head(&buffer->pollq); 2069e69c935SLars-Peter Clausen kref_init(&buffer->ref); 2074a605357SLars-Peter Clausen if (!buffer->watermark) 20837d34556SJosselin Costanzi buffer->watermark = 1; 209a980e046SJonathan Cameron } 210a980e046SJonathan Cameron EXPORT_SYMBOL(iio_buffer_init); 211a980e046SJonathan Cameron 212a980e046SJonathan Cameron static ssize_t iio_show_scan_index(struct device *dev, 213a980e046SJonathan Cameron struct device_attribute *attr, 214a980e046SJonathan Cameron char *buf) 215a980e046SJonathan Cameron { 216a980e046SJonathan Cameron return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index); 217a980e046SJonathan Cameron } 218a980e046SJonathan Cameron 219a980e046SJonathan Cameron static ssize_t iio_show_fixed_type(struct device *dev, 220a980e046SJonathan Cameron struct device_attribute *attr, 221a980e046SJonathan Cameron char *buf) 222a980e046SJonathan Cameron { 223a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 224a980e046SJonathan Cameron u8 type = this_attr->c->scan_type.endianness; 225a980e046SJonathan Cameron 226a980e046SJonathan Cameron if (type == IIO_CPU) { 227a980e046SJonathan Cameron #ifdef __LITTLE_ENDIAN 228a980e046SJonathan Cameron type = IIO_LE; 229a980e046SJonathan Cameron #else 230a980e046SJonathan Cameron type = IIO_BE; 231a980e046SJonathan Cameron #endif 232a980e046SJonathan Cameron } 2330ee8546aSSrinivas Pandruvada if (this_attr->c->scan_type.repeat > 1) 2340ee8546aSSrinivas Pandruvada return sprintf(buf, "%s:%c%d/%dX%d>>%u\n", 2350ee8546aSSrinivas Pandruvada iio_endian_prefix[type], 2360ee8546aSSrinivas Pandruvada this_attr->c->scan_type.sign, 2370ee8546aSSrinivas Pandruvada this_attr->c->scan_type.realbits, 2380ee8546aSSrinivas Pandruvada this_attr->c->scan_type.storagebits, 2390ee8546aSSrinivas Pandruvada this_attr->c->scan_type.repeat, 2400ee8546aSSrinivas Pandruvada this_attr->c->scan_type.shift); 2410ee8546aSSrinivas Pandruvada else 242a980e046SJonathan Cameron return sprintf(buf, "%s:%c%d/%d>>%u\n", 243a980e046SJonathan Cameron iio_endian_prefix[type], 244a980e046SJonathan Cameron this_attr->c->scan_type.sign, 245a980e046SJonathan Cameron this_attr->c->scan_type.realbits, 246a980e046SJonathan Cameron this_attr->c->scan_type.storagebits, 247a980e046SJonathan Cameron this_attr->c->scan_type.shift); 248a980e046SJonathan Cameron } 249a980e046SJonathan Cameron 250a980e046SJonathan Cameron static ssize_t iio_scan_el_show(struct device *dev, 251a980e046SJonathan Cameron struct device_attribute *attr, 252a980e046SJonathan Cameron char *buf) 253a980e046SJonathan Cameron { 254a980e046SJonathan Cameron int ret; 255e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 256a980e046SJonathan Cameron 2572076a20fSAlec Berg /* Ensure ret is 0 or 1. */ 2582076a20fSAlec Berg ret = !!test_bit(to_iio_dev_attr(attr)->address, 259a980e046SJonathan Cameron indio_dev->buffer->scan_mask); 260a980e046SJonathan Cameron 261a980e046SJonathan Cameron return sprintf(buf, "%d\n", ret); 262a980e046SJonathan Cameron } 263a980e046SJonathan Cameron 264217a5cf0SLars-Peter Clausen /* Note NULL used as error indicator as it doesn't make sense. */ 265217a5cf0SLars-Peter Clausen static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks, 266217a5cf0SLars-Peter Clausen unsigned int masklength, 2671e1ec286SLars-Peter Clausen const unsigned long *mask, 2681e1ec286SLars-Peter Clausen bool strict) 269217a5cf0SLars-Peter Clausen { 270217a5cf0SLars-Peter Clausen if (bitmap_empty(mask, masklength)) 271217a5cf0SLars-Peter Clausen return NULL; 272217a5cf0SLars-Peter Clausen while (*av_masks) { 2731e1ec286SLars-Peter Clausen if (strict) { 2741e1ec286SLars-Peter Clausen if (bitmap_equal(mask, av_masks, masklength)) 2751e1ec286SLars-Peter Clausen return av_masks; 2761e1ec286SLars-Peter Clausen } else { 277217a5cf0SLars-Peter Clausen if (bitmap_subset(mask, av_masks, masklength)) 278217a5cf0SLars-Peter Clausen return av_masks; 2791e1ec286SLars-Peter Clausen } 280217a5cf0SLars-Peter Clausen av_masks += BITS_TO_LONGS(masklength); 281217a5cf0SLars-Peter Clausen } 282217a5cf0SLars-Peter Clausen return NULL; 283217a5cf0SLars-Peter Clausen } 284217a5cf0SLars-Peter Clausen 285217a5cf0SLars-Peter Clausen static bool iio_validate_scan_mask(struct iio_dev *indio_dev, 286217a5cf0SLars-Peter Clausen const unsigned long *mask) 287217a5cf0SLars-Peter Clausen { 288217a5cf0SLars-Peter Clausen if (!indio_dev->setup_ops->validate_scan_mask) 289217a5cf0SLars-Peter Clausen return true; 290217a5cf0SLars-Peter Clausen 291217a5cf0SLars-Peter Clausen return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask); 292217a5cf0SLars-Peter Clausen } 293217a5cf0SLars-Peter Clausen 294217a5cf0SLars-Peter Clausen /** 295217a5cf0SLars-Peter Clausen * iio_scan_mask_set() - set particular bit in the scan mask 296217a5cf0SLars-Peter Clausen * @indio_dev: the iio device 297217a5cf0SLars-Peter Clausen * @buffer: the buffer whose scan mask we are interested in 298217a5cf0SLars-Peter Clausen * @bit: the bit to be set. 299217a5cf0SLars-Peter Clausen * 300217a5cf0SLars-Peter Clausen * Note that at this point we have no way of knowing what other 301217a5cf0SLars-Peter Clausen * buffers might request, hence this code only verifies that the 302217a5cf0SLars-Peter Clausen * individual buffers request is plausible. 303217a5cf0SLars-Peter Clausen */ 304217a5cf0SLars-Peter Clausen static int iio_scan_mask_set(struct iio_dev *indio_dev, 305217a5cf0SLars-Peter Clausen struct iio_buffer *buffer, int bit) 306217a5cf0SLars-Peter Clausen { 307217a5cf0SLars-Peter Clausen const unsigned long *mask; 308217a5cf0SLars-Peter Clausen unsigned long *trialmask; 309217a5cf0SLars-Peter Clausen 310217a5cf0SLars-Peter Clausen trialmask = kmalloc(sizeof(*trialmask)* 311217a5cf0SLars-Peter Clausen BITS_TO_LONGS(indio_dev->masklength), 312217a5cf0SLars-Peter Clausen GFP_KERNEL); 313217a5cf0SLars-Peter Clausen 314217a5cf0SLars-Peter Clausen if (trialmask == NULL) 315217a5cf0SLars-Peter Clausen return -ENOMEM; 316217a5cf0SLars-Peter Clausen if (!indio_dev->masklength) { 317231bfe53SDan Carpenter WARN(1, "Trying to set scanmask prior to registering buffer\n"); 318217a5cf0SLars-Peter Clausen goto err_invalid_mask; 319217a5cf0SLars-Peter Clausen } 320217a5cf0SLars-Peter Clausen bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength); 321217a5cf0SLars-Peter Clausen set_bit(bit, trialmask); 322217a5cf0SLars-Peter Clausen 323217a5cf0SLars-Peter Clausen if (!iio_validate_scan_mask(indio_dev, trialmask)) 324217a5cf0SLars-Peter Clausen goto err_invalid_mask; 325217a5cf0SLars-Peter Clausen 326217a5cf0SLars-Peter Clausen if (indio_dev->available_scan_masks) { 327217a5cf0SLars-Peter Clausen mask = iio_scan_mask_match(indio_dev->available_scan_masks, 328217a5cf0SLars-Peter Clausen indio_dev->masklength, 3291e1ec286SLars-Peter Clausen trialmask, false); 330217a5cf0SLars-Peter Clausen if (!mask) 331217a5cf0SLars-Peter Clausen goto err_invalid_mask; 332217a5cf0SLars-Peter Clausen } 333217a5cf0SLars-Peter Clausen bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength); 334217a5cf0SLars-Peter Clausen 335217a5cf0SLars-Peter Clausen kfree(trialmask); 336217a5cf0SLars-Peter Clausen 337217a5cf0SLars-Peter Clausen return 0; 338217a5cf0SLars-Peter Clausen 339217a5cf0SLars-Peter Clausen err_invalid_mask: 340217a5cf0SLars-Peter Clausen kfree(trialmask); 341217a5cf0SLars-Peter Clausen return -EINVAL; 342217a5cf0SLars-Peter Clausen } 343217a5cf0SLars-Peter Clausen 344a980e046SJonathan Cameron static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit) 345a980e046SJonathan Cameron { 346a980e046SJonathan Cameron clear_bit(bit, buffer->scan_mask); 347a980e046SJonathan Cameron return 0; 348a980e046SJonathan Cameron } 349a980e046SJonathan Cameron 350a980e046SJonathan Cameron static ssize_t iio_scan_el_store(struct device *dev, 351a980e046SJonathan Cameron struct device_attribute *attr, 352a980e046SJonathan Cameron const char *buf, 353a980e046SJonathan Cameron size_t len) 354a980e046SJonathan Cameron { 355a980e046SJonathan Cameron int ret; 356a980e046SJonathan Cameron bool state; 357e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 358a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 359a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 360a980e046SJonathan Cameron 361a980e046SJonathan Cameron ret = strtobool(buf, &state); 362a980e046SJonathan Cameron if (ret < 0) 363a980e046SJonathan Cameron return ret; 364a980e046SJonathan Cameron mutex_lock(&indio_dev->mlock); 365705ee2c9SLars-Peter Clausen if (iio_buffer_is_active(indio_dev->buffer)) { 366a980e046SJonathan Cameron ret = -EBUSY; 367a980e046SJonathan Cameron goto error_ret; 368a980e046SJonathan Cameron } 369a980e046SJonathan Cameron ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address); 370a980e046SJonathan Cameron if (ret < 0) 371a980e046SJonathan Cameron goto error_ret; 372a980e046SJonathan Cameron if (!state && ret) { 373a980e046SJonathan Cameron ret = iio_scan_mask_clear(buffer, this_attr->address); 374a980e046SJonathan Cameron if (ret) 375a980e046SJonathan Cameron goto error_ret; 376a980e046SJonathan Cameron } else if (state && !ret) { 377a980e046SJonathan Cameron ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address); 378a980e046SJonathan Cameron if (ret) 379a980e046SJonathan Cameron goto error_ret; 380a980e046SJonathan Cameron } 381a980e046SJonathan Cameron 382a980e046SJonathan Cameron error_ret: 383a980e046SJonathan Cameron mutex_unlock(&indio_dev->mlock); 384a980e046SJonathan Cameron 385a980e046SJonathan Cameron return ret < 0 ? ret : len; 386a980e046SJonathan Cameron 387a980e046SJonathan Cameron } 388a980e046SJonathan Cameron 389a980e046SJonathan Cameron static ssize_t iio_scan_el_ts_show(struct device *dev, 390a980e046SJonathan Cameron struct device_attribute *attr, 391a980e046SJonathan Cameron char *buf) 392a980e046SJonathan Cameron { 393e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 394a980e046SJonathan Cameron return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp); 395a980e046SJonathan Cameron } 396a980e046SJonathan Cameron 397a980e046SJonathan Cameron static ssize_t iio_scan_el_ts_store(struct device *dev, 398a980e046SJonathan Cameron struct device_attribute *attr, 399a980e046SJonathan Cameron const char *buf, 400a980e046SJonathan Cameron size_t len) 401a980e046SJonathan Cameron { 402a980e046SJonathan Cameron int ret; 403e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 404a980e046SJonathan Cameron bool state; 405a980e046SJonathan Cameron 406a980e046SJonathan Cameron ret = strtobool(buf, &state); 407a980e046SJonathan Cameron if (ret < 0) 408a980e046SJonathan Cameron return ret; 409a980e046SJonathan Cameron 410a980e046SJonathan Cameron mutex_lock(&indio_dev->mlock); 411705ee2c9SLars-Peter Clausen if (iio_buffer_is_active(indio_dev->buffer)) { 412a980e046SJonathan Cameron ret = -EBUSY; 413a980e046SJonathan Cameron goto error_ret; 414a980e046SJonathan Cameron } 415a980e046SJonathan Cameron indio_dev->buffer->scan_timestamp = state; 416a980e046SJonathan Cameron error_ret: 417a980e046SJonathan Cameron mutex_unlock(&indio_dev->mlock); 418a980e046SJonathan Cameron 419a980e046SJonathan Cameron return ret ? ret : len; 420a980e046SJonathan Cameron } 421a980e046SJonathan Cameron 422a980e046SJonathan Cameron static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev, 423a980e046SJonathan Cameron const struct iio_chan_spec *chan) 424a980e046SJonathan Cameron { 425a980e046SJonathan Cameron int ret, attrcount = 0; 426a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 427a980e046SJonathan Cameron 428a980e046SJonathan Cameron ret = __iio_add_chan_devattr("index", 429a980e046SJonathan Cameron chan, 430a980e046SJonathan Cameron &iio_show_scan_index, 431a980e046SJonathan Cameron NULL, 432a980e046SJonathan Cameron 0, 4333704432fSJonathan Cameron IIO_SEPARATE, 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 ret = __iio_add_chan_devattr("type", 440a980e046SJonathan Cameron chan, 441a980e046SJonathan Cameron &iio_show_fixed_type, 442a980e046SJonathan Cameron NULL, 443a980e046SJonathan Cameron 0, 444a980e046SJonathan Cameron 0, 445a980e046SJonathan Cameron &indio_dev->dev, 446a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 447a980e046SJonathan Cameron if (ret) 44892825ff9SHartmut Knaack return ret; 449a980e046SJonathan Cameron attrcount++; 450a980e046SJonathan Cameron if (chan->type != IIO_TIMESTAMP) 451a980e046SJonathan Cameron ret = __iio_add_chan_devattr("en", 452a980e046SJonathan Cameron chan, 453a980e046SJonathan Cameron &iio_scan_el_show, 454a980e046SJonathan Cameron &iio_scan_el_store, 455a980e046SJonathan Cameron chan->scan_index, 456a980e046SJonathan Cameron 0, 457a980e046SJonathan Cameron &indio_dev->dev, 458a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 459a980e046SJonathan Cameron else 460a980e046SJonathan Cameron ret = __iio_add_chan_devattr("en", 461a980e046SJonathan Cameron chan, 462a980e046SJonathan Cameron &iio_scan_el_ts_show, 463a980e046SJonathan Cameron &iio_scan_el_ts_store, 464a980e046SJonathan Cameron chan->scan_index, 465a980e046SJonathan Cameron 0, 466a980e046SJonathan Cameron &indio_dev->dev, 467a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 4689572588cSPeter Meerwald if (ret) 46992825ff9SHartmut Knaack return ret; 470a980e046SJonathan Cameron attrcount++; 471a980e046SJonathan Cameron ret = attrcount; 472a980e046SJonathan Cameron return ret; 473a980e046SJonathan Cameron } 474a980e046SJonathan Cameron 47508e7e0adSLars-Peter Clausen static ssize_t iio_buffer_read_length(struct device *dev, 476a980e046SJonathan Cameron struct device_attribute *attr, 477a980e046SJonathan Cameron char *buf) 478a980e046SJonathan Cameron { 479e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 480a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 481a980e046SJonathan Cameron 48237495660SLars-Peter Clausen return sprintf(buf, "%d\n", buffer->length); 483a980e046SJonathan Cameron } 484a980e046SJonathan Cameron 48508e7e0adSLars-Peter Clausen static ssize_t iio_buffer_write_length(struct device *dev, 486a980e046SJonathan Cameron struct device_attribute *attr, 48708e7e0adSLars-Peter Clausen const char *buf, size_t len) 488a980e046SJonathan Cameron { 489e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 490a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 491948ad205SLars-Peter Clausen unsigned int val; 492948ad205SLars-Peter Clausen int ret; 493a980e046SJonathan Cameron 494948ad205SLars-Peter Clausen ret = kstrtouint(buf, 10, &val); 495a980e046SJonathan Cameron if (ret) 496a980e046SJonathan Cameron return ret; 497a980e046SJonathan Cameron 49837495660SLars-Peter Clausen if (val == buffer->length) 499a980e046SJonathan Cameron return len; 500a980e046SJonathan Cameron 501a980e046SJonathan Cameron mutex_lock(&indio_dev->mlock); 502705ee2c9SLars-Peter Clausen if (iio_buffer_is_active(indio_dev->buffer)) { 503a980e046SJonathan Cameron ret = -EBUSY; 504a980e046SJonathan Cameron } else { 505a980e046SJonathan Cameron buffer->access->set_length(buffer, val); 506a980e046SJonathan Cameron ret = 0; 507a980e046SJonathan Cameron } 50837d34556SJosselin Costanzi if (ret) 50937d34556SJosselin Costanzi goto out; 51037d34556SJosselin Costanzi if (buffer->length && buffer->length < buffer->watermark) 51137d34556SJosselin Costanzi buffer->watermark = buffer->length; 51237d34556SJosselin Costanzi out: 513a980e046SJonathan Cameron mutex_unlock(&indio_dev->mlock); 514a980e046SJonathan Cameron 515a980e046SJonathan Cameron return ret ? ret : len; 516a980e046SJonathan Cameron } 517a980e046SJonathan Cameron 51808e7e0adSLars-Peter Clausen static ssize_t iio_buffer_show_enable(struct device *dev, 519a980e046SJonathan Cameron struct device_attribute *attr, 520a980e046SJonathan Cameron char *buf) 521a980e046SJonathan Cameron { 522e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 523705ee2c9SLars-Peter Clausen return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer)); 524a980e046SJonathan Cameron } 525a980e046SJonathan Cameron 526182b4905SLars-Peter Clausen static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev, 527182b4905SLars-Peter Clausen unsigned int scan_index) 528182b4905SLars-Peter Clausen { 529182b4905SLars-Peter Clausen const struct iio_chan_spec *ch; 530182b4905SLars-Peter Clausen unsigned int bytes; 531182b4905SLars-Peter Clausen 532182b4905SLars-Peter Clausen ch = iio_find_channel_from_si(indio_dev, scan_index); 533182b4905SLars-Peter Clausen bytes = ch->scan_type.storagebits / 8; 534182b4905SLars-Peter Clausen if (ch->scan_type.repeat > 1) 535182b4905SLars-Peter Clausen bytes *= ch->scan_type.repeat; 536182b4905SLars-Peter Clausen return bytes; 537182b4905SLars-Peter Clausen } 538182b4905SLars-Peter Clausen 539182b4905SLars-Peter Clausen static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev) 540182b4905SLars-Peter Clausen { 541182b4905SLars-Peter Clausen return iio_storage_bytes_for_si(indio_dev, 542182b4905SLars-Peter Clausen indio_dev->scan_index_timestamp); 543182b4905SLars-Peter Clausen } 544182b4905SLars-Peter Clausen 545183f4173SPeter Meerwald static int iio_compute_scan_bytes(struct iio_dev *indio_dev, 546183f4173SPeter Meerwald const unsigned long *mask, bool timestamp) 547a980e046SJonathan Cameron { 548a980e046SJonathan Cameron unsigned bytes = 0; 549a980e046SJonathan Cameron int length, i; 550a980e046SJonathan Cameron 551a980e046SJonathan Cameron /* How much space will the demuxed element take? */ 552a980e046SJonathan Cameron for_each_set_bit(i, mask, 553a980e046SJonathan Cameron indio_dev->masklength) { 554182b4905SLars-Peter Clausen length = iio_storage_bytes_for_si(indio_dev, i); 555a980e046SJonathan Cameron bytes = ALIGN(bytes, length); 556a980e046SJonathan Cameron bytes += length; 557a980e046SJonathan Cameron } 558182b4905SLars-Peter Clausen 559a980e046SJonathan Cameron if (timestamp) { 560182b4905SLars-Peter Clausen length = iio_storage_bytes_for_timestamp(indio_dev); 561a980e046SJonathan Cameron bytes = ALIGN(bytes, length); 562a980e046SJonathan Cameron bytes += length; 563a980e046SJonathan Cameron } 564a980e046SJonathan Cameron return bytes; 565a980e046SJonathan Cameron } 566a980e046SJonathan Cameron 5679e69c935SLars-Peter Clausen static void iio_buffer_activate(struct iio_dev *indio_dev, 5689e69c935SLars-Peter Clausen struct iio_buffer *buffer) 5699e69c935SLars-Peter Clausen { 5709e69c935SLars-Peter Clausen iio_buffer_get(buffer); 5719e69c935SLars-Peter Clausen list_add(&buffer->buffer_list, &indio_dev->buffer_list); 5729e69c935SLars-Peter Clausen } 5739e69c935SLars-Peter Clausen 5749e69c935SLars-Peter Clausen static void iio_buffer_deactivate(struct iio_buffer *buffer) 5759e69c935SLars-Peter Clausen { 5769e69c935SLars-Peter Clausen list_del_init(&buffer->buffer_list); 57737d34556SJosselin Costanzi wake_up_interruptible(&buffer->pollq); 5789e69c935SLars-Peter Clausen iio_buffer_put(buffer); 5799e69c935SLars-Peter Clausen } 5809e69c935SLars-Peter Clausen 5811250186aSLars-Peter Clausen static void iio_buffer_deactivate_all(struct iio_dev *indio_dev) 5821250186aSLars-Peter Clausen { 5831250186aSLars-Peter Clausen struct iio_buffer *buffer, *_buffer; 5841250186aSLars-Peter Clausen 5851250186aSLars-Peter Clausen list_for_each_entry_safe(buffer, _buffer, 5861250186aSLars-Peter Clausen &indio_dev->buffer_list, buffer_list) 5871250186aSLars-Peter Clausen iio_buffer_deactivate(buffer); 5881250186aSLars-Peter Clausen } 5891250186aSLars-Peter Clausen 590e18a2ad4SLars-Peter Clausen static int iio_buffer_enable(struct iio_buffer *buffer, 591e18a2ad4SLars-Peter Clausen struct iio_dev *indio_dev) 592e18a2ad4SLars-Peter Clausen { 593e18a2ad4SLars-Peter Clausen if (!buffer->access->enable) 594e18a2ad4SLars-Peter Clausen return 0; 595e18a2ad4SLars-Peter Clausen return buffer->access->enable(buffer, indio_dev); 596e18a2ad4SLars-Peter Clausen } 597e18a2ad4SLars-Peter Clausen 598e18a2ad4SLars-Peter Clausen static int iio_buffer_disable(struct iio_buffer *buffer, 599e18a2ad4SLars-Peter Clausen struct iio_dev *indio_dev) 600e18a2ad4SLars-Peter Clausen { 601e18a2ad4SLars-Peter Clausen if (!buffer->access->disable) 602e18a2ad4SLars-Peter Clausen return 0; 603e18a2ad4SLars-Peter Clausen return buffer->access->disable(buffer, indio_dev); 604e18a2ad4SLars-Peter Clausen } 605e18a2ad4SLars-Peter Clausen 6068e050996SLars-Peter Clausen static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev, 6078e050996SLars-Peter Clausen struct iio_buffer *buffer) 6088e050996SLars-Peter Clausen { 6098e050996SLars-Peter Clausen unsigned int bytes; 6108e050996SLars-Peter Clausen 6118e050996SLars-Peter Clausen if (!buffer->access->set_bytes_per_datum) 6128e050996SLars-Peter Clausen return; 6138e050996SLars-Peter Clausen 6148e050996SLars-Peter Clausen bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask, 6158e050996SLars-Peter Clausen buffer->scan_timestamp); 6168e050996SLars-Peter Clausen 6178e050996SLars-Peter Clausen buffer->access->set_bytes_per_datum(buffer, bytes); 6188e050996SLars-Peter Clausen } 6198e050996SLars-Peter Clausen 620fcc1b2f5SLars-Peter Clausen static int iio_buffer_request_update(struct iio_dev *indio_dev, 621fcc1b2f5SLars-Peter Clausen struct iio_buffer *buffer) 622fcc1b2f5SLars-Peter Clausen { 623fcc1b2f5SLars-Peter Clausen int ret; 624fcc1b2f5SLars-Peter Clausen 625fcc1b2f5SLars-Peter Clausen iio_buffer_update_bytes_per_datum(indio_dev, buffer); 626fcc1b2f5SLars-Peter Clausen if (buffer->access->request_update) { 627fcc1b2f5SLars-Peter Clausen ret = buffer->access->request_update(buffer); 628fcc1b2f5SLars-Peter Clausen if (ret) { 629fcc1b2f5SLars-Peter Clausen dev_dbg(&indio_dev->dev, 630fcc1b2f5SLars-Peter Clausen "Buffer not started: buffer parameter update failed (%d)\n", 631fcc1b2f5SLars-Peter Clausen ret); 632fcc1b2f5SLars-Peter Clausen return ret; 633fcc1b2f5SLars-Peter Clausen } 634fcc1b2f5SLars-Peter Clausen } 635fcc1b2f5SLars-Peter Clausen 636fcc1b2f5SLars-Peter Clausen return 0; 637fcc1b2f5SLars-Peter Clausen } 638fcc1b2f5SLars-Peter Clausen 639248be5aaSLars-Peter Clausen static void iio_free_scan_mask(struct iio_dev *indio_dev, 640248be5aaSLars-Peter Clausen const unsigned long *mask) 641248be5aaSLars-Peter Clausen { 642248be5aaSLars-Peter Clausen /* If the mask is dynamically allocated free it, otherwise do nothing */ 643248be5aaSLars-Peter Clausen if (!indio_dev->available_scan_masks) 644248be5aaSLars-Peter Clausen kfree(mask); 645248be5aaSLars-Peter Clausen } 646248be5aaSLars-Peter Clausen 6476e509c4dSLars-Peter Clausen struct iio_device_config { 6486e509c4dSLars-Peter Clausen unsigned int mode; 649f0566c0cSLars-Peter Clausen unsigned int watermark; 6506e509c4dSLars-Peter Clausen const unsigned long *scan_mask; 6516e509c4dSLars-Peter Clausen unsigned int scan_bytes; 6526e509c4dSLars-Peter Clausen bool scan_timestamp; 6536e509c4dSLars-Peter Clausen }; 6546e509c4dSLars-Peter Clausen 6556e509c4dSLars-Peter Clausen static int iio_verify_update(struct iio_dev *indio_dev, 6566e509c4dSLars-Peter Clausen struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer, 6576e509c4dSLars-Peter Clausen struct iio_device_config *config) 6586e509c4dSLars-Peter Clausen { 6596e509c4dSLars-Peter Clausen unsigned long *compound_mask; 6606e509c4dSLars-Peter Clausen const unsigned long *scan_mask; 6611e1ec286SLars-Peter Clausen bool strict_scanmask = false; 6626e509c4dSLars-Peter Clausen struct iio_buffer *buffer; 6636e509c4dSLars-Peter Clausen bool scan_timestamp; 664225d59adSLars-Peter Clausen unsigned int modes; 6656e509c4dSLars-Peter Clausen 6666e509c4dSLars-Peter Clausen memset(config, 0, sizeof(*config)); 6671bef2c1dSIrina Tirdea config->watermark = ~0; 6686e509c4dSLars-Peter Clausen 6696e509c4dSLars-Peter Clausen /* 6706e509c4dSLars-Peter Clausen * If there is just one buffer and we are removing it there is nothing 6716e509c4dSLars-Peter Clausen * to verify. 6726e509c4dSLars-Peter Clausen */ 6736e509c4dSLars-Peter Clausen if (remove_buffer && !insert_buffer && 6746e509c4dSLars-Peter Clausen list_is_singular(&indio_dev->buffer_list)) 6756e509c4dSLars-Peter Clausen return 0; 6766e509c4dSLars-Peter Clausen 677225d59adSLars-Peter Clausen modes = indio_dev->modes; 678225d59adSLars-Peter Clausen 679225d59adSLars-Peter Clausen list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 680225d59adSLars-Peter Clausen if (buffer == remove_buffer) 681225d59adSLars-Peter Clausen continue; 682225d59adSLars-Peter Clausen modes &= buffer->access->modes; 683f0566c0cSLars-Peter Clausen config->watermark = min(config->watermark, buffer->watermark); 684225d59adSLars-Peter Clausen } 685225d59adSLars-Peter Clausen 686f0566c0cSLars-Peter Clausen if (insert_buffer) { 687225d59adSLars-Peter Clausen modes &= insert_buffer->access->modes; 688f0566c0cSLars-Peter Clausen config->watermark = min(config->watermark, 689f0566c0cSLars-Peter Clausen insert_buffer->watermark); 690f0566c0cSLars-Peter Clausen } 691225d59adSLars-Peter Clausen 6926e509c4dSLars-Peter Clausen /* Definitely possible for devices to support both of these. */ 693225d59adSLars-Peter Clausen if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) { 6946e509c4dSLars-Peter Clausen config->mode = INDIO_BUFFER_TRIGGERED; 695225d59adSLars-Peter Clausen } else if (modes & INDIO_BUFFER_HARDWARE) { 6961e1ec286SLars-Peter Clausen /* 6971e1ec286SLars-Peter Clausen * Keep things simple for now and only allow a single buffer to 6981e1ec286SLars-Peter Clausen * be connected in hardware mode. 6991e1ec286SLars-Peter Clausen */ 7001e1ec286SLars-Peter Clausen if (insert_buffer && !list_empty(&indio_dev->buffer_list)) 7011e1ec286SLars-Peter Clausen return -EINVAL; 7026e509c4dSLars-Peter Clausen config->mode = INDIO_BUFFER_HARDWARE; 7031e1ec286SLars-Peter Clausen strict_scanmask = true; 704225d59adSLars-Peter Clausen } else if (modes & INDIO_BUFFER_SOFTWARE) { 7056e509c4dSLars-Peter Clausen config->mode = INDIO_BUFFER_SOFTWARE; 7066e509c4dSLars-Peter Clausen } else { 7076e509c4dSLars-Peter Clausen /* Can only occur on first buffer */ 7086e509c4dSLars-Peter Clausen if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 7096e509c4dSLars-Peter Clausen dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n"); 7106e509c4dSLars-Peter Clausen return -EINVAL; 7116e509c4dSLars-Peter Clausen } 7126e509c4dSLars-Peter Clausen 7136e509c4dSLars-Peter Clausen /* What scan mask do we actually have? */ 7146e509c4dSLars-Peter Clausen compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), 7156e509c4dSLars-Peter Clausen sizeof(long), GFP_KERNEL); 7166e509c4dSLars-Peter Clausen if (compound_mask == NULL) 7176e509c4dSLars-Peter Clausen return -ENOMEM; 7186e509c4dSLars-Peter Clausen 7196e509c4dSLars-Peter Clausen scan_timestamp = false; 7206e509c4dSLars-Peter Clausen 7216e509c4dSLars-Peter Clausen list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 7226e509c4dSLars-Peter Clausen if (buffer == remove_buffer) 7236e509c4dSLars-Peter Clausen continue; 7246e509c4dSLars-Peter Clausen bitmap_or(compound_mask, compound_mask, buffer->scan_mask, 7256e509c4dSLars-Peter Clausen indio_dev->masklength); 7266e509c4dSLars-Peter Clausen scan_timestamp |= buffer->scan_timestamp; 7276e509c4dSLars-Peter Clausen } 7286e509c4dSLars-Peter Clausen 7296e509c4dSLars-Peter Clausen if (insert_buffer) { 7306e509c4dSLars-Peter Clausen bitmap_or(compound_mask, compound_mask, 7316e509c4dSLars-Peter Clausen insert_buffer->scan_mask, indio_dev->masklength); 7326e509c4dSLars-Peter Clausen scan_timestamp |= insert_buffer->scan_timestamp; 7336e509c4dSLars-Peter Clausen } 7346e509c4dSLars-Peter Clausen 7356e509c4dSLars-Peter Clausen if (indio_dev->available_scan_masks) { 7366e509c4dSLars-Peter Clausen scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks, 7376e509c4dSLars-Peter Clausen indio_dev->masklength, 7381e1ec286SLars-Peter Clausen compound_mask, 7391e1ec286SLars-Peter Clausen strict_scanmask); 7406e509c4dSLars-Peter Clausen kfree(compound_mask); 7416e509c4dSLars-Peter Clausen if (scan_mask == NULL) 7426e509c4dSLars-Peter Clausen return -EINVAL; 7436e509c4dSLars-Peter Clausen } else { 7446e509c4dSLars-Peter Clausen scan_mask = compound_mask; 7456e509c4dSLars-Peter Clausen } 7466e509c4dSLars-Peter Clausen 7476e509c4dSLars-Peter Clausen config->scan_bytes = iio_compute_scan_bytes(indio_dev, 7486e509c4dSLars-Peter Clausen scan_mask, scan_timestamp); 7496e509c4dSLars-Peter Clausen config->scan_mask = scan_mask; 7506e509c4dSLars-Peter Clausen config->scan_timestamp = scan_timestamp; 7516e509c4dSLars-Peter Clausen 7526e509c4dSLars-Peter Clausen return 0; 7536e509c4dSLars-Peter Clausen } 7546e509c4dSLars-Peter Clausen 755623d74e3SLars-Peter Clausen static int iio_enable_buffers(struct iio_dev *indio_dev, 756623d74e3SLars-Peter Clausen struct iio_device_config *config) 757623d74e3SLars-Peter Clausen { 758e18a2ad4SLars-Peter Clausen struct iio_buffer *buffer; 759623d74e3SLars-Peter Clausen int ret; 760623d74e3SLars-Peter Clausen 761623d74e3SLars-Peter Clausen indio_dev->active_scan_mask = config->scan_mask; 762623d74e3SLars-Peter Clausen indio_dev->scan_timestamp = config->scan_timestamp; 763623d74e3SLars-Peter Clausen indio_dev->scan_bytes = config->scan_bytes; 764623d74e3SLars-Peter Clausen 765623d74e3SLars-Peter Clausen iio_update_demux(indio_dev); 766623d74e3SLars-Peter Clausen 767623d74e3SLars-Peter Clausen /* Wind up again */ 768623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->preenable) { 769623d74e3SLars-Peter Clausen ret = indio_dev->setup_ops->preenable(indio_dev); 770623d74e3SLars-Peter Clausen if (ret) { 771623d74e3SLars-Peter Clausen dev_dbg(&indio_dev->dev, 772623d74e3SLars-Peter Clausen "Buffer not started: buffer preenable failed (%d)\n", ret); 773623d74e3SLars-Peter Clausen goto err_undo_config; 774623d74e3SLars-Peter Clausen } 775623d74e3SLars-Peter Clausen } 776623d74e3SLars-Peter Clausen 777623d74e3SLars-Peter Clausen if (indio_dev->info->update_scan_mode) { 778623d74e3SLars-Peter Clausen ret = indio_dev->info 779623d74e3SLars-Peter Clausen ->update_scan_mode(indio_dev, 780623d74e3SLars-Peter Clausen indio_dev->active_scan_mask); 781623d74e3SLars-Peter Clausen if (ret < 0) { 782623d74e3SLars-Peter Clausen dev_dbg(&indio_dev->dev, 783623d74e3SLars-Peter Clausen "Buffer not started: update scan mode failed (%d)\n", 784623d74e3SLars-Peter Clausen ret); 785623d74e3SLars-Peter Clausen goto err_run_postdisable; 786623d74e3SLars-Peter Clausen } 787623d74e3SLars-Peter Clausen } 788623d74e3SLars-Peter Clausen 789f0566c0cSLars-Peter Clausen if (indio_dev->info->hwfifo_set_watermark) 790f0566c0cSLars-Peter Clausen indio_dev->info->hwfifo_set_watermark(indio_dev, 791f0566c0cSLars-Peter Clausen config->watermark); 792f0566c0cSLars-Peter Clausen 793e18a2ad4SLars-Peter Clausen list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 794e18a2ad4SLars-Peter Clausen ret = iio_buffer_enable(buffer, indio_dev); 795e18a2ad4SLars-Peter Clausen if (ret) 796e18a2ad4SLars-Peter Clausen goto err_disable_buffers; 797e18a2ad4SLars-Peter Clausen } 798e18a2ad4SLars-Peter Clausen 799623d74e3SLars-Peter Clausen indio_dev->currentmode = config->mode; 800623d74e3SLars-Peter Clausen 801623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->postenable) { 802623d74e3SLars-Peter Clausen ret = indio_dev->setup_ops->postenable(indio_dev); 803623d74e3SLars-Peter Clausen if (ret) { 804623d74e3SLars-Peter Clausen dev_dbg(&indio_dev->dev, 805623d74e3SLars-Peter Clausen "Buffer not started: postenable failed (%d)\n", ret); 806e18a2ad4SLars-Peter Clausen goto err_disable_buffers; 807623d74e3SLars-Peter Clausen } 808623d74e3SLars-Peter Clausen } 809623d74e3SLars-Peter Clausen 810623d74e3SLars-Peter Clausen return 0; 811623d74e3SLars-Peter Clausen 812e18a2ad4SLars-Peter Clausen err_disable_buffers: 813e18a2ad4SLars-Peter Clausen list_for_each_entry_continue_reverse(buffer, &indio_dev->buffer_list, 814e18a2ad4SLars-Peter Clausen buffer_list) 815e18a2ad4SLars-Peter Clausen iio_buffer_disable(buffer, indio_dev); 816623d74e3SLars-Peter Clausen err_run_postdisable: 817623d74e3SLars-Peter Clausen indio_dev->currentmode = INDIO_DIRECT_MODE; 818623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->postdisable) 819623d74e3SLars-Peter Clausen indio_dev->setup_ops->postdisable(indio_dev); 820623d74e3SLars-Peter Clausen err_undo_config: 821623d74e3SLars-Peter Clausen indio_dev->active_scan_mask = NULL; 822623d74e3SLars-Peter Clausen 823623d74e3SLars-Peter Clausen return ret; 824623d74e3SLars-Peter Clausen } 825623d74e3SLars-Peter Clausen 826623d74e3SLars-Peter Clausen static int iio_disable_buffers(struct iio_dev *indio_dev) 827623d74e3SLars-Peter Clausen { 828e18a2ad4SLars-Peter Clausen struct iio_buffer *buffer; 8291250186aSLars-Peter Clausen int ret = 0; 8301250186aSLars-Peter Clausen int ret2; 831623d74e3SLars-Peter Clausen 832623d74e3SLars-Peter Clausen /* Wind down existing buffers - iff there are any */ 833623d74e3SLars-Peter Clausen if (list_empty(&indio_dev->buffer_list)) 834623d74e3SLars-Peter Clausen return 0; 835623d74e3SLars-Peter Clausen 8361250186aSLars-Peter Clausen /* 8371250186aSLars-Peter Clausen * If things go wrong at some step in disable we still need to continue 8381250186aSLars-Peter Clausen * to perform the other steps, otherwise we leave the device in a 8391250186aSLars-Peter Clausen * inconsistent state. We return the error code for the first error we 8401250186aSLars-Peter Clausen * encountered. 8411250186aSLars-Peter Clausen */ 8421250186aSLars-Peter Clausen 843623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->predisable) { 8441250186aSLars-Peter Clausen ret2 = indio_dev->setup_ops->predisable(indio_dev); 8451250186aSLars-Peter Clausen if (ret2 && !ret) 8461250186aSLars-Peter Clausen ret = ret2; 847623d74e3SLars-Peter Clausen } 848623d74e3SLars-Peter Clausen 849e18a2ad4SLars-Peter Clausen list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 850e18a2ad4SLars-Peter Clausen ret2 = iio_buffer_disable(buffer, indio_dev); 851e18a2ad4SLars-Peter Clausen if (ret2 && !ret) 852e18a2ad4SLars-Peter Clausen ret = ret2; 853e18a2ad4SLars-Peter Clausen } 854e18a2ad4SLars-Peter Clausen 855623d74e3SLars-Peter Clausen indio_dev->currentmode = INDIO_DIRECT_MODE; 856623d74e3SLars-Peter Clausen 857623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->postdisable) { 8581250186aSLars-Peter Clausen ret2 = indio_dev->setup_ops->postdisable(indio_dev); 8591250186aSLars-Peter Clausen if (ret2 && !ret) 8601250186aSLars-Peter Clausen ret = ret2; 861623d74e3SLars-Peter Clausen } 862623d74e3SLars-Peter Clausen 8631250186aSLars-Peter Clausen iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask); 8641250186aSLars-Peter Clausen indio_dev->active_scan_mask = NULL; 8651250186aSLars-Peter Clausen 8661250186aSLars-Peter Clausen return ret; 867623d74e3SLars-Peter Clausen } 868623d74e3SLars-Peter Clausen 869a9519456SLars-Peter Clausen static int __iio_update_buffers(struct iio_dev *indio_dev, 87084b36ce5SJonathan Cameron struct iio_buffer *insert_buffer, 87184b36ce5SJonathan Cameron struct iio_buffer *remove_buffer) 872a980e046SJonathan Cameron { 8736e509c4dSLars-Peter Clausen struct iio_device_config new_config; 8741250186aSLars-Peter Clausen int ret; 8756e509c4dSLars-Peter Clausen 8766e509c4dSLars-Peter Clausen ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer, 8776e509c4dSLars-Peter Clausen &new_config); 8786e509c4dSLars-Peter Clausen if (ret) 8796e509c4dSLars-Peter Clausen return ret; 880a980e046SJonathan Cameron 881fcc1b2f5SLars-Peter Clausen if (insert_buffer) { 882fcc1b2f5SLars-Peter Clausen ret = iio_buffer_request_update(indio_dev, insert_buffer); 883fcc1b2f5SLars-Peter Clausen if (ret) 8846e509c4dSLars-Peter Clausen goto err_free_config; 885fcc1b2f5SLars-Peter Clausen } 886fcc1b2f5SLars-Peter Clausen 887623d74e3SLars-Peter Clausen ret = iio_disable_buffers(indio_dev); 8881250186aSLars-Peter Clausen if (ret) 8891250186aSLars-Peter Clausen goto err_deactivate_all; 890623d74e3SLars-Peter Clausen 89184b36ce5SJonathan Cameron if (remove_buffer) 8929e69c935SLars-Peter Clausen iio_buffer_deactivate(remove_buffer); 89384b36ce5SJonathan Cameron if (insert_buffer) 8949e69c935SLars-Peter Clausen iio_buffer_activate(indio_dev, insert_buffer); 89584b36ce5SJonathan Cameron 89684b36ce5SJonathan Cameron /* If no buffers in list, we are done */ 8971250186aSLars-Peter Clausen if (list_empty(&indio_dev->buffer_list)) 89884b36ce5SJonathan Cameron return 0; 899a980e046SJonathan Cameron 900623d74e3SLars-Peter Clausen ret = iio_enable_buffers(indio_dev, &new_config); 9011250186aSLars-Peter Clausen if (ret) 9021250186aSLars-Peter Clausen goto err_deactivate_all; 903623d74e3SLars-Peter Clausen 904623d74e3SLars-Peter Clausen return 0; 9056e509c4dSLars-Peter Clausen 9061250186aSLars-Peter Clausen err_deactivate_all: 9071250186aSLars-Peter Clausen /* 9081250186aSLars-Peter Clausen * We've already verified that the config is valid earlier. If things go 9091250186aSLars-Peter Clausen * wrong in either enable or disable the most likely reason is an IO 9101250186aSLars-Peter Clausen * error from the device. In this case there is no good recovery 9111250186aSLars-Peter Clausen * strategy. Just make sure to disable everything and leave the device 9121250186aSLars-Peter Clausen * in a sane state. With a bit of luck the device might come back to 9131250186aSLars-Peter Clausen * life again later and userspace can try again. 9141250186aSLars-Peter Clausen */ 9151250186aSLars-Peter Clausen iio_buffer_deactivate_all(indio_dev); 9161250186aSLars-Peter Clausen 9176e509c4dSLars-Peter Clausen err_free_config: 9186e509c4dSLars-Peter Clausen iio_free_scan_mask(indio_dev, new_config.scan_mask); 9196e509c4dSLars-Peter Clausen return ret; 92084b36ce5SJonathan Cameron } 921a9519456SLars-Peter Clausen 922a9519456SLars-Peter Clausen int iio_update_buffers(struct iio_dev *indio_dev, 923a9519456SLars-Peter Clausen struct iio_buffer *insert_buffer, 924a9519456SLars-Peter Clausen struct iio_buffer *remove_buffer) 925a9519456SLars-Peter Clausen { 926a9519456SLars-Peter Clausen int ret; 927a9519456SLars-Peter Clausen 9283909fab5SLars-Peter Clausen if (insert_buffer == remove_buffer) 9293909fab5SLars-Peter Clausen return 0; 9303909fab5SLars-Peter Clausen 931a9519456SLars-Peter Clausen mutex_lock(&indio_dev->info_exist_lock); 932a9519456SLars-Peter Clausen mutex_lock(&indio_dev->mlock); 933a9519456SLars-Peter Clausen 9343909fab5SLars-Peter Clausen if (insert_buffer && iio_buffer_is_active(insert_buffer)) 9353909fab5SLars-Peter Clausen insert_buffer = NULL; 9363909fab5SLars-Peter Clausen 9373909fab5SLars-Peter Clausen if (remove_buffer && !iio_buffer_is_active(remove_buffer)) 9383909fab5SLars-Peter Clausen remove_buffer = NULL; 9393909fab5SLars-Peter Clausen 9403909fab5SLars-Peter Clausen if (!insert_buffer && !remove_buffer) { 9413909fab5SLars-Peter Clausen ret = 0; 9423909fab5SLars-Peter Clausen goto out_unlock; 9433909fab5SLars-Peter Clausen } 9443909fab5SLars-Peter Clausen 945a9519456SLars-Peter Clausen if (indio_dev->info == NULL) { 946a9519456SLars-Peter Clausen ret = -ENODEV; 947a9519456SLars-Peter Clausen goto out_unlock; 948a9519456SLars-Peter Clausen } 949a9519456SLars-Peter Clausen 950a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer); 951a9519456SLars-Peter Clausen 952a9519456SLars-Peter Clausen out_unlock: 953a9519456SLars-Peter Clausen mutex_unlock(&indio_dev->mlock); 954a9519456SLars-Peter Clausen mutex_unlock(&indio_dev->info_exist_lock); 955a9519456SLars-Peter Clausen 956a9519456SLars-Peter Clausen return ret; 957a9519456SLars-Peter Clausen } 95884b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_update_buffers); 95984b36ce5SJonathan Cameron 960623d74e3SLars-Peter Clausen void iio_disable_all_buffers(struct iio_dev *indio_dev) 961623d74e3SLars-Peter Clausen { 962623d74e3SLars-Peter Clausen iio_disable_buffers(indio_dev); 9631250186aSLars-Peter Clausen iio_buffer_deactivate_all(indio_dev); 964623d74e3SLars-Peter Clausen } 965623d74e3SLars-Peter Clausen 96608e7e0adSLars-Peter Clausen static ssize_t iio_buffer_store_enable(struct device *dev, 96784b36ce5SJonathan Cameron struct device_attribute *attr, 96884b36ce5SJonathan Cameron const char *buf, 96984b36ce5SJonathan Cameron size_t len) 97084b36ce5SJonathan Cameron { 97184b36ce5SJonathan Cameron int ret; 97284b36ce5SJonathan Cameron bool requested_state; 97384b36ce5SJonathan Cameron struct iio_dev *indio_dev = dev_to_iio_dev(dev); 97484b36ce5SJonathan Cameron bool inlist; 97584b36ce5SJonathan Cameron 97684b36ce5SJonathan Cameron ret = strtobool(buf, &requested_state); 97784b36ce5SJonathan Cameron if (ret < 0) 97884b36ce5SJonathan Cameron return ret; 97984b36ce5SJonathan Cameron 98084b36ce5SJonathan Cameron mutex_lock(&indio_dev->mlock); 98184b36ce5SJonathan Cameron 98284b36ce5SJonathan Cameron /* Find out if it is in the list */ 983705ee2c9SLars-Peter Clausen inlist = iio_buffer_is_active(indio_dev->buffer); 98484b36ce5SJonathan Cameron /* Already in desired state */ 98584b36ce5SJonathan Cameron if (inlist == requested_state) 98684b36ce5SJonathan Cameron goto done; 98784b36ce5SJonathan Cameron 98884b36ce5SJonathan Cameron if (requested_state) 989a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, 99084b36ce5SJonathan Cameron indio_dev->buffer, NULL); 99184b36ce5SJonathan Cameron else 992a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, 99384b36ce5SJonathan Cameron NULL, indio_dev->buffer); 99484b36ce5SJonathan Cameron 99584b36ce5SJonathan Cameron done: 99684b36ce5SJonathan Cameron mutex_unlock(&indio_dev->mlock); 99784b36ce5SJonathan Cameron return (ret < 0) ? ret : len; 99884b36ce5SJonathan Cameron } 99984b36ce5SJonathan Cameron 1000d967cb6bSLars-Peter Clausen static const char * const iio_scan_elements_group_name = "scan_elements"; 1001d967cb6bSLars-Peter Clausen 100237d34556SJosselin Costanzi static ssize_t iio_buffer_show_watermark(struct device *dev, 100337d34556SJosselin Costanzi struct device_attribute *attr, 100437d34556SJosselin Costanzi char *buf) 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 100937d34556SJosselin Costanzi return sprintf(buf, "%u\n", buffer->watermark); 101037d34556SJosselin Costanzi } 101137d34556SJosselin Costanzi 101237d34556SJosselin Costanzi static ssize_t iio_buffer_store_watermark(struct device *dev, 101337d34556SJosselin Costanzi struct device_attribute *attr, 101437d34556SJosselin Costanzi const char *buf, 101537d34556SJosselin Costanzi size_t len) 101637d34556SJosselin Costanzi { 101737d34556SJosselin Costanzi struct iio_dev *indio_dev = dev_to_iio_dev(dev); 101837d34556SJosselin Costanzi struct iio_buffer *buffer = indio_dev->buffer; 101937d34556SJosselin Costanzi unsigned int val; 102037d34556SJosselin Costanzi int ret; 102137d34556SJosselin Costanzi 102237d34556SJosselin Costanzi ret = kstrtouint(buf, 10, &val); 102337d34556SJosselin Costanzi if (ret) 102437d34556SJosselin Costanzi return ret; 102537d34556SJosselin Costanzi if (!val) 102637d34556SJosselin Costanzi return -EINVAL; 102737d34556SJosselin Costanzi 102837d34556SJosselin Costanzi mutex_lock(&indio_dev->mlock); 102937d34556SJosselin Costanzi 103037d34556SJosselin Costanzi if (val > buffer->length) { 103137d34556SJosselin Costanzi ret = -EINVAL; 103237d34556SJosselin Costanzi goto out; 103337d34556SJosselin Costanzi } 103437d34556SJosselin Costanzi 103537d34556SJosselin Costanzi if (iio_buffer_is_active(indio_dev->buffer)) { 103637d34556SJosselin Costanzi ret = -EBUSY; 103737d34556SJosselin Costanzi goto out; 103837d34556SJosselin Costanzi } 103937d34556SJosselin Costanzi 104037d34556SJosselin Costanzi buffer->watermark = val; 104137d34556SJosselin Costanzi out: 104237d34556SJosselin Costanzi mutex_unlock(&indio_dev->mlock); 104337d34556SJosselin Costanzi 104437d34556SJosselin Costanzi return ret ? ret : len; 104537d34556SJosselin Costanzi } 104637d34556SJosselin Costanzi 104708e7e0adSLars-Peter Clausen static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length, 104808e7e0adSLars-Peter Clausen iio_buffer_write_length); 10498d92db28SLars-Peter Clausen static struct device_attribute dev_attr_length_ro = __ATTR(length, 10508d92db28SLars-Peter Clausen S_IRUGO, iio_buffer_read_length, NULL); 105108e7e0adSLars-Peter Clausen static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, 105208e7e0adSLars-Peter Clausen iio_buffer_show_enable, iio_buffer_store_enable); 105337d34556SJosselin Costanzi static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR, 105437d34556SJosselin Costanzi iio_buffer_show_watermark, iio_buffer_store_watermark); 1055b440655bSLars-Peter Clausen static struct device_attribute dev_attr_watermark_ro = __ATTR(watermark, 1056b440655bSLars-Peter Clausen S_IRUGO, iio_buffer_show_watermark, NULL); 105708e7e0adSLars-Peter Clausen 10586da9b382SOctavian Purdila static struct attribute *iio_buffer_attrs[] = { 10596da9b382SOctavian Purdila &dev_attr_length.attr, 10606da9b382SOctavian Purdila &dev_attr_enable.attr, 106137d34556SJosselin Costanzi &dev_attr_watermark.attr, 10626da9b382SOctavian Purdila }; 10636da9b382SOctavian Purdila 1064d967cb6bSLars-Peter Clausen int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev) 1065d967cb6bSLars-Peter Clausen { 1066d967cb6bSLars-Peter Clausen struct iio_dev_attr *p; 1067d967cb6bSLars-Peter Clausen struct attribute **attr; 1068d967cb6bSLars-Peter Clausen struct iio_buffer *buffer = indio_dev->buffer; 1069d967cb6bSLars-Peter Clausen int ret, i, attrn, attrcount, attrcount_orig = 0; 1070d967cb6bSLars-Peter Clausen const struct iio_chan_spec *channels; 1071d967cb6bSLars-Peter Clausen 1072629bc023SLars-Peter Clausen channels = indio_dev->channels; 1073629bc023SLars-Peter Clausen if (channels) { 1074629bc023SLars-Peter Clausen int ml = indio_dev->masklength; 1075629bc023SLars-Peter Clausen 1076629bc023SLars-Peter Clausen for (i = 0; i < indio_dev->num_channels; i++) 1077629bc023SLars-Peter Clausen ml = max(ml, channels[i].scan_index + 1); 1078629bc023SLars-Peter Clausen indio_dev->masklength = ml; 1079629bc023SLars-Peter Clausen } 1080629bc023SLars-Peter Clausen 1081d967cb6bSLars-Peter Clausen if (!buffer) 1082d967cb6bSLars-Peter Clausen return 0; 1083d967cb6bSLars-Peter Clausen 108408e7e0adSLars-Peter Clausen attrcount = 0; 108508e7e0adSLars-Peter Clausen if (buffer->attrs) { 108608e7e0adSLars-Peter Clausen while (buffer->attrs[attrcount] != NULL) 108708e7e0adSLars-Peter Clausen attrcount++; 108808e7e0adSLars-Peter Clausen } 108908e7e0adSLars-Peter Clausen 10906da9b382SOctavian Purdila attr = kcalloc(attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1, 10916da9b382SOctavian Purdila sizeof(struct attribute *), GFP_KERNEL); 10926da9b382SOctavian Purdila if (!attr) 109308e7e0adSLars-Peter Clausen return -ENOMEM; 109408e7e0adSLars-Peter Clausen 10956da9b382SOctavian Purdila memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs)); 10966da9b382SOctavian Purdila if (!buffer->access->set_length) 10976da9b382SOctavian Purdila attr[0] = &dev_attr_length_ro.attr; 10986da9b382SOctavian Purdila 1099b440655bSLars-Peter Clausen if (buffer->access->flags & INDIO_BUFFER_FLAG_FIXED_WATERMARK) 1100b440655bSLars-Peter Clausen attr[2] = &dev_attr_watermark_ro.attr; 1101b440655bSLars-Peter Clausen 110208e7e0adSLars-Peter Clausen if (buffer->attrs) 11036da9b382SOctavian Purdila memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs, 11046da9b382SOctavian Purdila sizeof(struct attribute *) * attrcount); 11056da9b382SOctavian Purdila 11066da9b382SOctavian Purdila attr[attrcount + ARRAY_SIZE(iio_buffer_attrs)] = NULL; 11076da9b382SOctavian Purdila 11086da9b382SOctavian Purdila buffer->buffer_group.name = "buffer"; 11096da9b382SOctavian Purdila buffer->buffer_group.attrs = attr; 111008e7e0adSLars-Peter Clausen 111108e7e0adSLars-Peter Clausen indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group; 111208e7e0adSLars-Peter Clausen 1113d967cb6bSLars-Peter Clausen if (buffer->scan_el_attrs != NULL) { 1114d967cb6bSLars-Peter Clausen attr = buffer->scan_el_attrs->attrs; 1115d967cb6bSLars-Peter Clausen while (*attr++ != NULL) 1116d967cb6bSLars-Peter Clausen attrcount_orig++; 1117d967cb6bSLars-Peter Clausen } 1118d967cb6bSLars-Peter Clausen attrcount = attrcount_orig; 1119d967cb6bSLars-Peter Clausen INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list); 1120d967cb6bSLars-Peter Clausen channels = indio_dev->channels; 1121d967cb6bSLars-Peter Clausen if (channels) { 1122d967cb6bSLars-Peter Clausen /* new magic */ 1123d967cb6bSLars-Peter Clausen for (i = 0; i < indio_dev->num_channels; i++) { 1124d967cb6bSLars-Peter Clausen if (channels[i].scan_index < 0) 1125d967cb6bSLars-Peter Clausen continue; 1126d967cb6bSLars-Peter Clausen 1127d967cb6bSLars-Peter Clausen ret = iio_buffer_add_channel_sysfs(indio_dev, 1128d967cb6bSLars-Peter Clausen &channels[i]); 1129d967cb6bSLars-Peter Clausen if (ret < 0) 1130d967cb6bSLars-Peter Clausen goto error_cleanup_dynamic; 1131d967cb6bSLars-Peter Clausen attrcount += ret; 1132d967cb6bSLars-Peter Clausen if (channels[i].type == IIO_TIMESTAMP) 1133d967cb6bSLars-Peter Clausen indio_dev->scan_index_timestamp = 1134d967cb6bSLars-Peter Clausen channels[i].scan_index; 1135d967cb6bSLars-Peter Clausen } 1136d967cb6bSLars-Peter Clausen if (indio_dev->masklength && buffer->scan_mask == NULL) { 1137d967cb6bSLars-Peter Clausen buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), 1138d967cb6bSLars-Peter Clausen sizeof(*buffer->scan_mask), 1139d967cb6bSLars-Peter Clausen GFP_KERNEL); 1140d967cb6bSLars-Peter Clausen if (buffer->scan_mask == NULL) { 1141d967cb6bSLars-Peter Clausen ret = -ENOMEM; 1142d967cb6bSLars-Peter Clausen goto error_cleanup_dynamic; 1143d967cb6bSLars-Peter Clausen } 1144d967cb6bSLars-Peter Clausen } 1145d967cb6bSLars-Peter Clausen } 1146d967cb6bSLars-Peter Clausen 1147d967cb6bSLars-Peter Clausen buffer->scan_el_group.name = iio_scan_elements_group_name; 1148d967cb6bSLars-Peter Clausen 1149d967cb6bSLars-Peter Clausen buffer->scan_el_group.attrs = kcalloc(attrcount + 1, 1150d967cb6bSLars-Peter Clausen sizeof(buffer->scan_el_group.attrs[0]), 1151d967cb6bSLars-Peter Clausen GFP_KERNEL); 1152d967cb6bSLars-Peter Clausen if (buffer->scan_el_group.attrs == NULL) { 1153d967cb6bSLars-Peter Clausen ret = -ENOMEM; 1154d967cb6bSLars-Peter Clausen goto error_free_scan_mask; 1155d967cb6bSLars-Peter Clausen } 1156d967cb6bSLars-Peter Clausen if (buffer->scan_el_attrs) 1157d967cb6bSLars-Peter Clausen memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs, 1158d967cb6bSLars-Peter Clausen sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig); 1159d967cb6bSLars-Peter Clausen attrn = attrcount_orig; 1160d967cb6bSLars-Peter Clausen 1161d967cb6bSLars-Peter Clausen list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l) 1162d967cb6bSLars-Peter Clausen buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr; 1163d967cb6bSLars-Peter Clausen indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group; 1164d967cb6bSLars-Peter Clausen 1165d967cb6bSLars-Peter Clausen return 0; 1166d967cb6bSLars-Peter Clausen 1167d967cb6bSLars-Peter Clausen error_free_scan_mask: 1168d967cb6bSLars-Peter Clausen kfree(buffer->scan_mask); 1169d967cb6bSLars-Peter Clausen error_cleanup_dynamic: 1170d967cb6bSLars-Peter Clausen iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list); 117108e7e0adSLars-Peter Clausen kfree(indio_dev->buffer->buffer_group.attrs); 1172d967cb6bSLars-Peter Clausen 1173d967cb6bSLars-Peter Clausen return ret; 1174d967cb6bSLars-Peter Clausen } 1175d967cb6bSLars-Peter Clausen 1176d967cb6bSLars-Peter Clausen void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev) 1177d967cb6bSLars-Peter Clausen { 1178d967cb6bSLars-Peter Clausen if (!indio_dev->buffer) 1179d967cb6bSLars-Peter Clausen return; 1180d967cb6bSLars-Peter Clausen 1181d967cb6bSLars-Peter Clausen kfree(indio_dev->buffer->scan_mask); 118208e7e0adSLars-Peter Clausen kfree(indio_dev->buffer->buffer_group.attrs); 1183d967cb6bSLars-Peter Clausen kfree(indio_dev->buffer->scan_el_group.attrs); 1184d967cb6bSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list); 1185d967cb6bSLars-Peter Clausen } 1186d967cb6bSLars-Peter Clausen 1187a980e046SJonathan Cameron /** 118881636632SLars-Peter Clausen * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected 118981636632SLars-Peter Clausen * @indio_dev: the iio device 119081636632SLars-Peter Clausen * @mask: scan mask to be checked 119181636632SLars-Peter Clausen * 119281636632SLars-Peter Clausen * Return true if exactly one bit is set in the scan mask, false otherwise. It 119381636632SLars-Peter Clausen * can be used for devices where only one channel can be active for sampling at 119481636632SLars-Peter Clausen * a time. 119581636632SLars-Peter Clausen */ 119681636632SLars-Peter Clausen bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev, 119781636632SLars-Peter Clausen const unsigned long *mask) 119881636632SLars-Peter Clausen { 119981636632SLars-Peter Clausen return bitmap_weight(mask, indio_dev->masklength) == 1; 120081636632SLars-Peter Clausen } 120181636632SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot); 120281636632SLars-Peter Clausen 1203a980e046SJonathan Cameron int iio_scan_mask_query(struct iio_dev *indio_dev, 1204a980e046SJonathan Cameron struct iio_buffer *buffer, int bit) 1205a980e046SJonathan Cameron { 1206a980e046SJonathan Cameron if (bit > indio_dev->masklength) 1207a980e046SJonathan Cameron return -EINVAL; 1208a980e046SJonathan Cameron 1209a980e046SJonathan Cameron if (!buffer->scan_mask) 1210a980e046SJonathan Cameron return 0; 1211a980e046SJonathan Cameron 12122076a20fSAlec Berg /* Ensure return value is 0 or 1. */ 12132076a20fSAlec Berg return !!test_bit(bit, buffer->scan_mask); 1214a980e046SJonathan Cameron }; 1215a980e046SJonathan Cameron EXPORT_SYMBOL_GPL(iio_scan_mask_query); 1216a980e046SJonathan Cameron 1217a980e046SJonathan Cameron /** 12180123635aSCristina Opriceana * struct iio_demux_table - table describing demux memcpy ops 1219a980e046SJonathan Cameron * @from: index to copy from 1220a980e046SJonathan Cameron * @to: index to copy to 1221a980e046SJonathan Cameron * @length: how many bytes to copy 1222a980e046SJonathan Cameron * @l: list head used for management 1223a980e046SJonathan Cameron */ 1224a980e046SJonathan Cameron struct iio_demux_table { 1225a980e046SJonathan Cameron unsigned from; 1226a980e046SJonathan Cameron unsigned to; 1227a980e046SJonathan Cameron unsigned length; 1228a980e046SJonathan Cameron struct list_head l; 1229a980e046SJonathan Cameron }; 1230a980e046SJonathan Cameron 12315d65d920SLars-Peter Clausen static const void *iio_demux(struct iio_buffer *buffer, 12325d65d920SLars-Peter Clausen const void *datain) 1233a980e046SJonathan Cameron { 1234a980e046SJonathan Cameron struct iio_demux_table *t; 1235a980e046SJonathan Cameron 1236a980e046SJonathan Cameron if (list_empty(&buffer->demux_list)) 1237a980e046SJonathan Cameron return datain; 1238a980e046SJonathan Cameron list_for_each_entry(t, &buffer->demux_list, l) 1239a980e046SJonathan Cameron memcpy(buffer->demux_bounce + t->to, 1240a980e046SJonathan Cameron datain + t->from, t->length); 1241a980e046SJonathan Cameron 1242a980e046SJonathan Cameron return buffer->demux_bounce; 1243a980e046SJonathan Cameron } 1244a980e046SJonathan Cameron 12455d65d920SLars-Peter Clausen static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data) 1246a980e046SJonathan Cameron { 12475d65d920SLars-Peter Clausen const void *dataout = iio_demux(buffer, data); 124837d34556SJosselin Costanzi int ret; 1249a980e046SJonathan Cameron 125037d34556SJosselin Costanzi ret = buffer->access->store_to(buffer, dataout); 125137d34556SJosselin Costanzi if (ret) 125237d34556SJosselin Costanzi return ret; 125337d34556SJosselin Costanzi 125437d34556SJosselin Costanzi /* 125537d34556SJosselin Costanzi * We can't just test for watermark to decide if we wake the poll queue 125637d34556SJosselin Costanzi * because read may request less samples than the watermark. 125737d34556SJosselin Costanzi */ 125837d34556SJosselin Costanzi wake_up_interruptible_poll(&buffer->pollq, POLLIN | POLLRDNORM); 125937d34556SJosselin Costanzi return 0; 1260a980e046SJonathan Cameron } 1261a980e046SJonathan Cameron 1262a980e046SJonathan Cameron static void iio_buffer_demux_free(struct iio_buffer *buffer) 1263a980e046SJonathan Cameron { 1264a980e046SJonathan Cameron struct iio_demux_table *p, *q; 1265a980e046SJonathan Cameron list_for_each_entry_safe(p, q, &buffer->demux_list, l) { 1266a980e046SJonathan Cameron list_del(&p->l); 1267a980e046SJonathan Cameron kfree(p); 1268a980e046SJonathan Cameron } 1269a980e046SJonathan Cameron } 1270a980e046SJonathan Cameron 127184b36ce5SJonathan Cameron 12725d65d920SLars-Peter Clausen int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) 127384b36ce5SJonathan Cameron { 127484b36ce5SJonathan Cameron int ret; 127584b36ce5SJonathan Cameron struct iio_buffer *buf; 127684b36ce5SJonathan Cameron 127784b36ce5SJonathan Cameron list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) { 127884b36ce5SJonathan Cameron ret = iio_push_to_buffer(buf, data); 127984b36ce5SJonathan Cameron if (ret < 0) 128084b36ce5SJonathan Cameron return ret; 128184b36ce5SJonathan Cameron } 128284b36ce5SJonathan Cameron 128384b36ce5SJonathan Cameron return 0; 128484b36ce5SJonathan Cameron } 128584b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_push_to_buffers); 128684b36ce5SJonathan Cameron 1287cbe88bccSLars-Peter Clausen static int iio_buffer_add_demux(struct iio_buffer *buffer, 1288cbe88bccSLars-Peter Clausen struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc, 1289cbe88bccSLars-Peter Clausen unsigned int length) 1290cbe88bccSLars-Peter Clausen { 1291cbe88bccSLars-Peter Clausen 1292cbe88bccSLars-Peter Clausen if (*p && (*p)->from + (*p)->length == in_loc && 1293cbe88bccSLars-Peter Clausen (*p)->to + (*p)->length == out_loc) { 1294cbe88bccSLars-Peter Clausen (*p)->length += length; 1295cbe88bccSLars-Peter Clausen } else { 12967cdca178SJonathan Cameron *p = kmalloc(sizeof(**p), GFP_KERNEL); 1297cbe88bccSLars-Peter Clausen if (*p == NULL) 1298cbe88bccSLars-Peter Clausen return -ENOMEM; 1299cbe88bccSLars-Peter Clausen (*p)->from = in_loc; 1300cbe88bccSLars-Peter Clausen (*p)->to = out_loc; 1301cbe88bccSLars-Peter Clausen (*p)->length = length; 1302cbe88bccSLars-Peter Clausen list_add_tail(&(*p)->l, &buffer->demux_list); 1303cbe88bccSLars-Peter Clausen } 1304cbe88bccSLars-Peter Clausen 1305cbe88bccSLars-Peter Clausen return 0; 1306cbe88bccSLars-Peter Clausen } 1307cbe88bccSLars-Peter Clausen 130884b36ce5SJonathan Cameron static int iio_buffer_update_demux(struct iio_dev *indio_dev, 130984b36ce5SJonathan Cameron struct iio_buffer *buffer) 1310a980e046SJonathan Cameron { 1311a980e046SJonathan Cameron int ret, in_ind = -1, out_ind, length; 1312a980e046SJonathan Cameron unsigned in_loc = 0, out_loc = 0; 1313cbe88bccSLars-Peter Clausen struct iio_demux_table *p = NULL; 1314a980e046SJonathan Cameron 1315a980e046SJonathan Cameron /* Clear out any old demux */ 1316a980e046SJonathan Cameron iio_buffer_demux_free(buffer); 1317a980e046SJonathan Cameron kfree(buffer->demux_bounce); 1318a980e046SJonathan Cameron buffer->demux_bounce = NULL; 1319a980e046SJonathan Cameron 1320a980e046SJonathan Cameron /* First work out which scan mode we will actually have */ 1321a980e046SJonathan Cameron if (bitmap_equal(indio_dev->active_scan_mask, 1322a980e046SJonathan Cameron buffer->scan_mask, 1323a980e046SJonathan Cameron indio_dev->masklength)) 1324a980e046SJonathan Cameron return 0; 1325a980e046SJonathan Cameron 1326a980e046SJonathan Cameron /* Now we have the two masks, work from least sig and build up sizes */ 1327a980e046SJonathan Cameron for_each_set_bit(out_ind, 132861bd55ceSLars-Peter Clausen buffer->scan_mask, 1329a980e046SJonathan Cameron indio_dev->masklength) { 1330a980e046SJonathan Cameron in_ind = find_next_bit(indio_dev->active_scan_mask, 1331a980e046SJonathan Cameron indio_dev->masklength, 1332a980e046SJonathan Cameron in_ind + 1); 1333a980e046SJonathan Cameron while (in_ind != out_ind) { 1334a980e046SJonathan Cameron in_ind = find_next_bit(indio_dev->active_scan_mask, 1335a980e046SJonathan Cameron indio_dev->masklength, 1336a980e046SJonathan Cameron in_ind + 1); 1337182b4905SLars-Peter Clausen length = iio_storage_bytes_for_si(indio_dev, in_ind); 1338a980e046SJonathan Cameron /* Make sure we are aligned */ 133961072dbcSLars-Peter Clausen in_loc = roundup(in_loc, length) + length; 1340a980e046SJonathan Cameron } 1341182b4905SLars-Peter Clausen length = iio_storage_bytes_for_si(indio_dev, in_ind); 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 /* Relies on scan_timestamp being last */ 1351a980e046SJonathan Cameron if (buffer->scan_timestamp) { 1352182b4905SLars-Peter Clausen length = iio_storage_bytes_for_timestamp(indio_dev); 135361072dbcSLars-Peter Clausen out_loc = roundup(out_loc, length); 135461072dbcSLars-Peter Clausen in_loc = roundup(in_loc, length); 1355cbe88bccSLars-Peter Clausen ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); 1356cbe88bccSLars-Peter Clausen if (ret) 1357cbe88bccSLars-Peter Clausen goto error_clear_mux_table; 1358a980e046SJonathan Cameron out_loc += length; 1359a980e046SJonathan Cameron in_loc += length; 1360a980e046SJonathan Cameron } 1361a980e046SJonathan Cameron buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL); 1362a980e046SJonathan Cameron if (buffer->demux_bounce == NULL) { 1363a980e046SJonathan Cameron ret = -ENOMEM; 1364a980e046SJonathan Cameron goto error_clear_mux_table; 1365a980e046SJonathan Cameron } 1366a980e046SJonathan Cameron return 0; 1367a980e046SJonathan Cameron 1368a980e046SJonathan Cameron error_clear_mux_table: 1369a980e046SJonathan Cameron iio_buffer_demux_free(buffer); 1370a980e046SJonathan Cameron 1371a980e046SJonathan Cameron return ret; 1372a980e046SJonathan Cameron } 137384b36ce5SJonathan Cameron 137484b36ce5SJonathan Cameron int iio_update_demux(struct iio_dev *indio_dev) 137584b36ce5SJonathan Cameron { 137684b36ce5SJonathan Cameron struct iio_buffer *buffer; 137784b36ce5SJonathan Cameron int ret; 137884b36ce5SJonathan Cameron 137984b36ce5SJonathan Cameron list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 138084b36ce5SJonathan Cameron ret = iio_buffer_update_demux(indio_dev, buffer); 138184b36ce5SJonathan Cameron if (ret < 0) 138284b36ce5SJonathan Cameron goto error_clear_mux_table; 138384b36ce5SJonathan Cameron } 138484b36ce5SJonathan Cameron return 0; 138584b36ce5SJonathan Cameron 138684b36ce5SJonathan Cameron error_clear_mux_table: 138784b36ce5SJonathan Cameron list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) 138884b36ce5SJonathan Cameron iio_buffer_demux_free(buffer); 138984b36ce5SJonathan Cameron 139084b36ce5SJonathan Cameron return ret; 139184b36ce5SJonathan Cameron } 1392a980e046SJonathan Cameron EXPORT_SYMBOL_GPL(iio_update_demux); 13939e69c935SLars-Peter Clausen 13949e69c935SLars-Peter Clausen /** 13959e69c935SLars-Peter Clausen * iio_buffer_release() - Free a buffer's resources 13969e69c935SLars-Peter Clausen * @ref: Pointer to the kref embedded in the iio_buffer struct 13979e69c935SLars-Peter Clausen * 13989e69c935SLars-Peter Clausen * This function is called when the last reference to the buffer has been 13999e69c935SLars-Peter Clausen * dropped. It will typically free all resources allocated by the buffer. Do not 14009e69c935SLars-Peter Clausen * call this function manually, always use iio_buffer_put() when done using a 14019e69c935SLars-Peter Clausen * buffer. 14029e69c935SLars-Peter Clausen */ 14039e69c935SLars-Peter Clausen static void iio_buffer_release(struct kref *ref) 14049e69c935SLars-Peter Clausen { 14059e69c935SLars-Peter Clausen struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref); 14069e69c935SLars-Peter Clausen 14079e69c935SLars-Peter Clausen buffer->access->release(buffer); 14089e69c935SLars-Peter Clausen } 14099e69c935SLars-Peter Clausen 14109e69c935SLars-Peter Clausen /** 14119e69c935SLars-Peter Clausen * iio_buffer_get() - Grab a reference to the buffer 14129e69c935SLars-Peter Clausen * @buffer: The buffer to grab a reference for, may be NULL 14139e69c935SLars-Peter Clausen * 14149e69c935SLars-Peter Clausen * Returns the pointer to the buffer that was passed into the function. 14159e69c935SLars-Peter Clausen */ 14169e69c935SLars-Peter Clausen struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer) 14179e69c935SLars-Peter Clausen { 14189e69c935SLars-Peter Clausen if (buffer) 14199e69c935SLars-Peter Clausen kref_get(&buffer->ref); 14209e69c935SLars-Peter Clausen 14219e69c935SLars-Peter Clausen return buffer; 14229e69c935SLars-Peter Clausen } 14239e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_get); 14249e69c935SLars-Peter Clausen 14259e69c935SLars-Peter Clausen /** 14269e69c935SLars-Peter Clausen * iio_buffer_put() - Release the reference to the buffer 14279e69c935SLars-Peter Clausen * @buffer: The buffer to release the reference for, may be NULL 14289e69c935SLars-Peter Clausen */ 14299e69c935SLars-Peter Clausen void iio_buffer_put(struct iio_buffer *buffer) 14309e69c935SLars-Peter Clausen { 14319e69c935SLars-Peter Clausen if (buffer) 14329e69c935SLars-Peter Clausen kref_put(&buffer->ref, iio_buffer_release); 14339e69c935SLars-Peter Clausen } 14349e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_put); 1435