1*a03c4f9dSLoGin use alloc::sync::Arc; 22a7d773dSTingHuang 3*a03c4f9dSLoGin use crate::{ 4*a03c4f9dSLoGin driver::base::device::{ 5*a03c4f9dSLoGin bus::Bus, 6*a03c4f9dSLoGin driver::{driver_manager, Driver}, 7*a03c4f9dSLoGin }, 8*a03c4f9dSLoGin syscall::SystemError, 9*a03c4f9dSLoGin }; 10b087521eSChiichen 11*a03c4f9dSLoGin use super::{platform_bus, platform_device::PlatformDevice}; 12*a03c4f9dSLoGin 132a7d773dSTingHuang /// @brief: 实现该trait的设备驱动实例应挂载在platform总线上, 142a7d773dSTingHuang /// 同时应该实现Driver trait 15*a03c4f9dSLoGin /// 16*a03c4f9dSLoGin /// ## 注意 17*a03c4f9dSLoGin /// 18*a03c4f9dSLoGin /// 应当在所有实现这个trait的结构体上方,添加 `#[cast_to([sync] PlatformDriver)]`, 19*a03c4f9dSLoGin /// 否则运行时将报错“该对象不是PlatformDriver” 202a7d773dSTingHuang pub trait PlatformDriver: Driver { 21*a03c4f9dSLoGin /// 检测设备是否能绑定到这个驱动 22*a03c4f9dSLoGin /// 23*a03c4f9dSLoGin /// 如果能,则把设备的driver指向这个驱动。 24*a03c4f9dSLoGin /// 请注意,这个函数不应该把driver加入驱动的devices列表,相关工作会在外部的函数里面处理。 25*a03c4f9dSLoGin fn probe(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError>; 26*a03c4f9dSLoGin fn remove(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError>; 27*a03c4f9dSLoGin fn shutdown(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError>; 28*a03c4f9dSLoGin fn suspend(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError>; 29*a03c4f9dSLoGin fn resume(&self, device: &Arc<dyn PlatformDevice>) -> Result<(), SystemError>; 30b087521eSChiichen } 31*a03c4f9dSLoGin 32*a03c4f9dSLoGin #[inline(always)] 33*a03c4f9dSLoGin pub fn platform_driver_manager() -> &'static PlatformDriverManager { 34*a03c4f9dSLoGin &PlatformDriverManager 35*a03c4f9dSLoGin } 36*a03c4f9dSLoGin 37*a03c4f9dSLoGin #[derive(Debug)] 38*a03c4f9dSLoGin pub struct PlatformDriverManager; 39*a03c4f9dSLoGin 40*a03c4f9dSLoGin impl PlatformDriverManager { 41*a03c4f9dSLoGin /// 注册平台设备驱动 42*a03c4f9dSLoGin /// 43*a03c4f9dSLoGin /// 参考 https://opengrok.ringotek.cn/xref/linux-6.1.9/drivers/base/platform.c?fi=__platform_driver_register#861 44*a03c4f9dSLoGin pub fn register(&self, driver: Arc<dyn PlatformDriver>) -> Result<(), SystemError> { 45*a03c4f9dSLoGin driver.set_bus(Some(platform_bus() as Arc<dyn Bus>)); 46*a03c4f9dSLoGin return driver_manager().register(driver as Arc<dyn Driver>); 47*a03c4f9dSLoGin } 48*a03c4f9dSLoGin 49*a03c4f9dSLoGin /// 卸载平台设备驱动 50*a03c4f9dSLoGin #[allow(dead_code)] 51*a03c4f9dSLoGin pub fn unregister(&self, driver: &Arc<dyn PlatformDriver>) { 52*a03c4f9dSLoGin driver_manager().unregister(&(driver.clone() as Arc<dyn Driver>)); 53b087521eSChiichen } 542a7d773dSTingHuang } 55