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 * @buffer_idx: the IIO buffer index
81 * @name: the channel name
82 * @generic_name: the channel type name
84 * Returns a value >= 0 on success, otherwise a negative error code.
86 static int iioutils_get_type(unsigned int *is_signed, unsigned int *bytes,
87 unsigned int *bits_used, unsigned int *shift,
88 uint64_t *mask, unsigned int *be,
89 const char *device_dir, int buffer_idx,
90 const char *name, const char *generic_name)
95 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
96 char signchar, endianchar;
98 const struct dirent *ent;
100 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir, buffer_idx);
104 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
107 goto error_free_scan_el_dir;
109 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
112 goto error_free_builtname;
115 dp = opendir(scan_el_dir);
118 goto error_free_builtname_generic;
122 while (ent = readdir(dp), ent)
123 if ((strcmp(builtname, ent->d_name) == 0) ||
124 (strcmp(builtname_generic, ent->d_name) == 0)) {
125 ret = asprintf(&filename,
126 "%s/%s", scan_el_dir, ent->d_name);
132 sysfsfp = fopen(filename, "r");
135 fprintf(stderr, "failed to open %s\n",
137 goto error_free_filename;
140 ret = fscanf(sysfsfp,
149 "failed to pass scan type description\n");
150 goto error_close_sysfsfp;
151 } else if (ret != 5) {
154 "scan type description didn't match\n");
155 goto error_close_sysfsfp;
158 *be = (endianchar == 'b');
160 if (*bits_used == 64)
163 *mask = (1ULL << *bits_used) - 1ULL;
165 *is_signed = (signchar == 's');
166 if (fclose(sysfsfp)) {
168 fprintf(stderr, "Failed to close %s\n",
170 goto error_free_filename;
178 * Avoid having a more generic entry overwriting
181 if (strcmp(builtname, ent->d_name) == 0)
188 perror("iioutils_get_type(): Failed to close file");
195 if (closedir(dp) == -1)
196 perror("iioutils_get_type(): Failed to close directory");
198 error_free_builtname_generic:
199 free(builtname_generic);
200 error_free_builtname:
202 error_free_scan_el_dir:
209 * iioutils_get_param_float() - read a float value from a channel parameter
210 * @output: output the float value
211 * @param_name: the parameter name to read
212 * @device_dir: the IIO device directory in sysfs
213 * @name: the channel name
214 * @generic_name: the channel type name
216 * Returns a value >= 0 on success, otherwise a negative error code.
218 int iioutils_get_param_float(float *output, const char *param_name,
219 const char *device_dir, const char *name,
220 const char *generic_name)
225 char *builtname, *builtname_generic;
226 char *filename = NULL;
227 const struct dirent *ent;
229 ret = asprintf(&builtname, "%s_%s", name, param_name);
233 ret = asprintf(&builtname_generic,
234 "%s_%s", generic_name, param_name);
237 goto error_free_builtname;
240 dp = opendir(device_dir);
243 goto error_free_builtname_generic;
247 while (ent = readdir(dp), ent)
248 if ((strcmp(builtname, ent->d_name) == 0) ||
249 (strcmp(builtname_generic, ent->d_name) == 0)) {
250 ret = asprintf(&filename,
251 "%s/%s", device_dir, ent->d_name);
257 sysfsfp = fopen(filename, "r");
260 goto error_free_filename;
264 if (fscanf(sysfsfp, "%f", output) != 1)
265 ret = errno ? -errno : -ENODATA;
274 if (closedir(dp) == -1)
275 perror("iioutils_get_param_float(): Failed to close directory");
277 error_free_builtname_generic:
278 free(builtname_generic);
279 error_free_builtname:
286 * bsort_channel_array_by_index() - sort the array in index order
287 * @ci_array: the iio_channel_info array to be sorted
288 * @cnt: the amount of array elements
291 void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt)
293 struct iio_channel_info temp;
296 for (x = 0; x < cnt; x++)
297 for (y = 0; y < (cnt - 1); y++)
298 if (ci_array[y].index > ci_array[y + 1].index) {
299 temp = ci_array[y + 1];
300 ci_array[y + 1] = ci_array[y];
306 * build_channel_array() - function to figure out what channels are present
307 * @device_dir: the IIO device directory in sysfs
308 * @buffer_idx: the IIO buffer for this channel array
309 * @ci_array: output the resulting array of iio_channel_info
310 * @counter: output the amount of array elements
312 * Returns 0 on success, otherwise a negative error code.
314 int build_channel_array(const char *device_dir, int buffer_idx,
315 struct iio_channel_info **ci_array, int *counter)
320 struct iio_channel_info *current;
322 const struct dirent *ent;
327 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir, buffer_idx);
331 dp = opendir(scan_el_dir);
334 goto error_free_name;
337 while (ent = readdir(dp), ent)
338 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
340 ret = asprintf(&filename,
341 "%s/%s", scan_el_dir, ent->d_name);
344 goto error_close_dir;
347 sysfsfp = fopen(filename, "r");
351 goto error_close_dir;
355 if (fscanf(sysfsfp, "%i", &ret) != 1) {
356 ret = errno ? -errno : -ENODATA;
358 perror("build_channel_array(): Failed to close file");
361 goto error_close_dir;
366 if (fclose(sysfsfp)) {
369 goto error_close_dir;
375 *ci_array = malloc(sizeof(**ci_array) * (*counter));
378 goto error_close_dir;
382 while (ent = readdir(dp), ent) {
383 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
385 int current_enabled = 0;
387 current = &(*ci_array)[count++];
388 ret = asprintf(&filename,
389 "%s/%s", scan_el_dir, ent->d_name);
392 /* decrement count to avoid freeing name */
394 goto error_cleanup_array;
397 sysfsfp = fopen(filename, "r");
402 goto error_cleanup_array;
406 if (fscanf(sysfsfp, "%i", ¤t_enabled) != 1) {
407 ret = errno ? -errno : -ENODATA;
410 goto error_cleanup_array;
413 if (fclose(sysfsfp)) {
417 goto error_cleanup_array;
420 if (!current_enabled) {
426 current->scale = 1.0;
428 current->name = strndup(ent->d_name,
429 strlen(ent->d_name) -
431 if (!current->name) {
435 goto error_cleanup_array;
438 /* Get the generic and specific name elements */
439 ret = iioutils_break_up_name(current->name,
440 ¤t->generic_name);
445 goto error_cleanup_array;
448 ret = asprintf(&filename,
455 goto error_cleanup_array;
458 sysfsfp = fopen(filename, "r");
461 fprintf(stderr, "failed to open %s\n",
464 goto error_cleanup_array;
468 if (fscanf(sysfsfp, "%u", ¤t->index) != 1) {
469 ret = errno ? -errno : -ENODATA;
471 perror("build_channel_array(): Failed to close file");
474 goto error_cleanup_array;
477 if (fclose(sysfsfp)) {
480 goto error_cleanup_array;
485 ret = iioutils_get_param_float(¤t->scale,
489 current->generic_name);
490 if ((ret < 0) && (ret != -ENOENT))
491 goto error_cleanup_array;
493 ret = iioutils_get_param_float(¤t->offset,
497 current->generic_name);
498 if ((ret < 0) && (ret != -ENOENT))
499 goto error_cleanup_array;
501 ret = iioutils_get_type(¤t->is_signed,
510 current->generic_name);
512 goto error_cleanup_array;
516 if (closedir(dp) == -1) {
518 goto error_cleanup_array;
522 /* reorder so that the array is in index order */
523 bsort_channel_array_by_index(*ci_array, *counter);
528 for (i = count - 1; i >= 0; i--) {
529 free((*ci_array)[i].name);
530 free((*ci_array)[i].generic_name);
537 if (closedir(dp) == -1)
538 perror("build_channel_array(): Failed to close dir");
546 static int calc_digits(int num)
550 /* It takes a digit to represent zero */
563 * find_type_by_name() - function to match top level types by name
564 * @name: top level type instance name
565 * @type: the type of top level instance being searched
567 * Returns the device number of a matched IIO device on success, otherwise a
568 * negative error code.
569 * Typical types this is used for are device and trigger.
571 int find_type_by_name(const char *name, const char *type)
573 const struct dirent *ent;
574 int number, numstrlen, ret;
578 char thisname[IIO_MAX_NAME_LENGTH];
581 dp = opendir(iio_dir);
583 fprintf(stderr, "No industrialio devices available\n");
587 while (ent = readdir(dp), ent) {
588 if (strcmp(ent->d_name, ".") != 0 &&
589 strcmp(ent->d_name, "..") != 0 &&
590 strlen(ent->d_name) > strlen(type) &&
591 strncmp(ent->d_name, type, strlen(type)) == 0) {
593 ret = sscanf(ent->d_name + strlen(type), "%d", &number);
597 "failed to read element number\n");
598 goto error_close_dir;
599 } else if (ret != 1) {
602 "failed to match element number\n");
603 goto error_close_dir;
606 numstrlen = calc_digits(number);
607 /* verify the next character is not a colon */
608 if (strncmp(ent->d_name + strlen(type) + numstrlen,
610 filename = malloc(strlen(iio_dir) + strlen(type)
614 goto error_close_dir;
617 ret = sprintf(filename, "%s%s%d/name", iio_dir,
621 goto error_close_dir;
624 namefp = fopen(filename, "r");
632 if (fscanf(namefp, "%s", thisname) != 1) {
633 ret = errno ? -errno : -ENODATA;
634 goto error_close_dir;
637 if (fclose(namefp)) {
639 goto error_close_dir;
642 if (strcmp(name, thisname) == 0) {
643 if (closedir(dp) == -1)
651 if (closedir(dp) == -1)
657 if (closedir(dp) == -1)
658 perror("find_type_by_name(): Failed to close directory");
663 static int _write_sysfs_int(const char *filename, const char *basedir, int val,
669 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
674 ret = sprintf(temp, "%s/%s", basedir, filename);
678 sysfsfp = fopen(temp, "w");
681 fprintf(stderr, "failed to open %s\n", temp);
685 ret = fprintf(sysfsfp, "%d", val);
688 perror("_write_sysfs_int(): Failed to close dir");
693 if (fclose(sysfsfp)) {
699 sysfsfp = fopen(temp, "r");
702 fprintf(stderr, "failed to open %s\n", temp);
706 if (fscanf(sysfsfp, "%d", &test) != 1) {
707 ret = errno ? -errno : -ENODATA;
709 perror("_write_sysfs_int(): Failed to close dir");
714 if (fclose(sysfsfp)) {
721 "Possible failure in int write %d to %s/%s\n",
722 val, basedir, filename);
733 * write_sysfs_int() - write an integer value to a sysfs file
734 * @filename: name of the file to write to
735 * @basedir: the sysfs directory in which the file is to be found
736 * @val: integer value to write to file
738 * Returns a value >= 0 on success, otherwise a negative error code.
740 int write_sysfs_int(const char *filename, const char *basedir, int val)
742 return _write_sysfs_int(filename, basedir, val, 0);
746 * write_sysfs_int_and_verify() - write an integer value to a sysfs file
748 * @filename: name of the file to write to
749 * @basedir: the sysfs directory in which the file is to be found
750 * @val: integer value to write to file
752 * Returns a value >= 0 on success, otherwise a negative error code.
754 int write_sysfs_int_and_verify(const char *filename, const char *basedir,
757 return _write_sysfs_int(filename, basedir, val, 1);
760 static int _write_sysfs_string(const char *filename, const char *basedir,
761 const char *val, int verify)
765 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
768 fprintf(stderr, "Memory allocation failed\n");
772 ret = sprintf(temp, "%s/%s", basedir, filename);
776 sysfsfp = fopen(temp, "w");
779 fprintf(stderr, "Could not open %s\n", temp);
783 ret = fprintf(sysfsfp, "%s", val);
786 perror("_write_sysfs_string(): Failed to close dir");
791 if (fclose(sysfsfp)) {
797 sysfsfp = fopen(temp, "r");
800 fprintf(stderr, "Could not open file to verify\n");
804 if (fscanf(sysfsfp, "%s", temp) != 1) {
805 ret = errno ? -errno : -ENODATA;
807 perror("_write_sysfs_string(): Failed to close dir");
812 if (fclose(sysfsfp)) {
817 if (strcmp(temp, val) != 0) {
819 "Possible failure in string write of %s "
820 "Should be %s written to %s/%s\n", temp, val,
833 * write_sysfs_string_and_verify() - string write, readback and verify
834 * @filename: name of file to write to
835 * @basedir: the sysfs directory in which the file is to be found
836 * @val: the string to write
838 * Returns a value >= 0 on success, otherwise a negative error code.
840 int write_sysfs_string_and_verify(const char *filename, const char *basedir,
843 return _write_sysfs_string(filename, basedir, val, 1);
847 * write_sysfs_string() - write string to a sysfs file
848 * @filename: name of file to write to
849 * @basedir: the sysfs directory in which the file is to be found
850 * @val: the string to write
852 * Returns a value >= 0 on success, otherwise a negative error code.
854 int write_sysfs_string(const char *filename, const char *basedir,
857 return _write_sysfs_string(filename, basedir, val, 0);
861 * read_sysfs_posint() - read an integer value from file
862 * @filename: name of file to read from
863 * @basedir: the sysfs directory in which the file is to be found
865 * Returns the read integer value >= 0 on success, otherwise a negative error
868 int read_sysfs_posint(const char *filename, const char *basedir)
872 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
875 fprintf(stderr, "Memory allocation failed");
879 ret = sprintf(temp, "%s/%s", basedir, filename);
883 sysfsfp = fopen(temp, "r");
890 if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
891 ret = errno ? -errno : -ENODATA;
893 perror("read_sysfs_posint(): Failed to close dir");
908 * read_sysfs_float() - read a float value from file
909 * @filename: name of file to read from
910 * @basedir: the sysfs directory in which the file is to be found
911 * @val: output the read float value
913 * Returns a value >= 0 on success, otherwise a negative error code.
915 int read_sysfs_float(const char *filename, const char *basedir, float *val)
919 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
922 fprintf(stderr, "Memory allocation failed");
926 ret = sprintf(temp, "%s/%s", basedir, filename);
930 sysfsfp = fopen(temp, "r");
937 if (fscanf(sysfsfp, "%f\n", val) != 1) {
938 ret = errno ? -errno : -ENODATA;
940 perror("read_sysfs_float(): Failed to close dir");
955 * read_sysfs_string() - read a string from file
956 * @filename: name of file to read from
957 * @basedir: the sysfs directory in which the file is to be found
958 * @str: output the read string
960 * Returns a value >= 0 on success, otherwise a negative error code.
962 int read_sysfs_string(const char *filename, const char *basedir, char *str)
966 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
969 fprintf(stderr, "Memory allocation failed");
973 ret = sprintf(temp, "%s/%s", basedir, filename);
977 sysfsfp = fopen(temp, "r");
984 if (fscanf(sysfsfp, "%s\n", str) != 1) {
985 ret = errno ? -errno : -ENODATA;
987 perror("read_sysfs_string(): Failed to close dir");