xref: /DragonOS/kernel/src/driver/pci/raw_device.rs (revision 7c28051e8c601312d3d0fd7bcb71bc71450d10c0)
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     inner: RwLock<InnerPciGeneralDevice>,
28     kobj_state: LockedKObjectState,
29     dev_id: PciDeviceID,
30     header: Arc<PciDeviceStructureGeneralDevice>,
31 }
32 
33 #[derive(Debug)]
34 struct InnerPciGeneralDevice {
35     name: Option<String>,
36     kobject_common: KObjectCommonData,
37     device_common: DeviceCommonData,
38 }
39 
40 impl From<Arc<PciDeviceStructureGeneralDevice>> for PciGeneralDevice {
41     fn from(value: Arc<PciDeviceStructureGeneralDevice>) -> Self {
42         // let value = Arc::new(value.clone());
43         let name: String = value.common_header.bus_device_function.into();
44         let kobj_state = LockedKObjectState::new(None);
45         let dev_id = PciDeviceID::dummpy();
46 
47         // dev_id.set_special(PciSpecifiedData::Virtio());
48         let res = Self {
49             inner: RwLock::new(InnerPciGeneralDevice {
50                 name: None,
51                 kobject_common: KObjectCommonData::default(),
52                 device_common: DeviceCommonData::default(),
53             }),
54             kobj_state,
55             dev_id,
56             header: value,
57         };
58         res.set_name(name);
59         res
60     }
61 }
62 
63 impl PciDevice for PciGeneralDevice {
64     fn dynid(&self) -> PciDeviceID {
65         self.dev_id
66     }
67 
68     fn vendor(&self) -> u16 {
69         self.header.common_header.vendor_id
70     }
71 
72     fn device_id(&self) -> u16 {
73         self.header.common_header.device_id
74     }
75 
76     fn subsystem_vendor(&self) -> u16 {
77         self.header.subsystem_vendor_id
78     }
79 
80     fn subsystem_device(&self) -> u16 {
81         self.header.subsystem_id
82     }
83 
84     fn class_code(&self) -> u8 {
85         self.header.common_header.class_code
86     }
87 
88     fn revision(&self) -> u8 {
89         self.header.common_header.revision_id
90     }
91 
92     fn irq_type(&self) -> &RwLock<super::pci_irq::IrqType> {
93         &self.header.irq_type
94     }
95 
96     fn irq_line(&self) -> u8 {
97         self.header.interrupt_line
98     }
99 
100     fn interface_code(&self) -> u8 {
101         self.header.common_header.prog_if
102     }
103 
104     fn subclass(&self) -> u8 {
105         self.header.common_header.subclass
106     }
107 }
108 
109 impl Device for PciGeneralDevice {
110     fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]> {
111         Some(&[&BasicPciReadOnlyAttrs])
112     }
113 
114     fn bus(&self) -> Option<Weak<dyn Bus>> {
115         self.inner.read().device_common.bus.clone()
116     }
117 
118     fn class(&self) -> Option<Arc<dyn Class>> {
119         let mut guard = self.inner.write();
120         let r = guard.device_common.class.clone()?.upgrade();
121         if r.is_none() {
122             guard.device_common.class = None;
123         }
124 
125         return r;
126     }
127 
128     fn driver(&self) -> Option<Arc<dyn Driver>> {
129         self.inner.read().device_common.driver.clone()?.upgrade()
130     }
131 
132     fn dev_type(&self) -> DeviceType {
133         DeviceType::Pci
134     }
135 
136     fn id_table(&self) -> IdTable {
137         IdTable::new("testPci".to_string(), None)
138     }
139 
140     fn can_match(&self) -> bool {
141         true
142     }
143 
144     fn is_dead(&self) -> bool {
145         false
146     }
147 
148     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
149         self.inner.write().device_common.bus = bus;
150     }
151 
152     fn set_can_match(&self, _can_match: bool) {}
153 
154     fn set_class(&self, class: Option<Weak<dyn Class>>) {
155         self.inner.write().device_common.class = class;
156     }
157 
158     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
159         self.inner.write().device_common.driver = driver
160     }
161 
162     fn state_synced(&self) -> bool {
163         true
164     }
165 
166     fn dev_parent(&self) -> Option<Weak<dyn Device>> {
167         self.inner.write().device_common.parent.clone()
168     }
169 
170     fn set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>) {
171         self.inner.write().device_common.parent = dev_parent;
172     }
173 }
174 
175 impl KObject for PciGeneralDevice {
176     fn as_any_ref(&self) -> &dyn Any {
177         self
178     }
179 
180     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
181         self.inner.write().kobject_common.kern_inode = inode;
182     }
183 
184     fn inode(&self) -> Option<Arc<KernFSInode>> {
185         self.inner.read().kobject_common.kern_inode.clone()
186     }
187 
188     fn parent(&self) -> Option<Weak<dyn KObject>> {
189         self.inner.read().kobject_common.parent.clone()
190     }
191 
192     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
193         self.inner.write().kobject_common.parent = parent;
194     }
195 
196     fn kset(&self) -> Option<Arc<KSet>> {
197         self.inner.read().kobject_common.kset.clone()
198     }
199 
200     fn set_kset(&self, kset: Option<Arc<KSet>>) {
201         self.inner.write().kobject_common.kset = kset;
202     }
203 
204     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
205         self.inner.read().kobject_common.kobj_type
206     }
207 
208     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
209         self.inner.write().kobject_common.kobj_type = ktype;
210     }
211 
212     fn name(&self) -> String {
213         self.inner.read().name.clone().unwrap()
214     }
215 
216     fn set_name(&self, name: String) {
217         self.inner.write().name = Some(name);
218     }
219 
220     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
221         self.kobj_state.read()
222     }
223 
224     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
225         self.kobj_state.write()
226     }
227 
228     fn set_kobj_state(&self, state: KObjectState) {
229         *self.kobj_state.write() = state;
230     }
231 }
232