2020-05-05 16:22:16 +01:00
|
|
|
/*
|
2020-07-03 10:31:22 +01:00
|
|
|
* usb.c
|
2020-05-05 16:22:16 +01:00
|
|
|
*
|
2020-07-03 10:31:22 +01:00
|
|
|
* Heavily based in usb_comms from libnx.
|
|
|
|
*
|
2020-07-06 01:10:07 +01:00
|
|
|
* Copyright (c) 2018-2020, Switchbrew and libnx contributors.
|
2020-07-03 10:31:22 +01:00
|
|
|
* Copyright (c) 2020, DarkMatterCore <pabloacurielz@gmail.com>.
|
|
|
|
*
|
|
|
|
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
|
|
|
|
*
|
|
|
|
* nxdumptool is free software; you can redistribute it and/or modify it
|
2020-05-05 16:22:16 +01:00
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
2020-07-03 10:31:22 +01:00
|
|
|
* nxdumptool is distributed in the hope it will be useful, but WITHOUT
|
2020-05-05 16:22:16 +01:00
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "utils.h"
|
2020-07-03 10:31:22 +01:00
|
|
|
#include "usb.h"
|
2020-05-05 16:22:16 +01:00
|
|
|
|
2020-05-06 07:01:00 +01:00
|
|
|
#define USB_ABI_VERSION 1
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
#define USB_CMD_HEADER_MAGIC 0x4E584454 /* "NXDT". */
|
2020-05-05 16:22:16 +01:00
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
#define USB_TRANSFER_ALIGNMENT 0x1000 /* 4 KiB. */
|
|
|
|
#define USB_TRANSFER_TIMEOUT 5 /* 5 seconds. */
|
2020-05-05 16:22:16 +01:00
|
|
|
|
|
|
|
/* Type definitions. */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
RwLock lock, lock_in, lock_out;
|
|
|
|
bool initialized;
|
|
|
|
UsbDsInterface *interface;
|
|
|
|
UsbDsEndpoint *endpoint_in, *endpoint_out;
|
|
|
|
} usbDeviceInterface;
|
|
|
|
|
|
|
|
typedef enum {
|
2020-05-08 04:48:22 +01:00
|
|
|
UsbCommandType_StartSession = 0,
|
|
|
|
UsbCommandType_SendFileProperties = 1,
|
2020-05-13 15:09:51 +01:00
|
|
|
UsbCommandType_SendNspHeader = 2, ///< Needs to be implemented.
|
2020-05-08 04:48:22 +01:00
|
|
|
UsbCommandType_EndSession = 3
|
2020-05-05 16:22:16 +01:00
|
|
|
} UsbCommandType;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
u32 magic;
|
|
|
|
u32 cmd;
|
|
|
|
u32 cmd_block_size;
|
|
|
|
u8 reserved[0x4];
|
|
|
|
} UsbCommandHeader;
|
|
|
|
|
|
|
|
typedef struct {
|
2020-05-06 07:01:00 +01:00
|
|
|
u8 app_ver_major;
|
|
|
|
u8 app_ver_minor;
|
|
|
|
u8 app_ver_micro;
|
|
|
|
u8 abi_version;
|
|
|
|
u8 reserved[0xC];
|
2020-05-08 04:48:22 +01:00
|
|
|
} UsbCommandStartSession;
|
2020-05-05 16:22:16 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
u64 file_size;
|
|
|
|
u32 filename_length;
|
|
|
|
u8 reserved_1[0x4];
|
|
|
|
char filename[FS_MAX_PATH];
|
|
|
|
u8 reserved_2[0xF];
|
|
|
|
} UsbCommandSendFileProperties;
|
|
|
|
|
|
|
|
typedef enum {
|
2020-07-03 10:31:22 +01:00
|
|
|
///< Expected response code.
|
2020-05-07 12:08:54 +01:00
|
|
|
UsbStatusType_Success = 0,
|
2020-05-05 16:22:16 +01:00
|
|
|
|
2020-07-03 10:31:22 +01:00
|
|
|
///< Internal usage.
|
2020-05-07 12:08:54 +01:00
|
|
|
UsbStatusType_InvalidCommandSize = 1,
|
|
|
|
UsbStatusType_WriteCommandFailed = 2,
|
|
|
|
UsbStatusType_ReadStatusFailed = 3,
|
2020-05-05 16:22:16 +01:00
|
|
|
|
2020-07-03 10:31:22 +01:00
|
|
|
///< These can be returned by the host device.
|
2020-05-09 05:48:46 +01:00
|
|
|
UsbStatusType_InvalidMagicWord = 4,
|
|
|
|
UsbStatusType_UnsupportedCommand = 5,
|
2020-05-07 12:08:54 +01:00
|
|
|
UsbStatusType_UnsupportedAbiVersion = 6,
|
2020-05-09 07:32:01 +01:00
|
|
|
UsbStatusType_MalformedCommand = 7,
|
|
|
|
UsbStatusType_HostIoError = 8
|
2020-05-05 16:22:16 +01:00
|
|
|
} UsbStatusType;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
u32 magic;
|
|
|
|
u32 status;
|
|
|
|
u8 reserved[0x8];
|
|
|
|
} UsbStatus;
|
|
|
|
|
|
|
|
/* Global variables. */
|
|
|
|
|
|
|
|
static RwLock g_usbDeviceLock = {0};
|
2020-05-13 15:09:51 +01:00
|
|
|
static usbDeviceInterface g_usbDeviceInterface = {0};
|
|
|
|
static bool g_usbDeviceInterfaceInitialized = false;
|
2020-05-05 16:22:16 +01:00
|
|
|
|
2020-05-13 15:09:51 +01:00
|
|
|
static Event *g_usbStateChangeEvent = NULL;
|
|
|
|
static thrd_t g_usbDetectionThread;
|
2020-07-03 10:31:22 +01:00
|
|
|
static UEvent g_usbDetectionThreadExitEvent = {0}, g_usbTimeoutEvent = {0};
|
2020-05-13 15:09:51 +01:00
|
|
|
static bool g_usbDetectionThreadCreated = false, g_usbHostAvailable = false, g_usbSessionStarted = false;
|
2020-05-08 04:48:22 +01:00
|
|
|
|
2020-05-05 16:22:16 +01:00
|
|
|
static u8 *g_usbTransferBuffer = NULL;
|
|
|
|
static u64 g_usbTransferRemainingSize = 0;
|
|
|
|
|
|
|
|
/* Function prototypes. */
|
|
|
|
|
2020-05-13 15:09:51 +01:00
|
|
|
static bool usbCreateDetectionThread(void);
|
|
|
|
static void usbDestroyDetectionThread(void);
|
|
|
|
static int usbDetectionThreadFunc(void *arg);
|
|
|
|
|
|
|
|
static bool usbStartSession(void);
|
|
|
|
static void usbEndSession(void);
|
2020-05-07 12:08:54 +01:00
|
|
|
|
2020-05-05 16:22:16 +01:00
|
|
|
NX_INLINE void usbPrepareCommandHeader(u32 cmd, u32 cmd_block_size);
|
|
|
|
static u32 usbSendCommand(size_t cmd_size);
|
|
|
|
NX_INLINE void usbLogStatusDetail(u32 status);
|
|
|
|
|
|
|
|
NX_INLINE bool usbAllocateTransferBuffer(void);
|
|
|
|
NX_INLINE void usbFreeTransferBuffer(void);
|
|
|
|
|
|
|
|
static bool usbInitializeComms(void);
|
|
|
|
static void usbCloseComms(void);
|
|
|
|
|
|
|
|
static void usbFreeDeviceInterface(void);
|
|
|
|
|
|
|
|
NX_INLINE bool usbInitializeDeviceInterface(void);
|
|
|
|
static bool usbInitializeDeviceInterface5x(void);
|
|
|
|
static bool usbInitializeDeviceInterface1x(void);
|
|
|
|
|
2020-05-07 12:08:54 +01:00
|
|
|
NX_INLINE bool usbIsHostAvailable(void);
|
|
|
|
|
|
|
|
NX_INLINE bool usbRead(void *buf, size_t size);
|
|
|
|
NX_INLINE bool usbWrite(void *buf, size_t size);
|
2020-05-05 16:22:16 +01:00
|
|
|
static bool usbTransferData(void *buf, size_t size, UsbDsEndpoint *endpoint);
|
|
|
|
|
|
|
|
bool usbInitialize(void)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
rwlockWriteLock(&g_usbDeviceLock);
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Allocate USB transfer buffer. */
|
2020-05-05 16:22:16 +01:00
|
|
|
if (!usbAllocateTransferBuffer())
|
|
|
|
{
|
|
|
|
LOGFILE("Failed to allocate memory for the USB transfer buffer!");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Initialize USB device interface. */
|
2020-05-05 16:22:16 +01:00
|
|
|
if (!usbInitializeComms())
|
|
|
|
{
|
|
|
|
LOGFILE("Failed to initialize USB device interface!");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Retrieve USB state change kernel event. */
|
2020-05-13 15:09:51 +01:00
|
|
|
g_usbStateChangeEvent = usbDsGetStateChangeEvent();
|
|
|
|
if (!g_usbStateChangeEvent)
|
|
|
|
{
|
|
|
|
LOGFILE("Failed to retrieve USB state change kernel event!");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Create usermode exit event. */
|
2020-05-13 15:09:51 +01:00
|
|
|
ueventCreate(&g_usbDetectionThreadExitEvent, true);
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Create usermode USB timeout event. */
|
2020-07-03 10:31:22 +01:00
|
|
|
ueventCreate(&g_usbTimeoutEvent, true);
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Create USB detection thread. */
|
2020-05-13 15:09:51 +01:00
|
|
|
if (!(g_usbDetectionThreadCreated = usbCreateDetectionThread())) goto exit;
|
|
|
|
|
2020-05-05 16:22:16 +01:00
|
|
|
ret = true;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
rwlockWriteUnlock(&g_usbDeviceLock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void usbExit(void)
|
|
|
|
{
|
|
|
|
rwlockWriteLock(&g_usbDeviceLock);
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Destroy USB detection thread. */
|
2020-05-13 15:09:51 +01:00
|
|
|
if (g_usbDetectionThreadCreated)
|
|
|
|
{
|
|
|
|
usbDestroyDetectionThread();
|
|
|
|
g_usbDetectionThreadCreated = false;
|
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Clear USB state change kernel event. */
|
2020-05-13 15:09:51 +01:00
|
|
|
g_usbStateChangeEvent = NULL;
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Close USB device interface. */
|
2020-05-05 16:22:16 +01:00
|
|
|
usbCloseComms();
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Free USB transfer buffer. */
|
2020-05-05 16:22:16 +01:00
|
|
|
usbFreeTransferBuffer();
|
|
|
|
|
|
|
|
rwlockWriteUnlock(&g_usbDeviceLock);
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:08:54 +01:00
|
|
|
void *usbAllocatePageAlignedBuffer(size_t size)
|
2020-05-05 16:22:16 +01:00
|
|
|
{
|
2020-05-07 12:08:54 +01:00
|
|
|
if (!size) return NULL;
|
|
|
|
return memalign(USB_TRANSFER_ALIGNMENT, size);
|
2020-05-05 16:22:16 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 15:09:51 +01:00
|
|
|
bool usbIsReady(void)
|
2020-05-05 16:22:16 +01:00
|
|
|
{
|
|
|
|
rwlockWriteLock(&g_usbDeviceLock);
|
|
|
|
rwlockWriteLock(&(g_usbDeviceInterface.lock));
|
2020-05-13 15:09:51 +01:00
|
|
|
bool ret = (g_usbHostAvailable && g_usbSessionStarted);
|
2020-05-05 16:22:16 +01:00
|
|
|
rwlockWriteUnlock(&(g_usbDeviceInterface.lock));
|
|
|
|
rwlockWriteUnlock(&g_usbDeviceLock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool usbSendFileProperties(u64 file_size, const char *filename)
|
|
|
|
{
|
|
|
|
rwlockWriteLock(&g_usbDeviceLock);
|
|
|
|
rwlockWriteLock(&(g_usbDeviceInterface.lock));
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
UsbCommandSendFileProperties *cmd_block = NULL;
|
|
|
|
size_t cmd_size = 0;
|
|
|
|
u32 status = UsbStatusType_Success;
|
|
|
|
u32 filename_length = 0;
|
|
|
|
|
2020-05-13 15:09:51 +01:00
|
|
|
if (!g_usbTransferBuffer || !g_usbDeviceInterfaceInitialized || !g_usbDeviceInterface.initialized || !g_usbHostAvailable || !g_usbSessionStarted || g_usbTransferRemainingSize > 0 || !filename || \
|
2020-05-05 16:22:16 +01:00
|
|
|
!(filename_length = (u32)strlen(filename)) || filename_length >= FS_MAX_PATH)
|
|
|
|
{
|
|
|
|
LOGFILE("Invalid parameters!");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
usbPrepareCommandHeader(UsbCommandType_SendFileProperties, (u32)sizeof(UsbCommandSendFileProperties));
|
|
|
|
|
|
|
|
cmd_block = (UsbCommandSendFileProperties*)(g_usbTransferBuffer + sizeof(UsbCommandHeader));
|
|
|
|
memset(cmd_block, 0, sizeof(UsbCommandSendFileProperties));
|
2020-05-06 15:04:10 +01:00
|
|
|
|
2020-05-05 16:22:16 +01:00
|
|
|
cmd_block->file_size = file_size;
|
|
|
|
cmd_block->filename_length = filename_length;
|
2020-07-03 10:31:22 +01:00
|
|
|
sprintf(cmd_block->filename, "%s", filename);
|
2020-05-05 16:22:16 +01:00
|
|
|
|
|
|
|
cmd_size = (sizeof(UsbCommandHeader) + sizeof(UsbCommandSendFileProperties));
|
|
|
|
|
|
|
|
status = usbSendCommand(cmd_size);
|
|
|
|
if (status == UsbStatusType_Success)
|
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
g_usbTransferRemainingSize = file_size;
|
|
|
|
} else {
|
|
|
|
usbLogStatusDetail(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
rwlockWriteUnlock(&(g_usbDeviceInterface.lock));
|
|
|
|
rwlockWriteUnlock(&g_usbDeviceLock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:08:54 +01:00
|
|
|
bool usbSendFileData(void *data, u64 data_size)
|
2020-05-05 16:22:16 +01:00
|
|
|
{
|
|
|
|
rwlockWriteLock(&g_usbDeviceLock);
|
|
|
|
rwlockWriteLock(&(g_usbDeviceInterface.lock));
|
|
|
|
|
|
|
|
bool ret = false;
|
2020-05-06 15:04:10 +01:00
|
|
|
void *buf = NULL;
|
|
|
|
UsbStatus *cmd_status = NULL;
|
2020-05-05 16:22:16 +01:00
|
|
|
|
2020-05-13 15:09:51 +01:00
|
|
|
if (!g_usbTransferBuffer || !g_usbDeviceInterfaceInitialized || !g_usbDeviceInterface.initialized || !g_usbHostAvailable || !g_usbSessionStarted || !g_usbTransferRemainingSize || !data || \
|
|
|
|
!data_size || data_size > USB_TRANSFER_BUFFER_SIZE || data_size > g_usbTransferRemainingSize)
|
2020-05-05 16:22:16 +01:00
|
|
|
{
|
|
|
|
LOGFILE("Invalid parameters!");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Optimization for buffers that already are page aligned. */
|
2020-05-11 20:12:03 +01:00
|
|
|
if (IS_ALIGNED((u64)data, USB_TRANSFER_ALIGNMENT))
|
2020-05-06 15:04:10 +01:00
|
|
|
{
|
|
|
|
buf = data;
|
|
|
|
} else {
|
|
|
|
buf = g_usbTransferBuffer;
|
|
|
|
memcpy(buf, data, data_size);
|
|
|
|
}
|
2020-05-05 16:22:16 +01:00
|
|
|
|
2020-05-06 15:04:10 +01:00
|
|
|
if (!usbWrite(buf, data_size))
|
2020-05-05 16:22:16 +01:00
|
|
|
{
|
2020-05-06 15:04:10 +01:00
|
|
|
LOGFILE("Failed to write 0x%lX bytes long file data chunk!", data_size);
|
2020-05-05 16:22:16 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
g_usbTransferRemainingSize -= data_size;
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Check if this is the last chunk. */
|
2020-05-05 16:22:16 +01:00
|
|
|
if (!g_usbTransferRemainingSize)
|
|
|
|
{
|
2020-05-06 15:04:10 +01:00
|
|
|
if (!usbRead(g_usbTransferBuffer, sizeof(UsbStatus)))
|
2020-05-05 16:22:16 +01:00
|
|
|
{
|
2020-05-06 15:04:10 +01:00
|
|
|
LOGFILE("Failed to read 0x%lX bytes long status block!", sizeof(UsbStatus));
|
2020-05-05 16:22:16 +01:00
|
|
|
ret = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd_status = (UsbStatus*)g_usbTransferBuffer;
|
|
|
|
|
|
|
|
if (cmd_status->magic != __builtin_bswap32(USB_CMD_HEADER_MAGIC))
|
|
|
|
{
|
|
|
|
LOGFILE("Invalid status block magic word!");
|
|
|
|
ret = false;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = (cmd_status->status == UsbStatusType_Success);
|
|
|
|
if (!ret) usbLogStatusDetail(cmd_status->status);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if (!ret) g_usbTransferRemainingSize = 0;
|
|
|
|
|
|
|
|
rwlockWriteUnlock(&(g_usbDeviceInterface.lock));
|
|
|
|
rwlockWriteUnlock(&g_usbDeviceLock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-13 15:09:51 +01:00
|
|
|
static bool usbCreateDetectionThread(void)
|
2020-05-08 04:48:22 +01:00
|
|
|
{
|
2020-05-13 15:09:51 +01:00
|
|
|
if (thrd_create(&g_usbDetectionThread, usbDetectionThreadFunc, NULL) != thrd_success)
|
2020-05-08 04:48:22 +01:00
|
|
|
{
|
2020-05-13 15:09:51 +01:00
|
|
|
LOGFILE("Failed to create USB detection thread!");
|
|
|
|
return false;
|
2020-05-08 04:48:22 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 15:09:51 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usbDestroyDetectionThread(void)
|
|
|
|
{
|
|
|
|
/* Signal the exit event to terminate the USB detection thread */
|
|
|
|
ueventSignal(&g_usbDetectionThreadExitEvent);
|
2020-05-08 04:48:22 +01:00
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Wait for the USB detection thread to exit. */
|
2020-05-13 15:09:51 +01:00
|
|
|
thrd_join(g_usbDetectionThread, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usbDetectionThreadFunc(void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
2020-05-08 04:48:22 +01:00
|
|
|
|
2020-05-13 15:09:51 +01:00
|
|
|
Result rc = 0;
|
|
|
|
int idx = 0;
|
2020-05-08 04:48:22 +01:00
|
|
|
|
2020-07-03 10:31:22 +01:00
|
|
|
Waiter usb_change_event_waiter = waiterForEvent(g_usbStateChangeEvent);
|
|
|
|
Waiter usb_timeout_event_waiter = waiterForUEvent(&g_usbTimeoutEvent);
|
2020-05-13 15:09:51 +01:00
|
|
|
Waiter exit_event_waiter = waiterForUEvent(&g_usbDetectionThreadExitEvent);
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Wait until an event is triggered. */
|
2020-07-03 10:31:22 +01:00
|
|
|
rc = waitMulti(&idx, -1, usb_change_event_waiter, usb_timeout_event_waiter, exit_event_waiter);
|
2020-05-13 15:09:51 +01:00
|
|
|
if (R_FAILED(rc)) continue;
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Exit event triggered. */
|
2020-07-03 10:31:22 +01:00
|
|
|
if (idx == 2) break;
|
2020-05-13 15:09:51 +01:00
|
|
|
|
|
|
|
rwlockWriteLock(&g_usbDeviceLock);
|
|
|
|
rwlockWriteLock(&(g_usbDeviceInterface.lock));
|
|
|
|
|
2020-07-07 17:39:20 +01:00
|
|
|
/* Retrieve current USB connection status. */
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Only proceed if we're dealing with a status change. */
|
2020-05-13 15:09:51 +01:00
|
|
|
g_usbHostAvailable = usbIsHostAvailable();
|
2020-07-03 10:31:22 +01:00
|
|
|
g_usbSessionStarted = false;
|
2020-05-13 15:09:51 +01:00
|
|
|
g_usbTransferRemainingSize = 0;
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Start a USB session if we're connected to a host device and if the status change event was triggered. */
|
|
|
|
/* This will essentially hang this thread and all other threads that call USB-related functions until a session is established. */
|
2020-07-07 17:39:20 +01:00
|
|
|
if (g_usbHostAvailable && idx == 0) g_usbSessionStarted = usbStartSession();
|
2020-05-13 15:09:51 +01:00
|
|
|
|
|
|
|
rwlockWriteUnlock(&(g_usbDeviceInterface.lock));
|
|
|
|
rwlockWriteUnlock(&g_usbDeviceLock);
|
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Close USB session if needed. */
|
2020-05-13 15:09:51 +01:00
|
|
|
if (g_usbHostAvailable && g_usbSessionStarted) usbEndSession();
|
|
|
|
g_usbHostAvailable = g_usbSessionStarted = false;
|
2020-07-03 10:31:22 +01:00
|
|
|
g_usbTransferRemainingSize = 0;
|
2020-05-13 15:09:51 +01:00
|
|
|
|
|
|
|
return 0;
|
2020-05-08 04:48:22 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 15:09:51 +01:00
|
|
|
static bool usbStartSession(void)
|
2020-05-07 12:08:54 +01:00
|
|
|
{
|
2020-05-08 04:48:22 +01:00
|
|
|
UsbCommandStartSession *cmd_block = NULL;
|
2020-05-07 12:08:54 +01:00
|
|
|
size_t cmd_size = 0;
|
|
|
|
u32 status = UsbStatusType_Success;
|
|
|
|
|
|
|
|
if (!g_usbTransferBuffer || !g_usbDeviceInterfaceInitialized || !g_usbDeviceInterface.initialized)
|
|
|
|
{
|
|
|
|
LOGFILE("Invalid parameters!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-08 04:48:22 +01:00
|
|
|
usbPrepareCommandHeader(UsbCommandType_StartSession, (u32)sizeof(UsbCommandStartSession));
|
2020-05-07 12:08:54 +01:00
|
|
|
|
2020-05-08 04:48:22 +01:00
|
|
|
cmd_block = (UsbCommandStartSession*)(g_usbTransferBuffer + sizeof(UsbCommandHeader));
|
|
|
|
memset(cmd_block, 0, sizeof(UsbCommandStartSession));
|
2020-05-07 12:08:54 +01:00
|
|
|
|
|
|
|
cmd_block->app_ver_major = VERSION_MAJOR;
|
|
|
|
cmd_block->app_ver_minor = VERSION_MINOR;
|
|
|
|
cmd_block->app_ver_micro = VERSION_MICRO;
|
|
|
|
cmd_block->abi_version = USB_ABI_VERSION;
|
|
|
|
|
2020-05-08 04:48:22 +01:00
|
|
|
cmd_size = (sizeof(UsbCommandHeader) + sizeof(UsbCommandStartSession));
|
2020-05-07 12:08:54 +01:00
|
|
|
|
|
|
|
status = usbSendCommand(cmd_size);
|
|
|
|
if (status != UsbStatusType_Success) usbLogStatusDetail(status);
|
|
|
|
|
|
|
|
return (status == UsbStatusType_Success);
|
|
|
|
}
|
|
|
|
|
2020-05-13 15:09:51 +01:00
|
|
|
static void usbEndSession(void)
|
|
|
|
{
|
|
|
|
if (!g_usbTransferBuffer || !g_usbDeviceInterfaceInitialized || !g_usbDeviceInterface.initialized || !g_usbHostAvailable || !g_usbSessionStarted)
|
|
|
|
{
|
|
|
|
LOGFILE("Invalid parameters!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
usbPrepareCommandHeader(UsbCommandType_EndSession, 0);
|
|
|
|
|
|
|
|
if (!usbWrite(g_usbTransferBuffer, sizeof(UsbCommandHeader))) LOGFILE("Failed to send EndSession command!");
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:22:16 +01:00
|
|
|
NX_INLINE void usbPrepareCommandHeader(u32 cmd, u32 cmd_block_size)
|
|
|
|
{
|
2020-05-08 04:48:22 +01:00
|
|
|
if (cmd > UsbCommandType_EndSession) return;
|
2020-05-05 16:22:16 +01:00
|
|
|
UsbCommandHeader *cmd_header = (UsbCommandHeader*)g_usbTransferBuffer;
|
|
|
|
memset(cmd_header, 0, sizeof(UsbCommandHeader));
|
|
|
|
cmd_header->magic = __builtin_bswap32(USB_CMD_HEADER_MAGIC);
|
|
|
|
cmd_header->cmd = cmd;
|
|
|
|
cmd_header->cmd_block_size = cmd_block_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 usbSendCommand(size_t cmd_size)
|
|
|
|
{
|
|
|
|
u32 cmd = ((UsbCommandHeader*)g_usbTransferBuffer)->cmd;
|
|
|
|
UsbStatus *cmd_status = NULL;
|
|
|
|
|
2020-05-06 15:04:10 +01:00
|
|
|
if (cmd_size < sizeof(UsbCommandHeader) || cmd_size > USB_TRANSFER_BUFFER_SIZE)
|
2020-05-05 16:22:16 +01:00
|
|
|
{
|
|
|
|
LOGFILE("Invalid command size!");
|
|
|
|
return UsbStatusType_InvalidCommandSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!usbWrite(g_usbTransferBuffer, cmd_size))
|
|
|
|
{
|
|
|
|
LOGFILE("Failed to write 0x%lX bytes long block for type 0x%X command!", cmd_size, cmd);
|
|
|
|
return UsbStatusType_WriteCommandFailed;
|
|
|
|
}
|
|
|
|
|
2020-05-06 15:04:10 +01:00
|
|
|
if (!usbRead(g_usbTransferBuffer, sizeof(UsbStatus)))
|
2020-05-05 16:22:16 +01:00
|
|
|
{
|
2020-05-06 15:04:10 +01:00
|
|
|
LOGFILE("Failed to read 0x%lX bytes long status block for type 0x%X command!", sizeof(UsbStatus), cmd);
|
2020-05-05 16:22:16 +01:00
|
|
|
return UsbStatusType_ReadStatusFailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd_status = (UsbStatus*)g_usbTransferBuffer;
|
|
|
|
|
|
|
|
if (cmd_status->magic != __builtin_bswap32(USB_CMD_HEADER_MAGIC))
|
|
|
|
{
|
|
|
|
LOGFILE("Invalid status block magic word for type 0x%X command!", cmd);
|
|
|
|
return UsbStatusType_InvalidMagicWord;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd_status->status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NX_INLINE void usbLogStatusDetail(u32 status)
|
|
|
|
{
|
|
|
|
switch(status)
|
|
|
|
{
|
|
|
|
case UsbStatusType_Success:
|
|
|
|
case UsbStatusType_InvalidCommandSize:
|
|
|
|
case UsbStatusType_WriteCommandFailed:
|
|
|
|
case UsbStatusType_ReadStatusFailed:
|
2020-05-09 05:48:46 +01:00
|
|
|
break;
|
2020-05-05 16:22:16 +01:00
|
|
|
case UsbStatusType_InvalidMagicWord:
|
2020-05-09 05:48:46 +01:00
|
|
|
LOGFILE("Host replied with Invalid Magic Word status code.");
|
2020-05-05 16:22:16 +01:00
|
|
|
break;
|
2020-05-09 05:48:46 +01:00
|
|
|
case UsbStatusType_UnsupportedCommand:
|
|
|
|
LOGFILE("Host replied with Unsupported Command status code.");
|
2020-05-05 16:22:16 +01:00
|
|
|
break;
|
2020-05-06 07:01:00 +01:00
|
|
|
case UsbStatusType_UnsupportedAbiVersion:
|
|
|
|
LOGFILE("Host replied with Unsupported ABI Version status code.");
|
2020-05-05 16:22:16 +01:00
|
|
|
break;
|
2020-05-09 07:32:01 +01:00
|
|
|
case UsbStatusType_MalformedCommand:
|
|
|
|
LOGFILE("Host replied with Malformed Command status code.");
|
|
|
|
break;
|
2020-05-05 16:22:16 +01:00
|
|
|
case UsbStatusType_HostIoError:
|
|
|
|
LOGFILE("Host replied with I/O Error status code.");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOGFILE("Unknown status code: 0x%X.", status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NX_INLINE bool usbAllocateTransferBuffer(void)
|
|
|
|
{
|
|
|
|
if (g_usbTransferBuffer) return true;
|
|
|
|
g_usbTransferBuffer = memalign(USB_TRANSFER_ALIGNMENT, USB_TRANSFER_BUFFER_SIZE);
|
|
|
|
return (g_usbTransferBuffer != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
NX_INLINE void usbFreeTransferBuffer(void)
|
|
|
|
{
|
|
|
|
if (!g_usbTransferBuffer) return;
|
|
|
|
free(g_usbTransferBuffer);
|
|
|
|
g_usbTransferBuffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool usbInitializeComms(void)
|
|
|
|
{
|
|
|
|
Result rc = 0;
|
|
|
|
|
|
|
|
bool ret = (g_usbDeviceInterfaceInitialized && g_usbDeviceInterface.initialized);
|
|
|
|
if (ret) goto exit;
|
|
|
|
|
|
|
|
rc = usbDsInitialize();
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInitialize failed! (0x%08X).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hosversionAtLeast(5,0,0))
|
|
|
|
{
|
|
|
|
u8 manufacturer = 0, product = 0, serial_number = 0;
|
|
|
|
static const u16 supported_langs[1] = { 0x0409 };
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Send language descriptor. */
|
2020-05-05 16:22:16 +01:00
|
|
|
rc = usbDsAddUsbLanguageStringDescriptor(NULL, supported_langs, sizeof(supported_langs) / sizeof(u16));
|
2020-07-06 01:10:07 +01:00
|
|
|
if (R_FAILED(rc)) LOGFILE("usbDsAddUsbLanguageStringDescriptor failed! (0x%08X).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Send manufacturer. */
|
2020-05-05 16:22:16 +01:00
|
|
|
if (R_SUCCEEDED(rc))
|
|
|
|
{
|
|
|
|
rc = usbDsAddUsbStringDescriptor(&manufacturer, APP_AUTHOR);
|
2020-07-06 01:10:07 +01:00
|
|
|
if (R_FAILED(rc)) LOGFILE("usbDsAddUsbStringDescriptor failed! (0x%08X) (manufacturer).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Send product. */
|
2020-05-05 16:22:16 +01:00
|
|
|
if (R_SUCCEEDED(rc))
|
|
|
|
{
|
|
|
|
rc = usbDsAddUsbStringDescriptor(&product, APP_TITLE);
|
2020-07-06 01:10:07 +01:00
|
|
|
if (R_FAILED(rc)) LOGFILE("usbDsAddUsbStringDescriptor failed! (0x%08X) (product).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Send serial number. */
|
2020-05-05 16:22:16 +01:00
|
|
|
if (R_SUCCEEDED(rc))
|
|
|
|
{
|
|
|
|
rc = usbDsAddUsbStringDescriptor(&serial_number, APP_VERSION);
|
2020-07-06 01:10:07 +01:00
|
|
|
if (R_FAILED(rc)) LOGFILE("usbDsAddUsbStringDescriptor failed! (0x%08X) (serial number).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Send device descriptors. */
|
2020-05-10 10:07:31 +01:00
|
|
|
struct usb_device_descriptor device_descriptor = {
|
|
|
|
.bLength = USB_DT_DEVICE_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_DEVICE,
|
|
|
|
.bcdUSB = 0x0110,
|
|
|
|
.bDeviceClass = 0x00,
|
|
|
|
.bDeviceSubClass = 0x00,
|
|
|
|
.bDeviceProtocol = 0x00,
|
|
|
|
.bMaxPacketSize0 = 0x40,
|
|
|
|
.idVendor = 0x057e,
|
|
|
|
.idProduct = 0x3000,
|
|
|
|
.bcdDevice = 0x0100,
|
|
|
|
.iManufacturer = manufacturer,
|
|
|
|
.iProduct = product,
|
|
|
|
.iSerialNumber = serial_number,
|
|
|
|
.bNumConfigurations = 0x01
|
|
|
|
};
|
2020-05-05 16:22:16 +01:00
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Full Speed is USB 1.1. */
|
2020-05-05 16:22:16 +01:00
|
|
|
if (R_SUCCEEDED(rc))
|
|
|
|
{
|
|
|
|
rc = usbDsSetUsbDeviceDescriptor(UsbDeviceSpeed_Full, &device_descriptor);
|
2020-07-06 01:10:07 +01:00
|
|
|
if (R_FAILED(rc)) LOGFILE("usbDsSetUsbDeviceDescriptor failed! (0x%08X) (USB 1.1).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* High Speed is USB 2.0. */
|
2020-05-05 16:22:16 +01:00
|
|
|
device_descriptor.bcdUSB = 0x0200;
|
|
|
|
if (R_SUCCEEDED(rc))
|
|
|
|
{
|
|
|
|
rc = usbDsSetUsbDeviceDescriptor(UsbDeviceSpeed_High, &device_descriptor);
|
2020-07-06 01:10:07 +01:00
|
|
|
if (R_FAILED(rc)) LOGFILE("usbDsSetUsbDeviceDescriptor failed! (0x%08X) (USB 2.0).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Super Speed is USB 3.0. */
|
|
|
|
/* Upgrade packet size to 512. */
|
2020-05-05 16:22:16 +01:00
|
|
|
device_descriptor.bcdUSB = 0x0300;
|
|
|
|
device_descriptor.bMaxPacketSize0 = 0x09;
|
|
|
|
if (R_SUCCEEDED(rc))
|
|
|
|
{
|
|
|
|
rc = usbDsSetUsbDeviceDescriptor(UsbDeviceSpeed_Super, &device_descriptor);
|
2020-07-06 01:10:07 +01:00
|
|
|
if (R_FAILED(rc)) LOGFILE("usbDsSetUsbDeviceDescriptor failed! (0x%08X) (USB 3.0).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Define Binary Object Store. */
|
2020-05-10 10:07:31 +01:00
|
|
|
u8 bos[0x16] = {
|
2020-07-06 01:10:07 +01:00
|
|
|
/* USB 1.1. */
|
|
|
|
0x05, /* bLength. */
|
|
|
|
USB_DT_BOS, /* bDescriptorType. */
|
|
|
|
0x16, 0x00, /* wTotalLength. */
|
|
|
|
0x02, /* bNumDeviceCaps. */
|
2020-05-10 10:07:31 +01:00
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* USB 2.0. */
|
|
|
|
0x07, /* bLength. */
|
|
|
|
USB_DT_DEVICE_CAPABILITY, /* bDescriptorType. */
|
|
|
|
0x02, /* bDevCapabilityType. */
|
|
|
|
0x02, 0x00, 0x00, 0x00, /* dev_capability_data. */
|
2020-05-10 10:07:31 +01:00
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* USB 3.0. */
|
|
|
|
0x0A, /* bLength. */
|
|
|
|
USB_DT_DEVICE_CAPABILITY, /* bDescriptorType. */
|
|
|
|
0x03, /* bDevCapabilityType. */
|
2020-05-10 10:07:31 +01:00
|
|
|
0x00, 0x0E, 0x00, 0x03, 0x00, 0x00, 0x00
|
|
|
|
};
|
|
|
|
|
2020-05-05 16:22:16 +01:00
|
|
|
if (R_SUCCEEDED(rc))
|
|
|
|
{
|
|
|
|
rc = usbDsSetBinaryObjectStore(bos, sizeof(bos));
|
2020-07-06 01:10:07 +01:00
|
|
|
if (R_FAILED(rc)) LOGFILE("usbDsSetBinaryObjectStore failed! (0x%08X).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
}
|
2020-05-07 12:08:54 +01:00
|
|
|
} else {
|
|
|
|
static const UsbDsDeviceInfo device_info = {
|
|
|
|
.idVendor = 0x057e,
|
|
|
|
.idProduct = 0x3000,
|
|
|
|
.bcdDevice = 0x0100,
|
|
|
|
.Manufacturer = APP_AUTHOR,
|
|
|
|
.Product = APP_TITLE,
|
|
|
|
.SerialNumber = APP_VERSION
|
|
|
|
};
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Set VID, PID and BCD. */
|
2020-05-07 12:08:54 +01:00
|
|
|
rc = usbDsSetVidPidBcd(&device_info);
|
2020-07-06 01:10:07 +01:00
|
|
|
if (R_FAILED(rc)) LOGFILE("usbDsSetVidPidBcd failed! (0x%08X).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (R_FAILED(rc)) goto exit;
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Initialize USB device interface. */
|
2020-05-05 16:22:16 +01:00
|
|
|
rwlockWriteLock(&(g_usbDeviceInterface.lock));
|
|
|
|
rwlockWriteLock(&(g_usbDeviceInterface.lock_in));
|
|
|
|
rwlockWriteLock(&(g_usbDeviceInterface.lock_out));
|
|
|
|
|
|
|
|
bool dev_iface_init = usbInitializeDeviceInterface();
|
|
|
|
|
|
|
|
rwlockWriteUnlock(&(g_usbDeviceInterface.lock_out));
|
|
|
|
rwlockWriteUnlock(&(g_usbDeviceInterface.lock_in));
|
|
|
|
rwlockWriteUnlock(&(g_usbDeviceInterface.lock));
|
|
|
|
|
|
|
|
if (!dev_iface_init)
|
|
|
|
{
|
|
|
|
LOGFILE("Failed to initialize USB device interface!");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hosversionAtLeast(5,0,0))
|
|
|
|
{
|
|
|
|
rc = usbDsEnable();
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsEnable failed! (0x%08X).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = g_usbDeviceInterfaceInitialized = true;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if (!ret) usbCloseComms();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usbCloseComms(void)
|
|
|
|
{
|
|
|
|
usbDsExit();
|
|
|
|
g_usbDeviceInterfaceInitialized = false;
|
|
|
|
usbFreeDeviceInterface();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usbFreeDeviceInterface(void)
|
|
|
|
{
|
|
|
|
rwlockWriteLock(&(g_usbDeviceInterface.lock));
|
|
|
|
|
|
|
|
if (!g_usbDeviceInterface.initialized) {
|
|
|
|
rwlockWriteUnlock(&(g_usbDeviceInterface.lock));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rwlockWriteLock(&(g_usbDeviceInterface.lock_in));
|
|
|
|
rwlockWriteLock(&(g_usbDeviceInterface.lock_out));
|
|
|
|
|
|
|
|
g_usbDeviceInterface.initialized = false;
|
|
|
|
|
|
|
|
g_usbDeviceInterface.interface = NULL;
|
|
|
|
g_usbDeviceInterface.endpoint_in = NULL;
|
|
|
|
g_usbDeviceInterface.endpoint_out = NULL;
|
|
|
|
|
|
|
|
rwlockWriteUnlock(&(g_usbDeviceInterface.lock_out));
|
|
|
|
rwlockWriteUnlock(&(g_usbDeviceInterface.lock_in));
|
|
|
|
|
|
|
|
rwlockWriteUnlock(&(g_usbDeviceInterface.lock));
|
|
|
|
}
|
|
|
|
|
|
|
|
NX_INLINE bool usbInitializeDeviceInterface(void)
|
|
|
|
{
|
|
|
|
return (hosversionAtLeast(5,0,0) ? usbInitializeDeviceInterface5x() : usbInitializeDeviceInterface1x());
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool usbInitializeDeviceInterface5x(void)
|
|
|
|
{
|
|
|
|
Result rc = 0;
|
|
|
|
|
|
|
|
struct usb_interface_descriptor interface_descriptor = {
|
|
|
|
.bLength = USB_DT_INTERFACE_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
.bInterfaceNumber = 4,
|
|
|
|
.bNumEndpoints = 2,
|
|
|
|
.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
|
|
|
|
.bInterfaceSubClass = USB_CLASS_VENDOR_SPEC,
|
|
|
|
.bInterfaceProtocol = USB_CLASS_VENDOR_SPEC,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct usb_endpoint_descriptor endpoint_descriptor_in = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_ENDPOINT_IN,
|
|
|
|
.bmAttributes = USB_TRANSFER_TYPE_BULK,
|
|
|
|
.wMaxPacketSize = 0x40,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct usb_endpoint_descriptor endpoint_descriptor_out = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_ENDPOINT_OUT,
|
|
|
|
.bmAttributes = USB_TRANSFER_TYPE_BULK,
|
|
|
|
.wMaxPacketSize = 0x40,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct usb_ss_endpoint_companion_descriptor endpoint_companion = {
|
|
|
|
.bLength = sizeof(struct usb_ss_endpoint_companion_descriptor),
|
|
|
|
.bDescriptorType = USB_DT_SS_ENDPOINT_COMPANION,
|
|
|
|
.bMaxBurst = 0x0F,
|
|
|
|
.bmAttributes = 0x00,
|
|
|
|
.wBytesPerInterval = 0x00,
|
|
|
|
};
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Enable device interface. */
|
2020-05-05 16:22:16 +01:00
|
|
|
g_usbDeviceInterface.initialized = true;
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Setup interface. */
|
2020-05-05 16:22:16 +01:00
|
|
|
rc = usbDsRegisterInterface(&(g_usbDeviceInterface.interface));
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsRegisterInterface failed! (0x%08X).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface_descriptor.bInterfaceNumber = g_usbDeviceInterface.interface->interface_index;
|
|
|
|
endpoint_descriptor_in.bEndpointAddress += (interface_descriptor.bInterfaceNumber + 1);
|
|
|
|
endpoint_descriptor_out.bEndpointAddress += (interface_descriptor.bInterfaceNumber + 1);
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Full Speed config (USB 1.1). */
|
2020-05-05 16:22:16 +01:00
|
|
|
rc = usbDsInterface_AppendConfigurationData(g_usbDeviceInterface.interface, UsbDeviceSpeed_Full, &interface_descriptor, USB_DT_INTERFACE_SIZE);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_AppendConfigurationData failed! (0x%08X) (USB 1.1) (interface).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = usbDsInterface_AppendConfigurationData(g_usbDeviceInterface.interface, UsbDeviceSpeed_Full, &endpoint_descriptor_in, USB_DT_ENDPOINT_SIZE);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_AppendConfigurationData failed! (0x%08X) (USB 1.1) (in endpoint).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = usbDsInterface_AppendConfigurationData(g_usbDeviceInterface.interface, UsbDeviceSpeed_Full, &endpoint_descriptor_out, USB_DT_ENDPOINT_SIZE);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_AppendConfigurationData failed! (0x%08X) (USB 1.1) (out endpoint).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* High Speed config (USB 2.0). */
|
2020-05-05 16:22:16 +01:00
|
|
|
endpoint_descriptor_in.wMaxPacketSize = 0x200;
|
|
|
|
endpoint_descriptor_out.wMaxPacketSize = 0x200;
|
|
|
|
|
|
|
|
rc = usbDsInterface_AppendConfigurationData(g_usbDeviceInterface.interface, UsbDeviceSpeed_High, &interface_descriptor, USB_DT_INTERFACE_SIZE);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_AppendConfigurationData failed! (0x%08X) (USB 2.0) (interface).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = usbDsInterface_AppendConfigurationData(g_usbDeviceInterface.interface, UsbDeviceSpeed_High, &endpoint_descriptor_in, USB_DT_ENDPOINT_SIZE);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_AppendConfigurationData failed! (0x%08X) (USB 2.0) (in endpoint).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = usbDsInterface_AppendConfigurationData(g_usbDeviceInterface.interface, UsbDeviceSpeed_High, &endpoint_descriptor_out, USB_DT_ENDPOINT_SIZE);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_AppendConfigurationData failed! (0x%08X) (USB 2.0) (out endpoint).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Super Speed config (USB 3.0). */
|
2020-05-05 16:22:16 +01:00
|
|
|
endpoint_descriptor_in.wMaxPacketSize = 0x400;
|
|
|
|
endpoint_descriptor_out.wMaxPacketSize = 0x400;
|
|
|
|
|
|
|
|
rc = usbDsInterface_AppendConfigurationData(g_usbDeviceInterface.interface, UsbDeviceSpeed_Super, &interface_descriptor, USB_DT_INTERFACE_SIZE);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_AppendConfigurationData failed! (0x%08X) (USB 3.0) (interface).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = usbDsInterface_AppendConfigurationData(g_usbDeviceInterface.interface, UsbDeviceSpeed_Super, &endpoint_descriptor_in, USB_DT_ENDPOINT_SIZE);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_AppendConfigurationData failed! (0x%08X) (USB 3.0) (in endpoint).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = usbDsInterface_AppendConfigurationData(g_usbDeviceInterface.interface, UsbDeviceSpeed_Super, &endpoint_companion, USB_DT_SS_ENDPOINT_COMPANION_SIZE);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_AppendConfigurationData failed! (0x%08X) (USB 3.0) (in endpoint companion).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = usbDsInterface_AppendConfigurationData(g_usbDeviceInterface.interface, UsbDeviceSpeed_Super, &endpoint_descriptor_out, USB_DT_ENDPOINT_SIZE);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_AppendConfigurationData failed! (0x%08X) (USB 3.0) (out endpoint).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = usbDsInterface_AppendConfigurationData(g_usbDeviceInterface.interface, UsbDeviceSpeed_Super, &endpoint_companion, USB_DT_SS_ENDPOINT_COMPANION_SIZE);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_AppendConfigurationData failed! (0x%08X) (USB 3.0) (out endpoint companion).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Setup endpoints. */
|
2020-05-05 16:22:16 +01:00
|
|
|
rc = usbDsInterface_RegisterEndpoint(g_usbDeviceInterface.interface, &(g_usbDeviceInterface.endpoint_in), endpoint_descriptor_in.bEndpointAddress);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_RegisterEndpoint failed! (0x%08X) (in endpoint).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = usbDsInterface_RegisterEndpoint(g_usbDeviceInterface.interface, &(g_usbDeviceInterface.endpoint_out), endpoint_descriptor_out.bEndpointAddress);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_RegisterEndpoint failed! (0x%08X) (out endpoint).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = usbDsInterface_EnableInterface(g_usbDeviceInterface.interface);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_EnableInterface failed! (0x%08X).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool usbInitializeDeviceInterface1x(void)
|
|
|
|
{
|
|
|
|
Result rc = 0;
|
|
|
|
|
|
|
|
struct usb_interface_descriptor interface_descriptor = {
|
|
|
|
.bLength = USB_DT_INTERFACE_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
.bInterfaceNumber = 0,
|
|
|
|
.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
|
|
|
|
.bInterfaceSubClass = USB_CLASS_VENDOR_SPEC,
|
|
|
|
.bInterfaceProtocol = USB_CLASS_VENDOR_SPEC,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct usb_endpoint_descriptor endpoint_descriptor_in = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_ENDPOINT_IN,
|
|
|
|
.bmAttributes = USB_TRANSFER_TYPE_BULK,
|
|
|
|
.wMaxPacketSize = 0x200,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct usb_endpoint_descriptor endpoint_descriptor_out = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_ENDPOINT_OUT,
|
|
|
|
.bmAttributes = USB_TRANSFER_TYPE_BULK,
|
|
|
|
.wMaxPacketSize = 0x200,
|
|
|
|
};
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Enable device interface. */
|
2020-05-05 16:22:16 +01:00
|
|
|
g_usbDeviceInterface.initialized = true;
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Setup interface. */
|
2020-05-05 16:22:16 +01:00
|
|
|
rc = usbDsGetDsInterface(&(g_usbDeviceInterface.interface), &interface_descriptor, "usb");
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsGetDsInterface failed! (0x%08X).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Setup endpoints. */
|
2020-05-05 16:22:16 +01:00
|
|
|
rc = usbDsInterface_GetDsEndpoint(g_usbDeviceInterface.interface, &(g_usbDeviceInterface.endpoint_in), &endpoint_descriptor_in);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_GetDsEndpoint failed! (0x%08X) (in endpoint).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = usbDsInterface_GetDsEndpoint(g_usbDeviceInterface.interface, &(g_usbDeviceInterface.endpoint_out), &endpoint_descriptor_out);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_GetDsEndpoint failed! (0x%08X) (out endpoint).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = usbDsInterface_EnableInterface(g_usbDeviceInterface.interface);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsInterface_EnableInterface failed! (0x%08X).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:08:54 +01:00
|
|
|
NX_INLINE bool usbIsHostAvailable(void)
|
|
|
|
{
|
|
|
|
u32 state = 0;
|
|
|
|
Result rc = usbDsGetState(&state);
|
2020-05-13 15:09:51 +01:00
|
|
|
return (R_SUCCEEDED(rc) && state == 5);
|
2020-05-07 12:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NX_INLINE bool usbRead(void *buf, u64 size)
|
2020-05-05 16:22:16 +01:00
|
|
|
{
|
|
|
|
rwlockWriteLock(&(g_usbDeviceInterface.lock_out));
|
|
|
|
bool ret = usbTransferData(buf, size, g_usbDeviceInterface.endpoint_out);
|
|
|
|
rwlockWriteUnlock(&(g_usbDeviceInterface.lock_out));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:08:54 +01:00
|
|
|
NX_INLINE bool usbWrite(void *buf, u64 size)
|
2020-05-05 16:22:16 +01:00
|
|
|
{
|
|
|
|
rwlockWriteLock(&(g_usbDeviceInterface.lock_in));
|
|
|
|
bool ret = usbTransferData(buf, size, g_usbDeviceInterface.endpoint_in);
|
2020-05-11 20:12:03 +01:00
|
|
|
if (ret) usbDsEndpoint_SetZlt(g_usbDeviceInterface.endpoint_in, true);
|
2020-05-05 16:22:16 +01:00
|
|
|
rwlockWriteUnlock(&(g_usbDeviceInterface.lock_in));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:08:54 +01:00
|
|
|
static bool usbTransferData(void *buf, u64 size, UsbDsEndpoint *endpoint)
|
2020-05-05 16:22:16 +01:00
|
|
|
{
|
2020-05-11 20:12:03 +01:00
|
|
|
if (!buf || !IS_ALIGNED((u64)buf, USB_TRANSFER_ALIGNMENT) || !size || !endpoint)
|
2020-05-05 16:22:16 +01:00
|
|
|
{
|
|
|
|
LOGFILE("Invalid parameters!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!usbIsHostAvailable())
|
|
|
|
{
|
|
|
|
LOGFILE("USB host unavailable!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 urb_id = 0;
|
|
|
|
Result rc = 0;
|
|
|
|
UsbDsReportData report_data = {0};
|
|
|
|
u32 transferred_size = 0;
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Start an USB transfer using the provided endpoint. */
|
2020-05-05 16:22:16 +01:00
|
|
|
rc = usbDsEndpoint_PostBufferAsync(endpoint, buf, size, &urb_id);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsEndpoint_PostBufferAsync failed! (0x%08X).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Wait for the transfer to finish. */
|
|
|
|
/* If we're starting an USB transfer session, use an infinite timeout value to let the user start the companion app. */
|
2020-05-11 13:11:06 +01:00
|
|
|
u64 timeout = (g_usbSessionStarted ? (USB_TRANSFER_TIMEOUT * (u64)1000000000) : UINT64_MAX);
|
|
|
|
rc = eventWait(&(endpoint->CompletionEvent), timeout);
|
2020-05-05 16:22:16 +01:00
|
|
|
eventClear(&(endpoint->CompletionEvent));
|
|
|
|
|
2020-05-11 13:11:06 +01:00
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Cancel transfer. */
|
2020-05-11 13:11:06 +01:00
|
|
|
usbDsEndpoint_Cancel(endpoint);
|
2020-05-11 20:12:03 +01:00
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Safety measure: wait until the completion event is triggered again before proceeding. */
|
2020-05-11 20:12:03 +01:00
|
|
|
eventWait(&(endpoint->CompletionEvent), UINT64_MAX);
|
|
|
|
eventClear(&(endpoint->CompletionEvent));
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
/* Signal usermode USB timeout event if needed. */
|
|
|
|
/* This will "reset" the USB connection by making the background thread wait until a new session is established. */
|
2020-07-03 10:31:22 +01:00
|
|
|
if (g_usbSessionStarted) ueventSignal(&g_usbTimeoutEvent);
|
|
|
|
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("eventWait failed! (0x%08X).", rc);
|
2020-05-11 13:11:06 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:22:16 +01:00
|
|
|
rc = usbDsEndpoint_GetReportData(endpoint, &report_data);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsEndpoint_GetReportData failed! (0x%08X).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = usbDsParseReportData(&report_data, urb_id, NULL, &transferred_size);
|
|
|
|
if (R_FAILED(rc))
|
|
|
|
{
|
2020-07-06 01:10:07 +01:00
|
|
|
LOGFILE("usbDsParseReportData failed! (0x%08X).", rc);
|
2020-05-05 16:22:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (transferred_size != size)
|
|
|
|
{
|
|
|
|
LOGFILE("USB transfer failed! Expected 0x%lX bytes, got 0x%X bytes.", size, transferred_size);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|