diff options
author | Russell King <rmk+kernel@arm.linux.org.uk> | 2009-09-22 19:54:53 (GMT) |
---|---|---|
committer | Russell King <rmk+kernel@arm.linux.org.uk> | 2009-09-22 20:01:40 (GMT) |
commit | ae19ffbadc1b2100285a5b5b3d0a4e0a11390904 (patch) | |
tree | 3c2086ab67398a019089a47ca3f362a4bc6db74f /drivers/staging/iio/Documentation/iio_utils.h | |
parent | 34e84f39a27d059a3e6ec6e8b94aafa702e6f220 (diff) | |
parent | 9173a8ef24a6b1b8031507b35b8ffe5f85a87692 (diff) | |
download | linux-fsl-qoriq-ae19ffbadc1b2100285a5b5b3d0a4e0a11390904.tar.xz |
Merge branch 'master' into for-linus
Diffstat (limited to 'drivers/staging/iio/Documentation/iio_utils.h')
-rw-r--r-- | drivers/staging/iio/Documentation/iio_utils.h | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/drivers/staging/iio/Documentation/iio_utils.h b/drivers/staging/iio/Documentation/iio_utils.h new file mode 100644 index 0000000..74d3124 --- /dev/null +++ b/drivers/staging/iio/Documentation/iio_utils.h @@ -0,0 +1,159 @@ +/* IIO - useful set of util functionality + * + * Copyright (c) 2008 Jonathan Cameron + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + */ + +#define IIO_EVENT_CODE_RING_50_FULL 200 +#define IIO_EVENT_CODE_RING_75_FULL 201 +#define IIO_EVENT_CODE_RING_100_FULL 202 + +struct iio_event_data { + int id; + __s64 timestamp; +}; + + +inline char *find_ring_subelement(const char *directory, const char *subelement) +{ + DIR *dp; + const struct dirent *ent; + int pos; + char temp[100]; + char *returnstring; + dp = opendir(directory); + if (dp == NULL) { + printf("could not directory: %s\n", directory); + return NULL; + } + while (ent = readdir(dp), ent != NULL) { + if (strcmp(ent->d_name, ".") != 0 && + strcmp(ent->d_name, "..") != 0) { + if (strncmp(ent->d_name, subelement, strlen(subelement)) == 0) { + int length = sprintf(temp, "%s%s%s", directory, ent->d_name, "/"); + returnstring = malloc(length+1); + strncpy(returnstring, temp, length+1); + return returnstring; + + } + } + } + return 0; +} + + +char *find_type_by_name(const char *name, const char *type) +{ + const char *iio_dir = "/sys/class/iio/"; + const struct dirent *ent; + int cnt, pos, pos2; + + FILE *nameFile; + DIR *dp; + char thisname[100]; + char temp[100]; + + char *returnstring = NULL; + struct stat Stat; + pos = sprintf(temp, "%s", iio_dir); + dp = opendir(iio_dir); + if (dp == NULL) { + printf("No industrialio devices available"); + return NULL; + } + while (ent = readdir(dp), ent != NULL) { + cnt++; + /*reject . and .. */ + if (strcmp(ent->d_name, ".") != 0 && + strcmp(ent->d_name, "..") != 0) { + /*make sure it isn't a trigger!*/ + if (strncmp(ent->d_name, type, strlen(type)) == 0) { + /* build full path to new file */ + pos2 = pos + sprintf(temp + pos, "%s/", ent->d_name); + sprintf(temp + pos2, "name"); + printf("search location %s\n", temp); + nameFile = fopen(temp, "r"); + if (!nameFile) { + sprintf(temp + pos2, "modalias", ent->d_name); + nameFile = fopen(temp, "r"); + if (!nameFile) { + printf("Failed to find a name for device\n"); + return NULL; + } + } + fscanf(nameFile, "%s", thisname); + if (strcmp(name, thisname) == 0) { + returnstring = malloc(strlen(temp) + 1); + sprintf(temp + pos2, ""); + strcpy(returnstring, temp); + return returnstring; + } + fclose(nameFile); + + } + } + } +} + +int write_sysfs_int(char *filename, char *basedir, int val) +{ + int ret; + FILE *sysfsfp; + char temp[100]; + sprintf(temp, "%s%s", basedir, filename); + sysfsfp = fopen(temp, "w"); + if (sysfsfp == NULL) + return -1; + fprintf(sysfsfp, "%d", val); + fclose(sysfsfp); + return 0; +} + +/** + * write_sysfs_string_and_verify() - string write, readback and verify + * @filename: name of file to write to + * @basedir: the sysfs directory in which the file is to be found + * @val: the string to write + **/ +int write_sysfs_string_and_verify(char *filename, char *basedir, char *val) +{ + int ret; + FILE *sysfsfp; + char temp[100]; + sprintf(temp, "%s%s", basedir, filename); + sysfsfp = fopen(temp, "w"); + if (sysfsfp == NULL) + return -1; + fprintf(sysfsfp, "%s", val); + fclose(sysfsfp); + + sysfsfp = fopen(temp, "r"); + if (sysfsfp == NULL) + return -1; + fscanf(sysfsfp, "%s", temp); + if (strcmp(temp, val) != 0) { + printf("Possible failure in string write %s to %s%s \n", + val, + basedir, + filename); + return -1; + } + return 0; +} + +int read_sysfs_posint(char *filename, char *basedir) +{ + int ret; + FILE *sysfsfp; + char temp[100]; + sprintf(temp, "%s%s", basedir, filename); + sysfsfp = fopen(temp, "r"); + if (sysfsfp == NULL) + return -1; + fscanf(sysfsfp, "%d\n", &ret); + fclose(sysfsfp); + return ret; +} |