summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 2e2f1c23a0ce334891bdd90ed0ff8ae7c2546422 (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
/*
 * Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>

#include <sys/stat.h>

#include "compatible_devices.h"
#include "exynos4412_gpios.h"
#include "exynos4412_gpios_data.h"
#include "modem_gpios_data.h"
#include "tests.h"

int usage(char* progname)
{
	printf("Usage: %s <flash|gpl1|gpl2|gps|gpy0|modem|test>\n", progname);
	printf("\n");
	printf("Available commands:\n");
	printf("\tflash: dump flash GPIO (gpj1 1) status\n");
	printf("\tgpl1: dump GPL1 GPIOs status"
	       " (for tests on gpio127 and gpio128)\n");
	printf("\tgpl2: dump GPL2 GPIOs status"
	       " (for tests on gpio127 and gpio128)\n");
	printf("\tgps: dump GPS GPIOs status\n");
	printf("\tgpy0: dump GPY0 GPIOs status"
	       " (for tests on gpio127 and gpio128)\n");
	printf("\tmodem: dump the modem GPIOs status\n");
	printf("\ttest: run self tests\n");

	return 0;
}

int dump_flash_gpio_status(int debug, char* devmem, int fd, size_t  page_size)
{
	int rc;

	rc = dump_gpio_infos(debug, devmem, fd, page_size, "gpj1", 1);
	if (rc == -1) {
		printf("dump_gpio_infos failed with error %d\n", rc);

		/* TODO: return more precise error */
		return EX_UNAVAILABLE;
	}

	return 0;
}

int dump_flash_gps_status(int debug, char* devmem, int fd, size_t  page_size)
{
	int rc;

	rc = dump_gpio_infos(debug, devmem, fd, page_size, "gpl2", 1);
	if (rc == -1) {
		printf("dump_gpio_infos failed with error %d\n", rc);

		/* TODO: return more precise error */
		return EX_UNAVAILABLE;
	}

	rc = dump_gpio_infos(debug, devmem, fd, page_size, "gpl2", 2);
	if (rc == -1) {
		printf("dump_gpio_infos failed with error %d\n", rc);

		/* TODO: return more precise error */
		return EX_UNAVAILABLE;
	}

	return 0;
}

int dump_gpio_bank_status(int debug, char* devmem, int fd, size_t  page_size,
			  char* bank)
{
	int i;
	int nr_gpios;
	int rc;

	nr_gpios = get_bank_gpio_numbers(debug, bank);
	if (nr_gpios == -1) {
		/* The get_bank_gpio_numbers function
		 * already prints some error
		 */

		/* TODO: return more precise error */
		return EX_UNAVAILABLE;
	}

	for (i=0; i < nr_gpios; i++) {
		rc = dump_gpio_infos(debug, devmem, fd, page_size, bank, i);
		if (rc == -1) {
			printf("dump_gpio_infos failed with error %d\n", rc);

			/* TODO: return more precise error */
			return EX_UNAVAILABLE;
		}
	}

	return 0;
}

int main(int argc, char *argv[])
{
	int fd = 0;
	int rc = 0;
	int debug = 0;

	char *devmem = "/dev/mem";
	size_t page_size = 4096;

	assert(argc >= 1);

	if (argc != 2) {
		usage(argv[0]);
		return EX_USAGE;
	}

	if (!device_is_compatible(debug)) {
		printf("Exiting due to incompatible device\n");
		return 0;
	}

	/*
	 * TODO: check if root or tell the user to become root in case or
	 * access error
	 */

	fd = open(devmem, O_RDWR | O_SYNC);
	if (fd == -1) {
		rc = errno;
		printf("Opening %s failed with error %d: %s\n",
		       devmem, rc, strerror(rc));

		/* TODO: return more precise error */
		return EX_UNAVAILABLE;
	}

	if (argc == 2 && !strcmp("flash", argv[1])) {
		return dump_flash_gpio_status(debug, devmem, fd, page_size);
	} else if (argc == 2 && !strcmp("gpl1", argv[1])) {
		return dump_gpio_bank_status(debug, devmem, fd, page_size,
					     "gpl1");
	} else if (argc == 2 && !strcmp("gpl2", argv[1])) {
		return dump_gpio_bank_status(debug, devmem, fd, page_size,
					     "gpl2");
	} else if (argc == 2 && !strcmp("gps", argv[1])) {
		return dump_flash_gps_status(debug, devmem, fd, page_size);
	} else if (argc == 2 && !strcmp("gpy0", argv[1])) {
		return dump_gpio_bank_status(debug, devmem, fd, page_size,
					     "gpy0");
	} else if (argc == 2 && !strcmp("modem", argv[1])) {
		return dump_modem_gpio_infos(debug, devmem, fd, page_size);
	} else if (argc == 2 && !strcmp("test", argv[1])) {
		return run_tests(debug, devmem, fd, page_size);
	} else {
		usage(argv[0]);
		return EX_USAGE;
	}

	rc = close(fd);
	if (rc == -1) {
		rc = errno;
		printf("Closing %s failed with error %d: %s\n",
		       devmem, rc, strerror(rc));

		/* TODO: return more precise error */
		return EX_UNAVAILABLE;
	}

	return 0;
}