ftdi: don't wait forever if we fail
[openocd.git] / src / jtag / drivers / mpsse.c
index 0ef88ba2d57c1b0410a77067d7b66ebb2d2d05e8..3c1c97cdc23f1a3f14f1125042ccf1dc3b5e37ae 100644 (file)
@@ -872,6 +872,8 @@ int mpsse_flush(struct mpsse_ctx *ctx)
        libusb_fill_bulk_transfer(write_transfer, ctx->usb_dev, ctx->out_ep, ctx->write_buffer,
                ctx->write_count, write_cb, &write_result, ctx->usb_write_timeout);
        retval = libusb_submit_transfer(write_transfer);
+       if (retval != LIBUSB_SUCCESS)
+               goto error_check;
 
        if (ctx->read_count) {
                read_transfer = libusb_alloc_transfer(0);
@@ -879,22 +881,36 @@ int mpsse_flush(struct mpsse_ctx *ctx)
                        ctx->read_chunk_size, read_cb, &read_result,
                        ctx->usb_read_timeout);
                retval = libusb_submit_transfer(read_transfer);
+               if (retval != LIBUSB_SUCCESS)
+                       goto error_check;
        }
 
        /* Polling loop, more or less taken from libftdi */
        while (!write_result.done || !read_result.done) {
-               retval = libusb_handle_events(ctx->usb_ctx);
+               struct timeval timeout_usb;
+
+               timeout_usb.tv_sec = 1;
+               timeout_usb.tv_usec = 0;
+
+               retval = libusb_handle_events_timeout_completed(ctx->usb_ctx, &timeout_usb, NULL);
                keep_alive();
-               if (retval != LIBUSB_SUCCESS && retval != LIBUSB_ERROR_INTERRUPTED) {
+               if (retval == LIBUSB_ERROR_NO_DEVICE || retval == LIBUSB_ERROR_INTERRUPTED)
+                       break;
+
+               if (retval != LIBUSB_SUCCESS) {
                        libusb_cancel_transfer(write_transfer);
                        if (read_transfer)
                                libusb_cancel_transfer(read_transfer);
-                       while (!write_result.done || !read_result.done)
-                               if (libusb_handle_events(ctx->usb_ctx) != LIBUSB_SUCCESS)
+                       while (!write_result.done || !read_result.done) {
+                               retval = libusb_handle_events_timeout_completed(ctx->usb_ctx,
+                                                               &timeout_usb, NULL);
+                               if (retval != LIBUSB_SUCCESS)
                                        break;
+                       }
                }
        }
 
+error_check:
        if (retval != LIBUSB_SUCCESS) {
                LOG_ERROR("libusb_handle_events() failed with %s", libusb_error_name(retval));
                retval = ERROR_FAIL;

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)