Remove FSF address from GPL notices
[openocd.git] / src / jtag / drivers / remote_bitbang.c
1 /***************************************************************************
2 * Copyright (C) 2011 by Richard Uhler *
3 * ruhler@mit.edu *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #ifndef _WIN32
24 #include <sys/un.h>
25 #include <netdb.h>
26 #endif
27 #include <jtag/interface.h>
28 #include "bitbang.h"
29
30 /* arbitrary limit on host name length: */
31 #define REMOTE_BITBANG_HOST_MAX 255
32
33 #define REMOTE_BITBANG_RAISE_ERROR(expr ...) \
34 do { \
35 LOG_ERROR(expr); \
36 LOG_ERROR("Terminating openocd."); \
37 exit(-1); \
38 } while (0)
39
40 static char *remote_bitbang_host;
41 static char *remote_bitbang_port;
42
43 FILE *remote_bitbang_in;
44 FILE *remote_bitbang_out;
45
46 static void remote_bitbang_putc(int c)
47 {
48 if (EOF == fputc(c, remote_bitbang_out))
49 REMOTE_BITBANG_RAISE_ERROR("remote_bitbang_putc: %s", strerror(errno));
50 }
51
52 static int remote_bitbang_quit(void)
53 {
54 if (EOF == fputc('Q', remote_bitbang_out)) {
55 LOG_ERROR("fputs: %s", strerror(errno));
56 return ERROR_FAIL;
57 }
58
59 if (EOF == fflush(remote_bitbang_out)) {
60 LOG_ERROR("fflush: %s", strerror(errno));
61 return ERROR_FAIL;
62 }
63
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));
68 return ERROR_FAIL;
69 }
70
71 free(remote_bitbang_host);
72 free(remote_bitbang_port);
73
74 LOG_INFO("remote_bitbang interface quit");
75 return ERROR_OK;
76 }
77
78 /* Get the next read response. */
79 static int remote_bitbang_rread(void)
80 {
81 if (EOF == fflush(remote_bitbang_out)) {
82 remote_bitbang_quit();
83 REMOTE_BITBANG_RAISE_ERROR("fflush: %s", strerror(errno));
84 }
85
86 int c = fgetc(remote_bitbang_in);
87 switch (c) {
88 case '0':
89 return 0;
90 case '1':
91 return 1;
92 default:
93 remote_bitbang_quit();
94 REMOTE_BITBANG_RAISE_ERROR(
95 "remote_bitbang: invalid read response: %c(%i)", c, c);
96 }
97 }
98
99 static int remote_bitbang_read(void)
100 {
101 remote_bitbang_putc('R');
102 return remote_bitbang_rread();
103 }
104
105 static void remote_bitbang_write(int tck, int tms, int tdi)
106 {
107 char c = '0' + ((tck ? 0x4 : 0x0) | (tms ? 0x2 : 0x0) | (tdi ? 0x1 : 0x0));
108 remote_bitbang_putc(c);
109 }
110
111 static void remote_bitbang_reset(int trst, int srst)
112 {
113 char c = 'r' + ((trst ? 0x2 : 0x0) | (srst ? 0x1 : 0x0));
114 remote_bitbang_putc(c);
115 }
116
117 static void remote_bitbang_blink(int on)
118 {
119 char c = on ? 'B' : 'b';
120 remote_bitbang_putc(c);
121 }
122
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,
128 };
129
130 static int remote_bitbang_init_tcp(void)
131 {
132 struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM };
133 struct addrinfo *result, *rp;
134 int fd;
135
136 LOG_INFO("Connecting to %s:%s",
137 remote_bitbang_host ? remote_bitbang_host : "localhost",
138 remote_bitbang_port);
139
140 /* Obtain address(es) matching host/port */
141 int s = getaddrinfo(remote_bitbang_host, remote_bitbang_port, &hints, &result);
142 if (s != 0) {
143 LOG_ERROR("getaddrinfo: %s\n", gai_strerror(s));
144 return ERROR_FAIL;
145 }
146
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. */
151
152 for (rp = result; rp != NULL ; rp = rp->ai_next) {
153 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
154 if (fd == -1)
155 continue;
156
157 if (connect(fd, rp->ai_addr, rp->ai_addrlen) != -1)
158 break; /* Success */
159
160 close(fd);
161 }
162
163 freeaddrinfo(result); /* No longer needed */
164
165 if (rp == NULL) { /* No address succeeded */
166 LOG_ERROR("Failed to connect: %s", strerror(errno));
167 return ERROR_FAIL;
168 }
169
170 return fd;
171 }
172
173 static int remote_bitbang_init_unix(void)
174 {
175 if (remote_bitbang_host == NULL) {
176 LOG_ERROR("host/socket not specified");
177 return ERROR_FAIL;
178 }
179
180 LOG_INFO("Connecting to unix socket %s", remote_bitbang_host);
181 int fd = socket(PF_UNIX, SOCK_STREAM, 0);
182 if (fd < 0) {
183 LOG_ERROR("socket: %s", strerror(errno));
184 return ERROR_FAIL;
185 }
186
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';
191
192 if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
193 LOG_ERROR("connect: %s", strerror(errno));
194 return ERROR_FAIL;
195 }
196
197 return fd;
198 }
199
200 static int remote_bitbang_init(void)
201 {
202 int fd;
203 bitbang_interface = &remote_bitbang_bitbang;
204
205 LOG_INFO("Initializing remote_bitbang driver");
206 if (remote_bitbang_port == NULL)
207 fd = remote_bitbang_init_unix();
208 else
209 fd = remote_bitbang_init_tcp();
210
211 if (fd < 0)
212 return fd;
213
214 remote_bitbang_in = fdopen(fd, "r");
215 if (remote_bitbang_in == NULL) {
216 LOG_ERROR("fdopen: failed to open read stream");
217 close(fd);
218 return ERROR_FAIL;
219 }
220
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);
225 return ERROR_FAIL;
226 }
227
228 LOG_INFO("remote_bitbang driver initialized");
229 return ERROR_OK;
230 }
231
232 COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_port_command)
233 {
234 if (CMD_ARGC == 1) {
235 uint16_t port;
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]);
239 return ERROR_OK;
240 }
241 return ERROR_COMMAND_SYNTAX_ERROR;
242 }
243
244 COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_host_command)
245 {
246 if (CMD_ARGC == 1) {
247 free(remote_bitbang_host);
248 remote_bitbang_host = strdup(CMD_ARGV[0]);
249 return ERROR_OK;
250 }
251 return ERROR_COMMAND_SYNTAX_ERROR;
252 }
253
254 static const struct command_registration remote_bitbang_command_handlers[] = {
255 {
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",
262 },
263 {
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",
270 },
271 COMMAND_REGISTRATION_DONE,
272 };
273
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,
280 };