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