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