jlink: add usb location support
[openocd.git] / src / jtag / drivers / jlink.c
index cc04953cd690d15fec1b6b3c0f9841f4111ad969..8ce7c0d22fda8a39e24961da5706292654160164 100644 (file)
@@ -38,6 +38,7 @@
 #include <jtag/interface.h>
 #include <jtag/swd.h>
 #include <jtag/commands.h>
 #include <jtag/interface.h>
 #include <jtag/swd.h>
 #include <jtag/commands.h>
+#include <jtag/drivers/jtag_usb_common.h>
 
 #include <libjaylink/libjaylink.h>
 
 
 #include <libjaylink/libjaylink.h>
 
@@ -50,6 +51,7 @@ static uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE];
 
 static uint32_t serial_number;
 static bool use_serial_number;
 
 static uint32_t serial_number;
 static bool use_serial_number;
+static bool use_usb_location;
 static enum jaylink_usb_address usb_address;
 static bool use_usb_address;
 static enum jaylink_target_interface iface = JAYLINK_TIF_JTAG;
 static enum jaylink_usb_address usb_address;
 static bool use_usb_address;
 static enum jaylink_target_interface iface = JAYLINK_TIF_JTAG;
@@ -534,6 +536,31 @@ static int jaylink_log_handler(const struct jaylink_context *ctx,
        return 0;
 }
 
        return 0;
 }
 
+static bool jlink_usb_location_equal(struct jaylink_device *dev)
+{
+       int retval;
+       uint8_t bus;
+       uint8_t *ports;
+       size_t num_ports;
+       bool equal = false;
+
+       retval = jaylink_device_get_usb_bus_ports(dev, &bus, &ports, &num_ports);
+
+       if (retval == JAYLINK_ERR_NOT_SUPPORTED) {
+               return false;
+       } else if (retval != JAYLINK_OK) {
+               LOG_WARNING("jaylink_device_get_usb_bus_ports() failed: %s.",
+                       jaylink_strerror(retval));
+               return false;
+       }
+
+       equal = jtag_usb_location_equal(bus, ports,     num_ports);
+       free(ports);
+
+       return equal;
+}
+
+
 static int jlink_init(void)
 {
        int ret;
 static int jlink_init(void)
 {
        int ret;
@@ -595,7 +622,9 @@ static int jlink_init(void)
                return ERROR_JTAG_INIT_FAILED;
        }
 
                return ERROR_JTAG_INIT_FAILED;
        }
 
-       if (!use_serial_number && !use_usb_address && num_devices > 1) {
+       use_usb_location = (jtag_usb_get_location() != NULL);
+
+       if (!use_serial_number && !use_usb_address && !use_usb_location && num_devices > 1) {
                LOG_ERROR("Multiple devices found, specify the desired device.");
                jaylink_free_devices(devs, true);
                jaylink_exit(jayctx);
                LOG_ERROR("Multiple devices found, specify the desired device.");
                jaylink_free_devices(devs, true);
                jaylink_exit(jayctx);
@@ -605,8 +634,10 @@ static int jlink_init(void)
        found_device = false;
 
        for (i = 0; devs[i]; i++) {
        found_device = false;
 
        for (i = 0; devs[i]; i++) {
+               struct jaylink_device *dev = devs[i];
+
                if (use_serial_number) {
                if (use_serial_number) {
-                       ret = jaylink_device_get_serial_number(devs[i], &tmp);
+                       ret = jaylink_device_get_serial_number(dev, &tmp);
 
                        if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
                                continue;
 
                        if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
                                continue;
@@ -621,7 +652,7 @@ static int jlink_init(void)
                }
 
                if (use_usb_address) {
                }
 
                if (use_usb_address) {
-                       ret = jaylink_device_get_usb_address(devs[i], &address);
+                       ret = jaylink_device_get_usb_address(dev, &address);
 
                        if (ret == JAYLINK_ERR_NOT_SUPPORTED) {
                                continue;
 
                        if (ret == JAYLINK_ERR_NOT_SUPPORTED) {
                                continue;
@@ -635,7 +666,10 @@ static int jlink_init(void)
                                continue;
                }
 
                                continue;
                }
 
-               ret = jaylink_open(devs[i], &devh);
+               if (use_usb_location && !jlink_usb_location_equal(dev))
+                       continue;
+
+               ret = jaylink_open(dev, &devh);
 
                if (ret == JAYLINK_OK) {
                        found_device = true;
 
                if (ret == JAYLINK_OK) {
                        found_device = true;

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)