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> |
| 23 | |
| 24 | #include "iio.h" |
| 25 | #include "iio_core.h" |
| 26 | #include "sysfs.h" |
Jonathan Cameron | af5046a | 2011-10-26 17:41:32 +0100 | [diff] [blame] | 27 | #include "buffer.h" |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 28 | |
| 29 | static const char * const iio_endian_prefix[] = { |
| 30 | [IIO_BE] = "be", |
| 31 | [IIO_LE] = "le", |
| 32 | }; |
| 33 | |
| 34 | /** |
| 35 | * iio_buffer_read_first_n_outer() - chrdev read for buffer access |
| 36 | * |
| 37 | * This function relies on all buffer implementations having an |
| 38 | * iio_buffer as their first element. |
| 39 | **/ |
| 40 | ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf, |
| 41 | size_t n, loff_t *f_ps) |
| 42 | { |
| 43 | struct iio_dev *indio_dev = filp->private_data; |
| 44 | struct iio_buffer *rb = indio_dev->buffer; |
| 45 | |
Jonathan Cameron | 96e00f1 | 2011-10-26 17:27:45 +0100 | [diff] [blame] | 46 | if (!rb || !rb->access->read_first_n) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 47 | return -EINVAL; |
| 48 | return rb->access->read_first_n(rb, n, buf); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * iio_buffer_poll() - poll the buffer to find out if it has data |
| 53 | */ |
| 54 | unsigned int iio_buffer_poll(struct file *filp, |
| 55 | struct poll_table_struct *wait) |
| 56 | { |
| 57 | struct iio_dev *indio_dev = filp->private_data; |
| 58 | struct iio_buffer *rb = indio_dev->buffer; |
| 59 | |
| 60 | poll_wait(filp, &rb->pollq, wait); |
| 61 | if (rb->stufftoread) |
| 62 | return POLLIN | POLLRDNORM; |
| 63 | /* need a way of knowing if there may be enough data... */ |
| 64 | return 0; |
| 65 | } |
| 66 | |
Jonathan Cameron | 30eb82f | 2011-09-21 11:16:02 +0100 | [diff] [blame] | 67 | int iio_chrdev_buffer_open(struct iio_dev *indio_dev) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 68 | { |
| 69 | struct iio_buffer *rb = indio_dev->buffer; |
Jonathan Cameron | 30eb82f | 2011-09-21 11:16:02 +0100 | [diff] [blame] | 70 | if (!rb) |
Jonathan Cameron | 96e00f1 | 2011-10-26 17:27:45 +0100 | [diff] [blame] | 71 | return 0; |
Jonathan Cameron | 30eb82f | 2011-09-21 11:16:02 +0100 | [diff] [blame] | 72 | if (rb->access->mark_in_use) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 73 | rb->access->mark_in_use(rb); |
Jonathan Cameron | 30eb82f | 2011-09-21 11:16:02 +0100 | [diff] [blame] | 74 | return 0; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void iio_chrdev_buffer_release(struct iio_dev *indio_dev) |
| 78 | { |
| 79 | struct iio_buffer *rb = indio_dev->buffer; |
| 80 | |
Jonathan Cameron | 96e00f1 | 2011-10-26 17:27:45 +0100 | [diff] [blame] | 81 | if (!rb) |
| 82 | return; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 83 | clear_bit(IIO_BUSY_BIT_POS, &rb->flags); |
| 84 | if (rb->access->unmark_in_use) |
| 85 | rb->access->unmark_in_use(rb); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame] | 88 | void iio_buffer_init(struct iio_buffer *buffer) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 89 | { |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 90 | INIT_LIST_HEAD(&buffer->demux_list); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 91 | init_waitqueue_head(&buffer->pollq); |
| 92 | } |
| 93 | EXPORT_SYMBOL(iio_buffer_init); |
| 94 | |
| 95 | static ssize_t iio_show_scan_index(struct device *dev, |
| 96 | struct device_attribute *attr, |
| 97 | char *buf) |
| 98 | { |
| 99 | return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index); |
| 100 | } |
| 101 | |
| 102 | static ssize_t iio_show_fixed_type(struct device *dev, |
| 103 | struct device_attribute *attr, |
| 104 | char *buf) |
| 105 | { |
| 106 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 107 | u8 type = this_attr->c->scan_type.endianness; |
| 108 | |
| 109 | if (type == IIO_CPU) { |
Jonathan Cameron | 9d5d115 | 2011-10-04 16:02:08 +0100 | [diff] [blame] | 110 | #ifdef __LITTLE_ENDIAN |
| 111 | type = IIO_LE; |
| 112 | #else |
| 113 | type = IIO_BE; |
| 114 | #endif |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 115 | } |
| 116 | return sprintf(buf, "%s:%c%d/%d>>%u\n", |
| 117 | iio_endian_prefix[type], |
| 118 | this_attr->c->scan_type.sign, |
| 119 | this_attr->c->scan_type.realbits, |
| 120 | this_attr->c->scan_type.storagebits, |
| 121 | this_attr->c->scan_type.shift); |
| 122 | } |
| 123 | |
| 124 | static ssize_t iio_scan_el_show(struct device *dev, |
| 125 | struct device_attribute *attr, |
| 126 | char *buf) |
| 127 | { |
| 128 | int ret; |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 129 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 130 | |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 131 | ret = test_bit(to_iio_dev_attr(attr)->address, |
| 132 | indio_dev->buffer->scan_mask); |
| 133 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 134 | return sprintf(buf, "%d\n", ret); |
| 135 | } |
| 136 | |
| 137 | static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit) |
| 138 | { |
| 139 | clear_bit(bit, buffer->scan_mask); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static ssize_t iio_scan_el_store(struct device *dev, |
| 144 | struct device_attribute *attr, |
| 145 | const char *buf, |
| 146 | size_t len) |
| 147 | { |
| 148 | int ret = 0; |
| 149 | bool state; |
| 150 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 151 | struct iio_buffer *buffer = indio_dev->buffer; |
| 152 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 153 | |
| 154 | state = !(buf[0] == '0'); |
| 155 | mutex_lock(&indio_dev->mlock); |
Lars-Peter Clausen | 5fd6218 | 2011-12-19 15:23:43 +0100 | [diff] [blame] | 156 | if (iio_buffer_enabled(indio_dev)) { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 157 | ret = -EBUSY; |
| 158 | goto error_ret; |
| 159 | } |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame] | 160 | ret = iio_scan_mask_query(indio_dev, buffer, this_attr->address); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 161 | if (ret < 0) |
| 162 | goto error_ret; |
| 163 | if (!state && ret) { |
| 164 | ret = iio_scan_mask_clear(buffer, this_attr->address); |
| 165 | if (ret) |
| 166 | goto error_ret; |
| 167 | } else if (state && !ret) { |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame] | 168 | ret = iio_scan_mask_set(indio_dev, buffer, this_attr->address); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 169 | if (ret) |
| 170 | goto error_ret; |
| 171 | } |
| 172 | |
| 173 | error_ret: |
| 174 | mutex_unlock(&indio_dev->mlock); |
| 175 | |
Lars-Peter Clausen | 5a2a6e1 | 2011-12-08 18:35:53 +0100 | [diff] [blame] | 176 | return ret < 0 ? ret : len; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 177 | |
| 178 | } |
| 179 | |
| 180 | static ssize_t iio_scan_el_ts_show(struct device *dev, |
| 181 | struct device_attribute *attr, |
| 182 | char *buf) |
| 183 | { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 184 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 185 | return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static ssize_t iio_scan_el_ts_store(struct device *dev, |
| 189 | struct device_attribute *attr, |
| 190 | const char *buf, |
| 191 | size_t len) |
| 192 | { |
| 193 | int ret = 0; |
| 194 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 195 | bool state; |
| 196 | |
| 197 | state = !(buf[0] == '0'); |
| 198 | mutex_lock(&indio_dev->mlock); |
Lars-Peter Clausen | 5fd6218 | 2011-12-19 15:23:43 +0100 | [diff] [blame] | 199 | if (iio_buffer_enabled(indio_dev)) { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 200 | ret = -EBUSY; |
| 201 | goto error_ret; |
| 202 | } |
| 203 | indio_dev->buffer->scan_timestamp = state; |
| 204 | error_ret: |
| 205 | mutex_unlock(&indio_dev->mlock); |
| 206 | |
| 207 | return ret ? ret : len; |
| 208 | } |
| 209 | |
| 210 | static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev, |
| 211 | const struct iio_chan_spec *chan) |
| 212 | { |
| 213 | int ret, attrcount = 0; |
| 214 | struct iio_buffer *buffer = indio_dev->buffer; |
| 215 | |
| 216 | ret = __iio_add_chan_devattr("index", |
| 217 | chan, |
| 218 | &iio_show_scan_index, |
| 219 | NULL, |
| 220 | 0, |
| 221 | 0, |
| 222 | &indio_dev->dev, |
| 223 | &buffer->scan_el_dev_attr_list); |
| 224 | if (ret) |
| 225 | goto error_ret; |
| 226 | attrcount++; |
| 227 | ret = __iio_add_chan_devattr("type", |
| 228 | chan, |
| 229 | &iio_show_fixed_type, |
| 230 | NULL, |
| 231 | 0, |
| 232 | 0, |
| 233 | &indio_dev->dev, |
| 234 | &buffer->scan_el_dev_attr_list); |
| 235 | if (ret) |
| 236 | goto error_ret; |
| 237 | attrcount++; |
| 238 | if (chan->type != IIO_TIMESTAMP) |
| 239 | ret = __iio_add_chan_devattr("en", |
| 240 | chan, |
| 241 | &iio_scan_el_show, |
| 242 | &iio_scan_el_store, |
| 243 | chan->scan_index, |
| 244 | 0, |
| 245 | &indio_dev->dev, |
| 246 | &buffer->scan_el_dev_attr_list); |
| 247 | else |
| 248 | ret = __iio_add_chan_devattr("en", |
| 249 | chan, |
| 250 | &iio_scan_el_ts_show, |
| 251 | &iio_scan_el_ts_store, |
| 252 | chan->scan_index, |
| 253 | 0, |
| 254 | &indio_dev->dev, |
| 255 | &buffer->scan_el_dev_attr_list); |
| 256 | attrcount++; |
| 257 | ret = attrcount; |
| 258 | error_ret: |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | static void iio_buffer_remove_and_free_scan_dev_attr(struct iio_dev *indio_dev, |
| 263 | struct iio_dev_attr *p) |
| 264 | { |
| 265 | kfree(p->dev_attr.attr.name); |
| 266 | kfree(p); |
| 267 | } |
| 268 | |
| 269 | static void __iio_buffer_attr_cleanup(struct iio_dev *indio_dev) |
| 270 | { |
| 271 | struct iio_dev_attr *p, *n; |
| 272 | struct iio_buffer *buffer = indio_dev->buffer; |
| 273 | |
| 274 | list_for_each_entry_safe(p, n, |
| 275 | &buffer->scan_el_dev_attr_list, l) |
| 276 | iio_buffer_remove_and_free_scan_dev_attr(indio_dev, p); |
| 277 | } |
| 278 | |
| 279 | static const char * const iio_scan_elements_group_name = "scan_elements"; |
| 280 | |
| 281 | int iio_buffer_register(struct iio_dev *indio_dev, |
| 282 | const struct iio_chan_spec *channels, |
| 283 | int num_channels) |
| 284 | { |
| 285 | struct iio_dev_attr *p; |
| 286 | struct attribute **attr; |
| 287 | struct iio_buffer *buffer = indio_dev->buffer; |
| 288 | int ret, i, attrn, attrcount, attrcount_orig = 0; |
| 289 | |
| 290 | if (buffer->attrs) |
| 291 | indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs; |
| 292 | |
| 293 | if (buffer->scan_el_attrs != NULL) { |
| 294 | attr = buffer->scan_el_attrs->attrs; |
| 295 | while (*attr++ != NULL) |
| 296 | attrcount_orig++; |
| 297 | } |
| 298 | attrcount = attrcount_orig; |
| 299 | INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list); |
| 300 | if (channels) { |
| 301 | /* new magic */ |
| 302 | for (i = 0; i < num_channels; i++) { |
| 303 | /* Establish necessary mask length */ |
| 304 | if (channels[i].scan_index > |
| 305 | (int)indio_dev->masklength - 1) |
| 306 | indio_dev->masklength |
| 307 | = indio_dev->channels[i].scan_index + 1; |
| 308 | |
| 309 | ret = iio_buffer_add_channel_sysfs(indio_dev, |
| 310 | &channels[i]); |
| 311 | if (ret < 0) |
| 312 | goto error_cleanup_dynamic; |
| 313 | attrcount += ret; |
Jonathan Cameron | beb8060 | 2011-12-05 21:37:11 +0000 | [diff] [blame] | 314 | if (channels[i].type == IIO_TIMESTAMP) |
| 315 | buffer->scan_index_timestamp = |
| 316 | channels[i].scan_index; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 317 | } |
| 318 | if (indio_dev->masklength && buffer->scan_mask == NULL) { |
Thomas Meyer | d83fb18 | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 319 | buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength), |
| 320 | sizeof(*buffer->scan_mask), |
| 321 | GFP_KERNEL); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 322 | if (buffer->scan_mask == NULL) { |
| 323 | ret = -ENOMEM; |
| 324 | goto error_cleanup_dynamic; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | buffer->scan_el_group.name = iio_scan_elements_group_name; |
| 330 | |
Thomas Meyer | d83fb18 | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 331 | buffer->scan_el_group.attrs = kcalloc(attrcount + 1, |
| 332 | sizeof(buffer->scan_el_group.attrs[0]), |
| 333 | GFP_KERNEL); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 334 | if (buffer->scan_el_group.attrs == NULL) { |
| 335 | ret = -ENOMEM; |
| 336 | goto error_free_scan_mask; |
| 337 | } |
| 338 | if (buffer->scan_el_attrs) |
| 339 | memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs, |
| 340 | sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig); |
| 341 | attrn = attrcount_orig; |
| 342 | |
| 343 | list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l) |
| 344 | buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr; |
| 345 | indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group; |
| 346 | |
| 347 | return 0; |
| 348 | |
| 349 | error_free_scan_mask: |
| 350 | kfree(buffer->scan_mask); |
| 351 | error_cleanup_dynamic: |
| 352 | __iio_buffer_attr_cleanup(indio_dev); |
| 353 | |
| 354 | return ret; |
| 355 | } |
| 356 | EXPORT_SYMBOL(iio_buffer_register); |
| 357 | |
| 358 | void iio_buffer_unregister(struct iio_dev *indio_dev) |
| 359 | { |
| 360 | kfree(indio_dev->buffer->scan_mask); |
| 361 | kfree(indio_dev->buffer->scan_el_group.attrs); |
| 362 | __iio_buffer_attr_cleanup(indio_dev); |
| 363 | } |
| 364 | EXPORT_SYMBOL(iio_buffer_unregister); |
| 365 | |
| 366 | ssize_t iio_buffer_read_length(struct device *dev, |
| 367 | struct device_attribute *attr, |
| 368 | char *buf) |
| 369 | { |
| 370 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 371 | struct iio_buffer *buffer = indio_dev->buffer; |
| 372 | |
| 373 | if (buffer->access->get_length) |
| 374 | return sprintf(buf, "%d\n", |
| 375 | buffer->access->get_length(buffer)); |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | EXPORT_SYMBOL(iio_buffer_read_length); |
| 380 | |
| 381 | ssize_t iio_buffer_write_length(struct device *dev, |
| 382 | struct device_attribute *attr, |
| 383 | const char *buf, |
| 384 | size_t len) |
| 385 | { |
| 386 | int ret; |
| 387 | ulong val; |
| 388 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 389 | struct iio_buffer *buffer = indio_dev->buffer; |
| 390 | |
| 391 | ret = strict_strtoul(buf, 10, &val); |
| 392 | if (ret) |
| 393 | return ret; |
| 394 | |
| 395 | if (buffer->access->get_length) |
| 396 | if (val == buffer->access->get_length(buffer)) |
| 397 | return len; |
| 398 | |
Lars-Peter Clausen | e38c79e | 2011-12-19 15:23:44 +0100 | [diff] [blame^] | 399 | mutex_lock(&indio_dev->mlock); |
| 400 | if (iio_buffer_enabled(indio_dev)) { |
| 401 | ret = -EBUSY; |
| 402 | } else { |
| 403 | if (buffer->access->set_length) { |
| 404 | buffer->access->set_length(buffer, val); |
| 405 | if (buffer->access->mark_param_change) |
| 406 | buffer->access->mark_param_change(buffer); |
| 407 | } |
| 408 | ret = 0; |
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 | mutex_unlock(&indio_dev->mlock); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 411 | |
Lars-Peter Clausen | e38c79e | 2011-12-19 15:23:44 +0100 | [diff] [blame^] | 412 | return ret ? ret : len; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 413 | } |
| 414 | EXPORT_SYMBOL(iio_buffer_write_length); |
| 415 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 416 | ssize_t iio_buffer_store_enable(struct device *dev, |
| 417 | struct device_attribute *attr, |
| 418 | const char *buf, |
| 419 | size_t len) |
| 420 | { |
| 421 | int ret; |
| 422 | bool requested_state, current_state; |
| 423 | int previous_mode; |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 424 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 425 | struct iio_buffer *buffer = indio_dev->buffer; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 426 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 427 | mutex_lock(&indio_dev->mlock); |
| 428 | previous_mode = indio_dev->currentmode; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 429 | requested_state = !(buf[0] == '0'); |
Lars-Peter Clausen | d4a6882 | 2011-12-19 15:23:42 +0100 | [diff] [blame] | 430 | current_state = iio_buffer_enabled(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 431 | if (current_state == requested_state) { |
| 432 | printk(KERN_INFO "iio-buffer, current state requested again\n"); |
| 433 | goto done; |
| 434 | } |
| 435 | if (requested_state) { |
Jonathan Cameron | 1612244 | 2011-12-05 22:18:14 +0000 | [diff] [blame] | 436 | if (indio_dev->setup_ops->preenable) { |
| 437 | ret = indio_dev->setup_ops->preenable(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 438 | if (ret) { |
| 439 | printk(KERN_ERR |
| 440 | "Buffer not started:" |
| 441 | "buffer preenable failed\n"); |
| 442 | goto error_ret; |
| 443 | } |
| 444 | } |
| 445 | if (buffer->access->request_update) { |
| 446 | ret = buffer->access->request_update(buffer); |
| 447 | if (ret) { |
| 448 | printk(KERN_INFO |
| 449 | "Buffer not started:" |
| 450 | "buffer parameter update failed\n"); |
| 451 | goto error_ret; |
| 452 | } |
| 453 | } |
| 454 | if (buffer->access->mark_in_use) |
| 455 | buffer->access->mark_in_use(buffer); |
| 456 | /* Definitely possible for devices to support both of these.*/ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 457 | if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) { |
| 458 | if (!indio_dev->trig) { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 459 | printk(KERN_INFO |
| 460 | "Buffer not started: no trigger\n"); |
| 461 | ret = -EINVAL; |
| 462 | if (buffer->access->unmark_in_use) |
| 463 | buffer->access->unmark_in_use(buffer); |
| 464 | goto error_ret; |
| 465 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 466 | indio_dev->currentmode = INDIO_BUFFER_TRIGGERED; |
| 467 | } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) |
| 468 | indio_dev->currentmode = INDIO_BUFFER_HARDWARE; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 469 | else { /* should never be reached */ |
| 470 | ret = -EINVAL; |
| 471 | goto error_ret; |
| 472 | } |
| 473 | |
Jonathan Cameron | 1612244 | 2011-12-05 22:18:14 +0000 | [diff] [blame] | 474 | if (indio_dev->setup_ops->postenable) { |
| 475 | ret = indio_dev->setup_ops->postenable(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 476 | if (ret) { |
| 477 | printk(KERN_INFO |
| 478 | "Buffer not started:" |
| 479 | "postenable failed\n"); |
| 480 | if (buffer->access->unmark_in_use) |
| 481 | buffer->access->unmark_in_use(buffer); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 482 | indio_dev->currentmode = previous_mode; |
Jonathan Cameron | 1612244 | 2011-12-05 22:18:14 +0000 | [diff] [blame] | 483 | if (indio_dev->setup_ops->postdisable) |
| 484 | indio_dev->setup_ops-> |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 485 | postdisable(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 486 | goto error_ret; |
| 487 | } |
| 488 | } |
| 489 | } else { |
Jonathan Cameron | 1612244 | 2011-12-05 22:18:14 +0000 | [diff] [blame] | 490 | if (indio_dev->setup_ops->predisable) { |
| 491 | ret = indio_dev->setup_ops->predisable(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 492 | if (ret) |
| 493 | goto error_ret; |
| 494 | } |
| 495 | if (buffer->access->unmark_in_use) |
| 496 | buffer->access->unmark_in_use(buffer); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 497 | indio_dev->currentmode = INDIO_DIRECT_MODE; |
Jonathan Cameron | 1612244 | 2011-12-05 22:18:14 +0000 | [diff] [blame] | 498 | if (indio_dev->setup_ops->postdisable) { |
| 499 | ret = indio_dev->setup_ops->postdisable(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 500 | if (ret) |
| 501 | goto error_ret; |
| 502 | } |
| 503 | } |
| 504 | done: |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 505 | mutex_unlock(&indio_dev->mlock); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 506 | return len; |
| 507 | |
| 508 | error_ret: |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 509 | mutex_unlock(&indio_dev->mlock); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 510 | return ret; |
| 511 | } |
| 512 | EXPORT_SYMBOL(iio_buffer_store_enable); |
| 513 | |
| 514 | ssize_t iio_buffer_show_enable(struct device *dev, |
| 515 | struct device_attribute *attr, |
| 516 | char *buf) |
| 517 | { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 518 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Lars-Peter Clausen | d4a6882 | 2011-12-19 15:23:42 +0100 | [diff] [blame] | 519 | return sprintf(buf, "%d\n", iio_buffer_enabled(indio_dev)); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 520 | } |
| 521 | EXPORT_SYMBOL(iio_buffer_show_enable); |
| 522 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 523 | /* note NULL used as error indicator as it doesn't make sense. */ |
| 524 | static unsigned long *iio_scan_mask_match(unsigned long *av_masks, |
| 525 | unsigned int masklength, |
| 526 | unsigned long *mask) |
| 527 | { |
| 528 | if (bitmap_empty(mask, masklength)) |
| 529 | return NULL; |
| 530 | while (*av_masks) { |
| 531 | if (bitmap_subset(mask, av_masks, masklength)) |
| 532 | return av_masks; |
| 533 | av_masks += BITS_TO_LONGS(masklength); |
| 534 | } |
| 535 | return NULL; |
| 536 | } |
| 537 | |
Jonathan Cameron | 959d295 | 2011-12-05 21:37:13 +0000 | [diff] [blame] | 538 | int iio_sw_buffer_preenable(struct iio_dev *indio_dev) |
| 539 | { |
| 540 | struct iio_buffer *buffer = indio_dev->buffer; |
| 541 | const struct iio_chan_spec *ch; |
| 542 | unsigned bytes = 0; |
| 543 | int length, i; |
| 544 | dev_dbg(&indio_dev->dev, "%s\n", __func__); |
| 545 | |
| 546 | /* How much space will the demuxed element take? */ |
| 547 | for_each_set_bit(i, buffer->scan_mask, |
| 548 | indio_dev->masklength) { |
| 549 | ch = iio_find_channel_from_si(indio_dev, i); |
| 550 | length = ch->scan_type.storagebits/8; |
| 551 | bytes = ALIGN(bytes, length); |
| 552 | bytes += length; |
| 553 | } |
| 554 | if (buffer->scan_timestamp) { |
| 555 | ch = iio_find_channel_from_si(indio_dev, |
| 556 | buffer->scan_index_timestamp); |
| 557 | length = ch->scan_type.storagebits/8; |
| 558 | bytes = ALIGN(bytes, length); |
| 559 | bytes += length; |
| 560 | } |
| 561 | buffer->access->set_bytes_per_datum(buffer, bytes); |
| 562 | |
| 563 | /* What scan mask do we actually have ?*/ |
| 564 | if (indio_dev->available_scan_masks) |
| 565 | indio_dev->active_scan_mask = |
| 566 | iio_scan_mask_match(indio_dev->available_scan_masks, |
| 567 | indio_dev->masklength, |
| 568 | buffer->scan_mask); |
| 569 | else |
| 570 | indio_dev->active_scan_mask = buffer->scan_mask; |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 571 | iio_update_demux(indio_dev); |
| 572 | |
| 573 | if (indio_dev->info->update_scan_mode) |
| 574 | return indio_dev->info |
| 575 | ->update_scan_mode(indio_dev, |
| 576 | indio_dev->active_scan_mask); |
Jonathan Cameron | 959d295 | 2011-12-05 21:37:13 +0000 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | EXPORT_SYMBOL(iio_sw_buffer_preenable); |
| 580 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 581 | /** |
| 582 | * iio_scan_mask_set() - set particular bit in the scan mask |
| 583 | * @buffer: the buffer whose scan mask we are interested in |
| 584 | * @bit: the bit to be set. |
| 585 | **/ |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame] | 586 | int iio_scan_mask_set(struct iio_dev *indio_dev, |
| 587 | struct iio_buffer *buffer, int bit) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 588 | { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 589 | unsigned long *mask; |
| 590 | unsigned long *trialmask; |
| 591 | |
| 592 | trialmask = kmalloc(sizeof(*trialmask)* |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 593 | BITS_TO_LONGS(indio_dev->masklength), |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 594 | GFP_KERNEL); |
| 595 | |
| 596 | if (trialmask == NULL) |
| 597 | return -ENOMEM; |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 598 | if (!indio_dev->masklength) { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 599 | WARN_ON("trying to set scanmask prior to registering buffer\n"); |
| 600 | kfree(trialmask); |
| 601 | return -EINVAL; |
| 602 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 603 | bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 604 | set_bit(bit, trialmask); |
| 605 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 606 | if (indio_dev->available_scan_masks) { |
| 607 | mask = iio_scan_mask_match(indio_dev->available_scan_masks, |
| 608 | indio_dev->masklength, |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 609 | trialmask); |
| 610 | if (!mask) { |
| 611 | kfree(trialmask); |
| 612 | return -EINVAL; |
| 613 | } |
| 614 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 615 | bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 616 | |
| 617 | kfree(trialmask); |
| 618 | |
| 619 | return 0; |
| 620 | }; |
| 621 | EXPORT_SYMBOL_GPL(iio_scan_mask_set); |
| 622 | |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame] | 623 | int iio_scan_mask_query(struct iio_dev *indio_dev, |
| 624 | struct iio_buffer *buffer, int bit) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 625 | { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 626 | if (bit > indio_dev->masklength) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 627 | return -EINVAL; |
| 628 | |
| 629 | if (!buffer->scan_mask) |
| 630 | return 0; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 631 | |
Lars-Peter Clausen | 5a2a6e1 | 2011-12-08 18:35:53 +0100 | [diff] [blame] | 632 | return test_bit(bit, buffer->scan_mask); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 633 | }; |
| 634 | EXPORT_SYMBOL_GPL(iio_scan_mask_query); |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 635 | |
| 636 | /** |
| 637 | * struct iio_demux_table() - table describing demux memcpy ops |
| 638 | * @from: index to copy from |
| 639 | * @to: index to copy to |
| 640 | * @length: how many bytes to copy |
| 641 | * @l: list head used for management |
| 642 | */ |
| 643 | struct iio_demux_table { |
| 644 | unsigned from; |
| 645 | unsigned to; |
| 646 | unsigned length; |
| 647 | struct list_head l; |
| 648 | }; |
| 649 | |
| 650 | static unsigned char *iio_demux(struct iio_buffer *buffer, |
| 651 | unsigned char *datain) |
| 652 | { |
| 653 | struct iio_demux_table *t; |
| 654 | |
| 655 | if (list_empty(&buffer->demux_list)) |
| 656 | return datain; |
| 657 | list_for_each_entry(t, &buffer->demux_list, l) |
| 658 | memcpy(buffer->demux_bounce + t->to, |
| 659 | datain + t->from, t->length); |
| 660 | |
| 661 | return buffer->demux_bounce; |
| 662 | } |
| 663 | |
| 664 | int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data, |
| 665 | s64 timestamp) |
| 666 | { |
| 667 | unsigned char *dataout = iio_demux(buffer, data); |
| 668 | |
| 669 | return buffer->access->store_to(buffer, dataout, timestamp); |
| 670 | } |
| 671 | EXPORT_SYMBOL_GPL(iio_push_to_buffer); |
| 672 | |
| 673 | int iio_update_demux(struct iio_dev *indio_dev) |
| 674 | { |
| 675 | const struct iio_chan_spec *ch; |
| 676 | struct iio_buffer *buffer = indio_dev->buffer; |
| 677 | int ret, in_ind = -1, out_ind, length; |
| 678 | unsigned in_loc = 0, out_loc = 0; |
| 679 | struct iio_demux_table *p, *q; |
| 680 | |
| 681 | /* Clear out any old demux */ |
| 682 | list_for_each_entry_safe(p, q, &buffer->demux_list, l) { |
| 683 | list_del(&p->l); |
| 684 | kfree(p); |
| 685 | } |
| 686 | kfree(buffer->demux_bounce); |
| 687 | buffer->demux_bounce = NULL; |
| 688 | |
| 689 | /* First work out which scan mode we will actually have */ |
| 690 | if (bitmap_equal(indio_dev->active_scan_mask, |
| 691 | buffer->scan_mask, |
| 692 | indio_dev->masklength)) |
| 693 | return 0; |
| 694 | |
| 695 | /* Now we have the two masks, work from least sig and build up sizes */ |
| 696 | for_each_set_bit(out_ind, |
| 697 | indio_dev->active_scan_mask, |
| 698 | indio_dev->masklength) { |
| 699 | in_ind = find_next_bit(indio_dev->active_scan_mask, |
| 700 | indio_dev->masklength, |
| 701 | in_ind + 1); |
| 702 | while (in_ind != out_ind) { |
| 703 | in_ind = find_next_bit(indio_dev->active_scan_mask, |
| 704 | indio_dev->masklength, |
| 705 | in_ind + 1); |
| 706 | ch = iio_find_channel_from_si(indio_dev, in_ind); |
| 707 | length = ch->scan_type.storagebits/8; |
| 708 | /* Make sure we are aligned */ |
| 709 | in_loc += length; |
| 710 | if (in_loc % length) |
| 711 | in_loc += length - in_loc % length; |
| 712 | } |
| 713 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 714 | if (p == NULL) { |
| 715 | ret = -ENOMEM; |
| 716 | goto error_clear_mux_table; |
| 717 | } |
| 718 | ch = iio_find_channel_from_si(indio_dev, in_ind); |
| 719 | length = ch->scan_type.storagebits/8; |
| 720 | if (out_loc % length) |
| 721 | out_loc += length - out_loc % length; |
| 722 | if (in_loc % length) |
| 723 | in_loc += length - in_loc % length; |
| 724 | p->from = in_loc; |
| 725 | p->to = out_loc; |
| 726 | p->length = length; |
| 727 | list_add_tail(&p->l, &buffer->demux_list); |
| 728 | out_loc += length; |
| 729 | in_loc += length; |
| 730 | } |
| 731 | /* Relies on scan_timestamp being last */ |
| 732 | if (buffer->scan_timestamp) { |
| 733 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 734 | if (p == NULL) { |
| 735 | ret = -ENOMEM; |
| 736 | goto error_clear_mux_table; |
| 737 | } |
| 738 | ch = iio_find_channel_from_si(indio_dev, |
| 739 | buffer->scan_index_timestamp); |
| 740 | length = ch->scan_type.storagebits/8; |
| 741 | if (out_loc % length) |
| 742 | out_loc += length - out_loc % length; |
| 743 | if (in_loc % length) |
| 744 | in_loc += length - in_loc % length; |
| 745 | p->from = in_loc; |
| 746 | p->to = out_loc; |
| 747 | p->length = length; |
| 748 | list_add_tail(&p->l, &buffer->demux_list); |
| 749 | out_loc += length; |
| 750 | in_loc += length; |
| 751 | } |
| 752 | buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL); |
| 753 | if (buffer->demux_bounce == NULL) { |
| 754 | ret = -ENOMEM; |
| 755 | goto error_clear_mux_table; |
| 756 | } |
| 757 | return 0; |
| 758 | |
| 759 | error_clear_mux_table: |
| 760 | list_for_each_entry_safe(p, q, &buffer->demux_list, l) { |
| 761 | list_del(&p->l); |
| 762 | kfree(p); |
| 763 | } |
| 764 | return ret; |
| 765 | } |
| 766 | EXPORT_SYMBOL_GPL(iio_update_demux); |