From a5be3a82a89c7a9f7b406613e7a889fe67376262 Mon Sep 17 00:00:00 2001 From: Per Nilsson Date: Fri, 27 Dec 2024 17:44:25 +0100 Subject: [PATCH] libusb: Improved debug messages --- lib/yubihsm_libusb.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/yubihsm_libusb.c b/lib/yubihsm_libusb.c index e83c9d3cc..13dbe1084 100644 --- a/lib/yubihsm_libusb.c +++ b/lib/yubihsm_libusb.c @@ -153,15 +153,20 @@ int usb_write(yh_backend *state, unsigned char *buf, long unsigned len) { /* TODO: does this need to loop and transmit several times? */ int ret = libusb_bulk_transfer(state->handle, 0x01, buf, len, &transferred, 0); - DBG_INFO("Write of %lu %d, err %d", len, transferred, ret); - if (ret != 0 || transferred != (int) len) { + DBG_INFO("Write of %lu %d, ret %d", len, transferred, ret); + if (ret != 0) { + DBG_ERR("Failed usb_write with ret: %d (%s)", ret, libusb_strerror(ret)); + return 0; + } else if (transferred != (int) len) { DBG_ERR("Transferred did not match len of write %d-%lu", transferred, len); return 0; } if (len % 64 == 0) { + transferred = 0; /* this writes the ZLP */ ret = libusb_bulk_transfer(state->handle, 0x01, buf, 0, &transferred, 0); if (ret != 0) { + DBG_ERR("Failed usb_write ZLP with ret: %d (%s)", ret, libusb_strerror(ret)); return 0; } } @@ -182,7 +187,7 @@ int usb_read(yh_backend *state, unsigned char *buf, unsigned long *len) { /* TODO: does this need to loop for all data?*/ ret = libusb_bulk_transfer(state->handle, 0x81, buf, *len, &transferred, 0); if (ret != 0) { - DBG_ERR("Failed usb_read with ret: %d", ret); + DBG_ERR("Failed usb_read with ret: %d (%s)", ret, libusb_strerror(ret)); return 0; } DBG_INFO("Read, transfer %d", transferred);