xref: /DragonOS/kernel/src/driver/base/subsys.rs (revision 81294aa2e6b257f0de5e3c28c3f3c89798330836)
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<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: 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<Weak<dyn Device>>> {
110         return self.devices.read();
111     }
112 
113     pub fn drivers(&self) -> RwLockReadGuard<Vec<Weak<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         let driver_weak = Arc::downgrade(driver);
159         if drivers.iter().any(|d| d.ptr_eq(&driver_weak)) {
160             return Err(SystemError::EEXIST);
161         }
162         drivers.push(driver_weak);
163         return Ok(());
164     }
165 
166     pub fn remove_driver_from_vec(&self, driver: &Arc<dyn Driver>) {
167         let mut drivers = self.drivers.write();
168         let driver_weak = Arc::downgrade(driver);
169         let index = drivers.iter().position(|d| d.ptr_eq(&driver_weak));
170         if let Some(index) = index {
171             drivers.remove(index);
172         }
173     }
174 
175     pub fn add_device_to_vec(&self, device: &Arc<dyn Device>) -> Result<(), SystemError> {
176         let mut devices = self.devices.write();
177         let device_weak = Arc::downgrade(device);
178         if devices.iter().any(|d| d.ptr_eq(&device_weak)) {
179             return Err(SystemError::EEXIST);
180         }
181         devices.push(device_weak);
182         return Ok(());
183     }
184 
185     #[allow(dead_code)]
186     pub fn remove_device_from_vec(&self, device: &Arc<dyn Device>) {
187         let mut devices = self.devices.write();
188         let device_weak = Arc::downgrade(device);
189         let index = devices.iter().position(|d| d.ptr_eq(&device_weak));
190         if let Some(index) = index {
191             devices.remove(index);
192         }
193     }
194 }
195 
196 /// 参考: https://opengrok.ringotek.cn/xref/linux-6.1.9/include/linux/device.h#63
197 pub trait SubSysInterface: Debug + Send + Sync {
198     fn name(&self) -> &str;
199     fn bus(&self) -> Option<Weak<dyn Bus>>;
200     fn set_bus(&self, bus: Option<Weak<dyn Bus>>);
201     fn add_device(&self, _device: &Arc<dyn Device>) -> Result<(), SystemError> {
202         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
203     }
204     fn remove_device(&self, device: &Arc<dyn Device>);
205 }
206