From 7c3b6307d9ca654b435798557dba22baf6de3df0 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 31 Jul 2024 23:23:44 +0300 Subject: [PATCH] Kernel: Cleanup USB initialization code --- kernel/kernel/USB/Device.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/kernel/kernel/USB/Device.cpp b/kernel/kernel/USB/Device.cpp index f12b34a9..c16cac86 100644 --- a/kernel/kernel/USB/Device.cpp +++ b/kernel/kernel/USB/Device.cpp @@ -10,14 +10,18 @@ namespace Kernel BAN::ErrorOr USBDevice::initialize() { + dprintln_if(DEBUG_USB, "initializing control endpoint"); + TRY(initialize_control_endpoint()); m_dma_buffer = TRY(DMARegion::create(1024)); + dprintln_if(DEBUG_USB, "getting device descriptor"); + USBDeviceRequest request; request.bmRequestType = USB::RequestType::DeviceToHost | USB::RequestType::Standard | USB::RequestType::Device; request.bRequest = USB::Request::GET_DESCRIPTOR; - request.wValue = 0x0100; + request.wValue = static_cast(USB::DescriptorType::DEVICE) << 8; request.wIndex = 0; request.wLength = sizeof(USBDeviceDescriptor); auto transferred = TRY(send_request(request, m_dma_buffer->paddr())); @@ -236,11 +240,17 @@ namespace Kernel USBDeviceRequest request; request.bmRequestType = USB::RequestType::DeviceToHost | USB::RequestType::Standard | USB::RequestType::Device; request.bRequest = USB::Request::GET_DESCRIPTOR; - request.wValue = 0x0200 | index; + request.wValue = (static_cast(USB::DescriptorType::CONFIGURATION) << 8) | index; request.wIndex = 0; request.wLength = m_dma_buffer->size(); 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(m_dma_buffer->vaddr()); dprintln_if(DEBUG_USB, "configuration {} is {} bytes", index, +configuration.wTotalLength);