]> git.gag.com Git - fw/openocd/commitdiff
jtag/libusb_helper: permit adapters to compute their custom serials
authorTarek BOCHKATI <tarek.bouchkati@gmail.com>
Fri, 6 Mar 2020 12:52:23 +0000 (13:52 +0100)
committerOleksij Rempel <linux@rempel-privat.de>
Sun, 22 Mar 2020 08:17:06 +0000 (08:17 +0000)
introduce a callback function that could be passed to jtag_libusb_open
to permit adapters to compute their custom/exotic serials.

the callback should return a non NULL value only when the serial could not
be retrieved by the standard 'libusb_get_string_descriptor_ascii'

Change-Id: Ib95d6bdfc4ee655eda538fba8becfa183c3b0059
Signed-off-by: Tarek BOCHKATI <tarek.bouchkati@gmail.com>
Reviewed-on: http://openocd.zylin.com/5496
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
Tested-by: jenkins
Reviewed-by: Oleksij Rempel <linux@rempel-privat.de>
src/jtag/aice/aice_usb.c
src/jtag/drivers/ft232r.c
src/jtag/drivers/kitprog.c
src/jtag/drivers/libusb_helper.c
src/jtag/drivers/libusb_helper.h
src/jtag/drivers/opendous.c
src/jtag/drivers/openjtag.c
src/jtag/drivers/osbdm.c
src/jtag/drivers/stlink_usb.c
src/jtag/drivers/usb_blaster/ublast2_access_libusb.c

index a5cccfef4c3786c91772aca3a4bf57b5a62ffdca..442754209399195b5fe4e40e6842627454e67a4d 100644 (file)
@@ -2111,7 +2111,7 @@ static int aice_usb_open(struct aice_port_param_s *param)
        const uint16_t pids[] = { param->pid, 0 };
        struct libusb_device_handle *devh;
 
-       if (jtag_libusb_open(vids, pids, NULL, &devh) != ERROR_OK)
+       if (jtag_libusb_open(vids, pids, NULL, &devh, NULL) != ERROR_OK)
                return ERROR_FAIL;
 
        /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
@@ -2135,7 +2135,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, &devh, NULL)) != ERROR_OK) {
                usleep(1000);
                timeout--;
                if (!timeout)
index 4812362a375ef2bc0cd86b5af97f8e77f7485a84..c9ed304a876b4d588c14ea100768b2366aa8a912 100644 (file)
@@ -257,7 +257,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, &adapter, NULL)) {
                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;
index 0c1e74c42cb65c03345f51e08a71c0db88ecfa76..28ed7ac619548204a55ab2a5d9be414a42a1ea2d 100644 (file)
@@ -280,7 +280,7 @@ static int kitprog_usb_open(void)
        const uint16_t pids[] = { PID, 0 };
 
        if (jtag_libusb_open(vids, pids, kitprog_serial,
-                       &kitprog_handle->usb_handle) != ERROR_OK) {
+                       &kitprog_handle->usb_handle, NULL) != ERROR_OK) {
                LOG_ERROR("Failed to open or find the device");
                return ERROR_FAIL;
        }
index 5a8129cb93d08b641f70f5ee8f972326824a5fc8..fbbfb4114bc07635d6203773012f2a78b2de6535 100644 (file)
@@ -58,7 +58,7 @@ static int jtag_libusb_error(int err)
        }
 }
 
