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 | |
| 399 | if (buffer->access->set_length) { |
| 400 | buffer->access->set_length(buffer, val); |
| 401 | if (buffer->access->mark_param_change) |
| 402 | buffer->access->mark_param_change(buffer); |
| 403 | } |
| 404 | |
| 405 | return len; |
| 406 | } |
| 407 | EXPORT_SYMBOL(iio_buffer_write_length); |
| 408 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 409 | ssize_t iio_buffer_store_enable(struct device *dev, |
| 410 | struct device_attribute *attr, |
| 411 | const char *buf, |
| 412 | size_t len) |
| 413 | { |
| 414 | int ret; |
| 415 | bool requested_state, current_state; |
| 416 | int previous_mode; |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 417 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 418 | struct iio_buffer *buffer = indio_dev->buffer; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 419 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 420 | mutex_lock(&indio_dev->mlock); |
| 421 | previous_mode = indio_dev->currentmode; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 422 | requested_state = !(buf[0] == '0'); |
Lars-Peter Clausen | d4a6882 | 2011-12-19 15:23:42 +0100 | [diff] [blame] | 423 | current_state = iio_buffer_enabled(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 424 | if (current_state == requested_state) { |
| 425 | printk(KERN_INFO "iio-buffer, current state requested again\n"); |
| 426 | goto done; |
| 427 | } |
| 428 | if (requested_state) { |
Jonathan Cameron | 1612244 | 2011-12-05 22:18:14 +0000 | [diff] [blame] | 429 | if (indio_dev->setup_ops->preenable) { |
| 430 | ret = indio_dev->setup_ops->preenable(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 431 | if (ret) { |
| 432 | printk(KERN_ERR |
| 433 | "Buffer not started:" |
| 434 | "buffer preenable failed\n"); |
| 435 | goto error_ret; |
| 436 | } |
| 437 | } |
| 438 | if (buffer->access->request_update) { |
| 439 | ret = buffer->access->request_update(buffer); |
| 440 | if (ret) { |
| 441 | printk(KERN_INFO |
| 442 | "Buffer not started:" |
| 443 | "buffer parameter update failed\n"); |
| 444 | goto error_ret; |
| 445 | } |
| 446 | } |
| 447 | if (buffer->access->mark_in_use) |
| 448 | buffer->access->mark_in_use(buffer); |
| 449 | /* Definitely possible for devices to support both of these.*/ |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 450 | if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) { |
| 451 | if (!indio_dev->trig) { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 452 | printk(KERN_INFO |
| 453 | "Buffer not started: no trigger\n"); |
| 454 | ret = -EINVAL; |
| 455 | if (buffer->access->unmark_in_use) |
| 456 | buffer->access->unmark_in_use(buffer); |
| 457 | goto error_ret; |
| 458 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 459 | indio_dev->currentmode = INDIO_BUFFER_TRIGGERED; |
| 460 | } else if (indio_dev->modes & INDIO_BUFFER_HARDWARE) |
| 461 | indio_dev->currentmode = INDIO_BUFFER_HARDWARE; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 462 | else { /* should never be reached */ |
| 463 | ret = -EINVAL; |
| 464 | goto error_ret; |
| 465 | } |
| 466 | |
Jonathan Cameron | 1612244 | 2011-12-05 22:18:14 +0000 | [diff] [blame] | 467 | if (indio_dev->setup_ops->postenable) { |
| 468 | ret = indio_dev->setup_ops->postenable(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 469 | if (ret) { |
| 470 | printk(KERN_INFO |
| 471 | "Buffer not started:" |
| 472 | "postenable failed\n"); |
| 473 | if (buffer->access->unmark_in_use) |
| 474 | buffer->access->unmark_in_use(buffer); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 475 | indio_dev->currentmode = previous_mode; |
Jonathan Cameron | 1612244 | 2011-12-05 22:18:14 +0000 | [diff] [blame] | 476 | if (indio_dev->setup_ops->postdisable) |
| 477 | indio_dev->setup_ops-> |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 478 | postdisable(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 479 | goto error_ret; |
| 480 | } |
| 481 | } |
| 482 | } else { |
Jonathan Cameron | 1612244 | 2011-12-05 22:18:14 +0000 | [diff] [blame] | 483 | if (indio_dev->setup_ops->predisable) { |
| 484 | ret = indio_dev->setup_ops->predisable(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 485 | if (ret) |
| 486 | goto error_ret; |
| 487 | } |
| 488 | if (buffer->access->unmark_in_use) |
| 489 | buffer->access->unmark_in_use(buffer); |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 490 | indio_dev->currentmode = INDIO_DIRECT_MODE; |
Jonathan Cameron | 1612244 | 2011-12-05 22:18:14 +0000 | [diff] [blame] | 491 | if (indio_dev->setup_ops->postdisable) { |
| 492 | ret = indio_dev->setup_ops->postdisable(indio_dev); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 493 | if (ret) |
| 494 | goto error_ret; |
| 495 | } |
| 496 | } |
| 497 | done: |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 498 | mutex_unlock(&indio_dev->mlock); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 499 | return len; |
| 500 | |
| 501 | error_ret: |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 502 | mutex_unlock(&indio_dev->mlock); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 503 | return ret; |
| 504 | } |
| 505 | EXPORT_SYMBOL(iio_buffer_store_enable); |
| 506 | |
| 507 | ssize_t iio_buffer_show_enable(struct device *dev, |
| 508 | struct device_attribute *attr, |
| 509 | char *buf) |
| 510 | { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 511 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
Lars-Peter Clausen | d4a6882 | 2011-12-19 15:23:42 +0100 | [diff] [blame] | 512 | return sprintf(buf, "%d\n", iio_buffer_enabled(indio_dev)); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 513 | } |
| 514 | EXPORT_SYMBOL(iio_buffer_show_enable); |
| 515 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 516 | /* note NULL used as error indicator as it doesn't make sense. */ |
| 517 | static unsigned long *iio_scan_mask_match(unsigned long *av_masks, |
| 518 | unsigned int masklength, |
| 519 | unsigned long *mask) |
| 520 | { |
| 521 | if (bitmap_empty(mask, masklength)) |
| 522 | return NULL; |
| 523 | while (*av_masks) { |
| 524 | if (bitmap_subset(mask, av_masks, masklength)) |
| 525 | return av_masks; |
| 526 | av_masks += BITS_TO_LONGS(masklength); |
| 527 | } |
| 528 | return NULL; |
| 529 | } |
| 530 | |
Jonathan Cameron | 959d295 | 2011-12-05 21:37:13 +0000 | [diff] [blame] | 531 | int iio_sw_buffer_preenable(struct iio_dev *indio_dev) |
| 532 | { |
| 533 | struct iio_buffer *buffer = indio_dev->buffer; |
| 534 | const struct iio_chan_spec *ch; |
| 535 | unsigned bytes = 0; |
| 536 | int length, i; |
| 537 | dev_dbg(&indio_dev->dev, "%s\n", __func__); |
| 538 | |
| 539 | /* How much space will the demuxed element take? */ |
| 540 | for_each_set_bit(i, buffer->scan_mask, |
| 541 | indio_dev->masklength) { |
| 542 | ch = iio_find_channel_from_si(indio_dev, i); |
| 543 | length = ch->scan_type.storagebits/8; |
| 544 | bytes = ALIGN(bytes, length); |
| 545 | bytes += length; |
| 546 | } |
| 547 | if (buffer->scan_timestamp) { |
| 548 | ch = iio_find_channel_from_si(indio_dev, |
| 549 | buffer->scan_index_timestamp); |
| 550 | length = ch->scan_type.storagebits/8; |
| 551 | bytes = ALIGN(bytes, length); |
| 552 | bytes += length; |
| 553 | } |
| 554 | buffer->access->set_bytes_per_datum(buffer, bytes); |
| 555 | |
| 556 | /* What scan mask do we actually have ?*/ |
| 557 | if (indio_dev->available_scan_masks) |
| 558 | indio_dev->active_scan_mask = |
| 559 | iio_scan_mask_match(indio_dev->available_scan_masks, |
| 560 | indio_dev->masklength, |
| 561 | buffer->scan_mask); |
| 562 | else |
| 563 | indio_dev->active_scan_mask = buffer->scan_mask; |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 564 | iio_update_demux(indio_dev); |
| 565 | |
| 566 | if (indio_dev->info->update_scan_mode) |
| 567 | return indio_dev->info |
| 568 | ->update_scan_mode(indio_dev, |
| 569 | indio_dev->active_scan_mask); |
Jonathan Cameron | 959d295 | 2011-12-05 21:37:13 +0000 | [diff] [blame] | 570 | return 0; |
| 571 | } |
| 572 | EXPORT_SYMBOL(iio_sw_buffer_preenable); |
| 573 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 574 | /** |
| 575 | * iio_scan_mask_set() - set particular bit in the scan mask |
| 576 | * @buffer: the buffer whose scan mask we are interested in |
| 577 | * @bit: the bit to be set. |
| 578 | **/ |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame] | 579 | int iio_scan_mask_set(struct iio_dev *indio_dev, |
| 580 | struct iio_buffer *buffer, int bit) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 581 | { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 582 | unsigned long *mask; |
| 583 | unsigned long *trialmask; |
| 584 | |
| 585 | trialmask = kmalloc(sizeof(*trialmask)* |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 586 | BITS_TO_LONGS(indio_dev->masklength), |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 587 | GFP_KERNEL); |
| 588 | |
| 589 | if (trialmask == NULL) |
| 590 | return -ENOMEM; |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 591 | if (!indio_dev->masklength) { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 592 | WARN_ON("trying to set scanmask prior to registering buffer\n"); |
| 593 | kfree(trialmask); |
| 594 | return -EINVAL; |
| 595 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 596 | bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 597 | set_bit(bit, trialmask); |
| 598 | |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 599 | if (indio_dev->available_scan_masks) { |
| 600 | mask = iio_scan_mask_match(indio_dev->available_scan_masks, |
| 601 | indio_dev->masklength, |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 602 | trialmask); |
| 603 | if (!mask) { |
| 604 | kfree(trialmask); |
| 605 | return -EINVAL; |
| 606 | } |
| 607 | } |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 608 | bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 609 | |
| 610 | kfree(trialmask); |
| 611 | |
| 612 | return 0; |
| 613 | }; |
| 614 | EXPORT_SYMBOL_GPL(iio_scan_mask_set); |
| 615 | |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame] | 616 | int iio_scan_mask_query(struct iio_dev *indio_dev, |
| 617 | struct iio_buffer *buffer, int bit) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 618 | { |
Jonathan Cameron | f8c6f4e | 2011-10-06 17:14:35 +0100 | [diff] [blame] | 619 | if (bit > indio_dev->masklength) |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 620 | return -EINVAL; |
| 621 | |
| 622 | if (!buffer->scan_mask) |
| 623 | return 0; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 624 | |
Lars-Peter Clausen | 5a2a6e1 | 2011-12-08 18:35:53 +0100 | [diff] [blame] | 625 | return test_bit(bit, buffer->scan_mask); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 626 | }; |
| 627 | EXPORT_SYMBOL_GPL(iio_scan_mask_query); |
Jonathan Cameron | 5ada4ea | 2011-12-05 21:37:14 +0000 | [diff] [blame] | 628 | |
| 629 | /** |
| 630 | * struct iio_demux_table() - table describing demux memcpy ops |
| 631 | * @from: index to copy from |
| 632 | * @to: index to copy to |
| 633 | * @length: how many bytes to copy |
| 634 | * @l: list head used for management |
| 635 | */ |
| 636 | struct iio_demux_table { |
| 637 | unsigned from; |
| 638 | unsigned to; |
| 639 | unsigned length; |
| 640 | struct list_head l; |
| 641 | }; |
| 642 | |
| 643 | static unsigned char *iio_demux(struct iio_buffer *buffer, |
| 644 | unsigned char *datain) |
| 645 | { |
| 646 | struct iio_demux_table *t; |
| 647 | |
| 648 | if (list_empty(&buffer->demux_list)) |
| 649 | return datain; |
| 650 | list_for_each_entry(t, &buffer->demux_list, l) |
| 651 | memcpy(buffer->demux_bounce + t->to, |
| 652 | datain + t->from, t->length); |
| 653 | |
| 654 | return buffer->demux_bounce; |
| 655 | } |
| 656 | |
| 657 | int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data, |
| 658 | s64 timestamp) |
| 659 | { |
| 660 | unsigned char *dataout = iio_demux(buffer, data); |
| 661 | |
| 662 | return buffer->access->store_to(buffer, dataout, timestamp); |
| 663 | } |
| 664 | EXPORT_SYMBOL_GPL(iio_push_to_buffer); |
| 665 | |
| 666 | int iio_update_demux(struct iio_dev *indio_dev) |
| 667 | { |
| 668 | const struct iio_chan_spec *ch; |
| 669 | struct iio_buffer *buffer = indio_dev->buffer; |
| 670 | int ret, in_ind = -1, out_ind, length; |
| 671 | unsigned in_loc = 0, out_loc = 0; |
| 672 | struct iio_demux_table *p, *q; |
| 673 | |
| 674 | /* Clear out any old demux */ |
| 675 | list_for_each_entry_safe(p, q, &buffer->demux_list, l) { |
| 676 | list_del(&p->l); |
| 677 | kfree(p); |
| 678 | } |
| 679 | kfree(buffer->demux_bounce); |
| 680 | buffer->demux_bounce = NULL; |
| 681 | |
| 682 | /* First work out which scan mode we will actually have */ |
| 683 | if (bitmap_equal(indio_dev->active_scan_mask, |
| 684 | buffer->scan_mask, |
| 685 | indio_dev->masklength)) |
| 686 | return 0; |
| 687 | |
| 688 | /* Now we have the two masks, work from least sig and build up sizes */ |
| 689 | for_each_set_bit(out_ind, |
| 690 | indio_dev->active_scan_mask, |
| 691 | indio_dev->masklength) { |
| 692 | in_ind = find_next_bit(indio_dev->active_scan_mask, |
| 693 | indio_dev->masklength, |
| 694 | in_ind + 1); |
| 695 | while (in_ind != out_ind) { |
| 696 | in_ind = find_next_bit(indio_dev->active_scan_mask, |
| 697 | indio_dev->masklength, |
| 698 | in_ind + 1); |
| 699 | ch = iio_find_channel_from_si(indio_dev, in_ind); |
| 700 | length = ch->scan_type.storagebits/8; |
| 701 | /* Make sure we are aligned */ |
| 702 | in_loc += length; |
| 703 | if (in_loc % length) |
| 704 | in_loc += length - in_loc % length; |
| 705 | } |
| 706 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 707 | if (p == NULL) { |
| 708 | ret = -ENOMEM; |
| 709 | goto error_clear_mux_table; |
| 710 | } |
| 711 | ch = iio_find_channel_from_si(indio_dev, in_ind); |
| 712 | length = ch->scan_type.storagebits/8; |
| 713 | if (out_loc % length) |
| 714 | out_loc += length - out_loc % length; |
| 715 | if (in_loc % length) |
| 716 | in_loc += length - in_loc % length; |
| 717 | p->from = in_loc; |
| 718 | p->to = out_loc; |
| 719 | p->length = length; |
| 720 | list_add_tail(&p->l, &buffer->demux_list); |
| 721 | out_loc += length; |
| 722 | in_loc += length; |
| 723 | } |
| 724 | /* Relies on scan_timestamp being last */ |
| 725 | if (buffer->scan_timestamp) { |
| 726 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
| 727 | if (p == NULL) { |
| 728 | ret = -ENOMEM; |
| 729 | goto error_clear_mux_table; |
| 730 | } |
| 731 | ch = iio_find_channel_from_si(indio_dev, |
| 732 | buffer->scan_index_timestamp); |
| 733 | length = ch->scan_type.storagebits/8; |
| 734 | if (out_loc % length) |
| 735 | out_loc += length - out_loc % length; |
| 736 | if (in_loc % length) |
| 737 | in_loc += length - in_loc % length; |
| 738 | p->from = in_loc; |
| 739 | p->to = out_loc; |
| 740 | p->length = length; |
| 741 | list_add_tail(&p->l, &buffer->demux_list); |
| 742 | out_loc += length; |
| 743 | in_loc += length; |
| 744 | } |
| 745 | buffer->demux_bounce = kzalloc(out_loc, GFP_KERNEL); |
| 746 | if (buffer->demux_bounce == NULL) { |
| 747 | ret = -ENOMEM; |
| 748 | goto error_clear_mux_table; |
| 749 | } |
| 750 | return 0; |
| 751 | |
| 752 | error_clear_mux_table: |
| 753 | list_for_each_entry_safe(p, q, &buffer->demux_list, l) { |
| 754 | list_del(&p->l); |
| 755 | kfree(p); |
| 756 | } |
| 757 | return ret; |
| 758 | } |
| 759 | EXPORT_SYMBOL_GPL(iio_update_demux); |