aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/iio/Documentation/iio_utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/staging/iio/Documentation/iio_utils.h')
-rw-r--r--drivers/staging/iio/Documentation/iio_utils.h63
1 files changed, 37 insertions, 26 deletions
diff --git a/drivers/staging/iio/Documentation/iio_utils.h b/drivers/staging/iio/Documentation/iio_utils.h
index 0372424..8095727 100644
--- a/drivers/staging/iio/Documentation/iio_utils.h
+++ b/drivers/staging/iio/Documentation/iio_utils.h
@@ -51,7 +51,7 @@ static int iioutils_break_up_name(const char *full_name,
w = working;
r = working;
- while(*r != '\0') {
+ while (*r != '\0') {
if (!isdigit(*r)) {
*w = *r;
w++;
@@ -113,7 +113,7 @@ inline int iioutils_get_type(unsigned *is_signed,
DIR *dp;
char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
char signchar;
- unsigned sizeint, padint;
+ unsigned padint;
const struct dirent *ent;
ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
@@ -159,7 +159,7 @@ inline int iioutils_get_type(unsigned *is_signed,
fscanf(sysfsfp,
"%c%u/%u", &signchar, bits_used, &padint);
*bytes = padint / 8;
- if (sizeint == 64)
+ if (*bits_used == 64)
*mask = ~0;
else
*mask = (1 << *bits_used) - 1;
@@ -242,6 +242,26 @@ error_ret:
return ret;
}
+/**
+ * bsort_channel_array_by_index() - reorder so that the array is in index order
+ *
+ **/
+
+inline void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
+ int cnt)
+{
+
+ struct iio_channel_info temp;
+ int x, y;
+
+ for (x = 0; x < cnt; x++)
+ for (y = 0; y < (cnt - 1); y++)
+ if ((*ci_array)[y].index > (*ci_array)[y+1].index) {
+ temp = (*ci_array)[y + 1];
+ (*ci_array)[y + 1] = (*ci_array)[y];
+ (*ci_array)[y] = temp;
+ }
+}
/**
* build_channel_array() - function to figure out what channels are present
@@ -254,7 +274,7 @@ inline int build_channel_array(const char *device_dir,
{
DIR *dp;
FILE *sysfsfp;
- int count = 0, temp, i;
+ int count, temp, i;
struct iio_channel_info *current;
int ret;
const struct dirent *ent;
@@ -293,12 +313,13 @@ inline int build_channel_array(const char *device_dir,
fclose(sysfsfp);
free(filename);
}
- *ci_array = malloc(sizeof(**ci_array)*(*counter));
+ *ci_array = malloc(sizeof(**ci_array) * (*counter));
if (*ci_array == NULL) {
ret = -ENOMEM;
goto error_close_dir;
}
seekdir(dp, 0);
+ count = 0;
while (ent = readdir(dp), ent != NULL) {
if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
"_en") == 0) {
@@ -319,7 +340,13 @@ inline int build_channel_array(const char *device_dir,
}
fscanf(sysfsfp, "%u", &current->enabled);
fclose(sysfsfp);
- free(filename);
+
+ if (!current->enabled) {
+ free(filename);
+ count--;
+ continue;
+ }
+
current->scale = 1.0;
current->offset = 0;
current->name = strndup(ent->d_name,
@@ -374,31 +401,15 @@ inline int build_channel_array(const char *device_dir,
current->generic_name);
}
}
- /* reorder so that the array is in index order*/
- current = malloc(sizeof(**ci_array)**counter);
- if (current == NULL) {
- ret = -ENOMEM;
- goto error_cleanup_array;
- }
+
closedir(dp);
- count = 0;
- temp = 0;
- while (count < *counter)
- for (i = 0; i < *counter; i++)
- if ((*ci_array)[i].index == temp) {
- memcpy(&current[count++],
- &(*ci_array)[i],
- sizeof(*current));
- temp++;
- break;
- }
- free(*ci_array);
- *ci_array = current;
+ /* reorder so that the array is in index order */
+ bsort_channel_array_by_index(ci_array, *counter);
return 0;
error_cleanup_array:
- for (i = count - 1; i >= 0; i++)
+ for (i = count - 1; i >= 0; i--)
free((*ci_array)[i].name);
free(*ci_array);
error_close_dir: