xref: /DragonOS/kernel/src/driver/pci/device.rs (revision 2b7818e80e00fcfe4d03533f587cc125ea5e4bec)
1 use alloc::{
2     string::{String, ToString},
3     sync::{Arc, Weak},
4 };
5 use system_error::SystemError;
6 
7 use crate::{
8     driver::base::{
9         device::{
10             bus::Bus, device_manager, driver::Driver, Device, DeviceCommonData, DeviceType, IdTable,
11         },
12         kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState},
13         kset::KSet,
14     },
15     filesystem::kernfs::KernFSInode,
16     libs::{rwlock::RwLockWriteGuard, spinlock::SpinLock},
17 };
18 
19 use super::{
20     dev_id::PciDeviceID,
21     subsys::{pci_bus, pci_bus_device},
22 };
23 
24 /// # 结构功能
25 /// 该结构为Pci设备的管理器,使用该结构可以将pci设备添加到sysfs中
26 pub struct PciDeviceManager;
27 
28 pub fn pci_device_manager() -> &'static PciDeviceManager {
29     &PciDeviceManager
30 }
31 
32 impl PciDeviceManager {
33     /// #函数的功能
34     /// 将pci设备注册到sysfs中
35     ///
36     /// ## 参数:
37     /// - 'pci_dev':需要添加的pci设备
38     ///
39     /// ## 返回值:
40     /// - OK(()) :表示成功
41     /// - Err(e) :失败原因
42     pub fn device_add(&self, pci_dev: Arc<dyn PciDevice>) -> Result<(), SystemError> {
43         // pci设备一般放置在/sys/device/pci:xxxx下
44         if pci_dev.parent().is_none() {
45             pci_dev.set_parent(Some(Arc::downgrade(
46                 &(pci_bus_device() as Arc<dyn KObject>),
47             )));
48         }
49         // 设置设备的总线
50         pci_dev.set_bus(Some(Arc::downgrade(&(pci_bus() as Arc<dyn Bus>))));
51         // 对设备进行默认的初始化
52         device_manager().device_default_initialize(&(pci_dev.clone() as Arc<dyn Device>));
53         // 使用设备管理器注册设备,当设备被注册后,会根据它的总线字段,在对应的总线上扫描驱动,并尝试进行匹配
54         let r = device_manager().add_device(pci_dev.clone() as Arc<dyn Device>);
55 
56         if r.is_ok() {
57             //todo:这里可能还要处理一些设置成功后设备状态的变化
58             return Ok(());
59         } else {
60             //todo:这里可能有一些添加失败的处理
61             return r;
62         }
63     }
64 }
65 
66 /// #trait功能
67 /// 要进入sysfs的Pci设备应当实现的trait
68 pub trait PciDevice: Device {
69     /// # 函数的功能
70     /// 返回本设备的PciDeviceID,该ID用于driver和device之间的匹配
71     ///
72     /// ## 返回值
73     /// - 'PciDeviceID' :本设备的PciDeviceID
74     fn dynid(&self) -> PciDeviceID;
75 
76     /// # 函数的功能
77     /// 返回本设备的供应商(vendor)ID
78     ///
79     /// ## 返回值
80     /// - u16 :表示供应商ID
81     fn vendor(&self) -> u16;
82     fn device_id(&self) -> u16;
83     fn subsystem_vendor(&self) -> u16;
84     fn subsystem_device(&self) -> u16;
85 }
86 
87 /// #结构功能
88 /// 由于Pci总线本身就属于一个设备,故该结构代表Pci总线(控制器)本身
89 /// 它对应/sys/device/pci
90 #[derive(Debug)]
91 #[cast_to([sync] Device)]
92 pub struct PciBusDevice {
93     // inner: SpinLock<InnerPciBusDevice>,
94     device_data: SpinLock<DeviceCommonData>,
95     kobj_data: SpinLock<KObjectCommonData>,
96     kobj_state: LockedKObjectState,
97     name: String,
98 }
99 
100 impl PciBusDevice {
101     pub fn new(parent: Option<Weak<dyn KObject>>) -> Arc<Self> {
102         let common_device = DeviceCommonData::default();
103         let common_kobj = KObjectCommonData::default();
104         let bus_device = Self {
105             device_data: SpinLock::new(common_device),
106             kobj_data: SpinLock::new(common_kobj),
107             kobj_state: LockedKObjectState::new(None),
108             name: "pci".to_string(),
109         };
110         bus_device.set_parent(parent);
111         return Arc::new(bus_device);
112     }
113 }
114 
115 impl KObject for PciBusDevice {
116     fn as_any_ref(&self) -> &dyn core::any::Any {
117         self
118     }
119 
120     fn parent(&self) -> Option<alloc::sync::Weak<dyn KObject>> {
121         self.kobj_data.lock().parent.clone()
122     }
123 
124     fn inode(&self) -> Option<Arc<KernFSInode>> {
125         self.kobj_data.lock().kern_inode.clone()
126     }
127 
128     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
129         self.kobj_data.lock().kern_inode = inode;
130     }
131 
132     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
133         self.kobj_data.lock().kobj_type
134     }
135 
136     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
137         self.kobj_data.lock().kobj_type = ktype
138     }
139 
140     fn kset(&self) -> Option<Arc<KSet>> {
141         self.kobj_data.lock().kset.clone()
142     }
143 
144     fn kobj_state(
145         &self,
146     ) -> crate::libs::rwlock::RwLockReadGuard<crate::driver::base::kobject::KObjectState> {
147         self.kobj_state.read()
148     }
149 
150     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
151         self.kobj_state.write()
152     }
153 
154     fn set_kobj_state(&self, state: KObjectState) {
155         *self.kobj_state.write() = state;
156     }
157 
158     fn name(&self) -> String {
159         self.name.clone()
160     }
161 
162     fn set_name(&self, _name: String) {
163         //do nothing; it's not supposed to change this struct's name
164     }
165 
166     fn set_kset(&self, kset: Option<Arc<KSet>>) {
167         self.kobj_data.lock().kset = kset;
168     }
169 
170     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
171         self.kobj_data.lock().parent = parent;
172     }
173 }
174 
175 impl Device for PciBusDevice {
176     fn dev_type(&self) -> DeviceType {
177         return DeviceType::Bus;
178     }
179 
180     fn id_table(&self) -> IdTable {
181         IdTable::new("pci".to_string(), None)
182     }
183 
184     fn bus(&self) -> Option<Weak<dyn Bus>> {
185         self.device_data.lock().bus.clone()
186     }
187 
188     fn set_bus(&self, bus: Option<alloc::sync::Weak<dyn Bus>>) {
189         self.device_data.lock().bus = bus
190     }
191 
192     fn driver(&self) -> Option<Arc<dyn Driver>> {
193         self.device_data.lock().driver.clone()?.upgrade()
194     }
195 
196     fn is_dead(&self) -> bool {
197         false
198     }
199 
200     fn set_driver(&self, driver: Option<alloc::sync::Weak<dyn Driver>>) {
201         self.device_data.lock().driver = driver;
202     }
203 
204     fn can_match(&self) -> bool {
205         todo!()
206     }
207 
208     fn set_can_match(&self, _can_match: bool) {
209         todo!()
210     }
211 
212     fn set_class(&self, _class: Option<alloc::sync::Weak<dyn crate::driver::base::class::Class>>) {
213         todo!()
214     }
215 
216     fn state_synced(&self) -> bool {
217         todo!()
218     }
219 }
220