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