usbDsWaitReady: use deadline timeout

This commit is contained in:
Michael Theall 2020-12-04 19:40:38 -06:00 committed by fincs
parent f82d97f88a
commit 1e9fe9b199

View File

@ -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;
}