jtag/adapter: move 'usb location' code in adapter.c 40/6640/2
authorAntonio Borneo <borneo.antonio@gmail.com>
Wed, 6 Oct 2021 21:17:30 +0000 (23:17 +0200)
committerAntonio Borneo <borneo.antonio@gmail.com>
Sat, 13 Nov 2021 10:47:02 +0000 (10:47 +0000)
The configuration code for adapter parameters is spread around.

Add a struct in adapter.c aimed at containing all the adapter's
configuration data.
Move in adapter.c the code related to configuring 'usb location'
and the copyright tag.
Add adapter.h to export the functions.
While there:
- rework the copyright and the SPDX tag;
- rename the 'usb location' functions;
- remove the JTAG_SRC variable in Makefile.am.

Change-Id: I4fe0d32991a8a30e315807180688035ae9ee01ce
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/6640
Tested-by: jenkins
Reviewed-by: Tarek BOCHKATI <tarek.bouchkati@gmail.com>
src/jtag/Makefile.am
src/jtag/adapter.c
src/jtag/adapter.h [new file with mode: 0644]
src/jtag/drivers/Makefile.am
src/jtag/drivers/ftdi.c
src/jtag/drivers/jlink.c
src/jtag/drivers/jtag_usb_common.c [deleted file]
src/jtag/drivers/jtag_usb_common.h [deleted file]
src/jtag/drivers/libusb_helper.c

index 4ed5e7aa0b1b384d4328e3ebae85647862a2cc43..cbdfb205478d75cc5941bfabbb44bfbc8f32b924 100644 (file)
@@ -1,6 +1,5 @@
 noinst_LTLIBRARIES += %D%/libjtag.la
 
-JTAG_SRCS = %D%/commands.c
 %C%_libjtag_la_LIBADD =
 
 if HLADAPTER
@@ -18,6 +17,8 @@ include %D%/drivers/Makefile.am
 
 %C%_libjtag_la_SOURCES = \
        %D%/adapter.c \
+       %D%/adapter.h \
+       %D%/commands.c \
        %D%/core.c \
        %D%/interface.c \
        %D%/interfaces.c \
@@ -31,7 +32,6 @@ include %D%/drivers/Makefile.am
        %D%/jtag.h \
        %D%/swd.h \
        %D%/swim.h \
-       %D%/tcl.h \
-       $(JTAG_SRCS)
+       %D%/tcl.h
 
 STARTUP_TCL_SRCS += %D%/startup.tcl
index 80d5ab048a991e752c20199cd84cbba121c37684..47a1d794b3bcfd65b86f1363de5b481e2126b620 100644 (file)
@@ -1,41 +1,22 @@
-/***************************************************************************
- *   Copyright (C) 2005 by Dominic Rath                                    *
- *   Dominic.Rath@gmx.de                                                   *
- *                                                                         *
- *   Copyright (C) 2007-2010 Øyvind Harboe                                 *
- *   oyvind.harboe@zylin.com                                               *
- *                                                                         *
- *   Copyright (C) 2009 SoftPLC Corporation                                *
- *       http://softplc.com                                                *
- *   dick@softplc.com                                                      *
- *                                                                         *
- *   Copyright (C) 2009 Zachary T Welch                                    *
- *   zw@superlucidity.net                                                  *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
- ***************************************************************************/
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de>
+ * Copyright (C) 2007-2010 Øyvind Harboe <oyvind.harboe@zylin.com>
+ * Copyright (C) 2009 SoftPLC Corporation, http://softplc.com, Dick Hollenbeck <dick@softplc.com>
+ * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net>
+ * Copyright (C) 2018 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
+ */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
+#include "adapter.h"
 #include "jtag.h"
 #include "minidriver.h"
 #include "interface.h"
 #include "interfaces.h"
 #include <transport/transport.h>
