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.
 
 
 
 
 
 

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