xref: /DragonOS/kernel/src/driver/pci/raw_device.rs (revision a8753f8fffb992e4d3bbd21eda431081b395af6b)
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<&PciDeviceStructureGeneralDevice> for PciGeneralDevice {
41     fn from(value: &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 
85 impl Device for PciGeneralDevice {
86     fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]> {
87         Some(&[&BasicPciReadOnlyAttrs])
88     }
89 
90     fn bus(&self) -> Option<Weak<dyn Bus>> {
91         self.inner.read().device_common.bus.clone()
92     }
93 
94     fn class(&self) -> Option<Arc<dyn Class>> {
95         let mut guard = self.inner.write();
96         let r = guard.device_common.class.clone()?.upgrade();
97         if r.is_none() {
98             guard.device_common.class = None;
99         }
100 
101         return r;
102     }
103 
104     fn driver(&self) -> Option<Arc<dyn Driver>> {
105         self.inner.read().device_common.driver.clone()?.upgrade()
106     }
107 
108     fn dev_type(&self) -> DeviceType {
109         DeviceType::Pci
110     }
111 
112     fn id_table(&self) -> IdTable {
113         IdTable::new("testPci".to_string(), None)
114     }
115 
116     fn can_match(&self) -> bool {
117         true
118     }
119 
120     fn is_dead(&self) -> bool {
121         false
122     }
123 
124     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
125         self.inner.write().device_common.bus = bus;
126     }
127 
128     fn set_can_match(&self, _can_match: bool) {}
129 
130     fn set_class(&self, class: Option<Weak<dyn Class>>) {
131         self.inner.write().device_common.class = class;
132     }
133 
134     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
135         self.inner.write().device_common.driver = driver
136     }
137 
138     fn state_synced(&self) -> bool {
139         true
140     }
141 
142     fn dev_parent(&self) -> Option<Weak<dyn Device>> {
143         self.inner.write().device_common.parent.clone()
144     }
145 
146     fn set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>) {
147         self.inner.write().device_common.parent = dev_parent;
148     }
149 }
150 
151 impl KObject for PciGeneralDevice {
152     fn as_any_ref(&self) -> &dyn Any {
153         self
154     }
155 
156     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
157         self.inner.write().kobject_common.kern_inode = inode;
158     }
159 
160     fn inode(&self) -> Option<Arc<KernFSInode>> {
161         self.inner.read().kobject_common.kern_inode.clone()
162     }
163 
164     fn parent(&self) -> Option<Weak<dyn KObject>> {
165         self.inner.read().kobject_common.parent.clone()
166     }
167 
168     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
169         self.inner.write().kobject_common.parent = parent;
170     }
171 
172     fn kset(&self) -> Option<Arc<KSet>> {
173         self.inner.read().kobject_common.kset.clone()
174     }
175 
176     fn set_kset(&self, kset: Option<Arc<KSet>>) {
177         self.inner.write().kobject_common.kset = kset;
178     }
179 
180     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
181         self.inner.read().kobject_common.kobj_type
182     }
183 
184     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
185         self.inner.write().kobject_common.kobj_type = ktype;
186     }
187 
188     fn name(&self) -> String {
189         self.inner.read().name.clone().unwrap()
190     }
191 
192     fn set_name(&self, name: String) {
193         self.inner.write().name = Some(name);
194     }
195 
196     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
197         self.kobj_state.read()
198     }
199 
200     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
201         self.kobj_state.write()
202     }
203 
204     fn set_kobj_state(&self, state: KObjectState) {
205         *self.kobj_state.write() = state;
206     }
207 }
208