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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
29 #include <jtag/interface.h>
33 #define UNIX_PATH_LEN 108
36 /* arbitrary limit on host name length: */
37 #define REMOTE_BITBANG_HOST_MAX 255
39 #define REMOTE_BITBANG_RAISE_ERROR(expr ...) \
42 LOG_ERROR("Terminating openocd."); \
46 static char remote_bitbang_host
[REMOTE_BITBANG_HOST_MAX
] = "openocd";
47 static uint16_t remote_bitbang_port
;
49 FILE *remote_bitbang_in
;
50 FILE *remote_bitbang_out
;
52 static void remote_bitbang_putc(int c
)
54 if (EOF
== fputc(c
, remote_bitbang_out
))
55 REMOTE_BITBANG_RAISE_ERROR("remote_bitbang_putc: %s", strerror(errno
));
58 static int remote_bitbang_quit(void)
60 if (EOF
== fputc('Q', remote_bitbang_out
)) {
61 LOG_ERROR("fputs: %s", strerror(errno
));
65 if (EOF
== fflush(remote_bitbang_out
)) {
66 LOG_ERROR("fflush: %s", strerror(errno
));
70 /* We only need to close one of the FILE*s, because they both use the same */
71 /* underlying file descriptor. */
72 if (EOF
== fclose(remote_bitbang_out
)) {
73 LOG_ERROR("fclose: %s", strerror(errno
));
77 LOG_INFO("remote_bitbang interface quit");
81 /* Get the next read response. */
82 static int remote_bitbang_rread(void)
84 if (EOF
== fflush(remote_bitbang_out
)) {
85 remote_bitbang_quit();
86 REMOTE_BITBANG_RAISE_ERROR("fflush: %s", strerror(errno
));
89 int c
= fgetc(remote_bitbang_in
);
96 remote_bitbang_quit();
97 REMOTE_BITBANG_RAISE_ERROR(
98 "remote_bitbang: invalid read response: %c(%i)", c
, c
);
102 static int remote_bitbang_read(void)
104 remote_bitbang_putc('R');
105 return remote_bitbang_rread();
108 static void remote_bitbang_write(int tck
, int tms
, int tdi
)
110 char c
= '0' + ((tck
? 0x4 : 0x0) | (tms
? 0x2 : 0x0) | (tdi
? 0x1 : 0x0));
111 remote_bitbang_putc(c
);
114 static void remote_bitbang_reset(int trst
, int srst
)
116 char c
= 'r' + ((trst
? 0x2 : 0x0) | (srst
? 0x1 : 0x0));
117 remote_bitbang_putc(c
);
120 static void remote_bitbang_blink(int on
)
122 char c
= on
? 'B' : 'b';
123 remote_bitbang_putc(c
);
126 static struct bitbang_interface remote_bitbang_bitbang
= {
127 .read
= &remote_bitbang_read
,
128 .write
= &remote_bitbang_write
,
129 .reset
= &remote_bitbang_reset
,
130 .blink
= &remote_bitbang_blink
,
133 static int remote_bitbang_init_tcp(void)
135 LOG_INFO("Connecting to %s:%i", remote_bitbang_host
, remote_bitbang_port
);
136 int fd
= socket(PF_INET
, SOCK_STREAM
, 0);
138 LOG_ERROR("socket: %s", strerror(errno
));
142 struct hostent
*hent
= gethostbyname(remote_bitbang_host
);
144 char *errorstr
= "???";
147 errorstr
= "host not found";
150 errorstr
= "no address";
153 errorstr
= "no recovery";
156 errorstr
= "try again";
159 LOG_ERROR("gethostbyname: %s", errorstr
);
163 struct sockaddr_in addr
;
164 addr
.sin_family
= AF_INET
;
165 addr
.sin_port
= htons(remote_bitbang_port
);
166 addr
.sin_addr
= *(struct in_addr
*)hent
->h_addr
;
167 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr_in
)) < 0) {
168 LOG_ERROR("connect: %s", strerror(errno
));
172 remote_bitbang_in
= fdopen(fd
, "r");
173 if (remote_bitbang_in
== NULL
) {
174 LOG_ERROR("fdopen: failed to open read stream");
178 remote_bitbang_out
= fdopen(fd
, "w");
179 if (remote_bitbang_out
== NULL
) {
180 LOG_ERROR("fdopen: failed to open write stream");
184 LOG_INFO("remote_bitbang driver initialized");
188 static int remote_bitbang_init_unix(void)
190 LOG_INFO("Connecting to unix socket %s", remote_bitbang_host
);
191 int fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
193 LOG_ERROR("socket: %s", strerror(errno
));
197 struct sockaddr_un addr
;
198 addr
.sun_family
= AF_UNIX
;
199 strncpy(addr
.sun_path
, remote_bitbang_host
, UNIX_PATH_LEN
);
200 addr
.sun_path
[UNIX_PATH_LEN
-1] = '\0';
202 if (connect(fd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr_un
)) < 0) {
203 LOG_ERROR("connect: %s", strerror(errno
));
207 remote_bitbang_in
= fdopen(fd
, "r");
208 if (remote_bitbang_in
== NULL
) {
209 LOG_ERROR("fdopen: failed to open read stream");
213 remote_bitbang_out
= fdopen(fd
, "w");
214 if (remote_bitbang_out
== NULL
) {
215 LOG_ERROR("fdopen: failed to open write stream");
219 LOG_INFO("remote_bitbang driver initialized");
223 static int remote_bitbang_init(void)
225 bitbang_interface
= &remote_bitbang_bitbang
;
227 LOG_INFO("Initializing remote_bitbang driver");
228 if (remote_bitbang_port
== 0)
229 return remote_bitbang_init_unix();
230 return remote_bitbang_init_tcp();
233 COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_port_command
)
236 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], remote_bitbang_port
);
239 return ERROR_COMMAND_SYNTAX_ERROR
;
242 COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_host_command
)
245 strncpy(remote_bitbang_host
, CMD_ARGV
[0], REMOTE_BITBANG_HOST_MAX
);
246 remote_bitbang_host
[REMOTE_BITBANG_HOST_MAX
-1] = '\0';
249 return ERROR_COMMAND_SYNTAX_ERROR
;
252 static const struct command_registration remote_bitbang_command_handlers
[] = {
254 .name
= "remote_bitbang_port",
255 .handler
= remote_bitbang_handle_remote_bitbang_port_command
,
256 .mode
= COMMAND_CONFIG
,
257 .help
= "Set the port to use to connect to the remote jtag.\n"
258 " if 0, use unix sockets to connect to the remote jtag.",
259 .usage
= "port_number",
262 .name
= "remote_bitbang_host",
263 .handler
= remote_bitbang_handle_remote_bitbang_host_command
,
264 .mode
= COMMAND_CONFIG
,
265 .help
= "Set the host to use to connect to the remote jtag.\n"
266 " if port is 0, this is the name of the unix socket to use.",
267 .usage
= "host_name",
269 COMMAND_REGISTRATION_DONE
,
272 struct jtag_interface remote_bitbang_interface
= {
273 .name
= "remote_bitbang",
274 .execute_queue
= &bitbang_execute_queue
,
275 .commands
= remote_bitbang_command_handlers
,
276 .init
= &remote_bitbang_init
,
277 .quit
= &remote_bitbang_quit
,