1 // SPDX-License-Identifier: GPL-2.0-only
2 /* IIO - useful set of util functionality
3 *
4 * Copyright (c) 2008 Jonathan Cameron
5 */
6 #include <string.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <dirent.h>
11 #include <errno.h>
12 #include <ctype.h>
13 #include "iio_utils.h"
14
15 const char *iio_dir = "/sys/bus/iio/devices/";
16
17 static char * const iio_direction[] = {
18 "in",
19 "out",
20 };
21
22 /**
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
26 *
27 * Returns 0 on success, or a negative error code if string extraction failed.
28 **/
iioutils_break_up_name(const char * full_name,char ** generic_name)29 int iioutils_break_up_name(const char *full_name, char **generic_name)
30 {
31 char *current;
32 char *w, *r;
33 char *working, *prefix = "";
34 int i, ret;
35
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];
40 break;
41 }
42
43 current = strdup(full_name + strlen(prefix) + 1);
44 if (!current)
45 return -ENOMEM;
46
47 working = strtok(current, "_\0");
48 if (!working) {
49 free(current);
50 return -EINVAL;
51 }
52
53 w = working;
54 r = working;
55
56 while (*r != '\0') {
57 if (!isdigit(*r)) {
58 *w = *r;
59 w++;
60 }
61
62 r++;
63 }
64 *w = '\0';
65 ret = asprintf(generic_name, "%s_%s", prefix, working);
66 free(current);
67
68 return (ret == -1) ? -ENOMEM : 0;
69 }
70
71 /**
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
82 *
83 * Returns a value >= 0 on success, otherwise a negative error code.
84 **/
iioutils_get_type(unsigned * is_signed,unsigned * bytes,unsigned * bits_used,unsigned * shift,uint64_t * mask,unsigned * be,const char * device_dir,const char * name,const char * generic_name)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)
89 {
90 FILE *sysfsfp;
91 int ret;
92 DIR *dp;
93 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
94 char signchar, endianchar;
95 unsigned padint;
96 const struct dirent *ent;
97
98 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
99 if (ret < 0)
100 return -ENOMEM;
101
102 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
103 if (ret < 0) {
104 ret = -ENOMEM;
105 goto error_free_scan_el_dir;
106 }
107 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
108 if (ret < 0) {
109 ret = -ENOMEM;
110 goto error_free_builtname;
111 }
112
113 dp = opendir(scan_el_dir);
114 if (!dp) {
115 ret = -errno;
116 goto error_free_builtname_generic;
117 }
118
119 ret = -ENOENT;
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);
125 if (ret < 0) {
126 ret = -ENOMEM;
127 goto error_closedir;
128 }
129
130 sysfsfp = fopen(filename, "r");
131 if (!sysfsfp) {
132 ret = -errno;
133 fprintf(stderr, "failed to open %s\n",
134 filename);
135 goto error_free_filename;
136 }
137
138 ret = fscanf(sysfsfp,
139 "%ce:%c%u/%u>>%u",
140 &endianchar,
141 &signchar,
142 bits_used,
143 &padint, shift);
144 if (ret < 0) {
145 ret = -errno;
146 fprintf(stderr,
147 "failed to pass scan type description\n");
148 goto error_close_sysfsfp;
149 } else if (ret != 5) {
150 ret = -EIO;
151 fprintf(stderr,
152 "scan type description didn't match\n");
153 goto error_close_sysfsfp;
154 }
155
156 *be = (endianchar == 'b');
157 *bytes = padint / 8;
158 if (*bits_used == 64)
159 *mask = ~(0ULL);
160 else
161 *mask = (1ULL << *bits_used) - 1ULL;
162
163 *is_signed = (signchar == 's');
164 if (fclose(sysfsfp)) {
165 ret = -errno;
166 fprintf(stderr, "Failed to close %s\n",
167 filename);
168 goto error_free_filename;
169 }
170
171 sysfsfp = 0;
172 free(filename);
173 filename = 0;
174
175 /*
176 * Avoid having a more generic entry overwriting
177 * the settings.
178 */
179 if (strcmp(builtname, ent->d_name) == 0)
180 break;
181 }
182
183 error_close_sysfsfp:
184 if (sysfsfp)
185 if (fclose(sysfsfp))
186 perror("iioutils_get_type(): Failed to close file");
187
188 error_free_filename:
189 if (filename)
190 free(filename);
191
192 error_closedir:
193 if (closedir(dp) == -1)
194 perror("iioutils_get_type(): Failed to close directory");
195
196 error_free_builtname_generic:
197 free(builtname_generic);
198 error_free_builtname:
199 free(builtname);
200 error_free_scan_el_dir:
201 free(scan_el_dir);
202
203 return ret;
204 }
205
206 /**
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
213 *
214 * Returns a value >= 0 on success, otherwise a negative error code.
215 **/
iioutils_get_param_float(float * output,const char * param_name,const char * device_dir,const char * name,const char * generic_name)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)
219 {
220 FILE *sysfsfp;
221 int ret;
222 DIR *dp;
223 char *builtname, *builtname_generic;
224 char *filename = NULL;
225 const struct dirent *ent;
226
227 ret = asprintf(&builtname, "%s_%s", name, param_name);
228 if (ret < 0)
229 return -ENOMEM;
230
231 ret = asprintf(&builtname_generic,
232 "%s_%s", generic_name, param_name);
233 if (ret < 0) {
234 ret = -ENOMEM;
235 goto error_free_builtname;
236 }
237
238 dp = opendir(device_dir);
239 if (!dp) {
240 ret = -errno;
241 goto error_free_builtname_generic;
242 }
243
244 ret = -ENOENT;
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);
250 if (ret < 0) {
251 ret = -ENOMEM;
252 goto error_closedir;
253 }
254
255 sysfsfp = fopen(filename, "r");
256 if (!sysfsfp) {
257 ret = -errno;
258 goto error_free_filename;
259 }
260
261 errno = 0;
262 if (fscanf(sysfsfp, "%f", output) != 1)
263 ret = errno ? -errno : -ENODATA;
264
265 break;
266 }
267 error_free_filename:
268 if (filename)
269 free(filename);
270
271 error_closedir:
272 if (closedir(dp) == -1)
273 perror("iioutils_get_param_float(): Failed to close directory");
274
275 error_free_builtname_generic:
276 free(builtname_generic);
277 error_free_builtname:
278 free(builtname);
279
280 return ret;
281 }
282
283 /**
284 * bsort_channel_array_by_index() - sort the array in index order
285 * @ci_array: the iio_channel_info array to be sorted
286 * @cnt: the amount of array elements
287 **/
288
bsort_channel_array_by_index(struct iio_channel_info * ci_array,int cnt)289 void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt)
290 {
291 struct iio_channel_info temp;
292 int x, y;
293
294 for (x = 0; x < cnt; x++)
295 for (y = 0; y < (cnt - 1); y++)
296 if (ci_array[y].index > ci_array[y + 1].index) {
297 temp = ci_array[y + 1];
298 ci_array[y + 1] = ci_array[y];
299 ci_array[y] = temp;
300 }
301 }
302
303 /**
304 * build_channel_array() - function to figure out what channels are present
305 * @device_dir: the IIO device directory in sysfs
306 * @ci_array: output the resulting array of iio_channel_info
307 * @counter: output the amount of array elements
308 *
309 * Returns 0 on success, otherwise a negative error code.
310 **/
build_channel_array(const char * device_dir,struct iio_channel_info ** ci_array,int * counter)311 int build_channel_array(const char *device_dir,
312 struct iio_channel_info **ci_array, int *counter)
313 {
314 DIR *dp;
315 FILE *sysfsfp;
316 int count = 0, i;
317 struct iio_channel_info *current;
318 int ret;
319 const struct dirent *ent;
320 char *scan_el_dir;
321 char *filename;
322
323 *counter = 0;
324 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
325 if (ret < 0)
326 return -ENOMEM;
327
328 dp = opendir(scan_el_dir);
329 if (!dp) {
330 ret = -errno;
331 goto error_free_name;
332 }
333
334 while (ent = readdir(dp), ent)
335 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
336 "_en") == 0) {
337 ret = asprintf(&filename,
338 "%s/%s", scan_el_dir, ent->d_name);
339 if (ret < 0) {
340 ret = -ENOMEM;
341 goto error_close_dir;
342 }
343
344 sysfsfp = fopen(filename, "r");
345 if (!sysfsfp) {
346 ret = -errno;
347 free(filename);
348 goto error_close_dir;
349 }
350
351 errno = 0;
352 if (fscanf(sysfsfp, "%i", &ret) != 1) {
353 ret = errno ? -errno : -ENODATA;
354 if (fclose(sysfsfp))
355 perror("build_channel_array(): Failed to close file");
356
357 free(filename);
358 goto error_close_dir;
359 }
360 if (ret == 1)
361 (*counter)++;
362
363 if (fclose(sysfsfp)) {
364 ret = -errno;
365 free(filename);
366 goto error_close_dir;
367 }
368
369 free(filename);
370 }
371
372 *ci_array = malloc(sizeof(**ci_array) * (*counter));
373 if (!*ci_array) {
374 ret = -ENOMEM;
375 goto error_close_dir;
376 }
377
378 seekdir(dp, 0);
379 while (ent = readdir(dp), ent) {
380 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
381 "_en") == 0) {
382 int current_enabled = 0;
383
384 current = &(*ci_array)[count++];
385 ret = asprintf(&filename,
386 "%s/%s", scan_el_dir, ent->d_name);
387 if (ret < 0) {
388 ret = -ENOMEM;
389 /* decrement count to avoid freeing name */
390 count--;
391 goto error_cleanup_array;
392 }
393
394 sysfsfp = fopen(filename, "r");
395 if (!sysfsfp) {
396 ret = -errno;
397 free(filename);
398 count--;
399 goto error_cleanup_array;
400 }
401
402 errno = 0;
403 if (fscanf(sysfsfp, "%i", ¤t_enabled) != 1) {
404 ret = errno ? -errno : -ENODATA;
405 free(filename);
406 count--;
407 goto error_cleanup_array;
408 }
409
410 if (fclose(sysfsfp)) {
411 ret = -errno;
412 free(filename);
413 count--;
414 goto error_cleanup_array;
415 }
416
417 if (!current_enabled) {
418 free(filename);
419 count--;
420 continue;
421 }
422
423 current->scale = 1.0;
424 current->offset = 0;
425 current->name = strndup(ent->d_name,
426 strlen(ent->d_name) -
427 strlen("_en"));
428 if (!current->name) {
429 free(filename);
430 ret = -ENOMEM;
431 count--;
432 goto error_cleanup_array;
433 }
434
435 /* Get the generic and specific name elements */
436 ret = iioutils_break_up_name(current->name,
437 ¤t->generic_name);
438 if (ret) {
439 free(filename);
440 free(current->name);
441 count--;
442 goto error_cleanup_array;
443 }
444
445 ret = asprintf(&filename,
446 "%s/%s_index",
447 scan_el_dir,
448 current->name);
449 if (ret < 0) {
450 free(filename);
451 ret = -ENOMEM;
452 goto error_cleanup_array;
453 }
454
455 sysfsfp = fopen(filename, "r");
456 if (!sysfsfp) {
457 ret = -errno;
458 fprintf(stderr, "failed to open %s\n",
459 filename);
460 free(filename);
461 goto error_cleanup_array;
462 }
463
464 errno = 0;
465 if (fscanf(sysfsfp, "%u", ¤t->index) != 1) {
466 ret = errno ? -errno : -ENODATA;
467 if (fclose(sysfsfp))
468 perror("build_channel_array(): Failed to close file");
469
470 free(filename);
471 goto error_cleanup_array;
472 }
473
474 if (fclose(sysfsfp)) {
475 ret = -errno;
476 free(filename);
477 goto error_cleanup_array;
478 }
479
480 free(filename);
481 /* Find the scale */
482 ret = iioutils_get_param_float(¤t->scale,
483 "scale",
484 device_dir,
485 current->name,
486 current->generic_name);
487 if ((ret < 0) && (ret != -ENOENT))
488 goto error_cleanup_array;
489
490 ret = iioutils_get_param_float(¤t->offset,
491 "offset",
492 device_dir,
493 current->name,
494 current->generic_name);
495 if ((ret < 0) && (ret != -ENOENT))
496 goto error_cleanup_array;
497
498 ret = iioutils_get_type(¤t->is_signed,
499 ¤t->bytes,
500 ¤t->bits_used,
501 ¤t->shift,
502 ¤t->mask,
503 ¤t->be,
504 device_dir,
505 current->name,
506 current->generic_name);
507 if (ret < 0)
508 goto error_cleanup_array;
509 }
510 }
511
512 if (closedir(dp) == -1) {
513 ret = -errno;
514 goto error_cleanup_array;
515 }
516
517 free(scan_el_dir);
518 /* reorder so that the array is in index order */
519 bsort_channel_array_by_index(*ci_array, *counter);
520
521 return 0;
522
523 error_cleanup_array:
524 for (i = count - 1; i >= 0; i--) {
525 free((*ci_array)[i].name);
526 free((*ci_array)[i].generic_name);
527 }
528 free(*ci_array);
529 *ci_array = NULL;
530 *counter = 0;
531 error_close_dir:
532 if (dp)
533 if (closedir(dp) == -1)
534 perror("build_channel_array(): Failed to close dir");
535
536 error_free_name:
537 free(scan_el_dir);
538
539 return ret;
540 }
541
calc_digits(int num)542 static int calc_digits(int num)
543 {
544 int count = 0;
545
546 /* It takes a digit to represent zero */
547 if (!num)
548 return 1;
549
550 while (num != 0) {
551 num /= 10;
552 count++;
553 }
554
555 return count;
556 }
557
558 /**
559 * find_type_by_name() - function to match top level types by name
560 * @name: top level type instance name
561 * @type: the type of top level instance being searched
562 *
563 * Returns the device number of a matched IIO device on success, otherwise a
564 * negative error code.
565 * Typical types this is used for are device and trigger.
566 **/
find_type_by_name(const char * name,const char * type)567 int find_type_by_name(const char *name, const char *type)
568 {
569 const struct dirent *ent;
570 int number, numstrlen, ret;
571
572 FILE *namefp;
573 DIR *dp;
574 char thisname[IIO_MAX_NAME_LENGTH];
575 char *filename;
576
577 dp = opendir(iio_dir);
578 if (!dp) {
579 fprintf(stderr, "No industrialio devices available\n");
580 return -ENODEV;
581 }
582
583 while (ent = readdir(dp), ent) {
584 if (strcmp(ent->d_name, ".") != 0 &&
585 strcmp(ent->d_name, "..") != 0 &&
586 strlen(ent->d_name) > strlen(type) &&
587 strncmp(ent->d_name, type, strlen(type)) == 0) {
588 errno = 0;
589 ret = sscanf(ent->d_name + strlen(type), "%d", &number);
590 if (ret < 0) {
591 ret = -errno;
592 fprintf(stderr,
593 "failed to read element number\n");
594 goto error_close_dir;
595 } else if (ret != 1) {
596 ret = -EIO;
597 fprintf(stderr,
598 "failed to match element number\n");
599 goto error_close_dir;
600 }
601
602 numstrlen = calc_digits(number);
603 /* verify the next character is not a colon */
604 if (strncmp(ent->d_name + strlen(type) + numstrlen,
605 ":", 1) != 0) {
606 filename = malloc(strlen(iio_dir) + strlen(type)
607 + numstrlen + 6);
608 if (!filename) {
609 ret = -ENOMEM;
610 goto error_close_dir;
611 }
612
613 ret = sprintf(filename, "%s%s%d/name", iio_dir,
614 type, number);
615 if (ret < 0) {
616 free(filename);
617 goto error_close_dir;
618 }
619
620 namefp = fopen(filename, "r");
621 if (!namefp) {
622 free(filename);
623 continue;
624 }
625
626 free(filename);
627 errno = 0;
628 if (fscanf(namefp, "%s", thisname) != 1) {
629 ret = errno ? -errno : -ENODATA;
630 goto error_close_dir;
631 }
632
633 if (fclose(namefp)) {
634 ret = -errno;
635 goto error_close_dir;
636 }
637
638 if (strcmp(name, thisname) == 0) {
639 if (closedir(dp) == -1)
640 return -errno;
641
642 return number;
643 }
644 }
645 }
646 }
647 if (closedir(dp) == -1)
648 return -errno;
649
650 return -ENODEV;
651
652 error_close_dir:
653 if (closedir(dp) == -1)
654 perror("find_type_by_name(): Failed to close directory");
655
656 return ret;
657 }
658
_write_sysfs_int(const char * filename,const char * basedir,int val,int verify)659 static int _write_sysfs_int(const char *filename, const char *basedir, int val,
660 int verify)
661 {
662 int ret = 0;
663 FILE *sysfsfp;
664 int test;
665 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
666
667 if (!temp)
668 return -ENOMEM;
669
670 ret = sprintf(temp, "%s/%s", basedir, filename);
671 if (ret < 0)
672 goto error_free;
673
674 sysfsfp = fopen(temp, "w");
675 if (!sysfsfp) {
676 ret = -errno;
677 fprintf(stderr, "failed to open %s\n", temp);
678 goto error_free;
679 }
680
681 ret = fprintf(sysfsfp, "%d", val);
682 if (ret < 0) {
683 if (fclose(sysfsfp))
684 perror("_write_sysfs_int(): Failed to close dir");
685
686 goto error_free;
687 }
688
689 if (fclose(sysfsfp)) {
690 ret = -errno;
691 goto error_free;
692 }
693
694 if (verify) {
695 sysfsfp = fopen(temp, "r");
696 if (!sysfsfp) {
697 ret = -errno;
698 fprintf(stderr, "failed to open %s\n", temp);
699 goto error_free;
700 }
701
702 if (fscanf(sysfsfp, "%d", &test) != 1) {
703 ret = errno ? -errno : -ENODATA;
704 if (fclose(sysfsfp))
705 perror("_write_sysfs_int(): Failed to close dir");
706
707 goto error_free;
708 }
709
710 if (fclose(sysfsfp)) {
711 ret = -errno;
712 goto error_free;
713 }
714
715 if (test != val) {
716 fprintf(stderr,
717 "Possible failure in int write %d to %s/%s\n",
718 val, basedir, filename);
719 ret = -1;
720 }
721 }
722
723 error_free:
724 free(temp);
725 return ret;
726 }
727
728 /**
729 * write_sysfs_int() - write an integer value to a sysfs file
730 * @filename: name of the file to write to
731 * @basedir: the sysfs directory in which the file is to be found
732 * @val: integer value to write to file
733 *
734 * Returns a value >= 0 on success, otherwise a negative error code.
735 **/
write_sysfs_int(const char * filename,const char * basedir,int val)736 int write_sysfs_int(const char *filename, const char *basedir, int val)
737 {
738 return _write_sysfs_int(filename, basedir, val, 0);
739 }
740
741 /**
742 * write_sysfs_int_and_verify() - write an integer value to a sysfs file
743 * and verify
744 * @filename: name of the file to write to
745 * @basedir: the sysfs directory in which the file is to be found
746 * @val: integer value to write to file
747 *
748 * Returns a value >= 0 on success, otherwise a negative error code.
749 **/
write_sysfs_int_and_verify(const char * filename,const char * basedir,int val)750 int write_sysfs_int_and_verify(const char *filename, const char *basedir,
751 int val)
752 {
753 return _write_sysfs_int(filename, basedir, val, 1);
754 }
755
_write_sysfs_string(const char * filename,const char * basedir,const char * val,int verify)756 static int _write_sysfs_string(const char *filename, const char *basedir,
757 const char *val, int verify)
758 {
759 int ret = 0;
760 FILE *sysfsfp;
761 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
762
763 if (!temp) {
764 fprintf(stderr, "Memory allocation failed\n");
765 return -ENOMEM;
766 }
767
768 ret = sprintf(temp, "%s/%s", basedir, filename);
769 if (ret < 0)
770 goto error_free;
771
772 sysfsfp = fopen(temp, "w");
773 if (!sysfsfp) {
774 ret = -errno;
775 fprintf(stderr, "Could not open %s\n", temp);
776 goto error_free;
777 }
778
779 ret = fprintf(sysfsfp, "%s", val);
780 if (ret < 0) {
781 if (fclose(sysfsfp))
782 perror("_write_sysfs_string(): Failed to close dir");
783
784 goto error_free;
785 }
786
787 if (fclose(sysfsfp)) {
788 ret = -errno;
789 goto error_free;
790 }
791
792 if (verify) {
793 sysfsfp = fopen(temp, "r");
794 if (!sysfsfp) {
795 ret = -errno;
796 fprintf(stderr, "Could not open file to verify\n");
797 goto error_free;
798 }
799
800 if (fscanf(sysfsfp, "%s", temp) != 1) {
801 ret = errno ? -errno : -ENODATA;
802 if (fclose(sysfsfp))
803 perror("_write_sysfs_string(): Failed to close dir");
804
805 goto error_free;
806 }
807
808 if (fclose(sysfsfp)) {
809 ret = -errno;
810 goto error_free;
811 }
812
813 if (strcmp(temp, val) != 0) {
814 fprintf(stderr,
815 "Possible failure in string write of %s "
816 "Should be %s written to %s/%s\n", temp, val,
817 basedir, filename);
818 ret = -1;
819 }
820 }
821
822 error_free:
823 free(temp);
824
825 return ret;
826 }
827
828 /**
829 * write_sysfs_string_and_verify() - string write, readback and verify
830 * @filename: name of file to write to
831 * @basedir: the sysfs directory in which the file is to be found
832 * @val: the string to write
833 *
834 * Returns a value >= 0 on success, otherwise a negative error code.
835 **/
write_sysfs_string_and_verify(const char * filename,const char * basedir,const char * val)836 int write_sysfs_string_and_verify(const char *filename, const char *basedir,
837 const char *val)
838 {
839 return _write_sysfs_string(filename, basedir, val, 1);
840 }
841
842 /**
843 * write_sysfs_string() - write string to a sysfs file
844 * @filename: name of file to write to
845 * @basedir: the sysfs directory in which the file is to be found
846 * @val: the string to write
847 *
848 * Returns a value >= 0 on success, otherwise a negative error code.
849 **/
write_sysfs_string(const char * filename,const char * basedir,const char * val)850 int write_sysfs_string(const char *filename, const char *basedir,
851 const char *val)
852 {
853 return _write_sysfs_string(filename, basedir, val, 0);
854 }
855
856 /**
857 * read_sysfs_posint() - read an integer value from file
858 * @filename: name of file to read from
859 * @basedir: the sysfs directory in which the file is to be found
860 *
861 * Returns the read integer value >= 0 on success, otherwise a negative error
862 * code.
863 **/
read_sysfs_posint(const char * filename,const char * basedir)864 int read_sysfs_posint(const char *filename, const char *basedir)
865 {
866 int ret;
867 FILE *sysfsfp;
868 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
869
870 if (!temp) {
871 fprintf(stderr, "Memory allocation failed");
872 return -ENOMEM;
873 }
874
875 ret = sprintf(temp, "%s/%s", basedir, filename);
876 if (ret < 0)
877 goto error_free;
878
879 sysfsfp = fopen(temp, "r");
880 if (!sysfsfp) {
881 ret = -errno;
882 goto error_free;
883 }
884
885 errno = 0;
886 if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
887 ret = errno ? -errno : -ENODATA;
888 if (fclose(sysfsfp))
889 perror("read_sysfs_posint(): Failed to close dir");
890
891 goto error_free;
892 }
893
894 if (fclose(sysfsfp))
895 ret = -errno;
896
897 error_free:
898 free(temp);
899
900 return ret;
901 }
902
903 /**
904 * read_sysfs_float() - read a float value from file
905 * @filename: name of file to read from
906 * @basedir: the sysfs directory in which the file is to be found
907 * @val: output the read float value
908 *
909 * Returns a value >= 0 on success, otherwise a negative error code.
910 **/
read_sysfs_float(const char * filename,const char * basedir,float * val)911 int read_sysfs_float(const char *filename, const char *basedir, float *val)
912 {
913 int ret = 0;
914 FILE *sysfsfp;
915 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
916
917 if (!temp) {
918 fprintf(stderr, "Memory allocation failed");
919 return -ENOMEM;
920 }
921
922 ret = sprintf(temp, "%s/%s", basedir, filename);
923 if (ret < 0)
924 goto error_free;
925
926 sysfsfp = fopen(temp, "r");
927 if (!sysfsfp) {
928 ret = -errno;
929 goto error_free;
930 }
931
932 errno = 0;
933 if (fscanf(sysfsfp, "%f\n", val) != 1) {
934 ret = errno ? -errno : -ENODATA;
935 if (fclose(sysfsfp))
936 perror("read_sysfs_float(): Failed to close dir");
937
938 goto error_free;
939 }
940
941 if (fclose(sysfsfp))
942 ret = -errno;
943
944 error_free:
945 free(temp);
946
947 return ret;
948 }
949
950 /**
951 * read_sysfs_string() - read a string from file
952 * @filename: name of file to read from
953 * @basedir: the sysfs directory in which the file is to be found
954 * @str: output the read string
955 *
956 * Returns a value >= 0 on success, otherwise a negative error code.
957 **/
read_sysfs_string(const char * filename,const char * basedir,char * str)958 int read_sysfs_string(const char *filename, const char *basedir, char *str)
959 {
960 int ret = 0;
961 FILE *sysfsfp;
962 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
963
964 if (!temp) {
965 fprintf(stderr, "Memory allocation failed");
966 return -ENOMEM;
967 }
968
969 ret = sprintf(temp, "%s/%s", basedir, filename);
970 if (ret < 0)
971 goto error_free;
972
973 sysfsfp = fopen(temp, "r");
974 if (!sysfsfp) {
975 ret = -errno;
976 goto error_free;
977 }
978
979 errno = 0;
980 if (fscanf(sysfsfp, "%s\n", str) != 1) {
981 ret = errno ? -errno : -ENODATA;
982 if (fclose(sysfsfp))
983 perror("read_sysfs_string(): Failed to close dir");
984
985 goto error_free;
986 }
987
988 if (fclose(sysfsfp))
989 ret = -errno;
990
991 error_free:
992 free(temp);
993
994 return ret;
995 }
996