cmsis-dap: don't update the packet size across backends.
[fw/openocd] / src / jtag / drivers / cmsis_dap_usb_bulk.c
index 0134c0376557808ab0cdfe28797a13c3a9da5b8a..7efea5166689d26339de7fcc377afaddddc2eda4 100644 (file)
@@ -50,6 +50,9 @@ struct cmsis_dap_backend_data {
 
 static int cmsis_dap_usb_interface = -1;
 
+static void cmsis_dap_usb_close(struct cmsis_dap *dap);
+static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz);
+
 static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t pids[], char *serial)
 {
        int err;
@@ -356,12 +359,21 @@ static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t p
                                return ERROR_FAIL;
                        }
 
-                       dap->packet_size = packet_size + 1; /* "+ 1" for compatibility with the HID backend */
+                       dap->packet_size = packet_size;
+                       dap->packet_buffer_size = packet_size + REPORT_ID_SIZE;
                        dap->bdata->usb_ctx = ctx;
                        dap->bdata->dev_handle = dev_handle;
                        dap->bdata->ep_out = ep_out;
                        dap->bdata->ep_in = ep_in;
                        dap->bdata->interface = interface_num;
+
+                       dap->packet_buffer = malloc(dap->packet_buffer_size);
+                       if (dap->packet_buffer == NULL) {
+                               LOG_ERROR("unable to allocate memory");
+                               cmsis_dap_usb_close(dap);
+                               return ERROR_FAIL;
+                       }
+
                        return ERROR_OK;
                }
 
@@ -381,6 +393,8 @@ static void cmsis_dap_usb_close(struct cmsis_dap *dap)
        libusb_exit(dap->bdata->usb_ctx);
        free(dap->bdata);
        dap->bdata = NULL;
+       free(dap->packet_buffer);
+       dap->packet_buffer = NULL;
 }
 
 static int cmsis_dap_usb_read(struct cmsis_dap *dap, int timeout_ms)
@@ -399,7 +413,7 @@ static int cmsis_dap_usb_read(struct cmsis_dap *dap, int timeout_ms)
                }
        }
 
-       memset(&dap->packet_buffer[transferred], 0, dap->packet_size - transferred);
+       memset(&dap->packet_buffer[transferred], 0, dap->packet_buffer_size - transferred);
 
        return transferred;
 }
@@ -411,7 +425,7 @@ static int cmsis_dap_usb_write(struct cmsis_dap *dap, int txlen, int timeout_ms)
 
        /* skip the first byte that is only used by the HID backend */
        err = libusb_bulk_transfer(dap->bdata->dev_handle, dap->bdata->ep_out,
-                                                       dap->packet_buffer + 1, txlen - 1, &transferred, timeout_ms);
+                                                       dap->packet_buffer + REPORT_ID_SIZE, txlen - REPORT_ID_SIZE, &transferred, timeout_ms);
        if (err) {
                if (err == LIBUSB_ERROR_TIMEOUT) {
                        return ERROR_TIMEOUT_REACHED;
@@ -424,6 +438,22 @@ static int cmsis_dap_usb_write(struct cmsis_dap *dap, int txlen, int timeout_ms)
        return transferred;
 }
 
+static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz)
+{
+       unsigned int packet_buffer_size = pkt_sz + REPORT_ID_SIZE;
+       uint8_t *buf = malloc(packet_buffer_size);
+       if (buf == NULL) {
+               LOG_ERROR("unable to allocate CMSIS-DAP packet buffer");
+               return ERROR_FAIL;
+       }
+
+       dap->packet_buffer = buf;
+       dap->packet_size = pkt_sz;
+       dap->packet_buffer_size = packet_buffer_size;
+
+       return ERROR_OK;
+}
+
 COMMAND_HANDLER(cmsis_dap_handle_usb_interface_command)
 {
        if (CMD_ARGC == 1)
@@ -451,4 +481,5 @@ const struct cmsis_dap_backend cmsis_dap_usb_backend = {
        .close = cmsis_dap_usb_close,
        .read = cmsis_dap_usb_read,
        .write = cmsis_dap_usb_write,
+       .packet_buffer_alloc = cmsis_dap_usb_alloc,
 };