aboutsummaryrefslogtreecommitdiff
path: root/gpio.c
blob: 9f323eb1ba70e009dccba2bddb5f3a06a68e576c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * Power debug tool (powerdebug)
 *
 * Copyright (C) 2016, Linaro Limited.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#endif
#include <mntent.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/stat.h>

#include "powerdebug.h"
#include "display.h"
#include "tree.h"
#include "utils.h"

#define SYSFS_GPIO "/sys/class/gpio"

#define MAX_VALUE_BYTE	10

struct gpio_info {
	bool expanded;
	int active_low;
	int value;
	char direction[MAX_VALUE_BYTE];
	char edge[MAX_VALUE_BYTE];
	char *prefix;
} *gpios_info;

static struct tree *gpio_tree = NULL;

static struct gpio_info *gpio_alloc(void)
{
	struct gpio_info *gi;

	gi = malloc(sizeof(*gi));
	if (gi) {
		memset(gi, -1, sizeof(*gi));
		memset(gi->direction, 0, MAX_VALUE_BYTE);
		memset(gi->edge, 0, MAX_VALUE_BYTE);
		gi->prefix = NULL;
	}

	return gi;
}

static int gpio_filter_cb(const char *name)
{
	/* let's ignore some directories in order to avoid to be
	 * pulled inside the sysfs circular symlinks mess/hell
	 * (choose the word which fit better)
	 */
	if (!strcmp(name, "device"))
		return 1;

	if (!strcmp(name, "subsystem"))
		return 1;

	if (!strcmp(name, "driver"))
		return 1;

        /* we want to ignore the gpio chips */
	if (strstr(name, "chip"))
		return 1;

        /* we are not interested by the power value */
	if (!strcmp(name, "power"))
		return 1;

	return 0;
}

static inline int read_gpio_cb(struct tree *t, void *data)
{
	struct gpio_info *gpio = t->private;

	file_read_value(t->path, "active_low", "%d", &gpio->active_low);
	file_read_value(t->path, "value", "%d", &gpio->value);
	file_read_value(t->path, "edge", "%8s", &gpio->edge);
	file_read_value(t->path, "direction", "%4s", &gpio->direction);

	return 0;
}

static int read_gpio_info(struct tree *tree)
{
	return tree_for_each(tree, read_gpio_cb, NULL);
}

static int fill_gpio_cb(struct tree *t, void *data)
{
	struct gpio_info *gpio;

	gpio = gpio_alloc();
	if (!gpio)
		return -1;
	t->private = gpio;

        /* we skip the root node but we set it expanded for its children */
	if (!t->parent) {
		gpio->expanded = true;
		return 0;
	}

	return read_gpio_cb(t, data);

}

static int fill_gpio_tree(void)
{
	return tree_for_each(gpio_tree, fill_gpio_cb, NULL);
}

static int dump_gpio_cb(struct tree *t, void *data)
{
	struct gpio_info *gpio = t->private;
	struct gpio_info *pgpio;

	if (!t->parent) {
		printf("/\n");
		gpio->prefix = "";
		return 0;
	}

	pgpio = t->parent->private;

	if (!gpio->prefix)
		if (asprintf(&gpio->prefix, "%s%s%s", pgpio->prefix,
			     t->depth > 1 ? "   ": "", t->next ? "|" : " ") < 0)
			return -1;

	printf("%s%s-- %s (", gpio->prefix,  !t->next ? "`" : "", t->name);

	if (gpio->active_low != -1)
		printf(" active_low:%d", gpio->active_low);

	if (gpio->value != -1)
		printf(", value:%d", gpio->value);

	if (gpio->edge[0] != 0)
		printf(", edge:%s", gpio->edge);

	if (gpio->direction[0] != 0)
		printf(", direction:%s", gpio->direction);

	printf(" )\n");

	return 0;
}

int dump_gpio_info(void)
{
	return tree_for_each(gpio_tree, dump_gpio_cb, NULL);
}


static char *gpio_line(struct tree *t)
{
	struct gpio_info *gpio = t->private;
	char *gpioline;

	if (asprintf(&gpioline, "%-20s %-10d %-10d %-10s %-10s", t->name,
		     gpio->value, gpio->active_low, gpio->edge, gpio->direction) < 0)
		return NULL;

	return gpioline;
}

