iio: Add helper function for initializing triggered buffers
authorLars-Peter Clausen <lars@metafoo.de>
Mon, 18 Jun 2012 16:33:48 +0000 (18:33 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 19 Jun 2012 00:26:09 +0000 (17:26 -0700)
Add a helper function for executing the common tasks which are usually involved
in setting up a simple software ringbuffer. It will allocate the buffer,
allocate the pollfunc and register the buffer.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Acked-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/iio/Kconfig
drivers/iio/Makefile
drivers/iio/industrialio-triggered-buffer.c [new file with mode: 0644]
include/linux/iio/triggered_buffer.h [new file with mode: 0644]

index 103349f2b3b5be0657c2d8138b55ad7a6af41c57..612073f6c540260615107b69a92c14acbeb0a578 100644 (file)
@@ -30,6 +30,13 @@ config IIO_KFIFO_BUF
          no buffer events so it is up to userspace to work out how
          often to read from the buffer.
 
+config IIO_TRIGGERED_BUFFER
+       tristate
+       select IIO_TRIGGER
+       select IIO_KFIFO_BUF
+       help
+         Provides helper functions for setting up triggered buffers.
+
 endif # IIO_BUFFER
 
 config IIO_TRIGGER
index c38fa2a40af27630b94783d7bdb34ff60d0e8213..34309abb79798079ff963f5e054f9184945eafa5 100644 (file)
@@ -7,6 +7,7 @@ industrialio-y := industrialio-core.o industrialio-event.o inkern.o
 industrialio-$(CONFIG_IIO_BUFFER) += industrialio-buffer.o
 industrialio-$(CONFIG_IIO_TRIGGER) += industrialio-trigger.o
 
+obj-$(CONFIG_IIO_TRIGGERED_BUFFER) += industrialio-triggered-buffer.o
 obj-$(CONFIG_IIO_KFIFO_BUF) += kfifo_buf.o
 
 obj-y += adc/
diff --git a/drivers/iio/industrialio-triggered-buffer.c b/drivers/iio/industrialio-triggered-buffer.c
new file mode 100644 (file)
index 0000000..46c619b
--- /dev/null
@@ -0,0 +1,110 @@
+ /*
+ * Copyright (c) 2012 Analog Devices, Inc.
+ *  Author: Lars-Peter Clausen <lars@metafoo.de>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ */
+
+#include <linux/kernel.h>
+#include <linux/export.h>
+#include <linux/module.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/kfifo_buf.h>
+#include <linux/iio/triggered_buffer.h>
+#include <linux/iio/trigger_consumer.h>
+
+static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = {
+       .preenable = &iio_sw_buffer_preenable,
+       .postenable = &iio_triggered_buffer_postenable,
+       .predisable = &iio_triggered_buffer_predisable,
+};
+
+/**
+ * iio_triggered_buffer_setup() - Setup triggered buffer and pollfunc
+ * @indio_dev:         IIO device structure
+ * @pollfunc_bh:       Function which will be used as pollfunc bottom half
+ * @pollfunc_th:       Function which will be used as pollfunc top half
+ * @setup_ops:         Buffer setup functions to use for this device.
+ *                     If NULL the default setup functions for triggered
+ *                     buffers will be used.
+ *
+ * This function combines some common tasks which will normally be performed
+ * when setting up a triggered buffer. It will allocate the buffer and the
+ * pollfunc, as well as register the buffer with the IIO core.
+ *
+ * Before calling this function the indio_dev structure should already be
+ * completely initialized, but not yet registered. In practice this means that
+ * this function should be called right before iio_device_register().
+ *
+ * To free the resources allocated by this function call
+ * iio_triggered_buffer_cleanup().
+ */
+int iio_triggered_buffer_setup(struct iio_dev *indio_dev,
+       irqreturn_t (*pollfunc_bh)(int irq, void *p),
+       irqreturn_t (*pollfunc_th)(int irq, void *p),
+       const struct iio_buffer_setup_ops *setup_ops)
+{
+       int ret;
+
+       indio_dev->buffer = iio_kfifo_allocate(indio_dev);
+       if (!indio_dev->buffer) {
+               ret = -ENOMEM;
+               goto error_ret;
+       }
+
+       indio_dev->pollfunc = iio_alloc_pollfunc(pollfunc_bh,
+                                                pollfunc_th,
+                                                IRQF_ONESHOT,
+                                                indio_dev,
+                                                "%s_consumer%d",
+                                                indio_dev->name,
+                                                indio_dev->id);
+       if (indio_dev->pollfunc == NULL) {
+               ret = -ENOMEM;
+               goto error_kfifo_free;
+       }
+
+       /* Ring buffer functions - here trigger setup related */
+       if (setup_ops)
+               indio_dev->setup_ops = setup_ops;
+       else
+               indio_dev->setup_ops = &iio_triggered_buffer_setup_ops;
+
+       /* Flag that polled ring buffering is possible */
+       indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
+
+       ret = iio_buffer_register(indio_dev,
+                                 indio_dev->channels,
+                                 indio_dev->num_channels);
+       if (ret)
+               goto error_dealloc_pollfunc;
+
+       return 0;
+
+error_dealloc_pollfunc:
+       iio_dealloc_pollfunc(indio_dev->pollfunc);
+error_kfifo_free:
+       iio_kfifo_free(indio_dev->buffer);
+error_ret:
+       return ret;
+}
+EXPORT_SYMBOL(iio_triggered_buffer_setup);
+
+/**
+ * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup()
+ * @indio_dev: IIO device structure
+ */
+void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
+{
+       iio_buffer_unregister(indio_dev);
+       iio_dealloc_pollfunc(indio_dev->pollfunc);
+       iio_kfifo_free(indio_dev->buffer);
+}
+EXPORT_SYMBOL(iio_triggered_buffer_cleanup);
+
+MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
+MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/iio/triggered_buffer.h b/include/linux/iio/triggered_buffer.h
new file mode 100644 (file)
index 0000000..c378ebe
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef _LINUX_IIO_TRIGGERED_BUFFER_H_
+#define _LINUX_IIO_TRIGGERED_BUFFER_H_
+
+#include <linux/interrupt.h>
+
+struct iio_dev;
+struct iio_buffer_setup_ops;
+
+int iio_triggered_buffer_setup(struct iio_dev *indio_dev,
+       irqreturn_t (*pollfunc_bh)(int irq, void *p),
+       irqreturn_t (*pollfunc_th)(int irq, void *p),
+       const struct iio_buffer_setup_ops *setup_ops);
+void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev);
+
+#endif