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