target/xtensa: avoid IHI for writes to non-executable memory
[openocd.git] / src / jtag / drivers / vsllink.c
blob34525d5468d695834c14d1e151c1194310ed804c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2009-2010 by Simon Qian <SimonQian@SimonQian.com> *
5 ***************************************************************************/
7 /* Versaloon is a programming tool for multiple MCUs.
8 * It's distributed under GPLv3.
9 * You can find it at http://www.Versaloon.com/.
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
16 #include <jtag/adapter.h>
17 #include <jtag/interface.h>
18 #include <jtag/commands.h>
19 #include <jtag/swd.h>
20 #include <libusb.h>
22 #include "versaloon/versaloon_include.h"
23 #include "versaloon/versaloon.h"
25 static int vsllink_tms_offset;
27 struct pending_scan_result {
28 int src_offset;
29 int dest_offset;
30 int length; /* Number of bits to read */
31 struct scan_command *command; /* Corresponding scan command */
32 uint8_t *ack;
33 uint8_t *buffer;
34 bool last; /* indicate the last scan pending */
37 #define MAX_PENDING_SCAN_RESULTS 256
39 static int pending_scan_results_length;
40 static struct pending_scan_result
41 pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
43 /* Queue command functions */
44 static void vsllink_end_state(tap_state_t state);
45 static void vsllink_state_move(void);
46 static void vsllink_path_move(int num_states, tap_state_t *path);
47 static void vsllink_tms(int num_bits, const uint8_t *bits);
48 static void vsllink_runtest(int num_cycles);
49 static void vsllink_stableclocks(int num_cycles, int tms);
50 static void vsllink_scan(bool ir_scan, enum scan_type type,
51 uint8_t *buffer, int scan_size, struct scan_command *command);
52 static int vsllink_reset(int trst, int srst);
54 /* VSLLink tap buffer functions */
55 static void vsllink_tap_append_step(int tms, int tdi);
56 static void vsllink_tap_init(void);
57 static int vsllink_tap_execute(void);
58 static void vsllink_tap_ensure_pending(int scans);
59 static void vsllink_tap_append_scan(int length, uint8_t *buffer,
60 struct scan_command *command);
62 /* VSLLink SWD functions */
63 static int_least32_t vsllink_swd_frequency(int_least32_t hz);
64 static int vsllink_swd_switch_seq(enum swd_special_seq seq);
66 /* VSLLink lowlevel functions */
67 struct vsllink {
68 struct libusb_context *libusb_ctx;
69 struct libusb_device_handle *usb_device_handle;
72 static int vsllink_usb_open(struct vsllink *vsllink);
73 static void vsllink_usb_close(struct vsllink *vsllink);
75 static void vsllink_debug_buffer(uint8_t *buffer, int length);
77 static int tap_length;
78 static int tap_buffer_size;
79 static uint8_t *tms_buffer;
80 static uint8_t *tdi_buffer;
81 static uint8_t *tdo_buffer;
83 static bool swd_mode;
85 static struct vsllink *vsllink_handle;
87 static int vsllink_execute_queue(struct jtag_command *cmd_queue)
89 struct jtag_command *cmd = cmd_queue;
90 int scan_size;
91 enum scan_type type;
92 uint8_t *buffer;
94 LOG_DEBUG_IO("-------------------------------------"
95 " vsllink "
96 "-------------------------------------");
98 while (cmd) {
99 switch (cmd->type) {
100 case JTAG_RUNTEST:
101 LOG_DEBUG_IO("runtest %i cycles, end in %s",
102 cmd->cmd.runtest->num_cycles,
103 tap_state_name(cmd->cmd.runtest->end_state));
105 vsllink_end_state(cmd->cmd.runtest->end_state);
106 vsllink_runtest(cmd->cmd.runtest->num_cycles);
107 break;
109 case JTAG_TLR_RESET:
110 LOG_DEBUG_IO("statemove end in %s",
111 tap_state_name(cmd->cmd.statemove->end_state));
113 vsllink_end_state(cmd->cmd.statemove->end_state);
114 vsllink_state_move();
115 break;
117 case JTAG_PATHMOVE:
118 LOG_DEBUG_IO("pathmove: %i states, end in %s",
119 cmd->cmd.pathmove->num_states,
120 tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
122 vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
123 break;
125 case JTAG_SCAN:
126 LOG_DEBUG_IO("JTAG Scan...");
128 vsllink_end_state(cmd->cmd.scan->end_state);
130 scan_size = jtag_build_buffer(
131 cmd->cmd.scan, &buffer);
133 if (cmd->cmd.scan->ir_scan)
134 LOG_DEBUG_IO(
135 "JTAG Scan write IR(%d bits), "
136 "end in %s:",
137 scan_size,
138 tap_state_name(cmd->cmd.scan->end_state));
140 else
141 LOG_DEBUG_IO(
142 "JTAG Scan write DR(%d bits), "
143 "end in %s:",
144 scan_size,
145 tap_state_name(cmd->cmd.scan->end_state));
147 if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO))
148 vsllink_debug_buffer(buffer, DIV_ROUND_UP(scan_size, 8));
150 type = jtag_scan_type(cmd->cmd.scan);
152 vsllink_scan(cmd->cmd.scan->ir_scan,
153 type, buffer, scan_size,
154 cmd->cmd.scan);
155 break;
157 case JTAG_SLEEP:
158 LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
159 vsllink_tap_execute();
160 jtag_sleep(cmd->cmd.sleep->us);
161 break;
163 case JTAG_STABLECLOCKS:
164 LOG_DEBUG_IO("add %d clocks",
165 cmd->cmd.stableclocks->num_cycles);
167 switch (tap_get_state()) {
168 case TAP_RESET:
169 /* tms must be '1' to stay
170 * n TAP_RESET mode
172 scan_size = 1;
173 break;
174 case TAP_DRSHIFT:
175 case TAP_IDLE:
176 case TAP_DRPAUSE:
177 case TAP_IRSHIFT:
178 case TAP_IRPAUSE:
179 /* else, tms should be '0' */
180 scan_size = 0;
181 break;
182 /* above stable states are OK */
183 default:
184 LOG_ERROR("jtag_add_clocks() "
185 "in non-stable state \"%s\"",
186 tap_state_name(tap_get_state())
188 exit(-1);
190 vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size);
191 break;
193 case JTAG_TMS:
194 LOG_DEBUG_IO("add %d jtag tms",
195 cmd->cmd.tms->num_bits);
197 vsllink_tms(cmd->cmd.tms->num_bits, cmd->cmd.tms->bits);
198 break;
200 default:
201 LOG_ERROR("BUG: unknown JTAG command type "
202 "encountered: %d", cmd->type);
203 exit(-1);
205 cmd = cmd->next;
208 return vsllink_tap_execute();
211 static int vsllink_speed(int speed)
213 if (swd_mode) {
214 vsllink_swd_frequency(speed * 1000);
215 return ERROR_OK;
218 versaloon_interface.adaptors.jtag_raw.config(0, (uint16_t)speed);
219 return versaloon_interface.adaptors.peripheral_commit();
222 static int vsllink_khz(int khz, int *jtag_speed)
224 *jtag_speed = khz;
226 return ERROR_OK;
229 static int vsllink_speed_div(int jtag_speed, int *khz)
231 *khz = jtag_speed;
233 return ERROR_OK;
236 static void vsllink_free_buffer(void)
238 free(tdi_buffer);
239 tdi_buffer = NULL;
241 free(tdo_buffer);
242 tdo_buffer = NULL;
244 free(tms_buffer);
245 tms_buffer = NULL;
248 static int vsllink_quit(void)
250 versaloon_interface.adaptors.gpio.config(0, GPIO_SRST | GPIO_TRST,
251 0, 0, GPIO_SRST | GPIO_TRST);
252 versaloon_interface.adaptors.gpio.fini(0);
254 if (swd_mode)
255 versaloon_interface.adaptors.swd.fini(0);
256 else
257 versaloon_interface.adaptors.jtag_raw.fini(0);
259 versaloon_interface.adaptors.peripheral_commit();
260 versaloon_interface.fini();
262 vsllink_free_buffer();
263 vsllink_usb_close(vsllink_handle);
265 libusb_exit(vsllink_handle->libusb_ctx);
266 free(vsllink_handle);
268 return ERROR_OK;
271 static int vsllink_interface_init(void)
273 vsllink_handle = malloc(sizeof(struct vsllink));
274 if (!vsllink_handle) {
275 LOG_ERROR("unable to allocate memory");
276 return ERROR_FAIL;
279 libusb_init(&vsllink_handle->libusb_ctx);
281 if (vsllink_usb_open(vsllink_handle) != ERROR_OK) {
282 LOG_ERROR("Can't find USB JTAG Interface!"
283 "Please check connection and permissions.");
284 return ERROR_JTAG_INIT_FAILED;
286 LOG_DEBUG("vsllink found on %04X:%04X",
287 versaloon_interface.usb_setting.vid,
288 versaloon_interface.usb_setting.pid);
289 versaloon_usb_device_handle = vsllink_handle->usb_device_handle;
291 if (versaloon_interface.init() != ERROR_OK)
292 return ERROR_FAIL;
293 if (versaloon_interface.usb_setting.buf_size < 32) {
294 versaloon_interface.fini();
295 return ERROR_FAIL;
298 return ERROR_OK;
301 static int vsllink_init(void)
303 int retval = vsllink_interface_init();
304 if (retval != ERROR_OK)
305 return retval;
307 versaloon_interface.adaptors.gpio.init(0);
308 versaloon_interface.adaptors.gpio.config(0, GPIO_SRST, 0, GPIO_SRST,
309 GPIO_SRST);
310 versaloon_interface.adaptors.delay.delayms(100);
311 versaloon_interface.adaptors.peripheral_commit();
313 if (swd_mode) {
314 versaloon_interface.adaptors.gpio.config(0, GPIO_TRST, 0,
315 GPIO_TRST, GPIO_TRST);
316 versaloon_interface.adaptors.swd.init(0);
317 vsllink_swd_frequency(adapter_get_speed_khz() * 1000);
318 vsllink_swd_switch_seq(JTAG_TO_SWD);
320 } else {
321 /* malloc buffer size for tap */
322 tap_buffer_size = versaloon_interface.usb_setting.buf_size / 2 - 32;
323 vsllink_free_buffer();
324 tdi_buffer = malloc(tap_buffer_size);
325 tdo_buffer = malloc(tap_buffer_size);
326 tms_buffer = malloc(tap_buffer_size);
327 if ((!tdi_buffer) || (!tdo_buffer) || (!tms_buffer)) {
328 vsllink_quit();
329 return ERROR_FAIL;
332 versaloon_interface.adaptors.jtag_raw.init(0);
333 versaloon_interface.adaptors.jtag_raw.config(0, adapter_get_speed_khz());
334 versaloon_interface.adaptors.gpio.config(0, GPIO_SRST | GPIO_TRST,
335 GPIO_TRST, GPIO_SRST, GPIO_SRST);
338 if (versaloon_interface.adaptors.peripheral_commit() != ERROR_OK)
339 return ERROR_FAIL;
341 vsllink_reset(0, 0);
342 vsllink_tap_init();
343 return ERROR_OK;
346 /**************************************************************************
347 * Queue command implementations */
349 static void vsllink_end_state(tap_state_t state)
351 if (tap_is_state_stable(state))
352 tap_set_end_state(state);
353 else {
354 LOG_ERROR("BUG: %i is not a valid end state", state);
355 exit(-1);
359 /* Goes to the end state. */
360 static void vsllink_state_move(void)
362 int i;
363 uint8_t tms_scan = tap_get_tms_path(tap_get_state(),
364 tap_get_end_state());
365 uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(),
366 tap_get_end_state());
368 for (i = 0; i < tms_scan_bits; i++)
369 vsllink_tap_append_step((tms_scan >> i) & 1, 0);
371 tap_set_state(tap_get_end_state());
374 static void vsllink_path_move(int num_states, tap_state_t *path)
376 for (int i = 0; i < num_states; i++) {
377 if (path[i] == tap_state_transition(tap_get_state(), false))
378 vsllink_tap_append_step(0, 0);
379 else if (path[i] == tap_state_transition(tap_get_state(), true))
380 vsllink_tap_append_step(1, 0);
381 else {
382 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
383 tap_state_name(tap_get_state()),
384 tap_state_name(path[i]));
385 exit(-1);
388 tap_set_state(path[i]);
391 tap_set_end_state(tap_get_state());
394 static void vsllink_tms(int num_bits, const uint8_t *bits)
396 for (int i = 0; i < num_bits; i++)
397 vsllink_tap_append_step((bits[i / 8] >> (i % 8)) & 1, 0);
400 static void vsllink_stableclocks(int num_cycles, int tms)
402 while (num_cycles > 0) {
403 vsllink_tap_append_step(tms, 0);
404 num_cycles--;
408 static void vsllink_runtest(int num_cycles)
410 tap_state_t saved_end_state = tap_get_end_state();
412 if (tap_get_state() != TAP_IDLE) {
413 /* enter IDLE state */
414 vsllink_end_state(TAP_IDLE);
415 vsllink_state_move();
418 vsllink_stableclocks(num_cycles, 0);
420 /* post-process */
421 /* set end_state */
422 vsllink_end_state(saved_end_state);
423 if (tap_get_end_state() != tap_get_end_state())
424 vsllink_state_move();
427 static void vsllink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer,
428 int scan_size, struct scan_command *command)
430 tap_state_t saved_end_state;
432 saved_end_state = tap_get_end_state();
434 /* Move to appropriate scan state */
435 vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
437 if (tap_get_state() != tap_get_end_state())
438 vsllink_state_move();
439 vsllink_end_state(saved_end_state);
441 /* Scan */
442 vsllink_tap_append_scan(scan_size, buffer, command);
444 /* Goto Pause and record position to insert tms:0 */
445 vsllink_tap_append_step(0, 0);
446 vsllink_tms_offset = tap_length;
448 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
450 if (tap_get_state() != tap_get_end_state())
451 vsllink_state_move();
454 static int vsllink_reset(int trst, int srst)
456 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
458 if (!srst)
459 versaloon_interface.adaptors.gpio.config(0, GPIO_SRST, 0, GPIO_SRST, GPIO_SRST);
460 else
461 versaloon_interface.adaptors.gpio.config(0, GPIO_SRST, GPIO_SRST, 0, 0);
463 if (!swd_mode) {
464 if (!trst)
465 versaloon_interface.adaptors.gpio.out(0, GPIO_TRST, GPIO_TRST);
466 else
467 versaloon_interface.adaptors.gpio.out(0, GPIO_TRST, 0);
470 return versaloon_interface.adaptors.peripheral_commit();
473 COMMAND_HANDLER(vsllink_handle_usb_vid_command)
475 if (CMD_ARGC != 1)
476 return ERROR_COMMAND_SYNTAX_ERROR;
478 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0],
479 versaloon_interface.usb_setting.vid);
480 return ERROR_OK;
483 COMMAND_HANDLER(vsllink_handle_usb_pid_command)
485 if (CMD_ARGC != 1)
486 return ERROR_COMMAND_SYNTAX_ERROR;
487 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0],
488 versaloon_interface.usb_setting.pid);
489 return ERROR_OK;
492 COMMAND_HANDLER(vsllink_handle_usb_bulkin_command)
494 if (CMD_ARGC != 1)
495 return ERROR_COMMAND_SYNTAX_ERROR;
497 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
498 versaloon_interface.usb_setting.ep_in);
500 versaloon_interface.usb_setting.ep_in |= 0x80;
502 return ERROR_OK;
505 COMMAND_HANDLER(vsllink_handle_usb_bulkout_command)
507 if (CMD_ARGC != 1)
508 return ERROR_COMMAND_SYNTAX_ERROR;
510 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
511 versaloon_interface.usb_setting.ep_out);
513 versaloon_interface.usb_setting.ep_out &= ~0x80;
515 return ERROR_OK;
518 COMMAND_HANDLER(vsllink_handle_usb_interface_command)
520 if (CMD_ARGC != 1)
521 return ERROR_COMMAND_SYNTAX_ERROR;
523 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
524 versaloon_interface.usb_setting.interface);
525 return ERROR_OK;
528 /**************************************************************************
529 * VSLLink tap functions */
531 static void vsllink_tap_init(void)
533 tap_length = 0;
534 pending_scan_results_length = 0;
535 vsllink_tms_offset = 0;
538 static void vsllink_tap_ensure_pending(int scans)
540 int available_scans =
541 MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
543 if (scans > available_scans)
544 vsllink_tap_execute();
547 static void vsllink_tap_append_step(int tms, int tdi)
549 int index_var = tap_length / 8;
551 int bit_index = tap_length % 8;
552 uint8_t bit = 1 << bit_index;
554 if (tms)
555 tms_buffer[index_var] |= bit;
556 else
557 tms_buffer[index_var] &= ~bit;
559 if (tdi)
560 tdi_buffer[index_var] |= bit;
561 else
562 tdi_buffer[index_var] &= ~bit;
564 tap_length++;
566 if (tap_buffer_size * 8 <= tap_length)
567 vsllink_tap_execute();
570 static void vsllink_tap_append_scan(int length, uint8_t *buffer,
571 struct scan_command *command)
573 struct pending_scan_result *pending_scan_result;
574 int len_tmp, len_all, i;
576 len_all = 0;
577 while (len_all < length) {
578 vsllink_tap_ensure_pending(1);
579 pending_scan_result =
580 &pending_scan_results_buffer[
581 pending_scan_results_length];
583 if ((length - len_all) > (tap_buffer_size * 8 - tap_length)) {
584 /* Use all memory available
585 vsllink_tap_append_step will commit automatically */
586 len_tmp = tap_buffer_size * 8 - tap_length;
587 pending_scan_result->last = false;
588 } else {
589 len_tmp = length - len_all;
590 pending_scan_result->last = true;
592 pending_scan_result->src_offset = tap_length;
593 pending_scan_result->dest_offset = len_all;
594 pending_scan_result->length = len_tmp;
595 pending_scan_result->command = command;
596 pending_scan_result->buffer = buffer;
597 pending_scan_results_length++;
599 for (i = 0; i < len_tmp; i++) {
600 vsllink_tap_append_step(((len_all + i) < length-1
601 ? 0 : 1),
602 (buffer[(len_all + i)/8]
603 >> ((len_all + i)%8)) & 1);
606 len_all += len_tmp;
610 static int vsllink_jtag_execute(void)
612 int i;
613 int result;
615 if (tap_length <= 0)
616 return ERROR_OK;
618 versaloon_interface.adaptors.jtag_raw.execute(0, tdi_buffer, tms_buffer,
619 tdo_buffer, tap_length);
621 result = versaloon_interface.adaptors.peripheral_commit();
623 if (result == ERROR_OK) {
624 for (i = 0; i < pending_scan_results_length; i++) {
625 struct pending_scan_result *pending_scan_result =
626 &pending_scan_results_buffer[i];
627 uint8_t *buffer = pending_scan_result->buffer;
628 int length = pending_scan_result->length;
629 int src_first = pending_scan_result->src_offset;
630 int dest_first = pending_scan_result->dest_offset;
631 bool last = pending_scan_result->last;
633 struct scan_command *command;
635 command = pending_scan_result->command;
636 buf_set_buf(tdo_buffer, src_first, buffer, dest_first, length);
638 LOG_DEBUG_IO(
639 "JTAG scan read(%d bits, from src %d bits to dest %d bits):",
640 length, src_first, dest_first);
641 if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO))
642 vsllink_debug_buffer(buffer + dest_first / 8, DIV_ROUND_UP(length, 7));
644 if (last) {
645 if (jtag_read_buffer(buffer, command)
646 != ERROR_OK) {
647 vsllink_tap_init();
648 return ERROR_JTAG_QUEUE_FAILED;
651 free(pending_scan_result->buffer);
654 } else {
655 LOG_ERROR("vsllink_jtag_execute failure");
656 return ERROR_JTAG_QUEUE_FAILED;
659 vsllink_tap_init();
661 return ERROR_OK;
664 static int vsllink_tap_execute(void)
666 if (swd_mode)
667 return ERROR_OK;
669 return vsllink_jtag_execute();
672 static int vsllink_swd_init(void)
674 LOG_INFO("VSLLink SWD mode enabled");
675 swd_mode = true;
677 return ERROR_OK;
680 static int_least32_t vsllink_swd_frequency(int_least32_t hz)
682 const int_least32_t delay2hz[] = {
683 1850000, 235000, 130000, 102000, 85000, 72000
686 if (hz > 0) {
687 uint16_t delay = UINT16_MAX;
689 for (uint16_t i = 0; i < ARRAY_SIZE(delay2hz); i++) {
690 if (hz >= delay2hz[i]) {
691 hz = delay2hz[i];
692 delay = i;
693 break;
697 if (delay == UINT16_MAX)
698 delay = (500000 / hz) - 1;
700 /* Calculate retry count after a WAIT response. This will give
701 * a retry timeout at about ~250 ms. 54 is the number of bits
702 * found in a transaction. */
703 uint16_t retry_count = 250 * hz / 1000 / 54;
705 LOG_DEBUG("SWD delay: %d, retry count: %d", delay, retry_count);
707 versaloon_interface.adaptors.swd.config(0, 2, retry_count, delay);
710 return hz;
713 static int vsllink_swd_switch_seq(enum swd_special_seq seq)
715 switch (seq) {
716 case LINE_RESET:
717 LOG_DEBUG("SWD line reset");
718 versaloon_interface.adaptors.swd.seqout(0, swd_seq_line_reset,
719 swd_seq_line_reset_len);
720 break;
721 case JTAG_TO_SWD:
722 LOG_DEBUG("JTAG-to-SWD");
723 versaloon_interface.adaptors.swd.seqout(0, swd_seq_jtag_to_swd,
724 swd_seq_jtag_to_swd_len);
725 break;
726 case SWD_TO_JTAG:
727 LOG_DEBUG("SWD-to-JTAG");
728 versaloon_interface.adaptors.swd.seqout(0, swd_seq_swd_to_jtag,
729 swd_seq_swd_to_jtag_len);
730 break;
731 default:
732 LOG_ERROR("Sequence %d not supported", seq);
733 return ERROR_FAIL;
736 return ERROR_OK;
739 static void vsllink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
741 versaloon_interface.adaptors.swd.transact(0, cmd, value, NULL);
744 static void vsllink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
746 versaloon_interface.adaptors.swd.transact(0, cmd, &value, NULL);
749 static int vsllink_swd_run_queue(void)
751 return versaloon_interface.adaptors.peripheral_commit();
754 /****************************************************************************
755 * VSLLink USB low-level functions */
757 static int vsllink_check_usb_strings(
758 struct libusb_device_handle *usb_device_handle,
759 struct libusb_device_descriptor *usb_desc)
761 char desc_string[256];
762 int retval;
764 if (adapter_get_required_serial()) {
765 retval = libusb_get_string_descriptor_ascii(usb_device_handle,
766 usb_desc->iSerialNumber, (unsigned char *)desc_string,
767 sizeof(desc_string));
768 if (retval < 0)
769 return ERROR_FAIL;
771 if (strncmp(desc_string, adapter_get_required_serial(),
772 sizeof(desc_string)))
773 return ERROR_FAIL;
776 retval = libusb_get_string_descriptor_ascii(usb_device_handle,
777 usb_desc->iProduct, (unsigned char *)desc_string,
778 sizeof(desc_string));
779 if (retval < 0)
780 return ERROR_FAIL;
782 if (!strstr(desc_string, "Versaloon"))
783 return ERROR_FAIL;
785 return ERROR_OK;
788 static int vsllink_usb_open(struct vsllink *vsllink)
790 ssize_t num_devices, i;
791 struct libusb_device **usb_devices;
792 struct libusb_device_descriptor usb_desc;
793 struct libusb_device_handle *usb_device_handle;
794 int retval;
796 num_devices = libusb_get_device_list(vsllink->libusb_ctx, &usb_devices);
798 if (num_devices <= 0)
799 return ERROR_FAIL;
801 for (i = 0; i < num_devices; i++) {
802 struct libusb_device *device = usb_devices[i];
804 retval = libusb_get_device_descriptor(device, &usb_desc);
805 if (retval != 0)
806 continue;
808 if (usb_desc.idVendor != versaloon_interface.usb_setting.vid ||
809 usb_desc.idProduct != versaloon_interface.usb_setting.pid)
810 continue;
812 retval = libusb_open(device, &usb_device_handle);
813 if (retval != 0)
814 continue;
816 retval = vsllink_check_usb_strings(usb_device_handle, &usb_desc);
817 if (retval == ERROR_OK)
818 break;
820 libusb_close(usb_device_handle);
823 libusb_free_device_list(usb_devices, 1);
825 if (i == num_devices)
826 return ERROR_FAIL;
828 retval = libusb_claim_interface(usb_device_handle,
829 versaloon_interface.usb_setting.interface);
830 if (retval != 0) {
831 LOG_ERROR("unable to claim interface");
832 libusb_close(usb_device_handle);
833 return ERROR_FAIL;
836 vsllink->usb_device_handle = usb_device_handle;
837 return ERROR_OK;
840 static void vsllink_usb_close(struct vsllink *vsllink)
842 libusb_release_interface(vsllink->usb_device_handle,
843 versaloon_interface.usb_setting.interface);
844 libusb_close(vsllink->usb_device_handle);
847 #define BYTES_PER_LINE 16
849 static void vsllink_debug_buffer(uint8_t *buffer, int length)
851 char line[81];
852 char s[4];
853 int i;
854 int j;
856 for (i = 0; i < length; i += BYTES_PER_LINE) {
857 snprintf(line, 5, "%04x", i & 0xffff);
858 for (j = i; j < i + BYTES_PER_LINE && j < length; j++) {
859 snprintf(s, 4, " %02x", buffer[j]);
860 strcat(line, s);
862 LOG_DEBUG_IO("%s", line);
866 static const struct command_registration vsllink_subcommand_handlers[] = {
868 .name = "usb_vid",
869 .handler = &vsllink_handle_usb_vid_command,
870 .mode = COMMAND_CONFIG,
871 .help = "Set USB VID",
872 .usage = "<vid>",
875 .name = "usb_pid",
876 .handler = &vsllink_handle_usb_pid_command,
877 .mode = COMMAND_CONFIG,
878 .help = "Set USB PID",
879 .usage = "<pid>",
882 .name = "usb_bulkin",
883 .handler = &vsllink_handle_usb_bulkin_command,
884 .mode = COMMAND_CONFIG,
885 .help = "Set USB input endpoint",
886 .usage = "<ep_in>",
889 .name = "usb_bulkout",
890 .handler = &vsllink_handle_usb_bulkout_command,
891 .mode = COMMAND_CONFIG,
892 .help = "Set USB output endpoint",
893 .usage = "<ep_out>",
896 .name = "usb_interface",
897 .handler = &vsllink_handle_usb_interface_command,
898 .mode = COMMAND_CONFIG,
899 .help = "Set USB output interface",
900 .usage = "<interface>",
902 COMMAND_REGISTRATION_DONE
905 static const struct command_registration vsllink_command_handlers[] = {
907 .name = "vsllink",
908 .mode = COMMAND_ANY,
909 .help = "perform vsllink management",
910 .chain = vsllink_subcommand_handlers,
911 .usage = "",
913 COMMAND_REGISTRATION_DONE
916 static const char * const vsllink_transports[] = {"jtag", "swd", NULL};
918 static const struct swd_driver vsllink_swd_driver = {
919 .init = vsllink_swd_init,
920 .switch_seq = vsllink_swd_switch_seq,
921 .read_reg = vsllink_swd_read_reg,
922 .write_reg = vsllink_swd_write_reg,
923 .run = vsllink_swd_run_queue,
926 static struct jtag_interface vsllink_interface = {
927 .supported = DEBUG_CAP_TMS_SEQ,
928 .execute_queue = vsllink_execute_queue,
931 struct adapter_driver vsllink_adapter_driver = {
932 .name = "vsllink",
933 .transports = vsllink_transports,
934 .commands = vsllink_command_handlers,
936 .init = vsllink_init,
937 .quit = vsllink_quit,
938 .reset = vsllink_reset,
939 .speed = vsllink_speed,
940 .khz = vsllink_khz,
941 .speed_div = vsllink_speed_div,
943 .jtag_ops = &vsllink_interface,
944 .swd_ops = &vsllink_swd_driver,