1 /***************************************************************************
2 * Copyright (C) 2011 by Richard Uhler *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
27 #include <jtag/interface.h>
30 /* arbitrary limit on host name length: */
31 #define REMOTE_BITBANG_HOST_MAX 255
33 #define REMOTE_BITBANG_RAISE_ERROR(expr ...) \
36 LOG_ERROR("Terminating openocd."); \
40 static char *remote_bitbang_host;
41 static char *remote_bitbang_port;
43 FILE *remote_bitbang_in;
44 FILE *remote_bitbang_out;
46 static void remote_bitbang_putc(int c)
48 if (EOF == fputc(c, remote_bitbang_out))
49 REMOTE_BITBANG_RAISE_ERROR("remote_bitbang_putc: %s", strerror(errno));
52 static int remote_bitbang_quit(void)
54 if (EOF == fputc('Q', remote_bitbang_out)) {
55 LOG_ERROR("fputs: %s", strerror(errno));
59 if (EOF == fflush(remote_bitbang_out)) {
60 LOG_ERROR("fflush: %s", strerror(errno));
64 /* We only need to close one of the FILE*s, because they both use the same */
65 /* underlying file descriptor. */
66 if (EOF == fclose(remote_bitbang_out)) {
67 LOG_ERROR("fclose: %s", strerror(errno));
71 free(remote_bitbang_host);
72 free(remote_bitbang_port);
74 LOG_INFO("remote_bitbang interface quit");
78 /* Get the next read response. */
79 static int remote_bitbang_rread(void)
81 if (EOF == fflush(remote_bitbang_out)) {
82 remote_bitbang_quit();
83 REMOTE_BITBANG_RAISE_ERROR("fflush: %s", strerror(errno));
86 int c = fgetc(remote_bitbang_in);
93 remote_bitbang_quit();
94 REMOTE_BITBANG_RAISE_ERROR(
95 "remote_bitbang: invalid read response: %c(%i)", c, c);
99 static int remote_bitbang_read(void)
101 remote_bitbang_putc('R');
102 return remote_bitbang_rread();
105 static void remote_bitbang_write(int tck, int tms, int tdi)
107 char c = '0' + ((tck ? 0x4 : 0x0) | (tms ? 0x2 : 0x0) | (tdi ? 0x1 : 0x0));
108 remote_bitbang_putc(c);
111 static void remote_bitbang_reset(int trst, int srst)
113 char c = 'r' + ((trst ? 0x2 : 0x0) | (srst ? 0x1 : 0x0));
114 remote_bitbang_putc(c);
117 static void remote_bitbang_blink(int on)
119 char c = on ? 'B' : 'b';
120 remote_bitbang_putc(c);
123 static struct bitbang_interface remote_bitbang_bitbang = {
124 .read = &remote_bitbang_read,
125 .write = &remote_bitbang_write,
126 .reset = &remote_bitbang_reset,
127 .blink = &remote_bitbang_blink,
130 static int remote_bitbang_init_tcp(void)
132 struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM };
133 struct addrinfo *result, *rp;
136 LOG_INFO("Connecting to %s:%s",
137 remote_bitbang_host ? remote_bitbang_host : "localhost",
138 remote_bitbang_port);
140 /* Obtain address(es) matching host/port */
141 int s = getaddrinfo(remote_bitbang_host, remote_bitbang_port, &hints, &result);
143 LOG_ERROR("getaddrinfo: %s\n", gai_strerror(s));
147 /* getaddrinfo() returns a list of address structures.
148 Try each address until we successfully connect(2).
149 If socket(2) (or connect(2)) fails, we (close the socket
150 and) try the next address. */
152 for (rp = result; rp != NULL ; rp = rp->ai_next) {
153 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
157 if (connect(fd, rp->ai_addr, rp->ai_addrlen) != -1)
163 freeaddrinfo(result); /* No longer needed */
165 if (rp == NULL) { /* No address succeeded */
166 LOG_ERROR("Failed to connect: %s", strerror(errno));
173 static int remote_bitbang_init_unix(void)
175 if (remote_bitbang_host == NULL) {
176 LOG_ERROR("host/socket not specified");
180 LOG_INFO("Connecting to unix socket %s", remote_bitbang_host);
181 int fd = socket(PF_UNIX, SOCK_STREAM, 0);
183 LOG_ERROR("socket: %s", strerror(errno));
187 struct sockaddr_un addr;
188 addr.sun_family = AF_UNIX;
189 strncpy(addr.sun_path, remote_bitbang_host, sizeof(addr.sun_path));
190 addr.sun_path[sizeof(addr.sun_path)-1] = '\0';
192 if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
193 LOG_ERROR("connect: %s", strerror(errno));
200 static int remote_bitbang_init(void)
203 bitbang_interface = &remote_bitbang_bitbang;
205 LOG_INFO("Initializing remote_bitbang driver");
206 if (remote_bitbang_port == NULL)
207 fd = remote_bitbang_init_unix();
209 fd = remote_bitbang_init_tcp();
214 remote_bitbang_in = fdopen(fd, "r");
215 if (remote_bitbang_in == NULL) {
216 LOG_ERROR("fdopen: failed to open read stream");
221 remote_bitbang_out = fdopen(fd, "w");
222 if (remote_bitbang_out == NULL) {
223 LOG_ERROR("fdopen: failed to open write stream");
224 fclose(remote_bitbang_in);
228 LOG_INFO("remote_bitbang driver initialized");
232 COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_port_command)
236 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
237 free(remote_bitbang_port);
238 remote_bitbang_port = port == 0 ? NULL : strdup(CMD_ARGV[0]);
241 return ERROR_COMMAND_SYNTAX_ERROR;
244 COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_host_command)
247 free(remote_bitbang_host);
248 remote_bitbang_host = strdup(CMD_ARGV[0]);
251 return ERROR_COMMAND_SYNTAX_ERROR;
254 static const struct command_registration remote_bitbang_command_handlers[] = {
256 .name = "remote_bitbang_port",
257 .handler = remote_bitbang_handle_remote_bitbang_port_command,
258 .mode = COMMAND_CONFIG,
259 .help = "Set the port to use to connect to the remote jtag.\n"
260 " if 0 or unset, use unix sockets to connect to the remote jtag.",
261 .usage = "port_number",
264 .name = "remote_bitbang_host",
265 .handler = remote_bitbang_handle_remote_bitbang_host_command,
266 .mode = COMMAND_CONFIG,
267 .help = "Set the host to use to connect to the remote jtag.\n"
268 " if port is 0 or unset, this is the name of the unix socket to use.",
269 .usage = "host_name",
271 COMMAND_REGISTRATION_DONE,
274 struct jtag_interface remote_bitbang_interface = {
275 .name = "remote_bitbang",
276 .execute_queue = &bitbang_execute_queue,
277 .commands = remote_bitbang_command_handlers,
278 .init = &remote_bitbang_init,
279 .quit = &remote_bitbang_quit,