aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/devices/root_bus.cpp
blob: a7f3c1a8477b9c9aca7c18cca85fb659f5cfe8d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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