This is an automated email from Gerrit.

Oleksij Rempel ([email protected]) just uploaded a new patch set to 
Gerrit, which you can find at http://openocd.zylin.com/4580

-- gerrit

commit 7c66eca7c814ab5567826de2caa1c381a8234b64
Author: Oleksij Rempel <[email protected]>
Date:   Wed Jun 27 14:54:21 2018 +0200

    jtag: drivers: provide initial support for usb path filtering
    
    With this patch drivers will be able to use usb path filtering.
    The path format is identical to the format provided by linux kernel:
    bus-port.port....
    With this format it should be easier just to copy and paste
    path found in dmesg.
    
    Change-Id: I8bafa6fcb7a66ff68cc961a376f97f4f3dee35aa
    Signed-off-by: Oleksij Rempel <[email protected]>

diff --git a/src/jtag/aice/aice_usb.c b/src/jtag/aice/aice_usb.c
index 50468f3..69660a6 100644
--- a/src/jtag/aice/aice_usb.c
+++ b/src/jtag/aice/aice_usb.c
@@ -2097,7 +2097,7 @@ static int aice_usb_open(struct aice_port_param_s *param)
        const uint16_t pids[] = { param->pid, 0 };
        struct jtag_libusb_device_handle *devh;
 
-       if (jtag_libusb_open(vids, pids, NULL, &devh) != ERROR_OK)
+       if (jtag_libusb_open(vids, pids, NULL, NULL, &devh) != ERROR_OK)
                return ERROR_FAIL;
 
        /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
@@ -2121,7 +2121,7 @@ static int aice_usb_open(struct aice_port_param_s *param)
        /* reopen jlink after usb_reset
         * on win32 this may take a second or two to re-enumerate */
        int retval;
