1 /* IIO - useful set of util functionality
3 * Copyright (c) 2008 Jonathan Cameron
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.
18 /* Made up value to limit allocation sizes */
19 #define IIO_MAX_NAME_LENGTH 30
21 #define FORMAT_SCAN_ELEMENTS_DIR "%s/scan_elements"
22 #define FORMAT_TYPE_FILE "%s_type"
24 const char *iio_dir = "/sys/bus/iio/devices/";
27 * iioutils_break_up_name() - extract generic name from full channel name
28 * @full_name: the full channel name
29 * @generic_name: the output generic channel name
31 inline int iioutils_break_up_name(const char *full_name,
38 current = strdup(full_name);
39 working = strtok(current, "_\0");
51 *generic_name = strdup(working);
58 * struct iio_channel_info - information about a given channel
60 * @generic_name: general name for channel type
61 * @scale: scale factor to be applied for conversion to si units
62 * @offset: offset to be applied for conversion to si units
63 * @index: the channel index in the buffer output
64 * @bytes: number of bytes occupied in buffer output
65 * @mask: a bit mask for the raw output
66 * @is_signed: is the raw value stored signed
67 * @enabled: is this channel enabled
69 struct iio_channel_info {
85 * iioutils_get_type() - find and process _type attribute data
86 * @is_signed: output whether channel is signed
87 * @bytes: output how many bytes the channel storage occupies
88 * @mask: output a bit mask for the raw data
90 * @device_dir: the iio device directory
91 * @name: the channel name
92 * @generic_name: the channel type name
94 inline int iioutils_get_type(unsigned *is_signed,
100 const char *device_dir,
102 const char *generic_name)
107 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
108 char signchar, endianchar;
110 const struct dirent *ent;
112 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
117 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
120 goto error_free_scan_el_dir;
122 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
125 goto error_free_builtname;
128 dp = opendir(scan_el_dir);
131 goto error_free_builtname_generic;
133 while (ent = readdir(dp), ent != NULL)
135 * Do we allow devices to override a generic name with
138 if ((strcmp(builtname, ent->d_name) == 0) ||
139 (strcmp(builtname_generic, ent->d_name) == 0)) {
140 ret = asprintf(&filename,
141 "%s/%s", scan_el_dir, ent->d_name);
146 sysfsfp = fopen(filename, "r");
147 if (sysfsfp == NULL) {
148 printf("failed to open %s\n", filename);
150 goto error_free_filename;
153 ret = fscanf(sysfsfp,
160 printf("failed to pass scan type description\n");
162 goto error_close_sysfsfp;
164 *be = (endianchar == 'b');
166 if (*bits_used == 64)
169 *mask = (1 << *bits_used) - 1;
188 error_free_builtname_generic:
189 free(builtname_generic);
190 error_free_builtname:
192 error_free_scan_el_dir:
198 inline int iioutils_get_param_float(float *output,
199 const char *param_name,
200 const char *device_dir,
202 const char *generic_name)
207 char *builtname, *builtname_generic;
208 char *filename = NULL;
209 const struct dirent *ent;
211 ret = asprintf(&builtname, "%s_%s", name, param_name);
216 ret = asprintf(&builtname_generic,
217 "%s_%s", generic_name, param_name);
220 goto error_free_builtname;
222 dp = opendir(device_dir);
225 goto error_free_builtname_generic;
227 while (ent = readdir(dp), ent != NULL)
228 if ((strcmp(builtname, ent->d_name) == 0) ||
229 (strcmp(builtname_generic, ent->d_name) == 0)) {
230 ret = asprintf(&filename,
231 "%s/%s", device_dir, ent->d_name);
236 sysfsfp = fopen(filename, "r");
239 goto error_free_filename;
241 fscanf(sysfsfp, "%f", output);
249 error_free_builtname_generic:
250 free(builtname_generic);
251 error_free_builtname:
258 * bsort_channel_array_by_index() - reorder so that the array is in index order
262 inline void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
266 struct iio_channel_info temp;
269 for (x = 0; x < cnt; x++)
270 for (y = 0; y < (cnt - 1); y++)
271 if ((*ci_array)[y].index > (*ci_array)[y+1].index) {
272 temp = (*ci_array)[y + 1];
273 (*ci_array)[y + 1] = (*ci_array)[y];
274 (*ci_array)[y] = temp;
279 * build_channel_array() - function to figure out what channels are present
280 * @device_dir: the IIO device directory in sysfs
283 inline int build_channel_array(const char *device_dir,
284 struct iio_channel_info **ci_array,
290 struct iio_channel_info *current;
292 const struct dirent *ent;
297 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
302 dp = opendir(scan_el_dir);
305 goto error_free_name;
307 while (ent = readdir(dp), ent != NULL)
308 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
310 ret = asprintf(&filename,
311 "%s/%s", scan_el_dir, ent->d_name);
314 goto error_close_dir;
316 sysfsfp = fopen(filename, "r");
317 if (sysfsfp == NULL) {
320 goto error_close_dir;
322 fscanf(sysfsfp, "%i", &ret);
328 *ci_array = malloc(sizeof(**ci_array) * (*counter));
329 if (*ci_array == NULL) {
331 goto error_close_dir;
335 while (ent = readdir(dp), ent != NULL) {
336 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
338 int current_enabled = 0;
340 current = &(*ci_array)[count++];
341 ret = asprintf(&filename,
342 "%s/%s", scan_el_dir, ent->d_name);
345 /* decrement count to avoid freeing name */
347 goto error_cleanup_array;
349 sysfsfp = fopen(filename, "r");
350 if (sysfsfp == NULL) {
353 goto error_cleanup_array;
355 fscanf(sysfsfp, "%i", ¤t_enabled);
358 if (!current_enabled) {
364 current->scale = 1.0;
366 current->name = strndup(ent->d_name,
367 strlen(ent->d_name) -
369 if (current->name == NULL) {
372 goto error_cleanup_array;
374 /* Get the generic and specific name elements */
375 ret = iioutils_break_up_name(current->name,
376 ¤t->generic_name);
379 goto error_cleanup_array;
381 ret = asprintf(&filename,
388 goto error_cleanup_array;
390 sysfsfp = fopen(filename, "r");
391 fscanf(sysfsfp, "%u", ¤t->index);
395 ret = iioutils_get_param_float(¤t->scale,
399 current->generic_name);
401 goto error_cleanup_array;
402 ret = iioutils_get_param_float(¤t->offset,
406 current->generic_name);
408 goto error_cleanup_array;
409 ret = iioutils_get_type(¤t->is_signed,
417 current->generic_name);
422 /* reorder so that the array is in index order */
423 bsort_channel_array_by_index(ci_array, *counter);
428 for (i = count - 1; i >= 0; i--)
429 free((*ci_array)[i].name);
440 * find_type_by_name() - function to match top level types by name
441 * @name: top level type instance name
442 * @type: the type of top level instance being sort
444 * Typical types this is used for are device and trigger.
446 inline int find_type_by_name(const char *name, const char *type)
448 const struct dirent *ent;
449 int number, numstrlen;
453 char thisname[IIO_MAX_NAME_LENGTH];
456 dp = opendir(iio_dir);
458 printf("No industrialio devices available\n");
462 while (ent = readdir(dp), ent != NULL) {
463 if (strcmp(ent->d_name, ".") != 0 &&
464 strcmp(ent->d_name, "..") != 0 &&
465 strlen(ent->d_name) > strlen(type) &&
466 strncmp(ent->d_name, type, strlen(type)) == 0) {
467 numstrlen = sscanf(ent->d_name + strlen(type),
470 /* verify the next character is not a colon */
471 if (strncmp(ent->d_name + strlen(type) + numstrlen,
474 filename = malloc(strlen(iio_dir)
478 if (filename == NULL) {
482 sprintf(filename, "%s%s%d/name",
486 nameFile = fopen(filename, "r");
492 fscanf(nameFile, "%s", thisname);
494 if (strcmp(name, thisname) == 0) {
505 inline int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
510 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
514 sprintf(temp, "%s/%s", basedir, filename);
515 sysfsfp = fopen(temp, "w");
516 if (sysfsfp == NULL) {
517 printf("failed to open %s\n", temp);
521 fprintf(sysfsfp, "%d", val);
524 sysfsfp = fopen(temp, "r");
525 if (sysfsfp == NULL) {
526 printf("failed to open %s\n", temp);
530 fscanf(sysfsfp, "%d", &test);
533 printf("Possible failure in int write %d to %s%s\n",
545 int write_sysfs_int(char *filename, char *basedir, int val)
547 return _write_sysfs_int(filename, basedir, val, 0);
550 int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
552 return _write_sysfs_int(filename, basedir, val, 1);
555 int _write_sysfs_string(char *filename, char *basedir, char *val, int verify)
559 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
562 printf("Memory allocation failed\n");
565 sprintf(temp, "%s/%s", basedir, filename);
566 sysfsfp = fopen(temp, "w");
567 if (sysfsfp == NULL) {
568 printf("Could not open %s\n", temp);
572 fprintf(sysfsfp, "%s", val);
575 sysfsfp = fopen(temp, "r");
576 if (sysfsfp == NULL) {
577 printf("could not open file to verify\n");
581 fscanf(sysfsfp, "%s", temp);
583 if (strcmp(temp, val) != 0) {
584 printf("Possible failure in string write of %s "
586 "written to %s\%s\n",
601 * write_sysfs_string_and_verify() - string write, readback and verify
602 * @filename: name of file to write to
603 * @basedir: the sysfs directory in which the file is to be found
604 * @val: the string to write
606 int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
608 return _write_sysfs_string(filename, basedir, val, 1);
611 int write_sysfs_string(char *filename, char *basedir, char *val)
613 return _write_sysfs_string(filename, basedir, val, 0);
616 int read_sysfs_posint(char *filename, char *basedir)
620 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
623 printf("Memory allocation failed");
626 sprintf(temp, "%s/%s", basedir, filename);
627 sysfsfp = fopen(temp, "r");
628 if (sysfsfp == NULL) {
632 fscanf(sysfsfp, "%d\n", &ret);
639 int read_sysfs_float(char *filename, char *basedir, float *val)
643 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
646 printf("Memory allocation failed");
649 sprintf(temp, "%s/%s", basedir, filename);
650 sysfsfp = fopen(temp, "r");
651 if (sysfsfp == NULL) {
655 fscanf(sysfsfp, "%f\n", val);
662 int read_sysfs_string(const char *filename, const char *basedir, char *str)
666 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
669 printf("Memory allocation failed");
672 sprintf(temp, "%s/%s", basedir, filename);
673 sysfsfp = fopen(temp, "r");
674 if (sysfsfp == NULL) {
678 fscanf(sysfsfp, "%s\n", str);