xref: /DragonOS/kernel/src/driver/base/subsys.rs (revision 8d72b68da9339ec97e1b8929bcf2946f0fd83cd5)
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         return self.class.lock().clone();
103     }
104 
105     pub fn set_class(&self, class: Option<Weak<dyn Class>>) {
106         *self.class.lock() = class;
107     }
108 
109     pub fn devices(&self) -> RwLockReadGuard<Vec<Arc<dyn Device>>> {
110         return self.devices.read();
111     }
112 
113     pub fn drivers(&self) -> RwLockReadGuard<Vec<Arc<dyn Driver>>> {
114         return self.drivers.read();
115     }
116 
117     pub fn drivers_autoprobe(&self) -> bool {
118         return self.drivers_autoprobe.load(Ordering::SeqCst);
119     }
120 
121     pub fn set_drivers_autoprobe(&self, drivers_autoprobe: bool) {
122         self.drivers_autoprobe
123             .store(drivers_autoprobe, Ordering::SeqCst);
124     }
125 
126     #[allow(dead_code)]
127     #[inline]
128     pub fn devices_kset(&self) -> Option<Arc<KSet>> {
129         return self.ksets.read().devices_kset.clone();
130     }
131 
132     #[allow(dead_code)]
133     #[inline]
134     pub fn set_devices_kset(&self, devices_kset: Arc<KSet>) {
135         self.ksets.write().devices_kset = Some(devices_kset);
136     }
137 
138     #[allow(dead_code)]
139     #[inline]
140     pub fn drivers_kset(&self) -> Option<Arc<KSet>> {
141         return self.ksets.read().drivers_kset.clone();
142     }
143 
144     pub fn set_drivers_kset(&self, drivers_kset: Arc<KSet>) {
145         self.ksets.write().drivers_kset = Some(drivers_kset);
146     }
147 
148     pub fn bus_notifier(&self) -> &AtomicNotifierChain<BusNotifyEvent, Arc<dyn Device>> {
149         return &self.bus_notifier;
150     }
151 
152     pub fn interfaces(&self) -> &'static [&'static dyn SubSysInterface] {
153         return self.interfaces;
154     }
155 
156     pub fn add_driver_to_vec(&self, driver: &Arc<dyn Driver>) -> Result<(), SystemError> {
157         let mut drivers = self.drivers.write();
158         if drivers.iter().any(|d| Arc::ptr_eq(d, driver)) {
159             return Err(SystemError::EEXIST);
160         }
161         drivers.push(driver.clone());
162         return Ok(());
163     }
164 
165     pub fn remove_driver_from_vec(&self, driver: &Arc<dyn Driver>) {
166         let mut drivers = self.drivers.write();
167         let index = drivers.iter().position(|d| Arc::ptr_eq(d, driver));
168         if let Some(index) = index {
169             drivers.remove(index);
170         }
171     }
172 
173     pub fn add_device_to_vec(&self, device: &Arc<dyn Device>) -> Result<(), SystemError> {
174         let mut devices = self.devices.write();
175         if devices.iter().any(|d| Arc::ptr_eq(d, device)) {
176             return Err(SystemError::EEXIST);
177         }
178         devices.push(device.clone());
179         return Ok(());
180     }
181 
182     #[allow(dead_code)]
183     pub fn remove_device_from_vec(&self, device: &Arc<dyn Device>) {
184         let mut devices = self.devices.write();
185         let index = devices.iter().position(|d| Arc::ptr_eq(d, device));
186         if let Some(index) = index {
187             devices.remove(index);
188         }
189     }
190 }
191 
192 /// 参考: https://code.dragonos.org.cn/xref/linux-6.1.9/include/linux/device.h#63
193 pub trait SubSysInterface: Debug + Send + Sync {
194     fn name(&self) -> &str;
195     fn bus(&self) -> Option<Weak<dyn Bus>>;
196     fn set_bus(&self, bus: Option<Weak<dyn Bus>>);
197     fn add_device(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> {
198         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
199     }
200     fn remove_device(&self, device: &Arc<dyn Device>);
201 }
202