xref: /DragonOS/kernel/src/driver/pci/test/pt_device.rs (revision 28fe4ad2a0b0d8b5abf1f0cb402b1c3204b42242)
1 use core::any::Any;
2 
3 use alloc::{
4     string::{String, ToString},
5     sync::{Arc, Weak},
6 };
7 use system_error::SystemError;
8 
9 use crate::{
10     driver::{
11         base::{
12             class::Class,
13             device::{bus::Bus, driver::Driver, Device, DeviceCommonData, DeviceType, IdTable},
14             kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState},
15             kset::KSet,
16         },
17         pci::{dev_id::PciDeviceID, device::PciDevice},
18     },
19     filesystem::{
20         kernfs::KernFSInode,
21         sysfs::{
22             file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport, SYSFS_ATTR_MODE_RO,
23         },
24         vfs::syscall::ModeType,
25     },
26     libs::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
27 };
28 #[derive(Debug)]
29 #[cast_to([sync] Device)]
30 #[cast_to([sync] PciDevice)]
31 /// # 结构功能
32 /// 这是一个测试用的PciDevice,也可以作为新PciDevice的参考
33 /// 它需要实现KObject PciDevice Device这些接口
34 /// 并通过函数pci_device_manager().device_add()来将设备进行接入
35 pub struct TestDevice {
36     device_data: RwLock<DeviceCommonData>,
37     kobj_data: RwLock<KObjectCommonData>,
38     kobj_state: LockedKObjectState,
39 }
40 
41 impl TestDevice {
new() -> Self42     pub fn new() -> Self {
43         let common_dev = RwLock::new(DeviceCommonData::default());
44         let common_kobj = RwLock::new(KObjectCommonData::default());
45         Self {
46             device_data: common_dev,
47             kobj_data: common_kobj,
48             kobj_state: LockedKObjectState::new(None),
49         }
50     }
51 }
52 
53 impl PciDevice for TestDevice {
dynid(&self) -> PciDeviceID54     fn dynid(&self) -> PciDeviceID {
55         PciDeviceID::dummpy()
56     }
57 
vendor(&self) -> u1658     fn vendor(&self) -> u16 {
59         return 0xffff;
60     }
61 
device_id(&self) -> u1662     fn device_id(&self) -> u16 {
63         return 0xffff;
64     }
65 
subsystem_vendor(&self) -> u1666     fn subsystem_vendor(&self) -> u16 {
67         return 0xffff;
68     }
69 
subsystem_device(&self) -> u1670     fn subsystem_device(&self) -> u16 {
71         return 0xffff;
72     }
73 }
74 
75 impl Device for TestDevice {
attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]>76     fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]> {
77         Some(&[&HelloAttr])
78     }
79 
bus(&self) -> Option<Weak<dyn Bus>>80     fn bus(&self) -> Option<Weak<dyn Bus>> {
81         self.device_data.read().bus.clone()
82     }
83 
class(&self) -> Option<Arc<dyn Class>>84     fn class(&self) -> Option<Arc<dyn Class>> {
85         let mut guard = self.device_data.write();
86         let r = guard.class.clone()?.upgrade();
87         if r.is_none() {
88             guard.class = None;
89         }
90 
91         return r;
92     }
93 
driver(&self) -> Option<Arc<dyn Driver>>94     fn driver(&self) -> Option<Arc<dyn Driver>> {
95         self.device_data.read().driver.clone()?.upgrade()
96     }
97 
dev_type(&self) -> DeviceType98     fn dev_type(&self) -> DeviceType {
99         DeviceType::Pci
100     }
101 
id_table(&self) -> IdTable102     fn id_table(&self) -> IdTable {
103         IdTable::new("testPci".to_string(), None)
104     }
105 
can_match(&self) -> bool106     fn can_match(&self) -> bool {
107         true
108     }
109 
is_dead(&self) -> bool110     fn is_dead(&self) -> bool {
111         false
112     }
113 
set_bus(&self, bus: Option<Weak<dyn Bus>>)114     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
115         self.device_data.write().bus = bus
116     }
117 
set_can_match(&self, _can_match: bool)118     fn set_can_match(&self, _can_match: bool) {
119         //todo
120     }
121 
set_class(&self, class: Option<Weak<dyn Class>>)122     fn set_class(&self, class: Option<Weak<dyn Class>>) {
123         self.device_data.write().class = class
124     }
125 
set_driver(&self, driver: Option<Weak<dyn Driver>>)126     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
127         self.device_data.write().driver = driver
128     }
129 
state_synced(&self) -> bool130     fn state_synced(&self) -> bool {
131         true
132     }
133 
dev_parent(&self) -> Option<Weak<dyn Device>>134     fn dev_parent(&self) -> Option<Weak<dyn Device>> {
135         self.device_data.read().parent.clone()
136     }
137 
set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>)138     fn set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>) {
139         self.device_data.write().parent = dev_parent
140     }
141 }
142 
143 impl KObject for TestDevice {
as_any_ref(&self) -> &dyn Any144     fn as_any_ref(&self) -> &dyn Any {
145         self
146     }
147 
set_inode(&self, inode: Option<Arc<KernFSInode>>)148     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
149         self.kobj_data.write().kern_inode = inode;
150     }
151 
inode(&self) -> Option<Arc<KernFSInode>>152     fn inode(&self) -> Option<Arc<KernFSInode>> {
153         self.kobj_data.read().kern_inode.clone()
154     }
155 
parent(&self) -> Option<Weak<dyn KObject>>156     fn parent(&self) -> Option<Weak<dyn KObject>> {
157         self.kobj_data.read().parent.clone()
158     }
159 
set_parent(&self, parent: Option<Weak<dyn KObject>>)160     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
161         self.kobj_data.write().parent = parent;
162     }
163 
kset(&self) -> Option<Arc<KSet>>164     fn kset(&self) -> Option<Arc<KSet>> {
165         self.kobj_data.read().kset.clone()
166     }
167 
set_kset(&self, kset: Option<Arc<KSet>>)168     fn set_kset(&self, kset: Option<Arc<KSet>>) {
169         self.kobj_data.write().kset = kset;
170     }
171 
kobj_type(&self) -> Option<&'static dyn KObjType>172     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
173         self.kobj_data.read().kobj_type
174     }
175 
set_kobj_type(&self, ktype: Option<&'static dyn KObjType>)176     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
177         self.kobj_data.write().kobj_type = ktype;
178     }
179 
name(&self) -> String180     fn name(&self) -> String {
181         "PciTest".to_string()
182     }
183 
set_name(&self, _name: String)184     fn set_name(&self, _name: String) {
185         // do nothing
186     }
187 
kobj_state(&self) -> RwLockReadGuard<KObjectState>188     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
189         self.kobj_state.read()
190     }
191 
kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState>192     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
193         self.kobj_state.write()
194     }
195 
set_kobj_state(&self, state: KObjectState)196     fn set_kobj_state(&self, state: KObjectState) {
197         *self.kobj_state.write() = state;
198     }
199 }
200 
201 #[derive(Debug)]
202 pub struct HelloAttr;
203 
204 impl AttributeGroup for HelloAttr {
name(&self) -> Option<&str>205     fn name(&self) -> Option<&str> {
206         return Some("TestAttr");
207     }
208 
attrs(&self) -> &[&'static dyn Attribute]209     fn attrs(&self) -> &[&'static dyn Attribute] {
210         &[&Hello]
211     }
212 
is_visible( &self, _kobj: Arc<dyn KObject>, attr: &'static dyn Attribute, ) -> Option<ModeType>213     fn is_visible(
214         &self,
215         _kobj: Arc<dyn KObject>,
216         attr: &'static dyn Attribute,
217     ) -> Option<ModeType> {
218         return Some(attr.mode());
219     }
220 }
221 #[derive(Debug)]
222 pub struct Hello;
223 
224 impl Attribute for Hello {
mode(&self) -> ModeType225     fn mode(&self) -> ModeType {
226         SYSFS_ATTR_MODE_RO
227     }
228 
name(&self) -> &str229     fn name(&self) -> &str {
230         "Hello"
231     }
232 
show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError>233     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
234         return sysfs_emit_str(_buf, "Hello Pci");
235     }
236 
store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>237     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
238         todo!()
239     }
240 
support(&self) -> SysFSOpsSupport241     fn support(&self) -> SysFSOpsSupport {
242         SysFSOpsSupport::ATTR_SHOW
243     }
244 }
245