iio_utils.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. printf("failed to open %s\n", filename);
  121. ret = -errno;
  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. printf("failed to pass scan type description\n");
  132. ret = -errno;
  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. free(filename);
  313. ret = -errno;
  314. goto error_cleanup_array;
  315. }
  316. fscanf(sysfsfp, "%i", &current_enabled);
  317. fclose(sysfsfp);
  318. if (!current_enabled) {
  319. free(filename);
  320. count--;
  321. continue;
  322. }
  323. current->scale = 1.0;
  324. current->offset = 0;
  325. current->name = strndup(ent->d_name,
  326. strlen(ent->d_name) -
  327. strlen("_en"));
  328. if (current->name == NULL) {
  329. free(filename);
  330. ret = -ENOMEM;
  331. goto error_cleanup_array;
  332. }
  333. /* Get the generic and specific name elements */
  334. ret = iioutils_break_up_name(current->name,
  335. &current->generic_name);
  336. if (ret) {
  337. free(filename);
  338. goto error_cleanup_array;
  339. }
  340. ret = asprintf(&filename,
  341. "%s/%s_index",
  342. scan_el_dir,
  343. current->name);
  344. if (ret < 0) {
  345. free(filename);
  346. ret = -ENOMEM;
  347. goto error_cleanup_array;
  348. }
  349. sysfsfp = fopen(filename, "r");
  350. fscanf(sysfsfp, "%u", &current->index);
  351. fclose(sysfsfp);
  352. free(filename);
  353. /* Find the scale */
  354. ret = iioutils_get_param_float(&current->scale,
  355. "scale",
  356. device_dir,
  357. current->name,
  358. current->generic_name);
  359. if (ret < 0)
  360. goto error_cleanup_array;
  361. ret = iioutils_get_param_float(&current->offset,
  362. "offset",
  363. device_dir,
  364. current->name,
  365. current->generic_name);
  366. if (ret < 0)
  367. goto error_cleanup_array;
  368. ret = iioutils_get_type(&current->is_signed,
  369. &current->bytes,
  370. &current->bits_used,
  371. &current->shift,
  372. &current->mask,
  373. &current->be,
  374. device_dir,
  375. current->name,
  376. current->generic_name);
  377. }
  378. }
  379. closedir(dp);
  380. /* reorder so that the array is in index order */
  381. bsort_channel_array_by_index(ci_array, *counter);
  382. return 0;
  383. error_cleanup_array:
  384. for (i = count - 1; i >= 0; i--)
  385. free((*ci_array)[i].name);
  386. free(*ci_array);
  387. error_close_dir:
  388. closedir(dp);
  389. error_free_name:
  390. free(scan_el_dir);
  391. error_ret:
  392. return ret;
  393. }
  394. /**
  395. * find_type_by_name() - function to match top level types by name
  396. * @name: top level type instance name
  397. * @type: the type of top level instance being sort
  398. *
  399. * Typical types this is used for are device and trigger.
  400. **/
  401. int find_type_by_name(const char *name, const char *type)
  402. {
  403. const struct dirent *ent;
  404. int number, numstrlen;
  405. FILE *nameFile;
  406. DIR *dp;
  407. char thisname[IIO_MAX_NAME_LENGTH];
  408. char *filename;
  409. dp = opendir(iio_dir);
  410. if (dp == NULL) {
  411. printf("No industrialio devices available\n");
  412. return -ENODEV;
  413. }
  414. while (ent = readdir(dp), ent != NULL) {
  415. if (strcmp(ent->d_name, ".") != 0 &&
  416. strcmp(ent->d_name, "..") != 0 &&
  417. strlen(ent->d_name) > strlen(type) &&
  418. strncmp(ent->d_name, type, strlen(type)) == 0) {
  419. numstrlen = sscanf(ent->d_name + strlen(type),
  420. "%d",
  421. &number);
  422. /* verify the next character is not a colon */
  423. if (strncmp(ent->d_name + strlen(type) + numstrlen,
  424. ":",
  425. 1) != 0) {
  426. filename = malloc(strlen(iio_dir)
  427. + strlen(type)
  428. + numstrlen
  429. + 6);
  430. if (filename == NULL) {
  431. closedir(dp);
  432. return -ENOMEM;
  433. }
  434. sprintf(filename, "%s%s%d/name",
  435. iio_dir,
  436. type,
  437. number);
  438. nameFile = fopen(filename, "r");
  439. if (!nameFile) {
  440. free(filename);
  441. continue;
  442. }
  443. free(filename);
  444. fscanf(nameFile, "%s", thisname);
  445. fclose(nameFile);
  446. if (strcmp(name, thisname) == 0) {
  447. closedir(dp);
  448. return number;
  449. }
  450. }
  451. }
  452. }
  453. closedir(dp);
  454. return -ENODEV;
  455. }
  456. int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
  457. {
  458. int ret = 0;
  459. FILE *sysfsfp;
  460. int test;
  461. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  462. if (temp == NULL)
  463. return -ENOMEM;
  464. sprintf(temp, "%s/%s", basedir, filename);
  465. sysfsfp = fopen(temp, "w");
  466. if (sysfsfp == NULL) {
  467. printf("failed to open %s\n", temp);
  468. ret = -errno;
  469. goto error_free;
  470. }
  471. fprintf(sysfsfp, "%d", val);
  472. fclose(sysfsfp);
  473. if (verify) {
  474. sysfsfp = fopen(temp, "r");
  475. if (sysfsfp == NULL) {
  476. printf("failed to open %s\n", temp);
  477. ret = -errno;
  478. goto error_free;
  479. }
  480. fscanf(sysfsfp, "%d", &test);
  481. fclose(sysfsfp);
  482. if (test != val) {
  483. printf("Possible failure in int write %d to %s%s\n",
  484. val,
  485. basedir,
  486. filename);
  487. ret = -1;
  488. }
  489. }
  490. error_free:
  491. free(temp);
  492. return ret;
  493. }
  494. int write_sysfs_int(char *filename, char *basedir, int val)
  495. {
  496. return _write_sysfs_int(filename, basedir, val, 0);
  497. }
  498. int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
  499. {
  500. return _write_sysfs_int(filename, basedir, val, 1);
  501. }
  502. int _write_sysfs_string(char *filename, char *basedir, char *val, int verify)
  503. {
  504. int ret = 0;
  505. FILE *sysfsfp;
  506. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  507. if (temp == NULL) {
  508. printf("Memory allocation failed\n");
  509. return -ENOMEM;
  510. }
  511. sprintf(temp, "%s/%s", basedir, filename);
  512. sysfsfp = fopen(temp, "w");
  513. if (sysfsfp == NULL) {
  514. printf("Could not open %s\n", temp);
  515. ret = -errno;
  516. goto error_free;
  517. }
  518. fprintf(sysfsfp, "%s", val);
  519. fclose(sysfsfp);
  520. if (verify) {
  521. sysfsfp = fopen(temp, "r");
  522. if (sysfsfp == NULL) {
  523. printf("could not open file to verify\n");
  524. ret = -errno;
  525. goto error_free;
  526. }
  527. fscanf(sysfsfp, "%s", temp);
  528. fclose(sysfsfp);
  529. if (strcmp(temp, val) != 0) {
  530. printf("Possible failure in string write of %s "
  531. "Should be %s "
  532. "written to %s\%s\n",
  533. temp,
  534. val,
  535. basedir,
  536. filename);
  537. ret = -1;
  538. }
  539. }
  540. error_free:
  541. free(temp);
  542. return ret;
  543. }
  544. /**
  545. * write_sysfs_string_and_verify() - string write, readback and verify
  546. * @filename: name of file to write to
  547. * @basedir: the sysfs directory in which the file is to be found
  548. * @val: the string to write
  549. **/
  550. int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
  551. {
  552. return _write_sysfs_string(filename, basedir, val, 1);
  553. }
  554. int write_sysfs_string(char *filename, char *basedir, char *val)
  555. {
  556. return _write_sysfs_string(filename, basedir, val, 0);
  557. }
  558. int read_sysfs_posint(char *filename, char *basedir)
  559. {
  560. int ret;
  561. FILE *sysfsfp;
  562. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  563. if (temp == NULL) {
  564. printf("Memory allocation failed");
  565. return -ENOMEM;
  566. }
  567. sprintf(temp, "%s/%s", basedir, filename);
  568. sysfsfp = fopen(temp, "r");
  569. if (sysfsfp == NULL) {
  570. ret = -errno;
  571. goto error_free;
  572. }
  573. fscanf(sysfsfp, "%d\n", &ret);
  574. fclose(sysfsfp);
  575. error_free:
  576. free(temp);
  577. return ret;
  578. }
  579. int read_sysfs_float(char *filename, char *basedir, float *val)
  580. {
  581. int ret = 0;
  582. FILE *sysfsfp;
  583. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  584. if (temp == NULL) {
  585. printf("Memory allocation failed");
  586. return -ENOMEM;
  587. }
  588. sprintf(temp, "%s/%s", basedir, filename);
  589. sysfsfp = fopen(temp, "r");
  590. if (sysfsfp == NULL) {
  591. ret = -errno;
  592. goto error_free;
  593. }
  594. fscanf(sysfsfp, "%f\n", val);
  595. fclose(sysfsfp);
  596. error_free:
  597. free(temp);
  598. return ret;
  599. }
  600. int read_sysfs_string(const char *filename, const char *basedir, char *str)
  601. {
  602. int ret = 0;
  603. FILE *sysfsfp;
  604. char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
  605. if (temp == NULL) {
  606. printf("Memory allocation failed");
  607. return -ENOMEM;
  608. }
  609. sprintf(temp, "%s/%s", basedir, filename);
  610. sysfsfp = fopen(temp, "r");
  611. if (sysfsfp == NULL) {
  612. ret = -errno;
  613. goto error_free;
  614. }
  615. fscanf(sysfsfp, "%s\n", str);
  616. fclose(sysfsfp);
  617. error_free:
  618. free(temp);
  619. return ret;
  620. }
  621. #endif /* _IIO_UTILS_H */