linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@cam.ac.uk>
To: Lars-Peter Clausen <lars@metafoo.de>
Cc: linux-iio@vger.kernel.org
Subject: Re: [v2 1/9] staging:iio: Add helper function for initializing triggered buffers
Date: Fri, 08 Jun 2012 12:36:34 +0100	[thread overview]
Message-ID: <4FD1E3C2.1060100@cam.ac.uk> (raw)
In-Reply-To: <1339145332-26811-1-git-send-email-lars@metafoo.de>

On 6/8/2012 9:48 AM, Lars-Peter Clausen wrote:
> 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>
>
> ---
> Changes since v1:
> 	* Move function declarations to their own header
And forgot to add said header...
> 	* Mention that iio_triggered_buffer_setup should usually be called right
> 	  before iio_device_register
> 	* Minor code cleanups
> ---
>   drivers/iio/Kconfig                         |    7 ++
>   drivers/iio/Makefile                        |    1 +
>   drivers/iio/industrialio-triggered-buffer.c |  110 +++++++++++++++++++++++++++
>   3 files changed, 118 insertions(+)
>   create mode 100644 drivers/iio/industrialio-triggered-buffer.c
>
> diff --git a/drivers/iio/Kconfig b/drivers/iio/Kconfig
> index 103349f..612073f 100644
> --- a/drivers/iio/Kconfig
> +++ b/drivers/iio/Kconfig
> @@ -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
> diff --git a/drivers/iio/Makefile b/drivers/iio/Makefile
> index c38fa2a..34309ab 100644
> --- a/drivers/iio/Makefile
> +++ b/drivers/iio/Makefile
> @@ -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
> index 0000000..a462a57
> --- /dev/null
> +++ b/drivers/iio/industrialio-triggered-buffer.c
> @@ -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 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
> + * iio_triggered_buffer_cleanup() should be called.
> + */
> +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");


  parent reply	other threads:[~2012-06-08 11:36 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-08  8:48 [v2 1/9] staging:iio: Add helper function for initializing triggered buffers Lars-Peter Clausen
2012-06-08  8:48 ` [v2 2/9] iio:adc:at91: Use new triggered buffer setup helper Lars-Peter Clausen
2012-06-15 16:32   ` Lars-Peter Clausen
2012-06-18  7:56     ` Maxime Ripard
2012-06-18  8:23       ` Lars-Peter Clausen
2012-06-18  8:38         ` Jonathan Cameron
2012-06-18 12:07           ` Maxime Ripard
2012-06-08  8:48 ` [v2 3/9] staging:iio:adc:ad7192: Use new triggered buffer setup helper function Lars-Peter Clausen
2012-06-08  8:48 ` [v2 4/9] staging:iio:adc:ad7298: " Lars-Peter Clausen
2012-06-18  9:45   ` Lars-Peter Clausen
2012-06-18 11:06     ` Jonathan Cameron
2012-06-08  8:48 ` [v2 5/9] staging:iio:adc:ad7476: " Lars-Peter Clausen
2012-06-08  8:48 ` [v2 6/9] staging:iio:adc:ad7606: " Lars-Peter Clausen
2012-06-08  8:48 ` [v2 7/9] staging:iio:adc:ad7793: " Lars-Peter Clausen
2012-06-08  8:48 ` [v2 8/9] staging:iio:adc:ad7887: " Lars-Peter Clausen
2012-06-08  8:48 ` [v2 9/9] staging:iio:adc:ad799x: " Lars-Peter Clausen
2012-06-08 11:36 ` Jonathan Cameron [this message]
2012-06-08 12:13 ` [PATCG v3 1/9] staging:iio: Add helper function for initializing triggered buffers Lars-Peter Clausen
2012-06-08 12:11   ` Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4FD1E3C2.1060100@cam.ac.uk \
    --to=jic23@cam.ac.uk \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).