1343b7f5c00825056d9abf183d1cb25e4a351e8a
[openocd.git] / src / jtag / drivers / opendous.c
blob1343b7f5c00825056d9abf183d1cb25e4a351e8a
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 ***************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
35 #include <jtag/interface.h>
36 #include <jtag/commands.h>
37 #include "libusb_common.h"
38 #include <string.h>
39 #include <sys/timeb.h>
40 #include <time.h>
42 #define OPENDOUS_MAX_VIDS_PIDS 4
43 /* define some probes with similar interface */
44 struct opendous_probe {
45 char *name;
46 uint16_t VID[OPENDOUS_MAX_VIDS_PIDS];
47 uint16_t PID[OPENDOUS_MAX_VIDS_PIDS];
48 uint8_t READ_EP;
49 uint8_t WRITE_EP;
50 uint8_t CONTROL_TRANSFER;
51 int BUFFERSIZE;
54 static struct opendous_probe opendous_probes[] = {
55 {"usbprog-jtag", {0x1781, 0}, {0x0C63, 0}, 0x82, 0x02, 0x00, 510 },
56 {"opendous", {0x1781, 0x03EB, 0}, {0xC0C0, 0x204F, 0}, 0x81, 0x02, 0x00, 360 },
57 {"usbvlab", {0x16C0, 0}, {0x05DC, 0}, 0x81, 0x02, 0x01, 360 },
58 {NULL, {0x0000}, {0x0000}, 0x00, 0x00, 0x00, 0 }
61 #define OPENDOUS_WRITE_ENDPOINT (opendous_probe->WRITE_EP)
62 #define OPENDOUS_READ_ENDPOINT (opendous_probe->READ_EP)
64 static unsigned int opendous_hw_jtag_version = 1;
66 #define OPENDOUS_USB_TIMEOUT 1000
68 #define OPENDOUS_USB_BUFFER_SIZE (opendous_probe->BUFFERSIZE)
69 #define OPENDOUS_IN_BUFFER_SIZE (OPENDOUS_USB_BUFFER_SIZE)
70 #define OPENDOUS_OUT_BUFFER_SIZE (OPENDOUS_USB_BUFFER_SIZE)
72 /* Global USB buffers */
73 static uint8_t *usb_in_buffer;
74 static uint8_t *usb_out_buffer;
76 /* Constants for OPENDOUS command */
78 #define OPENDOUS_MAX_SPEED 66
79 #define OPENDOUS_MAX_TAP_TRANSMIT ((opendous_probe->BUFFERSIZE)-10)
80 #define OPENDOUS_MAX_INPUT_DATA (OPENDOUS_MAX_TAP_TRANSMIT*4)
82 /* TAP */
83 #define OPENDOUS_TAP_BUFFER_SIZE 65536
85 struct pending_scan_result {
86 int first; /* First bit position in tdo_buffer to read */
87 int length; /* Number of bits to read */
88 struct scan_command *command; /* Corresponding scan command */
89 uint8_t *buffer;
92 static int pending_scan_results_length;
93 static struct pending_scan_result *pending_scan_results_buffer;
95 #define MAX_PENDING_SCAN_RESULTS (OPENDOUS_MAX_INPUT_DATA)
97 /* JTAG usb commands */
98 #define JTAG_CMD_TAP_OUTPUT 0x0
99 #define JTAG_CMD_SET_TRST 0x1
100 #define JTAG_CMD_SET_SRST 0x2
101 #define JTAG_CMD_READ_INPUT 0x3
102 #define JTAG_CMD_TAP_OUTPUT_EMU 0x4
103 #define JTAG_CMD_SET_DELAY 0x5
104 #define JTAG_CMD_SET_SRST_TRST 0x6
105 #define JTAG_CMD_READ_CONFIG 0x7
107 /* usbvlab control transfer */
108 #define FUNC_START_BOOTLOADER 30
109 #define FUNC_WRITE_DATA 0x50
110 #define FUNC_READ_DATA 0x51
112 static char *opendous_type;
113 static struct opendous_probe *opendous_probe;
115 /* External interface functions */
116 static int opendous_execute_queue(void);
117 static int opendous_init(void);
118 static int opendous_quit(void);
120 /* Queue command functions */
121 static void opendous_end_state(tap_state_t state);
122 static void opendous_state_move(void);
123 static void opendous_path_move(int num_states, tap_state_t *path);
124 static void opendous_runtest(int num_cycles);
125 static void opendous_scan(int ir_scan, enum scan_type type, uint8_t *buffer,
126 int scan_size, struct scan_command *command);
127 static void opendous_reset(int trst, int srst);
128 static void opendous_simple_command(uint8_t command, uint8_t _data);
129 static int opendous_get_status(void);
131 /* opendous tap buffer functions */
132 static void opendous_tap_init(void);
133 static int opendous_tap_execute(void);
134 static void opendous_tap_ensure_space(int scans, int bits);
135 static void opendous_tap_append_step(int tms, int tdi);
136 static void opendous_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command);
138 /* opendous lowlevel functions */
139 struct opendous_jtag {
140 struct jtag_libusb_device_handle *usb_handle;
143 static struct opendous_jtag *opendous_usb_open(void);
144 static void opendous_usb_close(struct opendous_jtag *opendous_jtag);
145 static int opendous_usb_message(struct opendous_jtag *opendous_jtag, int out_length, int in_length);
146 static int opendous_usb_write(struct opendous_jtag *opendous_jtag, int out_length);
147 static int opendous_usb_read(struct opendous_jtag *opendous_jtag);
149 /* helper functions */
150 int opendous_get_version_info(void);
152 #ifdef _DEBUG_USB_COMMS_
153 char time_str[50];
154 static void opendous_debug_buffer(uint8_t *buffer, int length);
155 char *opendous_get_time(char *);
156 #endif
158 static struct opendous_jtag *opendous_jtag_handle;
160 /***************************************************************************/
161 /* External interface implementation */
163 COMMAND_HANDLER(opendous_handle_opendous_type_command)
165 if (CMD_ARGC == 0)
166 return ERROR_OK;
168 /* only if the cable name wasn't overwritten by cmdline */
169 if (opendous_type == NULL) {
170 /* REVISIT first verify that it's listed in cables[] ... */
171 opendous_type = strdup(CMD_ARGV[0]);
174 /* REVISIT it's probably worth returning the current value ... */
176 return ERROR_OK;
179 COMMAND_HANDLER(opendous_handle_opendous_info_command)
181 if (opendous_get_version_info() == ERROR_OK) {
182 /* attempt to get status */
183 opendous_get_status();
186 return ERROR_OK;
189 COMMAND_HANDLER(opendous_handle_opendous_hw_jtag_command)
191 switch (CMD_ARGC) {
192 case 0:
193 command_print(CMD_CTX, "opendous hw jtag %i", opendous_hw_jtag_version);
194 break;
196 case 1: {
197 int request_version = atoi(CMD_ARGV[0]);
198 switch (request_version) {
199 case 2:
200 case 3:
201 opendous_hw_jtag_version = request_version;
202 break;
204 default:
205 return ERROR_COMMAND_SYNTAX_ERROR;
207 break;
210 default:
211 return ERROR_COMMAND_SYNTAX_ERROR;
214 return ERROR_OK;
217 static const struct command_registration opendous_command_handlers[] = {
219 .name = "opendous_info",
220 .handler = &opendous_handle_opendous_info_command,
221 .mode = COMMAND_EXEC,
222 .help = "show opendous info",
225 .name = "opendous_hw_jtag",
226 .handler = &opendous_handle_opendous_hw_jtag_command,
227 .mode = COMMAND_EXEC,
228 .help = "access opendous HW JTAG command version",
229 .usage = "[2|3]",
232 .name = "opendous_type",
233 .handler = &opendous_handle_opendous_type_command,
234 .mode = COMMAND_CONFIG,
235 .help = "set opendous type",
236 .usage = "[usbvlab|usbprog-jtag|opendous]",
238 COMMAND_REGISTRATION_DONE
241 struct jtag_interface opendous_interface = {
242 .name = "opendous",
243 .commands = opendous_command_handlers,
244 .execute_queue = opendous_execute_queue,
245 .init = opendous_init,
246 .quit = opendous_quit,
249 static int opendous_execute_queue(void)
251 struct jtag_command *cmd = jtag_command_queue;
252 int scan_size;
253 enum scan_type type;
254 uint8_t *buffer;
256 while (cmd != NULL) {
257 switch (cmd->type) {
258 case JTAG_RUNTEST:
259 DEBUG_JTAG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
260 cmd->cmd.runtest->end_state);
262 if (cmd->cmd.runtest->end_state != -1)
263 opendous_end_state(cmd->cmd.runtest->end_state);
264 opendous_runtest(cmd->cmd.runtest->num_cycles);
265 break;
267 case JTAG_TLR_RESET:
268 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
270 if (cmd->cmd.statemove->end_state != -1)
271 opendous_end_state(cmd->cmd.statemove->end_state);
272 opendous_state_move();
273 break;
275 case JTAG_PATHMOVE:
276 DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
277 cmd->cmd.pathmove->num_states, \
278 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
280 opendous_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
281 break;
283 case JTAG_SCAN:
284 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
286 if (cmd->cmd.scan->end_state != -1)
287 opendous_end_state(cmd->cmd.scan->end_state);
289 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
290 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
292 #ifdef _DEBUG_USB_COMMS_
293 opendous_debug_buffer(buffer, (scan_size + 7) / 8);
294 #endif
295 type = jtag_scan_type(cmd->cmd.scan);
296 opendous_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
297 break;
299 case JTAG_RESET:
300 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
302 opendous_tap_execute();
304 if (cmd->cmd.reset->trst == 1)
305 tap_set_state(TAP_RESET);
306 opendous_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
307 break;
309 case JTAG_SLEEP:
310 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
311 opendous_tap_execute();
312 jtag_sleep(cmd->cmd.sleep->us);
313 break;
315 default:
316 LOG_ERROR("BUG: unknown JTAG command type encountered");
317 exit(-1);
319 cmd = cmd->next;
321 return opendous_tap_execute();
324 static int opendous_init(void)
326 int check_cnt;
327 struct opendous_probe *cur_opendous_probe;
329 cur_opendous_probe = opendous_probes;
331 if (opendous_type == NULL) {
332 opendous_type = strdup("opendous");
333 LOG_WARNING("No opendous_type specified, using default 'opendous'");
336 while (cur_opendous_probe->name) {
337 if (strcmp(cur_opendous_probe->name, opendous_type) == 0) {
338 opendous_probe = cur_opendous_probe;
339 break;
341 cur_opendous_probe++;
344 if (!opendous_probe) {
345 LOG_ERROR("No matching cable found for %s", opendous_type);
346 return ERROR_JTAG_INIT_FAILED;
350 usb_in_buffer = malloc(opendous_probe->BUFFERSIZE);
351 usb_out_buffer = malloc(opendous_probe->BUFFERSIZE);
353 pending_scan_results_buffer = (struct pending_scan_result *)
354 malloc(MAX_PENDING_SCAN_RESULTS * sizeof(struct pending_scan_result));
356 opendous_jtag_handle = opendous_usb_open();
358 if (opendous_jtag_handle == 0) {
359 LOG_ERROR("Cannot find opendous Interface! Please check connection and permissions.");
360 return ERROR_JTAG_INIT_FAILED;
363 check_cnt = 0;
364 while (check_cnt < 3) {
365 if (opendous_get_version_info() == ERROR_OK) {
366 /* attempt to get status */
367 opendous_get_status();
368 break;
371 check_cnt++;
374 LOG_INFO("opendous JTAG Interface ready");
376 opendous_reset(0, 0);
377 opendous_tap_init();
379 return ERROR_OK;
382 static int opendous_quit(void)
384 opendous_usb_close(opendous_jtag_handle);
386 if (usb_out_buffer) {
387 free(usb_out_buffer);
388 usb_out_buffer = NULL;
391 if (usb_in_buffer) {
392 free(usb_in_buffer);
393 usb_in_buffer = NULL;
396 if (pending_scan_results_buffer) {
397 free(pending_scan_results_buffer);
398 pending_scan_results_buffer = NULL;
401 if (opendous_type) {
402 free(opendous_type);
403 opendous_type = NULL;
406 return ERROR_OK;
409 /***************************************************************************/
410 /* Queue command implementations */
412 void opendous_end_state(tap_state_t state)
414 if (tap_is_state_stable(state))
415 tap_set_end_state(state);
416 else {
417 LOG_ERROR("BUG: %i is not a valid end state", state);
418 exit(-1);
422 /* Goes to the end state. */
423 void opendous_state_move(void)
425 int i;
426 int tms = 0;
427 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
428 uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
430 for (i = 0; i < tms_scan_bits; i++) {
431 tms = (tms_scan >> i) & 1;
432 opendous_tap_append_step(tms, 0);
435 tap_set_state(tap_get_end_state());
438 void opendous_path_move(int num_states, tap_state_t *path)
440 int i;
442 for (i = 0; i < num_states; i++) {
443 if (path[i] == tap_state_transition(tap_get_state(), false))
444 opendous_tap_append_step(0, 0);
445 else if (path[i] == tap_state_transition(tap_get_state(), true))
446 opendous_tap_append_step(1, 0);
447 else {
448 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
449 tap_state_name(tap_get_state()), tap_state_name(path[i]));
450 exit(-1);
453 tap_set_state(path[i]);
456 tap_set_end_state(tap_get_state());
459 void opendous_runtest(int num_cycles)
461 int i;
463 tap_state_t saved_end_state = tap_get_end_state();
465 /* only do a state_move when we're not already in IDLE */
466 if (tap_get_state() != TAP_IDLE) {
467 opendous_end_state(TAP_IDLE);
468 opendous_state_move();
471 /* execute num_cycles */
472 for (i = 0; i < num_cycles; i++)
473 opendous_tap_append_step(0, 0);
475 /* finish in end_state */
476 opendous_end_state(saved_end_state);
477 if (tap_get_state() != tap_get_end_state())
478 opendous_state_move();
481 void opendous_scan(int ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command)
483 tap_state_t saved_end_state;
485 opendous_tap_ensure_space(1, scan_size + 8);
487 saved_end_state = tap_get_end_state();
489 /* Move to appropriate scan state */
490 opendous_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
492 if (tap_get_state() != tap_get_end_state())
493 opendous_state_move();
495 opendous_end_state(saved_end_state);
497 /* Scan */
498 opendous_tap_append_scan(scan_size, buffer, command);
500 /* We are in Exit1, go to Pause */
501 opendous_tap_append_step(0, 0);
503 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
505 if (tap_get_state() != tap_get_end_state())
506 opendous_state_move();
509 void opendous_reset(int trst, int srst)
511 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
513 /* Signals are active low */
514 #if 0
515 if (srst == 0)
516 opendous_simple_command(JTAG_CMD_SET_SRST, 1);
517 else if (srst == 1)
518 opendous_simple_command(JTAG_CMD_SET_SRST, 0);
520 if (trst == 0)
521 opendous_simple_command(JTAG_CMD_SET_TRST, 1);
522 else if (trst == 1)
523 opendous_simple_command(JTAG_CMD_SET_TRST, 0);
524 #endif
526 srst = srst ? 0 : 1;
527 trst = trst ? 0 : 2;
528 opendous_simple_command(JTAG_CMD_SET_SRST_TRST, srst | trst);
531 void opendous_simple_command(uint8_t command, uint8_t _data)
533 int result;
535 DEBUG_JTAG_IO("0x%02x 0x%02x", command, _data);
537 usb_out_buffer[0] = 2;
538 usb_out_buffer[1] = 0;
539 usb_out_buffer[2] = command;
540 usb_out_buffer[3] = _data;
542 result = opendous_usb_message(opendous_jtag_handle, 4, 1);
543 if (result != 1)
544 LOG_ERROR("opendous command 0x%02x failed (%d)", command, result);
547 int opendous_get_status(void)
549 return ERROR_OK;
552 int opendous_get_version_info(void)
554 return ERROR_OK;
557 /***************************************************************************/
558 /* Estick tap functions */
560 static int tap_length;
561 static uint8_t tms_buffer[OPENDOUS_TAP_BUFFER_SIZE];
562 static uint8_t tdo_buffer[OPENDOUS_TAP_BUFFER_SIZE];
564 static int last_tms;
566 void opendous_tap_init(void)
568 tap_length = 0;
569 pending_scan_results_length = 0;
572 void opendous_tap_ensure_space(int scans, int bits)
574 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
575 int available_bits = OPENDOUS_TAP_BUFFER_SIZE / 2 - tap_length;
577 if ((scans > available_scans) || (bits > available_bits))
578 opendous_tap_execute();
581 void opendous_tap_append_step(int tms, int tdi)
583 last_tms = tms;
584 unsigned char _tms = tms ? 1 : 0;
585 unsigned char _tdi = tdi ? 1 : 0;
587 opendous_tap_ensure_space(0, 1);
589 int tap_index = tap_length / 4;
590 int bits = (tap_length % 4) * 2;
592 if (tap_length < OPENDOUS_TAP_BUFFER_SIZE) {
593 if (!bits)
594 tms_buffer[tap_index] = 0;
596 tms_buffer[tap_index] |= (_tdi << bits)|(_tms << (bits + 1)) ;
597 tap_length++;
598 } else
599 LOG_ERROR("opendous_tap_append_step, overflow");
602 void opendous_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command)
604 DEBUG_JTAG_IO("append scan, length = %d", length);
606 struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
607 int i;
609 pending_scan_result->first = tap_length;
610 pending_scan_result->length = length;
611 pending_scan_result->command = command;
612 pending_scan_result->buffer = buffer;
614 for (i = 0; i < length; i++)
615 opendous_tap_append_step((i < length-1 ? 0 : 1), (buffer[i / 8] >> (i % 8)) & 1);
616 pending_scan_results_length++;
619 /* Pad and send a tap sequence to the device, and receive the answer.
620 * For the purpose of padding we assume that we are in idle or pause state. */
621 int opendous_tap_execute(void)
623 int byte_length;
624 int i, j;
625 int result;
627 #ifdef _DEBUG_USB_COMMS_
628 int byte_length_out;
629 #endif
631 if (tap_length > 0) {
633 /* memset(tdo_buffer,0,OPENDOUS_TAP_BUFFER_SIZE); */
634 /* LOG_INFO("OPENDOUS tap execute %d",tap_length); */
635 byte_length = (tap_length + 3) / 4;
637 #ifdef _DEBUG_USB_COMMS_
638 byte_length_out = (tap_length + 7) / 8;
639 LOG_DEBUG("opendous is sending %d bytes", byte_length);
640 #endif
642 for (j = 0, i = 0; j < byte_length;) {
644 int receive;
645 int transmit = byte_length - j;
646 if (transmit > OPENDOUS_MAX_TAP_TRANSMIT) {
647 transmit = OPENDOUS_MAX_TAP_TRANSMIT;
648 receive = (OPENDOUS_MAX_TAP_TRANSMIT) / 2;
649 usb_out_buffer[2] = JTAG_CMD_TAP_OUTPUT;
650 } else {
651 usb_out_buffer[2] = JTAG_CMD_TAP_OUTPUT | ((tap_length % 4) << 4);
652 receive = (transmit + 1) / 2;
654 usb_out_buffer[0] = (transmit + 1) & 0xff;
655 usb_out_buffer[1] = ((transmit + 1) >> 8) & 0xff;
657 memmove(usb_out_buffer + 3, tms_buffer + j, transmit);
658 result = opendous_usb_message(opendous_jtag_handle, 3 + transmit, receive);
659 if (result != receive) {
660 LOG_ERROR("opendous_tap_execute, wrong result %d, expected %d", result, receive);
661 return ERROR_JTAG_QUEUE_FAILED;
664 memmove(tdo_buffer + i, usb_in_buffer, receive);
665 i += receive;
666 j += transmit;
669 #ifdef _DEBUG_USB_COMMS_
670 LOG_DEBUG("opendous tap result %d", byte_length_out);
671 opendous_debug_buffer(tdo_buffer, byte_length_out);
672 #endif
674 /* LOG_INFO("eStick tap execute %d",tap_length); */
675 for (i = 0; i < pending_scan_results_length; i++) {
677 struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i];
678 uint8_t *buffer = pending_scan_result->buffer;
679 int length = pending_scan_result->length;
680 int first = pending_scan_result->first;
681 struct scan_command *command = pending_scan_result->command;
683 /* Copy to buffer */
684 buf_set_buf(tdo_buffer, first, buffer, 0, length);
686 DEBUG_JTAG_IO("pending scan result, length = %d", length);
688 #ifdef _DEBUG_USB_COMMS_
689 opendous_debug_buffer(buffer, byte_length_out);
690 #endif
692 if (jtag_read_buffer(buffer, command) != ERROR_OK) {
693 opendous_tap_init();
694 return ERROR_JTAG_QUEUE_FAILED;
697 if (pending_scan_result->buffer != NULL)
698 free(pending_scan_result->buffer);
701 opendous_tap_init();
704 return ERROR_OK;
707 /*****************************************************************************/
708 /* Estick USB low-level functions */
710 struct opendous_jtag *opendous_usb_open(void)
712 struct opendous_jtag *result;
714 struct jtag_libusb_device_handle *devh;
715 if (jtag_libusb_open(opendous_probe->VID, opendous_probe->PID, &devh) != ERROR_OK)
716 return NULL;
718 jtag_libusb_set_configuration(devh, 0);
719 jtag_libusb_claim_interface(devh, 0);
721 result = (struct opendous_jtag *) malloc(sizeof(struct opendous_jtag));
722 result->usb_handle = devh;
723 return result;
726 void opendous_usb_close(struct opendous_jtag *opendous_jtag)
728 jtag_libusb_close(opendous_jtag->usb_handle);
729 free(opendous_jtag);
732 /* Send a message and receive the reply. */
733 int opendous_usb_message(struct opendous_jtag *opendous_jtag, int out_length, int in_length)
735 int result;
737 result = opendous_usb_write(opendous_jtag, out_length);
738 if (result == out_length) {
739 result = opendous_usb_read(opendous_jtag);
740 if (result == in_length)
741 return result;
742 else {
743 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
744 return -1;
746 } else {
747 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
748 return -1;
752 /* Write data from out_buffer to USB. */
753 int opendous_usb_write(struct opendous_jtag *opendous_jtag, int out_length)
755 int result;
757 if (out_length > OPENDOUS_OUT_BUFFER_SIZE) {
758 LOG_ERROR("opendous_jtag_write illegal out_length=%d (max=%d)", out_length, OPENDOUS_OUT_BUFFER_SIZE);
759 return -1;
762 #ifdef _DEBUG_USB_COMMS_
763 LOG_DEBUG("%s: USB write begin", opendous_get_time(time_str));
764 #endif
765 if (opendous_probe->CONTROL_TRANSFER) {
766 result = jtag_libusb_control_transfer(opendous_jtag->usb_handle,
767 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
768 FUNC_WRITE_DATA, 0, 0, (char *) usb_out_buffer, out_length, OPENDOUS_USB_TIMEOUT);
769 } else {
770 result = jtag_libusb_bulk_write(opendous_jtag->usb_handle, OPENDOUS_WRITE_ENDPOINT, \
771 (char *)usb_out_buffer, out_length, OPENDOUS_USB_TIMEOUT);
773 #ifdef _DEBUG_USB_COMMS_
774 LOG_DEBUG("%s: USB write end: %d bytes", opendous_get_time(time_str), result);
775 #endif
777 DEBUG_JTAG_IO("opendous_usb_write, out_length = %d, result = %d", out_length, result);
779 #ifdef _DEBUG_USB_COMMS_
780 opendous_debug_buffer(usb_out_buffer, out_length);
781 #endif
782 return result;
785 /* Read data from USB into in_buffer. */
786 int opendous_usb_read(struct opendous_jtag *opendous_jtag)
788 #ifdef _DEBUG_USB_COMMS_
789 LOG_DEBUG("%s: USB read begin", opendous_get_time(time_str));
790 #endif
791 int result;
792 if (opendous_probe->CONTROL_TRANSFER) {
793 result = jtag_libusb_control_transfer(opendous_jtag->usb_handle,
794 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN,
795 FUNC_READ_DATA, 0, 0, (char *) usb_in_buffer, OPENDOUS_IN_BUFFER_SIZE, OPENDOUS_USB_TIMEOUT);
796 } else {
797 result = jtag_libusb_bulk_read(opendous_jtag->usb_handle, OPENDOUS_READ_ENDPOINT,
798 (char *)usb_in_buffer, OPENDOUS_IN_BUFFER_SIZE, OPENDOUS_USB_TIMEOUT);
800 #ifdef _DEBUG_USB_COMMS_
801 LOG_DEBUG("%s: USB read end: %d bytes", opendous_get_time(time_str), result);
802 #endif
803 DEBUG_JTAG_IO("opendous_usb_read, result = %d", result);
805 #ifdef _DEBUG_USB_COMMS_
806 opendous_debug_buffer(usb_in_buffer, result);
807 #endif
808 return result;
811 #ifdef _DEBUG_USB_COMMS_
812 #define BYTES_PER_LINE 16
814 void opendous_debug_buffer(uint8_t *buffer, int length)
816 char line[81];
817 char s[4];
818 int i;
819 int j;
821 for (i = 0; i < length; i += BYTES_PER_LINE) {
822 snprintf(line, 5, "%04x", i);
823 for (j = i; j < i + BYTES_PER_LINE && j < length; j++) {
824 snprintf(s, 4, " %02x", buffer[j]);
825 strcat(line, s);
827 LOG_DEBUG("%s", line);
831 char *opendous_get_time(char *str)
833 struct timeb timebuffer;
834 char *timeline;
836 ftime(&timebuffer);
837 timeline = ctime(&(timebuffer.time));
838 snprintf(str, 49, "%.8s.%hu", &timeline[11], timebuffer.millitm);
839 return str;
841 #endif