mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 05:37:44 +00:00
Kernel: Replace KResult and KResultOr<T> with Error and ErrorOr<T>
We now use AK::Error and AK::ErrorOr<T> in both kernel and userspace! This was a slightly tedious refactoring that took a long time, so it's not unlikely that some bugs crept in. Nevertheless, it does pass basic functionality testing, and it's just real nice to finally see the same pattern in all contexts. :^)
This commit is contained in:
parent
7ee10c6926
commit
79fa9765ca
262 changed files with 2415 additions and 2600 deletions
|
@ -23,7 +23,7 @@ SysFSUSBDeviceInformation::~SysFSUSBDeviceInformation()
|
|||
{
|
||||
}
|
||||
|
||||
KResult SysFSUSBDeviceInformation::try_generate(KBufferBuilder& builder)
|
||||
ErrorOr<void> SysFSUSBDeviceInformation::try_generate(KBufferBuilder& builder)
|
||||
{
|
||||
VERIFY(m_lock.is_locked());
|
||||
JsonArraySerializer array { builder };
|
||||
|
@ -44,10 +44,10 @@ KResult SysFSUSBDeviceInformation::try_generate(KBufferBuilder& builder)
|
|||
obj.add("num_configurations", m_device->device_descriptor().num_configurations);
|
||||
obj.finish();
|
||||
array.finish();
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
KResult SysFSUSBDeviceInformation::refresh_data(OpenFileDescription& description) const
|
||||
ErrorOr<void> SysFSUSBDeviceInformation::refresh_data(OpenFileDescription& description) const
|
||||
{
|
||||
MutexLocker lock(m_lock);
|
||||
auto& cached_data = description.data();
|
||||
|
@ -60,10 +60,10 @@ KResult SysFSUSBDeviceInformation::refresh_data(OpenFileDescription& description
|
|||
typed_cached_data.buffer = builder.build();
|
||||
if (!typed_cached_data.buffer)
|
||||
return ENOMEM;
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
KResultOr<size_t> SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const
|
||||
ErrorOr<size_t> SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const
|
||||
{
|
||||
dbgln_if(PROCFS_DEBUG, "SysFSUSBDeviceInformation @ {}: read_bytes offset: {} count: {}", name(), offset, count);
|
||||
|
||||
|
@ -71,13 +71,13 @@ KResultOr<size_t> SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t cou
|
|||
VERIFY(buffer.user_or_kernel_ptr());
|
||||
|
||||
if (!description)
|
||||
return KResult(EIO);
|
||||
return Error::from_errno(EIO);
|
||||
|
||||
MutexLocker locker(m_lock);
|
||||
|
||||
if (!description->data()) {
|
||||
dbgln("SysFSUSBDeviceInformation: Do not have cached data!");
|
||||
return KResult(EIO);
|
||||
return Error::from_errno(EIO);
|
||||
}
|
||||
|
||||
auto& typed_cached_data = static_cast<SysFSInodeData&>(*description->data());
|
||||
|
@ -91,7 +91,7 @@ KResultOr<size_t> SysFSUSBDeviceInformation::read_bytes(off_t offset, size_t cou
|
|||
return nread;
|
||||
}
|
||||
|
||||
KResult SysFSUSBBusDirectory::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
|
||||
ErrorOr<void> SysFSUSBBusDirectory::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
|
||||
{
|
||||
SpinlockLocker lock(m_lock);
|
||||
// Note: if the parent directory is null, it means something bad happened as this should not happen for the USB directory.
|
||||
|
@ -103,7 +103,7 @@ KResult SysFSUSBBusDirectory::traverse_as_directory(unsigned fsid, Function<bool
|
|||
InodeIdentifier identifier = { fsid, device_node.component_index() };
|
||||
callback({ device_node.name(), identifier, 0 });
|
||||
}
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
RefPtr<SysFSComponent> SysFSUSBBusDirectory::lookup(StringView name)
|
||||
|
|
|
@ -26,15 +26,15 @@ public:
|
|||
protected:
|
||||
explicit SysFSUSBDeviceInformation(USB::Device& device);
|
||||
|
||||
virtual KResultOr<size_t> read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const override;
|
||||
virtual ErrorOr<size_t> read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const override;
|
||||
|
||||
IntrusiveListNode<SysFSUSBDeviceInformation, RefPtr<SysFSUSBDeviceInformation>> m_list_node;
|
||||
|
||||
NonnullRefPtr<USB::Device> m_device;
|
||||
|
||||
private:
|
||||
KResult try_generate(KBufferBuilder&);
|
||||
virtual KResult refresh_data(OpenFileDescription& description) const override;
|
||||
ErrorOr<void> try_generate(KBufferBuilder&);
|
||||
virtual ErrorOr<void> refresh_data(OpenFileDescription& description) const override;
|
||||
mutable Mutex m_lock { "SysFSUSBDeviceInformation" };
|
||||
};
|
||||
|
||||
|
@ -46,7 +46,7 @@ public:
|
|||
void plug(USB::Device&);
|
||||
void unplug(USB::Device&);
|
||||
|
||||
virtual KResult traverse_as_directory(unsigned, Function<bool(FileSystem::DirectoryEntryView const&)>) const override;
|
||||
virtual ErrorOr<void> traverse_as_directory(unsigned, Function<bool(FileSystem::DirectoryEntryView const&)>) const override;
|
||||
virtual RefPtr<SysFSComponent> lookup(StringView name) override;
|
||||
|
||||
private:
|
||||
|
|
|
@ -62,7 +62,7 @@ static constexpr u16 UCHI_PORTSC_NON_WRITE_CLEAR_BIT_MASK = 0x1FF5; // This is u
|
|||
static constexpr u8 UHCI_NUMBER_OF_ISOCHRONOUS_TDS = 128;
|
||||
static constexpr u16 UHCI_NUMBER_OF_FRAMES = 1024;
|
||||
|
||||
KResultOr<NonnullRefPtr<UHCIController>> UHCIController::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
|
||||
ErrorOr<NonnullRefPtr<UHCIController>> UHCIController::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
|
||||
{
|
||||
// NOTE: This assumes that address is pointing to a valid UHCI controller.
|
||||
auto controller = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) UHCIController(pci_device_identifier)));
|
||||
|
@ -70,7 +70,7 @@ KResultOr<NonnullRefPtr<UHCIController>> UHCIController::try_to_initialize(PCI::
|
|||
return controller;
|
||||
}
|
||||
|
||||
KResult UHCIController::initialize()
|
||||
ErrorOr<void> UHCIController::initialize()
|
||||
{
|
||||
dmesgln("UHCI: Controller found {} @ {}", PCI::get_hardware_id(pci_address()), pci_address());
|
||||
dmesgln("UHCI: I/O base {}", m_io_base);
|
||||
|
@ -93,7 +93,7 @@ UNMAP_AFTER_INIT UHCIController::~UHCIController()
|
|||
{
|
||||
}
|
||||
|
||||
KResult UHCIController::reset()
|
||||
ErrorOr<void> UHCIController::reset()
|
||||
{
|
||||
TRY(stop());
|
||||
|
||||
|
@ -126,10 +126,10 @@ KResult UHCIController::reset()
|
|||
write_usbintr(0);
|
||||
dbgln("UHCI: Reset completed");
|
||||
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT KResult UHCIController::create_structures()
|
||||
UNMAP_AFTER_INIT ErrorOr<void> UHCIController::create_structures()
|
||||
{
|
||||
m_queue_head_pool = TRY(UHCIDescriptorPool<QueueHead>::try_create("Queue Head Pool"sv));
|
||||
|
||||
|
@ -173,7 +173,7 @@ UNMAP_AFTER_INIT KResult UHCIController::create_structures()
|
|||
m_transfer_descriptor_pool->print_pool_information();
|
||||
}
|
||||
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
UNMAP_AFTER_INIT void UHCIController::setup_schedule()
|
||||
|
@ -245,7 +245,7 @@ TransferDescriptor* UHCIController::allocate_transfer_descriptor()
|
|||
return m_transfer_descriptor_pool->try_take_free_descriptor();
|
||||
}
|
||||
|
||||
KResult UHCIController::stop()
|
||||
ErrorOr<void> UHCIController::stop()
|
||||
{
|
||||
write_usbcmd(read_usbcmd() & ~UHCI_USBCMD_RUN);
|
||||
// FIXME: Timeout
|
||||
|
@ -253,10 +253,10 @@ KResult UHCIController::stop()
|
|||
if (read_usbsts() & UHCI_USBSTS_HOST_CONTROLLER_HALTED)
|
||||
break;
|
||||
}
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
KResult UHCIController::start()
|
||||
ErrorOr<void> UHCIController::start()
|
||||
{
|
||||
write_usbcmd(read_usbcmd() | UHCI_USBCMD_RUN);
|
||||
// FIXME: Timeout
|
||||
|
@ -268,7 +268,7 @@ KResult UHCIController::start()
|
|||
|
||||
m_root_hub = TRY(UHCIRootHub::try_create(*this));
|
||||
TRY(m_root_hub->setup({}));
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
TransferDescriptor* UHCIController::create_transfer_descriptor(Pipe& pipe, PacketID direction, size_t data_len)
|
||||
|
@ -303,7 +303,7 @@ TransferDescriptor* UHCIController::create_transfer_descriptor(Pipe& pipe, Packe
|
|||
return td;
|
||||
}
|
||||
|
||||
KResult UHCIController::create_chain(Pipe& pipe, PacketID direction, Ptr32<u8>& buffer_address, size_t max_size, size_t transfer_size, TransferDescriptor** td_chain, TransferDescriptor** last_td)
|
||||
ErrorOr<void> UHCIController::create_chain(Pipe& pipe, PacketID direction, Ptr32<u8>& buffer_address, size_t max_size, size_t transfer_size, TransferDescriptor** td_chain, TransferDescriptor** last_td)
|
||||
{
|
||||
// We need to create `n` transfer descriptors based on the max
|
||||
// size of each transfer (which we've learned from the device already by reading
|
||||
|
@ -345,7 +345,7 @@ KResult UHCIController::create_chain(Pipe& pipe, PacketID direction, Ptr32<u8>&
|
|||
|
||||
*last_td = current_td;
|
||||
*td_chain = first_td;
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
void UHCIController::free_descriptor_chain(TransferDescriptor* first_descriptor)
|
||||
|
@ -361,7 +361,7 @@ void UHCIController::free_descriptor_chain(TransferDescriptor* first_descriptor)
|
|||
}
|
||||
}
|
||||
|
||||
KResultOr<size_t> UHCIController::submit_control_transfer(Transfer& transfer)
|
||||
ErrorOr<size_t> UHCIController::submit_control_transfer(Transfer& transfer)
|
||||
{
|
||||
Pipe& pipe = transfer.pipe(); // Short circuit the pipe related to this transfer
|
||||
bool direction_in = (transfer.request().request_type & USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST) == USB_REQUEST_TRANSFER_DIRECTION_DEVICE_TO_HOST;
|
||||
|
@ -589,7 +589,7 @@ void UHCIController::reset_port(u8 port)
|
|||
m_port_reset_change_statuses |= (1 << port);
|
||||
}
|
||||
|
||||
KResult UHCIController::set_port_feature(Badge<UHCIRootHub>, u8 port, HubFeatureSelector feature_selector)
|
||||
ErrorOr<void> UHCIController::set_port_feature(Badge<UHCIRootHub>, u8 port, HubFeatureSelector feature_selector)
|
||||
{
|
||||
// The check is done by UHCIRootHub.
|
||||
VERIFY(port < NUMBER_OF_ROOT_PORTS);
|
||||
|
@ -621,10 +621,10 @@ KResult UHCIController::set_port_feature(Badge<UHCIRootHub>, u8 port, HubFeature
|
|||
return EINVAL;
|
||||
}
|
||||
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
KResult UHCIController::clear_port_feature(Badge<UHCIRootHub>, u8 port, HubFeatureSelector feature_selector)
|
||||
ErrorOr<void> UHCIController::clear_port_feature(Badge<UHCIRootHub>, u8 port, HubFeatureSelector feature_selector)
|
||||
{
|
||||
// The check is done by UHCIRootHub.
|
||||
VERIFY(port < NUMBER_OF_ROOT_PORTS);
|
||||
|
@ -670,7 +670,7 @@ KResult UHCIController::clear_port_feature(Badge<UHCIRootHub>, u8 port, HubFeatu
|
|||
else
|
||||
write_portsc2(port_data);
|
||||
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,24 +33,24 @@ class UHCIController final
|
|||
|
||||
public:
|
||||
static constexpr u8 NUMBER_OF_ROOT_PORTS = 2;
|
||||
static KResultOr<NonnullRefPtr<UHCIController>> try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier);
|
||||
static ErrorOr<NonnullRefPtr<UHCIController>> try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier);
|
||||
virtual ~UHCIController() override;
|
||||
|
||||
virtual StringView purpose() const override { return "UHCI"sv; }
|
||||
|
||||
virtual KResult initialize() override;
|
||||
virtual KResult reset() override;
|
||||
virtual KResult stop() override;
|
||||
virtual KResult start() override;
|
||||
virtual ErrorOr<void> initialize() override;
|
||||
virtual ErrorOr<void> reset() override;
|
||||
virtual ErrorOr<void> stop() override;
|
||||
virtual ErrorOr<void> start() override;
|
||||
void spawn_port_proc();
|
||||
|
||||
void do_debug_transfer();
|
||||
|
||||
virtual KResultOr<size_t> submit_control_transfer(Transfer& transfer) override;
|
||||
virtual ErrorOr<size_t> submit_control_transfer(Transfer& transfer) override;
|
||||
|
||||
void get_port_status(Badge<UHCIRootHub>, u8, HubStatus&);
|
||||
KResult set_port_feature(Badge<UHCIRootHub>, u8, HubFeatureSelector);
|
||||
KResult clear_port_feature(Badge<UHCIRootHub>, u8, HubFeatureSelector);
|
||||
ErrorOr<void> set_port_feature(Badge<UHCIRootHub>, u8, HubFeatureSelector);
|
||||
ErrorOr<void> clear_port_feature(Badge<UHCIRootHub>, u8, HubFeatureSelector);
|
||||
|
||||
private:
|
||||
explicit UHCIController(PCI::DeviceIdentifier const& pci_device_identifier);
|
||||
|
@ -75,12 +75,12 @@ private:
|
|||
|
||||
virtual bool handle_irq(const RegisterState&) override;
|
||||
|
||||
KResult create_structures();
|
||||
ErrorOr<void> create_structures();
|
||||
void setup_schedule();
|
||||
size_t poll_transfer_queue(QueueHead& transfer_queue);
|
||||
|
||||
TransferDescriptor* create_transfer_descriptor(Pipe& pipe, PacketID direction, size_t data_len);
|
||||
KResult create_chain(Pipe& pipe, PacketID direction, Ptr32<u8>& buffer_address, size_t max_size, size_t transfer_size, TransferDescriptor** td_chain, TransferDescriptor** last_td);
|
||||
ErrorOr<void> create_chain(Pipe& pipe, PacketID direction, Ptr32<u8>& buffer_address, size_t max_size, size_t transfer_size, TransferDescriptor** td_chain, TransferDescriptor** last_td);
|
||||
void free_descriptor_chain(TransferDescriptor* first_descriptor);
|
||||
|
||||
QueueHead* allocate_queue_head();
|
||||
|
|
|
@ -28,7 +28,7 @@ class UHCIDescriptorPool {
|
|||
static_assert(sizeof(T) <= PAGE_SIZE);
|
||||
|
||||
public:
|
||||
static KResultOr<NonnullOwnPtr<UHCIDescriptorPool<T>>> try_create(StringView name)
|
||||
static ErrorOr<NonnullOwnPtr<UHCIDescriptorPool<T>>> try_create(StringView name)
|
||||
{
|
||||
auto pool_memory_block = TRY(MM.allocate_kernel_region(PAGE_SIZE, "UHCI Descriptor Pool", Memory::Region::Access::ReadWrite));
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) UHCIDescriptorPool(move(pool_memory_block), name));
|
||||
|
|
|
@ -83,7 +83,7 @@ static USBHubDescriptor uhci_root_hub_hub_descriptor = {
|
|||
0x0, // Self-powered
|
||||
};
|
||||
|
||||
KResultOr<NonnullOwnPtr<UHCIRootHub>> UHCIRootHub::try_create(NonnullRefPtr<UHCIController> uhci_controller)
|
||||
ErrorOr<NonnullOwnPtr<UHCIRootHub>> UHCIRootHub::try_create(NonnullRefPtr<UHCIController> uhci_controller)
|
||||
{
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) UHCIRootHub(uhci_controller));
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ UHCIRootHub::UHCIRootHub(NonnullRefPtr<UHCIController> uhci_controller)
|
|||
{
|
||||
}
|
||||
|
||||
KResult UHCIRootHub::setup(Badge<UHCIController>)
|
||||
ErrorOr<void> UHCIRootHub::setup(Badge<UHCIController>)
|
||||
{
|
||||
m_hub = TRY(Hub::try_create_root_hub(m_uhci_controller, Device::DeviceSpeed::FullSpeed));
|
||||
|
||||
|
@ -104,10 +104,10 @@ KResult UHCIRootHub::setup(Badge<UHCIController>)
|
|||
// NOTE: The root hub is no longer on the default address.
|
||||
TRY(m_hub->enumerate_and_power_on_hub());
|
||||
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
KResultOr<size_t> UHCIRootHub::handle_control_transfer(Transfer& transfer)
|
||||
ErrorOr<size_t> UHCIRootHub::handle_control_transfer(Transfer& transfer)
|
||||
{
|
||||
auto& request = transfer.request();
|
||||
auto* request_data = transfer.buffer().as_ptr() + sizeof(USBRequestData);
|
||||
|
|
|
@ -6,9 +6,9 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Error.h>
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <Kernel/API/KResult.h>
|
||||
#include <Kernel/Bus/USB/USBHub.h>
|
||||
#include <Kernel/Bus/USB/USBTransfer.h>
|
||||
|
||||
|
@ -18,16 +18,16 @@ class UHCIController;
|
|||
|
||||
class UHCIRootHub {
|
||||
public:
|
||||
static KResultOr<NonnullOwnPtr<UHCIRootHub>> try_create(NonnullRefPtr<UHCIController>);
|
||||
static ErrorOr<NonnullOwnPtr<UHCIRootHub>> try_create(NonnullRefPtr<UHCIController>);
|
||||
|
||||
UHCIRootHub(NonnullRefPtr<UHCIController>);
|
||||
~UHCIRootHub() = default;
|
||||
|
||||
KResult setup(Badge<UHCIController>);
|
||||
ErrorOr<void> setup(Badge<UHCIController>);
|
||||
|
||||
u8 device_address() const { return m_hub->address(); }
|
||||
|
||||
KResultOr<size_t> handle_control_transfer(Transfer& transfer);
|
||||
ErrorOr<size_t> handle_control_transfer(Transfer& transfer);
|
||||
|
||||
void check_for_port_updates() { m_hub->check_for_port_updates(); }
|
||||
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Error.h>
|
||||
#include <AK/RefCounted.h>
|
||||
#include <Kernel/API/KResult.h>
|
||||
#include <Kernel/Bus/USB/USBDevice.h>
|
||||
#include <Kernel/Bus/USB/USBTransfer.h>
|
||||
|
||||
|
@ -17,13 +17,13 @@ class USBController : public RefCounted<USBController> {
|
|||
public:
|
||||
virtual ~USBController() = default;
|
||||
|
||||
virtual KResult initialize() = 0;
|
||||
virtual ErrorOr<void> initialize() = 0;
|
||||
|
||||
virtual KResult reset() = 0;
|
||||
virtual KResult stop() = 0;
|
||||
virtual KResult start() = 0;
|
||||
virtual ErrorOr<void> reset() = 0;
|
||||
virtual ErrorOr<void> stop() = 0;
|
||||
virtual ErrorOr<void> start() = 0;
|
||||
|
||||
virtual KResultOr<size_t> submit_control_transfer(Transfer&) = 0;
|
||||
virtual ErrorOr<size_t> submit_control_transfer(Transfer&) = 0;
|
||||
|
||||
u8 allocate_address();
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
namespace Kernel::USB {
|
||||
|
||||
KResultOr<NonnullRefPtr<Device>> Device::try_create(USBController const& controller, u8 port, DeviceSpeed speed)
|
||||
ErrorOr<NonnullRefPtr<Device>> Device::try_create(USBController const& controller, u8 port, DeviceSpeed speed)
|
||||
{
|
||||
auto pipe = TRY(Pipe::try_create_pipe(controller, Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, 8, 0));
|
||||
auto device = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Device(controller, port, speed, move(pipe))));
|
||||
|
@ -56,7 +56,7 @@ Device::~Device()
|
|||
{
|
||||
}
|
||||
|
||||
KResult Device::enumerate_device()
|
||||
ErrorOr<void> Device::enumerate_device()
|
||||
{
|
||||
USBDeviceDescriptor dev_descriptor {};
|
||||
|
||||
|
@ -118,7 +118,7 @@ KResult Device::enumerate_device()
|
|||
dbgln_if(USB_DEBUG, "USB Device: Set address to {}", m_address);
|
||||
|
||||
memcpy(&m_device_descriptor, &dev_descriptor, sizeof(USBDeviceDescriptor));
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,13 +27,13 @@ public:
|
|||
};
|
||||
|
||||
public:
|
||||
static KResultOr<NonnullRefPtr<Device>> try_create(USBController const&, u8, DeviceSpeed);
|
||||
static ErrorOr<NonnullRefPtr<Device>> try_create(USBController const&, u8, DeviceSpeed);
|
||||
|
||||
Device(USBController const&, u8, DeviceSpeed, NonnullOwnPtr<Pipe> default_pipe);
|
||||
Device(Device const& device, NonnullOwnPtr<Pipe> default_pipe);
|
||||
virtual ~Device();
|
||||
|
||||
KResult enumerate_device();
|
||||
ErrorOr<void> enumerate_device();
|
||||
|
||||
u8 port() const { return m_device_port; }
|
||||
DeviceSpeed speed() const { return m_device_speed; }
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
namespace Kernel::USB {
|
||||
|
||||
KResultOr<NonnullRefPtr<Hub>> Hub::try_create_root_hub(NonnullRefPtr<USBController> controller, DeviceSpeed device_speed)
|
||||
ErrorOr<NonnullRefPtr<Hub>> Hub::try_create_root_hub(NonnullRefPtr<USBController> controller, DeviceSpeed device_speed)
|
||||
{
|
||||
// NOTE: Enumeration does not happen here, as the controller must know what the device address is at all times during enumeration to intercept requests.
|
||||
auto pipe = TRY(Pipe::try_create_pipe(controller, Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, 8, 0));
|
||||
|
@ -22,7 +22,7 @@ KResultOr<NonnullRefPtr<Hub>> Hub::try_create_root_hub(NonnullRefPtr<USBControll
|
|||
return hub;
|
||||
}
|
||||
|
||||
KResultOr<NonnullRefPtr<Hub>> Hub::try_create_from_device(Device const& device)
|
||||
ErrorOr<NonnullRefPtr<Hub>> Hub::try_create_from_device(Device const& device)
|
||||
{
|
||||
auto pipe = TRY(Pipe::try_create_pipe(device.controller(), Pipe::Type::Control, Pipe::Direction::Bidirectional, 0, device.device_descriptor().max_packet_size, device.address()));
|
||||
auto hub = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Hub(device, move(pipe))));
|
||||
|
@ -40,7 +40,7 @@ Hub::Hub(Device const& device, NonnullOwnPtr<Pipe> default_pipe)
|
|||
{
|
||||
}
|
||||
|
||||
KResult Hub::enumerate_and_power_on_hub()
|
||||
ErrorOr<void> Hub::enumerate_and_power_on_hub()
|
||||
{
|
||||
// USBDevice::enumerate_device must be called before this.
|
||||
VERIFY(m_address > 0);
|
||||
|
@ -85,11 +85,11 @@ KResult Hub::enumerate_and_power_on_hub()
|
|||
|
||||
memcpy(&m_hub_descriptor, &descriptor, sizeof(USBHubDescriptor));
|
||||
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
// USB 2.0 Specification Section 11.24.2.7
|
||||
KResult Hub::get_port_status(u8 port, HubStatus& hub_status)
|
||||
ErrorOr<void> Hub::get_port_status(u8 port, HubStatus& hub_status)
|
||||
{
|
||||
// Ports are 1-based.
|
||||
if (port == 0 || port > m_hub_descriptor.number_of_downstream_ports)
|
||||
|
@ -103,29 +103,29 @@ KResult Hub::get_port_status(u8 port, HubStatus& hub_status)
|
|||
return EIO;
|
||||
}
|
||||
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
// USB 2.0 Specification Section 11.24.2.2
|
||||
KResult Hub::clear_port_feature(u8 port, HubFeatureSelector feature_selector)
|
||||
ErrorOr<void> Hub::clear_port_feature(u8 port, HubFeatureSelector feature_selector)
|
||||
{
|
||||
// Ports are 1-based.
|
||||
if (port == 0 || port > m_hub_descriptor.number_of_downstream_ports)
|
||||
return EINVAL;
|
||||
|
||||
TRY(m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_REQUEST_RECIPIENT_OTHER, HubRequest::CLEAR_FEATURE, feature_selector, port, 0, nullptr));
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
// USB 2.0 Specification Section 11.24.2.13
|
||||
KResult Hub::set_port_feature(u8 port, HubFeatureSelector feature_selector)
|
||||
ErrorOr<void> Hub::set_port_feature(u8 port, HubFeatureSelector feature_selector)
|
||||
{
|
||||
// Ports are 1-based.
|
||||
if (port == 0 || port > m_hub_descriptor.number_of_downstream_ports)
|
||||
return EINVAL;
|
||||
|
||||
TRY(m_default_pipe->control_transfer(USB_REQUEST_TRANSFER_DIRECTION_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_REQUEST_RECIPIENT_OTHER, HubRequest::SET_FEATURE, feature_selector, port, 0, nullptr));
|
||||
return KSuccess;
|
||||
return {};
|
||||
}
|
||||
|
||||
void Hub::remove_children_from_sysfs()
|
||||
|
@ -140,16 +140,14 @@ void Hub::check_for_port_updates()
|
|||
dbgln_if(USB_DEBUG, "USB Hub: Checking for port updates on port {}...", port_number);
|
||||
|
||||
HubStatus port_status {};
|
||||
auto result = get_port_status(port_number, port_status);
|
||||
if (result.is_error()) {
|
||||
if (auto result = get_port_status(port_number, port_status); result.is_error()) {
|
||||
dbgln("USB Hub: Error occurred when getting status for port {}: {}. Checking next port instead.", port_number, result.error());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (port_status.change & PORT_STATUS_CONNECT_STATUS_CHANGED) {
|
||||
// Clear the connection status change notification.
|
||||
result = clear_port_feature(port_number, HubFeatureSelector::C_PORT_CONNECTION);
|
||||
if (result.is_error()) {
|
||||
if (auto result = clear_port_feature(port_number, HubFeatureSelector::C_PORT_CONNECTION); result.is_error()) {
|
||||
dbgln("USB Hub: Error occurred when clearing port connection change for port {}: {}.", port_number, result.error());
|
||||
return;
|
||||
}
|
||||
|
@ -174,8 +172,7 @@ void Hub::check_for_port_updates()
|
|||
IO::delay(debounce_disconnect_check_interval);
|
||||
debounce_timer += debounce_disconnect_check_interval;
|
||||
|
||||
result = get_port_status(port_number, port_status);
|
||||
if (result.is_error()) {
|
||||
if (auto result = get_port_status(port_number, port_status); result.is_error()) {
|
||||
dbgln("USB Hub: Error occurred when getting status while debouncing port {}: {}.", port_number, result.error());
|
||||
return;
|
||||
}
|
||||
|
@ -185,8 +182,8 @@ void Hub::check_for_port_updates()
|
|||
|
||||
dbgln_if(USB_DEBUG, "USB Hub: Connection status changed while debouncing, resetting debounce timer.");
|
||||
debounce_timer = 0;
|
||||
result = clear_port_feature(port_number, HubFeatureSelector::C_PORT_CONNECTION);
|
||||
if (result.is_error()) {
|
||||
|
||||
if (auto result = clear_port_feature(port_number, HubFeatureSelector::C_PORT_CONNECTION); result.is_error()) {
|
||||
dbgln("USB Hub: Error occurred when clearing port connection change while debouncing port {}: {}.", port_number, result.error());
|
||||
return;
|
||||
}
|
||||
|
@ -194,8 +191,7 @@ void Hub::check_for_port_updates()
|
|||
|
||||
// Reset the port
|
||||
dbgln_if(USB_DEBUG, "USB Hub: Debounce finished. Driving reset...");
|
||||
result = set_port_feature(port_number, HubFeatureSelector::PORT_RESET);
|
||||
if (result.is_error()) {
|
||||
if (auto result = set_port_feature(port_number, HubFeatureSelector::PORT_RESET); result.is_error()) {
|
||||
dbgln("USB Hub: Error occurred when resetting port {}: {}.", port_number, result.error());
|
||||
return;
|
||||
}
|
||||
|
@ -207,8 +203,7 @@ void Hub::check_for_port_updates()
|
|||
constexpr u16 reset_delay = 10 * 1000;
|
||||
IO::delay(reset_delay);
|
||||
|
||||
result = get_port_status(port_number, port_status);
|
||||
if (result.is_error()) {
|
||||
if (auto result = get_port_status(port_number, port_status); result.is_error()) {
|
||||
dbgln("USB Hub: Error occurred when getting status while resetting port {}: {}.", port_number, result.error());
|
||||
return;
|
||||
}
|
||||
|
@ -218,8 +213,8 @@ void Hub::check_for_port_updates()
|
|||
}
|
||||
|
||||
// Stop asserting reset. This also causes the port to become enabled.
|
||||
result = clear_port_feature(port_number, HubFeatureSelector::C_PORT_RESET);
|
||||
if (result.is_error()) {
|
||||
|
||||
if (auto result = clear_port_feature(port_number, HubFeatureSelector::C_PORT_RESET); result.is_error()) {
|
||||
dbgln("USB Hub: Error occurred when resetting port {}: {}.", port_number, result.error());
|
||||
return;
|
||||
}
|
||||
|
@ -233,8 +228,7 @@ void Hub::check_for_port_updates()
|
|||
|
||||
// The port is ready to go. This is where we start communicating with the device to set up a driver for it.
|
||||
|
||||
result = get_port_status(port_number, port_status);
|
||||
if (result.is_error()) {
|
||||
if (auto result = get_port_status(port_number, port_status); result.is_error()) {
|
||||
dbgln("USB Hub: Error occurred when getting status for port {} after reset: {}.", port_number, result.error());
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -80,18 +80,18 @@ static constexpr u16 PORT_STATUS_RESET_CHANGED = (1 << 4);
|
|||
|
||||
class Hub : public Device {
|
||||
public:
|
||||
static KResultOr<NonnullRefPtr<Hub>> try_create_root_hub(NonnullRefPtr<USBController>, DeviceSpeed);
|
||||
static KResultOr<NonnullRefPtr<Hub>> try_create_from_device(Device const&);
|
||||
static ErrorOr<NonnullRefPtr<Hub>> try_create_root_hub(NonnullRefPtr<USBController>, DeviceSpeed);
|
||||
static ErrorOr<NonnullRefPtr<Hub>> try_create_from_device(Device const&);
|
||||
|
||||
virtual ~Hub() override = default;
|
||||
|
||||
KResult enumerate_and_power_on_hub();
|
||||
ErrorOr<void> enumerate_and_power_on_hub();
|
||||
|
||||
KResult get_port_status(u8, HubStatus&);
|
||||
KResult clear_port_feature(u8, HubFeatureSelector);
|
||||
KResult set_port_feature(u8, HubFeatureSelector);
|
||||
ErrorOr<void> get_port_status(u8, HubStatus&);
|
||||
ErrorOr<void> clear_port_feature(u8, HubFeatureSelector);
|
||||
ErrorOr<void> set_port_feature(u8, HubFeatureSelector);
|
||||
|
||||
KResult reset_port(u8);
|
||||
ErrorOr<void> reset_port(u8);
|
||||
|
||||
void check_for_port_updates();
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
namespace Kernel::USB {
|
||||
|
||||
KResultOr<NonnullOwnPtr<Pipe>> Pipe::try_create_pipe(USBController const& controller, Type type, Direction direction, u8 endpoint_address, u16 max_packet_size, i8 device_address, u8 poll_interval)
|
||||
ErrorOr<NonnullOwnPtr<Pipe>> Pipe::try_create_pipe(USBController const& controller, Type type, Direction direction, u8 endpoint_address, u16 max_packet_size, i8 device_address, u8 poll_interval)
|
||||
{
|
||||
return adopt_nonnull_own_or_enomem(new (nothrow) Pipe(controller, type, direction, endpoint_address, max_packet_size, poll_interval, device_address));
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ Pipe::Pipe(USBController const& controller, Type type, Direction direction, u8 e
|
|||
{
|
||||
}
|
||||
|
||||
KResultOr<size_t> Pipe::control_transfer(u8 request_type, u8 request, u16 value, u16 index, u16 length, void* data)
|
||||
ErrorOr<size_t> Pipe::control_transfer(u8 request_type, u8 request, u16 value, u16 index, u16 length, void* data)
|
||||
{
|
||||
USBRequestData usb_request;
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ public:
|
|||
};
|
||||
|
||||
public:
|
||||
static KResultOr<NonnullOwnPtr<Pipe>> try_create_pipe(USBController const& controller, Type type, Direction direction, u8 endpoint_address, u16 max_packet_size, i8 device_address, u8 poll_interval = 0);
|
||||
static ErrorOr<NonnullOwnPtr<Pipe>> try_create_pipe(USBController const& controller, Type type, Direction direction, u8 endpoint_address, u16 max_packet_size, i8 device_address, u8 poll_interval = 0);
|
||||
|
||||
Type type() const { return m_type; }
|
||||
Direction direction() const { return m_direction; }
|
||||
|
@ -57,7 +57,7 @@ public:
|
|||
void set_toggle(bool toggle) { m_data_toggle = toggle; }
|
||||
void set_device_address(i8 addr) { m_device_address = addr; }
|
||||
|
||||
KResultOr<size_t> control_transfer(u8 request_type, u8 request, u16 value, u16 index, u16 length, void* data);
|
||||
ErrorOr<size_t> control_transfer(u8 request_type, u8 request, u16 value, u16 index, u16 length, void* data);
|
||||
|
||||
Pipe(USBController const& controller, Type type, Direction direction, u16 max_packet_size);
|
||||
Pipe(USBController const& controller, Type type, Direction direction, USBEndpointDescriptor& endpoint);
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
namespace Kernel::USB {
|
||||
|
||||
KResultOr<NonnullRefPtr<Transfer>> Transfer::try_create(Pipe& pipe, u16 length)
|
||||
ErrorOr<NonnullRefPtr<Transfer>> Transfer::try_create(Pipe& pipe, u16 length)
|
||||
{
|
||||
// Initialize data buffer for transfer
|
||||
// This will definitely need to be refactored in the future, I doubt this will scale well...
|
||||
|
|
|
@ -19,7 +19,7 @@ namespace Kernel::USB {
|
|||
|
||||
class Transfer : public RefCounted<Transfer> {
|
||||
public:
|
||||
static KResultOr<NonnullRefPtr<Transfer>> try_create(Pipe&, u16 length);
|
||||
static ErrorOr<NonnullRefPtr<Transfer>> try_create(Pipe&, u16 length);
|
||||
|
||||
public:
|
||||
Transfer() = delete;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue