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, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
19 ***************************************************************************/
29 #include <jtag/interface.h>
32 /* arbitrary limit on host name length: */
33 #define REMOTE_BITBANG_HOST_MAX 255
35 #define REMOTE_BITBANG_RAISE_ERROR(expr ...) \
38 LOG_ERROR("Terminating openocd."); \
42 static char *remote_bitbang_host;
43 static char *remote_bitbang_port;
45 FILE *remote_bitbang_in;
46 FILE *remote_bitbang_out;
48 static void remote_bitbang_putc(int c)
50 if (EOF == fputc(c, remote_bitbang_out))
51 REMOTE_BITBANG_RAISE_ERROR("remote_bitbang_putc: %s", strerror(errno));
54 static int remote_bitbang_quit(void)
56 if (EOF == fputc('Q', remote_bitbang_out)) {
57 LOG_ERROR("fputs: %s", strerror(errno));
61 if (EOF == fflush(remote_bitbang_out)) {
62 LOG_ERROR("fflush: %s", strerror(errno));
66 /* We only need to close one of the FILE*s, because they both use the same */
67 /* underlying file descriptor. */
68 if (EOF == fclose(remote_bitbang_out)) {
69 LOG_ERROR("fclose: %s", strerror(errno));
73 free(remote_bitbang_host);
74 free(remote_bitbang_port);
76 LOG_INFO("remote_bitbang interface quit");
80 /* Get the next read response. */
81 static int remote_bitbang_rread(void)
83 if (EOF == fflush(remote_bitbang_out)) {
84 remote_bitbang_quit();
85 REMOTE_BITBANG_RAISE_ERROR("fflush: %s", strerror(errno));
88 int c = fgetc(remote_bitbang_in);
95 remote_bitbang_quit();
96 REMOTE_BITBANG_RAISE_ERROR(
97 "remote_bitbang: invalid read response: %c(%i)", c, c);
101 static int remote_bitbang_read(void)
103 remote_bitbang_putc('R');
104 return remote_bitbang_rread();
107 static void remote_bitbang_write(int tck, int tms, int tdi)
109 char c = '0' + ((tck ? 0x4 : 0x0) | (tms ? 0x2 : 0x0) | (tdi ? 0x1 : 0x0));
110 remote_bitbang_putc(c);
113 static void remote_bitbang_reset(int trst, int srst)
115 char c = 'r' + ((trst ? 0x2 : 0x0) | (srst ? 0x1 : 0x0));
116 remote_bitbang_putc(c);
119 static void remote_bitbang_blink(int on)
121 char c = on ? 'B' : 'b';
122 remote_bitbang_putc(c);
125 static struct bitbang_interface remote_bitbang_bitbang = {
126 .read = &remote_bitbang_read,
127 .write = &remote_bitbang_write,
128 .reset = &remote_bitbang_reset,
129 .blink = &remote_bitbang_blink,
132 static int remote_bitbang_init_tcp(void)
134 struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM };
135 struct addrinfo *result, *rp;
138 LOG_INFO("Connecting to %s:%s",
139 remote_bitbang_host ? remote_bitbang_host : "localhost",
140 remote_bitbang_port);
142 /* Obtain address(es) matching host/port */
143 int s = getaddrinfo(remote_bitbang_host, remote_bitbang_port, &hints, &result);
145 LOG_ERROR("getaddrinfo: %s\n", gai_strerror(s));
149 /* getaddrinfo() returns a list of address structures.
150 Try each address until we successfully connect(2).
151 If socket(2) (or connect(2)) fails, we (close the socket
152 and) try the next address. */
154 for (rp = result; rp != NULL ; rp = rp->ai_next) {
155 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
159 if (connect(fd, rp->ai_addr, rp->ai_addrlen) != -1)
165 freeaddrinfo(result); /* No longer needed */
167 if (rp == NULL) { /* No address succeeded */
168 LOG_ERROR("Failed to connect: %s", strerror(errno));
175 static int remote_bitbang_init_unix(void)
177 if (remote_bitbang_host == NULL) {
178 LOG_ERROR("host/socket not specified");
182 LOG_INFO("Connecting to unix socket %s", remote_bitbang_host);
183 int fd = socket(PF_UNIX, SOCK_STREAM, 0);
185 LOG_ERROR("socket: %s", strerror(errno));
189 struct sockaddr_un addr;
190 addr.sun_family = AF_UNIX;
191 strncpy(addr.sun_path, remote_bitbang_host, sizeof(addr.sun_path));
192 addr.sun_path[sizeof(addr.sun_path)-1] = '\0';
194 if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
195 LOG_ERROR("connect: %s", strerror(errno));
202 static int remote_bitbang_init(void)
205 bitbang_interface = &remote_bitbang_bitbang;
207 LOG_INFO("Initializing remote_bitbang driver");
208 if (remote_bitbang_port == NULL)
209 fd = remote_bitbang_init_unix();
211 fd = remote_bitbang_init_tcp();
216 remote_bitbang_in = fdopen(fd, "r");
217 if (remote_bitbang_in == NULL) {
218 LOG_ERROR("fdopen: failed to open read stream");
223 remote_bitbang_out = fdopen(fd, "w");
224 if (remote_bitbang_out == NULL) {
225 LOG_ERROR("fdopen: failed to open write stream");
226 fclose(remote_bitbang_in);
230 LOG_INFO("remote_bitbang driver initialized");
234 COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_port_command)
238 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
239 free(remote_bitbang_port);
240 remote_bitbang_port = port == 0 ? NULL : strdup(CMD_ARGV[0]);
243 return ERROR_COMMAND_SYNTAX_ERROR;
246 COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_host_command)
249 free(remote_bitbang_host);
250 remote_bitbang_host = strdup(CMD_ARGV[0]);
253 return ERROR_COMMAND_SYNTAX_ERROR;
256 static const struct command_registration remote_bitbang_command_handlers[] = {
258 .name = "remote_bitbang_port",
259 .handler = remote_bitbang_handle_remote_bitbang_port_command,
260 .mode = COMMAND_CONFIG,
261 .help = "Set the port to use to connect to the remote jtag.\n"
262 " if 0 or unset, use unix sockets to connect to the remote jtag.",
263 .usage = "port_number",
266 .name = "remote_bitbang_host",
267 .handler = remote_bitbang_handle_remote_bitbang_host_command,
268 .mode = COMMAND_CONFIG,
269 .help = "Set the host to use to connect to the remote jtag.\n"
270 " if port is 0 or unset, this is the name of the unix socket to use.",
271 .usage = "host_name",
273 COMMAND_REGISTRATION_DONE,
276 struct jtag_interface remote_bitbang_interface = {
277 .name = "remote_bitbang",
278 .execute_queue = &bitbang_execute_queue,
279 .commands = remote_bitbang_command_handlers,
280 .init = &remote_bitbang_init,
281 .quit = &remote_bitbang_quit,