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");
162 *be = (endianchar == 'b');
164 if (*bits_used == 64)
167 *mask = (1 << *bits_used) - 1;
182 error_free_builtname_generic:
183 free(builtname_generic);
184 error_free_builtname:
186 error_free_scan_el_dir:
192 inline int iioutils_get_param_float(float *output,
193 const char *param_name,
194 const char *device_dir,
196 const char *generic_name)
201 char *builtname, *builtname_generic;
202 char *filename = NULL;
203 const struct dirent *ent;
205 ret = asprintf(&builtname, "%s_%s", name, param_name);
210 ret = asprintf(&builtname_generic,
211 "%s_%s", generic_name, param_name);
214 goto error_free_builtname;
216 dp = opendir(device_dir);
219 goto error_free_builtname_generic;
221 while (ent = readdir(dp), ent != NULL)
222 if ((strcmp(builtname, ent->d_name) == 0) ||
223 (strcmp(builtname_generic, ent->d_name) == 0)) {
224 ret = asprintf(&filename,
225 "%s/%s", device_dir, ent->d_name);
230 sysfsfp = fopen(filename, "r");
233 goto error_free_filename;
235 fscanf(sysfsfp, "%f", output);
243 error_free_builtname_generic:
244 free(builtname_generic);
245 error_free_builtname:
252 * bsort_channel_array_by_index() - reorder so that the array is in index order
256 inline void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
260 struct iio_channel_info temp;
263 for (x = 0; x < cnt; x++)
264 for (y = 0; y < (cnt - 1); y++)
265 if ((*ci_array)[y].index > (*ci_array)[y+1].index) {
266 temp = (*ci_array)[y + 1];
267 (*ci_array)[y + 1] = (*ci_array)[y];
268 (*ci_array)[y] = temp;
273 * build_channel_array() - function to figure out what channels are present
274 * @device_dir: the IIO device directory in sysfs
277 inline int build_channel_array(const char *device_dir,
278 struct iio_channel_info **ci_array,
284 struct iio_channel_info *current;
286 const struct dirent *ent;
291 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
296 dp = opendir(scan_el_dir);
299 goto error_free_name;
301 while (ent = readdir(dp), ent != NULL)
302 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
304 ret = asprintf(&filename,
305 "%s/%s", scan_el_dir, ent->d_name);
308 goto error_close_dir;
310 sysfsfp = fopen(filename, "r");
311 if (sysfsfp == NULL) {
314 goto error_close_dir;
316 fscanf(sysfsfp, "%u", &ret);
322 *ci_array = malloc(sizeof(**ci_array) * (*counter));
323 if (*ci_array == NULL) {
325 goto error_close_dir;
329 while (ent = readdir(dp), ent != NULL) {
330 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
332 current = &(*ci_array)[count++];
333 ret = asprintf(&filename,
334 "%s/%s", scan_el_dir, ent->d_name);
337 /* decrement count to avoid freeing name */
339 goto error_cleanup_array;
341 sysfsfp = fopen(filename, "r");
342 if (sysfsfp == NULL) {
345 goto error_cleanup_array;
347 fscanf(sysfsfp, "%u", ¤t->enabled);
350 if (!current->enabled) {
356 current->scale = 1.0;
358 current->name = strndup(ent->d_name,
359 strlen(ent->d_name) -
361 if (current->name == NULL) {
364 goto error_cleanup_array;
366 /* Get the generic and specific name elements */
367 ret = iioutils_break_up_name(current->name,
368 ¤t->generic_name);
371 goto error_cleanup_array;
373 ret = asprintf(&filename,
380 goto error_cleanup_array;
382 sysfsfp = fopen(filename, "r");
383 fscanf(sysfsfp, "%u", ¤t->index);
387 ret = iioutils_get_param_float(¤t->scale,
391 current->generic_name);
393 goto error_cleanup_array;
394 ret = iioutils_get_param_float(¤t->offset,
398 current->generic_name);
400 goto error_cleanup_array;
401 ret = iioutils_get_type(¤t->is_signed,
409 current->generic_name);
414 /* reorder so that the array is in index order */
415 bsort_channel_array_by_index(ci_array, *counter);
420 for (i = count - 1; i >= 0; i--)
421 free((*ci_array)[i].name);
432 * find_type_by_name() - function to match top level types by name
433 * @name: top level type instance name
434 * @type: the type of top level instance being sort
436 * Typical types this is used for are device and trigger.
438 inline int find_type_by_name(const char *name, const char *type)
440 const struct dirent *ent;
441 int number, numstrlen;
445 char thisname[IIO_MAX_NAME_LENGTH];
448 dp = opendir(iio_dir);
450 printf("No industrialio devices available");
454 while (ent = readdir(dp), ent != NULL) {
455 if (strcmp(ent->d_name, ".") != 0 &&
456 strcmp(ent->d_name, "..") != 0 &&
457 strlen(ent->d_name) > strlen(type) &&
458 strncmp(ent->d_name, type, strlen(type)) == 0) {
459 numstrlen = sscanf(ent->d_name + strlen(type),
462 /* verify the next character is not a colon */
463 if (strncmp(ent->d_name + strlen(type) + numstrlen,
466 filename = malloc(strlen(iio_dir)
470 if (filename == NULL)
472 sprintf(filename, "%s%s%d/name",
476 nameFile = fopen(filename, "r");
480 fscanf(nameFile, "%s", thisname);
481 if (strcmp(name, thisname) == 0)
490 inline int _write_sysfs_int(char *filename, char *basedir, int val, int verify)
495 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
498 sprintf(temp, "%s/%s", basedir, filename);
499 sysfsfp = fopen(temp, "w");
500 if (sysfsfp == NULL) {
501 printf("failed to open %s\n", temp);
505 fprintf(sysfsfp, "%d", val);
508 sysfsfp = fopen(temp, "r");
509 if (sysfsfp == NULL) {
510 printf("failed to open %s\n", temp);
514 fscanf(sysfsfp, "%d", &test);
516 printf("Possible failure in int write %d to %s%s\n",
528 int write_sysfs_int(char *filename, char *basedir, int val)
530 return _write_sysfs_int(filename, basedir, val, 0);
533 int write_sysfs_int_and_verify(char *filename, char *basedir, int val)
535 return _write_sysfs_int(filename, basedir, val, 1);
538 int _write_sysfs_string(char *filename, char *basedir, char *val, int verify)
542 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
544 printf("Memory allocation failed\n");
547 sprintf(temp, "%s/%s", basedir, filename);
548 sysfsfp = fopen(temp, "w");
549 if (sysfsfp == NULL) {
550 printf("Could not open %s\n", temp);
554 fprintf(sysfsfp, "%s", val);
557 sysfsfp = fopen(temp, "r");
558 if (sysfsfp == NULL) {
559 printf("could not open file to verify\n");
563 fscanf(sysfsfp, "%s", temp);
564 if (strcmp(temp, val) != 0) {
565 printf("Possible failure in string write of %s "
567 "written to %s\%s\n",
582 * write_sysfs_string_and_verify() - string write, readback and verify
583 * @filename: name of file to write to
584 * @basedir: the sysfs directory in which the file is to be found
585 * @val: the string to write
587 int write_sysfs_string_and_verify(char *filename, char *basedir, char *val)
589 return _write_sysfs_string(filename, basedir, val, 1);
592 int write_sysfs_string(char *filename, char *basedir, char *val)
594 return _write_sysfs_string(filename, basedir, val, 0);
597 int read_sysfs_posint(char *filename, char *basedir)
601 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
603 printf("Memory allocation failed");
606 sprintf(temp, "%s/%s", basedir, filename);
607 sysfsfp = fopen(temp, "r");
608 if (sysfsfp == NULL) {
612 fscanf(sysfsfp, "%d\n", &ret);
619 int read_sysfs_float(char *filename, char *basedir, float *val)
623 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
625 printf("Memory allocation failed");
628 sprintf(temp, "%s/%s", basedir, filename);
629 sysfsfp = fopen(temp, "r");
630 if (sysfsfp == NULL) {
634 fscanf(sysfsfp, "%f\n", val);