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.
 
 
 
 
 
 

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