From 7356a742c9a1762c0c469dc523ce18a77d0fa229 Mon Sep 17 00:00:00 2001 From: cathery Date: Mon, 25 Nov 2019 15:58:01 +0300 Subject: [PATCH] Fixed input packets bleeding into each other to fix #78 --- SwitchUSB/include/SwitchUSBEndpoint.h | 2 ++ SwitchUSB/source/SwitchUSBEndpoint.cpp | 33 +++++++++++++++----------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/SwitchUSB/include/SwitchUSBEndpoint.h b/SwitchUSB/include/SwitchUSBEndpoint.h index bf06d0f..d2d6b41 100644 --- a/SwitchUSB/include/SwitchUSBEndpoint.h +++ b/SwitchUSB/include/SwitchUSBEndpoint.h @@ -10,6 +10,8 @@ private: UsbHsClientIfSession *m_ifSession; usb_endpoint_descriptor *m_descriptor; + void *m_buffer = nullptr; + public: //Pass the necessary information to be able to open the endpoint SwitchUSBEndpoint(UsbHsClientIfSession &if_session, usb_endpoint_descriptor &desc); diff --git a/SwitchUSB/source/SwitchUSBEndpoint.cpp b/SwitchUSB/source/SwitchUSBEndpoint.cpp index 4c86dad..05fa1e3 100644 --- a/SwitchUSB/source/SwitchUSBEndpoint.cpp +++ b/SwitchUSB/source/SwitchUSBEndpoint.cpp @@ -15,60 +15,65 @@ SwitchUSBEndpoint::~SwitchUSBEndpoint() Result SwitchUSBEndpoint::Open(int maxPacketSize) { - Result rc = usbHsIfOpenUsbEp(m_ifSession, &m_epSession, 1, (maxPacketSize != 0 ? maxPacketSize : m_descriptor->wMaxPacketSize), m_descriptor); + maxPacketSize = maxPacketSize != 0 ? maxPacketSize : m_descriptor->wMaxPacketSize; + + Result rc = usbHsIfOpenUsbEp(m_ifSession, &m_epSession, 1, maxPacketSize, m_descriptor); if (R_FAILED(rc)) return 73011; + + m_buffer = memalign(0x1000, maxPacketSize); + if (m_buffer == nullptr) + return -1; return rc; } void SwitchUSBEndpoint::Close() { + if (m_buffer != nullptr) + { + free(m_buffer); + m_buffer = nullptr; + } + usbHsEpClose(&m_epSession); } Result SwitchUSBEndpoint::Write(const void *inBuffer, size_t bufferSize) { - void *temp_buffer = memalign(0x1000, bufferSize); - if (temp_buffer == nullptr) + if (m_buffer == nullptr) return -1; - u32 transferredSize = 0; for (size_t byte = 0; byte != bufferSize; ++byte) { - static_cast(temp_buffer)[byte] = static_cast(inBuffer)[byte]; + static_cast(m_buffer)[byte] = static_cast(inBuffer)[byte]; } - Result rc = usbHsEpPostBuffer(&m_epSession, temp_buffer, bufferSize, &transferredSize); + Result rc = usbHsEpPostBuffer(&m_epSession, m_buffer, bufferSize, &transferredSize); if (R_SUCCEEDED(rc)) { svcSleepThread(m_descriptor->bInterval * 1e+6L); } - - free(temp_buffer); return rc; } Result SwitchUSBEndpoint::Read(void *outBuffer, size_t bufferSize) { - void *temp_buffer = memalign(0x1000, bufferSize); - if (temp_buffer == nullptr) + if (m_buffer == nullptr) return -1; u32 transferredSize; - Result rc = usbHsEpPostBuffer(&m_epSession, temp_buffer, bufferSize, &transferredSize); + Result rc = usbHsEpPostBuffer(&m_epSession, m_buffer, bufferSize, &transferredSize); if (R_SUCCEEDED(rc)) { for (u32 byte = 0; byte != transferredSize; ++byte) { - static_cast(outBuffer)[byte] = static_cast(temp_buffer)[byte]; + static_cast(outBuffer)[byte] = static_cast(m_buffer)[byte]; } } - free(temp_buffer); - return rc; }