1 use core::{ 2 fmt::Debug, 3 sync::atomic::{AtomicBool, Ordering}, 4 }; 5 6 use alloc::{ 7 string::String, 8 sync::{Arc, Weak}, 9 vec::Vec, 10 }; 11 12 use crate::{ 13 libs::{ 14 notifier::AtomicNotifierChain, 15 rwlock::{RwLock, RwLockReadGuard}, 16 spinlock::SpinLock, 17 }, 18 syscall::SystemError, 19 }; 20 21 use super::{ 22 device::{ 23 bus::{Bus, BusNotifyEvent}, 24 driver::Driver, 25 Device, 26 }, 27 kset::KSet, 28 }; 29 30 /// 一个用于存储bus/class的驱动核心部分的信息的结构体 31 #[derive(Debug)] 32 pub struct SubSysPrivate { 33 /// 用于定义这个子系统的kset 34 subsys: Arc<KSet>, 35 ksets: RwLock<SubSysKSets>, 36 /// 指向拥有当前结构体的`dyn bus`对象的弱引用 37 bus: SpinLock<Weak<dyn Bus>>, 38 drivers_autoprobe: AtomicBool, 39 /// 当前总线上的所有设备 40 devices: RwLock<Vec<Weak<dyn Device>>>, 41 /// 当前总线上的所有驱动 42 drivers: RwLock<Vec<Weak<dyn Driver>>>, 43 interfaces: &'static [&'static dyn SubSysInterface], 44 bus_notifier: AtomicNotifierChain<BusNotifyEvent, Arc<dyn Device>>, 45 } 46 47 #[derive(Debug)] 48 struct SubSysKSets { 49 /// 子系统的`devices`目录 50 devices_kset: Option<Arc<KSet>>, 51 /// 子系统的`drivers`目录 52 drivers_kset: Option<Arc<KSet>>, 53 } 54 55 impl SubSysKSets { 56 pub fn new() -> Self { 57 return Self { 58 devices_kset: None, 59 drivers_kset: None, 60 }; 61 } 62 } 63 64 impl SubSysPrivate { 65 pub fn new( 66 name: String, 67 bus: Weak<dyn Bus>, 68 interfaces: &'static [&'static dyn SubSysInterface], 69 ) -> Self { 70 let subsys = KSet::new(name); 71 return Self { 72 subsys, 73 ksets: RwLock::new(SubSysKSets::new()), 74 drivers_autoprobe: AtomicBool::new(false), 75 bus: SpinLock::new(bus), 76 devices: RwLock::new(Vec::new()), 77 drivers: RwLock::new(Vec::new()), 78 interfaces, 79 bus_notifier: AtomicNotifierChain::new(), 80 }; 81 } 82 83 pub fn subsys(&self) -> Arc<KSet> { 84 return self.subsys.clone(); 85 } 86 87 #[inline] 88 #[allow(dead_code)] 89 pub fn bus(&self) -> Weak<dyn Bus> { 90 return self.bus.lock().clone(); 91 } 92 93 pub fn set_bus(&self, bus: Weak<dyn Bus>) { 94 *self.bus.lock() = bus; 95 } 96 97 pub fn devices(&self) -> RwLockReadGuard<Vec<Weak<dyn Device>>> { 98 return self.devices.read(); 99 } 100 101 pub fn drivers(&self) -> RwLockReadGuard<Vec<Weak<dyn Driver>>> { 102 return self.drivers.read(); 103 } 104 105 pub fn drivers_autoprobe(&self) -> bool { 106 return self.drivers_autoprobe.load(Ordering::SeqCst); 107 } 108 109 pub fn set_drivers_autoprobe(&self, drivers_autoprobe: bool) { 110 self.drivers_autoprobe 111 .store(drivers_autoprobe, Ordering::SeqCst); 112 } 113 114 #[allow(dead_code)] 115 #[inline] 116 pub fn devices_kset(&self) -> Option<Arc<KSet>> { 117 return self.ksets.read().devices_kset.clone(); 118 } 119 120 #[allow(dead_code)] 121 #[inline] 122 pub fn set_devices_kset(&self, devices_kset: Arc<KSet>) { 123 self.ksets.write().devices_kset = Some(devices_kset); 124 } 125 126 #[allow(dead_code)] 127 #[inline] 128 pub fn drivers_kset(&self) -> Option<Arc<KSet>> { 129 return self.ksets.read().drivers_kset.clone(); 130 } 131 132 pub fn set_drivers_kset(&self, drivers_kset: Arc<KSet>) { 133 self.ksets.write().drivers_kset = Some(drivers_kset); 134 } 135 136 pub fn bus_notifier(&self) -> &AtomicNotifierChain<BusNotifyEvent, Arc<dyn Device>> { 137 return &self.bus_notifier; 138 } 139 140 pub fn interfaces(&self) -> &'static [&'static dyn SubSysInterface] { 141 return self.interfaces; 142 } 143 144 pub fn add_driver_to_vec(&self, driver: &Arc<dyn Driver>) -> Result<(), SystemError> { 145 let mut drivers = self.drivers.write(); 146 let driver_weak = Arc::downgrade(driver); 147 if drivers.iter().any(|d| d.ptr_eq(&driver_weak)) { 148 return Err(SystemError::EEXIST); 149 } 150 drivers.push(driver_weak); 151 return Ok(()); 152 } 153 154 pub fn remove_driver_from_vec(&self, driver: &Arc<dyn Driver>) { 155 let mut drivers = self.drivers.write(); 156 let driver_weak = Arc::downgrade(driver); 157 let index = drivers.iter().position(|d| d.ptr_eq(&driver_weak)); 158 if let Some(index) = index { 159 drivers.remove(index); 160 } 161 } 162 163 pub fn add_device_to_vec(&self, device: &Arc<dyn Device>) -> Result<(), SystemError> { 164 let mut devices = self.devices.write(); 165 let device_weak = Arc::downgrade(device); 166 if devices.iter().any(|d| d.ptr_eq(&device_weak)) { 167 return Err(SystemError::EEXIST); 168 } 169 devices.push(device_weak); 170 return Ok(()); 171 } 172 173 #[allow(dead_code)] 174 pub fn remove_device_from_vec(&self, device: &Arc<dyn Device>) { 175 let mut devices = self.devices.write(); 176 let device_weak = Arc::downgrade(device); 177 let index = devices.iter().position(|d| d.ptr_eq(&device_weak)); 178 if let Some(index) = index { 179 devices.remove(index); 180 } 181 } 182 } 183 184 /// 参考: https://opengrok.ringotek.cn/xref/linux-6.1.9/include/linux/device.h#63 185 pub trait SubSysInterface: Debug + Send + Sync { 186 fn name(&self) -> &str; 187 fn bus(&self) -> Option<Weak<dyn Bus>>; 188 fn set_bus(&self, bus: Option<Weak<dyn Bus>>); 189 fn add_device(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> { 190 return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 191 } 192 fn remove_device(&self, device: &Arc<dyn Device>); 193 } 194