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