Commit | Line | Data |
---|---|---|
7026ea4b JC |
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 ring 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> | |
17 | #include <linux/device.h> | |
7026ea4b | 18 | #include <linux/fs.h> |
7026ea4b | 19 | #include <linux/cdev.h> |
5a0e3ad6 | 20 | #include <linux/slab.h> |
a7348347 | 21 | #include <linux/poll.h> |
7026ea4b JC |
22 | |
23 | #include "iio.h" | |
24 | #include "ring_generic.h" | |
25 | ||
7026ea4b | 26 | /** |
c3e5d410 | 27 | * iio_ring_open() - chrdev file open for ring buffer access |
7026ea4b JC |
28 | * |
29 | * This function relies on all ring buffer implementations having an | |
30 | * iio_ring_buffer as their first element. | |
31 | **/ | |
0deebb4f | 32 | static int iio_ring_open(struct inode *inode, struct file *filp) |
7026ea4b JC |
33 | { |
34 | struct iio_handler *hand | |
35 | = container_of(inode->i_cdev, struct iio_handler, chrdev); | |
36 | struct iio_ring_buffer *rb = hand->private; | |
37 | ||
38 | filp->private_data = hand->private; | |
5565a450 JC |
39 | if (rb->access->mark_in_use) |
40 | rb->access->mark_in_use(rb); | |
7026ea4b JC |
41 | |
42 | return 0; | |
43 | } | |
44 | ||
45 | /** | |
c3e5d410 | 46 | * iio_ring_release() - chrdev file close ring buffer access |
7026ea4b JC |
47 | * |
48 | * This function relies on all ring buffer implementations having an | |
49 | * iio_ring_buffer as their first element. | |
50 | **/ | |
0deebb4f | 51 | static int iio_ring_release(struct inode *inode, struct file *filp) |
7026ea4b JC |
52 | { |
53 | struct cdev *cd = inode->i_cdev; | |
54 | struct iio_handler *hand = iio_cdev_to_handler(cd); | |
55 | struct iio_ring_buffer *rb = hand->private; | |
56 | ||
57 | clear_bit(IIO_BUSY_BIT_POS, &rb->access_handler.flags); | |
5565a450 JC |
58 | if (rb->access->unmark_in_use) |
59 | rb->access->unmark_in_use(rb); | |
7026ea4b JC |
60 | |
61 | return 0; | |
62 | } | |
63 | ||
64 | /** | |
b4281733 | 65 | * iio_ring_read_first_n_outer() - chrdev read for ring buffer access |
7026ea4b JC |
66 | * |
67 | * This function relies on all ring buffer implementations having an | |
68 | * iio_ring _bufer as their first element. | |
69 | **/ | |
b4281733 JC |
70 | static ssize_t iio_ring_read_first_n_outer(struct file *filp, char __user *buf, |
71 | size_t n, loff_t *f_ps) | |
7026ea4b JC |
72 | { |
73 | struct iio_ring_buffer *rb = filp->private_data; | |
d5857d65 | 74 | |
5565a450 | 75 | if (!rb->access->read_first_n) |
7026ea4b | 76 | return -EINVAL; |
8d213f24 | 77 | return rb->access->read_first_n(rb, n, buf); |
7026ea4b JC |
78 | } |
79 | ||
a7348347 JC |
80 | /** |
81 | * iio_ring_poll() - poll the ring to find out if it has data | |
82 | */ | |
83 | static unsigned int iio_ring_poll(struct file *filp, | |
84 | struct poll_table_struct *wait) | |
85 | { | |
86 | struct iio_ring_buffer *rb = filp->private_data; | |
a7348347 JC |
87 | |
88 | poll_wait(filp, &rb->pollq, wait); | |
89 | if (rb->stufftoread) | |
90 | return POLLIN | POLLRDNORM; | |
91 | /* need a way of knowing if there may be enough data... */ | |
8d213f24 | 92 | return 0; |
a7348347 JC |
93 | } |
94 | ||
7026ea4b | 95 | static const struct file_operations iio_ring_fileops = { |
b4281733 | 96 | .read = iio_ring_read_first_n_outer, |
7026ea4b JC |
97 | .release = iio_ring_release, |
98 | .open = iio_ring_open, | |
a7348347 | 99 | .poll = iio_ring_poll, |
7026ea4b | 100 | .owner = THIS_MODULE, |
6038f373 | 101 | .llseek = noop_llseek, |
7026ea4b JC |
102 | }; |
103 | ||
3feb0797 | 104 | void iio_ring_access_release(struct device *dev) |
7026ea4b JC |
105 | { |
106 | struct iio_ring_buffer *buf | |
3feb0797 | 107 | = container_of(dev, struct iio_ring_buffer, dev); |
7026ea4b JC |
108 | cdev_del(&buf->access_handler.chrdev); |
109 | iio_device_free_chrdev_minor(MINOR(dev->devt)); | |
110 | } | |
3feb0797 | 111 | EXPORT_SYMBOL(iio_ring_access_release); |
7026ea4b JC |
112 | |
113 | static inline int | |
3feb0797 | 114 | __iio_request_ring_buffer_chrdev(struct iio_ring_buffer *buf, |
8d213f24 JC |
115 | struct module *owner, |
116 | int id) | |
7026ea4b | 117 | { |
8d213f24 | 118 | int ret; |
7026ea4b JC |
119 | |
120 | buf->access_handler.flags = 0; | |
3feb0797 JC |
121 | buf->dev.bus = &iio_bus_type; |
122 | device_initialize(&buf->dev); | |
7026ea4b | 123 | |
8d213f24 JC |
124 | ret = iio_device_get_chrdev_minor(); |
125 | if (ret < 0) | |
7026ea4b | 126 | goto error_device_put; |
8d213f24 JC |
127 | |
128 | buf->dev.devt = MKDEV(MAJOR(iio_devt), ret); | |
3feb0797 JC |
129 | dev_set_name(&buf->dev, "%s:buffer%d", |
130 | dev_name(buf->dev.parent), | |
8d213f24 | 131 | id); |
3feb0797 | 132 | ret = device_add(&buf->dev); |
7026ea4b | 133 | if (ret < 0) { |
3feb0797 | 134 | printk(KERN_ERR "failed to add the ring dev\n"); |
758d988c | 135 | goto error_device_put; |
7026ea4b | 136 | } |
7026ea4b JC |
137 | cdev_init(&buf->access_handler.chrdev, &iio_ring_fileops); |
138 | buf->access_handler.chrdev.owner = owner; | |
3feb0797 | 139 | ret = cdev_add(&buf->access_handler.chrdev, buf->dev.devt, 1); |
7026ea4b | 140 | if (ret) { |
3feb0797 | 141 | printk(KERN_ERR "failed to allocate ring chrdev\n"); |
7026ea4b JC |
142 | goto error_device_unregister; |
143 | } | |
144 | return 0; | |
758d988c | 145 | |
7026ea4b | 146 | error_device_unregister: |
3feb0797 | 147 | device_unregister(&buf->dev); |
7026ea4b | 148 | error_device_put: |
3feb0797 | 149 | put_device(&buf->dev); |
7026ea4b JC |
150 | |
151 | return ret; | |
152 | } | |
153 | ||
3feb0797 | 154 | static void __iio_free_ring_buffer_chrdev(struct iio_ring_buffer *buf) |
7026ea4b | 155 | { |
3feb0797 | 156 | device_unregister(&buf->dev); |
7026ea4b JC |
157 | } |
158 | ||
159 | void iio_ring_buffer_init(struct iio_ring_buffer *ring, | |
160 | struct iio_dev *dev_info) | |
161 | { | |
7026ea4b | 162 | ring->indio_dev = dev_info; |
7026ea4b | 163 | ring->access_handler.private = ring; |
a7348347 | 164 | init_waitqueue_head(&ring->pollq); |
7026ea4b JC |
165 | } |
166 | EXPORT_SYMBOL(iio_ring_buffer_init); | |
167 | ||
1d892719 | 168 | static ssize_t iio_show_scan_index(struct device *dev, |
8d213f24 JC |
169 | struct device_attribute *attr, |
170 | char *buf) | |
1d892719 | 171 | { |
8d213f24 | 172 | return sprintf(buf, "%u\n", to_iio_dev_attr(attr)->c->scan_index); |
1d892719 JC |
173 | } |
174 | ||
175 | static ssize_t iio_show_fixed_type(struct device *dev, | |
176 | struct device_attribute *attr, | |
177 | char *buf) | |
178 | { | |
179 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
180 | return sprintf(buf, "%c%d/%d>>%u\n", | |
181 | this_attr->c->scan_type.sign, | |
182 | this_attr->c->scan_type.realbits, | |
183 | this_attr->c->scan_type.storagebits, | |
184 | this_attr->c->scan_type.shift); | |
185 | } | |
186 | ||
8d213f24 JC |
187 | static ssize_t iio_scan_el_show(struct device *dev, |
188 | struct device_attribute *attr, | |
189 | char *buf) | |
190 | { | |
191 | int ret; | |
192 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
193 | ||
194 | ret = iio_scan_mask_query(ring, to_iio_dev_attr(attr)->address); | |
195 | if (ret < 0) | |
196 | return ret; | |
197 | return sprintf(buf, "%d\n", ret); | |
198 | } | |
199 | ||
200 | static int iio_scan_mask_clear(struct iio_ring_buffer *ring, int bit) | |
201 | { | |
202 | if (bit > IIO_MAX_SCAN_LENGTH) | |
203 | return -EINVAL; | |
204 | ring->scan_mask &= ~(1 << bit); | |
205 | ring->scan_count--; | |
206 | return 0; | |
207 | } | |
208 | ||
209 | static ssize_t iio_scan_el_store(struct device *dev, | |
210 | struct device_attribute *attr, | |
211 | const char *buf, | |
212 | size_t len) | |
213 | { | |
214 | int ret = 0; | |
215 | bool state; | |
216 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
217 | struct iio_dev *indio_dev = ring->indio_dev; | |
218 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
219 | ||
220 | state = !(buf[0] == '0'); | |
221 | mutex_lock(&indio_dev->mlock); | |
222 | if (indio_dev->currentmode == INDIO_RING_TRIGGERED) { | |
223 | ret = -EBUSY; | |
224 | goto error_ret; | |
225 | } | |
226 | ret = iio_scan_mask_query(ring, this_attr->address); | |
227 | if (ret < 0) | |
228 | goto error_ret; | |
229 | if (!state && ret) { | |
230 | ret = iio_scan_mask_clear(ring, this_attr->address); | |
231 | if (ret) | |
232 | goto error_ret; | |
233 | } else if (state && !ret) { | |
234 | ret = iio_scan_mask_set(ring, this_attr->address); | |
235 | if (ret) | |
236 | goto error_ret; | |
237 | } | |
238 | ||
239 | error_ret: | |
240 | mutex_unlock(&indio_dev->mlock); | |
241 | ||
242 | return ret ? ret : len; | |
243 | ||
244 | } | |
245 | ||
246 | static ssize_t iio_scan_el_ts_show(struct device *dev, | |
247 | struct device_attribute *attr, | |
248 | char *buf) | |
249 | { | |
250 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
251 | return sprintf(buf, "%d\n", ring->scan_timestamp); | |
252 | } | |
253 | ||
254 | static ssize_t iio_scan_el_ts_store(struct device *dev, | |
255 | struct device_attribute *attr, | |
256 | const char *buf, | |
257 | size_t len) | |
258 | { | |
259 | int ret = 0; | |
260 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
261 | struct iio_dev *indio_dev = ring->indio_dev; | |
262 | bool state; | |
263 | state = !(buf[0] == '0'); | |
264 | mutex_lock(&indio_dev->mlock); | |
265 | if (indio_dev->currentmode == INDIO_RING_TRIGGERED) { | |
266 | ret = -EBUSY; | |
267 | goto error_ret; | |
268 | } | |
269 | ring->scan_timestamp = state; | |
270 | error_ret: | |
271 | mutex_unlock(&indio_dev->mlock); | |
272 | ||
273 | return ret ? ret : len; | |
274 | } | |
275 | ||
1d892719 JC |
276 | static int iio_ring_add_channel_sysfs(struct iio_ring_buffer *ring, |
277 | const struct iio_chan_spec *chan) | |
278 | { | |
279 | int ret; | |
280 | ||
281 | ret = __iio_add_chan_devattr("index", "scan_elements", | |
282 | chan, | |
283 | &iio_show_scan_index, | |
284 | NULL, | |
285 | 0, | |
286 | 0, | |
287 | &ring->dev, | |
288 | &ring->scan_el_dev_attr_list); | |
289 | if (ret) | |
290 | goto error_ret; | |
291 | ||
292 | ret = __iio_add_chan_devattr("type", "scan_elements", | |
293 | chan, | |
294 | &iio_show_fixed_type, | |
295 | NULL, | |
296 | 0, | |
297 | 0, | |
298 | &ring->dev, | |
299 | &ring->scan_el_dev_attr_list); | |
1d892719 JC |
300 | if (ret) |
301 | goto error_ret; | |
302 | ||
a88b3ebc JC |
303 | if (chan->type != IIO_TIMESTAMP) |
304 | ret = __iio_add_chan_devattr("en", "scan_elements", | |
305 | chan, | |
306 | &iio_scan_el_show, | |
307 | &iio_scan_el_store, | |
308 | chan->scan_index, | |
309 | 0, | |
310 | &ring->dev, | |
311 | &ring->scan_el_dev_attr_list); | |
312 | else | |
313 | ret = __iio_add_chan_devattr("en", "scan_elements", | |
314 | chan, | |
315 | &iio_scan_el_ts_show, | |
316 | &iio_scan_el_ts_store, | |
317 | chan->scan_index, | |
318 | 0, | |
319 | &ring->dev, | |
320 | &ring->scan_el_dev_attr_list); | |
1d892719 JC |
321 | error_ret: |
322 | return ret; | |
323 | } | |
324 | ||
1d892719 JC |
325 | static void iio_ring_remove_and_free_scan_dev_attr(struct iio_ring_buffer *ring, |
326 | struct iio_dev_attr *p) | |
327 | { | |
328 | sysfs_remove_file_from_group(&ring->dev.kobj, | |
329 | &p->dev_attr.attr, "scan_elements"); | |
330 | kfree(p->dev_attr.attr.name); | |
331 | kfree(p); | |
332 | } | |
333 | ||
334 | static struct attribute *iio_scan_el_dummy_attrs[] = { | |
335 | NULL | |
336 | }; | |
337 | ||
338 | static struct attribute_group iio_scan_el_dummy_group = { | |
339 | .name = "scan_elements", | |
340 | .attrs = iio_scan_el_dummy_attrs | |
341 | }; | |
342 | ||
343 | static void __iio_ring_attr_cleanup(struct iio_ring_buffer *ring) | |
344 | { | |
345 | struct iio_dev_attr *p, *n; | |
a88b3ebc | 346 | int anydynamic = !list_empty(&ring->scan_el_dev_attr_list); |
1d892719 JC |
347 | list_for_each_entry_safe(p, n, |
348 | &ring->scan_el_dev_attr_list, l) | |
349 | iio_ring_remove_and_free_scan_dev_attr(ring, p); | |
1d892719 JC |
350 | |
351 | if (ring->scan_el_attrs) | |
352 | sysfs_remove_group(&ring->dev.kobj, | |
353 | ring->scan_el_attrs); | |
354 | else if (anydynamic) | |
355 | sysfs_remove_group(&ring->dev.kobj, | |
356 | &iio_scan_el_dummy_group); | |
357 | } | |
358 | ||
359 | int iio_ring_buffer_register_ex(struct iio_ring_buffer *ring, int id, | |
360 | const struct iio_chan_spec *channels, | |
361 | int num_channels) | |
362 | { | |
363 | int ret, i; | |
758d988c | 364 | |
8d213f24 | 365 | ret = __iio_request_ring_buffer_chrdev(ring, ring->owner, id); |
7026ea4b | 366 | if (ret) |
3feb0797 | 367 | goto error_ret; |
8d213f24 | 368 | |
bf32963c MS |
369 | if (ring->scan_el_attrs) { |
370 | ret = sysfs_create_group(&ring->dev.kobj, | |
371 | ring->scan_el_attrs); | |
372 | if (ret) { | |
373 | dev_err(&ring->dev, | |
374 | "Failed to add sysfs scan elements\n"); | |
3feb0797 | 375 | goto error_free_ring_buffer_chrdev; |
bf32963c | 376 | } |
1d892719 JC |
377 | } else if (channels) { |
378 | ret = sysfs_create_group(&ring->dev.kobj, | |
379 | &iio_scan_el_dummy_group); | |
380 | if (ret) | |
3feb0797 | 381 | goto error_free_ring_buffer_chrdev; |
bf32963c MS |
382 | } |
383 | ||
1d892719 | 384 | INIT_LIST_HEAD(&ring->scan_el_dev_attr_list); |
1d892719 JC |
385 | if (channels) { |
386 | /* new magic */ | |
387 | for (i = 0; i < num_channels; i++) { | |
388 | ret = iio_ring_add_channel_sysfs(ring, &channels[i]); | |
389 | if (ret < 0) | |
390 | goto error_cleanup_dynamic; | |
391 | } | |
392 | } | |
393 | ||
394 | return 0; | |
395 | error_cleanup_dynamic: | |
396 | __iio_ring_attr_cleanup(ring); | |
3feb0797 JC |
397 | error_free_ring_buffer_chrdev: |
398 | __iio_free_ring_buffer_chrdev(ring); | |
7026ea4b JC |
399 | error_ret: |
400 | return ret; | |
401 | } | |
1d892719 JC |
402 | EXPORT_SYMBOL(iio_ring_buffer_register_ex); |
403 | ||
7026ea4b JC |
404 | void iio_ring_buffer_unregister(struct iio_ring_buffer *ring) |
405 | { | |
1d892719 | 406 | __iio_ring_attr_cleanup(ring); |
3feb0797 | 407 | __iio_free_ring_buffer_chrdev(ring); |
7026ea4b JC |
408 | } |
409 | EXPORT_SYMBOL(iio_ring_buffer_unregister); | |
410 | ||
411 | ssize_t iio_read_ring_length(struct device *dev, | |
412 | struct device_attribute *attr, | |
413 | char *buf) | |
414 | { | |
7026ea4b JC |
415 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); |
416 | ||
5565a450 | 417 | if (ring->access->get_length) |
8d213f24 JC |
418 | return sprintf(buf, "%d\n", |
419 | ring->access->get_length(ring)); | |
7026ea4b | 420 | |
8d213f24 | 421 | return 0; |
7026ea4b JC |
422 | } |
423 | EXPORT_SYMBOL(iio_read_ring_length); | |
424 | ||
0abd2428 | 425 | ssize_t iio_write_ring_length(struct device *dev, |
7026ea4b JC |
426 | struct device_attribute *attr, |
427 | const char *buf, | |
428 | size_t len) | |
429 | { | |
430 | int ret; | |
431 | ulong val; | |
432 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
8d213f24 | 433 | |
7026ea4b JC |
434 | ret = strict_strtoul(buf, 10, &val); |
435 | if (ret) | |
436 | return ret; | |
437 | ||
5565a450 JC |
438 | if (ring->access->get_length) |
439 | if (val == ring->access->get_length(ring)) | |
7026ea4b JC |
440 | return len; |
441 | ||
5565a450 JC |
442 | if (ring->access->set_length) { |
443 | ring->access->set_length(ring, val); | |
444 | if (ring->access->mark_param_change) | |
445 | ring->access->mark_param_change(ring); | |
7026ea4b JC |
446 | } |
447 | ||
448 | return len; | |
449 | } | |
450 | EXPORT_SYMBOL(iio_write_ring_length); | |
451 | ||
ffcab07a | 452 | ssize_t iio_read_ring_bytes_per_datum(struct device *dev, |
7026ea4b JC |
453 | struct device_attribute *attr, |
454 | char *buf) | |
455 | { | |
7026ea4b JC |
456 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); |
457 | ||
5565a450 | 458 | if (ring->access->get_bytes_per_datum) |
8d213f24 JC |
459 | return sprintf(buf, "%d\n", |
460 | ring->access->get_bytes_per_datum(ring)); | |
7026ea4b | 461 | |
8d213f24 | 462 | return 0; |
7026ea4b | 463 | } |
ffcab07a | 464 | EXPORT_SYMBOL(iio_read_ring_bytes_per_datum); |
7026ea4b JC |
465 | |
466 | ssize_t iio_store_ring_enable(struct device *dev, | |
467 | struct device_attribute *attr, | |
468 | const char *buf, | |
469 | size_t len) | |
470 | { | |
471 | int ret; | |
472 | bool requested_state, current_state; | |
473 | int previous_mode; | |
474 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
475 | struct iio_dev *dev_info = ring->indio_dev; | |
476 | ||
477 | mutex_lock(&dev_info->mlock); | |
478 | previous_mode = dev_info->currentmode; | |
479 | requested_state = !(buf[0] == '0'); | |
480 | current_state = !!(previous_mode & INDIO_ALL_RING_MODES); | |
481 | if (current_state == requested_state) { | |
482 | printk(KERN_INFO "iio-ring, current state requested again\n"); | |
483 | goto done; | |
484 | } | |
485 | if (requested_state) { | |
5565a450 JC |
486 | if (ring->setup_ops->preenable) { |
487 | ret = ring->setup_ops->preenable(dev_info); | |
7026ea4b JC |
488 | if (ret) { |
489 | printk(KERN_ERR | |
490 | "Buffer not started:" | |
491 | "ring preenable failed\n"); | |
492 | goto error_ret; | |
493 | } | |
494 | } | |
5565a450 JC |
495 | if (ring->access->request_update) { |
496 | ret = ring->access->request_update(ring); | |
7026ea4b JC |
497 | if (ret) { |
498 | printk(KERN_INFO | |
499 | "Buffer not started:" | |
500 | "ring parameter update failed\n"); | |
501 | goto error_ret; | |
502 | } | |
503 | } | |
5565a450 JC |
504 | if (ring->access->mark_in_use) |
505 | ring->access->mark_in_use(ring); | |
7026ea4b JC |
506 | /* Definitely possible for devices to support both of these.*/ |
507 | if (dev_info->modes & INDIO_RING_TRIGGERED) { | |
508 | if (!dev_info->trig) { | |
509 | printk(KERN_INFO | |
510 | "Buffer not started: no trigger\n"); | |
511 | ret = -EINVAL; | |
5565a450 JC |
512 | if (ring->access->unmark_in_use) |
513 | ring->access->unmark_in_use(ring); | |
7026ea4b JC |
514 | goto error_ret; |
515 | } | |
516 | dev_info->currentmode = INDIO_RING_TRIGGERED; | |
517 | } else if (dev_info->modes & INDIO_RING_HARDWARE_BUFFER) | |
518 | dev_info->currentmode = INDIO_RING_HARDWARE_BUFFER; | |
519 | else { /* should never be reached */ | |
520 | ret = -EINVAL; | |
521 | goto error_ret; | |
522 | } | |
523 | ||
5565a450 | 524 | if (ring->setup_ops->postenable) { |
5565a450 | 525 | ret = ring->setup_ops->postenable(dev_info); |
7026ea4b JC |
526 | if (ret) { |
527 | printk(KERN_INFO | |
528 | "Buffer not started:" | |
529 | "postenable failed\n"); | |
5565a450 JC |
530 | if (ring->access->unmark_in_use) |
531 | ring->access->unmark_in_use(ring); | |
7026ea4b | 532 | dev_info->currentmode = previous_mode; |
5565a450 JC |
533 | if (ring->setup_ops->postdisable) |
534 | ring->setup_ops->postdisable(dev_info); | |
7026ea4b JC |
535 | goto error_ret; |
536 | } | |
537 | } | |
538 | } else { | |
5565a450 JC |
539 | if (ring->setup_ops->predisable) { |
540 | ret = ring->setup_ops->predisable(dev_info); | |
7026ea4b JC |
541 | if (ret) |
542 | goto error_ret; | |
543 | } | |
5565a450 JC |
544 | if (ring->access->unmark_in_use) |
545 | ring->access->unmark_in_use(ring); | |
7026ea4b | 546 | dev_info->currentmode = INDIO_DIRECT_MODE; |
5565a450 JC |
547 | if (ring->setup_ops->postdisable) { |
548 | ret = ring->setup_ops->postdisable(dev_info); | |
7026ea4b JC |
549 | if (ret) |
550 | goto error_ret; | |
551 | } | |
552 | } | |
553 | done: | |
554 | mutex_unlock(&dev_info->mlock); | |
555 | return len; | |
556 | ||
557 | error_ret: | |
558 | mutex_unlock(&dev_info->mlock); | |
559 | return ret; | |
560 | } | |
561 | EXPORT_SYMBOL(iio_store_ring_enable); | |
8d213f24 | 562 | |
7026ea4b JC |
563 | ssize_t iio_show_ring_enable(struct device *dev, |
564 | struct device_attribute *attr, | |
565 | char *buf) | |
566 | { | |
567 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
568 | return sprintf(buf, "%d\n", !!(ring->indio_dev->currentmode | |
569 | & INDIO_ALL_RING_MODES)); | |
570 | } | |
571 | EXPORT_SYMBOL(iio_show_ring_enable); | |
572 | ||
5565a450 JC |
573 | int iio_sw_ring_preenable(struct iio_dev *indio_dev) |
574 | { | |
575 | struct iio_ring_buffer *ring = indio_dev->ring; | |
576 | size_t size; | |
577 | dev_dbg(&indio_dev->dev, "%s\n", __func__); | |
578 | /* Check if there are any scan elements enabled, if not fail*/ | |
579 | if (!(ring->scan_count || ring->scan_timestamp)) | |
580 | return -EINVAL; | |
581 | if (ring->scan_timestamp) | |
582 | if (ring->scan_count) | |
583 | /* Timestamp (aligned to s64) and data */ | |
584 | size = (((ring->scan_count * ring->bpe) | |
585 | + sizeof(s64) - 1) | |
586 | & ~(sizeof(s64) - 1)) | |
587 | + sizeof(s64); | |
588 | else /* Timestamp only */ | |
589 | size = sizeof(s64); | |
590 | else /* Data only */ | |
591 | size = ring->scan_count * ring->bpe; | |
592 | ring->access->set_bytes_per_datum(ring, size); | |
593 | ||
594 | return 0; | |
595 | } | |
596 | EXPORT_SYMBOL(iio_sw_ring_preenable); |