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