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; 1135dba4b14SColin 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 2129f466777SJonathan Cameron /** 2139f466777SJonathan Cameron * iio_buffer_set_attrs - Set buffer specific attributes 2149f466777SJonathan Cameron * @buffer: The buffer for which we are setting attributes 2159f466777SJonathan Cameron * @attrs: Pointer to a null terminated list of pointers to attributes 2169f466777SJonathan Cameron */ 2179f466777SJonathan Cameron void iio_buffer_set_attrs(struct iio_buffer *buffer, 2189f466777SJonathan Cameron const struct attribute **attrs) 2199f466777SJonathan Cameron { 2209f466777SJonathan Cameron buffer->attrs = attrs; 2219f466777SJonathan Cameron } 2229f466777SJonathan Cameron EXPORT_SYMBOL_GPL(iio_buffer_set_attrs); 2239f466777SJonathan Cameron 224a980e046SJonathan Cameron static ssize_t iio_show_scan_index(struct device *dev, 225a980e046SJonathan Cameron struct device_attribute *attr, 226a980e046SJonathan Cameron char *buf) 227a980e046SJonathan Cameron { 228a980e046SJonathan Cameron return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index); 229a980e046SJonathan Cameron } 230a980e046SJonathan Cameron 231a980e046SJonathan Cameron static ssize_t iio_show_fixed_type(struct device *dev, 232a980e046SJonathan Cameron struct device_attribute *attr, 233a980e046SJonathan Cameron char *buf) 234a980e046SJonathan Cameron { 235a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 236a980e046SJonathan Cameron u8 type = this_attr->c->scan_type.endianness; 237a980e046SJonathan Cameron 238a980e046SJonathan Cameron if (type == IIO_CPU) { 239a980e046SJonathan Cameron #ifdef __LITTLE_ENDIAN 240a980e046SJonathan Cameron type = IIO_LE; 241a980e046SJonathan Cameron #else 242a980e046SJonathan Cameron type = IIO_BE; 243a980e046SJonathan Cameron #endif 244a980e046SJonathan Cameron } 2450ee8546aSSrinivas Pandruvada if (this_attr->c->scan_type.repeat > 1) 2460ee8546aSSrinivas Pandruvada return sprintf(buf, "%s:%c%d/%dX%d>>%u\n", 2470ee8546aSSrinivas Pandruvada iio_endian_prefix[type], 2480ee8546aSSrinivas Pandruvada this_attr->c->scan_type.sign, 2490ee8546aSSrinivas Pandruvada this_attr->c->scan_type.realbits, 2500ee8546aSSrinivas Pandruvada this_attr->c->scan_type.storagebits, 2510ee8546aSSrinivas Pandruvada this_attr->c->scan_type.repeat, 2520ee8546aSSrinivas Pandruvada this_attr->c->scan_type.shift); 2530ee8546aSSrinivas Pandruvada else 254a980e046SJonathan Cameron return sprintf(buf, "%s:%c%d/%d>>%u\n", 255a980e046SJonathan Cameron iio_endian_prefix[type], 256a980e046SJonathan Cameron this_attr->c->scan_type.sign, 257a980e046SJonathan Cameron this_attr->c->scan_type.realbits, 258a980e046SJonathan Cameron this_attr->c->scan_type.storagebits, 259a980e046SJonathan Cameron this_attr->c->scan_type.shift); 260a980e046SJonathan Cameron } 261a980e046SJonathan Cameron 262a980e046SJonathan Cameron static ssize_t iio_scan_el_show(struct device *dev, 263a980e046SJonathan Cameron struct device_attribute *attr, 264a980e046SJonathan Cameron char *buf) 265a980e046SJonathan Cameron { 266a980e046SJonathan Cameron int ret; 267e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 268a980e046SJonathan Cameron 2692076a20fSAlec Berg /* Ensure ret is 0 or 1. */ 2702076a20fSAlec Berg ret = !!test_bit(to_iio_dev_attr(attr)->address, 271a980e046SJonathan Cameron indio_dev->buffer->scan_mask); 272a980e046SJonathan Cameron 273a980e046SJonathan Cameron return sprintf(buf, "%d\n", ret); 274a980e046SJonathan Cameron } 275a980e046SJonathan Cameron 276217a5cf0SLars-Peter Clausen /* Note NULL used as error indicator as it doesn't make sense. */ 277217a5cf0SLars-Peter Clausen static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks, 278217a5cf0SLars-Peter Clausen unsigned int masklength, 2791e1ec286SLars-Peter Clausen const unsigned long *mask, 2801e1ec286SLars-Peter Clausen bool strict) 281217a5cf0SLars-Peter Clausen { 282217a5cf0SLars-Peter Clausen if (bitmap_empty(mask, masklength)) 283217a5cf0SLars-Peter Clausen return NULL; 284217a5cf0SLars-Peter Clausen while (*av_masks) { 2851e1ec286SLars-Peter Clausen if (strict) { 2861e1ec286SLars-Peter Clausen if (bitmap_equal(mask, av_masks, masklength)) 2871e1ec286SLars-Peter Clausen return av_masks; 2881e1ec286SLars-Peter Clausen } else { 289217a5cf0SLars-Peter Clausen if (bitmap_subset(mask, av_masks, masklength)) 290217a5cf0SLars-Peter Clausen return av_masks; 2911e1ec286SLars-Peter Clausen } 292217a5cf0SLars-Peter Clausen av_masks += BITS_TO_LONGS(masklength); 293217a5cf0SLars-Peter Clausen } 294217a5cf0SLars-Peter Clausen return NULL; 295217a5cf0SLars-Peter Clausen } 296217a5cf0SLars-Peter Clausen 297217a5cf0SLars-Peter Clausen static bool iio_validate_scan_mask(struct iio_dev *indio_dev, 298217a5cf0SLars-Peter Clausen const unsigned long *mask) 299217a5cf0SLars-Peter Clausen { 300217a5cf0SLars-Peter Clausen if (!indio_dev->setup_ops->validate_scan_mask) 301217a5cf0SLars-Peter Clausen return true; 302217a5cf0SLars-Peter Clausen 303217a5cf0SLars-Peter Clausen return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask); 304217a5cf0SLars-Peter Clausen } 305217a5cf0SLars-Peter Clausen 306217a5cf0SLars-Peter Clausen /** 307217a5cf0SLars-Peter Clausen * iio_scan_mask_set() - set particular bit in the scan mask 308217a5cf0SLars-Peter Clausen * @indio_dev: the iio device 309217a5cf0SLars-Peter Clausen * @buffer: the buffer whose scan mask we are interested in 310217a5cf0SLars-Peter Clausen * @bit: the bit to be set. 311217a5cf0SLars-Peter Clausen * 312217a5cf0SLars-Peter Clausen * Note that at this point we have no way of knowing what other 313217a5cf0SLars-Peter Clausen * buffers might request, hence this code only verifies that the 314217a5cf0SLars-Peter Clausen * individual buffers request is plausible. 315217a5cf0SLars-Peter Clausen */ 316217a5cf0SLars-Peter Clausen static int iio_scan_mask_set(struct iio_dev *indio_dev, 317217a5cf0SLars-Peter Clausen struct iio_buffer *buffer, int bit) 318217a5cf0SLars-Peter Clausen { 319217a5cf0SLars-Peter Clausen const unsigned long *mask; 320217a5cf0SLars-Peter Clausen unsigned long *trialmask; 321217a5cf0SLars-Peter Clausen 322057ac1acSMarkus Elfring trialmask = kmalloc_array(BITS_TO_LONGS(indio_dev->masklength), 323057ac1acSMarkus Elfring sizeof(*trialmask), 324217a5cf0SLars-Peter Clausen GFP_KERNEL); 325217a5cf0SLars-Peter Clausen if (trialmask == NULL) 326217a5cf0SLars-Peter Clausen return -ENOMEM; 327217a5cf0SLars-Peter Clausen if (!indio_dev->masklength) { 328231bfe53SDan Carpenter WARN(1, "Trying to set scanmask prior to registering buffer\n"); 329217a5cf0SLars-Peter Clausen goto err_invalid_mask; 330217a5cf0SLars-Peter Clausen } 331217a5cf0SLars-Peter Clausen bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength); 332217a5cf0SLars-Peter Clausen set_bit(bit, trialmask); 333217a5cf0SLars-Peter Clausen 334217a5cf0SLars-Peter Clausen if (!iio_validate_scan_mask(indio_dev, trialmask)) 335217a5cf0SLars-Peter Clausen goto err_invalid_mask; 336217a5cf0SLars-Peter Clausen 337217a5cf0SLars-Peter Clausen if (indio_dev->available_scan_masks) { 338217a5cf0SLars-Peter Clausen mask = iio_scan_mask_match(indio_dev->available_scan_masks, 339217a5cf0SLars-Peter Clausen indio_dev->masklength, 3401e1ec286SLars-Peter Clausen trialmask, false); 341217a5cf0SLars-Peter Clausen if (!mask) 342217a5cf0SLars-Peter Clausen goto err_invalid_mask; 343217a5cf0SLars-Peter Clausen } 344217a5cf0SLars-Peter Clausen bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength); 345217a5cf0SLars-Peter Clausen 346217a5cf0SLars-Peter Clausen kfree(trialmask); 347217a5cf0SLars-Peter Clausen 348217a5cf0SLars-Peter Clausen return 0; 349217a5cf0SLars-Peter Clausen 350217a5cf0SLars-Peter Clausen err_invalid_mask: 351217a5cf0SLars-Peter Clausen kfree(trialmask); 352217a5cf0SLars-Peter Clausen return -EINVAL; 353217a5cf0SLars-Peter Clausen } 354217a5cf0SLars-Peter Clausen 355a980e046SJonathan Cameron static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit) 356a980e046SJonathan Cameron { 357a980e046SJonathan Cameron clear_bit(bit, buffer->scan_mask); 358a980e046SJonathan Cameron return 0; 359a980e046SJonathan Cameron } 360a980e046SJonathan Cameron 361*c2bf8d5fSJonathan Cameron static int iio_scan_mask_query(struct iio_dev *indio_dev, 362*c2bf8d5fSJonathan Cameron struct iio_buffer *buffer, int bit) 363*c2bf8d5fSJonathan Cameron { 364*c2bf8d5fSJonathan Cameron if (bit > indio_dev->masklength) 365*c2bf8d5fSJonathan Cameron return -EINVAL; 366*c2bf8d5fSJonathan Cameron 367*c2bf8d5fSJonathan Cameron if (!buffer->scan_mask) 368*c2bf8d5fSJonathan Cameron return 0; 369*c2bf8d5fSJonathan Cameron 370*c2bf8d5fSJonathan Cameron /* Ensure return value is 0 or 1. */ 371*c2bf8d5fSJonathan Cameron return !!test_bit(bit, buffer->scan_mask); 372*c2bf8d5fSJonathan Cameron }; 373*c2bf8d5fSJonathan Cameron 374a980e046SJonathan Cameron static ssize_t iio_scan_el_store(struct device *dev, 375a980e046SJonathan Cameron struct device_attribute *attr, 376a980e046SJonathan Cameron const char *buf, 377a980e046SJonathan Cameron size_t len) 378a980e046SJonathan Cameron { 379a980e046SJonathan Cameron int ret; 380a980e046SJonathan Cameron bool state; 381e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 382a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 383a980e046SJonathan Cameron struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 384a980e046SJonathan Cameron 385a980e046SJonathan Cameron ret = strtobool(buf, &state); 386a980e046SJonathan Cameron if (ret < 0) 387a980e046SJonathan Cameron return ret; 388a980e046SJonathan Cameron mutex_lock(&indio_dev->mlock); 389705ee2c9SLars-Peter Clausen if (iio_buffer_is_active(indio_dev->buffer)) { 390a980e046SJonathan Cameron ret = -EBUSY; 391a980e046SJonathan Cameron goto error_ret; 392a980e046SJonathan Cameron } 393a980e046SJonathan Cameron ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address); 394a980e046SJonathan Cameron if (ret < 0) 395a980e046SJonathan Cameron goto error_ret; 396a980e046SJonathan Cameron if (!state && ret) { 397a980e046SJonathan Cameron ret = iio_scan_mask_clear(buffer, this_attr->address); 398a980e046SJonathan Cameron if (ret) 399a980e046SJonathan Cameron goto error_ret; 400a980e046SJonathan Cameron } else if (state && !ret) { 401a980e046SJonathan Cameron ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address); 402a980e046SJonathan Cameron if (ret) 403a980e046SJonathan Cameron goto error_ret; 404a980e046SJonathan Cameron } 405a980e046SJonathan Cameron 406a980e046SJonathan Cameron error_ret: 407a980e046SJonathan Cameron mutex_unlock(&indio_dev->mlock); 408a980e046SJonathan Cameron 409a980e046SJonathan Cameron return ret < 0 ? ret : len; 410a980e046SJonathan Cameron 411a980e046SJonathan Cameron } 412a980e046SJonathan Cameron 413a980e046SJonathan Cameron static ssize_t iio_scan_el_ts_show(struct device *dev, 414a980e046SJonathan Cameron struct device_attribute *attr, 415a980e046SJonathan Cameron char *buf) 416a980e046SJonathan Cameron { 417e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 418a980e046SJonathan Cameron return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp); 419a980e046SJonathan Cameron } 420a980e046SJonathan Cameron 421a980e046SJonathan Cameron static ssize_t iio_scan_el_ts_store(struct device *dev, 422a980e046SJonathan Cameron struct device_attribute *attr, 423a980e046SJonathan Cameron const char *buf, 424a980e046SJonathan Cameron size_t len) 425a980e046SJonathan Cameron { 426a980e046SJonathan Cameron int ret; 427e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 428a980e046SJonathan Cameron bool state; 429a980e046SJonathan Cameron 430a980e046SJonathan Cameron ret = strtobool(buf, &state); 431a980e046SJonathan Cameron if (ret < 0) 432a980e046SJonathan Cameron return ret; 433a980e046SJonathan Cameron 434a980e046SJonathan Cameron mutex_lock(&indio_dev->mlock); 435705ee2c9SLars-Peter Clausen if (iio_buffer_is_active(indio_dev->buffer)) { 436a980e046SJonathan Cameron ret = -EBUSY; 437a980e046SJonathan Cameron goto error_ret; 438a980e046SJonathan Cameron } 439a980e046SJonathan Cameron indio_dev->buffer->scan_timestamp = state; 440a980e046SJonathan Cameron error_ret: 441a980e046SJonathan Cameron mutex_unlock(&indio_dev->mlock); 442a980e046SJonathan Cameron 443a980e046SJonathan Cameron return ret ? ret : len; 444a980e046SJonathan Cameron } 445a980e046SJonathan Cameron 446a980e046SJonathan Cameron static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev, 447a980e046SJonathan Cameron const struct iio_chan_spec *chan) 448a980e046SJonathan Cameron { 449a980e046SJonathan Cameron int ret, attrcount = 0; 450a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 451a980e046SJonathan Cameron 452a980e046SJonathan Cameron ret = __iio_add_chan_devattr("index", 453a980e046SJonathan Cameron chan, 454a980e046SJonathan Cameron &iio_show_scan_index, 455a980e046SJonathan Cameron NULL, 456a980e046SJonathan Cameron 0, 4573704432fSJonathan Cameron IIO_SEPARATE, 458a980e046SJonathan Cameron &indio_dev->dev, 459a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 460a980e046SJonathan Cameron if (ret) 46192825ff9SHartmut Knaack return ret; 462a980e046SJonathan Cameron attrcount++; 463a980e046SJonathan Cameron ret = __iio_add_chan_devattr("type", 464a980e046SJonathan Cameron chan, 465a980e046SJonathan Cameron &iio_show_fixed_type, 466a980e046SJonathan Cameron NULL, 467a980e046SJonathan Cameron 0, 468a980e046SJonathan Cameron 0, 469a980e046SJonathan Cameron &indio_dev->dev, 470a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 471a980e046SJonathan Cameron if (ret) 47292825ff9SHartmut Knaack return ret; 473a980e046SJonathan Cameron attrcount++; 474a980e046SJonathan Cameron if (chan->type != IIO_TIMESTAMP) 475a980e046SJonathan Cameron ret = __iio_add_chan_devattr("en", 476a980e046SJonathan Cameron chan, 477a980e046SJonathan Cameron &iio_scan_el_show, 478a980e046SJonathan Cameron &iio_scan_el_store, 479a980e046SJonathan Cameron chan->scan_index, 480a980e046SJonathan Cameron 0, 481a980e046SJonathan Cameron &indio_dev->dev, 482a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 483a980e046SJonathan Cameron else 484a980e046SJonathan Cameron ret = __iio_add_chan_devattr("en", 485a980e046SJonathan Cameron chan, 486a980e046SJonathan Cameron &iio_scan_el_ts_show, 487a980e046SJonathan Cameron &iio_scan_el_ts_store, 488a980e046SJonathan Cameron chan->scan_index, 489a980e046SJonathan Cameron 0, 490a980e046SJonathan Cameron &indio_dev->dev, 491a980e046SJonathan Cameron &buffer->scan_el_dev_attr_list); 4929572588cSPeter Meerwald if (ret) 49392825ff9SHartmut Knaack return ret; 494a980e046SJonathan Cameron attrcount++; 495a980e046SJonathan Cameron ret = attrcount; 496a980e046SJonathan Cameron return ret; 497a980e046SJonathan Cameron } 498a980e046SJonathan Cameron 49908e7e0adSLars-Peter Clausen static ssize_t iio_buffer_read_length(struct device *dev, 500a980e046SJonathan Cameron struct device_attribute *attr, 501a980e046SJonathan Cameron char *buf) 502a980e046SJonathan Cameron { 503e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 504a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 505a980e046SJonathan Cameron 50637495660SLars-Peter Clausen return sprintf(buf, "%d\n", buffer->length); 507a980e046SJonathan Cameron } 508a980e046SJonathan Cameron 50908e7e0adSLars-Peter Clausen static ssize_t iio_buffer_write_length(struct device *dev, 510a980e046SJonathan Cameron struct device_attribute *attr, 51108e7e0adSLars-Peter Clausen const char *buf, size_t len) 512a980e046SJonathan Cameron { 513e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 514a980e046SJonathan Cameron struct iio_buffer *buffer = indio_dev->buffer; 515948ad205SLars-Peter Clausen unsigned int val; 516948ad205SLars-Peter Clausen int ret; 517a980e046SJonathan Cameron 518948ad205SLars-Peter Clausen ret = kstrtouint(buf, 10, &val); 519a980e046SJonathan Cameron if (ret) 520a980e046SJonathan Cameron return ret; 521a980e046SJonathan Cameron 52237495660SLars-Peter Clausen if (val == buffer->length) 523a980e046SJonathan Cameron return len; 524a980e046SJonathan Cameron 525a980e046SJonathan Cameron mutex_lock(&indio_dev->mlock); 526705ee2c9SLars-Peter Clausen if (iio_buffer_is_active(indio_dev->buffer)) { 527a980e046SJonathan Cameron ret = -EBUSY; 528a980e046SJonathan Cameron } else { 529a980e046SJonathan Cameron buffer->access->set_length(buffer, val); 530a980e046SJonathan Cameron ret = 0; 531a980e046SJonathan Cameron } 53237d34556SJosselin Costanzi if (ret) 53337d34556SJosselin Costanzi goto out; 53437d34556SJosselin Costanzi if (buffer->length && buffer->length < buffer->watermark) 53537d34556SJosselin Costanzi buffer->watermark = buffer->length; 53637d34556SJosselin Costanzi out: 537a980e046SJonathan Cameron mutex_unlock(&indio_dev->mlock); 538a980e046SJonathan Cameron 539a980e046SJonathan Cameron return ret ? ret : len; 540a980e046SJonathan Cameron } 541a980e046SJonathan Cameron 54208e7e0adSLars-Peter Clausen static ssize_t iio_buffer_show_enable(struct device *dev, 543a980e046SJonathan Cameron struct device_attribute *attr, 544a980e046SJonathan Cameron char *buf) 545a980e046SJonathan Cameron { 546e53f5ac5SLars-Peter Clausen struct iio_dev *indio_dev = dev_to_iio_dev(dev); 547705ee2c9SLars-Peter Clausen return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer)); 548a980e046SJonathan Cameron } 549a980e046SJonathan Cameron 550182b4905SLars-Peter Clausen static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev, 551182b4905SLars-Peter Clausen unsigned int scan_index) 552182b4905SLars-Peter Clausen { 553182b4905SLars-Peter Clausen const struct iio_chan_spec *ch; 554182b4905SLars-Peter Clausen unsigned int bytes; 555182b4905SLars-Peter Clausen 556182b4905SLars-Peter Clausen ch = iio_find_channel_from_si(indio_dev, scan_index); 557182b4905SLars-Peter Clausen bytes = ch->scan_type.storagebits / 8; 558182b4905SLars-Peter Clausen if (ch->scan_type.repeat > 1) 559182b4905SLars-Peter Clausen bytes *= ch->scan_type.repeat; 560182b4905SLars-Peter Clausen return bytes; 561182b4905SLars-Peter Clausen } 562182b4905SLars-Peter Clausen 563182b4905SLars-Peter Clausen static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev) 564182b4905SLars-Peter Clausen { 565182b4905SLars-Peter Clausen return iio_storage_bytes_for_si(indio_dev, 566182b4905SLars-Peter Clausen indio_dev->scan_index_timestamp); 567182b4905SLars-Peter Clausen } 568182b4905SLars-Peter Clausen 569183f4173SPeter Meerwald static int iio_compute_scan_bytes(struct iio_dev *indio_dev, 570183f4173SPeter Meerwald const unsigned long *mask, bool timestamp) 571a980e046SJonathan Cameron { 572a980e046SJonathan Cameron unsigned bytes = 0; 573a980e046SJonathan Cameron int length, i; 574a980e046SJonathan Cameron 575a980e046SJonathan Cameron /* How much space will the demuxed element take? */ 576a980e046SJonathan Cameron for_each_set_bit(i, mask, 577a980e046SJonathan Cameron indio_dev->masklength) { 578182b4905SLars-Peter Clausen length = iio_storage_bytes_for_si(indio_dev, i); 579a980e046SJonathan Cameron bytes = ALIGN(bytes, length); 580a980e046SJonathan Cameron bytes += length; 581a980e046SJonathan Cameron } 582182b4905SLars-Peter Clausen 583a980e046SJonathan Cameron if (timestamp) { 584182b4905SLars-Peter Clausen length = iio_storage_bytes_for_timestamp(indio_dev); 585a980e046SJonathan Cameron bytes = ALIGN(bytes, length); 586a980e046SJonathan Cameron bytes += length; 587a980e046SJonathan Cameron } 588a980e046SJonathan Cameron return bytes; 589a980e046SJonathan Cameron } 590a980e046SJonathan Cameron 5919e69c935SLars-Peter Clausen static void iio_buffer_activate(struct iio_dev *indio_dev, 5929e69c935SLars-Peter Clausen struct iio_buffer *buffer) 5939e69c935SLars-Peter Clausen { 5949e69c935SLars-Peter Clausen iio_buffer_get(buffer); 5959e69c935SLars-Peter Clausen list_add(&buffer->buffer_list, &indio_dev->buffer_list); 5969e69c935SLars-Peter Clausen } 5979e69c935SLars-Peter Clausen 5989e69c935SLars-Peter Clausen static void iio_buffer_deactivate(struct iio_buffer *buffer) 5999e69c935SLars-Peter Clausen { 6009e69c935SLars-Peter Clausen list_del_init(&buffer->buffer_list); 60137d34556SJosselin Costanzi wake_up_interruptible(&buffer->pollq); 6029e69c935SLars-Peter Clausen iio_buffer_put(buffer); 6039e69c935SLars-Peter Clausen } 6049e69c935SLars-Peter Clausen 6051250186aSLars-Peter Clausen static void iio_buffer_deactivate_all(struct iio_dev *indio_dev) 6061250186aSLars-Peter Clausen { 6071250186aSLars-Peter Clausen struct iio_buffer *buffer, *_buffer; 6081250186aSLars-Peter Clausen 6091250186aSLars-Peter Clausen list_for_each_entry_safe(buffer, _buffer, 6101250186aSLars-Peter Clausen &indio_dev->buffer_list, buffer_list) 6111250186aSLars-Peter Clausen iio_buffer_deactivate(buffer); 6121250186aSLars-Peter Clausen } 6131250186aSLars-Peter Clausen 614e18a2ad4SLars-Peter Clausen static int iio_buffer_enable(struct iio_buffer *buffer, 615e18a2ad4SLars-Peter Clausen struct iio_dev *indio_dev) 616e18a2ad4SLars-Peter Clausen { 617e18a2ad4SLars-Peter Clausen if (!buffer->access->enable) 618e18a2ad4SLars-Peter Clausen return 0; 619e18a2ad4SLars-Peter Clausen return buffer->access->enable(buffer, indio_dev); 620e18a2ad4SLars-Peter Clausen } 621e18a2ad4SLars-Peter Clausen 622e18a2ad4SLars-Peter Clausen static int iio_buffer_disable(struct iio_buffer *buffer, 623e18a2ad4SLars-Peter Clausen struct iio_dev *indio_dev) 624e18a2ad4SLars-Peter Clausen { 625e18a2ad4SLars-Peter Clausen if (!buffer->access->disable) 626e18a2ad4SLars-Peter Clausen return 0; 627e18a2ad4SLars-Peter Clausen return buffer->access->disable(buffer, indio_dev); 628e18a2ad4SLars-Peter Clausen } 629e18a2ad4SLars-Peter Clausen 6308e050996SLars-Peter Clausen static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev, 6318e050996SLars-Peter Clausen struct iio_buffer *buffer) 6328e050996SLars-Peter Clausen { 6338e050996SLars-Peter Clausen unsigned int bytes; 6348e050996SLars-Peter Clausen 6358e050996SLars-Peter Clausen if (!buffer->access->set_bytes_per_datum) 6368e050996SLars-Peter Clausen return; 6378e050996SLars-Peter Clausen 6388e050996SLars-Peter Clausen bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask, 6398e050996SLars-Peter Clausen buffer->scan_timestamp); 6408e050996SLars-Peter Clausen 6418e050996SLars-Peter Clausen buffer->access->set_bytes_per_datum(buffer, bytes); 6428e050996SLars-Peter Clausen } 6438e050996SLars-Peter Clausen 644fcc1b2f5SLars-Peter Clausen static int iio_buffer_request_update(struct iio_dev *indio_dev, 645fcc1b2f5SLars-Peter Clausen struct iio_buffer *buffer) 646fcc1b2f5SLars-Peter Clausen { 647fcc1b2f5SLars-Peter Clausen int ret; 648fcc1b2f5SLars-Peter Clausen 649fcc1b2f5SLars-Peter Clausen iio_buffer_update_bytes_per_datum(indio_dev, buffer); 650fcc1b2f5SLars-Peter Clausen if (buffer->access->request_update) { 651fcc1b2f5SLars-Peter Clausen ret = buffer->access->request_update(buffer); 652fcc1b2f5SLars-Peter Clausen if (ret) { 653fcc1b2f5SLars-Peter Clausen dev_dbg(&indio_dev->dev, 654fcc1b2f5SLars-Peter Clausen "Buffer not started: buffer parameter update failed (%d)\n", 655fcc1b2f5SLars-Peter Clausen ret); 656fcc1b2f5SLars-Peter Clausen return ret; 657fcc1b2f5SLars-Peter Clausen } 658fcc1b2f5SLars-Peter Clausen } 659fcc1b2f5SLars-Peter Clausen 660fcc1b2f5SLars-Peter Clausen return 0; 661fcc1b2f5SLars-Peter Clausen } 662fcc1b2f5SLars-Peter Clausen 663248be5aaSLars-Peter Clausen static void iio_free_scan_mask(struct iio_dev *indio_dev, 664248be5aaSLars-Peter Clausen const unsigned long *mask) 665248be5aaSLars-Peter Clausen { 666248be5aaSLars-Peter Clausen /* If the mask is dynamically allocated free it, otherwise do nothing */ 667248be5aaSLars-Peter Clausen if (!indio_dev->available_scan_masks) 668248be5aaSLars-Peter Clausen kfree(mask); 669248be5aaSLars-Peter Clausen } 670248be5aaSLars-Peter Clausen 6716e509c4dSLars-Peter Clausen struct iio_device_config { 6726e509c4dSLars-Peter Clausen unsigned int mode; 673f0566c0cSLars-Peter Clausen unsigned int watermark; 6746e509c4dSLars-Peter Clausen const unsigned long *scan_mask; 6756e509c4dSLars-Peter Clausen unsigned int scan_bytes; 6766e509c4dSLars-Peter Clausen bool scan_timestamp; 6776e509c4dSLars-Peter Clausen }; 6786e509c4dSLars-Peter Clausen 6796e509c4dSLars-Peter Clausen static int iio_verify_update(struct iio_dev *indio_dev, 6806e509c4dSLars-Peter Clausen struct iio_buffer *insert_buffer, struct iio_buffer *remove_buffer, 6816e509c4dSLars-Peter Clausen struct iio_device_config *config) 6826e509c4dSLars-Peter Clausen { 6836e509c4dSLars-Peter Clausen unsigned long *compound_mask; 6846e509c4dSLars-Peter Clausen const unsigned long *scan_mask; 6851e1ec286SLars-Peter Clausen bool strict_scanmask = false; 6866e509c4dSLars-Peter Clausen struct iio_buffer *buffer; 6876e509c4dSLars-Peter Clausen bool scan_timestamp; 688225d59adSLars-Peter Clausen unsigned int modes; 6896e509c4dSLars-Peter Clausen 6906e509c4dSLars-Peter Clausen memset(config, 0, sizeof(*config)); 6911bef2c1dSIrina Tirdea config->watermark = ~0; 6926e509c4dSLars-Peter Clausen 6936e509c4dSLars-Peter Clausen /* 6946e509c4dSLars-Peter Clausen * If there is just one buffer and we are removing it there is nothing 6956e509c4dSLars-Peter Clausen * to verify. 6966e509c4dSLars-Peter Clausen */ 6976e509c4dSLars-Peter Clausen if (remove_buffer && !insert_buffer && 6986e509c4dSLars-Peter Clausen list_is_singular(&indio_dev->buffer_list)) 6996e509c4dSLars-Peter Clausen return 0; 7006e509c4dSLars-Peter Clausen 701225d59adSLars-Peter Clausen modes = indio_dev->modes; 702225d59adSLars-Peter Clausen 703225d59adSLars-Peter Clausen list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 704225d59adSLars-Peter Clausen if (buffer == remove_buffer) 705225d59adSLars-Peter Clausen continue; 706225d59adSLars-Peter Clausen modes &= buffer->access->modes; 707f0566c0cSLars-Peter Clausen config->watermark = min(config->watermark, buffer->watermark); 708225d59adSLars-Peter Clausen } 709225d59adSLars-Peter Clausen 710f0566c0cSLars-Peter Clausen if (insert_buffer) { 711225d59adSLars-Peter Clausen modes &= insert_buffer->access->modes; 712f0566c0cSLars-Peter Clausen config->watermark = min(config->watermark, 713f0566c0cSLars-Peter Clausen insert_buffer->watermark); 714f0566c0cSLars-Peter Clausen } 715225d59adSLars-Peter Clausen 7166e509c4dSLars-Peter Clausen /* Definitely possible for devices to support both of these. */ 717225d59adSLars-Peter Clausen if ((modes & INDIO_BUFFER_TRIGGERED) && indio_dev->trig) { 7186e509c4dSLars-Peter Clausen config->mode = INDIO_BUFFER_TRIGGERED; 719225d59adSLars-Peter Clausen } else if (modes & INDIO_BUFFER_HARDWARE) { 7201e1ec286SLars-Peter Clausen /* 7211e1ec286SLars-Peter Clausen * Keep things simple for now and only allow a single buffer to 7221e1ec286SLars-Peter Clausen * be connected in hardware mode. 7231e1ec286SLars-Peter Clausen */ 7241e1ec286SLars-Peter Clausen if (insert_buffer && !list_empty(&indio_dev->buffer_list)) 7251e1ec286SLars-Peter Clausen return -EINVAL; 7266e509c4dSLars-Peter Clausen config->mode = INDIO_BUFFER_HARDWARE; 7271e1ec286SLars-Peter Clausen strict_scanmask = true; 728225d59adSLars-Peter Clausen } else if (modes & INDIO_BUFFER_SOFTWARE) { 7296e509c4dSLars-Peter Clausen config->mode = INDIO_BUFFER_SOFTWARE; 7306e509c4dSLars-Peter Clausen } else { 7316e509c4dSLars-Peter Clausen /* Can only occur on first buffer */ 7326e509c4dSLars-Peter Clausen if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 7336e509c4dSLars-Peter Clausen dev_dbg(&indio_dev->dev, "Buffer not started: no trigger\n"); 7346e509c4dSLars-Peter Clausen return -EINVAL; 7356e509c4dSLars-Peter Clausen } 7366e509c4dSLars-Peter Clausen 7376e509c4dSLars-Peter Clausen /* What scan mask do we actually have? */ 7386e509c4dSLars-Peter Clausen compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), 7396e509c4dSLars-Peter Clausen sizeof(long), GFP_KERNEL); 7406e509c4dSLars-Peter Clausen if (compound_mask == NULL) 7416e509c4dSLars-Peter Clausen return -ENOMEM; 7426e509c4dSLars-Peter Clausen 7436e509c4dSLars-Peter Clausen scan_timestamp = false; 7446e509c4dSLars-Peter Clausen 7456e509c4dSLars-Peter Clausen list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 7466e509c4dSLars-Peter Clausen if (buffer == remove_buffer) 7476e509c4dSLars-Peter Clausen continue; 7486e509c4dSLars-Peter Clausen bitmap_or(compound_mask, compound_mask, buffer->scan_mask, 7496e509c4dSLars-Peter Clausen indio_dev->masklength); 7506e509c4dSLars-Peter Clausen scan_timestamp |= buffer->scan_timestamp; 7516e509c4dSLars-Peter Clausen } 7526e509c4dSLars-Peter Clausen 7536e509c4dSLars-Peter Clausen if (insert_buffer) { 7546e509c4dSLars-Peter Clausen bitmap_or(compound_mask, compound_mask, 7556e509c4dSLars-Peter Clausen insert_buffer->scan_mask, indio_dev->masklength); 7566e509c4dSLars-Peter Clausen scan_timestamp |= insert_buffer->scan_timestamp; 7576e509c4dSLars-Peter Clausen } 7586e509c4dSLars-Peter Clausen 7596e509c4dSLars-Peter Clausen if (indio_dev->available_scan_masks) { 7606e509c4dSLars-Peter Clausen scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks, 7616e509c4dSLars-Peter Clausen indio_dev->masklength, 7621e1ec286SLars-Peter Clausen compound_mask, 7631e1ec286SLars-Peter Clausen strict_scanmask); 7646e509c4dSLars-Peter Clausen kfree(compound_mask); 7656e509c4dSLars-Peter Clausen if (scan_mask == NULL) 7666e509c4dSLars-Peter Clausen return -EINVAL; 7676e509c4dSLars-Peter Clausen } else { 7686e509c4dSLars-Peter Clausen scan_mask = compound_mask; 7696e509c4dSLars-Peter Clausen } 7706e509c4dSLars-Peter Clausen 7716e509c4dSLars-Peter Clausen config->scan_bytes = iio_compute_scan_bytes(indio_dev, 7726e509c4dSLars-Peter Clausen scan_mask, scan_timestamp); 7736e509c4dSLars-Peter Clausen config->scan_mask = scan_mask; 7746e509c4dSLars-Peter Clausen config->scan_timestamp = scan_timestamp; 7756e509c4dSLars-Peter Clausen 7766e509c4dSLars-Peter Clausen return 0; 7776e509c4dSLars-Peter Clausen } 7786e509c4dSLars-Peter Clausen 77978c9981fSJonathan Cameron /** 78078c9981fSJonathan Cameron * struct iio_demux_table - table describing demux memcpy ops 78178c9981fSJonathan Cameron * @from: index to copy from 78278c9981fSJonathan Cameron * @to: index to copy to 78378c9981fSJonathan Cameron * @length: how many bytes to copy 78478c9981fSJonathan Cameron * @l: list head used for management 78578c9981fSJonathan Cameron */ 78678c9981fSJonathan Cameron struct iio_demux_table { 78778c9981fSJonathan Cameron unsigned from; 78878c9981fSJonathan Cameron unsigned to; 78978c9981fSJonathan Cameron unsigned length; 79078c9981fSJonathan Cameron struct list_head l; 79178c9981fSJonathan Cameron }; 79278c9981fSJonathan Cameron 79378c9981fSJonathan Cameron static void iio_buffer_demux_free(struct iio_buffer *buffer) 79478c9981fSJonathan Cameron { 79578c9981fSJonathan Cameron struct iio_demux_table *p, *q; 79678c9981fSJonathan Cameron list_for_each_entry_safe(p, q, &buffer->demux_list, l) { 79778c9981fSJonathan Cameron list_del(&p->l); 79878c9981fSJonathan Cameron kfree(p); 79978c9981fSJonathan Cameron } 80078c9981fSJonathan Cameron } 80178c9981fSJonathan Cameron 80278c9981fSJonathan Cameron static int iio_buffer_add_demux(struct iio_buffer *buffer, 80378c9981fSJonathan Cameron struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc, 80478c9981fSJonathan Cameron unsigned int length) 80578c9981fSJonathan Cameron { 80678c9981fSJonathan Cameron 80778c9981fSJonathan Cameron if (*p && (*p)->from + (*p)->length == in_loc && 80878c9981fSJonathan Cameron (*p)->to + (*p)->length == out_loc) { 80978c9981fSJonathan Cameron (*p)->length += length; 81078c9981fSJonathan Cameron } else { 81178c9981fSJonathan Cameron *p = kmalloc(sizeof(**p), GFP_KERNEL); 81278c9981fSJonathan Cameron if (*p == NULL) 81378c9981fSJonathan Cameron return -ENOMEM; 81478c9981fSJonathan Cameron (*p)->from = in_loc; 81578c9981fSJonathan Cameron (*p)->to = out_loc; 81678c9981fSJonathan Cameron (*p)->length = length; 81778c9981fSJonathan Cameron list_add_tail(&(*p)->l, &buffer->demux_list); 81878c9981fSJonathan Cameron } 81978c9981fSJonathan Cameron 82078c9981fSJonathan Cameron return 0; 82178c9981fSJonathan Cameron } 82278c9981fSJonathan Cameron 82378c9981fSJonathan Cameron static int iio_buffer_update_demux(struct iio_dev *indio_dev, 82478c9981fSJonathan Cameron struct iio_buffer *buffer) 82578c9981fSJonathan Cameron { 82678c9981fSJonathan Cameron int ret, in_ind = -1, out_ind, length; 82778c9981fSJonathan Cameron unsigned in_loc = 0, out_loc = 0; 82878c9981fSJonathan Cameron struct iio_demux_table *p = NULL; 82978c9981fSJonathan Cameron 83078c9981fSJonathan Cameron /* Clear out any old demux */ 83178c9981fSJonathan Cameron iio_buffer_demux_free(buffer); 83278c9981fSJonathan Cameron kfree(buffer->demux_bounce); 83378c9981fSJonathan Cameron buffer->demux_bounce = NULL; 83478c9981fSJonathan Cameron 83578c9981fSJonathan Cameron /* First work out which scan mode we will actually have */ 83678c9981fSJonathan Cameron if (bitmap_equal(indio_dev->active_scan_mask, 83778c9981fSJonathan Cameron buffer->scan_mask, 83878c9981fSJonathan Cameron indio_dev->masklength)) 83978c9981fSJonathan Cameron return 0; 84078c9981fSJonathan Cameron 84178c9981fSJonathan Cameron /* Now we have the two masks, work from least sig and build up sizes */ 84278c9981fSJonathan Cameron for_each_set_bit(out_ind, 84378c9981fSJonathan Cameron buffer->scan_mask, 84478c9981fSJonathan Cameron indio_dev->masklength) { 84578c9981fSJonathan Cameron in_ind = find_next_bit(indio_dev->active_scan_mask, 84678c9981fSJonathan Cameron indio_dev->masklength, 84778c9981fSJonathan Cameron in_ind + 1); 84878c9981fSJonathan Cameron while (in_ind != out_ind) { 84978c9981fSJonathan Cameron in_ind = find_next_bit(indio_dev->active_scan_mask, 85078c9981fSJonathan Cameron indio_dev->masklength, 85178c9981fSJonathan Cameron in_ind + 1); 85278c9981fSJonathan Cameron length = iio_storage_bytes_for_si(indio_dev, in_ind); 85378c9981fSJonathan Cameron /* Make sure we are aligned */ 85478c9981fSJonathan Cameron in_loc = roundup(in_loc, length) + length; 85578c9981fSJonathan Cameron } 85678c9981fSJonathan Cameron length = iio_storage_bytes_for_si(indio_dev, in_ind); 85778c9981fSJonathan Cameron out_loc = roundup(out_loc, length); 85878c9981fSJonathan Cameron in_loc = roundup(in_loc, length); 85978c9981fSJonathan Cameron ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); 86078c9981fSJonathan Cameron if (ret) 86178c9981fSJonathan Cameron goto error_clear_mux_table; 86278c9981fSJonathan Cameron out_loc += length; 86378c9981fSJonathan Cameron in_loc += length; 86478c9981fSJonathan Cameron } 86578c9981fSJonathan Cameron /* Relies on scan_timestamp being last */ 86678c9981fSJonathan Cameron if (buffer->scan_timestamp) { 86778c9981fSJonathan Cameron length = iio_storage_bytes_for_timestamp(indio_dev); 86878c9981fSJonathan Cameron out_loc = roundup(out_loc, length); 86978c9981fSJonathan Cameron in_loc = roundup(in_loc, length); 87078c9981fSJonathan Cameron ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); 87178c9981fSJonathan Cameron if (ret) 87278c9981fSJonathan Cameron goto error_clear_mux_table; 87378c9981fSJonathan Cameron out_loc += length; 87478c9981fSJonathan Cameron in_loc += length; 87578c9981fSJonathan Cameron } 87678c9981fSJonathan Cameron buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL); 87778c9981fSJonathan Cameron if (buffer->demux_bounce == NULL) { 87878c9981fSJonathan Cameron ret = -ENOMEM; 87978c9981fSJonathan Cameron goto error_clear_mux_table; 88078c9981fSJonathan Cameron } 88178c9981fSJonathan Cameron return 0; 88278c9981fSJonathan Cameron 88378c9981fSJonathan Cameron error_clear_mux_table: 88478c9981fSJonathan Cameron iio_buffer_demux_free(buffer); 88578c9981fSJonathan Cameron 88678c9981fSJonathan Cameron return ret; 88778c9981fSJonathan Cameron } 88878c9981fSJonathan Cameron 88978c9981fSJonathan Cameron static int iio_update_demux(struct iio_dev *indio_dev) 89078c9981fSJonathan Cameron { 89178c9981fSJonathan Cameron struct iio_buffer *buffer; 89278c9981fSJonathan Cameron int ret; 89378c9981fSJonathan Cameron 89478c9981fSJonathan Cameron list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 89578c9981fSJonathan Cameron ret = iio_buffer_update_demux(indio_dev, buffer); 89678c9981fSJonathan Cameron if (ret < 0) 89778c9981fSJonathan Cameron goto error_clear_mux_table; 89878c9981fSJonathan Cameron } 89978c9981fSJonathan Cameron return 0; 90078c9981fSJonathan Cameron 90178c9981fSJonathan Cameron error_clear_mux_table: 90278c9981fSJonathan Cameron list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) 90378c9981fSJonathan Cameron iio_buffer_demux_free(buffer); 90478c9981fSJonathan Cameron 90578c9981fSJonathan Cameron return ret; 90678c9981fSJonathan Cameron } 90778c9981fSJonathan Cameron 908623d74e3SLars-Peter Clausen static int iio_enable_buffers(struct iio_dev *indio_dev, 909623d74e3SLars-Peter Clausen struct iio_device_config *config) 910623d74e3SLars-Peter Clausen { 911e18a2ad4SLars-Peter Clausen struct iio_buffer *buffer; 912623d74e3SLars-Peter Clausen int ret; 913623d74e3SLars-Peter Clausen 914623d74e3SLars-Peter Clausen indio_dev->active_scan_mask = config->scan_mask; 915623d74e3SLars-Peter Clausen indio_dev->scan_timestamp = config->scan_timestamp; 916623d74e3SLars-Peter Clausen indio_dev->scan_bytes = config->scan_bytes; 917623d74e3SLars-Peter Clausen 918623d74e3SLars-Peter Clausen iio_update_demux(indio_dev); 919623d74e3SLars-Peter Clausen 920623d74e3SLars-Peter Clausen /* Wind up again */ 921623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->preenable) { 922623d74e3SLars-Peter Clausen ret = indio_dev->setup_ops->preenable(indio_dev); 923623d74e3SLars-Peter Clausen if (ret) { 924623d74e3SLars-Peter Clausen dev_dbg(&indio_dev->dev, 925623d74e3SLars-Peter Clausen "Buffer not started: buffer preenable failed (%d)\n", ret); 926623d74e3SLars-Peter Clausen goto err_undo_config; 927623d74e3SLars-Peter Clausen } 928623d74e3SLars-Peter Clausen } 929623d74e3SLars-Peter Clausen 930623d74e3SLars-Peter Clausen if (indio_dev->info->update_scan_mode) { 931623d74e3SLars-Peter Clausen ret = indio_dev->info 932623d74e3SLars-Peter Clausen ->update_scan_mode(indio_dev, 933623d74e3SLars-Peter Clausen indio_dev->active_scan_mask); 934623d74e3SLars-Peter Clausen if (ret < 0) { 935623d74e3SLars-Peter Clausen dev_dbg(&indio_dev->dev, 936623d74e3SLars-Peter Clausen "Buffer not started: update scan mode failed (%d)\n", 937623d74e3SLars-Peter Clausen ret); 938623d74e3SLars-Peter Clausen goto err_run_postdisable; 939623d74e3SLars-Peter Clausen } 940623d74e3SLars-Peter Clausen } 941623d74e3SLars-Peter Clausen 942f0566c0cSLars-Peter Clausen if (indio_dev->info->hwfifo_set_watermark) 943f0566c0cSLars-Peter Clausen indio_dev->info->hwfifo_set_watermark(indio_dev, 944f0566c0cSLars-Peter Clausen config->watermark); 945f0566c0cSLars-Peter Clausen 946e18a2ad4SLars-Peter Clausen list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 947e18a2ad4SLars-Peter Clausen ret = iio_buffer_enable(buffer, indio_dev); 948e18a2ad4SLars-Peter Clausen if (ret) 949e18a2ad4SLars-Peter Clausen goto err_disable_buffers; 950e18a2ad4SLars-Peter Clausen } 951e18a2ad4SLars-Peter Clausen 952623d74e3SLars-Peter Clausen indio_dev->currentmode = config->mode; 953623d74e3SLars-Peter Clausen 954623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->postenable) { 955623d74e3SLars-Peter Clausen ret = indio_dev->setup_ops->postenable(indio_dev); 956623d74e3SLars-Peter Clausen if (ret) { 957623d74e3SLars-Peter Clausen dev_dbg(&indio_dev->dev, 958623d74e3SLars-Peter Clausen "Buffer not started: postenable failed (%d)\n", ret); 959e18a2ad4SLars-Peter Clausen goto err_disable_buffers; 960623d74e3SLars-Peter Clausen } 961623d74e3SLars-Peter Clausen } 962623d74e3SLars-Peter Clausen 963623d74e3SLars-Peter Clausen return 0; 964623d74e3SLars-Peter Clausen 965e18a2ad4SLars-Peter Clausen err_disable_buffers: 966e18a2ad4SLars-Peter Clausen list_for_each_entry_continue_reverse(buffer, &indio_dev->buffer_list, 967e18a2ad4SLars-Peter Clausen buffer_list) 968e18a2ad4SLars-Peter Clausen iio_buffer_disable(buffer, indio_dev); 969623d74e3SLars-Peter Clausen err_run_postdisable: 970623d74e3SLars-Peter Clausen indio_dev->currentmode = INDIO_DIRECT_MODE; 971623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->postdisable) 972623d74e3SLars-Peter Clausen indio_dev->setup_ops->postdisable(indio_dev); 973623d74e3SLars-Peter Clausen err_undo_config: 974623d74e3SLars-Peter Clausen indio_dev->active_scan_mask = NULL; 975623d74e3SLars-Peter Clausen 976623d74e3SLars-Peter Clausen return ret; 977623d74e3SLars-Peter Clausen } 978623d74e3SLars-Peter Clausen 979623d74e3SLars-Peter Clausen static int iio_disable_buffers(struct iio_dev *indio_dev) 980623d74e3SLars-Peter Clausen { 981e18a2ad4SLars-Peter Clausen struct iio_buffer *buffer; 9821250186aSLars-Peter Clausen int ret = 0; 9831250186aSLars-Peter Clausen int ret2; 984623d74e3SLars-Peter Clausen 985623d74e3SLars-Peter Clausen /* Wind down existing buffers - iff there are any */ 986623d74e3SLars-Peter Clausen if (list_empty(&indio_dev->buffer_list)) 987623d74e3SLars-Peter Clausen return 0; 988623d74e3SLars-Peter Clausen 9891250186aSLars-Peter Clausen /* 9901250186aSLars-Peter Clausen * If things go wrong at some step in disable we still need to continue 9911250186aSLars-Peter Clausen * to perform the other steps, otherwise we leave the device in a 9921250186aSLars-Peter Clausen * inconsistent state. We return the error code for the first error we 9931250186aSLars-Peter Clausen * encountered. 9941250186aSLars-Peter Clausen */ 9951250186aSLars-Peter Clausen 996623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->predisable) { 9971250186aSLars-Peter Clausen ret2 = indio_dev->setup_ops->predisable(indio_dev); 9981250186aSLars-Peter Clausen if (ret2 && !ret) 9991250186aSLars-Peter Clausen ret = ret2; 1000623d74e3SLars-Peter Clausen } 1001623d74e3SLars-Peter Clausen 1002e18a2ad4SLars-Peter Clausen list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { 1003e18a2ad4SLars-Peter Clausen ret2 = iio_buffer_disable(buffer, indio_dev); 1004e18a2ad4SLars-Peter Clausen if (ret2 && !ret) 1005e18a2ad4SLars-Peter Clausen ret = ret2; 1006e18a2ad4SLars-Peter Clausen } 1007e18a2ad4SLars-Peter Clausen 1008623d74e3SLars-Peter Clausen indio_dev->currentmode = INDIO_DIRECT_MODE; 1009623d74e3SLars-Peter Clausen 1010623d74e3SLars-Peter Clausen if (indio_dev->setup_ops->postdisable) { 10111250186aSLars-Peter Clausen ret2 = indio_dev->setup_ops->postdisable(indio_dev); 10121250186aSLars-Peter Clausen if (ret2 && !ret) 10131250186aSLars-Peter Clausen ret = ret2; 1014623d74e3SLars-Peter Clausen } 1015623d74e3SLars-Peter Clausen 10161250186aSLars-Peter Clausen iio_free_scan_mask(indio_dev, indio_dev->active_scan_mask); 10171250186aSLars-Peter Clausen indio_dev->active_scan_mask = NULL; 10181250186aSLars-Peter Clausen 10191250186aSLars-Peter Clausen return ret; 1020623d74e3SLars-Peter Clausen } 1021623d74e3SLars-Peter Clausen 1022a9519456SLars-Peter Clausen static int __iio_update_buffers(struct iio_dev *indio_dev, 102384b36ce5SJonathan Cameron struct iio_buffer *insert_buffer, 102484b36ce5SJonathan Cameron struct iio_buffer *remove_buffer) 1025a980e046SJonathan Cameron { 10266e509c4dSLars-Peter Clausen struct iio_device_config new_config; 10271250186aSLars-Peter Clausen int ret; 10286e509c4dSLars-Peter Clausen 10296e509c4dSLars-Peter Clausen ret = iio_verify_update(indio_dev, insert_buffer, remove_buffer, 10306e509c4dSLars-Peter Clausen &new_config); 10316e509c4dSLars-Peter Clausen if (ret) 10326e509c4dSLars-Peter Clausen return ret; 1033a980e046SJonathan Cameron 1034fcc1b2f5SLars-Peter Clausen if (insert_buffer) { 1035fcc1b2f5SLars-Peter Clausen ret = iio_buffer_request_update(indio_dev, insert_buffer); 1036fcc1b2f5SLars-Peter Clausen if (ret) 10376e509c4dSLars-Peter Clausen goto err_free_config; 1038fcc1b2f5SLars-Peter Clausen } 1039fcc1b2f5SLars-Peter Clausen 1040623d74e3SLars-Peter Clausen ret = iio_disable_buffers(indio_dev); 10411250186aSLars-Peter Clausen if (ret) 10421250186aSLars-Peter Clausen goto err_deactivate_all; 1043623d74e3SLars-Peter Clausen 104484b36ce5SJonathan Cameron if (remove_buffer) 10459e69c935SLars-Peter Clausen iio_buffer_deactivate(remove_buffer); 104684b36ce5SJonathan Cameron if (insert_buffer) 10479e69c935SLars-Peter Clausen iio_buffer_activate(indio_dev, insert_buffer); 104884b36ce5SJonathan Cameron 104984b36ce5SJonathan Cameron /* If no buffers in list, we are done */ 10501250186aSLars-Peter Clausen if (list_empty(&indio_dev->buffer_list)) 105184b36ce5SJonathan Cameron return 0; 1052a980e046SJonathan Cameron 1053623d74e3SLars-Peter Clausen ret = iio_enable_buffers(indio_dev, &new_config); 10541250186aSLars-Peter Clausen if (ret) 10551250186aSLars-Peter Clausen goto err_deactivate_all; 1056623d74e3SLars-Peter Clausen 1057623d74e3SLars-Peter Clausen return 0; 10586e509c4dSLars-Peter Clausen 10591250186aSLars-Peter Clausen err_deactivate_all: 10601250186aSLars-Peter Clausen /* 10611250186aSLars-Peter Clausen * We've already verified that the config is valid earlier. If things go 10621250186aSLars-Peter Clausen * wrong in either enable or disable the most likely reason is an IO 10631250186aSLars-Peter Clausen * error from the device. In this case there is no good recovery 10641250186aSLars-Peter Clausen * strategy. Just make sure to disable everything and leave the device 10651250186aSLars-Peter Clausen * in a sane state. With a bit of luck the device might come back to 10661250186aSLars-Peter Clausen * life again later and userspace can try again. 10671250186aSLars-Peter Clausen */ 10681250186aSLars-Peter Clausen iio_buffer_deactivate_all(indio_dev); 10691250186aSLars-Peter Clausen 10706e509c4dSLars-Peter Clausen err_free_config: 10716e509c4dSLars-Peter Clausen iio_free_scan_mask(indio_dev, new_config.scan_mask); 10726e509c4dSLars-Peter Clausen return ret; 107384b36ce5SJonathan Cameron } 1074a9519456SLars-Peter Clausen 1075a9519456SLars-Peter Clausen int iio_update_buffers(struct iio_dev *indio_dev, 1076a9519456SLars-Peter Clausen struct iio_buffer *insert_buffer, 1077a9519456SLars-Peter Clausen struct iio_buffer *remove_buffer) 1078a9519456SLars-Peter Clausen { 1079a9519456SLars-Peter Clausen int ret; 1080a9519456SLars-Peter Clausen 10813909fab5SLars-Peter Clausen if (insert_buffer == remove_buffer) 10823909fab5SLars-Peter Clausen return 0; 10833909fab5SLars-Peter Clausen 1084a9519456SLars-Peter Clausen mutex_lock(&indio_dev->info_exist_lock); 1085a9519456SLars-Peter Clausen mutex_lock(&indio_dev->mlock); 1086a9519456SLars-Peter Clausen 10873909fab5SLars-Peter Clausen if (insert_buffer && iio_buffer_is_active(insert_buffer)) 10883909fab5SLars-Peter Clausen insert_buffer = NULL; 10893909fab5SLars-Peter Clausen 10903909fab5SLars-Peter Clausen if (remove_buffer && !iio_buffer_is_active(remove_buffer)) 10913909fab5SLars-Peter Clausen remove_buffer = NULL; 10923909fab5SLars-Peter Clausen 10933909fab5SLars-Peter Clausen if (!insert_buffer && !remove_buffer) { 10943909fab5SLars-Peter Clausen ret = 0; 10953909fab5SLars-Peter Clausen goto out_unlock; 10963909fab5SLars-Peter Clausen } 10973909fab5SLars-Peter Clausen 1098a9519456SLars-Peter Clausen if (indio_dev->info == NULL) { 1099a9519456SLars-Peter Clausen ret = -ENODEV; 1100a9519456SLars-Peter Clausen goto out_unlock; 1101a9519456SLars-Peter Clausen } 1102a9519456SLars-Peter Clausen 1103a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer); 1104a9519456SLars-Peter Clausen 1105a9519456SLars-Peter Clausen out_unlock: 1106a9519456SLars-Peter Clausen mutex_unlock(&indio_dev->mlock); 1107a9519456SLars-Peter Clausen mutex_unlock(&indio_dev->info_exist_lock); 1108a9519456SLars-Peter Clausen 1109a9519456SLars-Peter Clausen return ret; 1110a9519456SLars-Peter Clausen } 111184b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_update_buffers); 111284b36ce5SJonathan Cameron 1113623d74e3SLars-Peter Clausen void iio_disable_all_buffers(struct iio_dev *indio_dev) 1114623d74e3SLars-Peter Clausen { 1115623d74e3SLars-Peter Clausen iio_disable_buffers(indio_dev); 11161250186aSLars-Peter Clausen iio_buffer_deactivate_all(indio_dev); 1117623d74e3SLars-Peter Clausen } 1118623d74e3SLars-Peter Clausen 111908e7e0adSLars-Peter Clausen static ssize_t iio_buffer_store_enable(struct device *dev, 112084b36ce5SJonathan Cameron struct device_attribute *attr, 112184b36ce5SJonathan Cameron const char *buf, 112284b36ce5SJonathan Cameron size_t len) 112384b36ce5SJonathan Cameron { 112484b36ce5SJonathan Cameron int ret; 112584b36ce5SJonathan Cameron bool requested_state; 112684b36ce5SJonathan Cameron struct iio_dev *indio_dev = dev_to_iio_dev(dev); 112784b36ce5SJonathan Cameron bool inlist; 112884b36ce5SJonathan Cameron 112984b36ce5SJonathan Cameron ret = strtobool(buf, &requested_state); 113084b36ce5SJonathan Cameron if (ret < 0) 113184b36ce5SJonathan Cameron return ret; 113284b36ce5SJonathan Cameron 113384b36ce5SJonathan Cameron mutex_lock(&indio_dev->mlock); 113484b36ce5SJonathan Cameron 113584b36ce5SJonathan Cameron /* Find out if it is in the list */ 1136705ee2c9SLars-Peter Clausen inlist = iio_buffer_is_active(indio_dev->buffer); 113784b36ce5SJonathan Cameron /* Already in desired state */ 113884b36ce5SJonathan Cameron if (inlist == requested_state) 113984b36ce5SJonathan Cameron goto done; 114084b36ce5SJonathan Cameron 114184b36ce5SJonathan Cameron if (requested_state) 1142a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, 114384b36ce5SJonathan Cameron indio_dev->buffer, NULL); 114484b36ce5SJonathan Cameron else 1145a9519456SLars-Peter Clausen ret = __iio_update_buffers(indio_dev, 114684b36ce5SJonathan Cameron NULL, indio_dev->buffer); 114784b36ce5SJonathan Cameron 114884b36ce5SJonathan Cameron done: 114984b36ce5SJonathan Cameron mutex_unlock(&indio_dev->mlock); 115084b36ce5SJonathan Cameron return (ret < 0) ? ret : len; 115184b36ce5SJonathan Cameron } 115284b36ce5SJonathan Cameron 1153d967cb6bSLars-Peter Clausen static const char * const iio_scan_elements_group_name = "scan_elements"; 1154d967cb6bSLars-Peter Clausen 115537d34556SJosselin Costanzi static ssize_t iio_buffer_show_watermark(struct device *dev, 115637d34556SJosselin Costanzi struct device_attribute *attr, 115737d34556SJosselin Costanzi char *buf) 115837d34556SJosselin Costanzi { 115937d34556SJosselin Costanzi struct iio_dev *indio_dev = dev_to_iio_dev(dev); 116037d34556SJosselin Costanzi struct iio_buffer *buffer = indio_dev->buffer; 116137d34556SJosselin Costanzi 116237d34556SJosselin Costanzi return sprintf(buf, "%u\n", buffer->watermark); 116337d34556SJosselin Costanzi } 116437d34556SJosselin Costanzi 116537d34556SJosselin Costanzi static ssize_t iio_buffer_store_watermark(struct device *dev, 116637d34556SJosselin Costanzi struct device_attribute *attr, 116737d34556SJosselin Costanzi const char *buf, 116837d34556SJosselin Costanzi size_t len) 116937d34556SJosselin Costanzi { 117037d34556SJosselin Costanzi struct iio_dev *indio_dev = dev_to_iio_dev(dev); 117137d34556SJosselin Costanzi struct iio_buffer *buffer = indio_dev->buffer; 117237d34556SJosselin Costanzi unsigned int val; 117337d34556SJosselin Costanzi int ret; 117437d34556SJosselin Costanzi 117537d34556SJosselin Costanzi ret = kstrtouint(buf, 10, &val); 117637d34556SJosselin Costanzi if (ret) 117737d34556SJosselin Costanzi return ret; 117837d34556SJosselin Costanzi if (!val) 117937d34556SJosselin Costanzi return -EINVAL; 118037d34556SJosselin Costanzi 118137d34556SJosselin Costanzi mutex_lock(&indio_dev->mlock); 118237d34556SJosselin Costanzi 118337d34556SJosselin Costanzi if (val > buffer->length) { 118437d34556SJosselin Costanzi ret = -EINVAL; 118537d34556SJosselin Costanzi goto out; 118637d34556SJosselin Costanzi } 118737d34556SJosselin Costanzi 118837d34556SJosselin Costanzi if (iio_buffer_is_active(indio_dev->buffer)) { 118937d34556SJosselin Costanzi ret = -EBUSY; 119037d34556SJosselin Costanzi goto out; 119137d34556SJosselin Costanzi } 119237d34556SJosselin Costanzi 119337d34556SJosselin Costanzi buffer->watermark = val; 119437d34556SJosselin Costanzi out: 119537d34556SJosselin Costanzi mutex_unlock(&indio_dev->mlock); 119637d34556SJosselin Costanzi 119737d34556SJosselin Costanzi return ret ? ret : len; 119837d34556SJosselin Costanzi } 119937d34556SJosselin Costanzi 120008e7e0adSLars-Peter Clausen static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length, 120108e7e0adSLars-Peter Clausen iio_buffer_write_length); 12028d92db28SLars-Peter Clausen static struct device_attribute dev_attr_length_ro = __ATTR(length, 12038d92db28SLars-Peter Clausen S_IRUGO, iio_buffer_read_length, NULL); 120408e7e0adSLars-Peter Clausen static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, 120508e7e0adSLars-Peter Clausen iio_buffer_show_enable, iio_buffer_store_enable); 120637d34556SJosselin Costanzi static DEVICE_ATTR(watermark, S_IRUGO | S_IWUSR, 120737d34556SJosselin Costanzi iio_buffer_show_watermark, iio_buffer_store_watermark); 1208b440655bSLars-Peter Clausen static struct device_attribute dev_attr_watermark_ro = __ATTR(watermark, 1209b440655bSLars-Peter Clausen S_IRUGO, iio_buffer_show_watermark, NULL); 121008e7e0adSLars-Peter Clausen 12116da9b382SOctavian Purdila static struct attribute *iio_buffer_attrs[] = { 12126da9b382SOctavian Purdila &dev_attr_length.attr, 12136da9b382SOctavian Purdila &dev_attr_enable.attr, 121437d34556SJosselin Costanzi &dev_attr_watermark.attr, 12156da9b382SOctavian Purdila }; 12166da9b382SOctavian Purdila 1217d967cb6bSLars-Peter Clausen int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev) 1218d967cb6bSLars-Peter Clausen { 1219d967cb6bSLars-Peter Clausen struct iio_dev_attr *p; 1220d967cb6bSLars-Peter Clausen struct attribute **attr; 1221d967cb6bSLars-Peter Clausen struct iio_buffer *buffer = indio_dev->buffer; 1222d967cb6bSLars-Peter Clausen int ret, i, attrn, attrcount, attrcount_orig = 0; 1223d967cb6bSLars-Peter Clausen const struct iio_chan_spec *channels; 1224d967cb6bSLars-Peter Clausen 1225629bc023SLars-Peter Clausen channels = indio_dev->channels; 1226629bc023SLars-Peter Clausen if (channels) { 1227629bc023SLars-Peter Clausen int ml = indio_dev->masklength; 1228629bc023SLars-Peter Clausen 1229629bc023SLars-Peter Clausen for (i = 0; i < indio_dev->num_channels; i++) 1230629bc023SLars-Peter Clausen ml = max(ml, channels[i].scan_index + 1); 1231629bc023SLars-Peter Clausen indio_dev->masklength = ml; 1232629bc023SLars-Peter Clausen } 1233629bc023SLars-Peter Clausen 1234d967cb6bSLars-Peter Clausen if (!buffer) 1235d967cb6bSLars-Peter Clausen return 0; 1236d967cb6bSLars-Peter Clausen 123708e7e0adSLars-Peter Clausen attrcount = 0; 123808e7e0adSLars-Peter Clausen if (buffer->attrs) { 123908e7e0adSLars-Peter Clausen while (buffer->attrs[attrcount] != NULL) 124008e7e0adSLars-Peter Clausen attrcount++; 124108e7e0adSLars-Peter Clausen } 124208e7e0adSLars-Peter Clausen 12436da9b382SOctavian Purdila attr = kcalloc(attrcount + ARRAY_SIZE(iio_buffer_attrs) + 1, 12446da9b382SOctavian Purdila sizeof(struct attribute *), GFP_KERNEL); 12456da9b382SOctavian Purdila if (!attr) 124608e7e0adSLars-Peter Clausen return -ENOMEM; 124708e7e0adSLars-Peter Clausen 12486da9b382SOctavian Purdila memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs)); 12496da9b382SOctavian Purdila if (!buffer->access->set_length) 12506da9b382SOctavian Purdila attr[0] = &dev_attr_length_ro.attr; 12516da9b382SOctavian Purdila 1252b440655bSLars-Peter Clausen if (buffer->access->flags & INDIO_BUFFER_FLAG_FIXED_WATERMARK) 1253b440655bSLars-Peter Clausen attr[2] = &dev_attr_watermark_ro.attr; 1254b440655bSLars-Peter Clausen 125508e7e0adSLars-Peter Clausen if (buffer->attrs) 12566da9b382SOctavian Purdila memcpy(&attr[ARRAY_SIZE(iio_buffer_attrs)], buffer->attrs, 12576da9b382SOctavian Purdila sizeof(struct attribute *) * attrcount); 12586da9b382SOctavian Purdila 12596da9b382SOctavian Purdila attr[attrcount + ARRAY_SIZE(iio_buffer_attrs)] = NULL; 12606da9b382SOctavian Purdila 12616da9b382SOctavian Purdila buffer->buffer_group.name = "buffer"; 12626da9b382SOctavian Purdila buffer->buffer_group.attrs = attr; 126308e7e0adSLars-Peter Clausen 126408e7e0adSLars-Peter Clausen indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group; 126508e7e0adSLars-Peter Clausen 1266d967cb6bSLars-Peter Clausen if (buffer->scan_el_attrs != NULL) { 1267d967cb6bSLars-Peter Clausen attr = buffer->scan_el_attrs->attrs; 1268d967cb6bSLars-Peter Clausen while (*attr++ != NULL) 1269d967cb6bSLars-Peter Clausen attrcount_orig++; 1270d967cb6bSLars-Peter Clausen } 1271d967cb6bSLars-Peter Clausen attrcount = attrcount_orig; 1272d967cb6bSLars-Peter Clausen INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list); 1273d967cb6bSLars-Peter Clausen channels = indio_dev->channels; 1274d967cb6bSLars-Peter Clausen if (channels) { 1275d967cb6bSLars-Peter Clausen /* new magic */ 1276d967cb6bSLars-Peter Clausen for (i = 0; i < indio_dev->num_channels; i++) { 1277d967cb6bSLars-Peter Clausen if (channels[i].scan_index < 0) 1278d967cb6bSLars-Peter Clausen continue; 1279d967cb6bSLars-Peter Clausen 1280d967cb6bSLars-Peter Clausen ret = iio_buffer_add_channel_sysfs(indio_dev, 1281d967cb6bSLars-Peter Clausen &channels[i]); 1282d967cb6bSLars-Peter Clausen if (ret < 0) 1283d967cb6bSLars-Peter Clausen goto error_cleanup_dynamic; 1284d967cb6bSLars-Peter Clausen attrcount += ret; 1285d967cb6bSLars-Peter Clausen if (channels[i].type == IIO_TIMESTAMP) 1286d967cb6bSLars-Peter Clausen indio_dev->scan_index_timestamp = 1287d967cb6bSLars-Peter Clausen channels[i].scan_index; 1288d967cb6bSLars-Peter Clausen } 1289d967cb6bSLars-Peter Clausen if (indio_dev->masklength && buffer->scan_mask == NULL) { 1290d967cb6bSLars-Peter Clausen buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), 1291d967cb6bSLars-Peter Clausen sizeof(*buffer->scan_mask), 1292d967cb6bSLars-Peter Clausen GFP_KERNEL); 1293d967cb6bSLars-Peter Clausen if (buffer->scan_mask == NULL) { 1294d967cb6bSLars-Peter Clausen ret = -ENOMEM; 1295d967cb6bSLars-Peter Clausen goto error_cleanup_dynamic; 1296d967cb6bSLars-Peter Clausen } 1297d967cb6bSLars-Peter Clausen } 1298d967cb6bSLars-Peter Clausen } 1299d967cb6bSLars-Peter Clausen 1300d967cb6bSLars-Peter Clausen buffer->scan_el_group.name = iio_scan_elements_group_name; 1301d967cb6bSLars-Peter Clausen 1302d967cb6bSLars-Peter Clausen buffer->scan_el_group.attrs = kcalloc(attrcount + 1, 1303d967cb6bSLars-Peter Clausen sizeof(buffer->scan_el_group.attrs[0]), 1304d967cb6bSLars-Peter Clausen GFP_KERNEL); 1305d967cb6bSLars-Peter Clausen if (buffer->scan_el_group.attrs == NULL) { 1306d967cb6bSLars-Peter Clausen ret = -ENOMEM; 1307d967cb6bSLars-Peter Clausen goto error_free_scan_mask; 1308d967cb6bSLars-Peter Clausen } 1309d967cb6bSLars-Peter Clausen if (buffer->scan_el_attrs) 1310d967cb6bSLars-Peter Clausen memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs, 1311d967cb6bSLars-Peter Clausen sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig); 1312d967cb6bSLars-Peter Clausen attrn = attrcount_orig; 1313d967cb6bSLars-Peter Clausen 1314d967cb6bSLars-Peter Clausen list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l) 1315d967cb6bSLars-Peter Clausen buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr; 1316d967cb6bSLars-Peter Clausen indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group; 1317d967cb6bSLars-Peter Clausen 1318d967cb6bSLars-Peter Clausen return 0; 1319d967cb6bSLars-Peter Clausen 1320d967cb6bSLars-Peter Clausen error_free_scan_mask: 1321d967cb6bSLars-Peter Clausen kfree(buffer->scan_mask); 1322d967cb6bSLars-Peter Clausen error_cleanup_dynamic: 1323d967cb6bSLars-Peter Clausen iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list); 132408e7e0adSLars-Peter Clausen kfree(indio_dev->buffer->buffer_group.attrs); 1325d967cb6bSLars-Peter Clausen 1326d967cb6bSLars-Peter Clausen return ret; 1327d967cb6bSLars-Peter Clausen } 1328d967cb6bSLars-Peter Clausen 1329d967cb6bSLars-Peter Clausen void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev) 1330d967cb6bSLars-Peter Clausen { 1331d967cb6bSLars-Peter Clausen if (!indio_dev->buffer) 1332d967cb6bSLars-Peter Clausen return; 1333d967cb6bSLars-Peter Clausen 1334d967cb6bSLars-Peter Clausen kfree(indio_dev->buffer->scan_mask); 133508e7e0adSLars-Peter Clausen kfree(indio_dev->buffer->buffer_group.attrs); 1336d967cb6bSLars-Peter Clausen kfree(indio_dev->buffer->scan_el_group.attrs); 1337d967cb6bSLars-Peter Clausen iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list); 1338d967cb6bSLars-Peter Clausen } 1339d967cb6bSLars-Peter Clausen 1340a980e046SJonathan Cameron /** 134181636632SLars-Peter Clausen * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected 134281636632SLars-Peter Clausen * @indio_dev: the iio device 134381636632SLars-Peter Clausen * @mask: scan mask to be checked 134481636632SLars-Peter Clausen * 134581636632SLars-Peter Clausen * Return true if exactly one bit is set in the scan mask, false otherwise. It 134681636632SLars-Peter Clausen * can be used for devices where only one channel can be active for sampling at 134781636632SLars-Peter Clausen * a time. 134881636632SLars-Peter Clausen */ 134981636632SLars-Peter Clausen bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev, 135081636632SLars-Peter Clausen const unsigned long *mask) 135181636632SLars-Peter Clausen { 135281636632SLars-Peter Clausen return bitmap_weight(mask, indio_dev->masklength) == 1; 135381636632SLars-Peter Clausen } 135481636632SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot); 135581636632SLars-Peter Clausen 13565d65d920SLars-Peter Clausen static const void *iio_demux(struct iio_buffer *buffer, 13575d65d920SLars-Peter Clausen const void *datain) 1358a980e046SJonathan Cameron { 1359a980e046SJonathan Cameron struct iio_demux_table *t; 1360a980e046SJonathan Cameron 1361a980e046SJonathan Cameron if (list_empty(&buffer->demux_list)) 1362a980e046SJonathan Cameron return datain; 1363a980e046SJonathan Cameron list_for_each_entry(t, &buffer->demux_list, l) 1364a980e046SJonathan Cameron memcpy(buffer->demux_bounce + t->to, 1365a980e046SJonathan Cameron datain + t->from, t->length); 1366a980e046SJonathan Cameron 1367a980e046SJonathan Cameron return buffer->demux_bounce; 1368a980e046SJonathan Cameron } 1369a980e046SJonathan Cameron 13705d65d920SLars-Peter Clausen static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data) 1371a980e046SJonathan Cameron { 13725d65d920SLars-Peter Clausen const void *dataout = iio_demux(buffer, data); 137337d34556SJosselin Costanzi int ret; 1374a980e046SJonathan Cameron 137537d34556SJosselin Costanzi ret = buffer->access->store_to(buffer, dataout); 137637d34556SJosselin Costanzi if (ret) 137737d34556SJosselin Costanzi return ret; 137837d34556SJosselin Costanzi 137937d34556SJosselin Costanzi /* 138037d34556SJosselin Costanzi * We can't just test for watermark to decide if we wake the poll queue 138137d34556SJosselin Costanzi * because read may request less samples than the watermark. 138237d34556SJosselin Costanzi */ 138337d34556SJosselin Costanzi wake_up_interruptible_poll(&buffer->pollq, POLLIN | POLLRDNORM); 138437d34556SJosselin Costanzi return 0; 1385a980e046SJonathan Cameron } 1386a980e046SJonathan Cameron 13875d65d920SLars-Peter Clausen int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) 138884b36ce5SJonathan Cameron { 138984b36ce5SJonathan Cameron int ret; 139084b36ce5SJonathan Cameron struct iio_buffer *buf; 139184b36ce5SJonathan Cameron 139284b36ce5SJonathan Cameron list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) { 139384b36ce5SJonathan Cameron ret = iio_push_to_buffer(buf, data); 139484b36ce5SJonathan Cameron if (ret < 0) 139584b36ce5SJonathan Cameron return ret; 139684b36ce5SJonathan Cameron } 139784b36ce5SJonathan Cameron 139884b36ce5SJonathan Cameron return 0; 139984b36ce5SJonathan Cameron } 140084b36ce5SJonathan Cameron EXPORT_SYMBOL_GPL(iio_push_to_buffers); 140184b36ce5SJonathan Cameron 14029e69c935SLars-Peter Clausen /** 14039e69c935SLars-Peter Clausen * iio_buffer_release() - Free a buffer's resources 14049e69c935SLars-Peter Clausen * @ref: Pointer to the kref embedded in the iio_buffer struct 14059e69c935SLars-Peter Clausen * 14069e69c935SLars-Peter Clausen * This function is called when the last reference to the buffer has been 14079e69c935SLars-Peter Clausen * dropped. It will typically free all resources allocated by the buffer. Do not 14089e69c935SLars-Peter Clausen * call this function manually, always use iio_buffer_put() when done using a 14099e69c935SLars-Peter Clausen * buffer. 14109e69c935SLars-Peter Clausen */ 14119e69c935SLars-Peter Clausen static void iio_buffer_release(struct kref *ref) 14129e69c935SLars-Peter Clausen { 14139e69c935SLars-Peter Clausen struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref); 14149e69c935SLars-Peter Clausen 14159e69c935SLars-Peter Clausen buffer->access->release(buffer); 14169e69c935SLars-Peter Clausen } 14179e69c935SLars-Peter Clausen 14189e69c935SLars-Peter Clausen /** 14199e69c935SLars-Peter Clausen * iio_buffer_get() - Grab a reference to the buffer 14209e69c935SLars-Peter Clausen * @buffer: The buffer to grab a reference for, may be NULL 14219e69c935SLars-Peter Clausen * 14229e69c935SLars-Peter Clausen * Returns the pointer to the buffer that was passed into the function. 14239e69c935SLars-Peter Clausen */ 14249e69c935SLars-Peter Clausen struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer) 14259e69c935SLars-Peter Clausen { 14269e69c935SLars-Peter Clausen if (buffer) 14279e69c935SLars-Peter Clausen kref_get(&buffer->ref); 14289e69c935SLars-Peter Clausen 14299e69c935SLars-Peter Clausen return buffer; 14309e69c935SLars-Peter Clausen } 14319e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_get); 14329e69c935SLars-Peter Clausen 14339e69c935SLars-Peter Clausen /** 14349e69c935SLars-Peter Clausen * iio_buffer_put() - Release the reference to the buffer 14359e69c935SLars-Peter Clausen * @buffer: The buffer to release the reference for, may be NULL 14369e69c935SLars-Peter Clausen */ 14379e69c935SLars-Peter Clausen void iio_buffer_put(struct iio_buffer *buffer) 14389e69c935SLars-Peter Clausen { 14399e69c935SLars-Peter Clausen if (buffer) 14409e69c935SLars-Peter Clausen kref_put(&buffer->ref, iio_buffer_release); 14419e69c935SLars-Peter Clausen } 14429e69c935SLars-Peter Clausen EXPORT_SYMBOL_GPL(iio_buffer_put); 1443