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 static char *remote_bitbang_host;
34 static char *remote_bitbang_port;
36 static FILE *remote_bitbang_file;
37 static int remote_bitbang_fd;
39 /* Circular buffer. When start == end, the buffer is empty. */
40 static char remote_bitbang_buf[64];
41 static unsigned remote_bitbang_start;
42 static unsigned remote_bitbang_end;
44 static int remote_bitbang_buf_full(void)
46 return remote_bitbang_end ==
47 ((remote_bitbang_start + sizeof(remote_bitbang_buf) - 1) %
48 sizeof(remote_bitbang_buf));
51 /* Read any incoming data, placing it into the buffer. */
52 static int remote_bitbang_fill_buf(void)
54 socket_nonblock(remote_bitbang_fd);
55 while (!remote_bitbang_buf_full()) {
56 unsigned contiguous_available_space;
57 if (remote_bitbang_end >= remote_bitbang_start) {
58 contiguous_available_space = sizeof(remote_bitbang_buf) -
60 if (remote_bitbang_start == 0)
61 contiguous_available_space -= 1;
63 contiguous_available_space = remote_bitbang_start -
64 remote_bitbang_end - 1;
66 ssize_t count = read(remote_bitbang_fd,
67 remote_bitbang_buf + remote_bitbang_end,
68 contiguous_available_space);
70 remote_bitbang_end += count;
71 if (remote_bitbang_end == sizeof(remote_bitbang_buf))
72 remote_bitbang_end = 0;
73 } else if (count == 0) {
75 } else if (count < 0) {
76 if (errno == EAGAIN) {
79 LOG_ERROR("remote_bitbang_fill_buf: %s (%d)",
80 strerror(errno), errno);
89 static int remote_bitbang_putc(int c)
91 if (EOF == fputc(c, remote_bitbang_file)) {
92 LOG_ERROR("remote_bitbang_putc: %s", strerror(errno));
98 static int remote_bitbang_quit(void)
100 if (EOF == fputc('Q', remote_bitbang_file)) {
101 LOG_ERROR("fputs: %s", strerror(errno));
105 if (EOF == fflush(remote_bitbang_file)) {
106 LOG_ERROR("fflush: %s", strerror(errno));
110 /* We only need to close one of the FILE*s, because they both use the same */
111 /* underlying file descriptor. */
112 if (EOF == fclose(remote_bitbang_file)) {
113 LOG_ERROR("fclose: %s", strerror(errno));
117 free(remote_bitbang_host);
118 free(remote_bitbang_port);
120 LOG_INFO("remote_bitbang interface quit");
124 static bb_value_t char_to_int(int c)
132 remote_bitbang_quit();
133 LOG_ERROR("remote_bitbang: invalid read response: %c(%i)", c, c);
138 /* Get the next read response. */
139 static bb_value_t remote_bitbang_rread(void)
141 if (EOF == fflush(remote_bitbang_file)) {
142 remote_bitbang_quit();
143 LOG_ERROR("fflush: %s", strerror(errno));
147 /* Enable blocking access. */
148 socket_block(remote_bitbang_fd);
150 ssize_t count = read(remote_bitbang_fd, &c, 1);
152 return char_to_int(c);
154 remote_bitbang_quit();
155 LOG_ERROR("read: count=%d, error=%s", (int) count, strerror(errno));
160 static int remote_bitbang_sample(void)
162 if (remote_bitbang_fill_buf() != ERROR_OK)
164 assert(!remote_bitbang_buf_full());
165 return remote_bitbang_putc('R');
168 static bb_value_t remote_bitbang_read_sample(void)
170 if (remote_bitbang_start != remote_bitbang_end) {
171 int c = remote_bitbang_buf[remote_bitbang_start];
172 remote_bitbang_start =
173 (remote_bitbang_start + 1) % sizeof(remote_bitbang_buf);
174 return char_to_int(c);
176 return remote_bitbang_rread();
179 static int remote_bitbang_write(int tck, int tms, int tdi)
181 char c = '0' + ((tck ? 0x4 : 0x0) | (tms ? 0x2 : 0x0) | (tdi ? 0x1 : 0x0));
182 return remote_bitbang_putc(c);
185 static int remote_bitbang_reset(int trst, int srst)
187 char c = 'r' + ((trst ? 0x2 : 0x0) | (srst ? 0x1 : 0x0));
188 return remote_bitbang_putc(c);
191 static int remote_bitbang_blink(int on)
193 char c = on ? 'B' : 'b';
194 return remote_bitbang_putc(c);
197 static struct bitbang_interface remote_bitbang_bitbang = {
198 .buf_size = sizeof(remote_bitbang_buf) - 1,
199 .sample = &remote_bitbang_sample,
200 .read_sample = &remote_bitbang_read_sample,
201 .write = &remote_bitbang_write,
202 .blink = &remote_bitbang_blink,
205 static int remote_bitbang_init_tcp(void)
207 struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM };
208 struct addrinfo *result, *rp;
211 LOG_INFO("Connecting to %s:%s",
212 remote_bitbang_host ? remote_bitbang_host : "localhost",
213 remote_bitbang_port);
215 /* Obtain address(es) matching host/port */
216 int s = getaddrinfo(remote_bitbang_host, remote_bitbang_port, &hints, &result);
218 LOG_ERROR("getaddrinfo: %s\n", gai_strerror(s));
222 /* getaddrinfo() returns a list of address structures.
223 Try each address until we successfully connect(2).
224 If socket(2) (or connect(2)) fails, we (close the socket
225 and) try the next address. */
227 for (rp = result; rp != NULL ; rp = rp->ai_next) {
228 fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
232 if (connect(fd, rp->ai_addr, rp->ai_addrlen) != -1)
238 freeaddrinfo(result); /* No longer needed */
240 if (rp == NULL) { /* No address succeeded */
241 LOG_ERROR("Failed to connect: %s", strerror(errno));
248 static int remote_bitbang_init_unix(void)
250 if (remote_bitbang_host == NULL) {
251 LOG_ERROR("host/socket not specified");
255 LOG_INFO("Connecting to unix socket %s", remote_bitbang_host);
256 int fd = socket(PF_UNIX, SOCK_STREAM, 0);
258 LOG_ERROR("socket: %s", strerror(errno));
262 struct sockaddr_un addr;
263 addr.sun_family = AF_UNIX;
264 strncpy(addr.sun_path, remote_bitbang_host, sizeof(addr.sun_path));
265 addr.sun_path[sizeof(addr.sun_path)-1] = '\0';
267 if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
268 LOG_ERROR("connect: %s", strerror(errno));
275 static int remote_bitbang_init(void)
277 bitbang_interface = &remote_bitbang_bitbang;
279 remote_bitbang_start = 0;
280 remote_bitbang_end = 0;
282 LOG_INFO("Initializing remote_bitbang driver");
283 if (remote_bitbang_port == NULL)
284 remote_bitbang_fd = remote_bitbang_init_unix();
286 remote_bitbang_fd = remote_bitbang_init_tcp();
288 if (remote_bitbang_fd < 0)
289 return remote_bitbang_fd;
291 remote_bitbang_file = fdopen(remote_bitbang_fd, "w+");
292 if (remote_bitbang_file == NULL) {
293 LOG_ERROR("fdopen: failed to open write stream");
294 close(remote_bitbang_fd);
298 LOG_INFO("remote_bitbang driver initialized");
302 COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_port_command)
306 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
307 free(remote_bitbang_port);
308 remote_bitbang_port = port == 0 ? NULL : strdup(CMD_ARGV[0]);
311 return ERROR_COMMAND_SYNTAX_ERROR;
314 COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_host_command)
317 free(remote_bitbang_host);
318 remote_bitbang_host = strdup(CMD_ARGV[0]);
321 return ERROR_COMMAND_SYNTAX_ERROR;
324 static const struct command_registration remote_bitbang_command_handlers[] = {
326 .name = "remote_bitbang_port",
327 .handler = remote_bitbang_handle_remote_bitbang_port_command,
328 .mode = COMMAND_CONFIG,
329 .help = "Set the port to use to connect to the remote jtag.\n"
330 " if 0 or unset, use unix sockets to connect to the remote jtag.",
331 .usage = "port_number",
334 .name = "remote_bitbang_host",
335 .handler = remote_bitbang_handle_remote_bitbang_host_command,
336 .mode = COMMAND_CONFIG,
337 .help = "Set the host to use to connect to the remote jtag.\n"
338 " if port is 0 or unset, this is the name of the unix socket to use.",
339 .usage = "host_name",
341 COMMAND_REGISTRATION_DONE,
344 static struct jtag_interface remote_bitbang_interface = {
345 .execute_queue = &bitbang_execute_queue,
348 struct adapter_driver remote_bitbang_adapter_driver = {
349 .name = "remote_bitbang",
350 .transports = jtag_only,
351 .commands = remote_bitbang_command_handlers,
353 .init = &remote_bitbang_init,
354 .quit = &remote_bitbang_quit,
355 .reset = &remote_bitbang_reset,
357 .jtag_ops = &remote_bitbang_interface,