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
;
57 cb_buff
= kzalloc(sizeof(*cb_buff
), GFP_KERNEL
);
59 return ERR_PTR(-ENOMEM
);
61 iio_buffer_init(&cb_buff
->buffer
);
63 cb_buff
->private = private;
65 cb_buff
->buffer
.access
= &iio_cb_access
;
66 INIT_LIST_HEAD(&cb_buff
->buffer
.demux_list
);
68 cb_buff
->channels
= iio_channel_get_all(dev
);
69 if (IS_ERR(cb_buff
->channels
)) {
70 ret
= PTR_ERR(cb_buff
->channels
);
71 goto error_free_cb_buff
;
74 cb_buff
->indio_dev
= cb_buff
->channels
[0].indio_dev
;
75 cb_buff
->buffer
.scan_mask
= bitmap_zalloc(cb_buff
->indio_dev
->masklength
,
77 if (cb_buff
->buffer
.scan_mask
== NULL
) {
79 goto error_release_channels
;
81 chan
= &cb_buff
->channels
[0];
82 while (chan
->indio_dev
) {
83 if (chan
->indio_dev
!= cb_buff
->indio_dev
) {
85 goto error_free_scan_mask
;
87 set_bit(chan
->channel
->scan_index
,
88 cb_buff
->buffer
.scan_mask
);
95 bitmap_free(cb_buff
->buffer
.scan_mask
);
96 error_release_channels
:
97 iio_channel_release_all(cb_buff
->channels
);
102 EXPORT_SYMBOL_GPL(iio_channel_get_all_cb
);
104 int iio_channel_cb_set_buffer_watermark(struct iio_cb_buffer
*cb_buff
,
109 cb_buff
->buffer
.watermark
= watermark
;
113 EXPORT_SYMBOL_GPL(iio_channel_cb_set_buffer_watermark
);
115 int iio_channel_start_all_cb(struct iio_cb_buffer
*cb_buff
)
117 return iio_update_buffers(cb_buff
->indio_dev
, &cb_buff
->buffer
,
120 EXPORT_SYMBOL_GPL(iio_channel_start_all_cb
);
122 void iio_channel_stop_all_cb(struct iio_cb_buffer
*cb_buff
)
124 iio_update_buffers(cb_buff
->indio_dev
, NULL
, &cb_buff
->buffer
);
126 EXPORT_SYMBOL_GPL(iio_channel_stop_all_cb
);
128 void iio_channel_release_all_cb(struct iio_cb_buffer
*cb_buff
)
130 iio_channel_release_all(cb_buff
->channels
);
131 iio_buffer_put(&cb_buff
->buffer
);
133 EXPORT_SYMBOL_GPL(iio_channel_release_all_cb
);
136 *iio_channel_cb_get_channels(const struct iio_cb_buffer
*cb_buffer
)
138 return cb_buffer
->channels
;
140 EXPORT_SYMBOL_GPL(iio_channel_cb_get_channels
);
143 *iio_channel_cb_get_iio_dev(const struct iio_cb_buffer
*cb_buffer
)
145 return cb_buffer
->indio_dev
;
147 EXPORT_SYMBOL_GPL(iio_channel_cb_get_iio_dev
);
149 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
150 MODULE_DESCRIPTION("Industrial I/O callback buffer");
151 MODULE_LICENSE("GPL");