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