+// 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"
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)
+static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t pids[], const char *serial)
{
int err;
struct libusb_context *ctx;
* - 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
/* If the interface is reliably identified
* then we need not insist on setting USB class, subclass and protocol
* exactly as the specification requires.
+ * Just filter out the well known classes, mainly CDC and MSC.
* At least KitProg3 uses class 0 contrary to the specification */
- if (intf_identified_reliably) {
+ if (intf_identified_reliably &&
+ (intf_desc->bInterfaceClass == 0 || intf_desc->bInterfaceClass > 0x12)) {
LOG_WARNING("Using CMSIS-DAPv2 interface %d with wrong class %" PRId8
" subclass %" PRId8 " or protocol %" PRId8,
interface_num,
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);
return ERROR_FAIL;
}
- 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 == NULL) {
- LOG_ERROR("unable to allocate memory");
+ err = cmsis_dap_usb_alloc(dap, packet_size);
+ if (err != ERROR_OK)
cmsis_dap_usb_close(dap);
- return ERROR_FAIL;
- }
-
- dap->command = dap->packet_buffer;
- dap->response = dap->packet_buffer;
- return ERROR_OK;
+ return err;
}
libusb_close(dev_handle);
static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz)
{
uint8_t *buf = malloc(pkt_sz);
- if (buf == NULL) {
+ 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;
+ /* Prevent sending zero size USB packets */
+ dap->packet_usable_size = pkt_sz - 1;
dap->command = dap->packet_buffer;
dap->response = dap->packet_buffer;
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>");