From fbc31c3e385363f9eaba93238756a739e09ce873 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 23 Jul 2026 21:09:34 +0200 Subject: kapi: add driver registry --- kapi/kapi/devices/bus.hpp | 5 ++++ kapi/kapi/devices/device.hpp | 27 +++++++++++++++++++ kapi/kapi/devices/driver_registry.hpp | 51 +++++++++++++++++++++++++++++++++++ kapi/kapi/devices/manager.hpp | 6 +++++ kapi/kapi/test_support/devices.hpp | 1 + 5 files changed, 90 insertions(+) create mode 100644 kapi/kapi/devices/driver_registry.hpp (limited to 'kapi') diff --git a/kapi/kapi/devices/bus.hpp b/kapi/kapi/devices/bus.hpp index 6356e166..0e654072 100644 --- a/kapi/kapi/devices/bus.hpp +++ b/kapi/kapi/devices/bus.hpp @@ -41,6 +41,11 @@ namespace kapi::devices [[nodiscard]] auto children() const -> std::span const>; + //! Get this bus's protocol facet, if present. + //! + //! @return this bus's protocol facet if it has one, nullptr otherwise. + [[nodiscard]] virtual auto protocol() -> struct bus_protocol *; + protected: //! Enumerate the devices on the bus. //! diff --git a/kapi/kapi/devices/device.hpp b/kapi/kapi/devices/device.hpp index 6ffd052d..c1b2065b 100644 --- a/kapi/kapi/devices/device.hpp +++ b/kapi/kapi/devices/device.hpp @@ -14,6 +14,15 @@ namespace kapi::devices //! @addtogroup kapi-devices-kernel-defined //! @{ + enum struct state + { + uninitialized, + present, + bound, + failed, + removed, + }; + /** * @brief Base device identified by a name. */ @@ -65,6 +74,21 @@ namespace kapi::devices //! @return a shared pointer to the parent bus, or nullptr if this device has no parent. [[nodiscard]] auto parent() const -> kstd::shared_ptr; + //! Get this devices current state. + [[nodiscard]] auto state() const noexcept -> state; + + //! Set the lifecycle state of this device. + auto set_state(enum state state) -> void; + + //! Bind this device to the given driver. + auto bind_driver(kstd::weak_ptr driver) -> void; + + //! Get the driver data associated with this device. + [[nodiscard]] auto driver_data() const -> kstd::shared_ptr const &; + + //! Set the driver data for this device. + auto set_driver_data(kstd::shared_ptr data) -> void; + protected: auto virtual query_interface(interface interface) -> void *; @@ -75,6 +99,9 @@ namespace kapi::devices kstd::string m_name; kstd::weak_ptr m_parent; + devices::state m_state{state::uninitialized}; + kstd::weak_ptr m_driver{}; + kstd::shared_ptr m_driver_data{}; }; //! @} diff --git a/kapi/kapi/devices/driver_registry.hpp b/kapi/kapi/devices/driver_registry.hpp new file mode 100644 index 00000000..26a81678 --- /dev/null +++ b/kapi/kapi/devices/driver_registry.hpp @@ -0,0 +1,51 @@ +#ifndef TEACHOS_KAPI_DEVICES_DRIVER_REGISTRY_HPP +#define TEACHOS_KAPI_DEVICES_DRIVER_REGISTRY_HPP + +#include +#include + +#include +#include + +namespace kapi::devices +{ + + //! @addtogroup kapi-devices-kernel-defined + //! @{ + + struct driver_registry + { + driver_registry() = default; + + auto static init() -> void; + + auto static get() -> driver_registry &; + + //! Register a new driver. + //! + //! The driver will be tried against all devices in the device tree that are not yet bound by a different driver. + //! + //! @param driver The driver to register. + auto add(kstd::shared_ptr driver) -> void; + + //! Notify the registry about a new device having been attached to the device tree. + //! + //! The device will be tried against all currently registered drivers. + //! + //! @param device The device that was attached. + auto device_attached(kstd::shared_ptr const & device) -> void; + + private: + //! Try to bind a given device against every currently registered driver, if it is not bound yet. + //! + //! Driver matching will occur in driver registration order. The highest priority driver will win. + auto try_bind(kstd::shared_ptr const & device) -> void; + + kstd::vector> m_drivers; + }; + + //! @} + +} // namespace kapi::devices + +#endif \ No newline at end of file diff --git a/kapi/kapi/devices/manager.hpp b/kapi/kapi/devices/manager.hpp index 5b620b27..de6ebf97 100644 --- a/kapi/kapi/devices/manager.hpp +++ b/kapi/kapi/devices/manager.hpp @@ -6,6 +6,7 @@ #include #include +#include #include @@ -33,6 +34,11 @@ namespace kapi::devices //! @return a shared pointer to the device iff. the device was found and is still alive, nullptr otherwise. auto find_device(std::string_view name) -> kstd::shared_ptr; + //! Get all currently registered devices. + //! + //! @return every device current reachable through the registry. + [[nodiscard]] auto all() -> kstd::vector>; + //! @} } // namespace kapi::devices diff --git a/kapi/kapi/test_support/devices.hpp b/kapi/kapi/test_support/devices.hpp index ab0911fc..b4d410d6 100644 --- a/kapi/kapi/test_support/devices.hpp +++ b/kapi/kapi/test_support/devices.hpp @@ -5,6 +5,7 @@ namespace kapi::test_support::devices { auto deinit() -> void; auto deinit_interface_registry() -> void; + auto deinit_driver_registry() -> void; } // namespace kapi::test_support::devices #endif -- cgit v1.2.3