-#include <jtag/drivers/jtag_usb_common.h>
 
 #ifdef HAVE_STRINGS_H
 #include <strings.h>
 struct adapter_driver *adapter_driver;
 const char * const jtag_only[] = { "jtag", NULL };
 
+/**
+ * Adapter configuration
+ */
+static struct {
+       char *usb_location;
+} adapter_config;
+
+/*
+ * 1 char: bus
+ * 2 * 7 chars: max 7 ports
+ * 1 char: test for overflow
+ * ------
+ * 16 chars
+ */
+#define USB_MAX_LOCATION_LENGTH         16
+
+#ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
+static void adapter_usb_set_location(const char *location)
+{
+       if (strnlen(location, USB_MAX_LOCATION_LENGTH) == USB_MAX_LOCATION_LENGTH)
+               LOG_WARNING("usb location string is too long!!");
+
+       free(adapter_config.usb_location);
+
+       adapter_config.usb_location = strndup(location, USB_MAX_LOCATION_LENGTH);
+}
+#endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
+
+const char *adapter_usb_get_location(void)
+{
+       return adapter_config.usb_location;
+}
+
+bool adapter_usb_location_equal(uint8_t dev_bus, uint8_t *port_path, size_t path_len)
+{
+       size_t path_step, string_length;
+       char *ptr, *loc;
+       bool equal = false;
+
+       if (!adapter_usb_get_location())
+               return equal;
+
+       /* strtok need non const char */
+       loc = strndup(adapter_usb_get_location(), USB_MAX_LOCATION_LENGTH);
+       string_length = strnlen(loc, USB_MAX_LOCATION_LENGTH);
+
+       ptr = strtok(loc, "-");
+       if (!ptr) {
+               LOG_WARNING("no '-' in usb path\n");
+               goto done;
+       }
+
+       string_length -= strnlen(ptr, string_length);
+       /* check bus mismatch */
+       if (atoi(ptr) != dev_bus)
+               goto done;
+
+       path_step = 0;
+       while (path_step < path_len) {
+               ptr = strtok(NULL, ".");
+
+               /* no more tokens in path */
+               if (!ptr)
+                       break;
+
+               /* path mismatch at some step */
+               if (path_step < path_len && atoi(ptr) != port_path[path_step])
+                       break;
+
+               path_step++;
+               string_length -= strnlen(ptr, string_length) + 1;
+       };
+
+       /* walked the full path, all elements match */
+       if (path_step == path_len && !string_length)
+               equal = true;
+
+done:
+       free(loc);
+       return equal;
+}
+
 static int jim_adapter_name(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
 {
        struct jim_getopt_info goi;
@@ -501,9 +564,9 @@ COMMAND_HANDLER(handle_adapter_reset_de_assert)
 COMMAND_HANDLER(handle_usb_location_command)
 {
        if (CMD_ARGC == 1)
-               jtag_usb_set_location(CMD_ARGV[0]);
+               adapter_usb_set_location(CMD_ARGV[0]);
 
-       command_print(CMD, "adapter usb location: %s", jtag_usb_get_location());
+       command_print(CMD, "adapter usb location: %s", adapter_usb_get_location());
 
        return ERROR_OK;
 }
diff --git a/src/jtag/adapter.h b/src/jtag/adapter.h
new file mode 100644 (file)
index 0000000..854ee9c
--- /dev/null
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (c) 2018 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
+ */
+
+#ifndef OPENOCD_JTAG_ADAPTER_H
+#define OPENOCD_JTAG_ADAPTER_H
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+/** @returns USB location string set with command 'adapter usb location' */
+const char *adapter_usb_get_location(void);
+
+/** @returns true if USB location string is "<dev_bus>-<port_path[0]>[.<port_path[1]>[...]]" */
+bool adapter_usb_location_equal(uint8_t dev_bus, uint8_t *port_path, size_t path_len);
+
+#endif /* OPENOCD_JTAG_ADAPTER_H */
index da60f366e48617aaf23af300397217814be72421..c2161523da5b4b51b7970675574676b68c78d1c7 100644 (file)
@@ -19,7 +19,6 @@ DRIVERFILES =
 
 # Standard Driver: common files
 DRIVERFILES += %D%/driver.c
-DRIVERFILES += %D%/jtag_usb_common.c
 
 if USE_LIBUSB1
 DRIVERFILES += %D%/libusb_helper.c
@@ -187,7 +186,6 @@ endif
 DRIVERHEADERS = \
        %D%/bitbang.h \
        %D%/bitq.h \
-       %D%/jtag_usb_common.h \
        %D%/libftdi_helper.h \
        %D%/libusb_helper.h \
        %D%/cmsis_dap.h \
index 82298c23de3461ca19e63ba9ad5e17db288783c1..5e7eae073fa4d3a405c85a8851b6c1c5470652e0 100644 (file)
@@ -69,7 +69,7 @@
 #endif
 
 /* project specific includes */
-#include <jtag/drivers/jtag_usb_common.h>
+#include <jtag/adapter.h>
 #include <jtag/interface.h>
 #include <jtag/swd.h>
 #include <transport/transport.h>
@@ -672,7 +672,7 @@ static int ftdi_initialize(void)
 
        for (int i = 0; ftdi_vid[i] || ftdi_pid[i]; i++) {
                mpsse_ctx = mpsse_open(&ftdi_vid[i], &ftdi_pid[i], ftdi_device_desc,
-                               ftdi_serial, jtag_usb_get_location(), ftdi_channel);
+                               ftdi_serial, adapter_usb_get_location(), ftdi_channel);
                if (mpsse_ctx)
                        break;
        }
index 53ae1dfaeb644c742e8532fb35b546a5512b0426..12ac05fa6e2a4381c16076fda21f8969dd777beb 100644 (file)
@@ -38,7 +38,7 @@
 #include <jtag/interface.h>
 #include <jtag/swd.h>
 #include <jtag/commands.h>
-#include <jtag/drivers/jtag_usb_common.h>
+#include <jtag/adapter.h>
 #include <helper/replacements.h>
 #include <target/cortex_m.h>
 
@@ -547,7 +547,7 @@ static bool jlink_usb_location_equal(struct jaylink_device *dev)
                return false;
        }
 
