Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 1 | /* The industrial I/O core |
| 2 | * |
| 3 | * Copyright (c) 2008 Jonathan Cameron |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | * |
| 9 | * Handling of buffer allocation / resizing. |
| 10 | * |
| 11 | * |
| 12 | * Things to look at here. |
| 13 | * - Better memory allocation techniques? |
| 14 | * - Alternative access techniques? |
| 15 | */ |
| 16 | #include <linux/kernel.h> |
Paul Gortmaker | 8e336a7 | 2011-07-10 13:09:12 -0400 | [diff] [blame] | 17 | #include <linux/export.h> |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 18 | #include <linux/device.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/cdev.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/poll.h> |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 23 | #include <linux/sched.h> |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 24 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 25 | #include <linux/iio/iio.h> |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 26 | #include "iio_core.h" |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 27 | #include <linux/iio/sysfs.h> |
| 28 | #include <linux/iio/buffer.h> |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 29 | |
| 30 | static const char * const iio_endian_prefix[] = { |
| 31 | [IIO_BE] = "be", |
| 32 | [IIO_LE] = "le", |
| 33 | }; |
| 34 | |
Lars-Peter Clausen | 705ee2c | 2013-09-15 16:31:00 +0100 | [diff] [blame] | 35 | static bool iio_buffer_is_active(struct iio_buffer *buf) |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 36 | { |
Lars-Peter Clausen | 705ee2c | 2013-09-15 16:31:00 +0100 | [diff] [blame] | 37 | return !list_empty(&buf->buffer_list); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Lars-Peter Clausen | 647cc7b | 2013-11-25 14:56:00 +0000 | [diff] [blame] | 40 | static bool iio_buffer_data_available(struct iio_buffer *buf) |
| 41 | { |
Josselin Costanzi | 9dd4694 | 2014-06-27 17:20:00 +0100 | [diff] [blame] | 42 | return buf->access->data_available(buf); |
Lars-Peter Clausen | 647cc7b | 2013-11-25 14:56:00 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 45 | /** |
| 46 | * iio_buffer_read_first_n_outer() - chrdev read for buffer access |
| 47 | * |
| 48 | * This function relies on all buffer implementations having an |
| 49 | * iio_buffer as their first element. |
| 50 | **/ |
| 51 | ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf, |
| 52 | size_t n, loff_t *f_ps) |
| 53 | { |
| 54 | struct iio_dev *indio_dev = filp->private_data; |
| 55 | struct iio_buffer *rb = indio_dev->buffer; |
Lars-Peter Clausen | ee551a1 | 2013-11-25 14:56:00 +0000 | [diff] [blame] | 56 | int ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 57 | |
Lars-Peter Clausen | f18e7a0 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 58 | if (!indio_dev->info) |
| 59 | return -ENODEV; |
| 60 | |
Jonathan Cameron | 96e00f1 | 2011-10-26 17:27:45 +0100 | [diff] [blame] | 61 | if (!rb || !rb->access->read_first_n) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 62 | return -EINVAL; |
Lars-Peter Clausen | ee551a1 | 2013-11-25 14:56:00 +0000 | [diff] [blame] | 63 | |
| 64 | do { |
| 65 | if (!iio_buffer_data_available(rb)) { |
| 66 | if (filp->f_flags & O_NONBLOCK) |
| 67 | return -EAGAIN; |
| 68 | |
| 69 | ret = wait_event_interruptible(rb->pollq, |
| 70 | iio_buffer_data_available(rb) || |
| 71 | indio_dev->info == NULL); |
| 72 | if (ret) |
| 73 | return ret; |
| 74 | if (indio_dev->info == NULL) |
| 75 | return -ENODEV; |
| 76 | } |
| 77 | |
| 78 | ret = rb->access->read_first_n(rb, n, buf); |
| 79 | if (ret == 0 && (filp->f_flags & O_NONBLOCK)) |
| 80 | ret = -EAGAIN; |
| 81 | } while (ret == 0); |
| 82 | |
| 83 | return ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * iio_buffer_poll() - poll the buffer to find out if it has data |
| 88 | */ |
| 89 | unsigned int iio_buffer_poll(struct file *filp, |
| 90 | struct poll_table_struct *wait) |
| 91 | { |
| 92 | struct iio_dev *indio_dev = filp->private_data; |
| 93 | struct iio_buffer *rb = indio_dev->buffer; |
| 94 | |
Lars-Peter Clausen | f18e7a0 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 95 | if (!indio_dev->info) |
| 96 | return -ENODEV; |
| 97 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 98 | poll_wait(filp, &rb->pollq, wait); |
Lars-Peter Clausen | 647cc7b | 2013-11-25 14:56:00 +0000 | [diff] [blame] | 99 | if (iio_buffer_data_available(rb)) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 100 | return POLLIN | POLLRDNORM; |
| 101 | /* need a way of knowing if there may be enough data... */ |
| 102 | return 0; |
| 103 | } |
| 104 | |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 105 | /** |
| 106 | * iio_buffer_wakeup_poll - Wakes up the buffer waitqueue |
| 107 | * @indio_dev: The IIO device |
| 108 | * |
| 109 | * Wakes up the event waitqueue used for poll(). Should usually |
| 110 | * be called when the device is unregistered. |
| 111 | */ |
| 112 | void iio_buffer_wakeup_poll(struct iio_dev *indio_dev) |
| 113 | { |
| 114 | if (!indio_dev->buffer) |
| 115 | return; |
| 116 | |
| 117 | wake_up(&indio_dev->buffer->pollq); |
| 118 | } |
| 119 | |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame] | 120 | void iio_buffer_init(struct iio_buffer *buffer) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 121 | { |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 122 | INIT_LIST_HEAD(&buffer->demux_list); |
Lars-Peter Clausen | 705ee2c | 2013-09-15 16:31:00 +0100 | [diff] [blame] | 123 | INIT_LIST_HEAD(&buffer->buffer_list); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 124 | init_waitqueue_head(&buffer->pollq); |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 125 | kref_init(&buffer->ref); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 126 | } |
| 127 | EXPORT_SYMBOL(iio_buffer_init); |
| 128 | |
| 129 | static ssize_t iio_show_scan_index(struct device *dev, |
| 130 | struct device_attribute *attr, |
| 131 | char *buf) |
| 132 | { |
| 133 | return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index); |
| 134 | } |
| 135 | |
| 136 | static ssize_t iio_show_fixed_type(struct device *dev, |
| 137 | struct device_attribute *attr, |
| 138 | char *buf) |
| 139 | { |
| 140 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 141 | u8 type = this_attr->c->scan_type.endianness; |
| 142 | |
| 143 | if (type == IIO_CPU) { |
Jonathan Cameron | 9d5d115 | 2011-10-04 16:02:08 +0100 | [diff] [blame] | 144 | #ifdef __LITTLE_ENDIAN |
| 145 | type = IIO_LE; |
| 146 | #else |
| 147 | type = IIO_BE; |
| 148 | #endif |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 149 | } |
Srinivas Pandruvada | 0ee8546 | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 150 | if (this_attr->c->scan_type.repeat > 1) |
| 151 | return sprintf(buf, "%s:%c%d/%dX%d>>%u\n", |
| 152 | iio_endian_prefix[type], |
| 153 | this_attr->c->scan_type.sign, |
| 154 | this_attr->c->scan_type.realbits, |
| 155 | this_attr->c->scan_type.storagebits, |
| 156 | this_attr->c->scan_type.repeat, |
| 157 | this_attr->c->scan_type.shift); |
| 158 | else |
| 159 | return sprintf(buf, "%s:%c%d/%d>>%u\n", |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 160 | iio_endian_prefix[type], |
| 161 | this_attr->c->scan_type.sign, |
| 162 | this_attr->c->scan_type.realbits, |
| 163 | this_attr->c->scan_type.storagebits, |
| 164 | this_attr->c->scan_type.shift); |
| 165 | } |
| 166 | |
| 167 | static ssize_t iio_scan_el_show(struct device *dev, |
| 168 | struct device_attribute *attr, |
| 169 | char *buf) |
| 170 | { |
| 171 | int ret; |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 172 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 173 | |
Alec Berg | 2076a20 | 2014-03-19 18:50:00 +0000 | [diff] [blame] | 174 | /* Ensure ret is 0 or 1. */ |
| 175 | ret = !!test_bit(to_iio_dev_attr(attr)->address, |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 176 | indio_dev->buffer->scan_mask); |
| 177 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 178 | return sprintf(buf, "%d\n", ret); |
| 179 | } |
| 180 | |
Lars-Peter Clausen | 217a5cf | 2014-11-26 18:55:09 +0100 | [diff] [blame^] | 181 | /* Note NULL used as error indicator as it doesn't make sense. */ |
| 182 | static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks, |
| 183 | unsigned int masklength, |
| 184 | const unsigned long *mask) |
| 185 | { |
| 186 | if (bitmap_empty(mask, masklength)) |
| 187 | return NULL; |
| 188 | while (*av_masks) { |
| 189 | if (bitmap_subset(mask, av_masks, masklength)) |
| 190 | return av_masks; |
| 191 | av_masks += BITS_TO_LONGS(masklength); |
| 192 | } |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | static bool iio_validate_scan_mask(struct iio_dev *indio_dev, |
| 197 | const unsigned long *mask) |
| 198 | { |
| 199 | if (!indio_dev->setup_ops->validate_scan_mask) |
| 200 | return true; |
| 201 | |
| 202 | return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * iio_scan_mask_set() - set particular bit in the scan mask |
| 207 | * @indio_dev: the iio device |
| 208 | * @buffer: the buffer whose scan mask we are interested in |
| 209 | * @bit: the bit to be set. |
| 210 | * |
| 211 | * Note that at this point we have no way of knowing what other |
| 212 | * buffers might request, hence this code only verifies that the |
| 213 | * individual buffers request is plausible. |
| 214 | */ |
| 215 | static int iio_scan_mask_set(struct iio_dev *indio_dev, |
| 216 | struct iio_buffer *buffer, int bit) |
| 217 | { |
| 218 | const unsigned long *mask; |
| 219 | unsigned long *trialmask; |
| 220 | |
| 221 | trialmask = kmalloc(sizeof(*trialmask)* |
| 222 | BITS_TO_LONGS(indio_dev->masklength), |
| 223 | GFP_KERNEL); |
| 224 | |
| 225 | if (trialmask == NULL) |
| 226 | return -ENOMEM; |
| 227 | if (!indio_dev->masklength) { |
| 228 | WARN_ON("Trying to set scanmask prior to registering buffer\n"); |
| 229 | goto err_invalid_mask; |
| 230 | } |
| 231 | bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength); |
| 232 | set_bit(bit, trialmask); |
| 233 | |
| 234 | if (!iio_validate_scan_mask(indio_dev, trialmask)) |
| 235 | goto err_invalid_mask; |
| 236 | |
| 237 | if (indio_dev->available_scan_masks) { |
| 238 | mask = iio_scan_mask_match(indio_dev->available_scan_masks, |
| 239 | indio_dev->masklength, |
| 240 | trialmask); |
| 241 | if (!mask) |
| 242 | goto err_invalid_mask; |
| 243 | } |
| 244 | bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength); |
| 245 | |
| 246 | kfree(trialmask); |
| 247 | |
| 248 | return 0; |
| 249 | |
| 250 | err_invalid_mask: |
| 251 | kfree(trialmask); |
| 252 | return -EINVAL; |
| 253 | } |
| 254 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 255 | static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit) |
| 256 | { |
| 257 | clear_bit(bit, buffer->scan_mask); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static ssize_t iio_scan_el_store(struct device *dev, |
| 262 | struct device_attribute *attr, |
| 263 | const char *buf, |
| 264 | size_t len) |
| 265 | { |
Jonathan Cameron | a714af2 | 2012-04-21 10:09:32 +0100 | [diff] [blame] | 266 | int ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 267 | bool state; |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 268 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 269 | struct iio_buffer *buffer = indio_dev->buffer; |
| 270 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 271 | |
Jonathan Cameron | a714af2 | 2012-04-21 10:09:32 +0100 | [diff] [blame] | 272 | ret = strtobool(buf, &state); |
| 273 | if (ret < 0) |
| 274 | return ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 275 | mutex_lock(&indio_dev->mlock); |
Lars-Peter Clausen | 705ee2c | 2013-09-15 16:31:00 +0100 | [diff] [blame] | 276 | if (iio_buffer_is_active(indio_dev->buffer)) { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 277 | ret = -EBUSY; |
| 278 | goto error_ret; |
| 279 | } |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame] | 280 | ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 281 | if (ret < 0) |
| 282 | goto error_ret; |
| 283 | if (!state && ret) { |
| 284 | ret = iio_scan_mask_clear(buffer, this_attr->address); |
| 285 | if (ret) |
| 286 | goto error_ret; |
| 287 | } else if (state && !ret) { |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame] | 288 | ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 289 | if (ret) |
| 290 | goto error_ret; |
| 291 | } |
| 292 | |
| 293 | error_ret: |
| 294 | mutex_unlock(&indio_dev->mlock); |
| 295 | |
Lars-Peter Clausen | 5a2a6e1 | 2011-12-08 18:35:53 +0100 | [diff] [blame] | 296 | return ret < 0 ? ret : len; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 297 | |
| 298 | } |
| 299 | |
| 300 | static ssize_t iio_scan_el_ts_show(struct device *dev, |
| 301 | struct device_attribute *attr, |
| 302 | char *buf) |
| 303 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 304 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 305 | return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | static ssize_t iio_scan_el_ts_store(struct device *dev, |
| 309 | struct device_attribute *attr, |
| 310 | const char *buf, |
| 311 | size_t len) |
| 312 | { |
Jonathan Cameron | a714af2 | 2012-04-21 10:09:32 +0100 | [diff] [blame] | 313 | int ret; |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 314 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 315 | bool state; |
| 316 | |
Jonathan Cameron | a714af2 | 2012-04-21 10:09:32 +0100 | [diff] [blame] | 317 | ret = strtobool(buf, &state); |
| 318 | if (ret < 0) |
| 319 | return ret; |
| 320 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 321 | mutex_lock(&indio_dev->mlock); |
Lars-Peter Clausen | 705ee2c | 2013-09-15 16:31:00 +0100 | [diff] [blame] | 322 | if (iio_buffer_is_active(indio_dev->buffer)) { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 323 | ret = -EBUSY; |
| 324 | goto error_ret; |
| 325 | } |
| 326 | indio_dev->buffer->scan_timestamp = state; |
| 327 | error_ret: |
| 328 | mutex_unlock(&indio_dev->mlock); |
| 329 | |
| 330 | return ret ? ret : len; |
| 331 | } |
| 332 | |
| 333 | static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev, |
| 334 | const struct iio_chan_spec *chan) |
| 335 | { |
| 336 | int ret, attrcount = 0; |
| 337 | struct iio_buffer *buffer = indio_dev->buffer; |
| 338 | |
| 339 | ret = __iio_add_chan_devattr("index", |
| 340 | chan, |
| 341 | &iio_show_scan_index, |
| 342 | NULL, |
| 343 | 0, |
Jonathan Cameron | 3704432 | 2013-09-08 14:57:00 +0100 | [diff] [blame] | 344 | IIO_SEPARATE, |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 345 | &indio_dev->dev, |
| 346 | &buffer->scan_el_dev_attr_list); |
| 347 | if (ret) |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 348 | return ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 349 | attrcount++; |
| 350 | ret = __iio_add_chan_devattr("type", |
| 351 | chan, |
| 352 | &iio_show_fixed_type, |
| 353 | NULL, |
| 354 | 0, |
| 355 | 0, |
| 356 | &indio_dev->dev, |
| 357 | &buffer->scan_el_dev_attr_list); |
| 358 | if (ret) |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 359 | return ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 360 | attrcount++; |
| 361 | if (chan->type != IIO_TIMESTAMP) |
| 362 | ret = __iio_add_chan_devattr("en", |
| 363 | chan, |
| 364 | &iio_scan_el_show, |
| 365 | &iio_scan_el_store, |
| 366 | chan->scan_index, |
| 367 | 0, |
| 368 | &indio_dev->dev, |
| 369 | &buffer->scan_el_dev_attr_list); |
| 370 | else |
| 371 | ret = __iio_add_chan_devattr("en", |
| 372 | chan, |
| 373 | &iio_scan_el_ts_show, |
| 374 | &iio_scan_el_ts_store, |
| 375 | chan->scan_index, |
| 376 | 0, |
| 377 | &indio_dev->dev, |
| 378 | &buffer->scan_el_dev_attr_list); |
Peter Meerwald | 9572588 | 2013-09-17 23:42:00 +0100 | [diff] [blame] | 379 | if (ret) |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 380 | return ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 381 | attrcount++; |
| 382 | ret = attrcount; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 383 | return ret; |
| 384 | } |
| 385 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 386 | static const char * const iio_scan_elements_group_name = "scan_elements"; |
| 387 | |
| 388 | int iio_buffer_register(struct iio_dev *indio_dev, |
| 389 | const struct iio_chan_spec *channels, |
| 390 | int num_channels) |
| 391 | { |
| 392 | struct iio_dev_attr *p; |
| 393 | struct attribute **attr; |
| 394 | struct iio_buffer *buffer = indio_dev->buffer; |
| 395 | int ret, i, attrn, attrcount, attrcount_orig = 0; |
| 396 | |
| 397 | if (buffer->attrs) |
| 398 | indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs; |
| 399 | |
| 400 | if (buffer->scan_el_attrs != NULL) { |
| 401 | attr = buffer->scan_el_attrs->attrs; |
| 402 | while (*attr++ != NULL) |
| 403 | attrcount_orig++; |
| 404 | } |
| 405 | attrcount = attrcount_orig; |
| 406 | INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list); |
| 407 | if (channels) { |
| 408 | /* new magic */ |
| 409 | for (i = 0; i < num_channels; i++) { |
Lars-Peter Clausen | f5b81dd | 2012-06-18 18:33:47 +0200 | [diff] [blame] | 410 | if (channels[i].scan_index < 0) |
| 411 | continue; |
| 412 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 413 | /* Establish necessary mask length */ |
| 414 | if (channels[i].scan_index > |
| 415 | (int)indio_dev->masklength - 1) |
| 416 | indio_dev->masklength |
Lars-Peter Clausen | e1dc7be | 2012-07-02 14:52:56 +0200 | [diff] [blame] | 417 | = channels[i].scan_index + 1; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 418 | |
| 419 | ret = iio_buffer_add_channel_sysfs(indio_dev, |
| 420 | &channels[i]); |
| 421 | if (ret < 0) |
| 422 | goto error_cleanup_dynamic; |
| 423 | attrcount += ret; |
Jonathan Cameron | beb8060 | 2011-12-05 21:37:11 +0000 | [diff] [blame] | 424 | if (channels[i].type == IIO_TIMESTAMP) |
Jonathan Cameron | f126480 | 2012-04-21 10:09:34 +0100 | [diff] [blame] | 425 | indio_dev->scan_index_timestamp = |
Jonathan Cameron | beb8060 | 2011-12-05 21:37:11 +0000 | [diff] [blame] | 426 | channels[i].scan_index; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 427 | } |
| 428 | if (indio_dev->masklength && buffer->scan_mask == NULL) { |
Thomas Meyer | d83fb18 | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 429 | buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), |
| 430 | sizeof(*buffer->scan_mask), |
| 431 | GFP_KERNEL); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 432 | if (buffer->scan_mask == NULL) { |
| 433 | ret = -ENOMEM; |
| 434 | goto error_cleanup_dynamic; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | buffer->scan_el_group.name = iio_scan_elements_group_name; |
| 440 | |
Thomas Meyer | d83fb18 | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 441 | buffer->scan_el_group.attrs = kcalloc(attrcount + 1, |
| 442 | sizeof(buffer->scan_el_group.attrs[0]), |
| 443 | GFP_KERNEL); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 444 | if (buffer->scan_el_group.attrs == NULL) { |
| 445 | ret = -ENOMEM; |
| 446 | goto error_free_scan_mask; |
| 447 | } |
| 448 | if (buffer->scan_el_attrs) |
| 449 | memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs, |
| 450 | sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig); |
| 451 | attrn = attrcount_orig; |
| 452 | |
| 453 | list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l) |
| 454 | buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr; |
| 455 | indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group; |
| 456 | |
| 457 | return 0; |
| 458 | |
| 459 | error_free_scan_mask: |
| 460 | kfree(buffer->scan_mask); |
| 461 | error_cleanup_dynamic: |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 462 | iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 463 | |
| 464 | return ret; |
| 465 | } |
| 466 | EXPORT_SYMBOL(iio_buffer_register); |
| 467 | |
| 468 | void iio_buffer_unregister(struct iio_dev *indio_dev) |
| 469 | { |
| 470 | kfree(indio_dev->buffer->scan_mask); |
| 471 | kfree(indio_dev->buffer->scan_el_group.attrs); |
Lars-Peter Clausen | 84088eb | 2013-10-07 12:50:00 +0100 | [diff] [blame] | 472 | iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 473 | } |
| 474 | EXPORT_SYMBOL(iio_buffer_unregister); |
| 475 | |
| 476 | ssize_t iio_buffer_read_length(struct device *dev, |
| 477 | struct device_attribute *attr, |
| 478 | char *buf) |
| 479 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 480 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 481 | struct iio_buffer *buffer = indio_dev->buffer; |
| 482 | |
| 483 | if (buffer->access->get_length) |
| 484 | return sprintf(buf, "%d\n", |
| 485 | buffer->access->get_length(buffer)); |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | EXPORT_SYMBOL(iio_buffer_read_length); |
| 490 | |
| 491 | ssize_t iio_buffer_write_length(struct device *dev, |
| 492 | struct device_attribute *attr, |
| 493 | const char *buf, |
| 494 | size_t len) |
| 495 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 496 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 497 | struct iio_buffer *buffer = indio_dev->buffer; |
Lars-Peter Clausen | 948ad20 | 2012-10-18 14:47:00 +0100 | [diff] [blame] | 498 | unsigned int val; |
| 499 | int ret; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 500 | |
Lars-Peter Clausen | 948ad20 | 2012-10-18 14:47:00 +0100 | [diff] [blame] | 501 | ret = kstrtouint(buf, 10, &val); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 502 | if (ret) |
| 503 | return ret; |
| 504 | |
| 505 | if (buffer->access->get_length) |
| 506 | if (val == buffer->access->get_length(buffer)) |
| 507 | return len; |
| 508 | |
Lars-Peter Clausen | e38c79e | 2011-12-19 15:23:44 +0100 | [diff] [blame] | 509 | mutex_lock(&indio_dev->mlock); |
Lars-Peter Clausen | 705ee2c | 2013-09-15 16:31:00 +0100 | [diff] [blame] | 510 | if (iio_buffer_is_active(indio_dev->buffer)) { |
Lars-Peter Clausen | e38c79e | 2011-12-19 15:23:44 +0100 | [diff] [blame] | 511 | ret = -EBUSY; |
| 512 | } else { |
Lars-Peter Clausen | 869871b | 2011-12-19 15:23:48 +0100 | [diff] [blame] | 513 | if (buffer->access->set_length) |
Lars-Peter Clausen | e38c79e | 2011-12-19 15:23:44 +0100 | [diff] [blame] | 514 | buffer->access->set_length(buffer, val); |
Lars-Peter Clausen | e38c79e | 2011-12-19 15:23:44 +0100 | [diff] [blame] | 515 | ret = 0; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 516 | } |
Lars-Peter Clausen | e38c79e | 2011-12-19 15:23:44 +0100 | [diff] [blame] | 517 | mutex_unlock(&indio_dev->mlock); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 518 | |
Lars-Peter Clausen | e38c79e | 2011-12-19 15:23:44 +0100 | [diff] [blame] | 519 | return ret ? ret : len; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 520 | } |
| 521 | EXPORT_SYMBOL(iio_buffer_write_length); |
| 522 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 523 | ssize_t iio_buffer_show_enable(struct device *dev, |
| 524 | struct device_attribute *attr, |
| 525 | char *buf) |
| 526 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 527 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 705ee2c | 2013-09-15 16:31:00 +0100 | [diff] [blame] | 528 | return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer)); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 529 | } |
| 530 | EXPORT_SYMBOL(iio_buffer_show_enable); |
| 531 | |
Peter Meerwald | 183f417 | 2013-09-18 22:10:00 +0100 | [diff] [blame] | 532 | static int iio_compute_scan_bytes(struct iio_dev *indio_dev, |
| 533 | const unsigned long *mask, bool timestamp) |
Jonathan Cameron | 959d295 | 2011-12-05 21:37:13 +0000 | [diff] [blame] | 534 | { |
Jonathan Cameron | 959d295 | 2011-12-05 21:37:13 +0000 | [diff] [blame] | 535 | const struct iio_chan_spec *ch; |
| 536 | unsigned bytes = 0; |
| 537 | int length, i; |
Jonathan Cameron | 6b3b58e | 2012-04-21 10:09:33 +0100 | [diff] [blame] | 538 | |
| 539 | /* How much space will the demuxed element take? */ |
| 540 | for_each_set_bit(i, mask, |
| 541 | indio_dev->masklength) { |
| 542 | ch = iio_find_channel_from_si(indio_dev, i); |
Srinivas Pandruvada | 0ee8546 | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 543 | if (ch->scan_type.repeat > 1) |
| 544 | length = ch->scan_type.storagebits / 8 * |
| 545 | ch->scan_type.repeat; |
| 546 | else |
| 547 | length = ch->scan_type.storagebits / 8; |
Jonathan Cameron | 6b3b58e | 2012-04-21 10:09:33 +0100 | [diff] [blame] | 548 | bytes = ALIGN(bytes, length); |
| 549 | bytes += length; |
| 550 | } |
| 551 | if (timestamp) { |
| 552 | ch = iio_find_channel_from_si(indio_dev, |
Jonathan Cameron | f126480 | 2012-04-21 10:09:34 +0100 | [diff] [blame] | 553 | indio_dev->scan_index_timestamp); |
Srinivas Pandruvada | 0ee8546 | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 554 | if (ch->scan_type.repeat > 1) |
| 555 | length = ch->scan_type.storagebits / 8 * |
| 556 | ch->scan_type.repeat; |
| 557 | else |
| 558 | length = ch->scan_type.storagebits / 8; |
Jonathan Cameron | 6b3b58e | 2012-04-21 10:09:33 +0100 | [diff] [blame] | 559 | bytes = ALIGN(bytes, length); |
| 560 | bytes += length; |
| 561 | } |
| 562 | return bytes; |
| 563 | } |
| 564 | |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 565 | static void iio_buffer_activate(struct iio_dev *indio_dev, |
| 566 | struct iio_buffer *buffer) |
| 567 | { |
| 568 | iio_buffer_get(buffer); |
| 569 | list_add(&buffer->buffer_list, &indio_dev->buffer_list); |
| 570 | } |
| 571 | |
| 572 | static void iio_buffer_deactivate(struct iio_buffer *buffer) |
| 573 | { |
| 574 | list_del_init(&buffer->buffer_list); |
| 575 | iio_buffer_put(buffer); |
| 576 | } |
| 577 | |
Lars-Peter Clausen | a87c82e | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 578 | void iio_disable_all_buffers(struct iio_dev *indio_dev) |
| 579 | { |
| 580 | struct iio_buffer *buffer, *_buffer; |
| 581 | |
| 582 | if (list_empty(&indio_dev->buffer_list)) |
| 583 | return; |
| 584 | |
| 585 | if (indio_dev->setup_ops->predisable) |
| 586 | indio_dev->setup_ops->predisable(indio_dev); |
| 587 | |
| 588 | list_for_each_entry_safe(buffer, _buffer, |
| 589 | &indio_dev->buffer_list, buffer_list) |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 590 | iio_buffer_deactivate(buffer); |
Lars-Peter Clausen | a87c82e | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 591 | |
| 592 | indio_dev->currentmode = INDIO_DIRECT_MODE; |
| 593 | if (indio_dev->setup_ops->postdisable) |
| 594 | indio_dev->setup_ops->postdisable(indio_dev); |
Lars-Peter Clausen | e086ed7 | 2013-10-15 09:38:00 +0100 | [diff] [blame] | 595 | |
| 596 | if (indio_dev->available_scan_masks == NULL) |
| 597 | kfree(indio_dev->active_scan_mask); |
Lars-Peter Clausen | a87c82e | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 598 | } |
| 599 | |
Lars-Peter Clausen | 8e05099 | 2013-10-14 17:49:00 +0100 | [diff] [blame] | 600 | static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev, |
| 601 | struct iio_buffer *buffer) |
| 602 | { |
| 603 | unsigned int bytes; |
| 604 | |
| 605 | if (!buffer->access->set_bytes_per_datum) |
| 606 | return; |
| 607 | |
| 608 | bytes = iio_compute_scan_bytes(indio_dev, buffer->scan_mask, |
| 609 | buffer->scan_timestamp); |
| 610 | |
| 611 | buffer->access->set_bytes_per_datum(buffer, bytes); |
| 612 | } |
| 613 | |
Lars-Peter Clausen | a951945 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 614 | static int __iio_update_buffers(struct iio_dev *indio_dev, |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 615 | struct iio_buffer *insert_buffer, |
| 616 | struct iio_buffer *remove_buffer) |
Jonathan Cameron | 6b3b58e | 2012-04-21 10:09:33 +0100 | [diff] [blame] | 617 | { |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 618 | int ret; |
| 619 | int success = 0; |
| 620 | struct iio_buffer *buffer; |
| 621 | unsigned long *compound_mask; |
| 622 | const unsigned long *old_mask; |
Jonathan Cameron | 959d295 | 2011-12-05 21:37:13 +0000 | [diff] [blame] | 623 | |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 624 | /* Wind down existing buffers - iff there are any */ |
| 625 | if (!list_empty(&indio_dev->buffer_list)) { |
| 626 | if (indio_dev->setup_ops->predisable) { |
| 627 | ret = indio_dev->setup_ops->predisable(indio_dev); |
| 628 | if (ret) |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 629 | return ret; |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 630 | } |
| 631 | indio_dev->currentmode = INDIO_DIRECT_MODE; |
| 632 | if (indio_dev->setup_ops->postdisable) { |
| 633 | ret = indio_dev->setup_ops->postdisable(indio_dev); |
| 634 | if (ret) |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 635 | return ret; |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | /* Keep a copy of current setup to allow roll back */ |
| 639 | old_mask = indio_dev->active_scan_mask; |
| 640 | if (!indio_dev->available_scan_masks) |
| 641 | indio_dev->active_scan_mask = NULL; |
| 642 | |
| 643 | if (remove_buffer) |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 644 | iio_buffer_deactivate(remove_buffer); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 645 | if (insert_buffer) |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 646 | iio_buffer_activate(indio_dev, insert_buffer); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 647 | |
| 648 | /* If no buffers in list, we are done */ |
| 649 | if (list_empty(&indio_dev->buffer_list)) { |
| 650 | indio_dev->currentmode = INDIO_DIRECT_MODE; |
| 651 | if (indio_dev->available_scan_masks == NULL) |
| 652 | kfree(old_mask); |
| 653 | return 0; |
| 654 | } |
Jonathan Cameron | 959d295 | 2011-12-05 21:37:13 +0000 | [diff] [blame] | 655 | |
Peter Meerwald | 9572588 | 2013-09-17 23:42:00 +0100 | [diff] [blame] | 656 | /* What scan mask do we actually have? */ |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 657 | compound_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), |
| 658 | sizeof(long), GFP_KERNEL); |
| 659 | if (compound_mask == NULL) { |
| 660 | if (indio_dev->available_scan_masks == NULL) |
| 661 | kfree(old_mask); |
| 662 | return -ENOMEM; |
| 663 | } |
| 664 | indio_dev->scan_timestamp = 0; |
| 665 | |
| 666 | list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { |
| 667 | bitmap_or(compound_mask, compound_mask, buffer->scan_mask, |
| 668 | indio_dev->masklength); |
| 669 | indio_dev->scan_timestamp |= buffer->scan_timestamp; |
| 670 | } |
| 671 | if (indio_dev->available_scan_masks) { |
Jonathan Cameron | 959d295 | 2011-12-05 21:37:13 +0000 | [diff] [blame] | 672 | indio_dev->active_scan_mask = |
| 673 | iio_scan_mask_match(indio_dev->available_scan_masks, |
| 674 | indio_dev->masklength, |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 675 | compound_mask); |
| 676 | if (indio_dev->active_scan_mask == NULL) { |
| 677 | /* |
| 678 | * Roll back. |
| 679 | * Note can only occur when adding a buffer. |
| 680 | */ |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 681 | iio_buffer_deactivate(insert_buffer); |
Peter Meerwald | d66e045 | 2013-09-18 22:10:00 +0100 | [diff] [blame] | 682 | if (old_mask) { |
| 683 | indio_dev->active_scan_mask = old_mask; |
| 684 | success = -EINVAL; |
| 685 | } |
| 686 | else { |
| 687 | kfree(compound_mask); |
| 688 | ret = -EINVAL; |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 689 | return ret; |
Peter Meerwald | d66e045 | 2013-09-18 22:10:00 +0100 | [diff] [blame] | 690 | } |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 691 | } |
| 692 | } else { |
| 693 | indio_dev->active_scan_mask = compound_mask; |
| 694 | } |
Lars-Peter Clausen | aff1eb4 | 2012-06-15 18:08:59 +0200 | [diff] [blame] | 695 | |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 696 | iio_update_demux(indio_dev); |
| 697 | |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 698 | /* Wind up again */ |
| 699 | if (indio_dev->setup_ops->preenable) { |
| 700 | ret = indio_dev->setup_ops->preenable(indio_dev); |
| 701 | if (ret) { |
| 702 | printk(KERN_ERR |
Michał Mirosław | bec1889 | 2013-05-04 14:19:00 +0100 | [diff] [blame] | 703 | "Buffer not started: buffer preenable failed (%d)\n", ret); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 704 | goto error_remove_inserted; |
| 705 | } |
| 706 | } |
| 707 | indio_dev->scan_bytes = |
| 708 | iio_compute_scan_bytes(indio_dev, |
| 709 | indio_dev->active_scan_mask, |
| 710 | indio_dev->scan_timestamp); |
Lars-Peter Clausen | 8e05099 | 2013-10-14 17:49:00 +0100 | [diff] [blame] | 711 | list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { |
| 712 | iio_buffer_update_bytes_per_datum(indio_dev, buffer); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 713 | if (buffer->access->request_update) { |
| 714 | ret = buffer->access->request_update(buffer); |
| 715 | if (ret) { |
| 716 | printk(KERN_INFO |
Michał Mirosław | bec1889 | 2013-05-04 14:19:00 +0100 | [diff] [blame] | 717 | "Buffer not started: buffer parameter update failed (%d)\n", ret); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 718 | goto error_run_postdisable; |
| 719 | } |
| 720 | } |
Lars-Peter Clausen | 8e05099 | 2013-10-14 17:49:00 +0100 | [diff] [blame] | 721 | } |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 722 | if (indio_dev->info->update_scan_mode) { |
| 723 | ret = indio_dev->info |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 724 | ->update_scan_mode(indio_dev, |
| 725 | indio_dev->active_scan_mask); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 726 | if (ret < 0) { |
Michał Mirosław | bec1889 | 2013-05-04 14:19:00 +0100 | [diff] [blame] | 727 | printk(KERN_INFO "Buffer not started: update scan mode failed (%d)\n", ret); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 728 | goto error_run_postdisable; |
| 729 | } |
| 730 | } |
Peter Meerwald | 9572588 | 2013-09-17 23:42:00 +0100 | [diff] [blame] | 731 | /* Definitely possible for devices to support both of these. */ |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 732 | if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) { |
| 733 | if (!indio_dev->trig) { |
| 734 | printk(KERN_INFO "Buffer not started: no trigger\n"); |
| 735 | ret = -EINVAL; |
| 736 | /* Can only occur on first buffer */ |
| 737 | goto error_run_postdisable; |
| 738 | } |
| 739 | indio_dev->currentmode = INDIO_BUFFER_TRIGGERED; |
| 740 | } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) { |
| 741 | indio_dev->currentmode = INDIO_BUFFER_HARDWARE; |
Peter Meerwald | 9572588 | 2013-09-17 23:42:00 +0100 | [diff] [blame] | 742 | } else { /* Should never be reached */ |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 743 | ret = -EINVAL; |
| 744 | goto error_run_postdisable; |
| 745 | } |
| 746 | |
| 747 | if (indio_dev->setup_ops->postenable) { |
| 748 | ret = indio_dev->setup_ops->postenable(indio_dev); |
| 749 | if (ret) { |
| 750 | printk(KERN_INFO |
Michał Mirosław | bec1889 | 2013-05-04 14:19:00 +0100 | [diff] [blame] | 751 | "Buffer not started: postenable failed (%d)\n", ret); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 752 | indio_dev->currentmode = INDIO_DIRECT_MODE; |
| 753 | if (indio_dev->setup_ops->postdisable) |
| 754 | indio_dev->setup_ops->postdisable(indio_dev); |
| 755 | goto error_disable_all_buffers; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | if (indio_dev->available_scan_masks) |
| 760 | kfree(compound_mask); |
| 761 | else |
| 762 | kfree(old_mask); |
| 763 | |
| 764 | return success; |
| 765 | |
| 766 | error_disable_all_buffers: |
| 767 | indio_dev->currentmode = INDIO_DIRECT_MODE; |
| 768 | error_run_postdisable: |
| 769 | if (indio_dev->setup_ops->postdisable) |
| 770 | indio_dev->setup_ops->postdisable(indio_dev); |
| 771 | error_remove_inserted: |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 772 | if (insert_buffer) |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 773 | iio_buffer_deactivate(insert_buffer); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 774 | indio_dev->active_scan_mask = old_mask; |
| 775 | kfree(compound_mask); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 776 | return ret; |
| 777 | } |
Lars-Peter Clausen | a951945 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 778 | |
| 779 | int iio_update_buffers(struct iio_dev *indio_dev, |
| 780 | struct iio_buffer *insert_buffer, |
| 781 | struct iio_buffer *remove_buffer) |
| 782 | { |
| 783 | int ret; |
| 784 | |
Lars-Peter Clausen | 3909fab | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 785 | if (insert_buffer == remove_buffer) |
| 786 | return 0; |
| 787 | |
Lars-Peter Clausen | a951945 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 788 | mutex_lock(&indio_dev->info_exist_lock); |
| 789 | mutex_lock(&indio_dev->mlock); |
| 790 | |
Lars-Peter Clausen | 3909fab | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 791 | if (insert_buffer && iio_buffer_is_active(insert_buffer)) |
| 792 | insert_buffer = NULL; |
| 793 | |
| 794 | if (remove_buffer && !iio_buffer_is_active(remove_buffer)) |
| 795 | remove_buffer = NULL; |
| 796 | |
| 797 | if (!insert_buffer && !remove_buffer) { |
| 798 | ret = 0; |
| 799 | goto out_unlock; |
| 800 | } |
| 801 | |
Lars-Peter Clausen | a951945 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 802 | if (indio_dev->info == NULL) { |
| 803 | ret = -ENODEV; |
| 804 | goto out_unlock; |
| 805 | } |
| 806 | |
| 807 | ret = __iio_update_buffers(indio_dev, insert_buffer, remove_buffer); |
| 808 | |
| 809 | out_unlock: |
| 810 | mutex_unlock(&indio_dev->mlock); |
| 811 | mutex_unlock(&indio_dev->info_exist_lock); |
| 812 | |
| 813 | return ret; |
| 814 | } |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 815 | EXPORT_SYMBOL_GPL(iio_update_buffers); |
| 816 | |
| 817 | ssize_t iio_buffer_store_enable(struct device *dev, |
| 818 | struct device_attribute *attr, |
| 819 | const char *buf, |
| 820 | size_t len) |
| 821 | { |
| 822 | int ret; |
| 823 | bool requested_state; |
| 824 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 825 | bool inlist; |
| 826 | |
| 827 | ret = strtobool(buf, &requested_state); |
| 828 | if (ret < 0) |
| 829 | return ret; |
| 830 | |
| 831 | mutex_lock(&indio_dev->mlock); |
| 832 | |
| 833 | /* Find out if it is in the list */ |
Lars-Peter Clausen | 705ee2c | 2013-09-15 16:31:00 +0100 | [diff] [blame] | 834 | inlist = iio_buffer_is_active(indio_dev->buffer); |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 835 | /* Already in desired state */ |
| 836 | if (inlist == requested_state) |
| 837 | goto done; |
| 838 | |
| 839 | if (requested_state) |
Lars-Peter Clausen | a951945 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 840 | ret = __iio_update_buffers(indio_dev, |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 841 | indio_dev->buffer, NULL); |
| 842 | else |
Lars-Peter Clausen | a951945 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 843 | ret = __iio_update_buffers(indio_dev, |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 844 | NULL, indio_dev->buffer); |
| 845 | |
| 846 | if (ret < 0) |
| 847 | goto done; |
| 848 | done: |
| 849 | mutex_unlock(&indio_dev->mlock); |
| 850 | return (ret < 0) ? ret : len; |
| 851 | } |
| 852 | EXPORT_SYMBOL(iio_buffer_store_enable); |
| 853 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 854 | /** |
Lars-Peter Clausen | 8163663 | 2012-07-09 10:00:00 +0100 | [diff] [blame] | 855 | * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected |
| 856 | * @indio_dev: the iio device |
| 857 | * @mask: scan mask to be checked |
| 858 | * |
| 859 | * Return true if exactly one bit is set in the scan mask, false otherwise. It |
| 860 | * can be used for devices where only one channel can be active for sampling at |
| 861 | * a time. |
| 862 | */ |
| 863 | bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev, |
| 864 | const unsigned long *mask) |
| 865 | { |
| 866 | return bitmap_weight(mask, indio_dev->masklength) == 1; |
| 867 | } |
| 868 | EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot); |
| 869 | |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame] | 870 | int iio_scan_mask_query(struct iio_dev *indio_dev, |
| 871 | struct iio_buffer *buffer, int bit) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 872 | { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 873 | if (bit > indio_dev->masklength) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 874 | return -EINVAL; |
| 875 | |
| 876 | if (!buffer->scan_mask) |
| 877 | return 0; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 878 | |
Alec Berg | 2076a20 | 2014-03-19 18:50:00 +0000 | [diff] [blame] | 879 | /* Ensure return value is 0 or 1. */ |
| 880 | return !!test_bit(bit, buffer->scan_mask); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 881 | }; |
| 882 | EXPORT_SYMBOL_GPL(iio_scan_mask_query); |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 883 | |
| 884 | /** |
| 885 | * struct iio_demux_table() - table describing demux memcpy ops |
| 886 | * @from: index to copy from |
Peter Meerwald | 99698b4 | 2012-08-26 13:43:00 +0100 | [diff] [blame] | 887 | * @to: index to copy to |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 888 | * @length: how many bytes to copy |
| 889 | * @l: list head used for management |
| 890 | */ |
| 891 | struct iio_demux_table { |
| 892 | unsigned from; |
| 893 | unsigned to; |
| 894 | unsigned length; |
| 895 | struct list_head l; |
| 896 | }; |
| 897 | |
Lars-Peter Clausen | 5d65d92 | 2013-09-15 17:50:00 +0100 | [diff] [blame] | 898 | static const void *iio_demux(struct iio_buffer *buffer, |
| 899 | const void *datain) |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 900 | { |
| 901 | struct iio_demux_table *t; |
| 902 | |
| 903 | if (list_empty(&buffer->demux_list)) |
| 904 | return datain; |
| 905 | list_for_each_entry(t, &buffer->demux_list, l) |
| 906 | memcpy(buffer->demux_bounce + t->to, |
| 907 | datain + t->from, t->length); |
| 908 | |
| 909 | return buffer->demux_bounce; |
| 910 | } |
| 911 | |
Lars-Peter Clausen | 5d65d92 | 2013-09-15 17:50:00 +0100 | [diff] [blame] | 912 | static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data) |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 913 | { |
Lars-Peter Clausen | 5d65d92 | 2013-09-15 17:50:00 +0100 | [diff] [blame] | 914 | const void *dataout = iio_demux(buffer, data); |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 915 | |
Lars-Peter Clausen | ce56ade | 2012-09-04 13:38:00 +0100 | [diff] [blame] | 916 | return buffer->access->store_to(buffer, dataout); |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 917 | } |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 918 | |
Jonathan Cameron | 842cd10 | 2012-04-21 10:09:45 +0100 | [diff] [blame] | 919 | static void iio_buffer_demux_free(struct iio_buffer *buffer) |
| 920 | { |
| 921 | struct iio_demux_table *p, *q; |
| 922 | list_for_each_entry_safe(p, q, &buffer->demux_list, l) { |
| 923 | list_del(&p->l); |
| 924 | kfree(p); |
| 925 | } |
| 926 | } |
| 927 | |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 928 | |
Lars-Peter Clausen | 5d65d92 | 2013-09-15 17:50:00 +0100 | [diff] [blame] | 929 | int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 930 | { |
| 931 | int ret; |
| 932 | struct iio_buffer *buf; |
| 933 | |
| 934 | list_for_each_entry(buf, &indio_dev->buffer_list, buffer_list) { |
| 935 | ret = iio_push_to_buffer(buf, data); |
| 936 | if (ret < 0) |
| 937 | return ret; |
| 938 | } |
| 939 | |
| 940 | return 0; |
| 941 | } |
| 942 | EXPORT_SYMBOL_GPL(iio_push_to_buffers); |
| 943 | |
Lars-Peter Clausen | cbe88bc | 2014-07-17 16:59:00 +0100 | [diff] [blame] | 944 | static int iio_buffer_add_demux(struct iio_buffer *buffer, |
| 945 | struct iio_demux_table **p, unsigned int in_loc, unsigned int out_loc, |
| 946 | unsigned int length) |
| 947 | { |
| 948 | |
| 949 | if (*p && (*p)->from + (*p)->length == in_loc && |
| 950 | (*p)->to + (*p)->length == out_loc) { |
| 951 | (*p)->length += length; |
| 952 | } else { |
Jonathan Cameron | 7cdca178 | 2014-08-08 09:43:00 +0100 | [diff] [blame] | 953 | *p = kmalloc(sizeof(**p), GFP_KERNEL); |
Lars-Peter Clausen | cbe88bc | 2014-07-17 16:59:00 +0100 | [diff] [blame] | 954 | if (*p == NULL) |
| 955 | return -ENOMEM; |
| 956 | (*p)->from = in_loc; |
| 957 | (*p)->to = out_loc; |
| 958 | (*p)->length = length; |
| 959 | list_add_tail(&(*p)->l, &buffer->demux_list); |
| 960 | } |
| 961 | |
| 962 | return 0; |
| 963 | } |
| 964 | |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 965 | static int iio_buffer_update_demux(struct iio_dev *indio_dev, |
| 966 | struct iio_buffer *buffer) |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 967 | { |
| 968 | const struct iio_chan_spec *ch; |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 969 | int ret, in_ind = -1, out_ind, length; |
| 970 | unsigned in_loc = 0, out_loc = 0; |
Lars-Peter Clausen | cbe88bc | 2014-07-17 16:59:00 +0100 | [diff] [blame] | 971 | struct iio_demux_table *p = NULL; |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 972 | |
| 973 | /* Clear out any old demux */ |
Jonathan Cameron | 842cd10 | 2012-04-21 10:09:45 +0100 | [diff] [blame] | 974 | iio_buffer_demux_free(buffer); |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 975 | kfree(buffer->demux_bounce); |
| 976 | buffer->demux_bounce = NULL; |
| 977 | |
| 978 | /* First work out which scan mode we will actually have */ |
| 979 | if (bitmap_equal(indio_dev->active_scan_mask, |
| 980 | buffer->scan_mask, |
| 981 | indio_dev->masklength)) |
| 982 | return 0; |
| 983 | |
| 984 | /* Now we have the two masks, work from least sig and build up sizes */ |
| 985 | for_each_set_bit(out_ind, |
Lars-Peter Clausen | 61bd55c | 2014-07-17 16:59:00 +0100 | [diff] [blame] | 986 | buffer->scan_mask, |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 987 | indio_dev->masklength) { |
| 988 | in_ind = find_next_bit(indio_dev->active_scan_mask, |
| 989 | indio_dev->masklength, |
| 990 | in_ind + 1); |
| 991 | while (in_ind != out_ind) { |
| 992 | in_ind = find_next_bit(indio_dev->active_scan_mask, |
| 993 | indio_dev->masklength, |
| 994 | in_ind + 1); |
| 995 | ch = iio_find_channel_from_si(indio_dev, in_ind); |
Srinivas Pandruvada | 0ee8546 | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 996 | if (ch->scan_type.repeat > 1) |
| 997 | length = ch->scan_type.storagebits / 8 * |
| 998 | ch->scan_type.repeat; |
| 999 | else |
| 1000 | length = ch->scan_type.storagebits / 8; |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 1001 | /* Make sure we are aligned */ |
Lars-Peter Clausen | 61072db | 2014-07-17 16:59:00 +0100 | [diff] [blame] | 1002 | in_loc = roundup(in_loc, length) + length; |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 1003 | } |
| 1004 | ch = iio_find_channel_from_si(indio_dev, in_ind); |
Srinivas Pandruvada | 0ee8546 | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 1005 | if (ch->scan_type.repeat > 1) |
| 1006 | length = ch->scan_type.storagebits / 8 * |
| 1007 | ch->scan_type.repeat; |
| 1008 | else |
| 1009 | length = ch->scan_type.storagebits / 8; |
Lars-Peter Clausen | 61072db | 2014-07-17 16:59:00 +0100 | [diff] [blame] | 1010 | out_loc = roundup(out_loc, length); |
| 1011 | in_loc = roundup(in_loc, length); |
Lars-Peter Clausen | cbe88bc | 2014-07-17 16:59:00 +0100 | [diff] [blame] | 1012 | ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); |
| 1013 | if (ret) |
| 1014 | goto error_clear_mux_table; |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 1015 | out_loc += length; |
| 1016 | in_loc += length; |
| 1017 | } |
| 1018 | /* Relies on scan_timestamp being last */ |
| 1019 | if (buffer->scan_timestamp) { |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 1020 | ch = iio_find_channel_from_si(indio_dev, |
Jonathan Cameron | f126480 | 2012-04-21 10:09:34 +0100 | [diff] [blame] | 1021 | indio_dev->scan_index_timestamp); |
Srinivas Pandruvada | 0ee8546 | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 1022 | if (ch->scan_type.repeat > 1) |
| 1023 | length = ch->scan_type.storagebits / 8 * |
| 1024 | ch->scan_type.repeat; |
| 1025 | else |
| 1026 | length = ch->scan_type.storagebits / 8; |
Lars-Peter Clausen | 61072db | 2014-07-17 16:59:00 +0100 | [diff] [blame] | 1027 | out_loc = roundup(out_loc, length); |
| 1028 | in_loc = roundup(in_loc, length); |
Lars-Peter Clausen | cbe88bc | 2014-07-17 16:59:00 +0100 | [diff] [blame] | 1029 | ret = iio_buffer_add_demux(buffer, &p, in_loc, out_loc, length); |
| 1030 | if (ret) |
| 1031 | goto error_clear_mux_table; |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 1032 | out_loc += length; |
| 1033 | in_loc += length; |
| 1034 | } |
| 1035 | buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL); |
| 1036 | if (buffer->demux_bounce == NULL) { |
| 1037 | ret = -ENOMEM; |
| 1038 | goto error_clear_mux_table; |
| 1039 | } |
| 1040 | return 0; |
| 1041 | |
| 1042 | error_clear_mux_table: |
Jonathan Cameron | 842cd10 | 2012-04-21 10:09:45 +0100 | [diff] [blame] | 1043 | iio_buffer_demux_free(buffer); |
| 1044 | |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 1045 | return ret; |
| 1046 | } |
Jonathan Cameron | 84b36ce | 2012-06-30 20:06:00 +0100 | [diff] [blame] | 1047 | |
| 1048 | int iio_update_demux(struct iio_dev *indio_dev) |
| 1049 | { |
| 1050 | struct iio_buffer *buffer; |
| 1051 | int ret; |
| 1052 | |
| 1053 | list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) { |
| 1054 | ret = iio_buffer_update_demux(indio_dev, buffer); |
| 1055 | if (ret < 0) |
| 1056 | goto error_clear_mux_table; |
| 1057 | } |
| 1058 | return 0; |
| 1059 | |
| 1060 | error_clear_mux_table: |
| 1061 | list_for_each_entry(buffer, &indio_dev->buffer_list, buffer_list) |
| 1062 | iio_buffer_demux_free(buffer); |
| 1063 | |
| 1064 | return ret; |
| 1065 | } |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 1066 | EXPORT_SYMBOL_GPL(iio_update_demux); |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 1067 | |
| 1068 | /** |
| 1069 | * iio_buffer_release() - Free a buffer's resources |
| 1070 | * @ref: Pointer to the kref embedded in the iio_buffer struct |
| 1071 | * |
| 1072 | * This function is called when the last reference to the buffer has been |
| 1073 | * dropped. It will typically free all resources allocated by the buffer. Do not |
| 1074 | * call this function manually, always use iio_buffer_put() when done using a |
| 1075 | * buffer. |
| 1076 | */ |
| 1077 | static void iio_buffer_release(struct kref *ref) |
| 1078 | { |
| 1079 | struct iio_buffer *buffer = container_of(ref, struct iio_buffer, ref); |
| 1080 | |
| 1081 | buffer->access->release(buffer); |
| 1082 | } |
| 1083 | |
| 1084 | /** |
| 1085 | * iio_buffer_get() - Grab a reference to the buffer |
| 1086 | * @buffer: The buffer to grab a reference for, may be NULL |
| 1087 | * |
| 1088 | * Returns the pointer to the buffer that was passed into the function. |
| 1089 | */ |
| 1090 | struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer) |
| 1091 | { |
| 1092 | if (buffer) |
| 1093 | kref_get(&buffer->ref); |
| 1094 | |
| 1095 | return buffer; |
| 1096 | } |
| 1097 | EXPORT_SYMBOL_GPL(iio_buffer_get); |
| 1098 | |
| 1099 | /** |
| 1100 | * iio_buffer_put() - Release the reference to the buffer |
| 1101 | * @buffer: The buffer to release the reference for, may be NULL |
| 1102 | */ |
| 1103 | void iio_buffer_put(struct iio_buffer *buffer) |
| 1104 | { |
| 1105 | if (buffer) |
| 1106 | kref_put(&buffer->ref, iio_buffer_release); |
| 1107 | } |
| 1108 | EXPORT_SYMBOL_GPL(iio_buffer_put); |