diff --git a/SwitchUSB/include/SwitchUSBEndpoint.h b/SwitchUSB/include/SwitchUSBEndpoint.h index 3033c6a..ff9f6fd 100644 --- a/SwitchUSB/include/SwitchUSBEndpoint.h +++ b/SwitchUSB/include/SwitchUSBEndpoint.h @@ -10,8 +10,6 @@ private: UsbHsClientIfSession *m_ifSession; usb_endpoint_descriptor *m_descriptor; - void *m_buffer; - public: //Pass the necessary information to be able to open the endpoint SwitchUSBEndpoint(UsbHsClientIfSession &if_session, usb_endpoint_descriptor &desc); diff --git a/SwitchUSB/include/SwitchUSBInterface.h b/SwitchUSB/include/SwitchUSBInterface.h index d50f811..73804d1 100644 --- a/SwitchUSB/include/SwitchUSBInterface.h +++ b/SwitchUSB/include/SwitchUSBInterface.h @@ -14,8 +14,6 @@ private: std::array, 15> m_inEndpoints; std::array, 15> m_outEndpoints; - void *m_controlTransferBuffer; - public: //Pass the specified interface to allow for opening the session SwitchUSBInterface(UsbHsInterface &interface); diff --git a/SwitchUSB/source/SwitchUSBEndpoint.cpp b/SwitchUSB/source/SwitchUSBEndpoint.cpp index 1eb2f0f..2c53152 100644 --- a/SwitchUSB/source/SwitchUSBEndpoint.cpp +++ b/SwitchUSB/source/SwitchUSBEndpoint.cpp @@ -6,12 +6,10 @@ SwitchUSBEndpoint::SwitchUSBEndpoint(UsbHsClientIfSession &if_session, usb_endpo : m_ifSession(&if_session), m_descriptor(&desc) { - m_buffer = memalign(0x1000, 64); } SwitchUSBEndpoint::~SwitchUSBEndpoint() { - free(m_buffer); Close(); } @@ -30,44 +28,47 @@ void SwitchUSBEndpoint::Close() Result SwitchUSBEndpoint::Write(const void *inBuffer, size_t bufferSize) { - Result rc = -1; - if (m_buffer != nullptr) + void *temp_buffer = memalign(0x1000, bufferSize); + if (temp_buffer == nullptr) + return -1; + + u32 transferredSize = 0; + + for (size_t byte = 0; byte != bufferSize; ++byte) { - u32 transferredSize = 0; - memset(m_buffer, 0, bufferSize); - - for (size_t byte = 0; byte != bufferSize; ++byte) - { - static_cast(m_buffer)[byte] = static_cast(inBuffer)[byte]; - } - - rc = usbHsEpPostBuffer(&m_epSession, m_buffer, bufferSize, &transferredSize); - - if (R_SUCCEEDED(rc)) - { - svcSleepThread(m_descriptor->bInterval * 1e+6L); - } + static_cast(temp_buffer)[byte] = static_cast(inBuffer)[byte]; } + + Result rc = usbHsEpPostBuffer(&m_epSession, temp_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) { - if (m_buffer == nullptr) + void *temp_buffer = memalign(0x1000, bufferSize); + if (temp_buffer == nullptr) return -1; u32 transferredSize; - memset(m_buffer, 0, bufferSize); - Result rc = usbHsEpPostBuffer(&m_epSession, m_buffer, bufferSize, &transferredSize); + Result rc = usbHsEpPostBuffer(&m_epSession, temp_buffer, bufferSize, &transferredSize); if (R_SUCCEEDED(rc)) { for (u32 byte = 0; byte != transferredSize; ++byte) { - static_cast(outBuffer)[byte] = static_cast(m_buffer)[byte]; + static_cast(outBuffer)[byte] = static_cast(temp_buffer)[byte]; } } + free(temp_buffer); + return rc; } diff --git a/SwitchUSB/source/SwitchUSBInterface.cpp b/SwitchUSB/source/SwitchUSBInterface.cpp index ec7b0ef..af5cdb2 100644 --- a/SwitchUSB/source/SwitchUSBInterface.cpp +++ b/SwitchUSB/source/SwitchUSBInterface.cpp @@ -6,12 +6,10 @@ SwitchUSBInterface::SwitchUSBInterface(UsbHsInterface &interface) : m_interface(interface) { - m_controlTransferBuffer = memalign(0x1000, 64); } SwitchUSBInterface::~SwitchUSBInterface() { - free(m_controlTransferBuffer); Close(); } @@ -68,26 +66,28 @@ void SwitchUSBInterface::Close() Result SwitchUSBInterface::ControlTransfer(u8 bmRequestType, u8 bmRequest, u16 wValue, u16 wIndex, u16 wLength, void *buffer) { - if (m_controlTransferBuffer == nullptr) + void *temp_buffer = memalign(0x1000, wLength); + if (temp_buffer == nullptr) return -1; u32 transferredSize; for (u16 byte = 0; byte != wLength; ++byte) { - static_cast(m_controlTransferBuffer)[byte] = static_cast(buffer)[byte]; + static_cast(temp_buffer)[byte] = static_cast(buffer)[byte]; } - Result rc = usbHsIfCtrlXfer(&m_session, bmRequestType, bmRequest, wValue, wIndex, wLength, m_controlTransferBuffer, &transferredSize); + Result rc = usbHsIfCtrlXfer(&m_session, bmRequestType, bmRequest, wValue, wIndex, wLength, temp_buffer, &transferredSize); if (R_SUCCEEDED(rc)) { memset(buffer, 0, wLength); for (u32 byte = 0; byte != transferredSize; ++byte) { - static_cast(buffer)[byte] = static_cast(m_controlTransferBuffer)[byte]; + static_cast(buffer)[byte] = static_cast(temp_buffer)[byte]; } } + free(temp_buffer); return rc; }