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