You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1932 lines
52 KiB

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