-static bool jtag_libusb_match(struct libusb_device_descriptor *dev_desc,
+static bool jtag_libusb_match_ids(struct libusb_device_descriptor *dev_desc,
                const uint16_t vids[], const uint16_t pids[])
 {
        for (unsigned i = 0; vids[i]; i++) {
@@ -123,9 +123,40 @@ static bool string_descriptor_equal(libusb_device_handle *device, uint8_t str_in
        return matched;
 }
 
+static bool jtag_libusb_match_serial(libusb_device_handle *device,
+               struct libusb_device_descriptor *dev_desc, const char *serial,
+               adapter_get_alternate_serial_fn adapter_get_alternate_serial)
+{
+       if (string_descriptor_equal(device, dev_desc->iSerialNumber, serial))
+               return true;
+
+       /* check the alternate serial helper */
+       if (!adapter_get_alternate_serial)
+               return false;
+
+       /* get the alternate serial */
+       char *alternate_serial = adapter_get_alternate_serial(device, dev_desc);
+
+       /* check possible failures */
+       if (alternate_serial == NULL)
+               return false;
+
+       /* then compare and free the alternate serial */
+       bool match = false;
+       if (strcmp(serial, alternate_serial) == 0)
+               match = true;
+       else
+               LOG_DEBUG("Device alternate serial number '%s' doesn't match requested serial '%s'",
+                               alternate_serial, serial);
+
+       free(alternate_serial);
+       return match;
+}
+
 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
                const char *serial,
-               struct libusb_device_handle **out)
+               struct libusb_device_handle **out,
+               adapter_get_alternate_serial_fn adapter_get_alternate_serial)
 {
        int cnt, idx, errCode;
        int retval = ERROR_FAIL;
@@ -143,7 +174,7 @@ int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
                if (libusb_get_device_descriptor(devs[idx], &dev_desc) != 0)
                        continue;
 
-               if (!jtag_libusb_match(&dev_desc, vids, pids))
+               if (!jtag_libusb_match_ids(&dev_desc, vids, pids))
                        continue;
 
                if (jtag_usb_get_location() && !jtag_libusb_location_equal(devs[idx]))
@@ -159,7 +190,7 @@ int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
 
                /* Device must be open to use libusb_get_string_descriptor_ascii. */
                if (serial != NULL &&
-                               !string_descriptor_equal(libusb_handle, dev_desc.iSerialNumber, serial)) {
+                               !jtag_libusb_match_serial(libusb_handle, &dev_desc, serial, adapter_get_alternate_serial)) {
                        serial_mismatch = true;
                        libusb_close(libusb_handle);
                        continue;
index 46e4954e7895d282fe999ef28f0565fa6f9a22c4..74bb23c52eac91d5b11f38e876dc890040b393c6 100644 (file)
 
 #include <libusb.h>
 
+/* this callback should return a non NULL value only when the serial could not
+ * be retrieved by the standard 'libusb_get_string_descriptor_ascii' */
+typedef char * (*adapter_get_alternate_serial_fn)(libusb_device_handle *device,
+               struct libusb_device_descriptor *dev_desc);
+
 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
                const char *serial,
-               struct libusb_device_handle **out);
+               struct libusb_device_handle **out,
+               adapter_get_alternate_serial_fn adapter_get_alternate_serial);
 void jtag_libusb_close(struct libusb_device_handle *dev);
 int jtag_libusb_control_transfer(struct libusb_device_handle *dev,
                uint8_t requestType, uint8_t request, uint16_t wValue,
index 7298a2a109e8c7b73f20076827993e77684e4195..6812ef6492f43ed414276ba39d1d14ec067612ea 100644 (file)
@@ -715,7 +715,7 @@ struct opendous_jtag *opendous_usb_open(void)
        struct opendous_jtag *result;
 
        struct 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, &devh, NULL) != ERROR_OK)
                return NULL;
 
        jtag_libusb_set_configuration(devh, 0);
index 6131df914cff32d1f739465a078c5c412e13c565..7eab5c1302bced6ca26b81e07f281fd20532960d 100644 (file)
@@ -449,7 +449,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, &usbh, NULL);
        if (ret != ERROR_OK) {
                LOG_ERROR("unable to open cy7c65215 device");
                goto err;
index aea126d0d34e0d13996b07462290658c41ee26cc..dc236660e227593774dc6bb2b71b11c26b0bf27f 100644 (file)
@@ -374,7 +374,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, &osbdm->devh, NULL) != ERROR_OK)
                return ERROR_FAIL;
 
        if (libusb_claim_interface(osbdm->devh, 0) != ERROR_OK)
index ca7a4df4e8a4c8dcb7fcfdefea1489adb86cacd5..d8d8d67e3a7ddf657cd5fdef77f5dbe05b4ff893 100644 (file)
@@ -2778,7 +2778,8 @@ 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, param->serial,
+                               &h->fd, NULL) != ERROR_OK) {
                        LOG_ERROR("open failed");
                        goto error_open;
                }
index 4f7ee6300740547431e3b9a4e4bdfe896d246223..b406c0321beee962601c94c14057f580e62cfda5 100644 (file)
@@ -195,7 +195,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, &temp, NULL) == ERROR_OK) {
                LOG_INFO("Altera USB-Blaster II (uninitialized) found");
                LOG_INFO("Loading firmware...");
                ret = load_usb_blaster_firmware(temp, low);
@@ -209,13 +209,15 @@ 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,
+                               &low->libusb_dev, NULL) != 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,
+                               &low->libusb_dev, NULL) != ERROR_OK && retry--) {
                        usleep(1000000);
                        LOG_INFO("Waiting for renumerate...");
                }