aboutsummaryrefslogtreecommitdiff
path: root/kernel/kapi/devices/bus.cpp
blob: 68874c45aadb8c4cf1df61b33c9bba6ade970f21 (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
56
57
58
59
60
61
62
63
64
65
#include "kapi/devices/bus.hpp"

#include "kapi/devices.hpp"
#include "kapi/system.hpp"

#include <kstd/memory>
#include <kstd/print>
#include <kstd/string>
#include <kstd/vector>

#include <algorithm>
#include <cstddef>
#include <utility>

namespace kapi::devices
{
  bus::bus(std::size_t major, std::size_t minor, kstd::string const & name)
      : device(major, minor, name)
  {}

  auto bus::init() -> bool
  {
    if (m_initialized.test_and_set())
    {
      return true;
    }

    if (!probe())
    {
      return false;
    }

    return std::ranges::fold_left(m_devices, true, [&](bool acc, auto & child) -> bool {
      kstd::println("[OS:DEV] Initializing child device {}@{}", child->name(), name());
      return child->init() && acc;
    });
  }

  auto bus::add_child(kstd::unique_ptr<device> child) -> void
  {
    auto observer = m_observers.emplace_back(child.get());
    m_devices.push_back(std::move(child));
    kapi::devices::register_device(*observer);

    if (m_initialized.test())
    {
      kstd::println("[OS:DEV] Initializing child device {}@{}", observer->name(), name());
      if (!observer->init())
      {
        kapi::system::panic("[OS:DEV] Failed to initialize child device");
      }
    }
  }

  [[nodiscard]] auto bus::children() const -> kstd::vector<kstd::observer_ptr<device>> const &
  {
    return m_observers;
  }

  auto bus::probe() -> bool
  {
    return true;
  }

}  // namespace kapi::devices