f2fs: Provide a splice-read wrapper
[linux-block.git] / tools / iio / iio_generic_buffer.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
e58537cc
JC
2/* Industrialio buffer test code.
3 *
4 * Copyright (c) 2008 Jonathan Cameron
5 *
e58537cc
JC
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
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.
e58537cc
JC
15 */
16
17#include <unistd.h>
bdcb31d0 18#include <stdlib.h>
e58537cc
JC
19#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>
30268a3d 26#include <string.h>
52615d47 27#include <poll.h>
117cf8b7 28#include <endian.h>
bb23378c 29#include <getopt.h>
1bcdfbcf 30#include <inttypes.h>
4f20d592
CDL
31#include <stdbool.h>
32#include <signal.h>
8827faab
AA
33#include <sys/ioctl.h>
34#include <linux/iio/buffer.h>
e58537cc
JC
35#include "iio_utils.h"
36
5f991a92
LW
37/**
38 * enum autochan - state for the automatic channel enabling mechanism
39 */
40enum autochan {
41 AUTOCHANNELS_DISABLED,
42 AUTOCHANNELS_ENABLED,
43 AUTOCHANNELS_ACTIVE,
44};
45
e58537cc
JC
46/**
47 * size_from_channelarray() - calculate the storage size of a scan
d8da0eee
PM
48 * @channels: the channel info array
49 * @num_channels: number of channels
e58537cc
JC
50 *
51 * Has the side effect of filling the channels[i].location values used
52 * in processing the buffer output.
53 **/
ebe51125 54static int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
e58537cc
JC
55{
56 int bytes = 0;
57 int i = 0;
ff39a252 58
e58537cc
JC
59 while (i < num_channels) {
60 if (bytes % channels[i].bytes == 0)
61 channels[i].location = bytes;
62 else
7663a4aa
HK
63 channels[i].location = bytes - bytes % channels[i].bytes
64 + channels[i].bytes;
65
e58537cc
JC
66 bytes = channels[i].location + channels[i].bytes;
67 i++;
68 }
7663a4aa 69
e58537cc
JC
70 return bytes;
71}
72
ebe51125 73static void print1byte(uint8_t input, struct iio_channel_info *info)
e8d0927a
TB
74{
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
ebe51125 90static void print2byte(uint16_t input, struct iio_channel_info *info)
52615d47 91{
117cf8b7 92 /* First swap if incorrect endian */
117cf8b7 93 if (info->be)
8e926134 94 input = be16toh(input);
117cf8b7 95 else
8e926134 96 input = le16toh(input);
117cf8b7 97
d8da0eee
PM
98 /*
99 * Shift before conversion to avoid sign extension
100 * of left aligned data
101 */
1525ecfc 102 input >>= info->shift;
8e926134 103 input &= info->mask;
52615d47 104 if (info->is_signed) {
8e926134
HK
105 int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
106 (16 - info->bits_used);
107 printf("%05f ", ((float)val + info->offset) * info->scale);
108 } else {
109 printf("%05f ", ((float)input + info->offset) * info->scale);
110 }
111}
ff39a252 112
ebe51125 113static void print4byte(uint32_t input, struct iio_channel_info *info)
8e926134
HK
114{
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);
52615d47 131 } else {
8e926134
HK
132 printf("%05f ", ((float)input + info->offset) * info->scale);
133 }
134}
ff39a252 135
ebe51125 136static void print8byte(uint64_t input, struct iio_channel_info *info)
8e926134
HK
137{
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);
52615d47
JC
161 }
162}
8e926134 163
e58537cc
JC
164/**
165 * process_scan() - print out the values in SI units
166 * @data: pointer to the start of the scan
7663a4aa
HK
167 * @channels: information about the channels.
168 * Note: size_from_channelarray must have been called first
169 * to fill the location offsets.
d8da0eee 170 * @num_channels: number of channels
e58537cc 171 **/
ebe51125
AA
172static void process_scan(char *data, struct iio_channel_info *channels,
173 int num_channels)
e58537cc
JC
174{
175 int k;
ff39a252 176
e58537cc 177 for (k = 0; k < num_channels; k++)
d8da0eee 178 switch (channels[k].bytes) {
e58537cc 179 /* only a few cases implemented so far */
e8d0927a
TB
180 case 1:
181 print1byte(*(uint8_t *)(data + channels[k].location),
182 &channels[k]);
183 break;
e58537cc 184 case 2:
d8da0eee
PM
185 print2byte(*(uint16_t *)(data + channels[k].location),
186 &channels[k]);
e58537cc 187 break;
6cffc1f8 188 case 4:
8e926134
HK
189 print4byte(*(uint32_t *)(data + channels[k].location),
190 &channels[k]);
6cffc1f8 191 break;
e58537cc 192 case 8:
8e926134
HK
193 print8byte(*(uint64_t *)(data + channels[k].location),
194 &channels[k]);
e58537cc
JC
195 break;
196 default:
197 break;
198 }
199 printf("\n");
200}
201
8827faab 202static int enable_disable_all_channels(char *dev_dir_name, int buffer_idx, int enable)
5f991a92
LW
203{
204 const struct dirent *ent;
205 char scanelemdir[256];
206 DIR *dp;
207 int ret;
208
209 snprintf(scanelemdir, sizeof(scanelemdir),
8827faab 210 FORMAT_SCAN_ELEMENTS_DIR, dev_dir_name, buffer_idx);
5f991a92
LW
211 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
ebe51125 242static void print_usage(void)
e06e3d71 243{
d9abc615
CO
244 fprintf(stderr, "Usage: generic_buffer [options]...\n"
245 "Capture, convert and output data from IIO device buffer\n"
5f991a92 246 " -a Auto-activate all available channels\n"
4e2cf0e2 247 " -A Force-activate ALL channels\n"
8827faab 248 " -b <n> The buffer which to open (by index), default 0\n"
55dda0ab 249 " -c <n> Do n conversions, or loop forever if n < 0\n"
d9abc615
CO
250 " -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"
de397db8
CDL
253 " --device-name -n <name>\n"
254 " --device-num -N <num>\n"
255 " Set device by name or number (mandatory)\n"
7c7e9dad
CDL
256 " --trigger-name -t <name>\n"
257 " --trigger-num -T <num>\n"
258 " Set trigger by name or number\n"
d9abc615 259 " -w <n> Set delay between reads in us (event-less mode)\n");
e06e3d71
HK
260}
261
ebe51125
AA
262static enum autochan autochannels = AUTOCHANNELS_DISABLED;
263static char *dev_dir_name = NULL;
264static char *buf_dir_name = NULL;
8827faab 265static int buffer_idx = 0;
ebe51125 266static bool current_trigger_set = false;
4f20d592 267
ebe51125 268static void cleanup(void)
4f20d592
CDL
269{
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) {
8827faab 293 ret = enable_disable_all_channels(dev_dir_name, buffer_idx, 0);
4f20d592
CDL
294 if (ret)
295 fprintf(stderr, "Failed to disable all channels\n");
296 autochannels = AUTOCHANNELS_DISABLED;
297 }
298}
299
ebe51125 300static void sig_handler(int signum)
4f20d592
CDL
301{
302 fprintf(stderr, "Caught signal %d\n", signum);
303 cleanup();
304 exit(-signum);
305}
306
ebe51125 307static void register_cleanup(void)
4f20d592
CDL
308{
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
de397db8
CDL
322static const struct option longopts[] = {
323 { "device-name", 1, 0, 'n' },
324 { "device-num", 1, 0, 'N' },
7c7e9dad
CDL
325 { "trigger-name", 1, 0, 't' },
326 { "trigger-num", 1, 0, 'T' },
de397db8
CDL
327 { },
328};
329
e58537cc
JC
330int main(int argc, char **argv)
331{
b119d3bc 332 long long num_loops = 2;
96df9799
JC
333 unsigned long timedelay = 1000000;
334 unsigned long buf_len = 128;
335
71b52d2c 336 ssize_t i;
55dda0ab 337 unsigned long long j;
71b52d2c
MK
338 unsigned long toread;
339 int ret, c;
8827faab
AA
340 struct stat st;
341 int fd = -1;
342 int buf_fd = -1;
e58537cc 343
4f20d592 344 int num_channels = 0;
e58537cc 345 char *trigger_name = NULL, *device_name = NULL;
e58537cc 346
4f20d592 347 char *data = NULL;
c77b3810 348 ssize_t read_size;
deb4d1fd 349 int dev_num = -1, trig_num = -1;
4f20d592 350 char *buffer_access = NULL;
e58537cc 351 int scan_size;
30268a3d 352 int noevents = 0;
b6d5be57 353 int notrigger = 0;
96df9799 354 char *dummy;
71ccbe5d 355 bool force_autochannels = false;
e58537cc 356
ddbc719f 357 struct iio_channel_info *channels = NULL;
e58537cc 358
4f20d592
CDL
359 register_cleanup();
360
8827faab 361 while ((c = getopt_long(argc, argv, "aAb:c:egl:n:N:t:T:w:?", longopts,
4e2cf0e2 362 NULL)) != -1) {
e58537cc 363 switch (c) {
5f991a92
LW
364 case 'a':
365 autochannels = AUTOCHANNELS_ENABLED;
366 break;
4e2cf0e2
ERR
367 case 'A':
368 autochannels = AUTOCHANNELS_ENABLED;
71ccbe5d 369 force_autochannels = true;
8827faab
AA
370 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;
96df9799 384 case 'c':
c8ce9903 385 errno = 0;
55dda0ab 386 num_loops = strtoll(optarg, &dummy, 10);
4f20d592
CDL
387 if (errno) {
388 ret = -errno;
389 goto error;
390 }
7663a4aa 391
96df9799 392 break;
e06e3d71
HK
393 case 'e':
394 noevents = 1;
395 break;
396 case 'g':
397 notrigger = 1;
96df9799
JC
398 break;
399 case 'l':
c8ce9903 400 errno = 0;
96df9799 401 buf_len = strtoul(optarg, &dummy, 10);
4f20d592
CDL
402 if (errno) {
403 ret = -errno;
404 goto error;
405 }
7663a4aa 406
96df9799 407 break;
e06e3d71 408 case 'n':
de397db8
CDL
409 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 }
e06e3d71
HK
418 break;
419 case 't':
4f20d592 420 trigger_name = strdup(optarg);
e06e3d71 421 break;
7c7e9dad
CDL
422 case 'T':
423 errno = 0;
424 trig_num = strtoul(optarg, &dummy, 10);
425 if (errno)
426 return -errno;
427 break;
e06e3d71
HK
428 case 'w':
429 errno = 0;
430 timedelay = strtoul(optarg, &dummy, 10);
4f20d592
CDL
431 if (errno) {
432 ret = -errno;
433 goto error;
434 }
b6d5be57 435 break;
e58537cc 436 case '?':
e06e3d71 437 print_usage();
4f20d592
CDL
438 ret = -1;
439 goto error;
e58537cc
JC
440 }
441 }
442
443 /* Find the device requested */
de397db8
CDL
444 if (dev_num < 0 && !device_name) {
445 fprintf(stderr, "Device not set\n");
446 print_usage();
447 ret = -1;
448 goto error;
449 } 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;
4f20d592 453 goto error;
de397db8
CDL
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 }
e58537cc
JC
461 }
462 printf("iio device number being used is %d\n", dev_num);
463
e9e45b43 464 ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
de397db8
CDL
465 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 }
4f20d592 479 }
b6d5be57 480
7c7e9dad
CDL
481 if (notrigger) {
482 printf("trigger-less mode selected\n");
f8e81d7e 483 } else if (trig_num >= 0) {
7c7e9dad
CDL
484 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 {
ff1ac639 498 if (!trigger_name) {
b6d5be57
KW
499 /*
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;
4f20d592 508 goto error;
b6d5be57 509 }
e58537cc 510 }
e58537cc 511
793d6b5e
LW
512 /* 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;
4f20d592 521 goto error;
793d6b5e
LW
522 }
523 }
524
b6d5be57
KW
525 trig_num = find_type_by_name(trigger_name, "trigger");
526 if (trig_num < 0) {
d9abc615
CO
527 fprintf(stderr, "Failed to find the trigger %s\n",
528 trigger_name);
e83a47cf 529 ret = trig_num;
4f20d592 530 goto error;
b6d5be57 531 }
7663a4aa 532
b6d5be57 533 printf("iio trigger number being used is %d\n", trig_num);
7663a4aa 534 }
e58537cc
JC
535
536 /*
537 * Parse the files in scan_elements to identify what channels are
538 * present
539 */
8827faab 540 ret = build_channel_array(dev_dir_name, buffer_idx, &channels, &num_channels);
e58537cc 541 if (ret) {
d9abc615
CO
542 fprintf(stderr, "Problem reading scan element information\n"
543 "diag %s\n", dev_dir_name);
4f20d592 544 goto error;
e58537cc 545 }
7c5fe411 546 if (num_channels && autochannels == AUTOCHANNELS_ENABLED &&
71ccbe5d 547 !force_autochannels) {
5f991a92
LW
548 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
4e2cf0e2 553 if ((!num_channels && autochannels == AUTOCHANNELS_ENABLED) ||
7c5fe411 554 (autochannels == AUTOCHANNELS_ENABLED && force_autochannels)) {
4e2cf0e2 555 fprintf(stderr, "Enabling all channels\n");
5f991a92 556
8827faab 557 ret = enable_disable_all_channels(dev_dir_name, buffer_idx, 1);
5f991a92
LW
558 if (ret) {
559 fprintf(stderr, "Failed to enable all channels\n");
4f20d592 560 goto error;
5f991a92
LW
561 }
562
563 /* This flags that we need to disable the channels again */
564 autochannels = AUTOCHANNELS_ACTIVE;
565
8827faab 566 ret = build_channel_array(dev_dir_name, buffer_idx, &channels,
5f991a92
LW
567 &num_channels);
568 if (ret) {
569 fprintf(stderr, "Problem reading scan element "
570 "information\n"
571 "diag %s\n", dev_dir_name);
4f20d592 572 goto error;
5f991a92
LW
573 }
574 if (!num_channels) {
575 fprintf(stderr, "Still no channels after "
576 "auto-enabling, giving up\n");
4f20d592 577 goto error;
5f991a92
LW
578 }
579 }
580
581 if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) {
53dabafe
LW
582 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
5f991a92 586 "/*_en or pass -a to autoenable channels and "
8827faab 587 "try again.\n", dev_dir_name, buffer_idx);
53dabafe 588 ret = -ENOENT;
4f20d592 589 goto error;
53dabafe 590 }
e58537cc
JC
591
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 */
1aa04278 597 ret = asprintf(&buf_dir_name,
8827faab 598 "%siio:device%d/buffer%d", iio_dir, dev_num, buffer_idx);
e58537cc
JC
599 if (ret < 0) {
600 ret = -ENOMEM;
4f20d592 601 goto error;
e58537cc 602 }
b6d5be57 603
8827faab
AA
604 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
b6d5be57
KW
617 if (!notrigger) {
618 printf("%s %s\n", dev_dir_name, trigger_name);
7663a4aa
HK
619 /*
620 * Set the device trigger to be the data ready trigger found
621 * above
622 */
b6d5be57
KW
623 ret = write_sysfs_string_and_verify("trigger/current_trigger",
624 dev_dir_name,
625 trigger_name);
626 if (ret < 0) {
d9abc615
CO
627 fprintf(stderr,
628 "Failed to write current_trigger file\n");
4f20d592 629 goto error;
b6d5be57 630 }
e58537cc
JC
631 }
632
8827faab
AA
633 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
e58537cc
JC
662 /* Setup ring buffer parameters */
663 ret = write_sysfs_int("length", buf_dir_name, buf_len);
664 if (ret < 0)
4f20d592 665 goto error;
e58537cc
JC
666
667 /* Enable the buffer */
668 ret = write_sysfs_int("enable", buf_dir_name, 1);
e7231491
IT
669 if (ret < 0) {
670 fprintf(stderr,
8827faab
AA
671 "Failed to enable buffer '%s': %s\n",
672 buf_dir_name, strerror(-ret));
4f20d592 673 goto error;
e7231491 674 }
7663a4aa 675
d8da0eee 676 scan_size = size_from_channelarray(channels, num_channels);
7663a4aa 677 data = malloc(scan_size * buf_len);
e58537cc
JC
678 if (!data) {
679 ret = -ENOMEM;
4f20d592 680 goto error;
e58537cc
JC
681 }
682
8827faab
AA
683 /**
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 }
e58537cc
JC
696 }
697
8827faab
AA
698 /* 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;
e58537cc 702
55dda0ab 703 for (j = 0; j < num_loops || num_loops < 0; j++) {
30268a3d 704 if (!noevents) {
52615d47 705 struct pollfd pfd = {
8827faab 706 .fd = buf_fd,
52615d47
JC
707 .events = POLLIN,
708 };
709
6bb7cac8
HK
710 ret = poll(&pfd, 1, -1);
711 if (ret < 0) {
712 ret = -errno;
4f20d592 713 goto error;
6bb7cac8
HK
714 } else if (ret == 0) {
715 continue;
716 }
717
30268a3d 718 } else {
96df9799 719 usleep(timedelay);
e58537cc 720 }
30268a3d 721
7c919b61
MV
722 toread = buf_len;
723
8827faab 724 read_size = read(buf_fd, data, toread * scan_size);
97b603a4 725 if (read_size < 0) {
8749948a 726 if (errno == EAGAIN) {
d9abc615 727 fprintf(stderr, "nothing available\n");
97b603a4 728 continue;
7663a4aa 729 } else {
97b603a4 730 break;
7663a4aa 731 }
e58537cc 732 }
7663a4aa
HK
733 for (i = 0; i < read_size / scan_size; i++)
734 process_scan(data + scan_size * i, channels,
e58537cc
JC
735 num_channels);
736 }
737
4f20d592
CDL
738error:
739 cleanup();
e58537cc 740
8827faab
AA
741 if (fd >= 0 && close(fd) == -1)
742 perror("Failed to close character device");
743 if (buf_fd >= 0 && close(buf_fd) == -1)
6bb7cac8 744 perror("Failed to close buffer");
e58537cc 745 free(buffer_access);
a71bfb4a 746 free(data);
e58537cc 747 free(buf_dir_name);
63f05c85
HK
748 for (i = num_channels - 1; i >= 0; i--) {
749 free(channels[i].name);
750 free(channels[i].generic_name);
751 }
752 free(channels);
4f20d592 753 free(trigger_name);
de397db8 754 free(device_name);
d3ccfc41 755 free(dev_dir_name);
0e799878 756
e58537cc
JC
757 return ret;
758}