blob: aadee6d34c74c112f84be5977f23135e34ee0d20 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +01002/* IIO - useful set of util functionality
3 *
4 * Copyright (c) 2008 Jonathan Cameron
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +01005 */
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +01006#include <string.h>
7#include <stdlib.h>
Jonathan Camerone58537c2010-10-08 12:14:14 +01008#include <stdio.h>
9#include <stdint.h>
Lars-Peter Clausenbc9f35d2011-10-26 17:27:44 +010010#include <dirent.h>
Peter Meerwaldbb233782012-06-25 23:12:17 +020011#include <errno.h>
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020012#include <ctype.h>
13#include "iio_utils.h"
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +010014
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +010015const char *iio_dir = "/sys/bus/iio/devices/";
16
Irina Tirdead9d7b992015-03-27 13:53:00 +020017static char * const iio_direction[] = {
18 "in",
19 "out",
20};
21
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +010022/**
Jonathan Camerone58537c2010-10-08 12:14:14 +010023 * iioutils_break_up_name() - extract generic name from full channel name
24 * @full_name: the full channel name
25 * @generic_name: the output generic channel name
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020026 *
27 * Returns 0 on success, or a negative error code if string extraction failed.
Jonathan Camerone58537c2010-10-08 12:14:14 +010028 **/
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020029int iioutils_break_up_name(const char *full_name, char **generic_name)
Jonathan Camerone58537c2010-10-08 12:14:14 +010030{
31 char *current;
32 char *w, *r;
Irina Tirdead9d7b992015-03-27 13:53:00 +020033 char *working, *prefix = "";
Hartmut Knaacke9e45b42015-05-31 14:40:02 +020034 int i, ret;
Melike Yurtoglu79bdd482014-10-03 23:35:54 +030035
Cristina Opriceana34cbea12015-07-13 16:20:11 +030036 for (i = 0; i < ARRAY_SIZE(iio_direction); i++)
Irina Tirdead9d7b992015-03-27 13:53:00 +020037 if (!strncmp(full_name, iio_direction[i],
38 strlen(iio_direction[i]))) {
39 prefix = iio_direction[i];
40 break;
41 }
42
43 current = strdup(full_name + strlen(prefix) + 1);
Hartmut Knaacke9e45b42015-05-31 14:40:02 +020044 if (!current)
45 return -ENOMEM;
46
Jonathan Camerone58537c2010-10-08 12:14:14 +010047 working = strtok(current, "_\0");
Hartmut Knaack53118552015-05-31 14:40:14 +020048 if (!working) {
49 free(current);
50 return -EINVAL;
51 }
Irina Tirdead9d7b992015-03-27 13:53:00 +020052
Jonathan Camerone58537c2010-10-08 12:14:14 +010053 w = working;
54 r = working;
55
Michael Hennerich8b68bb22011-03-08 08:55:48 +010056 while (*r != '\0') {
Jonathan Camerone58537c2010-10-08 12:14:14 +010057 if (!isdigit(*r)) {
58 *w = *r;
59 w++;
60 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020061
Jonathan Camerone58537c2010-10-08 12:14:14 +010062 r++;
63 }
64 *w = '\0';
Hartmut Knaacke9e45b42015-05-31 14:40:02 +020065 ret = asprintf(generic_name, "%s_%s", prefix, working);
Jonathan Camerone58537c2010-10-08 12:14:14 +010066 free(current);
67
Hartmut Knaacke9e45b42015-05-31 14:40:02 +020068 return (ret == -1) ? -ENOMEM : 0;
Jonathan Camerone58537c2010-10-08 12:14:14 +010069}
70
71/**
Jonathan Camerone58537c2010-10-08 12:14:14 +010072 * iioutils_get_type() - find and process _type attribute data
73 * @is_signed: output whether channel is signed
74 * @bytes: output how many bytes the channel storage occupies
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020075 * @bits_used: output number of valid bits of data
76 * @shift: output amount of bits to shift right data before applying bit mask
Jonathan Camerone58537c2010-10-08 12:14:14 +010077 * @mask: output a bit mask for the raw data
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020078 * @be: output if data in big endian
79 * @device_dir: the IIO device directory
Alexandru Ardelean8827faa2021-02-15 12:40:43 +020080 * @buffer_idx: the IIO buffer index
Jonathan Camerone58537c2010-10-08 12:14:14 +010081 * @name: the channel name
82 * @generic_name: the channel type name
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020083 *
84 * Returns a value >= 0 on success, otherwise a negative error code.
Jonathan Camerone58537c2010-10-08 12:14:14 +010085 **/
Alexandru Ardeleana605c8f2021-02-15 12:40:41 +020086static int iioutils_get_type(unsigned int *is_signed, unsigned int *bytes,
87 unsigned int *bits_used, unsigned int *shift,
88 uint64_t *mask, unsigned int *be,
Alexandru Ardelean8827faa2021-02-15 12:40:43 +020089 const char *device_dir, int buffer_idx,
90 const char *name, const char *generic_name)
Jonathan Camerone58537c2010-10-08 12:14:14 +010091{
92 FILE *sysfsfp;
93 int ret;
94 DIR *dp;
95 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000096 char signchar, endianchar;
Michael Hennerichfc7f95a2011-02-24 16:34:54 +010097 unsigned padint;
Jonathan Camerone58537c2010-10-08 12:14:14 +010098 const struct dirent *ent;
99
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200100 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir, buffer_idx);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200101 if (ret < 0)
102 return -ENOMEM;
103
Jonathan Camerone58537c2010-10-08 12:14:14 +0100104 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
105 if (ret < 0) {
106 ret = -ENOMEM;
107 goto error_free_scan_el_dir;
108 }
109 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
110 if (ret < 0) {
111 ret = -ENOMEM;
112 goto error_free_builtname;
113 }
114
115 dp = opendir(scan_el_dir);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300116 if (!dp) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100117 ret = -errno;
118 goto error_free_builtname_generic;
119 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200120
Hartmut Knaack53118552015-05-31 14:40:14 +0200121 ret = -ENOENT;
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300122 while (ent = readdir(dp), ent)
Jonathan Camerone58537c2010-10-08 12:14:14 +0100123 if ((strcmp(builtname, ent->d_name) == 0) ||
124 (strcmp(builtname_generic, ent->d_name) == 0)) {
125 ret = asprintf(&filename,
126 "%s/%s", scan_el_dir, ent->d_name);
127 if (ret < 0) {
128 ret = -ENOMEM;
129 goto error_closedir;
130 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200131
Jonathan Camerone58537c2010-10-08 12:14:14 +0100132 sysfsfp = fopen(filename, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300133 if (!sysfsfp) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100134 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300135 fprintf(stderr, "failed to open %s\n",
136 filename);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100137 goto error_free_filename;
138 }
Jonathan Camerona7f7c362011-12-04 19:10:58 +0000139
140 ret = fscanf(sysfsfp,
141 "%ce:%c%u/%u>>%u",
142 &endianchar,
143 &signchar,
144 bits_used,
145 &padint, shift);
146 if (ret < 0) {
Peter Meerwald578f7372012-06-25 23:13:24 +0200147 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300148 fprintf(stderr,
149 "failed to pass scan type description\n");
Peter Meerwald578f7372012-06-25 23:13:24 +0200150 goto error_close_sysfsfp;
Hartmut Knaackdc8b5d62015-05-31 14:39:59 +0200151 } else if (ret != 5) {
152 ret = -EIO;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300153 fprintf(stderr,
154 "scan type description didn't match\n");
Hartmut Knaackdc8b5d62015-05-31 14:39:59 +0200155 goto error_close_sysfsfp;
Jonathan Camerona7f7c362011-12-04 19:10:58 +0000156 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200157
Jonathan Cameron117cf8b2011-12-04 19:10:59 +0000158 *be = (endianchar == 'b');
Jonathan Camerone58537c2010-10-08 12:14:14 +0100159 *bytes = padint / 8;
Michael Hennerichfc7f95a2011-02-24 16:34:54 +0100160 if (*bits_used == 64)
Bastien Nocera208a68c82019-06-27 09:20:45 +0200161 *mask = ~(0ULL);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100162 else
Bastien Nocera208a68c82019-06-27 09:20:45 +0200163 *mask = (1ULL << *bits_used) - 1ULL;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200164
Hartmut Knaack33ebcb22015-05-31 14:40:19 +0200165 *is_signed = (signchar == 's');
Hartmut Knaack53118552015-05-31 14:40:14 +0200166 if (fclose(sysfsfp)) {
167 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300168 fprintf(stderr, "Failed to close %s\n",
169 filename);
Hartmut Knaack53118552015-05-31 14:40:14 +0200170 goto error_free_filename;
171 }
172
Hartmut Knaackace76e42015-05-31 14:40:20 +0200173 sysfsfp = 0;
Jonathan Camerona7f7c362011-12-04 19:10:58 +0000174 free(filename);
Jonathan Camerona7f7c362011-12-04 19:10:58 +0000175 filename = 0;
Matt Ranostay6356f1b2016-08-22 15:19:37 -0700176
177 /*
178 * Avoid having a more generic entry overwriting
179 * the settings.
180 */
181 if (strcmp(builtname, ent->d_name) == 0)
182 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100183 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200184
Peter Meerwald578f7372012-06-25 23:13:24 +0200185error_close_sysfsfp:
186 if (sysfsfp)
Hartmut Knaack53118552015-05-31 14:40:14 +0200187 if (fclose(sysfsfp))
188 perror("iioutils_get_type(): Failed to close file");
189
Jonathan Camerone58537c2010-10-08 12:14:14 +0100190error_free_filename:
191 if (filename)
192 free(filename);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200193
Jonathan Camerone58537c2010-10-08 12:14:14 +0100194error_closedir:
Hartmut Knaack53118552015-05-31 14:40:14 +0200195 if (closedir(dp) == -1)
196 perror("iioutils_get_type(): Failed to close directory");
197
Jonathan Camerone58537c2010-10-08 12:14:14 +0100198error_free_builtname_generic:
199 free(builtname_generic);
200error_free_builtname:
201 free(builtname);
202error_free_scan_el_dir:
203 free(scan_el_dir);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200204
Jonathan Camerone58537c2010-10-08 12:14:14 +0100205 return ret;
206}
207
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200208/**
209 * iioutils_get_param_float() - read a float value from a channel parameter
210 * @output: output the float value
211 * @param_name: the parameter name to read
212 * @device_dir: the IIO device directory in sysfs
213 * @name: the channel name
214 * @generic_name: the channel type name
215 *
216 * Returns a value >= 0 on success, otherwise a negative error code.
217 **/
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200218int iioutils_get_param_float(float *output, const char *param_name,
219 const char *device_dir, const char *name,
220 const char *generic_name)
Jonathan Camerone58537c2010-10-08 12:14:14 +0100221{
222 FILE *sysfsfp;
223 int ret;
224 DIR *dp;
225 char *builtname, *builtname_generic;
226 char *filename = NULL;
227 const struct dirent *ent;
228
229 ret = asprintf(&builtname, "%s_%s", name, param_name);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200230 if (ret < 0)
231 return -ENOMEM;
232
Jonathan Camerone58537c2010-10-08 12:14:14 +0100233 ret = asprintf(&builtname_generic,
234 "%s_%s", generic_name, param_name);
235 if (ret < 0) {
236 ret = -ENOMEM;
237 goto error_free_builtname;
238 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200239
Jonathan Camerone58537c2010-10-08 12:14:14 +0100240 dp = opendir(device_dir);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300241 if (!dp) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100242 ret = -errno;
243 goto error_free_builtname_generic;
244 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200245
Hartmut Knaack53118552015-05-31 14:40:14 +0200246 ret = -ENOENT;
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300247 while (ent = readdir(dp), ent)
Jonathan Camerone58537c2010-10-08 12:14:14 +0100248 if ((strcmp(builtname, ent->d_name) == 0) ||
249 (strcmp(builtname_generic, ent->d_name) == 0)) {
250 ret = asprintf(&filename,
251 "%s/%s", device_dir, ent->d_name);
252 if (ret < 0) {
253 ret = -ENOMEM;
254 goto error_closedir;
255 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200256
Jonathan Camerone58537c2010-10-08 12:14:14 +0100257 sysfsfp = fopen(filename, "r");
258 if (!sysfsfp) {
259 ret = -errno;
260 goto error_free_filename;
261 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200262
Hartmut Knaack53118552015-05-31 14:40:14 +0200263 errno = 0;
264 if (fscanf(sysfsfp, "%f", output) != 1)
265 ret = errno ? -errno : -ENODATA;
266
Jonathan Camerone58537c2010-10-08 12:14:14 +0100267 break;
268 }
269error_free_filename:
270 if (filename)
271 free(filename);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200272
Jonathan Camerone58537c2010-10-08 12:14:14 +0100273error_closedir:
Hartmut Knaack53118552015-05-31 14:40:14 +0200274 if (closedir(dp) == -1)
275 perror("iioutils_get_param_float(): Failed to close directory");
276
Jonathan Camerone58537c2010-10-08 12:14:14 +0100277error_free_builtname_generic:
278 free(builtname_generic);
279error_free_builtname:
280 free(builtname);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200281
Jonathan Camerone58537c2010-10-08 12:14:14 +0100282 return ret;
283}
284
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100285/**
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200286 * bsort_channel_array_by_index() - sort the array in index order
287 * @ci_array: the iio_channel_info array to be sorted
288 * @cnt: the amount of array elements
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100289 **/
290
Joo Aun Saw95ddd3f2015-07-25 01:23:29 +1000291void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt)
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100292{
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100293 struct iio_channel_info temp;
294 int x, y;
295
296 for (x = 0; x < cnt; x++)
297 for (y = 0; y < (cnt - 1); y++)
Joo Aun Saw95ddd3f2015-07-25 01:23:29 +1000298 if (ci_array[y].index > ci_array[y + 1].index) {
299 temp = ci_array[y + 1];
300 ci_array[y + 1] = ci_array[y];
301 ci_array[y] = temp;
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100302 }
303}
Jonathan Camerone58537c2010-10-08 12:14:14 +0100304
305/**
306 * build_channel_array() - function to figure out what channels are present
307 * @device_dir: the IIO device directory in sysfs
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200308 * @buffer_idx: the IIO buffer for this channel array
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200309 * @ci_array: output the resulting array of iio_channel_info
310 * @counter: output the amount of array elements
311 *
312 * Returns 0 on success, otherwise a negative error code.
Jonathan Camerone58537c2010-10-08 12:14:14 +0100313 **/
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200314int build_channel_array(const char *device_dir, int buffer_idx,
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200315 struct iio_channel_info **ci_array, int *counter)
Jonathan Camerone58537c2010-10-08 12:14:14 +0100316{
317 DIR *dp;
318 FILE *sysfsfp;
Hartmut Knaack1e7c3472015-05-31 14:40:21 +0200319 int count = 0, i;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100320 struct iio_channel_info *current;
321 int ret;
322 const struct dirent *ent;
323 char *scan_el_dir;
324 char *filename;
325
326 *counter = 0;
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200327 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir, buffer_idx);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200328 if (ret < 0)
329 return -ENOMEM;
330
Jonathan Camerone58537c2010-10-08 12:14:14 +0100331 dp = opendir(scan_el_dir);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300332 if (!dp) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100333 ret = -errno;
334 goto error_free_name;
335 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200336
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300337 while (ent = readdir(dp), ent)
Jonathan Camerone58537c2010-10-08 12:14:14 +0100338 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
339 "_en") == 0) {
340 ret = asprintf(&filename,
341 "%s/%s", scan_el_dir, ent->d_name);
342 if (ret < 0) {
343 ret = -ENOMEM;
344 goto error_close_dir;
345 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200346
Jonathan Camerone58537c2010-10-08 12:14:14 +0100347 sysfsfp = fopen(filename, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300348 if (!sysfsfp) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100349 ret = -errno;
350 free(filename);
351 goto error_close_dir;
352 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200353
Hartmut Knaack53118552015-05-31 14:40:14 +0200354 errno = 0;
355 if (fscanf(sysfsfp, "%i", &ret) != 1) {
356 ret = errno ? -errno : -ENODATA;
357 if (fclose(sysfsfp))
358 perror("build_channel_array(): Failed to close file");
359
360 free(filename);
361 goto error_close_dir;
362 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100363 if (ret == 1)
364 (*counter)++;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200365
Hartmut Knaack53118552015-05-31 14:40:14 +0200366 if (fclose(sysfsfp)) {
367 ret = -errno;
368 free(filename);
369 goto error_close_dir;
370 }
371
Jonathan Camerone58537c2010-10-08 12:14:14 +0100372 free(filename);
373 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200374
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100375 *ci_array = malloc(sizeof(**ci_array) * (*counter));
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300376 if (!*ci_array) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100377 ret = -ENOMEM;
378 goto error_close_dir;
379 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200380
Jonathan Camerone58537c2010-10-08 12:14:14 +0100381 seekdir(dp, 0);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300382 while (ent = readdir(dp), ent) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100383 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
384 "_en") == 0) {
Craig Markwardt66c65d92014-01-01 15:38:52 +0000385 int current_enabled = 0;
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300386
Jonathan Camerone58537c2010-10-08 12:14:14 +0100387 current = &(*ci_array)[count++];
388 ret = asprintf(&filename,
389 "%s/%s", scan_el_dir, ent->d_name);
390 if (ret < 0) {
391 ret = -ENOMEM;
392 /* decrement count to avoid freeing name */
393 count--;
394 goto error_cleanup_array;
395 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200396
Jonathan Camerone58537c2010-10-08 12:14:14 +0100397 sysfsfp = fopen(filename, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300398 if (!sysfsfp) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100399 ret = -errno;
Hartmut Knaack2b6a6e62015-05-31 14:39:48 +0200400 free(filename);
Hartmut Knaack121b5e52015-05-31 14:39:45 +0200401 count--;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100402 goto error_cleanup_array;
403 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200404
Hartmut Knaack53118552015-05-31 14:40:14 +0200405 errno = 0;
406 if (fscanf(sysfsfp, "%i", &current_enabled) != 1) {
407 ret = errno ? -errno : -ENODATA;
408 free(filename);
409 count--;
410 goto error_cleanup_array;
411 }
412
413 if (fclose(sysfsfp)) {
414 ret = -errno;
415 free(filename);
416 count--;
417 goto error_cleanup_array;
418 }
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100419
Craig Markwardt66c65d92014-01-01 15:38:52 +0000420 if (!current_enabled) {
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100421 free(filename);
422 count--;
423 continue;
424 }
425
Jonathan Camerone58537c2010-10-08 12:14:14 +0100426 current->scale = 1.0;
427 current->offset = 0;
428 current->name = strndup(ent->d_name,
429 strlen(ent->d_name) -
430 strlen("_en"));
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300431 if (!current->name) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100432 free(filename);
433 ret = -ENOMEM;
Hartmut Knaack121b5e52015-05-31 14:39:45 +0200434 count--;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100435 goto error_cleanup_array;
436 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200437
Jonathan Camerone58537c2010-10-08 12:14:14 +0100438 /* Get the generic and specific name elements */
439 ret = iioutils_break_up_name(current->name,
440 &current->generic_name);
441 if (ret) {
442 free(filename);
Hartmut Knaack121b5e52015-05-31 14:39:45 +0200443 free(current->name);
444 count--;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100445 goto error_cleanup_array;
446 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200447
Jonathan Camerone58537c2010-10-08 12:14:14 +0100448 ret = asprintf(&filename,
449 "%s/%s_index",
450 scan_el_dir,
451 current->name);
452 if (ret < 0) {
453 free(filename);
454 ret = -ENOMEM;
455 goto error_cleanup_array;
456 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200457
Jonathan Camerone58537c2010-10-08 12:14:14 +0100458 sysfsfp = fopen(filename, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300459 if (!sysfsfp) {
Hartmut Knaack53118552015-05-31 14:40:14 +0200460 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300461 fprintf(stderr, "failed to open %s\n",
462 filename);
Hartmut Knaack53118552015-05-31 14:40:14 +0200463 free(filename);
464 goto error_cleanup_array;
465 }
466
467 errno = 0;
468 if (fscanf(sysfsfp, "%u", &current->index) != 1) {
469 ret = errno ? -errno : -ENODATA;
470 if (fclose(sysfsfp))
471 perror("build_channel_array(): Failed to close file");
472
473 free(filename);
474 goto error_cleanup_array;
475 }
476
477 if (fclose(sysfsfp)) {
478 ret = -errno;
479 free(filename);
480 goto error_cleanup_array;
481 }
482
Jonathan Camerone58537c2010-10-08 12:14:14 +0100483 free(filename);
484 /* Find the scale */
485 ret = iioutils_get_param_float(&current->scale,
486 "scale",
487 device_dir,
488 current->name,
489 current->generic_name);
Joo Aun Saw7868dfd2015-07-23 12:53:47 +1000490 if ((ret < 0) && (ret != -ENOENT))
Jonathan Camerone58537c2010-10-08 12:14:14 +0100491 goto error_cleanup_array;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200492
Jonathan Camerone58537c2010-10-08 12:14:14 +0100493 ret = iioutils_get_param_float(&current->offset,
494 "offset",
495 device_dir,
496 current->name,
497 current->generic_name);
Joo Aun Saw7868dfd2015-07-23 12:53:47 +1000498 if ((ret < 0) && (ret != -ENOENT))
Jonathan Camerone58537c2010-10-08 12:14:14 +0100499 goto error_cleanup_array;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200500
Jonathan Camerone58537c2010-10-08 12:14:14 +0100501 ret = iioutils_get_type(&current->is_signed,
502 &current->bytes,
503 &current->bits_used,
Jonathan Cameron52615d42011-05-18 14:41:19 +0100504 &current->shift,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100505 &current->mask,
Jonathan Cameron117cf8b2011-12-04 19:10:59 +0000506 &current->be,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100507 device_dir,
Alexandru Ardelean8827faa2021-02-15 12:40:43 +0200508 buffer_idx,
Jonathan Camerone58537c2010-10-08 12:14:14 +0100509 current->name,
510 current->generic_name);
Hartmut Knaack53118552015-05-31 14:40:14 +0200511 if (ret < 0)
512 goto error_cleanup_array;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100513 }
514 }
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100515
Hartmut Knaack53118552015-05-31 14:40:14 +0200516 if (closedir(dp) == -1) {
517 ret = -errno;
518 goto error_cleanup_array;
519 }
520
Hartmut Knaack66dd08f2015-05-31 14:39:43 +0200521 free(scan_el_dir);
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100522 /* reorder so that the array is in index order */
Joo Aun Saw95ddd3f2015-07-25 01:23:29 +1000523 bsort_channel_array_by_index(*ci_array, *counter);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100524
525 return 0;
526
527error_cleanup_array:
Hartmut Knaack63f05c82015-05-31 14:39:44 +0200528 for (i = count - 1; i >= 0; i--) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100529 free((*ci_array)[i].name);
Hartmut Knaack63f05c82015-05-31 14:39:44 +0200530 free((*ci_array)[i].generic_name);
531 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100532 free(*ci_array);
Joo Aun Saw6b20f402015-07-25 01:23:28 +1000533 *ci_array = NULL;
534 *counter = 0;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100535error_close_dir:
Hartmut Knaack53118552015-05-31 14:40:14 +0200536 if (dp)
537 if (closedir(dp) == -1)
538 perror("build_channel_array(): Failed to close dir");
539
Jonathan Camerone58537c2010-10-08 12:14:14 +0100540error_free_name:
541 free(scan_el_dir);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200542
Jonathan Camerone58537c2010-10-08 12:14:14 +0100543 return ret;
544}
545
Joo Aun Saw5e37c522015-07-28 11:46:13 +1000546static int calc_digits(int num)
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200547{
548 int count = 0;
549
550 while (num != 0) {
551 num /= 10;
552 count++;
553 }
554
555 return count;
556}
557
Jonathan Camerone58537c2010-10-08 12:14:14 +0100558/**
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100559 * find_type_by_name() - function to match top level types by name
560 * @name: top level type instance name
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200561 * @type: the type of top level instance being searched
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100562 *
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200563 * Returns the device number of a matched IIO device on success, otherwise a
564 * negative error code.
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100565 * Typical types this is used for are device and trigger.
566 **/
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +0200567int find_type_by_name(const char *name, const char *type)
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100568{
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100569 const struct dirent *ent;
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200570 int number, numstrlen, ret;
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100571
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +0200572 FILE *namefp;
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100573 DIR *dp;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100574 char thisname[IIO_MAX_NAME_LENGTH];
575 char *filename;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100576
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100577 dp = opendir(iio_dir);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300578 if (!dp) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300579 fprintf(stderr, "No industrialio devices available\n");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100580 return -ENODEV;
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100581 }
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100582
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300583 while (ent = readdir(dp), ent) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100584 if (strcmp(ent->d_name, ".") != 0 &&
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200585 strcmp(ent->d_name, "..") != 0 &&
586 strlen(ent->d_name) > strlen(type) &&
587 strncmp(ent->d_name, type, strlen(type)) == 0) {
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200588 errno = 0;
589 ret = sscanf(ent->d_name + strlen(type), "%d", &number);
590 if (ret < 0) {
591 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300592 fprintf(stderr,
593 "failed to read element number\n");
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200594 goto error_close_dir;
595 } else if (ret != 1) {
596 ret = -EIO;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300597 fprintf(stderr,
598 "failed to match element number\n");
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200599 goto error_close_dir;
600 }
601
602 numstrlen = calc_digits(number);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100603 /* verify the next character is not a colon */
604 if (strncmp(ent->d_name + strlen(type) + numstrlen,
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200605 ":", 1) != 0) {
606 filename = malloc(strlen(iio_dir) + strlen(type)
607 + numstrlen + 6);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300608 if (!filename) {
Hartmut Knaack53118552015-05-31 14:40:14 +0200609 ret = -ENOMEM;
610 goto error_close_dir;
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200611 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200612
613 ret = sprintf(filename, "%s%s%d/name", iio_dir,
614 type, number);
615 if (ret < 0) {
616 free(filename);
617 goto error_close_dir;
618 }
619
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +0200620 namefp = fopen(filename, "r");
621 if (!namefp) {
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200622 free(filename);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100623 continue;
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200624 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200625
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100626 free(filename);
Hartmut Knaack53118552015-05-31 14:40:14 +0200627 errno = 0;
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +0200628 if (fscanf(namefp, "%s", thisname) != 1) {
Hartmut Knaack53118552015-05-31 14:40:14 +0200629 ret = errno ? -errno : -ENODATA;
630 goto error_close_dir;
631 }
632
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +0200633 if (fclose(namefp)) {
Hartmut Knaack53118552015-05-31 14:40:14 +0200634 ret = -errno;
635 goto error_close_dir;
636 }
637
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200638 if (strcmp(name, thisname) == 0) {
Hartmut Knaack53118552015-05-31 14:40:14 +0200639 if (closedir(dp) == -1)
640 return -errno;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200641
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200642 return number;
643 }
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100644 }
645 }
646 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200647 if (closedir(dp) == -1)
648 return -errno;
649
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100650 return -ENODEV;
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200651
652error_close_dir:
653 if (closedir(dp) == -1)
654 perror("find_type_by_name(): Failed to close directory");
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200655
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200656 return ret;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100657}
658
Hartmut Knaack9d475252015-05-31 14:40:36 +0200659static int _write_sysfs_int(const char *filename, const char *basedir, int val,
660 int verify)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100661{
Sebastian Andrzej Siewior11cb4542013-10-07 13:42:00 +0100662 int ret = 0;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100663 FILE *sysfsfp;
664 int test;
665 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300666
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300667 if (!temp)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100668 return -ENOMEM;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200669
Hartmut Knaack53118552015-05-31 14:40:14 +0200670 ret = sprintf(temp, "%s/%s", basedir, filename);
671 if (ret < 0)
672 goto error_free;
673
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100674 sysfsfp = fopen(temp, "w");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300675 if (!sysfsfp) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100676 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300677 fprintf(stderr, "failed to open %s\n", temp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100678 goto error_free;
679 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200680
Hartmut Knaack53118552015-05-31 14:40:14 +0200681 ret = fprintf(sysfsfp, "%d", val);
682 if (ret < 0) {
683 if (fclose(sysfsfp))
684 perror("_write_sysfs_int(): Failed to close dir");
685
686 goto error_free;
687 }
688
689 if (fclose(sysfsfp)) {
690 ret = -errno;
691 goto error_free;
692 }
693
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100694 if (verify) {
695 sysfsfp = fopen(temp, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300696 if (!sysfsfp) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100697 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300698 fprintf(stderr, "failed to open %s\n", temp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100699 goto error_free;
700 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200701
Hartmut Knaack53118552015-05-31 14:40:14 +0200702 if (fscanf(sysfsfp, "%d", &test) != 1) {
703 ret = errno ? -errno : -ENODATA;
704 if (fclose(sysfsfp))
705 perror("_write_sysfs_int(): Failed to close dir");
706
707 goto error_free;
708 }
709
710 if (fclose(sysfsfp)) {
711 ret = -errno;
712 goto error_free;
713 }
714
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100715 if (test != val) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300716 fprintf(stderr,
717 "Possible failure in int write %d to %s/%s\n",
718 val, basedir, filename);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100719 ret = -1;
720 }
721 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200722
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100723error_free:
724 free(temp);
725 return ret;
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100726}
727
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200728/**
729 * write_sysfs_int() - write an integer value to a sysfs file
730 * @filename: name of the file to write to
731 * @basedir: the sysfs directory in which the file is to be found
732 * @val: integer value to write to file
733 *
734 * Returns a value >= 0 on success, otherwise a negative error code.
735 **/
Hartmut Knaack9d475252015-05-31 14:40:36 +0200736int write_sysfs_int(const char *filename, const char *basedir, int val)
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100737{
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100738 return _write_sysfs_int(filename, basedir, val, 0);
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100739}
740
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200741/**
742 * write_sysfs_int_and_verify() - write an integer value to a sysfs file
743 * and verify
744 * @filename: name of the file to write to
745 * @basedir: the sysfs directory in which the file is to be found
746 * @val: integer value to write to file
747 *
748 * Returns a value >= 0 on success, otherwise a negative error code.
749 **/
Hartmut Knaack9d475252015-05-31 14:40:36 +0200750int write_sysfs_int_and_verify(const char *filename, const char *basedir,
751 int val)
Jonathan Cameroneaf86ff2010-05-04 14:43:04 +0100752{
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100753 return _write_sysfs_int(filename, basedir, val, 1);
Jonathan Cameroneaf86ff2010-05-04 14:43:04 +0100754}
755
Hartmut Knaack9d475252015-05-31 14:40:36 +0200756static int _write_sysfs_string(const char *filename, const char *basedir,
757 const char *val, int verify)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100758{
Jonathan Camerone58537c2010-10-08 12:14:14 +0100759 int ret = 0;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100760 FILE *sysfsfp;
761 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300762
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300763 if (!temp) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300764 fprintf(stderr, "Memory allocation failed\n");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100765 return -ENOMEM;
766 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200767
Hartmut Knaack53118552015-05-31 14:40:14 +0200768 ret = sprintf(temp, "%s/%s", basedir, filename);
769 if (ret < 0)
770 goto error_free;
771
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100772 sysfsfp = fopen(temp, "w");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300773 if (!sysfsfp) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100774 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300775 fprintf(stderr, "Could not open %s\n", temp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100776 goto error_free;
777 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200778
Hartmut Knaack53118552015-05-31 14:40:14 +0200779 ret = fprintf(sysfsfp, "%s", val);
780 if (ret < 0) {
781 if (fclose(sysfsfp))
782 perror("_write_sysfs_string(): Failed to close dir");
783
784 goto error_free;
785 }
786
787 if (fclose(sysfsfp)) {
788 ret = -errno;
789 goto error_free;
790 }
791
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100792 if (verify) {
793 sysfsfp = fopen(temp, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300794 if (!sysfsfp) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100795 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300796 fprintf(stderr, "Could not open file to verify\n");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100797 goto error_free;
798 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200799
Hartmut Knaack53118552015-05-31 14:40:14 +0200800 if (fscanf(sysfsfp, "%s", temp) != 1) {
801 ret = errno ? -errno : -ENODATA;
802 if (fclose(sysfsfp))
803 perror("_write_sysfs_string(): Failed to close dir");
804
805 goto error_free;
806 }
807
808 if (fclose(sysfsfp)) {
809 ret = -errno;
810 goto error_free;
811 }
812
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100813 if (strcmp(temp, val) != 0) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300814 fprintf(stderr,
815 "Possible failure in string write of %s "
816 "Should be %s written to %s/%s\n", temp, val,
817 basedir, filename);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100818 ret = -1;
819 }
820 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200821
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100822error_free:
823 free(temp);
824
825 return ret;
826}
Jonathan Camerone58537c2010-10-08 12:14:14 +0100827
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100828/**
829 * write_sysfs_string_and_verify() - string write, readback and verify
830 * @filename: name of file to write to
831 * @basedir: the sysfs directory in which the file is to be found
832 * @val: the string to write
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200833 *
834 * Returns a value >= 0 on success, otherwise a negative error code.
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100835 **/
Hartmut Knaack9d475252015-05-31 14:40:36 +0200836int write_sysfs_string_and_verify(const char *filename, const char *basedir,
837 const char *val)
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100838{
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100839 return _write_sysfs_string(filename, basedir, val, 1);
840}
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100841
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200842/**
843 * write_sysfs_string() - write string to a sysfs file
844 * @filename: name of file to write to
845 * @basedir: the sysfs directory in which the file is to be found
846 * @val: the string to write
847 *
848 * Returns a value >= 0 on success, otherwise a negative error code.
849 **/
Hartmut Knaack9d475252015-05-31 14:40:36 +0200850int write_sysfs_string(const char *filename, const char *basedir,
851 const char *val)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100852{
853 return _write_sysfs_string(filename, basedir, val, 0);
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100854}
855
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200856/**
857 * read_sysfs_posint() - read an integer value from file
858 * @filename: name of file to read from
859 * @basedir: the sysfs directory in which the file is to be found
860 *
861 * Returns the read integer value >= 0 on success, otherwise a negative error
862 * code.
863 **/
Hartmut Knaack9d475252015-05-31 14:40:36 +0200864int read_sysfs_posint(const char *filename, const char *basedir)
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100865{
866 int ret;
867 FILE *sysfsfp;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100868 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300869
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300870 if (!temp) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300871 fprintf(stderr, "Memory allocation failed");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100872 return -ENOMEM;
873 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200874
Hartmut Knaack53118552015-05-31 14:40:14 +0200875 ret = sprintf(temp, "%s/%s", basedir, filename);
876 if (ret < 0)
877 goto error_free;
878
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100879 sysfsfp = fopen(temp, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300880 if (!sysfsfp) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100881 ret = -errno;
882 goto error_free;
883 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200884
Hartmut Knaack53118552015-05-31 14:40:14 +0200885 errno = 0;
886 if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
887 ret = errno ? -errno : -ENODATA;
888 if (fclose(sysfsfp))
889 perror("read_sysfs_posint(): Failed to close dir");
890
891 goto error_free;
892 }
893
894 if (fclose(sysfsfp))
895 ret = -errno;
896
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100897error_free:
898 free(temp);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200899
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100900 return ret;
901}
902
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200903/**
904 * read_sysfs_float() - read a float value from file
905 * @filename: name of file to read from
906 * @basedir: the sysfs directory in which the file is to be found
907 * @val: output the read float value
908 *
909 * Returns a value >= 0 on success, otherwise a negative error code.
910 **/
Hartmut Knaack9d475252015-05-31 14:40:36 +0200911int read_sysfs_float(const char *filename, const char *basedir, float *val)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100912{
Peter Meerwaldf5709d52014-12-06 06:00:00 +0000913 int ret = 0;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100914 FILE *sysfsfp;
915 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300916
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300917 if (!temp) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300918 fprintf(stderr, "Memory allocation failed");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100919 return -ENOMEM;
920 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200921
Hartmut Knaack53118552015-05-31 14:40:14 +0200922 ret = sprintf(temp, "%s/%s", basedir, filename);
923 if (ret < 0)
924 goto error_free;
925
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100926 sysfsfp = fopen(temp, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300927 if (!sysfsfp) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100928 ret = -errno;
929 goto error_free;
930 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200931
Hartmut Knaack53118552015-05-31 14:40:14 +0200932 errno = 0;
933 if (fscanf(sysfsfp, "%f\n", val) != 1) {
934 ret = errno ? -errno : -ENODATA;
935 if (fclose(sysfsfp))
936 perror("read_sysfs_float(): Failed to close dir");
937
938 goto error_free;
939 }
940
941 if (fclose(sysfsfp))
942 ret = -errno;
943
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100944error_free:
945 free(temp);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200946
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100947 return ret;
948}
Manuel Stahl49d916e2014-05-02 13:23:00 +0100949
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200950/**
951 * read_sysfs_string() - read a string from file
952 * @filename: name of file to read from
953 * @basedir: the sysfs directory in which the file is to be found
954 * @str: output the read string
955 *
956 * Returns a value >= 0 on success, otherwise a negative error code.
957 **/
Peter Meerwaldf5709d52014-12-06 06:00:00 +0000958int read_sysfs_string(const char *filename, const char *basedir, char *str)
Manuel Stahl49d916e2014-05-02 13:23:00 +0100959{
Peter Meerwaldf5709d52014-12-06 06:00:00 +0000960 int ret = 0;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100961 FILE *sysfsfp;
962 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300963
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300964 if (!temp) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300965 fprintf(stderr, "Memory allocation failed");
Manuel Stahl49d916e2014-05-02 13:23:00 +0100966 return -ENOMEM;
967 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200968
Hartmut Knaack53118552015-05-31 14:40:14 +0200969 ret = sprintf(temp, "%s/%s", basedir, filename);
970 if (ret < 0)
971 goto error_free;
972
Manuel Stahl49d916e2014-05-02 13:23:00 +0100973 sysfsfp = fopen(temp, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300974 if (!sysfsfp) {
Manuel Stahl49d916e2014-05-02 13:23:00 +0100975 ret = -errno;
976 goto error_free;
977 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200978
Hartmut Knaack53118552015-05-31 14:40:14 +0200979 errno = 0;
980 if (fscanf(sysfsfp, "%s\n", str) != 1) {
981 ret = errno ? -errno : -ENODATA;
982 if (fclose(sysfsfp))
983 perror("read_sysfs_string(): Failed to close dir");
984
985 goto error_free;
986 }
987
988 if (fclose(sysfsfp))
989 ret = -errno;
990
Manuel Stahl49d916e2014-05-02 13:23:00 +0100991error_free:
992 free(temp);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200993
Manuel Stahl49d916e2014-05-02 13:23:00 +0100994 return ret;
995}