Kernel: Fix bugs and cleanup USB and xHCI code and API

This commit is contained in:
2024-07-11 10:45:25 +03:00
parent 14dce1abac
commit e905634343
6 changed files with 187 additions and 89 deletions

View File

@@ -53,6 +53,18 @@ namespace Kernel
VendorSpecific = 0xFF,
};
enum DescriptorType : uint8_t
{
DEVICE = 1,
CONFIGURATION = 2,
STRING = 3,
INTERFACE = 4,
ENDPOINT = 5,
DEVICE_QUALIFIER = 6,
OTHER_SPEED_CONFIGURATION = 7,
INTERFACE_POWER = 8,
};
enum RequestType : uint8_t
{
HostToDevice = 0b0 << 7,
@@ -117,7 +129,7 @@ namespace Kernel
static_assert(sizeof(USBConfigurationDescriptor) == 9);
static constexpr size_t foo = sizeof(USBConfigurationDescriptor);
struct USBInterfaceDescritor
struct USBInterfaceDescriptor
{
uint8_t bLength;
uint8_t bDescriptorType;
@@ -129,7 +141,7 @@ namespace Kernel
uint8_t bInterfaceProtocol;
uint8_t iInterface;
};
static_assert(sizeof(USBInterfaceDescritor) == 9);
static_assert(sizeof(USBInterfaceDescriptor) == 9);
struct USBEndpointDescriptor
{

View File

@@ -2,6 +2,7 @@
#include <BAN/NoCopyMove.h>
#include <kernel/Memory/DMARegion.h>
#include <kernel/USB/USBManager.h>
#include <kernel/USB/Controller.h>
@@ -21,8 +22,9 @@ namespace Kernel
struct InterfaceDescriptor
{
USBInterfaceDescritor descriptor;
USBInterfaceDescriptor descriptor;
BAN::Vector<EndpointDescriptor> endpoints;
BAN::Vector<BAN::Vector<uint8_t>> misc_descriptors;
};
struct ConfigurationDescriptor
@@ -43,14 +45,21 @@ namespace Kernel
BAN::ErrorOr<void> initialize();
const BAN::Vector<ConfigurationDescriptor>& configurations() { return m_descriptor.configurations; }
virtual BAN::ErrorOr<size_t> send_request(const USBDeviceRequest&, paddr_t buffer) = 0;
static USB::SpeedClass determine_speed_class(uint64_t bits_per_second);
protected:
virtual BAN::ErrorOr<void> initialize_control_endpoint() = 0;
virtual BAN::ErrorOr<void> send_request(const USBDeviceRequest&, paddr_t buffer) = 0;
private:
BAN::ErrorOr<ConfigurationDescriptor> parse_configuration(size_t index);
private:
DeviceDescriptor m_descriptor;
BAN::UniqPtr<DMARegion> m_dma_buffer;
};
}

View File

@@ -227,7 +227,7 @@ namespace Kernel::XHCI
uint64_t data_buffer_pointer : 64;
uint32_t trb_transfer_length : 17;
uint32_t : 5;
uint32_t td_size : 5;
uint32_t interrupt_target : 10;
uint32_t cycle_bit : 1;

View File

@@ -18,21 +18,24 @@ namespace Kernel
struct Endpoint
{
BAN::UniqPtr<DMARegion> transfer_ring;
uint32_t dequeue_index { 0 };
uint32_t enqueue_index { 0 };
bool cycle_bit { 1 };
Mutex mutex;
volatile uint32_t transfer_count { 0 };
volatile XHCI::TRB completion_trb;
};
public:
static BAN::ErrorOr<BAN::UniqPtr<XHCIDevice>> create(XHCIController&, uint32_t port_id, uint32_t slot_id);
BAN::ErrorOr<size_t> send_request(const USBDeviceRequest&, paddr_t buffer) override;
void on_transfer_event(const volatile XHCI::TRB&);
protected:
BAN::ErrorOr<void> initialize_control_endpoint() override;
BAN::ErrorOr<void> send_request(const USBDeviceRequest&, paddr_t buffer) override;
private:
XHCIDevice(XHCIController& controller, uint32_t port_id, uint32_t slot_id)
@@ -46,7 +49,7 @@ namespace Kernel
void advance_endpoint_enqueue(Endpoint&, bool chain);
private:
static constexpr uint32_t m_transfer_ring_trb_count = 256;
static constexpr uint32_t m_transfer_ring_trb_count = PAGE_SIZE / sizeof(XHCI::TRB);
XHCIController& m_controller;
const uint32_t m_port_id;