1 use super::{
2 device_register, device_unregister,
3 driver::{driver_register, driver_unregister, Driver, DriverError},
4 Device, DeviceError, DeviceState, IdTable,
5 };
6 use crate::{
7 filesystem::{
8 sysfs::{
9 bus::{sys_bus_init, sys_bus_register},
10 SYS_BUS_INODE,
11 },
12 vfs::IndexNode,
13 },
14 libs::spinlock::SpinLock,
15 };
16 use alloc::{collections::BTreeMap, sync::Arc};
17 use core::fmt::Debug;
18 use lazy_static::lazy_static;
19
20 lazy_static! {
21 pub static ref BUS_MANAGER: Arc<LockedBusManager> = Arc::new(LockedBusManager::new());
22 }
23
24 /// @brief: 总线状态
25 #[derive(Debug, Copy, Clone)]
26 pub enum BusState {
27 NotInitialized = 0, // 未初始化
28 Initialized = 1, // 已初始化
29 UnDefined = 2, // 未定义的
30 }
31
32 /// @brief: 将u32类型转换为总线状态类型
33 impl From<u32> for BusState {
from(state: u32) -> Self34 fn from(state: u32) -> Self {
35 match state {
36 0 => BusState::NotInitialized,
37 1 => BusState::Initialized,
38 _ => BusState::UnDefined,
39 }
40 }
41 }
42
43 /// @brief: 将总线状态类型转换为u32类型
44 impl From<DeviceState> for BusState {
from(state: DeviceState) -> Self45 fn from(state: DeviceState) -> Self {
46 match state {
47 DeviceState::Initialized => BusState::Initialized,
48 DeviceState::NotInitialized => BusState::NotInitialized,
49 DeviceState::UnDefined => BusState::UnDefined,
50 }
51 }
52 }
53
54 /// @brief: 将总线状态类型转换为设备状态类型
55 impl From<BusState> for DeviceState {
from(state: BusState) -> Self56 fn from(state: BusState) -> Self {
57 match state {
58 BusState::Initialized => DeviceState::Initialized,
59 BusState::NotInitialized => DeviceState::NotInitialized,
60 BusState::UnDefined => DeviceState::UnDefined,
61 }
62 }
63 }
64
65 /// @brief: 总线驱动trait,所有总线驱动都应实现该trait
66 pub trait BusDriver: Driver {
67 /// @brief: 判断总线是否为空
68 /// @parameter: None
69 /// @return: 如果总线上设备和驱动的数量都为0,则返回true,否则,返回false
is_empty(&self) -> bool70 fn is_empty(&self) -> bool;
71 }
72
73 /// @brief: 总线设备trait,所有总线都应实现该trait
74 pub trait Bus: Device {}
75
76 /// @brief: 总线管理结构体
77 #[derive(Debug, Clone)]
78 pub struct BusManager {
79 buses: BTreeMap<IdTable, Arc<dyn Bus>>, // 总线设备表
80 bus_drvs: BTreeMap<IdTable, Arc<dyn BusDriver>>, // 总线驱动表
81 sys_info: Option<Arc<dyn IndexNode>>, // 总线inode
82 }
83
84 /// @brief: bus管理(锁)
85 pub struct LockedBusManager(SpinLock<BusManager>);
86
87 /// @brief: 总线管理方法集
88 impl LockedBusManager {
89 /// @brief: 创建总线管理实例
90 /// @parameter: None
91 /// @return: 总线管理实例
92 #[inline]
93 #[allow(dead_code)]
new() -> Self94 pub fn new() -> Self {
95 LockedBusManager(SpinLock::new(BusManager {
96 buses: BTreeMap::new(),
97 bus_drvs: BTreeMap::new(),
98 sys_info: Some(SYS_BUS_INODE()),
99 }))
100 }
101
102 /// @brief: 添加总线
103 /// @parameter id_table: 总线标识符,用于唯一标识该总线
104 /// @parameter bus_dev: 总线实例
105 /// @return: None
106 #[inline]
107 #[allow(dead_code)]
add_bus(&self, id_table: IdTable, bus_dev: Arc<dyn Bus>)108 pub fn add_bus(&self, id_table: IdTable, bus_dev: Arc<dyn Bus>) {
109 let mut bus_manager = self.0.lock();
110 bus_manager.buses.insert(id_table, bus_dev);
111 }
112
113 /// @brief: 添加总线驱动
114 /// @parameter id_table: 总线驱动标识符,用于唯一标识该总线驱动
115 /// @parameter bus_dev: 总线驱动实例
116 /// @return: None
117 #[inline]
118 #[allow(dead_code)]
add_driver(&self, id_table: IdTable, bus_drv: Arc<dyn BusDriver>)119 pub fn add_driver(&self, id_table: IdTable, bus_drv: Arc<dyn BusDriver>) {
120 let mut bus_manager = self.0.lock();
121 bus_manager.bus_drvs.insert(id_table, bus_drv);
122 }
123
124 /// @brief: 卸载总线
125 /// @parameter id_table: 总线标识符,用于唯一标识该总线
126 /// @return: None
127 #[inline]
128 #[allow(dead_code)]
remove_bus(&self, id_table: &IdTable)129 pub fn remove_bus(&self, id_table: &IdTable) {
130 let mut bus_manager = self.0.lock();
131 bus_manager.buses.remove(id_table);
132 }
133
134 /// @brief: 卸载总线驱动
135 /// @parameter id_table: 总线驱动标识符,用于唯一标识该总线驱动
136 /// @return: None
137 #[inline]
138 #[allow(dead_code)]
remove_bus_driver(&self, id_table: &IdTable)139 pub fn remove_bus_driver(&self, id_table: &IdTable) {
140 let mut bus_manager = self.0.lock();
141 bus_manager.bus_drvs.remove(id_table);
142 }
143
144 /// @brief: 获取总线设备
145 /// @parameter id_table: 总线标识符,用于唯一标识该总线
146 /// @return: 总线设备实例
147 #[inline]
148 #[allow(dead_code)]
get_bus(&self, id_table: &IdTable) -> Option<Arc<dyn Bus>>149 pub fn get_bus(&self, id_table: &IdTable) -> Option<Arc<dyn Bus>> {
150 let bus_manager = self.0.lock();
151 bus_manager.buses.get(id_table).cloned()
152 }
153
154 /// @brief: 获取总线驱动
155 /// @parameter id_table: 总线驱动标识符,用于唯一标识该总线驱动
156 /// @return: 总线驱动实例
157 #[inline]
158 #[allow(dead_code)]
get_driver(&self, id_table: &IdTable) -> Option<Arc<dyn BusDriver>>159 pub fn get_driver(&self, id_table: &IdTable) -> Option<Arc<dyn BusDriver>> {
160 let bus_manager = self.0.lock();
161 return bus_manager.bus_drvs.get(id_table).cloned();
162 }
163
164 /// @brief: 获取总线管理器的sys information
165 /// @parameter None
166 /// @return: sys inode
167 #[inline]
168 #[allow(dead_code)]
sys_info(&self) -> Option<Arc<dyn IndexNode>>169 fn sys_info(&self) -> Option<Arc<dyn IndexNode>> {
170 return self.0.lock().sys_info.clone();
171 }
172 }
173
174 /// @brief: 总线注册,将总线加入全局总线管理器中,并根据id table在sys/bus和sys/devices下生成文件夹
175 /// @parameter bus: Bus设备实体
176 /// @return: 成功:() 失败:DeviceError
bus_register<T: Bus>(bus: Arc<T>) -> Result<(), DeviceError>177 pub fn bus_register<T: Bus>(bus: Arc<T>) -> Result<(), DeviceError> {
178 BUS_MANAGER.add_bus(bus.id_table(), bus.clone());
179 match sys_bus_register(&bus.id_table().to_name()) {
180 Ok(inode) => {
181 let _ = sys_bus_init(&inode);
182 return device_register(bus);
183 }
184 Err(_) => Err(DeviceError::RegisterError),
185 }
186 }
187
188 /// @brief: 总线注销,将总线从全局总线管理器中删除,并在sys/bus和sys/devices下删除文件夹
189 /// @parameter bus: Bus设备实体
190 /// @return: 成功:() 失败:DeviceError
191 #[allow(dead_code)]
bus_unregister<T: Bus>(bus: Arc<T>) -> Result<(), DeviceError>192 pub fn bus_unregister<T: Bus>(bus: Arc<T>) -> Result<(), DeviceError> {
193 BUS_MANAGER.add_bus(bus.id_table(), bus.clone());
194 return device_unregister(bus);
195 }
196
197 /// @brief: 总线驱动注册,将总线驱动加入全局总线管理器中
198 /// @parameter bus: Bus设备驱动实体
199 /// @return: 成功:() 失败:DeviceError
bus_driver_register<T: BusDriver>(bus_driver: Arc<T>) -> Result<(), DriverError>200 pub fn bus_driver_register<T: BusDriver>(bus_driver: Arc<T>) -> Result<(), DriverError> {
201 BUS_MANAGER.add_driver(bus_driver.id_table(), bus_driver.clone());
202 return driver_register(bus_driver);
203 }
204
205 /// @brief: 总线驱动注销,将总线从全局总线管理器中删除
206 /// @parameter bus: Bus设备驱动实体
207 /// @return: 成功:() 失败:DeviceError
208 #[allow(dead_code)]
bus_driver_unregister<T: BusDriver>(bus_driver: Arc<T>) -> Result<(), DriverError>209 pub fn bus_driver_unregister<T: BusDriver>(bus_driver: Arc<T>) -> Result<(), DriverError> {
210 BUS_MANAGER.add_driver(bus_driver.id_table(), bus_driver.clone());
211 return driver_unregister(bus_driver);
212 }
213