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