forked from analogdevicesinc/libiio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.c
2048 lines (1691 loc) · 44.1 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
/*
* libiio - Library for interfacing industrial I/O (IIO) devices
*
* Copyright (C) 2014 Analog Devices, Inc.
* Author: Paul Cercueil <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* */
#include "debug.h"
#include "iio-private.h"
#include <dirent.h>
#include <errno.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>
#ifdef WITH_LOCAL_CONFIG
#include <ini.h>
#endif
#define DEFAULT_TIMEOUT_MS 1000
#define NB_BLOCKS 4
#define BLOCK_ALLOC_IOCTL _IOWR('i', 0xa0, struct block_alloc_req)
#define BLOCK_FREE_IOCTL _IO('i', 0xa1)
#define BLOCK_QUERY_IOCTL _IOWR('i', 0xa2, struct block)
#define BLOCK_ENQUEUE_IOCTL _IOWR('i', 0xa3, struct block)
#define BLOCK_DEQUEUE_IOCTL _IOWR('i', 0xa4, struct block)
#define BLOCK_FLAG_CYCLIC BIT(1)
/* Forward declarations */
static ssize_t local_read_dev_attr(const struct iio_device *dev,
const char *attr, char *dst, size_t len, enum iio_attr_type type);
static ssize_t local_read_chn_attr(const struct iio_channel *chn,
const char *attr, char *dst, size_t len);
static ssize_t local_write_dev_attr(const struct iio_device *dev,
const char *attr, const char *src, size_t len, enum iio_attr_type type);
static ssize_t local_write_chn_attr(const struct iio_channel *chn,
const char *attr, const char *src, size_t len);
struct block_alloc_req {
uint32_t type,
size,
count,
id;
};
struct block {
uint32_t id,
size,
bytes_used,
type,
flags,
offset;
uint64_t timestamp;
};
struct iio_context_pdata {
unsigned int rw_timeout_ms;
};
struct iio_device_pdata {
int fd;
bool blocking;
unsigned int samples_count;
unsigned int max_nb_blocks;
unsigned int allocated_nb_blocks;
struct block *blocks;
void **addrs;
int last_dequeued;
bool is_high_speed, cyclic, cyclic_buffer_enqueued, buffer_enabled;
int cancel_fd;
};
struct iio_channel_pdata {
char *enable_fn;
struct iio_channel_attr *protected_attrs;
unsigned int nb_protected_attrs;
};
static const char * const device_attrs_blacklist[] = {
"dev",
"uevent",
};
static const char * const buffer_attrs_reserved[] = {
"length",
"enable",
};
static int ioctl_nointr(int fd, unsigned long request, void *data)
{
int ret;
do {
ret = ioctl(fd, request, data);
} while (ret == -1 && errno == EINTR);
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]);
if (device->pdata) {
free(device->pdata->blocks);
free(device->pdata->addrs);
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 < ctx->nb_devices; i++) {
struct iio_device *dev = ctx->devices[i];
iio_device_close(dev);
local_free_pdata(dev);
}
free(ctx->pdata);
}
/** 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->nb_attrs + pdata->nb_protected_attrs < 2)
return 0;
if (chn->nb_attrs)
attr0 = ptr = chn->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->nb_attrs; i++)
can_fix = !strncmp(attr0, chn->attrs[i].name, len);
for (i = !chn->nb_attrs;
can_fix && i < pdata->nb_protected_attrs; 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;
strncpy(name, attr0, prefix_len - 1);
name[prefix_len - 1] = '\0';
DEBUG("Setting name of channel %s to %s\n", chn->id, name);
chn->name = name;
/* Shrink the attribute name */
for (i = 0; i < chn->nb_attrs; i++)
strcut(chn->attrs[i].name, prefix_len);
for (i = 0; i < pdata->nb_protected_attrs; i++)
strcut(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 >= 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;
}
static int device_check_ready(const struct iio_device *dev, short events,
struct timespec *start)
{
struct pollfd pollfd[2] = {
{
.fd = dev->pdata->fd,
.events = events,
}, {
.fd = dev->pdata->cancel_fd,
.events = POLLIN,
}
};
unsigned int rw_timeout_ms = dev->ctx->pdata->rw_timeout_ms;
int timeout_rel;
int ret;
if (!dev->pdata->blocking)
return 0;
do {
timeout_rel = get_rel_timeout_ms(start, rw_timeout_ms);
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 -ETIMEDOUT;
if (pollfd[0].revents & POLLNVAL)
return -EBADF;
if (!(pollfd[0].revents & events))
return -EIO;
return 0;
}
static ssize_t local_read(const struct iio_device *dev,
void *dst, size_t len, uint32_t *mask, size_t words)
{
struct iio_device_pdata *pdata = dev->pdata;
uintptr_t ptr = (uintptr_t) dst;
struct timespec start;
ssize_t readsize;
ssize_t ret;
if (pdata->fd == -1)
return -EBADF;
if (words != dev->words)
return -EINVAL;
memcpy(mask, dev->mask, words);
if (len == 0)
return 0;
clock_gettime(CLOCK_MONOTONIC, &start);
while (len > 0) {
ret = device_check_ready(dev, POLLIN, &start);
if (ret < 0)
break;
do {
ret = read(pdata->fd, (void *) ptr, len);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
if (pdata->blocking && 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_write(const struct iio_device *dev,
const void *src, size_t len)
{
struct iio_device_pdata *pdata = dev->pdata;
uintptr_t ptr = (uintptr_t) src;
struct timespec start;
ssize_t writtensize;
ssize_t ret;
if (pdata->fd == -1)
return -EBADF;
if (len == 0)
return 0;
clock_gettime(CLOCK_MONOTONIC, &start);
while (len > 0) {
ret = device_check_ready(dev, POLLOUT, &start);
if (ret < 0)
break;
do {
ret = write(pdata->fd, (void *) ptr, len);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
if (pdata->blocking && 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_enable_buffer(const struct iio_device *dev)
{
struct iio_device_pdata *pdata = dev->pdata;
ssize_t ret = 0;
if (!pdata->buffer_enabled) {
ret = local_write_dev_attr(dev,
"buffer/enable", "1", 2, false);
if (ret >= 0)
pdata->buffer_enabled = true;
}
return 0;
}
static int local_set_kernel_buffers_count(const struct iio_device *dev,
unsigned int nb_blocks)
{
struct iio_device_pdata *pdata = dev->pdata;
if (pdata->fd != -1)
return -EBUSY;
pdata->max_nb_blocks = nb_blocks;
return 0;
}
static ssize_t local_get_buffer(const struct iio_device *dev,
void **addr_ptr, size_t bytes_used,
uint32_t *mask, size_t words)
{
struct block block;
struct iio_device_pdata *pdata = dev->pdata;
struct timespec start;
char err_str[1024];
int f = pdata->fd;
ssize_t ret;
if (!pdata->is_high_speed)
return -ENOSYS;
if (f == -1)
return -EBADF;
if (!addr_ptr)
return -EINVAL;
if (pdata->last_dequeued >= 0) {
struct block *last_block = &pdata->blocks[pdata->last_dequeued];
if (pdata->cyclic) {
if (pdata->cyclic_buffer_enqueued)
return -EBUSY;
pdata->blocks[0].flags |= BLOCK_FLAG_CYCLIC;
pdata->cyclic_buffer_enqueued = true;
}
last_block->bytes_used = bytes_used;
ret = (ssize_t) ioctl_nointr(f,
BLOCK_ENQUEUE_IOCTL, last_block);
if (ret) {
ret = (ssize_t) -errno;
iio_strerror(errno, err_str, sizeof(err_str));
ERROR("Unable to enqueue block: %s\n", err_str);
return ret;
}
if (pdata->cyclic) {
*addr_ptr = pdata->addrs[pdata->last_dequeued];
return (ssize_t) last_block->bytes_used;
}
pdata->last_dequeued = -1;
}
clock_gettime(CLOCK_MONOTONIC, &start);
do {
ret = (ssize_t) device_check_ready(dev, POLLIN | POLLOUT, &start);
if (ret < 0)
return ret;
memset(&block, 0, sizeof(block));
ret = (ssize_t) ioctl_nointr(f, BLOCK_DEQUEUE_IOCTL, &block);
} while (pdata->blocking && ret == -1 && errno == EAGAIN);
if (ret) {
ret = (ssize_t) -errno;
if ((!pdata->blocking && ret != -EAGAIN) ||
(pdata->blocking && ret != -ETIMEDOUT)) {
iio_strerror(errno, err_str, sizeof(err_str));
ERROR("Unable to dequeue block: %s\n", err_str);
}
return ret;
}
/* Requested buffer size is too big! */
if (pdata->last_dequeued < 0 && bytes_used != block.size)
return -EFBIG;
pdata->last_dequeued = block.id;
*addr_ptr = pdata->addrs[block.id];
return (ssize_t) block.bytes_used;
}
static ssize_t local_read_all_dev_attrs(const struct iio_device *dev,
char *dst, size_t len, enum iio_attr_type type)
{
unsigned int i, nb;
char **attrs;
char *ptr = dst;
switch (type) {
case IIO_ATTR_TYPE_DEVICE:
nb = dev->nb_attrs;
attrs = dev->attrs;
break;
case IIO_ATTR_TYPE_DEBUG:
nb = dev->nb_debug_attrs;
attrs = dev->debug_attrs;
break;
case IIO_ATTR_TYPE_BUFFER:
nb = dev->nb_buffer_attrs;
attrs = dev->buffer_attrs;
break;
default:
return -EINVAL;
break;
}
for (i = 0; len >= 4 && i < nb; i++) {
/* Recursive! */
ssize_t ret = local_read_dev_attr(dev, attrs[i],
ptr + 4, len - 4, type);
*(uint32_t *) ptr = iio_htobe32(ret);
/* Align the length to 4 bytes */
if (ret > 0 && ret & 3)
ret = ((ret >> 2) + 1) << 2;
ptr += 4 + (ret < 0 ? 0 : ret);
len -= 4 + (ret < 0 ? 0 : ret);
}
return ptr - dst;
}
static ssize_t local_read_all_chn_attrs(const struct iio_channel *chn,
char *dst, size_t len)
{
unsigned int i;
char *ptr = dst;
for (i = 0; len >= 4 && i < chn->nb_attrs; i++) {
/* Recursive! */
ssize_t ret = local_read_chn_attr(chn,
chn->attrs[i].name, ptr + 4, len - 4);
*(uint32_t *) ptr = iio_htobe32(ret);
/* Align the length to 4 bytes */
if (ret > 0 && ret & 3)
ret = ((ret >> 2) + 1) << 2;
ptr += 4 + (ret < 0 ? 0 : ret);
len -= 4 + (ret < 0 ? 0 : ret);
}
return ptr - dst;
}
static int local_buffer_analyze(unsigned int nb, const char *src, size_t len)
{
while (nb--) {
int32_t val;
if (len < 4)
return -EINVAL;
val = (int32_t) iio_be32toh(*(uint32_t *) src);
src += 4;
len -= 4;
if (val > 0) {
if ((uint32_t) val > len)
return -EINVAL;
/* Align the length to 4 bytes */
if (val & 3)
val = ((val >> 2) + 1) << 2;
len -= val;
src += val;
}
}
/* We should have analyzed the whole buffer by now */
return !len ? 0 : -EINVAL;
}
static ssize_t local_write_all_dev_attrs(const struct iio_device *dev,
const char *src, size_t len, enum iio_attr_type type)
{
unsigned int i, nb;
char **attrs;
const char *ptr = src;
switch (type) {
case IIO_ATTR_TYPE_DEVICE:
nb = dev->nb_attrs;
attrs = dev->attrs;
break;
case IIO_ATTR_TYPE_DEBUG:
nb = dev->nb_debug_attrs;
attrs = dev->debug_attrs;
break;
case IIO_ATTR_TYPE_BUFFER:
nb = dev->nb_buffer_attrs;
attrs = dev->buffer_attrs;
break;
default:
return -EINVAL;
break;
}
/* First step: Verify that the buffer is in the correct format */
if (local_buffer_analyze(nb, src, len))
return -EINVAL;
/* Second step: write the attributes */
for (i = 0; i < nb; i++) {
int32_t val = (int32_t) iio_be32toh(*(uint32_t *) ptr);
ptr += 4;
if (val > 0) {
local_write_dev_attr(dev, attrs[i], ptr, val, type);
/* Align the length to 4 bytes */
if (val & 3)
val = ((val >> 2) + 1) << 2;
ptr += val;
}
}
return ptr - src;
}
static ssize_t local_write_all_chn_attrs(const struct iio_channel *chn,
const char *src, size_t len)
{
unsigned int i, nb = chn->nb_attrs;
const char *ptr = src;
/* First step: Verify that the buffer is in the correct format */
if (local_buffer_analyze(nb, src, len))
return -EINVAL;
/* Second step: write the attributes */
for (i = 0; i < nb; i++) {
int32_t val = (int32_t) iio_be32toh(*(uint32_t *) ptr);
ptr += 4;
if (val > 0) {
local_write_chn_attr(chn, chn->attrs[i].name, ptr, val);
/* Align the length to 4 bytes */
if (val & 3)
val = ((val >> 2) + 1) << 2;
ptr += val;
}
}
return ptr - src;
}
static ssize_t local_read_dev_attr(const struct iio_device *dev,
const char *attr, char *dst, size_t len, enum iio_attr_type type)
{
FILE *f;
char buf[1024];
ssize_t ret;
if (!attr)
return local_read_all_dev_attrs(dev, dst, len, type);
switch (type) {
case IIO_ATTR_TYPE_DEVICE:
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:
iio_snprintf(buf, sizeof(buf), "/sys/bus/iio/devices/%s/buffer/%s",
dev->id, attr);
break;
default:
return -EINVAL;
}
f = fopen(buf, "re");
if (!f)
return -errno;
ret = fread(dst, 1, len, f);
if (ret > 0)
dst[ret - 1] = '\0';
fflush(f);
if (ferror(f))
ret = -errno;
fclose(f);
return ret ? ret : -EIO;
}
static ssize_t local_write_dev_attr(const struct iio_device *dev,
const char *attr, const char *src, size_t len, enum iio_attr_type type)
{
FILE *f;
char buf[1024];
ssize_t ret;
if (!attr)
return local_write_all_dev_attrs(dev, src, len, type);
switch (type) {
case IIO_ATTR_TYPE_DEVICE:
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:
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 const char * get_filename(const struct iio_channel *chn,
const char *attr)
{
unsigned int i;
for (i = 0; i < chn->nb_attrs; i++)
if (!strcmp(attr, chn->attrs[i].name))
return chn->attrs[i].filename;
return attr;
}
static ssize_t local_read_chn_attr(const struct iio_channel *chn,
const char *attr, char *dst, size_t len)
{
if (!attr)
return local_read_all_chn_attrs(chn, dst, len);
attr = get_filename(chn, attr);
return local_read_dev_attr(chn->dev, attr, dst, len, false);
}
static ssize_t local_write_chn_attr(const struct iio_channel *chn,
const char *attr, const char *src, size_t len)
{
if (!attr)
return local_write_all_chn_attrs(chn, src, len);
attr = get_filename(chn, attr);
return local_write_dev_attr(chn->dev, attr, src, len, false);
}
static int channel_write_state(const struct iio_channel *chn, bool en)
{
ssize_t ret;
if (!chn->pdata->enable_fn) {
ERROR("Libiio bug: No \"en\" attribute parsed\n");
return -EINVAL;
}
ret = local_write_chn_attr(chn, chn->pdata->enable_fn, en ? "1" : "0", 2);
if (ret < 0)
return (int) ret;
else
return 0;
}
static int enable_high_speed(const struct iio_device *dev)
{
struct block_alloc_req req;
struct iio_device_pdata *pdata = dev->pdata;
unsigned int nb_blocks;
unsigned int i;
int ret, fd = pdata->fd;
if (pdata->cyclic) {
nb_blocks = 1;
DEBUG("Enabling cyclic mode\n");
} else {
nb_blocks = pdata->max_nb_blocks;
DEBUG("Cyclic mode not enabled\n");
}
pdata->blocks = calloc(nb_blocks, sizeof(*pdata->blocks));
if (!pdata->blocks)
return -ENOMEM;
pdata->addrs = calloc(nb_blocks, sizeof(*pdata->addrs));
if (!pdata->addrs) {
free(pdata->blocks);
pdata->blocks = NULL;
return -ENOMEM;
}
req.id = 0;
req.type = 0;
req.size = pdata->samples_count *
iio_device_get_sample_size_mask(dev, dev->mask, dev->words);
req.count = nb_blocks;
ret = ioctl_nointr(fd, BLOCK_ALLOC_IOCTL, &req);
if (ret < 0) {
ret = -errno;
goto err_freemem;
}
if (req.count == 0) {
ret = -ENOMEM;
goto err_block_free;
}
/* We might get less blocks than what we asked for */
pdata->allocated_nb_blocks = req.count;
/* mmap all the blocks */
for (i = 0; i < pdata->allocated_nb_blocks; i++) {
pdata->blocks[i].id = i;
ret = ioctl_nointr(fd, BLOCK_QUERY_IOCTL, &pdata->blocks[i]);
if (ret) {
ret = -errno;
goto err_munmap;
}
ret = ioctl_nointr(fd, BLOCK_ENQUEUE_IOCTL, &pdata->blocks[i]);
if (ret) {
ret = -errno;
goto err_munmap;
}
pdata->addrs[i] = mmap(0, pdata->blocks[i].size,
PROT_READ | PROT_WRITE,
MAP_SHARED, fd, pdata->blocks[i].offset);
if (pdata->addrs[i] == MAP_FAILED) {
ret = -errno;
goto err_munmap;
}
}
pdata->last_dequeued = -1;
return 0;
err_munmap:
for (; i > 0; i--)
munmap(pdata->addrs[i - 1], pdata->blocks[i - 1].size);
err_block_free:
ioctl_nointr(fd, BLOCK_FREE_IOCTL, 0);
pdata->allocated_nb_blocks = 0;
err_freemem:
free(pdata->addrs);
pdata->addrs = NULL;
free(pdata->blocks);
pdata->blocks = NULL;
return ret;
}
static int local_open(const struct iio_device *dev,
size_t samples_count, bool cyclic)
{
unsigned int i;
int ret;
char buf[1024];
struct iio_device_pdata *pdata = dev->pdata;
if (pdata->fd != -1)
return -EBUSY;
ret = local_write_dev_attr(dev, "buffer/enable", "0", 2, false);
if (ret < 0)
return ret;
iio_snprintf(buf, sizeof(buf), "%lu", (unsigned long) samples_count);
ret = local_write_dev_attr(dev, "buffer/length",
buf, strlen(buf) + 1, false);
if (ret < 0)
return ret;
pdata->cancel_fd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
if (pdata->cancel_fd == -1)
return -errno;
iio_snprintf(buf, sizeof(buf), "/dev/%s", dev->id);
pdata->fd = open(buf, O_RDWR | O_CLOEXEC | O_NONBLOCK);
if (pdata->fd == -1) {
ret = -errno;
goto err_close_cancel_fd;
}
/* Disable channels */
for (i = 0; i < dev->nb_channels; i++) {
struct iio_channel *chn = dev->channels[i];
if (chn->index >= 0 && !iio_channel_is_enabled(chn)) {
ret = channel_write_state(chn, false);
if (ret < 0)
goto err_close;
}
}
/* Enable channels */
for (i = 0; i < dev->nb_channels; i++) {
struct iio_channel *chn = dev->channels[i];
if (chn->index >= 0 && iio_channel_is_enabled(chn)) {
ret = channel_write_state(chn, true);
if (ret < 0)
goto err_close;
}
}
pdata->cyclic = cyclic;
pdata->cyclic_buffer_enqueued = false;
pdata->buffer_enabled = false;
pdata->samples_count = samples_count;
ret = enable_high_speed(dev);
if (ret < 0 && ret != -ENOSYS)
goto err_close;
pdata->is_high_speed = !ret;
if (!pdata->is_high_speed) {
unsigned long size = samples_count * pdata->max_nb_blocks;
WARNING("High-speed mode not enabled\n");
/* Cyclic mode is only supported in high-speed mode */
if (cyclic) {
ret = -EPERM;
goto err_close;
}
/* Increase the size of the kernel buffer, when using the
* low-speed interface. This avoids losing samples when
* refilling the iio_buffer. */
iio_snprintf(buf, sizeof(buf), "%lu", size);
ret = local_write_dev_attr(dev, "buffer/length",
buf, strlen(buf) + 1, false);
if (ret < 0)
goto err_close;
}
ret = local_enable_buffer(dev);
if (ret < 0)
goto err_close;
return 0;
err_close:
close(pdata->fd);
pdata->fd = -1;
err_close_cancel_fd:
close(pdata->cancel_fd);
pdata->cancel_fd = -1;
return ret;
}
static int local_close(const struct iio_device *dev)
{
struct iio_device_pdata *pdata = dev->pdata;
unsigned int i;
int ret;