1 /***************************************************************************
2 * Copyright (C) 2009-2010 by Simon Qian <SimonQian@SimonQian.com> *
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. *
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. *
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 * It's distributed under GPLv3.
22 * You can find it at http://www.SimonQian.com/en/Versaloon.
29 #include <jtag/interface.h>
30 #include <jtag/commands.h>
31 #include "usb_common.h"
33 //#define _VSLLINK_IN_DEBUG_MODE_
35 static uint16_t vsllink_usb_vid;
36 static uint16_t vsllink_usb_pid;
37 static uint8_t vsllink_usb_bulkout;
38 static uint8_t vsllink_usb_bulkin;
39 static uint8_t vsllink_usb_interface;
40 static int VSLLINK_USB_TIMEOUT = 1000;
42 static int vsllink_tms_offset = 0;
44 /* Global USB buffers */
45 static uint8_t* vsllink_usb_in_buffer = NULL;
46 static uint8_t* vsllink_usb_out_buffer = NULL;
47 static int vsllink_buffer_size = 128;
49 /* Constants for Versaloon command */
50 #define VERSALOON_GET_INFO 0x00
51 #define VERSALOON_GET_TVCC 0x01
53 /* Constants for VSLLink command */
54 #define VSLLINK_CMD_CONN 0x80
55 #define VSLLINK_CMD_DISCONN 0x81
56 #define VSLLINK_CMD_SET_SPEED 0x82
57 #define VSLLINK_CMD_SET_PORT 0x90
58 #define VSLLINK_CMD_GET_PORT 0x91
59 #define VSLLINK_CMD_SET_PORTDIR 0x92
60 #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
61 #define VSLLINK_CMD_HW_JTAGHLCMD 0xA1
62 #define VSLLINK_CMD_HW_SWDCMD 0xA2
63 #define VSLLINK_CMD_HW_JTAGRAWCMD 0xA3
65 #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
66 #define VSLLINK_CMDJTAGSEQ_TMSCLOCK 0x40
67 #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
69 #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
70 #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
72 #define JTAG_PINMSK_SRST (1 << 0)
73 #define JTAG_PINMSK_TRST (1 << 1)
74 #define JTAG_PINMSK_USR1 (1 << 2)
75 #define JTAG_PINMSK_USR2 (1 << 3)
76 #define JTAG_PINMSK_TCK (1 << 4)
77 #define JTAG_PINMSK_TMS (1 << 5)
78 #define JTAG_PINMSK_TDI (1 << 6)
79 #define JTAG_PINMSK_TDO (1 << 7)
81 struct pending_scan_result {
84 int length; /* Number of bits to read */
85 struct scan_command *command; /* Corresponding scan command */
87 bool last; /* indicate the last scan pending */
90 #define MAX_PENDING_SCAN_RESULTS 256
92 static int pending_scan_results_length;
93 static struct pending_scan_result \
94 pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
96 /* Queue command functions */
97 static void vsllink_end_state(tap_state_t state);
98 static void vsllink_state_move(void);
99 static void vsllink_path_move(int num_states, tap_state_t *path);
100 static void vsllink_runtest(int num_cycles);
101 static void vsllink_stableclocks(int num_cycles, int tms);
102 static void vsllink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, \
103 int scan_size, struct scan_command *command);
104 static void vsllink_reset(int trst, int srst);
105 static void vsllink_simple_command(uint8_t command);
107 /* VSLLink tap buffer functions */
108 static void vsllink_tap_append_step(int tms, int tdi);
109 static void vsllink_tap_init(void);
110 static int vsllink_tap_execute(void);
111 static void vsllink_tap_ensure_pending(int scans);
112 static void vsllink_tap_append_scan(int length, uint8_t *buffer, \
113 struct scan_command *command);
115 /* VSLLink lowlevel functions */
117 struct usb_dev_handle* usb_handle;
120 static struct vsllink *vsllink_usb_open(void);
121 static void vsllink_usb_close(struct vsllink *vsllink);
122 static int vsllink_usb_message(struct vsllink *vsllink, int out_length, \
124 static int vsllink_usb_write(struct vsllink *vsllink, int out_length);
125 static int vsllink_usb_read(struct vsllink *vsllink);
127 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
128 static void vsllink_debug_buffer(uint8_t *buffer, int length);
131 static int tap_length = 0;
132 static int tap_buffer_size = 0;
133 static uint8_t *tms_buffer = NULL;
134 static uint8_t *tdi_buffer = NULL;
135 static uint8_t *tdo_buffer = NULL;
137 static struct vsllink* vsllink_handle = NULL;
139 static void reset_command_pointer(void)
144 static int vsllink_execute_queue(void)
146 struct jtag_command *cmd = jtag_command_queue;
151 DEBUG_JTAG_IO( "-------------------------------------"
153 "-------------------------------------");
155 reset_command_pointer();
161 DEBUG_JTAG_IO("runtest %i cycles, end in %s", \
162 cmd->cmd.runtest->num_cycles, \
163 tap_state_name(cmd->cmd.runtest->end_state));
165 vsllink_end_state(cmd->cmd.runtest->end_state);
166 vsllink_runtest(cmd->cmd.runtest->num_cycles);
170 DEBUG_JTAG_IO("statemove end in %s", \
171 tap_state_name(cmd->cmd.statemove->end_state));
173 vsllink_end_state(cmd->cmd.statemove->end_state);
174 vsllink_state_move();
178 DEBUG_JTAG_IO("pathmove: %i states, end in %s", \
179 cmd->cmd.pathmove->num_states, \
180 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
182 vsllink_path_move(cmd->cmd.pathmove->num_states, \
183 cmd->cmd.pathmove->path);
187 vsllink_end_state(cmd->cmd.scan->end_state);
189 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
190 if (cmd->cmd.scan->ir_scan)
192 DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %s:", \
194 tap_state_name(cmd->cmd.scan->end_state));
198 DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %s:", \
200 tap_state_name(cmd->cmd.scan->end_state));
203 #ifdef _DEBUG_JTAG_IO_
204 vsllink_debug_buffer(buffer, DIV_ROUND_UP(scan_size, 8));
207 type = jtag_scan_type(cmd->cmd.scan);
209 vsllink_scan(cmd->cmd.scan->ir_scan, type, buffer, \
210 scan_size, cmd->cmd.scan);
214 DEBUG_JTAG_IO("reset trst: %i srst %i", \
215 cmd->cmd.reset->trst, \
216 cmd->cmd.reset->srst);
218 vsllink_tap_execute();
220 if (cmd->cmd.reset->trst == 1)
222 tap_set_state(TAP_RESET);
224 vsllink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
228 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
229 vsllink_tap_execute();
230 jtag_sleep(cmd->cmd.sleep->us);
233 case JTAG_STABLECLOCKS:
234 DEBUG_JTAG_IO("add %d clocks", \
235 cmd->cmd.stableclocks->num_cycles);
236 switch (tap_get_state())
239 // tms should be '1' to stay in TAP_RESET mode
247 // in other mode, tms should be '0'
249 break; /* above stable states are OK */
251 LOG_ERROR("jtag_add_clocks() in non-stable state \"%s\"",
252 tap_state_name(tap_get_state()));
255 vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, \
260 LOG_ERROR("BUG: unknown JTAG command type encountered: %d", \
267 return vsllink_tap_execute();
270 static int vsllink_speed(int speed)
274 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_SPEED;
275 vsllink_usb_out_buffer[1] = (speed >> 0) & 0xff;
276 vsllink_usb_out_buffer[2] = (speed >> 8) & 0xFF;
278 result = vsllink_usb_write(vsllink_handle, 3);
286 LOG_ERROR("VSLLink setting speed failed (%d)", result);
287 return ERROR_JTAG_DEVICE_ERROR;
293 static int vsllink_khz(int khz, int *jtag_speed)
300 static int vsllink_speed_div(int jtag_speed, int *khz)
307 static int vsllink_init(void)
309 int check_cnt, to_tmp;
311 char version_str[100];
313 vsllink_usb_in_buffer = malloc(vsllink_buffer_size);
314 vsllink_usb_out_buffer = malloc(vsllink_buffer_size);
315 if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
317 LOG_ERROR("Not enough memory");
321 vsllink_handle = vsllink_usb_open();
322 if (vsllink_handle == 0)
324 LOG_ERROR("Can't find USB JTAG Interface!"\
325 "Please check connection and permissions.");
326 return ERROR_JTAG_INIT_FAILED;
328 LOG_DEBUG("vsllink found on %04X:%04X", vsllink_usb_vid, vsllink_usb_pid);
330 to_tmp = VSLLINK_USB_TIMEOUT;
331 VSLLINK_USB_TIMEOUT = 100;
333 while (check_cnt < 5)
335 vsllink_simple_command(VERSALOON_GET_INFO);
336 result = vsllink_usb_read(vsllink_handle);
340 vsllink_usb_in_buffer[result] = 0;
341 vsllink_buffer_size = vsllink_usb_in_buffer[0] + \
342 (vsllink_usb_in_buffer[1] << 8);
343 strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, \
344 sizeof(version_str));
345 LOG_INFO("%s", version_str);
347 // free the pre-alloc memroy
348 free(vsllink_usb_in_buffer);
349 free(vsllink_usb_out_buffer);
350 vsllink_usb_in_buffer = NULL;
351 vsllink_usb_out_buffer = NULL;
354 vsllink_usb_in_buffer = malloc(vsllink_buffer_size);
355 vsllink_usb_out_buffer = malloc(vsllink_buffer_size);
356 if ((vsllink_usb_in_buffer == NULL) || \
357 (vsllink_usb_out_buffer == NULL))
359 LOG_ERROR("Not enough memory");
364 LOG_INFO("buffer size for USB is %d bytes", \
365 vsllink_buffer_size);
367 // alloc tms/tdi/tdo buffer
368 tap_buffer_size = (vsllink_buffer_size - 3) / 2;
369 tms_buffer = (uint8_t*)malloc(tap_buffer_size);
370 tdi_buffer = (uint8_t*)malloc(tap_buffer_size);
371 tdo_buffer = (uint8_t*)malloc(tap_buffer_size);
372 if ((tms_buffer == NULL) || (tdi_buffer == NULL) || \
373 (tdo_buffer == NULL))
375 LOG_ERROR("Not enough memory");
380 vsllink_simple_command(VSLLINK_CMD_DISCONN);
385 // Fail to access Versaloon
386 LOG_ERROR("VSLLink initial failed");
389 VSLLINK_USB_TIMEOUT = to_tmp;
391 /* Some older firmware versions sometimes fail if the
392 * voltage isn't read first.
394 vsllink_simple_command(VERSALOON_GET_TVCC);
395 result = vsllink_usb_read(vsllink_handle);
398 LOG_WARNING("Fail to get target voltage");
402 LOG_INFO("Target runs at %d mV", vsllink_usb_in_buffer[0] + \
403 (vsllink_usb_in_buffer[1] << 8));
406 // connect to vsllink
407 vsllink_usb_out_buffer[0] = VSLLINK_CMD_CONN;
408 vsllink_usb_out_buffer[1] = 1;
409 vsllink_usb_message(vsllink_handle, 2, 0);
410 if (vsllink_usb_read(vsllink_handle) > 2)
412 strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, \
413 sizeof(version_str));
414 LOG_INFO("%s", version_str);
417 // Set SRST and TRST to output, Set USR1 and USR2 to input
418 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
419 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | \
420 JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
421 vsllink_usb_out_buffer[2] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
422 if (vsllink_usb_write(vsllink_handle, 3) != 3)
424 LOG_ERROR("VSLLink USB send data error");
430 LOG_INFO("VSLLink Interface ready");
437 static int vsllink_quit(void)
439 if ((vsllink_usb_in_buffer != NULL) && (vsllink_usb_out_buffer != NULL))
441 // Set all pins to input
442 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
443 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | \
444 JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
445 vsllink_usb_out_buffer[2] = 0;
446 if (vsllink_usb_write(vsllink_handle, 3) != 3)
448 LOG_ERROR("VSLLink USB send data error");
453 vsllink_simple_command(VSLLINK_CMD_DISCONN);
454 vsllink_usb_close(vsllink_handle);
455 vsllink_handle = NULL;
458 if (vsllink_usb_in_buffer != NULL)
460 free(vsllink_usb_in_buffer);
461 vsllink_usb_in_buffer = NULL;
463 if (vsllink_usb_out_buffer != NULL)
465 free(vsllink_usb_out_buffer);
466 vsllink_usb_out_buffer = NULL;
472 /***************************************************************************/
473 /* Queue command implementations */
475 static void vsllink_end_state(tap_state_t state)
477 if (tap_is_state_stable(state))
479 tap_set_end_state(state);
483 LOG_ERROR("BUG: %i is not a valid end state", state);
488 /* Goes to the end state. */
489 static void vsllink_state_move(void)
492 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
493 uint8_t tms_scan_bits = \
494 tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
496 for (i = 0; i < tms_scan_bits; i++)
498 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
501 tap_set_state(tap_get_end_state());
504 static void vsllink_path_move(int num_states, tap_state_t *path)
506 for (int i = 0; i < num_states; i++)
508 if (path[i] == tap_state_transition(tap_get_state(), false))
510 vsllink_tap_append_step(0, 0);
512 else if (path[i] == tap_state_transition(tap_get_state(), true))
514 vsllink_tap_append_step(1, 0);
518 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", \
519 tap_state_name(tap_get_state()), \
520 tap_state_name(path[i]));
524 tap_set_state(path[i]);
527 tap_set_end_state(tap_get_state());
530 static void vsllink_stableclocks(int num_cycles, int tms)
532 while (num_cycles > 0)
534 vsllink_tap_append_step(tms, 0);
539 static void vsllink_runtest(int num_cycles)
541 tap_state_t saved_end_state = tap_get_end_state();
543 if (tap_get_state() != TAP_IDLE)
545 // enter into IDLE state
546 vsllink_end_state(TAP_IDLE);
547 vsllink_state_move();
550 vsllink_stableclocks(num_cycles, 0);
554 vsllink_end_state(saved_end_state);
555 if (tap_get_end_state() != tap_get_end_state())
557 vsllink_state_move();
561 static void vsllink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, \
562 int scan_size, struct scan_command *command)
564 tap_state_t saved_end_state;
566 saved_end_state = tap_get_end_state();
568 /* Move to appropriate scan state */
569 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
571 if (tap_get_state() != tap_get_end_state())
573 vsllink_state_move();
575 vsllink_end_state(saved_end_state);
578 vsllink_tap_append_scan(scan_size, buffer, command);
580 /* Goto Pause and record position to insert tms:0 */
581 vsllink_tap_append_step(0, 0);
582 vsllink_tms_offset = tap_length;
584 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
586 if (tap_get_state() != tap_get_end_state())
588 vsllink_state_move();
592 static void vsllink_reset(int trst, int srst)
596 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
598 /* Signals are active low */
599 vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORT;
600 vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
601 vsllink_usb_out_buffer[2] = 0;
604 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_SRST;
608 vsllink_usb_out_buffer[2] |= JTAG_PINMSK_TRST;
611 result = vsllink_usb_write(vsllink_handle, 3);
614 LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result);
618 static void vsllink_simple_command(uint8_t command)
622 DEBUG_JTAG_IO("0x%02x", command);
624 vsllink_usb_out_buffer[0] = command;
625 result = vsllink_usb_write(vsllink_handle, 1);
629 LOG_ERROR("VSLLink command 0x%02x failed (%d)", command, result);
633 COMMAND_HANDLER(vsllink_handle_mode_command)
636 LOG_ERROR("parameter error, "
637 "should be one parameter for mode");
644 COMMAND_HANDLER(vsllink_handle_usb_vid_command)
648 LOG_ERROR("parameter error, "
649 "should be one parameter for VID");
653 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], vsllink_usb_vid);
657 COMMAND_HANDLER(vsllink_handle_usb_pid_command)
661 LOG_ERROR("parameter error, "
662 "should be one parameter for PID");
665 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], vsllink_usb_pid);
669 COMMAND_HANDLER(vsllink_handle_usb_bulkin_command)
673 LOG_ERROR("parameter error, "
674 "should be one parameter for BULKIN endpoint");
678 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], vsllink_usb_bulkin);
680 vsllink_usb_bulkin |= 0x80;
685 COMMAND_HANDLER(vsllink_handle_usb_bulkout_command)
689 LOG_ERROR("parameter error, "
690 "should be one parameter for BULKOUT endpoint");
694 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], vsllink_usb_bulkout);
696 vsllink_usb_bulkout &= ~0x80;
701 COMMAND_HANDLER(vsllink_handle_usb_interface_command)
705 LOG_ERROR("parameter error, "
706 "should be one parameter for interface number");
710 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], vsllink_usb_interface);
714 /***************************************************************************/
715 /* VSLLink tap functions */
717 static void vsllink_tap_init(void)
720 pending_scan_results_length = 0;
721 vsllink_tms_offset = 0;
724 static void vsllink_tap_ensure_pending(int scans)
726 int available_scans = \
727 MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
729 if (scans > available_scans)
731 vsllink_tap_execute();
735 static void vsllink_tap_append_step(int tms, int tdi)
737 int index = tap_length / 8;
739 int bit_index = tap_length % 8;
740 uint8_t bit = 1 << bit_index;
744 tms_buffer[index] |= bit;
748 tms_buffer[index] &= ~bit;
753 tdi_buffer[index] |= bit;
757 tdi_buffer[index] &= ~bit;
761 if (tap_buffer_size * 8 <= tap_length)
763 vsllink_tap_execute();
767 static void vsllink_tap_append_scan(int length, uint8_t *buffer, \
768 struct scan_command *command)
770 struct pending_scan_result *pending_scan_result;
771 int len_tmp, len_all, i;
774 while (len_all < length)
776 vsllink_tap_ensure_pending(1);
777 pending_scan_result = \
778 &pending_scan_results_buffer[pending_scan_results_length];
780 if ((length - len_all) > (tap_buffer_size * 8 - tap_length))
782 /* Use all memory available
783 vsllink_tap_append_step will commit automatically */
784 len_tmp = tap_buffer_size * 8 - tap_length;
785 pending_scan_result->last = false;
789 len_tmp = length - len_all;
790 pending_scan_result->last = true;
792 pending_scan_result->src_offset = tap_length;
793 pending_scan_result->dest_offset = len_all;
794 pending_scan_result->length = len_tmp;
795 pending_scan_result->command = command;
796 pending_scan_result->buffer = buffer;
797 pending_scan_results_length++;
799 for (i = 0; i < len_tmp; i++)
801 vsllink_tap_append_step(((len_all + i) < length-1 ? 0 : 1), \
802 (buffer[(len_all + i)/8] >> ((len_all + i)%8)) & 1);
809 static int vsllink_tap_execute(void)
820 /* Pad data so that tap_length is divisible by 8 */
821 if ((tap_length % 8) != 0)
823 if (vsllink_tms_offset > 0)
825 /* append tms:0 at vsllink_tms_offset, which is in Pause */
826 int start_pos = DIV_ROUND_UP(tap_length, 8) - 1;
827 int end_pos = DIV_ROUND_UP(vsllink_tms_offset, 8) - 1;
828 int shift_cnt = (start_pos + 1) * 8 - tap_length;
829 uint8_t last_mask = ~((1 << (vsllink_tms_offset % 8)) - 1);
833 if (start_pos == end_pos)
835 tms_buffer[start_pos] = \
836 (tms_buffer[start_pos] & ~last_mask) | \
837 ((tms_buffer[start_pos] & last_mask) << shift_cnt);
838 tdi_buffer[start_pos] = \
839 (tdi_buffer[start_pos] & ~last_mask) | \
840 ((tdi_buffer[start_pos] & last_mask) << shift_cnt);
843 else if (start_pos == (end_pos + 1))
845 tms_buffer[start_pos] = \
846 (tms_buffer[start_pos] << shift_cnt) | \
847 ((tms_buffer[start_pos - 1] & last_mask) >> (8 - shift_cnt));
848 tdi_buffer[start_pos] = \
849 (tdi_buffer[start_pos] << shift_cnt) | \
850 ((tdi_buffer[start_pos - 1] & last_mask) >> (8 - shift_cnt));
854 tms_buffer[start_pos] = \
855 (tms_buffer[start_pos] << shift_cnt) | \
856 (tms_buffer[start_pos - 1] >> (8 - shift_cnt));
857 tdi_buffer[start_pos] = \
858 (tdi_buffer[start_pos] << shift_cnt) | \
859 (tdi_buffer[start_pos - 1] >> (8 - shift_cnt));
863 tap_length = DIV_ROUND_UP(tap_length, 8) * 8;
867 /* append data at last */
868 while ((tap_length % 8) != 0)
870 vsllink_tap_append_step((tap_get_state() == TAP_RESET)?1:0, 0);
874 byte_length = tap_length / 8;
876 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGRAWCMD;
877 vsllink_usb_out_buffer[1] = ((byte_length * 2 + 3) >> 0) & 0xff;
878 vsllink_usb_out_buffer[2] = ((byte_length * 2 + 3) >> 8) & 0xff;
880 memcpy(&vsllink_usb_out_buffer[3], tdi_buffer, byte_length);
881 memcpy(&vsllink_usb_out_buffer[3 + byte_length], tms_buffer, byte_length);
883 result = vsllink_usb_message(vsllink_handle, 3 + 2 * byte_length, \
886 if (result == byte_length)
888 for (i = 0; i < pending_scan_results_length; i++)
890 struct pending_scan_result *pending_scan_result = \
891 &pending_scan_results_buffer[i];
892 uint8_t *buffer = pending_scan_result->buffer;
893 int length = pending_scan_result->length;
894 int src_first = pending_scan_result->src_offset;
895 int dest_first = pending_scan_result->dest_offset;
896 bool last = pending_scan_result->last;
898 struct scan_command *command = pending_scan_result->command;
899 buf_set_buf(vsllink_usb_in_buffer, src_first, buffer, \
902 DEBUG_JTAG_IO("JTAG scan read(%d bits, from %d bits):", \
904 #ifdef _DEBUG_JTAG_IO_
905 vsllink_debug_buffer(buffer + dest_first / 8, DIV_ROUND_UP(length, 7));
910 if (jtag_read_buffer(buffer, command) != ERROR_OK)
913 return ERROR_JTAG_QUEUE_FAILED;
916 if (pending_scan_result->buffer != NULL)
918 free(pending_scan_result->buffer);
925 LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", \
926 result, byte_length);
927 return ERROR_JTAG_QUEUE_FAILED;
935 /*****************************************************************************/
936 /* VSLLink USB low-level functions */
938 static struct vsllink* vsllink_usb_open(void)
942 const uint16_t vids[] = { vsllink_usb_vid, 0 };
943 const uint16_t pids[] = { vsllink_usb_pid, 0 };
944 struct usb_dev_handle *dev;
945 if (jtag_usb_open(vids, pids, &dev) != ERROR_OK)
948 /* usb_set_configuration required under win32 */
949 struct usb_device *udev = usb_device(dev);
950 int ret = usb_set_configuration(dev, udev->config[0].bConfigurationValue);
953 LOG_ERROR("fail to set configuration to %d (error %d)."
954 "Not enough permissions for the device?",
955 udev->config[0].bConfigurationValue, ret);
958 ret = usb_claim_interface(dev, vsllink_usb_interface);
961 LOG_ERROR("fail to claim interface %d, %d returned",
962 vsllink_usb_interface, ret);
967 * This makes problems under Mac OS X. And is not needed
968 * under Windows. Hopefully this will not break a linux build
970 usb_set_altinterface(dev, 0);
973 struct vsllink *result = malloc(sizeof(struct vsllink));
974 result->usb_handle = dev;
978 static void vsllink_usb_close(struct vsllink *vsllink)
982 ret = usb_release_interface(vsllink->usb_handle, vsllink_usb_interface);
985 LOG_ERROR("fail to release interface %d, %d returned", \
986 vsllink_usb_interface, ret);
990 ret = usb_close(vsllink->usb_handle);
993 LOG_ERROR("fail to close usb, %d returned", ret);
1000 /* Send a message and receive the reply. */
1001 static int vsllink_usb_message(struct vsllink *vsllink, int out_length, \
1006 result = vsllink_usb_write(vsllink, out_length);
1007 if (result == out_length)
1011 result = vsllink_usb_read(vsllink);
1012 if (result == in_length)
1018 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", \
1027 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", \
1028 out_length, result);
1033 /* Write data from out_buffer to USB. */
1034 static int vsllink_usb_write(struct vsllink *vsllink, int out_length)
1038 if (out_length > vsllink_buffer_size)
1040 LOG_ERROR("vsllink_write illegal out_length=%d (max=%d)", \
1041 out_length, vsllink_buffer_size);
1045 result = usb_bulk_write(vsllink->usb_handle, vsllink_usb_bulkout, \
1046 (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
1048 DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", \
1049 out_length, result);
1051 #ifdef _DEBUG_USB_COMMS_
1052 LOG_DEBUG("USB out:");
1053 vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
1056 #ifdef _VSLLINK_IN_DEBUG_MODE_
1063 /* Read data from USB into in_buffer. */
1064 static int vsllink_usb_read(struct vsllink *vsllink)
1066 int result = usb_bulk_read(vsllink->usb_handle, vsllink_usb_bulkin, \
1067 (char *)vsllink_usb_in_buffer, vsllink_buffer_size, \
1068 VSLLINK_USB_TIMEOUT);
1070 DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
1072 #ifdef _DEBUG_USB_COMMS_
1073 LOG_DEBUG("USB in:");
1074 vsllink_debug_buffer(vsllink_usb_in_buffer, result);
1079 #define BYTES_PER_LINE 16
1081 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
1082 static void vsllink_debug_buffer(uint8_t *buffer, int length)
1089 for (i = 0; i < length; i += BYTES_PER_LINE)
1091 snprintf(line, 5, "%04x", i);
1092 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
1094 snprintf(s, 4, " %02x", buffer[j]);
1097 LOG_DEBUG("%s", line);
1100 #endif // _DEBUG_USB_COMMS_ || _DEBUG_JTAG_IO_
1102 static const struct command_registration vsllink_command_handlers[] = {
1104 .name = "vsllink_usb_vid",
1105 .handler = &vsllink_handle_usb_vid_command,
1106 .mode = COMMAND_CONFIG,
1109 .name = "vsllink_usb_pid",
1110 .handler = &vsllink_handle_usb_pid_command,
1111 .mode = COMMAND_CONFIG,
1114 .name = "vsllink_usb_bulkin",
1115 .handler = &vsllink_handle_usb_bulkin_command,
1116 .mode = COMMAND_CONFIG,
1119 .name = "vsllink_usb_bulkout",
1120 .handler = &vsllink_handle_usb_bulkout_command,
1121 .mode = COMMAND_CONFIG,
1124 .name = "vsllink_usb_interface",
1125 .handler = &vsllink_handle_usb_interface_command,
1126 .mode = COMMAND_CONFIG,
1129 .name = "vsllink_mode",
1130 .handler = &vsllink_handle_mode_command,
1131 .mode = COMMAND_CONFIG,
1133 COMMAND_REGISTRATION_DONE
1136 struct jtag_interface vsllink_interface = {
1138 .commands = vsllink_command_handlers,
1140 .init = vsllink_init,
1141 .quit = vsllink_quit,
1143 .speed = vsllink_speed,
1144 .speed_div = vsllink_speed_div,
1145 .execute_queue = vsllink_execute_queue,