jtag/drivers/cmsis_dap: use oocd_libusb_dev_mem_alloc() helper

On some systems (at least Windows/CYGWIN and macOS) libusb_dev_mem_alloc()
simply returns NULL. The helper can fall-back to malloc() to allocate
CMSIS-DAP pending command/response buffers.

Fixes: fd75e9e542 (jtag/drivers/cmsis_dap_bulk: use asynchronous libusb transfer)
Signed-off-by: Tomas Vanek <vanekt@fbl.cz>
Change-Id: I89660f6747ad9d494b8192711cbbee5764e058fa
Reviewed-on: https://review.openocd.org/c/openocd/+/8044
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
Tested-by: jenkins
This commit is contained in:
Tomas Vanek 2023-12-10 11:58:43 +01:00
parent 15f74c2595
commit 44e02e1f49
1 changed files with 18 additions and 23 deletions

View File

@ -33,12 +33,6 @@
#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,
@ -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);