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