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 "helper/system.h"
28 #include "helper/replacements.h"
29 #include <jtag/interface.h>
32 /* arbitrary limit on host name length: */
33 #define REMOTE_BITBANG_HOST_MAX 255
35 static char *remote_bitbang_host;
36 static char *remote_bitbang_port;
38 static int remote_bitbang_fd;
40 /* Circular buffer. When start == end, the buffer is empty. */
41 static char remote_bitbang_buf[64];
42 static unsigned remote_bitbang_start;
43 static unsigned remote_bitbang_end;
45 static int remote_bitbang_buf_full(void)
47 return remote_bitbang_end ==
48 ((remote_bitbang_start + sizeof(remote_bitbang_buf) - 1) %
49 sizeof(remote_bitbang_buf));
52 /* Read any incoming data, placing it into the buffer. */
53 static int remote_bitbang_fill_buf(void)
55 socket_nonblock(remote_bitbang_fd);
56 while (!remote_bitbang_buf_full()) {
57 unsigned contiguous_available_space;
58 if (remote_bitbang_end >= remote_bitbang_start) {
59 contiguous_available_space = sizeof(remote_bitbang_buf) -
61 if (remote_bitbang_start == 0)
62 contiguous_available_space -= 1;
64 contiguous_available_space = remote_bitbang_start -
65 remote_bitbang_end - 1;
67 ssize_t count = read_socket(remote_bitbang_fd,
68 remote_bitbang_buf + remote_bitbang_end,
69 contiguous_available_space);
71 remote_bitbang_end += count;
72 if (remote_bitbang_end == sizeof(remote_bitbang_buf))
73 remote_bitbang_end = 0;
74 } else if (count == 0) {
76 } else if (count < 0) {
78 if (WSAGetLastError() == WSAEWOULDBLOCK) {
80 if (errno == EAGAIN) {
84 log_socket_error("remote_bitbang_fill_buf");
93 static int remote_bitbang_putc(int c)
96 ssize_t count = write_socket(remote_bitbang_fd, &buf, sizeof(buf));
98 log_socket_error("remote_bitbang_putc");
104 static int remote_bitbang_quit(void)
106 if (remote_bitbang_putc('Q') == ERROR_FAIL)
109 if (close_socket(remote_bitbang_fd) != 0) {
110 log_socket_error("close_socket");
114 free(remote_bitbang_host);
115 free(remote_bitbang_port);
117 LOG_INFO("remote_bitbang interface quit");
121 static bb_value_t char_to_int(int c)
129 remote_bitbang_quit();
130 LOG_ERROR("remote_bitbang: invalid read response: %c(%i)", c, c);
135 /* Get the next read response. */
136 static bb_value_t remote_bitbang_rread(void)
138 /* Enable blocking access. */
139 socket_block(remote_bitbang_fd);
141 ssize_t count = read_socket(remote_bitbang_fd, &c, 1);
143 return char_to_int(c);
145 remote_bitbang_quit();
146 LOG_ERROR("read_socket: count=%d", (int) count);
147 log_socket_error("read_socket");
152 static int remote_bitbang_sample(void)
154 if (remote_bitbang_fill_buf() != ERROR_OK)
156 assert(!remote_bitbang_buf_full());
157 return remote_bitbang_putc('R');
160 static bb_value_t remote_bitbang_read_sample(void)
162 if (remote_bitbang_start != remote_bitbang_end) {
163 int c = remote_bitbang_buf[remote_bitbang_start];
164 remote_bitbang_start =
165 (remote_bitbang_start + 1) % sizeof(remote_bitbang_buf);
166 return char_to_int(c);
168 return remote_bitbang_rread();
171 static int remote_bitbang_write(int tck, int tms, int tdi)
173 char c = '0' + ((tck ? 0x4 : 0x0) | (tms ? 0x2 : 0x0) | (tdi ? 0x1 : 0x0));
174 return remote_bitbang_putc(c);
177 static int remote_bitbang_reset(int trst, int srst)
179 char c = 'r' + ((trst ? 0x2 : 0x0) | (srst ? 0x1 : 0x0));
180 return remote_bitbang_putc(c);
183 static int remote_bitbang_blink(int on)
185 char c = on ? 'B' : 'b';
186 return remote_bitbang_putc(c);
189 static struct bitbang_interface remote_bitbang_bitbang = {
190 .buf_size = sizeof(remote_bitbang_buf) - 1,
191 .sample = &remote_bitbang_sample,
192 .read_sample = &remote_bitbang_read_sample,
193 .write = &remote_bitbang_write,
194 .blink = &remote_bitbang_blink,
197 static int remote_bitbang_init_tcp(void)
199 struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM };
200 struct addrinfo *result, *rp;
203 LOG_INFO("Connecting to %s:%s",
204 remote_bitbang_host ? remote_bitbang_host : "localhost",
205 remote_bitbang_port);
207 /* Obtain address(es) matching host/port */
208 int s = getaddrinfo(remote_bitbang_host, remote_bitbang_port, &hints, &result);
210 LOG_ERROR("getaddrinfo: %s\n", gai_strerror(s));
214 /* getaddrinfo() returns a list of address structures.
215 Try each address until we successfully connect(2).
216 If socket(2) (or connect(2)) fails, we (close the socket
217 and) try the next address. */
219 for (rp = result; rp != NULL ; rp = rp->ai_next) {
220 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
224 if (connect(fd, rp->ai_addr, rp->ai_addrlen) != -1)
230 freeaddrinfo(result); /* No longer needed */
232 if (rp == NULL) { /* No address succeeded */
233 log_socket_error("Failed to connect");
240 static int remote_bitbang_init_unix(void)
242 if (remote_bitbang_host == NULL) {
243 LOG_ERROR("host/socket not specified");
247 LOG_INFO("Connecting to unix socket %s", remote_bitbang_host);
248 int fd = socket(PF_UNIX, SOCK_STREAM, 0);
250 log_socket_error("socket");
254 struct sockaddr_un addr;
255 addr.sun_family = AF_UNIX;
256 strncpy(addr.sun_path, remote_bitbang_host, sizeof(addr.sun_path));
257 addr.sun_path[sizeof(addr.sun_path)-1] = '\0';
259 if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
260 log_socket_error("connect");
267 static int remote_bitbang_init(void)
269 bitbang_interface = &remote_bitbang_bitbang;
271 remote_bitbang_start = 0;
272 remote_bitbang_end = 0;
274 LOG_INFO("Initializing remote_bitbang driver");
275 if (remote_bitbang_port == NULL)
276 remote_bitbang_fd = remote_bitbang_init_unix();
278 remote_bitbang_fd = remote_bitbang_init_tcp();
280 if (remote_bitbang_fd < 0)
281 return remote_bitbang_fd;
283 LOG_INFO("remote_bitbang driver initialized");
287 COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_port_command)
291 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
292 free(remote_bitbang_port);
293 remote_bitbang_port = port == 0 ? NULL : strdup(CMD_ARGV[0]);
296 return ERROR_COMMAND_SYNTAX_ERROR;
299 COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_host_command)
302 free(remote_bitbang_host);
303 remote_bitbang_host = strdup(CMD_ARGV[0]);
306 return ERROR_COMMAND_SYNTAX_ERROR;
309 static const struct command_registration remote_bitbang_command_handlers[] = {
311 .name = "remote_bitbang_port",
312 .handler = remote_bitbang_handle_remote_bitbang_port_command,
313 .mode = COMMAND_CONFIG,
314 .help = "Set the port to use to connect to the remote jtag.\n"
315 " if 0 or unset, use unix sockets to connect to the remote jtag.",
316 .usage = "port_number",
319 .name = "remote_bitbang_host",
320 .handler = remote_bitbang_handle_remote_bitbang_host_command,
321 .mode = COMMAND_CONFIG,
322 .help = "Set the host to use to connect to the remote jtag.\n"
323 " if port is 0 or unset, this is the name of the unix socket to use.",
324 .usage = "host_name",
326 COMMAND_REGISTRATION_DONE,
329 static struct jtag_interface remote_bitbang_interface = {
330 .execute_queue = &bitbang_execute_queue,
333 struct adapter_driver remote_bitbang_adapter_driver = {
334 .name = "remote_bitbang",
335 .transports = jtag_only,
336 .commands = remote_bitbang_command_handlers,
338 .init = &remote_bitbang_init,
339 .quit = &remote_bitbang_quit,
340 .reset = &remote_bitbang_reset,
342 .jtag_ops = &remote_bitbang_interface,