xref: /DragonOS/kernel/src/driver/pci/raw_device.rs (revision f79998f626801329580c782fd05e36cb2027f474)
1 use core::any::Any;
2 
3 use alloc::{
4     string::{String, ToString},
5     sync::{Arc, Weak},
6 };
7 
8 use crate::{
9     driver::base::{
10         class::Class,
11         device::{bus::Bus, driver::Driver, Device, DeviceCommonData, DeviceType, IdTable},
12         kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState},
13         kset::KSet,
14     },
15     filesystem::{kernfs::KernFSInode, sysfs::AttributeGroup},
16     libs::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
17 };
18 
19 use super::{
20     attr::BasicPciReadOnlyAttrs, dev_id::PciDeviceID, device::PciDevice,
21     pci::PciDeviceStructureGeneralDevice,
22 };
23 #[derive(Debug)]
24 #[cast_to([sync] Device)]
25 #[cast_to([sync] PciDevice)]
26 pub struct PciGeneralDevice {
27     device_data: RwLock<DeviceCommonData>,
28     kobj_data: RwLock<KObjectCommonData>,
29     name: RwLock<Option<String>>,
30     kobj_state: LockedKObjectState,
31     dev_id: PciDeviceID,
32     header: Arc<PciDeviceStructureGeneralDevice>,
33 }
34 
35 impl From<&PciDeviceStructureGeneralDevice> for PciGeneralDevice {
36     fn from(value: &PciDeviceStructureGeneralDevice) -> Self {
37         let value = Arc::new(value.clone());
38         let name: String = value.common_header.bus_device_function.into();
39         let kobj_state = LockedKObjectState::new(None);
40         let common_dev = RwLock::new(DeviceCommonData::default());
41         let common_kobj = RwLock::new(KObjectCommonData::default());
42         let dev_id = PciDeviceID::dummpy();
43 
44         // dev_id.set_special(PciSpecifiedData::Virtio());
45         let res = Self {
46             device_data: common_dev,
47             kobj_data: common_kobj,
48             kobj_state,
49             dev_id,
50             header: value,
51             name: RwLock::new(None),
52         };
53         res.set_name(name);
54         res
55     }
56 }
57 
58 impl PciDevice for PciGeneralDevice {
59     fn dynid(&self) -> PciDeviceID {
60         self.dev_id
61     }
62 
63     fn vendor(&self) -> u16 {
64         self.header.common_header.vendor_id
65     }
66 
67     fn device_id(&self) -> u16 {
68         self.header.common_header.device_id
69     }
70 
71     fn subsystem_vendor(&self) -> u16 {
72         self.header.subsystem_vendor_id
73     }
74 
75     fn subsystem_device(&self) -> u16 {
76         self.header.subsystem_id
77     }
78 }
79 
80 impl Device for PciGeneralDevice {
81     fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]> {
82         Some(&[&BasicPciReadOnlyAttrs])
83     }
84 
85     fn bus(&self) -> Option<Weak<dyn Bus>> {
86         self.device_data.read().bus.clone()
87     }
88 
89     fn class(&self) -> Option<Arc<dyn Class>> {
90         let mut guard = self.device_data.write();
91         let r = guard.class.clone()?.upgrade();
92         if r.is_none() {
93             guard.class = None;
94         }
95 
96         return r;
97     }
98 
99     fn driver(&self) -> Option<Arc<dyn Driver>> {
100         self.device_data.read().driver.clone()?.upgrade()
101     }
102 
103     fn dev_type(&self) -> DeviceType {
104         DeviceType::Pci
105     }
106 
107     fn id_table(&self) -> IdTable {
108         IdTable::new("testPci".to_string(), None)
109     }
110 
111     fn can_match(&self) -> bool {
112         true
113     }
114 
115     fn is_dead(&self) -> bool {
116         false
117     }
118 
119     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
120         self.device_data.write().bus = bus;
121     }
122 
123     fn set_can_match(&self, _can_match: bool) {}
124 
125     fn set_class(&self, class: Option<Weak<dyn Class>>) {
126         self.device_data.write().class = class;
127     }
128 
129     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
130         self.device_data.write().driver = driver
131     }
132 
133     fn state_synced(&self) -> bool {
134         true
135     }
136 }
137 
138 impl KObject for PciGeneralDevice {
139     fn as_any_ref(&self) -> &dyn Any {
140         self
141     }
142 
143     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
144         self.kobj_data.write().kern_inode = inode;
145     }
146 
147     fn inode(&self) -> Option<Arc<KernFSInode>> {
148         self.kobj_data.read().kern_inode.clone()
149     }
150 
151     fn parent(&self) -> Option<Weak<dyn KObject>> {
152         self.kobj_data.read().parent.clone()
153     }
154 
155     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
156         self.kobj_data.write().parent = parent;
157     }
158 
159     fn kset(&self) -> Option<Arc<KSet>> {
160         self.kobj_data.read().kset.clone()
161     }
162 
163     fn set_kset(&self, kset: Option<Arc<KSet>>) {
164         self.kobj_data.write().kset = kset;
165     }
166 
167     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
168         self.kobj_data.read().kobj_type
169     }
170 
171     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
172         self.kobj_data.write().kobj_type = ktype;
173     }
174 
175     fn name(&self) -> String {
176         self.name.read().clone().unwrap()
177     }
178 
179     fn set_name(&self, name: String) {
180         *self.name.write() = Some(name);
181     }
182 
183     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
184         self.kobj_state.read()
185     }
186 
187     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
188         self.kobj_state.write()
189     }
190 
191     fn set_kobj_state(&self, state: KObjectState) {
192         *self.kobj_state.write() = state;
193     }
194 }
195