1 /***************************************************************************
2 * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
3 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
36 #define JLINK_WRITE_ENDPOINT 0x02
37 #define JLINK_READ_ENDPOINT 0x81
39 static unsigned int jlink_write_ep = JLINK_WRITE_ENDPOINT;
40 static unsigned int jlink_read_ep = JLINK_READ_ENDPOINT;
41 static unsigned int jlink_hw_jtag_version = 2;
43 #define JLINK_USB_TIMEOUT 1000
45 // See Section 1.3.2 of the Segger JLink USB protocol manual
46 /* 2048 is the max value we can use here */
47 //#define JLINK_TAP_BUFFER_SIZE 2048
48 #define JLINK_TAP_BUFFER_SIZE 256
49 //#define JLINK_TAP_BUFFER_SIZE 384
51 #define JLINK_IN_BUFFER_SIZE 2048
52 #define JLINK_OUT_BUFFER_SIZE 2*2048+4
53 #define JLINK_EMU_RESULT_BUFFER_SIZE 64
55 /* Global USB buffers */
56 static u8 usb_in_buffer[JLINK_IN_BUFFER_SIZE];
57 static u8 usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
58 static u8 usb_emu_result_buffer[JLINK_EMU_RESULT_BUFFER_SIZE];
60 /* Constants for JLink command */
61 #define EMU_CMD_VERSION 0x01
62 #define EMU_CMD_SET_SPEED 0x05
63 #define EMU_CMD_GET_STATE 0x07
64 #define EMU_CMD_HW_CLOCK 0xc8
65 #define EMU_CMD_HW_TMS0 0xc9
66 #define EMU_CMD_HW_TMS1 0xca
67 #define EMU_CMD_HW_JTAG2 0xce
68 #define EMU_CMD_HW_JTAG3 0xcf
69 #define EMU_CMD_GET_MAX_MEM_BLOCK 0xd4
70 #define EMU_CMD_HW_RESET0 0xdc
71 #define EMU_CMD_HW_RESET1 0xdd
72 #define EMU_CMD_HW_TRST0 0xde
73 #define EMU_CMD_HW_TRST1 0xdf
74 #define EMU_CMD_GET_CAPS 0xe8
75 #define EMU_CMD_GET_HW_VERSION 0xf0
77 /* bits return from EMU_CMD_GET_CAPS */
78 #define EMU_CAP_GET_HW_VERSION 1
79 #define EMU_CAP_GET_MAX_BLOCK_SIZE 11
81 /* max speed 12MHz v5.0 jlink */
82 #define JLINK_MAX_SPEED 12000
84 /* External interface functions */
85 static int jlink_execute_queue(void);
86 static int jlink_speed(int speed);
87 static int jlink_speed_div(int speed, int* khz);
88 static int jlink_khz(int khz, int *jtag_speed);
89 static int jlink_register_commands(struct command_context_s *cmd_ctx);
90 static int jlink_init(void);
91 static int jlink_quit(void);
93 /* CLI command handler functions */
94 static int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
95 static int jlink_handle_jlink_hw_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
97 /* Queue command functions */
98 static void jlink_end_state(tap_state_t state);
99 static void jlink_state_move(void);
100 static void jlink_path_move(int num_states, tap_state_t *path);
101 static void jlink_runtest(int num_cycles);
102 static void jlink_scan(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
103 static void jlink_reset(int trst, int srst);
104 static void jlink_simple_command(u8 command);
105 static int jlink_get_status(void);
107 /* J-Link tap buffer functions */
108 static void jlink_tap_init(void);
109 static int jlink_tap_execute(void);
110 static void jlink_tap_ensure_space(int scans, int bits);
111 static void jlink_tap_append_step(int tms, int tdi);
112 static void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command);
114 /* Jlink lowlevel functions */
115 typedef struct jlink_jtag
117 struct usb_dev_handle* usb_handle;
120 static jlink_jtag_t *jlink_usb_open(void);
121 static void jlink_usb_close(jlink_jtag_t *jlink_jtag);
122 static int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length);
123 static int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length);
124 static int jlink_usb_read(jlink_jtag_t *jlink_jtag, int expected_size);
125 static int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag);
127 /* helper functions */
128 static int jlink_get_version_info(void);
130 #ifdef _DEBUG_USB_COMMS_
131 static void jlink_debug_buffer(u8 *buffer, int length);
134 static enum tap_state jlink_last_state = TAP_RESET;
136 static jlink_jtag_t* jlink_jtag_handle;
138 /***************************************************************************/
139 /* External interface implementation */
141 jtag_interface_t jlink_interface =
144 .execute_queue = jlink_execute_queue,
145 .speed = jlink_speed,
146 .speed_div = jlink_speed_div,
148 .register_commands = jlink_register_commands,
153 static void jlink_execute_end_state(jtag_command_t *cmd)
155 DEBUG_JTAG_IO("end_state: %i", cmd->cmd.end_state->end_state);
157 if (cmd->cmd.end_state->end_state != TAP_INVALID)
158 jlink_end_state(cmd->cmd.end_state->end_state);
161 static void jlink_execute_runtest(jtag_command_t *cmd)
163 DEBUG_JTAG_IO("runtest %i cycles, end in %i",
164 cmd->cmd.runtest->num_cycles,
165 cmd->cmd.runtest->end_state);
167 if (cmd->cmd.runtest->end_state != TAP_INVALID)
168 jlink_end_state(cmd->cmd.runtest->end_state);
170 jlink_runtest(cmd->cmd.runtest->num_cycles);
173 static void jlink_execute_statemove(jtag_command_t *cmd)
175 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
177 if (cmd->cmd.statemove->end_state != TAP_INVALID)
179 jlink_end_state(cmd->cmd.statemove->end_state);
184 static void jlink_execute_pathmove(jtag_command_t *cmd)
186 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
187 cmd->cmd.pathmove->num_states,
188 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
190 jlink_path_move(cmd->cmd.pathmove->num_states,
191 cmd->cmd.pathmove->path);
194 static void jlink_execute_scan(jtag_command_t *cmd)
200 DEBUG_JTAG_IO("scan end in %s", tap_state_name(cmd->cmd.scan->end_state));
202 if (cmd->cmd.scan->end_state != TAP_INVALID)
203 jlink_end_state(cmd->cmd.scan->end_state);
205 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
206 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
208 #ifdef _DEBUG_USB_COMMS_
209 jlink_debug_buffer(buffer, (scan_size + 7) / 8);
211 type = jtag_scan_type(cmd->cmd.scan);
212 jlink_scan(cmd->cmd.scan->ir_scan,
213 type, buffer, scan_size, cmd->cmd.scan);
216 static void jlink_execute_reset(jtag_command_t *cmd)
218 DEBUG_JTAG_IO("reset trst: %i srst %i",
219 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
222 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
226 static void jlink_execute_sleep(jtag_command_t *cmd)
228 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
230 jtag_sleep(cmd->cmd.sleep->us);
233 static void jlink_execute_command(jtag_command_t *cmd)
237 case JTAG_RUNTEST: jlink_execute_runtest(cmd); break;
238 case JTAG_STATEMOVE: jlink_execute_statemove(cmd); break;
239 case JTAG_PATHMOVE: jlink_execute_pathmove(cmd); break;
240 case JTAG_SCAN: jlink_execute_scan(cmd); break;
241 case JTAG_RESET: jlink_execute_reset(cmd); break;
242 case JTAG_SLEEP: jlink_execute_sleep(cmd); break;
244 LOG_ERROR("BUG: unknown JTAG command type encountered");
249 static int jlink_execute_queue(void)
251 jtag_command_t *cmd = jtag_command_queue;
255 jlink_execute_command(cmd);
259 return jlink_tap_execute();
262 /* Sets speed in kHz. */
263 static int jlink_speed(int speed)
267 if (speed > JLINK_MAX_SPEED)
269 LOG_INFO("Ignoring speed request: %dkHz exceeds %dkHz maximum",
270 speed, JLINK_MAX_SPEED);
274 /* check for RTCK setting */
278 usb_out_buffer[0] = EMU_CMD_SET_SPEED;
279 usb_out_buffer[1] = (speed >> 0) & 0xff;
280 usb_out_buffer[2] = (speed >> 8) & 0xff;
282 result = jlink_usb_write(jlink_jtag_handle, 3);
285 LOG_ERROR("J-Link setting speed failed (%d)", result);
286 return ERROR_JTAG_DEVICE_ERROR;
292 static int jlink_speed_div(int speed, int* khz)
299 static int jlink_khz(int khz, int *jtag_speed)
306 static int jlink_register_commands(struct command_context_s *cmd_ctx)
309 register_command(cmd_ctx, NULL, "jlink_info",
310 &jlink_handle_jlink_info_command, COMMAND_EXEC,
312 register_command(cmd_ctx, NULL, "jlink_hw_jtag",
313 &jlink_handle_jlink_hw_jtag_command, COMMAND_EXEC,
314 "set/get jlink hw jtag command version [2|3]");
318 static int jlink_init(void)
322 jlink_jtag_handle = jlink_usb_open();
324 if (jlink_jtag_handle == 0)
326 LOG_ERROR("Cannot find jlink Interface! Please check connection and permissions.");
327 return ERROR_JTAG_INIT_FAILED;
330 jlink_hw_jtag_version = 2;
332 while (check_cnt < 3)
334 if (jlink_get_version_info() == ERROR_OK)
336 /* attempt to get status */
346 LOG_INFO("J-Link initial read failed, don't worry");
349 LOG_INFO("J-Link JTAG Interface ready");
354 jlink_speed(jtag_speed);
359 static int jlink_quit(void)
361 jlink_usb_close(jlink_jtag_handle);
365 /***************************************************************************/
366 /* Queue command implementations */
368 static void jlink_end_state(tap_state_t state)
370 if (tap_is_state_stable(state))
372 tap_set_end_state(state);
376 LOG_ERROR("BUG: %i is not a valid end state", state);
381 /* Goes to the end state. */
382 static void jlink_state_move(void)
386 u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
387 u8 tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
389 for (i = 0; i < tms_scan_bits; i++)
391 tms = (tms_scan >> i) & 1;
392 jlink_tap_append_step(tms, 0);
395 tap_set_state(tap_get_end_state());
398 static void jlink_path_move(int num_states, tap_state_t *path)
402 for (i = 0; i < num_states; i++)
404 if (path[i] == tap_state_transition(tap_get_state(), false))
406 jlink_tap_append_step(0, 0);
408 else if (path[i] == tap_state_transition(tap_get_state(), true))
410 jlink_tap_append_step(1, 0);
414 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
418 tap_set_state(path[i]);
421 tap_set_end_state(tap_get_state());
424 static void jlink_runtest(int num_cycles)
428 tap_state_t saved_end_state = tap_get_end_state();
430 jlink_tap_ensure_space(1,num_cycles + 16);
432 /* only do a state_move when we're not already in IDLE */
433 if (tap_get_state() != TAP_IDLE)
435 jlink_end_state(TAP_IDLE);
440 /* execute num_cycles */
441 for (i = 0; i < num_cycles; i++)
443 jlink_tap_append_step(0, 0);
446 /* finish in end_state */
447 jlink_end_state(saved_end_state);
448 if (tap_get_state() != tap_get_end_state())
454 static void jlink_scan(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
456 tap_state_t saved_end_state;
458 jlink_tap_ensure_space(1, scan_size + 16);
460 saved_end_state = tap_get_end_state();
462 /* Move to appropriate scan state */
463 jlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
465 /* Only move if we're not already there */
466 if (tap_get_state() != tap_get_end_state())
469 jlink_end_state(saved_end_state);
472 jlink_tap_append_scan(scan_size, buffer, command);
474 /* We are in Exit1, go to Pause */
475 jlink_tap_append_step(0, 0);
477 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
479 if (tap_get_state() != tap_get_end_state())
485 static void jlink_reset(int trst, int srst)
487 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
489 /* Signals are active low */
492 jlink_simple_command(EMU_CMD_HW_RESET1);
496 jlink_simple_command(EMU_CMD_HW_RESET0);
501 jlink_simple_command(EMU_CMD_HW_TRST0);
505 jlink_simple_command(EMU_CMD_HW_TRST1);
507 jlink_end_state(TAP_RESET);
512 static void jlink_simple_command(u8 command)
516 DEBUG_JTAG_IO("0x%02x", command);
518 usb_out_buffer[0] = command;
519 result = jlink_usb_write(jlink_jtag_handle, 1);
523 LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
527 static int jlink_get_status(void)
531 jlink_simple_command(EMU_CMD_GET_STATE);
533 result = jlink_usb_read(jlink_jtag_handle, 8);
536 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
537 return ERROR_JTAG_DEVICE_ERROR;
540 int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
541 LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d\n", \
542 vref / 1000, vref % 1000, \
543 usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
544 usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
547 LOG_ERROR("Vref too low. Check Target Power\n");
552 static int jlink_get_version_info(void)
556 u32 jlink_caps, jlink_max_size;
558 /* query hardware version */
559 jlink_simple_command(EMU_CMD_VERSION);
561 result = jlink_usb_read(jlink_jtag_handle, 2);
564 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
565 return ERROR_JTAG_DEVICE_ERROR;
568 len = buf_get_u32(usb_in_buffer, 0, 16);
569 if (len > JLINK_IN_BUFFER_SIZE)
571 LOG_ERROR("J-Link command EMU_CMD_VERSION impossible return length 0x%0x", len);
572 len = JLINK_IN_BUFFER_SIZE;
575 result = jlink_usb_read(jlink_jtag_handle, len);
578 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
579 return ERROR_JTAG_DEVICE_ERROR;
582 usb_in_buffer[result] = 0;
583 LOG_INFO("%s", (char *)usb_in_buffer);
585 /* query hardware capabilities */
586 jlink_simple_command(EMU_CMD_GET_CAPS);
588 result = jlink_usb_read(jlink_jtag_handle, 4);
591 LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)\n", result);
592 return ERROR_JTAG_DEVICE_ERROR;
595 jlink_caps = buf_get_u32(usb_in_buffer, 0, 32);
596 LOG_INFO("JLink caps 0x%x", jlink_caps);
598 if (jlink_caps & (1 << EMU_CAP_GET_HW_VERSION))
600 /* query hardware version */
601 jlink_simple_command(EMU_CMD_GET_HW_VERSION);
603 result = jlink_usb_read(jlink_jtag_handle, 4);
606 LOG_ERROR("J-Link command EMU_CMD_GET_HW_VERSION failed (%d)\n", result);
607 return ERROR_JTAG_DEVICE_ERROR;
610 u32 jlink_hw_version = buf_get_u32(usb_in_buffer, 0, 32);
611 u32 major_revision = (jlink_hw_version / 10000) % 100;
612 if (major_revision >= 5)
613 jlink_hw_jtag_version = 3;
615 LOG_INFO("JLink hw version %i", jlink_hw_version);
618 if (jlink_caps & (1 << EMU_CAP_GET_MAX_BLOCK_SIZE))
620 /* query hardware maximum memory block */
621 jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK);
623 result = jlink_usb_read(jlink_jtag_handle, 4);
626 LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)\n", result);
627 return ERROR_JTAG_DEVICE_ERROR;
630 jlink_max_size = buf_get_u32(usb_in_buffer, 0, 32);
631 LOG_INFO("JLink max mem block %i", jlink_max_size);
637 static int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
639 if (jlink_get_version_info() == ERROR_OK)
641 /* attempt to get status */
648 static int jlink_handle_jlink_hw_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
652 command_print(cmd_ctx, "jlink hw jtag %i", jlink_hw_jtag_version);
655 int request_version = atoi(args[0]);
656 switch (request_version) {
658 jlink_hw_jtag_version = request_version;
661 return ERROR_COMMAND_SYNTAX_ERROR;
666 return ERROR_COMMAND_SYNTAX_ERROR;
672 /***************************************************************************/
673 /* J-Link tap functions */
676 static unsigned tap_length=0;
677 static u8 tms_buffer[JLINK_TAP_BUFFER_SIZE];
678 static u8 tdi_buffer[JLINK_TAP_BUFFER_SIZE];
679 static u8 tdo_buffer[JLINK_TAP_BUFFER_SIZE];
683 int first; /* First bit position in tdo_buffer to read */
684 int length; /* Number of bits to read */
685 scan_command_t *command; /* Corresponding scan command */
687 } pending_scan_result_t;
689 #define MAX_PENDING_SCAN_RESULTS 256
691 static int pending_scan_results_length;
692 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
694 static void jlink_tap_init(void)
697 pending_scan_results_length = 0;
700 static void jlink_tap_ensure_space(int scans, int bits)
702 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
703 int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length - 32;
705 if (scans > available_scans || bits > available_bits)
711 static void jlink_tap_append_step(int tms, int tdi)
713 int index = tap_length / 8;
715 if (index >= JLINK_TAP_BUFFER_SIZE)
717 LOG_ERROR("jlink_tap_append_step: overflow");
718 *(u32 *)0xFFFFFFFF = 0;
722 int bit_index = tap_length % 8;
723 u8 bit = 1 << bit_index;
725 // we do not pad TMS, so be sure to initialize all bits
728 tms_buffer[index] = tdi_buffer[index] = 0;
732 tms_buffer[index] |= bit;
734 tms_buffer[index] &= ~bit;
737 tdi_buffer[index] |= bit;
739 tdi_buffer[index] &= ~bit;
744 static void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
746 pending_scan_result_t *pending_scan_result =
747 &pending_scan_results_buffer[pending_scan_results_length];
750 pending_scan_result->first = tap_length;
751 pending_scan_result->length = length;
752 pending_scan_result->command = command;
753 pending_scan_result->buffer = buffer;
755 for (i = 0; i < length; i++)
757 int tms = (i < (length - 1)) ? 0 : 1;
758 int tdi = (buffer[i / 8] & (1 << (i % 8)))!=0;
759 jlink_tap_append_step(tms, tdi);
761 pending_scan_results_length++;
764 /* Pad and send a tap sequence to the device, and receive the answer.
765 * For the purpose of padding we assume that we are in idle or pause state. */
766 static int jlink_tap_execute(void)
775 /* JLink returns an extra NULL in packet when size of in message is a multiple of 64, creates problems with usb comms */
776 /* WARNING This will interfere with tap state counting */
777 while ((TAP_SCAN_BYTES(tap_length)%64)==0)
779 jlink_tap_append_step((tap_get_state() == TAP_RESET)?1:0, 0);
782 // number of full bytes (plus one if some would be left over)
783 byte_length = TAP_SCAN_BYTES(tap_length);
785 bool use_jtag3 = jlink_hw_jtag_version >= 3;
786 usb_out_buffer[0] = use_jtag3 ? EMU_CMD_HW_JTAG3 : EMU_CMD_HW_JTAG2;
787 usb_out_buffer[1] = 0;
788 usb_out_buffer[2] = (tap_length >> 0) & 0xff;
789 usb_out_buffer[3] = (tap_length >> 8) & 0xff;
790 memcpy(usb_out_buffer + 4, tms_buffer, byte_length);
791 memcpy(usb_out_buffer + 4 + byte_length, tdi_buffer, byte_length);
793 jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
794 tap_length, jlink_last_state);
796 result = jlink_usb_message(jlink_jtag_handle, 4 + 2 * byte_length, byte_length);
797 if (result != byte_length)
799 LOG_ERROR("jlink_tap_execute, wrong result %d (expected %d)", result, byte_length);
801 return ERROR_JTAG_QUEUE_FAILED;
804 memcpy(tdo_buffer, usb_in_buffer, byte_length);
806 for (i = 0; i < pending_scan_results_length; i++)
808 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
809 u8 *buffer = pending_scan_result->buffer;
810 int length = pending_scan_result->length;
811 int first = pending_scan_result->first;
812 scan_command_t *command = pending_scan_result->command;
815 buf_set_buf(tdo_buffer, first, buffer, 0, length);
817 DEBUG_JTAG_IO("pending scan result, length = %d", length);
819 #ifdef _DEBUG_USB_COMMS_
820 jlink_debug_buffer(buffer, byte_length);
823 if (jtag_read_buffer(buffer, command) != ERROR_OK)
826 return ERROR_JTAG_QUEUE_FAILED;
829 if (pending_scan_result->buffer != NULL)
831 free(pending_scan_result->buffer);
839 /*****************************************************************************/
840 /* JLink USB low-level functions */
842 static jlink_jtag_t* jlink_usb_open()
844 struct usb_bus *busses;
846 struct usb_device *dev;
848 jlink_jtag_t *result;
850 result = (jlink_jtag_t*) malloc(sizeof(jlink_jtag_t));
856 busses = usb_get_busses();
858 /* find jlink_jtag device in usb bus */
860 for (bus = busses; bus; bus = bus->next)
862 for (dev = bus->devices; dev; dev = dev->next)
864 if ((dev->descriptor.idVendor == VID) && (dev->descriptor.idProduct == PID))
866 result->usb_handle = usb_open(dev);
868 /* usb_set_configuration required under win32 */
869 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
870 usb_claim_interface(result->usb_handle, 0);
874 * This makes problems under Mac OS X. And is not needed
875 * under Windows. Hopefully this will not break a linux build
877 usb_set_altinterface(result->usb_handle, 0);
879 struct usb_interface *iface = dev->config->interface;
880 struct usb_interface_descriptor *desc = iface->altsetting;
881 for (int i = 0; i < desc->bNumEndpoints; i++)
883 u8 epnum = desc->endpoint[i].bEndpointAddress;
884 bool is_input = epnum & 0x80;
885 LOG_DEBUG("usb ep %s %02x", is_input ? "in" : "out", epnum);
887 jlink_read_ep = epnum;
889 jlink_write_ep = epnum;
901 static void jlink_usb_close(jlink_jtag_t *jlink_jtag)
903 usb_close(jlink_jtag->usb_handle);
907 /* Send a message and receive the reply. */
908 static int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length)
912 result = jlink_usb_write(jlink_jtag, out_length);
913 if (result != out_length)
915 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
917 return ERROR_JTAG_DEVICE_ERROR;
920 result = jlink_usb_read(jlink_jtag, in_length);
921 if ((result != in_length) && (result != (in_length + 1)))
923 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
925 return ERROR_JTAG_DEVICE_ERROR;
928 if (jlink_hw_jtag_version < 3)
931 int result2 = ERROR_OK;
932 if (result == in_length)
934 /* Must read the result from the EMU too */
935 result2 = jlink_usb_read_emu_result(jlink_jtag);
938 LOG_ERROR("jlink_usb_read_emu_result retried requested=1, result=%d, in_length=%i", result2,in_length);
939 /* Try again once, should only happen if (in_length%64==0) */
940 result2 = jlink_usb_read_emu_result(jlink_jtag);
943 LOG_ERROR("jlink_usb_read_emu_result failed "
944 "(requested=1, result=%d)", result2);
945 return ERROR_JTAG_DEVICE_ERROR;
949 /* Check the result itself */
950 result2 = usb_emu_result_buffer[0];
954 /* Save the result, then remove it from return value */
955 result2 = usb_in_buffer[result--];
960 LOG_ERROR("jlink_usb_message failed with result=%d)", result2);
961 return ERROR_JTAG_DEVICE_ERROR;
967 /* calls the given usb_bulk_* function, allowing for the data to trickle in with some timeouts */
968 static int usb_bulk_with_retries(
969 int (*f)(usb_dev_handle *, int, char *, int, int),
970 usb_dev_handle *dev, int ep,
971 char *bytes, int size, int timeout)
973 int tries = 3, count = 0;
975 while (tries && (count < size))
977 int result = f(dev, ep, bytes + count, size - count, timeout);
980 else if ((-ETIMEDOUT != result) || !--tries)
986 static int wrap_usb_bulk_write(usb_dev_handle *dev, int ep,
987 char *buff, int size, int timeout)
989 /* usb_bulk_write() takes const char *buff */
990 return usb_bulk_write(dev, ep, buff, size, timeout);
993 static inline int usb_bulk_write_ex(usb_dev_handle *dev, int ep,
994 char *bytes, int size, int timeout)
996 return usb_bulk_with_retries(&wrap_usb_bulk_write,
997 dev, ep, bytes, size, timeout);
1000 static inline int usb_bulk_read_ex(usb_dev_handle *dev, int ep,
1001 char *bytes, int size, int timeout)
1003 return usb_bulk_with_retries(&usb_bulk_read,
1004 dev, ep, bytes, size, timeout);
1007 /* Write data from out_buffer to USB. */
1008 static int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length)
1012 if (out_length > JLINK_OUT_BUFFER_SIZE)
1014 LOG_ERROR("jlink_jtag_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE);
1018 result = usb_bulk_write_ex(jlink_jtag->usb_handle, jlink_write_ep,
1019 (char *)usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
1021 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length, result);
1023 #ifdef _DEBUG_USB_COMMS_
1024 jlink_debug_buffer(usb_out_buffer, out_length);
1029 /* Read data from USB into in_buffer. */
1030 static int jlink_usb_read(jlink_jtag_t *jlink_jtag, int expected_size)
1032 int result = usb_bulk_read_ex(jlink_jtag->usb_handle, jlink_read_ep,
1033 (char *)usb_in_buffer, expected_size, JLINK_USB_TIMEOUT);
1035 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
1037 #ifdef _DEBUG_USB_COMMS_
1038 jlink_debug_buffer(usb_in_buffer, result);
1043 /* Read the result from the previous EMU cmd into result_buffer. */
1044 static int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag)
1046 int result = usb_bulk_read_ex(jlink_jtag->usb_handle, jlink_read_ep,
1047 (char *)usb_emu_result_buffer, 1 /* JLINK_EMU_RESULT_BUFFER_SIZE */,
1050 DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result);
1052 #ifdef _DEBUG_USB_COMMS_
1053 jlink_debug_buffer(usb_emu_result_buffer, result);
1058 #ifdef _DEBUG_USB_COMMS_
1059 #define BYTES_PER_LINE 16
1061 static void jlink_debug_buffer(u8 *buffer, int length)
1068 for (i = 0; i < length; i += BYTES_PER_LINE)
1070 snprintf(line, 5, "%04x", i);
1071 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
1073 snprintf(s, 4, " %02x", buffer[j]);
1076 LOG_DEBUG("%s", line);