iio_utils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /* IIO - useful set of util functionality
  2. *
  3. * Copyright (c) 2008 Jonathan Cameron
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #ifndef _IIO_UTILS_H
  10. #define _IIO_UTILS_H
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <stdint.h>
  15. #include <dirent.h>
  16. #include <errno.h>
  17. #include <ctype.h>
  18. #include "iio_utils.h"
  19. const char *iio_dir = "/sys/bus/iio/devices/";
  20. static char * const iio_direction[] = {
  21. "in",
  22. "out",
  23. };
  24. /**
  25. * iioutils_break_up_name() - extract generic name from full channel name
  26. * @full_name: the full channel name
  27. * @generic_name: the output generic channel name
  28. **/
  29. int iioutils_break_up_name(const char *full_name,
  30. char **generic_name)
  31. {
  32. char *current;
  33. char *w, *r;
  34. char *working, *prefix = "";
  35. int i;
  36. for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++)
  37. if (!strncmp(full_name, iio_direction[i],
  38. strlen(iio_direction[i]))) {
  39. prefix = iio_direction[i];
  40. break;
  41. }
  42. current = strdup(full_name + strlen(prefix) + 1);
  43. working = strtok(current, "_\0");
  44. w = working;
  45. r = working;
  46. while (*r != '\0') {
  47. if (!isdigit(*r)) {
  48. *w = *r;
  49. w++;
  50. }
  51. r++;
  52. }
  53. *w = '\0';
  54. asprintf(generic_name, "%s_%s", prefix, working);
  55. free(current);
  56. return 0;
  57. }
  58. /**
  59. * iioutils_get_type() - find and process _type attribute data
  60. * @is_signed: output whether channel is signed
  61. * @bytes: output how many bytes the channel storage occupies
  62. * @mask: output a bit mask for the raw data
  63. * @be: big endian
  64. * @device_dir: the iio device directory
  65. * @name: the channel name
  66. * @generic_name: the channel type name
  67. **/
  68. int iioutils_get_type(unsigned *is_signed,
  69. unsigned *bytes,
  70. unsigned *bits_used,
  71. unsigned *shift,
  72. uint64_t *mask,
  73. unsigned *be,
  74. const char *device_dir,
  75. const char *name,
  76. const char *generic_name)
  77. {
  78. FILE *sysfsfp;
  79. int ret;
  80. DIR *dp;
  81. char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
  82. char signchar, endianchar;
  83. unsigned padint;
  84. const struct dirent *ent;
  85. ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
  86. if (ret < 0) {
  87. ret = -ENOMEM;
  88. goto error_ret;
  89. }
  90. ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
  91. if (ret < 0) {
  92. ret = -ENOMEM;
  93. goto error_free_scan_el_dir;
  94. }
  95. ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
  96. if (ret < 0) {
  97. ret = -ENOMEM;
  98. goto error_free_builtname;
  99. }
  100. dp = opendir(scan_el_dir);
  101. if (dp == NULL) {
  102. ret = -errno;
  103. goto error_free_builtname_generic;
  104. }
  105. while (ent = readdir(dp), ent != NULL)
  106. /*
  107. * Do we allow devices to override a generic name with
  108. * a specific one?
  109. */
  110. if ((strcmp(builtname, ent->d_name) == 0) ||
  111. (strcmp(builtname_generic, ent->d_name) == 0)) {
  112. ret = asprintf(&filename,
  113. "%s/%s", scan_el_dir, ent->d_name);
  114. if (ret < 0) {
  115. ret = -ENOMEM;
  116. goto error_closedir;
  117. }
  118. sysfsfp = fopen(filename, "r");
  119. if (sysfsfp == NULL) {
  120. ret = -errno;
  121. printf("failed to open %s\n", filename);
  122. goto error_free_filename;
  123. }
  124. ret = fscanf(sysfsfp,
  125. "%ce:%c%u/%u>>%u",
  126. &endianchar,
  127. &signchar,
  128. bits_used,
  129. &padint, shift);
  130. if (ret < 0) {
  131. ret = -errno;
  132. printf("failed to pass scan type description\n");
  133. goto error_close_sysfsfp;
  134. }
  135. *be = (endianchar == 'b');
  136. *bytes = padint / 8;
  137. if (*bits_used == 64)
  138. *mask = ~0;
  139. else
  140. *mask = (1 << *bits_used) - 1;
  141. if (signchar == 's')
  142. *is_signed = 1;
  143. else
  144. *is_signed = 0;
  145. fclose(sysfsfp);
  146. free(filename);
  147. filename = 0;
  148. sysfsfp = 0;
  149. }
  150. error_close_sysfsfp:
  151. if (sysfsfp)
  152. fclose(sysfsfp);
  153. error_free_filename:
  154. if (filename)
  155. free(filename);
  156. error_closedir:
  157. closedir(dp);
  158. error_free_builtname_generic:
  159. free(builtname_generic);
  160. error_free_builtname:
  161. free(builtname);
  162. error_free_scan_el_dir:
  163. free(scan_el_dir);
  164. error_ret:
  165. return ret;
  166. }
  167. int iioutils_get_param_float(float *output,
  168. const char *param_name,
  169. const char *device_dir,
  170. const char *name,
  171. const char *generic_name)
  172. {
  173. FILE *sysfsfp;
  174. int ret;
  175. DIR *dp;
  176. char *builtname, *builtname_generic;
  177. char *filename = NULL;
  178. const struct dirent *ent;
  179. ret = asprintf(&builtname, "%s_%s", name, param_name);
  180. if (ret < 0) {
  181. ret = -ENOMEM;
  182. goto error_ret;
  183. }
  184. ret = asprintf(&builtname_generic,
  185. "%s_%s", generic_name, param_name);
  186. if (ret < 0) {
  187. ret = -ENOMEM;
  188. goto error_free_builtname;
  189. }
  190. dp = opendir(device_dir);
  191. if (dp == NULL) {
  192. ret = -errno;
  193. goto error_free_builtname_generic;
  194. }
  195. while (ent = readdir(dp), ent != NULL)
  196. if ((strcmp(builtname, ent->d_name) == 0) ||
  197. (strcmp(builtname_generic, ent->d_name) == 0)) {
  198. ret = asprintf(&filename,
  199. "%s/%s", device_dir, ent->d_name);
  200. if (ret < 0) {
  201. ret = -ENOMEM;
  202. goto error_closedir;
  203. }
  204. sysfsfp = fopen(filename, "r");
  205. if (!sysfsfp) {
  206. ret = -errno;
  207. goto error_free_filename;
  208. }
  209. fscanf(sysfsfp, "%f", output);
  210. break;
  211. }
  212. error_free_filename:
  213. if (filename)
  214. free(filename);
  215. error_closedir:
  216. closedir(dp);
  217. error_free_builtname_generic:
  218. free(builtname_generic);
  219. error_free_builtname:
  220. free(builtname);
  221. error_ret:
  222. return ret;
  223. }
  224. /**
  225. * bsort_channel_array_by_index() - reorder so that the array is in index order
  226. *
  227. **/
  228. void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
  229. int cnt)
  230. {
  231. struct iio_channel_info temp;
  232. int x, y;
  233. for (x = 0; x < cnt; x++)
  234. for (y = 0; y < (cnt - 1); y++)
  235. if ((*ci_array)[y].index > (*ci_array)[y+1].index) {
  236. temp = (*ci_array)[y + 1];
  237. (*ci_array)[y + 1] = (*ci_array)[y];
  238. (*ci_array)[y] = temp;
  239. }
  240. }
  241. /**
  242. * build_channel_array() - function to figure out what channels are present
  243. * @device_dir: the IIO device directory in sysfs
  244. * @
  245. **/
  246. int build_channel_array(const char *device_dir,
  247. struct iio_channel_info **ci_array,
  248. int *counter)
  249. {
  250. DIR *dp;
  251. FILE *sysfsfp;
  252. int count, i;
  253. struct iio_channel_info *current;
  254. int ret;
  255. const struct dirent *ent;
  256. char *scan_el_dir;
  257. char *filename;
  258. *counter = 0;
  259. ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
  260. if (ret < 0) {
  261. ret = -ENOMEM;
  262. goto error_ret;
  263. }
  264. dp = opendir(scan_el_dir);
  265. if (dp == NULL) {
  266. ret = -errno;
  267. goto error_free_name;
  268. }
  269. while (ent = readdir(dp), ent != NULL)
  270. if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
  271. "_en") == 0) {
  272. ret = asprintf(&filename,
  273. "%s/%s", scan_el_dir, ent->d_name);
  274. if (ret < 0) {
  275. ret = -ENOMEM;
  276. goto error_close_dir;
  277. }
  278. sysfsfp = fopen(filename, "r");
  279. if (sysfsfp == NULL) {
  280. ret = -errno;
  281. free(filename);
  282. goto error_close_dir;
  283. }
  284. fscanf(sysfsfp, "%i", &ret);
  285. if (ret == 1)
  286. (*counter)++;
  287. fclose(sysfsfp);
  288. free(filename);
  289. }
  290. *ci_array = malloc(sizeof(**ci_array) * (*counter));
  291. if (*ci_array == NULL) {
  292. ret = -ENOMEM;
  293. goto error_close_dir;
  294. }
  295. seekdir(dp, 0);
  296. count = 0;
  297. while (ent = readdir(dp), ent != NULL) {
  298. if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
  299. "_en") == 0) {
  300. int current_enabled = 0;
  301. current = &(*ci_array)[count++];
  302. ret = asprintf(&filename,
  303. "%s/%s", scan_el_dir, ent->d_name);
  304. if (ret < 0) {
  305. ret = -ENOMEM;
  306. /* decrement count to avoid freeing name */
  307. count--;
  308. goto error_cleanup_array;
  309. }
  310. sysfsfp = fopen(filename, "r");
  311. if (sysfsfp == NULL) {
  312. ret = -errno;
  313. free(filename);
  314. count--;
  315. goto error_cleanup_array;
  316. }
  317. fscanf(sysfsfp, "%i", &current_enabled);
  318. fclose(sysfsfp);
  319. if (!current_enabled) {
  320. free(filename);
  321. count--;
  322. continue;
  323. }
  324. current->scale = 1.0;
  325. current->offset = 0;
  326. current->name = strndup(ent->d_name,
  327. strlen(ent->d_name) -
  328. strlen("_en"));
  329. if (current->name == NULL) {
  330. free(filename);
  331. ret = -ENOMEM;
  332. count--;
  333. goto error_cleanup_array;
  334. }
  335. /* Get the generic and specific name elements */
  336. ret = iioutils_break_up_name(current->name,
  337. &current->generic_name);
  338. if (ret) {
  339. free(filename);
  340. free(current->name);
  341. count--;
  342. goto error_cleanup_array;
  343. }
  344. ret = asprintf(&filename,
  345. "%s/%s_index",
  346. scan_el_dir,
  347. current->name);
  348. if (ret < 0) {
  349. free(filename);
  350. ret = -ENOMEM;
  351. goto error_cleanup_array;
  352. }
  353. sysfsfp = fopen(filename, "r");
  354. fscanf(sysfsfp, "%u", &current->index);
  355. fclose(sysfsfp);
  356. free(filename);
  357. /* Find the scale */
  358. ret = iioutils_get_param_float(&current->scale,
  359. "scale",
  360. device_dir,
  361. current->name,
  362. current->generic_name);
  363. if (ret < 0)
  364. goto error_cleanup_array;
  365. ret = iioutils_get_param_float(&current->offset,
  366. "offset",
  367. device_dir,
  368. current->name,
  369. current->generic_name);
  370. if (ret < 0)
  371. goto error_cleanup_array;
  372. ret = iioutils_get_type(&current->is_signed,
  373. &current->bytes,
  374. &current->bits_used,
  375. &current->shift,
  376. &current->mask,
  377. &current->be,
  378. device_dir,
  379. current->name,
  380. current->generic_name);
  381. }
  382. }
  383. closedir(dp);
  384. free(scan_el_dir);
  385. /* reorder so that the array is in index order */
  386. bsort_channel_array_by_index(ci_array, *counter);
  387. return 0;
  388. error_cleanup_array:
  389. for (i = count - 1; i >= 0; i--) {
  390. free((*ci_array)[i].name);
  391. free((*ci_array)[i].generic_name);
  392. }
  393. free(*ci_array);
  394. error_close_dir:
  395. closedir(dp);
  396. error_free_name:
  397. free(scan_el_dir);
  398. error_ret:
  399. return ret;
  400. }
  401. /**
  402. * find_type_by_name() - function to match top level types by name
  403. * @name: top level type instance name
  404. * @type: the type of top level instance being sort
  405. *
  406. * Typical types this is used for are device and trigger.
  407. **/
  408. int find_type_by_name(const char *name, const char *type)
  409. {
  410. const struct dirent *ent;
  411. int number, numstrlen;
  412. FILE *nameFile;
  413. DIR *dp;
  414. char thisname[IIO_MAX_NAME_LENGTH];
  415. char *filename;
  416. dp = opendir(iio_dir);
  417. if (dp == NULL) {
  418. printf("No industrialio devices available\n");
  419. return -ENODEV;
  420. }
  421. while (ent = readdir(dp), ent != NULL) {
  422. if (strcmp(ent->d_name, ".") != 0 &&
  423. strcmp(ent->d_name, "..") != 0 &&
  424. strlen(ent->d_name) > strlen(type) &&
  425. strncmp(ent->d_name, type, strlen(type)) == 0) {
  426. numstrlen = sscanf(ent->d_name + strlen(type),
  427. "%d",
  428. &number);
  429. /* verify the next character is not a colon */
  430. if (strncmp(ent->d_name + strlen(type) + numstrlen,
  431. ":",
  432. 1) != 0) {
  433. filename = malloc(strlen(iio_dir)
  434. + strlen(type)
  435. + numstrlen
  436. + 6);
  437. if (filename == NULL) {
  438. closedir(dp);
  439. return -ENOMEM;
  440. }
  441. sprintf(filename, "%s%s%d/name",
  442. iio_dir,
  443. type,
  444. number);
  445. nameFile = fopen(filename, "r");
  446. if (!nameFile) {
  447. free(filename);
  448. continue;
  449. }
  450. free(filename);
  451. fscanf(nameFile, "%s", thisname);
  452. fclose(nameFile);
  453. if (strcmp(name, thisname) == 0) {
  454. closedir(dp);
  455. return number;
  456. }
  457. }
  458. }
  459. }
  460. closedir(dp);
  461. return -ENODEV;
  462. }
  463. int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
  464. {
  465. int ret = 0;
  466. FILE *sysfsfp;
  467. int test;
  468. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  469. if (temp == NULL)
  470. return -ENOMEM;
  471. sprintf(temp, "%s/%s", basedir, filename);
  472. sysfsfp = fopen(temp, "w");
  473. if (sysfsfp == NULL) {
  474. ret = -errno;
  475. printf("failed to open %s\n", temp);
  476. goto error_free;
  477. }
  478. fprintf(sysfsfp, "%d", val);
  479. fclose(sysfsfp);
  480. if (verify) {
  481. sysfsfp = fopen(temp, "r");
  482. if (sysfsfp == NULL) {
  483. ret = -errno;
  484. printf("failed to open %s\n", temp);
  485. goto error_free;
  486. }
  487. fscanf(sysfsfp, "%d", &test);
  488. fclose(sysfsfp);
  489. if (test != val) {
  490. printf("Possible failure in int write %d to %s%s\n",
  491. val,
  492. basedir,
  493. filename);
  494. ret = -1;
  495. }
  496. }
  497. error_free:
  498. free(temp);
  499. return ret;
  500. }
  501. int write_sysfs_int(char *filename, char *basedir, int val)
  502. {
  503. return _write_sysfs_int(filename, basedir, val, 0);
  504. }
  505. int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
  506. {
  507. return _write_sysfs_int(filename, basedir, val, 1);
  508. }
  509. int _write_sysfs_string(char *filename, char *basedir, char *val, int verify)
  510. {
  511. int ret = 0;
  512. FILE *sysfsfp;
  513. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  514. if (temp == NULL) {
  515. printf("Memory allocation failed\n");
  516. return -ENOMEM;
  517. }
  518. sprintf(temp, "%s/%s", basedir, filename);
  519. sysfsfp = fopen(temp, "w");
  520. if (sysfsfp == NULL) {
  521. ret = -errno;
  522. printf("Could not open %s\n", temp);
  523. goto error_free;
  524. }
  525. fprintf(sysfsfp, "%s", val);
  526. fclose(sysfsfp);
  527. if (verify) {
  528. sysfsfp = fopen(temp, "r");
  529. if (sysfsfp == NULL) {
  530. ret = -errno;
  531. printf("could not open file to verify\n");
  532. goto error_free;
  533. }
  534. fscanf(sysfsfp, "%s", temp);
  535. fclose(sysfsfp);
  536. if (strcmp(temp, val) != 0) {
  537. printf("Possible failure in string write of %s "
  538. "Should be %s "
  539. "written to %s\%s\n",
  540. temp,
  541. val,
  542. basedir,
  543. filename);
  544. ret = -1;
  545. }
  546. }
  547. error_free:
  548. free(temp);
  549. return ret;
  550. }
  551. /**
  552. * write_sysfs_string_and_verify() - string write, readback and verify
  553. * @filename: name of file to write to
  554. * @basedir: the sysfs directory in which the file is to be found
  555. * @val: the string to write
  556. **/
  557. int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
  558. {
  559. return _write_sysfs_string(filename, basedir, val, 1);
  560. }
  561. int write_sysfs_string(char *filename, char *basedir, char *val)
  562. {
  563. return _write_sysfs_string(filename, basedir, val, 0);
  564. }
  565. int read_sysfs_posint(char *filename, char *basedir)
  566. {
  567. int ret;
  568. FILE *sysfsfp;
  569. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  570. if (temp == NULL) {
  571. printf("Memory allocation failed");
  572. return -ENOMEM;
  573. }
  574. sprintf(temp, "%s/%s", basedir, filename);
  575. sysfsfp = fopen(temp, "r");
  576. if (sysfsfp == NULL) {
  577. ret = -errno;
  578. goto error_free;
  579. }
  580. fscanf(sysfsfp, "%d\n", &ret);
  581. fclose(sysfsfp);
  582. error_free:
  583. free(temp);
  584. return ret;
  585. }
  586. int read_sysfs_float(char *filename, char *basedir, float *val)
  587. {
  588. int ret = 0;
  589. FILE *sysfsfp;
  590. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  591. if (temp == NULL) {
  592. printf("Memory allocation failed");
  593. return -ENOMEM;
  594. }
  595. sprintf(temp, "%s/%s", basedir, filename);
  596. sysfsfp = fopen(temp, "r");
  597. if (sysfsfp == NULL) {
  598. ret = -errno;
  599. goto error_free;
  600. }
  601. fscanf(sysfsfp, "%f\n", val);
  602. fclose(sysfsfp);
  603. error_free:
  604. free(temp);
  605. return ret;
  606. }
  607. int read_sysfs_string(const char *filename, const char *basedir, char *str)
  608. {
  609. int ret = 0;
  610. FILE *sysfsfp;
  611. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  612. if (temp == NULL) {
  613. printf("Memory allocation failed");
  614. return -ENOMEM;
  615. }
  616. sprintf(temp, "%s/%s", basedir, filename);
  617. sysfsfp = fopen(temp, "r");
  618. if (sysfsfp == NULL) {
  619. ret = -errno;
  620. goto error_free;
  621. }
  622. fscanf(sysfsfp, "%s\n", str);
  623. fclose(sysfsfp);
  624. error_free:
  625. free(temp);
  626. return ret;
  627. }
  628. #endif /* _IIO_UTILS_H */