1 /* The industrial I/O core
3 * Copyright (c) 2008 Jonathan Cameron
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.
9 * Handling of ring allocation / resizing.
12 * Things to look at here.
13 * - Better memory allocation techniques?
14 * - Alternative access techniques?
16 #include <linux/kernel.h>
17 #include <linux/device.h>
18 #include <linux/interrupt.h>
20 #include <linux/poll.h>
21 #include <linux/module.h>
22 #include <linux/cdev.h>
23 #include <linux/idr.h>
24 #include <linux/slab.h>
27 #include "ring_generic.h"
29 /* IDR for ring buffer identifier */
30 static DEFINE_IDR(iio_ring_idr);
31 /* IDR for ring event identifier */
32 static DEFINE_IDR(iio_ring_event_idr);
33 /* IDR for ring access identifier */
34 static DEFINE_IDR(iio_ring_access_idr);
36 int iio_push_ring_event(struct iio_ring_buffer *ring_buf,
40 return __iio_push_event(&ring_buf->ev_int,
43 &ring_buf->shared_ev_pointer);
45 EXPORT_SYMBOL(iio_push_ring_event);
47 int iio_push_or_escallate_ring_event(struct iio_ring_buffer *ring_buf,
51 if (ring_buf->shared_ev_pointer.ev_p)
52 __iio_change_event(ring_buf->shared_ev_pointer.ev_p,
56 return iio_push_ring_event(ring_buf,
61 EXPORT_SYMBOL(iio_push_or_escallate_ring_event);
64 * iio_ring_open() chrdev file open for ring buffer access
66 * This function relies on all ring buffer implementations having an
67 * iio_ring_buffer as their first element.
69 int iio_ring_open(struct inode *inode, struct file *filp)
71 struct iio_handler *hand
72 = container_of(inode->i_cdev, struct iio_handler, chrdev);
73 struct iio_ring_buffer *rb = hand->private;
75 filp->private_data = hand->private;
76 if (rb->access.mark_in_use)
77 rb->access.mark_in_use(rb);
83 * iio_ring_release() -chrdev file close ring buffer access
85 * This function relies on all ring buffer implementations having an
86 * iio_ring_buffer as their first element.
88 int iio_ring_release(struct inode *inode, struct file *filp)
90 struct cdev *cd = inode->i_cdev;
91 struct iio_handler *hand = iio_cdev_to_handler(cd);
92 struct iio_ring_buffer *rb = hand->private;
94 clear_bit(IIO_BUSY_BIT_POS, &rb->access_handler.flags);
95 if (rb->access.unmark_in_use)
96 rb->access.unmark_in_use(rb);
102 * iio_ring_rip_outer() chrdev read for ring buffer access
104 * This function relies on all ring buffer implementations having an
105 * iio_ring _bufer as their first element.
107 ssize_t iio_ring_rip_outer(struct file *filp,
112 struct iio_ring_buffer *rb = filp->private_data;
113 int ret, dead_offset, copied;
115 /* rip lots must exist. */
116 if (!rb->access.rip_lots)
118 copied = rb->access.rip_lots(rb, count, &data, &dead_offset);
124 if (copy_to_user(buf, data + dead_offset, copied)) {
126 goto error_free_data_cpy;
128 /* In clever ring buffer designs this may not need to be freed.
129 * When such a design exists I'll add this to ring access funcs.
141 static const struct file_operations iio_ring_fileops = {
142 .read = iio_ring_rip_outer,
143 .release = iio_ring_release,
144 .open = iio_ring_open,
145 .owner = THIS_MODULE,
149 * __iio_request_ring_buffer_event_chrdev() allocate ring event chrdev
150 * @buf: ring buffer whose event chrdev we are allocating
151 * @owner: the module who owns the ring buffer (for ref counting)
152 * @dev: device with which the chrdev is associated
155 __iio_request_ring_buffer_event_chrdev(struct iio_ring_buffer *buf,
157 struct module *owner,
161 ret = iio_get_new_idr_val(&iio_ring_event_idr);
165 buf->ev_int.id = ret;
167 snprintf(buf->ev_int._name, 20,
170 ret = iio_setup_ev_int(&(buf->ev_int),
179 iio_free_idr_val(&iio_ring_event_idr, buf->ev_int.id);
185 __iio_free_ring_buffer_event_chrdev(struct iio_ring_buffer *buf)
187 iio_free_ev_int(&(buf->ev_int));
188 iio_free_idr_val(&iio_ring_event_idr, buf->ev_int.id);
191 static void iio_ring_access_release(struct device *dev)
193 struct iio_ring_buffer *buf
194 = access_dev_to_iio_ring_buffer(dev);
195 cdev_del(&buf->access_handler.chrdev);
196 iio_device_free_chrdev_minor(MINOR(dev->devt));
199 static struct device_type iio_ring_access_type = {
200 .release = iio_ring_access_release,
204 __iio_request_ring_buffer_access_chrdev(struct iio_ring_buffer *buf,
206 struct module *owner)
210 buf->access_handler.flags = 0;
212 buf->access_dev.parent = &buf->dev;
213 buf->access_dev.class = &iio_class;
214 buf->access_dev.type = &iio_ring_access_type;
215 device_initialize(&buf->access_dev);
217 minor = iio_device_get_chrdev_minor();
220 goto error_device_put;
222 buf->access_dev.devt = MKDEV(MAJOR(iio_devt), minor);
224 ret = iio_get_new_idr_val(&iio_ring_access_idr);
226 goto error_device_put;
228 buf->access_id = ret;
229 dev_set_name(&buf->access_dev, "ring_access%d", buf->access_id);
230 ret = device_add(&buf->access_dev);
232 printk(KERN_ERR "failed to add the ring access dev\n");
236 cdev_init(&buf->access_handler.chrdev, &iio_ring_fileops);
237 buf->access_handler.chrdev.owner = owner;
239 ret = cdev_add(&buf->access_handler.chrdev, buf->access_dev.devt, 1);
241 printk(KERN_ERR "failed to allocate ring access chrdev\n");
242 goto error_device_unregister;
245 error_device_unregister:
246 device_unregister(&buf->access_dev);
248 iio_free_idr_val(&iio_ring_access_idr, buf->access_id);
250 put_device(&buf->access_dev);
255 static void __iio_free_ring_buffer_access_chrdev(struct iio_ring_buffer *buf)
257 iio_free_idr_val(&iio_ring_access_idr, buf->access_id);
258 device_unregister(&buf->access_dev);
261 void iio_ring_buffer_init(struct iio_ring_buffer *ring,
262 struct iio_dev *dev_info)
264 if (ring->access.mark_param_change)
265 ring->access.mark_param_change(ring);
266 ring->indio_dev = dev_info;
267 ring->ev_int.private = ring;
268 ring->access_handler.private = ring;
270 EXPORT_SYMBOL(iio_ring_buffer_init);
272 int iio_ring_buffer_register(struct iio_ring_buffer *ring)
275 ret = iio_get_new_idr_val(&iio_ring_idr);
281 dev_set_name(&ring->dev, "ring_buffer%d", ring->id);
282 ret = device_add(&ring->dev);
286 ret = __iio_request_ring_buffer_event_chrdev(ring,
291 goto error_remove_device;
293 ret = __iio_request_ring_buffer_access_chrdev(ring,
298 goto error_free_ring_buffer_event_chrdev;
301 error_free_ring_buffer_event_chrdev:
302 __iio_free_ring_buffer_event_chrdev(ring);
304 device_del(&ring->dev);
306 iio_free_idr_val(&iio_ring_idr, ring->id);
310 EXPORT_SYMBOL(iio_ring_buffer_register);
312 void iio_ring_buffer_unregister(struct iio_ring_buffer *ring)
314 __iio_free_ring_buffer_access_chrdev(ring);
315 __iio_free_ring_buffer_event_chrdev(ring);
316 device_del(&ring->dev);
317 iio_free_idr_val(&iio_ring_idr, ring->id);
319 EXPORT_SYMBOL(iio_ring_buffer_unregister);
321 ssize_t iio_read_ring_length(struct device *dev,
322 struct device_attribute *attr,
326 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
328 if (ring->access.get_length)
329 len = sprintf(buf, "%d\n",
330 ring->access.get_length(ring));
334 EXPORT_SYMBOL(iio_read_ring_length);
336 ssize_t iio_write_ring_length(struct device *dev,
337 struct device_attribute *attr,
343 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
344 ret = strict_strtoul(buf, 10, &val);
348 if (ring->access.get_length)
349 if (val == ring->access.get_length(ring))
352 if (ring->access.set_length) {
353 ring->access.set_length(ring, val);
354 if (ring->access.mark_param_change)
355 ring->access.mark_param_change(ring);
360 EXPORT_SYMBOL(iio_write_ring_length);
362 ssize_t iio_read_ring_bps(struct device *dev,
363 struct device_attribute *attr,
367 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
369 if (ring->access.get_bpd)
370 len = sprintf(buf, "%d\n",
371 ring->access.get_bpd(ring));
375 EXPORT_SYMBOL(iio_read_ring_bps);
377 ssize_t iio_store_ring_enable(struct device *dev,
378 struct device_attribute *attr,
383 bool requested_state, current_state;
385 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
386 struct iio_dev *dev_info = ring->indio_dev;
388 mutex_lock(&dev_info->mlock);
389 previous_mode = dev_info->currentmode;
390 requested_state = !(buf[0] == '0');
391 current_state = !!(previous_mode & INDIO_ALL_RING_MODES);
392 if (current_state == requested_state) {
393 printk(KERN_INFO "iio-ring, current state requested again\n");
396 if (requested_state) {
397 if (ring->preenable) {
398 ret = ring->preenable(dev_info);
401 "Buffer not started:"
402 "ring preenable failed\n");
406 if (ring->access.request_update) {
407 ret = ring->access.request_update(ring);
410 "Buffer not started:"
411 "ring parameter update failed\n");
415 if (ring->access.mark_in_use)
416 ring->access.mark_in_use(ring);
417 /* Definitely possible for devices to support both of these.*/
418 if (dev_info->modes & INDIO_RING_TRIGGERED) {
419 if (!dev_info->trig) {
421 "Buffer not started: no trigger\n");
423 if (ring->access.unmark_in_use)
424 ring->access.unmark_in_use(ring);
427 dev_info->currentmode = INDIO_RING_TRIGGERED;
428 } else if (dev_info->modes & INDIO_RING_HARDWARE_BUFFER)
429 dev_info->currentmode = INDIO_RING_HARDWARE_BUFFER;
430 else { /* should never be reached */
435 if (ring->postenable) {
437 ret = ring->postenable(dev_info);
440 "Buffer not started:"
441 "postenable failed\n");
442 if (ring->access.unmark_in_use)
443 ring->access.unmark_in_use(ring);
444 dev_info->currentmode = previous_mode;
445 if (ring->postdisable)
446 ring->postdisable(dev_info);
451 if (ring->predisable) {
452 ret = ring->predisable(dev_info);
456 if (ring->access.unmark_in_use)
457 ring->access.unmark_in_use(ring);
458 dev_info->currentmode = INDIO_DIRECT_MODE;
459 if (ring->postdisable) {
460 ret = ring->postdisable(dev_info);
466 mutex_unlock(&dev_info->mlock);
470 mutex_unlock(&dev_info->mlock);
473 EXPORT_SYMBOL(iio_store_ring_enable);
474 ssize_t iio_show_ring_enable(struct device *dev,
475 struct device_attribute *attr,
478 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
479 return sprintf(buf, "%d\n", !!(ring->indio_dev->currentmode
480 & INDIO_ALL_RING_MODES));
482 EXPORT_SYMBOL(iio_show_ring_enable);
484 ssize_t iio_scan_el_show(struct device *dev,
485 struct device_attribute *attr,
489 struct iio_dev *indio_dev = dev_get_drvdata(dev);
490 struct iio_scan_el *this_el = to_iio_scan_el(attr);
492 ret = iio_scan_mask_query(indio_dev, this_el->number);
495 return sprintf(buf, "%d\n", ret);
497 EXPORT_SYMBOL(iio_scan_el_show);
499 ssize_t iio_scan_el_store(struct device *dev,
500 struct device_attribute *attr,
506 struct iio_dev *indio_dev = dev_get_drvdata(dev);
507 struct iio_scan_el *this_el = to_iio_scan_el(attr);
509 state = !(buf[0] == '0');
510 mutex_lock(&indio_dev->mlock);
511 if (indio_dev->currentmode == INDIO_RING_TRIGGERED) {
515 ret = iio_scan_mask_query(indio_dev, this_el->number);
519 ret = iio_scan_mask_clear(indio_dev, this_el->number);
522 indio_dev->scan_count--;
523 } else if (state && !ret) {
524 ret = iio_scan_mask_set(indio_dev, this_el->number);
527 indio_dev->scan_count++;
529 if (this_el->set_state)
530 ret = this_el->set_state(this_el, indio_dev, state);
532 mutex_unlock(&indio_dev->mlock);
534 return ret ? ret : len;
537 EXPORT_SYMBOL(iio_scan_el_store);
539 ssize_t iio_scan_el_ts_show(struct device *dev,
540 struct device_attribute *attr,
543 struct iio_dev *indio_dev = dev_get_drvdata(dev);
544 return sprintf(buf, "%d\n", indio_dev->scan_timestamp);
546 EXPORT_SYMBOL(iio_scan_el_ts_show);
548 ssize_t iio_scan_el_ts_store(struct device *dev,
549 struct device_attribute *attr,
554 struct iio_dev *indio_dev = dev_get_drvdata(dev);
556 state = !(buf[0] == '0');
557 mutex_lock(&indio_dev->mlock);
558 if (indio_dev->currentmode == INDIO_RING_TRIGGERED) {
562 indio_dev->scan_timestamp = state;
564 mutex_unlock(&indio_dev->mlock);
566 return ret ? ret : len;
568 EXPORT_SYMBOL(iio_scan_el_ts_store);