1 // SPDX-License-Identifier: GPL-2.0-only
2 /* IIO - useful set of util functionality
4 * Copyright (c) 2008 Jonathan Cameron
13 #include "iio_utils.h"
15 const char *iio_dir = "/sys/bus/iio/devices/";
17 static char * const iio_direction[] = {
23 * iioutils_break_up_name() - extract generic name from full channel name
24 * @full_name: the full channel name
25 * @generic_name: the output generic channel name
27 * Returns 0 on success, or a negative error code if string extraction failed.
29 int iioutils_break_up_name(const char *full_name, char **generic_name)
33 char *working, *prefix = "";
36 for (i = 0; i < ARRAY_SIZE(iio_direction); i++)
37 if (!strncmp(full_name, iio_direction[i],
38 strlen(iio_direction[i]))) {
39 prefix = iio_direction[i];
43 current = strdup(full_name + strlen(prefix) + 1);
47 working = strtok(current, "_\0");
65 ret = asprintf(generic_name, "%s_%s", prefix, working);
68 return (ret == -1) ? -ENOMEM : 0;
72 * iioutils_get_type() - find and process _type attribute data
73 * @is_signed: output whether channel is signed
74 * @bytes: output how many bytes the channel storage occupies
75 * @bits_used: output number of valid bits of data
76 * @shift: output amount of bits to shift right data before applying bit mask
77 * @mask: output a bit mask for the raw data
78 * @be: output if data in big endian
79 * @device_dir: the IIO device directory
80 * @name: the channel name
81 * @generic_name: the channel type name
83 * Returns a value >= 0 on success, otherwise a negative error code.
85 int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
86 unsigned *shift, uint64_t *mask, unsigned *be,
87 const char *device_dir, const char *name,
88 const char *generic_name)
93 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
94 char signchar, endianchar;
96 const struct dirent *ent;
98 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
102 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
105 goto error_free_scan_el_dir;
107 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
110 goto error_free_builtname;
113 dp = opendir(scan_el_dir);
116 goto error_free_builtname_generic;
120 while (ent = readdir(dp), ent)
121 if ((strcmp(builtname, ent->d_name) == 0) ||
122 (strcmp(builtname_generic, ent->d_name) == 0)) {
123 ret = asprintf(&filename,
124 "%s/%s", scan_el_dir, ent->d_name);
130 sysfsfp = fopen(filename, "r");
133 fprintf(stderr, "failed to open %s\n",
135 goto error_free_filename;
138 ret = fscanf(sysfsfp,
147 "failed to pass scan type description\n");
148 goto error_close_sysfsfp;
149 } else if (ret != 5) {
152 "scan type description didn't match\n");
153 goto error_close_sysfsfp;
156 *be = (endianchar == 'b');
158 if (*bits_used == 64)
161 *mask = (1ULL << *bits_used) - 1ULL;
163 *is_signed = (signchar == 's');
164 if (fclose(sysfsfp)) {
166 fprintf(stderr, "Failed to close %s\n",
168 goto error_free_filename;
176 * Avoid having a more generic entry overwriting
179 if (strcmp(builtname, ent->d_name) == 0)
186 perror("iioutils_get_type(): Failed to close file");
193 if (closedir(dp) == -1)
194 perror("iioutils_get_type(): Failed to close directory");
196 error_free_builtname_generic:
197 free(builtname_generic);
198 error_free_builtname:
200 error_free_scan_el_dir:
207 * iioutils_get_param_float() - read a float value from a channel parameter
208 * @output: output the float value
209 * @param_name: the parameter name to read
210 * @device_dir: the IIO device directory in sysfs
211 * @name: the channel name
212 * @generic_name: the channel type name
214 * Returns a value >= 0 on success, otherwise a negative error code.
216 int iioutils_get_param_float(float *output, const char *param_name,
217 const char *device_dir, const char *name,
218 const char *generic_name)
223 char *builtname, *builtname_generic;
224 char *filename = NULL;
225 const struct dirent *ent;
227 ret = asprintf(&builtname, "%s_%s", name, param_name);
231 ret = asprintf(&builtname_generic,
232 "%s_%s", generic_name, param_name);
235 goto error_free_builtname;
238 dp = opendir(device_dir);
241 goto error_free_builtname_generic;
245 while (ent = readdir(dp), ent)
246 if ((strcmp(builtname, ent->d_name) == 0) ||
247 (strcmp(builtname_generic, ent->d_name) == 0)) {
248 ret = asprintf(&filename,
249 "%s/%s", device_dir, ent->d_name);
255 sysfsfp = fopen(filename, "r");
258 goto error_free_filename;
262 if (fscanf(sysfsfp, "%f", output) != 1)
263 ret = errno ? -errno : -ENODATA;
273 if (closedir(dp) == -1)
274 perror("iioutils_get_param_float(): Failed to close directory");
276 error_free_builtname_generic:
277 free(builtname_generic);
278 error_free_builtname:
285 * bsort_channel_array_by_index() - sort the array in index order
286 * @ci_array: the iio_channel_info array to be sorted
287 * @cnt: the amount of array elements
290 void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt)
292 struct iio_channel_info temp;
295 for (x = 0; x < cnt; x++)
296 for (y = 0; y < (cnt - 1); y++)
297 if (ci_array[y].index > ci_array[y + 1].index) {
298 temp = ci_array[y + 1];
299 ci_array[y + 1] = ci_array[y];
305 * build_channel_array() - function to figure out what channels are present
306 * @device_dir: the IIO device directory in sysfs
307 * @ci_array: output the resulting array of iio_channel_info
308 * @counter: output the amount of array elements
310 * Returns 0 on success, otherwise a negative error code.
312 int build_channel_array(const char *device_dir,
313 struct iio_channel_info **ci_array, int *counter)
318 struct iio_channel_info *current;
320 const struct dirent *ent;
325 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
329 dp = opendir(scan_el_dir);
332 goto error_free_name;
335 while (ent = readdir(dp), ent)
336 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
338 ret = asprintf(&filename,
339 "%s/%s", scan_el_dir, ent->d_name);
342 goto error_close_dir;
345 sysfsfp = fopen(filename, "r");
349 goto error_close_dir;
353 if (fscanf(sysfsfp, "%i", &ret) != 1) {
354 ret = errno ? -errno : -ENODATA;
356 perror("build_channel_array(): Failed to close file");
358 goto error_close_dir;
363 if (fclose(sysfsfp)) {
365 goto error_close_dir;
370 *ci_array = malloc(sizeof(**ci_array) * (*counter));
373 goto error_close_dir;
377 while (ent = readdir(dp), ent) {
378 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
380 int current_enabled = 0;
382 current = &(*ci_array)[count++];
383 ret = asprintf(&filename,
384 "%s/%s", scan_el_dir, ent->d_name);
387 /* decrement count to avoid freeing name */
389 goto error_cleanup_array;
392 sysfsfp = fopen(filename, "r");
397 goto error_cleanup_array;
401 if (fscanf(sysfsfp, "%i", ¤t_enabled) != 1) {
402 ret = errno ? -errno : -ENODATA;
404 goto error_cleanup_array;
407 if (fclose(sysfsfp)) {
410 goto error_cleanup_array;
413 if (!current_enabled) {
418 current->scale = 1.0;
420 current->name = strndup(ent->d_name,
421 strlen(ent->d_name) -
423 if (!current->name) {
426 goto error_cleanup_array;
429 /* Get the generic and specific name elements */
430 ret = iioutils_break_up_name(current->name,
431 ¤t->generic_name);
435 goto error_cleanup_array;
438 ret = asprintf(&filename,
444 goto error_cleanup_array;
447 sysfsfp = fopen(filename, "r");
451 fprintf(stderr, "failed to open %s/%s_index\n",
452 scan_el_dir, current->name);
453 goto error_cleanup_array;
457 if (fscanf(sysfsfp, "%u", ¤t->index) != 1) {
458 ret = errno ? -errno : -ENODATA;
460 perror("build_channel_array(): Failed to close file");
462 goto error_cleanup_array;
465 if (fclose(sysfsfp)) {
467 goto error_cleanup_array;
471 ret = iioutils_get_param_float(¤t->scale,
475 current->generic_name);
476 if ((ret < 0) && (ret != -ENOENT))
477 goto error_cleanup_array;
479 ret = iioutils_get_param_float(¤t->offset,
483 current->generic_name);
484 if ((ret < 0) && (ret != -ENOENT))
485 goto error_cleanup_array;
487 ret = iioutils_get_type(¤t->is_signed,
495 current->generic_name);
497 goto error_cleanup_array;
501 if (closedir(dp) == -1) {
503 goto error_cleanup_array;
507 /* reorder so that the array is in index order */
508 bsort_channel_array_by_index(*ci_array, *counter);
513 for (i = count - 1; i >= 0; i--) {
514 free((*ci_array)[i].name);
515 free((*ci_array)[i].generic_name);
522 if (closedir(dp) == -1)
523 perror("build_channel_array(): Failed to close dir");
531 static int calc_digits(int num)
535 /* It takes a digit to represent zero */
548 * find_type_by_name() - function to match top level types by name
549 * @name: top level type instance name
550 * @type: the type of top level instance being searched
552 * Returns the device number of a matched IIO device on success, otherwise a
553 * negative error code.
554 * Typical types this is used for are device and trigger.
556 int find_type_by_name(const char *name, const char *type)
558 const struct dirent *ent;
559 int number, numstrlen, ret;
563 char thisname[IIO_MAX_NAME_LENGTH];
566 dp = opendir(iio_dir);
568 fprintf(stderr, "No industrialio devices available\n");
572 while (ent = readdir(dp), ent) {
573 if (strcmp(ent->d_name, ".") != 0 &&
574 strcmp(ent->d_name, "..") != 0 &&
575 strlen(ent->d_name) > strlen(type) &&
576 strncmp(ent->d_name, type, strlen(type)) == 0) {
578 ret = sscanf(ent->d_name + strlen(type), "%d", &number);
582 "failed to read element number\n");
583 goto error_close_dir;
584 } else if (ret != 1) {
587 "failed to match element number\n");
588 goto error_close_dir;
591 numstrlen = calc_digits(number);
592 /* verify the next character is not a colon */
593 if (strncmp(ent->d_name + strlen(type) + numstrlen,
595 filename = malloc(strlen(iio_dir) + strlen(type)
599 goto error_close_dir;
602 ret = sprintf(filename, "%s%s%d/name", iio_dir,
606 goto error_close_dir;
609 namefp = fopen(filename, "r");
617 if (fscanf(namefp, "%s", thisname) != 1) {
618 ret = errno ? -errno : -ENODATA;
619 goto error_close_dir;
622 if (fclose(namefp)) {
624 goto error_close_dir;
627 if (strcmp(name, thisname) == 0) {
628 if (closedir(dp) == -1)
636 if (closedir(dp) == -1)
642 if (closedir(dp) == -1)
643 perror("find_type_by_name(): Failed to close directory");
648 static int _write_sysfs_int(const char *filename, const char *basedir, int val,
654 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
659 ret = sprintf(temp, "%s/%s", basedir, filename);
663 sysfsfp = fopen(temp, "w");
666 fprintf(stderr, "failed to open %s\n", temp);
670 ret = fprintf(sysfsfp, "%d", val);
673 perror("_write_sysfs_int(): Failed to close dir");
678 if (fclose(sysfsfp)) {
684 sysfsfp = fopen(temp, "r");
687 fprintf(stderr, "failed to open %s\n", temp);
691 if (fscanf(sysfsfp, "%d", &test) != 1) {
692 ret = errno ? -errno : -ENODATA;
694 perror("_write_sysfs_int(): Failed to close dir");
699 if (fclose(sysfsfp)) {
706 "Possible failure in int write %d to %s/%s\n",
707 val, basedir, filename);
718 * write_sysfs_int() - write an integer value to a sysfs file
719 * @filename: name of the file to write to
720 * @basedir: the sysfs directory in which the file is to be found
721 * @val: integer value to write to file
723 * Returns a value >= 0 on success, otherwise a negative error code.
725 int write_sysfs_int(const char *filename, const char *basedir, int val)
727 return _write_sysfs_int(filename, basedir, val, 0);
731 * write_sysfs_int_and_verify() - write an integer value to a sysfs file
733 * @filename: name of the file to write to
734 * @basedir: the sysfs directory in which the file is to be found
735 * @val: integer value to write to file
737 * Returns a value >= 0 on success, otherwise a negative error code.
739 int write_sysfs_int_and_verify(const char *filename, const char *basedir,
742 return _write_sysfs_int(filename, basedir, val, 1);
745 static int _write_sysfs_string(const char *filename, const char *basedir,
746 const char *val, int verify)
750 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
753 fprintf(stderr, "Memory allocation failed\n");
757 ret = sprintf(temp, "%s/%s", basedir, filename);
761 sysfsfp = fopen(temp, "w");
764 fprintf(stderr, "Could not open %s\n", temp);
768 ret = fprintf(sysfsfp, "%s", val);
771 perror("_write_sysfs_string(): Failed to close dir");
776 if (fclose(sysfsfp)) {
782 sysfsfp = fopen(temp, "r");
785 fprintf(stderr, "Could not open file to verify\n");
789 if (fscanf(sysfsfp, "%s", temp) != 1) {
790 ret = errno ? -errno : -ENODATA;
792 perror("_write_sysfs_string(): Failed to close dir");
797 if (fclose(sysfsfp)) {
802 if (strcmp(temp, val) != 0) {
804 "Possible failure in string write of %s "
805 "Should be %s written to %s/%s\n", temp, val,
818 * write_sysfs_string_and_verify() - string write, readback and verify
819 * @filename: name of file to write to
820 * @basedir: the sysfs directory in which the file is to be found
821 * @val: the string to write
823 * Returns a value >= 0 on success, otherwise a negative error code.
825 int write_sysfs_string_and_verify(const char *filename, const char *basedir,
828 return _write_sysfs_string(filename, basedir, val, 1);
832 * write_sysfs_string() - write string to a sysfs file
833 * @filename: name of file to write to
834 * @basedir: the sysfs directory in which the file is to be found
835 * @val: the string to write
837 * Returns a value >= 0 on success, otherwise a negative error code.
839 int write_sysfs_string(const char *filename, const char *basedir,
842 return _write_sysfs_string(filename, basedir, val, 0);
846 * read_sysfs_posint() - read an integer value from file
847 * @filename: name of file to read from
848 * @basedir: the sysfs directory in which the file is to be found
850 * Returns the read integer value >= 0 on success, otherwise a negative error
853 int read_sysfs_posint(const char *filename, const char *basedir)
857 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
860 fprintf(stderr, "Memory allocation failed");
864 ret = sprintf(temp, "%s/%s", basedir, filename);
868 sysfsfp = fopen(temp, "r");
875 if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
876 ret = errno ? -errno : -ENODATA;
878 perror("read_sysfs_posint(): Failed to close dir");
893 * read_sysfs_float() - read a float value from file
894 * @filename: name of file to read from
895 * @basedir: the sysfs directory in which the file is to be found
896 * @val: output the read float value
898 * Returns a value >= 0 on success, otherwise a negative error code.
900 int read_sysfs_float(const char *filename, const char *basedir, float *val)
904 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
907 fprintf(stderr, "Memory allocation failed");
911 ret = sprintf(temp, "%s/%s", basedir, filename);
915 sysfsfp = fopen(temp, "r");
922 if (fscanf(sysfsfp, "%f\n", val) != 1) {
923 ret = errno ? -errno : -ENODATA;
925 perror("read_sysfs_float(): Failed to close dir");
940 * read_sysfs_string() - read a string from file
941 * @filename: name of file to read from
942 * @basedir: the sysfs directory in which the file is to be found
943 * @str: output the read string
945 * Returns a value >= 0 on success, otherwise a negative error code.
947 int read_sysfs_string(const char *filename, const char *basedir, char *str)
951 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
954 fprintf(stderr, "Memory allocation failed");
958 ret = sprintf(temp, "%s/%s", basedir, filename);
962 sysfsfp = fopen(temp, "r");
969 if (fscanf(sysfsfp, "%s\n", str) != 1) {
970 ret = errno ? -errno : -ENODATA;
972 perror("read_sysfs_string(): Failed to close dir");