-       equal = jtag_usb_location_equal(bus, ports,     num_ports);
+       equal = adapter_usb_location_equal(bus, ports, num_ports);
        free(ports);
 
        return equal;
@@ -573,7 +573,7 @@ static int jlink_open_device(uint32_t ifaces, bool *found_device)
                return ERROR_JTAG_INIT_FAILED;
        }
 
-       use_usb_location = !!jtag_usb_get_location();
+       use_usb_location = !!adapter_usb_get_location();
 
        if (!use_serial_number && !use_usb_address && !use_usb_location && num_devices > 1) {
                LOG_ERROR("Multiple devices found, specify the desired device");
diff --git a/src/jtag/drivers/jtag_usb_common.c b/src/jtag/drivers/jtag_usb_common.c
deleted file mode 100644 (file)
index 94cd7e7..0000000
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * SPDX-License-Identifier: GPL-2.0+
- * Copyright (c) 2018 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
- */
-
-#include <helper/log.h>
-#include <string.h>
-
-#include "jtag_usb_common.h"
-
-static char *jtag_usb_location;
-/*
- * 1 char: bus
- * 2 * 7 chars: max 7 ports
- * 1 char: test for overflow
- * ------
- * 16 chars
- */
-#define JTAG_USB_MAX_LOCATION_LENGTH   16
-
-void jtag_usb_set_location(const char *location)
-{
-       if (strnlen(location, JTAG_USB_MAX_LOCATION_LENGTH) ==
-           JTAG_USB_MAX_LOCATION_LENGTH)
-               LOG_WARNING("usb location string is too long!!\n");
-
-       free(jtag_usb_location);
-
-       jtag_usb_location = strndup(location, JTAG_USB_MAX_LOCATION_LENGTH);
-}
-
-const char *jtag_usb_get_location(void)
-{
-       return jtag_usb_location;
-}
-
-bool jtag_usb_location_equal(uint8_t dev_bus, uint8_t *port_path,
-                            size_t path_len)
-{
-       size_t path_step, string_length;
-       char *ptr, *loc;
-       bool equal = false;
-
-       /* strtok need non const char */
-       loc = strndup(jtag_usb_get_location(), JTAG_USB_MAX_LOCATION_LENGTH);
-       string_length = strnlen(loc, JTAG_USB_MAX_LOCATION_LENGTH);
-
-       ptr = strtok(loc, "-");
-       if (!ptr) {
-               LOG_WARNING("no '-' in usb path\n");
-               goto done;
-       }
-
-       string_length -= strnlen(ptr, string_length);
-       /* check bus mismatch */
-       if (atoi(ptr) != dev_bus)
-               goto done;
-
-       path_step = 0;
-       while (path_step < path_len) {
-               ptr = strtok(NULL, ".");
-
-               /* no more tokens in path */
-               if (!ptr)
-                       break;
-
-               /* path mismatch at some step */
-               if (path_step < path_len && atoi(ptr) != port_path[path_step])
-                       break;
-
-               path_step++;
-               string_length -= strnlen(ptr, string_length) + 1;
-       };
-
-       /* walked the full path, all elements match */
-       if (path_step == path_len && !string_length)
-               equal = true;
-
-done:
-       free(loc);
-       return equal;
-}
diff --git a/src/jtag/drivers/jtag_usb_common.h b/src/jtag/drivers/jtag_usb_common.h
deleted file mode 100644 (file)
index c4c28cc..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-/*
- * SPDX-License-Identifier: GPL-2.0+
- * Copyright (c) 2018 Pengutronix, Oleksij Rempel <kernel@pengutronix.de>
- */
-
-#ifndef OPENOCD_JTAG_USB_COMMON_H
-#define OPENOCD_JTAG_USB_COMMON_H
-
-#include <helper/replacements.h>
-#include <helper/types.h>
-
-void jtag_usb_set_location(const char *location);
-const char *jtag_usb_get_location(void);
-bool jtag_usb_location_equal(uint8_t dev_bus, uint8_t *port_path,
-                            size_t path_len);
-
-#endif /* OPENOCD_JTAG_USB_COMMON_H */
index 3308d8742c805d1e0ad08b3a45eda54d5bbb1381..b8f1124e3b70ee4a6dd7239c0e152f6c051b155b 100644 (file)
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
+
+#include <string.h>
+
 #include <helper/log.h>
-#include <jtag/drivers/jtag_usb_common.h>
+#include <jtag/adapter.h>
 #include "libusb_helper.h"
 
 /*
@@ -85,7 +88,7 @@ static bool jtag_libusb_location_equal(struct libusb_device *device)
        }
        dev_bus = libusb_get_bus_number(device);
 
-       return jtag_usb_location_equal(dev_bus, port_path, path_len);
+       return adapter_usb_location_equal(dev_bus, port_path, path_len);
 }
 #else /* HAVE_LIBUSB_GET_PORT_NUMBERS */
 static bool jtag_libusb_location_equal(struct libusb_device *device)
@@ -177,7 +180,7 @@ int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
                if (!jtag_libusb_match_ids(&dev_desc, vids, pids))
                        continue;
 
-               if (jtag_usb_get_location() && !jtag_libusb_location_equal(devs[idx]))
+               if (adapter_usb_get_location() && !jtag_libusb_location_equal(devs[idx]))
                        continue;
 
                err_code = libusb_open(devs[idx], &libusb_handle);

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)