From 7333864957dc3aaa240d08facbf0e192d5412b8a Mon Sep 17 00:00:00 2001 From: yellows8 Date: Tue, 10 Oct 2017 11:48:39 -0400 Subject: [PATCH] Added usbDsGetState() and usbDsWaitReady(). --- nx/include/switch/services/usb.h | 4 +++ nx/source/services/usb.c | 53 ++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/nx/include/switch/services/usb.h b/nx/include/switch/services/usb.h index f6170017..5e350bb5 100644 --- a/nx/include/switch/services/usb.h +++ b/nx/include/switch/services/usb.h @@ -193,8 +193,12 @@ void usbDsExit(void); Handle usbDsGetServiceSession(void); Handle usbDsGetStateChangeEvent(void); +Result usbDsGetState(u32 *out); Result usbDsGetDsInterface(UsbDsInterface** interface, struct usb_interface_descriptor* descriptor, const char *interface_name); +/// Wait for initialization to finish where data-transfer is usable. +Result usbDsWaitReady(void); + /// IDsInterface void usbDsInterface_Close(UsbDsInterface* interface); Result usbDsInterface_GetDsEndpoint(UsbDsInterface* interface, UsbDsEndpoint** endpoint, struct usb_endpoint_descriptor* descriptor); diff --git a/nx/source/services/usb.c b/nx/source/services/usb.c index 6847424c..12159903 100644 --- a/nx/source/services/usb.c +++ b/nx/source/services/usb.c @@ -271,6 +271,59 @@ static Result _usbDsGetEvent(Handle sessionhandle, Handle* handle_out, u64 cmd_i return rc; } +Result usbDsGetState(u32 *out) { + if(g_usbDsServiceSession==0)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED); + + IpcCommand c; + ipcInitialize(&c); + + struct { + u64 magic; + u64 cmd_id; + } *raw; + + raw = ipcPrepareHeader(&c, sizeof(*raw)); + + raw->magic = SFCI_MAGIC; + raw->cmd_id = 4; + + Result rc = ipcDispatch(g_usbDsServiceSession); + + if (R_SUCCEEDED(rc)) { + IpcCommandResponse r; + ipcParseResponse(&r); + + struct { + u64 magic; + u64 result; + u32 out; + } *resp = r.Raw; + + rc = resp->result; + if (R_SUCCEEDED(rc) && out)*out = resp->out; + } + + return rc; +} + +Result usbDsWaitReady(void) { + Result rc = 0; + u32 state=0; + s32 tmpindex = 0; + + rc = usbDsGetState(&state); + if (R_FAILED(rc)) return rc; + + while(R_SUCCEEDED(rc) && state!=5) + { + svcWaitSynchronization(&tmpindex, &g_usbDsStateChangeEvent, 1, U64_MAX); + svcClearEvent(g_usbDsStateChangeEvent); + rc = usbDsGetState(&state); + } + + return rc; +} + static Result _usbDsSetVidPidBcd(const usbDsDeviceInfo* deviceinfo) { if(g_usbDsServiceSession==0)return MAKERESULT(MODULE_LIBNX, LIBNX_NOTINITIALIZED);