blob: 7a6d61c6c0121f9c9cab0021602021c1eb44b8e6 [file] [log] [blame]
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +01001/* IIO - useful set of util functionality
2 *
3 * Copyright (c) 2008 Jonathan Cameron
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +01009#include <string.h>
10#include <stdlib.h>
Jonathan Camerone58537c2010-10-08 12:14:14 +010011#include <stdio.h>
12#include <stdint.h>
Lars-Peter Clausenbc9f35d2011-10-26 17:27:44 +010013#include <dirent.h>
Peter Meerwaldbb233782012-06-25 23:12:17 +020014#include <errno.h>
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +020015#include <ctype.h>
16#include "iio_utils.h"
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +010017
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +010018const char *iio_dir = "/sys/bus/iio/devices/";
19
Irina Tirdead9d7b992015-03-27 13:53:00 +020020static char * const iio_direction[] = {
21 "in",
22 "out",
23};
24
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +010025/**
Jonathan Camerone58537c2010-10-08 12:14:14 +010026 * iioutils_break_up_name() - extract generic name from full channel name
27 * @full_name: the full channel name
28 * @generic_name: the output generic channel name
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020029 *
30 * Returns 0 on success, or a negative error code if string extraction failed.
Jonathan Camerone58537c2010-10-08 12:14:14 +010031 **/
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020032int iioutils_break_up_name(const char *full_name, char **generic_name)
Jonathan Camerone58537c2010-10-08 12:14:14 +010033{
34 char *current;
35 char *w, *r;
Irina Tirdead9d7b992015-03-27 13:53:00 +020036 char *working, *prefix = "";
Hartmut Knaacke9e45b42015-05-31 14:40:02 +020037 int i, ret;
Melike Yurtoglu79bdd482014-10-03 23:35:54 +030038
Cristina Opriceana34cbea12015-07-13 16:20:11 +030039 for (i = 0; i < ARRAY_SIZE(iio_direction); i++)
Irina Tirdead9d7b992015-03-27 13:53:00 +020040 if (!strncmp(full_name, iio_direction[i],
41 strlen(iio_direction[i]))) {
42 prefix = iio_direction[i];
43 break;
44 }
45
46 current = strdup(full_name + strlen(prefix) + 1);
Hartmut Knaacke9e45b42015-05-31 14:40:02 +020047 if (!current)
48 return -ENOMEM;
49
Jonathan Camerone58537c2010-10-08 12:14:14 +010050 working = strtok(current, "_\0");
Hartmut Knaack53118552015-05-31 14:40:14 +020051 if (!working) {
52 free(current);
53 return -EINVAL;
54 }
Irina Tirdead9d7b992015-03-27 13:53:00 +020055
Jonathan Camerone58537c2010-10-08 12:14:14 +010056 w = working;
57 r = working;
58
Michael Hennerich8b68bb22011-03-08 08:55:48 +010059 while (*r != '\0') {
Jonathan Camerone58537c2010-10-08 12:14:14 +010060 if (!isdigit(*r)) {
61 *w = *r;
62 w++;
63 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020064
Jonathan Camerone58537c2010-10-08 12:14:14 +010065 r++;
66 }
67 *w = '\0';
Hartmut Knaacke9e45b42015-05-31 14:40:02 +020068 ret = asprintf(generic_name, "%s_%s", prefix, working);
Jonathan Camerone58537c2010-10-08 12:14:14 +010069 free(current);
70
Hartmut Knaacke9e45b42015-05-31 14:40:02 +020071 return (ret == -1) ? -ENOMEM : 0;
Jonathan Camerone58537c2010-10-08 12:14:14 +010072}
73
74/**
Jonathan Camerone58537c2010-10-08 12:14:14 +010075 * iioutils_get_type() - find and process _type attribute data
76 * @is_signed: output whether channel is signed
77 * @bytes: output how many bytes the channel storage occupies
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020078 * @bits_used: output number of valid bits of data
79 * @shift: output amount of bits to shift right data before applying bit mask
Jonathan Camerone58537c2010-10-08 12:14:14 +010080 * @mask: output a bit mask for the raw data
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020081 * @be: output if data in big endian
82 * @device_dir: the IIO device directory
Jonathan Camerone58537c2010-10-08 12:14:14 +010083 * @name: the channel name
84 * @generic_name: the channel type name
Hartmut Knaack5dc65d72015-05-31 14:40:16 +020085 *
86 * Returns a value >= 0 on success, otherwise a negative error code.
Jonathan Camerone58537c2010-10-08 12:14:14 +010087 **/
Hartmut Knaack7663a4a2015-06-10 21:51:20 +020088int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
89 unsigned *shift, uint64_t *mask, unsigned *be,
90 const char *device_dir, const char *name,
91 const char *generic_name)
Jonathan Camerone58537c2010-10-08 12:14:14 +010092{
93 FILE *sysfsfp;
94 int ret;
95 DIR *dp;
96 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
Jonathan Cameron117cf8b2011-12-04 19:10:59 +000097 char signchar, endianchar;
Michael Hennerichfc7f95a2011-02-24 16:34:54 +010098 unsigned padint;
Jonathan Camerone58537c2010-10-08 12:14:14 +010099 const struct dirent *ent;
100
101 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200102 if (ret < 0)
103 return -ENOMEM;
104
Jonathan Camerone58537c2010-10-08 12:14:14 +0100105 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
106 if (ret < 0) {
107 ret = -ENOMEM;
108 goto error_free_scan_el_dir;
109 }
110 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
111 if (ret < 0) {
112 ret = -ENOMEM;
113 goto error_free_builtname;
114 }
115
116 dp = opendir(scan_el_dir);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300117 if (!dp) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100118 ret = -errno;
119 goto error_free_builtname_generic;
120 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200121
Hartmut Knaack53118552015-05-31 14:40:14 +0200122 ret = -ENOENT;
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300123 while (ent = readdir(dp), ent)
Jonathan Camerone58537c2010-10-08 12:14:14 +0100124 if ((strcmp(builtname, ent->d_name) == 0) ||
125 (strcmp(builtname_generic, ent->d_name) == 0)) {
126 ret = asprintf(&filename,
127 "%s/%s", scan_el_dir, ent->d_name);
128 if (ret < 0) {
129 ret = -ENOMEM;
130 goto error_closedir;
131 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200132
Jonathan Camerone58537c2010-10-08 12:14:14 +0100133 sysfsfp = fopen(filename, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300134 if (!sysfsfp) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100135 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300136 fprintf(stderr, "failed to open %s\n",
137 filename);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100138 goto error_free_filename;
139 }
Jonathan Camerona7f7c362011-12-04 19:10:58 +0000140
141 ret = fscanf(sysfsfp,
142 "%ce:%c%u/%u>>%u",
143 &endianchar,
144 &signchar,
145 bits_used,
146 &padint, shift);
147 if (ret < 0) {
Peter Meerwald578f7372012-06-25 23:13:24 +0200148 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300149 fprintf(stderr,
150 "failed to pass scan type description\n");
Peter Meerwald578f7372012-06-25 23:13:24 +0200151 goto error_close_sysfsfp;
Hartmut Knaackdc8b5d62015-05-31 14:39:59 +0200152 } else if (ret != 5) {
153 ret = -EIO;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300154 fprintf(stderr,
155 "scan type description didn't match\n");
Hartmut Knaackdc8b5d62015-05-31 14:39:59 +0200156 goto error_close_sysfsfp;
Jonathan Camerona7f7c362011-12-04 19:10:58 +0000157 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200158
Jonathan Cameron117cf8b2011-12-04 19:10:59 +0000159 *be = (endianchar == 'b');
Jonathan Camerone58537c2010-10-08 12:14:14 +0100160 *bytes = padint / 8;
Michael Hennerichfc7f95a2011-02-24 16:34:54 +0100161 if (*bits_used == 64)
Jonathan Camerone58537c2010-10-08 12:14:14 +0100162 *mask = ~0;
163 else
Irina Tirdeaae067cb2015-07-24 16:28:05 +0300164 *mask = (1ULL << *bits_used) - 1;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200165
Hartmut Knaack33ebcb22015-05-31 14:40:19 +0200166 *is_signed = (signchar == 's');
Hartmut Knaack53118552015-05-31 14:40:14 +0200167 if (fclose(sysfsfp)) {
168 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300169 fprintf(stderr, "Failed to close %s\n",
170 filename);
Hartmut Knaack53118552015-05-31 14:40:14 +0200171 goto error_free_filename;
172 }
173
Hartmut Knaackace76e42015-05-31 14:40:20 +0200174 sysfsfp = 0;
Jonathan Camerona7f7c362011-12-04 19:10:58 +0000175 free(filename);
Jonathan Camerona7f7c362011-12-04 19:10:58 +0000176 filename = 0;
Matt Ranostay6356f1b2016-08-22 15:19:37 -0700177
178 /*
179 * Avoid having a more generic entry overwriting
180 * the settings.
181 */
182 if (strcmp(builtname, ent->d_name) == 0)
183 break;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100184 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200185
Peter Meerwald578f7372012-06-25 23:13:24 +0200186error_close_sysfsfp:
187 if (sysfsfp)
Hartmut Knaack53118552015-05-31 14:40:14 +0200188 if (fclose(sysfsfp))
189 perror("iioutils_get_type(): Failed to close file");
190
Jonathan Camerone58537c2010-10-08 12:14:14 +0100191error_free_filename:
192 if (filename)
193 free(filename);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200194
Jonathan Camerone58537c2010-10-08 12:14:14 +0100195error_closedir:
Hartmut Knaack53118552015-05-31 14:40:14 +0200196 if (closedir(dp) == -1)
197 perror("iioutils_get_type(): Failed to close directory");
198
Jonathan Camerone58537c2010-10-08 12:14:14 +0100199error_free_builtname_generic:
200 free(builtname_generic);
201error_free_builtname:
202 free(builtname);
203error_free_scan_el_dir:
204 free(scan_el_dir);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200205
Jonathan Camerone58537c2010-10-08 12:14:14 +0100206 return ret;
207}
208
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200209/**
210 * iioutils_get_param_float() - read a float value from a channel parameter
211 * @output: output the float value
212 * @param_name: the parameter name to read
213 * @device_dir: the IIO device directory in sysfs
214 * @name: the channel name
215 * @generic_name: the channel type name
216 *
217 * Returns a value >= 0 on success, otherwise a negative error code.
218 **/
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200219int iioutils_get_param_float(float *output, const char *param_name,
220 const char *device_dir, const char *name,
221 const char *generic_name)
Jonathan Camerone58537c2010-10-08 12:14:14 +0100222{
223 FILE *sysfsfp;
224 int ret;
225 DIR *dp;
226 char *builtname, *builtname_generic;
227 char *filename = NULL;
228 const struct dirent *ent;
229
230 ret = asprintf(&builtname, "%s_%s", name, param_name);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200231 if (ret < 0)
232 return -ENOMEM;
233
Jonathan Camerone58537c2010-10-08 12:14:14 +0100234 ret = asprintf(&builtname_generic,
235 "%s_%s", generic_name, param_name);
236 if (ret < 0) {
237 ret = -ENOMEM;
238 goto error_free_builtname;
239 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200240
Jonathan Camerone58537c2010-10-08 12:14:14 +0100241 dp = opendir(device_dir);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300242 if (!dp) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100243 ret = -errno;
244 goto error_free_builtname_generic;
245 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200246
Hartmut Knaack53118552015-05-31 14:40:14 +0200247 ret = -ENOENT;
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300248 while (ent = readdir(dp), ent)
Jonathan Camerone58537c2010-10-08 12:14:14 +0100249 if ((strcmp(builtname, ent->d_name) == 0) ||
250 (strcmp(builtname_generic, ent->d_name) == 0)) {
251 ret = asprintf(&filename,
252 "%s/%s", device_dir, ent->d_name);
253 if (ret < 0) {
254 ret = -ENOMEM;
255 goto error_closedir;
256 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200257
Jonathan Camerone58537c2010-10-08 12:14:14 +0100258 sysfsfp = fopen(filename, "r");
259 if (!sysfsfp) {
260 ret = -errno;
261 goto error_free_filename;
262 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200263
Hartmut Knaack53118552015-05-31 14:40:14 +0200264 errno = 0;
265 if (fscanf(sysfsfp, "%f", output) != 1)
266 ret = errno ? -errno : -ENODATA;
267
Jonathan Camerone58537c2010-10-08 12:14:14 +0100268 break;
269 }
270error_free_filename:
271 if (filename)
272 free(filename);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200273
Jonathan Camerone58537c2010-10-08 12:14:14 +0100274error_closedir:
Hartmut Knaack53118552015-05-31 14:40:14 +0200275 if (closedir(dp) == -1)
276 perror("iioutils_get_param_float(): Failed to close directory");
277
Jonathan Camerone58537c2010-10-08 12:14:14 +0100278error_free_builtname_generic:
279 free(builtname_generic);
280error_free_builtname:
281 free(builtname);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200282
Jonathan Camerone58537c2010-10-08 12:14:14 +0100283 return ret;
284}
285
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100286/**
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200287 * bsort_channel_array_by_index() - sort the array in index order
288 * @ci_array: the iio_channel_info array to be sorted
289 * @cnt: the amount of array elements
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100290 **/
291
Joo Aun Saw95ddd3f2015-07-25 01:23:29 +1000292void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt)
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100293{
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100294 struct iio_channel_info temp;
295 int x, y;
296
297 for (x = 0; x < cnt; x++)
298 for (y = 0; y < (cnt - 1); y++)
Joo Aun Saw95ddd3f2015-07-25 01:23:29 +1000299 if (ci_array[y].index > ci_array[y + 1].index) {
300 temp = ci_array[y + 1];
301 ci_array[y + 1] = ci_array[y];
302 ci_array[y] = temp;
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100303 }
304}
Jonathan Camerone58537c2010-10-08 12:14:14 +0100305
306/**
307 * build_channel_array() - function to figure out what channels are present
308 * @device_dir: the IIO device directory in sysfs
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 **/
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +0200314int build_channel_array(const char *device_dir,
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;
327 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
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,
508 current->name,
509 current->generic_name);
Hartmut Knaack53118552015-05-31 14:40:14 +0200510 if (ret < 0)
511 goto error_cleanup_array;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100512 }
513 }
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100514
Hartmut Knaack53118552015-05-31 14:40:14 +0200515 if (closedir(dp) == -1) {
516 ret = -errno;
517 goto error_cleanup_array;
518 }
519
Hartmut Knaack66dd08f2015-05-31 14:39:43 +0200520 free(scan_el_dir);
Michael Hennerich8b68bb22011-03-08 08:55:48 +0100521 /* reorder so that the array is in index order */
Joo Aun Saw95ddd3f2015-07-25 01:23:29 +1000522 bsort_channel_array_by_index(*ci_array, *counter);
Jonathan Camerone58537c2010-10-08 12:14:14 +0100523
524 return 0;
525
526error_cleanup_array:
Hartmut Knaack63f05c82015-05-31 14:39:44 +0200527 for (i = count - 1; i >= 0; i--) {
Jonathan Camerone58537c2010-10-08 12:14:14 +0100528 free((*ci_array)[i].name);
Hartmut Knaack63f05c82015-05-31 14:39:44 +0200529 free((*ci_array)[i].generic_name);
530 }
Jonathan Camerone58537c2010-10-08 12:14:14 +0100531 free(*ci_array);
Joo Aun Saw6b20f402015-07-25 01:23:28 +1000532 *ci_array = NULL;
533 *counter = 0;
Jonathan Camerone58537c2010-10-08 12:14:14 +0100534error_close_dir:
Hartmut Knaack53118552015-05-31 14:40:14 +0200535 if (dp)
536 if (closedir(dp) == -1)
537 perror("build_channel_array(): Failed to close dir");
538
Jonathan Camerone58537c2010-10-08 12:14:14 +0100539error_free_name:
540 free(scan_el_dir);
Hartmut Knaack0e799872015-05-31 14:40:17 +0200541
Jonathan Camerone58537c2010-10-08 12:14:14 +0100542 return ret;
543}
544
Joo Aun Saw5e37c522015-07-28 11:46:13 +1000545static int calc_digits(int num)
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200546{
547 int count = 0;
548
549 while (num != 0) {
550 num /= 10;
551 count++;
552 }
553
554 return count;
555}
556
Jonathan Camerone58537c2010-10-08 12:14:14 +0100557/**
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100558 * find_type_by_name() - function to match top level types by name
559 * @name: top level type instance name
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200560 * @type: the type of top level instance being searched
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100561 *
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200562 * Returns the device number of a matched IIO device on success, otherwise a
563 * negative error code.
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100564 * Typical types this is used for are device and trigger.
565 **/
Roberta Dobrescubdcb31d2015-02-26 10:49:24 +0200566int find_type_by_name(const char *name, const char *type)
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100567{
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100568 const struct dirent *ent;
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200569 int number, numstrlen, ret;
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100570
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +0200571 FILE *namefp;
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100572 DIR *dp;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100573 char thisname[IIO_MAX_NAME_LENGTH];
574 char *filename;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100575
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100576 dp = opendir(iio_dir);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300577 if (!dp) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300578 fprintf(stderr, "No industrialio devices available\n");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100579 return -ENODEV;
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100580 }
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100581
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300582 while (ent = readdir(dp), ent) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100583 if (strcmp(ent->d_name, ".") != 0 &&
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200584 strcmp(ent->d_name, "..") != 0 &&
585 strlen(ent->d_name) > strlen(type) &&
586 strncmp(ent->d_name, type, strlen(type)) == 0) {
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200587 errno = 0;
588 ret = sscanf(ent->d_name + strlen(type), "%d", &number);
589 if (ret < 0) {
590 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300591 fprintf(stderr,
592 "failed to read element number\n");
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200593 goto error_close_dir;
594 } else if (ret != 1) {
595 ret = -EIO;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300596 fprintf(stderr,
597 "failed to match element number\n");
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200598 goto error_close_dir;
599 }
600
601 numstrlen = calc_digits(number);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100602 /* verify the next character is not a colon */
603 if (strncmp(ent->d_name + strlen(type) + numstrlen,
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200604 ":", 1) != 0) {
605 filename = malloc(strlen(iio_dir) + strlen(type)
606 + numstrlen + 6);
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300607 if (!filename) {
Hartmut Knaack53118552015-05-31 14:40:14 +0200608 ret = -ENOMEM;
609 goto error_close_dir;
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200610 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200611
612 ret = sprintf(filename, "%s%s%d/name", iio_dir,
613 type, number);
614 if (ret < 0) {
615 free(filename);
616 goto error_close_dir;
617 }
618
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +0200619 namefp = fopen(filename, "r");
620 if (!namefp) {
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200621 free(filename);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100622 continue;
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200623 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200624
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100625 free(filename);
Hartmut Knaack53118552015-05-31 14:40:14 +0200626 errno = 0;
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +0200627 if (fscanf(namefp, "%s", thisname) != 1) {
Hartmut Knaack53118552015-05-31 14:40:14 +0200628 ret = errno ? -errno : -ENODATA;
629 goto error_close_dir;
630 }
631
Hartmut Knaacka9d7acc2015-06-10 21:51:21 +0200632 if (fclose(namefp)) {
Hartmut Knaack53118552015-05-31 14:40:14 +0200633 ret = -errno;
634 goto error_close_dir;
635 }
636
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200637 if (strcmp(name, thisname) == 0) {
Hartmut Knaack53118552015-05-31 14:40:14 +0200638 if (closedir(dp) == -1)
639 return -errno;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200640
Peter Meerwalda4d429e2012-06-25 23:13:25 +0200641 return number;
642 }
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100643 }
644 }
645 }
Hartmut Knaack53118552015-05-31 14:40:14 +0200646 if (closedir(dp) == -1)
647 return -errno;
648
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100649 return -ENODEV;
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200650
651error_close_dir:
652 if (closedir(dp) == -1)
653 perror("find_type_by_name(): Failed to close directory");
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200654
Hartmut Knaack096f9b82015-05-31 14:40:00 +0200655 return ret;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100656}
657
Hartmut Knaack9d475252015-05-31 14:40:36 +0200658static int _write_sysfs_int(const char *filename, const char *basedir, int val,
659 int verify)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100660{
Sebastian Andrzej Siewior11cb4542013-10-07 13:42:00 +0100661 int ret = 0;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100662 FILE *sysfsfp;
663 int test;
664 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300665
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300666 if (!temp)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100667 return -ENOMEM;
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200668
Hartmut Knaack53118552015-05-31 14:40:14 +0200669 ret = sprintf(temp, "%s/%s", basedir, filename);
670 if (ret < 0)
671 goto error_free;
672
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100673 sysfsfp = fopen(temp, "w");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300674 if (!sysfsfp) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100675 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300676 fprintf(stderr, "failed to open %s\n", temp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100677 goto error_free;
678 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200679
Hartmut Knaack53118552015-05-31 14:40:14 +0200680 ret = fprintf(sysfsfp, "%d", val);
681 if (ret < 0) {
682 if (fclose(sysfsfp))
683 perror("_write_sysfs_int(): Failed to close dir");
684
685 goto error_free;
686 }
687
688 if (fclose(sysfsfp)) {
689 ret = -errno;
690 goto error_free;
691 }
692
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100693 if (verify) {
694 sysfsfp = fopen(temp, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300695 if (!sysfsfp) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100696 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300697 fprintf(stderr, "failed to open %s\n", temp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100698 goto error_free;
699 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200700
Hartmut Knaack53118552015-05-31 14:40:14 +0200701 if (fscanf(sysfsfp, "%d", &test) != 1) {
702 ret = errno ? -errno : -ENODATA;
703 if (fclose(sysfsfp))
704 perror("_write_sysfs_int(): Failed to close dir");
705
706 goto error_free;
707 }
708
709 if (fclose(sysfsfp)) {
710 ret = -errno;
711 goto error_free;
712 }
713
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100714 if (test != val) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300715 fprintf(stderr,
716 "Possible failure in int write %d to %s/%s\n",
717 val, basedir, filename);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100718 ret = -1;
719 }
720 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200721
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100722error_free:
723 free(temp);
724 return ret;
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100725}
726
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200727/**
728 * write_sysfs_int() - write an integer value to a sysfs file
729 * @filename: name of the file to write to
730 * @basedir: the sysfs directory in which the file is to be found
731 * @val: integer value to write to file
732 *
733 * Returns a value >= 0 on success, otherwise a negative error code.
734 **/
Hartmut Knaack9d475252015-05-31 14:40:36 +0200735int write_sysfs_int(const char *filename, const char *basedir, int val)
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100736{
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100737 return _write_sysfs_int(filename, basedir, val, 0);
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100738}
739
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200740/**
741 * write_sysfs_int_and_verify() - write an integer value to a sysfs file
742 * and verify
743 * @filename: name of the file to write to
744 * @basedir: the sysfs directory in which the file is to be found
745 * @val: integer value to write to file
746 *
747 * Returns a value >= 0 on success, otherwise a negative error code.
748 **/
Hartmut Knaack9d475252015-05-31 14:40:36 +0200749int write_sysfs_int_and_verify(const char *filename, const char *basedir,
750 int val)
Jonathan Cameroneaf86ff2010-05-04 14:43:04 +0100751{
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100752 return _write_sysfs_int(filename, basedir, val, 1);
Jonathan Cameroneaf86ff2010-05-04 14:43:04 +0100753}
754
Hartmut Knaack9d475252015-05-31 14:40:36 +0200755static int _write_sysfs_string(const char *filename, const char *basedir,
756 const char *val, int verify)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100757{
Jonathan Camerone58537c2010-10-08 12:14:14 +0100758 int ret = 0;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100759 FILE *sysfsfp;
760 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300761
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300762 if (!temp) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300763 fprintf(stderr, "Memory allocation failed\n");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100764 return -ENOMEM;
765 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200766
Hartmut Knaack53118552015-05-31 14:40:14 +0200767 ret = sprintf(temp, "%s/%s", basedir, filename);
768 if (ret < 0)
769 goto error_free;
770
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100771 sysfsfp = fopen(temp, "w");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300772 if (!sysfsfp) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100773 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300774 fprintf(stderr, "Could not open %s\n", temp);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100775 goto error_free;
776 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200777
Hartmut Knaack53118552015-05-31 14:40:14 +0200778 ret = fprintf(sysfsfp, "%s", val);
779 if (ret < 0) {
780 if (fclose(sysfsfp))
781 perror("_write_sysfs_string(): Failed to close dir");
782
783 goto error_free;
784 }
785
786 if (fclose(sysfsfp)) {
787 ret = -errno;
788 goto error_free;
789 }
790
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100791 if (verify) {
792 sysfsfp = fopen(temp, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300793 if (!sysfsfp) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100794 ret = -errno;
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300795 fprintf(stderr, "Could not open file to verify\n");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100796 goto error_free;
797 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200798
Hartmut Knaack53118552015-05-31 14:40:14 +0200799 if (fscanf(sysfsfp, "%s", temp) != 1) {
800 ret = errno ? -errno : -ENODATA;
801 if (fclose(sysfsfp))
802 perror("_write_sysfs_string(): Failed to close dir");
803
804 goto error_free;
805 }
806
807 if (fclose(sysfsfp)) {
808 ret = -errno;
809 goto error_free;
810 }
811
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100812 if (strcmp(temp, val) != 0) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300813 fprintf(stderr,
814 "Possible failure in string write of %s "
815 "Should be %s written to %s/%s\n", temp, val,
816 basedir, filename);
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100817 ret = -1;
818 }
819 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200820
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100821error_free:
822 free(temp);
823
824 return ret;
825}
Jonathan Camerone58537c2010-10-08 12:14:14 +0100826
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100827/**
828 * write_sysfs_string_and_verify() - string write, readback and verify
829 * @filename: name of file to write to
830 * @basedir: the sysfs directory in which the file is to be found
831 * @val: the string to write
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200832 *
833 * Returns a value >= 0 on success, otherwise a negative error code.
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100834 **/
Hartmut Knaack9d475252015-05-31 14:40:36 +0200835int write_sysfs_string_and_verify(const char *filename, const char *basedir,
836 const char *val)
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100837{
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100838 return _write_sysfs_string(filename, basedir, val, 1);
839}
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100840
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200841/**
842 * write_sysfs_string() - write string to a sysfs file
843 * @filename: name of file to write to
844 * @basedir: the sysfs directory in which the file is to be found
845 * @val: the string to write
846 *
847 * Returns a value >= 0 on success, otherwise a negative error code.
848 **/
Hartmut Knaack9d475252015-05-31 14:40:36 +0200849int write_sysfs_string(const char *filename, const char *basedir,
850 const char *val)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100851{
852 return _write_sysfs_string(filename, basedir, val, 0);
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100853}
854
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200855/**
856 * read_sysfs_posint() - read an integer value from file
857 * @filename: name of file to read from
858 * @basedir: the sysfs directory in which the file is to be found
859 *
860 * Returns the read integer value >= 0 on success, otherwise a negative error
861 * code.
862 **/
Hartmut Knaack9d475252015-05-31 14:40:36 +0200863int read_sysfs_posint(const char *filename, const char *basedir)
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100864{
865 int ret;
866 FILE *sysfsfp;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100867 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300868
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300869 if (!temp) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300870 fprintf(stderr, "Memory allocation failed");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100871 return -ENOMEM;
872 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200873
Hartmut Knaack53118552015-05-31 14:40:14 +0200874 ret = sprintf(temp, "%s/%s", basedir, filename);
875 if (ret < 0)
876 goto error_free;
877
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100878 sysfsfp = fopen(temp, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300879 if (!sysfsfp) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100880 ret = -errno;
881 goto error_free;
882 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200883
Hartmut Knaack53118552015-05-31 14:40:14 +0200884 errno = 0;
885 if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
886 ret = errno ? -errno : -ENODATA;
887 if (fclose(sysfsfp))
888 perror("read_sysfs_posint(): Failed to close dir");
889
890 goto error_free;
891 }
892
893 if (fclose(sysfsfp))
894 ret = -errno;
895
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100896error_free:
897 free(temp);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200898
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100899 return ret;
900}
901
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200902/**
903 * read_sysfs_float() - read a float value from file
904 * @filename: name of file to read from
905 * @basedir: the sysfs directory in which the file is to be found
906 * @val: output the read float value
907 *
908 * Returns a value >= 0 on success, otherwise a negative error code.
909 **/
Hartmut Knaack9d475252015-05-31 14:40:36 +0200910int read_sysfs_float(const char *filename, const char *basedir, float *val)
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100911{
Peter Meerwaldf5709d52014-12-06 06:00:00 +0000912 int ret = 0;
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100913 FILE *sysfsfp;
914 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300915
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300916 if (!temp) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300917 fprintf(stderr, "Memory allocation failed");
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100918 return -ENOMEM;
919 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200920
Hartmut Knaack53118552015-05-31 14:40:14 +0200921 ret = sprintf(temp, "%s/%s", basedir, filename);
922 if (ret < 0)
923 goto error_free;
924
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100925 sysfsfp = fopen(temp, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300926 if (!sysfsfp) {
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100927 ret = -errno;
928 goto error_free;
929 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200930
Hartmut Knaack53118552015-05-31 14:40:14 +0200931 errno = 0;
932 if (fscanf(sysfsfp, "%f\n", val) != 1) {
933 ret = errno ? -errno : -ENODATA;
934 if (fclose(sysfsfp))
935 perror("read_sysfs_float(): Failed to close dir");
936
937 goto error_free;
938 }
939
940 if (fclose(sysfsfp))
941 ret = -errno;
942
Jonathan Cameron9d8ae6c2010-05-04 14:43:13 +0100943error_free:
944 free(temp);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200945
Jonathan Cameronc57f1ba2009-08-18 18:06:32 +0100946 return ret;
947}
Manuel Stahl49d916e2014-05-02 13:23:00 +0100948
Hartmut Knaack5dc65d72015-05-31 14:40:16 +0200949/**
950 * read_sysfs_string() - read a string from file
951 * @filename: name of file to read from
952 * @basedir: the sysfs directory in which the file is to be found
953 * @str: output the read string
954 *
955 * Returns a value >= 0 on success, otherwise a negative error code.
956 **/
Peter Meerwaldf5709d52014-12-06 06:00:00 +0000957int read_sysfs_string(const char *filename, const char *basedir, char *str)
Manuel Stahl49d916e2014-05-02 13:23:00 +0100958{
Peter Meerwaldf5709d52014-12-06 06:00:00 +0000959 int ret = 0;
Manuel Stahl49d916e2014-05-02 13:23:00 +0100960 FILE *sysfsfp;
961 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
Melike Yurtoglu79bdd482014-10-03 23:35:54 +0300962
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300963 if (!temp) {
Cristina Opriceanad9abc612015-07-17 18:43:42 +0300964 fprintf(stderr, "Memory allocation failed");
Manuel Stahl49d916e2014-05-02 13:23:00 +0100965 return -ENOMEM;
966 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200967
Hartmut Knaack53118552015-05-31 14:40:14 +0200968 ret = sprintf(temp, "%s/%s", basedir, filename);
969 if (ret < 0)
970 goto error_free;
971
Manuel Stahl49d916e2014-05-02 13:23:00 +0100972 sysfsfp = fopen(temp, "r");
Cristina Opriceanaff1ac632015-07-13 16:15:56 +0300973 if (!sysfsfp) {
Manuel Stahl49d916e2014-05-02 13:23:00 +0100974 ret = -errno;
975 goto error_free;
976 }
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200977
Hartmut Knaack53118552015-05-31 14:40:14 +0200978 errno = 0;
979 if (fscanf(sysfsfp, "%s\n", str) != 1) {
980 ret = errno ? -errno : -ENODATA;
981 if (fclose(sysfsfp))
982 perror("read_sysfs_string(): Failed to close dir");
983
984 goto error_free;
985 }
986
987 if (fclose(sysfsfp))
988 ret = -errno;
989
Manuel Stahl49d916e2014-05-02 13:23:00 +0100990error_free:
991 free(temp);
Hartmut Knaack7663a4a2015-06-10 21:51:20 +0200992
Manuel Stahl49d916e2014-05-02 13:23:00 +0100993 return ret;
994}