-
Notifications
You must be signed in to change notification settings - Fork 318
/
block.c
320 lines (251 loc) · 7.13 KB
/
block.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2022 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
*/
#include "iio-private.h"
#include <errno.h>
#include <iio/iio-lock.h>
#include <stdbool.h>
#include <string.h>
struct iio_block {
struct iio_buffer *buffer;
struct iio_block_pdata *pdata;
size_t size;
void *data;
struct iio_task_token *token;
size_t bytes_used;
int dmabuf_fd;
};
struct iio_block *
iio_buffer_create_block(struct iio_buffer *buf, size_t size)
{
const struct iio_device *dev = buf->dev;
const struct iio_backend_ops *ops = dev->ctx->ops;
struct iio_block_pdata *pdata;
size_t sample_size;
struct iio_block *block;
int ret;
sample_size = iio_device_get_sample_size(dev, buf->mask);
if (sample_size == 0 || size < sample_size)
return iio_ptr(-EINVAL);
block = zalloc(sizeof(*block));
if (!block)
return iio_ptr(-ENOMEM);
block->dmabuf_fd = -EINVAL;
if (ops->create_block) {
pdata = ops->create_block(buf->pdata, size, &block->data);
ret = iio_err(pdata);
if (!ret) {
block->pdata = pdata;
if (ops->get_dmabuf_fd)
block->dmabuf_fd = ops->get_dmabuf_fd(pdata);
} else if (ret != -ENOSYS) {
goto err_free_block;
}
}
if (!block->pdata) {
block->data = malloc(size);
if (!block->data) {
ret = -ENOMEM;
goto err_free_block;
}
if (size > buf->length)
buf->length = size;
buf->block_size = size;
}
block->buffer = buf;
block->size = size;
iio_mutex_lock(buf->lock);
buf->nb_blocks++;
iio_mutex_unlock(buf->lock);
return block;
err_free_block:
free(block);
return iio_ptr(ret);
}
void iio_block_destroy(struct iio_block *block)
{
struct iio_buffer *buf = block->buffer;
const struct iio_backend_ops *ops = buf->dev->ctx->ops;
if (block->token) {
iio_task_cancel(block->token);
iio_task_sync(block->token, 0);
}
if (ops->free_block && block->pdata)
ops->free_block(block->pdata);
else
free(block->data);
free(block);
iio_mutex_lock(buf->lock);
buf->nb_blocks--;
iio_mutex_unlock(buf->lock);
}
static int iio_block_write(struct iio_block *block)
{
const struct iio_backend_ops *ops = block->buffer->dev->ctx->ops;
size_t bytes_used = block->bytes_used;
ssize_t ret;
if (!ops->writebuf)
return -ENOSYS;
ret = ops->writebuf(block->buffer->pdata, block->data, bytes_used);
return ret < 0 ? (int) ret : 0;
}
static int iio_block_read(struct iio_block *block)
{
const struct iio_backend_ops *ops = block->buffer->dev->ctx->ops;
size_t bytes_used = block->bytes_used;
ssize_t ret;
if (!ops->readbuf)
return -ENOSYS;
ret = ops->readbuf(block->buffer->pdata, block->data, bytes_used);
return ret < 0 ? (int) ret : 0;
}
int iio_block_io(struct iio_block *block)
{
if (!iio_device_is_tx(block->buffer->dev))
return iio_block_read(block);
return iio_block_write(block);
}
int iio_block_enqueue(struct iio_block *block, size_t bytes_used, bool cyclic)
{
struct iio_buffer *buffer = block->buffer;
const struct iio_device *dev = buffer->dev;
const struct iio_backend_ops *ops = dev->ctx->ops;
if (bytes_used > block->size)
return -EINVAL;
if (!bytes_used)
bytes_used = block->size;
if (ops->enqueue_block && block->pdata)
return ops->enqueue_block(block->pdata, bytes_used, cyclic);
if (block->token) {
/* Already enqueued */
return -EPERM;
}
block->bytes_used = bytes_used;
buffer->cyclic = cyclic;
block->token = iio_task_enqueue(buffer->worker, block);
return iio_err(block->token);
}
int iio_block_dequeue(struct iio_block *block, bool nonblock)
{
struct iio_buffer *buffer = block->buffer;
const struct iio_backend_ops *ops = buffer->dev->ctx->ops;
struct iio_task_token *token;
if (ops->dequeue_block && block->pdata)
return ops->dequeue_block(block->pdata, nonblock);
iio_mutex_lock(buffer->lock);
token = block->token;
if (nonblock && token && !iio_task_is_done(token)) {
iio_mutex_unlock(buffer->lock);
return -EBUSY;
}
block->token = NULL;
iio_mutex_unlock(buffer->lock);
if (!token) {
/* Already dequeued */
return -EPERM;
}
return iio_task_sync(token, 0);
}
void *iio_block_start(const struct iio_block *block)
{
return block->data;
}
void *iio_block_end(const struct iio_block *block)
{
return (void *) ((uintptr_t) block->data + block->size);
}
void *iio_block_first(const struct iio_block *block,
const struct iio_channel *chn)
{
uintptr_t ptr = (uintptr_t)block->data, start = ptr;
const struct iio_device *dev = block->buffer->dev;
const struct iio_buffer *buf = block->buffer;
const struct iio_channel *cur;
unsigned int i;
size_t len;
/* Test if the block has samples for this channel */
if (!iio_channels_mask_test_bit(buf->mask, chn->number))
return iio_block_end(block);
for (i = 0; i < dev->nb_channels; i++) {
cur = dev->channels[i];
len = cur->format.length / 8 * cur->format.repeat;
/* NOTE: dev->channels are ordered by index */
if (cur->index < 0 || cur->index == chn->index)
break;
if (!iio_channels_mask_test_bit(buf->mask, cur->number))
continue;
/* Two channels with the same index use the same samples */
if (i > 0 && cur->index == dev->channels[i - 1]->index)
continue;
if ((ptr - start) % len)
ptr += len - ((ptr - start) % len);
ptr += len;
}
len = chn->format.length / 8;
if ((ptr - start) % len)
ptr += len - ((ptr - start) % len);
return (void *) ptr;
}
ssize_t
iio_block_foreach_sample(const struct iio_block *block,
const struct iio_channels_mask *mask,
ssize_t (*callback)(const struct iio_channel *,
void *, size_t, void *), void *d)
{
uintptr_t ptr = (uintptr_t) block->data,
start = ptr,
end = ptr + block->size;
const struct iio_buffer *buf = block->buffer;
const struct iio_device *dev = buf->dev;
size_t sample_size;
ssize_t processed = 0;
sample_size = iio_device_get_sample_size(dev, buf->mask);
if (sample_size == 0)
return -EINVAL;
while (end - ptr >= (size_t) sample_size) {
unsigned int i;
for (i = 0; i < dev->nb_channels; i++) {
const struct iio_channel *chn = dev->channels[i];
unsigned int length = chn->format.length / 8;
if (chn->index < 0)
break;
/* Test if the block has samples for this channel */
if (!iio_channels_mask_test_bit(buf->mask, chn->number))
continue;
if ((ptr - start) % length)
ptr += length - ((ptr - start) % length);
/* Test if the client wants samples from this channel */
if (iio_channels_mask_test_bit(mask, chn->number)) {
ssize_t ret = callback(chn,
(void *) ptr, length, d);
if (ret < 0)
return ret;
else
processed += ret;
}
if (i == dev->nb_channels - 1 || dev->channels[
i + 1]->index != chn->index)
ptr += length * chn->format.repeat;
}
}
return processed;
}
struct iio_buffer * iio_block_get_buffer(const struct iio_block *block)
{
return block->buffer;
}
int iio_block_get_dmabuf_fd(const struct iio_block *block)
{
return block->dmabuf_fd;
}
int iio_block_disable_cpu_access(struct iio_block *block, bool disable)
{
const struct iio_backend_ops *ops = block->buffer->dev->ctx->ops;
if (ops->disable_cpu_access)
return ops->disable_cpu_access(block->pdata, disable);
return -ENOSYS;
}