aboutsummaryrefslogtreecommitdiff
path: root/kapi
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-07-23 21:09:34 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-07-23 21:46:16 +0200
commitfbc31c3e385363f9eaba93238756a739e09ce873 (patch)
tree05343a458fb1e40df5babb46bca4a217fe6d1503 /kapi
parentcb87b4dd8e874d5d7b219487ca38f55756c205df (diff)
downloadkernel-fbc31c3e385363f9eaba93238756a739e09ce873.tar.xz
kernel-fbc31c3e385363f9eaba93238756a739e09ce873.zip
kapi: add driver registry
Diffstat (limited to 'kapi')
-rw-r--r--kapi/kapi/devices/bus.hpp5
-rw-r--r--kapi/kapi/devices/device.hpp27
-rw-r--r--kapi/kapi/devices/driver_registry.hpp51
-rw-r--r--kapi/kapi/devices/manager.hpp6
-rw-r--r--kapi/kapi/test_support/devices.hpp1
5 files changed, 90 insertions, 0 deletions
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<kstd::shared_ptr<device> 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<struct bus>;
+ //! 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<struct driver> driver) -> void;
+
+ //! Get the driver data associated with this device.
+ [[nodiscard]] auto driver_data() const -> kstd::shared_ptr<void> const &;
+
+ //! Set the driver data for this device.
+ auto set_driver_data(kstd::shared_ptr<void> data) -> void;
+
protected:
auto virtual query_interface(interface interface) -> void *;
@@ -75,6 +99,9 @@ namespace kapi::devices
kstd::string m_name;
kstd::weak_ptr<bus> m_parent;
+ devices::state m_state{state::uninitialized};
+ kstd::weak_ptr<struct driver> m_driver{};
+ kstd::shared_ptr<void> 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 <kapi/devices/device.hpp>
+#include <kapi/devices/driver.hpp>
+
+#include <kstd/memory.hpp>
+#include <kstd/vector.hpp>
+
+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> 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<device> 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<device> const & device) -> void;
+
+ kstd::vector<kstd::shared_ptr<driver>> 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 <kapi/devices/device.hpp>
#include <kstd/memory.hpp>
+#include <kstd/vector.hpp>
#include <string_view>
@@ -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<device>;
+ //! Get all currently registered devices.
+ //!
+ //! @return every device current reachable through the registry.
+ [[nodiscard]] auto all() -> kstd::vector<kstd::shared_ptr<device>>;
+
//! @}
} // 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