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