3c1a0462ae45213056e5d639dd151be404ee60de
[openocd.git] / src / jtag / vsllink.c
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 ***************************************************************************/
19
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.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "interface.h"
30 #include "commands.h"
31
32 #include <usb.h>
33
34
35 //#define _VSLLINK_IN_DEBUG_MODE_
36
37 #define VSLLINK_MODE_NORMAL 0
38 #define VSLLINK_MODE_DMA 1
39
40 static uint16_t vsllink_usb_vid;
41 static uint16_t vsllink_usb_pid;
42 static uint8_t vsllink_usb_bulkout;
43 static uint8_t vsllink_usb_bulkin;
44 static uint8_t vsllink_usb_interface;
45 static uint8_t vsllink_mode = VSLLINK_MODE_NORMAL;
46 static int VSLLINK_USB_TIMEOUT = 10000;
47
48 static int VSLLINK_BufferSize = 1024;
49
50 /* Global USB buffers */
51 static int vsllink_usb_out_buffer_idx;
52 static int vsllink_usb_in_want_length;
53 static uint8_t* vsllink_usb_in_buffer = NULL;
54 static uint8_t* vsllink_usb_out_buffer = NULL;
55
56 /* Constants for VSLLink command */
57 #define VSLLINK_CMD_CONN 0x80
58 #define VSLLINK_CMD_DISCONN 0x81
59 #define VSLLINK_CMD_SET_SPEED 0x82
60 #define VSLLINK_CMD_SET_PORT 0x90
61 #define VSLLINK_CMD_GET_PORT 0x91
62 #define VSLLINK_CMD_SET_PORTDIR 0x92
63 #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
64 #define VSLLINK_CMD_HW_JTAGHLCMD 0xA1
65 #define VSLLINK_CMD_HW_SWDCMD 0xA2
66 #define VSLLINK_CMD_HW_JTAGRAWCMD 0xA3
67
68 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
69 #define VSLLINK_CMDJTAGSEQ_TMSCLOCK 0x40
70 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
71
72 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
73 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
74
75 #define JTAG_PINMSK_SRST (1 << 0)
76 #define JTAG_PINMSK_TRST (1 << 1)
77 #define JTAG_PINMSK_USR1 (1 << 2)
78 #define JTAG_PINMSK_USR2 (1 << 3)
79 #define JTAG_PINMSK_TCK (1 << 4)
80 #define JTAG_PINMSK_TMS (1 << 5)
81 #define JTAG_PINMSK_TDI (1 << 6)
82 #define JTAG_PINMSK_TDO (1 << 7)
83
84
85 #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_ndx(from)][tap_move_ndx(to)]
86
87 /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
88 * 0: Test-Logic-Reset
89 * 1: Run-Test/Idle
90 * 2: Shift-DR
91 * 3: Pause-DR
92 * 4: Shift-IR
93 * 5: Pause-IR
94 *
95 * SD->SD and SI->SI have to be caught in interface specific code
96 */
97 static uint8_t VSLLINK_tap_move[6][6] =
98 {
99 /* TLR RTI SD PD SI PI */
100 {0xff, 0x7f, 0x2f, 0x0a, 0x37, 0x16}, /* TLR */
101 {0xff, 0x00, 0x45, 0x05, 0x4b, 0x0b}, /* RTI */
102 {0xff, 0x61, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
103 {0xfe, 0x60, 0x40, 0x5c, 0x3c, 0x5e}, /* PD */
104 {0xff, 0x61, 0x07, 0x17, 0x00, 0x01}, /* SI */
105 {0xfe, 0x60, 0x38, 0x5c, 0x40, 0x5e} /* PI */
106 };
107
108 typedef struct insert_insignificant_operation
109 {
110 unsigned char insert_value;
111 unsigned char insert_position;
112 }insert_insignificant_operation_t;
113
114 static insert_insignificant_operation_t VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[6][6] =
115 {
116 /* stuff offset */
117 {/* TLR */
118 {1, 0,}, /* TLR */
119 {1, 0,}, /* RTI */
120 {1, 0,}, /* SD */
121 {1, 0,}, /* PD */
122 {1, 0,}, /* SI */
123 {1, 0,}}, /* PI */
124 {/* RTI */
125 {1, 0,}, /* TLR */
126 {0, 0,}, /* RTI */
127 {0, 4,}, /* SD */
128 {0, 7,}, /* PD */
129 {0, 5,}, /* SI */
130 {0, 7,}}, /* PI */
131 {/* SD */
132 {0, 0,}, /* TLR */
133 {0, 0,}, /* RTI */
134 {0, 0,}, /* SD */
135 {0, 0,}, /* PD */
136 {0, 0,}, /* SI */
137 {0, 0,}}, /* PI */
138 {/* PD */
139 {0, 0,}, /* TLR */
140 {0, 0,}, /* RTI */
141 {0, 0,}, /* SD */
142 {0, 0,}, /* PD */
143 {0, 0,}, /* SI */
144 {0, 0,}}, /* PI */
145 {/* SI */
146 {0, 0,}, /* TLR */
147 {0, 0,}, /* RTI */
148 {0, 0,}, /* SD */
149 {0, 0,}, /* PD */
150 {0, 0,}, /* SI */
151 {0, 0,}}, /* PI */
152 {/* PI */
153 {0, 0,}, /* TLR */
154 {0, 0,}, /* RTI */
155 {0, 0,}, /* SD */
156 {0, 0,}, /* PD */
157 {0, 0,}, /* SI */
158 {0, 0,}}, /* PI */
159 };
160
161 static uint8_t VSLLINK_BIT_MSK[8] =
162 {
163 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
164 };
165
166 typedef struct
167 {
168 int offset;
169 int length; /* Number of bits to read */
170 scan_command_t *command; /* Corresponding scan command */
171 uint8_t *buffer;
172 } pending_scan_result_t;
173
174 #define MAX_PENDING_SCAN_RESULTS 256
175
176 static int pending_scan_results_length;
177 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
178
179 /* External interface functions */
180 static int vsllink_execute_queue(void);
181 static int vsllink_speed(int speed);
182 static int vsllink_khz(int khz, int *jtag_speed);
183 static int vsllink_speed_div(int jtag_speed, int *khz);
184 static int vsllink_register_commands(struct command_context_s *cmd_ctx);
185 static int vsllink_init(void);
186 static int vsllink_quit(void);
187
188 /* CLI command handler functions */
189 static int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
190 static int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
191 static int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
192 static int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
193 static int vsllink_handle_usb_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
194 static int vsllink_handle_mode_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
195
196 /* Queue command functions */
197 static void vsllink_end_state(tap_state_t state);
198 static void vsllink_state_move_dma(void);
199 static void vsllink_state_move_normal(void);
200 static void (*vsllink_state_move)(void);
201 static void vsllink_path_move_dma(int num_states, tap_state_t *path);
202 static void vsllink_path_move_normal(int num_states, tap_state_t *path);
203 static void (*vsllink_path_move)(int num_states, tap_state_t *path);
204 static void vsllink_runtest(int num_cycles);
205 static void vsllink_stableclocks_dma(int num_cycles, int tms);
206 static void vsllink_stableclocks_normal(int num_cycles, int tms);
207 static void (*vsllink_stableclocks)(int num_cycles, int tms);
208 static void vsllink_scan_dma(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, scan_command_t *command);
209 static void vsllink_scan_normal(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, scan_command_t *command);
210 static void (*vsllink_scan)(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, scan_command_t *command);
211 static void vsllink_reset(int trst, int srst);
212 static void vsllink_simple_command(uint8_t command);
213 static int vsllink_connect(void);
214 static int vsllink_disconnect(void);
215
216 /* VSLLink tap buffer functions */
217 static void vsllink_tap_append_step(int tms, int tdi);
218 static void vsllink_tap_init_dma(void);
219 static void vsllink_tap_init_normal(void);
220 static void (*vsllink_tap_init)(void);
221 static int vsllink_tap_execute_dma(void);
222 static int vsllink_tap_execute_normal(void);
223 static int (*vsllink_tap_execute)(void);
224 static void vsllink_tap_ensure_space_dma(int scans, int length);
225 static void vsllink_tap_ensure_space_normal(int scans, int length);
226 static void (*vsllink_tap_ensure_space)(int scans, int length);
227 static void vsllink_tap_append_scan_dma(int length, uint8_t *buffer, scan_command_t *command);
228 static void vsllink_tap_append_scan_normal(int length, uint8_t *buffer, scan_command_t *command, int offset);
229
230 /* VSLLink lowlevel functions */
231 typedef struct vsllink_jtag
232 {
233 struct usb_dev_handle* usb_handle;
234 } vsllink_jtag_t;
235
236 static vsllink_jtag_t *vsllink_usb_open(void);
237 static void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag);
238 static int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length);
239 static int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length);
240 static int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag);
241
242 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
243 static void vsllink_debug_buffer(uint8_t *buffer, int length);
244 #endif
245
246 static int vsllink_tms_data_len = 0;
247 static uint8_t* vsllink_tms_cmd_pos;
248
249 static int tap_length = 0;
250 static int tap_buffer_size = 0;
251 static uint8_t *tms_buffer = NULL;
252 static uint8_t *tdi_buffer = NULL;
253 static uint8_t *tdo_buffer = NULL;
254 static int last_tms;
255
256 static vsllink_jtag_t* vsllink_jtag_handle = NULL;
257
258 /***************************************************************************/
259 /* External interface implementation */
260
261 jtag_interface_t vsllink_interface =
262 {
263 .name = "vsllink",
264 .execute_queue = vsllink_execute_queue,
265 .speed = vsllink_speed,
266 .khz = vsllink_khz,
267 .speed_div = vsllink_speed_div,
268 .register_commands = vsllink_register_commands,
269 .init = vsllink_init,
270 .quit = vsllink_quit
271 };
272
273 static void reset_command_pointer(void)
274 {
275 if (vsllink_mode == VSLLINK_MODE_NORMAL)
276 {
277 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
278 vsllink_usb_out_buffer_idx = 3;
279 }
280 else
281 {
282 tap_length = 0;
283 }
284 }
285
286 static int vsllink_execute_queue(void)
287 {
288 jtag_command_t *cmd = jtag_command_queue;
289 int scan_size;
290 enum scan_type type;
291 uint8_t *buffer;
292
293 DEBUG_JTAG_IO("--------------------------------- vsllink -------------------------------------");
294
295 reset_command_pointer();
296 while (cmd != NULL)
297 {
298 switch (cmd->type)
299 {
300 case JTAG_RUNTEST:
301 DEBUG_JTAG_IO("runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles, \
302 tap_state_name(cmd->cmd.runtest->end_state));
303
304 vsllink_end_state(cmd->cmd.runtest->end_state);
305 vsllink_runtest(cmd->cmd.runtest->num_cycles);
306 break;
307
308 case JTAG_STATEMOVE:
309 DEBUG_JTAG_IO("statemove end in %s", tap_state_name(cmd->cmd.statemove->end_state));
310
311 vsllink_end_state(cmd->cmd.statemove->end_state);
312 vsllink_state_move();
313 break;
314
315 case JTAG_PATHMOVE:
316 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
317 cmd->cmd.pathmove->num_states, \
318 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
319
320 vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
321 break;
322
323 case JTAG_SCAN:
324 vsllink_end_state(cmd->cmd.scan->end_state);
325
326 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
327 if (cmd->cmd.scan->ir_scan)
328 {
329 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
330 }
331 else
332 {
333 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", scan_size, tap_state_name(cmd->cmd.scan->end_state));
334 }
335
336 #ifdef _DEBUG_JTAG_IO_
337 vsllink_debug_buffer(buffer, (scan_size + 7) >> 3);
338 #endif
339
340 type = jtag_scan_type(cmd->cmd.scan);
341
342 vsllink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
343 break;
344
345 case JTAG_RESET:
346 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
347
348 vsllink_tap_execute();
349
350 if (cmd->cmd.reset->trst == 1)
351 {
352 tap_set_state(TAP_RESET);
353 }
354 vsllink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
355 break;
356
357 case JTAG_SLEEP:
358 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
359 vsllink_tap_execute();
360 jtag_sleep(cmd->cmd.sleep->us);
361 break;
362
363 case JTAG_STABLECLOCKS:
364 DEBUG_JTAG_IO("add %d clocks", cmd->cmd.stableclocks->num_cycles);
365 switch (tap_get_state())
366 {
367 case TAP_RESET:
368 // tms should be '1' to stay in TAP_RESET mode
369 scan_size = 1;
370 break;
371 case TAP_DRSHIFT:
372 case TAP_IDLE:
373 case TAP_DRPAUSE:
374 case TAP_IRSHIFT:
375 case TAP_IRPAUSE:
376 // in other mode, tms should be '0'
377 scan_size = 0;
378 break; /* above stable states are OK */
379 default:
380 LOG_ERROR("jtag_add_clocks() was called with TAP in non-stable state \"%s\"",
381 tap_state_name(tap_get_state()));
382 exit(-1);
383 }
384 vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size);
385 break;
386
387 default:
388 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type);
389 exit(-1);
390 }
391 cmd = cmd->next;
392 }
393
394 return vsllink_tap_execute();
395 }
396
397 static int vsllink_speed(int speed)
398 {
399 int result;
400
401 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_SPEED;
402 vsllink_usb_out_buffer[1] = (speed >> 0) & 0xff;
403 vsllink_usb_out_buffer[2] = (speed >> 8) & 0xFF;
404
405 result = vsllink_usb_write(vsllink_jtag_handle, 3);
406
407 if (result == 3)
408 {
409 return ERROR_OK;
410 }
411 else
412 {
413 LOG_ERROR("VSLLink setting speed failed (%d)", result);
414 return ERROR_JTAG_DEVICE_ERROR;
415 }
416
417 return ERROR_OK;
418 }
419
420 static int vsllink_khz(int khz, int *jtag_speed)
421 {
422 *jtag_speed = khz;
423
424 return ERROR_OK;
425 }
426
427 static int vsllink_speed_div(int jtag_speed, int *khz)
428 {
429 *khz = jtag_speed;
430
431 return ERROR_OK;
432 }
433
434 static int vsllink_init(void)
435 {
436 int check_cnt, to_tmp;
437 int result;
438 char version_str[100];
439
440 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
441 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
442 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
443 {
444 LOG_ERROR("Not enough memory");
445 exit(-1);
446 }
447
448 vsllink_jtag_handle = vsllink_usb_open();
449
450 if (vsllink_jtag_handle == 0)
451 {
452 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
453 return ERROR_JTAG_INIT_FAILED;
454 }
455 LOG_DEBUG("vsllink found on %04X:%04X", vsllink_usb_vid, vsllink_usb_pid);
456
457 to_tmp = VSLLINK_USB_TIMEOUT;
458 VSLLINK_USB_TIMEOUT = 100;
459 check_cnt = 0;
460 while (check_cnt < 5)
461 {
462 vsllink_simple_command(0x00);
463 result = vsllink_usb_read(vsllink_jtag_handle);
464
465 if (result > 2)
466 {
467 vsllink_usb_in_buffer[result] = 0;
468 VSLLINK_BufferSize = vsllink_usb_in_buffer[0] + (vsllink_usb_in_buffer[1] << 8);
469 strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, sizeof(version_str));
470 LOG_INFO("%s", version_str);
471
472 // free the pre-alloc memroy
473 free(vsllink_usb_in_buffer);
474 free(vsllink_usb_out_buffer);
475 vsllink_usb_in_buffer = NULL;
476 vsllink_usb_out_buffer = NULL;
477
478 // alloc new memory
479 vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
480 vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
481 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
482 {
483 LOG_ERROR("Not enough memory");
484 exit(-1);
485 }
486 else
487 {
488 LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize);
489 }
490 // alloc memory for dma mode
491 if (vsllink_mode == VSLLINK_MODE_DMA)
492 {
493 tap_buffer_size = (VSLLINK_BufferSize - 3) / 2;
494 tms_buffer = (uint8_t*)malloc(tap_buffer_size);
495 tdi_buffer = (uint8_t*)malloc(tap_buffer_size);
496 tdo_buffer = (uint8_t*)malloc(tap_buffer_size);
497 if ((tms_buffer == NULL) || (tdi_buffer == NULL) || (tdo_buffer == NULL))
498 {
499 LOG_ERROR("Not enough memory");
500 exit(-1);
501 }
502 }
503 break;
504 }
505 vsllink_simple_command(VSLLINK_CMD_DISCONN);
506 check_cnt++;
507 }
508 if (check_cnt == 3)
509 {
510 // It's dangerout to proced
511 LOG_ERROR("VSLLink initial failed");
512 exit(-1);
513 }
514 VSLLINK_USB_TIMEOUT = to_tmp;
515
516 // connect to vsllink
517 vsllink_connect();
518 // initialize function pointers
519 if (vsllink_mode == VSLLINK_MODE_NORMAL)
520 {
521 // normal mode
522 vsllink_state_move = vsllink_state_move_normal;
523 vsllink_path_move = vsllink_path_move_normal;
524 vsllink_stableclocks = vsllink_stableclocks_normal;
525 vsllink_scan = vsllink_scan_normal;
526
527 vsllink_tap_init = vsllink_tap_init_normal;
528 vsllink_tap_execute = vsllink_tap_execute_normal;
529 vsllink_tap_ensure_space = vsllink_tap_ensure_space_normal;
530
531 LOG_INFO("vsllink run in NORMAL mode");
532 }
533 else
534 {
535 // dma mode
536 vsllink_state_move = vsllink_state_move_dma;
537 vsllink_path_move = vsllink_path_move_dma;
538 vsllink_stableclocks = vsllink_stableclocks_dma;
539 vsllink_scan = vsllink_scan_dma;
540
541 vsllink_tap_init = vsllink_tap_init_dma;
542 vsllink_tap_execute = vsllink_tap_execute_dma;
543 vsllink_tap_ensure_space = vsllink_tap_ensure_space_dma;
544
545 LOG_INFO("vsllink run in DMA mode");
546 }
547
548 // Set SRST and TRST to output, Set USR1 and USR2 to input
549 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
550 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
551 vsllink_usb_out_buffer[2] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
552 if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
553 {
554 LOG_ERROR("VSLLink USB send data error");
555 exit(-1);
556 }
557
558 vsllink_reset(0, 0);
559
560 LOG_INFO("VSLLink JTAG Interface ready");
561
562 vsllink_tap_init();
563
564 return ERROR_OK;
565 }
566
567 static int vsllink_quit(void)
568 {
569 if ((vsllink_usb_in_buffer != NULL) && (vsllink_usb_out_buffer != NULL))
570 {
571 // Set all pins to input
572 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
573 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
574 vsllink_usb_out_buffer[2] = 0;
575 if (vsllink_usb_write(vsllink_jtag_handle, 3) != 3)
576 {
577 LOG_ERROR("VSLLink USB send data error");
578 exit(-1);
579 }
580
581 // disconnect
582 vsllink_disconnect();
583 vsllink_usb_close(vsllink_jtag_handle);
584 vsllink_jtag_handle = NULL;
585 }
586
587 if (vsllink_usb_in_buffer != NULL)
588 {
589 free(vsllink_usb_in_buffer);
590 vsllink_usb_in_buffer = NULL;
591 }
592 if (vsllink_usb_out_buffer != NULL)
593 {
594 free(vsllink_usb_out_buffer);
595 vsllink_usb_out_buffer = NULL;
596 }
597
598 return ERROR_OK;
599 }
600
601 /***************************************************************************/
602 /* Queue command implementations */
603 static int vsllink_disconnect(void)
604 {
605 vsllink_simple_command(VSLLINK_CMD_DISCONN);
606 return ERROR_OK;
607 }
608
609 static int vsllink_connect(void)
610 {
611 char vsllink_str[100];
612
613 vsllink_usb_out_buffer[0] = VSLLINK_CMD_CONN;
614 vsllink_usb_out_buffer[1] = vsllink_mode;
615 vsllink_usb_message(vsllink_jtag_handle, 2, 0);
616 if (vsllink_usb_read(vsllink_jtag_handle) > 2)
617 {
618 strncpy(vsllink_str, (char *)vsllink_usb_in_buffer + 2, sizeof(vsllink_str));
619 LOG_INFO("%s", vsllink_str);
620 }
621
622 return ERROR_OK;
623 }
624
625 // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
626 // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set, no need to set it here.
627 static void vsllink_append_tms(void)
628 {
629 uint8_t tms_scan = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
630 uint16_t tms2;
631 insert_insignificant_operation_t *insert = \
632 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
633
634 if (((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_DRPAUSE) && (tap_get_state() != TAP_IRPAUSE)) || \
635 (vsllink_tms_data_len <= 0) || (vsllink_tms_data_len >= 8) || \
636 (vsllink_tms_cmd_pos == NULL))
637 {
638 LOG_ERROR("There MUST be some bugs in the driver");
639 exit(-1);
640 }
641
642 tms2 = (tms_scan & VSLLINK_BIT_MSK[insert->insert_position]) << \
643 vsllink_tms_data_len;
644 if (insert->insert_value == 1)
645 {
646 tms2 |= VSLLINK_BIT_MSK[8 - vsllink_tms_data_len] << \
647 (vsllink_tms_data_len + insert->insert_position);
648 }
649 tms2 |= (tms_scan >> insert->insert_position) << \
650 (8 + insert->insert_position);
651
652 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (tms2 >> 0) & 0xff;
653 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms2 >> 8) & 0xff;
654
655 vsllink_tms_data_len = 0;
656 vsllink_tms_cmd_pos = NULL;
657 }
658
659 static void vsllink_end_state(tap_state_t state)
660 {
661 if (tap_is_state_stable(state))
662 {
663 tap_set_end_state(state);
664 }
665 else
666 {
667 LOG_ERROR("BUG: %i is not a valid end state", state);
668 exit(-1);
669 }
670 }
671
672 /* Goes to the end state. */
673 static void vsllink_state_move_normal(void)
674 {
675 if (vsllink_tms_data_len > 0)
676 {
677 vsllink_append_tms();
678 }
679 else
680 {
681 vsllink_tap_ensure_space(0, 2);
682
683 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
684 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
685 }
686
687 tap_set_state(tap_get_end_state());
688 }
689 static void vsllink_state_move_dma(void)
690 {
691 int i, insert_length = (tap_length % 8) ? (8 - (tap_length % 8)) : 0;
692 insert_insignificant_operation_t *insert = \
693 &VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[tap_move_ndx(tap_get_state())][tap_move_ndx(tap_get_end_state())];
694 uint8_t tms_scan = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
695
696 if (tap_get_state() == TAP_RESET)
697 {
698 vsllink_tap_ensure_space(0, 8);
699
700 for (i = 0; i < 8; i++)
701 {
702 vsllink_tap_append_step(1, 0);
703 }
704 }
705
706 if (insert_length > 0)
707 {
708 vsllink_tap_ensure_space(0, 16);
709
710 for (i = 0; i < insert->insert_position; i++)
711 {
712 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
713 }
714 for (i = 0; i < insert_length; i++)
715 {
716 vsllink_tap_append_step(insert->insert_value, 0);
717 }
718 for (i = insert->insert_position; i < 8; i++)
719 {
720 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
721 }
722 }
723 else
724 {
725 vsllink_tap_ensure_space(0, 8);
726
727 for (i = 0; i < 8; i++)
728 {
729 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
730 }
731 }
732
733 tap_set_state(tap_get_end_state());
734 }
735
736 // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
737 static void vsllink_add_path(int start, int num, tap_state_t *path)
738 {
739 int i;
740
741 for (i = start; i < (start + num); i++)
742 {
743 if ((i & 7) == 0)
744 {
745 if (i > 0)
746 {
747 vsllink_usb_out_buffer_idx++;
748 }
749 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
750 }
751
752 if (path[i - start] == tap_state_transition(tap_get_state(), true))
753 {
754 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= 1 << (i & 7);
755 }
756 else if (path[i - start] == tap_state_transition(tap_get_state(), false))
757 {
758 // nothing to do
759 }
760 else
761 {
762 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
763 exit(-1);
764 }
765 tap_set_state(path[i - start]);
766 }
767 if ((i > 0) && ((i & 7) == 0))
768 {
769 vsllink_usb_out_buffer_idx++;
770 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
771 }
772
773 tap_set_end_state(tap_get_state());
774 }
775
776 static void vsllink_path_move_normal(int num_states, tap_state_t *path)
777 {
778 int i, tms_len, tms_cmd_pos, path_idx = 0;
779
780 if (vsllink_tms_data_len > 0)
781 {
782 // there are vsllink_tms_data_len more tms bits to be shifted
783 // so there are vsllink_tms_data_len + num_states tms bits in all
784 tms_len = vsllink_tms_data_len + num_states;
785 if (tms_len <= 16)
786 {
787 // merge into last tms shift
788 if (tms_len < 8)
789 {
790 // just append tms data to the last tms byte
791 vsllink_add_path(vsllink_tms_data_len, num_states, path);
792 }
793 else if (tms_len == 8)
794 {
795 // end last tms shift command
796 (*vsllink_tms_cmd_pos)--;
797 vsllink_add_path(vsllink_tms_data_len, num_states, path);
798 }
799 else if (tms_len < 16)
800 {
801 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
802 {
803 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
804 // there is enought tms length in the current tms shift command
805 (*vsllink_tms_cmd_pos)++;
806 vsllink_add_path(vsllink_tms_data_len, num_states, path);
807 }
808 else
809 {
810 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
811 // not enough tms length in the current tms shift command
812 // so a new command should be added
813 // first decrease byte length of last tms shift command
814 (*vsllink_tms_cmd_pos)--;
815 // append tms data to the last tms byte
816 vsllink_add_path(vsllink_tms_data_len, 8 - vsllink_tms_data_len, path);
817 path += 8 - vsllink_tms_data_len;
818 // add new command(3 bytes)
819 vsllink_tap_ensure_space(0, 3);
820 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
821 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
822 vsllink_add_path(0, num_states - (8 - vsllink_tms_data_len), path);
823 }
824 }
825 else if (tms_len == 16)
826 {
827 // end last tms shift command
828 vsllink_add_path(vsllink_tms_data_len, num_states, path);
829 }
830
831 vsllink_tms_data_len = (vsllink_tms_data_len + num_states) & 7;
832 if (vsllink_tms_data_len == 0)
833 {
834 vsllink_tms_cmd_pos = NULL;
835 }
836 num_states = 0;
837 }
838 else
839 {
840 vsllink_add_path(vsllink_tms_data_len, 16 - vsllink_tms_data_len, path);
841
842 path += 16 - vsllink_tms_data_len;
843 num_states -= 16 - vsllink_tms_data_len;
844 vsllink_tms_data_len = 0;
845 vsllink_tms_cmd_pos = NULL;
846 }
847 }
848
849 if (num_states > 0)
850 {
851 // Normal operation, don't need to append tms data
852 vsllink_tms_data_len = num_states & 7;
853
854 while (num_states > 0)
855 {
856 if (num_states > ((VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8))
857 {
858 i = (VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8;
859 }
860 else
861 {
862 i = num_states;
863 }
864 tms_len = (i + 7) >> 3;
865 vsllink_tap_ensure_space(0, tms_len + 2);
866 tms_cmd_pos = vsllink_usb_out_buffer_idx;
867 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (tms_len - 1);
868
869 vsllink_add_path(0, i, path + path_idx);
870
871 path_idx += i;
872 num_states -= i;
873 }
874
875 if (vsllink_tms_data_len > 0)
876 {
877 if (tms_len < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
878 {
879 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[tms_cmd_pos];
880 (*vsllink_tms_cmd_pos)++;
881 }
882 else
883 {
884 vsllink_usb_out_buffer[tms_cmd_pos]--;
885
886 tms_len = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
887 vsllink_tap_ensure_space(0, 3);
888 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
889 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
890 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = tms_len;
891 }
892 }
893 }
894 }
895 static void vsllink_path_move_dma(int num_states, tap_state_t *path)
896 {
897 int i, j = 0;
898
899 if (tap_length & 7)
900 {
901 if ((8 - (tap_length & 7)) < num_states)
902 {
903 j = 8 - (tap_length & 7);
904 }
905 else
906 {
907 j = num_states;
908 }
909 for (i = 0; i < j; i++)
910 {
911 if (path[i] == tap_state_transition(tap_get_state(), false))
912 {
913 vsllink_tap_append_step(0, 0);
914 }
915 else if (path[i] == tap_state_transition(tap_get_state(), true))
916 {
917 vsllink_tap_append_step(1, 0);
918 }
919 else
920 {
921 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
922 exit(-1);
923 }
924 tap_set_state(path[i]);
925 }
926 num_states -= j;
927 }
928
929 if (num_states > 0)
930 {
931 vsllink_tap_ensure_space(0, num_states);
932
933 for (i = 0; i < num_states; i++)
934 {
935 if (path[j + i] == tap_state_transition(tap_get_state(), false))
936 {
937 vsllink_tap_append_step(0, 0);
938 }
939 else if (path[j + i] == tap_state_transition(tap_get_state(), true))
940 {
941 vsllink_tap_append_step(1, 0);
942 }
943 else
944 {
945 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
946 exit(-1);
947 }
948 tap_set_state(path[j + i]);
949 }
950 }
951
952 tap_set_end_state(tap_get_state());
953 }
954
955 static void vsllink_stableclocks_normal(int num_cycles, int tms)
956 {
957 int tms_len;
958 uint16_t tms_append_byte;
959
960 if (vsllink_tms_data_len > 0)
961 {
962 // there are vsllink_tms_data_len more tms bits to be shifted
963 // so there are vsllink_tms_data_len + num_cycles tms bits in all
964 tms_len = vsllink_tms_data_len + num_cycles;
965 if (tms > 0)
966 {
967 // append '1' for tms
968 tms_append_byte = (uint16_t)((((1 << num_cycles) - 1) << vsllink_tms_data_len) & 0xFFFF);
969 }
970 else
971 {
972 // append '0' for tms
973 tms_append_byte = 0;
974 }
975 if (tms_len <= 16)
976 {
977 // merge into last tms shift
978 if (tms_len < 8)
979 {
980 // just add to vsllink_tms_data_len
981 // same result if tun through
982 //vsllink_tms_data_len += num_cycles;
983 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= (uint8_t)(tms_append_byte & 0xFF);
984 }
985 else if (tms_len == 8)
986 {
987 // end last tms shift command
988 // just reduce it, and append last tms byte
989 (*vsllink_tms_cmd_pos)--;
990 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
991 }
992 else if (tms_len < 16)
993 {
994 if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) < VSLLINK_CMDJTAGSEQ_LENMSK)
995 {
996 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
997 // there is enought tms length in the current tms shift command
998 // increase the tms byte length by 1 and set the last byte to 0
999 (*vsllink_tms_cmd_pos)++;
1000 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
1001 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (uint8_t)(tms_append_byte >> 8);
1002 }
1003 else
1004 {
1005 // every tms shift command can contain VSLLINK_CMDJTAGSEQ_LENMSK + 1 bytes in most
1006 // not enough tms length in the current tms shift command
1007 // so a new command should be added
1008 // first decrease byte length of last tms shift command
1009 (*vsllink_tms_cmd_pos)--;
1010 // append last tms byte and move the command pointer to the next empty position
1011 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
1012 // add new command(3 bytes)
1013 vsllink_tap_ensure_space(0, 3);
1014 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1015 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
1016 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (uint8_t)(tms_append_byte >> 8);
1017 }
1018 }
1019 else if (tms_len == 16)
1020 {
1021 // end last tms shift command
1022 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
1023 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (uint8_t)(tms_append_byte >> 8);
1024 }
1025
1026 vsllink_tms_data_len = tms_len & 7;
1027 if (vsllink_tms_data_len == 0)
1028 {
1029 vsllink_tms_cmd_pos = NULL;
1030 }
1031 num_cycles = 0;
1032 }
1033 else
1034 {
1035 // more shifts will be needed
1036 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (uint8_t)(tms_append_byte & 0xFF);
1037 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (uint8_t)(tms_append_byte >> 8);
1038
1039 num_cycles -= 16 - vsllink_tms_data_len;
1040 vsllink_tms_data_len = 0;
1041 vsllink_tms_cmd_pos = NULL;
1042 }
1043 }
1044 // from here vsllink_tms_data_len == 0 or num_cycles == 0
1045
1046 if (vsllink_tms_data_len > 0)
1047 {
1048 // num_cycles == 0
1049 // no need to shift
1050 if (num_cycles > 0)
1051 {
1052 LOG_ERROR("There MUST be some bugs in the driver");
1053 exit(-1);
1054 }
1055 }
1056 else
1057 {
1058 // get number of bytes left to be sent
1059 tms_len = num_cycles >> 3;
1060 if (tms_len > 0)
1061 {
1062 vsllink_tap_ensure_space(1, 5);
1063 // if tms_len > 0, vsllink_tms_data_len == 0
1064 // so just add new command
1065 // LSB of the command byte is the tms value when do the shifting
1066 if (tms > 0)
1067 {
1068 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK | 1;
1069 }
1070 else
1071 {
1072 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSCLOCK;
1073 }
1074 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 0) & 0xff;
1075 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 8) & 0xff;
1076 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 16) & 0xff;
1077 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms_len >> 24) & 0xff;
1078
1079 vsllink_usb_in_want_length += 1;
1080 pending_scan_results_buffer[pending_scan_results_length].buffer = NULL;
1081 pending_scan_results_length++;
1082
1083 if (tms_len > 0xFFFF)
1084 {
1085 vsllink_tap_execute();
1086 }
1087 }
1088
1089 // post-process
1090 vsllink_tms_data_len = num_cycles & 7;
1091 if (vsllink_tms_data_len > 0)
1092 {
1093 vsllink_tap_ensure_space(0, 3);
1094 vsllink_tms_cmd_pos = &vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1095 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
1096 if (tms > 0)
1097 {
1098 // append '1' for tms
1099 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = (1 << vsllink_tms_data_len) - 1;
1100 }
1101 else
1102 {
1103 // append '0' for tms
1104 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0x00;
1105 }
1106 }
1107 }
1108 }
1109 static void vsllink_stableclocks_dma(int num_cycles, int tms)
1110 {
1111 int i, cur_cycles;
1112
1113 if (tap_length & 7)
1114 {
1115 if ((8 - (tap_length & 7)) < num_cycles)
1116 {
1117 cur_cycles = 8 - (tap_length & 7);
1118 }
1119 else
1120 {
1121 cur_cycles = num_cycles;
1122 }
1123 for (i = 0; i < cur_cycles; i++)
1124 {
1125 vsllink_tap_append_step(tms, 0);
1126 }
1127 num_cycles -= cur_cycles;
1128 }
1129
1130 while (num_cycles > 0)
1131 {
1132 if (num_cycles > 8 * tap_buffer_size)
1133 {
1134 cur_cycles = 8 * tap_buffer_size;
1135 }
1136 else
1137 {
1138 cur_cycles = num_cycles;
1139 }
1140
1141 vsllink_tap_ensure_space(0, cur_cycles);
1142
1143 for (i = 0; i < cur_cycles; i++)
1144 {
1145 vsllink_tap_append_step(tms, 0);
1146 }
1147
1148 num_cycles -= cur_cycles;
1149 }
1150 }
1151
1152 static void vsllink_runtest(int num_cycles)
1153 {
1154 tap_state_t saved_end_state = tap_get_end_state();
1155
1156 if (tap_get_state() != TAP_IDLE)
1157 {
1158 // enter into IDLE state
1159 vsllink_end_state(TAP_IDLE);
1160 vsllink_state_move();
1161 }
1162
1163 vsllink_stableclocks(num_cycles, 0);
1164
1165 // post-process
1166 // set end_state
1167 vsllink_end_state(saved_end_state);
1168 tap_set_state(TAP_IDLE);
1169 if (tap_get_end_state() != TAP_IDLE)
1170 {
1171 vsllink_state_move();
1172 }
1173 }
1174
1175 static void vsllink_scan_normal(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, scan_command_t *command)
1176 {
1177 tap_state_t saved_end_state;
1178 uint8_t bits_left, tms_tmp, tdi_len;
1179 int i;
1180
1181 if (0 == scan_size)
1182 {
1183 return;
1184 }
1185
1186 tdi_len = ((scan_size + 7) >> 3);
1187 if ((tdi_len + 7) > VSLLINK_BufferSize)
1188 {
1189 LOG_ERROR("Your implementation of VSLLink has not enough buffer");
1190 exit(-1);
1191 }
1192
1193 saved_end_state = tap_get_end_state();
1194
1195 /* Move to appropriate scan state */
1196 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1197
1198 if (vsllink_tms_data_len > 0)
1199 {
1200 if (tap_get_state() == tap_get_end_state())
1201 {
1202 // already in IRSHIFT or DRSHIFT state
1203 // merge tms data in the last tms shift command into next scan command
1204 if (*vsllink_tms_cmd_pos < 1)
1205 {
1206 LOG_ERROR("There MUST be some bugs in the driver");
1207 exit(-1);
1208 }
1209 else if (*vsllink_tms_cmd_pos < 2)
1210 {
1211 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1212 vsllink_usb_out_buffer_idx--;
1213 }
1214 else
1215 {
1216 tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
1217 *vsllink_tms_cmd_pos -= 2;
1218 }
1219
1220 vsllink_tap_ensure_space(1, tdi_len + 7);
1221 // VSLLINK_CMDJTAGSEQ_SCAN ored by 1 means that tms_before is valid
1222 // which is merged from the last tms shift command
1223 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1224 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1225 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 8) & 0xff;
1226 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = tms_tmp;
1227 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[0] << (8 - vsllink_tms_data_len);
1228
1229 for (i = 0; i < tdi_len; i++)
1230 {
1231 buffer[i] >>= 8 - vsllink_tms_data_len;
1232 if (i != tdi_len)
1233 {
1234 buffer[i] += buffer[i + 1] << vsllink_tms_data_len;
1235 }
1236 }
1237
1238 vsllink_tap_append_scan_normal(scan_size - vsllink_tms_data_len, buffer, command, vsllink_tms_data_len);
1239 scan_size -= 8 - vsllink_tms_data_len;
1240 vsllink_tms_data_len = 0;
1241 }
1242 else
1243 {
1244 vsllink_state_move();
1245 vsllink_tap_ensure_space(1, tdi_len + 5);
1246
1247 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN;
1248 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 0) & 0xff;
1249 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 8) & 0xff;
1250
1251 vsllink_tap_append_scan_normal(scan_size, buffer, command, 0);
1252 }
1253 }
1254 else
1255 {
1256 vsllink_tap_ensure_space(1, tdi_len + 7);
1257
1258 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
1259 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
1260 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
1261 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1262 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1263
1264 vsllink_tap_append_scan_normal(scan_size, buffer, command, 8);
1265 }
1266 vsllink_end_state(saved_end_state);
1267
1268 bits_left = scan_size & 0x07;
1269 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1270
1271 if (bits_left > 0)
1272 {
1273 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << (bits_left - 1);
1274 }
1275 else
1276 {
1277 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << 7;
1278 }
1279
1280 if (tap_get_state() != tap_get_end_state())
1281 {
1282 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(tap_get_state(), tap_get_end_state());
1283 }
1284 else
1285 {
1286 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1287 }
1288
1289 tap_set_state(tap_get_end_state());
1290 }
1291 static void vsllink_scan_dma(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, scan_command_t *command)
1292 {
1293 tap_state_t saved_end_state;
1294
1295 saved_end_state = tap_get_end_state();
1296
1297 /* Move to appropriate scan state */
1298 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1299
1300 vsllink_state_move();
1301 vsllink_end_state(saved_end_state);
1302
1303 /* Scan */
1304 vsllink_tap_append_scan_dma(scan_size, buffer, command);
1305
1306 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1307 while (tap_length % 8 != 0)
1308 {
1309 // more 0s in Pause
1310 vsllink_tap_append_step(0, 0);
1311 }
1312
1313 if (tap_get_state() != tap_get_end_state())
1314 {
1315 vsllink_state_move();
1316 }
1317 }
1318
1319 static void vsllink_reset(int trst, int srst)
1320 {
1321 int result;
1322
1323 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
1324
1325 /* Signals are active low */
1326 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORT;
1327 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
1328 vsllink_usb_out_buffer[2] = 0;
1329 if (srst == 0)
1330 {
1331 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_SRST;
1332 }
1333 if (trst == 0)
1334 {
1335 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_TRST;
1336 }
1337
1338 result = vsllink_usb_write(vsllink_jtag_handle, 3);
1339 if (result != 3)
1340 {
1341 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result);
1342 }
1343 }
1344
1345 static void vsllink_simple_command(uint8_t command)
1346 {
1347 int result;
1348
1349 DEBUG_JTAG_IO("0x%02x", command);
1350
1351 vsllink_usb_out_buffer[0] = command;
1352 result = vsllink_usb_write(vsllink_jtag_handle, 1);
1353
1354 if (result != 1)
1355 {
1356 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command, result);
1357 }
1358 }
1359
1360 static int vsllink_register_commands(struct command_context_s *cmd_ctx)
1361 {
1362 register_command(cmd_ctx, NULL, "vsllink_usb_vid", vsllink_handle_usb_vid_command,
1363 COMMAND_CONFIG, NULL);
1364 register_command(cmd_ctx, NULL, "vsllink_usb_pid", vsllink_handle_usb_pid_command,
1365 COMMAND_CONFIG, NULL);
1366 register_command(cmd_ctx, NULL, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command,
1367 COMMAND_CONFIG, NULL);
1368 register_command(cmd_ctx, NULL, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command,
1369 COMMAND_CONFIG, NULL);
1370 register_command(cmd_ctx, NULL, "vsllink_usb_interface", vsllink_handle_usb_interface_command,
1371 COMMAND_CONFIG, NULL);
1372 register_command(cmd_ctx, NULL, "vsllink_mode", vsllink_handle_mode_command,
1373 COMMAND_CONFIG, NULL);
1374
1375 return ERROR_OK;
1376 }
1377
1378 static int vsllink_handle_mode_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1379 {
1380 if (argc != 1) {
1381 LOG_ERROR("parameter error, should be one parameter for VID");
1382 return ERROR_FAIL;
1383 }
1384
1385 if (!strcmp(args[0], "normal"))
1386 {
1387 vsllink_mode = VSLLINK_MODE_NORMAL;
1388 }
1389 else if (!strcmp(args[0], "dma"))
1390 {
1391 vsllink_mode = VSLLINK_MODE_DMA;
1392 }
1393 else
1394 {
1395 LOG_ERROR("invalid vsllink_mode: %s", args[0]);
1396 return ERROR_FAIL;
1397 }
1398
1399 return ERROR_OK;
1400 }
1401
1402 static int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1403 {
1404 if (argc != 1)
1405 {
1406 LOG_ERROR("parameter error, should be one parameter for VID");
1407 return ERROR_OK;
1408 }
1409
1410 COMMAND_PARSE_NUMBER(u16, args[0], vsllink_usb_vid);
1411 return ERROR_OK;
1412 }
1413
1414 static int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1415 {
1416 if (argc != 1)
1417 {
1418 LOG_ERROR("parameter error, should be one parameter for PID");
1419 return ERROR_OK;
1420 }
1421 COMMAND_PARSE_NUMBER(u16, args[0], vsllink_usb_pid);
1422 return ERROR_OK;
1423 }
1424
1425 static int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1426 {
1427 if (argc != 1)
1428 {
1429 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1430 return ERROR_OK;
1431 }
1432
1433 COMMAND_PARSE_NUMBER(u8, args[0], vsllink_usb_bulkin);
1434
1435 vsllink_usb_bulkin |= 0x80;
1436
1437 return ERROR_OK;
1438 }
1439
1440 static int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1441 {
1442 if (argc != 1)
1443 {
1444 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1445 return ERROR_OK;
1446 }
1447
1448 COMMAND_PARSE_NUMBER(u8, args[0], vsllink_usb_bulkout);
1449
1450 vsllink_usb_bulkout &= ~0x80;
1451
1452 return ERROR_OK;
1453 }
1454
1455 static int vsllink_handle_usb_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1456 {
1457 if (argc != 1)
1458 {
1459 LOG_ERROR("parameter error, should be one parameter for interface number");
1460 return ERROR_OK;
1461 }
1462
1463 COMMAND_PARSE_NUMBER(u8, args[0], vsllink_usb_interface);
1464 return ERROR_OK;
1465 }
1466
1467 /***************************************************************************/
1468 /* VSLLink tap functions */
1469
1470 static void vsllink_tap_init_normal(void)
1471 {
1472 vsllink_usb_out_buffer_idx = 0;
1473 vsllink_usb_in_want_length = 0;
1474 pending_scan_results_length = 0;
1475 }
1476 static void vsllink_tap_init_dma(void)
1477 {
1478 tap_length = 0;
1479 pending_scan_results_length = 0;
1480 }
1481
1482 static void vsllink_tap_ensure_space_normal(int scans, int length)
1483 {
1484 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1485 int available_bytes = VSLLINK_BufferSize - vsllink_usb_out_buffer_idx;
1486
1487 if (scans > available_scans || length > available_bytes)
1488 {
1489 vsllink_tap_execute();
1490 }
1491 }
1492 static void vsllink_tap_ensure_space_dma(int scans, int length)
1493 {
1494 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1495 int available_bytes = tap_buffer_size * 8 - tap_length;
1496
1497 if (scans > available_scans || length > available_bytes)
1498 {
1499 vsllink_tap_execute();
1500 }
1501 }
1502
1503 static void vsllink_tap_append_step(int tms, int tdi)
1504 {
1505 last_tms = tms;
1506 int index = tap_length / 8;
1507
1508 if (index < tap_buffer_size)
1509 {
1510 int bit_index = tap_length % 8;
1511 uint8_t bit = 1 << bit_index;
1512
1513 if (tms)
1514 {
1515 tms_buffer[index] |= bit;
1516 }
1517 else
1518 {
1519 tms_buffer[index] &= ~bit;
1520 }
1521
1522 if (tdi)
1523 {
1524 tdi_buffer[index] |= bit;
1525 }
1526 else
1527 {
1528 tdi_buffer[index] &= ~bit;
1529 }
1530
1531 tap_length++;
1532 }
1533 else
1534 {
1535 LOG_ERROR("buffer overflow, tap_length=%d", tap_length);
1536 }
1537 }
1538
1539 static void vsllink_tap_append_scan_normal(int length, uint8_t *buffer, scan_command_t *command, int offset)
1540 {
1541 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1542 int i;
1543
1544 if (offset > 0)
1545 {
1546 vsllink_usb_in_want_length += ((length + 7) >> 3) + 1;
1547 }
1548 else
1549 {
1550 vsllink_usb_in_want_length += (length + 7) >> 3;
1551 }
1552 pending_scan_result->length = length;
1553 pending_scan_result->offset = offset;
1554 pending_scan_result->command = command;
1555 pending_scan_result->buffer = buffer;
1556
1557 for (i = 0; i < ((length + 7) >> 3); i++)
1558 {
1559 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[i];
1560 }
1561
1562 pending_scan_results_length++;
1563 }
1564 static void vsllink_tap_append_scan_dma(int length, uint8_t *buffer, scan_command_t *command)
1565 {
1566 pending_scan_result_t *pending_scan_result;
1567 int len_tmp, len_all, i;
1568
1569 len_all = 0;
1570 while (len_all < length)
1571 {
1572 if ((length - len_all) > tap_buffer_size * 8)
1573 {
1574 len_tmp = tap_buffer_size * 8;
1575 }
1576 else
1577 {
1578 len_tmp = length - len_all;
1579 }
1580
1581 vsllink_tap_ensure_space(1, (len_tmp + 7) & ~7);
1582
1583 pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1584 pending_scan_result->offset = tap_length;
1585 pending_scan_result->length = len_tmp;
1586 pending_scan_result->command = command;
1587 pending_scan_result->buffer = buffer + len_all / 8;
1588
1589 for (i = 0; i < len_tmp; i++)
1590 {
1591 vsllink_tap_append_step(((len_all + i) < length-1 ? 0 : 1), (buffer[(len_all + i)/8] >> ((len_all + i)%8)) & 1);
1592 }
1593
1594 pending_scan_results_length++;
1595 len_all += len_tmp;
1596 }
1597 }
1598
1599 /* Pad and send a tap sequence to the device, and receive the answer.
1600 * For the purpose of padding we assume that we are in reset or idle or pause state. */
1601 static int vsllink_tap_execute_normal(void)
1602 {
1603 int i;
1604 int result;
1605 int first = 0;
1606
1607 if (vsllink_tms_data_len > 0)
1608 {
1609 if ((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_IRPAUSE) && (tap_get_state() != TAP_DRPAUSE))
1610 {
1611 LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", tap_state_name(tap_get_state()));
1612 }
1613
1614 if (vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] & (1 << (vsllink_tms_data_len - 1)))
1615 {
1616 // last tms bit is '1'
1617 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= 0xFF << vsllink_tms_data_len;
1618 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0xFF;
1619 vsllink_tms_data_len = 0;
1620 }
1621 else
1622 {
1623 // last tms bit is '0'
1624 vsllink_usb_out_buffer_idx++;
1625 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1626 vsllink_tms_data_len = 0;
1627 }
1628 }
1629
1630 if (vsllink_usb_out_buffer_idx > 3)
1631 {
1632 if (vsllink_usb_out_buffer[0] == VSLLINK_CMD_HW_JTAGSEQCMD)
1633 {
1634 vsllink_usb_out_buffer[1] = (vsllink_usb_out_buffer_idx >> 0) & 0xff;
1635 vsllink_usb_out_buffer[2] = (vsllink_usb_out_buffer_idx >> 8) & 0xff;
1636 }
1637
1638 result = vsllink_usb_message(vsllink_jtag_handle, vsllink_usb_out_buffer_idx, vsllink_usb_in_want_length);
1639
1640 if (result == vsllink_usb_in_want_length)
1641 {
1642 for (i = 0; i < pending_scan_results_length; i++)
1643 {
1644 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
1645 uint8_t *buffer = pending_scan_result->buffer;
1646 int length = pending_scan_result->length;
1647 int offset = pending_scan_result->offset;
1648 scan_command_t *command = pending_scan_result->command;
1649
1650 if (buffer != NULL)
1651 {
1652 // IRSHIFT or DRSHIFT
1653 buf_set_buf(vsllink_usb_in_buffer, first * 8 + offset, buffer, 0, length);
1654 first += (length + offset + 7) >> 3;
1655
1656 DEBUG_JTAG_IO("JTAG scan read(%d bits):", length);
1657 #ifdef _DEBUG_JTAG_IO_
1658 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1659 #endif
1660
1661 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1662 {
1663 vsllink_tap_init();
1664 return ERROR_JTAG_QUEUE_FAILED;
1665 }
1666
1667 free(pending_scan_result->buffer);
1668 pending_scan_result->buffer = NULL;
1669 }
1670 else
1671 {
1672 first++;
1673 }
1674 }
1675 }
1676 else
1677 {
1678 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, vsllink_usb_in_want_length);
1679 return ERROR_JTAG_QUEUE_FAILED;
1680 }
1681
1682 vsllink_tap_init();
1683 }
1684 reset_command_pointer();
1685
1686 return ERROR_OK;
1687 }
1688 static int vsllink_tap_execute_dma(void)
1689 {
1690 int byte_length;
1691 int i;
1692 int result;
1693
1694 if (tap_length > 0)
1695 {
1696 /* Pad last byte so that tap_length is divisible by 8 */
1697 while (tap_length % 8 != 0)
1698 {
1699 /* More of the last TMS value keeps us in the same state,
1700 * analogous to free-running JTAG interfaces. */
1701 vsllink_tap_append_step(last_tms, 0);
1702 }
1703 byte_length = tap_length / 8;
1704
1705 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGRAWCMD;
1706 vsllink_usb_out_buffer[1] = ((byte_length * 2 + 3) >> 0) & 0xff; // package size
1707 vsllink_usb_out_buffer[2] = ((byte_length * 2 + 3) >> 8) & 0xff;
1708
1709 memcpy(&vsllink_usb_out_buffer[3], tdi_buffer, byte_length);
1710 memcpy(&vsllink_usb_out_buffer[3 + byte_length], tms_buffer, byte_length);
1711
1712 result = vsllink_usb_message(vsllink_jtag_handle, 3 + 2 * byte_length, byte_length);
1713 if (result == byte_length)
1714 {
1715 for (i = 0; i < pending_scan_results_length; i++)
1716 {
1717 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
1718 uint8_t *buffer = pending_scan_result->buffer;
1719 int length = pending_scan_result->length;
1720 int first = pending_scan_result->offset;
1721
1722 scan_command_t *command = pending_scan_result->command;
1723 buf_set_buf(vsllink_usb_in_buffer, first, buffer, 0, length);
1724
1725 DEBUG_JTAG_IO("JTAG scan read(%d bits, from %d bits):", length, first);
1726 #ifdef _DEBUG_JTAG_IO_
1727 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1728 #endif
1729
1730 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1731 {
1732 vsllink_tap_init();
1733 return ERROR_JTAG_QUEUE_FAILED;
1734 }
1735
1736 if (pending_scan_result->buffer != NULL)
1737 {
1738 free(pending_scan_result->buffer);
1739 }
1740 }
1741 }
1742 else
1743 {
1744 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, byte_length);
1745 return ERROR_JTAG_QUEUE_FAILED;
1746 }
1747
1748 vsllink_tap_init();
1749 }
1750
1751 return ERROR_OK;
1752 }
1753
1754 /*****************************************************************************/
1755 /* VSLLink USB low-level functions */
1756
1757 static vsllink_jtag_t* vsllink_usb_open(void)
1758 {
1759 struct usb_bus *busses;
1760 struct usb_bus *bus;
1761 struct usb_device *dev;
1762 int ret;
1763
1764 vsllink_jtag_t *result;
1765
1766 result = (vsllink_jtag_t*) malloc(sizeof(vsllink_jtag_t));
1767
1768 usb_init();
1769 usb_find_busses();
1770 usb_find_devices();
1771
1772 busses = usb_get_busses();
1773
1774 /* find vsllink_jtag device in usb bus */
1775
1776 for (bus = busses; bus; bus = bus->next)
1777 {
1778 for (dev = bus->devices; dev; dev = dev->next)
1779 {
1780 if ((dev->descriptor.idVendor == vsllink_usb_vid) && (dev->descriptor.idProduct == vsllink_usb_pid))
1781 {
1782 result->usb_handle = usb_open(dev);
1783 if (NULL == result->usb_handle)
1784 {
1785 LOG_ERROR("failed to open %04X:%04X, not enough permissions?", vsllink_usb_vid, vsllink_usb_pid);
1786 exit(-1);
1787 }
1788
1789 /* usb_set_configuration required under win32 */
1790 ret = usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
1791 if (ret != 0)
1792 {
1793 LOG_ERROR("fail to set configuration to %d, %d returned, not enough permissions?", dev->config[0].bConfigurationValue, ret);
1794 exit(-1);
1795 }
1796 ret = usb_claim_interface(result->usb_handle, vsllink_usb_interface);
1797 if (ret != 0)
1798 {
1799 LOG_ERROR("fail to claim interface %d, %d returned", vsllink_usb_interface, ret);
1800 exit(-1);
1801 }
1802
1803 #if 0
1804 /*
1805 * This makes problems under Mac OS X. And is not needed
1806 * under Windows. Hopefully this will not break a linux build
1807 */
1808 usb_set_altinterface(result->usb_handle, 0);
1809 #endif
1810 return result;
1811 }
1812 }
1813 }
1814
1815 free(result);
1816 return NULL;
1817 }
1818
1819 static void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag)
1820 {
1821 int ret;
1822
1823 ret = usb_release_interface(vsllink_jtag->usb_handle, vsllink_usb_interface);
1824 if (ret != 0)
1825 {
1826 LOG_ERROR("fail to release interface %d, %d returned", vsllink_usb_interface, ret);
1827 exit(-1);
1828 }
1829
1830 ret = usb_close(vsllink_jtag->usb_handle);
1831 if (ret != 0)
1832 {
1833 LOG_ERROR("fail to close usb, %d returned", ret);
1834 exit(-1);
1835 }
1836
1837 free(vsllink_jtag);
1838 }
1839
1840 /* Send a message and receive the reply. */
1841 static int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length)
1842 {
1843 int result;
1844
1845 result = vsllink_usb_write(vsllink_jtag, out_length);
1846 if (result == out_length)
1847 {
1848 if (in_length > 0)
1849 {
1850 result = vsllink_usb_read(vsllink_jtag);
1851 if (result == in_length)
1852 {
1853 return result;
1854 }
1855 else
1856 {
1857 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
1858 return -1;
1859 }
1860 }
1861 return 0;
1862 }
1863 else
1864 {
1865 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
1866 return -1;
1867 }
1868 }
1869
1870 /* Write data from out_buffer to USB. */
1871 static int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length)
1872 {
1873 int result;
1874
1875 if (out_length > VSLLINK_BufferSize)
1876 {
1877 LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length, VSLLINK_BufferSize);
1878 return -1;
1879 }
1880
1881 result = usb_bulk_write(vsllink_jtag->usb_handle, vsllink_usb_bulkout, \
1882 (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
1883
1884 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length, result);
1885
1886 #ifdef _DEBUG_USB_COMMS_
1887 LOG_DEBUG("USB out:");
1888 vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
1889 #endif
1890
1891 #ifdef _VSLLINK_IN_DEBUG_MODE_
1892 usleep(100000);
1893 #endif
1894
1895 return result;
1896 }
1897
1898 /* Read data from USB into in_buffer. */
1899 static int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag)
1900 {
1901 int result = usb_bulk_read(vsllink_jtag->usb_handle, vsllink_usb_bulkin, \
1902 (char *)vsllink_usb_in_buffer, VSLLINK_BufferSize, VSLLINK_USB_TIMEOUT);
1903
1904 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
1905
1906 #ifdef _DEBUG_USB_COMMS_
1907 LOG_DEBUG("USB in:");
1908 vsllink_debug_buffer(vsllink_usb_in_buffer, result);
1909 #endif
1910 return result;
1911 }
1912
1913 #define BYTES_PER_LINE 16
1914
1915 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
1916 static void vsllink_debug_buffer(uint8_t *buffer, int length)
1917 {
1918 char line[81];
1919 char s[4];
1920 int i;
1921 int j;
1922
1923 for (i = 0; i < length; i += BYTES_PER_LINE)
1924 {
1925 snprintf(line, 5, "%04x", i);
1926 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
1927 {
1928 snprintf(s, 4, " %02x", buffer[j]);
1929 strcat(line, s);
1930 }
1931 LOG_DEBUG("%s", line);
1932 }
1933 }
1934 #endif // _DEBUG_USB_COMMS_ || _DEBUG_JTAG_IO_