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