iio_generic_buffer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Industrialio buffer test code.
  3. *
  4. * Copyright (c) 2008 Jonathan Cameron
  5. *
  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.
  15. */
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <dirent.h>
  19. #include <fcntl.h>
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <sys/stat.h>
  23. #include <sys/dir.h>
  24. #include <linux/types.h>
  25. #include <string.h>
  26. #include <poll.h>
  27. #include <endian.h>
  28. #include <getopt.h>
  29. #include <inttypes.h>
  30. #include <stdbool.h>
  31. #include <signal.h>
  32. #include <sys/ioctl.h>
  33. #include <linux/iio/buffer.h>
  34. #include "iio_utils.h"
  35. /**
  36. * enum autochan - state for the automatic channel enabling mechanism
  37. */
  38. enum autochan {
  39. AUTOCHANNELS_DISABLED,
  40. AUTOCHANNELS_ENABLED,
  41. AUTOCHANNELS_ACTIVE,
  42. };
  43. /**
  44. * size_from_channelarray() - calculate the storage size of a scan
  45. * @channels: the channel info array
  46. * @num_channels: number of channels
  47. *
  48. * Has the side effect of filling the channels[i].location values used
  49. * in processing the buffer output.
  50. **/
  51. static unsigned int size_from_channelarray(struct iio_channel_info *channels, int num_channels)
  52. {
  53. unsigned int bytes = 0;
  54. int i = 0, max = 0;
  55. unsigned int misalignment;
  56. while (i < num_channels) {
  57. if (channels[i].bytes > max)
  58. max = channels[i].bytes;
  59. if (bytes % channels[i].bytes == 0)
  60. channels[i].location = bytes;
  61. else
  62. channels[i].location = bytes - bytes % channels[i].bytes
  63. + channels[i].bytes;
  64. bytes = channels[i].location + channels[i].bytes;
  65. i++;
  66. }
  67. /*
  68. * We want the data in next sample to also be properly aligned so
  69. * we'll add padding at the end if needed. Adding padding only
  70. * works for channel data which size is 2^n bytes.
  71. */
  72. misalignment = bytes % max;
  73. if (misalignment)
  74. bytes += max - misalignment;
  75. return bytes;
  76. }
  77. static void print1byte(uint8_t input, struct iio_channel_info *info)
  78. {
  79. /*
  80. * Shift before conversion to avoid sign extension
  81. * of left aligned data
  82. */
  83. input >>= info->shift;
  84. input &= info->mask;
  85. if (info->is_signed) {
  86. int8_t val = (int8_t)(input << (8 - info->bits_used)) >>
  87. (8 - info->bits_used);
  88. printf("%05f ", ((float)val + info->offset) * info->scale);
  89. } else {
  90. printf("%05f ", ((float)input + info->offset) * info->scale);
  91. }
  92. }
  93. static void print2byte(uint16_t input, struct iio_channel_info *info)
  94. {
  95. /* First swap if incorrect endian */
  96. if (info->be)
  97. input = be16toh(input);
  98. else
  99. input = le16toh(input);
  100. /*
  101. * Shift before conversion to avoid sign extension
  102. * of left aligned data
  103. */
  104. input >>= info->shift;
  105. input &= info->mask;
  106. if (info->is_signed) {
  107. int16_t val = (int16_t)(input << (16 - info->bits_used)) >>
  108. (16 - info->bits_used);
  109. printf("%05f ", ((float)val + info->offset) * info->scale);
  110. } else {
  111. printf("%05f ", ((float)input + info->offset) * info->scale);
  112. }
  113. }
  114. static void print4byte(uint32_t input, struct iio_channel_info *info)
  115. {
  116. /* First swap if incorrect endian */
  117. if (info->be)
  118. input = be32toh(input);
  119. else
  120. input = le32toh(input);
  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. static void print8byte(uint64_t input, struct iio_channel_info *info)
  136. {
  137. /* First swap if incorrect endian */
  138. if (info->be)
  139. input = be64toh(input);
  140. else
  141. input = le64toh(input);
  142. /*
  143. * Shift before conversion to avoid sign extension
  144. * of left aligned data
  145. */
  146. input >>= info->shift;
  147. input &= info->mask;
  148. if (info->is_signed) {
  149. int64_t val = (int64_t)(input << (64 - info->bits_used)) >>
  150. (64 - info->bits_used);
  151. /* special case for timestamp */
  152. if (info->scale == 1.0f && info->offset == 0.0f)
  153. printf("%" PRId64 " ", val);
  154. else
  155. printf("%05f ",
  156. ((float)val + info->offset) * info->scale);
  157. } else {
  158. printf("%05f ", ((float)input + info->offset) * info->scale);
  159. }
  160. }
  161. /**
  162. * process_scan() - print out the values in SI units
  163. * @data: pointer to the start of the scan
  164. * @channels: information about the channels.
  165. * Note: size_from_channelarray must have been called first
  166. * to fill the location offsets.
  167. * @num_channels: number of channels
  168. **/
  169. static void process_scan(char *data, struct iio_channel_info *channels,
  170. int num_channels)
  171. {
  172. int k;
  173. for (k = 0; k < num_channels; k++)
  174. switch (channels[k].bytes) {
  175. /* only a few cases implemented so far */
  176. case 1:
  177. print1byte(*(uint8_t *)(data + channels[k].location),
  178. &channels[k]);
  179. break;
  180. case 2:
  181. print2byte(*(uint16_t *)(data + channels[k].location),
  182. &channels[k]);
  183. break;
  184. case 4:
  185. print4byte(*(uint32_t *)(data + channels[k].location),
  186. &channels[k]);
  187. break;
  188. case 8:
  189. print8byte(*(uint64_t *)(data + channels[k].location),
  190. &channels[k]);
  191. break;
  192. default:
  193. break;
  194. }
  195. printf("\n");
  196. }
  197. static int enable_disable_all_channels(char *dev_dir_name, int buffer_idx, int enable)
  198. {
  199. const struct dirent *ent;
  200. char scanelemdir[256];
  201. DIR *dp;
  202. int ret;
  203. snprintf(scanelemdir, sizeof(scanelemdir),
  204. FORMAT_SCAN_ELEMENTS_DIR, dev_dir_name, buffer_idx);
  205. scanelemdir[sizeof(scanelemdir)-1] = '\0';
  206. dp = opendir(scanelemdir);
  207. if (!dp) {
  208. fprintf(stderr, "Enabling/disabling channels: can't open %s\n",
  209. scanelemdir);
  210. return -EIO;
  211. }
  212. ret = -ENOENT;
  213. while (ent = readdir(dp), ent) {
  214. if (iioutils_check_suffix(ent->d_name, "_en")) {
  215. printf("%sabling: %s\n",
  216. enable ? "En" : "Dis",
  217. ent->d_name);
  218. ret = write_sysfs_int(ent->d_name, scanelemdir,
  219. enable);
  220. if (ret < 0)
  221. fprintf(stderr, "Failed to enable/disable %s\n",
  222. ent->d_name);
  223. }
  224. }
  225. if (closedir(dp) == -1) {
  226. perror("Enabling/disabling channels: "
  227. "Failed to close directory");
  228. return -errno;
  229. }
  230. return 0;
  231. }
  232. static void print_usage(void)
  233. {
  234. fprintf(stderr, "Usage: generic_buffer [options]...\n"
  235. "Capture, convert and output data from IIO device buffer\n"
  236. " -a Auto-activate all available channels\n"
  237. " -A Force-activate ALL channels\n"
  238. " -b <n> The buffer which to open (by index), default 0\n"
  239. " -c <n> Do n conversions, or loop forever if n < 0\n"
  240. " -e Disable wait for event (new data)\n"
  241. " -g Use trigger-less mode\n"
  242. " -l <n> Set buffer length to n samples\n"
  243. " --device-name -n <name>\n"
  244. " --device-num -N <num>\n"
  245. " Set device by name or number (mandatory)\n"
  246. " --trigger-name -t <name>\n"
  247. " --trigger-num -T <num>\n"
  248. " Set trigger by name or number\n"
  249. " -w <n> Set delay between reads in us (event-less mode)\n");
  250. }
  251. static enum autochan autochannels = AUTOCHANNELS_DISABLED;
  252. static char *dev_dir_name = NULL;
  253. static char *buf_dir_name = NULL;
  254. static int buffer_idx = 0;
  255. static bool current_trigger_set = false;
  256. static void cleanup(void)
  257. {
  258. int ret;
  259. /* Disable trigger */
  260. if (dev_dir_name && current_trigger_set) {
  261. /* Disconnect the trigger - just write a dummy name. */
  262. ret = write_sysfs_string("trigger/current_trigger",
  263. dev_dir_name, "NULL");
  264. if (ret < 0)
  265. fprintf(stderr, "Failed to disable trigger: %s\n",
  266. strerror(-ret));
  267. current_trigger_set = false;
  268. }
  269. /* Disable buffer */
  270. if (buf_dir_name) {
  271. ret = write_sysfs_int("enable", buf_dir_name, 0);
  272. if (ret < 0)
  273. fprintf(stderr, "Failed to disable buffer: %s\n",
  274. strerror(-ret));
  275. }
  276. /* Disable channels if auto-enabled */
  277. if (dev_dir_name && autochannels == AUTOCHANNELS_ACTIVE) {
  278. ret = enable_disable_all_channels(dev_dir_name, buffer_idx, 0);
  279. if (ret)
  280. fprintf(stderr, "Failed to disable all channels\n");
  281. autochannels = AUTOCHANNELS_DISABLED;
  282. }
  283. }
  284. static void sig_handler(int signum)
  285. {
  286. fprintf(stderr, "Caught signal %d\n", signum);
  287. cleanup();
  288. exit(-signum);
  289. }
  290. static void register_cleanup(void)
  291. {
  292. struct sigaction sa = { .sa_handler = sig_handler };
  293. const int signums[] = { SIGINT, SIGTERM, SIGABRT };
  294. int ret, i;
  295. for (i = 0; i < ARRAY_SIZE(signums); ++i) {
  296. ret = sigaction(signums[i], &sa, NULL);
  297. if (ret) {
  298. perror("Failed to register signal handler");
  299. exit(-1);
  300. }
  301. }
  302. }
  303. static const struct option longopts[] = {
  304. { "device-name", 1, 0, 'n' },
  305. { "device-num", 1, 0, 'N' },
  306. { "trigger-name", 1, 0, 't' },
  307. { "trigger-num", 1, 0, 'T' },
  308. { },
  309. };
  310. int main(int argc, char **argv)
  311. {
  312. long long num_loops = 2;
  313. unsigned long timedelay = 1000000;
  314. unsigned long buf_len = 128;
  315. ssize_t i;
  316. unsigned long long j;
  317. unsigned long toread;
  318. int ret, c;
  319. struct stat st;
  320. int fd = -1;
  321. int buf_fd = -1;
  322. int num_channels = 0;
  323. char *trigger_name = NULL, *device_name = NULL;
  324. char *data = NULL;
  325. ssize_t read_size;
  326. int dev_num = -1, trig_num = -1;
  327. char *buffer_access = NULL;
  328. unsigned int scan_size;
  329. int noevents = 0;
  330. int notrigger = 0;
  331. char *dummy;
  332. bool force_autochannels = false;
  333. struct iio_channel_info *channels = NULL;
  334. register_cleanup();
  335. while ((c = getopt_long(argc, argv, "aAb:c:egl:n:N:t:T:w:?", longopts,
  336. NULL)) != -1) {
  337. switch (c) {
  338. case 'a':
  339. autochannels = AUTOCHANNELS_ENABLED;
  340. break;
  341. case 'A':
  342. autochannels = AUTOCHANNELS_ENABLED;
  343. force_autochannels = true;
  344. break;
  345. case 'b':
  346. errno = 0;
  347. buffer_idx = strtoll(optarg, &dummy, 10);
  348. if (errno) {
  349. ret = -errno;
  350. goto error;
  351. }
  352. if (buffer_idx < 0) {
  353. ret = -ERANGE;
  354. goto error;
  355. }
  356. break;
  357. case 'c':
  358. errno = 0;
  359. num_loops = strtoll(optarg, &dummy, 10);
  360. if (errno) {
  361. ret = -errno;
  362. goto error;
  363. }
  364. break;
  365. case 'e':
  366. noevents = 1;
  367. break;
  368. case 'g':
  369. notrigger = 1;
  370. break;
  371. case 'l':
  372. errno = 0;
  373. buf_len = strtoul(optarg, &dummy, 10);
  374. if (errno) {
  375. ret = -errno;
  376. goto error;
  377. }
  378. break;
  379. case 'n':
  380. device_name = strdup(optarg);
  381. break;
  382. case 'N':
  383. errno = 0;
  384. dev_num = strtoul(optarg, &dummy, 10);
  385. if (errno) {
  386. ret = -errno;
  387. goto error;
  388. }
  389. break;
  390. case 't':
  391. trigger_name = strdup(optarg);
  392. break;
  393. case 'T':
  394. errno = 0;
  395. trig_num = strtoul(optarg, &dummy, 10);
  396. if (errno)
  397. return -errno;
  398. break;
  399. case 'w':
  400. errno = 0;
  401. timedelay = strtoul(optarg, &dummy, 10);
  402. if (errno) {
  403. ret = -errno;
  404. goto error;
  405. }
  406. break;
  407. case '?':
  408. print_usage();
  409. ret = -1;
  410. goto error;
  411. }
  412. }
  413. /* Find the device requested */
  414. if (dev_num < 0 && !device_name) {
  415. fprintf(stderr, "Device not set\n");
  416. print_usage();
  417. ret = -1;
  418. goto error;
  419. } else if (dev_num >= 0 && device_name) {
  420. fprintf(stderr, "Only one of --device-num or --device-name needs to be set\n");
  421. print_usage();
  422. ret = -1;
  423. goto error;
  424. } else if (dev_num < 0) {
  425. dev_num = find_type_by_name(device_name, "iio:device");
  426. if (dev_num < 0) {
  427. fprintf(stderr, "Failed to find the %s\n", device_name);
  428. ret = dev_num;
  429. goto error;
  430. }
  431. }
  432. printf("iio device number being used is %d\n", dev_num);
  433. ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num);
  434. if (ret < 0)
  435. return -ENOMEM;
  436. /* Fetch device_name if specified by number */
  437. if (!device_name) {
  438. device_name = malloc(IIO_MAX_NAME_LENGTH);
  439. if (!device_name) {
  440. ret = -ENOMEM;
  441. goto error;
  442. }
  443. ret = read_sysfs_string("name", dev_dir_name, device_name);
  444. if (ret < 0) {
  445. fprintf(stderr, "Failed to read name of device %d\n", dev_num);
  446. goto error;
  447. }
  448. }
  449. if (notrigger) {
  450. printf("trigger-less mode selected\n");
  451. } else if (trig_num >= 0) {
  452. char *trig_dev_name;
  453. ret = asprintf(&trig_dev_name, "%strigger%d", iio_dir, trig_num);
  454. if (ret < 0) {
  455. return -ENOMEM;
  456. }
  457. trigger_name = malloc(IIO_MAX_NAME_LENGTH);
  458. ret = read_sysfs_string("name", trig_dev_name, trigger_name);
  459. free(trig_dev_name);
  460. if (ret < 0) {
  461. fprintf(stderr, "Failed to read trigger%d name from\n", trig_num);
  462. return ret;
  463. }
  464. printf("iio trigger number being used is %d\n", trig_num);
  465. } else {
  466. if (!trigger_name) {
  467. /*
  468. * Build the trigger name. If it is device associated
  469. * its name is <device_name>_dev[n] where n matches
  470. * the device number found above.
  471. */
  472. ret = asprintf(&trigger_name,
  473. "%s-dev%d", device_name, dev_num);
  474. if (ret < 0) {
  475. ret = -ENOMEM;
  476. goto error;
  477. }
  478. }
  479. /* Look for this "-devN" trigger */
  480. trig_num = find_type_by_name(trigger_name, "trigger");
  481. if (trig_num < 0) {
  482. /* OK try the simpler "-trigger" suffix instead */
  483. free(trigger_name);
  484. ret = asprintf(&trigger_name,
  485. "%s-trigger", device_name);
  486. if (ret < 0) {
  487. ret = -ENOMEM;
  488. goto error;
  489. }
  490. }
  491. trig_num = find_type_by_name(trigger_name, "trigger");
  492. if (trig_num < 0) {
  493. fprintf(stderr, "Failed to find the trigger %s\n",
  494. trigger_name);
  495. ret = trig_num;
  496. goto error;
  497. }
  498. printf("iio trigger number being used is %d\n", trig_num);
  499. }
  500. /*
  501. * Parse the files in scan_elements to identify what channels are
  502. * present
  503. */
  504. ret = build_channel_array(dev_dir_name, buffer_idx, &channels, &num_channels);
  505. if (ret) {
  506. fprintf(stderr, "Problem reading scan element information\n"
  507. "diag %s\n", dev_dir_name);
  508. goto error;
  509. }
  510. if (num_channels && autochannels == AUTOCHANNELS_ENABLED &&
  511. !force_autochannels) {
  512. fprintf(stderr, "Auto-channels selected but some channels "
  513. "are already activated in sysfs\n");
  514. fprintf(stderr, "Proceeding without activating any channels\n");
  515. }
  516. if ((!num_channels && autochannels == AUTOCHANNELS_ENABLED) ||
  517. (autochannels == AUTOCHANNELS_ENABLED && force_autochannels)) {
  518. fprintf(stderr, "Enabling all channels\n");
  519. ret = enable_disable_all_channels(dev_dir_name, buffer_idx, 1);
  520. if (ret) {
  521. fprintf(stderr, "Failed to enable all channels\n");
  522. goto error;
  523. }
  524. /* This flags that we need to disable the channels again */
  525. autochannels = AUTOCHANNELS_ACTIVE;
  526. ret = build_channel_array(dev_dir_name, buffer_idx, &channels,
  527. &num_channels);
  528. if (ret) {
  529. fprintf(stderr, "Problem reading scan element "
  530. "information\n"
  531. "diag %s\n", dev_dir_name);
  532. goto error;
  533. }
  534. if (!num_channels) {
  535. fprintf(stderr, "Still no channels after "
  536. "auto-enabling, giving up\n");
  537. goto error;
  538. }
  539. }
  540. if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) {
  541. fprintf(stderr,
  542. "No channels are enabled, we have nothing to scan.\n");
  543. fprintf(stderr, "Enable channels manually in "
  544. FORMAT_SCAN_ELEMENTS_DIR
  545. "/*_en or pass -a to autoenable channels and "
  546. "try again.\n", dev_dir_name, buffer_idx);
  547. ret = -ENOENT;
  548. goto error;
  549. }
  550. /*
  551. * Construct the directory name for the associated buffer.
  552. * As we know that the lis3l02dq has only one buffer this may
  553. * be built rather than found.
  554. */
  555. ret = asprintf(&buf_dir_name,
  556. "%siio:device%d/buffer%d", iio_dir, dev_num, buffer_idx);
  557. if (ret < 0) {
  558. ret = -ENOMEM;
  559. goto error;
  560. }
  561. if (stat(buf_dir_name, &st)) {
  562. fprintf(stderr, "Could not stat() '%s', got error %d: %s\n",
  563. buf_dir_name, errno, strerror(errno));
  564. ret = -errno;
  565. goto error;
  566. }
  567. if (!S_ISDIR(st.st_mode)) {
  568. fprintf(stderr, "File '%s' is not a directory\n", buf_dir_name);
  569. ret = -EFAULT;
  570. goto error;
  571. }
  572. if (!notrigger) {
  573. printf("%s %s\n", dev_dir_name, trigger_name);
  574. /*
  575. * Set the device trigger to be the data ready trigger found
  576. * above
  577. */
  578. ret = write_sysfs_string_and_verify("trigger/current_trigger",
  579. dev_dir_name,
  580. trigger_name);
  581. if (ret < 0) {
  582. fprintf(stderr,
  583. "Failed to write current_trigger file\n");
  584. goto error;
  585. }
  586. }
  587. ret = asprintf(&buffer_access, "/dev/iio:device%d", dev_num);
  588. if (ret < 0) {
  589. ret = -ENOMEM;
  590. goto error;
  591. }
  592. /* Attempt to open non blocking the access dev */
  593. fd = open(buffer_access, O_RDONLY | O_NONBLOCK);
  594. if (fd == -1) { /* TODO: If it isn't there make the node */
  595. ret = -errno;
  596. fprintf(stderr, "Failed to open %s\n", buffer_access);
  597. goto error;
  598. }
  599. /* specify for which buffer index we want an FD */
  600. buf_fd = buffer_idx;
  601. ret = ioctl(fd, IIO_BUFFER_GET_FD_IOCTL, &buf_fd);
  602. if (ret == -1 || buf_fd == -1) {
  603. ret = -errno;
  604. if (ret == -ENODEV || ret == -EINVAL)
  605. fprintf(stderr,
  606. "Device does not have this many buffers\n");
  607. else
  608. fprintf(stderr, "Failed to retrieve buffer fd\n");
  609. goto error;
  610. }
  611. /* Setup ring buffer parameters */
  612. ret = write_sysfs_int("length", buf_dir_name, buf_len);
  613. if (ret < 0)
  614. goto error;
  615. /* Enable the buffer */
  616. ret = write_sysfs_int("enable", buf_dir_name, 1);
  617. if (ret < 0) {
  618. fprintf(stderr,
  619. "Failed to enable buffer '%s': %s\n",
  620. buf_dir_name, strerror(-ret));
  621. goto error;
  622. }
  623. scan_size = size_from_channelarray(channels, num_channels);
  624. size_t total_buf_len = scan_size * buf_len;
  625. if (scan_size > 0 && total_buf_len / scan_size != buf_len) {
  626. ret = -EFAULT;
  627. perror("Integer overflow happened when calculate scan_size * buf_len");
  628. goto error;
  629. }
  630. data = malloc(total_buf_len);
  631. if (!data) {
  632. ret = -ENOMEM;
  633. goto error;
  634. }
  635. /**
  636. * This check is being done here for sanity reasons, however it
  637. * should be omitted under normal operation.
  638. * If this is buffer0, we check that we get EBUSY after this point.
  639. */
  640. if (buffer_idx == 0) {
  641. errno = 0;
  642. read_size = read(fd, data, 1);
  643. if (read_size > -1 || errno != EBUSY) {
  644. ret = -EFAULT;
  645. perror("Reading from '%s' should not be possible after ioctl()");
  646. goto error;
  647. }
  648. }
  649. /* close now the main chardev FD and let the buffer FD work */
  650. if (close(fd) == -1)
  651. perror("Failed to close character device file");
  652. fd = -1;
  653. for (j = 0; j < num_loops || num_loops < 0; j++) {
  654. if (!noevents) {
  655. struct pollfd pfd = {
  656. .fd = buf_fd,
  657. .events = POLLIN,
  658. };
  659. ret = poll(&pfd, 1, -1);
  660. if (ret < 0) {
  661. ret = -errno;
  662. goto error;
  663. } else if (ret == 0) {
  664. continue;
  665. }
  666. } else {
  667. usleep(timedelay);
  668. }
  669. toread = buf_len;
  670. read_size = read(buf_fd, data, toread * scan_size);
  671. if (read_size < 0) {
  672. if (errno == EAGAIN) {
  673. fprintf(stderr, "nothing available\n");
  674. continue;
  675. } else {
  676. break;
  677. }
  678. }
  679. for (i = 0; i < read_size / scan_size; i++)
  680. process_scan(data + scan_size * i, channels,
  681. num_channels);
  682. }
  683. error:
  684. cleanup();
  685. if (fd >= 0 && close(fd) == -1)
  686. perror("Failed to close character device");
  687. if (buf_fd >= 0 && close(buf_fd) == -1)
  688. perror("Failed to close buffer");
  689. free(buffer_access);
  690. free(data);
  691. free(buf_dir_name);
  692. for (i = num_channels - 1; i >= 0; i--) {
  693. free(channels[i].name);
  694. free(channels[i].generic_name);
  695. }
  696. free(channels);
  697. free(trigger_name);
  698. free(device_name);
  699. free(dev_dir_name);
  700. return ret;
  701. }