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