forked from analogdevicesinc/libiio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.c
417 lines (335 loc) · 8.89 KB
/
device.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2014 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
*/
#include "attr.h"
#include "iio-private.h"
#include "sort.h"
#include <iio/iio-debug.h>
#include <inttypes.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
static const char * const xml_attr_prefix[] = {
"",
"debug-",
"buffer-",
};
static ssize_t iio_snprintf_xml_attr(const struct iio_attr *attr,
char *buf, ssize_t len)
{
switch (attr->type) {
case IIO_ATTR_TYPE_DEVICE:
case IIO_ATTR_TYPE_DEBUG:
case IIO_ATTR_TYPE_BUFFER:
return iio_snprintf(buf, len,
"<%sattribute name=\"%s\" />",
xml_attr_prefix[attr->type],
attr->name);
default:
return -EINVAL;
}
}
ssize_t iio_snprintf_device_xml(char *ptr, ssize_t len,
const struct iio_device *dev)
{
const struct iio_attr *attrs;
ssize_t ret, alen = 0;
unsigned int i;
enum iio_attr_type type;
ret = iio_snprintf(ptr, len, "<device id=\"%s\"", dev->id);
if (ret < 0)
return ret;
iio_update_xml_indexes(ret, &ptr, &len, &alen);
if (dev->name) {
ret = iio_snprintf(ptr, len, " name=\"%s\"", dev->name);
if (ret < 0)
return ret;
iio_update_xml_indexes(ret, &ptr, &len, &alen);
}
if (dev->label) {
ret = iio_snprintf(ptr, len, " label=\"%s\"", dev->label);
if (ret < 0)
return ret;
iio_update_xml_indexes(ret, &ptr, &len, &alen);
}
ret = iio_snprintf(ptr, len, " >");
if (ret < 0)
return ret;
iio_update_xml_indexes(ret, &ptr, &len, &alen);
for (i = 0; i < dev->nb_channels; i++) {
ret = iio_snprintf_channel_xml(ptr, len, dev->channels[i]);
if (ret < 0)
return ret;
iio_update_xml_indexes(ret, &ptr, &len, &alen);
}
for (type = IIO_ATTR_TYPE_DEVICE; type <= IIO_ATTR_TYPE_BUFFER; type++) {
for (i = 0; i < dev->attrlist[type].num; i++) {
attrs = dev->attrlist[type].attrs;
ret = iio_snprintf_xml_attr(&attrs[i], ptr, len);
if (ret < 0)
return ret;
iio_update_xml_indexes(ret, &ptr, &len, &alen);
}
}
ret = iio_snprintf(ptr, len, "</device>");
if (ret < 0)
return ret;
return alen + ret;
}
const char * iio_device_get_id(const struct iio_device *dev)
{
return dev->id;
}
const char * iio_device_get_name(const struct iio_device *dev)
{
return dev->name;
}
const char * iio_device_get_label(const struct iio_device *dev)
{
return dev->label;
}
unsigned int iio_device_get_channels_count(const struct iio_device *dev)
{
return dev->nb_channels;
}
struct iio_channel * iio_device_get_channel(const struct iio_device *dev,
unsigned int index)
{
if (index >= dev->nb_channels)
return NULL;
else
return dev->channels[index];
}
struct iio_channel * iio_device_find_channel(const struct iio_device *dev,
const char *name, bool output)
{
unsigned int i;
for (i = 0; i < dev->nb_channels; i++) {
struct iio_channel *chn = dev->channels[i];
if (iio_channel_is_output(chn) != output)
continue;
if (!strcmp(chn->id, name) ||
(chn->name && !strcmp(chn->name, name)))
return chn;
}
return NULL;
}
unsigned int iio_device_get_attrs_count(const struct iio_device *dev)
{
return dev->attrlist[IIO_ATTR_TYPE_DEVICE].num;
}
const struct iio_attr *
iio_device_get_attr(const struct iio_device *dev, unsigned int index)
{
return iio_attr_get(&dev->attrlist[IIO_ATTR_TYPE_DEVICE], index);
}
const struct iio_attr *
iio_device_find_attr(const struct iio_device *dev, const char *name)
{
return iio_attr_find(&dev->attrlist[IIO_ATTR_TYPE_DEVICE], name);
}
unsigned int iio_device_get_debug_attrs_count(const struct iio_device *dev)
{
return dev->attrlist[IIO_ATTR_TYPE_DEBUG].num;
}
const struct iio_attr *
iio_device_get_debug_attr(const struct iio_device *dev, unsigned int index)
{
return iio_attr_get(&dev->attrlist[IIO_ATTR_TYPE_DEBUG], index);
}
const struct iio_attr *
iio_device_find_debug_attr(const struct iio_device *dev, const char *name)
{
return iio_attr_find(&dev->attrlist[IIO_ATTR_TYPE_DEBUG], name);
}
bool iio_device_is_tx(const struct iio_device *dev)
{
struct iio_channel *ch;
unsigned int i;
for (i = 0; i < dev->nb_channels; i++) {
ch = dev->channels[i];
if (iio_channel_is_output(ch) && iio_channel_is_scan_element(ch))
return true;
}
return false;
}
void iio_device_set_data(struct iio_device *dev, void *data)
{
dev->userdata = data;
}
void * iio_device_get_data(const struct iio_device *dev)
{
return dev->userdata;
}
bool iio_device_is_trigger(const struct iio_device *dev)
{
/* A trigger has a name, an id which starts by "trigger",
* and zero channels. */
unsigned int nb = iio_device_get_channels_count(dev);
const char *name = iio_device_get_name(dev),
*id = iio_device_get_id(dev);
return ((nb == 0) && !!name &&
!strncmp(id, "trigger", sizeof("trigger") - 1));
}
const struct iio_device *
iio_device_get_trigger(const struct iio_device *dev)
{
if (dev->ctx->ops->get_trigger)
return dev->ctx->ops->get_trigger(dev);
else
return iio_ptr(-ENOSYS);
}
int iio_device_set_trigger(const struct iio_device *dev,
const struct iio_device *trigger)
{
if (trigger && !iio_device_is_trigger(trigger))
return -EINVAL;
else if (dev->ctx->ops->set_trigger)
return dev->ctx->ops->set_trigger(dev, trigger);
else
return -ENOSYS;
}
void free_device(struct iio_device *dev)
{
enum iio_attr_type type;
unsigned int i;
for (type = IIO_ATTR_TYPE_DEVICE; type <= IIO_ATTR_TYPE_BUFFER; type++)
iio_free_attrs(&dev->attrlist[type]);
for (i = 0; i < dev->nb_channels; i++)
free_channel(dev->channels[i]);
free(dev->channels);
free(dev->label);
free(dev->name);
free(dev->id);
free(dev);
}
ssize_t iio_device_get_sample_size(const struct iio_device *dev,
const struct iio_channels_mask *mask)
{
ssize_t size = 0;
unsigned int i, largest = 1;
const struct iio_channel *prev = NULL;
if (mask->words != (dev->nb_channels + 31) / 32)
return -EINVAL;
for (i = 0; i < dev->nb_channels; i++) {
const struct iio_channel *chn = dev->channels[i];
unsigned int length = chn->format.length / 8 *
chn->format.repeat;
if (chn->index < 0)
break;
if (!iio_channels_mask_test_bit(mask, chn->number))
continue;
if (prev && chn->index == prev->index) {
prev = chn;
continue;
}
if (length > largest)
largest = length;
if (size % length)
size += 2 * length - (size % length);
else
size += length;
prev = chn;
}
if (size % largest)
size += largest - (size % largest);
return size;
}
int iio_device_reg_write(struct iio_device *dev,
uint32_t address, uint32_t value)
{
const struct iio_attr *attr;
ssize_t ret;
char buf[1024];
attr = iio_device_find_debug_attr(dev, "direct_reg_access");
if (!attr)
return -ENOENT;
iio_snprintf(buf, sizeof(buf), "0x%" PRIx32 " 0x%" PRIx32,
address, value);
ret = iio_attr_write_string(attr, buf);
return (int) (ret < 0 ? ret : 0);
}
int iio_device_reg_read(struct iio_device *dev,
uint32_t address, uint32_t *value)
{
/* NOTE: There is a race condition here. But it is extremely unlikely to
* happen, and as this is a debug function, it shouldn't be used for
* something else than debug. */
const struct iio_attr *attr;
long long val;
int ret;
attr = iio_device_find_debug_attr(dev, "direct_reg_access");
if (!attr)
return -ENOENT;
ret = iio_attr_write_longlong(attr, (long long) address);
if (ret < 0)
return ret;
ret = iio_attr_read_longlong(attr, &val);
if (!ret)
*value = (uint32_t) val;
return ret;
}
const struct iio_context * iio_device_get_context(const struct iio_device *dev)
{
return dev->ctx;
}
struct iio_device_pdata * iio_device_get_pdata(const struct iio_device *dev)
{
return dev->pdata;
}
void iio_device_set_pdata(struct iio_device *dev, struct iio_device_pdata *d)
{
dev->pdata = d;
}
struct iio_channel * iio_device_add_channel(struct iio_device *dev, long index,
const char *id, const char *name,
bool output, bool scan_element,
const struct iio_data_format *fmt)
{
struct iio_channel *chn, **chns;
char *new_id, *new_name = NULL;
unsigned int i;
chn = zalloc(sizeof(*chn));
if (!chn)
return NULL;
new_id = iio_strdup(id);
if (!new_id)
goto err_free_chn;
if (name) {
new_name = iio_strdup(name);
if (!new_name)
goto err_free_id;
}
chn->id = new_id;
chn->name = new_name;
chn->dev = dev;
chn->is_output = output;
chn->is_scan_element = scan_element;
chn->index = index;
memcpy(&chn->format, fmt, sizeof(*fmt));
if (!chn->format.repeat)
chn->format.repeat = 1;
chns = realloc(dev->channels, (dev->nb_channels + 1) * sizeof(*chn));
if (!chns)
goto err_free_name;
chns[dev->nb_channels++] = chn;
dev->channels = chns;
dev_dbg(dev, "Added channel \'%s\'\n", id);
iio_channel_init_finalize(chn);
/* Reorder channels by index */
iio_sort_channels(dev);
for (i = 0; i < dev->nb_channels; i++)
dev->channels[i]->number = i;
return chn;
err_free_name:
free(new_name);
err_free_id:
free(new_id);
err_free_chn:
free(chn);
return NULL;
}