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