Kernel: Cleanup USB initialization code

This commit is contained in:
Bananymous 2024-07-31 23:23:44 +03:00
parent e52dac3b25
commit 7c3b6307d9
1 changed files with 12 additions and 2 deletions

View File

@ -10,14 +10,18 @@ namespace Kernel
BAN::ErrorOr<void> USBDevice::initialize() BAN::ErrorOr<void> USBDevice::initialize()
{ {
dprintln_if(DEBUG_USB, "initializing control endpoint");
TRY(initialize_control_endpoint()); TRY(initialize_control_endpoint());
m_dma_buffer = TRY(DMARegion::create(1024)); m_dma_buffer = TRY(DMARegion::create(1024));
dprintln_if(DEBUG_USB, "getting device descriptor");
USBDeviceRequest request; USBDeviceRequest request;
request.bmRequestType = USB::RequestType::DeviceToHost | USB::RequestType::Standard | USB::RequestType::Device; request.bmRequestType = USB::RequestType::DeviceToHost | USB::RequestType::Standard | USB::RequestType::Device;
request.bRequest = USB::Request::GET_DESCRIPTOR; request.bRequest = USB::Request::GET_DESCRIPTOR;
request.wValue = 0x0100; request.wValue = static_cast<uint16_t>(USB::DescriptorType::DEVICE) << 8;
request.wIndex = 0; request.wIndex = 0;
request.wLength = sizeof(USBDeviceDescriptor); request.wLength = sizeof(USBDeviceDescriptor);
auto transferred = TRY(send_request(request, m_dma_buffer->paddr())); auto transferred = TRY(send_request(request, m_dma_buffer->paddr()));
@ -236,11 +240,17 @@ namespace Kernel
USBDeviceRequest request; USBDeviceRequest request;
request.bmRequestType = USB::RequestType::DeviceToHost | USB::RequestType::Standard | USB::RequestType::Device; request.bmRequestType = USB::RequestType::DeviceToHost | USB::RequestType::Standard | USB::RequestType::Device;
request.bRequest = USB::Request::GET_DESCRIPTOR; request.bRequest = USB::Request::GET_DESCRIPTOR;
request.wValue = 0x0200 | index; request.wValue = (static_cast<uint16_t>(USB::DescriptorType::CONFIGURATION) << 8) | index;
request.wIndex = 0; request.wIndex = 0;
request.wLength = m_dma_buffer->size(); request.wLength = m_dma_buffer->size();
auto transferred = TRY(send_request(request, m_dma_buffer->paddr())); auto transferred = TRY(send_request(request, m_dma_buffer->paddr()));
if (transferred < sizeof(USBConfigurationDescriptor))
{
dwarnln("usb device responded with only {} bytes of configuration", transferred);
return BAN::Error::from_errno(EINVAL);
}
auto configuration = *reinterpret_cast<const USBConfigurationDescriptor*>(m_dma_buffer->vaddr()); auto configuration = *reinterpret_cast<const USBConfigurationDescriptor*>(m_dma_buffer->vaddr());
dprintln_if(DEBUG_USB, "configuration {} is {} bytes", index, +configuration.wTotalLength); dprintln_if(DEBUG_USB, "configuration {} is {} bytes", index, +configuration.wTotalLength);