static int _gpio_print_info_cb(struct tree *t, void *data)
{
	int *line = data;
	char *buffer;

        /* we skip the root node of the tree */
	if (!t->parent)
		return 0;

	buffer = gpio_line(t);
	if (!buffer)
		return -1;

	display_print_line(GPIO, *line, buffer, 0, t);

	(*line)++;

	free(buffer);

	return 0;
}

static int gpio_print_info_cb(struct tree *t, void *data)
{
        /* we skip the root node of the tree */
	if (!t->parent)
		return 0;

	return _gpio_print_info_cb(t, data);
}

static int gpio_print_header(void)
{
	char *buf;
	int ret;

	if (asprintf(&buf, "%-20s %-10s %-10s %-10s %-10s",
		     "Name", "Value", "Active_low", "Edge", "Direction") < 0)
		return -1;

	ret = display_column_name(buf);

	free(buf);

	return ret;
}

static int gpio_print_info(struct tree *tree)
{
	int ret, line = 0;

	display_reset_cursor(GPIO);

	gpio_print_header();

	ret = tree_for_each(tree, gpio_print_info_cb, &line);

	display_refresh_pad(GPIO);

	return ret;
}

void export_free_gpios(void)
{
	FILE *fgpio, *fgpio_export;
	int i, gpio_max = 0;
	char *line = NULL;
	ssize_t nrread;
	size_t len = 0;

	fgpio = fopen("/sys/kernel/debug/gpio", "r");
	if (!fgpio) {
		printf("failed to read debugfs gpio file\n");
		return;
	}

	fgpio_export = fopen("/sys/class/gpio/export", "w");
	if (!fgpio_export) {
		printf("failed to write open gpio-export file\n");
		goto out;
	}

	/* export the gpios */
	while ((nrread = getline(&line, &len, fgpio)) != -1) {
		if (strstr(line, "GPIOs"))
			sscanf(line, "%*[^-]-%d", &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);
	}

	free(line);

	if (fgpio)
		fclose(fgpio);
out:
	if (fgpio_export)
		fclose(fgpio_export);

	return;
}

static int gpio_load_info(void)
{
	if (gpio_tree)
		return 0;

	export_free_gpios();

	gpio_tree = tree_load(SYSFS_GPIO, gpio_filter_cb, false);
	if (!gpio_tree)
		return -1;

	if (fill_gpio_tree())
		return -1;

	return 0;
}

int gpio_dump(void)
{
	int ret;

	if (gpio_load_info())
		return -1;

	printf("\nGpio Tree :\n");
	printf("***********\n");
	ret = dump_gpio_info();
	printf("\n\n");

	return ret;
}

static int gpio_display(bool refresh)
{
	if (gpio_load_info()) {
                display_print_error(GPIO, 0, "Failed to read gpio info");
                return 0; /* we don't want this to be a critical error */
	}

	if (refresh && read_gpio_info(gpio_tree))
		return -1;

	return gpio_print_info(gpio_tree);
}

static int gpio_change(int keyvalue)
{
	struct tree *t = display_get_row_data(GPIO);
	struct gpio_info *gpio = t->private;

	if (!t || !gpio)
		return -1;

	switch (keyvalue) {
	case 'D':
		/* Only change direction when gpio interrupt not set.*/
		if (!strstr(gpio->edge, "none"))
			return 0;

		if (strstr(gpio->direction, "in"))
			strcpy(gpio->direction, "out");
		else if (strstr(gpio->direction, "out"))
			strcpy(gpio->direction, "in");
		file_write_value(t->path, "direction", "%s", &gpio->direction);
		file_read_value(t->path, "direction", "%s", &gpio->direction);
		file_read_value(t->path, "value", "%d", &gpio->value);

		break;

	case 'V':
		/* Only change value when gpio direction is out. */
		if (!strstr(gpio->edge, "none")
			 || !strstr(gpio->direction, "out"))
			return 0;

		if (gpio->value)
			file_write_value(t->path, "direction", "%s", &"low");
		else
			file_write_value(t->path, "direction", "%s", &"high");
		file_read_value(t->path, "value", "%d", &gpio->value);

		break;

	default:
		return -1;
	}

	return 0;
}

static struct display_ops gpio_ops = {
	.display = gpio_display,
	.change = gpio_change,
};

/*
 * Initialize the gpio framework
 */
int gpio_init(struct powerdebug_options *options)
{
	if (!(options->flags & GPIO_OPTION))
		return 0;

	return display_register(GPIO, &gpio_ops);
}