1 use alloc::{ 2 string::{String, ToString}, 3 sync::{Arc, Weak}, 4 vec::Vec, 5 }; 6 7 use crate::{ 8 driver::{ 9 base::{ 10 device::{ 11 bus::Bus, 12 driver::{Driver, DriverCommonData}, 13 Device, IdTable, 14 }, 15 kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState}, 16 kset::KSet, 17 }, 18 pci::{dev_id::PciDeviceID, device::PciDevice, driver::PciDriver}, 19 }, 20 filesystem::kernfs::KernFSInode, 21 libs::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}, 22 }; 23 #[derive(Debug)] 24 #[cast_to([sync] PciDriver)] 25 pub struct TestDriver { 26 driver_data: RwLock<DriverCommonData>, 27 kobj_data: RwLock<KObjectCommonData>, 28 kobj_state: LockedKObjectState, 29 pub locked_dynid_list: RwLock<Vec<Arc<PciDeviceID>>>, 30 } 31 32 /// # 结构功能 33 /// 本结构体是测试用的驱动,目前暂时保留,否则将出现大量dead code 34 /// 在编写了实际的pci驱动后,可将该驱动删除 35 impl TestDriver { new() -> Self36 pub fn new() -> Self { 37 Self { 38 driver_data: RwLock::new(DriverCommonData::default()), 39 kobj_data: RwLock::new(KObjectCommonData::default()), 40 kobj_state: LockedKObjectState::new(None), 41 locked_dynid_list: RwLock::new(vec![]), 42 } 43 } 44 } 45 46 impl PciDriver for TestDriver { add_dynid(&mut self, id: PciDeviceID) -> Result<(), system_error::SystemError>47 fn add_dynid(&mut self, id: PciDeviceID) -> Result<(), system_error::SystemError> { 48 let id = Arc::new(id); 49 self.locked_dynid_list.write().push(id); 50 Ok(()) 51 } 52 locked_dynid_list(&self) -> Option<Vec<Arc<PciDeviceID>>>53 fn locked_dynid_list(&self) -> Option<Vec<Arc<PciDeviceID>>> { 54 Some(self.locked_dynid_list.read().clone()) 55 } 56 probe( &self, _device: &Arc<dyn PciDevice>, _id: &PciDeviceID, ) -> Result<(), system_error::SystemError>57 fn probe( 58 &self, 59 _device: &Arc<dyn PciDevice>, 60 _id: &PciDeviceID, 61 ) -> Result<(), system_error::SystemError> { 62 Ok(()) 63 } 64 remove(&self, _device: &Arc<dyn PciDevice>) -> Result<(), system_error::SystemError>65 fn remove(&self, _device: &Arc<dyn PciDevice>) -> Result<(), system_error::SystemError> { 66 Ok(()) 67 } 68 resume(&self, _device: &Arc<dyn PciDevice>) -> Result<(), system_error::SystemError>69 fn resume(&self, _device: &Arc<dyn PciDevice>) -> Result<(), system_error::SystemError> { 70 Ok(()) 71 } 72 shutdown(&self, _device: &Arc<dyn PciDevice>) -> Result<(), system_error::SystemError>73 fn shutdown(&self, _device: &Arc<dyn PciDevice>) -> Result<(), system_error::SystemError> { 74 Ok(()) 75 } 76 suspend(&self, _device: &Arc<dyn PciDevice>) -> Result<(), system_error::SystemError>77 fn suspend(&self, _device: &Arc<dyn PciDevice>) -> Result<(), system_error::SystemError> { 78 Ok(()) 79 } 80 } 81 82 impl Driver for TestDriver { id_table(&self) -> Option<IdTable>83 fn id_table(&self) -> Option<IdTable> { 84 Some(IdTable::new("PciTestDriver".to_string(), None)) 85 } 86 devices(&self) -> Vec<Arc<dyn Device>>87 fn devices(&self) -> Vec<Arc<dyn Device>> { 88 self.driver_data.read().devices.clone() 89 } 90 add_device(&self, device: Arc<dyn Device>)91 fn add_device(&self, device: Arc<dyn Device>) { 92 let mut guard = self.driver_data.write(); 93 // check if the device is already in the list 94 if guard.devices.iter().any(|dev| Arc::ptr_eq(dev, &device)) { 95 return; 96 } 97 98 guard.devices.push(device); 99 } 100 delete_device(&self, device: &Arc<dyn Device>)101 fn delete_device(&self, device: &Arc<dyn Device>) { 102 let mut guard = self.driver_data.write(); 103 guard.devices.retain(|dev| !Arc::ptr_eq(dev, device)); 104 } 105 set_bus(&self, bus: Option<Weak<dyn Bus>>)106 fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 107 self.driver_data.write().bus = bus; 108 } 109 bus(&self) -> Option<Weak<dyn Bus>>110 fn bus(&self) -> Option<Weak<dyn Bus>> { 111 self.driver_data.read().bus.clone() 112 } 113 } 114 115 impl KObject for TestDriver { as_any_ref(&self) -> &dyn core::any::Any116 fn as_any_ref(&self) -> &dyn core::any::Any { 117 self 118 } 119 set_inode(&self, inode: Option<Arc<KernFSInode>>)120 fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 121 self.kobj_data.write().kern_inode = inode; 122 } 123 inode(&self) -> Option<Arc<KernFSInode>>124 fn inode(&self) -> Option<Arc<KernFSInode>> { 125 self.kobj_data.read().kern_inode.clone() 126 } 127 parent(&self) -> Option<Weak<dyn KObject>>128 fn parent(&self) -> Option<Weak<dyn KObject>> { 129 self.kobj_data.read().parent.clone() 130 } 131 set_parent(&self, parent: Option<Weak<dyn KObject>>)132 fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 133 self.kobj_data.write().parent = parent; 134 } 135 kset(&self) -> Option<Arc<KSet>>136 fn kset(&self) -> Option<Arc<KSet>> { 137 self.kobj_data.read().kset.clone() 138 } 139 set_kset(&self, kset: Option<Arc<KSet>>)140 fn set_kset(&self, kset: Option<Arc<KSet>>) { 141 self.kobj_data.write().kset = kset; 142 } 143 kobj_type(&self) -> Option<&'static dyn KObjType>144 fn kobj_type(&self) -> Option<&'static dyn KObjType> { 145 self.kobj_data.read().kobj_type 146 } 147 set_kobj_type(&self, ktype: Option<&'static dyn KObjType>)148 fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 149 self.kobj_data.write().kobj_type = ktype; 150 } 151 name(&self) -> String152 fn name(&self) -> String { 153 "PciTestDriver".to_string() 154 } 155 set_name(&self, _name: String)156 fn set_name(&self, _name: String) { 157 // do nothing 158 } 159 kobj_state(&self) -> RwLockReadGuard<KObjectState>160 fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 161 self.kobj_state.read() 162 } 163 kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState>164 fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 165 self.kobj_state.write() 166 } 167 set_kobj_state(&self, state: KObjectState)168 fn set_kobj_state(&self, state: KObjectState) { 169 *self.kobj_state.write() = state; 170 } 171 } 172