1 use super::IdTable; 2 use crate::{ 3 driver::Driver, filesystem::vfs::IndexNode, libs::spinlock::SpinLock, syscall::SystemError, 4 }; 5 use alloc::{collections::BTreeMap, sync::Arc}; 6 use core::fmt::Debug; 7 8 lazy_static! { 9 pub static ref DRIVER_MANAGER: Arc<LockedDriverManager> = Arc::new(LockedDriverManager::new()); 10 } 11 12 /// @brief: Driver error 13 #[allow(dead_code)] 14 #[derive(Debug, PartialEq, Eq, Clone, Copy)] 15 pub enum DriverError { 16 ProbeError, // 探测设备失败(该驱动不能初始化这个设备) 17 RegisterError, // 设备注册失败 18 AllocateResourceError, // 获取设备所需资源失败 19 UnsupportedOperation, // 不支持的操作 20 UnInitialized, // 未初始化 21 } 22 23 impl Into<SystemError> for DriverError { 24 fn into(self) -> SystemError { 25 match self { 26 DriverError::ProbeError => SystemError::ENODEV, 27 DriverError::RegisterError => SystemError::ENODEV, 28 DriverError::AllocateResourceError => SystemError::EIO, 29 DriverError::UnsupportedOperation => SystemError::EIO, 30 DriverError::UnInitialized => SystemError::ENODEV, 31 } 32 } 33 } 34 35 /// @brief: 驱动管理器(锁) 36 #[derive(Debug)] 37 pub struct LockedDriverManager(SpinLock<DriverManager>); 38 39 impl LockedDriverManager { 40 /// @brief: 创建一个新的驱动管理器(锁) 41 /// @parameter None 42 /// @return: LockedDriverManager实体 43 #[inline] 44 fn new() -> LockedDriverManager { 45 LockedDriverManager(SpinLock::new(DriverManager::new())) 46 } 47 48 /// @brief: 添加驱动 49 /// @parameter id_table: 驱动标识符,用于唯一标识该驱动 50 /// @parameter drv: 驱动实例 51 /// @return: None 52 #[inline] 53 #[allow(dead_code)] 54 pub fn add_driver(&self, id_table: IdTable, drv: Arc<dyn Driver>) { 55 let mut driver_manager = self.0.lock(); 56 driver_manager.drivers.insert(id_table, drv); 57 } 58 59 /// @brief: 卸载驱动 60 /// @parameter id_table: 驱动标识符,用于唯一标识该驱动 61 /// @return: None 62 #[inline] 63 #[allow(dead_code)] 64 pub fn remove_driver(&self, id_table: &IdTable) { 65 let mut driver_manager = self.0.lock(); 66 driver_manager.drivers.remove(id_table); 67 } 68 69 /// @brief: 获取驱动 70 /// @parameter id_table: 驱动标识符,用于唯一标识该驱动 71 /// @return: 驱动实例 72 #[inline] 73 #[allow(dead_code)] 74 pub fn get_driver(&self, id_table: &IdTable) -> Option<Arc<dyn Driver>> { 75 let driver_manager = self.0.lock(); 76 driver_manager.drivers.get(id_table).cloned() 77 } 78 79 /// @brief: 获取驱动管理器的sys information 80 /// @parameter id_table: 设备标识符,用于唯一标识该驱动 81 /// @return: 驱动实例 82 #[inline] 83 #[allow(dead_code)] 84 fn get_sys_info(&self) -> Option<Arc<dyn IndexNode>> { 85 return self.0.lock().sys_info.clone(); 86 } 87 } 88 89 /// @brief: 驱动管理器 90 #[derive(Debug, Clone)] 91 pub struct DriverManager { 92 drivers: BTreeMap<IdTable, Arc<dyn Driver>>, // 所有驱动 93 sys_info: Option<Arc<dyn IndexNode>>, // sys information 94 } 95 96 impl DriverManager { 97 /// @brief: 创建一个新的设备管理器 98 /// @parameter: None 99 /// @return: Manager实体 100 #[inline] 101 fn new() -> DriverManager { 102 DriverManager { 103 drivers: BTreeMap::new(), 104 sys_info: None, 105 } 106 } 107 } 108 109 /// @brief: 驱动注册 110 /// @parameter: name: 驱动名 111 /// @return: 操作成功,返回(),操作失败,返回错误码 112 pub fn driver_register(driver: Arc<dyn Driver>) -> Result<(), DriverError> { 113 DRIVER_MANAGER.add_driver(driver.id_table(), driver); 114 return Ok(()); 115 } 116 117 /// @brief: 驱动卸载 118 /// @parameter: name: 驱动名 119 /// @return: 操作成功,返回(),操作失败,返回错误码 120 #[allow(dead_code)] 121 pub fn driver_unregister(driver: Arc<dyn Driver>) -> Result<(), DriverError> { 122 DRIVER_MANAGER.remove_driver(&driver.id_table()); 123 return Ok(()); 124 } 125