aboutsummaryrefslogtreecommitdiff
path: root/kernel/kapi/devices/bus.cpp
blob: d917eb96a441d49a7055d3a9df9928593e4b8629 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <kapi/devices/bus.hpp>

#include <kapi/devices.hpp>
#include <kapi/devices/bus_protocol.hpp>
#include <kapi/devices/driver_registry.hpp>
#include <kapi/system.hpp>

#include <kstd/memory.hpp>
#include <kstd/string.hpp>
#include <kstd/vector.hpp>

#include <algorithm>
#include <span>
#include <utility>

namespace kapi::devices
{
  bus::bus(kstd::string const & name)
      : device{name}
  {}

  bus::bus(kstd::string const & name, bus_protocol & protocol)
      : device{name}
      , m_protocol{&protocol}
  {}

  bus::~bus()
  {
    auto children = m_devices;
    for (auto const & child : children)
    {
      remove_child(*child);
    }
  }

  auto bus::add_child(kstd::shared_ptr<device> child) -> void
  {
    child->set_parent(kstd::static_pointer_cast<bus>(shared_from_this()));

    if (!kapi::devices::device_registry::get().add(child))
    {
      kapi::system::panic("[OS:DEV] Failed to register child device {}", child->name());
    }

    // TODO: lock bus

    auto & attached = m_devices.emplace_back(std::move(child));
    attached->set_state(state::present);

    // TODO: unlock bus

    driver_registry::get().device_attached(attached);
  }

  auto bus::remove_child(device & device) -> bool  // NOLINT(misc-no-recursion)
  {
    // TODO: lock bus

    auto found = std::ranges::find_if(m_devices, [&](auto const & d) { return d.get() == &device; });
    if (!found)
    {
      return false;
    }

    auto lifeline = *found;
    m_devices.erase(found);

    // TODO: unlock bus

    do_remove_child(*lifeline);
    return true;
  }

  [[nodiscard]] auto bus::children() const -> std::span<kstd::shared_ptr<device> const>
  {
    return {m_devices.data(), m_devices.size()};
  }

  auto bus::query_facet(kapi::capabilities::facet_id facet) -> void *
  {
    if (facet == bus::id)
    {
      return this;
    }
    else if (facet == bus_protocol::id)
    {
      return m_protocol;
    }

    return device::query_facet(facet);
  }

  auto bus::do_remove_child(device & device) -> void  // NOLINT(misc-no-recursion)
  {
    if (device.state() == state::removed)
    {
      return;
    }

    if (auto child_bus = device.facet<bus>())
    {
      auto grandchildren = kstd::vector<kstd::shared_ptr<kapi::devices::device>>{};
      // TODO: lock child bus
      // NOTE: this could be more efficient one vector::assign is implemented.
      grandchildren = kstd::vector(child_bus->m_devices.begin(), child_bus->m_devices.end());
      // TODO: unlock child bus

      for (auto & grandchild : grandchildren)
      {
        child_bus->remove_child(*grandchild);
      }
    }

    device.set_state(state::removed);

    if (auto bound_driver = device.bound_driver())
    {
      bound_driver->unbind(device);
    }

    facet_registry::get().withdraw_all_for(device);
    device_registry::get().remove(device);
    device.set_parent(kstd::weak_ptr<bus>{});
  }

}  // namespace kapi::devices