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