Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / iio / buffer / industrialio-buffer-cb.c
blob4c12b7a94af59ae9c3ac01b17b8704f85ec22e87
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* The industrial I/O callback buffer
3 */
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/err.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);
17 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);
38 kfree(cb_buff);
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,
50 void *private),
51 void *private)
53 int ret;
54 struct iio_cb_buffer *cb_buff;
55 struct iio_channel *chan;
57 if (!cb) {
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);
63 if (cb_buff == NULL)
64 return ERR_PTR(-ENOMEM);
66 iio_buffer_init(&cb_buff->buffer);
68 cb_buff->private = private;
69 cb_buff->cb = cb;
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,
81 GFP_KERNEL);
82 if (cb_buff->buffer.scan_mask == NULL) {
83 ret = -ENOMEM;
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) {
89 ret = -EINVAL;
90 goto error_free_scan_mask;
92 set_bit(chan->channel->scan_index,
93 cb_buff->buffer.scan_mask);
94 chan++;
97 return cb_buff;
99 error_free_scan_mask:
100 bitmap_free(cb_buff->buffer.scan_mask);
101 error_release_channels:
102 iio_channel_release_all(cb_buff->channels);
103 error_free_cb_buff:
104 kfree(cb_buff);
105 return ERR_PTR(ret);
107 EXPORT_SYMBOL_GPL(iio_channel_get_all_cb);
109 int iio_channel_cb_set_buffer_watermark(struct iio_cb_buffer *cb_buff,
110 size_t watermark)
112 if (!watermark)
113 return -EINVAL;
114 cb_buff->buffer.watermark = watermark;
116 return 0;
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,
123 NULL);
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);
140 struct iio_channel
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);
147 struct iio_dev
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");