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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)