jtag/mpsse: mpsse_flush should not treat LIBUSB_ERROR_INTERRUPTED as an error
[openocd.git] / src / jtag / drivers / cmsis_dap_usb_bulk.c
index aaab5804d17c3d81fd62e712c2581605c2589543..8d0cb544d7bb481d4bd7f2514b70236c0def9a8b 100644 (file)
 #include "cmsis_dap.h"
 #include "libusb_helper.h"
 
-#if !defined(LIBUSB_API_VERSION) || (LIBUSB_API_VERSION < 0x01000105) \
-               || defined(_WIN32) || defined(__CYGWIN__)
-       #define libusb_dev_mem_alloc(dev, sz) malloc(sz)
-       #define libusb_dev_mem_free(dev, buffer, sz) free(buffer)
-#endif
-
 enum {
        CMSIS_DAP_TRANSFER_PENDING = 0, /* must be 0, used in libusb_handle_events_completed */
        CMSIS_DAP_TRANSFER_IDLE,
@@ -368,7 +362,7 @@ static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t p
                        if (err)
                                LOG_WARNING("could not claim interface: %s", libusb_strerror(err));
 
-                       dap->bdata = malloc(sizeof(struct cmsis_dap_backend_data));
+                       dap->bdata = calloc(1, sizeof(struct cmsis_dap_backend_data));
                        if (!dap->bdata) {
                                LOG_ERROR("unable to allocate memory");
                                libusb_release_interface(dev_handle, interface_num);
@@ -599,33 +593,34 @@ static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz)
        dap->command = dap->packet_buffer;
        dap->response = dap->packet_buffer;
 
+       struct cmsis_dap_backend_data *bdata = dap->bdata;
        for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) {
-               dap->bdata->command_transfers[i].buffer =
-                       libusb_dev_mem_alloc(dap->bdata->dev_handle, pkt_sz);
-               if (!dap->bdata->command_transfers[i].buffer) {
-                       LOG_ERROR("unable to allocate CMSIS-DAP packet buffer");
-                       return ERROR_FAIL;
-               }
-               dap->bdata->response_transfers[i].buffer =
-                       libusb_dev_mem_alloc(dap->bdata->dev_handle, pkt_sz);
-               if (!dap->bdata->response_transfers[i].buffer) {
-                       LOG_ERROR("unable to allocate CMSIS-DAP packet buffer");
+               bdata->command_transfers[i].buffer =
+                       oocd_libusb_dev_mem_alloc(bdata->dev_handle, pkt_sz);
+
+               bdata->response_transfers[i].buffer =
+                       oocd_libusb_dev_mem_alloc(bdata->dev_handle, pkt_sz);
+
+               if (!bdata->command_transfers[i].buffer
+                       || !bdata->response_transfers[i].buffer) {
+                       LOG_ERROR("unable to allocate CMSIS-DAP pending packet buffer");
                        return ERROR_FAIL;
                }
        }
-
        return ERROR_OK;
 }
 
 static void cmsis_dap_usb_free(struct cmsis_dap *dap)
 {
+       struct cmsis_dap_backend_data *bdata = dap->bdata;
+
        for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) {
-               libusb_dev_mem_free(dap->bdata->dev_handle,
-                       dap->bdata->command_transfers[i].buffer, dap->packet_size);
-               dap->bdata->command_transfers[i].buffer = NULL;
-               libusb_dev_mem_free(dap->bdata->dev_handle,
-                       dap->bdata->response_transfers[i].buffer, dap->packet_size);
-               dap->bdata->response_transfers[i].buffer = NULL;
+               oocd_libusb_dev_mem_free(bdata->dev_handle,
+                       bdata->command_transfers[i].buffer, dap->packet_size);
+               oocd_libusb_dev_mem_free(bdata->dev_handle,
+                       bdata->response_transfers[i].buffer, dap->packet_size);
+               bdata->command_transfers[i].buffer = NULL;
+               bdata->response_transfers[i].buffer = NULL;
        }
 
        free(dap->packet_buffer);
@@ -634,6 +629,19 @@ static void cmsis_dap_usb_free(struct cmsis_dap *dap)
        dap->response = NULL;
 }
 
+static void cmsis_dap_usb_cancel_all(struct cmsis_dap *dap)
+{
+       for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) {
+               if (dap->bdata->command_transfers[i].status == CMSIS_DAP_TRANSFER_PENDING)
+                       libusb_cancel_transfer(dap->bdata->command_transfers[i].transfer);
+               if (dap->bdata->response_transfers[i].status == CMSIS_DAP_TRANSFER_PENDING)
+                       libusb_cancel_transfer(dap->bdata->response_transfers[i].transfer);
+
+               dap->bdata->command_transfers[i].status = CMSIS_DAP_TRANSFER_IDLE;
+               dap->bdata->response_transfers[i].status = CMSIS_DAP_TRANSFER_IDLE;
+       }
+}
+
 COMMAND_HANDLER(cmsis_dap_handle_usb_interface_command)
 {
        if (CMD_ARGC == 1)
@@ -663,4 +671,5 @@ const struct cmsis_dap_backend cmsis_dap_usb_backend = {
        .write = cmsis_dap_usb_write,
        .packet_buffer_alloc = cmsis_dap_usb_alloc,
        .packet_buffer_free = cmsis_dap_usb_free,
+       .cancel_all = cmsis_dap_usb_cancel_all,
 };

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)