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