- Displays information about free gpios by exporting & unexporting
Also, attached.
diff --git a/gpio.c b/gpio.c index 4a42cb7..eb69f56 100644 --- a/gpio.c +++ b/gpio.c @@ -46,6 +46,7 @@ struct gpio_info {
static struct tree *gpio_tree = NULL; static bool gpio_error = false; +int gpio_max;
static struct gpio_info *gpio_alloc(void) { @@ -320,16 +321,17 @@ static struct display_ops gpio_ops = { .change = gpio_change, };
-void export_gpios(void) +void export_free_gpios(void) { - FILE *fgpio, *fgpio_export; - int gpio[256], num = 0; + FILE *fgpio = NULL, *fgpio_export = NULL; + int i; char *line = NULL; - ssize_t read, len; + ssize_t read; + size_t len;
fgpio = fopen("/sys/kernel/debug/gpio", "r"); if (!fgpio) { - printf("failed to read debugfs gpio file\n"); + printf("failed to read open debugfs gpio file\n"); goto out; }
@@ -340,17 +342,51 @@ void export_gpios(void) }
/* export the gpios */ - while (read = getline(&line, &len, fgpio) != -1) { - char *str; - - if (strstr(line, "gpio-")) { - str = strtok(line, " "); - sscanf(str, "gpio-%d", &gpio[num]); - fprintf(fgpio_export, "%d", gpio[num]); - num++; - } + while ((read = getline(&line, &len, fgpio)) != -1) { + if (strstr(line, "GPIOs")) + sscanf(line, "%*[^-]-%d%*s", &gpio_max); + } + + printf("log: total gpios = %d\n", gpio_max); + for (i = 0 ; i <= gpio_max ; i++) { + char command[50] = ""; + + sprintf(command, "echo %d > /sys/class/gpio/export", i); + if (system(command) < 0) + printf("error: failed to export gpio-%d\n", i); } out: + if (fgpio) + fclose(fgpio); + + if (fgpio_export) + fclose(fgpio_export); + + return; +} + +void unexport_free_gpios(void) +{ + FILE *fgpio_unexport = NULL; + int i; + + fgpio_unexport = fopen("/sys/class/gpio/unexport", "w"); + if (!fgpio_unexport) { + printf("failed to write open gpio-unexport file\n"); + goto out; + } + + for (i = 0 ; i <= gpio_max ; i++) { + char command[50] = ""; + + sprintf(command, "echo %d > /sys/class/gpio/unexport", i); + if (system(command) < 0) + printf("error: failed to unexport gpio-%d\n", i); + } +out: + if (fgpio_unexport) + fclose(fgpio_unexport); + return; }
@@ -368,7 +404,7 @@ int gpio_init(void) if (access(SYSFS_GPIO, F_OK)) gpio_error = true; /* set the flag */
- export_gpios(); + export_free_gpios();
gpio_tree = tree_load(SYSFS_GPIO, gpio_filter_cb, false); if (!gpio_tree) @@ -377,5 +413,7 @@ int gpio_init(void) if (fill_gpio_tree()) return -1;
+ unexport_free_gpios(); + return ret; }