1 #include <linux/kernel.h>
2 #include <linux/slab.h>
4 #include <linux/export.h>
5 #include <linux/iio/buffer.h>
6 #include <linux/iio/consumer.h>
9 struct iio_buffer buffer
;
10 int (*cb
)(const void *data
, void *private);
12 struct iio_channel
*channels
;
15 static struct iio_cb_buffer
*buffer_to_cb_buffer(struct iio_buffer
*buffer
)
17 return container_of(buffer
, struct iio_cb_buffer
, buffer
);
20 static int iio_buffer_cb_store_to(struct iio_buffer
*buffer
, const void *data
)
22 struct iio_cb_buffer
*cb_buff
= buffer_to_cb_buffer(buffer
);
23 return cb_buff
->cb(data
, cb_buff
->private);
26 static void iio_buffer_cb_release(struct iio_buffer
*buffer
)
28 struct iio_cb_buffer
*cb_buff
= buffer_to_cb_buffer(buffer
);
29 kfree(cb_buff
->buffer
.scan_mask
);
33 static const struct iio_buffer_access_funcs iio_cb_access
= {
34 .store_to
= &iio_buffer_cb_store_to
,
35 .release
= &iio_buffer_cb_release
,
38 struct iio_cb_buffer
*iio_channel_get_all_cb(struct device
*dev
,
39 int (*cb
)(const void *data
,
44 struct iio_cb_buffer
*cb_buff
;
45 struct iio_dev
*indio_dev
;
46 struct iio_channel
*chan
;
48 cb_buff
= kzalloc(sizeof(*cb_buff
), GFP_KERNEL
);
50 return ERR_PTR(-ENOMEM
);
52 iio_buffer_init(&cb_buff
->buffer
);
54 cb_buff
->private = private;
56 cb_buff
->buffer
.access
= &iio_cb_access
;
57 INIT_LIST_HEAD(&cb_buff
->buffer
.demux_list
);
59 cb_buff
->channels
= iio_channel_get_all(dev
);
60 if (IS_ERR(cb_buff
->channels
)) {
61 ret
= PTR_ERR(cb_buff
->channels
);
62 goto error_free_cb_buff
;
65 indio_dev
= cb_buff
->channels
[0].indio_dev
;
66 cb_buff
->buffer
.scan_mask
67 = kcalloc(BITS_TO_LONGS(indio_dev
->masklength
), sizeof(long),
69 if (cb_buff
->buffer
.scan_mask
== NULL
) {
71 goto error_release_channels
;
73 chan
= &cb_buff
->channels
[0];
74 while (chan
->indio_dev
) {
75 if (chan
->indio_dev
!= indio_dev
) {
77 goto error_free_scan_mask
;
79 set_bit(chan
->channel
->scan_index
,
80 cb_buff
->buffer
.scan_mask
);
87 kfree(cb_buff
->buffer
.scan_mask
);
88 error_release_channels
:
89 iio_channel_release_all(cb_buff
->channels
);
94 EXPORT_SYMBOL_GPL(iio_channel_get_all_cb
);
96 int iio_channel_start_all_cb(struct iio_cb_buffer
*cb_buff
)
98 return iio_update_buffers(cb_buff
->channels
[0].indio_dev
,
102 EXPORT_SYMBOL_GPL(iio_channel_start_all_cb
);
104 void iio_channel_stop_all_cb(struct iio_cb_buffer
*cb_buff
)
106 iio_update_buffers(cb_buff
->channels
[0].indio_dev
,
110 EXPORT_SYMBOL_GPL(iio_channel_stop_all_cb
);
112 void iio_channel_release_all_cb(struct iio_cb_buffer
*cb_buff
)
114 iio_channel_release_all(cb_buff
->channels
);
115 iio_buffer_put(&cb_buff
->buffer
);
117 EXPORT_SYMBOL_GPL(iio_channel_release_all_cb
);
120 *iio_channel_cb_get_channels(const struct iio_cb_buffer
*cb_buffer
)
122 return cb_buffer
->channels
;
124 EXPORT_SYMBOL_GPL(iio_channel_cb_get_channels
);