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.
10 /* Made up value to limit allocation sizes */
18 #define IIO_MAX_NAME_LENGTH 30
20 #define FORMAT_SCAN_ELEMENTS_DIR "%s/scan_elements"
21 #define FORMAT_TYPE_FILE "%s_type"
23 const char *iio_dir = "/sys/bus/iio/devices/";
26 * 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
30 static int iioutils_break_up_name(const char *full_name,
36 current = strdup(full_name);
37 working = strtok(current, "_\0");
49 *generic_name = strdup(working);
56 * struct iio_channel_info - information about a given channel
58 * @generic_name: general name for channel type
59 * @scale: scale factor to be applied for conversion to si units
60 * @offset: offset to be applied for conversion to si units
61 * @index: the channel index in the buffer output
62 * @bytes: number of bytes occupied in buffer output
63 * @mask: a bit mask for the raw output
64 * @is_signed: is the raw value stored signed
65 * @enabled: is this channel enabled
67 struct iio_channel_info {
84 * iioutils_get_type() - find and process _type attribute data
85 * @is_signed: output whether channel is signed
86 * @bytes: output how many bytes the channel storage occupies
87 * @mask: output a bit mask for the raw data
89 * @device_dir: the iio device directory
90 * @name: the channel name
91 * @generic_name: the channel type name
93 inline int iioutils_get_type(unsigned *is_signed,
99 const char *device_dir,
101 const char *generic_name)
106 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
107 char signchar, endianchar;
109 const struct dirent *ent;
111 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
116 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
119 goto error_free_scan_el_dir;
121 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
124 goto error_free_builtname;
127 dp = opendir(scan_el_dir);
130 goto error_free_builtname_generic;
132 while (ent = readdir(dp), ent != NULL)
134 * Do we allow devices to override a generic name with
137 if ((strcmp(builtname, ent->d_name) == 0) ||
138 (strcmp(builtname_generic, ent->d_name) == 0)) {
139 ret = asprintf(&filename,
140 "%s/%s", scan_el_dir, ent->d_name);
145 sysfsfp = fopen(filename, "r");
146 if (sysfsfp == NULL) {
147 printf("failed to open %s\n", filename);
149 goto error_free_filename;
152 ret = fscanf(sysfsfp,
159 printf("failed to pass scan type description\n");
161 goto error_close_sysfsfp;
163 *be = (endianchar == 'b');
165 if (*bits_used == 64)
168 *mask = (1 << *bits_used) - 1;
187 error_free_builtname_generic:
188 free(builtname_generic);
189 error_free_builtname:
191 error_free_scan_el_dir:
197 inline int iioutils_get_param_float(float *output,
198 const char *param_name,
199 const char *device_dir,
201 const char *generic_name)
206 char *builtname, *builtname_generic;
207 char *filename = NULL;
208 const struct dirent *ent;
210 ret = asprintf(&builtname, "%s_%s", name, param_name);
215 ret = asprintf(&builtname_generic,
216 "%s_%s", generic_name, param_name);
219 goto error_free_builtname;
221 dp = opendir(device_dir);
224 goto error_free_builtname_generic;
226 while (ent = readdir(dp), ent != NULL)
227 if ((strcmp(builtname, ent->d_name) == 0) ||
228 (strcmp(builtname_generic, ent->d_name) == 0)) {
229 ret = asprintf(&filename,
230 "%s/%s", device_dir, ent->d_name);
235 sysfsfp = fopen(filename, "r");
238 goto error_free_filename;
240 fscanf(sysfsfp, "%f", output);
248 error_free_builtname_generic:
249 free(builtname_generic);
250 error_free_builtname:
257 * bsort_channel_array_by_index() - reorder so that the array is in index order
261 inline void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
265 struct iio_channel_info temp;
268 for (x = 0; x < cnt; x++)
269 for (y = 0; y < (cnt - 1); y++)
270 if ((*ci_array)[y].index > (*ci_array)[y+1].index) {
271 temp = (*ci_array)[y + 1];
272 (*ci_array)[y + 1] = (*ci_array)[y];
273 (*ci_array)[y] = temp;
278 * build_channel_array() - function to figure out what channels are present
279 * @device_dir: the IIO device directory in sysfs
282 inline int build_channel_array(const char *device_dir,
283 struct iio_channel_info **ci_array,
289 struct iio_channel_info *current;
291 const struct dirent *ent;
296 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
301 dp = opendir(scan_el_dir);
304 goto error_free_name;
306 while (ent = readdir(dp), ent != NULL)
307 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
309 ret = asprintf(&filename,
310 "%s/%s", scan_el_dir, ent->d_name);
313 goto error_close_dir;
315 sysfsfp = fopen(filename, "r");
316 if (sysfsfp == NULL) {
319 goto error_close_dir;
321 fscanf(sysfsfp, "%u", &ret);
327 *ci_array = malloc(sizeof(**ci_array) * (*counter));
328 if (*ci_array == NULL) {
330 goto error_close_dir;
334 while (ent = readdir(dp), ent != NULL) {
335 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
337 current = &(*ci_array)[count++];
338 ret = asprintf(&filename,
339 "%s/%s", scan_el_dir, ent->d_name);
342 /* decrement count to avoid freeing name */
344 goto error_cleanup_array;
346 sysfsfp = fopen(filename, "r");
347 if (sysfsfp == NULL) {
350 goto error_cleanup_array;
352 fscanf(sysfsfp, "%u", ¤t->enabled);
355 if (!current->enabled) {
361 current->scale = 1.0;
363 current->name = strndup(ent->d_name,
364 strlen(ent->d_name) -
366 if (current->name == NULL) {
369 goto error_cleanup_array;
371 /* Get the generic and specific name elements */
372 ret = iioutils_break_up_name(current->name,
373 ¤t->generic_name);
376 goto error_cleanup_array;
378 ret = asprintf(&filename,
385 goto error_cleanup_array;
387 sysfsfp = fopen(filename, "r");
388 fscanf(sysfsfp, "%u", ¤t->index);
392 ret = iioutils_get_param_float(¤t->scale,
396 current->generic_name);
398 goto error_cleanup_array;
399 ret = iioutils_get_param_float(¤t->offset,
403 current->generic_name);
405 goto error_cleanup_array;
406 ret = iioutils_get_type(¤t->is_signed,
414 current->generic_name);
419 /* reorder so that the array is in index order */
420 bsort_channel_array_by_index(ci_array, *counter);
425 for (i = count - 1; i >= 0; i--)
426 free((*ci_array)[i].name);
437 * find_type_by_name() - function to match top level types by name
438 * @name: top level type instance name
439 * @type: the type of top level instance being sort
441 * Typical types this is used for are device and trigger.
443 inline int find_type_by_name(const char *name, const char *type)
445 const struct dirent *ent;
446 int number, numstrlen;
450 char thisname[IIO_MAX_NAME_LENGTH];
453 dp = opendir(iio_dir);
455 printf("No industrialio devices available");
459 while (ent = readdir(dp), ent != NULL) {
460 if (strcmp(ent->d_name, ".") != 0 &&
461 strcmp(ent->d_name, "..") != 0 &&
462 strlen(ent->d_name) > strlen(type) &&
463 strncmp(ent->d_name, type, strlen(type)) == 0) {
464 numstrlen = sscanf(ent->d_name + strlen(type),
467 /* verify the next character is not a colon */
468 if (strncmp(ent->d_name + strlen(type) + numstrlen,
471 filename = malloc(strlen(iio_dir)
475 if (filename == NULL)
477 sprintf(filename, "%s%s%d/name",
481 nameFile = fopen(filename, "r");
485 fscanf(nameFile, "%s", thisname);
486 if (strcmp(name, thisname) == 0)
495 inline int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
500 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
503 sprintf(temp, "%s/%s", basedir, filename);
504 sysfsfp = fopen(temp, "w");
505 if (sysfsfp == NULL) {
506 printf("failed to open %s\n", temp);
510 fprintf(sysfsfp, "%d", val);
513 sysfsfp = fopen(temp, "r");
514 if (sysfsfp == NULL) {
515 printf("failed to open %s\n", temp);
519 fscanf(sysfsfp, "%d", &test);
521 printf("Possible failure in int write %d to %s%s\n",
533 int write_sysfs_int(char *filename, char *basedir, int val)
535 return _write_sysfs_int(filename, basedir, val, 0);
538 int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
540 return _write_sysfs_int(filename, basedir, val, 1);
543 int _write_sysfs_string(char *filename, char *basedir, char *val, int verify)
547 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
549 printf("Memory allocation failed\n");
552 sprintf(temp, "%s/%s", basedir, filename);
553 sysfsfp = fopen(temp, "w");
554 if (sysfsfp == NULL) {
555 printf("Could not open %s\n", temp);
559 fprintf(sysfsfp, "%s", val);
562 sysfsfp = fopen(temp, "r");
563 if (sysfsfp == NULL) {
564 printf("could not open file to verify\n");
568 fscanf(sysfsfp, "%s", temp);
569 if (strcmp(temp, val) != 0) {
570 printf("Possible failure in string write of %s "
572 "written to %s\%s\n",
587 * write_sysfs_string_and_verify() - string write, readback and verify
588 * @filename: name of file to write to
589 * @basedir: the sysfs directory in which the file is to be found
590 * @val: the string to write
592 int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
594 return _write_sysfs_string(filename, basedir, val, 1);
597 int write_sysfs_string(char *filename, char *basedir, char *val)
599 return _write_sysfs_string(filename, basedir, val, 0);
602 int read_sysfs_posint(char *filename, char *basedir)
606 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
608 printf("Memory allocation failed");
611 sprintf(temp, "%s/%s", basedir, filename);
612 sysfsfp = fopen(temp, "r");
613 if (sysfsfp == NULL) {
617 fscanf(sysfsfp, "%d\n", &ret);
624 int read_sysfs_float(char *filename, char *basedir, float *val)
628 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
630 printf("Memory allocation failed");
633 sprintf(temp, "%s/%s", basedir, filename);
634 sysfsfp = fopen(temp, "r");
635 if (sysfsfp == NULL) {
639 fscanf(sysfsfp, "%f\n", val);