-       while ((retval = jtag_libusb_open(vids, pids, NULL, &devh)) != 
ERROR_OK) {
+       while ((retval = jtag_libusb_open(vids, pids, NULL, NULL, &devh)) != 
ERROR_OK) {
                usleep(1000);
                timeout--;
                if (!timeout)
diff --git a/src/jtag/drivers/ft232r.c b/src/jtag/drivers/ft232r.c
index fc3d75f..36329df 100644
--- a/src/jtag/drivers/ft232r.c
+++ b/src/jtag/drivers/ft232r.c
@@ -230,7 +230,7 @@ static int ft232r_init(void)
 {
        uint16_t avids[] = {ft232r_vid, 0};
        uint16_t apids[] = {ft232r_pid, 0};
-       if (jtag_libusb_open(avids, apids, ft232r_serial_desc, &adapter)) {
+       if (jtag_libusb_open(avids, apids, ft232r_serial_desc, NULL, &adapter)) 
{
                LOG_ERROR("ft232r not found: vid=%04x, pid=%04x, serial=%s\n",
                        ft232r_vid, ft232r_pid, (ft232r_serial_desc == NULL) ? 
"[any]" : ft232r_serial_desc);
                return ERROR_JTAG_INIT_FAILED;
diff --git a/src/jtag/drivers/kitprog.c b/src/jtag/drivers/kitprog.c
index e3ad84d..d9583bc 100644
--- a/src/jtag/drivers/kitprog.c
+++ b/src/jtag/drivers/kitprog.c
@@ -279,7 +279,7 @@ static int kitprog_usb_open(void)
        const uint16_t vids[] = { VID, 0 };
        const uint16_t pids[] = { PID, 0 };
 
-       if (jtag_libusb_open(vids, pids, kitprog_serial,
+       if (jtag_libusb_open(vids, pids, kitprog_serial, NULL,
                        &kitprog_handle->usb_handle) != ERROR_OK) {
                LOG_ERROR("Failed to open or find the device");
                return ERROR_FAIL;
diff --git a/src/jtag/drivers/libusb0_common.c 
b/src/jtag/drivers/libusb0_common.c
index 1825543..d487cc1 100644
--- a/src/jtag/drivers/libusb0_common.c
+++ b/src/jtag/drivers/libusb0_common.c
@@ -64,7 +64,7 @@ static bool string_descriptor_equal(usb_dev_handle *device, 
uint8_t str_index,
 }
 
 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
-               const char *serial,
+               const char *serial, const char *location,
                struct jtag_libusb_device_handle **out)
 {
        int retval = -ENODEV;
diff --git a/src/jtag/drivers/libusb0_common.h 
b/src/jtag/drivers/libusb0_common.h
index baa9e3c..014ecd8 100644
--- a/src/jtag/drivers/libusb0_common.h
+++ b/src/jtag/drivers/libusb0_common.h
@@ -52,7 +52,7 @@ static inline int 
jtag_libusb_release_interface(jtag_libusb_device_handle *devh,
 }
 
 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
-               const char *serial,
+               const char *serial, const char *location,
                struct jtag_libusb_device_handle **out);
 void jtag_libusb_close(jtag_libusb_device_handle *dev);
 int jtag_libusb_control_transfer(jtag_libusb_device_handle *dev,
diff --git a/src/jtag/drivers/libusb1_common.c 
b/src/jtag/drivers/libusb1_common.c
index 89f8092..9af5f37 100644
--- a/src/jtag/drivers/libusb1_common.c
+++ b/src/jtag/drivers/libusb1_common.c
@@ -23,6 +23,12 @@
 #include "log.h"
 #include "libusb1_common.h"
 
+/*
+ * comment from libusb:
+ * As per the USB 3.0 specs, the current maximum limit for the depth is 7.
+ */
+#define MAX_USB_PORTS  7
+
 static struct libusb_context *jtag_libusb_context; /**< Libusb context **/
 static libusb_device **devs; /**< The usb device list **/
 
@@ -38,6 +44,61 @@ static bool jtag_libusb_match(struct 
libusb_device_descriptor *dev_desc,
        return false;
 }
 
+static int jtag_libusb_location_equal(libusb_device *device, const char 
*location)
+{
+       uint8_t port_path[MAX_USB_PORTS];
+       uint8_t dev_bus;
+       int path_step, path_len;
+       int result = 0;
+       char *ptr, *loc;
+
+       /* strtok need non const char */
+       loc = strdup(location);
+
+       path_len = libusb_get_port_numbers(device, port_path, MAX_USB_PORTS);
+       if (path_len == LIBUSB_ERROR_OVERFLOW) {
+               fprintf(stderr, "cannot determine path to usb device! (more 
than %i ports in path)\n",
+                       MAX_USB_PORTS);
+               goto done;
+       }
+
+       ptr = strtok(loc, "-");
+       if (ptr == NULL) {
+               printf("no '-' in path\n");
+               goto done;
+       }
+
+       dev_bus = libusb_get_bus_number(device);
+       /* check bus mismatch */
+       if (atoi(ptr) != dev_bus)
+               goto done;
+
+       path_step = 0;
+       while (path_step < MAX_USB_PORTS) {
+               ptr = strtok(NULL, ".");
+
+               /* no more tokens in path */
+               if (ptr == NULL)
+                       break;
+
+               /* path mismatch at some step */
+               if (path_step < path_len && atoi(ptr) != port_path[path_step])
+                       break;
+
+               path_step++;
+       };
+
+       /* walked the full path, all elements match */
+       if (path_step == path_len)
+               result = 1;
+       else
+               fprintf(stderr, " excluded by device path option\n");
+
+done:
+       free(loc);
+       return result;
+}
+
 /* Returns true if the string descriptor indexed by str_index in device 
matches string */
 static bool string_descriptor_equal(libusb_device_handle *device, uint8_t 
str_index,
                                                                        const 
char *string)
@@ -67,7 +128,7 @@ static bool string_descriptor_equal(libusb_device_handle 
*device, uint8_t str_in
 }
 
 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
-               const char *serial,
+               const char *serial, const char *location,
                struct jtag_libusb_device_handle **out)
 {
        int cnt, idx, errCode;
@@ -103,6 +164,11 @@ int jtag_libusb_open(const uint16_t vids[], const uint16_t 
pids[],
                        continue;
                }
 
+               if (location && !jtag_libusb_location_equal(devs[idx], 
location)) {
+                       libusb_close(libusb_handle);
+                       continue;
+               }
+
                /* Success. */
                *out = libusb_handle;
                retval = 0;
diff --git a/src/jtag/drivers/libusb1_common.h 
b/src/jtag/drivers/libusb1_common.h
index 7c73d29..cbb6756 100644
--- a/src/jtag/drivers/libusb1_common.h
+++ b/src/jtag/drivers/libusb1_common.h
@@ -46,7 +46,7 @@ static inline int 
jtag_libusb_release_interface(jtag_libusb_device_handle *devh,
 }
 
 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
-               const char *serial,
+               const char *serial, const char *location,
                struct jtag_libusb_device_handle **out);
 void jtag_libusb_close(jtag_libusb_device_handle *dev);
 int jtag_libusb_control_transfer(jtag_libusb_device_handle *dev,
diff --git a/src/jtag/drivers/opendous.c b/src/jtag/drivers/opendous.c
index 458df34..7a19e69 100644
--- a/src/jtag/drivers/opendous.c
+++ b/src/jtag/drivers/opendous.c
@@ -707,7 +707,7 @@ struct opendous_jtag *opendous_usb_open(void)
        struct opendous_jtag *result;
 
        struct jtag_libusb_device_handle *devh;
-       if (jtag_libusb_open(opendous_probe->VID, opendous_probe->PID, NULL, 
&devh) != ERROR_OK)
+       if (jtag_libusb_open(opendous_probe->VID, opendous_probe->PID, NULL, 
NULL, &devh) != ERROR_OK)
                return NULL;
 
        jtag_libusb_set_configuration(devh, 0);
diff --git a/src/jtag/drivers/openjtag.c b/src/jtag/drivers/openjtag.c
index 8f11b4b..b350803 100644
--- a/src/jtag/drivers/openjtag.c
+++ b/src/jtag/drivers/openjtag.c
@@ -451,7 +451,7 @@ static int openjtag_init_cy7c65215(void)
        int ret;
 
        usbh = NULL;
-       ret = jtag_libusb_open(cy7c65215_vids, cy7c65215_pids, NULL, &usbh);
+       ret = jtag_libusb_open(cy7c65215_vids, cy7c65215_pids, NULL, NULL, 
&usbh);
        if (ret != ERROR_OK) {
                LOG_ERROR("unable to open cy7c65215 device");
                goto err;
diff --git a/src/jtag/drivers/osbdm.c b/src/jtag/drivers/osbdm.c
index 5db36a1..899023a 100644
--- a/src/jtag/drivers/osbdm.c
+++ b/src/jtag/drivers/osbdm.c
@@ -373,7 +373,7 @@ static int osbdm_flush(struct osbdm *osbdm, struct queue* 
queue)
 static int osbdm_open(struct osbdm *osbdm)
 {
        (void)memset(osbdm, 0, sizeof(*osbdm));
-       if (jtag_libusb_open(osbdm_vid, osbdm_pid, NULL, &osbdm->devh) != 
ERROR_OK)
+       if (jtag_libusb_open(osbdm_vid, osbdm_pid, NULL, NULL, &osbdm->devh) != 
ERROR_OK)
                return ERROR_FAIL;
 
        if (jtag_libusb_claim_interface(osbdm->devh, 0) != ERROR_OK)
diff --git a/src/jtag/drivers/stlink_usb.c b/src/jtag/drivers/stlink_usb.c
index 99f96b9..2590662 100644
--- a/src/jtag/drivers/stlink_usb.c
+++ b/src/jtag/drivers/stlink_usb.c
@@ -2034,7 +2034,7 @@ static int stlink_usb_open(struct hl_interface_param_s 
*param, void **fd)
          in order to become operational.
         */
        do {
-               if (jtag_libusb_open(param->vid, param->pid, param->serial, 
&h->fd) != ERROR_OK) {
+               if (jtag_libusb_open(param->vid, param->pid, NULL, 
param->serial, &h->fd) != ERROR_OK) {
                        LOG_ERROR("open failed");
                        goto error_open;
                }
diff --git a/src/jtag/drivers/usb_blaster/ublast2_access_libusb.c 
b/src/jtag/drivers/usb_blaster/ublast2_access_libusb.c
index d991733..3875ce1 100644
--- a/src/jtag/drivers/usb_blaster/ublast2_access_libusb.c
+++ b/src/jtag/drivers/usb_blaster/ublast2_access_libusb.c
@@ -186,7 +186,7 @@ static int ublast2_libusb_init(struct ublast_lowlevel *low)
        bool renumeration = false;
        int ret;
 
-       if (jtag_libusb_open(vids, pids, NULL, &temp) == ERROR_OK) {
+       if (jtag_libusb_open(vids, pids, NULL, NULL, &temp) == ERROR_OK) {
                LOG_INFO("Altera USB-Blaster II (uninitialized) found");
                LOG_INFO("Loading firmware...");
                ret = load_usb_blaster_firmware(temp, low);
@@ -200,13 +200,13 @@ static int ublast2_libusb_init(struct ublast_lowlevel 
*low)
        const uint16_t pids_renum[] = { low->ublast_pid, 0 };
 
        if (renumeration == false) {
-               if (jtag_libusb_open(vids_renum, pids_renum, NULL, 
&low->libusb_dev) != ERROR_OK) {
+               if (jtag_libusb_open(vids_renum, pids_renum, NULL, NULL, 
&low->libusb_dev) != ERROR_OK) {
                        LOG_ERROR("Altera USB-Blaster II not found");
                        return ERROR_FAIL;
                }
        } else {
                int retry = 10;
-               while (jtag_libusb_open(vids_renum, pids_renum, NULL, 
&low->libusb_dev) != ERROR_OK && retry--) {
+               while (jtag_libusb_open(vids_renum, pids_renum, NULL, NULL, 
&low->libusb_dev) != ERROR_OK && retry--) {
                        usleep(1000000);
                        LOG_INFO("Waiting for renumerate...");
                }

-- 

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
OpenOCD-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openocd-devel

Reply via email to