Zach Welch <zw@superlucidity.net> use static keyword in jtag layer and drivers
[openocd.git] / src / jtag / vsllink.c
blob7e9bbc1f02aefd57ea3dbf9c89cc05e815d34f3b
1 /***************************************************************************
2 * Copyright (C) 2009 by Simon Qian <SimonQian@SimonQian.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
20 /* Versaloon is a programming tool for multiple MCUs.
21 * OpenOCD and MSP430 supports are distributed under GPLv2.
22 * You can find it at http://www.SimonQian.com/en/Versaloon.
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include "replacements.h"
31 #include "jtag.h"
33 #include <usb.h>
34 #include <string.h>
36 #include "log.h"
38 //#define _VSLLINK_IN_DEBUG_MODE_
40 /* enable this to view USB communication
42 #if 0
43 #define _DEBUG_USB_COMMS_
44 #endif
46 #ifdef _DEBUG_JTAG_IO_
47 #define DEBUG_JTAG_IO(expr ...) LOG_DEBUG(expr)
48 #else
49 #define DEBUG_JTAG_IO(expr ...)
50 #endif
52 static u16 vsllink_vid;
53 static u16 vsllink_pid;
54 static u8 vsllink_bulkout;
55 static u8 vsllink_bulkin;
57 #define VSLLINK_USB_TIMEOUT 10000
59 static int VSLLINK_BufferSize = 1024;
61 /* Global USB buffers */
62 static int vsllink_usb_out_buffer_idx;
63 static int vsllink_usb_in_want_length;
64 static u8* vsllink_usb_in_buffer = NULL;
65 static u8* vsllink_usb_out_buffer = NULL;
67 /* Constants for VSLLink command */
68 #define VSLLINK_CMD_CONN 0x80
69 #define VSLLINK_CMD_DISCONN 0x81
70 #define VSLLINK_CMD_SET_SPEED 0x82
71 #define VSLLINK_CMD_SET_PORT 0x90
72 #define VSLLINK_CMD_GET_PORT 0x91
73 #define VSLLINK_CMD_SET_PORTDIR 0x92
74 #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
75 #define VSLLINK_CMD_HW_JTAGHLCMD 0xA1
76 #define VSLLINK_CMD_HW_SWDCMD 0xA2
78 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
79 #define VSLLINK_CMDJTAGSEQ_TMSCLOCK 0x40
80 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
82 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
83 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
85 #define JTAG_PINMSK_SRST (1 << 0)
86 #define JTAG_PINMSK_TRST (1 << 1)
87 #define JTAG_PINMSK_USR1 (1 << 2)
88 #define JTAG_PINMSK_USR2 (1 << 3)
89 #define JTAG_PINMSK_TCK (1 << 4)
90 #define JTAG_PINMSK_TMS (1 << 5)
91 #define JTAG_PINMSK_TDI (1 << 6)
92 #define JTAG_PINMSK_TDO (1 << 7)
95 #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_ndx(from)][tap_move_ndx(to)]
97 /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
98 * 0: Test-Logic-Reset
99 * 1: Run-Test/Idle
100 * 2: Shift-DR
101 * 3: Pause-DR
102 * 4: Shift-IR
103 * 5: Pause-IR
105 * SD->SD and SI->SI have to be caught in interface specific code
107 static u8 VSLLINK_tap_move[6][6] =
109 /* TLR RTI SD PD SI PI */
110 {0xff, 0x7f, 0x2f, 0x0a, 0x37, 0x16}, /* TLR */
111 {0xff, 0x00, 0x45, 0x05, 0x4b, 0x0b}, /* RTI */
112 {0xff, 0x61, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
113 {0xff, 0x60, 0x40, 0x5c, 0x3c, 0x5e}, /* PD */
114 {0xff, 0x61, 0x07, 0x17, 0x00, 0x01}, /* SI */
115 {0xff, 0x60, 0x38, 0x5c, 0x40, 0x5e} /* PI */
118 typedef struct insert_insignificant_operation
120 unsigned char insert_value;
121 unsigned char insert_position;
122 }insert_insignificant_operation_t;
124 static insert_insignificant_operation_t VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[6][6] =
126 /* stuff offset */
127 {/* TLR */
128 {1, 0,}, /* TLR */
129 {1, 0,}, /* RTI */
130 {1, 0,}, /* SD */
131 {1, 0,}, /* PD */
132 {1, 0,}, /* SI */
133 {1, 0,}}, /* PI */
134 {/* RTI */
135 {1, 0,}, /* TLR */
136 {0, 0,}, /* RTI */
137 {0, 4,}, /* SD */
138 {0, 7,}, /* PD */
139 {0, 5,}, /* SI */
140 {0, 7,}}, /* PI */
141 {/* SD */
142 {0, 0,}, /* TLR */
143 {0, 0,}, /* RTI */
144 {0, 0,}, /* SD */
145 {0, 0,}, /* PD */
146 {0, 0,}, /* SI */
147 {0, 0,}}, /* PI */
148 {/* PD */
149 {0, 0,}, /* TLR */
150 {0, 0,}, /* RTI */
151 {0, 0,}, /* SD */
152 {0, 0,}, /* PD */
153 {0, 0,}, /* SI */
154 {0, 0,}}, /* PI */
155 {/* SI */
156 {0, 0,}, /* TLR */
157 {0, 0,}, /* RTI */
158 {0, 0,}, /* SD */
159 {0, 0,}, /* PD */
160 {0, 0,}, /* SI */
161 {0, 0,}}, /* PI */
162 {/* PI */
163 {0, 0,}, /* TLR */
164 {0, 0,}, /* RTI */
165 {0, 0,}, /* SD */
166 {0, 0,}, /* PD */
167 {0, 0,}, /* SI */
168 {0, 0,}}, /* PI */
171 static u8 VSLLINK_BIT_MSK[8] =
173 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
176 typedef struct
178 int length; /* Number of bits to read */
179 int offset;
180 scan_command_t *command; /* Corresponding scan command */
181 u8 *buffer;
182 } pending_scan_result_t;
184 #define MAX_PENDING_SCAN_RESULTS 256
186 static int pending_scan_results_length;
187 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
189 /* External interface functions */
190 static int vsllink_execute_queue(void);
191 static int vsllink_speed(int speed);
192 static int vsllink_khz(int khz, int *jtag_speed);
193 static int vsllink_speed_div(int jtag_speed, int *khz);
194 static int vsllink_register_commands(struct command_context_s *cmd_ctx);
195 static int vsllink_init(void);
196 static int vsllink_quit(void);
198 /* CLI command handler functions */
199 static int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
200 static int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
201 static int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
202 static int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
204 /* Queue command functions */
205 static void vsllink_end_state(tap_state_t state);
206 static void vsllink_state_move(void);
207 static void vsllink_path_move(int num_states, tap_state_t *path);
208 static void vsllink_runtest(int num_cycles);
209 static void vsllink_stableclocks(int num_cycles, int tms);
210 static void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
211 static void vsllink_reset(int trst, int srst);
212 static void vsllink_simple_command(u8 command);
214 /* VSLLink tap buffer functions */
215 static void vsllink_tap_init(void);
216 static int vsllink_tap_execute(void);
217 static void vsllink_tap_ensure_space(int scans, int bytes);
218 static void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset);
220 /* VSLLink lowlevel functions */
221 typedef struct vsllink_jtag
223 struct usb_dev_handle* usb_handle;
224 } vsllink_jtag_t;
226 static vsllink_jtag_t *vsllink_usb_open(void);
227 static void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag);
228 static int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length);
229 static int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length);
230 static int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag);
232 #ifdef _DEBUG_USB_COMMS_
233 static void vsllink_debug_buffer(u8 *buffer, int length);
234 #endif
236 static int vsllink_tms_data_len = 0;
237 static u8* vsllink_tms_cmd_pos;
239 static vsllink_jtag_t* vsllink_jtag_handle;
241 /***************************************************************************/
242 /* External interface implementation */
244 jtag_interface_t vsllink_interface =
246 .name = "vsllink",
247 .execute_queue = vsllink_execute_queue,
248 .speed = vsllink_speed,
249 .khz = vsllink_khz,
250 .speed_div = vsllink_speed_div,
251 .register_commands = vsllink_register_commands,
252 .init = vsllink_init,
253 .quit = vsllink_quit
256 static int vsllink_execute_queue(void)
258 jtag_command_t *cmd = jtag_command_queue;
259 int scan_size;
260 enum scan_type type;
261 u8 *buffer;
263 DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
265 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
266 vsllink_usb_out_buffer_idx = 3;
267 while (cmd != NULL)
269 switch (cmd->type)
271 case JTAG_END_STATE:
272 DEBUG_JTAG_IO("end_state: %s", tap_state_name(cmd->cmd.end_state->end_state));
274 if (cmd->cmd.end_state->end_state != TAP_INVALID)
276 vsllink_end_state(cmd->cmd.end_state->end_state);
278 break;
280 case JTAG_RUNTEST:
281 DEBUG_JTAG_IO( "runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles, \
282 tap_state_name(cmd->cmd.runtest->end_state));
284 if (cmd->cmd.runtest->end_state != TAP_INVALID)
286 vsllink_end_state(cmd->cmd.runtest->end_state);
288 vsllink_runtest(cmd->cmd.runtest->num_cycles);
289 break;
291 case JTAG_STATEMOVE:
292 DEBUG_JTAG_IO("statemove end in %s", tap_state_name(cmd->cmd.statemove->end_state));
294 if (cmd->cmd.statemove->end_state != TAP_INVALID)
296 vsllink_end_state(cmd->cmd.statemove->end_state);
298 vsllink_state_move();
299 break;
301 case JTAG_PATHMOVE:
302 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
303 cmd->cmd.pathmove->num_states, \
304 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
306 vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
307 break;
309 case JTAG_SCAN:
310 if (cmd->cmd.scan->end_state != TAP_INVALID)
312 vsllink_end_state(cmd->cmd.scan->end_state);
315 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
316 if (cmd->cmd.scan->ir_scan)
318 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
320 else
322 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
325 #ifdef _DEBUG_JTAG_IO_
326 vsllink_debug_buffer(buffer, (scan_size + 7) >> 3);
327 #endif
329 type = jtag_scan_type(cmd->cmd.scan);
331 vsllink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
332 break;
334 case JTAG_RESET:
335 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
337 vsllink_tap_execute();
339 if (cmd->cmd.reset->trst == 1)
341 tap_set_state(TAP_RESET);
343 vsllink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
345 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
346 vsllink_usb_out_buffer_idx = 3;
347 break;
349 case JTAG_SLEEP:
350 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
351 vsllink_tap_execute();
352 jtag_sleep(cmd->cmd.sleep->us);
353 break;
355 case JTAG_STABLECLOCKS:
356 DEBUG_JTAG_IO("add %d clocks", cmd->cmd.stableclocks->num_cycles);
357 switch(tap_get_state())
359 case TAP_RESET:
360 // tms should be '1' to stay in TAP_RESET mode
361 scan_size = 1;
362 break;
363 case TAP_DRSHIFT:
364 case TAP_IDLE:
365 case TAP_DRPAUSE:
366 case TAP_IRSHIFT:
367 case TAP_IRPAUSE:
368 // in other mode, tms should be '0'
369 scan_size = 0;
370 break; /* above stable states are OK */
371 default:
372 LOG_ERROR( "jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
373 tap_state_name(tap_get_state()) );
374 exit(-1);
376 vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size);
377 break;
379 default:
380 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type);
381 exit(-1);
383 cmd = cmd->next;
386 return vsllink_tap_execute();
389 static int vsllink_speed(int speed)
391 int result;
393 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_SPEED;
394 vsllink_usb_out_buffer[1] = (speed >> 0) & 0xff;
395 vsllink_usb_out_buffer[2] = (speed >> 8) & 0xFF;
397 result = vsllink_usb_write(vsllink_jtag_handle, 3);
399 if (result == 3)
401 return ERROR_OK;
403 else
405 LOG_ERROR("VSLLink setting speed failed (%d)", result);
406 return ERROR_JTAG_DEVICE_ERROR;
409 return ERROR_OK;
412 static int vsllink_khz(int khz, int *jtag_speed)
414 *jtag_speed = khz;
416 return ERROR_OK;
419 static int vsllink_speed_div(int jtag_speed, int *khz)
421 *khz = jtag_speed;
423 return ERROR_OK;
426 static int vsllink_register_commands(struct command_context_s *cmd_ctx)
428 register_command(cmd_ctx, NULL, "vsllink_usb_vid", vsllink_handle_usb_vid_command,
429 COMMAND_CONFIG, NULL);
430 register_command(cmd_ctx, NULL, "vsllink_usb_pid", vsllink_handle_usb_pid_command,
431 COMMAND_CONFIG, NULL);
432 register_command(cmd_ctx, NULL, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command,
433 COMMAND_CONFIG, NULL);
434 register_command(cmd_ctx, NULL, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command,
435 COMMAND_CONFIG, NULL);
437 return ERROR_OK;
440 static int vsllink_init(void)
442 int check_cnt;
443 int result;
444 char version_str[100];
446 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
447 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
448 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
450 LOG_ERROR("Not enough memory");
451 exit(-1);
454 vsllink_jtag_handle = vsllink_usb_open();
456 if (vsllink_jtag_handle == 0)
458 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
459 return ERROR_JTAG_INIT_FAILED;
462 check_cnt = 0;
463 while (check_cnt < 3)
465 vsllink_simple_command(VSLLINK_CMD_CONN);
466 result = vsllink_usb_read(vsllink_jtag_handle);
468 if (result > 2)
470 vsllink_usb_in_buffer[result] = 0;
471 VSLLINK_BufferSize = vsllink_usb_in_buffer[0] + (vsllink_usb_in_buffer[1] << 8);
472 strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, sizeof(version_str));
473 LOG_INFO(version_str);
475 // free the pre-alloc memroy
476 free(vsllink_usb_in_buffer);
477 free(vsllink_usb_out_buffer);
478 vsllink_usb_in_buffer = NULL;
479 vsllink_usb_out_buffer = NULL;
481 // alloc new memory
482 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
483 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
484 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
486 LOG_ERROR("Not enough memory");
487 exit(-1);
489 else
491 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize);
493 break;
495 vsllink_simple_command(VSLLINK_CMD_DISCONN);
497 check_cnt++;
500 if (check_cnt == 3)
502 // It's dangerout to proced
503 LOG_ERROR("VSLLink initial failed");
504 exit(-1);
507 // Set SRST and TRST to output, Set USR1 and USR2 to input
508 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
509 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
510 vsllink_usb_out_buffer[2] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
511 if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
513 LOG_ERROR("VSLLink USB send data error");
514 exit(-1);
517 vsllink_reset(0, 0);
519 LOG_INFO("VSLLink JTAG Interface ready");
521 vsllink_tap_init();
523 return ERROR_OK;
526 static int vsllink_quit(void)
528 if ((vsllink_usb_in_buffer != NULL) && (vsllink_usb_out_buffer != NULL))
530 // Set all pins to input
531 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
532 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
533 vsllink_usb_out_buffer[2] = 0;
534 if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
536 LOG_ERROR("VSLLink USB send data error");
537 exit(-1);
540 // disconnect
541 vsllink_simple_command(VSLLINK_CMD_DISCONN);
542 vsllink_usb_close(vsllink_jtag_handle);
545 if (vsllink_usb_in_buffer != NULL)
547 free(vsllink_usb_in_buffer);
549 if (vsllink_usb_out_buffer != NULL)
551 free(vsllink_usb_out_buffer);
553 return ERROR_OK;
556 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
557 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
558 static void vsllink_append_tms(void)
560 u8 tms_scan = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
561 u16 tms2;
562 tap_state_t end_state = tap_get_end_state();
564 if (((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_DRPAUSE) && (tap_get_state() != TAP_IRPAUSE)) || \
565 (vsllink_tms_data_len <= 0) || (vsllink_tms_data_len >= 8) || \
566 (vsllink_tms_cmd_pos == NULL))
568 LOG_ERROR("There MUST be some bugs in the driver");
569 exit(-1);
572 tms2 = (tms_scan & VSLLINK_BIT_MSK[VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_position]) << \
573 vsllink_tms_data_len;
574 if (VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_value == 1)
576 tms2 |= VSLLINK_BIT_MSK[8 - vsllink_tms_data_len] << \
577 (vsllink_tms_data_len + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_position);
579 tms2 |= (tms_scan >> VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_position) << \
580 (8 + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(end_state)].insert_position);
582 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (tms2 >> 0) & 0xff;
583 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms2 >> 8) & 0xff;
585 vsllink_tms_data_len = 0;
586 vsllink_tms_cmd_pos = NULL;
589 /***************************************************************************/
590 /* Queue command implementations */
592 static void vsllink_end_state(tap_state_t state)
594 if (tap_is_state_stable(state))
596 tap_set_end_state(state);
598 else
600 LOG_ERROR("BUG: %i is not a valid end state", state);
601 exit(-1);
605 /* Goes to the end state. */
606 static void vsllink_state_move(void)
608 if (vsllink_tms_data_len > 0)
610 vsllink_append_tms();
612 else
614 vsllink_tap_ensure_space(0, 2);
616 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
617 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
620 tap_set_state(tap_get_end_state());
623 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
624 static void vsllink_add_path(int start, int num, tap_state_t *path)
626 int i;
628 for (i = start; i < (start + num); i++)
630 if ((i & 7) == 0)
632 if (i > 0)
634 vsllink_usb_out_buffer_idx++;
636 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
639 if (path[i - start] == tap_state_transition(tap_get_state(), true))
641 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= 1 << (i & 7);
643 else if (path[i - start] == tap_state_transition(tap_get_state(), false))
645 // nothing to do
647 else
649 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
650 exit(-1);
652 tap_set_state(path[i - start]);
654 if ((i > 0) && ((i & 7) == 0))
656 vsllink_usb_out_buffer_idx++;
657 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
660 tap_set_end_state(tap_get_state());
663 static void vsllink_path_move(int num_states, tap_state_t *path)
665 int i, tms_len, tms_cmd_pos, path_idx = 0;
667 if (vsllink_tms_data_len > 0)
669 // there are vsllink_tms_data_len more tms bits to be shifted
670 // so there are vsllink_tms_data_len + num_states tms bits in all
671 tms_len = vsllink_tms_data_len + num_states;
672 if (tms_len <= 16)
674 // merge into last tms shift
675 if (tms_len < 8)
677 // just append tms data to the last tms byte
678 vsllink_add_path(vsllink_tms_data_len, num_states, path);
680 else if (tms_len == 8)
682 // end last tms shift command
683 (*vsllink_tms_cmd_pos)--;
684 vsllink_add_path(vsllink_tms_data_len, num_states, path);
686 else if (tms_len < 16)
688 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
690 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
691 // there is enought tms length in the current tms shift command
692 (*vsllink_tms_cmd_pos)++;
693 vsllink_add_path(vsllink_tms_data_len, num_states, path);
695 else
697 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
698 // not enough tms length in the current tms shift command
699 // so a new command should be added
700 // first decrease byte length of last tms shift command
701 (*vsllink_tms_cmd_pos)--;
702 // append tms data to the last tms byte
703 vsllink_add_path(vsllink_tms_data_len, 8 - vsllink_tms_data_len, path);
704 path += 8 - vsllink_tms_data_len;
705 // add new command(3 bytes)
706 vsllink_tap_ensure_space(0, 3);
707 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
708 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
709 vsllink_add_path(0, num_states - (8 - vsllink_tms_data_len), path);
712 else if (tms_len == 16)
714 // end last tms shift command
715 vsllink_add_path(vsllink_tms_data_len, num_states, path);
718 vsllink_tms_data_len = (vsllink_tms_data_len + num_states) & 7;
719 if (vsllink_tms_data_len == 0)
721 vsllink_tms_cmd_pos = NULL;
723 num_states = 0;
725 else
727 vsllink_add_path(vsllink_tms_data_len, 16 - vsllink_tms_data_len, path);
729 path += 16 - vsllink_tms_data_len;
730 num_states -= 16 - vsllink_tms_data_len;
731 vsllink_tms_data_len = 0;
732 vsllink_tms_cmd_pos = NULL;
736 if (num_states > 0)
738 // Normal operation, don't need to append tms data
739 vsllink_tms_data_len = num_states & 7;
741 while (num_states > 0)
743 if (num_states > ((VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8))
745 i = (VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8;
747 else
749 i = num_states;
751 tms_len = (i + 7) >> 3;
752 vsllink_tap_ensure_space(0, tms_len + 2);
753 tms_cmd_pos = vsllink_usb_out_buffer_idx;
754 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (tms_len - 1);
756 vsllink_add_path(0, i, path + path_idx);
758 path_idx += i;
759 num_states -= i;
762 if (vsllink_tms_data_len > 0)
764 if (tms_len < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
766 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[tms_cmd_pos];
767 (*vsllink_tms_cmd_pos)++;
769 else
771 vsllink_usb_out_buffer[tms_cmd_pos]--;
773 tms_len = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
774 vsllink_tap_ensure_space(0, 3);
775 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
776 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
777 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = tms_len;
783 static void vsllink_stableclocks(int num_cycles, int tms)
785 int tms_len;
786 u16 tms_append_byte;
788 if (vsllink_tms_data_len > 0)
790 // there are vsllink_tms_data_len more tms bits to be shifted
791 // so there are vsllink_tms_data_len + num_cycles tms bits in all
792 tms_len = vsllink_tms_data_len + num_cycles;
793 if (tms > 0)
795 // append '1' for tms
796 tms_append_byte = (u16)((((1 << num_cycles) - 1) << vsllink_tms_data_len) & 0xFFFF);
798 else
800 // append '0' for tms
801 tms_append_byte = 0;
803 if (tms_len <= 16)
805 // merge into last tms shift
806 if (tms_len < 8)
808 // just add to vsllink_tms_data_len
809 // same result if tun through
810 //vsllink_tms_data_len += num_cycles;
811 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= (u8)(tms_append_byte & 0xFF);
813 else if (tms_len == 8)
815 // end last tms shift command
816 // just reduce it, and append last tms byte
817 (*vsllink_tms_cmd_pos)--;
818 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
820 else if (tms_len < 16)
822 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
824 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
825 // there is enought tms length in the current tms shift command
826 // increase the tms byte length by 1 and set the last byte to 0
827 (*vsllink_tms_cmd_pos)++;
828 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
829 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (u8)(tms_append_byte >> 8);
831 else
833 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
834 // not enough tms length in the current tms shift command
835 // so a new command should be added
836 // first decrease byte length of last tms shift command
837 (*vsllink_tms_cmd_pos)--;
838 // append last tms byte and move the command pointer to the next empty position
839 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
840 // add new command(3 bytes)
841 vsllink_tap_ensure_space(0, 3);
842 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
843 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
844 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (u8)(tms_append_byte >> 8);
847 else if (tms_len == 16)
849 // end last tms shift command
850 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
851 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (u8)(tms_append_byte >> 8);
854 vsllink_tms_data_len = tms_len & 7;
855 if (vsllink_tms_data_len == 0)
857 vsllink_tms_cmd_pos = NULL;
859 num_cycles = 0;
861 else
863 // more shifts will be needed
864 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (u8)(tms_append_byte & 0xFF);
865 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (u8)(tms_append_byte >> 8);
867 num_cycles -= 16 - vsllink_tms_data_len;
868 vsllink_tms_data_len = 0;
869 vsllink_tms_cmd_pos = NULL;
872 // from here vsllink_tms_data_len == 0 or num_cycles == 0
874 if (vsllink_tms_data_len > 0)
876 // num_cycles == 0
877 // no need to shift
878 if (num_cycles > 0)
880 LOG_ERROR("There MUST be some bugs in the driver");
881 exit(-1);
884 else
886 // get number of bytes left to be sent
887 tms_len = num_cycles >> 3;
888 if (tms_len > 0)
890 vsllink_tap_ensure_space(1, 5);
891 // if tms_len > 0, vsllink_tms_data_len == 0
892 // so just add new command
893 // LSB of the command byte is the tms value when do the shifting
894 if (tms > 0)
896 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK | 1;
898 else
900 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK;
902 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 0) & 0xff;
903 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 8) & 0xff;
904 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 16) & 0xff;
905 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 24) & 0xff;
907 vsllink_usb_in_want_length += 1;
908 pending_scan_results_buffer[pending_scan_results_length].buffer = NULL;
909 pending_scan_results_length++;
911 if (tms_len > 0xFFFF)
913 vsllink_tap_execute();
917 // post-process
918 vsllink_tms_data_len = num_cycles & 7;
919 if (vsllink_tms_data_len > 0)
921 vsllink_tap_ensure_space(0, 3);
922 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
923 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
924 if (tms > 0)
926 // append '1' for tms
927 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (1 << vsllink_tms_data_len) - 1;
929 else
931 // append '0' for tms
932 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0x00;
938 static void vsllink_runtest(int num_cycles)
940 tap_state_t saved_end_state = tap_get_end_state();
942 if (tap_get_state() != TAP_IDLE)
944 // enter into IDLE state
945 vsllink_end_state(TAP_IDLE);
946 vsllink_state_move();
949 vsllink_stableclocks(num_cycles, 0);
951 // post-process
952 // set end_state
953 vsllink_end_state(saved_end_state);
954 tap_set_state(TAP_IDLE);
955 if (tap_get_end_state() != TAP_IDLE)
957 vsllink_state_move();
961 static void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
963 tap_state_t saved_end_state;
964 u8 bits_left, tms_tmp, tdi_len;
965 int i;
967 if (0 == scan_size )
969 return;
972 tdi_len = ((scan_size + 7) >> 3);
973 if ((tdi_len + 7) > VSLLINK_BufferSize)
975 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
976 exit(-1);
979 saved_end_state = tap_get_end_state();
981 /* Move to appropriate scan state */
982 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
984 if (vsllink_tms_data_len > 0)
986 if (tap_get_state() == tap_get_end_state())
988 // already in IRSHIFT or DRSHIFT state
989 // merge tms data in the last tms shift command into next scan command
990 if(*vsllink_tms_cmd_pos < 1)
992 LOG_ERROR("There MUST be some bugs in the driver");
993 exit(-1);
995 else if(*vsllink_tms_cmd_pos < 2)
997 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
998 vsllink_usb_out_buffer_idx--;
1000 else
1002 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1003 *vsllink_tms_cmd_pos -= 2;
1006 vsllink_tap_ensure_space(1, tdi_len + 7);
1007 // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
1008 // which is merged from the last tms shift command
1009 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1010 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1011 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
1012 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = tms_tmp;
1013 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[0] << (8 - vsllink_tms_data_len);
1015 for (i = 0; i < tdi_len; i++)
1017 buffer[i] >>= 8 - vsllink_tms_data_len;
1018 if (i != tdi_len)
1020 buffer[i] += buffer[i + 1] << vsllink_tms_data_len;
1024 vsllink_tap_append_scan(scan_size - vsllink_tms_data_len, buffer, command, vsllink_tms_data_len);
1025 scan_size -= 8 - vsllink_tms_data_len;
1026 vsllink_tms_data_len = 0;
1028 else
1030 vsllink_append_tms();
1031 vsllink_tap_ensure_space(1, tdi_len + 5);
1033 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN;
1034 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 0) & 0xff;
1035 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 8) & 0xff;
1037 vsllink_tap_append_scan(scan_size, buffer, command, 0);
1040 else
1042 vsllink_tap_ensure_space(1, tdi_len + 7);
1044 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1045 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1046 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
1047 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1048 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1050 vsllink_tap_append_scan(scan_size, buffer, command, 8);
1052 vsllink_end_state(saved_end_state);
1054 bits_left = scan_size & 0x07;
1055 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1057 if (bits_left > 0)
1059 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << (bits_left - 1);
1061 else
1063 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << 7;
1066 if (tap_get_state() != tap_get_end_state())
1068 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1070 else
1072 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1075 tap_set_state(tap_get_end_state());
1078 static void vsllink_reset(int trst, int srst)
1080 int result;
1082 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
1084 /* Signals are active low */
1085 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORT;
1086 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
1087 vsllink_usb_out_buffer[2] = 0;
1088 if (srst == 0)
1090 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_SRST;
1092 if (trst == 0)
1094 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_TRST;
1097 result = vsllink_usb_write(vsllink_jtag_handle, 3);
1098 if (result != 3)
1100 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result);
1104 static void vsllink_simple_command(u8 command)
1106 int result;
1108 DEBUG_JTAG_IO("0x%02x", command);
1110 vsllink_usb_out_buffer[0] = command;
1111 result = vsllink_usb_write(vsllink_jtag_handle, 1);
1113 if (result != 1)
1115 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command, result);
1119 static int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1121 if (argc != 1) {
1122 LOG_ERROR("parameter error, should be one parameter for VID");
1123 return ERROR_OK;
1126 vsllink_vid = strtol(args[0], NULL, 0);
1128 return ERROR_OK;
1131 static int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1133 if (argc != 1) {
1134 LOG_ERROR("parameter error, should be one parameter for PID");
1135 return ERROR_OK;
1138 vsllink_pid = strtol(args[0], NULL, 0);
1140 return ERROR_OK;
1143 static int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1145 if (argc != 1) {
1146 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1147 return ERROR_OK;
1150 vsllink_bulkin = strtol(args[0], NULL, 0) | 0x80;
1152 return ERROR_OK;
1155 static int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1157 if (argc != 1) {
1158 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1159 return ERROR_OK;
1162 vsllink_bulkout = strtol(args[0], NULL, 0);
1164 return ERROR_OK;
1167 /***************************************************************************/
1168 /* VSLLink tap functions */
1170 static void vsllink_tap_init(void)
1172 vsllink_usb_out_buffer_idx = 0;
1173 vsllink_usb_in_want_length = 0;
1174 pending_scan_results_length = 0;
1177 static void vsllink_tap_ensure_space(int scans, int bytes)
1179 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1180 int available_bytes = VSLLINK_BufferSize - vsllink_usb_out_buffer_idx;
1182 if (scans > available_scans || bytes > available_bytes)
1184 vsllink_tap_execute();
1188 static void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset)
1190 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1191 int i;
1193 if (offset > 0)
1195 vsllink_usb_in_want_length += ((length + 7) >> 3) + 1;
1197 else
1199 vsllink_usb_in_want_length += (length + 7) >> 3;
1201 pending_scan_result->length = length;
1202 pending_scan_result->offset = offset;
1203 pending_scan_result->command = command;
1204 pending_scan_result->buffer = buffer;
1206 for (i = 0; i < ((length + 7) >> 3); i++)
1208 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[i];
1211 pending_scan_results_length++;
1214 /* Pad and send a tap sequence to the device, and receive the answer.
1215 * For the purpose of padding we assume that we are in reset or idle or pause state. */
1216 static int vsllink_tap_execute(void)
1218 int i;
1219 int result;
1220 int first = 0;
1222 if (vsllink_tms_data_len > 0)
1224 if((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_IRPAUSE) && (tap_get_state() != TAP_DRPAUSE))
1226 LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", tap_state_name(tap_get_state()));
1229 if (vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] & (1 << (vsllink_tms_data_len - 1)))
1231 // last tms bit is '1'
1232 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= 0xFF << vsllink_tms_data_len;
1233 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0xFF;
1234 vsllink_tms_data_len = 0;
1236 else
1238 // last tms bit is '0'
1239 vsllink_usb_out_buffer_idx++;
1240 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1241 vsllink_tms_data_len = 0;
1245 if (vsllink_usb_out_buffer_idx > 3)
1247 if (vsllink_usb_out_buffer[0] == VSLLINK_CMD_HW_JTAGSEQCMD)
1249 vsllink_usb_out_buffer[1] = (vsllink_usb_out_buffer_idx >> 0) & 0xff;
1250 vsllink_usb_out_buffer[2] = (vsllink_usb_out_buffer_idx >> 8) & 0xff;
1253 result = vsllink_usb_message(vsllink_jtag_handle, vsllink_usb_out_buffer_idx, vsllink_usb_in_want_length);
1255 if (result == vsllink_usb_in_want_length)
1257 for (i = 0; i < pending_scan_results_length; i++)
1259 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
1260 u8 *buffer = pending_scan_result->buffer;
1261 int length = pending_scan_result->length;
1262 int offset = pending_scan_result->offset;
1263 scan_command_t *command = pending_scan_result->command;
1265 if (buffer != NULL)
1267 // IRSHIFT or DRSHIFT
1268 buf_set_buf(vsllink_usb_in_buffer, first * 8 + offset, buffer, 0, length);
1269 first += (length + offset + 7) >> 3;
1271 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length);
1272 #ifdef _DEBUG_JTAG_IO_
1273 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1274 #endif
1276 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1278 vsllink_tap_init();
1279 return ERROR_JTAG_QUEUE_FAILED;
1282 free(pending_scan_result->buffer);
1283 pending_scan_result->buffer = NULL;
1285 else
1287 first++;
1291 else
1293 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, vsllink_usb_in_want_length);
1294 return ERROR_JTAG_QUEUE_FAILED;
1297 vsllink_tap_init();
1300 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
1301 vsllink_usb_out_buffer_idx = 3;
1303 return ERROR_OK;
1306 /*****************************************************************************/
1307 /* VSLLink USB low-level functions */
1309 vsllink_jtag_t* vsllink_usb_open(void)
1311 struct usb_bus *busses;
1312 struct usb_bus *bus;
1313 struct usb_device *dev;
1315 vsllink_jtag_t *result;
1317 result = (vsllink_jtag_t*) malloc(sizeof(vsllink_jtag_t));
1319 usb_init();
1320 usb_find_busses();
1321 usb_find_devices();
1323 busses = usb_get_busses();
1325 /* find vsllink_jtag device in usb bus */
1327 for (bus = busses; bus; bus = bus->next)
1329 for (dev = bus->devices; dev; dev = dev->next)
1331 if ((dev->descriptor.idVendor == vsllink_vid) && (dev->descriptor.idProduct == vsllink_pid))
1333 result->usb_handle = usb_open(dev);
1335 /* usb_set_configuration required under win32 */
1336 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
1337 usb_claim_interface(result->usb_handle, 0);
1339 #if 0
1341 * This makes problems under Mac OS X. And is not needed
1342 * under Windows. Hopefully this will not break a linux build
1344 usb_set_altinterface(result->usb_handle, 0);
1345 #endif
1346 return result;
1351 free(result);
1352 return NULL;
1355 static void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag)
1357 usb_close(vsllink_jtag->usb_handle);
1358 free(vsllink_jtag);
1361 /* Send a message and receive the reply. */
1362 static int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length)
1364 int result;
1366 result = vsllink_usb_write(vsllink_jtag, out_length);
1367 if (result == out_length)
1369 if (in_length > 0)
1371 result = vsllink_usb_read(vsllink_jtag);
1372 if (result == in_length )
1374 return result;
1376 else
1378 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
1379 return -1;
1382 return 0;
1384 else
1386 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
1387 return -1;
1391 /* Write data from out_buffer to USB. */
1392 static int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length)
1394 int result;
1396 if (out_length > VSLLINK_BufferSize)
1398 LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length, VSLLINK_BufferSize);
1399 return -1;
1402 result = usb_bulk_write(vsllink_jtag->usb_handle, vsllink_bulkout, \
1403 (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
1405 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length, result);
1407 #ifdef _DEBUG_USB_COMMS_
1408 LOG_DEBUG("USB out:");
1409 vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
1410 #endif
1412 #ifdef _VSLLINK_IN_DEBUG_MODE_
1413 usleep(100000);
1414 #endif
1416 return result;
1419 /* Read data from USB into in_buffer. */
1420 static int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag)
1422 int result = usb_bulk_read(vsllink_jtag->usb_handle, vsllink_bulkin, \
1423 (char *)vsllink_usb_in_buffer, VSLLINK_BufferSize, VSLLINK_USB_TIMEOUT);
1425 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
1427 #ifdef _DEBUG_USB_COMMS_
1428 LOG_DEBUG("USB in:");
1429 vsllink_debug_buffer(vsllink_usb_in_buffer, result);
1430 #endif
1431 return result;
1434 #define BYTES_PER_LINE 16
1436 #ifdef _DEBUG_USB_COMMS_
1437 static void vsllink_debug_buffer(u8 *buffer, int length)
1439 char line[81];
1440 char s[4];
1441 int i;
1442 int j;
1444 for (i = 0; i < length; i += BYTES_PER_LINE)
1446 snprintf(line, 5, "%04x", i);
1447 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
1449 snprintf(s, 4, " %02x", buffer[j]);
1450 strcat(line, s);
1452 LOG_DEBUG(line);
1455 #endif // _DEBUG_USB_COMMS_