From 44e02e1f49cc09703cb3b4088d0c1c4f9e2d9c87 Mon Sep 17 00:00:00 2001 From: Tomas Vanek Date: Sun, 10 Dec 2023 11:58:43 +0100 Subject: [PATCH] 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: fd75e9e54270 (jtag/drivers/cmsis_dap_bulk: use asynchronous libusb transfer) Signed-off-by: Tomas Vanek Change-Id: I89660f6747ad9d494b8192711cbbee5764e058fa Reviewed-on: https://review.openocd.org/c/openocd/+/8044 Reviewed-by: Antonio Borneo Tested-by: jenkins --- src/jtag/drivers/cmsis_dap_usb_bulk.c | 41 ++++++++++++--------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/jtag/drivers/cmsis_dap_usb_bulk.c b/src/jtag/drivers/cmsis_dap_usb_bulk.c index 17e490f05a..92a972a04f 100644 --- a/src/jtag/drivers/cmsis_dap_usb_bulk.c +++ b/src/jtag/drivers/cmsis_dap_usb_bulk.c @@ -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);