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