1 use super::device::{ 2 bus::{Bus, BusDriver, BusState, BUS_MANAGER}, 3 driver::Driver, 4 Device, DeviceError, DeviceState, DeviceType, IdTable, 5 }; 6 use crate::libs::{mutex::Mutex, rwlock::RwLock}; 7 use alloc::{ 8 collections::{BTreeMap, BTreeSet}, 9 sync::Arc, 10 vec::Vec, 11 }; 12 use core::fmt::Debug; 13 use lazy_static::lazy_static; 14 use platform_device::PlatformDevice; 15 use platform_driver::PlatformDriver; 16 17 pub mod platform_device; 18 pub mod platform_driver; 19 20 /// @brief: platform总线匹配表 21 /// 总线上的设备和驱动都存在一份匹配表 22 /// 根据匹配表条目是否匹配来辨识设备和驱动能否进行匹配 23 #[derive(Debug)] 24 pub struct CompatibleTable(BTreeSet<&'static str>); 25 26 /// @brief: 匹配表操作方法集 27 impl CompatibleTable { 28 /// @brief: 创建一个新的匹配表 29 /// @parameter id_vec: 匹配条目数组 30 /// @return: 匹配表 31 #[inline] 32 #[allow(dead_code)] 33 pub fn new(id_vec: Vec<&'static str>) -> CompatibleTable { 34 CompatibleTable(BTreeSet::from_iter(id_vec.iter().cloned())) 35 } 36 37 /// @brief: 判断两个匹配表是否能够匹配 38 /// @parameter other: 其他匹配表 39 /// @return: 如果匹配成功,返回true,否则,返回false 40 #[allow(dead_code)] 41 pub fn matches(&self, other: &CompatibleTable) -> bool { 42 for id in &self.0 { 43 if other.0.contains(id) { 44 return true; 45 } 46 } 47 return false; 48 } 49 } 50 51 /// @brief: platform总线驱动 52 #[derive(Debug)] 53 pub struct PlatformBusDriver { 54 drivers: RwLock<BTreeMap<IdTable, Arc<dyn PlatformDriver>>>, // 总线上所有驱动 55 devices: RwLock<BTreeMap<IdTable, Arc<dyn PlatformDevice>>>, // 总线上所有设备 56 } 57 58 impl PlatformBusDriver { 59 /// @brief: 创建一个platform总线驱动,该驱动用于匹配plaform总线 60 /// @parameter: None 61 /// @return: platfor总线驱动 62 #[inline] 63 #[allow(dead_code)] 64 pub fn new() -> Self { 65 Self { 66 drivers: RwLock::new(BTreeMap::new()), 67 devices: RwLock::new(BTreeMap::new()), 68 } 69 } 70 71 /// @brief: 获取该驱动的匹配表 72 /// @parameter: None 73 /// @return: 驱动的匹配表 74 #[inline] 75 #[allow(dead_code)] 76 fn get_compatible_table(&self) -> CompatibleTable { 77 CompatibleTable::new(vec!["platform"]) 78 } 79 80 /// @brief: 根据设备标识符获取platform总线上的设备 81 /// @parameter id_table: 设备标识符 82 /// @return: 总线上的设备 83 #[inline] 84 #[allow(dead_code)] 85 fn get_device(&self, id_table: &IdTable) -> Option<Arc<dyn PlatformDevice>> { 86 let device_map = self.devices.read(); 87 return device_map.get(id_table).cloned(); 88 } 89 90 /// @brief: 根据设备驱动标识符获取platform总线上的驱动 91 /// @parameter id_table: 设备驱动标识符 92 /// @return: 总线上的驱动 93 #[inline] 94 #[allow(dead_code)] 95 fn get_driver(&self, id_table: &IdTable) -> Option<Arc<dyn PlatformDriver>> { 96 let driver_map = self.drivers.read(); 97 return driver_map.get(id_table).cloned(); 98 } 99 100 /// @brief: 注册platform类型驱动 101 /// @parameter driver: platform类型驱动,该驱动需要实现PlatformDriver trait 102 /// @return: 注册成功,返回Ok(()),,注册失败,返回BusError类型 103 #[allow(dead_code)] 104 fn register_platform_driver( 105 &mut self, 106 driver: Arc<dyn PlatformDriver>, 107 ) -> Result<(), DeviceError> { 108 let id_table = driver.get_id_table(); 109 110 let mut drivers = self.drivers.write(); 111 // 如果存在同类型的驱动,返回错误 112 if drivers.contains_key(&id_table) { 113 return Err(DeviceError::DriverExists); 114 } else { 115 drivers.insert(id_table.clone(), driver.clone()); 116 return Ok(()); 117 } 118 } 119 120 /// @brief: 卸载platform类型驱动 121 /// @parameter driver: platform类型驱动,该驱动需挂载在plaform总线之上 122 /// @return: None 123 #[allow(dead_code)] 124 #[inline] 125 fn unregister_platform_driver(&mut self, driver: Arc<dyn PlatformDriver>) { 126 let id_table = driver.get_id_table(); 127 self.drivers.write().remove(&id_table); 128 } 129 130 /// @brief: 注册platform类型设备 131 /// @parameter driver: platform类型设备,该驱动需要实现PlatformDevice trait 132 /// @return: 注册成功,返回Ok(()),,注册失败,返回BusError类型 133 #[allow(dead_code)] 134 fn register_platform_device( 135 &mut self, 136 device: Arc<dyn PlatformDevice>, 137 ) -> Result<(), DeviceError> { 138 let id_table = device.get_id_table(); 139 140 let mut devices = self.devices.write(); 141 if devices.contains_key(&id_table) { 142 return Err(DeviceError::DeviceExists); 143 } else { 144 devices.insert(id_table.clone(), device.clone()); 145 return Ok(()); 146 } 147 } 148 149 /// @brief: 卸载platform类型设备 150 /// @parameter device: platform类型设备,该驱设备需挂载在plaform总线之上 151 /// @return: None 152 #[inline] 153 #[allow(dead_code)] 154 fn unregister_platform_device(&mut self, device: Arc<dyn PlatformDevice>) { 155 let id_table = device.get_id_table(); 156 self.devices.write().remove(&id_table); 157 } 158 159 /// @brief: 匹配platform类型驱动 160 /// @parameter driver: platform类型驱动 161 /// @return: 如果匹配成功,返回成功驱动的设备数,否则,返回BusError类型 162 #[allow(dead_code)] 163 fn driver_match_device(&self, driver: Arc<dyn PlatformDriver>) -> Result<i32, DeviceError> { 164 let mut num = 0; 165 let devices = self.devices.read(); 166 167 for (_dev_id_table, device) in devices.iter() { 168 if device 169 .get_compatible_table() 170 .matches(&driver.get_compatible_table()) 171 { 172 if !device.is_initialized() { 173 // 设备未初始化,调用驱动probe函数 174 match driver.probe(device.clone()) { 175 Ok(()) => { 176 num = num + 1; 177 device.set_state(DeviceState::Initialized) 178 }, 179 // 可以驱动很多设备,一个设备初始化出错即返回 180 Err(_) => return Err(DeviceError::InitializeFailed), 181 } 182 } 183 } 184 } 185 if num == 0 { 186 return Err(DeviceError::NoDeviceForDriver); 187 } else { 188 return Ok(num); 189 } 190 } 191 192 /// @brief: 匹配platform上的设备 193 /// @parameter driver: platform类型设备 194 /// @return: 如果匹配成功,返回Ok(()),否则,返回BusError类型 195 #[allow(dead_code)] 196 fn device_match_driver(&self, device: Arc<dyn PlatformDevice>) -> Result<(), DeviceError> { 197 let drivers = self.drivers.read(); 198 for (_drv_id_table, driver) in &*drivers { 199 if driver 200 .get_compatible_table() 201 .matches(&device.get_compatible_table()) 202 { 203 match driver.probe(device.clone()) { 204 Ok(_driver) => { 205 // 将设备状态置为已初始化 206 device.set_state(DeviceState::Initialized); 207 return Ok(()); 208 } 209 Err(_) => return Err(DeviceError::InitializeFailed), 210 } 211 } 212 } 213 return Err(DeviceError::NoDriverForDevice); 214 } 215 } 216 217 /// @brief: 为PlatformBusDriver实现Driver trait 218 impl Driver for PlatformBusDriver { 219 fn get_id_table(&self) -> IdTable { 220 IdTable::new("PlatformBusDriver", 0) 221 } 222 } 223 224 /// @brief: 为PlatformBusDriver实现BusDriver trait 225 impl BusDriver for PlatformBusDriver { 226 fn is_empty(&self) -> bool { 227 if self.devices.read().is_empty() && self.drivers.read().is_empty() { 228 return true; 229 } else { 230 return false; 231 } 232 } 233 } 234 235 /// @brief: platform总线 236 #[derive(Debug, Clone)] 237 pub struct Platform { 238 state: Arc<Mutex<BusState>>, // 总线状态 239 driver: Option<Arc<PlatformBusDriver>>, // 总线驱动 240 } 241 242 /// @brief: platform方法集 243 impl Platform { 244 /// @brief: 创建一个platform总线实例 245 /// @parameter: None 246 /// @return: platform总线实例 247 pub fn new() -> Self { 248 Self { 249 state: Arc::new(Mutex::new(BusState::NotInitialized)), 250 driver: Option::None, 251 } 252 } 253 254 /// @brief: 获取总线的匹配表 255 /// @parameter: None 256 /// @return: platform总线匹配表 257 #[inline] 258 #[allow(dead_code)] 259 fn get_compatible_table(&self) -> CompatibleTable { 260 CompatibleTable::new(vec!["platform"]) 261 } 262 263 /// @brief: 判断总线是否初始化 264 /// @parameter: None 265 /// @return: 已初始化,返回true,否则,返回false 266 #[inline] 267 #[allow(dead_code)] 268 fn is_initialized(&self) -> bool { 269 let state = self.state.lock(); 270 match *state { 271 BusState::Initialized => true, 272 _ => false, 273 } 274 } 275 276 /// @brief: 设置总线状态 277 /// @parameter set_state: 总线状态BusState 278 /// @return: None 279 #[inline] 280 fn set_state(&self, set_state: BusState) { 281 let mut state = self.state.lock(); 282 *state = set_state; 283 } 284 285 /// @brief: 获取总线状态 286 /// @parameter: None 287 /// @return: 总线状态 288 #[inline] 289 #[allow(dead_code)] 290 fn get_state(&self) -> BusState { 291 let state = self.state.lock(); 292 return *state; 293 } 294 295 /// @brief: 296 /// @parameter: None 297 /// @return: 总线状态 298 #[inline] 299 #[allow(dead_code)] 300 fn set_driver(&mut self, driver: Option<Arc<PlatformBusDriver>>) { 301 self.driver = driver; 302 } 303 } 304 305 /// @brief: 为Platform实现Device trait,platform总线也是一种设备,属于总线设备类型 306 impl Device for Platform { 307 /// @brief: 获取platform设备类型 308 /// @parameter: None 309 /// @return: Bus类型 310 #[inline] 311 #[allow(dead_code)] 312 fn get_type(&self) -> DeviceType { 313 return DeviceType::Bus; 314 } 315 316 /// @brief: 获取platform设备标识符 317 /// @parameter: None 318 /// @return: platform总线设备标识符 319 #[inline] 320 #[allow(dead_code)] 321 fn get_id_table(&self) -> IdTable { 322 IdTable::new("platform", 0) 323 } 324 } 325 326 /// @brief: 为Platform实现Bus trait,platform总线是一种总线设备 327 impl Bus for Platform {} 328 329 lazy_static! { 330 pub static ref BUS_PLATFORM_DRIVER: Arc<PlatformBusDriver> = Arc::new(PlatformBusDriver::new()); 331 pub static ref BUS_PLATFORM_DEVICE: Arc<Platform> = Arc::new(Platform::new()); 332 } 333 334 /// @brief: 初始化platform总线 335 /// @parameter: None 336 /// @return: None 337 #[allow(dead_code)] 338 pub fn platform_bus_init() { 339 BUS_MANAGER.add_bus_driver( 340 BUS_PLATFORM_DRIVER.get_id_table(), 341 BUS_PLATFORM_DRIVER.clone(), 342 ); 343 BUS_MANAGER.add_bus( 344 BUS_PLATFORM_DEVICE.get_id_table(), 345 BUS_PLATFORM_DEVICE.clone(), 346 ); 347 BUS_PLATFORM_DEVICE.set_state(BusState::Initialized); 348 } 349 350 #[no_mangle] 351 extern "C" fn c_platform_bus_init() { 352 BUS_MANAGER.add_bus_driver( 353 BUS_PLATFORM_DRIVER.get_id_table(), 354 BUS_PLATFORM_DRIVER.clone(), 355 ); 356 BUS_MANAGER.add_bus( 357 BUS_PLATFORM_DEVICE.get_id_table(), 358 BUS_PLATFORM_DEVICE.clone(), 359 ); 360 BUS_PLATFORM_DEVICE.set_state(BusState::Initialized); 361 } 362