Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (C) 2010, Linaro Limited. |
| 3 | * |
| 4 | * This file is part of PowerDebug. |
| 5 | * |
| 6 | * All rights reserved. This program and the accompanying materials |
| 7 | * are made available under the terms of the Eclipse Public License v1.0 |
| 8 | * which accompanies this distribution, and is available at |
| 9 | * http://www.eclipse.org/legal/epl-v10.html |
| 10 | * |
| 11 | * Contributors: |
| 12 | * Daniel Lezcano <daniel.lezcano@linaro.org> (IBM Corporation) |
| 13 | * - initial API and implementation |
| 14 | *******************************************************************************/ |
| 15 | |
| 16 | #ifndef _GNU_SOURCE |
| 17 | #define _GNU_SOURCE |
| 18 | #include <stdio.h> |
| 19 | #undef _GNU_SOURCE |
| 20 | #endif |
| 21 | #include <mntent.h> |
| 22 | #include <string.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | #include <sys/param.h> |
| 27 | #include <sys/stat.h> |
| 28 | |
| 29 | #include "powerdebug.h" |
| 30 | #include "display.h" |
| 31 | #include "tree.h" |
| 32 | #include "utils.h" |
| 33 | |
| 34 | #define SYSFS_GPIO "/sys/class/gpio" |
| 35 | |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 36 | struct gpio_info { |
| 37 | bool expanded; |
| 38 | int active_low; |
| 39 | int value; |
| 40 | int direction; |
| 41 | int edge; |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 42 | char *prefix; |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 43 | } *gpios_info; |
| 44 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 45 | static struct tree *gpio_tree = NULL; |
| 46 | |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 47 | static struct gpio_info *gpio_alloc(void) |
| 48 | { |
| 49 | struct gpio_info *gi; |
| 50 | |
| 51 | gi = malloc(sizeof(*gi)); |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 52 | if (gi) { |
| 53 | memset(gi, -1, sizeof(*gi)); |
| 54 | gi->prefix = NULL; |
| 55 | } |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 56 | |
| 57 | return gi; |
| 58 | } |
| 59 | |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 60 | static int gpio_filter_cb(const char *name) |
| 61 | { |
| 62 | /* let's ignore some directories in order to avoid to be |
| 63 | * pulled inside the sysfs circular symlinks mess/hell |
| 64 | * (choose the word which fit better) |
| 65 | */ |
| 66 | if (!strcmp(name, "device")) |
| 67 | return 1; |
| 68 | |
| 69 | if (!strcmp(name, "subsystem")) |
| 70 | return 1; |
| 71 | |
| 72 | if (!strcmp(name, "driver")) |
| 73 | return 1; |
| 74 | |
| 75 | /* we want to ignore the gpio chips */ |
| 76 | if (strstr(name, "chip")) |
| 77 | return 1; |
| 78 | |
| 79 | /* we are not interested by the power value */ |
| 80 | if (!strcmp(name, "power")) |
| 81 | return 1; |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 86 | static inline int read_gpio_cb(struct tree *t, void *data) |
| 87 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 88 | struct gpio_info *gpio = t->private; |
| 89 | |
| 90 | file_read_value(t->path, "active_low", "%d", &gpio->active_low); |
| 91 | file_read_value(t->path, "value", "%d", &gpio->value); |
| 92 | file_read_value(t->path, "edge", "%d", &gpio->edge); |
| 93 | file_read_value(t->path, "direction", "%d", &gpio->direction); |
| 94 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int read_gpio_info(struct tree *tree) |
| 99 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 100 | return tree_for_each(tree, read_gpio_cb, NULL); |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static int fill_gpio_cb(struct tree *t, void *data) |
| 104 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 105 | struct gpio_info *gpio; |
| 106 | |
| 107 | gpio = gpio_alloc(); |
| 108 | if (!gpio) |
| 109 | return -1; |
| 110 | t->private = gpio; |
| 111 | |
| 112 | /* we skip the root node but we set it expanded for its children */ |
| 113 | if (!t->parent) { |
| 114 | gpio->expanded = true; |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | return read_gpio_cb(t, data); |
| 119 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static int fill_gpio_tree(void) |
| 123 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 124 | return tree_for_each(gpio_tree, fill_gpio_cb, NULL); |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 127 | static int dump_gpio_cb(struct tree *t, void *data) |
| 128 | { |
| 129 | struct gpio_info *gpio = t->private; |
| 130 | struct gpio_info *pgpio; |
| 131 | |
| 132 | if (!t->parent) { |
| 133 | printf("/\n"); |
| 134 | gpio->prefix = ""; |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | pgpio = t->parent->private; |
| 139 | |
| 140 | if (!gpio->prefix) |
| 141 | if (asprintf(&gpio->prefix, "%s%s%s", pgpio->prefix, |
| 142 | t->depth > 1 ? " ": "", t->next ? "|" : " ") < 0) |
| 143 | return -1; |
| 144 | |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 145 | printf("%s%s-- %s (", gpio->prefix, !t->next ? "`" : "", t->name); |
| 146 | |
| 147 | if (gpio->active_low != -1) |
| 148 | printf(" active_low:%d", gpio->active_low); |
| 149 | |
| 150 | if (gpio->value != -1) |
| 151 | printf(", value:%d", gpio->value); |
| 152 | |
| 153 | if (gpio->edge != -1) |
| 154 | printf(", edge:%d", gpio->edge); |
| 155 | |
| 156 | if (gpio->direction != -1) |
| 157 | printf(", direction:%d", gpio->direction); |
| 158 | |
| 159 | printf(" )\n"); |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | int dump_gpio_info(void) |
| 165 | { |
| 166 | return tree_for_each(gpio_tree, dump_gpio_cb, NULL); |
| 167 | } |
| 168 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 169 | int gpio_dump(void) |
| 170 | { |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 171 | int ret; |
| 172 | |
| 173 | printf("\nGpio Tree :\n"); |
| 174 | printf("***********\n"); |
| 175 | ret = dump_gpio_info(); |
| 176 | printf("\n\n"); |
| 177 | |
| 178 | return ret; |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 181 | static char *gpio_line(struct tree *t) |
| 182 | { |
| 183 | struct gpio_info *gpio = t->private; |
| 184 | char *gpioline; |
| 185 | |
| 186 | if (asprintf(&gpioline, "%-20s %-10d %-10d %-10d %-10d", t-> name, |
| 187 | gpio->value, gpio->active_low, gpio->edge, gpio->direction) < 0) |
| 188 | return NULL; |
| 189 | |
| 190 | return gpioline; |
| 191 | } |
| 192 | |
| 193 | static int _gpio_print_info_cb(struct tree *t, void *data) |
| 194 | { |
| 195 | int *line = data; |
| 196 | char *buffer; |
| 197 | |
| 198 | /* we skip the root node of the tree */ |
| 199 | if (!t->parent) |
| 200 | return 0; |
| 201 | |
| 202 | buffer = gpio_line(t); |
| 203 | if (!buffer) |
| 204 | return -1; |
| 205 | |
| 206 | display_print_line(GPIO, *line, buffer, 0, t); |
| 207 | |
| 208 | (*line)++; |
| 209 | |
| 210 | free(buffer); |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int gpio_print_info_cb(struct tree *t, void *data) |
| 216 | { |
| 217 | /* we skip the root node of the tree */ |
| 218 | if (!t->parent) |
| 219 | return 0; |
| 220 | |
| 221 | return _gpio_print_info_cb(t, data); |
| 222 | } |
| 223 | |
| 224 | static int gpio_print_header(void) |
| 225 | { |
| 226 | char *buf; |
| 227 | int ret; |
| 228 | |
| 229 | if (asprintf(&buf, "%-20s %-10s %-10s %-10s %-10s", |
| 230 | "Name", "Value", "Active_low", "Edge", "Direction") < 0) |
| 231 | return -1; |
| 232 | |
| 233 | ret = display_column_name(buf); |
| 234 | |
| 235 | free(buf); |
| 236 | |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | static int gpio_print_info(struct tree *tree) |
| 241 | { |
| 242 | int ret, line = 0; |
| 243 | |
| 244 | display_reset_cursor(GPIO); |
| 245 | |
| 246 | gpio_print_header(); |
| 247 | |
| 248 | ret = tree_for_each(tree, gpio_print_info_cb, &line); |
| 249 | |
| 250 | display_refresh_pad(GPIO); |
| 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | static int gpio_display(bool refresh) |
| 256 | { |
| 257 | if (refresh && read_gpio_info(gpio_tree)) |
| 258 | return -1; |
| 259 | |
| 260 | return gpio_print_info(gpio_tree); |
| 261 | } |
| 262 | |
| 263 | static struct display_ops gpio_ops = { |
| 264 | .display = gpio_display, |
| 265 | }; |
| 266 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 267 | /* |
| 268 | * Initialize the gpio framework |
| 269 | */ |
| 270 | int gpio_init(void) |
| 271 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 272 | gpio_tree = tree_load(SYSFS_GPIO, gpio_filter_cb, false); |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 273 | if (!gpio_tree) |
| 274 | return -1; |
| 275 | |
| 276 | if (fill_gpio_tree()) |
| 277 | return -1; |
| 278 | |
| 279 | return display_register(GPIO, &gpio_ops); |
| 280 | } |