RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / iio / Documentation / iio_utils.h
blob014f6684faba94f8ba6c28fcd606a131050bfdde
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.
8 */
10 /* Made up value to limit allocation sizes */
11 #include <string.h>
12 #include <stdlib.h>
14 #define IIO_MAX_NAME_LENGTH 30
16 #define IIO_EVENT_CODE_RING_50_FULL 200
17 #define IIO_EVENT_CODE_RING_75_FULL 201
18 #define IIO_EVENT_CODE_RING_100_FULL 202
20 const char *iio_dir = "/sys/bus/iio/devices/";
22 struct iio_event_data {
23 int id;
24 __s64 timestamp;
27 /**
28 * find_type_by_name() - function to match top level types by name
29 * @name: top level type instance name
30 * @type: the type of top level instance being sort
32 * Typical types this is used for are device and trigger.
33 **/
34 inline int find_type_by_name(const char *name, const char *type)
36 const struct dirent *ent;
37 int number, numstrlen;
39 FILE *nameFile;
40 DIR *dp;
41 char thisname[IIO_MAX_NAME_LENGTH];
42 char *filename;
43 struct stat Stat;
45 dp = opendir(iio_dir);
46 if (dp == NULL) {
47 printf("No industrialio devices available");
48 return -ENODEV;
51 while (ent = readdir(dp), ent != NULL) {
52 if (strcmp(ent->d_name, ".") != 0 &&
53 strcmp(ent->d_name, "..") != 0 &&
54 strlen(ent->d_name) > strlen(type) &&
55 strncmp(ent->d_name, type, strlen(type)) == 0) {
56 numstrlen = sscanf(ent->d_name + strlen(type),
57 "%d",
58 &number);
59 /* verify the next character is not a colon */
60 if (strncmp(ent->d_name + strlen(type) + numstrlen,
61 ":",
62 1) != 0) {
63 filename = malloc(strlen(iio_dir)
64 + strlen(type)
65 + numstrlen
66 + 6);
67 if (filename == NULL)
68 return -ENOMEM;
69 sprintf(filename, "%s%s%d/name",
70 iio_dir,
71 type,
72 number);
73 nameFile = fopen(filename, "r");
74 if (!nameFile)
75 continue;
76 free(filename);
77 fscanf(nameFile, "%s", thisname);
78 if (strcmp(name, thisname) == 0)
79 return number;
80 fclose(nameFile);
84 return -ENODEV;
87 inline int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
89 int ret;
90 FILE *sysfsfp;
91 int test;
92 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
93 if (temp == NULL)
94 return -ENOMEM;
95 sprintf(temp, "%s/%s", basedir, filename);
96 sysfsfp = fopen(temp, "w");
97 if (sysfsfp == NULL) {
98 printf("failed to open %s\n", temp);
99 ret = -errno;
100 goto error_free;
102 fprintf(sysfsfp, "%d", val);
103 fclose(sysfsfp);
104 if (verify) {
105 sysfsfp = fopen(temp, "r");
106 if (sysfsfp == NULL) {
107 printf("failed to open %s\n", temp);
108 ret = -errno;
109 goto error_free;
111 fscanf(sysfsfp, "%d", &test);
112 if (test != val) {
113 printf("Possible failure in int write %d to %s%s\n",
114 val,
115 basedir,
116 filename);
117 ret = -1;
120 error_free:
121 free(temp);
122 return ret;
125 int write_sysfs_int(char *filename, char *basedir, int val)
127 return _write_sysfs_int(filename, basedir, val, 0);
130 int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
132 return _write_sysfs_int(filename, basedir, val, 1);
135 int _write_sysfs_string(char *filename, char *basedir, char *val, int verify)
137 int ret;
138 FILE *sysfsfp;
139 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
140 if (temp == NULL) {
141 printf("Memory allocation failed\n");
142 return -ENOMEM;
144 sprintf(temp, "%s/%s", basedir, filename);
145 sysfsfp = fopen(temp, "w");
146 if (sysfsfp == NULL) {
147 printf("Could not open %s\n", temp);
148 ret = -errno;
149 goto error_free;
151 fprintf(sysfsfp, "%s", val);
152 fclose(sysfsfp);
153 if (verify) {
154 sysfsfp = fopen(temp, "r");
155 if (sysfsfp == NULL) {
156 ret = -errno;
157 goto error_free;
159 fscanf(sysfsfp, "%s", temp);
160 if (strcmp(temp, val) != 0) {
161 printf("Possible failure in string write of %s "
162 "Should be %s "
163 "writen to %s\%s\n",
164 temp,
165 val,
166 basedir,
167 filename);
168 ret = -1;
171 error_free:
172 free(temp);
174 return ret;
177 * write_sysfs_string_and_verify() - string write, readback and verify
178 * @filename: name of file to write to
179 * @basedir: the sysfs directory in which the file is to be found
180 * @val: the string to write
182 int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
184 return _write_sysfs_string(filename, basedir, val, 1);
187 int write_sysfs_string(char *filename, char *basedir, char *val)
189 return _write_sysfs_string(filename, basedir, val, 0);
192 int read_sysfs_posint(char *filename, char *basedir)
194 int ret;
195 FILE *sysfsfp;
196 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
197 if (temp == NULL) {
198 printf("Memory allocation failed");
199 return -ENOMEM;
201 sprintf(temp, "%s/%s", basedir, filename);
202 sysfsfp = fopen(temp, "r");
203 if (sysfsfp == NULL) {
204 ret = -errno;
205 goto error_free;
207 fscanf(sysfsfp, "%d\n", &ret);
208 fclose(sysfsfp);
209 error_free:
210 free(temp);
211 return ret;
214 int read_sysfs_float(char *filename, char *basedir, float *val)
216 float ret = 0;
217 FILE *sysfsfp;
218 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
219 if (temp == NULL) {
220 printf("Memory allocation failed");
221 return -ENOMEM;
223 sprintf(temp, "%s/%s", basedir, filename);
224 sysfsfp = fopen(temp, "r");
225 if (sysfsfp == NULL) {
226 ret = -errno;
227 goto error_free;
229 fscanf(sysfsfp, "%f\n", val);
230 fclose(sysfsfp);
231 error_free:
232 free(temp);
233 return ret;