Transform 'u16' to 'uint16_t'
[fw/openocd] / 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         return parse_u16(args[0], &vsllink_usb_vid);
1411 }
1412
1413 static int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1414 {
1415         if (argc != 1)
1416         {
1417                 LOG_ERROR("parameter error, should be one parameter for PID");
1418                 return ERROR_OK;
1419         }
1420         return parse_u16(args[0], &vsllink_usb_pid);
1421 }
1422
1423 static int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1424 {
1425         if (argc != 1)
1426         {
1427                 LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
1428                 return ERROR_OK;
1429         }
1430
1431         int retval = parse_u8(args[0], &vsllink_usb_bulkin);
1432         if (ERROR_OK == retval)
1433                 vsllink_usb_bulkin |= 0x80;
1434
1435         return retval;
1436 }
1437
1438 static int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1439 {
1440         if (argc != 1)
1441         {
1442                 LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
1443                 return ERROR_OK;
1444         }
1445
1446         int retval = parse_u8(args[0], &vsllink_usb_bulkout);
1447         if (ERROR_OK == retval)
1448                 vsllink_usb_bulkout &= ~0x80;
1449
1450         return retval;
1451 }
1452
1453 static int vsllink_handle_usb_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1454 {
1455         if (argc != 1)
1456         {
1457                 LOG_ERROR("parameter error, should be one parameter for interface number");
1458                 return ERROR_OK;
1459         }
1460
1461         return parse_u8(args[0], &vsllink_usb_interface);
1462 }
1463
1464 /***************************************************************************/
1465 /* VSLLink tap functions */
1466
1467 static void vsllink_tap_init_normal(void)
1468 {
1469         vsllink_usb_out_buffer_idx = 0;
1470         vsllink_usb_in_want_length = 0;
1471         pending_scan_results_length = 0;
1472 }
1473 static void vsllink_tap_init_dma(void)
1474 {
1475         tap_length = 0;
1476         pending_scan_results_length = 0;
1477 }
1478
1479 static void vsllink_tap_ensure_space_normal(int scans, int length)
1480 {
1481         int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1482         int available_bytes = VSLLINK_BufferSize - vsllink_usb_out_buffer_idx;
1483
1484         if (scans > available_scans || length > available_bytes)
1485         {
1486                 vsllink_tap_execute();
1487         }
1488 }
1489 static void vsllink_tap_ensure_space_dma(int scans, int length)
1490 {
1491         int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1492         int available_bytes = tap_buffer_size * 8 - tap_length;
1493
1494         if (scans > available_scans || length > available_bytes)
1495         {
1496                 vsllink_tap_execute();
1497         }
1498 }
1499
1500 static void vsllink_tap_append_step(int tms, int tdi)
1501 {
1502         last_tms = tms;
1503         int index = tap_length / 8;
1504
1505         if (index < tap_buffer_size)
1506         {
1507                 int bit_index = tap_length % 8;
1508                 uint8_t bit = 1 << bit_index;
1509
1510                 if (tms)
1511                 {
1512                         tms_buffer[index] |= bit;
1513                 }
1514                 else
1515                 {
1516                         tms_buffer[index] &= ~bit;
1517                 }
1518
1519                 if (tdi)
1520                 {
1521                         tdi_buffer[index] |= bit;
1522                 }
1523                 else
1524                 {
1525                         tdi_buffer[index] &= ~bit;
1526                 }
1527
1528                 tap_length++;
1529         }
1530         else
1531         {
1532                 LOG_ERROR("buffer overflow, tap_length=%d", tap_length);
1533         }
1534 }
1535
1536 static void vsllink_tap_append_scan_normal(int length, uint8_t *buffer, scan_command_t *command, int offset)
1537 {
1538         pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1539         int i;
1540
1541         if (offset > 0)
1542         {
1543                 vsllink_usb_in_want_length += ((length + 7) >> 3) + 1;
1544         }
1545         else
1546         {
1547                 vsllink_usb_in_want_length += (length + 7) >> 3;
1548         }
1549         pending_scan_result->length = length;
1550         pending_scan_result->offset = offset;
1551         pending_scan_result->command = command;
1552         pending_scan_result->buffer = buffer;
1553
1554         for (i = 0; i < ((length + 7) >> 3); i++)
1555         {
1556                 vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[i];
1557         }
1558
1559         pending_scan_results_length++;
1560 }
1561 static void vsllink_tap_append_scan_dma(int length, uint8_t *buffer, scan_command_t *command)
1562 {
1563         pending_scan_result_t *pending_scan_result;
1564         int len_tmp, len_all, i;
1565
1566         len_all = 0;
1567         while (len_all < length)
1568         {
1569                 if ((length - len_all) > tap_buffer_size * 8)
1570                 {
1571                         len_tmp = tap_buffer_size * 8;
1572                 }
1573                 else
1574                 {
1575                         len_tmp = length - len_all;
1576                 }
1577
1578                 vsllink_tap_ensure_space(1, (len_tmp + 7) & ~7);
1579
1580                 pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
1581                 pending_scan_result->offset = tap_length;
1582                 pending_scan_result->length = len_tmp;
1583                 pending_scan_result->command = command;
1584                 pending_scan_result->buffer = buffer + len_all / 8;
1585
1586                 for (i = 0; i < len_tmp; i++)
1587                 {
1588                         vsllink_tap_append_step(((len_all+i) < length-1 ? 0 : 1), (buffer[(len_all+i)/8] >> ((len_all+i)%8)) & 1);
1589                 }
1590
1591                 pending_scan_results_length++;
1592                 len_all += len_tmp;
1593         }
1594 }
1595
1596 /* Pad and send a tap sequence to the device, and receive the answer.
1597  * For the purpose of padding we assume that we are in reset or idle or pause state. */
1598 static int vsllink_tap_execute_normal(void)
1599 {
1600         int i;
1601         int result;
1602         int first = 0;
1603
1604         if (vsllink_tms_data_len > 0)
1605         {
1606                 if((tap_get_state() != TAP_RESET) && (tap_get_state() != TAP_IDLE) && (tap_get_state() != TAP_IRPAUSE) && (tap_get_state() != TAP_DRPAUSE))
1607                 {
1608                         LOG_WARNING("%s is not in RESET or IDLE or PAUSR state", tap_state_name(tap_get_state()));
1609                 }
1610
1611                 if (vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] & (1 << (vsllink_tms_data_len - 1)))
1612                 {
1613                         // last tms bit is '1'
1614                         vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= 0xFF << vsllink_tms_data_len;
1615                         vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0xFF;
1616                         vsllink_tms_data_len = 0;
1617                 }
1618                 else
1619                 {
1620                         // last tms bit is '0'
1621                         vsllink_usb_out_buffer_idx++;
1622                         vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
1623                         vsllink_tms_data_len = 0;
1624                 }
1625         }
1626
1627         if (vsllink_usb_out_buffer_idx > 3)
1628         {
1629                 if (vsllink_usb_out_buffer[0] == VSLLINK_CMD_HW_JTAGSEQCMD)
1630                 {
1631                         vsllink_usb_out_buffer[1] = (vsllink_usb_out_buffer_idx >> 0) & 0xff;
1632                         vsllink_usb_out_buffer[2] = (vsllink_usb_out_buffer_idx >> 8) & 0xff;
1633                 }
1634
1635                 result = vsllink_usb_message(vsllink_jtag_handle, vsllink_usb_out_buffer_idx, vsllink_usb_in_want_length);
1636
1637                 if (result == vsllink_usb_in_want_length)
1638                 {
1639                         for (i = 0; i < pending_scan_results_length; i++)
1640                         {
1641                                 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
1642                                 uint8_t *buffer = pending_scan_result->buffer;
1643                                 int length = pending_scan_result->length;
1644                                 int offset = pending_scan_result->offset;
1645                                 scan_command_t *command = pending_scan_result->command;
1646
1647                                 if (buffer != NULL)
1648                                 {
1649                                         // IRSHIFT or DRSHIFT
1650                                         buf_set_buf(vsllink_usb_in_buffer, first * 8 + offset, buffer, 0, length);
1651                                         first += (length + offset + 7) >> 3;
1652
1653                                         DEBUG_JTAG_IO("JTAG scan read(%d bits):", length);
1654 #ifdef _DEBUG_JTAG_IO_
1655                                         vsllink_debug_buffer(buffer, (length + 7) >> 3);
1656 #endif
1657
1658                                         if (jtag_read_buffer(buffer, command) != ERROR_OK)
1659                                         {
1660                                                 vsllink_tap_init();
1661                                                 return ERROR_JTAG_QUEUE_FAILED;
1662                                         }
1663
1664                                         free(pending_scan_result->buffer);
1665                                         pending_scan_result->buffer = NULL;
1666                                 }
1667                                 else
1668                                 {
1669                                         first++;
1670                                 }
1671                         }
1672                 }
1673                 else
1674                 {
1675                         LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, vsllink_usb_in_want_length);
1676                         return ERROR_JTAG_QUEUE_FAILED;
1677                 }
1678
1679                 vsllink_tap_init();
1680         }
1681         reset_command_pointer();
1682
1683         return ERROR_OK;
1684 }
1685 static int vsllink_tap_execute_dma(void)
1686 {
1687         int byte_length;
1688         int i;
1689         int result;
1690
1691         if (tap_length > 0)
1692         {
1693                 /* Pad last byte so that tap_length is divisible by 8 */
1694                 while (tap_length % 8 != 0)
1695                 {
1696                         /* More of the last TMS value keeps us in the same state,
1697                          * analogous to free-running JTAG interfaces. */
1698                         vsllink_tap_append_step(last_tms, 0);
1699                 }
1700                 byte_length = tap_length / 8;
1701
1702                 vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGRAWCMD;
1703                 vsllink_usb_out_buffer[1] = ((byte_length * 2 + 3) >> 0) & 0xff;                // package size
1704                 vsllink_usb_out_buffer[2] = ((byte_length * 2 + 3) >> 8) & 0xff;
1705
1706                 memcpy(&vsllink_usb_out_buffer[3], tdi_buffer, byte_length);
1707                 memcpy(&vsllink_usb_out_buffer[3 + byte_length], tms_buffer, byte_length);
1708
1709                 result = vsllink_usb_message(vsllink_jtag_handle, 3 + 2 * byte_length, byte_length);
1710                 if (result == byte_length)
1711                 {
1712                         for (i = 0; i < pending_scan_results_length; i++)
1713                         {
1714                                 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
1715                                 uint8_t *buffer = pending_scan_result->buffer;
1716                                 int length = pending_scan_result->length;
1717                                 int first = pending_scan_result->offset;
1718
1719                                 scan_command_t *command = pending_scan_result->command;
1720                                 buf_set_buf(vsllink_usb_in_buffer, first, buffer, 0, length);
1721
1722                                 DEBUG_JTAG_IO("JTAG scan read(%d bits, from %d bits):", length, first);
1723 #ifdef _DEBUG_JTAG_IO_
1724                                 vsllink_debug_buffer(buffer, (length + 7) >> 3);
1725 #endif
1726
1727                                 if (jtag_read_buffer(buffer, command) != ERROR_OK)
1728                                 {
1729                                         vsllink_tap_init();
1730                                         return ERROR_JTAG_QUEUE_FAILED;
1731                                 }
1732
1733                                 if (pending_scan_result->buffer != NULL)
1734                                 {
1735                                         free(pending_scan_result->buffer);
1736                                 }
1737                         }
1738                 }
1739                 else
1740                 {
1741                         LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, byte_length);
1742                         return ERROR_JTAG_QUEUE_FAILED;
1743                 }
1744
1745                 vsllink_tap_init();
1746         }
1747
1748         return ERROR_OK;
1749 }
1750
1751 /*****************************************************************************/
1752 /* VSLLink USB low-level functions */
1753
1754 static vsllink_jtag_t* vsllink_usb_open(void)
1755 {
1756         struct usb_bus *busses;
1757         struct usb_bus *bus;
1758         struct usb_device *dev;
1759         int ret;
1760
1761         vsllink_jtag_t *result;
1762
1763         result = (vsllink_jtag_t*) malloc(sizeof(vsllink_jtag_t));
1764
1765         usb_init();
1766         usb_find_busses();
1767         usb_find_devices();
1768
1769         busses = usb_get_busses();
1770
1771         /* find vsllink_jtag device in usb bus */
1772
1773         for (bus = busses; bus; bus = bus->next)
1774         {
1775                 for (dev = bus->devices; dev; dev = dev->next)
1776                 {
1777                         if ((dev->descriptor.idVendor == vsllink_usb_vid) && (dev->descriptor.idProduct == vsllink_usb_pid))
1778                         {
1779                                 result->usb_handle = usb_open(dev);
1780                                 if (NULL == result->usb_handle)
1781                                 {
1782                                         LOG_ERROR("failed to open %04X:%04X, not enough permissions?", vsllink_usb_vid, vsllink_usb_pid);
1783                                         exit(-1);
1784                                 }
1785
1786                                 /* usb_set_configuration required under win32 */
1787                                 ret = usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
1788                                 if (ret != 0)
1789                                 {
1790                                         LOG_ERROR("fail to set configuration to %d, %d returned, not enough permissions?", dev->config[0].bConfigurationValue, ret);
1791                                         exit(-1);
1792                                 }
1793                                 ret = usb_claim_interface(result->usb_handle, vsllink_usb_interface);
1794                                 if (ret != 0)
1795                                 {
1796                                         LOG_ERROR("fail to claim interface %d, %d returned", vsllink_usb_interface, ret);
1797                                         exit(-1);
1798                                 }
1799
1800 #if 0
1801                                 /*
1802                                  * This makes problems under Mac OS X. And is not needed
1803                                  * under Windows. Hopefully this will not break a linux build
1804                                  */
1805                                 usb_set_altinterface(result->usb_handle, 0);
1806 #endif
1807                                 return result;
1808                         }
1809                 }
1810         }
1811
1812         free(result);
1813         return NULL;
1814 }
1815
1816 static void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag)
1817 {
1818         int ret;
1819
1820         ret = usb_release_interface(vsllink_jtag->usb_handle, vsllink_usb_interface);
1821         if (ret != 0)
1822         {
1823                 LOG_ERROR("fail to release interface %d, %d returned", vsllink_usb_interface, ret);
1824                 exit(-1);
1825         }
1826
1827         ret = usb_close(vsllink_jtag->usb_handle);
1828         if (ret != 0)
1829         {
1830                 LOG_ERROR("fail to close usb, %d returned", ret);
1831                 exit(-1);
1832         }
1833
1834         free(vsllink_jtag);
1835 }
1836
1837 /* Send a message and receive the reply. */
1838 static int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length)
1839 {
1840         int result;
1841
1842         result = vsllink_usb_write(vsllink_jtag, out_length);
1843         if (result == out_length)
1844         {
1845                 if (in_length > 0)
1846                 {
1847                         result = vsllink_usb_read(vsllink_jtag);
1848                         if (result == in_length )
1849                         {
1850                                 return result;
1851                         }
1852                         else
1853                         {
1854                                 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
1855                                 return -1;
1856                         }
1857                 }
1858                 return 0;
1859         }
1860         else
1861         {
1862                 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
1863                 return -1;
1864         }
1865 }
1866
1867 /* Write data from out_buffer to USB. */
1868 static int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length)
1869 {
1870         int result;
1871
1872         if (out_length > VSLLINK_BufferSize)
1873         {
1874                 LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length, VSLLINK_BufferSize);
1875                 return -1;
1876         }
1877
1878         result = usb_bulk_write(vsllink_jtag->usb_handle, vsllink_usb_bulkout, \
1879                 (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
1880
1881         DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length, result);
1882
1883 #ifdef _DEBUG_USB_COMMS_
1884         LOG_DEBUG("USB out:");
1885         vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
1886 #endif
1887
1888 #ifdef _VSLLINK_IN_DEBUG_MODE_
1889         usleep(100000);
1890 #endif
1891
1892         return result;
1893 }
1894
1895 /* Read data from USB into in_buffer. */
1896 static int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag)
1897 {
1898         int result = usb_bulk_read(vsllink_jtag->usb_handle, vsllink_usb_bulkin, \
1899                 (char *)vsllink_usb_in_buffer, VSLLINK_BufferSize, VSLLINK_USB_TIMEOUT);
1900
1901         DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
1902
1903 #ifdef _DEBUG_USB_COMMS_
1904         LOG_DEBUG("USB in:");
1905         vsllink_debug_buffer(vsllink_usb_in_buffer, result);
1906 #endif
1907         return result;
1908 }
1909
1910 #define BYTES_PER_LINE  16
1911
1912 #if defined _DEBUG_USB_COMMS_ || defined _DEBUG_JTAG_IO_
1913 static void vsllink_debug_buffer(uint8_t *buffer, int length)
1914 {
1915         char line[81];
1916         char s[4];
1917         int i;
1918         int j;
1919
1920         for (i = 0; i < length; i += BYTES_PER_LINE)
1921         {
1922                 snprintf(line, 5, "%04x", i);
1923                 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
1924                 {
1925                         snprintf(s, 4, " %02x", buffer[j]);
1926                         strcat(line, s);
1927                 }
1928                 LOG_DEBUG("%s", line);
1929         }
1930 }
1931 #endif // _DEBUG_USB_COMMS_ || _DEBUG_JTAG_IO_