diff --git a/src/hal/user_comps/xhc-whb04b-6/usb.cc b/src/hal/user_comps/xhc-whb04b-6/usb.cc index 8532376c372..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) { @@ -457,7 +460,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 +697,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); }