Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 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 | */ |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <stdint.h> |
Lars-Peter Clausen | bc9f35d | 2011-10-26 17:27:44 +0100 | [diff] [blame] | 13 | #include <dirent.h> |
Peter Meerwald | bb23378 | 2012-06-25 23:12:17 +0200 | [diff] [blame] | 14 | #include <errno.h> |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 15 | #include <ctype.h> |
| 16 | #include "iio_utils.h" |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 17 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 18 | const char *iio_dir = "/sys/bus/iio/devices/"; |
| 19 | |
Irina Tirdea | d9d7b99 | 2015-03-27 13:53:00 +0200 | [diff] [blame] | 20 | static char * const iio_direction[] = { |
| 21 | "in", |
| 22 | "out", |
| 23 | }; |
| 24 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 25 | /** |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 26 | * iioutils_break_up_name() - extract generic name from full channel name |
| 27 | * @full_name: the full channel name |
| 28 | * @generic_name: the output generic channel name |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 29 | * |
| 30 | * 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] | 31 | **/ |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 32 | int iioutils_break_up_name(const char *full_name, char **generic_name) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 33 | { |
| 34 | char *current; |
| 35 | char *w, *r; |
Irina Tirdea | d9d7b99 | 2015-03-27 13:53:00 +0200 | [diff] [blame] | 36 | char *working, *prefix = ""; |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 37 | int i, ret; |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 38 | |
Cristina Opriceana | 34cbea1 | 2015-07-13 16:20:11 +0300 | [diff] [blame] | 39 | for (i = 0; i < ARRAY_SIZE(iio_direction); i++) |
Irina Tirdea | d9d7b99 | 2015-03-27 13:53:00 +0200 | [diff] [blame] | 40 | if (!strncmp(full_name, iio_direction[i], |
| 41 | strlen(iio_direction[i]))) { |
| 42 | prefix = iio_direction[i]; |
| 43 | break; |
| 44 | } |
| 45 | |
| 46 | current = strdup(full_name + strlen(prefix) + 1); |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 47 | if (!current) |
| 48 | return -ENOMEM; |
| 49 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 50 | working = strtok(current, "_\0"); |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 51 | if (!working) { |
| 52 | free(current); |
| 53 | return -EINVAL; |
| 54 | } |
Irina Tirdea | d9d7b99 | 2015-03-27 13:53:00 +0200 | [diff] [blame] | 55 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 56 | w = working; |
| 57 | r = working; |
| 58 | |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 59 | while (*r != '\0') { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 60 | if (!isdigit(*r)) { |
| 61 | *w = *r; |
| 62 | w++; |
| 63 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 64 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 65 | r++; |
| 66 | } |
| 67 | *w = '\0'; |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 68 | ret = asprintf(generic_name, "%s_%s", prefix, working); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 69 | free(current); |
| 70 | |
Hartmut Knaack | e9e45b4 | 2015-05-31 14:40:02 +0200 | [diff] [blame] | 71 | return (ret == -1) ? -ENOMEM : 0; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 75 | * iioutils_get_type() - find and process _type attribute data |
| 76 | * @is_signed: output whether channel is signed |
| 77 | * @bytes: output how many bytes the channel storage occupies |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 78 | * @bits_used: output number of valid bits of data |
| 79 | * @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] | 80 | * @mask: output a bit mask for the raw data |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 81 | * @be: output if data in big endian |
| 82 | * @device_dir: the IIO device directory |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 83 | * @name: the channel name |
| 84 | * @generic_name: the channel type name |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 85 | * |
| 86 | * Returns a value >= 0 on success, otherwise a negative error code. |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 87 | **/ |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 88 | int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used, |
| 89 | unsigned *shift, uint64_t *mask, unsigned *be, |
| 90 | const char *device_dir, const char *name, |
| 91 | const char *generic_name) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 92 | { |
| 93 | FILE *sysfsfp; |
| 94 | int ret; |
| 95 | DIR *dp; |
| 96 | char *scan_el_dir, *builtname, *builtname_generic, *filename = 0; |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 97 | char signchar, endianchar; |
Michael Hennerich | fc7f95a | 2011-02-24 16:34:54 +0100 | [diff] [blame] | 98 | unsigned padint; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 99 | const struct dirent *ent; |
| 100 | |
| 101 | ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 102 | if (ret < 0) |
| 103 | return -ENOMEM; |
| 104 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 105 | ret = asprintf(&builtname, FORMAT_TYPE_FILE, name); |
| 106 | if (ret < 0) { |
| 107 | ret = -ENOMEM; |
| 108 | goto error_free_scan_el_dir; |
| 109 | } |
| 110 | ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name); |
| 111 | if (ret < 0) { |
| 112 | ret = -ENOMEM; |
| 113 | goto error_free_builtname; |
| 114 | } |
| 115 | |
| 116 | dp = opendir(scan_el_dir); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 117 | if (!dp) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 118 | ret = -errno; |
| 119 | goto error_free_builtname_generic; |
| 120 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 121 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 122 | ret = -ENOENT; |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 123 | while (ent = readdir(dp), ent) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 124 | /* |
| 125 | * Do we allow devices to override a generic name with |
| 126 | * a specific one? |
| 127 | */ |
| 128 | if ((strcmp(builtname, ent->d_name) == 0) || |
| 129 | (strcmp(builtname_generic, ent->d_name) == 0)) { |
| 130 | ret = asprintf(&filename, |
| 131 | "%s/%s", scan_el_dir, ent->d_name); |
| 132 | if (ret < 0) { |
| 133 | ret = -ENOMEM; |
| 134 | goto error_closedir; |
| 135 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 136 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 137 | sysfsfp = fopen(filename, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 138 | if (!sysfsfp) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 139 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 140 | fprintf(stderr, "failed to open %s\n", |
| 141 | filename); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 142 | goto error_free_filename; |
| 143 | } |
Jonathan Cameron | a7f7c36 | 2011-12-04 19:10:58 +0000 | [diff] [blame] | 144 | |
| 145 | ret = fscanf(sysfsfp, |
| 146 | "%ce:%c%u/%u>>%u", |
| 147 | &endianchar, |
| 148 | &signchar, |
| 149 | bits_used, |
| 150 | &padint, shift); |
| 151 | if (ret < 0) { |
Peter Meerwald | 578f737 | 2012-06-25 23:13:24 +0200 | [diff] [blame] | 152 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 153 | fprintf(stderr, |
| 154 | "failed to pass scan type description\n"); |
Peter Meerwald | 578f737 | 2012-06-25 23:13:24 +0200 | [diff] [blame] | 155 | goto error_close_sysfsfp; |
Hartmut Knaack | dc8b5d6 | 2015-05-31 14:39:59 +0200 | [diff] [blame] | 156 | } else if (ret != 5) { |
| 157 | ret = -EIO; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 158 | fprintf(stderr, |
| 159 | "scan type description didn't match\n"); |
Hartmut Knaack | dc8b5d6 | 2015-05-31 14:39:59 +0200 | [diff] [blame] | 160 | goto error_close_sysfsfp; |
Jonathan Cameron | a7f7c36 | 2011-12-04 19:10:58 +0000 | [diff] [blame] | 161 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 162 | |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 163 | *be = (endianchar == 'b'); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 164 | *bytes = padint / 8; |
Michael Hennerich | fc7f95a | 2011-02-24 16:34:54 +0100 | [diff] [blame] | 165 | if (*bits_used == 64) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 166 | *mask = ~0; |
| 167 | else |
Irina Tirdea | ae067cb | 2015-07-24 16:28:05 +0300 | [diff] [blame] | 168 | *mask = (1ULL << *bits_used) - 1; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 169 | |
Hartmut Knaack | 33ebcb2 | 2015-05-31 14:40:19 +0200 | [diff] [blame] | 170 | *is_signed = (signchar == 's'); |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 171 | if (fclose(sysfsfp)) { |
| 172 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 173 | fprintf(stderr, "Failed to close %s\n", |
| 174 | filename); |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 175 | goto error_free_filename; |
| 176 | } |
| 177 | |
Hartmut Knaack | ace76e4 | 2015-05-31 14:40:20 +0200 | [diff] [blame] | 178 | sysfsfp = 0; |
Jonathan Cameron | a7f7c36 | 2011-12-04 19:10:58 +0000 | [diff] [blame] | 179 | free(filename); |
Jonathan Cameron | a7f7c36 | 2011-12-04 19:10:58 +0000 | [diff] [blame] | 180 | filename = 0; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 181 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 182 | |
Peter Meerwald | 578f737 | 2012-06-25 23:13:24 +0200 | [diff] [blame] | 183 | error_close_sysfsfp: |
| 184 | if (sysfsfp) |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 185 | if (fclose(sysfsfp)) |
| 186 | perror("iioutils_get_type(): Failed to close file"); |
| 187 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 188 | error_free_filename: |
| 189 | if (filename) |
| 190 | free(filename); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 191 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 192 | error_closedir: |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 193 | if (closedir(dp) == -1) |
| 194 | perror("iioutils_get_type(): Failed to close directory"); |
| 195 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 202 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 203 | return ret; |
| 204 | } |
| 205 | |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 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 | **/ |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 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) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 228 | if (ret < 0) |
| 229 | return -ENOMEM; |
| 230 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 237 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 238 | dp = opendir(device_dir); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 239 | if (!dp) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 240 | ret = -errno; |
| 241 | goto error_free_builtname_generic; |
| 242 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 243 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 244 | ret = -ENOENT; |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 245 | while (ent = readdir(dp), ent) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 254 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 255 | sysfsfp = fopen(filename, "r"); |
| 256 | if (!sysfsfp) { |
| 257 | ret = -errno; |
| 258 | goto error_free_filename; |
| 259 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 260 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 261 | errno = 0; |
| 262 | if (fscanf(sysfsfp, "%f", output) != 1) |
| 263 | ret = errno ? -errno : -ENODATA; |
| 264 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 265 | break; |
| 266 | } |
| 267 | error_free_filename: |
| 268 | if (filename) |
| 269 | free(filename); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 270 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 271 | error_closedir: |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 272 | if (closedir(dp) == -1) |
| 273 | perror("iioutils_get_param_float(): Failed to close directory"); |
| 274 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 275 | error_free_builtname_generic: |
| 276 | free(builtname_generic); |
| 277 | error_free_builtname: |
| 278 | free(builtname); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 279 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 280 | return ret; |
| 281 | } |
| 282 | |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 283 | /** |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 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 |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 287 | **/ |
| 288 | |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 289 | 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] | 290 | { |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 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++) |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 296 | if ((*ci_array)[y].index > (*ci_array)[y + 1].index) { |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 297 | temp = (*ci_array)[y + 1]; |
| 298 | (*ci_array)[y + 1] = (*ci_array)[y]; |
| 299 | (*ci_array)[y] = temp; |
| 300 | } |
| 301 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 302 | |
| 303 | /** |
| 304 | * build_channel_array() - function to figure out what channels are present |
| 305 | * @device_dir: the IIO device directory in sysfs |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 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. |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 310 | **/ |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 311 | int build_channel_array(const char *device_dir, |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 312 | struct iio_channel_info **ci_array, int *counter) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 313 | { |
| 314 | DIR *dp; |
| 315 | FILE *sysfsfp; |
Hartmut Knaack | 1e7c347 | 2015-05-31 14:40:21 +0200 | [diff] [blame] | 316 | int count = 0, i; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 325 | if (ret < 0) |
| 326 | return -ENOMEM; |
| 327 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 328 | dp = opendir(scan_el_dir); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 329 | if (!dp) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 330 | ret = -errno; |
| 331 | goto error_free_name; |
| 332 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 333 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 334 | while (ent = readdir(dp), ent) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 343 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 344 | sysfsfp = fopen(filename, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 345 | if (!sysfsfp) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 346 | ret = -errno; |
| 347 | free(filename); |
| 348 | goto error_close_dir; |
| 349 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 350 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 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 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 360 | if (ret == 1) |
| 361 | (*counter)++; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 362 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 363 | if (fclose(sysfsfp)) { |
| 364 | ret = -errno; |
| 365 | free(filename); |
| 366 | goto error_close_dir; |
| 367 | } |
| 368 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 369 | free(filename); |
| 370 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 371 | |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 372 | *ci_array = malloc(sizeof(**ci_array) * (*counter)); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 373 | if (!*ci_array) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 374 | ret = -ENOMEM; |
| 375 | goto error_close_dir; |
| 376 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 377 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 378 | seekdir(dp, 0); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 379 | while (ent = readdir(dp), ent) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 380 | if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), |
| 381 | "_en") == 0) { |
Craig Markwardt | 66c65d9 | 2014-01-01 15:38:52 +0000 | [diff] [blame] | 382 | int current_enabled = 0; |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 383 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 393 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 394 | sysfsfp = fopen(filename, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 395 | if (!sysfsfp) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 396 | ret = -errno; |
Hartmut Knaack | 2b6a6e6 | 2015-05-31 14:39:48 +0200 | [diff] [blame] | 397 | free(filename); |
Hartmut Knaack | 121b5e5 | 2015-05-31 14:39:45 +0200 | [diff] [blame] | 398 | count--; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 399 | goto error_cleanup_array; |
| 400 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 401 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 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 | } |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 416 | |
Craig Markwardt | 66c65d9 | 2014-01-01 15:38:52 +0000 | [diff] [blame] | 417 | if (!current_enabled) { |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 418 | free(filename); |
| 419 | count--; |
| 420 | continue; |
| 421 | } |
| 422 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 423 | current->scale = 1.0; |
| 424 | current->offset = 0; |
| 425 | current->name = strndup(ent->d_name, |
| 426 | strlen(ent->d_name) - |
| 427 | strlen("_en")); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 428 | if (!current->name) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 429 | free(filename); |
| 430 | ret = -ENOMEM; |
Hartmut Knaack | 121b5e5 | 2015-05-31 14:39:45 +0200 | [diff] [blame] | 431 | count--; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 432 | goto error_cleanup_array; |
| 433 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 434 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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); |
Hartmut Knaack | 121b5e5 | 2015-05-31 14:39:45 +0200 | [diff] [blame] | 440 | free(current->name); |
| 441 | count--; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 442 | goto error_cleanup_array; |
| 443 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 444 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 454 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 455 | sysfsfp = fopen(filename, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 456 | if (!sysfsfp) { |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 457 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 458 | fprintf(stderr, "failed to open %s\n", |
| 459 | filename); |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 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 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 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); |
Joo Aun Saw | 7868dfd | 2015-07-23 12:53:47 +1000 | [diff] [blame] | 487 | if ((ret < 0) && (ret != -ENOENT)) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 488 | goto error_cleanup_array; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 489 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 490 | ret = iioutils_get_param_float(¤t->offset, |
| 491 | "offset", |
| 492 | device_dir, |
| 493 | current->name, |
| 494 | current->generic_name); |
Joo Aun Saw | 7868dfd | 2015-07-23 12:53:47 +1000 | [diff] [blame] | 495 | if ((ret < 0) && (ret != -ENOENT)) |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 496 | goto error_cleanup_array; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 497 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 498 | ret = iioutils_get_type(¤t->is_signed, |
| 499 | ¤t->bytes, |
| 500 | ¤t->bits_used, |
Jonathan Cameron | 52615d4 | 2011-05-18 14:41:19 +0100 | [diff] [blame] | 501 | ¤t->shift, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 502 | ¤t->mask, |
Jonathan Cameron | 117cf8b | 2011-12-04 19:10:59 +0000 | [diff] [blame] | 503 | ¤t->be, |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 504 | device_dir, |
| 505 | current->name, |
| 506 | current->generic_name); |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 507 | if (ret < 0) |
| 508 | goto error_cleanup_array; |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 509 | } |
| 510 | } |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 511 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 512 | if (closedir(dp) == -1) { |
| 513 | ret = -errno; |
| 514 | goto error_cleanup_array; |
| 515 | } |
| 516 | |
Hartmut Knaack | 66dd08f | 2015-05-31 14:39:43 +0200 | [diff] [blame] | 517 | free(scan_el_dir); |
Michael Hennerich | 8b68bb2 | 2011-03-08 08:55:48 +0100 | [diff] [blame] | 518 | /* reorder so that the array is in index order */ |
| 519 | bsort_channel_array_by_index(ci_array, *counter); |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 520 | |
| 521 | return 0; |
| 522 | |
| 523 | error_cleanup_array: |
Hartmut Knaack | 63f05c8 | 2015-05-31 14:39:44 +0200 | [diff] [blame] | 524 | for (i = count - 1; i >= 0; i--) { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 525 | free((*ci_array)[i].name); |
Hartmut Knaack | 63f05c8 | 2015-05-31 14:39:44 +0200 | [diff] [blame] | 526 | free((*ci_array)[i].generic_name); |
| 527 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 528 | free(*ci_array); |
| 529 | error_close_dir: |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 530 | if (dp) |
| 531 | if (closedir(dp) == -1) |
| 532 | perror("build_channel_array(): Failed to close dir"); |
| 533 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 534 | error_free_name: |
| 535 | free(scan_el_dir); |
Hartmut Knaack | 0e79987 | 2015-05-31 14:40:17 +0200 | [diff] [blame] | 536 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 537 | return ret; |
| 538 | } |
| 539 | |
Joo Aun Saw | 5e37c52 | 2015-07-28 11:46:13 +1000 | [diff] [blame^] | 540 | static int calc_digits(int num) |
Hartmut Knaack | 096f9b8 | 2015-05-31 14:40:00 +0200 | [diff] [blame] | 541 | { |
| 542 | int count = 0; |
| 543 | |
| 544 | while (num != 0) { |
| 545 | num /= 10; |
| 546 | count++; |
| 547 | } |
| 548 | |
| 549 | return count; |
| 550 | } |
| 551 | |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 552 | /** |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 553 | * find_type_by_name() - function to match top level types by name |
| 554 | * @name: top level type instance name |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 555 | * @type: the type of top level instance being searched |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 556 | * |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 557 | * Returns the device number of a matched IIO device on success, otherwise a |
| 558 | * negative error code. |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 559 | * Typical types this is used for are device and trigger. |
| 560 | **/ |
Roberta Dobrescu | bdcb31d | 2015-02-26 10:49:24 +0200 | [diff] [blame] | 561 | int find_type_by_name(const char *name, const char *type) |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 562 | { |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 563 | const struct dirent *ent; |
Hartmut Knaack | 096f9b8 | 2015-05-31 14:40:00 +0200 | [diff] [blame] | 564 | int number, numstrlen, ret; |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 565 | |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 566 | FILE *namefp; |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 567 | DIR *dp; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 568 | char thisname[IIO_MAX_NAME_LENGTH]; |
| 569 | char *filename; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 570 | |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 571 | dp = opendir(iio_dir); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 572 | if (!dp) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 573 | fprintf(stderr, "No industrialio devices available\n"); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 574 | return -ENODEV; |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 575 | } |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 576 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 577 | while (ent = readdir(dp), ent) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 578 | if (strcmp(ent->d_name, ".") != 0 && |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 579 | strcmp(ent->d_name, "..") != 0 && |
| 580 | strlen(ent->d_name) > strlen(type) && |
| 581 | strncmp(ent->d_name, type, strlen(type)) == 0) { |
Hartmut Knaack | 096f9b8 | 2015-05-31 14:40:00 +0200 | [diff] [blame] | 582 | errno = 0; |
| 583 | ret = sscanf(ent->d_name + strlen(type), "%d", &number); |
| 584 | if (ret < 0) { |
| 585 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 586 | fprintf(stderr, |
| 587 | "failed to read element number\n"); |
Hartmut Knaack | 096f9b8 | 2015-05-31 14:40:00 +0200 | [diff] [blame] | 588 | goto error_close_dir; |
| 589 | } else if (ret != 1) { |
| 590 | ret = -EIO; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 591 | fprintf(stderr, |
| 592 | "failed to match element number\n"); |
Hartmut Knaack | 096f9b8 | 2015-05-31 14:40:00 +0200 | [diff] [blame] | 593 | goto error_close_dir; |
| 594 | } |
| 595 | |
| 596 | numstrlen = calc_digits(number); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 597 | /* verify the next character is not a colon */ |
| 598 | if (strncmp(ent->d_name + strlen(type) + numstrlen, |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 599 | ":", 1) != 0) { |
| 600 | filename = malloc(strlen(iio_dir) + strlen(type) |
| 601 | + numstrlen + 6); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 602 | if (!filename) { |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 603 | ret = -ENOMEM; |
| 604 | goto error_close_dir; |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 605 | } |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 606 | |
| 607 | ret = sprintf(filename, "%s%s%d/name", iio_dir, |
| 608 | type, number); |
| 609 | if (ret < 0) { |
| 610 | free(filename); |
| 611 | goto error_close_dir; |
| 612 | } |
| 613 | |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 614 | namefp = fopen(filename, "r"); |
| 615 | if (!namefp) { |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 616 | free(filename); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 617 | continue; |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 618 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 619 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 620 | free(filename); |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 621 | errno = 0; |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 622 | if (fscanf(namefp, "%s", thisname) != 1) { |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 623 | ret = errno ? -errno : -ENODATA; |
| 624 | goto error_close_dir; |
| 625 | } |
| 626 | |
Hartmut Knaack | a9d7acc | 2015-06-10 21:51:21 +0200 | [diff] [blame] | 627 | if (fclose(namefp)) { |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 628 | ret = -errno; |
| 629 | goto error_close_dir; |
| 630 | } |
| 631 | |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 632 | if (strcmp(name, thisname) == 0) { |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 633 | if (closedir(dp) == -1) |
| 634 | return -errno; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 635 | |
Peter Meerwald | a4d429e | 2012-06-25 23:13:25 +0200 | [diff] [blame] | 636 | return number; |
| 637 | } |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 638 | } |
| 639 | } |
| 640 | } |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 641 | if (closedir(dp) == -1) |
| 642 | return -errno; |
| 643 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 644 | return -ENODEV; |
Hartmut Knaack | 096f9b8 | 2015-05-31 14:40:00 +0200 | [diff] [blame] | 645 | |
| 646 | error_close_dir: |
| 647 | if (closedir(dp) == -1) |
| 648 | perror("find_type_by_name(): Failed to close directory"); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 649 | |
Hartmut Knaack | 096f9b8 | 2015-05-31 14:40:00 +0200 | [diff] [blame] | 650 | return ret; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 651 | } |
| 652 | |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 653 | static int _write_sysfs_int(const char *filename, const char *basedir, int val, |
| 654 | int verify) |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 655 | { |
Sebastian Andrzej Siewior | 11cb454 | 2013-10-07 13:42:00 +0100 | [diff] [blame] | 656 | int ret = 0; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 657 | FILE *sysfsfp; |
| 658 | int test; |
| 659 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 660 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 661 | if (!temp) |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 662 | return -ENOMEM; |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 663 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 664 | ret = sprintf(temp, "%s/%s", basedir, filename); |
| 665 | if (ret < 0) |
| 666 | goto error_free; |
| 667 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 668 | sysfsfp = fopen(temp, "w"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 669 | if (!sysfsfp) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 670 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 671 | fprintf(stderr, "failed to open %s\n", temp); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 672 | goto error_free; |
| 673 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 674 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 675 | ret = fprintf(sysfsfp, "%d", val); |
| 676 | if (ret < 0) { |
| 677 | if (fclose(sysfsfp)) |
| 678 | perror("_write_sysfs_int(): Failed to close dir"); |
| 679 | |
| 680 | goto error_free; |
| 681 | } |
| 682 | |
| 683 | if (fclose(sysfsfp)) { |
| 684 | ret = -errno; |
| 685 | goto error_free; |
| 686 | } |
| 687 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 688 | if (verify) { |
| 689 | sysfsfp = fopen(temp, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 690 | if (!sysfsfp) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 691 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 692 | fprintf(stderr, "failed to open %s\n", temp); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 693 | goto error_free; |
| 694 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 695 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 696 | if (fscanf(sysfsfp, "%d", &test) != 1) { |
| 697 | ret = errno ? -errno : -ENODATA; |
| 698 | if (fclose(sysfsfp)) |
| 699 | perror("_write_sysfs_int(): Failed to close dir"); |
| 700 | |
| 701 | goto error_free; |
| 702 | } |
| 703 | |
| 704 | if (fclose(sysfsfp)) { |
| 705 | ret = -errno; |
| 706 | goto error_free; |
| 707 | } |
| 708 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 709 | if (test != val) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 710 | fprintf(stderr, |
| 711 | "Possible failure in int write %d to %s/%s\n", |
| 712 | val, basedir, filename); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 713 | ret = -1; |
| 714 | } |
| 715 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 716 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 717 | error_free: |
| 718 | free(temp); |
| 719 | return ret; |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 720 | } |
| 721 | |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 722 | /** |
| 723 | * write_sysfs_int() - write an integer value to a sysfs file |
| 724 | * @filename: name of the file to write to |
| 725 | * @basedir: the sysfs directory in which the file is to be found |
| 726 | * @val: integer value to write to file |
| 727 | * |
| 728 | * Returns a value >= 0 on success, otherwise a negative error code. |
| 729 | **/ |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 730 | int write_sysfs_int(const char *filename, const char *basedir, int val) |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 731 | { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 732 | return _write_sysfs_int(filename, basedir, val, 0); |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 733 | } |
| 734 | |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 735 | /** |
| 736 | * write_sysfs_int_and_verify() - write an integer value to a sysfs file |
| 737 | * and verify |
| 738 | * @filename: name of the file to write to |
| 739 | * @basedir: the sysfs directory in which the file is to be found |
| 740 | * @val: integer value to write to file |
| 741 | * |
| 742 | * Returns a value >= 0 on success, otherwise a negative error code. |
| 743 | **/ |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 744 | int write_sysfs_int_and_verify(const char *filename, const char *basedir, |
| 745 | int val) |
Jonathan Cameron | eaf86ff | 2010-05-04 14:43:04 +0100 | [diff] [blame] | 746 | { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 747 | return _write_sysfs_int(filename, basedir, val, 1); |
Jonathan Cameron | eaf86ff | 2010-05-04 14:43:04 +0100 | [diff] [blame] | 748 | } |
| 749 | |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 750 | static int _write_sysfs_string(const char *filename, const char *basedir, |
| 751 | const char *val, int verify) |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 752 | { |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 753 | int ret = 0; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 754 | FILE *sysfsfp; |
| 755 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 756 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 757 | if (!temp) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 758 | fprintf(stderr, "Memory allocation failed\n"); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 759 | return -ENOMEM; |
| 760 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 761 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 762 | ret = sprintf(temp, "%s/%s", basedir, filename); |
| 763 | if (ret < 0) |
| 764 | goto error_free; |
| 765 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 766 | sysfsfp = fopen(temp, "w"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 767 | if (!sysfsfp) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 768 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 769 | fprintf(stderr, "Could not open %s\n", temp); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 770 | goto error_free; |
| 771 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 772 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 773 | ret = fprintf(sysfsfp, "%s", val); |
| 774 | if (ret < 0) { |
| 775 | if (fclose(sysfsfp)) |
| 776 | perror("_write_sysfs_string(): Failed to close dir"); |
| 777 | |
| 778 | goto error_free; |
| 779 | } |
| 780 | |
| 781 | if (fclose(sysfsfp)) { |
| 782 | ret = -errno; |
| 783 | goto error_free; |
| 784 | } |
| 785 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 786 | if (verify) { |
| 787 | sysfsfp = fopen(temp, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 788 | if (!sysfsfp) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 789 | ret = -errno; |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 790 | fprintf(stderr, "Could not open file to verify\n"); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 791 | goto error_free; |
| 792 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 793 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 794 | if (fscanf(sysfsfp, "%s", temp) != 1) { |
| 795 | ret = errno ? -errno : -ENODATA; |
| 796 | if (fclose(sysfsfp)) |
| 797 | perror("_write_sysfs_string(): Failed to close dir"); |
| 798 | |
| 799 | goto error_free; |
| 800 | } |
| 801 | |
| 802 | if (fclose(sysfsfp)) { |
| 803 | ret = -errno; |
| 804 | goto error_free; |
| 805 | } |
| 806 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 807 | if (strcmp(temp, val) != 0) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 808 | fprintf(stderr, |
| 809 | "Possible failure in string write of %s " |
| 810 | "Should be %s written to %s/%s\n", temp, val, |
| 811 | basedir, filename); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 812 | ret = -1; |
| 813 | } |
| 814 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 815 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 816 | error_free: |
| 817 | free(temp); |
| 818 | |
| 819 | return ret; |
| 820 | } |
Jonathan Cameron | e58537c | 2010-10-08 12:14:14 +0100 | [diff] [blame] | 821 | |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 822 | /** |
| 823 | * write_sysfs_string_and_verify() - string write, readback and verify |
| 824 | * @filename: name of file to write to |
| 825 | * @basedir: the sysfs directory in which the file is to be found |
| 826 | * @val: the string to write |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 827 | * |
| 828 | * Returns a value >= 0 on success, otherwise a negative error code. |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 829 | **/ |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 830 | int write_sysfs_string_and_verify(const char *filename, const char *basedir, |
| 831 | const char *val) |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 832 | { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 833 | return _write_sysfs_string(filename, basedir, val, 1); |
| 834 | } |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 835 | |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 836 | /** |
| 837 | * write_sysfs_string() - write string to a sysfs file |
| 838 | * @filename: name of file to write to |
| 839 | * @basedir: the sysfs directory in which the file is to be found |
| 840 | * @val: the string to write |
| 841 | * |
| 842 | * Returns a value >= 0 on success, otherwise a negative error code. |
| 843 | **/ |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 844 | int write_sysfs_string(const char *filename, const char *basedir, |
| 845 | const char *val) |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 846 | { |
| 847 | return _write_sysfs_string(filename, basedir, val, 0); |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 848 | } |
| 849 | |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 850 | /** |
| 851 | * read_sysfs_posint() - read an integer value from file |
| 852 | * @filename: name of file to read from |
| 853 | * @basedir: the sysfs directory in which the file is to be found |
| 854 | * |
| 855 | * Returns the read integer value >= 0 on success, otherwise a negative error |
| 856 | * code. |
| 857 | **/ |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 858 | int read_sysfs_posint(const char *filename, const char *basedir) |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 859 | { |
| 860 | int ret; |
| 861 | FILE *sysfsfp; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 862 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 863 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 864 | if (!temp) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 865 | fprintf(stderr, "Memory allocation failed"); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 866 | return -ENOMEM; |
| 867 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 868 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 869 | ret = sprintf(temp, "%s/%s", basedir, filename); |
| 870 | if (ret < 0) |
| 871 | goto error_free; |
| 872 | |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 873 | sysfsfp = fopen(temp, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 874 | if (!sysfsfp) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 875 | ret = -errno; |
| 876 | goto error_free; |
| 877 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 878 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 879 | errno = 0; |
| 880 | if (fscanf(sysfsfp, "%d\n", &ret) != 1) { |
| 881 | ret = errno ? -errno : -ENODATA; |
| 882 | if (fclose(sysfsfp)) |
| 883 | perror("read_sysfs_posint(): Failed to close dir"); |
| 884 | |
| 885 | goto error_free; |
| 886 | } |
| 887 | |
| 888 | if (fclose(sysfsfp)) |
| 889 | ret = -errno; |
| 890 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 891 | error_free: |
| 892 | free(temp); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 893 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 894 | return ret; |
| 895 | } |
| 896 | |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 897 | /** |
| 898 | * read_sysfs_float() - read a float value from file |
| 899 | * @filename: name of file to read from |
| 900 | * @basedir: the sysfs directory in which the file is to be found |
| 901 | * @val: output the read float value |
| 902 | * |
| 903 | * Returns a value >= 0 on success, otherwise a negative error code. |
| 904 | **/ |
Hartmut Knaack | 9d47525 | 2015-05-31 14:40:36 +0200 | [diff] [blame] | 905 | int read_sysfs_float(const char *filename, const char *basedir, float *val) |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 906 | { |
Peter Meerwald | f5709d5 | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 907 | int ret = 0; |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 908 | FILE *sysfsfp; |
| 909 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 910 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 911 | if (!temp) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 912 | fprintf(stderr, "Memory allocation failed"); |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 913 | return -ENOMEM; |
| 914 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 915 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 916 | ret = sprintf(temp, "%s/%s", basedir, filename); |
| 917 | if (ret < 0) |
| 918 | goto error_free; |
| 919 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 920 | sysfsfp = fopen(temp, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 921 | if (!sysfsfp) { |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 922 | ret = -errno; |
| 923 | goto error_free; |
| 924 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 925 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 926 | errno = 0; |
| 927 | if (fscanf(sysfsfp, "%f\n", val) != 1) { |
| 928 | ret = errno ? -errno : -ENODATA; |
| 929 | if (fclose(sysfsfp)) |
| 930 | perror("read_sysfs_float(): Failed to close dir"); |
| 931 | |
| 932 | goto error_free; |
| 933 | } |
| 934 | |
| 935 | if (fclose(sysfsfp)) |
| 936 | ret = -errno; |
| 937 | |
Jonathan Cameron | 9d8ae6c | 2010-05-04 14:43:13 +0100 | [diff] [blame] | 938 | error_free: |
| 939 | free(temp); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 940 | |
Jonathan Cameron | c57f1ba | 2009-08-18 18:06:32 +0100 | [diff] [blame] | 941 | return ret; |
| 942 | } |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 943 | |
Hartmut Knaack | 5dc65d7 | 2015-05-31 14:40:16 +0200 | [diff] [blame] | 944 | /** |
| 945 | * read_sysfs_string() - read a string from file |
| 946 | * @filename: name of file to read from |
| 947 | * @basedir: the sysfs directory in which the file is to be found |
| 948 | * @str: output the read string |
| 949 | * |
| 950 | * Returns a value >= 0 on success, otherwise a negative error code. |
| 951 | **/ |
Peter Meerwald | f5709d5 | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 952 | int read_sysfs_string(const char *filename, const char *basedir, char *str) |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 953 | { |
Peter Meerwald | f5709d5 | 2014-12-06 06:00:00 +0000 | [diff] [blame] | 954 | int ret = 0; |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 955 | FILE *sysfsfp; |
| 956 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
Melike Yurtoglu | 79bdd48 | 2014-10-03 23:35:54 +0300 | [diff] [blame] | 957 | |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 958 | if (!temp) { |
Cristina Opriceana | d9abc61 | 2015-07-17 18:43:42 +0300 | [diff] [blame] | 959 | fprintf(stderr, "Memory allocation failed"); |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 960 | return -ENOMEM; |
| 961 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 962 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 963 | ret = sprintf(temp, "%s/%s", basedir, filename); |
| 964 | if (ret < 0) |
| 965 | goto error_free; |
| 966 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 967 | sysfsfp = fopen(temp, "r"); |
Cristina Opriceana | ff1ac63 | 2015-07-13 16:15:56 +0300 | [diff] [blame] | 968 | if (!sysfsfp) { |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 969 | ret = -errno; |
| 970 | goto error_free; |
| 971 | } |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 972 | |
Hartmut Knaack | 5311855 | 2015-05-31 14:40:14 +0200 | [diff] [blame] | 973 | errno = 0; |
| 974 | if (fscanf(sysfsfp, "%s\n", str) != 1) { |
| 975 | ret = errno ? -errno : -ENODATA; |
| 976 | if (fclose(sysfsfp)) |
| 977 | perror("read_sysfs_string(): Failed to close dir"); |
| 978 | |
| 979 | goto error_free; |
| 980 | } |
| 981 | |
| 982 | if (fclose(sysfsfp)) |
| 983 | ret = -errno; |
| 984 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 985 | error_free: |
| 986 | free(temp); |
Hartmut Knaack | 7663a4a | 2015-06-10 21:51:20 +0200 | [diff] [blame] | 987 | |
Manuel Stahl | 49d916e | 2014-05-02 13:23:00 +0100 | [diff] [blame] | 988 | return ret; |
| 989 | } |