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