blob: 2491c54a5e4fb5b7ed275586a68a7274aa5cf6cc [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jonathan Camerone58537c2010-10-08 12:14:14 +01002/* Industrialio buffer test code.
3 *
4 * Copyright (c) 2008 Jonathan Cameron
5 *
Jonathan Camerone58537c2010-10-08 12:14:14 +01006 * 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
9 * conversion.
10 *
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.
Jonathan Camerone58537c2010-10-08 12:14:14 +010015 */
16
17#include <unistd.h>
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020018#include <stdlib.h>
Jonathan Camerone58537c2010-10-08 12:14:14 +010019#include <dirent.h>
20#include <fcntl.h>
21#include <stdio.h>
22#include <errno.h>
23#include <sys/stat.h>
24#include <sys/dir.h>
25#include <linux/types.h>
Jonathan Cameron30268a32011-02-11 13:09:12 +000026#include <string.h>
Jonathan Cameron52615d42011-05-18 14:41:19 +010027#include <poll.h>
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000028#include <endian.h>
Peter Meerwaldbb233782012-06-25 23:12:17 +020029#include <getopt.h>
Peter Meerwald1bcdfbc2012-06-25 23:12:16 +020030#include <inttypes.h>
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +030031#include <stdbool.h>
32#include <signal.h>
Alexandru Ardelean8827faa2021-02-15 12:40:43 +020033#include <sys/ioctl.h>
34#include <linux/iio/buffer.h>
Jonathan Camerone58537c2010-10-08 12:14:14 +010035#include "iio_utils.h"
36
Jonathan Camerone58537c2010-10-08 12:14:14 +010037/**
Linus Walleij5f991a92016-04-14 10:26:47 +020038 * enum autochan - state for the automatic channel enabling mechanism
39 */
40enum autochan {
41 AUTOCHANNELS_DISABLED,
42 AUTOCHANNELS_ENABLED,
43 AUTOCHANNELS_ACTIVE,
44};
45
46/**
Jonathan Camerone58537c2010-10-08 12:14:14 +010047 * size_from_channelarray() - calculate the storage size of a scan
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +020048 * @channels: the channel info array
49 * @num_channels: number of channels
Jonathan Camerone58537c2010-10-08 12:14:14 +010050 *
51 * Has the side effect of filling the channels[i].location values used
52 * in processing the buffer output.
53 **/
Alexandru Ardeleanebe51122021-02-15 12:40:42 +020054static int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
Jonathan Camerone58537c2010-10-08 12:14:14 +010055{
56 int bytes = 0;
57 int i = 0;
Melike Yurtogluff39a252014-10-03 23:34:50 +030058
Jonathan Camerone58537c2010-10-08 12:14:14 +010059 while (i < num_channels) {
60 if (bytes % channels[i].bytes == 0)
61 channels[i].location = bytes;
62 else
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020063 channels[i].location = bytes - bytes % channels[i].bytes
64 + channels[i].bytes;
65
Jonathan Camerone58537c2010-10-08 12:14:14 +010066 bytes = channels[i].location + channels[i].bytes;
67 i++;
68 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020069
Jonathan Camerone58537c2010-10-08 12:14:14 +010070 return bytes;
71}
72
Alexandru Ardeleanebe51122021-02-15 12:40:42 +020073static void print1byte(uint8_t input, struct iio_channel_info *info)
Tiberiu Breanae8d09272015-07-03 12:57:36 +030074{
75 /*
76 * Shift before conversion to avoid sign extension
77 * of left aligned data
78 */
79 input >>= info->shift;
80 input &= info->mask;
81 if (info->is_signed) {
82 int8_t val = (int8_t)(input << (8 - info->bits_used)) >>
83 (8 - info->bits_used);
84 printf("%05f ", ((float)val + info->offset) * info->scale);
85 } else {
86 printf("%05f ", ((float)input + info->offset) * info->scale);
87 }
88}
89
Alexandru Ardeleanebe51122021-02-15 12:40:42 +020090static void print2byte(uint16_t input, struct iio_channel_info *info)
Jonathan Cameron52615d42011-05-18 14:41:19 +010091{
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000092 /* First swap if incorrect endian */
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000093 if (info->be)
Hartmut Knaack8e926132015-05-31 14:39:58 +020094 input = be16toh(input);
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000095 else
Hartmut Knaack8e926132015-05-31 14:39:58 +020096 input = le16toh(input);
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000097
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +020098 /*
99 * Shift before conversion to avoid sign extension
100 * of left aligned data
101 */
Aya Mahfouz1525ecf2015-02-26 11:45:26 +0200102 input >>= info->shift;
Hartmut Knaack8e926132015-05-31 14:39:58 +0200103 input &= info->mask;
Jonathan Cameron52615d42011-05-18 14:41:19 +0100104 if (info->is_signed) {
Hartmut Knaack8e926132015-05-31 14:39:58 +0200105 int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
106 (16 - info->bits_used);
107 printf("%05f ", ((float)val + info->offset) * info->scale);
Jonathan Cameron52615d42011-05-18 14:41:19 +0100108 } else {
Hartmut Knaack8e926132015-05-31 14:39:58 +0200109 printf("%05f ", ((float)input + info->offset) * info->scale);
Jonathan Cameron52615d42011-05-18 14:41:19 +0100110 }
111}
Hartmut Knaack8e926132015-05-31 14:39:58 +0200112
Alexandru Ardeleanebe51122021-02-15 12:40:42 +0200113static void print4byte(uint32_t input, struct iio_channel_info *info)
Hartmut Knaack8e926132015-05-31 14:39:58 +0200114{
115 /* First swap if incorrect endian */
116 if (info->be)
117 input = be32toh(input);
118 else
119 input = le32toh(input);
120
121 /*
122 * Shift before conversion to avoid sign extension
123 * of left aligned data
124 */
125 input >>= info->shift;
126 input &= info->mask;
127 if (info->is_signed) {
128 int32_t val = (int32_t)(input << (32 - info->bits_used)) >>
129 (32 - info->bits_used);
130 printf("%05f ", ((float)val + info->offset) * info->scale);
131 } else {
132 printf("%05f ", ((float)input + info->offset) * info->scale);
133 }
134}
135
Alexandru Ardeleanebe51122021-02-15 12:40:42 +0200136static void print8byte(uint64_t input, struct iio_channel_info *info)
Hartmut Knaack8e926132015-05-31 14:39:58 +0200137{
138 /* First swap if incorrect endian */
139 if (info->be)
140 input = be64toh(input);
141 else
142 input = le64toh(input);
143
144 /*
145 * Shift before conversion to avoid sign extension
146 * of left aligned data
147 */
148 input >>= info->shift;
149 input &= info->mask;
150 if (info->is_signed) {
151 int64_t val = (int64_t)(input << (64 - info->bits_used)) >>
152 (64 - info->bits_used);
153 /* special case for timestamp */
154 if (info->scale == 1.0f && info->offset == 0.0f)
155 printf("%" PRId64 " ", val);
156 else
157 printf("%05f ",
158 ((float)val + info->offset) * info->scale);
159 } else {
160 printf("%05f ", ((float)input + info->offset) * info->scale);
161 }
162}
163
Jonathan Camerone58537c2010-10-08 12:14:14 +0100164/**
165 * process_scan() - print out the values in SI units
166 * @data: pointer to the start of the scan
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200167 * @channels: information about the channels.
168 * Note: size_from_channelarray must have been called first
169 * to fill the location offsets.
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200170 * @num_channels: number of channels
Jonathan Camerone58537c2010-10-08 12:14:14 +0100171 **/
Alexandru Ardeleanebe51122021-02-15 12:40:42 +0200172static void process_scan(char *data, struct iio_channel_info *channels,
173 int num_channels)
Jonathan Camerone58537c2010-10-08 12:14:14 +0100174{
175 int k;
Melike Yurtogluff39a252014-10-03 23:34:50 +0300176
Jonathan Camerone58537c2010-10-08 12:14:14 +0100177 for (k = 0; k < num_channels; k++)
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200178 switch (channels[k].bytes) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100179 /* only a few cases implemented so far */
Tiberiu Breanae8d09272015-07-03 12:57:36 +0300180 case 1:
181 print1byte(*(uint8_t *)(data + channels[k].location),
182 &channels[k]);
183 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100184 case 2:
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200185 print2byte(*(uint16_t *)(data + channels[k].location),
186 &channels[k]);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100187 break;
Marek Vasut6cffc1f2012-08-12 16:21:00 +0100188 case 4:
Hartmut Knaack8e926132015-05-31 14:39:58 +0200189 print4byte(*(uint32_t *)(data + channels[k].location),
190 &channels[k]);
Marek Vasut6cffc1f2012-08-12 16:21:00 +0100191 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100192 case 8:
Hartmut Knaack8e926132015-05-31 14:39:58 +0200193 print8byte(*(uint64_t *)(data + channels[k].location),
194 &channels[k]);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100195 break;
196 default:
197 break;
198 }
199 printf("\n");
200}
201
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200202static int enable_disable_all_channels(char *dev_dir_name, int buffer_idx, int enable)
Linus Walleij5f991a92016-04-14 10:26:47 +0200203{
204 const struct dirent *ent;
205 char scanelemdir[256];
206 DIR *dp;
207 int ret;
208
209 snprintf(scanelemdir, sizeof(scanelemdir),
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200210 FORMAT_SCAN_ELEMENTS_DIR, dev_dir_name, buffer_idx);
Linus Walleij5f991a92016-04-14 10:26:47 +0200211 scanelemdir[sizeof(scanelemdir)-1] = '\0';
212
213 dp = opendir(scanelemdir);
214 if (!dp) {
215 fprintf(stderr, "Enabling/disabling channels: can't open %s\n",
216 scanelemdir);
217 return -EIO;
218 }
219
220 ret = -ENOENT;
221 while (ent = readdir(dp), ent) {
222 if (iioutils_check_suffix(ent->d_name, "_en")) {
223 printf("%sabling: %s\n",
224 enable ? "En" : "Dis",
225 ent->d_name);
226 ret = write_sysfs_int(ent->d_name, scanelemdir,
227 enable);
228 if (ret < 0)
229 fprintf(stderr, "Failed to enable/disable %s\n",
230 ent->d_name);
231 }
232 }
233
234 if (closedir(dp) == -1) {
235 perror("Enabling/disabling channels: "
236 "Failed to close directory");
237 return -errno;
238 }
239 return 0;
240}
241
Alexandru Ardeleanebe51122021-02-15 12:40:42 +0200242static void print_usage(void)
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200243{
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300244 fprintf(stderr, "Usage: generic_buffer [options]...\n"
245 "Capture, convert and output data from IIO device buffer\n"
Linus Walleij5f991a92016-04-14 10:26:47 +0200246 " -a Auto-activate all available channels\n"
Eva Rachel Retuya4e2cf0e2016-10-14 18:18:39 +0800247 " -A Force-activate ALL channels\n"
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200248 " -b <n> The buffer which to open (by index), default 0\n"
Martin Kelly55dda0a2018-05-17 17:14:46 -0700249 " -c <n> Do n conversions, or loop forever if n < 0\n"
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300250 " -e Disable wait for event (new data)\n"
251 " -g Use trigger-less mode\n"
252 " -l <n> Set buffer length to n samples\n"
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300253 " --device-name -n <name>\n"
254 " --device-num -N <num>\n"
255 " Set device by name or number (mandatory)\n"
Crestez Dan Leonard7c7e9da2016-05-23 21:39:58 +0300256 " --trigger-name -t <name>\n"
257 " --trigger-num -T <num>\n"
258 " Set trigger by name or number\n"
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300259 " -w <n> Set delay between reads in us (event-less mode)\n");
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200260}
261
Alexandru Ardeleanebe51122021-02-15 12:40:42 +0200262static enum autochan autochannels = AUTOCHANNELS_DISABLED;
263static char *dev_dir_name = NULL;
264static char *buf_dir_name = NULL;
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200265static int buffer_idx = 0;
Alexandru Ardeleanebe51122021-02-15 12:40:42 +0200266static bool current_trigger_set = false;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300267
Alexandru Ardeleanebe51122021-02-15 12:40:42 +0200268static void cleanup(void)
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300269{
270 int ret;
271
272 /* Disable trigger */
273 if (dev_dir_name && current_trigger_set) {
274 /* Disconnect the trigger - just write a dummy name. */
275 ret = write_sysfs_string("trigger/current_trigger",
276 dev_dir_name, "NULL");
277 if (ret < 0)
278 fprintf(stderr, "Failed to disable trigger: %s\n",
279 strerror(-ret));
280 current_trigger_set = false;
281 }
282
283 /* Disable buffer */
284 if (buf_dir_name) {
285 ret = write_sysfs_int("enable", buf_dir_name, 0);
286 if (ret < 0)
287 fprintf(stderr, "Failed to disable buffer: %s\n",
288 strerror(-ret));
289 }
290
291 /* Disable channels if auto-enabled */
292 if (dev_dir_name && autochannels == AUTOCHANNELS_ACTIVE) {
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200293 ret = enable_disable_all_channels(dev_dir_name, buffer_idx, 0);
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300294 if (ret)
295 fprintf(stderr, "Failed to disable all channels\n");
296 autochannels = AUTOCHANNELS_DISABLED;
297 }
298}
299
Alexandru Ardeleanebe51122021-02-15 12:40:42 +0200300static void sig_handler(int signum)
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300301{
302 fprintf(stderr, "Caught signal %d\n", signum);
303 cleanup();
304 exit(-signum);
305}
306
Alexandru Ardeleanebe51122021-02-15 12:40:42 +0200307static void register_cleanup(void)
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300308{
309 struct sigaction sa = { .sa_handler = sig_handler };
310 const int signums[] = { SIGINT, SIGTERM, SIGABRT };
311 int ret, i;
312
313 for (i = 0; i < ARRAY_SIZE(signums); ++i) {
314 ret = sigaction(signums[i], &sa, NULL);
315 if (ret) {
316 perror("Failed to register signal handler");
317 exit(-1);
318 }
319 }
320}
321
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300322static const struct option longopts[] = {
323 { "device-name", 1, 0, 'n' },
324 { "device-num", 1, 0, 'N' },
Crestez Dan Leonard7c7e9da2016-05-23 21:39:58 +0300325 { "trigger-name", 1, 0, 't' },
326 { "trigger-num", 1, 0, 'T' },
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300327 { },
328};
329
Jonathan Camerone58537c2010-10-08 12:14:14 +0100330int main(int argc, char **argv)
331{
Martin Kellyb119d3b2019-01-11 23:13:09 +0000332 long long num_loops = 2;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000333 unsigned long timedelay = 1000000;
334 unsigned long buf_len = 128;
335
Martin Kelly71b52d22018-05-17 17:14:45 -0700336 ssize_t i;
Martin Kelly55dda0a2018-05-17 17:14:46 -0700337 unsigned long long j;
Martin Kelly71b52d22018-05-17 17:14:45 -0700338 unsigned long toread;
339 int ret, c;
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200340 struct stat st;
341 int fd = -1;
342 int buf_fd = -1;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100343
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300344 int num_channels = 0;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100345 char *trigger_name = NULL, *device_name = NULL;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100346
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300347 char *data = NULL;
Jonathan Cameronc77b3812011-04-15 18:56:00 +0100348 ssize_t read_size;
Crestez Dan Leonarddeb4d1f2016-06-03 21:56:29 +0300349 int dev_num = -1, trig_num = -1;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300350 char *buffer_access = NULL;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100351 int scan_size;
Jonathan Cameron30268a32011-02-11 13:09:12 +0000352 int noevents = 0;
Karol Wronab6d5be572014-11-04 15:29:39 +0100353 int notrigger = 0;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000354 char *dummy;
Eva Rachel Retuya71ccbe52016-10-20 17:19:23 +0800355 bool force_autochannels = false;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100356
Alison Schofieldddbc7192016-07-26 20:17:43 -0700357 struct iio_channel_info *channels = NULL;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100358
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300359 register_cleanup();
360
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200361 while ((c = getopt_long(argc, argv, "aAb:c:egl:n:N:t:T:w:?", longopts,
Eva Rachel Retuya4e2cf0e2016-10-14 18:18:39 +0800362 NULL)) != -1) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100363 switch (c) {
Linus Walleij5f991a92016-04-14 10:26:47 +0200364 case 'a':
365 autochannels = AUTOCHANNELS_ENABLED;
366 break;
Eva Rachel Retuya4e2cf0e2016-10-14 18:18:39 +0800367 case 'A':
368 autochannels = AUTOCHANNELS_ENABLED;
Eva Rachel Retuya71ccbe52016-10-20 17:19:23 +0800369 force_autochannels = true;
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200370 break;
371 case 'b':
372 errno = 0;
373 buffer_idx = strtoll(optarg, &dummy, 10);
374 if (errno) {
375 ret = -errno;
376 goto error;
377 }
378 if (buffer_idx < 0) {
379 ret = -ERANGE;
380 goto error;
381 }
382
383 break;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000384 case 'c':
Hartmut Knaackc8ce9902015-05-31 14:40:03 +0200385 errno = 0;
Martin Kelly55dda0a2018-05-17 17:14:46 -0700386 num_loops = strtoll(optarg, &dummy, 10);
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300387 if (errno) {
388 ret = -errno;
389 goto error;
390 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200391
Jonathan Cameron96df9792011-02-11 13:09:13 +0000392 break;
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200393 case 'e':
394 noevents = 1;
395 break;
396 case 'g':
397 notrigger = 1;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000398 break;
399 case 'l':
Hartmut Knaackc8ce9902015-05-31 14:40:03 +0200400 errno = 0;
Jonathan Cameron96df9792011-02-11 13:09:13 +0000401 buf_len = strtoul(optarg, &dummy, 10);
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300402 if (errno) {
403 ret = -errno;
404 goto error;
405 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200406
Jonathan Cameron96df9792011-02-11 13:09:13 +0000407 break;
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200408 case 'n':
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300409 device_name = strdup(optarg);
410 break;
411 case 'N':
412 errno = 0;
413 dev_num = strtoul(optarg, &dummy, 10);
414 if (errno) {
415 ret = -errno;
416 goto error;
417 }
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200418 break;
419 case 't':
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300420 trigger_name = strdup(optarg);
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200421 break;
Crestez Dan Leonard7c7e9da2016-05-23 21:39:58 +0300422 case 'T':
423 errno = 0;
424 trig_num = strtoul(optarg, &dummy, 10);
425 if (errno)
426 return -errno;
427 break;
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200428 case 'w':
429 errno = 0;
430 timedelay = strtoul(optarg, &dummy, 10);
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300431 if (errno) {
432 ret = -errno;
433 goto error;
434 }
Karol Wronab6d5be572014-11-04 15:29:39 +0100435 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100436 case '?':
Hartmut Knaacke06e3d72015-05-31 14:40:22 +0200437 print_usage();
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300438 ret = -1;
439 goto error;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100440 }
441 }
442
443 /* Find the device requested */
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300444 if (dev_num < 0 && !device_name) {
445 fprintf(stderr, "Device not set\n");
446 print_usage();
447 ret = -1;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300448 goto error;
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300449 } else if (dev_num >= 0 && device_name) {
450 fprintf(stderr, "Only one of --device-num or --device-name needs to be set\n");
451 print_usage();
452 ret = -1;
453 goto error;
454 } else if (dev_num < 0) {
455 dev_num = find_type_by_name(device_name, "iio:device");
456 if (dev_num < 0) {
457 fprintf(stderr, "Failed to find the %s\n", device_name);
458 ret = dev_num;
459 goto error;
460 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100461 }
462 printf("iio device number being used is %d\n", dev_num);
463
Hartmut Knaacke9e45b42015-05-31 14:40:02 +0200464 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300465 if (ret < 0)
466 return -ENOMEM;
467 /* Fetch device_name if specified by number */
468 if (!device_name) {
469 device_name = malloc(IIO_MAX_NAME_LENGTH);
470 if (!device_name) {
471 ret = -ENOMEM;
472 goto error;
473 }
474 ret = read_sysfs_string("name", dev_dir_name, device_name);
475 if (ret < 0) {
476 fprintf(stderr, "Failed to read name of device %d\n", dev_num);
477 goto error;
478 }
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300479 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100480
Crestez Dan Leonard7c7e9da2016-05-23 21:39:58 +0300481 if (notrigger) {
482 printf("trigger-less mode selected\n");
Gregor Boirief8e81d72016-08-24 15:28:27 +0200483 } else if (trig_num >= 0) {
Crestez Dan Leonard7c7e9da2016-05-23 21:39:58 +0300484 char *trig_dev_name;
485 ret = asprintf(&trig_dev_name, "%strigger%d", iio_dir, trig_num);
486 if (ret < 0) {
487 return -ENOMEM;
488 }
489 trigger_name = malloc(IIO_MAX_NAME_LENGTH);
490 ret = read_sysfs_string("name", trig_dev_name, trigger_name);
491 free(trig_dev_name);
492 if (ret < 0) {
493 fprintf(stderr, "Failed to read trigger%d name from\n", trig_num);
494 return ret;
495 }
496 printf("iio trigger number being used is %d\n", trig_num);
497 } else {
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300498 if (!trigger_name) {
Karol Wronab6d5be572014-11-04 15:29:39 +0100499 /*
500 * Build the trigger name. If it is device associated
501 * its name is <device_name>_dev[n] where n matches
502 * the device number found above.
503 */
504 ret = asprintf(&trigger_name,
505 "%s-dev%d", device_name, dev_num);
506 if (ret < 0) {
507 ret = -ENOMEM;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300508 goto error;
Karol Wronab6d5be572014-11-04 15:29:39 +0100509 }
510 }
511
Linus Walleij793d6b52016-03-24 09:39:14 +0100512 /* Look for this "-devN" trigger */
513 trig_num = find_type_by_name(trigger_name, "trigger");
514 if (trig_num < 0) {
515 /* OK try the simpler "-trigger" suffix instead */
516 free(trigger_name);
517 ret = asprintf(&trigger_name,
518 "%s-trigger", device_name);
519 if (ret < 0) {
520 ret = -ENOMEM;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300521 goto error;
Linus Walleij793d6b52016-03-24 09:39:14 +0100522 }
523 }
524
Karol Wronab6d5be572014-11-04 15:29:39 +0100525 trig_num = find_type_by_name(trigger_name, "trigger");
526 if (trig_num < 0) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300527 fprintf(stderr, "Failed to find the trigger %s\n",
528 trigger_name);
Hartmut Knaacke83a47c2015-05-31 14:39:57 +0200529 ret = trig_num;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300530 goto error;
Karol Wronab6d5be572014-11-04 15:29:39 +0100531 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200532
Karol Wronab6d5be572014-11-04 15:29:39 +0100533 printf("iio trigger number being used is %d\n", trig_num);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200534 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100535
536 /*
537 * Parse the files in scan_elements to identify what channels are
538 * present
539 */
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200540 ret = build_channel_array(dev_dir_name, buffer_idx, &channels, &num_channels);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100541 if (ret) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300542 fprintf(stderr, "Problem reading scan element information\n"
543 "diag %s\n", dev_dir_name);
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300544 goto error;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100545 }
Eva Rachel Retuya7c5fe412016-10-20 17:19:24 +0800546 if (num_channels && autochannels == AUTOCHANNELS_ENABLED &&
Eva Rachel Retuya71ccbe52016-10-20 17:19:23 +0800547 !force_autochannels) {
Linus Walleij5f991a92016-04-14 10:26:47 +0200548 fprintf(stderr, "Auto-channels selected but some channels "
549 "are already activated in sysfs\n");
550 fprintf(stderr, "Proceeding without activating any channels\n");
551 }
552
Eva Rachel Retuya4e2cf0e2016-10-14 18:18:39 +0800553 if ((!num_channels && autochannels == AUTOCHANNELS_ENABLED) ||
Eva Rachel Retuya7c5fe412016-10-20 17:19:24 +0800554 (autochannels == AUTOCHANNELS_ENABLED && force_autochannels)) {
Eva Rachel Retuya4e2cf0e2016-10-14 18:18:39 +0800555 fprintf(stderr, "Enabling all channels\n");
Linus Walleij5f991a92016-04-14 10:26:47 +0200556
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200557 ret = enable_disable_all_channels(dev_dir_name, buffer_idx, 1);
Linus Walleij5f991a92016-04-14 10:26:47 +0200558 if (ret) {
559 fprintf(stderr, "Failed to enable all channels\n");
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300560 goto error;
Linus Walleij5f991a92016-04-14 10:26:47 +0200561 }
562
563 /* This flags that we need to disable the channels again */
564 autochannels = AUTOCHANNELS_ACTIVE;
565
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200566 ret = build_channel_array(dev_dir_name, buffer_idx, &channels,
Linus Walleij5f991a92016-04-14 10:26:47 +0200567 &num_channels);
568 if (ret) {
569 fprintf(stderr, "Problem reading scan element "
570 "information\n"
571 "diag %s\n", dev_dir_name);
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300572 goto error;
Linus Walleij5f991a92016-04-14 10:26:47 +0200573 }
574 if (!num_channels) {
575 fprintf(stderr, "Still no channels after "
576 "auto-enabling, giving up\n");
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300577 goto error;
Linus Walleij5f991a92016-04-14 10:26:47 +0200578 }
579 }
580
581 if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) {
Linus Walleij53dabaf2015-08-10 10:55:08 +0200582 fprintf(stderr,
583 "No channels are enabled, we have nothing to scan.\n");
584 fprintf(stderr, "Enable channels manually in "
585 FORMAT_SCAN_ELEMENTS_DIR
Linus Walleij5f991a92016-04-14 10:26:47 +0200586 "/*_en or pass -a to autoenable channels and "
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200587 "try again.\n", dev_dir_name, buffer_idx);
Linus Walleij53dabaf2015-08-10 10:55:08 +0200588 ret = -ENOENT;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300589 goto error;
Linus Walleij53dabaf2015-08-10 10:55:08 +0200590 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100591
592 /*
593 * Construct the directory name for the associated buffer.
594 * As we know that the lis3l02dq has only one buffer this may
595 * be built rather than found.
596 */
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100597 ret = asprintf(&buf_dir_name,
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200598 "%siio:device%d/buffer%d", iio_dir, dev_num, buffer_idx);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100599 if (ret < 0) {
600 ret = -ENOMEM;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300601 goto error;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100602 }
Karol Wronab6d5be572014-11-04 15:29:39 +0100603
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200604 if (stat(buf_dir_name, &st)) {
605 fprintf(stderr, "Could not stat() '%s', got error %d: %s\n",
606 buf_dir_name, errno, strerror(errno));
607 ret = -errno;
608 goto error;
609 }
610
611 if (!S_ISDIR(st.st_mode)) {
612 fprintf(stderr, "File '%s' is not a directory\n", buf_dir_name);
613 ret = -EFAULT;
614 goto error;
615 }
616
Karol Wronab6d5be572014-11-04 15:29:39 +0100617 if (!notrigger) {
618 printf("%s %s\n", dev_dir_name, trigger_name);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200619 /*
620 * Set the device trigger to be the data ready trigger found
621 * above
622 */
Karol Wronab6d5be572014-11-04 15:29:39 +0100623 ret = write_sysfs_string_and_verify("trigger/current_trigger",
624 dev_dir_name,
625 trigger_name);
626 if (ret < 0) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300627 fprintf(stderr,
628 "Failed to write current_trigger file\n");
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300629 goto error;
Karol Wronab6d5be572014-11-04 15:29:39 +0100630 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100631 }
632
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200633 ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
634 if (ret < 0) {
635 ret = -ENOMEM;
636 goto error;
637 }
638
639 /* Attempt to open non blocking the access dev */
640 fd = open(buffer_access, O_RDONLY | O_NONBLOCK);
641 if (fd == -1) { /* TODO: If it isn't there make the node */
642 ret = -errno;
643 fprintf(stderr, "Failed to open %s\n", buffer_access);
644 goto error;
645 }
646
647 /* specify for which buffer index we want an FD */
648 buf_fd = buffer_idx;
649
650 ret = ioctl(fd, IIO_BUFFER_GET_FD_IOCTL, &buf_fd);
651 if (ret == -1 || buf_fd == -1) {
652 ret = -errno;
653 if (ret == -ENODEV || ret == -EINVAL)
654 fprintf(stderr,
655 "Device does not have this many buffers\n");
656 else
657 fprintf(stderr, "Failed to retrieve buffer fd\n");
658
659 goto error;
660 }
661
Jonathan Camerone58537c2010-10-08 12:14:14 +0100662 /* Setup ring buffer parameters */
663 ret = write_sysfs_int("length", buf_dir_name, buf_len);
664 if (ret < 0)
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300665 goto error;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100666
667 /* Enable the buffer */
668 ret = write_sysfs_int("enable", buf_dir_name, 1);
Irina Tirdeae7231492015-07-24 16:28:06 +0300669 if (ret < 0) {
670 fprintf(stderr,
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200671 "Failed to enable buffer '%s': %s\n",
672 buf_dir_name, strerror(-ret));
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300673 goto error;
Irina Tirdeae7231492015-07-24 16:28:06 +0300674 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200675
Peter Meerwaldd8da0ee2012-06-25 23:12:14 +0200676 scan_size = size_from_channelarray(channels, num_channels);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200677 data = malloc(scan_size * buf_len);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100678 if (!data) {
679 ret = -ENOMEM;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300680 goto error;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100681 }
682
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200683 /**
684 * This check is being done here for sanity reasons, however it
685 * should be omitted under normal operation.
686 * If this is buffer0, we check that we get EBUSY after this point.
687 */
688 if (buffer_idx == 0) {
689 errno = 0;
690 read_size = read(fd, data, 1);
691 if (read_size > -1 || errno != EBUSY) {
692 ret = -EFAULT;
693 perror("Reading from '%s' should not be possible after ioctl()");
694 goto error;
695 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100696 }
697
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200698 /* close now the main chardev FD and let the buffer FD work */
699 if (close(fd) == -1)
700 perror("Failed to close character device file");
701 fd = -1;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100702
Martin Kelly55dda0a2018-05-17 17:14:46 -0700703 for (j = 0; j < num_loops || num_loops < 0; j++) {
Jonathan Cameron30268a32011-02-11 13:09:12 +0000704 if (!noevents) {
Jonathan Cameron52615d42011-05-18 14:41:19 +0100705 struct pollfd pfd = {
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200706 .fd = buf_fd,
Jonathan Cameron52615d42011-05-18 14:41:19 +0100707 .events = POLLIN,
708 };
709
Hartmut Knaack6bb7cac2015-05-31 14:40:04 +0200710 ret = poll(&pfd, 1, -1);
711 if (ret < 0) {
712 ret = -errno;
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300713 goto error;
Hartmut Knaack6bb7cac2015-05-31 14:40:04 +0200714 } else if (ret == 0) {
715 continue;
716 }
717
Jonathan Cameron52615d42011-05-18 14:41:19 +0100718 toread = buf_len;
Jonathan Cameron30268a32011-02-11 13:09:12 +0000719 } else {
Jonathan Cameron96df9792011-02-11 13:09:13 +0000720 usleep(timedelay);
Jonathan Cameron30268a32011-02-11 13:09:12 +0000721 toread = 64;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100722 }
Jonathan Cameron30268a32011-02-11 13:09:12 +0000723
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200724 read_size = read(buf_fd, data, toread * scan_size);
Peter Meerwald97b603a2014-12-06 06:00:00 +0000725 if (read_size < 0) {
Hartmut Knaack87499482015-05-31 14:39:56 +0200726 if (errno == EAGAIN) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300727 fprintf(stderr, "nothing available\n");
Peter Meerwald97b603a2014-12-06 06:00:00 +0000728 continue;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200729 } else {
Peter Meerwald97b603a2014-12-06 06:00:00 +0000730 break;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200731 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100732 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200733 for (i = 0; i < read_size / scan_size; i++)
734 process_scan(data + scan_size * i, channels,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100735 num_channels);
736 }
737
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300738error:
739 cleanup();
Jonathan Camerone58537c2010-10-08 12:14:14 +0100740
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200741 if (fd >= 0 && close(fd) == -1)
742 perror("Failed to close character device");
743 if (buf_fd >= 0 && close(buf_fd) == -1)
Hartmut Knaack6bb7cac2015-05-31 14:40:04 +0200744 perror("Failed to close buffer");
Jonathan Camerone58537c2010-10-08 12:14:14 +0100745 free(buffer_access);
Hartmut Knaacka71bfb42015-05-31 14:39:41 +0200746 free(data);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100747 free(buf_dir_name);
Hartmut Knaack63f05c82015-05-31 14:39:44 +0200748 for (i = num_channels - 1; i >= 0; i--) {
749 free(channels[i].name);
750 free(channels[i].generic_name);
751 }
752 free(channels);
Crestez Dan Leonard4f20d592016-05-23 21:39:56 +0300753 free(trigger_name);
Crestez Dan Leonardde397db2016-05-23 21:39:57 +0300754 free(device_name);
Hartmut Knaackd3ccfc42015-05-31 14:39:42 +0200755 free(dev_dir_name);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200756
Jonathan Camerone58537c2010-10-08 12:14:14 +0100757 return ret;
758}