106d5e247SLoGin use self::{platform_device::PlatformBusDevice, subsys::PlatformBus}; 206d5e247SLoGin 306d5e247SLoGin use super::{ 406d5e247SLoGin device::{ 506d5e247SLoGin bus::{bus_register, Bus, BusState}, 606d5e247SLoGin device_unregister, sys_devices_kset, DeviceNumber, DevicePrivateData, IdTable, 706d5e247SLoGin }, 806d5e247SLoGin kobject::KObject, 92a7d773dSTingHuang }; 10*91e9d4abSLoGin use crate::driver::base::device::device_register; 1106d5e247SLoGin use alloc::{collections::BTreeSet, string::ToString, sync::Arc, vec::Vec}; 1278bf93f0SYJwu2023 use core::fmt::Debug; 13*91e9d4abSLoGin use system_error::SystemError; 14*91e9d4abSLoGin use unified_init::{define_unified_initializer_slice, unified_init}; 152a7d773dSTingHuang 162a7d773dSTingHuang pub mod platform_device; 172a7d773dSTingHuang pub mod platform_driver; 1806d5e247SLoGin pub mod subsys; 1906d5e247SLoGin 2006d5e247SLoGin static mut PLATFORM_BUS_DEVICE: Option<Arc<PlatformBusDevice>> = None; 2106d5e247SLoGin static mut PLATFORM_BUS: Option<Arc<PlatformBus>> = None; 2206d5e247SLoGin 23*91e9d4abSLoGin define_unified_initializer_slice!(PLATFORM_DEVICE_INITIALIZER); 24*91e9d4abSLoGin 2506d5e247SLoGin #[allow(dead_code)] 2606d5e247SLoGin #[inline(always)] 2706d5e247SLoGin pub fn platform_bus_device() -> Arc<PlatformBusDevice> { 2806d5e247SLoGin unsafe { PLATFORM_BUS_DEVICE.clone().unwrap() } 2906d5e247SLoGin } 3006d5e247SLoGin 3106d5e247SLoGin #[allow(dead_code)] 3206d5e247SLoGin #[inline(always)] 3306d5e247SLoGin pub fn platform_bus() -> Arc<PlatformBus> { 3406d5e247SLoGin unsafe { PLATFORM_BUS.clone().unwrap() } 3506d5e247SLoGin } 362a7d773dSTingHuang 372a7d773dSTingHuang /// @brief: platform总线匹配表 382a7d773dSTingHuang /// 总线上的设备和驱动都存在一份匹配表 392a7d773dSTingHuang /// 根据匹配表条目是否匹配来辨识设备和驱动能否进行匹配 40b087521eSChiichen #[derive(Debug, Clone)] 412a7d773dSTingHuang pub struct CompatibleTable(BTreeSet<&'static str>); 422a7d773dSTingHuang 432a7d773dSTingHuang /// @brief: 匹配表操作方法集 442a7d773dSTingHuang impl CompatibleTable { 452a7d773dSTingHuang /// @brief: 创建一个新的匹配表 462a7d773dSTingHuang /// @parameter id_vec: 匹配条目数组 472a7d773dSTingHuang /// @return: 匹配表 482a7d773dSTingHuang #[inline] 492a7d773dSTingHuang #[allow(dead_code)] 502a7d773dSTingHuang pub fn new(id_vec: Vec<&'static str>) -> CompatibleTable { 512a7d773dSTingHuang CompatibleTable(BTreeSet::from_iter(id_vec.iter().cloned())) 522a7d773dSTingHuang } 532a7d773dSTingHuang 542a7d773dSTingHuang /// @brief: 判断两个匹配表是否能够匹配 552a7d773dSTingHuang /// @parameter other: 其他匹配表 562a7d773dSTingHuang /// @return: 如果匹配成功,返回true,否则,返回false 572a7d773dSTingHuang #[allow(dead_code)] 582a7d773dSTingHuang pub fn matches(&self, other: &CompatibleTable) -> bool { 59b087521eSChiichen self.0.intersection(&other.0).next().is_some() 602a7d773dSTingHuang } 61b087521eSChiichen 62b087521eSChiichen /// @brief: 添加一组匹配条目 63b087521eSChiichen /// @param: 64b087521eSChiichen #[allow(dead_code)] 65b087521eSChiichen pub fn add_device(&mut self, devices: Vec<&'static str>) { 66b087521eSChiichen for str in devices { 67b087521eSChiichen self.0.insert(str); 682a7d773dSTingHuang } 692a7d773dSTingHuang } 702a7d773dSTingHuang } 712a7d773dSTingHuang 722a7d773dSTingHuang /// @brief: 初始化platform总线 732a7d773dSTingHuang /// @parameter: None 742a7d773dSTingHuang /// @return: None 7506d5e247SLoGin /// 7606d5e247SLoGin /// 参考: https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/base/platform.c?fi=platform_bus_init#1511 77e0de0fd6STingHuang pub fn platform_bus_init() -> Result<(), SystemError> { 7806d5e247SLoGin let platform_device: Arc<PlatformBusDevice> = PlatformBusDevice::new( 7906d5e247SLoGin DevicePrivateData::new( 8008a2ee40SLoGin IdTable::new("platform".to_string(), Some(DeviceNumber::new(0))), 81b087521eSChiichen BusState::NotInitialized.into(), 8206d5e247SLoGin ), 8306d5e247SLoGin Some(Arc::downgrade(&(sys_devices_kset() as Arc<dyn KObject>))), 8406d5e247SLoGin ); 8506d5e247SLoGin unsafe { PLATFORM_BUS_DEVICE = Some(platform_device.clone()) }; 8606d5e247SLoGin // 注册到/sys/devices下 8706d5e247SLoGin device_register(platform_device.clone())?; 882a7d773dSTingHuang 8906d5e247SLoGin let paltform_bus = PlatformBus::new(); 9006d5e247SLoGin // 注册到/sys/bus下 9106d5e247SLoGin let r = bus_register(paltform_bus.clone() as Arc<dyn Bus>); 9206d5e247SLoGin if r.is_err() { 9306d5e247SLoGin device_unregister(platform_device.clone()); 9406d5e247SLoGin unsafe { PLATFORM_BUS_DEVICE = None }; 9506d5e247SLoGin return r; 9606d5e247SLoGin } 9706d5e247SLoGin unsafe { PLATFORM_BUS = Some(paltform_bus) }; 9806d5e247SLoGin 99*91e9d4abSLoGin unified_init!(PLATFORM_DEVICE_INITIALIZER); 100*91e9d4abSLoGin 10106d5e247SLoGin return r; 1022a7d773dSTingHuang } 103