1
0
mirror of https://github.com/cathery/sys-con.git synced 2024-07-05 10:48:46 +00:00

Revert back to allocating aligned memory when needed

This commit is contained in:
cathery 2019-11-11 21:23:52 +03:00
parent 744c42a250
commit 1219a249be
4 changed files with 29 additions and 32 deletions

View File

@ -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);

View File

@ -14,8 +14,6 @@ private:
std::array<std::unique_ptr<IUSBEndpoint>, 15> m_inEndpoints;
std::array<std::unique_ptr<IUSBEndpoint>, 15> m_outEndpoints;
void *m_controlTransferBuffer;
public:
//Pass the specified interface to allow for opening the session
SwitchUSBInterface(UsbHsInterface &interface);

View File

@ -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<uint8_t *>(m_buffer)[byte] = static_cast<const uint8_t *>(inBuffer)[byte];
}
rc = usbHsEpPostBuffer(&m_epSession, m_buffer, bufferSize, &transferredSize);
if (R_SUCCEEDED(rc))
{
svcSleepThread(m_descriptor->bInterval * 1e+6L);
}
static_cast<uint8_t *>(temp_buffer)[byte] = static_cast<const uint8_t *>(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<uint8_t *>(outBuffer)[byte] = static_cast<uint8_t *>(m_buffer)[byte];
static_cast<uint8_t *>(outBuffer)[byte] = static_cast<uint8_t *>(temp_buffer)[byte];
}
}
free(temp_buffer);
return rc;
}

View File

@ -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<uint8_t *>(m_controlTransferBuffer)[byte] = static_cast<uint8_t *>(buffer)[byte];
static_cast<uint8_t *>(temp_buffer)[byte] = static_cast<uint8_t *>(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<uint8_t *>(buffer)[byte] = static_cast<uint8_t *>(m_controlTransferBuffer)[byte];
static_cast<uint8_t *>(buffer)[byte] = static_cast<uint8_t *>(temp_buffer)[byte];
}
}
free(temp_buffer);
return rc;
}