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