GNU Linux-libre 4.9.282-gnu1
[releases.git] / drivers / iio / buffer / industrialio-buffer-cb.c
1 /* The industrial I/O callback buffer
2  *
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License version 2 as published by
5  * the Free Software Foundation.
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/err.h>
12 #include <linux/export.h>
13 #include <linux/iio/buffer.h>
14 #include <linux/iio/consumer.h>
15
16 struct iio_cb_buffer {
17         struct iio_buffer buffer;
18         int (*cb)(const void *data, void *private);
19         void *private;
20         struct iio_channel *channels;
21         struct iio_dev *indio_dev;
22 };
23
24 static struct iio_cb_buffer *buffer_to_cb_buffer(struct iio_buffer *buffer)
25 {
26         return container_of(buffer, struct iio_cb_buffer, buffer);
27 }
28
29 static int iio_buffer_cb_store_to(struct iio_buffer *buffer, const void *data)
30 {
31         struct iio_cb_buffer *cb_buff = buffer_to_cb_buffer(buffer);
32         return cb_buff->cb(data, cb_buff->private);
33 }
34
35 static void iio_buffer_cb_release(struct iio_buffer *buffer)
36 {
37         struct iio_cb_buffer *cb_buff = buffer_to_cb_buffer(buffer);
38         kfree(cb_buff->buffer.scan_mask);
39         kfree(cb_buff);
40 }
41
42 static const struct iio_buffer_access_funcs iio_cb_access = {
43         .store_to = &iio_buffer_cb_store_to,
44         .release = &iio_buffer_cb_release,
45
46         .modes = INDIO_BUFFER_SOFTWARE | INDIO_BUFFER_TRIGGERED,
47 };
48
49 struct iio_cb_buffer *iio_channel_get_all_cb(struct device *dev,
50                                              int (*cb)(const void *data,
51                                                        void *private),
52                                              void *private)
53 {
54         int ret;
55         struct iio_cb_buffer *cb_buff;
56         struct iio_channel *chan;
57
58         cb_buff = kzalloc(sizeof(*cb_buff), GFP_KERNEL);
59         if (cb_buff == NULL)
60                 return ERR_PTR(-ENOMEM);
61
62         iio_buffer_init(&cb_buff->buffer);
63
64         cb_buff->private = private;
65         cb_buff->cb = cb;
66         cb_buff->buffer.access = &iio_cb_access;
67         INIT_LIST_HEAD(&cb_buff->buffer.demux_list);
68
69         cb_buff->channels = iio_channel_get_all(dev);
70         if (IS_ERR(cb_buff->channels)) {
71                 ret = PTR_ERR(cb_buff->channels);
72                 goto error_free_cb_buff;
73         }
74
75         cb_buff->indio_dev = cb_buff->channels[0].indio_dev;
76         cb_buff->buffer.scan_mask
77                 = kcalloc(BITS_TO_LONGS(cb_buff->indio_dev->masklength),
78                           sizeof(long), GFP_KERNEL);
79         if (cb_buff->buffer.scan_mask == NULL) {
80                 ret = -ENOMEM;
81                 goto error_release_channels;
82         }
83         chan = &cb_buff->channels[0];
84         while (chan->indio_dev) {
85                 if (chan->indio_dev != cb_buff->indio_dev) {
86                         ret = -EINVAL;
87                         goto error_free_scan_mask;
88                 }
89                 set_bit(chan->channel->scan_index,
90                         cb_buff->buffer.scan_mask);
91                 chan++;
92         }
93
94         return cb_buff;
95
96 error_free_scan_mask:
97         kfree(cb_buff->buffer.scan_mask);
98 error_release_channels:
99         iio_channel_release_all(cb_buff->channels);
100 error_free_cb_buff:
101         kfree(cb_buff);
102         return ERR_PTR(ret);
103 }
104 EXPORT_SYMBOL_GPL(iio_channel_get_all_cb);
105
106 int iio_channel_start_all_cb(struct iio_cb_buffer *cb_buff)
107 {
108         return iio_update_buffers(cb_buff->indio_dev, &cb_buff->buffer,
109                                   NULL);
110 }
111 EXPORT_SYMBOL_GPL(iio_channel_start_all_cb);
112
113 void iio_channel_stop_all_cb(struct iio_cb_buffer *cb_buff)
114 {
115         iio_update_buffers(cb_buff->indio_dev, NULL, &cb_buff->buffer);
116 }
117 EXPORT_SYMBOL_GPL(iio_channel_stop_all_cb);
118
119 void iio_channel_release_all_cb(struct iio_cb_buffer *cb_buff)
120 {
121         iio_channel_release_all(cb_buff->channels);
122         iio_buffer_put(&cb_buff->buffer);
123 }
124 EXPORT_SYMBOL_GPL(iio_channel_release_all_cb);
125
126 struct iio_channel
127 *iio_channel_cb_get_channels(const struct iio_cb_buffer *cb_buffer)
128 {
129         return cb_buffer->channels;
130 }
131 EXPORT_SYMBOL_GPL(iio_channel_cb_get_channels);
132
133 struct iio_dev
134 *iio_channel_cb_get_iio_dev(const struct iio_cb_buffer *cb_buffer)
135 {
136         return cb_buffer->indio_dev;
137 }
138 EXPORT_SYMBOL_GPL(iio_channel_cb_get_iio_dev);
139
140 MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
141 MODULE_DESCRIPTION("Industrial I/O callback buffer");
142 MODULE_LICENSE("GPL");