xref: /DragonOS/kernel/src/driver/pci/test/pt_device.rs (revision 7c28051e8c601312d3d0fd7bcb71bc71450d10c0)
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, pci_irq::IrqType},
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     static_type: RwLock<IrqType>,
40 }
41 
42 impl TestDevice {
43     pub fn new() -> Self {
44         let common_dev = RwLock::new(DeviceCommonData::default());
45         let common_kobj = RwLock::new(KObjectCommonData::default());
46         Self {
47             device_data: common_dev,
48             kobj_data: common_kobj,
49             kobj_state: LockedKObjectState::new(None),
50             static_type: RwLock::new(IrqType::Unused),
51         }
52     }
53 }
54 
55 impl PciDevice for TestDevice {
56     fn dynid(&self) -> PciDeviceID {
57         PciDeviceID::dummpy()
58     }
59 
60     fn vendor(&self) -> u16 {
61         return 0xffff;
62     }
63 
64     fn device_id(&self) -> u16 {
65         return 0xffff;
66     }
67 
68     fn subsystem_vendor(&self) -> u16 {
69         return 0xffff;
70     }
71 
72     fn subsystem_device(&self) -> u16 {
73         return 0xffff;
74     }
75 
76     fn class_code(&self) -> u8 {
77         return 0xff;
78     }
79 
80     fn irq_line(&self) -> u8 {
81         return 0xff;
82     }
83 
84     fn revision(&self) -> u8 {
85         return 0xff;
86     }
87 
88     fn irq_type(&self) -> &RwLock<crate::driver::pci::pci_irq::IrqType> {
89         return &self.static_type;
90     }
91 
92     fn interface_code(&self) -> u8 {
93         return 0xff;
94     }
95 
96     fn subclass(&self) -> u8 {
97         return 0xff;
98     }
99 }
100 
101 impl Device for TestDevice {
102     fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]> {
103         Some(&[&HelloAttr])
104     }
105 
106     fn bus(&self) -> Option<Weak<dyn Bus>> {
107         self.device_data.read().bus.clone()
108     }
109 
110     fn class(&self) -> Option<Arc<dyn Class>> {
111         let mut guard = self.device_data.write();
112         let r = guard.class.clone()?.upgrade();
113         if r.is_none() {
114             guard.class = None;
115         }
116 
117         return r;
118     }
119 
120     fn driver(&self) -> Option<Arc<dyn Driver>> {
121         self.device_data.read().driver.clone()?.upgrade()
122     }
123 
124     fn dev_type(&self) -> DeviceType {
125         DeviceType::Pci
126     }
127 
128     fn id_table(&self) -> IdTable {
129         IdTable::new("testPci".to_string(), None)
130     }
131 
132     fn can_match(&self) -> bool {
133         true
134     }
135 
136     fn is_dead(&self) -> bool {
137         false
138     }
139 
140     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
141         self.device_data.write().bus = bus
142     }
143 
144     fn set_can_match(&self, _can_match: bool) {
145         //todo
146     }
147 
148     fn set_class(&self, class: Option<Weak<dyn Class>>) {
149         self.device_data.write().class = class
150     }
151 
152     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
153         self.device_data.write().driver = driver
154     }
155 
156     fn state_synced(&self) -> bool {
157         true
158     }
159 
160     fn dev_parent(&self) -> Option<Weak<dyn Device>> {
161         self.device_data.read().parent.clone()
162     }
163 
164     fn set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>) {
165         self.device_data.write().parent = dev_parent
166     }
167 }
168 
169 impl KObject for TestDevice {
170     fn as_any_ref(&self) -> &dyn Any {
171         self
172     }
173 
174     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
175         self.kobj_data.write().kern_inode = inode;
176     }
177 
178     fn inode(&self) -> Option<Arc<KernFSInode>> {
179         self.kobj_data.read().kern_inode.clone()
180     }
181 
182     fn parent(&self) -> Option<Weak<dyn KObject>> {
183         self.kobj_data.read().parent.clone()
184     }
185 
186     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
187         self.kobj_data.write().parent = parent;
188     }
189 
190     fn kset(&self) -> Option<Arc<KSet>> {
191         self.kobj_data.read().kset.clone()
192     }
193 
194     fn set_kset(&self, kset: Option<Arc<KSet>>) {
195         self.kobj_data.write().kset = kset;
196     }
197 
198     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
199         self.kobj_data.read().kobj_type
200     }
201 
202     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
203         self.kobj_data.write().kobj_type = ktype;
204     }
205 
206     fn name(&self) -> String {
207         "PciTest".to_string()
208     }
209 
210     fn set_name(&self, _name: String) {
211         // do nothing
212     }
213 
214     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
215         self.kobj_state.read()
216     }
217 
218     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
219         self.kobj_state.write()
220     }
221 
222     fn set_kobj_state(&self, state: KObjectState) {
223         *self.kobj_state.write() = state;
224     }
225 }
226 
227 #[derive(Debug)]
228 pub struct HelloAttr;
229 
230 impl AttributeGroup for HelloAttr {
231     fn name(&self) -> Option<&str> {
232         return Some("TestAttr");
233     }
234 
235     fn attrs(&self) -> &[&'static dyn Attribute] {
236         &[&Hello]
237     }
238 
239     fn is_visible(
240         &self,
241         _kobj: Arc<dyn KObject>,
242         attr: &'static dyn Attribute,
243     ) -> Option<ModeType> {
244         return Some(attr.mode());
245     }
246 }
247 #[derive(Debug)]
248 pub struct Hello;
249 
250 impl Attribute for Hello {
251     fn mode(&self) -> ModeType {
252         SYSFS_ATTR_MODE_RO
253     }
254 
255     fn name(&self) -> &str {
256         "Hello"
257     }
258 
259     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
260         return sysfs_emit_str(_buf, "Hello Pci");
261     }
262 
263     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
264         todo!()
265     }
266 
267     fn support(&self) -> SysFSOpsSupport {
268         SysFSOpsSupport::ATTR_SHOW
269     }
270 }
271