设备管理器 — 管理所有设备节点和驱动程序。
More...
#include <device_manager.hpp>
设备管理器 — 管理所有设备节点和驱动程序。
- Copyright
- Copyright The SimpleKernel Contributors
- Precondition
- 调用任何方法前,内存子系统必须已完成初始化
- Postcondition
- ProbeAll() 执行完成后,已绑定的设备可供使用
Definition at line 23 of file device_manager.hpp.
◆ DeviceManager() [1/3]
| DeviceManager::DeviceManager |
( |
| ) |
|
|
default |
◆ ~DeviceManager()
| DeviceManager::~DeviceManager |
( |
| ) |
|
|
default |
◆ DeviceManager() [2/3]
◆ DeviceManager() [3/3]
◆ FindDevice()
根据名称查找设备。
- Note
- 只读路径 — 枚举完成后 device_count_ 和 devices_ 稳定, 并发调用者可安全使用。
- Parameters
-
- Returns
- Expected<DeviceNode*> 设备指针,或 kDeviceNotFound
Definition at line 49 of file device_manager.cpp.
49 {
50 assert(name != nullptr && "FindDevice: name must not be null");
55 }
57}
etl::flat_map< const char *, size_t, kMaxDevices, CStrLess > name_index_
DeviceNode devices_[kMaxDevices]
◆ FindDevicesByType()
按类型枚举设备。
- Parameters
-
| type | 设备类型 |
| out | 输出的设备节点指针数组 |
| max | 最大结果数量 |
- Returns
- size_t 找到的匹配设备数量
Definition at line 59 of file device_manager.cpp.
60 {
61 assert((out != nullptr || max == 0) &&
62 "FindDevicesByType: out must not be null when max > 0");
64 size_t found = 0;
68 }
69 }
70 return found;
71}
◆ GetRegistry()
◆ operator=() [1/2]
◆ operator=() [2/2]
◆ ProbeAll()
| auto DeviceManager::ProbeAll |
( |
| ) |
-> Expected<void> |
匹配已注册的驱动程序并探测所有未绑定的设备。
- Returns
- Expected<void> 成功返回 void,失败返回错误
- Copyright
- Copyright The SimpleKernel Contributors
Definition at line 11 of file device_manager.cpp.
11 {
13
14 size_t probed = 0;
15 size_t no_driver_count = 0;
18
20 if (drv == nullptr) {
21 ++no_driver_count;
22 continue;
23 }
24
25 if (!drv->match(node)) {
26 continue;
27 }
28
29 if (node.bound) {
30 continue;
31 }
32 node.bound = true;
33
34 drv->probe(node).or_else([&](auto&& err) {
35 klog::Err(
"DeviceManager: probe '{}' failed: {}", node.name,
36 err.message());
37 node.bound = false;
39 });
40
41 ++probed;
42 }
43
44 klog::Info(
"DeviceManager: probed {} device(s), {} skipped (no driver)",
45 probed, no_driver_count);
46 return {};
47}
auto FindDriver(const DeviceNode &node) -> const DriverEntry *
查找 match_table 中含有 node.compatible 字符串的第一个驱动 (flat_map 查找,O(Cn · log T))。
std::expected< T, Error > Expected
std::expected 别名模板
auto Err(etl::format_string< Args... > fmt, Args &&... args) -> void
以 ERROR 级别记录日志
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
◆ RegisterBus()
| auto DeviceManager::RegisterBus |
( |
B & |
bus | ) |
-> Expected<void> |
|
inline |
注册总线并立即枚举其设备。
- Template Parameters
-
- Parameters
-
- Returns
- Expected<void> 成功返回 void,失败返回错误
Definition at line 33 of file device_manager.hpp.
33 {
35
38 }
39
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) {
54 }
55 }
57
58 klog::Info(
"DeviceManager: bus '{}' enumerated {} device(s)", B::GetName(),
59 count);
60 return {};
61 }
static constexpr size_t kMaxDevices
uint32_t dev_id
由 DeviceManager 分配的全局设备 ID
char name[32]
可读的设备名称(来自 FDT 节点名)
◆ device_count_
| size_t DeviceManager::device_count_ {0} |
|
private |
◆ devices_
◆ kMaxDevices
| constexpr size_t DeviceManager::kMaxDevices = 64 |
|
staticconstexprprivate |
◆ lock_
| SpinLock DeviceManager::lock_ {"device_manager"} |
|
private |
◆ name_index_
◆ next_dev_id_
| uint32_t DeviceManager::next_dev_id_ {0} |
|
private |
◆ registry_
The documentation for this class was generated from the following files: