aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/devices/root_bus.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/src/devices/root_bus.cpp')
-rw-r--r--kernel/src/devices/root_bus.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/kernel/src/devices/root_bus.cpp b/kernel/src/devices/root_bus.cpp
new file mode 100644
index 0000000..a7f3c1a
--- /dev/null
+++ b/kernel/src/devices/root_bus.cpp
@@ -0,0 +1,55 @@
+#include "kernel/devices/root_bus.hpp"
+
+#include "kapi/devices.hpp"
+#include "kapi/devices/bus.hpp"
+#include "kapi/devices/device.hpp"
+#include "kapi/system.hpp"
+
+#include <kstd/memory>
+#include <kstd/print>
+#include <kstd/vector>
+
+#include <algorithm>
+#include <utility>
+
+namespace kernel::devices
+{
+
+ root_bus::root_bus()
+ : kapi::devices::bus{kapi::devices::allocate_major_number(), 0, "system"}
+ {}
+
+ auto root_bus::add_child(kstd::unique_ptr<device> child) -> void
+ {
+ auto observer = m_observers.emplace_back(child.get());
+ m_children.push_back(std::move(child));
+
+ if (m_initialized)
+ {
+ kstd::println("Initializing child device '{}'", observer->name());
+ if (!observer->init())
+ {
+ kapi::system::panic("[kernel:devices] Failed to initialize child device");
+ }
+ }
+ }
+
+ auto root_bus::children() const -> kstd::vector<kstd::observer_ptr<device>> const &
+ {
+ return m_observers;
+ }
+
+ auto root_bus::init() -> bool
+ {
+ if (m_initialized)
+ {
+ kapi::system::panic("[kernel:devices] Root bus already initialized!");
+ }
+
+ return std::ranges::fold_left(m_children, true, [](bool acc, auto & child) -> bool {
+ kstd::println("[kernel:devices] Initializing child device '{}'", child->name());
+ return acc && child->init();
+ });
+ }
+
+} // namespace kernel::devices \ No newline at end of file