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