xref: /DragonOS/kernel/src/driver/pci/test/pt_device.rs (revision a8753f8fffb992e4d3bbd21eda431081b395af6b)
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 {
42     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 {
54     fn dynid(&self) -> PciDeviceID {
55         PciDeviceID::dummpy()
56     }
57 
58     fn vendor(&self) -> u16 {
59         return 0xffff;
60     }
61 
62     fn device_id(&self) -> u16 {
63         return 0xffff;
64     }
65 
66     fn subsystem_vendor(&self) -> u16 {
67         return 0xffff;
68     }
69 
70     fn subsystem_device(&self) -> u16 {
71         return 0xffff;
72     }
73 }
74 
75 impl Device for TestDevice {
76     fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]> {
77         Some(&[&HelloAttr])
78     }
79 
80     fn bus(&self) -> Option<Weak<dyn Bus>> {
81         self.device_data.read().bus.clone()
82     }
83 
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 
94     fn driver(&self) -> Option<Arc<dyn Driver>> {
95         self.device_data.read().driver.clone()?.upgrade()
96     }
97 
98     fn dev_type(&self) -> DeviceType {
99         DeviceType::Pci
100     }
101 
102     fn id_table(&self) -> IdTable {
103         IdTable::new("testPci".to_string(), None)
104     }
105 
106     fn can_match(&self) -> bool {
107         true
108     }
109 
110     fn is_dead(&self) -> bool {
111         false
112     }
113 
114     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
115         self.device_data.write().bus = bus
116     }
117 
118     fn set_can_match(&self, _can_match: bool) {
119         //todo
120     }
121 
122     fn set_class(&self, class: Option<Weak<dyn Class>>) {
123         self.device_data.write().class = class
124     }
125 
126     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
127         self.device_data.write().driver = driver
128     }
129 
130     fn state_synced(&self) -> bool {
131         true
132     }
133 
134     fn dev_parent(&self) -> Option<Weak<dyn Device>> {
135         self.device_data.read().parent.clone()
136     }
137 
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 {
144     fn as_any_ref(&self) -> &dyn Any {
145         self
146     }
147 
148     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
149         self.kobj_data.write().kern_inode = inode;
150     }
151 
152     fn inode(&self) -> Option<Arc<KernFSInode>> {
153         self.kobj_data.read().kern_inode.clone()
154     }
155 
156     fn parent(&self) -> Option<Weak<dyn KObject>> {
157         self.kobj_data.read().parent.clone()
158     }
159 
160     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
161         self.kobj_data.write().parent = parent;
162     }
163 
164     fn kset(&self) -> Option<Arc<KSet>> {
165         self.kobj_data.read().kset.clone()
166     }
167 
168     fn set_kset(&self, kset: Option<Arc<KSet>>) {
169         self.kobj_data.write().kset = kset;
170     }
171 
172     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
173         self.kobj_data.read().kobj_type
174     }
175 
176     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
177         self.kobj_data.write().kobj_type = ktype;
178     }
179 
180     fn name(&self) -> String {
181         "PciTest".to_string()
182     }
183 
184     fn set_name(&self, _name: String) {
185         // do nothing
186     }
187 
188     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
189         self.kobj_state.read()
190     }
191 
192     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
193         self.kobj_state.write()
194     }
195 
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 {
205     fn name(&self) -> Option<&str> {
206         return Some("TestAttr");
207     }
208 
209     fn attrs(&self) -> &[&'static dyn Attribute] {
210         &[&Hello]
211     }
212 
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 {
225     fn mode(&self) -> ModeType {
226         SYSFS_ATTR_MODE_RO
227     }
228 
229     fn name(&self) -> &str {
230         "Hello"
231     }
232 
233     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
234         return sysfs_emit_str(_buf, "Hello Pci");
235     }
236 
237     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
238         todo!()
239     }
240 
241     fn support(&self) -> SysFSOpsSupport {
242         SysFSOpsSupport::ATTR_SHOW
243     }
244 }
245