1 use super::device::{ 2 bus::{bus_driver_register, bus_register, Bus, BusDriver, BusState}, 3 driver::DriverError, 4 Device, DeviceError, DeviceNumber, DevicePrivateData, DeviceResource, DeviceType, IdTable, 5 KObject, 6 }; 7 use crate::{ 8 driver::Driver, filesystem::vfs::IndexNode, libs::spinlock::SpinLock, syscall::SystemError, 9 }; 10 use alloc::{ 11 collections::{BTreeMap, BTreeSet}, 12 string::ToString, 13 sync::Arc, 14 vec::Vec, 15 }; 16 use core::fmt::Debug; 17 use platform_device::PlatformDevice; 18 use platform_driver::PlatformDriver; 19 20 pub mod platform_device; 21 pub mod platform_driver; 22 23 /// @brief: platform总线匹配表 24 /// 总线上的设备和驱动都存在一份匹配表 25 /// 根据匹配表条目是否匹配来辨识设备和驱动能否进行匹配 26 #[derive(Debug, Clone)] 27 pub struct CompatibleTable(BTreeSet<&'static str>); 28 29 /// @brief: 匹配表操作方法集 30 impl CompatibleTable { 31 /// @brief: 创建一个新的匹配表 32 /// @parameter id_vec: 匹配条目数组 33 /// @return: 匹配表 34 #[inline] 35 #[allow(dead_code)] 36 pub fn new(id_vec: Vec<&'static str>) -> CompatibleTable { 37 CompatibleTable(BTreeSet::from_iter(id_vec.iter().cloned())) 38 } 39 40 /// @brief: 判断两个匹配表是否能够匹配 41 /// @parameter other: 其他匹配表 42 /// @return: 如果匹配成功,返回true,否则,返回false 43 #[allow(dead_code)] 44 pub fn matches(&self, other: &CompatibleTable) -> bool { 45 self.0.intersection(&other.0).next().is_some() 46 } 47 48 /// @brief: 添加一组匹配条目 49 /// @param: 50 #[allow(dead_code)] 51 pub fn add_device(&mut self, devices: Vec<&'static str>) { 52 for str in devices { 53 self.0.insert(str); 54 } 55 } 56 } 57 58 #[derive(Debug)] 59 pub struct LockedPlatformBusDriver(SpinLock<PlatformBusDriver>); 60 61 impl LockedPlatformBusDriver { 62 /// @brief: 创建一个platform总线加锁驱动,该驱动用于匹配plaform总线 63 /// @parameter: None 64 /// @return: platfor总线驱动 65 #[inline] 66 #[allow(dead_code)] 67 pub fn new() -> LockedPlatformBusDriver { 68 LockedPlatformBusDriver(SpinLock::new(PlatformBusDriver::new())) 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.0.lock().devices; 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.0.lock().drivers; 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(&self, driver: Arc<dyn PlatformDriver>) -> Result<(), DeviceError> { 105 let id_table = driver.id_table(); 106 107 let drivers = &mut self.0.lock().drivers; 108 // 如果存在同类型的驱动,返回错误 109 if drivers.contains_key(&id_table) { 110 return Err(DeviceError::DriverExists); 111 } else { 112 drivers.insert(id_table.clone(), driver.clone()); 113 return Ok(()); 114 } 115 } 116 117 /// @brief: 卸载platform类型驱动 118 /// @parameter driver: platform类型驱动,该驱动需挂载在plaform总线之上 119 /// @return: None 120 #[allow(dead_code)] 121 #[inline] 122 fn unregister_platform_driver( 123 &mut self, 124 driver: Arc<dyn PlatformDriver>, 125 ) -> Result<(), DeviceError> { 126 let id_table = driver.id_table(); 127 self.0.lock().drivers.remove(&id_table); 128 return Ok(()); 129 } 130 131 /// @brief: 注册platform类型设备 132 /// @parameter driver: platform类型设备,该驱动需要实现PlatformDevice trait 133 /// @return: 注册成功,返回Ok(()),,注册失败,返回BusError类型 134 #[allow(dead_code)] 135 fn register_platform_device( 136 &mut self, 137 device: Arc<dyn PlatformDevice>, 138 ) -> Result<(), DeviceError> { 139 let id_table = device.id_table(); 140 141 let devices = &mut self.0.lock().devices; 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.id_table(); 157 self.0.lock().devices.remove(&id_table); 158 } 159 } 160 161 /// @brief: platform总线驱动 162 #[derive(Debug)] 163 pub struct PlatformBusDriver { 164 drivers: BTreeMap<IdTable, Arc<dyn PlatformDriver>>, // 总线上所有驱动 165 devices: BTreeMap<IdTable, Arc<dyn PlatformDevice>>, // 总线上所有设备 166 sys_info: Option<Arc<dyn IndexNode>>, 167 } 168 169 impl PlatformBusDriver { 170 /// @brief: 创建一个platform总线驱动,该驱动用于匹配plaform总线 171 /// @parameter: None 172 /// @return: platfor总线驱动 173 #[inline] 174 #[allow(dead_code)] 175 pub fn new() -> Self { 176 Self { 177 drivers: BTreeMap::new(), 178 devices: BTreeMap::new(), 179 sys_info: None, 180 } 181 } 182 } 183 184 /// @brief: 为PlatformBusDriver实现Driver trait 185 impl Driver for LockedPlatformBusDriver { 186 #[inline] 187 fn as_any_ref(&self) -> &dyn core::any::Any { 188 self 189 } 190 191 #[inline] 192 fn id_table(&self) -> IdTable { 193 return IdTable::new("PlatformBusDriver".to_string(), DeviceNumber::new(0)); 194 } 195 196 #[inline] 197 #[allow(dead_code)] 198 fn sys_info(&self) -> Option<Arc<dyn IndexNode>> { 199 return self.0.lock().sys_info.clone(); 200 } 201 202 #[inline] 203 #[allow(dead_code)] 204 fn set_sys_info(&self, sys_info: Option<Arc<dyn IndexNode>>) { 205 self.0.lock().sys_info = sys_info; 206 } 207 208 fn probe(&self, _data: &DevicePrivateData) -> Result<(), DriverError> { 209 todo!() 210 } 211 212 fn load( 213 &self, 214 _data: DevicePrivateData, 215 _resource: Option<DeviceResource>, 216 ) -> Result<Arc<dyn Device>, DriverError> { 217 todo!() 218 } 219 } 220 221 /// @brief: 为PlatformBusDriver实现BusDriver trait 222 impl BusDriver for LockedPlatformBusDriver { 223 fn is_empty(&self) -> bool { 224 if self.0.lock().devices.is_empty() && self.0.lock().drivers.is_empty() { 225 return true; 226 } else { 227 return false; 228 } 229 } 230 } 231 232 impl KObject for LockedPlatformBusDriver {} 233 234 #[derive(Debug)] 235 pub struct LockedPlatform(SpinLock<Platform>); 236 237 impl LockedPlatform { 238 /// @brief: 创建一个加锁的platform总线实例 239 /// @parameter: None 240 /// @return: platform总线实例 241 pub fn new(data: DevicePrivateData) -> LockedPlatform { 242 LockedPlatform(SpinLock::new(Platform::new(data))) 243 } 244 245 /// @brief: 获取总线的匹配表 246 /// @parameter: None 247 /// @return: platform总线匹配表 248 #[inline] 249 #[allow(dead_code)] 250 fn compatible_table(&self) -> CompatibleTable { 251 CompatibleTable::new(vec!["platform"]) 252 } 253 254 /// @brief: 判断总线是否初始化 255 /// @parameter: None 256 /// @return: 已初始化,返回true,否则,返回false 257 #[inline] 258 #[allow(dead_code)] 259 fn is_initialized(&self) -> bool { 260 let state = self.0.lock().state; 261 match state { 262 BusState::Initialized => true, 263 _ => false, 264 } 265 } 266 267 /// @brief: 设置总线状态 268 /// @parameter set_state: 总线状态BusState 269 /// @return: None 270 #[inline] 271 fn set_state(&self, set_state: BusState) { 272 let state = &mut self.0.lock().state; 273 *state = set_state; 274 } 275 276 /// @brief: 获取总线状态 277 /// @parameter: None 278 /// @return: 总线状态 279 #[inline] 280 #[allow(dead_code)] 281 fn get_state(&self) -> BusState { 282 let state = self.0.lock().state; 283 return state; 284 } 285 286 // /// @brief: 287 // /// @parameter: None 288 // /// @return: 总线状态 289 // #[inline] 290 // #[allow(dead_code)] 291 // fn set_driver(&self, driver: Option<Arc<LockedPlatformBusDriver>>) { 292 // self.0.lock().driver = driver; 293 // } 294 } 295 296 /// @brief: platform总线 297 #[derive(Debug, Clone)] 298 pub struct Platform { 299 _data: DevicePrivateData, 300 state: BusState, // 总线状态 301 sys_info: Option<Arc<dyn IndexNode>>, // 总线sys information 302 } 303 304 /// @brief: platform方法集 305 impl Platform { 306 /// @brief: 创建一个platform总线实例 307 /// @parameter: None 308 /// @return: platform总线实例 309 pub fn new(_data: DevicePrivateData) -> Self { 310 Self { 311 _data, 312 state: BusState::NotInitialized, 313 sys_info: Option::None, 314 } 315 } 316 } 317 318 /// @brief: 为Platform实现Device trait,platform总线也是一种设备,属于总线设备类型 319 impl Device for LockedPlatform { 320 #[inline] 321 #[allow(dead_code)] 322 fn dev_type(&self) -> DeviceType { 323 return DeviceType::Bus; 324 } 325 326 #[inline] 327 #[allow(dead_code)] 328 fn id_table(&self) -> IdTable { 329 IdTable::new("platform".to_string(), DeviceNumber::new(0)) 330 } 331 332 #[inline] 333 fn set_sys_info(&self, sys_info: Option<Arc<dyn IndexNode>>) { 334 self.0.lock().sys_info = sys_info; 335 } 336 337 #[inline] 338 #[allow(dead_code)] 339 fn sys_info(&self) -> Option<Arc<dyn IndexNode>> { 340 return self.0.lock().sys_info.clone(); 341 } 342 343 fn as_any_ref(&self) -> &dyn core::any::Any { 344 self 345 } 346 } 347 348 /// @brief: 为Platform实现Bus trait,platform总线是一种总线设备 349 impl Bus for LockedPlatform {} 350 351 impl KObject for LockedPlatform {} 352 353 /// @brief: 初始化platform总线 354 /// @parameter: None 355 /// @return: None 356 pub fn platform_bus_init() -> Result<(), SystemError> { 357 let platform_driver: Arc<LockedPlatformBusDriver> = Arc::new(LockedPlatformBusDriver::new()); 358 let platform_device: Arc<LockedPlatform> = 359 Arc::new(LockedPlatform::new(DevicePrivateData::new( 360 IdTable::new("platform".to_string(), DeviceNumber::new(0)), 361 None, 362 CompatibleTable::new(vec!["platform"]), 363 BusState::NotInitialized.into(), 364 ))); 365 bus_register(platform_device.clone()).map_err(|e| e.into())?; 366 platform_device.set_state(BusState::Initialized); 367 //platform_device.set_driver(Some(platform_driver.clone())); 368 bus_driver_register(platform_driver.clone()).map_err(|e| e.into())?; 369 370 return Ok(()); 371 } 372