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