Repository navigation
Expand file tree
/
Copy pathdevice.c
More file actions
572 lines (456 loc) · 12.5 KB
/
Copy pathdevice.c
File metadata and controls
572 lines (456 loc) · 12.5 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// SPDX-License-Identifier: MIT
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2014 Analog Devices, Inc.
* Author: Paul Cercueil <paul.cercueil@analog.com>
*/
#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,
const char *value,
char *buf, ssize_t len)
{
ssize_t ret, alen = 0;
switch (attr->type) {
case IIO_ATTR_TYPE_DEVICE:
case IIO_ATTR_TYPE_DEBUG:
case IIO_ATTR_TYPE_BUFFER:
break;
default:
return -EINVAL;
}
ret = iio_snprintf(buf, len, "<%sattribute name=\"%s\"",
xml_attr_prefix[attr->type], attr->name);
if (ret < 0)
return ret;
iio_update_xml_indexes(ret, &buf, &len, &alen);
if (value) {
ret = iio_xml_print_and_sanitized_param(buf, len,
" value=\"",
value, "\"");
if (ret < 0)
return ret;
iio_update_xml_indexes(ret, &buf, &len, &alen);
}
ret = iio_snprintf(buf, len, " />");
if (ret < 0)
return ret;
return alen + ret;
}
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;
bool include_values = dev->ctx->params.flags
& IIO_CTX_XML_INCLUDE_VALUES;
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 (i = 0; i < dev->nb_buffers; i++) {
const struct iio_buffer *buf = dev->buffers[i];
unsigned int j;
ret = iio_snprintf(ptr, len, "<buffer index=\"%u\" >", buf->idx);
if (ret < 0)
return ret;
iio_update_xml_indexes(ret, &ptr, &len, &alen);
for (j = 0; j < buf->attrlist.num; j++) {
const char *val = NULL;
if (include_values && buf->values)
val = buf->values[j];
ret = iio_snprintf(ptr, len, "<attribute name=\"%s\"", buf->attrlist.attrs[j].name);
if (ret < 0)
return ret;
iio_update_xml_indexes(ret, &ptr, &len, &alen);
if (val) {
ret = iio_xml_print_and_sanitized_param(ptr, len,
" value=\"", val, "\"");
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);
}
ret = iio_snprintf(ptr, len, "</buffer>");
if (ret < 0)
return ret;
iio_update_xml_indexes(ret, &ptr, &len, &alen);
}
for (type = IIO_ATTR_TYPE_DEVICE; type <= IIO_ATTR_TYPE_DEBUG; type++) {
for (i = 0; i < dev->attrlist[type].num; i++) {
const char *val = NULL;
attrs = dev->attrlist[type].attrs;
if (include_values && dev->values[type])
val = dev->values[type][i];
ret = iio_snprintf_xml_attr(&attrs[i], val, ptr, len);
if (ret < 0)
return ret;
iio_update_xml_indexes(ret, &ptr, &len, &alen);
}
}
/* Backward compatibility: To make sure that older clients will still work,
* we also need to create <buffer-attributes> tags. Note that given that
* multi buffer is not still supported (and won't ever be in older versions),
* only the first buffer is considered.
*/
if (dev->nb_buffers == 1) {
const struct iio_buffer *buf = dev->buffers[0];
unsigned int j;
for (j = 0; j < buf->attrlist.num; j++) {
ret = iio_snprintf_xml_attr(&buf->attrlist.attrs[j], NULL, 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->label && !strcmp(chn->label, name)) ||
(chn->name && !strcmp(chn->name, name)))
return chn;
}
return NULL;
}
unsigned int iio_device_get_buffers_count(const struct iio_device *dev)
{
return dev->nb_buffers;
}
struct iio_buffer * iio_device_get_buffer(const struct iio_device *dev,
unsigned int index)
{
if (index >= dev->nb_buffers)
return NULL;
return dev->buffers[index];
}
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_DEBUG; type++)
iio_free_attrs(&dev->attrlist[type]);
for (i = 0; i < dev->nb_buffers; i++)
free_buffer(dev->buffers[i]);
free(dev->buffers);
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;
const struct iio_backend_ops *ops = dev->ctx->ops;
/* Use atomic backend operation if available */
if (ops && ops->reg_write) {
return ops->reg_write(dev, address, value);
}
/* Fallback to the original attribute-based implementation.
* NOTE: This has a potential race condition with reg_read operations
* when multiple clients access the same IIOD server. */
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)
{
const struct iio_attr *attr;
const struct iio_backend_ops *ops = dev->ctx->ops;
/* Use atomic backend operation if available */
if (ops && ops->reg_read) {
return ops->reg_read(dev, address, value);
}
/* Fallback to the original attribute-based implementation. */
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_buffer * iio_device_add_buffer(struct iio_device *dev, unsigned int idx)
{
struct iio_buffer *buf, **bufs;
buf = zalloc(sizeof(*buf));
if (!buf)
return NULL;
buf->dev = dev;
buf->idx = idx;
bufs = realloc(dev->buffers, (dev->nb_buffers + 1) * sizeof(*dev->buffers));
if (!bufs) {
free(buf);
return NULL;
}
bufs[dev->nb_buffers++] = buf;
dev->buffers = bufs;
return buf;
}
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, const char *label,
bool output, bool scan_element,
const struct iio_data_format *fmt)
{
struct iio_channel *chn, **chns;
char *new_id, *new_name = NULL, *new_label = 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;
}
if (label) {
new_label = iio_strdup(label);
if (!new_label)
goto err_free_name;
}
chn->id = new_id;
chn->name = new_name;
chn->label = new_label;
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_label;
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 unless context is created from xml which
* should provide already sorted channels. */
if (strcmp(dev->ctx->name, "xml"))
iio_sort_channels(dev);
for (i = 0; i < dev->nb_channels; i++)
dev->channels[i]->number = i;
return chn;
err_free_label:
free(new_label);
err_free_name:
free(new_name);
err_free_id:
free(new_id);
err_free_chn:
free(chn);
return NULL;
}