openocd: fix SPDX tag format for files .c
[openocd.git] / src / jtag / drivers / cmsis_dap_usb_bulk.c
index 0134c0376557808ab0cdfe28797a13c3a9da5b8a..a738200ea57d878bd35f5ef25c6e873edc24ef19 100644 (file)
@@ -1,3 +1,5 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /***************************************************************************
  *   Copyright (C) 2018 by MickaĆ«l Thomas                                  *
  *   mickael9@gmail.com                                                    *
  *                                                                         *
  *   Copyright (C) 2013 by Spencer Oliver                                  *
  *   spen@spen-soft.co.uk                                                  *
- *                                                                         *
- *   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
 #include "config.h"
 #endif
 
+#include <helper/system.h>
 #include <libusb.h>
 #include <helper/log.h>
+#include <helper/replacements.h>
 
 #include "cmsis_dap.h"
 
 struct cmsis_dap_backend_data {
-       libusb_context *usb_ctx;
-       libusb_device_handle *dev_handle;
+       struct libusb_context *usb_ctx;
+       struct libusb_device_handle *dev_handle;
        unsigned int ep_out;
        unsigned int ep_in;
        int interface;
@@ -50,11 +41,14 @@ struct cmsis_dap_backend_data {
 
 static int cmsis_dap_usb_interface = -1;
 
-static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t pids[], char *serial)
+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[], const char *serial)
 {
        int err;
-       libusb_context *ctx;
-       libusb_device **device_list;
+       struct libusb_context *ctx;
+       struct libusb_device **device_list;
 
        err = libusb_init(&ctx);
        if (err) {
@@ -70,7 +64,7 @@ static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t p
        }
 
        for (int i = 0; i < num_devices; i++) {
-               libusb_device *dev = device_list[i];
+               struct libusb_device *dev = device_list[i];
                struct libusb_device_descriptor dev_desc;
 
                err = libusb_get_device_descriptor(dev, &dev_desc);
@@ -98,7 +92,7 @@ static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t p
                if (dev_desc.iSerialNumber == 0 && serial && serial[0])
                        continue;
 
-               libusb_device_handle *dev_handle = NULL;
+               struct libusb_device_handle *dev_handle = NULL;
                err = libusb_open(dev, &dev_handle);
                if (err) {
                        /* It's to be expected that most USB devices can't be opened
@@ -257,7 +251,7 @@ static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t p
                                 * - config asked explicitly for an interface number
                                 * - the device has only one interface
                                 * The later two cases should be honored only if we know
-                                * we are on the rigt device */
+                                * we are on the right device */
                                bool intf_identified_reliably = cmsis_dap_in_interface_str
                                                        || (device_identified_reliably &&
                                                                        (cmsis_dap_usb_interface != -1
@@ -348,7 +342,7 @@ static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t p
                                LOG_WARNING("could not claim interface: %s", libusb_strerror(err));
 
                        dap->bdata = malloc(sizeof(struct cmsis_dap_backend_data));
-                       if (dap->bdata == NULL) {
+                       if (!dap->bdata) {
                                LOG_ERROR("unable to allocate memory");
                                libusb_release_interface(dev_handle, interface_num);
                                libusb_close(dev_handle);
@@ -356,12 +350,24 @@ 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;
                        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) {
+                               LOG_ERROR("unable to allocate memory");
+                               cmsis_dap_usb_close(dap);
+                               return ERROR_FAIL;
+                       }
+
+                       dap->command = dap->packet_buffer;
+                       dap->response = dap->packet_buffer;
+
                        return ERROR_OK;
                }
 
@@ -381,6 +387,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 +407,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 +419,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, txlen, &transferred, timeout_ms);
        if (err) {
                if (err == LIBUSB_ERROR_TIMEOUT) {
                        return ERROR_TIMEOUT_REACHED;
@@ -424,10 +432,28 @@ 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)
+{
+       uint8_t *buf = malloc(pkt_sz);
+       if (!buf) {
+               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 = pkt_sz;
+
+       dap->command = dap->packet_buffer;
+       dap->response = dap->packet_buffer;
+
+       return ERROR_OK;
+}
+
 COMMAND_HANDLER(cmsis_dap_handle_usb_interface_command)
 {
        if (CMD_ARGC == 1)
-               cmsis_dap_usb_interface = strtoul(CMD_ARGV[0], NULL, 10);
+               COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], cmsis_dap_usb_interface);
        else
                LOG_ERROR("expected exactly one argument to cmsis_dap_usb_interface <interface_number>");
 
@@ -451,4 +477,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,
 };

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)