1
0
mirror of https://github.com/cathery/sys-con.git synced 2024-10-04 13:29:43 +00:00

Fixed input packets bleeding into each other to fix #78

This commit is contained in:
cathery 2019-11-25 15:58:01 +03:00
parent c2e12bc2e9
commit 7356a742c9
2 changed files with 21 additions and 14 deletions

View File

@ -10,6 +10,8 @@ private:
UsbHsClientIfSession *m_ifSession; UsbHsClientIfSession *m_ifSession;
usb_endpoint_descriptor *m_descriptor; usb_endpoint_descriptor *m_descriptor;
void *m_buffer = nullptr;
public: public:
//Pass the necessary information to be able to open the endpoint //Pass the necessary information to be able to open the endpoint
SwitchUSBEndpoint(UsbHsClientIfSession &if_session, usb_endpoint_descriptor &desc); SwitchUSBEndpoint(UsbHsClientIfSession &if_session, usb_endpoint_descriptor &desc);

View File

@ -15,60 +15,65 @@ SwitchUSBEndpoint::~SwitchUSBEndpoint()
Result SwitchUSBEndpoint::Open(int maxPacketSize) 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)) if (R_FAILED(rc))
return 73011; return 73011;
m_buffer = memalign(0x1000, maxPacketSize);
if (m_buffer == nullptr)
return -1;
return rc; return rc;
} }
void SwitchUSBEndpoint::Close() void SwitchUSBEndpoint::Close()
{ {
if (m_buffer != nullptr)
{
free(m_buffer);
m_buffer = nullptr;
}
usbHsEpClose(&m_epSession); usbHsEpClose(&m_epSession);
} }
Result SwitchUSBEndpoint::Write(const void *inBuffer, size_t bufferSize) Result SwitchUSBEndpoint::Write(const void *inBuffer, size_t bufferSize)
{ {
void *temp_buffer = memalign(0x1000, bufferSize); if (m_buffer == nullptr)
if (temp_buffer == nullptr)
return -1; return -1;
u32 transferredSize = 0; u32 transferredSize = 0;
for (size_t byte = 0; byte != bufferSize; ++byte) for (size_t byte = 0; byte != bufferSize; ++byte)
{ {
static_cast<uint8_t *>(temp_buffer)[byte] = static_cast<const uint8_t *>(inBuffer)[byte]; static_cast<uint8_t *>(m_buffer)[byte] = static_cast<const uint8_t *>(inBuffer)[byte];
} }
Result rc = usbHsEpPostBuffer(&m_epSession, temp_buffer, bufferSize, &transferredSize); Result rc = usbHsEpPostBuffer(&m_epSession, m_buffer, bufferSize, &transferredSize);
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc))
{ {
svcSleepThread(m_descriptor->bInterval * 1e+6L); svcSleepThread(m_descriptor->bInterval * 1e+6L);
} }
free(temp_buffer);
return rc; return rc;
} }
Result SwitchUSBEndpoint::Read(void *outBuffer, size_t bufferSize) Result SwitchUSBEndpoint::Read(void *outBuffer, size_t bufferSize)
{ {
void *temp_buffer = memalign(0x1000, bufferSize); if (m_buffer == nullptr)
if (temp_buffer == nullptr)
return -1; return -1;
u32 transferredSize; u32 transferredSize;
Result rc = usbHsEpPostBuffer(&m_epSession, temp_buffer, bufferSize, &transferredSize); Result rc = usbHsEpPostBuffer(&m_epSession, m_buffer, bufferSize, &transferredSize);
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc))
{ {
for (u32 byte = 0; byte != transferredSize; ++byte) for (u32 byte = 0; byte != transferredSize; ++byte)
{ {
static_cast<uint8_t *>(outBuffer)[byte] = static_cast<uint8_t *>(temp_buffer)[byte]; static_cast<uint8_t *>(outBuffer)[byte] = static_cast<uint8_t *>(m_buffer)[byte];
} }
} }
free(temp_buffer);
return rc; return rc;
} }