SimpleKernel 1.17.0
Loading...
Searching...
No Matches
device_manager.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <etl/singleton.h>
8
9#include <cassert>
10
11#include "device_node.hpp"
12#include "driver_registry.hpp"
13#include "expected.hpp"
14#include "kernel_log.hpp"
15#include "spinlock.hpp"
16
24 public:
32 template <Bus B>
33 auto RegisterBus(B& bus) -> Expected<void> {
34 LockGuard guard(lock_);
35
37 return std::unexpected(Error(ErrorCode::kOutOfMemory));
38 }
39
40 size_t remaining = kMaxDevices - device_count_;
41 auto result = bus.Enumerate(devices_ + device_count_, remaining);
42 if (!result.has_value()) {
43 klog::Err("DeviceManager: bus '{}' enumeration failed: {}", B::GetName(),
44 result.error().message());
45 return std::unexpected(result.error());
46 }
47
48 size_t count = result.value();
49 for (size_t i = 0; i < count; ++i) {
51 if (!name_index_.full()) {
52 name_index_.insert(
54 }
55 }
56 device_count_ += count;
57
58 klog::Info("DeviceManager: bus '{}' enumerated {} device(s)", B::GetName(),
59 count);
60 return {};
61 }
62
68 [[nodiscard]] auto ProbeAll() -> Expected<void>;
69
78 [[nodiscard]] auto FindDevice(const char* name) -> Expected<DeviceNode*>;
79
88 [[nodiscard]] auto FindDevicesByType(DeviceType type, DeviceNode** out,
89 size_t max) -> size_t;
90
96 [[nodiscard]] auto GetRegistry() -> DriverRegistry& { return registry_; }
97
100 DeviceManager() = default;
101 ~DeviceManager() = default;
102 DeviceManager(const DeviceManager&) = delete;
104 auto operator=(const DeviceManager&) -> DeviceManager& = delete;
107
108 private:
109 static constexpr size_t kMaxDevices = 64;
111 size_t device_count_{0};
112 uint32_t next_dev_id_{0};
114 SpinLock lock_{"device_manager"};
115 etl::flat_map<const char*, size_t, kMaxDevices, CStrLess> name_index_;
116};
117
118using DeviceManagerSingleton = etl::singleton<DeviceManager>;
设备管理器 — 管理所有设备节点和驱动程序。
static constexpr size_t kMaxDevices
auto FindDevicesByType(DeviceType type, DeviceNode **out, size_t max) -> size_t
按类型枚举设备。
auto operator=(DeviceManager &&) -> DeviceManager &=delete
auto FindDevice(const char *name) -> Expected< DeviceNode * >
根据名称查找设备。
etl::flat_map< const char *, size_t, kMaxDevices, CStrLess > name_index_
DeviceNode devices_[kMaxDevices]
auto operator=(const DeviceManager &) -> DeviceManager &=delete
DeviceManager()=default
auto ProbeAll() -> Expected< void >
匹配已注册的驱动程序并探测所有未绑定的设备。
DeviceManager(DeviceManager &&)=delete
auto RegisterBus(B &bus) -> Expected< void >
注册总线并立即枚举其设备。
DriverRegistry registry_
~DeviceManager()=default
DeviceManager(const DeviceManager &)=delete
auto GetRegistry() -> DriverRegistry &
访问驱动注册表。
驱动注册表 — 以 ETL vector 存储 DriverEntry,并附带 flat_map 兼容索引。
RAII 风格的锁守卫模板类
Definition spinlock.hpp:131
自旋锁
Definition spinlock.hpp:27
etl::singleton< DeviceManager > DeviceManagerSingleton
DeviceType
设备分类
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
auto Err(etl::format_string< Args... > fmt, Args &&... args) -> void
以 ERROR 级别记录日志
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
单个设备的硬件资源描述。
uint32_t dev_id
由 DeviceManager 分配的全局设备 ID
char name[32]
可读的设备名称(来自 FDT 节点名)
错误类型,用于 std::expected
Definition expected.hpp:343