From d6261949247ae785c0cdf2ebbaa84850d24cf41f Mon Sep 17 00:00:00 2001 From: Hans Baier Date: Fri, 13 Nov 2026 15:31:41 +0700 Subject: [PATCH 1/2] xhc-whb04b-6: reconnect instead of aborting on libusb errors The component asserts on libusb return codes in three places, so any error the asserts do not list kills the process and takes its HAL pins with it. A USB port reset from userspace is enough to trigger it: transfer error: 5 transmission failed, try to reconnect ... Assertion `(0 == r) || (r == LIBUSB_ERROR_NO_DEVICE)' failed That path is also reachable after an unplug, a hub reset or a power dip. Log the unexpected code and let the existing reconnect logic do its work, which is what master already does: there, the checks moved into Usb::process() and no longer assert. A failed receive submit is handled the same way: it retries the connection instead of returning EXIT_FAILURE, which unloads the component. --- src/hal/user_comps/xhc-whb04b-6/usb.cc | 12 +++++-- .../user_comps/xhc-whb04b-6/xhc-whb04b6.cc | 32 +++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/hal/user_comps/xhc-whb04b-6/usb.cc b/src/hal/user_comps/xhc-whb04b-6/usb.cc index 8532376c372..49d550a5396 100644 --- a/src/hal/user_comps/xhc-whb04b-6/usb.cc +++ b/src/hal/user_comps/xhc-whb04b-6/usb.cc @@ -457,7 +457,12 @@ bool Usb::setupAsyncTransfer() //! timeout[ms] 750); int r = libusb_submit_transfer(inTransfer); - assert(0 == r); + // A submit can fail right after a reset; aborting here would take the HAL + // pins with it, so report it and let the caller reconnect. + if (r != LIBUSB_SUCCESS) + { + std::cerr << "libusb_submit_transfer returned " << r << endl; + } return (0 == r); } // ---------------------------------------------------------------------- @@ -689,7 +694,10 @@ bool Usb::init() if (libusb_kernel_driver_active(deviceHandle, 0) == 1) { int r = libusb_detach_kernel_driver(deviceHandle, 0); - assert(0 == r); + if (r != LIBUSB_SUCCESS) + { + std::cerr << "libusb_detach_kernel_driver returned " << r << endl; + } *verboseInitOut << " ok" << endl; } else diff --git a/src/hal/user_comps/xhc-whb04b-6/xhc-whb04b6.cc b/src/hal/user_comps/xhc-whb04b-6/xhc-whb04b6.cc index 5cd64d28382..eff7249d838 100644 --- a/src/hal/user_comps/xhc-whb04b-6/xhc-whb04b6.cc +++ b/src/hal/user_comps/xhc-whb04b-6/xhc-whb04b6.cc @@ -450,8 +450,14 @@ int XhcWhb04b6Component::run() *mInitCout << "init enabling reception ..."; if (!enableReceiveAsyncTransfer()) { - std::cerr << endl << "failed to enable reception" << endl; - return EXIT_FAILURE; + // Do not exit: a receiver that is still settling after a reset + // fails here, and exiting would unload the component. + std::cerr << endl << "failed to enable reception, retrying" << endl; + libusb_close(getUsbDeviceHandle()); + mUsb.setDeviceHandle(nullptr); + teardownUsb(); + mUsb.setDoReconnect(true); + continue; } *mInitCout << " ok" << endl; } @@ -505,8 +511,16 @@ void XhcWhb04b6Component::process() timeout.tv_usec = 200 * 1000; int r = libusb_handle_events_timeout_completed(getUsbContext(), &timeout, nullptr); - assert((r == LIBUSB_SUCCESS) || (r == LIBUSB_ERROR_NO_DEVICE) || (r == LIBUSB_ERROR_BUSY) || - (r == LIBUSB_ERROR_TIMEOUT) || (r == LIBUSB_ERROR_INTERRUPTED)); + // Return codes that a reset or an unplug produces (NOT_FOUND, IO) + // are not in the original assert; it aborted the component instead + // of reconnecting. Log anything unexpected and reconnect. + if (r != LIBUSB_SUCCESS && r != LIBUSB_ERROR_NO_DEVICE && r != LIBUSB_ERROR_BUSY && + r != LIBUSB_ERROR_TIMEOUT && r != LIBUSB_ERROR_INTERRUPTED && r != LIBUSB_ERROR_NOT_FOUND && + r != LIBUSB_ERROR_IO) + { + std::cerr << "handle_events returned " << r << ", reconnecting" << endl; + mUsb.setDoReconnect(true); + } if (mHal.isSimulationModeEnabled()) { linuxcncSimulate(); @@ -521,9 +535,15 @@ void XhcWhb04b6Component::process() tv.tv_sec = 1; tv.tv_usec = 0; int r = libusb_handle_events_timeout_completed(getUsbContext(), &tv, nullptr); - assert(0 == r); + if (r != LIBUSB_SUCCESS) + { + std::cerr << "cleanup: handle_events returned " << r << endl; + } r = libusb_release_interface(getUsbDeviceHandle(), 0); - assert((0 == r) || (r == LIBUSB_ERROR_NO_DEVICE)); + if (r != LIBUSB_SUCCESS && r != LIBUSB_ERROR_NO_DEVICE) + { + std::cerr << "cleanup: release_interface returned " << r << endl; + } libusb_close(getUsbDeviceHandle()); mUsb.setDeviceHandle(nullptr); } From a7f14d8cb8d4e2f2c0c60d83b602f29ddfffb1ca Mon Sep 17 00:00:00 2001 From: Hans Baier Date: Fri, 13 Nov 2026 15:31:45 +0700 Subject: [PATCH 2/2] xhc-whb04b-6: bound the display control transfer timeout Usb::sendDisplayData() passes timeout 0 to libusb_control_transfer(), and libusb reads 0 as "wait forever". The call runs on the thread that also drains USB events for the jog dial, so when a receiver stops completing the transfer the handwheel stops responding with no error, no log line, and pendant.is-connected still TRUE. Unplugging the receiver is the only way out, because the disconnect finally makes the blocked transfer fail. A one second deadline turns that stall into an error the reconnect path can act on. A healthy transfer completes in well under a millisecond, so the deadline costs nothing in normal operation. --- src/hal/user_comps/xhc-whb04b-6/usb.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/hal/user_comps/xhc-whb04b-6/usb.cc b/src/hal/user_comps/xhc-whb04b-6/usb.cc index 49d550a5396..5570202db7a 100644 --- a/src/hal/user_comps/xhc-whb04b-6/usb.cc +++ b/src/hal/user_comps/xhc-whb04b-6/usb.cc @@ -229,8 +229,11 @@ void Usb::sendDisplayData() block.asBuffer.asBytes, // wLength, data length blockSize, - // transfer timeout[ms] - 0); + // transfer timeout[ms]: libusb reads 0 as "wait forever", which + // blocks this thread - and the jog dial with it - when a receiver + // stops completing the transfer. A deadline turns that stall into + // an error the reconnect path can act on. + 1000); if (r < 0) {