1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Industrialio buffer test code.
4 * Copyright (c) 2008 Jonathan Cameron
6 * This program is primarily intended as an example application.
7 * Reads the current buffer setup from sysfs and starts a short capture
8 * from the specified device, pretty printing the result after appropriate
11 * Command line parameters
12 * generic_buffer -n <device_name> -t <trigger_name>
13 * If trigger name is not specified the program assumes you want a dataready
14 * trigger associated with the device and goes looking for it.
25 #include <linux/types.h>
33 #include <sys/ioctl.h>
34 #include <linux/iio/buffer.h>
35 #include "iio_utils.h"
38 * enum autochan - state for the automatic channel enabling mechanism
41 AUTOCHANNELS_DISABLED
,
47 * size_from_channelarray() - calculate the storage size of a scan
48 * @channels: the channel info array
49 * @num_channels: number of channels
51 * Has the side effect of filling the channels[i].location values used
52 * in processing the buffer output.
54 static unsigned int size_from_channelarray(struct iio_channel_info
*channels
, int num_channels
)
56 unsigned int bytes
= 0;
58 unsigned int misalignment
;
60 while (i
< num_channels
) {
61 if (channels
[i
].bytes
> max
)
62 max
= channels
[i
].bytes
;
63 if (bytes
% channels
[i
].bytes
== 0)
64 channels
[i
].location
= bytes
;
66 channels
[i
].location
= bytes
- bytes
% channels
[i
].bytes
69 bytes
= channels
[i
].location
+ channels
[i
].bytes
;
73 * We want the data in next sample to also be properly aligned so
74 * we'll add padding at the end if needed. Adding padding only
75 * works for channel data which size is 2^n bytes.
77 misalignment
= bytes
% max
;
79 bytes
+= max
- misalignment
;
84 static void print1byte(uint8_t input
, struct iio_channel_info
*info
)
87 * Shift before conversion to avoid sign extension
88 * of left aligned data
90 input
>>= info
->shift
;
92 if (info
->is_signed
) {
93 int8_t val
= (int8_t)(input
<< (8 - info
->bits_used
)) >>
94 (8 - info
->bits_used
);
95 printf("%05f ", ((float)val
+ info
->offset
) * info
->scale
);
97 printf("%05f ", ((float)input
+ info
->offset
) * info
->scale
);
101 static void print2byte(uint16_t input
, struct iio_channel_info
*info
)
103 /* First swap if incorrect endian */
105 input
= be16toh(input
);
107 input
= le16toh(input
);
110 * Shift before conversion to avoid sign extension
111 * of left aligned data
113 input
>>= info
->shift
;
115 if (info
->is_signed
) {
116 int16_t val
= (int16_t)(input
<< (16 - info
->bits_used
)) >>
117 (16 - info
->bits_used
);
118 printf("%05f ", ((float)val
+ info
->offset
) * info
->scale
);
120 printf("%05f ", ((float)input
+ info
->offset
) * info
->scale
);
124 static void print4byte(uint32_t input
, struct iio_channel_info
*info
)
126 /* First swap if incorrect endian */
128 input
= be32toh(input
);
130 input
= le32toh(input
);
133 * Shift before conversion to avoid sign extension
134 * of left aligned data
136 input
>>= info
->shift
;
138 if (info
->is_signed
) {
139 int32_t val
= (int32_t)(input
<< (32 - info
->bits_used
)) >>
140 (32 - info
->bits_used
);
141 printf("%05f ", ((float)val
+ info
->offset
) * info
->scale
);
143 printf("%05f ", ((float)input
+ info
->offset
) * info
->scale
);
147 static void print8byte(uint64_t input
, struct iio_channel_info
*info
)
149 /* First swap if incorrect endian */
151 input
= be64toh(input
);
153 input
= le64toh(input
);
156 * Shift before conversion to avoid sign extension
157 * of left aligned data
159 input
>>= info
->shift
;
161 if (info
->is_signed
) {
162 int64_t val
= (int64_t)(input
<< (64 - info
->bits_used
)) >>
163 (64 - info
->bits_used
);
164 /* special case for timestamp */
165 if (info
->scale
== 1.0f
&& info
->offset
== 0.0f
)
166 printf("%" PRId64
" ", val
);
169 ((float)val
+ info
->offset
) * info
->scale
);
171 printf("%05f ", ((float)input
+ info
->offset
) * info
->scale
);
176 * process_scan() - print out the values in SI units
177 * @data: pointer to the start of the scan
178 * @channels: information about the channels.
179 * Note: size_from_channelarray must have been called first
180 * to fill the location offsets.
181 * @num_channels: number of channels
183 static void process_scan(char *data
, struct iio_channel_info
*channels
,
188 for (k
= 0; k
< num_channels
; k
++)
189 switch (channels
[k
].bytes
) {
190 /* only a few cases implemented so far */
192 print1byte(*(uint8_t *)(data
+ channels
[k
].location
),
196 print2byte(*(uint16_t *)(data
+ channels
[k
].location
),
200 print4byte(*(uint32_t *)(data
+ channels
[k
].location
),
204 print8byte(*(uint64_t *)(data
+ channels
[k
].location
),
213 static int enable_disable_all_channels(char *dev_dir_name
, int buffer_idx
, int enable
)
215 const struct dirent
*ent
;
216 char scanelemdir
[256];
220 snprintf(scanelemdir
, sizeof(scanelemdir
),
221 FORMAT_SCAN_ELEMENTS_DIR
, dev_dir_name
, buffer_idx
);
222 scanelemdir
[sizeof(scanelemdir
)-1] = '\0';
224 dp
= opendir(scanelemdir
);
226 fprintf(stderr
, "Enabling/disabling channels: can't open %s\n",
232 while (ent
= readdir(dp
), ent
) {
233 if (iioutils_check_suffix(ent
->d_name
, "_en")) {
234 printf("%sabling: %s\n",
235 enable
? "En" : "Dis",
237 ret
= write_sysfs_int(ent
->d_name
, scanelemdir
,
240 fprintf(stderr
, "Failed to enable/disable %s\n",
245 if (closedir(dp
) == -1) {
246 perror("Enabling/disabling channels: "
247 "Failed to close directory");
253 static void print_usage(void)
255 fprintf(stderr
, "Usage: generic_buffer [options]...\n"
256 "Capture, convert and output data from IIO device buffer\n"
257 " -a Auto-activate all available channels\n"
258 " -A Force-activate ALL channels\n"
259 " -b <n> The buffer which to open (by index), default 0\n"
260 " -c <n> Do n conversions, or loop forever if n < 0\n"
261 " -e Disable wait for event (new data)\n"
262 " -g Use trigger-less mode\n"
263 " -l <n> Set buffer length to n samples\n"
264 " --device-name -n <name>\n"
265 " --device-num -N <num>\n"
266 " Set device by name or number (mandatory)\n"
267 " --trigger-name -t <name>\n"
268 " --trigger-num -T <num>\n"
269 " Set trigger by name or number\n"
270 " -w <n> Set delay between reads in us (event-less mode)\n");
273 static enum autochan autochannels
= AUTOCHANNELS_DISABLED
;
274 static char *dev_dir_name
= NULL
;
275 static char *buf_dir_name
= NULL
;
276 static int buffer_idx
= 0;
277 static bool current_trigger_set
= false;
279 static void cleanup(void)
283 /* Disable trigger */
284 if (dev_dir_name
&& current_trigger_set
) {
285 /* Disconnect the trigger - just write a dummy name. */
286 ret
= write_sysfs_string("trigger/current_trigger",
287 dev_dir_name
, "NULL");
289 fprintf(stderr
, "Failed to disable trigger: %s\n",
291 current_trigger_set
= false;
296 ret
= write_sysfs_int("enable", buf_dir_name
, 0);
298 fprintf(stderr
, "Failed to disable buffer: %s\n",
302 /* Disable channels if auto-enabled */
303 if (dev_dir_name
&& autochannels
== AUTOCHANNELS_ACTIVE
) {
304 ret
= enable_disable_all_channels(dev_dir_name
, buffer_idx
, 0);
306 fprintf(stderr
, "Failed to disable all channels\n");
307 autochannels
= AUTOCHANNELS_DISABLED
;
311 static void sig_handler(int signum
)
313 fprintf(stderr
, "Caught signal %d\n", signum
);
318 static void register_cleanup(void)
320 struct sigaction sa
= { .sa_handler
= sig_handler
};
321 const int signums
[] = { SIGINT
, SIGTERM
, SIGABRT
};
324 for (i
= 0; i
< ARRAY_SIZE(signums
); ++i
) {
325 ret
= sigaction(signums
[i
], &sa
, NULL
);
327 perror("Failed to register signal handler");
333 static const struct option longopts
[] = {
334 { "device-name", 1, 0, 'n' },
335 { "device-num", 1, 0, 'N' },
336 { "trigger-name", 1, 0, 't' },
337 { "trigger-num", 1, 0, 'T' },
341 int main(int argc
, char **argv
)
343 long long num_loops
= 2;
344 unsigned long timedelay
= 1000000;
345 unsigned long buf_len
= 128;
348 unsigned long long j
;
349 unsigned long toread
;
355 int num_channels
= 0;
356 char *trigger_name
= NULL
, *device_name
= NULL
;
360 int dev_num
= -1, trig_num
= -1;
361 char *buffer_access
= NULL
;
362 unsigned int scan_size
;
366 bool force_autochannels
= false;
368 struct iio_channel_info
*channels
= NULL
;
372 while ((c
= getopt_long(argc
, argv
, "aAb:c:egl:n:N:t:T:w:?", longopts
,
376 autochannels
= AUTOCHANNELS_ENABLED
;
379 autochannels
= AUTOCHANNELS_ENABLED
;
380 force_autochannels
= true;
384 buffer_idx
= strtoll(optarg
, &dummy
, 10);
389 if (buffer_idx
< 0) {
397 num_loops
= strtoll(optarg
, &dummy
, 10);
412 buf_len
= strtoul(optarg
, &dummy
, 10);
420 device_name
= strdup(optarg
);
424 dev_num
= strtoul(optarg
, &dummy
, 10);
431 trigger_name
= strdup(optarg
);
435 trig_num
= strtoul(optarg
, &dummy
, 10);
441 timedelay
= strtoul(optarg
, &dummy
, 10);
454 /* Find the device requested */
455 if (dev_num
< 0 && !device_name
) {
456 fprintf(stderr
, "Device not set\n");
460 } else if (dev_num
>= 0 && device_name
) {
461 fprintf(stderr
, "Only one of --device-num or --device-name needs to be set\n");
465 } else if (dev_num
< 0) {
466 dev_num
= find_type_by_name(device_name
, "iio:device");
468 fprintf(stderr
, "Failed to find the %s\n", device_name
);
473 printf("iio device number being used is %d\n", dev_num
);
475 ret
= asprintf(&dev_dir_name
, "%siio:device%d", iio_dir
, dev_num
);
478 /* Fetch device_name if specified by number */
480 device_name
= malloc(IIO_MAX_NAME_LENGTH
);
485 ret
= read_sysfs_string("name", dev_dir_name
, device_name
);
487 fprintf(stderr
, "Failed to read name of device %d\n", dev_num
);
493 printf("trigger-less mode selected\n");
494 } else if (trig_num
>= 0) {
496 ret
= asprintf(&trig_dev_name
, "%strigger%d", iio_dir
, trig_num
);
500 trigger_name
= malloc(IIO_MAX_NAME_LENGTH
);
505 ret
= read_sysfs_string("name", trig_dev_name
, trigger_name
);
508 fprintf(stderr
, "Failed to read trigger%d name from\n", trig_num
);
511 printf("iio trigger number being used is %d\n", trig_num
);
515 * Build the trigger name. If it is device associated
516 * its name is <device_name>_dev[n] where n matches
517 * the device number found above.
519 ret
= asprintf(&trigger_name
,
520 "%s-dev%d", device_name
, dev_num
);
527 /* Look for this "-devN" trigger */
528 trig_num
= find_type_by_name(trigger_name
, "trigger");
530 /* OK try the simpler "-trigger" suffix instead */
532 ret
= asprintf(&trigger_name
,
533 "%s-trigger", device_name
);
540 trig_num
= find_type_by_name(trigger_name
, "trigger");
542 fprintf(stderr
, "Failed to find the trigger %s\n",
548 printf("iio trigger number being used is %d\n", trig_num
);
552 * Parse the files in scan_elements to identify what channels are
555 ret
= build_channel_array(dev_dir_name
, buffer_idx
, &channels
, &num_channels
);
557 fprintf(stderr
, "Problem reading scan element information\n"
558 "diag %s\n", dev_dir_name
);
561 if (num_channels
&& autochannels
== AUTOCHANNELS_ENABLED
&&
562 !force_autochannels
) {
563 fprintf(stderr
, "Auto-channels selected but some channels "
564 "are already activated in sysfs\n");
565 fprintf(stderr
, "Proceeding without activating any channels\n");
568 if ((!num_channels
&& autochannels
== AUTOCHANNELS_ENABLED
) ||
569 (autochannels
== AUTOCHANNELS_ENABLED
&& force_autochannels
)) {
570 fprintf(stderr
, "Enabling all channels\n");
572 ret
= enable_disable_all_channels(dev_dir_name
, buffer_idx
, 1);
574 fprintf(stderr
, "Failed to enable all channels\n");
578 /* This flags that we need to disable the channels again */
579 autochannels
= AUTOCHANNELS_ACTIVE
;
581 ret
= build_channel_array(dev_dir_name
, buffer_idx
, &channels
,
584 fprintf(stderr
, "Problem reading scan element "
586 "diag %s\n", dev_dir_name
);
590 fprintf(stderr
, "Still no channels after "
591 "auto-enabling, giving up\n");
596 if (!num_channels
&& autochannels
== AUTOCHANNELS_DISABLED
) {
598 "No channels are enabled, we have nothing to scan.\n");
599 fprintf(stderr
, "Enable channels manually in "
600 FORMAT_SCAN_ELEMENTS_DIR
601 "/*_en or pass -a to autoenable channels and "
602 "try again.\n", dev_dir_name
, buffer_idx
);
608 * Construct the directory name for the associated buffer.
609 * As we know that the lis3l02dq has only one buffer this may
610 * be built rather than found.
612 ret
= asprintf(&buf_dir_name
,
613 "%siio:device%d/buffer%d", iio_dir
, dev_num
, buffer_idx
);
619 if (stat(buf_dir_name
, &st
)) {
620 fprintf(stderr
, "Could not stat() '%s', got error %d: %s\n",
621 buf_dir_name
, errno
, strerror(errno
));
626 if (!S_ISDIR(st
.st_mode
)) {
627 fprintf(stderr
, "File '%s' is not a directory\n", buf_dir_name
);
633 printf("%s %s\n", dev_dir_name
, trigger_name
);
635 * Set the device trigger to be the data ready trigger found
638 ret
= write_sysfs_string_and_verify("trigger/current_trigger",
643 "Failed to write current_trigger file\n");
648 ret
= asprintf(&buffer_access
, "/dev/iio:device%d", dev_num
);
654 /* Attempt to open non blocking the access dev */
655 fd
= open(buffer_access
, O_RDONLY
| O_NONBLOCK
);
656 if (fd
== -1) { /* TODO: If it isn't there make the node */
658 fprintf(stderr
, "Failed to open %s\n", buffer_access
);
662 /* specify for which buffer index we want an FD */
665 ret
= ioctl(fd
, IIO_BUFFER_GET_FD_IOCTL
, &buf_fd
);
666 if (ret
== -1 || buf_fd
== -1) {
668 if (ret
== -ENODEV
|| ret
== -EINVAL
)
670 "Device does not have this many buffers\n");
672 fprintf(stderr
, "Failed to retrieve buffer fd\n");
677 /* Setup ring buffer parameters */
678 ret
= write_sysfs_int("length", buf_dir_name
, buf_len
);
682 /* Enable the buffer */
683 ret
= write_sysfs_int("enable", buf_dir_name
, 1);
686 "Failed to enable buffer '%s': %s\n",
687 buf_dir_name
, strerror(-ret
));
691 scan_size
= size_from_channelarray(channels
, num_channels
);
693 size_t total_buf_len
= scan_size
* buf_len
;
695 if (scan_size
> 0 && total_buf_len
/ scan_size
!= buf_len
) {
697 perror("Integer overflow happened when calculate scan_size * buf_len");
701 data
= malloc(total_buf_len
);
708 * This check is being done here for sanity reasons, however it
709 * should be omitted under normal operation.
710 * If this is buffer0, we check that we get EBUSY after this point.
712 if (buffer_idx
== 0) {
714 read_size
= read(fd
, data
, 1);
715 if (read_size
> -1 || errno
!= EBUSY
) {
717 perror("Reading from '%s' should not be possible after ioctl()");
722 /* close now the main chardev FD and let the buffer FD work */
724 perror("Failed to close character device file");
727 for (j
= 0; j
< num_loops
|| num_loops
< 0; j
++) {
729 struct pollfd pfd
= {
734 ret
= poll(&pfd
, 1, -1);
738 } else if (ret
== 0) {
748 read_size
= read(buf_fd
, data
, toread
* scan_size
);
750 if (errno
== EAGAIN
) {
751 fprintf(stderr
, "nothing available\n");
757 for (i
= 0; i
< read_size
/ scan_size
; i
++)
758 process_scan(data
+ scan_size
* i
, channels
,
765 if (fd
>= 0 && close(fd
) == -1)
766 perror("Failed to close character device");
767 if (buf_fd
>= 0 && close(buf_fd
) == -1)
768 perror("Failed to close buffer");
772 for (i
= num_channels
- 1; i
>= 0; i
--) {
773 free(channels
[i
].name
);
774 free(channels
[i
].generic_name
);