2019-10-31 18:00:42 +00:00
|
|
|
#include "SwitchUSBEndpoint.h"
|
|
|
|
#include <cstring>
|
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
SwitchUSBEndpoint::SwitchUSBEndpoint(UsbHsClientIfSession &if_session, usb_endpoint_descriptor &desc)
|
2019-11-06 12:28:56 +00:00
|
|
|
: m_ifSession(&if_session),
|
|
|
|
m_descriptor(&desc)
|
2019-10-31 18:00:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SwitchUSBEndpoint::~SwitchUSBEndpoint()
|
|
|
|
{
|
2020-04-17 09:05:02 +00:00
|
|
|
if (m_buffer != nullptr)
|
|
|
|
{
|
|
|
|
free(m_buffer);
|
|
|
|
m_buffer = nullptr;
|
|
|
|
}
|
2019-10-31 18:00:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 17:56:22 +00:00
|
|
|
Result SwitchUSBEndpoint::Open(int maxPacketSize)
|
2019-10-31 18:00:42 +00:00
|
|
|
{
|
2019-11-25 12:58:01 +00:00
|
|
|
maxPacketSize = maxPacketSize != 0 ? maxPacketSize : m_descriptor->wMaxPacketSize;
|
|
|
|
|
|
|
|
Result rc = usbHsIfOpenUsbEp(m_ifSession, &m_epSession, 1, maxPacketSize, m_descriptor);
|
2019-10-31 18:00:42 +00:00
|
|
|
if (R_FAILED(rc))
|
|
|
|
return 73011;
|
2019-11-25 12:58:01 +00:00
|
|
|
|
2020-04-17 09:05:02 +00:00
|
|
|
if (m_buffer != nullptr)
|
|
|
|
free(m_buffer);
|
2019-11-25 12:58:01 +00:00
|
|
|
m_buffer = memalign(0x1000, maxPacketSize);
|
|
|
|
if (m_buffer == nullptr)
|
|
|
|
return -1;
|
2019-10-31 18:00:42 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SwitchUSBEndpoint::Close()
|
|
|
|
{
|
|
|
|
usbHsEpClose(&m_epSession);
|
|
|
|
}
|
|
|
|
|
2019-11-08 08:59:57 +00:00
|
|
|
Result SwitchUSBEndpoint::Write(const void *inBuffer, size_t bufferSize)
|
2019-10-31 18:00:42 +00:00
|
|
|
{
|
2019-11-25 12:58:01 +00:00
|
|
|
if (m_buffer == nullptr)
|
2019-11-11 18:23:52 +00:00
|
|
|
return -1;
|
|
|
|
u32 transferredSize = 0;
|
2019-10-31 18:00:42 +00:00
|
|
|
|
2020-04-17 09:04:36 +00:00
|
|
|
memcpy(m_buffer, inBuffer, bufferSize);
|
2019-11-08 12:55:54 +00:00
|
|
|
|
2019-11-25 12:58:01 +00:00
|
|
|
Result rc = usbHsEpPostBuffer(&m_epSession, m_buffer, bufferSize, &transferredSize);
|
2019-11-11 18:23:52 +00:00
|
|
|
|
|
|
|
if (R_SUCCEEDED(rc))
|
|
|
|
{
|
|
|
|
svcSleepThread(m_descriptor->bInterval * 1e+6L);
|
2019-10-31 18:00:42 +00:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result SwitchUSBEndpoint::Read(void *outBuffer, size_t bufferSize)
|
|
|
|
{
|
2019-11-25 12:58:01 +00:00
|
|
|
if (m_buffer == nullptr)
|
2019-10-31 18:00:42 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
u32 transferredSize;
|
|
|
|
|
2019-11-25 12:58:01 +00:00
|
|
|
Result rc = usbHsEpPostBuffer(&m_epSession, m_buffer, bufferSize, &transferredSize);
|
2019-10-31 18:00:42 +00:00
|
|
|
|
|
|
|
if (R_SUCCEEDED(rc))
|
|
|
|
{
|
2020-04-17 09:04:36 +00:00
|
|
|
memcpy(outBuffer, m_buffer, transferredSize);
|
2019-10-31 18:00:42 +00:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
IUSBEndpoint::Direction SwitchUSBEndpoint::GetDirection()
|
|
|
|
{
|
2019-11-06 12:28:56 +00:00
|
|
|
return ((m_descriptor->bEndpointAddress & USB_ENDPOINT_IN) ? USB_ENDPOINT_IN : USB_ENDPOINT_OUT);
|
2019-10-31 18:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IUSBEndpoint::EndpointDescriptor *SwitchUSBEndpoint::GetDescriptor()
|
|
|
|
{
|
2019-11-06 12:28:56 +00:00
|
|
|
return reinterpret_cast<IUSBEndpoint::EndpointDescriptor *>(m_descriptor);
|
2019-10-31 18:00:42 +00:00
|
|
|
}
|