arm-jtag-ew: In armjtagew_init(), set initial JTAG speed to 32 kHz (before TAP initia...
[fw/openocd] / src / jtag / drivers / arm-jtag-ew.c
1 /***************************************************************************
2  *   Copyright (C) 2009 by Dimitar Dimitrov <dinuxbg@gmail.com>            *
3  *   based on Dominic Rath's and Benedikt Sauter's usbprog.c               *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <jtag/interface.h>
26 #include <jtag/commands.h>
27 #include <usb.h>
28 #include "usb_common.h"
29
30
31 #define USB_VID                                         0x15ba
32 #define USB_PID                                         0x001e
33
34 #define ARMJTAGEW_EPT_BULK_OUT          0x01u
35 #define ARMJTAGEW_EPT_BULK_IN           0x82u
36
37 #define ARMJTAGEW_USB_TIMEOUT           2000
38
39 #define ARMJTAGEW_IN_BUFFER_SIZE        (4*1024)
40 #define ARMJTAGEW_OUT_BUFFER_SIZE       (4*1024)
41
42
43 /* USB command request codes. */
44 #define CMD_GET_VERSION                         0x00
45 #define CMD_SELECT_DPIMPL                       0x10
46 #define CMD_SET_TCK_FREQUENCY           0x11
47 #define CMD_GET_TCK_FREQUENCY           0x12
48 #define CMD_MEASURE_MAX_TCK_FREQ        0x15
49 #define CMD_MEASURE_RTCK_RESPONSE       0x16
50 #define CMD_TAP_SHIFT                           0x17
51 #define CMD_SET_TAPHW_STATE                     0x20
52 #define CMD_GET_TAPHW_STATE                     0x21
53 #define CMD_TGPWR_SETUP                         0x22
54
55 /* Global USB buffers */
56 static uint8_t usb_in_buffer[ARMJTAGEW_IN_BUFFER_SIZE];
57 static uint8_t usb_out_buffer[ARMJTAGEW_OUT_BUFFER_SIZE];
58
59 /* Queue command functions */
60 static void armjtagew_end_state(tap_state_t state);
61 static void armjtagew_state_move(void);
62 static void armjtagew_path_move(int num_states, tap_state_t *path);
63 static void armjtagew_runtest(int num_cycles);
64 static void armjtagew_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command);
65 static void armjtagew_reset(int trst, int srst);
66 //static void armjtagew_simple_command(uint8_t command);
67 static int armjtagew_get_status(void);
68
69 /* tap buffer functions */
70 static void armjtagew_tap_init(void);
71 static int armjtagew_tap_execute(void);
72 static void armjtagew_tap_ensure_space(int scans, int bits);
73 static void armjtagew_tap_append_step(int tms, int tdi);
74 static void armjtagew_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command);
75
76 /* ARM-JTAG-EW lowlevel functions */
77 struct armjtagew {
78         struct usb_dev_handle* usb_handle;
79 };
80
81 static struct armjtagew *armjtagew_usb_open(void);
82 static void armjtagew_usb_close(struct armjtagew *armjtagew);
83 static int armjtagew_usb_message(struct armjtagew *armjtagew, int out_length, int in_length);
84 static int armjtagew_usb_write(struct armjtagew *armjtagew, int out_length);
85 static int armjtagew_usb_read(struct armjtagew *armjtagew, int exp_in_length);
86
87 /* helper functions */
88 static int armjtagew_get_version_info(void);
89
90 #ifdef _DEBUG_USB_COMMS_
91 static void armjtagew_debug_buffer(uint8_t *buffer, int length);
92 #endif
93
94 static struct armjtagew* armjtagew_handle;
95
96
97
98 /***************************************************************************/
99 /* External interface implementation */
100
101 static int armjtagew_execute_queue(void)
102 {
103         struct jtag_command *cmd = jtag_command_queue;
104         int scan_size;
105         enum scan_type type;
106         uint8_t *buffer;
107
108         while (cmd != NULL)
109         {
110                 switch (cmd->type)
111                 {
112                         case JTAG_RUNTEST:
113                                 DEBUG_JTAG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
114                                         cmd->cmd.runtest->end_state);
115
116                                 armjtagew_end_state(cmd->cmd.runtest->end_state);
117                                 armjtagew_runtest(cmd->cmd.runtest->num_cycles);
118                                 break;
119
120                         case JTAG_TLR_RESET:
121                                 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
122
123                                 armjtagew_end_state(cmd->cmd.statemove->end_state);
124                                 armjtagew_state_move();
125                                 break;
126
127                         case JTAG_PATHMOVE:
128                                 DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
129                                         cmd->cmd.pathmove->num_states, \
130                                         cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
131
132                                 armjtagew_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
133                                 break;
134
135                         case JTAG_SCAN:
136                                 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
137
138                                 armjtagew_end_state(cmd->cmd.scan->end_state);
139
140                                 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
141                                 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
142
143 #ifdef _DEBUG_USB_COMMS_
144                                 armjtagew_debug_buffer(buffer, (scan_size + 7) / 8);
145 #endif
146                                 type = jtag_scan_type(cmd->cmd.scan);
147                                 armjtagew_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
148                                 break;
149
150                         case JTAG_RESET:
151                                 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
152
153                                 armjtagew_tap_execute();
154
155                                 if (cmd->cmd.reset->trst == 1)
156                                 {
157                                         tap_set_state(TAP_RESET);
158                                 }
159                                 armjtagew_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
160                                 break;
161
162                         case JTAG_SLEEP:
163                                 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
164                                 armjtagew_tap_execute();
165                                 jtag_sleep(cmd->cmd.sleep->us);
166                                 break;
167
168                         default:
169                                 LOG_ERROR("BUG: unknown JTAG command type encountered");
170                                 exit(-1);
171                 }
172                 cmd = cmd->next;
173         }
174
175         return armjtagew_tap_execute();
176 }
177
178
179 /* Sets speed in kHz. */
180 static int armjtagew_speed(int speed)
181 {
182     int result;
183     int speed_real;
184
185
186     usb_out_buffer[0] = CMD_SET_TCK_FREQUENCY;
187         buf_set_u32(usb_out_buffer + 1, 0, 32, speed*1000);
188
189     result = armjtagew_usb_message(armjtagew_handle, 5, 4);
190
191     if (result < 0)
192     {
193         LOG_ERROR("ARM-JTAG-EW setting speed failed (%d)", result);
194         return ERROR_JTAG_DEVICE_ERROR;
195     }
196
197         usb_out_buffer[0] = CMD_GET_TCK_FREQUENCY;
198     result = armjtagew_usb_message(armjtagew_handle, 1, 4);
199         speed_real = (int)buf_get_u32(usb_in_buffer,0,32) / 1000;
200         if (result < 0)
201         {
202         LOG_ERROR("ARM-JTAG-EW getting speed failed (%d)", result);
203         return ERROR_JTAG_DEVICE_ERROR;
204         }
205         else
206         {
207         LOG_INFO("Requested speed %dkHz, emulator reported %dkHz.", speed, speed_real);
208         }
209
210     return ERROR_OK;
211 }
212
213
214 static int armjtagew_khz(int khz, int *jtag_speed)
215 {
216         *jtag_speed = khz;
217
218         return ERROR_OK;
219 }
220
221 static int armjtagew_speed_div(int speed, int* khz)
222 {
223         *khz = speed;
224
225         return ERROR_OK;
226 }
227
228
229 static int armjtagew_init(void)
230 {
231         int check_cnt;
232
233         armjtagew_handle = armjtagew_usb_open();
234
235         if (armjtagew_handle == 0)
236         {
237                 LOG_ERROR("Cannot find ARM-JTAG-EW Interface! Please check connection and permissions.");
238                 return ERROR_JTAG_INIT_FAILED;
239         }
240
241         check_cnt = 0;
242         while (check_cnt < 3)
243         {
244                 if (armjtagew_get_version_info() == ERROR_OK)
245                 {
246                         /* attempt to get status */
247                         armjtagew_get_status();
248                         break;
249                 }
250
251                 check_cnt++;
252         }
253
254         if (check_cnt == 3)
255         {
256                 LOG_INFO("ARM-JTAG-EW initial read failed, don't worry");
257         }
258
259         // Initial JTAG speed (for reset and initialization): 32 kHz
260         armjtagew_speed(32);
261
262         LOG_INFO("ARM-JTAG-EW JTAG Interface ready");
263
264         armjtagew_reset(0, 0);
265         armjtagew_tap_init();
266
267         return ERROR_OK;
268 }
269
270 static int armjtagew_quit(void)
271 {
272         armjtagew_usb_close(armjtagew_handle);
273         return ERROR_OK;
274 }
275
276 /***************************************************************************/
277 /* Queue command implementations */
278
279 static void armjtagew_end_state(tap_state_t state)
280 {
281         if (tap_is_state_stable(state))
282         {
283                 tap_set_end_state(state);
284         }
285         else
286         {
287                 LOG_ERROR("BUG: %i is not a valid end state", state);
288                 exit(-1);
289         }
290 }
291
292 /* Goes to the end state. */
293 static void armjtagew_state_move(void)
294 {
295         int i;
296         int tms = 0;
297         uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
298         int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
299
300         for (i = 0; i < tms_count; i++)
301         {
302                 tms = (tms_scan >> i) & 1;
303                 armjtagew_tap_append_step(tms, 0);
304         }
305
306         tap_set_state(tap_get_end_state());
307 }
308
309 static void armjtagew_path_move(int num_states, tap_state_t *path)
310 {
311         int i;
312
313         for (i = 0; i < num_states; i++)
314         {
315                 /*
316                  * TODO: The ARM-JTAG-EW hardware delays TDI with 3 TCK cycles when in RTCK mode.
317                  * Either handle that here, or update the documentation with examples
318                  * how to fix that in the configuration files.
319                  */
320                 if (path[i] == tap_state_transition(tap_get_state(), false))
321                 {
322                         armjtagew_tap_append_step(0, 0);
323                 }
324                 else if (path[i] == tap_state_transition(tap_get_state(), true))
325                 {
326                         armjtagew_tap_append_step(1, 0);
327                 }
328                 else
329                 {
330                         LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
331                         exit(-1);
332                 }
333
334                 tap_set_state(path[i]);
335         }
336
337         tap_set_end_state(tap_get_state());
338 }
339
340 static void armjtagew_runtest(int num_cycles)
341 {
342         int i;
343
344         tap_state_t saved_end_state = tap_get_end_state();
345
346         /* only do a state_move when we're not already in IDLE */
347         if (tap_get_state() != TAP_IDLE)
348         {
349                 armjtagew_end_state(TAP_IDLE);
350                 armjtagew_state_move();
351         }
352
353         /* execute num_cycles */
354         for (i = 0; i < num_cycles; i++)
355         {
356                 armjtagew_tap_append_step(0, 0);
357         }
358
359         /* finish in end_state */
360         armjtagew_end_state(saved_end_state);
361         if (tap_get_state() != tap_get_end_state())
362         {
363                 armjtagew_state_move();
364         }
365 }
366
367 static void armjtagew_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command)
368 {
369         tap_state_t saved_end_state;
370
371         armjtagew_tap_ensure_space(1, scan_size + 8);
372
373         saved_end_state = tap_get_end_state();
374
375         /* Move to appropriate scan state */
376         armjtagew_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
377
378         armjtagew_state_move();
379         armjtagew_end_state(saved_end_state);
380
381         /* Scan */
382         armjtagew_tap_append_scan(scan_size, buffer, command);
383
384         /* We are in Exit1, go to Pause */
385         armjtagew_tap_append_step(0, 0);
386
387         tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
388
389         if (tap_get_state() != tap_get_end_state())
390         {
391                 armjtagew_state_move();
392         }
393 }
394
395 static void armjtagew_reset(int trst, int srst)
396 {
397         const uint8_t trst_mask = (1u << 5);
398         const uint8_t srst_mask = (1u << 6);
399         uint8_t val = 0;
400         uint8_t outp_en = 0;
401         uint8_t change_mask = 0;
402         int result;
403
404         LOG_DEBUG("trst: %i, srst: %i", trst, srst);
405
406         if (srst == 0)
407         {
408                 val |= srst_mask;
409                 outp_en &= ~srst_mask;          /* tristate */
410                 change_mask |= srst_mask;
411         }
412         else if (srst == 1)
413         {
414                 val &= ~srst_mask;
415                 outp_en |= srst_mask;
416                 change_mask |= srst_mask;
417         }
418
419         if (trst == 0)
420         {
421                 val |= trst_mask;
422                 outp_en &= ~trst_mask;          /* tristate */
423                 change_mask |= trst_mask;
424         }
425         else if (trst == 1)
426         {
427                 val &= ~trst_mask;
428                 outp_en |= trst_mask;
429                 change_mask |= trst_mask;
430         }
431
432         usb_out_buffer[0] = CMD_SET_TAPHW_STATE;
433         usb_out_buffer[1] = val;
434         usb_out_buffer[2] = outp_en;
435         usb_out_buffer[3] = change_mask;
436         result = armjtagew_usb_write(armjtagew_handle, 4);
437         if (result != 4)
438         {
439                 LOG_ERROR("ARM-JTAG-EW TRST/SRST pin set failed failed (%d)", result);
440         }
441 }
442
443
444 static int armjtagew_get_status(void)
445 {
446         int result;
447
448         usb_out_buffer[0] = CMD_GET_TAPHW_STATE;
449         result = armjtagew_usb_message(armjtagew_handle, 1, 12);
450
451         if (result == 0)
452         {
453                 unsigned int u_tg = buf_get_u32(usb_in_buffer, 0, 16);
454                 LOG_INFO("U_tg = %d mV, U_aux = %d mV, U_tgpwr = %d mV, I_tgpwr = %d mA, D1 = %d, Target power %s %s",
455                          (int)(buf_get_u32(usb_in_buffer + 0, 0, 16)),
456                          (int)(buf_get_u32(usb_in_buffer + 2, 0, 16)),
457                          (int)(buf_get_u32(usb_in_buffer + 4, 0, 16)),
458                          (int)(buf_get_u32(usb_in_buffer + 6, 0, 16)),
459                          usb_in_buffer[9],
460                          usb_in_buffer[11] ? "OVERCURRENT" : "OK",
461                          usb_in_buffer[10] ? "enabled" : "disabled");
462
463                 if (u_tg < 1500)
464                 {
465                         LOG_ERROR("Vref too low. Check Target Power");
466                 }
467         }
468         else
469         {
470                 LOG_ERROR("ARM-JTAG-EW command CMD_GET_TAPHW_STATE failed (%d)", result);
471         }
472
473         return ERROR_OK;
474 }
475
476 static int armjtagew_get_version_info(void)
477 {
478         int result;
479         char sn[16];
480         char auxinfo[257];
481
482         /* query hardware version */
483         usb_out_buffer[0] = CMD_GET_VERSION;
484         result = armjtagew_usb_message(armjtagew_handle, 1, 4 + 15 + 256);
485
486         if (result != 0)
487         {
488                 LOG_ERROR("ARM-JTAG-EW command CMD_GET_VERSION failed (%d)", result);
489                 return ERROR_JTAG_DEVICE_ERROR;
490         }
491
492
493         memcpy(sn, usb_in_buffer + 4, 15);
494         sn[15] = '\0';
495         memcpy(auxinfo, usb_in_buffer + 4+15, 256);
496         auxinfo[256] = '\0';
497
498         LOG_INFO("ARM-JTAG-EW firmware version %d.%d, hardware revision %c, SN=%s, Additional info: %s", \
499                         usb_in_buffer[1], usb_in_buffer[0], \
500                         isgraph(usb_in_buffer[2]) ? usb_in_buffer[2] : 'X', \
501                         sn, auxinfo);
502
503         if (1 != usb_in_buffer[1] || 6 != usb_in_buffer[0])
504         {
505                 LOG_WARNING("ARM-JTAG-EW firmware version %d.%d is untested with this version of OpenOCD. You might experience unexpected behavior.", usb_in_buffer[1], usb_in_buffer[0]);
506         }
507         return ERROR_OK;
508 }
509
510 COMMAND_HANDLER(armjtagew_handle_armjtagew_info_command)
511 {
512         if (armjtagew_get_version_info() == ERROR_OK)
513         {
514                 /* attempt to get status */
515                 armjtagew_get_status();
516         }
517
518         return ERROR_OK;
519 }
520
521 static const struct command_registration armjtagew_command_handlers[] = {
522         {
523                 .name = "armjtagew_info",
524                 .handler = &armjtagew_handle_armjtagew_info_command,
525                 .mode = COMMAND_EXEC,
526                 .help = "query armjtagew info",
527         },
528         COMMAND_REGISTRATION_DONE
529 };
530
531 struct jtag_interface armjtagew_interface = {
532         .name = "arm-jtag-ew",
533         .commands = armjtagew_command_handlers,
534         .transports = jtag_only,
535
536         .execute_queue = armjtagew_execute_queue,
537         .speed = armjtagew_speed,
538         .speed_div = armjtagew_speed_div,
539         .khz = armjtagew_khz,
540         .init = armjtagew_init,
541         .quit = armjtagew_quit,
542 };
543
544 /***************************************************************************/
545 /* ARM-JTAG-EW tap functions */
546
547 /* 2048 is the max value we can use here */
548 #define ARMJTAGEW_TAP_BUFFER_SIZE 2048
549
550 static int tap_length;
551 static uint8_t tms_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
552 static uint8_t tdi_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
553 static uint8_t tdo_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
554
555 struct pending_scan_result {
556         int first;      /* First bit position in tdo_buffer to read */
557         int length; /* Number of bits to read */
558         struct scan_command *command; /* Corresponding scan command */
559         uint8_t *buffer;
560 };
561
562 #define MAX_PENDING_SCAN_RESULTS 256
563
564 static int pending_scan_results_length;
565 static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
566
567 static int last_tms;
568
569 static void armjtagew_tap_init(void)
570 {
571         tap_length = 0;
572         pending_scan_results_length = 0;
573 }
574
575 static void armjtagew_tap_ensure_space(int scans, int bits)
576 {
577         int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
578         int available_bits = ARMJTAGEW_TAP_BUFFER_SIZE * 8 - tap_length;
579
580         if (scans > available_scans || bits > available_bits)
581         {
582                 armjtagew_tap_execute();
583         }
584 }
585
586 static void armjtagew_tap_append_step(int tms, int tdi)
587 {
588         last_tms = tms;
589         int index_local = tap_length / 8;
590
591         if (index_local < ARMJTAGEW_TAP_BUFFER_SIZE)
592         {
593                 int bit_index = tap_length % 8;
594                 uint8_t bit = 1 << bit_index;
595
596                 if (tms)
597                 {
598                         tms_buffer[index_local] |= bit;
599                 }
600                 else
601                 {
602                         tms_buffer[index_local] &= ~bit;
603                 }
604
605                 if (tdi)
606                 {
607                         tdi_buffer[index_local] |= bit;
608                 }
609                 else
610                 {
611                         tdi_buffer[index_local] &= ~bit;
612                 }
613
614                 tap_length++;
615         }
616         else
617         {
618                 LOG_ERROR("armjtagew_tap_append_step, overflow");
619         }
620 }
621
622 void armjtagew_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command)
623 {
624         struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
625         int i;
626
627         pending_scan_result->first = tap_length;
628         pending_scan_result->length = length;
629         pending_scan_result->command = command;
630         pending_scan_result->buffer = buffer;
631
632         for (i = 0; i < length; i++)
633         {
634                 armjtagew_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
635         }
636         pending_scan_results_length++;
637 }
638
639 /* Pad and send a tap sequence to the device, and receive the answer.
640  * For the purpose of padding we assume that we are in idle or pause state. */
641 static int armjtagew_tap_execute(void)
642 {
643         int byte_length;
644         int tms_offset;
645         int tdi_offset;
646         int i;
647         int result;
648
649         if (tap_length > 0)
650         {
651                 /* Pad last byte so that tap_length is divisible by 8 */
652                 while (tap_length % 8 != 0)
653                 {
654                         /* More of the last TMS value keeps us in the same state,
655                          * analogous to free-running JTAG interfaces. */
656                         armjtagew_tap_append_step(last_tms, 0);
657                 }
658
659                 byte_length = tap_length / 8;
660
661                 usb_out_buffer[0] = CMD_TAP_SHIFT;
662                 buf_set_u32(usb_out_buffer + 1, 0, 16, byte_length);
663
664                 tms_offset = 3;
665                 for (i = 0; i < byte_length; i++)
666                 {
667                         usb_out_buffer[tms_offset + i] = flip_u32(tms_buffer[i],8);
668                 }
669
670                 tdi_offset = tms_offset + byte_length;
671                 for (i = 0; i < byte_length; i++)
672                 {
673                         usb_out_buffer[tdi_offset + i] = flip_u32(tdi_buffer[i],8);
674                 }
675
676                 result = armjtagew_usb_message(armjtagew_handle, 3 + 2 * byte_length, byte_length + 4);
677
678                 if (result == 0)
679                 {
680                         int stat_local;
681
682                         stat_local = (int)buf_get_u32(usb_in_buffer + byte_length, 0, 32);
683                         if (stat_local) {
684                                 LOG_ERROR("armjtagew_tap_execute, emulator returned error code %d for a CMD_TAP_SHIFT command", stat_local);
685                                 return ERROR_JTAG_QUEUE_FAILED;
686                         }
687
688                         for (i = 0; i < byte_length; i++)
689                         {
690                                 tdo_buffer[i] = flip_u32(usb_in_buffer[i],8);
691                         }
692
693                         for (i = 0; i < pending_scan_results_length; i++)
694                         {
695                                 struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i];
696                                 uint8_t *buffer = pending_scan_result->buffer;
697                                 int length = pending_scan_result->length;
698                                 int first = pending_scan_result->first;
699                                 struct scan_command *command = pending_scan_result->command;
700
701                                 /* Copy to buffer */
702                                 buf_set_buf(tdo_buffer, first, buffer, 0, length);
703
704                                 DEBUG_JTAG_IO("pending scan result, length = %d", length);
705
706 #ifdef _DEBUG_USB_COMMS_
707                                 armjtagew_debug_buffer(buffer, byte_length);
708 #endif
709
710                                 if (jtag_read_buffer(buffer, command) != ERROR_OK)
711                                 {
712                                         armjtagew_tap_init();
713                                         return ERROR_JTAG_QUEUE_FAILED;
714                                 }
715
716                                 if (pending_scan_result->buffer != NULL)
717                                 {
718                                         free(pending_scan_result->buffer);
719                                 }
720                         }
721                 }
722                 else
723                 {
724                         LOG_ERROR("armjtagew_tap_execute, wrong result %d, expected %d", result, byte_length);
725                         return ERROR_JTAG_QUEUE_FAILED;
726                 }
727
728                 armjtagew_tap_init();
729         }
730
731         return ERROR_OK;
732 }
733
734 /*****************************************************************************/
735 /* JLink USB low-level functions */
736
737 static struct armjtagew* armjtagew_usb_open()
738 {
739         usb_init();
740
741         const uint16_t vids[] = { USB_VID, 0 };
742         const uint16_t pids[] = { USB_PID, 0 };
743         struct usb_dev_handle *dev;
744         if (jtag_usb_open(vids, pids, &dev) != ERROR_OK)
745                 return NULL;
746
747         struct armjtagew *result = malloc(sizeof(struct armjtagew));
748         result->usb_handle = dev;
749
750 #if 0
751         /* usb_set_configuration required under win32 */
752         usb_set_configuration(dev, dev->config[0].bConfigurationValue);
753 #endif
754         usb_claim_interface(dev, 0);
755 #if 0
756         /*
757          * This makes problems under Mac OS X. And is not needed
758          * under Windows. Hopefully this will not break a linux build
759          */
760         usb_set_altinterface(dev, 0);
761 #endif
762         return result;
763 }
764
765 static void armjtagew_usb_close(struct armjtagew *armjtagew)
766 {
767         usb_close(armjtagew->usb_handle);
768         free(armjtagew);
769 }
770
771 /* Send a message and receive the reply. */
772 static int armjtagew_usb_message(struct armjtagew *armjtagew, int out_length, int in_length)
773 {
774         int result;
775
776         result = armjtagew_usb_write(armjtagew, out_length);
777         if (result == out_length)
778         {
779                 result = armjtagew_usb_read(armjtagew, in_length);
780                 if (result != in_length)
781                 {
782                         LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
783                         return -1;
784                 }
785         }
786         else
787         {
788                 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
789                 return -1;
790         }
791         return 0;
792 }
793
794 /* Write data from out_buffer to USB. */
795 static int armjtagew_usb_write(struct armjtagew *armjtagew, int out_length)
796 {
797         int result;
798
799         if (out_length > ARMJTAGEW_OUT_BUFFER_SIZE)
800         {
801                 LOG_ERROR("armjtagew_write illegal out_length=%d (max=%d)", out_length, ARMJTAGEW_OUT_BUFFER_SIZE);
802                 return -1;
803         }
804
805         result = usb_bulk_write(armjtagew->usb_handle, ARMJTAGEW_EPT_BULK_OUT, \
806                 (char*)usb_out_buffer, out_length, ARMJTAGEW_USB_TIMEOUT);
807
808         DEBUG_JTAG_IO("armjtagew_usb_write, out_length = %d, result = %d", out_length, result);
809
810 #ifdef _DEBUG_USB_COMMS_
811         armjtagew_debug_buffer(usb_out_buffer, out_length);
812 #endif
813         return result;
814 }
815
816 /* Read data from USB into in_buffer. */
817 static int armjtagew_usb_read(struct armjtagew *armjtagew, int exp_in_length)
818 {
819         int result = usb_bulk_read(armjtagew->usb_handle, ARMJTAGEW_EPT_BULK_IN, \
820                 (char*)usb_in_buffer, exp_in_length, ARMJTAGEW_USB_TIMEOUT);
821
822         DEBUG_JTAG_IO("armjtagew_usb_read, result = %d", result);
823
824 #ifdef _DEBUG_USB_COMMS_
825         armjtagew_debug_buffer(usb_in_buffer, result);
826 #endif
827         return result;
828 }
829
830
831 #ifdef _DEBUG_USB_COMMS_
832 #define BYTES_PER_LINE  16
833
834 static void armjtagew_debug_buffer(uint8_t *buffer, int length)
835 {
836         char line[81];
837         char s[4];
838         int i;
839         int j;
840
841         for (i = 0; i < length; i += BYTES_PER_LINE)
842         {
843                 snprintf(line, 5, "%04x", i);
844                 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
845                 {
846                         snprintf(s, 4, " %02x", buffer[j]);
847                         strcat(line, s);
848                 }
849                 LOG_DEBUG("%s", line);
850         }
851 }
852 #endif
853