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.
 
 
 
 
 
 

1333 lines
34 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007 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 "replacements.h"
  27. #include "jtag.h"
  28. #include <usb.h>
  29. #include <string.h>
  30. #include "log.h"
  31. //#define _VSLLINK_IN_DEBUG_MODE_
  32. /* enable this to view USB communication
  33. */
  34. #if 0
  35. #define _DEBUG_USB_COMMS_
  36. #endif
  37. #ifdef _DEBUG_JTAG_IO_
  38. #define DEBUG_JTAG_IO(expr ...) LOG_DEBUG(expr)
  39. #else
  40. #define DEBUG_JTAG_IO(expr ...)
  41. #endif
  42. #define VID 0x03EB
  43. #define PID 0x2103
  44. #define VSLLINK_WRITE_ENDPOINT 0x02
  45. #define VSLLINK_READ_ENDPOINT 0x82
  46. u16 vsllink_vid = VID;
  47. u16 vsllink_pid = PID;
  48. u8 vsllink_bulkout = VSLLINK_WRITE_ENDPOINT;
  49. u8 vsllink_bulkin = VSLLINK_READ_ENDPOINT;
  50. #define VSLLINK_USB_TIMEOUT 1000
  51. static int VSLLINK_BufferSize = 1024;
  52. /* Global USB buffers */
  53. static int vsllink_usb_out_buffer_idx;
  54. static int vsllink_usb_in_want_length;
  55. static u8* vsllink_usb_in_buffer = NULL;
  56. static u8* vsllink_usb_out_buffer = NULL;
  57. /* Constants for VSLLink command */
  58. #define VSLLINK_CMD_CONN 0x80
  59. #define VSLLINK_CMD_DISCONN 0x81
  60. #define VSLLINK_CMD_SET_SPEED 0x82
  61. #define VSLLINK_CMD_SET_PORT 0x90
  62. #define VSLLINK_CMD_GET_PORT 0x91
  63. #define VSLLINK_CMD_SET_PORTDIR 0x92
  64. #define VSLLINK_CMD_HW_JTAGSEQCMD 0xA0
  65. #define VSLLINK_CMDJTAGSEQ_TMSBYTE 0x00
  66. #define VSLLINK_CMDJTAGSEQ_SCAN 0x80
  67. #define VSLLINK_CMDJTAGSEQ_CMDMSK 0xC0
  68. #define VSLLINK_CMDJTAGSEQ_LENMSK 0x3F
  69. #define JTAG_PINMSK_SRST (1 << 0)
  70. #define JTAG_PINMSK_TRST (1 << 1)
  71. #define JTAG_PINMSK_USR1 (1 << 2)
  72. #define JTAG_PINMSK_USR2 (1 << 3)
  73. #define JTAG_PINMSK_TCK (1 << 4)
  74. #define JTAG_PINMSK_TMS (1 << 5)
  75. #define JTAG_PINMSK_TDI (1 << 6)
  76. #define JTAG_PINMSK_TDO (1 << 7)
  77. #define VSLLINK_TAP_MOVE(from, to) VSLLINK_tap_move[tap_move_map[from]][tap_move_map[to]]
  78. /* VSLLINK_tap_move[i][j]: tap movement command to go from state i to state j
  79. * 0: Test-Logic-Reset
  80. * 1: Run-Test/Idle
  81. * 2: Shift-DR
  82. * 3: Pause-DR
  83. * 4: Shift-IR
  84. * 5: Pause-IR
  85. *
  86. * SD->SD and SI->SI have to be caught in interface specific code
  87. */
  88. u8 VSLLINK_tap_move[6][6] =
  89. {
  90. /* TLR RTI SD PD SI PI */
  91. {0xff, 0x00, 0x2f, 0x0a, 0x37, 0x16}, /* TLR */
  92. {0xff, 0x00, 0x45, 0x05, 0x4b, 0x0b}, /* RTI */
  93. {0xff, 0x61, 0x00, 0x01, 0x0f, 0x2f}, /* SD */
  94. {0xff, 0x60, 0x40, 0x17, 0x3c, 0x2f}, /* PD */
  95. {0xff, 0x61, 0x07, 0x17, 0x00, 0x01}, /* SI */
  96. {0xff, 0x60, 0x38, 0x17, 0x40, 0x2f} /* PI */
  97. };
  98. u8 VSLLINK_TAP_MOVE_FROM_E1[6] =
  99. {
  100. // TLR RTI SD PD SI PI
  101. 0xff, 0x60, 0x38, 0x5c, 0x3c, 0x5E
  102. };
  103. u8 VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[7][6][2] =
  104. {
  105. /* stuff offset */
  106. {/* TLR */
  107. {1, 0,}, /* TLR */
  108. {1, 0,}, /* RTI */
  109. {1, 0,}, /* SD */
  110. {1, 0,}, /* PD */
  111. {1, 0,}, /* SI */
  112. {1, 0,}}, /* PI */
  113. {/* RTI */
  114. {1, 0,}, /* TLR */
  115. {0, 0,}, /* RTI */
  116. {0, 4,}, /* SD */
  117. {0, 7,}, /* PD */
  118. {0, 5,}, /* SI */
  119. {0, 7,}}, /* PI */
  120. {/* SD */
  121. {0, 0,}, /* TLR */
  122. {0, 0,}, /* RTI */
  123. {0, 0,}, /* SD */
  124. {0, 0,}, /* PD */
  125. {0, 0,}, /* SI */
  126. {0, 0,}}, /* PI */
  127. {/* PD */
  128. {0, 0,}, /* TLR */
  129. {0, 0,}, /* RTI */
  130. {0, 0,}, /* SD */
  131. {0, 0,}, /* PD */
  132. {0, 0,}, /* SI */
  133. {0, 0,}}, /* PI */
  134. {/* SI */
  135. {0, 0,}, /* TLR */
  136. {0, 0,}, /* RTI */
  137. {0, 0,}, /* SD */
  138. {0, 0,}, /* PD */
  139. {0, 0,}, /* SI */
  140. {0, 0,}}, /* PI */
  141. {/* PI */
  142. {0, 0,}, /* TLR */
  143. {0, 0,}, /* RTI */
  144. {0, 0,}, /* SD */
  145. {0, 0,}, /* PD */
  146. {0, 0,}, /* SI */
  147. {0, 0,}}, /* PI */
  148. };
  149. u8 VSLLINK_BIT_MSK[8] =
  150. {
  151. 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f
  152. };
  153. /* External interface functions */
  154. int vsllink_execute_queue(void);
  155. int vsllink_speed(int speed);
  156. int vsllink_khz(int khz, int *jtag_speed);
  157. int vsllink_speed_div(int jtag_speed, int *khz);
  158. int vsllink_register_commands(struct command_context_s *cmd_ctx);
  159. int vsllink_init(void);
  160. int vsllink_quit(void);
  161. /* CLI command handler functions */
  162. int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  163. int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  164. int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  165. int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  166. /* Queue command functions */
  167. void vsllink_end_state(enum tap_state state);
  168. void vsllink_state_move(void);
  169. void vsllink_path_move(int num_states, enum tap_state *path);
  170. void vsllink_runtest(int num_cycles);
  171. void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
  172. void vsllink_reset(int trst, int srst);
  173. void vsllink_simple_command(u8 command);
  174. /* VSLLink tap buffer functions */
  175. void vsllink_tap_init(void);
  176. int vsllink_tap_execute(void);
  177. void vsllink_tap_ensure_space(int scans, int bytes);
  178. void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset);
  179. /* VSLLink lowlevel functions */
  180. typedef struct vsllink_jtag
  181. {
  182. struct usb_dev_handle* usb_handle;
  183. } vsllink_jtag_t;
  184. vsllink_jtag_t *vsllink_usb_open(void);
  185. void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag);
  186. int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length);
  187. int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length);
  188. int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag);
  189. void vsllink_debug_buffer(u8 *buffer, int length);
  190. int vsllink_tms_data_len = 0;
  191. u8* vsllink_tms_cmd_pos;
  192. vsllink_jtag_t* vsllink_jtag_handle;
  193. /***************************************************************************/
  194. /* External interface implementation */
  195. jtag_interface_t vsllink_interface =
  196. {
  197. .name = "vsllink",
  198. .execute_queue = vsllink_execute_queue,
  199. .speed = vsllink_speed,
  200. .khz = vsllink_khz,
  201. .speed_div = vsllink_speed_div,
  202. .register_commands = vsllink_register_commands,
  203. .init = vsllink_init,
  204. .quit = vsllink_quit
  205. };
  206. int vsllink_execute_queue(void)
  207. {
  208. jtag_command_t *cmd = jtag_command_queue;
  209. int scan_size;
  210. enum scan_type type;
  211. u8 *buffer;
  212. DEBUG_JTAG_IO("--------------------------------------------------------------------------------");
  213. vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
  214. vsllink_usb_out_buffer_idx = 3;
  215. while (cmd != NULL)
  216. {
  217. switch (cmd->type)
  218. {
  219. case JTAG_END_STATE:
  220. DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
  221. if (cmd->cmd.end_state->end_state != -1)
  222. {
  223. vsllink_end_state(cmd->cmd.end_state->end_state);
  224. }
  225. break;
  226. case JTAG_RUNTEST:
  227. DEBUG_JTAG_IO( "runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
  228. cmd->cmd.runtest->end_state);
  229. if (cmd->cmd.runtest->end_state != -1)
  230. {
  231. vsllink_end_state(cmd->cmd.runtest->end_state);
  232. }
  233. vsllink_runtest(cmd->cmd.runtest->num_cycles);
  234. break;
  235. case JTAG_STATEMOVE:
  236. DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
  237. if (cmd->cmd.statemove->end_state != -1)
  238. {
  239. vsllink_end_state(cmd->cmd.statemove->end_state);
  240. }
  241. vsllink_state_move();
  242. break;
  243. case JTAG_PATHMOVE:
  244. DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
  245. cmd->cmd.pathmove->num_states, \
  246. cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  247. vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
  248. break;
  249. case JTAG_SCAN:
  250. if (cmd->cmd.scan->end_state != -1)
  251. {
  252. vsllink_end_state(cmd->cmd.scan->end_state);
  253. }
  254. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  255. if (cmd->cmd.scan->ir_scan)
  256. {
  257. DEBUG_JTAG_IO("JTAG Scan write IR(%d bits), end in %d:", scan_size, cmd->cmd.scan->end_state);
  258. }
  259. else
  260. {
  261. DEBUG_JTAG_IO("JTAG Scan write DR(%d bits), end in %d:", scan_size, cmd->cmd.scan->end_state);
  262. }
  263. #ifdef _DEBUG_JTAG_IO_
  264. vsllink_debug_buffer(buffer, (scan_size + 7) >> 3);
  265. #endif
  266. type = jtag_scan_type(cmd->cmd.scan);
  267. vsllink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
  268. break;
  269. case JTAG_RESET:
  270. DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  271. vsllink_tap_execute();
  272. if (cmd->cmd.reset->trst == 1)
  273. {
  274. cur_state = TAP_RESET;
  275. }
  276. vsllink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  277. vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
  278. vsllink_usb_out_buffer_idx = 3;
  279. break;
  280. case JTAG_SLEEP:
  281. DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
  282. vsllink_tap_execute();
  283. jtag_sleep(cmd->cmd.sleep->us);
  284. break;
  285. default:
  286. LOG_ERROR("BUG: unknown JTAG command type encountered");
  287. exit(-1);
  288. }
  289. cmd = cmd->next;
  290. }
  291. return vsllink_tap_execute();
  292. }
  293. int vsllink_speed(int speed)
  294. {
  295. int result;
  296. vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_SPEED;
  297. vsllink_usb_out_buffer[1] = (speed >> 0) & 0xff;
  298. vsllink_usb_out_buffer[2] = (speed >> 8) & 0xFF;
  299. result = vsllink_usb_write(vsllink_jtag_handle, 3);
  300. if (result == 3)
  301. {
  302. return ERROR_OK;
  303. }
  304. else
  305. {
  306. LOG_ERROR("VSLLink setting speed failed (%d)", result);
  307. return ERROR_JTAG_DEVICE_ERROR;
  308. }
  309. return ERROR_OK;
  310. }
  311. int vsllink_khz(int khz, int *jtag_speed)
  312. {
  313. *jtag_speed = khz;
  314. return ERROR_OK;
  315. }
  316. int vsllink_speed_div(int jtag_speed, int *khz)
  317. {
  318. *khz = jtag_speed;
  319. return ERROR_OK;
  320. }
  321. int vsllink_register_commands(struct command_context_s *cmd_ctx)
  322. {
  323. register_command(cmd_ctx, NULL, "vsllink_usb_vid", vsllink_handle_usb_vid_command,
  324. COMMAND_CONFIG, NULL);
  325. register_command(cmd_ctx, NULL, "vsllink_usb_pid", vsllink_handle_usb_pid_command,
  326. COMMAND_CONFIG, NULL);
  327. register_command(cmd_ctx, NULL, "vsllink_usb_bulkin", vsllink_handle_usb_bulkin_command,
  328. COMMAND_CONFIG, NULL);
  329. register_command(cmd_ctx, NULL, "vsllink_usb_bulkout", vsllink_handle_usb_bulkout_command,
  330. COMMAND_CONFIG, NULL);
  331. return ERROR_OK;
  332. }
  333. int vsllink_init(void)
  334. {
  335. int check_cnt;
  336. int result;
  337. char version_str[100];
  338. vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
  339. vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
  340. if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
  341. {
  342. LOG_ERROR("Not enough memory");
  343. exit(-1);
  344. }
  345. vsllink_jtag_handle = vsllink_usb_open();
  346. if (vsllink_jtag_handle == 0)
  347. {
  348. LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
  349. return ERROR_JTAG_INIT_FAILED;
  350. }
  351. check_cnt = 0;
  352. while (check_cnt < 3)
  353. {
  354. vsllink_simple_command(VSLLINK_CMD_CONN);
  355. result = vsllink_usb_read(vsllink_jtag_handle);
  356. if (result > 2)
  357. {
  358. vsllink_usb_in_buffer[result] = 0;
  359. VSLLINK_BufferSize = vsllink_usb_in_buffer[0] + (vsllink_usb_in_buffer[1] << 8);
  360. strncpy(version_str, (char *)vsllink_usb_in_buffer + 2, sizeof(version_str));
  361. LOG_INFO(version_str);
  362. // free the pre-alloc memroy
  363. free(vsllink_usb_in_buffer);
  364. free(vsllink_usb_out_buffer);
  365. vsllink_usb_in_buffer = NULL;
  366. vsllink_usb_out_buffer = NULL;
  367. // alloc new memory
  368. vsllink_usb_in_buffer = malloc(VSLLINK_BufferSize);
  369. vsllink_usb_out_buffer = malloc(VSLLINK_BufferSize);
  370. if ((vsllink_usb_in_buffer == NULL) || (vsllink_usb_out_buffer == NULL))
  371. {
  372. LOG_ERROR("Not enough memory");
  373. exit(-1);
  374. }
  375. else
  376. {
  377. LOG_INFO("buffer size for USB is %d bytes", VSLLINK_BufferSize);
  378. }
  379. break;
  380. }
  381. vsllink_simple_command(VSLLINK_CMD_DISCONN);
  382. check_cnt++;
  383. }
  384. if (check_cnt == 3)
  385. {
  386. // It's dangerout to proced
  387. LOG_ERROR("VSLLink initial failed");
  388. exit(-1);
  389. }
  390. // Set SRST and TRST to output, Set USR1 and USR2 to input
  391. vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORTDIR;
  392. vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST | JTAG_PINMSK_USR1 | JTAG_PINMSK_USR2;
  393. vsllink_usb_out_buffer[2] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
  394. result = vsllink_usb_write(vsllink_jtag_handle, 3);
  395. if (result != 3)
  396. {
  397. LOG_ERROR("VSLLink USB send data error");
  398. exit(-1);
  399. }
  400. vsllink_reset(0, 0);
  401. LOG_INFO("VSLLink JTAG Interface ready");
  402. vsllink_tap_init();
  403. return ERROR_OK;
  404. }
  405. int vsllink_quit(void)
  406. {
  407. if ((vsllink_usb_in_buffer != NULL) && (vsllink_usb_out_buffer != NULL))
  408. {
  409. vsllink_simple_command(VSLLINK_CMD_DISCONN);
  410. vsllink_usb_close(vsllink_jtag_handle);
  411. }
  412. if (vsllink_usb_in_buffer != NULL)
  413. {
  414. free(vsllink_usb_in_buffer);
  415. }
  416. if (vsllink_usb_out_buffer != NULL)
  417. {
  418. free(vsllink_usb_out_buffer);
  419. }
  420. return ERROR_OK;
  421. }
  422. // when vsllink_tms_data_len > 0, vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] is the byte that need to be appended.
  423. // length of VSLLINK_CMDJTAGSEQ_TMSBYTE has been set.
  424. void VSLLINK_add_tms_from_RTI(enum tap_state state)
  425. {
  426. u8 tms_scan = VSLLINK_TAP_MOVE(TAP_IDLE, state);
  427. u16 tms2;
  428. if ((cur_state != TAP_IDLE) || (state == TAP_IDLE) || (vsllink_tms_data_len <= 0) || (vsllink_tms_data_len >= 8) || (vsllink_tms_cmd_pos == NULL))
  429. {
  430. LOG_ERROR("There MUST be some bugs in the driver");
  431. exit(-1);
  432. }
  433. tms2 = (tms_scan & VSLLINK_BIT_MSK[VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[1][tap_move_map[state]][1]]) << vsllink_tms_data_len;
  434. if (VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[1][tap_move_map[state]][0] == 1)
  435. {
  436. tms2 |= VSLLINK_BIT_MSK[8 - vsllink_tms_data_len] << (vsllink_tms_data_len + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[1][tap_move_map[state]][1]);
  437. }
  438. tms2 |= (tms_scan >> VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[1][tap_move_map[state]][1]) << (8 + VSLLINK_TAP_MOVE_INSERT_INSIGNIFICANT[1][tap_move_map[state]][1]);
  439. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] |= (tms2 >> 0) & 0xff;
  440. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tms2 >> 8) & 0xff;
  441. vsllink_tms_data_len = 0;
  442. vsllink_tms_cmd_pos = NULL;
  443. }
  444. /***************************************************************************/
  445. /* Queue command implementations */
  446. void vsllink_end_state(enum tap_state state)
  447. {
  448. if (tap_move_map[state] != -1)
  449. {
  450. end_state = state;
  451. }
  452. else
  453. {
  454. LOG_ERROR("BUG: %i is not a valid end state", state);
  455. exit(-1);
  456. }
  457. }
  458. /* Goes to the end state. */
  459. void vsllink_state_move(void)
  460. {
  461. if (vsllink_tms_data_len > 0)
  462. {
  463. VSLLINK_add_tms_from_RTI(end_state);
  464. }
  465. else
  466. {
  467. vsllink_tap_ensure_space(0, 2);
  468. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
  469. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
  470. }
  471. cur_state = end_state;
  472. }
  473. // write tms from current vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx]
  474. void vsllink_add_path(int start, int num, enum tap_state *path)
  475. {
  476. int i;
  477. for (i = start; i < (start + num); i++)
  478. {
  479. if ((i & 7) == 0)
  480. {
  481. if (i > 0)
  482. {
  483. vsllink_usb_out_buffer[++vsllink_usb_out_buffer_idx] = 0;
  484. }
  485. else
  486. {
  487. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = 0;
  488. }
  489. }
  490. if (path[i - start] == tap_transitions[cur_state].high)
  491. {
  492. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] |= 1 << (i & 7);
  493. }
  494. else
  495. {
  496. LOG_ERROR("BUG: %d -> %d isn't a valid TAP transition", cur_state, path[i]);
  497. exit(-1);
  498. }
  499. cur_state = path[i];
  500. }
  501. end_state = cur_state;
  502. }
  503. void vsllink_path_move(int num_states, enum tap_state *path)
  504. {
  505. int i, tms_len, tms_cmd_pos, path_idx = 0;
  506. if (vsllink_tms_data_len > 0)
  507. {
  508. if ((vsllink_tms_data_len + num_states) < 8)
  509. {
  510. vsllink_add_path(vsllink_tms_data_len, num_states, path);
  511. num_states = 0;
  512. }
  513. else if ((vsllink_tms_data_len + num_states) < 16)
  514. {
  515. if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) \
  516. < VSLLINK_CMDJTAGSEQ_LENMSK)
  517. {
  518. *vsllink_tms_cmd_pos++;
  519. vsllink_add_path(vsllink_tms_data_len, num_states, path);
  520. }
  521. else
  522. {
  523. // need a new VSLLINK_CMDJTAGSEQ_TMSBYTE command
  524. // if vsllink_tms_data_len > 0, length of VSLLINK_CMDJTAGSEQ_TMSBYTE MUST be > 1(tms_len > 2)
  525. *vsllink_tms_cmd_pos--;
  526. vsllink_add_path(vsllink_tms_data_len, 8 - vsllink_tms_data_len, path);
  527. vsllink_usb_out_buffer_idx++;
  528. vsllink_tap_ensure_space(0, 3);
  529. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
  530. vsllink_tms_cmd_pos = vsllink_usb_out_buffer + vsllink_usb_out_buffer_idx;
  531. vsllink_add_path(vsllink_tms_data_len, num_states + vsllink_tms_data_len - 8, path + 8 - vsllink_tms_data_len);
  532. }
  533. vsllink_tms_data_len = (vsllink_tms_data_len + num_states) & 7;
  534. num_states = 0;
  535. }
  536. else
  537. {
  538. vsllink_add_path(vsllink_tms_data_len, 16 - vsllink_tms_data_len, path);
  539. path_idx = 16 - vsllink_tms_data_len;
  540. vsllink_usb_out_buffer_idx++;
  541. num_states -= 16 - vsllink_tms_data_len;
  542. path += 16 - vsllink_tms_data_len;
  543. vsllink_tms_data_len = 0;
  544. vsllink_tms_cmd_pos = NULL;
  545. }
  546. }
  547. if (num_states > 0)
  548. {
  549. // Normal operation, don't need to append tms data
  550. vsllink_tms_data_len = num_states & 7;
  551. while (num_states > 0)
  552. {
  553. if (num_states > ((VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8))
  554. {
  555. i = (VSLLINK_CMDJTAGSEQ_LENMSK + 1) * 8;
  556. }
  557. else
  558. {
  559. i = num_states;
  560. }
  561. tms_len = (i + 7) >> 3;
  562. vsllink_tap_ensure_space(0, tms_len + 2);
  563. tms_cmd_pos = vsllink_usb_out_buffer_idx;
  564. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (tms_len - 1);
  565. vsllink_add_path(0, i, path + path_idx);
  566. path_idx += i;
  567. num_states -= i;
  568. }
  569. if (vsllink_tms_data_len > 0)
  570. {
  571. if (tms_len < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
  572. {
  573. vsllink_usb_out_buffer[tms_cmd_pos]++;
  574. vsllink_usb_out_buffer = vsllink_usb_out_buffer + tms_cmd_pos;
  575. }
  576. else
  577. {
  578. vsllink_usb_out_buffer[tms_cmd_pos]--;
  579. tms_len = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  580. vsllink_tap_ensure_space(0, 3);
  581. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
  582. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] = tms_len;
  583. }
  584. }
  585. }
  586. }
  587. void vsllink_runtest(int num_cycles)
  588. {
  589. int i, j;
  590. int tms_len, first_tms = 0, tms_cmd_pos;
  591. enum tap_state saved_end_state = end_state;
  592. if (cur_state != TAP_IDLE)
  593. {
  594. vsllink_end_state(TAP_IDLE);
  595. if (vsllink_tms_data_len > 0)
  596. {
  597. VSLLINK_add_tms_from_RTI(end_state);
  598. }
  599. else
  600. {
  601. first_tms = 1;
  602. }
  603. }
  604. if (vsllink_tms_data_len > 0)
  605. {
  606. // cur_state == TAP_IDLE
  607. if ((vsllink_tms_data_len + num_cycles) < 8)
  608. {
  609. vsllink_tms_data_len += num_cycles;
  610. num_cycles = 0;
  611. }
  612. else if ((vsllink_tms_data_len + num_cycles) < 16)
  613. {
  614. if ((*vsllink_tms_cmd_pos & VSLLINK_CMDJTAGSEQ_LENMSK) \
  615. < VSLLINK_CMDJTAGSEQ_LENMSK)
  616. {
  617. *vsllink_tms_cmd_pos++;
  618. vsllink_usb_out_buffer[++vsllink_usb_out_buffer_idx] = 0;
  619. }
  620. else
  621. {
  622. // need a new VSLLINK_CMDJTAGSEQ_TMSBYTE command
  623. // if vsllink_tms_data_len > 0, length of VSLLINK_CMDJTAGSEQ_TMSBYTE MUST be > 1(tms_len > 2)
  624. *vsllink_tms_cmd_pos--;
  625. vsllink_tap_ensure_space(0, 3);
  626. vsllink_usb_out_buffer[++vsllink_usb_out_buffer_idx] = VSLLINK_CMDJTAGSEQ_TMSBYTE | 1;
  627. vsllink_tms_cmd_pos = vsllink_usb_out_buffer + vsllink_usb_out_buffer_idx;
  628. vsllink_usb_out_buffer[++vsllink_usb_out_buffer_idx] = 0;
  629. }
  630. vsllink_tms_data_len = (vsllink_tms_data_len + num_cycles) & 7;
  631. num_cycles = 0;
  632. }
  633. else
  634. {
  635. vsllink_usb_out_buffer[++vsllink_usb_out_buffer_idx] = 0;
  636. vsllink_usb_out_buffer_idx++;
  637. num_cycles -= 16 - vsllink_tms_data_len;
  638. vsllink_tms_data_len = 0;
  639. vsllink_tms_cmd_pos = NULL;
  640. }
  641. }
  642. tms_len = ((num_cycles + 7) >> 3) + first_tms;
  643. if (tms_len > 0)
  644. {
  645. // Normal operation, don't need to append tms data
  646. vsllink_tms_data_len = num_cycles & 7;
  647. if (vsllink_tms_data_len > 0)
  648. {
  649. tms_len += 1;
  650. }
  651. // tms_len includes the length of tms byte to append
  652. // Make sure there is enough space
  653. // 1 more byte maybe needed for the last tms move
  654. vsllink_tap_ensure_space(0, (tms_len / VSLLINK_CMDJTAGSEQ_LENMSK) + tms_len + 1);
  655. while(tms_len > 0)
  656. {
  657. if (tms_len > (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
  658. {
  659. i = VSLLINK_CMDJTAGSEQ_LENMSK + 1;
  660. }
  661. else
  662. {
  663. i = tms_len;
  664. }
  665. tms_cmd_pos = vsllink_usb_out_buffer_idx;
  666. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE | (i - 1);
  667. if (first_tms)
  668. {
  669. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
  670. first_tms = 0;
  671. j = i - 1;
  672. }
  673. else
  674. {
  675. j = i;
  676. }
  677. while (j-- > 0)
  678. {
  679. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
  680. }
  681. tms_len -= i;
  682. }
  683. // post process vsllink_usb_out_buffer_idx
  684. if (vsllink_tms_data_len > 0)
  685. {
  686. vsllink_usb_out_buffer_idx -= 2;
  687. }
  688. // Set end_state
  689. vsllink_end_state(saved_end_state);
  690. cur_state = TAP_IDLE;
  691. if (saved_end_state != TAP_IDLE)
  692. {
  693. if (vsllink_tms_data_len > 0)
  694. {
  695. VSLLINK_add_tms_from_RTI(end_state);
  696. }
  697. else
  698. {
  699. if (i < (VSLLINK_CMDJTAGSEQ_LENMSK + 1))
  700. {
  701. vsllink_usb_out_buffer[tms_cmd_pos]++;
  702. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(TAP_IDLE, end_state);
  703. }
  704. else
  705. {
  706. vsllink_tap_ensure_space(0, 2);
  707. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
  708. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(TAP_IDLE, end_state);
  709. }
  710. }
  711. cur_state = saved_end_state;
  712. }
  713. if (vsllink_tms_data_len > 0)
  714. {
  715. vsllink_tms_cmd_pos = vsllink_usb_out_buffer + tms_cmd_pos;
  716. }
  717. }
  718. else
  719. {
  720. // Set end_state if no RTI shifts
  721. vsllink_end_state(saved_end_state);
  722. cur_state = TAP_IDLE;
  723. if (saved_end_state != TAP_IDLE)
  724. {
  725. vsllink_tap_ensure_space(0, 2);
  726. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_TMSBYTE;
  727. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(TAP_IDLE, end_state);
  728. cur_state = saved_end_state;
  729. }
  730. }
  731. }
  732. void vsllink_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
  733. {
  734. enum tap_state saved_end_state;
  735. u8 bits_left, tms_tmp, tdi_len;
  736. int i;
  737. tdi_len = ((scan_size + 7) >> 3);
  738. if ((tdi_len + 7) > VSLLINK_BufferSize)
  739. {
  740. LOG_ERROR("Your implementation of VSLLink has not enough buffer");
  741. exit(-1);
  742. }
  743. saved_end_state = end_state;
  744. /* Move to appropriate scan state */
  745. vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  746. if (vsllink_tms_data_len > 0)
  747. {
  748. if (cur_state == end_state)
  749. {
  750. *vsllink_tms_cmd_pos--;
  751. tms_tmp = vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx];
  752. vsllink_tap_ensure_space(1, tdi_len + 7);
  753. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
  754. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
  755. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
  756. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = tms_tmp;
  757. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[0] << (8 - vsllink_tms_data_len);
  758. for (i = 0; i < tdi_len; i++)
  759. {
  760. buffer[i] >>= 8 - vsllink_tms_data_len;
  761. if (i != tdi_len)
  762. {
  763. buffer[i] += buffer[i + 1] << vsllink_tms_data_len;
  764. }
  765. }
  766. vsllink_tap_append_scan(scan_size - vsllink_tms_data_len, buffer, command, vsllink_tms_data_len);
  767. scan_size -= 8 - vsllink_tms_data_len;
  768. }
  769. else
  770. {
  771. VSLLINK_add_tms_from_RTI(end_state);
  772. vsllink_tap_ensure_space(1, tdi_len + 5);
  773. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN;
  774. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 0) & 0xff;
  775. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = (tdi_len >> 8) & 0xff;
  776. vsllink_tap_append_scan(scan_size, buffer, command, 0);
  777. }
  778. }
  779. else
  780. {
  781. vsllink_tap_ensure_space(1, tdi_len + 7);
  782. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_CMDJTAGSEQ_SCAN | 1;
  783. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1) >> 0) & 0xff;
  784. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = ((tdi_len + 1)>> 8) & 0xff;
  785. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE(cur_state, end_state);
  786. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
  787. vsllink_tap_append_scan(scan_size, buffer, command, 8);
  788. }
  789. vsllink_end_state(saved_end_state);
  790. bits_left = scan_size & 0x07;
  791. cur_state = ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE;
  792. if (bits_left > 0)
  793. {
  794. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << (bits_left - 1);
  795. }
  796. else
  797. {
  798. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 1 << 7;
  799. }
  800. if (cur_state != end_state)
  801. {
  802. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = VSLLINK_TAP_MOVE_FROM_E1[tap_move_map[end_state]];
  803. }
  804. else
  805. {
  806. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
  807. }
  808. cur_state = end_state;
  809. }
  810. void vsllink_reset(int trst, int srst)
  811. {
  812. int result;
  813. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  814. /* Signals are active low */
  815. vsllink_usb_out_buffer[0] = VSLLINK_CMD_SET_PORT;
  816. vsllink_usb_out_buffer[1] = JTAG_PINMSK_SRST | JTAG_PINMSK_TRST;
  817. vsllink_usb_out_buffer[2] = 0;
  818. if (srst == 0)
  819. {
  820. vsllink_usb_out_buffer[2] |= JTAG_PINMSK_SRST;
  821. }
  822. if (trst == 0)
  823. {
  824. vsllink_usb_out_buffer[2] |= JTAG_PINMSK_TRST;
  825. }
  826. result = vsllink_usb_write(vsllink_jtag_handle, 3);
  827. if (result != 3)
  828. {
  829. LOG_ERROR("VSLLink command VSLLINK_CMD_SET_PORT failed (%d)", result);
  830. }
  831. }
  832. void vsllink_simple_command(u8 command)
  833. {
  834. int result;
  835. DEBUG_JTAG_IO("0x%02x", command);
  836. vsllink_usb_out_buffer[0] = command;
  837. result = vsllink_usb_write(vsllink_jtag_handle, 1);
  838. if (result != 1)
  839. {
  840. LOG_ERROR("VSLLink command 0x%02x failed (%d)", command, result);
  841. }
  842. }
  843. int vsllink_handle_usb_vid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  844. {
  845. if (argc != 1) {
  846. LOG_ERROR("parameter error, should be one parameter for VID");
  847. return ERROR_OK;
  848. }
  849. vsllink_vid = strtol(args[0], NULL, 0);
  850. return ERROR_OK;
  851. }
  852. int vsllink_handle_usb_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  853. {
  854. if (argc != 1) {
  855. LOG_ERROR("parameter error, should be one parameter for PID");
  856. return ERROR_OK;
  857. }
  858. vsllink_pid = strtol(args[0], NULL, 0);
  859. return ERROR_OK;
  860. }
  861. int vsllink_handle_usb_bulkin_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  862. {
  863. if (argc != 1) {
  864. LOG_ERROR("parameter error, should be one parameter for BULKIN endpoint");
  865. return ERROR_OK;
  866. }
  867. vsllink_bulkin = strtol(args[0], NULL, 0) | 0x80;
  868. return ERROR_OK;
  869. }
  870. int vsllink_handle_usb_bulkout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  871. {
  872. if (argc != 1) {
  873. LOG_ERROR("parameter error, should be one parameter for BULKOUT endpoint");
  874. return ERROR_OK;
  875. }
  876. vsllink_bulkout = strtol(args[0], NULL, 0);
  877. return ERROR_OK;
  878. }
  879. /***************************************************************************/
  880. /* VSLLink tap functions */
  881. typedef struct
  882. {
  883. int length; /* Number of bits to read */
  884. int offset;
  885. scan_command_t *command; /* Corresponding scan command */
  886. u8 *buffer;
  887. } pending_scan_result_t;
  888. #define MAX_PENDING_SCAN_RESULTS 256
  889. static int pending_scan_results_length;
  890. static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
  891. void vsllink_tap_init()
  892. {
  893. vsllink_usb_out_buffer_idx = 0;
  894. vsllink_usb_in_want_length = 0;
  895. pending_scan_results_length = 0;
  896. }
  897. void vsllink_tap_ensure_space(int scans, int bytes)
  898. {
  899. int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
  900. int available_bytes = VSLLINK_BufferSize - vsllink_usb_out_buffer_idx;
  901. if (scans > available_scans || bytes > available_bytes)
  902. {
  903. vsllink_tap_execute();
  904. vsllink_usb_out_buffer[0] = VSLLINK_CMD_HW_JTAGSEQCMD;
  905. vsllink_usb_out_buffer_idx = 3;
  906. }
  907. }
  908. void vsllink_tap_append_scan(int length, u8 *buffer, scan_command_t *command, int offset)
  909. {
  910. pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
  911. int i;
  912. if (offset > 0)
  913. {
  914. vsllink_usb_in_want_length += ((length + 7) >> 3) + 1;
  915. }
  916. else
  917. {
  918. vsllink_usb_in_want_length += (length + 7) >> 3;
  919. }
  920. pending_scan_result->length = length;
  921. pending_scan_result->offset = offset;
  922. pending_scan_result->command = command;
  923. pending_scan_result->buffer = buffer;
  924. for (i = 0; i < ((length + 7) >> 3); i++)
  925. {
  926. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = buffer[i];
  927. }
  928. pending_scan_results_length++;
  929. }
  930. /* Pad and send a tap sequence to the device, and receive the answer.
  931. * For the purpose of padding we assume that we are in idle or pause state. */
  932. int vsllink_tap_execute()
  933. {
  934. int i;
  935. int result;
  936. int first = 0;
  937. if (vsllink_tms_data_len > 0)
  938. {
  939. if (vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx] & (1 << (vsllink_tms_data_len - 1)))
  940. {
  941. // last tms bit is '1'
  942. // the only possible state is TLR, no need to control the number of shifts in RLT
  943. // There MUST be some errors in the code
  944. LOG_ERROR("last tms bit is '1'");
  945. exit(-1);
  946. }
  947. else
  948. {
  949. // last tms bit is '0'
  950. vsllink_usb_out_buffer_idx++;
  951. vsllink_usb_out_buffer[vsllink_usb_out_buffer_idx++] = 0;
  952. vsllink_tms_data_len = 0;
  953. }
  954. }
  955. if (vsllink_usb_out_buffer_idx > 3)
  956. {
  957. if (vsllink_usb_out_buffer[0] == VSLLINK_CMD_HW_JTAGSEQCMD)
  958. {
  959. vsllink_usb_out_buffer[1] = (vsllink_usb_out_buffer_idx >> 0) & 0xff;
  960. vsllink_usb_out_buffer[2] = (vsllink_usb_out_buffer_idx >> 8) & 0xff;
  961. }
  962. result = vsllink_usb_message(vsllink_jtag_handle, vsllink_usb_out_buffer_idx, vsllink_usb_in_want_length);
  963. if (result == vsllink_usb_in_want_length)
  964. {
  965. for (i = 0; i < pending_scan_results_length; i++)
  966. {
  967. pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
  968. u8 *buffer = pending_scan_result->buffer;
  969. int length = pending_scan_result->length;
  970. int offset = pending_scan_result->offset;
  971. scan_command_t *command = pending_scan_result->command;
  972. /* Copy to buffer */
  973. buf_set_buf(vsllink_usb_in_buffer, first * 8 + offset, buffer, 0, length);
  974. first += (length + offset + 7) >> 3;
  975. DEBUG_JTAG_IO("JTAG scan read(%d bits):", length);
  976. #ifdef _DEBUG_JTAG_IO_
  977. vsllink_debug_buffer(buffer, (length + 7) >> 3);
  978. #endif
  979. if (jtag_read_buffer(buffer, command) != ERROR_OK)
  980. {
  981. vsllink_tap_init();
  982. return ERROR_JTAG_QUEUE_FAILED;
  983. }
  984. if (pending_scan_result->buffer != NULL)
  985. {
  986. free(pending_scan_result->buffer);
  987. pending_scan_result->buffer = NULL;
  988. }
  989. }
  990. }
  991. else
  992. {
  993. LOG_ERROR("vsllink_tap_execute, wrong result %d, expected %d", result, vsllink_usb_in_want_length);
  994. return ERROR_JTAG_QUEUE_FAILED;
  995. }
  996. vsllink_tap_init();
  997. }
  998. return ERROR_OK;
  999. }
  1000. /*****************************************************************************/
  1001. /* VSLLink USB low-level functions */
  1002. vsllink_jtag_t* vsllink_usb_open()
  1003. {
  1004. struct usb_bus *busses;
  1005. struct usb_bus *bus;
  1006. struct usb_device *dev;
  1007. vsllink_jtag_t *result;
  1008. result = (vsllink_jtag_t*) malloc(sizeof(vsllink_jtag_t));
  1009. usb_init();
  1010. usb_find_busses();
  1011. usb_find_devices();
  1012. busses = usb_get_busses();
  1013. /* find vsllink_jtag device in usb bus */
  1014. for (bus = busses; bus; bus = bus->next)
  1015. {
  1016. for (dev = bus->devices; dev; dev = dev->next)
  1017. {
  1018. if ((dev->descriptor.idVendor == vsllink_vid) && (dev->descriptor.idProduct == vsllink_pid))
  1019. {
  1020. result->usb_handle = usb_open(dev);
  1021. /* usb_set_configuration required under win32 */
  1022. usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
  1023. usb_claim_interface(result->usb_handle, 0);
  1024. #if 0
  1025. /*
  1026. * This makes problems under Mac OS X. And is not needed
  1027. * under Windows. Hopefully this will not break a linux build
  1028. */
  1029. usb_set_altinterface(result->usb_handle, 0);
  1030. #endif
  1031. return result;
  1032. }
  1033. }
  1034. }
  1035. free(result);
  1036. return NULL;
  1037. }
  1038. void vsllink_usb_close(vsllink_jtag_t *vsllink_jtag)
  1039. {
  1040. usb_close(vsllink_jtag->usb_handle);
  1041. free(vsllink_jtag);
  1042. }
  1043. /* Send a message and receive the reply. */
  1044. int vsllink_usb_message(vsllink_jtag_t *vsllink_jtag, int out_length, int in_length)
  1045. {
  1046. int result;
  1047. result = vsllink_usb_write(vsllink_jtag, out_length);
  1048. if (result == out_length)
  1049. {
  1050. if (in_length > 0)
  1051. {
  1052. result = vsllink_usb_read(vsllink_jtag);
  1053. if (result == in_length )
  1054. {
  1055. return result;
  1056. }
  1057. else
  1058. {
  1059. LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
  1060. return -1;
  1061. }
  1062. }
  1063. return 0;
  1064. }
  1065. else
  1066. {
  1067. LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
  1068. return -1;
  1069. }
  1070. }
  1071. /* Write data from out_buffer to USB. */
  1072. int vsllink_usb_write(vsllink_jtag_t *vsllink_jtag, int out_length)
  1073. {
  1074. int result;
  1075. if (out_length > VSLLINK_BufferSize)
  1076. {
  1077. LOG_ERROR("vsllink_jtag_write illegal out_length=%d (max=%d)", out_length, VSLLINK_BufferSize);
  1078. return -1;
  1079. }
  1080. result = usb_bulk_write(vsllink_jtag->usb_handle, vsllink_bulkout, \
  1081. (char *)vsllink_usb_out_buffer, out_length, VSLLINK_USB_TIMEOUT);
  1082. DEBUG_JTAG_IO("vsllink_usb_write, out_length = %d, result = %d", out_length, result);
  1083. #ifdef _DEBUG_USB_COMMS_
  1084. LOG_DEBUG("USB out:");
  1085. vsllink_debug_buffer(vsllink_usb_out_buffer, out_length);
  1086. #endif
  1087. #ifdef _VSLLINK_IN_DEBUG_MODE_
  1088. usleep(100000);
  1089. #endif
  1090. return result;
  1091. }
  1092. /* Read data from USB into in_buffer. */
  1093. int vsllink_usb_read(vsllink_jtag_t *vsllink_jtag)
  1094. {
  1095. int result = usb_bulk_read(vsllink_jtag->usb_handle, vsllink_bulkin, \
  1096. (char *)vsllink_usb_in_buffer, VSLLINK_BufferSize, VSLLINK_USB_TIMEOUT);
  1097. DEBUG_JTAG_IO("vsllink_usb_read, result = %d", result);
  1098. #ifdef _DEBUG_USB_COMMS_
  1099. LOG_DEBUG("USB in:");
  1100. vsllink_debug_buffer(vsllink_usb_in_buffer, result);
  1101. #endif
  1102. return result;
  1103. }
  1104. #define BYTES_PER_LINE 16
  1105. void vsllink_debug_buffer(u8 *buffer, int length)
  1106. {
  1107. char line[81];
  1108. char s[4];
  1109. int i;
  1110. int j;
  1111. for (i = 0; i < length; i += BYTES_PER_LINE)
  1112. {
  1113. snprintf(line, 5, "%04x", i);
  1114. for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
  1115. {
  1116. snprintf(s, 4, " %02x", buffer[j]);
  1117. strcat(line, s);
  1118. }
  1119. LOG_DEBUG(line);
  1120. }
  1121. }