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; | |
39 | if (rb->access.mark_in_use) | |
40 | rb->access.mark_in_use(rb); | |
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); | |
58 | if (rb->access.unmark_in_use) | |
59 | rb->access.unmark_in_use(rb); | |
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; | |
b26a2188 | 74 | int ret; |
d5857d65 | 75 | |
7026ea4b | 76 | /* rip lots must exist. */ |
b4281733 | 77 | if (!rb->access.read_first_n) |
7026ea4b | 78 | return -EINVAL; |
b26a2188 | 79 | ret = rb->access.read_first_n(rb, n, buf); |
7026ea4b | 80 | |
7026ea4b JC |
81 | return ret; |
82 | } | |
83 | ||
a7348347 JC |
84 | /** |
85 | * iio_ring_poll() - poll the ring to find out if it has data | |
86 | */ | |
87 | static unsigned int iio_ring_poll(struct file *filp, | |
88 | struct poll_table_struct *wait) | |
89 | { | |
90 | struct iio_ring_buffer *rb = filp->private_data; | |
91 | int ret = 0; | |
92 | ||
93 | poll_wait(filp, &rb->pollq, wait); | |
94 | if (rb->stufftoread) | |
95 | return POLLIN | POLLRDNORM; | |
96 | /* need a way of knowing if there may be enough data... */ | |
97 | return ret; | |
98 | } | |
99 | ||
7026ea4b | 100 | static const struct file_operations iio_ring_fileops = { |
b4281733 | 101 | .read = iio_ring_read_first_n_outer, |
7026ea4b JC |
102 | .release = iio_ring_release, |
103 | .open = iio_ring_open, | |
a7348347 | 104 | .poll = iio_ring_poll, |
7026ea4b | 105 | .owner = THIS_MODULE, |
6038f373 | 106 | .llseek = noop_llseek, |
7026ea4b JC |
107 | }; |
108 | ||
3feb0797 | 109 | void iio_ring_access_release(struct device *dev) |
7026ea4b JC |
110 | { |
111 | struct iio_ring_buffer *buf | |
3feb0797 | 112 | = container_of(dev, struct iio_ring_buffer, dev); |
7026ea4b JC |
113 | cdev_del(&buf->access_handler.chrdev); |
114 | iio_device_free_chrdev_minor(MINOR(dev->devt)); | |
115 | } | |
3feb0797 | 116 | EXPORT_SYMBOL(iio_ring_access_release); |
7026ea4b JC |
117 | |
118 | static inline int | |
3feb0797 | 119 | __iio_request_ring_buffer_chrdev(struct iio_ring_buffer *buf, |
7026ea4b JC |
120 | struct module *owner) |
121 | { | |
122 | int ret, minor; | |
123 | ||
124 | buf->access_handler.flags = 0; | |
125 | ||
3feb0797 JC |
126 | buf->dev.bus = &iio_bus_type; |
127 | device_initialize(&buf->dev); | |
7026ea4b JC |
128 | |
129 | minor = iio_device_get_chrdev_minor(); | |
130 | if (minor < 0) { | |
131 | ret = minor; | |
132 | goto error_device_put; | |
133 | } | |
3feb0797 JC |
134 | buf->dev.devt = MKDEV(MAJOR(iio_devt), minor); |
135 | dev_set_name(&buf->dev, "%s:buffer%d", | |
136 | dev_name(buf->dev.parent), | |
137 | buf->id); | |
138 | ret = device_add(&buf->dev); | |
7026ea4b | 139 | if (ret < 0) { |
3feb0797 | 140 | printk(KERN_ERR "failed to add the ring dev\n"); |
758d988c | 141 | goto error_device_put; |
7026ea4b | 142 | } |
7026ea4b JC |
143 | cdev_init(&buf->access_handler.chrdev, &iio_ring_fileops); |
144 | buf->access_handler.chrdev.owner = owner; | |
3feb0797 | 145 | ret = cdev_add(&buf->access_handler.chrdev, buf->dev.devt, 1); |
7026ea4b | 146 | if (ret) { |
3feb0797 | 147 | printk(KERN_ERR "failed to allocate ring chrdev\n"); |
7026ea4b JC |
148 | goto error_device_unregister; |
149 | } | |
150 | return 0; | |
758d988c | 151 | |
7026ea4b | 152 | error_device_unregister: |
3feb0797 | 153 | device_unregister(&buf->dev); |
7026ea4b | 154 | error_device_put: |
3feb0797 | 155 | put_device(&buf->dev); |
7026ea4b JC |
156 | |
157 | return ret; | |
158 | } | |
159 | ||
3feb0797 | 160 | static void __iio_free_ring_buffer_chrdev(struct iio_ring_buffer *buf) |
7026ea4b | 161 | { |
3feb0797 | 162 | device_unregister(&buf->dev); |
7026ea4b JC |
163 | } |
164 | ||
165 | void iio_ring_buffer_init(struct iio_ring_buffer *ring, | |
166 | struct iio_dev *dev_info) | |
167 | { | |
168 | if (ring->access.mark_param_change) | |
169 | ring->access.mark_param_change(ring); | |
170 | ring->indio_dev = dev_info; | |
7026ea4b | 171 | ring->access_handler.private = ring; |
a7348347 | 172 | init_waitqueue_head(&ring->pollq); |
7026ea4b JC |
173 | } |
174 | EXPORT_SYMBOL(iio_ring_buffer_init); | |
175 | ||
1d892719 JC |
176 | static ssize_t iio_show_scan_index(struct device *dev, |
177 | struct device_attribute *attr, | |
178 | char *buf) | |
179 | { | |
180 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
181 | return sprintf(buf, "%u\n", this_attr->c->scan_index); | |
182 | } | |
183 | ||
184 | static ssize_t iio_show_fixed_type(struct device *dev, | |
185 | struct device_attribute *attr, | |
186 | char *buf) | |
187 | { | |
188 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
189 | return sprintf(buf, "%c%d/%d>>%u\n", | |
190 | this_attr->c->scan_type.sign, | |
191 | this_attr->c->scan_type.realbits, | |
192 | this_attr->c->scan_type.storagebits, | |
193 | this_attr->c->scan_type.shift); | |
194 | } | |
195 | ||
196 | static int __iio_add_chan_scan_elattr(const char *postfix, | |
197 | const char *group, | |
198 | const struct iio_chan_spec *chan, | |
199 | struct device *dev, | |
200 | struct list_head *attr_list) | |
7026ea4b JC |
201 | { |
202 | int ret; | |
1d892719 JC |
203 | struct iio_scan_el *scan_el; |
204 | ||
205 | scan_el = kzalloc(sizeof *scan_el, GFP_KERNEL); | |
206 | if (scan_el == NULL) { | |
207 | ret = -ENOMEM; | |
208 | goto error_ret; | |
209 | } | |
210 | if (chan->type != IIO_TIMESTAMP) | |
211 | ret = __iio_device_attr_init(&scan_el->dev_attr, postfix, chan, | |
212 | iio_scan_el_show, | |
213 | iio_scan_el_store, 0); | |
214 | else /* | |
215 | * Timestamp handled separately because it simplifies a lot of | |
216 | * drivers by ensuring they don't have to know its magic index | |
217 | */ | |
218 | ret = __iio_device_attr_init(&scan_el->dev_attr, postfix, chan, | |
219 | iio_scan_el_ts_show, | |
220 | iio_scan_el_ts_store, 0); | |
221 | if (ret) | |
222 | goto error_free_scan_el; | |
223 | ||
224 | scan_el->number = chan->scan_index; | |
225 | ||
226 | ret = sysfs_add_file_to_group(&dev->kobj, | |
227 | &scan_el->dev_attr.attr, | |
228 | group); | |
229 | if (ret < 0) | |
230 | goto error_device_attr_deinit; | |
231 | ||
232 | list_add(&scan_el->l, attr_list); | |
233 | ||
234 | return 0; | |
235 | error_device_attr_deinit: | |
236 | __iio_device_attr_deinit(&scan_el->dev_attr); | |
237 | error_free_scan_el: | |
238 | kfree(scan_el); | |
239 | error_ret: | |
240 | return ret; | |
241 | } | |
242 | ||
243 | static int iio_ring_add_channel_sysfs(struct iio_ring_buffer *ring, | |
244 | const struct iio_chan_spec *chan) | |
245 | { | |
246 | int ret; | |
247 | ||
248 | ret = __iio_add_chan_devattr("index", "scan_elements", | |
249 | chan, | |
250 | &iio_show_scan_index, | |
251 | NULL, | |
252 | 0, | |
253 | 0, | |
254 | &ring->dev, | |
255 | &ring->scan_el_dev_attr_list); | |
256 | if (ret) | |
257 | goto error_ret; | |
258 | ||
259 | ret = __iio_add_chan_devattr("type", "scan_elements", | |
260 | chan, | |
261 | &iio_show_fixed_type, | |
262 | NULL, | |
263 | 0, | |
264 | 0, | |
265 | &ring->dev, | |
266 | &ring->scan_el_dev_attr_list); | |
267 | ||
268 | if (ret) | |
269 | goto error_ret; | |
270 | ||
271 | ret = __iio_add_chan_scan_elattr("en", "scan_elements", | |
272 | chan, &ring->dev, | |
273 | &ring->scan_el_en_attr_list); | |
274 | ||
275 | error_ret: | |
276 | return ret; | |
277 | } | |
278 | ||
279 | static void iio_ring_remove_and_free_scan_el_attr(struct iio_ring_buffer *ring, | |
280 | struct iio_scan_el *p) | |
281 | { | |
282 | sysfs_remove_file_from_group(&ring->dev.kobj, | |
283 | &p->dev_attr.attr, "scan_elements"); | |
284 | kfree(p->dev_attr.attr.name); | |
285 | kfree(p); | |
286 | } | |
287 | ||
288 | static void iio_ring_remove_and_free_scan_dev_attr(struct iio_ring_buffer *ring, | |
289 | struct iio_dev_attr *p) | |
290 | { | |
291 | sysfs_remove_file_from_group(&ring->dev.kobj, | |
292 | &p->dev_attr.attr, "scan_elements"); | |
293 | kfree(p->dev_attr.attr.name); | |
294 | kfree(p); | |
295 | } | |
296 | ||
297 | static struct attribute *iio_scan_el_dummy_attrs[] = { | |
298 | NULL | |
299 | }; | |
300 | ||
301 | static struct attribute_group iio_scan_el_dummy_group = { | |
302 | .name = "scan_elements", | |
303 | .attrs = iio_scan_el_dummy_attrs | |
304 | }; | |
305 | ||
306 | static void __iio_ring_attr_cleanup(struct iio_ring_buffer *ring) | |
307 | { | |
308 | struct iio_dev_attr *p, *n; | |
309 | struct iio_scan_el *q, *m; | |
310 | int anydynamic = !(list_empty(&ring->scan_el_dev_attr_list) && | |
311 | list_empty(&ring->scan_el_en_attr_list)); | |
312 | list_for_each_entry_safe(p, n, | |
313 | &ring->scan_el_dev_attr_list, l) | |
314 | iio_ring_remove_and_free_scan_dev_attr(ring, p); | |
315 | list_for_each_entry_safe(q, m, | |
316 | &ring->scan_el_en_attr_list, l) | |
317 | iio_ring_remove_and_free_scan_el_attr(ring, q); | |
318 | ||
319 | if (ring->scan_el_attrs) | |
320 | sysfs_remove_group(&ring->dev.kobj, | |
321 | ring->scan_el_attrs); | |
322 | else if (anydynamic) | |
323 | sysfs_remove_group(&ring->dev.kobj, | |
324 | &iio_scan_el_dummy_group); | |
325 | } | |
326 | ||
327 | int iio_ring_buffer_register_ex(struct iio_ring_buffer *ring, int id, | |
328 | const struct iio_chan_spec *channels, | |
329 | int num_channels) | |
330 | { | |
331 | int ret, i; | |
758d988c JC |
332 | |
333 | ring->id = id; | |
7026ea4b | 334 | |
3feb0797 | 335 | ret = __iio_request_ring_buffer_chrdev(ring, ring->owner); |
7026ea4b JC |
336 | |
337 | if (ret) | |
3feb0797 | 338 | goto error_ret; |
bf32963c MS |
339 | if (ring->scan_el_attrs) { |
340 | ret = sysfs_create_group(&ring->dev.kobj, | |
341 | ring->scan_el_attrs); | |
342 | if (ret) { | |
343 | dev_err(&ring->dev, | |
344 | "Failed to add sysfs scan elements\n"); | |
3feb0797 | 345 | goto error_free_ring_buffer_chrdev; |
bf32963c | 346 | } |
1d892719 JC |
347 | } else if (channels) { |
348 | ret = sysfs_create_group(&ring->dev.kobj, | |
349 | &iio_scan_el_dummy_group); | |
350 | if (ret) | |
3feb0797 | 351 | goto error_free_ring_buffer_chrdev; |
bf32963c MS |
352 | } |
353 | ||
1d892719 JC |
354 | INIT_LIST_HEAD(&ring->scan_el_dev_attr_list); |
355 | INIT_LIST_HEAD(&ring->scan_el_en_attr_list); | |
356 | if (channels) { | |
357 | /* new magic */ | |
358 | for (i = 0; i < num_channels; i++) { | |
359 | ret = iio_ring_add_channel_sysfs(ring, &channels[i]); | |
360 | if (ret < 0) | |
361 | goto error_cleanup_dynamic; | |
362 | } | |
363 | } | |
364 | ||
365 | return 0; | |
366 | error_cleanup_dynamic: | |
367 | __iio_ring_attr_cleanup(ring); | |
3feb0797 JC |
368 | error_free_ring_buffer_chrdev: |
369 | __iio_free_ring_buffer_chrdev(ring); | |
7026ea4b JC |
370 | error_ret: |
371 | return ret; | |
372 | } | |
1d892719 JC |
373 | EXPORT_SYMBOL(iio_ring_buffer_register_ex); |
374 | ||
375 | int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id) | |
376 | { | |
377 | return iio_ring_buffer_register_ex(ring, id, NULL, 0); | |
378 | } | |
7026ea4b JC |
379 | EXPORT_SYMBOL(iio_ring_buffer_register); |
380 | ||
381 | void iio_ring_buffer_unregister(struct iio_ring_buffer *ring) | |
382 | { | |
1d892719 | 383 | __iio_ring_attr_cleanup(ring); |
3feb0797 | 384 | __iio_free_ring_buffer_chrdev(ring); |
7026ea4b JC |
385 | } |
386 | EXPORT_SYMBOL(iio_ring_buffer_unregister); | |
387 | ||
388 | ssize_t iio_read_ring_length(struct device *dev, | |
389 | struct device_attribute *attr, | |
390 | char *buf) | |
391 | { | |
392 | int len = 0; | |
393 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
394 | ||
395 | if (ring->access.get_length) | |
396 | len = sprintf(buf, "%d\n", | |
397 | ring->access.get_length(ring)); | |
398 | ||
399 | return len; | |
400 | } | |
401 | EXPORT_SYMBOL(iio_read_ring_length); | |
402 | ||
0abd2428 | 403 | ssize_t iio_write_ring_length(struct device *dev, |
7026ea4b JC |
404 | struct device_attribute *attr, |
405 | const char *buf, | |
406 | size_t len) | |
407 | { | |
408 | int ret; | |
409 | ulong val; | |
410 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
411 | ret = strict_strtoul(buf, 10, &val); | |
412 | if (ret) | |
413 | return ret; | |
414 | ||
415 | if (ring->access.get_length) | |
416 | if (val == ring->access.get_length(ring)) | |
417 | return len; | |
418 | ||
419 | if (ring->access.set_length) { | |
420 | ring->access.set_length(ring, val); | |
421 | if (ring->access.mark_param_change) | |
422 | ring->access.mark_param_change(ring); | |
423 | } | |
424 | ||
425 | return len; | |
426 | } | |
427 | EXPORT_SYMBOL(iio_write_ring_length); | |
428 | ||
ffcab07a | 429 | ssize_t iio_read_ring_bytes_per_datum(struct device *dev, |
7026ea4b JC |
430 | struct device_attribute *attr, |
431 | char *buf) | |
432 | { | |
433 | int len = 0; | |
434 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
435 | ||
ffcab07a | 436 | if (ring->access.get_bytes_per_datum) |
7026ea4b | 437 | len = sprintf(buf, "%d\n", |
ffcab07a | 438 | ring->access.get_bytes_per_datum(ring)); |
7026ea4b JC |
439 | |
440 | return len; | |
441 | } | |
ffcab07a | 442 | EXPORT_SYMBOL(iio_read_ring_bytes_per_datum); |
7026ea4b JC |
443 | |
444 | ssize_t iio_store_ring_enable(struct device *dev, | |
445 | struct device_attribute *attr, | |
446 | const char *buf, | |
447 | size_t len) | |
448 | { | |
449 | int ret; | |
450 | bool requested_state, current_state; | |
451 | int previous_mode; | |
452 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
453 | struct iio_dev *dev_info = ring->indio_dev; | |
454 | ||
455 | mutex_lock(&dev_info->mlock); | |
456 | previous_mode = dev_info->currentmode; | |
457 | requested_state = !(buf[0] == '0'); | |
458 | current_state = !!(previous_mode & INDIO_ALL_RING_MODES); | |
459 | if (current_state == requested_state) { | |
460 | printk(KERN_INFO "iio-ring, current state requested again\n"); | |
461 | goto done; | |
462 | } | |
463 | if (requested_state) { | |
464 | if (ring->preenable) { | |
465 | ret = ring->preenable(dev_info); | |
466 | if (ret) { | |
467 | printk(KERN_ERR | |
468 | "Buffer not started:" | |
469 | "ring preenable failed\n"); | |
470 | goto error_ret; | |
471 | } | |
472 | } | |
473 | if (ring->access.request_update) { | |
474 | ret = ring->access.request_update(ring); | |
475 | if (ret) { | |
476 | printk(KERN_INFO | |
477 | "Buffer not started:" | |
478 | "ring parameter update failed\n"); | |
479 | goto error_ret; | |
480 | } | |
481 | } | |
482 | if (ring->access.mark_in_use) | |
483 | ring->access.mark_in_use(ring); | |
484 | /* Definitely possible for devices to support both of these.*/ | |
485 | if (dev_info->modes & INDIO_RING_TRIGGERED) { | |
486 | if (!dev_info->trig) { | |
487 | printk(KERN_INFO | |
488 | "Buffer not started: no trigger\n"); | |
489 | ret = -EINVAL; | |
490 | if (ring->access.unmark_in_use) | |
491 | ring->access.unmark_in_use(ring); | |
492 | goto error_ret; | |
493 | } | |
494 | dev_info->currentmode = INDIO_RING_TRIGGERED; | |
495 | } else if (dev_info->modes & INDIO_RING_HARDWARE_BUFFER) | |
496 | dev_info->currentmode = INDIO_RING_HARDWARE_BUFFER; | |
497 | else { /* should never be reached */ | |
498 | ret = -EINVAL; | |
499 | goto error_ret; | |
500 | } | |
501 | ||
502 | if (ring->postenable) { | |
503 | ||
504 | ret = ring->postenable(dev_info); | |
505 | if (ret) { | |
506 | printk(KERN_INFO | |
507 | "Buffer not started:" | |
508 | "postenable failed\n"); | |
509 | if (ring->access.unmark_in_use) | |
510 | ring->access.unmark_in_use(ring); | |
511 | dev_info->currentmode = previous_mode; | |
512 | if (ring->postdisable) | |
513 | ring->postdisable(dev_info); | |
514 | goto error_ret; | |
515 | } | |
516 | } | |
517 | } else { | |
518 | if (ring->predisable) { | |
519 | ret = ring->predisable(dev_info); | |
520 | if (ret) | |
521 | goto error_ret; | |
522 | } | |
523 | if (ring->access.unmark_in_use) | |
524 | ring->access.unmark_in_use(ring); | |
525 | dev_info->currentmode = INDIO_DIRECT_MODE; | |
526 | if (ring->postdisable) { | |
527 | ret = ring->postdisable(dev_info); | |
528 | if (ret) | |
529 | goto error_ret; | |
530 | } | |
531 | } | |
532 | done: | |
533 | mutex_unlock(&dev_info->mlock); | |
534 | return len; | |
535 | ||
536 | error_ret: | |
537 | mutex_unlock(&dev_info->mlock); | |
538 | return ret; | |
539 | } | |
540 | EXPORT_SYMBOL(iio_store_ring_enable); | |
541 | ssize_t iio_show_ring_enable(struct device *dev, | |
542 | struct device_attribute *attr, | |
543 | char *buf) | |
544 | { | |
545 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); | |
546 | return sprintf(buf, "%d\n", !!(ring->indio_dev->currentmode | |
547 | & INDIO_ALL_RING_MODES)); | |
548 | } | |
549 | EXPORT_SYMBOL(iio_show_ring_enable); | |
550 | ||
551 | ssize_t iio_scan_el_show(struct device *dev, | |
552 | struct device_attribute *attr, | |
553 | char *buf) | |
554 | { | |
555 | int ret; | |
bf32963c | 556 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); |
7026ea4b JC |
557 | struct iio_scan_el *this_el = to_iio_scan_el(attr); |
558 | ||
bf32963c | 559 | ret = iio_scan_mask_query(ring, this_el->number); |
7026ea4b JC |
560 | if (ret < 0) |
561 | return ret; | |
562 | return sprintf(buf, "%d\n", ret); | |
563 | } | |
564 | EXPORT_SYMBOL(iio_scan_el_show); | |
565 | ||
566 | ssize_t iio_scan_el_store(struct device *dev, | |
567 | struct device_attribute *attr, | |
568 | const char *buf, | |
569 | size_t len) | |
570 | { | |
571 | int ret = 0; | |
572 | bool state; | |
bf32963c MS |
573 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); |
574 | struct iio_dev *indio_dev = ring->indio_dev; | |
7026ea4b JC |
575 | struct iio_scan_el *this_el = to_iio_scan_el(attr); |
576 | ||
577 | state = !(buf[0] == '0'); | |
578 | mutex_lock(&indio_dev->mlock); | |
579 | if (indio_dev->currentmode == INDIO_RING_TRIGGERED) { | |
580 | ret = -EBUSY; | |
581 | goto error_ret; | |
582 | } | |
bf32963c | 583 | ret = iio_scan_mask_query(ring, this_el->number); |
7026ea4b JC |
584 | if (ret < 0) |
585 | goto error_ret; | |
586 | if (!state && ret) { | |
bf32963c | 587 | ret = iio_scan_mask_clear(ring, this_el->number); |
7026ea4b JC |
588 | if (ret) |
589 | goto error_ret; | |
7026ea4b | 590 | } else if (state && !ret) { |
bf32963c | 591 | ret = iio_scan_mask_set(ring, this_el->number); |
7026ea4b JC |
592 | if (ret) |
593 | goto error_ret; | |
7026ea4b JC |
594 | } |
595 | if (this_el->set_state) | |
596 | ret = this_el->set_state(this_el, indio_dev, state); | |
597 | error_ret: | |
598 | mutex_unlock(&indio_dev->mlock); | |
599 | ||
600 | return ret ? ret : len; | |
601 | ||
602 | } | |
603 | EXPORT_SYMBOL(iio_scan_el_store); | |
604 | ||
605 | ssize_t iio_scan_el_ts_show(struct device *dev, | |
606 | struct device_attribute *attr, | |
607 | char *buf) | |
608 | { | |
bf32963c MS |
609 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); |
610 | return sprintf(buf, "%d\n", ring->scan_timestamp); | |
7026ea4b JC |
611 | } |
612 | EXPORT_SYMBOL(iio_scan_el_ts_show); | |
613 | ||
614 | ssize_t iio_scan_el_ts_store(struct device *dev, | |
615 | struct device_attribute *attr, | |
616 | const char *buf, | |
617 | size_t len) | |
618 | { | |
619 | int ret = 0; | |
bf32963c MS |
620 | struct iio_ring_buffer *ring = dev_get_drvdata(dev); |
621 | struct iio_dev *indio_dev = ring->indio_dev; | |
7026ea4b JC |
622 | bool state; |
623 | state = !(buf[0] == '0'); | |
624 | mutex_lock(&indio_dev->mlock); | |
625 | if (indio_dev->currentmode == INDIO_RING_TRIGGERED) { | |
626 | ret = -EBUSY; | |
627 | goto error_ret; | |
628 | } | |
bf32963c | 629 | ring->scan_timestamp = state; |
7026ea4b JC |
630 | error_ret: |
631 | mutex_unlock(&indio_dev->mlock); | |
632 | ||
633 | return ret ? ret : len; | |
634 | } | |
635 | EXPORT_SYMBOL(iio_scan_el_ts_store); |