1 use self::{platform_device::PlatformBusDevice, subsys::PlatformBus};
2
3 use super::{
4 device::{
5 bus::{bus_register, Bus, BusState},
6 device_unregister, sys_devices_kset, DevicePrivateData, IdTable,
7 },
8 kobject::KObject,
9 };
10 use crate::driver::base::device::device_register;
11 use alloc::{collections::BTreeSet, string::ToString, sync::Arc, vec::Vec};
12 use core::fmt::Debug;
13 use system_error::SystemError;
14 use unified_init::{define_unified_initializer_slice, unified_init};
15
16 pub mod platform_device;
17 pub mod platform_driver;
18 pub mod subsys;
19
20 static mut PLATFORM_BUS_DEVICE: Option<Arc<PlatformBusDevice>> = None;
21 static mut PLATFORM_BUS: Option<Arc<PlatformBus>> = None;
22
23 define_unified_initializer_slice!(PLATFORM_DEVICE_INITIALIZER);
24
25 #[allow(dead_code)]
26 #[inline(always)]
platform_bus_device() -> Arc<PlatformBusDevice>27 pub fn platform_bus_device() -> Arc<PlatformBusDevice> {
28 unsafe { PLATFORM_BUS_DEVICE.clone().unwrap() }
29 }
30
31 #[allow(dead_code)]
32 #[inline(always)]
platform_bus() -> Arc<PlatformBus>33 pub fn platform_bus() -> Arc<PlatformBus> {
34 unsafe { PLATFORM_BUS.clone().unwrap() }
35 }
36
37 /// @brief: platform总线匹配表
38 /// 总线上的设备和驱动都存在一份匹配表
39 /// 根据匹配表条目是否匹配来辨识设备和驱动能否进行匹配
40 #[derive(Debug, Clone)]
41 pub struct CompatibleTable(BTreeSet<&'static str>);
42
43 /// @brief: 匹配表操作方法集
44 impl CompatibleTable {
45 /// @brief: 创建一个新的匹配表
46 /// @parameter id_vec: 匹配条目数组
47 /// @return: 匹配表
48 #[inline]
49 #[allow(dead_code)]
new(id_vec: Vec<&'static str>) -> CompatibleTable50 pub fn new(id_vec: Vec<&'static str>) -> CompatibleTable {
51 CompatibleTable(BTreeSet::from_iter(id_vec.iter().cloned()))
52 }
53
54 /// @brief: 判断两个匹配表是否能够匹配
55 /// @parameter other: 其他匹配表
56 /// @return: 如果匹配成功,返回true,否则,返回false
57 #[allow(dead_code)]
matches(&self, other: &CompatibleTable) -> bool58 pub fn matches(&self, other: &CompatibleTable) -> bool {
59 self.0.intersection(&other.0).next().is_some()
60 }
61
62 /// @brief: 添加一组匹配条目
63 /// @param:
64 #[allow(dead_code)]
add_device(&mut self, devices: Vec<&'static str>)65 pub fn add_device(&mut self, devices: Vec<&'static str>) {
66 for str in devices {
67 self.0.insert(str);
68 }
69 }
70 }
71
72 /// @brief: 初始化platform总线
73 /// @parameter: None
74 /// @return: None
75 ///
76 /// 参考: https://code.dragonos.org.cn/xref/linux-6.1.9/drivers/base/platform.c?fi=platform_bus_init#1511
platform_bus_init() -> Result<(), SystemError>77 pub fn platform_bus_init() -> Result<(), SystemError> {
78 let platform_device: Arc<PlatformBusDevice> = PlatformBusDevice::new(
79 DevicePrivateData::new(
80 IdTable::new("platform".to_string(), None),
81 BusState::NotInitialized.into(),
82 ),
83 Some(Arc::downgrade(&(sys_devices_kset() as Arc<dyn KObject>))),
84 );
85 unsafe { PLATFORM_BUS_DEVICE = Some(platform_device.clone()) };
86 // 注册到/sys/devices下
87 device_register(platform_device.clone())?;
88
89 let paltform_bus = PlatformBus::new();
90 // 注册到/sys/bus下
91 let r = bus_register(paltform_bus.clone() as Arc<dyn Bus>);
92 if r.is_err() {
93 device_unregister(platform_device.clone());
94 unsafe { PLATFORM_BUS_DEVICE = None };
95 return r;
96 }
97 unsafe { PLATFORM_BUS = Some(paltform_bus) };
98
99 unified_init!(PLATFORM_DEVICE_INITIALIZER);
100
101 return r;
102 }
103