xref: /DragonOS/kernel/src/driver/pci/test/pt_device.rs (revision 69715438f21c9a4da5198b92aee5c27aa8c44543)
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 {
new() -> Self43     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 {
dynid(&self) -> PciDeviceID56     fn dynid(&self) -> PciDeviceID {
57         PciDeviceID::dummpy()
58     }
59 
vendor(&self) -> u1660     fn vendor(&self) -> u16 {
61         return 0xffff;
62     }
63 
device_id(&self) -> u1664     fn device_id(&self) -> u16 {
65         return 0xffff;
66     }
67 
subsystem_vendor(&self) -> u1668     fn subsystem_vendor(&self) -> u16 {
69         return 0xffff;
70     }
71 
subsystem_device(&self) -> u1672     fn subsystem_device(&self) -> u16 {
73         return 0xffff;
74     }
75 
class_code(&self) -> u876     fn class_code(&self) -> u8 {
77         return 0xff;
78     }
79 
irq_line(&self) -> u880     fn irq_line(&self) -> u8 {
81         return 0xff;
82     }
83 
revision(&self) -> u884     fn revision(&self) -> u8 {
85         return 0xff;
86     }
87 
irq_type(&self) -> &RwLock<crate::driver::pci::pci_irq::IrqType>88     fn irq_type(&self) -> &RwLock<crate::driver::pci::pci_irq::IrqType> {
89         return &self.static_type;
90     }
91 
interface_code(&self) -> u892     fn interface_code(&self) -> u8 {
93         return 0xff;
94     }
95 
subclass(&self) -> u896     fn subclass(&self) -> u8 {
97         return 0xff;
98     }
99 }
100 
101 impl Device for TestDevice {
attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]>102     fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]> {
103         Some(&[&HelloAttr])
104     }
105 
bus(&self) -> Option<Weak<dyn Bus>>106     fn bus(&self) -> Option<Weak<dyn Bus>> {
107         self.device_data.read().bus.clone()
108     }
109 
class(&self) -> Option<Arc<dyn Class>>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 
driver(&self) -> Option<Arc<dyn Driver>>120     fn driver(&self) -> Option<Arc<dyn Driver>> {
121         self.device_data.read().driver.clone()?.upgrade()
122     }
123 
dev_type(&self) -> DeviceType124     fn dev_type(&self) -> DeviceType {
125         DeviceType::Pci
126     }
127 
id_table(&self) -> IdTable128     fn id_table(&self) -> IdTable {
129         IdTable::new("testPci".to_string(), None)
130     }
131 
can_match(&self) -> bool132     fn can_match(&self) -> bool {
133         true
134     }
135 
is_dead(&self) -> bool136     fn is_dead(&self) -> bool {
137         false
138     }
139 
set_bus(&self, bus: Option<Weak<dyn Bus>>)140     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
141         self.device_data.write().bus = bus
142     }
143 
set_can_match(&self, _can_match: bool)144     fn set_can_match(&self, _can_match: bool) {
145         //todo
146     }
147 
set_class(&self, class: Option<Weak<dyn Class>>)148     fn set_class(&self, class: Option<Weak<dyn Class>>) {
149         self.device_data.write().class = class
150     }
151 
set_driver(&self, driver: Option<Weak<dyn Driver>>)152     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
153         self.device_data.write().driver = driver
154     }
155 
state_synced(&self) -> bool156     fn state_synced(&self) -> bool {
157         true
158     }
159 
dev_parent(&self) -> Option<Weak<dyn Device>>160     fn dev_parent(&self) -> Option<Weak<dyn Device>> {
161         self.device_data.read().parent.clone()
162     }
163 
set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>)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 {
as_any_ref(&self) -> &dyn Any170     fn as_any_ref(&self) -> &dyn Any {
171         self
172     }
173 
set_inode(&self, inode: Option<Arc<KernFSInode>>)174     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
175         self.kobj_data.write().kern_inode = inode;
176     }
177 
inode(&self) -> Option<Arc<KernFSInode>>178     fn inode(&self) -> Option<Arc<KernFSInode>> {
179         self.kobj_data.read().kern_inode.clone()
180     }
181 
parent(&self) -> Option<Weak<dyn KObject>>182     fn parent(&self) -> Option<Weak<dyn KObject>> {
183         self.kobj_data.read().parent.clone()
184     }
185 
set_parent(&self, parent: Option<Weak<dyn KObject>>)186     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
187         self.kobj_data.write().parent = parent;
188     }
189 
kset(&self) -> Option<Arc<KSet>>190     fn kset(&self) -> Option<Arc<KSet>> {
191         self.kobj_data.read().kset.clone()
192     }
193 
set_kset(&self, kset: Option<Arc<KSet>>)194     fn set_kset(&self, kset: Option<Arc<KSet>>) {
195         self.kobj_data.write().kset = kset;
196     }
197 
kobj_type(&self) -> Option<&'static dyn KObjType>198     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
199         self.kobj_data.read().kobj_type
200     }
201 
set_kobj_type(&self, ktype: Option<&'static dyn KObjType>)202     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
203         self.kobj_data.write().kobj_type = ktype;
204     }
205 
name(&self) -> String206     fn name(&self) -> String {
207         "PciTest".to_string()
208     }
209 
set_name(&self, _name: String)210     fn set_name(&self, _name: String) {
211         // do nothing
212     }
213 
kobj_state(&self) -> RwLockReadGuard<KObjectState>214     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
215         self.kobj_state.read()
216     }
217 
kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState>218     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
219         self.kobj_state.write()
220     }
221 
set_kobj_state(&self, state: KObjectState)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 {
name(&self) -> Option<&str>231     fn name(&self) -> Option<&str> {
232         return Some("TestAttr");
233     }
234 
attrs(&self) -> &[&'static dyn Attribute]235     fn attrs(&self) -> &[&'static dyn Attribute] {
236         &[&Hello]
237     }
238 
is_visible( &self, _kobj: Arc<dyn KObject>, attr: &'static dyn Attribute, ) -> Option<ModeType>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 {
mode(&self) -> ModeType251     fn mode(&self) -> ModeType {
252         SYSFS_ATTR_MODE_RO
253     }
254 
name(&self) -> &str255     fn name(&self) -> &str {
256         "Hello"
257     }
258 
show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError>259     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
260         return sysfs_emit_str(_buf, "Hello Pci");
261     }
262 
store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>263     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
264         todo!()
265     }
266 
support(&self) -> SysFSOpsSupport267     fn support(&self) -> SysFSOpsSupport {
268         SysFSOpsSupport::ATTR_SHOW
269     }
270 }
271