xref: /DragonOS/kernel/src/driver/pci/test/pt_device.rs (revision 2b7818e80e00fcfe4d03533f587cc125ea5e4bec)
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 
135 impl KObject for TestDevice {
136     fn as_any_ref(&self) -> &dyn Any {
137         self
138     }
139 
140     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
141         self.kobj_data.write().kern_inode = inode;
142     }
143 
144     fn inode(&self) -> Option<Arc<KernFSInode>> {
145         self.kobj_data.read().kern_inode.clone()
146     }
147 
148     fn parent(&self) -> Option<Weak<dyn KObject>> {
149         self.kobj_data.read().parent.clone()
150     }
151 
152     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
153         self.kobj_data.write().parent = parent;
154     }
155 
156     fn kset(&self) -> Option<Arc<KSet>> {
157         self.kobj_data.read().kset.clone()
158     }
159 
160     fn set_kset(&self, kset: Option<Arc<KSet>>) {
161         self.kobj_data.write().kset = kset;
162     }
163 
164     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
165         self.kobj_data.read().kobj_type
166     }
167 
168     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
169         self.kobj_data.write().kobj_type = ktype;
170     }
171 
172     fn name(&self) -> String {
173         "PciTest".to_string()
174     }
175 
176     fn set_name(&self, _name: String) {
177         // do nothing
178     }
179 
180     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
181         self.kobj_state.read()
182     }
183 
184     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
185         self.kobj_state.write()
186     }
187 
188     fn set_kobj_state(&self, state: KObjectState) {
189         *self.kobj_state.write() = state;
190     }
191 }
192 
193 #[derive(Debug)]
194 pub struct HelloAttr;
195 
196 impl AttributeGroup for HelloAttr {
197     fn name(&self) -> Option<&str> {
198         return Some("TestAttr");
199     }
200 
201     fn attrs(&self) -> &[&'static dyn Attribute] {
202         &[&Hello]
203     }
204 
205     fn is_visible(
206         &self,
207         _kobj: Arc<dyn KObject>,
208         attr: &'static dyn Attribute,
209     ) -> Option<ModeType> {
210         return Some(attr.mode());
211     }
212 }
213 #[derive(Debug)]
214 pub struct Hello;
215 
216 impl Attribute for Hello {
217     fn mode(&self) -> ModeType {
218         SYSFS_ATTR_MODE_RO
219     }
220 
221     fn name(&self) -> &str {
222         "Hello"
223     }
224 
225     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
226         return sysfs_emit_str(_buf, "Hello Pci");
227     }
228 
229     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
230         todo!()
231     }
232 
233     fn support(&self) -> SysFSOpsSupport {
234         SysFSOpsSupport::ATTR_SHOW
235     }
236 }
237