Loading...
1 /*
2 * Copyright (c) 2012 Analog Devices, Inc.
3 * Author: Lars-Peter Clausen <lars@metafoo.de>
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
10#include <linux/kernel.h>
11#include <linux/export.h>
12#include <linux/module.h>
13#include <linux/iio/iio.h>
14#include <linux/iio/buffer.h>
15#include <linux/iio/kfifo_buf.h>
16#include <linux/iio/triggered_buffer.h>
17#include <linux/iio/trigger_consumer.h>
18
19static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = {
20 .postenable = &iio_triggered_buffer_postenable,
21 .predisable = &iio_triggered_buffer_predisable,
22};
23
24/**
25 * iio_triggered_buffer_setup() - Setup triggered buffer and pollfunc
26 * @indio_dev: IIO device structure
27 * @h: Function which will be used as pollfunc top half
28 * @thread: Function which will be used as pollfunc bottom half
29 * @setup_ops: Buffer setup functions to use for this device.
30 * If NULL the default setup functions for triggered
31 * buffers will be used.
32 *
33 * This function combines some common tasks which will normally be performed
34 * when setting up a triggered buffer. It will allocate the buffer and the
35 * pollfunc.
36 *
37 * Before calling this function the indio_dev structure should already be
38 * completely initialized, but not yet registered. In practice this means that
39 * this function should be called right before iio_device_register().
40 *
41 * To free the resources allocated by this function call
42 * iio_triggered_buffer_cleanup().
43 */
44int iio_triggered_buffer_setup(struct iio_dev *indio_dev,
45 irqreturn_t (*h)(int irq, void *p),
46 irqreturn_t (*thread)(int irq, void *p),
47 const struct iio_buffer_setup_ops *setup_ops)
48{
49 struct iio_buffer *buffer;
50 int ret;
51
52 buffer = iio_kfifo_allocate();
53 if (!buffer) {
54 ret = -ENOMEM;
55 goto error_ret;
56 }
57
58 iio_device_attach_buffer(indio_dev, buffer);
59
60 indio_dev->pollfunc = iio_alloc_pollfunc(h,
61 thread,
62 IRQF_ONESHOT,
63 indio_dev,
64 "%s_consumer%d",
65 indio_dev->name,
66 indio_dev->id);
67 if (indio_dev->pollfunc == NULL) {
68 ret = -ENOMEM;
69 goto error_kfifo_free;
70 }
71
72 /* Ring buffer functions - here trigger setup related */
73 if (setup_ops)
74 indio_dev->setup_ops = setup_ops;
75 else
76 indio_dev->setup_ops = &iio_triggered_buffer_setup_ops;
77
78 /* Flag that polled ring buffering is possible */
79 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
80
81 return 0;
82
83error_kfifo_free:
84 iio_kfifo_free(indio_dev->buffer);
85error_ret:
86 return ret;
87}
88EXPORT_SYMBOL(iio_triggered_buffer_setup);
89
90/**
91 * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup()
92 * @indio_dev: IIO device structure
93 */
94void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
95{
96 iio_dealloc_pollfunc(indio_dev->pollfunc);
97 iio_kfifo_free(indio_dev->buffer);
98}
99EXPORT_SYMBOL(iio_triggered_buffer_cleanup);
100
101MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
102MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
103MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2012 Analog Devices, Inc.
4 * Author: Lars-Peter Clausen <lars@metafoo.de>
5 */
6
7#include <linux/kernel.h>
8#include <linux/export.h>
9#include <linux/module.h>
10#include <linux/iio/iio.h>
11#include <linux/iio/buffer.h>
12#include <linux/iio/buffer_impl.h>
13#include <linux/iio/kfifo_buf.h>
14#include <linux/iio/triggered_buffer.h>
15#include <linux/iio/trigger_consumer.h>
16
17/**
18 * iio_triggered_buffer_setup_ext() - Setup triggered buffer and pollfunc
19 * @indio_dev: IIO device structure
20 * @h: Function which will be used as pollfunc top half
21 * @thread: Function which will be used as pollfunc bottom half
22 * @direction: Direction of the data stream (in/out).
23 * @setup_ops: Buffer setup functions to use for this device.
24 * If NULL the default setup functions for triggered
25 * buffers will be used.
26 * @buffer_attrs: Extra sysfs buffer attributes for this IIO buffer
27 *
28 * This function combines some common tasks which will normally be performed
29 * when setting up a triggered buffer. It will allocate the buffer and the
30 * pollfunc.
31 *
32 * Before calling this function the indio_dev structure should already be
33 * completely initialized, but not yet registered. In practice this means that
34 * this function should be called right before iio_device_register().
35 *
36 * To free the resources allocated by this function call
37 * iio_triggered_buffer_cleanup().
38 */
39int iio_triggered_buffer_setup_ext(struct iio_dev *indio_dev,
40 irqreturn_t (*h)(int irq, void *p),
41 irqreturn_t (*thread)(int irq, void *p),
42 enum iio_buffer_direction direction,
43 const struct iio_buffer_setup_ops *setup_ops,
44 const struct iio_dev_attr **buffer_attrs)
45{
46 struct iio_buffer *buffer;
47 int ret;
48
49 /*
50 * iio_triggered_buffer_cleanup() assumes that the buffer allocated here
51 * is assigned to indio_dev->buffer but this is only the case if this
52 * function is the first caller to iio_device_attach_buffer(). If
53 * indio_dev->buffer is already set then we can't proceed otherwise the
54 * cleanup function will try to free a buffer that was not allocated here.
55 */
56 if (indio_dev->buffer)
57 return -EADDRINUSE;
58
59 buffer = iio_kfifo_allocate();
60 if (!buffer) {
61 ret = -ENOMEM;
62 goto error_ret;
63 }
64
65 indio_dev->pollfunc = iio_alloc_pollfunc(h,
66 thread,
67 IRQF_ONESHOT,
68 indio_dev,
69 "%s_consumer%d",
70 indio_dev->name,
71 iio_device_id(indio_dev));
72 if (indio_dev->pollfunc == NULL) {
73 ret = -ENOMEM;
74 goto error_kfifo_free;
75 }
76
77 /* Ring buffer functions - here trigger setup related */
78 indio_dev->setup_ops = setup_ops;
79
80 /* Flag that polled ring buffering is possible */
81 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
82
83 buffer->direction = direction;
84 buffer->attrs = buffer_attrs;
85
86 ret = iio_device_attach_buffer(indio_dev, buffer);
87 if (ret < 0)
88 goto error_dealloc_pollfunc;
89
90 return 0;
91
92error_dealloc_pollfunc:
93 iio_dealloc_pollfunc(indio_dev->pollfunc);
94error_kfifo_free:
95 iio_kfifo_free(buffer);
96error_ret:
97 return ret;
98}
99EXPORT_SYMBOL(iio_triggered_buffer_setup_ext);
100
101/**
102 * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup_ext()
103 * @indio_dev: IIO device structure
104 */
105void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
106{
107 iio_dealloc_pollfunc(indio_dev->pollfunc);
108 iio_kfifo_free(indio_dev->buffer);
109}
110EXPORT_SYMBOL(iio_triggered_buffer_cleanup);
111
112static void devm_iio_triggered_buffer_clean(void *indio_dev)
113{
114 iio_triggered_buffer_cleanup(indio_dev);
115}
116
117int devm_iio_triggered_buffer_setup_ext(struct device *dev,
118 struct iio_dev *indio_dev,
119 irqreturn_t (*h)(int irq, void *p),
120 irqreturn_t (*thread)(int irq, void *p),
121 enum iio_buffer_direction direction,
122 const struct iio_buffer_setup_ops *ops,
123 const struct iio_dev_attr **buffer_attrs)
124{
125 int ret;
126
127 ret = iio_triggered_buffer_setup_ext(indio_dev, h, thread, direction,
128 ops, buffer_attrs);
129 if (ret)
130 return ret;
131
132 return devm_add_action_or_reset(dev, devm_iio_triggered_buffer_clean,
133 indio_dev);
134}
135EXPORT_SYMBOL_GPL(devm_iio_triggered_buffer_setup_ext);
136
137MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
138MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
139MODULE_LICENSE("GPL");