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