Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 2 | /* IIO - useful set of util functionality |
| 3 | * |
| 4 | * Copyright (c) 2008 Jonathan Cameron |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 5 | */ |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 6 | #include <string.h> |
| 7 | #include <stdlib.h> |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | #include <stdint.h> |
Lars-Peter Clausen | bc9f35d | 2011-10-26 17:27:44 +0100 | [diff] [blame] | 10 | #include <dirent.h> |
Peter Meerwald | bb23378 | 2012-06-25 23:12:17 +0200 | [diff] [blame] | 11 | #include <errno.h> |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 12 | #include <ctype.h> |
| 13 | #include "iio_utils.h" |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 14 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 15 | const char *iio_dir = "/sys/bus/iio/devices/"; |
| 16 | |
Irina Tirdea | d9d7b99 | 2015-03-27 13:53:00 +0200 | [diff] [blame] | 17 | static char * const iio_direction[] = { |
| 18 | "in", |
| 19 | "out", |
| 20 | }; |
| 21 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 22 | /** |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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 |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 26 | * |
| 27 | * Returns 0 on success, or a negative error code if string extraction failed. |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 28 | **/ |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 29 | int iioutils_break_up_name(const char *full_name, char **generic_name) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 30 | { |
| 31 | char *current; |
| 32 | char *w, *r; |
Irina Tirdea | d9d7b99 | 2015-03-27 13:53:00 +0200 | [diff] [blame] | 33 | char *working, *prefix = ""; |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 34 | int i, ret; |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 35 | |
Cristina Opriceana | 34cbea1 | 2015-07-13 16:20:11 +0300 | [diff] [blame] | 36 | for (i = 0; i < ARRAY_SIZE(iio_direction); i++) |
Irina Tirdea | d9d7b99 | 2015-03-27 13:53:00 +0200 | [diff] [blame] | 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); |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 44 | if (!current) |
| 45 | return -ENOMEM; |
| 46 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 47 | working = strtok(current, "_\0"); |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 48 | if (!working) { |
| 49 | free(current); |
| 50 | return -EINVAL; |
| 51 | } |
Irina Tirdea | d9d7b99 | 2015-03-27 13:53:00 +0200 | [diff] [blame] | 52 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 53 | w = working; |
| 54 | r = working; |
| 55 | |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 56 | while (*r != '\0') { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 57 | if (!isdigit(*r)) { |
| 58 | *w = *r; |
| 59 | w++; |
| 60 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 61 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 62 | r++; |
| 63 | } |
| 64 | *w = '\0'; |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 65 | ret = asprintf(generic_name, "%s_%s", prefix, working); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 66 | free(current); |
| 67 | |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 68 | return (ret == -1) ? -ENOMEM : 0; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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 |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 75 | * @bits_used: output number of valid bits of data |
| 76 | * @shift: output amount of bits to shift right data before applying bit mask |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 77 | * @mask: output a bit mask for the raw data |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 78 | * @be: output if data in big endian |
| 79 | * @device_dir: the IIO device directory |
Alexandru Ardelean | 8827faa | 2021-02-15 12:40:43 +0200 | [diff] [blame] | 80 | * @buffer_idx: the IIO buffer index |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 81 | * @name: the channel name |
| 82 | * @generic_name: the channel type name |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 83 | * |
| 84 | * Returns a value >= 0 on success, otherwise a negative error code. |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 85 | **/ |
Alexandru Ardelean | a605c8f | 2021-02-15 12:40:41 +0200 | [diff] [blame] | 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, |
Alexandru Ardelean | 8827faa | 2021-02-15 12:40:43 +0200 | [diff] [blame] | 89 | const char *device_dir, int buffer_idx, |
| 90 | const char *name, const char *generic_name) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 91 | { |
| 92 | FILE *sysfsfp; |
| 93 | int ret; |
| 94 | DIR *dp; |
| 95 | char *scan_el_dir, *builtname, *builtname_generic, *filename = 0; |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 96 | char signchar, endianchar; |
Michael Hennerich | fc7f95a | 2011-02-24 16:34:54 +0100 | [diff] [blame] | 97 | unsigned padint; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 98 | const struct dirent *ent; |
| 99 | |
Alexandru Ardelean | 8827faa | 2021-02-15 12:40:43 +0200 | [diff] [blame] | 100 | ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir, buffer_idx); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 101 | if (ret < 0) |
| 102 | return -ENOMEM; |
| 103 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 104 | ret = asprintf(&builtname, FORMAT_TYPE_FILE, name); |
| 105 | if (ret < 0) { |
| 106 | ret = -ENOMEM; |
| 107 | goto error_free_scan_el_dir; |
| 108 | } |
| 109 | ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name); |
| 110 | if (ret < 0) { |
| 111 | ret = -ENOMEM; |
| 112 | goto error_free_builtname; |
| 113 | } |
| 114 | |
| 115 | dp = opendir(scan_el_dir); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 116 | if (!dp) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 117 | ret = -errno; |
| 118 | goto error_free_builtname_generic; |
| 119 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 120 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 121 | ret = -ENOENT; |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 122 | while (ent = readdir(dp), ent) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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); |
| 127 | if (ret < 0) { |
| 128 | ret = -ENOMEM; |
| 129 | goto error_closedir; |
| 130 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 131 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 132 | sysfsfp = fopen(filename, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 133 | if (!sysfsfp) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 134 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 135 | fprintf(stderr, "failed to open %s\n", |
| 136 | filename); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 137 | goto error_free_filename; |
| 138 | } |
Jonathan Cameron | a7f7c36 | 2011-12-04 19:10:58 +0000 | [diff] [blame] | 139 | |
| 140 | ret = fscanf(sysfsfp, |
| 141 | "%ce:%c%u/%u>>%u", |
| 142 | &endianchar, |
| 143 | &signchar, |
| 144 | bits_used, |
| 145 | &padint, shift); |
| 146 | if (ret < 0) { |
Peter Meerwald | 578f737 | 2012-06-25 23:13:24 +0200 | [diff] [blame] | 147 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 148 | fprintf(stderr, |
| 149 | "failed to pass scan type description\n"); |
Peter Meerwald | 578f737 | 2012-06-25 23:13:24 +0200 | [diff] [blame] | 150 | goto error_close_sysfsfp; |
Hartmut Knaack | dc8b5d6 | 2015-05-31 14:39:59 +0200 | [diff] [blame] | 151 | } else if (ret != 5) { |
| 152 | ret = -EIO; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 153 | fprintf(stderr, |
| 154 | "scan type description didn't match\n"); |
Hartmut Knaack | dc8b5d6 | 2015-05-31 14:39:59 +0200 | [diff] [blame] | 155 | goto error_close_sysfsfp; |
Jonathan Cameron | a7f7c36 | 2011-12-04 19:10:58 +0000 | [diff] [blame] | 156 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 157 | |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 158 | *be = (endianchar == 'b'); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 159 | *bytes = padint / 8; |
Michael Hennerich | fc7f95a | 2011-02-24 16:34:54 +0100 | [diff] [blame] | 160 | if (*bits_used == 64) |
Bastien Nocera | 208a68c8 | 2019-06-27 09:20:45 +0200 | [diff] [blame] | 161 | *mask = ~(0ULL); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 162 | else |
Bastien Nocera | 208a68c8 | 2019-06-27 09:20:45 +0200 | [diff] [blame] | 163 | *mask = (1ULL << *bits_used) - 1ULL; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 164 | |
Hartmut Knaack | 33ebcb2 | 2015-05-31 14:40:19 +0200 | [diff] [blame] | 165 | *is_signed = (signchar == 's'); |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 166 | if (fclose(sysfsfp)) { |
| 167 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 168 | fprintf(stderr, "Failed to close %s\n", |
| 169 | filename); |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 170 | goto error_free_filename; |
| 171 | } |
| 172 | |
Hartmut Knaack | ace76e4 | 2015-05-31 14:40:20 +0200 | [diff] [blame] | 173 | sysfsfp = 0; |
Jonathan Cameron | a7f7c36 | 2011-12-04 19:10:58 +0000 | [diff] [blame] | 174 | free(filename); |
Jonathan Cameron | a7f7c36 | 2011-12-04 19:10:58 +0000 | [diff] [blame] | 175 | filename = 0; |
Matt Ranostay | 6356f1b | 2016-08-22 15:19:37 -0700 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * Avoid having a more generic entry overwriting |
| 179 | * the settings. |
| 180 | */ |
| 181 | if (strcmp(builtname, ent->d_name) == 0) |
| 182 | break; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 183 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 184 | |
Peter Meerwald | 578f737 | 2012-06-25 23:13:24 +0200 | [diff] [blame] | 185 | error_close_sysfsfp: |
| 186 | if (sysfsfp) |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 187 | if (fclose(sysfsfp)) |
| 188 | perror("iioutils_get_type(): Failed to close file"); |
| 189 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 190 | error_free_filename: |
| 191 | if (filename) |
| 192 | free(filename); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 193 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 194 | error_closedir: |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 195 | if (closedir(dp) == -1) |
| 196 | perror("iioutils_get_type(): Failed to close directory"); |
| 197 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 198 | error_free_builtname_generic: |
| 199 | free(builtname_generic); |
| 200 | error_free_builtname: |
| 201 | free(builtname); |
| 202 | error_free_scan_el_dir: |
| 203 | free(scan_el_dir); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 204 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 205 | return ret; |
| 206 | } |
| 207 | |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 208 | /** |
| 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 |
| 215 | * |
| 216 | * Returns a value >= 0 on success, otherwise a negative error code. |
| 217 | **/ |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 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) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 221 | { |
| 222 | FILE *sysfsfp; |
| 223 | int ret; |
| 224 | DIR *dp; |
| 225 | char *builtname, *builtname_generic; |
| 226 | char *filename = NULL; |
| 227 | const struct dirent *ent; |
| 228 | |
| 229 | ret = asprintf(&builtname, "%s_%s", name, param_name); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 230 | if (ret < 0) |
| 231 | return -ENOMEM; |
| 232 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 233 | ret = asprintf(&builtname_generic, |
| 234 | "%s_%s", generic_name, param_name); |
| 235 | if (ret < 0) { |
| 236 | ret = -ENOMEM; |
| 237 | goto error_free_builtname; |
| 238 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 239 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 240 | dp = opendir(device_dir); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 241 | if (!dp) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 242 | ret = -errno; |
| 243 | goto error_free_builtname_generic; |
| 244 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 245 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 246 | ret = -ENOENT; |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 247 | while (ent = readdir(dp), ent) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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); |
| 252 | if (ret < 0) { |
| 253 | ret = -ENOMEM; |
| 254 | goto error_closedir; |
| 255 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 256 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 257 | sysfsfp = fopen(filename, "r"); |
| 258 | if (!sysfsfp) { |
| 259 | ret = -errno; |
| 260 | goto error_free_filename; |
| 261 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 262 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 263 | errno = 0; |
| 264 | if (fscanf(sysfsfp, "%f", output) != 1) |
| 265 | ret = errno ? -errno : -ENODATA; |
| 266 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 267 | break; |
| 268 | } |
| 269 | error_free_filename: |
| 270 | if (filename) |
| 271 | free(filename); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 272 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 273 | error_closedir: |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 274 | if (closedir(dp) == -1) |
| 275 | perror("iioutils_get_param_float(): Failed to close directory"); |
| 276 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 277 | error_free_builtname_generic: |
| 278 | free(builtname_generic); |
| 279 | error_free_builtname: |
| 280 | free(builtname); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 281 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 282 | return ret; |
| 283 | } |
| 284 | |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 285 | /** |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 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 |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 289 | **/ |
| 290 | |
Joo Aun Saw | 95ddd3f | 2015-07-25 01:23:29 +1000 | [diff] [blame] | 291 | void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt) |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 292 | { |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 293 | struct iio_channel_info temp; |
| 294 | int x, y; |
| 295 | |
| 296 | for (x = 0; x < cnt; x++) |
| 297 | for (y = 0; y < (cnt - 1); y++) |
Joo Aun Saw | 95ddd3f | 2015-07-25 01:23:29 +1000 | [diff] [blame] | 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]; |
| 301 | ci_array[y] = temp; |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 302 | } |
| 303 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 304 | |
| 305 | /** |
| 306 | * build_channel_array() - function to figure out what channels are present |
| 307 | * @device_dir: the IIO device directory in sysfs |
Alexandru Ardelean | 8827faa | 2021-02-15 12:40:43 +0200 | [diff] [blame] | 308 | * @buffer_idx: the IIO buffer for this channel array |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 309 | * @ci_array: output the resulting array of iio_channel_info |
| 310 | * @counter: output the amount of array elements |
| 311 | * |
| 312 | * Returns 0 on success, otherwise a negative error code. |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 313 | **/ |
Alexandru Ardelean | 8827faa | 2021-02-15 12:40:43 +0200 | [diff] [blame] | 314 | int build_channel_array(const char *device_dir, int buffer_idx, |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 315 | struct iio_channel_info **ci_array, int *counter) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 316 | { |
| 317 | DIR *dp; |
| 318 | FILE *sysfsfp; |
Hartmut Knaack | 1e7c347 | 2015-05-31 14:40:21 +0200 | [diff] [blame] | 319 | int count = 0, i; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 320 | struct iio_channel_info *current; |
| 321 | int ret; |
| 322 | const struct dirent *ent; |
| 323 | char *scan_el_dir; |
| 324 | char *filename; |
| 325 | |
| 326 | *counter = 0; |
Alexandru Ardelean | 8827faa | 2021-02-15 12:40:43 +0200 | [diff] [blame] | 327 | ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir, buffer_idx); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 328 | if (ret < 0) |
| 329 | return -ENOMEM; |
| 330 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 331 | dp = opendir(scan_el_dir); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 332 | if (!dp) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 333 | ret = -errno; |
| 334 | goto error_free_name; |
| 335 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 336 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 337 | while (ent = readdir(dp), ent) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 338 | if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), |
| 339 | "_en") == 0) { |
| 340 | ret = asprintf(&filename, |
| 341 | "%s/%s", scan_el_dir, ent->d_name); |
| 342 | if (ret < 0) { |
| 343 | ret = -ENOMEM; |
| 344 | goto error_close_dir; |
| 345 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 346 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 347 | sysfsfp = fopen(filename, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 348 | if (!sysfsfp) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 349 | ret = -errno; |
| 350 | free(filename); |
| 351 | goto error_close_dir; |
| 352 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 353 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 354 | errno = 0; |
| 355 | if (fscanf(sysfsfp, "%i", &ret) != 1) { |
| 356 | ret = errno ? -errno : -ENODATA; |
| 357 | if (fclose(sysfsfp)) |
| 358 | perror("build_channel_array(): Failed to close file"); |
| 359 | |
| 360 | free(filename); |
| 361 | goto error_close_dir; |
| 362 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 363 | if (ret == 1) |
| 364 | (*counter)++; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 365 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 366 | if (fclose(sysfsfp)) { |
| 367 | ret = -errno; |
| 368 | free(filename); |
| 369 | goto error_close_dir; |
| 370 | } |
| 371 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 372 | free(filename); |
| 373 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 374 | |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 375 | *ci_array = malloc(sizeof(**ci_array) * (*counter)); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 376 | if (!*ci_array) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 377 | ret = -ENOMEM; |
| 378 | goto error_close_dir; |
| 379 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 380 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 381 | seekdir(dp, 0); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 382 | while (ent = readdir(dp), ent) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 383 | if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), |
| 384 | "_en") == 0) { |
Craig Markwardt | 66c65d9 | 2014-01-01 15:38:52 +0000 | [diff] [blame] | 385 | int current_enabled = 0; |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 386 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 387 | current = &(*ci_array)[count++]; |
| 388 | ret = asprintf(&filename, |
| 389 | "%s/%s", scan_el_dir, ent->d_name); |
| 390 | if (ret < 0) { |
| 391 | ret = -ENOMEM; |
| 392 | /* decrement count to avoid freeing name */ |
| 393 | count--; |
| 394 | goto error_cleanup_array; |
| 395 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 396 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 397 | sysfsfp = fopen(filename, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 398 | if (!sysfsfp) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 399 | ret = -errno; |
Hartmut Knaack | 2b6a6e6 | 2015-05-31 14:39:48 +0200 | [diff] [blame] | 400 | free(filename); |
Hartmut Knaack | 121b5e5 | 2015-05-31 14:39:45 +0200 | [diff] [blame] | 401 | count--; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 402 | goto error_cleanup_array; |
| 403 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 404 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 405 | errno = 0; |
| 406 | if (fscanf(sysfsfp, "%i", ¤t_enabled) != 1) { |
| 407 | ret = errno ? -errno : -ENODATA; |
| 408 | free(filename); |
| 409 | count--; |
| 410 | goto error_cleanup_array; |
| 411 | } |
| 412 | |
| 413 | if (fclose(sysfsfp)) { |
| 414 | ret = -errno; |
| 415 | free(filename); |
| 416 | count--; |
| 417 | goto error_cleanup_array; |
| 418 | } |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 419 | |
Craig Markwardt | 66c65d9 | 2014-01-01 15:38:52 +0000 | [diff] [blame] | 420 | if (!current_enabled) { |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 421 | free(filename); |
| 422 | count--; |
| 423 | continue; |
| 424 | } |
| 425 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 426 | current->scale = 1.0; |
| 427 | current->offset = 0; |
| 428 | current->name = strndup(ent->d_name, |
| 429 | strlen(ent->d_name) - |
| 430 | strlen("_en")); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 431 | if (!current->name) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 432 | free(filename); |
| 433 | ret = -ENOMEM; |
Hartmut Knaack | 121b5e5 | 2015-05-31 14:39:45 +0200 | [diff] [blame] | 434 | count--; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 435 | goto error_cleanup_array; |
| 436 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 437 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 438 | /* Get the generic and specific name elements */ |
| 439 | ret = iioutils_break_up_name(current->name, |
| 440 | ¤t->generic_name); |
| 441 | if (ret) { |
| 442 | free(filename); |
Hartmut Knaack | 121b5e5 | 2015-05-31 14:39:45 +0200 | [diff] [blame] | 443 | free(current->name); |
| 444 | count--; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 445 | goto error_cleanup_array; |
| 446 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 447 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 448 | ret = asprintf(&filename, |
| 449 | "%s/%s_index", |
| 450 | scan_el_dir, |
| 451 | current->name); |
| 452 | if (ret < 0) { |
| 453 | free(filename); |
| 454 | ret = -ENOMEM; |
| 455 | goto error_cleanup_array; |
| 456 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 457 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 458 | sysfsfp = fopen(filename, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 459 | if (!sysfsfp) { |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 460 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 461 | fprintf(stderr, "failed to open %s\n", |
| 462 | filename); |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 463 | free(filename); |
| 464 | goto error_cleanup_array; |
| 465 | } |
| 466 | |
| 467 | errno = 0; |
| 468 | if (fscanf(sysfsfp, "%u", ¤t->index) != 1) { |
| 469 | ret = errno ? -errno : -ENODATA; |
| 470 | if (fclose(sysfsfp)) |
| 471 | perror("build_channel_array(): Failed to close file"); |
| 472 | |
| 473 | free(filename); |
| 474 | goto error_cleanup_array; |
| 475 | } |
| 476 | |
| 477 | if (fclose(sysfsfp)) { |
| 478 | ret = -errno; |
| 479 | free(filename); |
| 480 | goto error_cleanup_array; |
| 481 | } |
| 482 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 483 | free(filename); |
| 484 | /* Find the scale */ |
| 485 | ret = iioutils_get_param_float(¤t->scale, |
| 486 | "scale", |
| 487 | device_dir, |
| 488 | current->name, |
| 489 | current->generic_name); |
Joo Aun Saw | 7868dfd | 2015-07-23 12:53:47 +1000 | [diff] [blame] | 490 | if ((ret < 0) && (ret != -ENOENT)) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 491 | goto error_cleanup_array; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 492 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 493 | ret = iioutils_get_param_float(¤t->offset, |
| 494 | "offset", |
| 495 | device_dir, |
| 496 | current->name, |
| 497 | current->generic_name); |
Joo Aun Saw | 7868dfd | 2015-07-23 12:53:47 +1000 | [diff] [blame] | 498 | if ((ret < 0) && (ret != -ENOENT)) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 499 | goto error_cleanup_array; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 500 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 501 | ret = iioutils_get_type(¤t->is_signed, |
| 502 | ¤t->bytes, |
| 503 | ¤t->bits_used, |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 504 | ¤t->shift, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 505 | ¤t->mask, |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 506 | ¤t->be, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 507 | device_dir, |
Alexandru Ardelean | 8827faa | 2021-02-15 12:40:43 +0200 | [diff] [blame] | 508 | buffer_idx, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 509 | current->name, |
| 510 | current->generic_name); |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 511 | if (ret < 0) |
| 512 | goto error_cleanup_array; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 513 | } |
| 514 | } |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 515 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 516 | if (closedir(dp) == -1) { |
| 517 | ret = -errno; |
| 518 | goto error_cleanup_array; |
| 519 | } |
| 520 | |
Hartmut Knaack | 66dd08f | 2015-05-31 14:39:43 +0200 | [diff] [blame] | 521 | free(scan_el_dir); |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 522 | /* reorder so that the array is in index order */ |
Joo Aun Saw | 95ddd3f | 2015-07-25 01:23:29 +1000 | [diff] [blame] | 523 | bsort_channel_array_by_index(*ci_array, *counter); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 524 | |
| 525 | return 0; |
| 526 | |
| 527 | error_cleanup_array: |
Hartmut Knaack | 63f05c8 | 2015-05-31 14:39:44 +0200 | [diff] [blame] | 528 | for (i = count - 1; i >= 0; i--) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 529 | free((*ci_array)[i].name); |
Hartmut Knaack | 63f05c8 | 2015-05-31 14:39:44 +0200 | [diff] [blame] | 530 | free((*ci_array)[i].generic_name); |
| 531 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 532 | free(*ci_array); |
Joo Aun Saw | 6b20f40 | 2015-07-25 01:23:28 +1000 | [diff] [blame] | 533 | *ci_array = NULL; |
| 534 | *counter = 0; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 535 | error_close_dir: |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 536 | if (dp) |
| 537 | if (closedir(dp) == -1) |
| 538 | perror("build_channel_array(): Failed to close dir"); |
| 539 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 540 | error_free_name: |
| 541 | free(scan_el_dir); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 542 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 543 | return ret; |
| 544 | } |
| 545 | |
Joo Aun Saw | 5e37c52 | 2015-07-28 11:46:13 +1000 | [diff] [blame] | 546 | static int calc_digits(int num) |
Hartmut Knaack | 096f9b8 | 2015-05-31 14:40:00 +0200 | [diff] [blame] | 547 | { |
| 548 | int count = 0; |
| 549 | |
| 550 | while (num != 0) { |
| 551 | num /= 10; |
| 552 | count++; |
| 553 | } |
| 554 | |
| 555 | return count; |
| 556 | } |
| 557 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 558 | /** |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 559 | * find_type_by_name() - function to match top level types by name |
| 560 | * @name: top level type instance name |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 561 | * @type: the type of top level instance being searched |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 562 | * |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 563 | * Returns the device number of a matched IIO device on success, otherwise a |
| 564 | * negative error code. |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 565 | * Typical types this is used for are device and trigger. |
| 566 | **/ |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 567 | int find_type_by_name(const char *name, const char *type) |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 568 | { |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 569 | const struct dirent *ent; |
Hartmut Knaack | 096f9b8 | 2015-05-31 14:40:00 +0200 | [diff] [blame] | 570 | int number, numstrlen, ret; |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 571 | |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 572 | FILE *namefp; |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 573 | DIR *dp; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 574 | char thisname[IIO_MAX_NAME_LENGTH]; |
| 575 | char *filename; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 576 | |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 577 | dp = opendir(iio_dir); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 578 | if (!dp) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 579 | fprintf(stderr, "No industrialio devices available\n"); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 580 | return -ENODEV; |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 581 | } |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 582 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 583 | while (ent = readdir(dp), ent) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 584 | if (strcmp(ent->d_name, ".") != 0 && |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 585 | strcmp(ent->d_name, "..") != 0 && |
| 586 | strlen(ent->d_name) > strlen(type) && |
| 587 | strncmp(ent->d_name, type, strlen(type)) == 0) { |
Hartmut Knaack | 096f9b8 | 2015-05-31 14:40:00 +0200 | [diff] [blame] | 588 | errno = 0; |
| 589 | ret = sscanf(ent->d_name + strlen(type), "%d", &number); |
| 590 | if (ret < 0) { |
| 591 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 592 | fprintf(stderr, |
| 593 | "failed to read element number\n"); |
Hartmut Knaack | 096f9b8 | 2015-05-31 14:40:00 +0200 | [diff] [blame] | 594 | goto error_close_dir; |
| 595 | } else if (ret != 1) { |
| 596 | ret = -EIO; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 597 | fprintf(stderr, |
| 598 | "failed to match element number\n"); |
Hartmut Knaack | 096f9b8 | 2015-05-31 14:40:00 +0200 | [diff] [blame] | 599 | goto error_close_dir; |
| 600 | } |
| 601 | |
| 602 | numstrlen = calc_digits(number); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 603 | /* verify the next character is not a colon */ |
| 604 | if (strncmp(ent->d_name + strlen(type) + numstrlen, |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 605 | ":", 1) != 0) { |
| 606 | filename = malloc(strlen(iio_dir) + strlen(type) |
| 607 | + numstrlen + 6); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 608 | if (!filename) { |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 609 | ret = -ENOMEM; |
| 610 | goto error_close_dir; |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 611 | } |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 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 | |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 620 | namefp = fopen(filename, "r"); |
| 621 | if (!namefp) { |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 622 | free(filename); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 623 | continue; |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 624 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 625 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 626 | free(filename); |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 627 | errno = 0; |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 628 | if (fscanf(namefp, "%s", thisname) != 1) { |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 629 | ret = errno ? -errno : -ENODATA; |
| 630 | goto error_close_dir; |
| 631 | } |
| 632 | |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 633 | if (fclose(namefp)) { |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 634 | ret = -errno; |
| 635 | goto error_close_dir; |
| 636 | } |
| 637 | |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 638 | if (strcmp(name, thisname) == 0) { |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 639 | if (closedir(dp) == -1) |
| 640 | return -errno; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 641 | |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 642 | return number; |
| 643 | } |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | } |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 647 | if (closedir(dp) == -1) |
| 648 | return -errno; |
| 649 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 650 | return -ENODEV; |
Hartmut Knaack | 096f9b8 | 2015-05-31 14:40:00 +0200 | [diff] [blame] | 651 | |
| 652 | error_close_dir: |
| 653 | if (closedir(dp) == -1) |
| 654 | perror("find_type_by_name(): Failed to close directory"); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 655 | |
Hartmut Knaack | 096f9b8 | 2015-05-31 14:40:00 +0200 | [diff] [blame] | 656 | return ret; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 657 | } |
| 658 | |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 659 | static int _write_sysfs_int(const char *filename, const char *basedir, int val, |
| 660 | int verify) |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 661 | { |
Sebastian Andrzej Siewior | 11cb454 | 2013-10-07 13:42:00 +0100 | [diff] [blame] | 662 | int ret = 0; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 663 | FILE *sysfsfp; |
| 664 | int test; |
| 665 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 666 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 667 | if (!temp) |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 668 | return -ENOMEM; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 669 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 670 | ret = sprintf(temp, "%s/%s", basedir, filename); |
| 671 | if (ret < 0) |
| 672 | goto error_free; |
| 673 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 674 | sysfsfp = fopen(temp, "w"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 675 | if (!sysfsfp) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 676 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 677 | fprintf(stderr, "failed to open %s\n", temp); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 678 | goto error_free; |
| 679 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 680 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 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 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 694 | if (verify) { |
| 695 | sysfsfp = fopen(temp, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 696 | if (!sysfsfp) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 697 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 698 | fprintf(stderr, "failed to open %s\n", temp); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 699 | goto error_free; |
| 700 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 701 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 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 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 715 | if (test != val) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 716 | fprintf(stderr, |
| 717 | "Possible failure in int write %d to %s/%s\n", |
| 718 | val, basedir, filename); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 719 | ret = -1; |
| 720 | } |
| 721 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 722 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 723 | error_free: |
| 724 | free(temp); |
| 725 | return ret; |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 726 | } |
| 727 | |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 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 | **/ |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 736 | int write_sysfs_int(const char *filename, const char *basedir, int val) |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 737 | { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 738 | return _write_sysfs_int(filename, basedir, val, 0); |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 739 | } |
| 740 | |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 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 | **/ |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 750 | int write_sysfs_int_and_verify(const char *filename, const char *basedir, |
| 751 | int val) |
Jonathan Cameron | eaf86ff | 2010-05-04 14:43:04 +0100 | [diff] [blame] | 752 | { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 753 | return _write_sysfs_int(filename, basedir, val, 1); |
Jonathan Cameron | eaf86ff | 2010-05-04 14:43:04 +0100 | [diff] [blame] | 754 | } |
| 755 | |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 756 | static int _write_sysfs_string(const char *filename, const char *basedir, |
| 757 | const char *val, int verify) |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 758 | { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 759 | int ret = 0; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 760 | FILE *sysfsfp; |
| 761 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 762 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 763 | if (!temp) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 764 | fprintf(stderr, "Memory allocation failed\n"); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 765 | return -ENOMEM; |
| 766 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 767 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 768 | ret = sprintf(temp, "%s/%s", basedir, filename); |
| 769 | if (ret < 0) |
| 770 | goto error_free; |
| 771 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 772 | sysfsfp = fopen(temp, "w"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 773 | if (!sysfsfp) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 774 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 775 | fprintf(stderr, "Could not open %s\n", temp); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 776 | goto error_free; |
| 777 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 778 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 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 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 792 | if (verify) { |
| 793 | sysfsfp = fopen(temp, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 794 | if (!sysfsfp) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 795 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 796 | fprintf(stderr, "Could not open file to verify\n"); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 797 | goto error_free; |
| 798 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 799 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 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 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 813 | if (strcmp(temp, val) != 0) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 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); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 818 | ret = -1; |
| 819 | } |
| 820 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 821 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 822 | error_free: |
| 823 | free(temp); |
| 824 | |
| 825 | return ret; |
| 826 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 827 | |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 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 |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 833 | * |
| 834 | * Returns a value >= 0 on success, otherwise a negative error code. |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 835 | **/ |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 836 | int write_sysfs_string_and_verify(const char *filename, const char *basedir, |
| 837 | const char *val) |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 838 | { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 839 | return _write_sysfs_string(filename, basedir, val, 1); |
| 840 | } |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 841 | |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 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 | **/ |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 850 | int write_sysfs_string(const char *filename, const char *basedir, |
| 851 | const char *val) |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 852 | { |
| 853 | return _write_sysfs_string(filename, basedir, val, 0); |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 854 | } |
| 855 | |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 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 | **/ |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 864 | int read_sysfs_posint(const char *filename, const char *basedir) |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 865 | { |
| 866 | int ret; |
| 867 | FILE *sysfsfp; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 868 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 869 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 870 | if (!temp) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 871 | fprintf(stderr, "Memory allocation failed"); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 872 | return -ENOMEM; |
| 873 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 874 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 875 | ret = sprintf(temp, "%s/%s", basedir, filename); |
| 876 | if (ret < 0) |
| 877 | goto error_free; |
| 878 | |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 879 | sysfsfp = fopen(temp, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 880 | if (!sysfsfp) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 881 | ret = -errno; |
| 882 | goto error_free; |
| 883 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 884 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 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 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 897 | error_free: |
| 898 | free(temp); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 899 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 900 | return ret; |
| 901 | } |
| 902 | |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 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 | **/ |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 911 | int read_sysfs_float(const char *filename, const char *basedir, float *val) |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 912 | { |
Peter Meerwald | f5709d5 | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 913 | int ret = 0; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 914 | FILE *sysfsfp; |
| 915 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 916 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 917 | if (!temp) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 918 | fprintf(stderr, "Memory allocation failed"); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 919 | return -ENOMEM; |
| 920 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 921 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 922 | ret = sprintf(temp, "%s/%s", basedir, filename); |
| 923 | if (ret < 0) |
| 924 | goto error_free; |
| 925 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 926 | sysfsfp = fopen(temp, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 927 | if (!sysfsfp) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 928 | ret = -errno; |
| 929 | goto error_free; |
| 930 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 931 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 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 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 944 | error_free: |
| 945 | free(temp); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 946 | |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 947 | return ret; |
| 948 | } |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 949 | |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 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 | **/ |
Peter Meerwald | f5709d5 | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 958 | int read_sysfs_string(const char *filename, const char *basedir, char *str) |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 959 | { |
Peter Meerwald | f5709d5 | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 960 | int ret = 0; |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 961 | FILE *sysfsfp; |
| 962 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 963 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 964 | if (!temp) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 965 | fprintf(stderr, "Memory allocation failed"); |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 966 | return -ENOMEM; |
| 967 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 968 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 969 | ret = sprintf(temp, "%s/%s", basedir, filename); |
| 970 | if (ret < 0) |
| 971 | goto error_free; |
| 972 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 973 | sysfsfp = fopen(temp, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 974 | if (!sysfsfp) { |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 975 | ret = -errno; |
| 976 | goto error_free; |
| 977 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 978 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 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 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 991 | error_free: |
| 992 | free(temp); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 993 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 994 | return ret; |
| 995 | } |