1 // SPDX-License-Identifier: GPL-2.0-only
2 /* The industrial I/O callback buffer
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/slab.h>
9 #include <linux/export.h>
10 #include <linux/iio/iio.h>
11 #include <linux/iio/buffer_impl.h>
12 #include <linux/iio/consumer.h>
14 struct iio_cb_buffer
{
15 struct iio_buffer buffer
;
16 int (*cb
)(const void *data
, void *private);
18 struct iio_channel
*channels
;
19 struct iio_dev
*indio_dev
;
22 static struct iio_cb_buffer
*buffer_to_cb_buffer(struct iio_buffer
*buffer
)
24 return container_of(buffer
, struct iio_cb_buffer
, buffer
);
27 static int iio_buffer_cb_store_to(struct iio_buffer
*buffer
, const void *data
)
29 struct iio_cb_buffer
*cb_buff
= buffer_to_cb_buffer(buffer
);
30 return cb_buff
->cb(data
, cb_buff
->private);
33 static void iio_buffer_cb_release(struct iio_buffer
*buffer
)
35 struct iio_cb_buffer
*cb_buff
= buffer_to_cb_buffer(buffer
);
37 bitmap_free(cb_buff
->buffer
.scan_mask
);
41 static const struct iio_buffer_access_funcs iio_cb_access
= {
42 .store_to
= &iio_buffer_cb_store_to
,
43 .release
= &iio_buffer_cb_release
,
45 .modes
= INDIO_BUFFER_SOFTWARE
| INDIO_BUFFER_TRIGGERED
,
48 struct iio_cb_buffer
*iio_channel_get_all_cb(struct device
*dev
,
49 int (*cb
)(const void *data
,
54 struct iio_cb_buffer
*cb_buff
;
55 struct iio_channel
*chan
;
58 dev_err(dev
, "Invalid arguments: A callback must be provided!\n");
59 return ERR_PTR(-EINVAL
);
62 cb_buff
= kzalloc(sizeof(*cb_buff
), GFP_KERNEL
);
64 return ERR_PTR(-ENOMEM
);
66 iio_buffer_init(&cb_buff
->buffer
);
68 cb_buff
->private = private;
70 cb_buff
->buffer
.access
= &iio_cb_access
;
71 INIT_LIST_HEAD(&cb_buff
->buffer
.demux_list
);
73 cb_buff
->channels
= iio_channel_get_all(dev
);
74 if (IS_ERR(cb_buff
->channels
)) {
75 ret
= PTR_ERR(cb_buff
->channels
);
76 goto error_free_cb_buff
;
79 cb_buff
->indio_dev
= cb_buff
->channels
[0].indio_dev
;
80 cb_buff
->buffer
.scan_mask
= bitmap_zalloc(cb_buff
->indio_dev
->masklength
,
82 if (cb_buff
->buffer
.scan_mask
== NULL
) {
84 goto error_release_channels
;
86 chan
= &cb_buff
->channels
[0];
87 while (chan
->indio_dev
) {
88 if (chan
->indio_dev
!= cb_buff
->indio_dev
) {
90 goto error_free_scan_mask
;
92 set_bit(chan
->channel
->scan_index
,
93 cb_buff
->buffer
.scan_mask
);
100 bitmap_free(cb_buff
->buffer
.scan_mask
);
101 error_release_channels
:
102 iio_channel_release_all(cb_buff
->channels
);
107 EXPORT_SYMBOL_GPL(iio_channel_get_all_cb
);
109 int iio_channel_cb_set_buffer_watermark(struct iio_cb_buffer
*cb_buff
,
114 cb_buff
->buffer
.watermark
= watermark
;
118 EXPORT_SYMBOL_GPL(iio_channel_cb_set_buffer_watermark
);
120 int iio_channel_start_all_cb(struct iio_cb_buffer
*cb_buff
)
122 return iio_update_buffers(cb_buff
->indio_dev
, &cb_buff
->buffer
,
125 EXPORT_SYMBOL_GPL(iio_channel_start_all_cb
);
127 void iio_channel_stop_all_cb(struct iio_cb_buffer
*cb_buff
)
129 iio_update_buffers(cb_buff
->indio_dev
, NULL
, &cb_buff
->buffer
);
131 EXPORT_SYMBOL_GPL(iio_channel_stop_all_cb
);
133 void iio_channel_release_all_cb(struct iio_cb_buffer
*cb_buff
)
135 iio_channel_release_all(cb_buff
->channels
);
136 iio_buffer_put(&cb_buff
->buffer
);
138 EXPORT_SYMBOL_GPL(iio_channel_release_all_cb
);
141 *iio_channel_cb_get_channels(const struct iio_cb_buffer
*cb_buffer
)
143 return cb_buffer
->channels
;
145 EXPORT_SYMBOL_GPL(iio_channel_cb_get_channels
);
148 *iio_channel_cb_get_iio_dev(const struct iio_cb_buffer
*cb_buffer
)
150 return cb_buffer
->indio_dev
;
152 EXPORT_SYMBOL_GPL(iio_channel_cb_get_iio_dev
);
154 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
155 MODULE_DESCRIPTION("Industrial I/O callback buffer");
156 MODULE_LICENSE("GPL");