target/xtensa: avoid IHI for writes to non-executable memory
[openocd.git] / src / jtag / drivers / mpsse.c
index a881803adfc10a0dc80f5a02008d503cf63cfd1f..f3499e3864fcb92ef8228d2b1f4ccaacb3b86f41 100644 (file)
@@ -1,19 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /**************************************************************************
  *   Copyright (C) 2012 by Andreas Fritiofson                              *
  *   andreas.fritiofson@gmail.com                                          *
- *                                                                         *
- *   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/>. *
  ***************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -22,6 +11,9 @@
 
 #include "mpsse.h"
 #include "helper/log.h"
+#include "helper/replacements.h"
+#include "helper/time_support.h"
+#include "libusb_helper.h"
 #include <libusb.h>
 
 /* Compatibility define for older libusb-1.0 */
@@ -61,8 +53,8 @@
 #define SIO_RESET_PURGE_TX 2
 
 struct mpsse_ctx {
-       libusb_context *usb_ctx;
-       libusb_device_handle *usb_dev;
+       struct libusb_context *usb_ctx;
+       struct libusb_device_handle *usb_dev;
        unsigned int usb_write_timeout;
        unsigned int usb_read_timeout;
        uint8_t in_ep;
@@ -84,7 +76,7 @@ struct mpsse_ctx {
 };
 
 /* 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,
+static bool string_descriptor_equal(struct libusb_device_handle *device, uint8_t str_index,
        const char *string)
 {
        int retval;
@@ -98,7 +90,7 @@ static bool string_descriptor_equal(libusb_device_handle *device, uint8_t str_in
        return strncmp(string, desc_string, sizeof(desc_string)) == 0;
 }
 
-static bool device_location_equal(libusb_device *device, const char *location)
+static bool device_location_equal(struct libusb_device *device, const char *location)
 {
        bool result = false;
 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
@@ -117,7 +109,7 @@ static bool device_location_equal(libusb_device *device, const char *location)
        LOG_DEBUG("device path has %i steps", path_len);
 
        ptr = strtok(loc, "-:");
-       if (ptr == NULL) {
+       if (!ptr) {
                LOG_DEBUG("no ':' in path");
                goto done;
        }
@@ -129,7 +121,7 @@ static bool device_location_equal(libusb_device *device, const char *location)
        path_step = 0;
        while (path_step < 7) {
                ptr = strtok(NULL, ".,");
-               if (ptr == NULL) {
+               if (!ptr) {
                        LOG_DEBUG("no more tokens in path at step %i", path_step);
                        break;
                }
@@ -157,10 +149,10 @@ static bool device_location_equal(libusb_device *device, const char *location)
  * Set any field to 0 as a wildcard. If the device is found true is returned, with ctx containing
  * the already opened handle. ctx->interface must be set to the desired interface (channel) number
  * prior to calling this function. */
-static bool open_matching_device(struct mpsse_ctx *ctx, const uint16_t *vid, const uint16_t *pid,
+static bool open_matching_device(struct mpsse_ctx *ctx, const uint16_t vids[], const uint16_t pids[],
        const char *product, const char *serial, const char *location)
 {
-       libusb_device **list;
+       struct libusb_device **list;
        struct libusb_device_descriptor desc;
        struct libusb_config_descriptor *config0;
        int err;
@@ -170,7 +162,7 @@ static bool open_matching_device(struct mpsse_ctx *ctx, const uint16_t *vid, con
                LOG_ERROR("libusb_get_device_list() failed with %s", libusb_error_name(cnt));
 
        for (ssize_t i = 0; i < cnt; i++) {
-               libusb_device *device = list[i];
+               struct libusb_device *device = list[i];
 
                err = libusb_get_device_descriptor(device, &desc);
                if (err != LIBUSB_SUCCESS) {
@@ -178,9 +170,7 @@ static bool open_matching_device(struct mpsse_ctx *ctx, const uint16_t *vid, con
                        continue;
                }
 
-               if (vid && *vid != desc.idVendor)
-                       continue;
-               if (pid && *pid != desc.idProduct)
+               if (!jtag_libusb_match_ids(&desc, vids, pids))
                        continue;
 
                err = libusb_open(device, &ctx->usb_dev);
@@ -212,7 +202,7 @@ static bool open_matching_device(struct mpsse_ctx *ctx, const uint16_t *vid, con
        libusb_free_device_list(list, 1);
 
        if (!found) {
-               LOG_ERROR("no device found");
+               /* The caller reports detailed error desc */
                return false;
        }
 
@@ -275,6 +265,24 @@ static bool open_matching_device(struct mpsse_ctx *ctx, const uint16_t *vid, con
        case 0x900:
                ctx->type = TYPE_FT232H;
                break;
+       case 0x2800:
+               ctx->type = TYPE_FT2233HP;
+               break;
+       case 0x2900:
+               ctx->type = TYPE_FT4233HP;
+               break;
+       case 0x3000:
+               ctx->type = TYPE_FT2232HP;
+               break;
+       case 0x3100:
+               ctx->type = TYPE_FT4232HP;
+               break;
+       case 0x3200:
+               ctx->type = TYPE_FT233HP;
+               break;
+       case 0x3300:
+               ctx->type = TYPE_FT232HP;
+               break;
        default:
                LOG_ERROR("unsupported FTDI chip type: 0x%04x", desc.bcdDevice);
                goto error;
@@ -316,14 +324,14 @@ error:
        return false;
 }
 
-struct mpsse_ctx *mpsse_open(const uint16_t *vid, const uint16_t *pid, const char *description,
+struct mpsse_ctx *mpsse_open(const uint16_t vids[], const uint16_t pids[], const char *description,
        const char *serial, const char *location, int channel)
 {
        struct mpsse_ctx *ctx = calloc(1, sizeof(*ctx));
        int err;
 
        if (!ctx)
-               return 0;
+               return NULL;
 
        bit_copy_queue_init(&ctx->read_queue);
        ctx->read_chunk_size = 16384;
@@ -352,18 +360,13 @@ struct mpsse_ctx *mpsse_open(const uint16_t *vid, const uint16_t *pid, const cha
                goto error;
        }
 
-       if (!open_matching_device(ctx, vid, pid, description, serial, location)) {
-               /* Four hex digits plus terminating zero each */
-               char vidstr[5];
-               char pidstr[5];
-               LOG_ERROR("unable to open ftdi device with vid %s, pid %s, description '%s', "
+       if (!open_matching_device(ctx, vids, pids, description, serial, location)) {
+               LOG_ERROR("unable to open ftdi device with description '%s', "
                                "serial '%s' at bus location '%s'",
-                               vid ? sprintf(vidstr, "%04x", *vid), vidstr : "*",
-                               pid ? sprintf(pidstr, "%04x", *pid), pidstr : "*",
                                description ? description : "*",
                                serial ? serial : "*",
                                location ? location : "*");
-               ctx->usb_dev = 0;
+               ctx->usb_dev = NULL;
                goto error;
        }
 
@@ -393,7 +396,7 @@ struct mpsse_ctx *mpsse_open(const uint16_t *vid, const uint16_t *pid, const cha
        return ctx;
 error:
        mpsse_close(ctx);
-       return 0;
+       return NULL;
 }
 
 void mpsse_close(struct mpsse_ctx *ctx)
@@ -403,13 +406,10 @@ void mpsse_close(struct mpsse_ctx *ctx)
        if (ctx->usb_ctx)
                libusb_exit(ctx->usb_ctx);
        bit_copy_discard(&ctx->read_queue);
-       if (ctx->write_buffer)
-               free(ctx->write_buffer);
-       if (ctx->read_buffer)
-               free(ctx->read_buffer);
-       if (ctx->read_chunk)
-               free(ctx->read_chunk);
 
+       free(ctx->write_buffer);
+       free(ctx->read_buffer);
+       free(ctx->read_chunk);
        free(ctx);
 }
 
@@ -483,13 +483,13 @@ static unsigned buffer_add_read(struct mpsse_ctx *ctx, uint8_t *in, unsigned in_
 void mpsse_clock_data_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset,
        unsigned length, uint8_t mode)
 {
-       mpsse_clock_data(ctx, out, out_offset, 0, 0, length, mode);
+       mpsse_clock_data(ctx, out, out_offset, NULL, 0, length, mode);
 }
 
 void mpsse_clock_data_in(struct mpsse_ctx *ctx, uint8_t *in, unsigned in_offset, unsigned length,
        uint8_t mode)
 {
-       mpsse_clock_data(ctx, 0, 0, in, in_offset, length, mode);
+       mpsse_clock_data(ctx, NULL, 0, in, in_offset, length, mode);
 }
 
 void mpsse_clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset, uint8_t *in,
@@ -566,7 +566,7 @@ void mpsse_clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_of
 void mpsse_clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset,
        unsigned length, bool tdi, uint8_t mode)
 {
-       mpsse_clock_tms_cs(ctx, out, out_offset, 0, 0, length, tdi, mode);
+       mpsse_clock_tms_cs(ctx, out, out_offset, NULL, 0, length, tdi, mode);
 }
 
 void mpsse_clock_tms_cs(struct mpsse_ctx *ctx, const uint8_t *out, unsigned out_offset, uint8_t *in,
@@ -860,7 +860,7 @@ int mpsse_flush(struct mpsse_ctx *ctx)
        if (ctx->write_count == 0)
                return retval;
 
-       struct libusb_transfer *read_transfer = 0;
+       struct libusb_transfer *read_transfer = NULL;
        struct transfer_result read_result = { .ctx = ctx, .done = true };
        if (ctx->read_count) {
                buffer_write_byte(ctx, 0x87); /* SEND_IMMEDIATE */
@@ -888,6 +888,8 @@ int mpsse_flush(struct mpsse_ctx *ctx)
        }
 
        /* Polling loop, more or less taken from libftdi */
+       int64_t start = timeval_ms();
+       int64_t warn_after = 2000;
        while (!write_result.done || !read_result.done) {
                struct timeval timeout_usb;
 
@@ -896,19 +898,21 @@ int mpsse_flush(struct mpsse_ctx *ctx)
 
                retval = libusb_handle_events_timeout_completed(ctx->usb_ctx, &timeout_usb, NULL);
                keep_alive();
-               if (retval == LIBUSB_ERROR_NO_DEVICE || retval == LIBUSB_ERROR_INTERRUPTED)
-                       break;
+
+               int64_t now = timeval_ms();
+               if (now - start > warn_after) {
+                       LOG_WARNING("Haven't made progress in mpsse_flush() for %" PRId64
+                                       "ms.", now - start);
+                       warn_after *= 2;
+               }
+
+               if (retval == LIBUSB_ERROR_INTERRUPTED)
+                       continue;
 
                if (retval != LIBUSB_SUCCESS) {
                        libusb_cancel_transfer(write_transfer);
                        if (read_transfer)
                                libusb_cancel_transfer(read_transfer);
-                       while (!write_result.done || !read_result.done) {
-                               retval = libusb_handle_events_timeout_completed(ctx->usb_ctx,
-                                                               &timeout_usb, NULL);
-                               if (retval != LIBUSB_SUCCESS)
-                                       break;
-                       }
                }
        }
 
@@ -937,12 +941,12 @@ error_check:
                retval = ERROR_OK;
        }
 
+       if (retval != ERROR_OK)
+               mpsse_purge(ctx);
+
        libusb_free_transfer(write_transfer);
        if (read_transfer)
                libusb_free_transfer(read_transfer);
 
-       if (retval != ERROR_OK)
-               mpsse_purge(ctx);
-
        return retval;
 }

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)