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.
 
 
 
 
 
 

828 lines
23 KiB

  1. /***************************************************************************
  2. * *
  3. * Copyright (C) 2009 by Cahya Wirawan <cahya@gmx.at> *
  4. * Based on opendous driver by Vladimir Fonov *
  5. * *
  6. * Copyright (C) 2009 by Vladimir Fonov <vladimir.fonov@gmai.com> *
  7. * Based on J-link driver by Juergen Stuber *
  8. * *
  9. * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
  10. * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
  11. * *
  12. * Copyright (C) 2008 by Spencer Oliver *
  13. * spen@spen-soft.co.uk *
  14. * *
  15. * This program is free software; you can redistribute it and/or modify *
  16. * it under the terms of the GNU General Public License as published by *
  17. * the Free Software Foundation; either version 2 of the License, or *
  18. * (at your option) any later version. *
  19. * *
  20. * This program is distributed in the hope that it will be useful, *
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  23. * GNU General Public License for more details. *
  24. * *
  25. * You should have received a copy of the GNU General Public License *
  26. * along with this program; if not, write to the *
  27. * Free Software Foundation, Inc., *
  28. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  29. ***************************************************************************/
  30. #ifdef HAVE_CONFIG_H
  31. #include "config.h"
  32. #endif
  33. #include <jtag/interface.h>
  34. #include <jtag/commands.h>
  35. #include "libusb_common.h"
  36. #include <string.h>
  37. #include <time.h>
  38. #define OPENDOUS_MAX_VIDS_PIDS 4
  39. /* define some probes with similar interface */
  40. struct opendous_probe {
  41. char *name;
  42. uint16_t VID[OPENDOUS_MAX_VIDS_PIDS];
  43. uint16_t PID[OPENDOUS_MAX_VIDS_PIDS];
  44. uint8_t READ_EP;
  45. uint8_t WRITE_EP;
  46. uint8_t CONTROL_TRANSFER;
  47. int BUFFERSIZE;
  48. };
  49. static struct opendous_probe opendous_probes[] = {
  50. {"usbprog-jtag", {0x1781, 0}, {0x0C63, 0}, 0x82, 0x02, 0x00, 510 },
  51. {"opendous", {0x1781, 0x03EB, 0}, {0xC0C0, 0x204F, 0}, 0x81, 0x02, 0x00, 360 },
  52. {"usbvlab", {0x16C0, 0}, {0x05DC, 0}, 0x81, 0x02, 0x01, 360 },
  53. {NULL, {0x0000}, {0x0000}, 0x00, 0x00, 0x00, 0 }
  54. };
  55. #define OPENDOUS_WRITE_ENDPOINT (opendous_probe->WRITE_EP)
  56. #define OPENDOUS_READ_ENDPOINT (opendous_probe->READ_EP)
  57. static unsigned int opendous_hw_jtag_version = 1;
  58. #define OPENDOUS_USB_TIMEOUT 1000
  59. #define OPENDOUS_USB_BUFFER_SIZE (opendous_probe->BUFFERSIZE)
  60. #define OPENDOUS_IN_BUFFER_SIZE (OPENDOUS_USB_BUFFER_SIZE)
  61. #define OPENDOUS_OUT_BUFFER_SIZE (OPENDOUS_USB_BUFFER_SIZE)
  62. /* Global USB buffers */
  63. static uint8_t *usb_in_buffer;
  64. static uint8_t *usb_out_buffer;
  65. /* Constants for OPENDOUS command */
  66. #define OPENDOUS_MAX_SPEED 66
  67. #define OPENDOUS_MAX_TAP_TRANSMIT ((opendous_probe->BUFFERSIZE)-10)
  68. #define OPENDOUS_MAX_INPUT_DATA (OPENDOUS_MAX_TAP_TRANSMIT*4)
  69. /* TAP */
  70. #define OPENDOUS_TAP_BUFFER_SIZE 65536
  71. struct pending_scan_result {
  72. int first; /* First bit position in tdo_buffer to read */
  73. int length; /* Number of bits to read */
  74. struct scan_command *command; /* Corresponding scan command */
  75. uint8_t *buffer;
  76. };
  77. static int pending_scan_results_length;
  78. static struct pending_scan_result *pending_scan_results_buffer;
  79. #define MAX_PENDING_SCAN_RESULTS (OPENDOUS_MAX_INPUT_DATA)
  80. /* JTAG usb commands */
  81. #define JTAG_CMD_TAP_OUTPUT 0x0
  82. #define JTAG_CMD_SET_TRST 0x1
  83. #define JTAG_CMD_SET_SRST 0x2
  84. #define JTAG_CMD_READ_INPUT 0x3
  85. #define JTAG_CMD_TAP_OUTPUT_EMU 0x4
  86. #define JTAG_CMD_SET_DELAY 0x5
  87. #define JTAG_CMD_SET_SRST_TRST 0x6
  88. #define JTAG_CMD_READ_CONFIG 0x7
  89. /* usbvlab control transfer */
  90. #define FUNC_START_BOOTLOADER 30
  91. #define FUNC_WRITE_DATA 0x50
  92. #define FUNC_READ_DATA 0x51
  93. static char *opendous_type;
  94. static struct opendous_probe *opendous_probe;
  95. /* External interface functions */
  96. static int opendous_execute_queue(void);
  97. static int opendous_init(void);
  98. static int opendous_quit(void);
  99. /* Queue command functions */
  100. static void opendous_end_state(tap_state_t state);
  101. static void opendous_state_move(void);
  102. static void opendous_path_move(int num_states, tap_state_t *path);
  103. static void opendous_runtest(int num_cycles);
  104. static void opendous_scan(int ir_scan, enum scan_type type, uint8_t *buffer,
  105. int scan_size, struct scan_command *command);
  106. static void opendous_reset(int trst, int srst);
  107. static void opendous_simple_command(uint8_t command, uint8_t _data);
  108. static int opendous_get_status(void);
  109. /* opendous tap buffer functions */
  110. static void opendous_tap_init(void);
  111. static int opendous_tap_execute(void);
  112. static void opendous_tap_ensure_space(int scans, int bits);
  113. static void opendous_tap_append_step(int tms, int tdi);
  114. static void opendous_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command);
  115. /* opendous lowlevel functions */
  116. struct opendous_jtag {
  117. struct jtag_libusb_device_handle *usb_handle;
  118. };
  119. static struct opendous_jtag *opendous_usb_open(void);
  120. static void opendous_usb_close(struct opendous_jtag *opendous_jtag);
  121. static int opendous_usb_message(struct opendous_jtag *opendous_jtag, int out_length, int in_length);
  122. static int opendous_usb_write(struct opendous_jtag *opendous_jtag, int out_length);
  123. static int opendous_usb_read(struct opendous_jtag *opendous_jtag);
  124. /* helper functions */
  125. int opendous_get_version_info(void);
  126. #ifdef _DEBUG_USB_COMMS_
  127. static void opendous_debug_buffer(uint8_t *buffer, int length);
  128. #endif
  129. static struct opendous_jtag *opendous_jtag_handle;
  130. /***************************************************************************/
  131. /* External interface implementation */
  132. COMMAND_HANDLER(opendous_handle_opendous_type_command)
  133. {
  134. if (CMD_ARGC == 0)
  135. return ERROR_OK;
  136. /* only if the cable name wasn't overwritten by cmdline */
  137. if (opendous_type == NULL) {
  138. /* REVISIT first verify that it's listed in cables[] ... */
  139. opendous_type = strdup(CMD_ARGV[0]);
  140. }
  141. /* REVISIT it's probably worth returning the current value ... */
  142. return ERROR_OK;
  143. }
  144. COMMAND_HANDLER(opendous_handle_opendous_info_command)
  145. {
  146. if (opendous_get_version_info() == ERROR_OK) {
  147. /* attempt to get status */
  148. opendous_get_status();
  149. }
  150. return ERROR_OK;
  151. }
  152. COMMAND_HANDLER(opendous_handle_opendous_hw_jtag_command)
  153. {
  154. switch (CMD_ARGC) {
  155. case 0:
  156. command_print(CMD_CTX, "opendous hw jtag %i", opendous_hw_jtag_version);
  157. break;
  158. case 1: {
  159. int request_version = atoi(CMD_ARGV[0]);
  160. switch (request_version) {
  161. case 2:
  162. case 3:
  163. opendous_hw_jtag_version = request_version;
  164. break;
  165. default:
  166. return ERROR_COMMAND_SYNTAX_ERROR;
  167. }
  168. break;
  169. }
  170. default:
  171. return ERROR_COMMAND_SYNTAX_ERROR;
  172. }
  173. return ERROR_OK;
  174. }
  175. static const struct command_registration opendous_command_handlers[] = {
  176. {
  177. .name = "opendous_info",
  178. .handler = &opendous_handle_opendous_info_command,
  179. .mode = COMMAND_EXEC,
  180. .help = "show opendous info",
  181. },
  182. {
  183. .name = "opendous_hw_jtag",
  184. .handler = &opendous_handle_opendous_hw_jtag_command,
  185. .mode = COMMAND_EXEC,
  186. .help = "access opendous HW JTAG command version",
  187. .usage = "[2|3]",
  188. },
  189. {
  190. .name = "opendous_type",
  191. .handler = &opendous_handle_opendous_type_command,
  192. .mode = COMMAND_CONFIG,
  193. .help = "set opendous type",
  194. .usage = "[usbvlab|usbprog-jtag|opendous]",
  195. },
  196. COMMAND_REGISTRATION_DONE
  197. };
  198. struct jtag_interface opendous_interface = {
  199. .name = "opendous",
  200. .commands = opendous_command_handlers,
  201. .execute_queue = opendous_execute_queue,
  202. .init = opendous_init,
  203. .quit = opendous_quit,
  204. };
  205. static int opendous_execute_queue(void)
  206. {
  207. struct jtag_command *cmd = jtag_command_queue;
  208. int scan_size;
  209. enum scan_type type;
  210. uint8_t *buffer;
  211. while (cmd != NULL) {
  212. switch (cmd->type) {
  213. case JTAG_RUNTEST:
  214. DEBUG_JTAG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
  215. cmd->cmd.runtest->end_state);
  216. if (cmd->cmd.runtest->end_state != -1)
  217. opendous_end_state(cmd->cmd.runtest->end_state);
  218. opendous_runtest(cmd->cmd.runtest->num_cycles);
  219. break;
  220. case JTAG_TLR_RESET:
  221. DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
  222. if (cmd->cmd.statemove->end_state != -1)
  223. opendous_end_state(cmd->cmd.statemove->end_state);
  224. opendous_state_move();
  225. break;
  226. case JTAG_PATHMOVE:
  227. DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
  228. cmd->cmd.pathmove->num_states, \
  229. cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  230. opendous_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
  231. break;
  232. case JTAG_SCAN:
  233. DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
  234. if (cmd->cmd.scan->end_state != -1)
  235. opendous_end_state(cmd->cmd.scan->end_state);
  236. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  237. DEBUG_JTAG_IO("scan input, length = %d", scan_size);
  238. #ifdef _DEBUG_USB_COMMS_
  239. opendous_debug_buffer(buffer, (scan_size + 7) / 8);
  240. #endif
  241. type = jtag_scan_type(cmd->cmd.scan);
  242. opendous_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
  243. break;
  244. case JTAG_RESET:
  245. DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  246. opendous_tap_execute();
  247. if (cmd->cmd.reset->trst == 1)
  248. tap_set_state(TAP_RESET);
  249. opendous_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  250. break;
  251. case JTAG_SLEEP:
  252. DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
  253. opendous_tap_execute();
  254. jtag_sleep(cmd->cmd.sleep->us);
  255. break;
  256. default:
  257. LOG_ERROR("BUG: unknown JTAG command type encountered");
  258. exit(-1);
  259. }
  260. cmd = cmd->next;
  261. }
  262. return opendous_tap_execute();
  263. }
  264. static int opendous_init(void)
  265. {
  266. int check_cnt;
  267. struct opendous_probe *cur_opendous_probe;
  268. cur_opendous_probe = opendous_probes;
  269. if (opendous_type == NULL) {
  270. opendous_type = strdup("opendous");
  271. LOG_WARNING("No opendous_type specified, using default 'opendous'");
  272. }
  273. while (cur_opendous_probe->name) {
  274. if (strcmp(cur_opendous_probe->name, opendous_type) == 0) {
  275. opendous_probe = cur_opendous_probe;
  276. break;
  277. }
  278. cur_opendous_probe++;
  279. }
  280. if (!opendous_probe) {
  281. LOG_ERROR("No matching cable found for %s", opendous_type);
  282. return ERROR_JTAG_INIT_FAILED;
  283. }
  284. usb_in_buffer = malloc(opendous_probe->BUFFERSIZE);
  285. usb_out_buffer = malloc(opendous_probe->BUFFERSIZE);
  286. pending_scan_results_buffer = malloc(
  287. MAX_PENDING_SCAN_RESULTS * sizeof(*pending_scan_results_buffer));
  288. opendous_jtag_handle = opendous_usb_open();
  289. if (opendous_jtag_handle == 0) {
  290. LOG_ERROR("Cannot find opendous Interface! Please check connection and permissions.");
  291. return ERROR_JTAG_INIT_FAILED;
  292. }
  293. check_cnt = 0;
  294. while (check_cnt < 3) {
  295. if (opendous_get_version_info() == ERROR_OK) {
  296. /* attempt to get status */
  297. opendous_get_status();
  298. break;
  299. }
  300. check_cnt++;
  301. }
  302. LOG_INFO("opendous JTAG Interface ready");
  303. opendous_reset(0, 0);
  304. opendous_tap_init();
  305. return ERROR_OK;
  306. }
  307. static int opendous_quit(void)
  308. {
  309. opendous_usb_close(opendous_jtag_handle);
  310. if (usb_out_buffer) {
  311. free(usb_out_buffer);
  312. usb_out_buffer = NULL;
  313. }
  314. if (usb_in_buffer) {
  315. free(usb_in_buffer);
  316. usb_in_buffer = NULL;
  317. }
  318. if (pending_scan_results_buffer) {
  319. free(pending_scan_results_buffer);
  320. pending_scan_results_buffer = NULL;
  321. }
  322. if (opendous_type) {
  323. free(opendous_type);
  324. opendous_type = NULL;
  325. }
  326. return ERROR_OK;
  327. }
  328. /***************************************************************************/
  329. /* Queue command implementations */
  330. void opendous_end_state(tap_state_t state)
  331. {
  332. if (tap_is_state_stable(state))
  333. tap_set_end_state(state);
  334. else {
  335. LOG_ERROR("BUG: %i is not a valid end state", state);
  336. exit(-1);
  337. }
  338. }
  339. /* Goes to the end state. */
  340. void opendous_state_move(void)
  341. {
  342. int i;
  343. int tms = 0;
  344. uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  345. uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  346. for (i = 0; i < tms_scan_bits; i++) {
  347. tms = (tms_scan >> i) & 1;
  348. opendous_tap_append_step(tms, 0);
  349. }
  350. tap_set_state(tap_get_end_state());
  351. }
  352. void opendous_path_move(int num_states, tap_state_t *path)
  353. {
  354. int i;
  355. for (i = 0; i < num_states; i++) {
  356. if (path[i] == tap_state_transition(tap_get_state(), false))
  357. opendous_tap_append_step(0, 0);
  358. else if (path[i] == tap_state_transition(tap_get_state(), true))
  359. opendous_tap_append_step(1, 0);
  360. else {
  361. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
  362. tap_state_name(tap_get_state()), tap_state_name(path[i]));
  363. exit(-1);
  364. }
  365. tap_set_state(path[i]);
  366. }
  367. tap_set_end_state(tap_get_state());
  368. }
  369. void opendous_runtest(int num_cycles)
  370. {
  371. int i;
  372. tap_state_t saved_end_state = tap_get_end_state();
  373. /* only do a state_move when we're not already in IDLE */
  374. if (tap_get_state() != TAP_IDLE) {
  375. opendous_end_state(TAP_IDLE);
  376. opendous_state_move();
  377. }
  378. /* execute num_cycles */
  379. for (i = 0; i < num_cycles; i++)
  380. opendous_tap_append_step(0, 0);
  381. /* finish in end_state */
  382. opendous_end_state(saved_end_state);
  383. if (tap_get_state() != tap_get_end_state())
  384. opendous_state_move();
  385. }
  386. void opendous_scan(int ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command)
  387. {
  388. tap_state_t saved_end_state;
  389. opendous_tap_ensure_space(1, scan_size + 8);
  390. saved_end_state = tap_get_end_state();
  391. /* Move to appropriate scan state */
  392. opendous_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  393. if (tap_get_state() != tap_get_end_state())
  394. opendous_state_move();
  395. opendous_end_state(saved_end_state);
  396. /* Scan */
  397. opendous_tap_append_scan(scan_size, buffer, command);
  398. /* We are in Exit1, go to Pause */
  399. opendous_tap_append_step(0, 0);
  400. tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
  401. if (tap_get_state() != tap_get_end_state())
  402. opendous_state_move();
  403. }
  404. void opendous_reset(int trst, int srst)
  405. {
  406. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  407. /* Signals are active low */
  408. #if 0
  409. if (srst == 0)
  410. opendous_simple_command(JTAG_CMD_SET_SRST, 1);
  411. else if (srst == 1)
  412. opendous_simple_command(JTAG_CMD_SET_SRST, 0);
  413. if (trst == 0)
  414. opendous_simple_command(JTAG_CMD_SET_TRST, 1);
  415. else if (trst == 1)
  416. opendous_simple_command(JTAG_CMD_SET_TRST, 0);
  417. #endif
  418. srst = srst ? 0 : 1;
  419. trst = trst ? 0 : 2;
  420. opendous_simple_command(JTAG_CMD_SET_SRST_TRST, srst | trst);
  421. }
  422. void opendous_simple_command(uint8_t command, uint8_t _data)
  423. {
  424. int result;
  425. DEBUG_JTAG_IO("0x%02x 0x%02x", command, _data);
  426. usb_out_buffer[0] = 2;
  427. usb_out_buffer[1] = 0;
  428. usb_out_buffer[2] = command;
  429. usb_out_buffer[3] = _data;
  430. result = opendous_usb_message(opendous_jtag_handle, 4, 1);
  431. if (result != 1)
  432. LOG_ERROR("opendous command 0x%02x failed (%d)", command, result);
  433. }
  434. int opendous_get_status(void)
  435. {
  436. return ERROR_OK;
  437. }
  438. int opendous_get_version_info(void)
  439. {
  440. return ERROR_OK;
  441. }
  442. /***************************************************************************/
  443. /* Estick tap functions */
  444. static int tap_length;
  445. static uint8_t tms_buffer[OPENDOUS_TAP_BUFFER_SIZE];
  446. static uint8_t tdo_buffer[OPENDOUS_TAP_BUFFER_SIZE];
  447. static int last_tms;
  448. void opendous_tap_init(void)
  449. {
  450. tap_length = 0;
  451. pending_scan_results_length = 0;
  452. }
  453. void opendous_tap_ensure_space(int scans, int bits)
  454. {
  455. int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
  456. int available_bits = OPENDOUS_TAP_BUFFER_SIZE / 2 - tap_length;
  457. if ((scans > available_scans) || (bits > available_bits))
  458. opendous_tap_execute();
  459. }
  460. void opendous_tap_append_step(int tms, int tdi)
  461. {
  462. last_tms = tms;
  463. unsigned char _tms = tms ? 1 : 0;
  464. unsigned char _tdi = tdi ? 1 : 0;
  465. opendous_tap_ensure_space(0, 1);
  466. int tap_index = tap_length / 4;
  467. int bits = (tap_length % 4) * 2;
  468. if (tap_length < OPENDOUS_TAP_BUFFER_SIZE) {
  469. if (!bits)
  470. tms_buffer[tap_index] = 0;
  471. tms_buffer[tap_index] |= (_tdi << bits)|(_tms << (bits + 1)) ;
  472. tap_length++;
  473. } else
  474. LOG_ERROR("opendous_tap_append_step, overflow");
  475. }
  476. void opendous_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command)
  477. {
  478. DEBUG_JTAG_IO("append scan, length = %d", length);
  479. struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
  480. int i;
  481. pending_scan_result->first = tap_length;
  482. pending_scan_result->length = length;
  483. pending_scan_result->command = command;
  484. pending_scan_result->buffer = buffer;
  485. for (i = 0; i < length; i++)
  486. opendous_tap_append_step((i < length-1 ? 0 : 1), (buffer[i / 8] >> (i % 8)) & 1);
  487. pending_scan_results_length++;
  488. }
  489. /* Pad and send a tap sequence to the device, and receive the answer.
  490. * For the purpose of padding we assume that we are in idle or pause state. */
  491. int opendous_tap_execute(void)
  492. {
  493. int byte_length;
  494. int i, j;
  495. int result;
  496. #ifdef _DEBUG_USB_COMMS_
  497. int byte_length_out;
  498. #endif
  499. if (tap_length > 0) {
  500. /* memset(tdo_buffer,0,OPENDOUS_TAP_BUFFER_SIZE); */
  501. /* LOG_INFO("OPENDOUS tap execute %d",tap_length); */
  502. byte_length = (tap_length + 3) / 4;
  503. #ifdef _DEBUG_USB_COMMS_
  504. byte_length_out = (tap_length + 7) / 8;
  505. LOG_DEBUG("opendous is sending %d bytes", byte_length);
  506. #endif
  507. for (j = 0, i = 0; j < byte_length;) {
  508. int receive;
  509. int transmit = byte_length - j;
  510. if (transmit > OPENDOUS_MAX_TAP_TRANSMIT) {
  511. transmit = OPENDOUS_MAX_TAP_TRANSMIT;
  512. receive = (OPENDOUS_MAX_TAP_TRANSMIT) / 2;
  513. usb_out_buffer[2] = JTAG_CMD_TAP_OUTPUT;
  514. } else {
  515. usb_out_buffer[2] = JTAG_CMD_TAP_OUTPUT | ((tap_length % 4) << 4);
  516. receive = (transmit + 1) / 2;
  517. }
  518. usb_out_buffer[0] = (transmit + 1) & 0xff;
  519. usb_out_buffer[1] = ((transmit + 1) >> 8) & 0xff;
  520. memmove(usb_out_buffer + 3, tms_buffer + j, transmit);
  521. result = opendous_usb_message(opendous_jtag_handle, 3 + transmit, receive);
  522. if (result != receive) {
  523. LOG_ERROR("opendous_tap_execute, wrong result %d, expected %d", result, receive);
  524. return ERROR_JTAG_QUEUE_FAILED;
  525. }
  526. memmove(tdo_buffer + i, usb_in_buffer, receive);
  527. i += receive;
  528. j += transmit;
  529. }
  530. #ifdef _DEBUG_USB_COMMS_
  531. LOG_DEBUG("opendous tap result %d", byte_length_out);
  532. opendous_debug_buffer(tdo_buffer, byte_length_out);
  533. #endif
  534. /* LOG_INFO("eStick tap execute %d",tap_length); */
  535. for (i = 0; i < pending_scan_results_length; i++) {
  536. struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i];
  537. uint8_t *buffer = pending_scan_result->buffer;
  538. int length = pending_scan_result->length;
  539. int first = pending_scan_result->first;
  540. struct scan_command *command = pending_scan_result->command;
  541. /* Copy to buffer */
  542. buf_set_buf(tdo_buffer, first, buffer, 0, length);
  543. DEBUG_JTAG_IO("pending scan result, length = %d", length);
  544. #ifdef _DEBUG_USB_COMMS_
  545. opendous_debug_buffer(buffer, byte_length_out);
  546. #endif
  547. if (jtag_read_buffer(buffer, command) != ERROR_OK) {
  548. opendous_tap_init();
  549. return ERROR_JTAG_QUEUE_FAILED;
  550. }
  551. if (pending_scan_result->buffer != NULL)
  552. free(pending_scan_result->buffer);
  553. }
  554. opendous_tap_init();
  555. }
  556. return ERROR_OK;
  557. }
  558. /*****************************************************************************/
  559. /* Estick USB low-level functions */
  560. struct opendous_jtag *opendous_usb_open(void)
  561. {
  562. struct opendous_jtag *result;
  563. struct jtag_libusb_device_handle *devh;
  564. if (jtag_libusb_open(opendous_probe->VID, opendous_probe->PID, NULL, &devh) != ERROR_OK)
  565. return NULL;
  566. jtag_libusb_set_configuration(devh, 0);
  567. jtag_libusb_claim_interface(devh, 0);
  568. result = malloc(sizeof(*result));
  569. result->usb_handle = devh;
  570. return result;
  571. }
  572. void opendous_usb_close(struct opendous_jtag *opendous_jtag)
  573. {
  574. jtag_libusb_close(opendous_jtag->usb_handle);
  575. free(opendous_jtag);
  576. }
  577. /* Send a message and receive the reply. */
  578. int opendous_usb_message(struct opendous_jtag *opendous_jtag, int out_length, int in_length)
  579. {
  580. int result;
  581. result = opendous_usb_write(opendous_jtag, out_length);
  582. if (result == out_length) {
  583. result = opendous_usb_read(opendous_jtag);
  584. if (result == in_length)
  585. return result;
  586. else {
  587. LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
  588. return -1;
  589. }
  590. } else {
  591. LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
  592. return -1;
  593. }
  594. }
  595. /* Write data from out_buffer to USB. */
  596. int opendous_usb_write(struct opendous_jtag *opendous_jtag, int out_length)
  597. {
  598. int result;
  599. if (out_length > OPENDOUS_OUT_BUFFER_SIZE) {
  600. LOG_ERROR("opendous_jtag_write illegal out_length=%d (max=%d)", out_length, OPENDOUS_OUT_BUFFER_SIZE);
  601. return -1;
  602. }
  603. #ifdef _DEBUG_USB_COMMS_
  604. LOG_DEBUG("USB write begin");
  605. #endif
  606. if (opendous_probe->CONTROL_TRANSFER) {
  607. result = jtag_libusb_control_transfer(opendous_jtag->usb_handle,
  608. LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
  609. FUNC_WRITE_DATA, 0, 0, (char *) usb_out_buffer, out_length, OPENDOUS_USB_TIMEOUT);
  610. } else {
  611. result = jtag_libusb_bulk_write(opendous_jtag->usb_handle, OPENDOUS_WRITE_ENDPOINT, \
  612. (char *)usb_out_buffer, out_length, OPENDOUS_USB_TIMEOUT);
  613. }
  614. #ifdef _DEBUG_USB_COMMS_
  615. LOG_DEBUG("USB write end: %d bytes", result);
  616. #endif
  617. DEBUG_JTAG_IO("opendous_usb_write, out_length = %d, result = %d", out_length, result);
  618. #ifdef _DEBUG_USB_COMMS_
  619. opendous_debug_buffer(usb_out_buffer, out_length);
  620. #endif
  621. return result;
  622. }
  623. /* Read data from USB into in_buffer. */
  624. int opendous_usb_read(struct opendous_jtag *opendous_jtag)
  625. {
  626. #ifdef _DEBUG_USB_COMMS_
  627. LOG_DEBUG("USB read begin");
  628. #endif
  629. int result;
  630. if (opendous_probe->CONTROL_TRANSFER) {
  631. result = jtag_libusb_control_transfer(opendous_jtag->usb_handle,
  632. LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN,
  633. FUNC_READ_DATA, 0, 0, (char *) usb_in_buffer, OPENDOUS_IN_BUFFER_SIZE, OPENDOUS_USB_TIMEOUT);
  634. } else {
  635. result = jtag_libusb_bulk_read(opendous_jtag->usb_handle, OPENDOUS_READ_ENDPOINT,
  636. (char *)usb_in_buffer, OPENDOUS_IN_BUFFER_SIZE, OPENDOUS_USB_TIMEOUT);
  637. }
  638. #ifdef _DEBUG_USB_COMMS_
  639. LOG_DEBUG("USB read end: %d bytes", result);
  640. #endif
  641. DEBUG_JTAG_IO("opendous_usb_read, result = %d", result);
  642. #ifdef _DEBUG_USB_COMMS_
  643. opendous_debug_buffer(usb_in_buffer, result);
  644. #endif
  645. return result;
  646. }
  647. #ifdef _DEBUG_USB_COMMS_
  648. #define BYTES_PER_LINE 16
  649. void opendous_debug_buffer(uint8_t *buffer, int length)
  650. {
  651. char line[81];
  652. char s[4];
  653. int i;
  654. int j;
  655. for (i = 0; i < length; i += BYTES_PER_LINE) {
  656. snprintf(line, 5, "%04x", i);
  657. for (j = i; j < i + BYTES_PER_LINE && j < length; j++) {
  658. snprintf(s, 4, " %02x", buffer[j]);
  659. strcat(line, s);
  660. }
  661. LOG_DEBUG("%s", line);
  662. }
  663. }
  664. #endif