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