1 use alloc::{ 2 string::{String, ToString}, 3 sync::{Arc, Weak}, 4 }; 5 use system_error::SystemError; 6 7 use crate::{ 8 driver::base::{ 9 device::{ 10 bus::Bus, device_manager, driver::Driver, Device, DeviceCommonData, DeviceType, IdTable, 11 }, 12 kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState}, 13 kset::KSet, 14 }, 15 filesystem::kernfs::KernFSInode, 16 libs::{ 17 rwlock::RwLockWriteGuard, 18 spinlock::{SpinLock, SpinLockGuard}, 19 }, 20 }; 21 22 use super::{ 23 dev_id::PciDeviceID, 24 subsys::{pci_bus, pci_bus_device}, 25 }; 26 27 /// # 结构功能 28 /// 该结构为Pci设备的管理器,使用该结构可以将pci设备添加到sysfs中 29 pub struct PciDeviceManager; 30 31 pub fn pci_device_manager() -> &'static PciDeviceManager { 32 &PciDeviceManager 33 } 34 35 impl PciDeviceManager { 36 /// #函数的功能 37 /// 将pci设备注册到sysfs中 38 /// 39 /// ## 参数: 40 /// - 'pci_dev':需要添加的pci设备 41 /// 42 /// ## 返回值: 43 /// - OK(()) :表示成功 44 /// - Err(e) :失败原因 45 pub fn device_add(&self, pci_dev: Arc<dyn PciDevice>) -> Result<(), SystemError> { 46 // pci设备一般放置在/sys/device/pci:xxxx下 47 if pci_dev.dev_parent().is_none() { 48 pci_dev.set_dev_parent(Some(Arc::downgrade(&(pci_bus_device() as Arc<dyn Device>)))); 49 } 50 // 设置设备的总线 51 pci_dev.set_bus(Some(Arc::downgrade(&(pci_bus() as Arc<dyn Bus>)))); 52 // 对设备进行默认的初始化 53 device_manager().device_default_initialize(&(pci_dev.clone() as Arc<dyn Device>)); 54 // 使用设备管理器注册设备,当设备被注册后,会根据它的总线字段,在对应的总线上扫描驱动,并尝试进行匹配 55 let r = device_manager().add_device(pci_dev.clone() as Arc<dyn Device>); 56 57 if r.is_ok() { 58 //todo:这里可能还要处理一些设置成功后设备状态的变化 59 return Ok(()); 60 } else { 61 //todo:这里可能有一些添加失败的处理 62 return r; 63 } 64 } 65 } 66 67 /// #trait功能 68 /// 要进入sysfs的Pci设备应当实现的trait 69 pub trait PciDevice: Device { 70 /// # 函数的功能 71 /// 返回本设备的PciDeviceID,该ID用于driver和device之间的匹配 72 /// 73 /// ## 返回值 74 /// - 'PciDeviceID' :本设备的PciDeviceID 75 fn dynid(&self) -> PciDeviceID; 76 77 /// # 函数的功能 78 /// 返回本设备的供应商(vendor)ID 79 /// 80 /// ## 返回值 81 /// - u16 :表示供应商ID 82 fn vendor(&self) -> u16; 83 fn device_id(&self) -> u16; 84 fn subsystem_vendor(&self) -> u16; 85 fn subsystem_device(&self) -> u16; 86 } 87 88 /// #结构功能 89 /// 由于Pci总线本身就属于一个设备,故该结构代表Pci总线(控制器)本身 90 /// 它对应/sys/device/pci 91 #[derive(Debug)] 92 #[cast_to([sync] Device)] 93 pub struct PciBusDevice { 94 inner: SpinLock<InnerPciBusDevice>, 95 kobj_state: LockedKObjectState, 96 name: String, 97 } 98 99 impl PciBusDevice { 100 pub fn new(parent: Option<Weak<dyn KObject>>) -> Arc<Self> { 101 let bus_device = Self { 102 inner: SpinLock::new(InnerPciBusDevice { 103 kobject_common: KObjectCommonData::default(), 104 device_common: DeviceCommonData::default(), 105 }), 106 kobj_state: LockedKObjectState::new(None), 107 name: "pci".to_string(), 108 }; 109 bus_device.set_parent(parent); 110 return Arc::new(bus_device); 111 } 112 113 fn inner(&self) -> SpinLockGuard<InnerPciBusDevice> { 114 self.inner.lock() 115 } 116 } 117 118 #[derive(Debug)] 119 struct InnerPciBusDevice { 120 kobject_common: KObjectCommonData, 121 device_common: DeviceCommonData, 122 } 123 124 impl KObject for PciBusDevice { 125 fn as_any_ref(&self) -> &dyn core::any::Any { 126 self 127 } 128 129 fn parent(&self) -> Option<alloc::sync::Weak<dyn KObject>> { 130 self.inner().kobject_common.parent.clone() 131 } 132 133 fn inode(&self) -> Option<Arc<KernFSInode>> { 134 self.inner().kobject_common.kern_inode.clone() 135 } 136 137 fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 138 self.inner().kobject_common.kern_inode = inode; 139 } 140 141 fn kobj_type(&self) -> Option<&'static dyn KObjType> { 142 self.inner().kobject_common.kobj_type 143 } 144 145 fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 146 self.inner().kobject_common.kobj_type = ktype 147 } 148 149 fn kset(&self) -> Option<Arc<KSet>> { 150 self.inner().kobject_common.kset.clone() 151 } 152 153 fn kobj_state( 154 &self, 155 ) -> crate::libs::rwlock::RwLockReadGuard<crate::driver::base::kobject::KObjectState> { 156 self.kobj_state.read() 157 } 158 159 fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 160 self.kobj_state.write() 161 } 162 163 fn set_kobj_state(&self, state: KObjectState) { 164 *self.kobj_state.write() = state; 165 } 166 167 fn name(&self) -> String { 168 self.name.clone() 169 } 170 171 fn set_name(&self, _name: String) { 172 //do nothing; it's not supposed to change this struct's name 173 } 174 175 fn set_kset(&self, kset: Option<Arc<KSet>>) { 176 self.inner().kobject_common.kset = kset; 177 } 178 179 fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 180 self.inner().kobject_common.parent = parent; 181 } 182 } 183 184 impl Device for PciBusDevice { 185 fn dev_type(&self) -> DeviceType { 186 return DeviceType::Bus; 187 } 188 189 fn id_table(&self) -> IdTable { 190 IdTable::new("pci".to_string(), None) 191 } 192 193 fn bus(&self) -> Option<Weak<dyn Bus>> { 194 self.inner().device_common.bus.clone() 195 } 196 197 fn set_bus(&self, bus: Option<alloc::sync::Weak<dyn Bus>>) { 198 self.inner().device_common.bus = bus 199 } 200 201 fn driver(&self) -> Option<Arc<dyn Driver>> { 202 self.inner().device_common.driver.clone()?.upgrade() 203 } 204 205 fn is_dead(&self) -> bool { 206 false 207 } 208 209 fn set_driver(&self, driver: Option<alloc::sync::Weak<dyn Driver>>) { 210 self.inner().device_common.driver = driver; 211 } 212 213 fn can_match(&self) -> bool { 214 todo!() 215 } 216 217 fn set_can_match(&self, _can_match: bool) { 218 todo!() 219 } 220 221 fn set_class(&self, _class: Option<alloc::sync::Weak<dyn crate::driver::base::class::Class>>) { 222 todo!() 223 } 224 225 fn state_synced(&self) -> bool { 226 todo!() 227 } 228 229 fn dev_parent(&self) -> Option<alloc::sync::Weak<dyn Device>> { 230 self.inner().device_common.get_parent_weak_or_clear() 231 } 232 233 fn set_dev_parent(&self, dev_parent: Option<alloc::sync::Weak<dyn Device>>) { 234 self.inner().device_common.parent = dev_parent; 235 } 236 } 237