From 47297daf3acc0fb6c11953e1c0cc399a7aab490f Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Sat, 9 May 2020 09:49:02 -0400 Subject: [PATCH] Some more USB fixes. --- source/main.c | 4 ++-- source/usb.c | 16 +++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/source/main.c b/source/main.c index 8d6db09..1fc2146 100644 --- a/source/main.c +++ b/source/main.c @@ -149,7 +149,7 @@ static int read_thread_func(void *arg) return -1; } - u8 *buf = usbAllocatePageAlignedBuffer(TEST_BUF_SIZE); + u8 *buf = malloc(TEST_BUF_SIZE); if (!buf) { shared_data->read_error = true; @@ -296,7 +296,7 @@ int main(int argc, char *argv[]) .id_offset = 0 }; - buf = malloc(TEST_BUF_SIZE); + buf = usbAllocatePageAlignedBuffer(TEST_BUF_SIZE); if (!buf) { consolePrint("buf failed\n"); diff --git a/source/usb.c b/source/usb.c index 4f399ea..d3da7e4 100644 --- a/source/usb.c +++ b/source/usb.c @@ -28,6 +28,8 @@ #define USB_CMD_HEADER_MAGIC 0x4E584454 /* "NXDT" */ +#define USB_SESSION_START_TIMEOUT 10 /* 10 seconds */ + #define USB_TRANSFER_ALIGNMENT 0x1000 /* 4 KiB */ #define USB_TRANSFER_TIMEOUT 5 /* 5 seconds */ @@ -188,15 +190,10 @@ bool usbStartSession(void) time_t start = time(NULL); time_t now = start; - while((now - start) < USB_TRANSFER_TIMEOUT) + while((now - start) < USB_SESSION_START_TIMEOUT) { - if (usbIsHostAvailable()) - { - /* Once the console has been connected to a host device, there's no need to keep running this loop */ - /* usbTransferData() implements its own timeout */ - ret = g_usbSessionStarted = _usbStartSession(); - break; - } + ret = g_usbSessionStarted = (usbIsHostAvailable() && _usbStartSession()); + if (ret) break; utilsSleep(1); now = time(NULL); @@ -950,6 +947,7 @@ static bool usbTransferData(void *buf, u64 size, UsbDsEndpoint *endpoint) if (R_FAILED(rc)) { LOGFILE("USB transfer timed out! (0x%08X)", rc); + usbDsEndpoint_Cancel(endpoint); return false; } @@ -963,7 +961,7 @@ static bool usbTransferData(void *buf, u64 size, UsbDsEndpoint *endpoint) rc = usbDsParseReportData(&report_data, urb_id, NULL, &transferred_size); if (R_FAILED(rc)) { - LOGFILE("usbDsEndpoint_GetReportData failed! (0x%08X)", rc); + LOGFILE("usbDsParseReportData failed! (0x%08X)", rc); return false; }