From 1e9fe9b19942b86efdfbf08f669756d8cb0ec87c Mon Sep 17 00:00:00 2001 From: Michael Theall Date: Fri, 4 Dec 2020 19:40:38 -0600 Subject: [PATCH] usbDsWaitReady: use deadline timeout --- nx/source/services/usbds.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/nx/source/services/usbds.c b/nx/source/services/usbds.c index b5f8c357..cc67e846 100644 --- a/nx/source/services/usbds.c +++ b/nx/source/services/usbds.c @@ -4,6 +4,7 @@ #include "services/usbds.h" #include "runtime/hosversion.h" #include "runtime/util/utf.h" +#include "switch/arm/counter.h" #define TOTAL_INTERFACES 4 #define TOTAL_ENDPOINTS_IN 16 @@ -234,12 +235,27 @@ Result usbDsWaitReady(u64 timeout) { rc = usbDsGetState(&state); if (R_FAILED(rc)) return rc; + if (state == UsbState_Configured) return 0; + + bool has_timeout = timeout != UINT64_MAX; + u64 deadline = 0; + + if (has_timeout) + deadline = armGetSystemTick() + armNsToTicks(timeout); + + do { + if (has_timeout) { + s64 remaining = deadline - armGetSystemTick(); + timeout = remaining > 0 ? armTicksToNs(remaining) : 0; + } - while (R_SUCCEEDED(rc) && state != UsbState_Configured) { eventWait(&g_usbDsStateChangeEvent, timeout); eventClear(&g_usbDsStateChangeEvent); rc = usbDsGetState(&state); - } + } while (R_SUCCEEDED(rc) && state != UsbState_Configured && timeout > 0); + + if (R_SUCCEEDED(rc) && state != UsbState_Configured && timeout == 0) + return KERNELRESULT(TimedOut); return rc; } @@ -259,7 +275,7 @@ Result usbDsParseReportData(UsbDsReportData *reportdata, u32 urbId, u32 *request if (pos == count) return MAKERESULT(Module_Libnx, LibnxError_NotFound); switch(entry->urb_status) { - case 0x3: + case 0x3: rc = 0; break;