-
Notifications
You must be signed in to change notification settings - Fork 318
/
local.c
2014 lines (1634 loc) · 43.8 KB
/
local.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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 "local.h"
#include "sort.h"
#include "deps/libini/ini.h"
#include <dirent.h>
#include <errno.h>
#include <iio/iio-backend.h>
#include <iio/iio-debug.h>
#include <iio/iio-lock.h>
#include <limits.h>
#include <poll.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/eventfd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/utsname.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#define NB_BLOCKS 4
#define IIO_GET_EVENT_FD_IOCTL _IOR('i', 0x90, int)
#define IIO_BUFFER_GET_FD_IOCTL _IOWR('i', 0x91, int)
/* Forward declarations */
static ssize_t local_write_dev_attr(const struct iio_device *dev,
unsigned int buf_id, const char *attr,
const char *src, size_t len,
enum iio_attr_type type);
static struct iio_context *
local_create_context(const struct iio_context_params *params, const char *args);
static int local_context_scan(const struct iio_context_params *params,
struct iio_scan *ctx, const char *args);
struct iio_context_pdata {
struct iio_mutex *lock;
};
struct iio_device_pdata {
int fd;
};
struct iio_event_stream_pdata {
const struct iio_device *dev;
int fd, cancel_fd;
};
struct iio_channel_pdata {
char *enable_fn;
struct iio_attr_list protected;
};
static const char * const device_attrs_denylist[] = {
"dev",
"uevent",
};
static const char * const buffer_attrs_reserved[] = {
"length",
"enable",
"watermark",
};
int ioctl_nointr(int fd, unsigned long request, void *data)
{
int ret;
do {
ret = ioctl(fd, request, data);
} while (ret == -1 && errno == EINTR);
if (ret == -1)
ret = -errno;
return ret;
}
static void local_free_channel_pdata(struct iio_channel *chn)
{
if (chn->pdata) {
free(chn->pdata->enable_fn);
free(chn->pdata);
}
}
static void local_free_pdata(struct iio_device *device)
{
unsigned int i;
for (i = 0; i < device->nb_channels; i++)
local_free_channel_pdata(device->channels[i]);
free(device->pdata);
}
static void local_shutdown(struct iio_context *ctx)
{
/* Free the backend data stored in every device structure */
unsigned int i;
for (i = 0; i < iio_context_get_devices_count(ctx); i++) {
struct iio_device *dev = iio_context_get_device(ctx, i);
if (dev->pdata && dev->pdata->fd >= 0)
close(dev->pdata->fd);
local_free_pdata(dev);
}
if (ctx->pdata && ctx->pdata->lock)
iio_mutex_destroy(ctx->pdata->lock);
}
/** Shrinks the first nb characters of a string
* e.g. strcut("foobar", 4) replaces the content with "ar". */
static void strcut(char *str, int nb)
{
char *ptr = str + nb;
while (*ptr)
*str++ = *ptr++;
*str = 0;
}
static int set_channel_name(struct iio_channel *chn)
{
struct iio_channel_pdata *pdata = chn->pdata;
size_t prefix_len = 0;
const char *attr0;
const char *ptr;
unsigned int i;
if (chn->attrlist.num + pdata->protected.num < 2)
return 0;
if (chn->attrlist.num)
attr0 = ptr = chn->attrlist.attrs[0].name;
else
attr0 = ptr = pdata->protected.attrs[0].name;
while (true) {
bool can_fix = true;
size_t len;
ptr = strchr(ptr, '_');
if (!ptr)
break;
len = ptr - attr0 + 1;
for (i = 1; can_fix && i < chn->attrlist.num; i++)
can_fix = !strncmp(attr0, chn->attrlist.attrs[i].name, len);
for (i = !chn->attrlist.num;
can_fix && i < pdata->protected.num; i++) {
can_fix = !strncmp(attr0,
pdata->protected.attrs[i].name, len);
}
if (!can_fix)
break;
prefix_len = len;
ptr = ptr + 1;
}
if (prefix_len) {
char *name;
name = malloc(prefix_len);
if (!name)
return -ENOMEM;
iio_strlcpy(name, attr0, prefix_len);
chn->name = name;
chn_dbg(chn, "Setting name of channel %s to %s\n", chn->id, name);
/* Shrink the attribute name */
for (i = 0; i < chn->attrlist.num; i++)
strcut((char *) chn->attrlist.attrs[i].name, prefix_len);
for (i = 0; i < pdata->protected.num; i++)
strcut((char *) pdata->protected.attrs[i].name, prefix_len);
}
return 0;
}
/*
* Used to generate the timeout parameter for operations like poll. Returns the
* number of ms until it is timeout_rel ms after the time specified in start. If
* timeout_rel is 0 returns -1 to indicate no timeout.
*
* The timeout that is specified for IIO operations is the maximum time a buffer
* push() or refill() operation should take before returning. poll() is used to
* wait for either data activity or for the timeout to elapse. poll() might get
* interrupted in which case it is called again or the read()/write() operation
* might not complete the full buffer size in one call in which case we go back
* to poll() again as well. Passing the same timeout as before would increase
* the total timeout and if repeated interruptions occur (e.g. by a timer
* signal) the operation might never time out or with significant delay. Hence
* before each poll() invocation the timeout is recalculated relative to the
* start of refill() or push() operation.
*/
static int get_rel_timeout_ms(struct timespec *start, unsigned int timeout_rel)
{
struct timespec now;
int diff_ms;
if (timeout_rel == 0) /* No timeout */
return -1;
clock_gettime(CLOCK_MONOTONIC, &now);
diff_ms = (now.tv_sec - start->tv_sec) * 1000;
diff_ms += (now.tv_nsec - start->tv_nsec) / 1000000;
if (diff_ms >= (int) timeout_rel) /* Expired */
return 0;
if (diff_ms > 0) /* Should never be false, but lets be safe */
timeout_rel -= diff_ms;
if (timeout_rel > INT_MAX)
return INT_MAX;
return (int) timeout_rel;
}
int buffer_check_ready(struct iio_buffer_pdata *pdata, int fd,
short events, struct timespec *start)
{
struct pollfd pollfd[2] = {
{
.fd = fd,
.events = events,
}, {
.fd = pdata->cancel_fd,
.events = POLLIN,
}
};
unsigned int rw_timeout_ms = pdata->dev->ctx->params.timeout_ms;
int timeout_rel, ret;
do {
if (start)
timeout_rel = get_rel_timeout_ms(start, rw_timeout_ms);
else
timeout_rel = 0; /* non-blocking */
ret = poll(pollfd, 2, timeout_rel);
} while (ret == -1 && errno == EINTR);
if ((pollfd[1].revents & POLLIN))
return -EBADF;
if (ret < 0)
return -errno;
if (!ret)
return start ? -ETIMEDOUT : -EBUSY;
if (pollfd[0].revents & POLLNVAL)
return -EBADF;
if (!(pollfd[0].revents & events))
return -EIO;
return 0;
}
static int
local_set_buffer_size(const struct iio_buffer_pdata *pdata, size_t nb_samples)
{
char buf[32];
ssize_t ret;
iio_snprintf(buf, sizeof(buf), "%zu", nb_samples);
ret = local_write_dev_attr(pdata->dev, pdata->idx, "length",
buf, strlen(buf) + 1, IIO_ATTR_TYPE_BUFFER);
if (ret < 0)
return (int) ret;
return 0;
}
static int
local_set_watermark(const struct iio_buffer_pdata *pdata, size_t nb_samples)
{
char buf[32];
ssize_t ret;
iio_snprintf(buf, sizeof(buf), "%zu", nb_samples);
ret = local_write_dev_attr(pdata->dev, pdata->idx, "watermark",
buf, strlen(buf) + 1, IIO_ATTR_TYPE_BUFFER);
if (ret < 0 && ret != -ENOENT && ret != -EACCES)
return (int) ret;
return 0;
}
static int local_do_enable_buffer(struct iio_buffer_pdata *pdata, bool enable)
{
int ret;
ret = (int) local_write_dev_attr(pdata->dev, pdata->idx, "enable",
enable ? "1" : "0",
2, IIO_ATTR_TYPE_BUFFER);
if (ret < 0)
return ret;
return 0;
}
static int local_enable_buffer(struct iio_buffer_pdata *pdata,
size_t nb_samples, bool enable, bool cyclic)
{
int ret;
if ((pdata->dmabuf_supported | pdata->mmap_supported) != !nb_samples)
return -EINVAL;
if (nb_samples) {
ret = local_set_buffer_size(pdata, nb_samples);
if (ret)
return ret;
ret = local_set_watermark(pdata, nb_samples);
if (ret)
return ret;
}
return local_do_enable_buffer(pdata, true);
}
static ssize_t local_readbuf(struct iio_buffer_pdata *buffer,
void *dst, size_t len)
{
uintptr_t ptr = (uintptr_t) dst;
struct timespec start;
int fd = buffer->fd;
ssize_t readsize;
ssize_t ret;
if (fd == -1)
return -EBADF;
if (len == 0)
return 0;
clock_gettime(CLOCK_MONOTONIC, &start);
while (len > 0) {
ret = buffer_check_ready(buffer, fd, POLLIN, &start);
if (ret < 0)
break;
do {
ret = read(fd, (void *) ptr, len);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
if (errno == EAGAIN)
continue;
ret = -errno;
break;
} else if (ret == 0) {
ret = -EIO;
break;
}
ptr += ret;
len -= ret;
}
readsize = (ssize_t)(ptr - (uintptr_t) dst);
if ((ret > 0 || ret == -EAGAIN) && (readsize > 0))
return readsize;
else
return ret;
}
static ssize_t
local_writebuf(struct iio_buffer_pdata *buffer, const void *src, size_t len)
{
uintptr_t ptr = (uintptr_t) src;
struct timespec start;
int fd = buffer->fd;
ssize_t writtensize;
ssize_t ret;
if (fd == -1)
return -EBADF;
if (len == 0)
return 0;
clock_gettime(CLOCK_MONOTONIC, &start);
while (len > 0) {
ret = buffer_check_ready(buffer, fd, POLLOUT, &start);
if (ret < 0)
break;
do {
ret = write(fd, (void *) ptr, len);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
if (errno == EAGAIN)
continue;
ret = -errno;
break;
} else if (ret == 0) {
ret = -EIO;
break;
}
ptr += ret;
len -= ret;
}
writtensize = (ssize_t)(ptr - (uintptr_t) src);
if ((ret > 0 || ret == -EAGAIN) && (writtensize > 0))
return writtensize;
else
return ret;
}
static ssize_t local_do_read_dev_attr(const char *id, unsigned int buf_id,
const char *attr, char *dst, size_t len,
enum iio_attr_type type)
{
FILE *f;
char buf[1024];
ssize_t ret;
if (buf_id > 0)
return -ENOSYS;
switch (type) {
case IIO_ATTR_TYPE_DEVICE:
case IIO_ATTR_TYPE_CHANNEL:
if (WITH_HWMON && id[0] == 'h') {
iio_snprintf(buf, sizeof(buf), "/sys/class/hwmon/%s/%s",
id, attr);
} else {
iio_snprintf(buf, sizeof(buf), "/sys/bus/iio/devices/%s/%s",
id, attr);
}
break;
case IIO_ATTR_TYPE_DEBUG:
iio_snprintf(buf, sizeof(buf), "/sys/kernel/debug/iio/%s/%s",
id, attr);
break;
case IIO_ATTR_TYPE_BUFFER:
if (buf_id > 0) {
iio_snprintf(buf, sizeof(buf),
"/sys/bus/iio/devices/%s/buffer%u/%s",
id, buf_id, attr);
} else {
iio_snprintf(buf, sizeof(buf),
"/sys/bus/iio/devices/%s/buffer/%s",
id, attr);
}
break;
default:
return -EINVAL;
}
f = fopen(buf, "re");
if (!f)
return -errno;
ret = fread(dst, 1, len, f);
/* if we didn't read the entire file, fail */
if (!feof(f))
ret = -EFBIG;
if (ret > 0)
dst[ret - 1] = '\0';
else
dst[0] = '\0';
fflush(f);
if (ferror(f))
ret = -errno;
fclose(f);
return ret;
}
static ssize_t local_read_dev_attr(const struct iio_device *dev,
unsigned int buf_id, const char *attr,
char *dst, size_t len,
enum iio_attr_type type)
{
const char *id = iio_device_get_id(dev);
return local_do_read_dev_attr(id, buf_id, attr, dst, len, type);
}
static ssize_t local_write_dev_attr(const struct iio_device *dev,
unsigned int buf_id, const char *attr,
const char *src, size_t len,
enum iio_attr_type type)
{
FILE *f;
char buf[1024];
ssize_t ret;
if (buf_id > 0)
return -ENOSYS;
switch (type) {
case IIO_ATTR_TYPE_DEVICE:
case IIO_ATTR_TYPE_CHANNEL:
if (WITH_HWMON && iio_device_is_hwmon(dev)) {
iio_snprintf(buf, sizeof(buf), "/sys/class/hwmon/%s/%s",
dev->id, attr);
} else {
iio_snprintf(buf, sizeof(buf), "/sys/bus/iio/devices/%s/%s",
dev->id, attr);
}
break;
case IIO_ATTR_TYPE_DEBUG:
iio_snprintf(buf, sizeof(buf), "/sys/kernel/debug/iio/%s/%s",
dev->id, attr);
break;
case IIO_ATTR_TYPE_BUFFER:
if (buf_id > 0) {
iio_snprintf(buf, sizeof(buf),
"/sys/bus/iio/devices/%s/buffer%u/%s",
dev->id, buf_id, attr);
} else {
iio_snprintf(buf, sizeof(buf),
"/sys/bus/iio/devices/%s/buffer/%s",
dev->id, attr);
}
break;
default:
return -EINVAL;
}
f = fopen(buf, "we");
if (!f)
return -errno;
ret = fwrite(src, 1, len, f);
fflush(f);
if (ferror(f))
ret = -errno;
fclose(f);
return ret ? ret : -EIO;
}
static int channel_write_state(const struct iio_channel *chn,
unsigned int idx, bool en)
{
enum iio_attr_type type = idx ? IIO_ATTR_TYPE_BUFFER : IIO_ATTR_TYPE_DEVICE;
ssize_t ret;
if (!chn->pdata->enable_fn) {
chn_err(chn, "Libiio bug: No \"en\" attribute parsed\n");
return -EINVAL;
}
ret = local_write_dev_attr(chn->dev, idx, chn->pdata->enable_fn,
en ? "1" : "0", 2, type);
if (ret < 0)
return (int) ret;
else
return 0;
}
static int channel_read_state(const struct iio_channel *chn, unsigned int idx)
{
enum iio_attr_type type = idx ? IIO_ATTR_TYPE_BUFFER : IIO_ATTR_TYPE_DEVICE;
char buf[8];
int err;
err = local_read_dev_attr(chn->dev, idx, chn->pdata->enable_fn,
buf, sizeof(buf), type);
if (err < 0)
return err;
return buf[0] == '1';
}
static const struct iio_device * local_get_trigger(const struct iio_device *dev)
{
const struct iio_device *cur;
char buf[1024];
unsigned int i;
ssize_t nb;
nb = local_read_dev_attr(dev, 0, "trigger/current_trigger",
buf, sizeof(buf), IIO_ATTR_TYPE_DEVICE);
if (nb < 0)
return iio_ptr(nb);
if (buf[0] == '\0')
return iio_ptr(-ENODEV);
nb = iio_context_get_devices_count(dev->ctx);
for (i = 0; i < (size_t) nb; i++) {
cur = iio_context_get_device(dev->ctx, i);
if (cur->name && !strcmp(cur->name, buf))
return cur;
}
return iio_ptr(-ENXIO);
}
static int local_set_trigger(const struct iio_device *dev,
const struct iio_device *trigger)
{
ssize_t nb;
const char *value = trigger ? trigger->name : "";
nb = local_write_dev_attr(dev, 0, "trigger/current_trigger", value,
strlen(value) + 1, IIO_ATTR_TYPE_DEVICE);
if (nb < 0)
return (int) nb;
else
return 0;
}
static bool is_channel(const struct iio_device *dev, const char *attr, bool strict)
{
char *ptr = NULL;
if (WITH_HWMON && iio_device_is_hwmon(dev))
return iio_channel_is_hwmon(attr);
if (!strncmp(attr, "in_timestamp_", sizeof("in_timestamp_") - 1))
return true;
if (!strncmp(attr, "in_", 3))
ptr = strchr(attr + 3, '_');
else if (!strncmp(attr, "out_", 4))
ptr = strchr(attr + 4, '_');
if (!ptr)
return false;
if (!strict)
return true;
if (*(ptr - 1) >= '0' && *(ptr - 1) <= '9')
return true;
if (find_channel_modifier(ptr + 1, NULL) != IIO_NO_MOD)
return true;
return false;
}
static char * get_channel_id(struct iio_device *dev, const char *attr)
{
char *res, *ptr = strchr(attr, '_');
size_t len;
if (!WITH_HWMON || !iio_device_is_hwmon(dev)) {
attr = ptr + 1;
ptr = strchr(attr, '_');
if (find_channel_modifier(ptr + 1, &len) != IIO_NO_MOD)
ptr += len + 1;
} else if (!ptr) {
/*
* Attribute is 'pwmX' without underscore: the attribute name
* is our channel ID.
*/
return iio_strdup(attr);
}
res = malloc(ptr - attr + 1);
if (!res)
return NULL;
memcpy(res, attr, ptr - attr);
res[ptr - attr] = 0;
return res;
}
static const char *
get_short_attr_name(struct iio_channel *chn, const char *attr)
{
char *ptr = strchr(attr, '_');
size_t len;
if (WITH_HWMON && iio_device_is_hwmon(chn->dev)) {
/*
* PWM hwmon devices can have an attribute named directly after
* the channel's ID; in that particular case we don't need to
* strip the prefix.
*/
return ptr ? ptr + 1 : attr;
}
ptr = strchr(ptr + 1, '_') + 1;
if (find_channel_modifier(ptr, &len) != IIO_NO_MOD)
ptr += len + 1;
if (chn->name) {
len = strlen(chn->name);
if (strncmp(chn->name, ptr, len) == 0 && ptr[len] == '_')
ptr += len + 1;
}
return ptr;
}
static int add_attr_to_device(struct iio_device *dev, const char *attr)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(device_attrs_denylist); i++)
if (!strcmp(device_attrs_denylist[i], attr))
return 0;
if (!strcmp(attr, "name") || !strcmp(attr, "label"))
return 0;
return iio_device_add_attr(dev, attr, IIO_ATTR_TYPE_DEVICE);
}
static int handle_protected_scan_element_attr(struct iio_channel *chn,
const char *name, const char *path)
{
struct iio_device *dev = chn->dev;
char buf[1024];
int ret;
if (!strcmp(name, "index")) {
ret = local_read_dev_attr(dev, 0, path, buf, sizeof(buf),
IIO_ATTR_TYPE_DEVICE);
if (ret > 0) {
char *end;
long long value;
errno = 0;
value = strtoll(buf, &end, 0);
if (end == buf || value < 0 || errno == ERANGE)
return -EINVAL;
chn->index = (long) value;
}
} else if (!strcmp(name, "type")) {
ret = local_read_dev_attr(dev, 0, path, buf, sizeof(buf),
IIO_ATTR_TYPE_DEVICE);
if (ret > 0) {
char endian, sign;
if (strchr(buf, 'X')) {
iio_sscanf(buf, "%ce:%c%u/%uX%u>>%u",
#ifdef _MSC_BUILD
&endian, sizeof(endian),
&sign, sizeof(sign),
#else
&endian, &sign,
#endif
&chn->format.bits, &chn->format.length,
&chn->format.repeat, &chn->format.shift);
} else {
chn->format.repeat = 1;
iio_sscanf(buf, "%ce:%c%u/%u>>%u",
#ifdef _MSC_BUILD
&endian, sizeof(endian),
&sign, sizeof(sign),
#else
&endian, &sign,
#endif
&chn->format.bits, &chn->format.length,
&chn->format.shift);
}
chn->format.is_signed = (sign == 's' || sign == 'S');
chn->format.is_fully_defined =
(sign == 'S' || sign == 'U'||
chn->format.bits == chn->format.length);
chn->format.is_be = endian == 'b';
}
} else if (!strcmp(name, "en")) {
if (chn->pdata->enable_fn) {
chn_err(chn, "Libiio bug: \"en\" attribute already "
"parsed for channel %s!\n", chn->id);
return -EINVAL;
}
chn->pdata->enable_fn = iio_strdup(path);
if (!chn->pdata->enable_fn)
return -ENOMEM;
} else {
return -EINVAL;
}
return 0;
}
static int handle_scan_elements(struct iio_channel *chn)
{
struct iio_channel_pdata *pdata = chn->pdata;
unsigned int i;
for (i = 0; i < pdata->protected.num; i++) {
int ret = handle_protected_scan_element_attr(chn,
pdata->protected.attrs[i].name,
pdata->protected.attrs[i].filename);
if (ret < 0)
return ret;
}
return 0;
}
static void free_protected_attrs(struct iio_channel *chn)
{
struct iio_channel_pdata *pdata = chn->pdata;
iio_free_attrs(&pdata->protected);
}
static int add_attr_to_channel(struct iio_channel *chn,
const char *name, const char *path,
bool is_scan_element)
{
struct iio_channel_pdata *pdata = chn->pdata;
union iio_pointer p = { .chn = chn, };
struct iio_attr_list *attrs;
int ret;
name = get_short_attr_name(chn, name);
attrs = is_scan_element ? &pdata->protected : &chn->attrlist;
ret = iio_add_attr(p, attrs, name, path, IIO_ATTR_TYPE_CHANNEL);
if (ret)
return ret;
chn_dbg(chn, "Added%s attr \'%s\' to channel \'%s\'\n",
is_scan_element ? " protected" : "", name, chn->id);
return 0;
}
static int add_channel_to_device(struct iio_device *dev,
struct iio_channel *chn)
{
struct iio_channel **channels = realloc(dev->channels,
(dev->nb_channels + 1) * sizeof(struct iio_channel *));
if (!channels)
return -ENOMEM;
channels[dev->nb_channels++] = chn;
dev->channels = channels;
dev_dbg(dev, "Added %s channel \'%s\' to device \'%s\'\n",
chn->is_output ? "output" : "input", chn->id, dev->id);
return 0;
}
static struct iio_channel *create_channel(struct iio_device *dev,
char *id, const char *attr, const char *path,
bool is_scan_element)
{
struct iio_channel *chn;
int err = -ENOMEM;
chn = zalloc(sizeof(*chn));
if (!chn)
return iio_ptr(-ENOMEM);
chn->pdata = zalloc(sizeof(*chn->pdata));
if (!chn->pdata)
goto err_free_chn;
if (!WITH_HWMON || !iio_device_is_hwmon(dev)) {
if (!strncmp(attr, "out_", 4)) {
chn->is_output = true;
} else if (strncmp(attr, "in_", 3)) {
err = -EINVAL;
goto err_free_chn_pdata;
}
}
chn->dev = dev;
chn->id = id;
chn->is_scan_element = is_scan_element;
chn->index = -ENOENT;
err = add_attr_to_channel(chn, attr, path, is_scan_element);
if (err)
goto err_free_chn_pdata;
return chn;
err_free_chn_pdata:
free(chn->pdata->enable_fn);
free(chn->pdata);
err_free_chn:
free(chn);
return iio_ptr(err);
}
static int add_channel(struct iio_device *dev, const char *name,
const char *path, bool dir_is_scan_elements)
{
struct iio_channel *chn;
char *channel_id;
unsigned int i;
int ret;
channel_id = get_channel_id(dev, name);
if (!channel_id)
return -ENOMEM;
for (i = 0; i < dev->nb_channels; i++) {
chn = dev->channels[i];
if (!strcmp(chn->id, channel_id)
&& chn->is_output == (name[0] == 'o')) {
free(channel_id);
ret = add_attr_to_channel(chn, name, path,
dir_is_scan_elements);
chn->is_scan_element |= dir_is_scan_elements && !ret;
return ret;
}
}
chn = create_channel(dev, channel_id, name, path, dir_is_scan_elements);
ret = iio_err(chn);
if (ret) {
free(channel_id);
return ret;
}
iio_channel_init_finalize(chn);
ret = add_channel_to_device(dev, chn);
if (ret) {
local_free_channel_pdata(chn);
free_channel(chn);
}
return ret;
}
/*
* Possible return values:
* 0 = Attribute should not be moved to the channel
* 1 = Attribute should be moved to the channel and it is a shared attribute
* 2 = Attribute should be moved to the channel and it is a private attribute
*/
static unsigned int is_global_attr(struct iio_channel *chn, const char *attr)
{
unsigned int len;
char *ptr, *dashptr;
if (!chn->is_output && !strncmp(attr, "in_", 3))
attr += 3;
else if (chn->is_output && !strncmp(attr, "out_", 4))
attr += 4;
else
return 0;
ptr = strchr(attr, '_');
if (!ptr)
return 0;
len = ptr - attr;
// Check for matching global differential attr, like "voltage-voltage"
dashptr = strchr(attr, '-');
if (dashptr && dashptr > attr && dashptr < ptr) {
unsigned int len1 = dashptr - attr;
unsigned int len2 = ptr - dashptr - 1;
const char* iddashptr = strchr(chn->id, '-');
if (iddashptr && strlen(iddashptr + 1) > len2 &&
(unsigned int)(iddashptr - chn->id) > len1 &&
chn->id[len1] >= '0' && chn->id[len1] <= '9' &&
!strncmp(chn->id, attr, len1) &&
iddashptr[len2 + 1] >= '0' && iddashptr[len2 + 1] <= '9' &&
!strncmp(iddashptr + 1, dashptr + 1, len2))
return 1;
}
if (strncmp(chn->id, attr, len))
return 0;
chn_dbg(chn, "Found match: %s and %s\n", chn->id, attr);
if (chn->id[len] >= '0' && chn->id[len] <= '9') {
if (chn->name) {
size_t name_len = strlen(chn->name);
if (strncmp(chn->name, attr + len + 1, name_len) == 0 &&
attr[len + 1 + name_len] == '_')
return 2;