xref: /DragonOS/kernel/src/driver/pci/raw_device.rs (revision 28fe4ad2a0b0d8b5abf1f0cb402b1c3204b42242)
11f4877a4S曾俊 use core::any::Any;
21f4877a4S曾俊 
31f4877a4S曾俊 use alloc::{
41f4877a4S曾俊     string::{String, ToString},
51f4877a4S曾俊     sync::{Arc, Weak},
61f4877a4S曾俊 };
71f4877a4S曾俊 
81f4877a4S曾俊 use crate::{
91f4877a4S曾俊     driver::base::{
101f4877a4S曾俊         class::Class,
111f4877a4S曾俊         device::{bus::Bus, driver::Driver, Device, DeviceCommonData, DeviceType, IdTable},
121f4877a4S曾俊         kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState},
131f4877a4S曾俊         kset::KSet,
141f4877a4S曾俊     },
151f4877a4S曾俊     filesystem::{kernfs::KernFSInode, sysfs::AttributeGroup},
161f4877a4S曾俊     libs::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard},
171f4877a4S曾俊 };
181f4877a4S曾俊 
191f4877a4S曾俊 use super::{
201f4877a4S曾俊     attr::BasicPciReadOnlyAttrs, dev_id::PciDeviceID, device::PciDevice,
211f4877a4S曾俊     pci::PciDeviceStructureGeneralDevice,
221f4877a4S曾俊 };
231f4877a4S曾俊 #[derive(Debug)]
241f4877a4S曾俊 #[cast_to([sync] Device)]
251f4877a4S曾俊 #[cast_to([sync] PciDevice)]
261f4877a4S曾俊 pub struct PciGeneralDevice {
27*28fe4ad2S黄铭涛     inner: RwLock<InnerPciGeneralDevice>,
281f4877a4S曾俊     kobj_state: LockedKObjectState,
291f4877a4S曾俊     dev_id: PciDeviceID,
301f4877a4S曾俊     header: Arc<PciDeviceStructureGeneralDevice>,
311f4877a4S曾俊 }
321f4877a4S曾俊 
33*28fe4ad2S黄铭涛 #[derive(Debug)]
34*28fe4ad2S黄铭涛 struct InnerPciGeneralDevice {
35*28fe4ad2S黄铭涛     name: Option<String>,
36*28fe4ad2S黄铭涛     kobject_common: KObjectCommonData,
37*28fe4ad2S黄铭涛     device_common: DeviceCommonData,
38*28fe4ad2S黄铭涛 }
39*28fe4ad2S黄铭涛 
401f4877a4S曾俊 impl From<&PciDeviceStructureGeneralDevice> for PciGeneralDevice {
from(value: &PciDeviceStructureGeneralDevice) -> Self411f4877a4S曾俊     fn from(value: &PciDeviceStructureGeneralDevice) -> Self {
421f4877a4S曾俊         let value = Arc::new(value.clone());
431f4877a4S曾俊         let name: String = value.common_header.bus_device_function.into();
441f4877a4S曾俊         let kobj_state = LockedKObjectState::new(None);
451f4877a4S曾俊         let dev_id = PciDeviceID::dummpy();
461f4877a4S曾俊 
471f4877a4S曾俊         // dev_id.set_special(PciSpecifiedData::Virtio());
481f4877a4S曾俊         let res = Self {
49*28fe4ad2S黄铭涛             inner: RwLock::new(InnerPciGeneralDevice {
50*28fe4ad2S黄铭涛                 name: None,
51*28fe4ad2S黄铭涛                 kobject_common: KObjectCommonData::default(),
52*28fe4ad2S黄铭涛                 device_common: DeviceCommonData::default(),
53*28fe4ad2S黄铭涛             }),
541f4877a4S曾俊             kobj_state,
551f4877a4S曾俊             dev_id,
561f4877a4S曾俊             header: value,
571f4877a4S曾俊         };
581f4877a4S曾俊         res.set_name(name);
591f4877a4S曾俊         res
601f4877a4S曾俊     }
611f4877a4S曾俊 }
621f4877a4S曾俊 
631f4877a4S曾俊 impl PciDevice for PciGeneralDevice {
dynid(&self) -> PciDeviceID641f4877a4S曾俊     fn dynid(&self) -> PciDeviceID {
651f4877a4S曾俊         self.dev_id
661f4877a4S曾俊     }
671f4877a4S曾俊 
vendor(&self) -> u16681f4877a4S曾俊     fn vendor(&self) -> u16 {
691f4877a4S曾俊         self.header.common_header.vendor_id
701f4877a4S曾俊     }
711f4877a4S曾俊 
device_id(&self) -> u16721f4877a4S曾俊     fn device_id(&self) -> u16 {
731f4877a4S曾俊         self.header.common_header.device_id
741f4877a4S曾俊     }
751f4877a4S曾俊 
subsystem_vendor(&self) -> u16761f4877a4S曾俊     fn subsystem_vendor(&self) -> u16 {
771f4877a4S曾俊         self.header.subsystem_vendor_id
781f4877a4S曾俊     }
791f4877a4S曾俊 
subsystem_device(&self) -> u16801f4877a4S曾俊     fn subsystem_device(&self) -> u16 {
811f4877a4S曾俊         self.header.subsystem_id
821f4877a4S曾俊     }
831f4877a4S曾俊 }
841f4877a4S曾俊 
851f4877a4S曾俊 impl Device for PciGeneralDevice {
attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]>861f4877a4S曾俊     fn attribute_groups(&self) -> Option<&'static [&'static dyn AttributeGroup]> {
871f4877a4S曾俊         Some(&[&BasicPciReadOnlyAttrs])
881f4877a4S曾俊     }
891f4877a4S曾俊 
bus(&self) -> Option<Weak<dyn Bus>>901f4877a4S曾俊     fn bus(&self) -> Option<Weak<dyn Bus>> {
91*28fe4ad2S黄铭涛         self.inner.read().device_common.bus.clone()
921f4877a4S曾俊     }
931f4877a4S曾俊 
class(&self) -> Option<Arc<dyn Class>>941f4877a4S曾俊     fn class(&self) -> Option<Arc<dyn Class>> {
95*28fe4ad2S黄铭涛         let mut guard = self.inner.write();
96*28fe4ad2S黄铭涛         let r = guard.device_common.class.clone()?.upgrade();
971f4877a4S曾俊         if r.is_none() {
98*28fe4ad2S黄铭涛             guard.device_common.class = None;
991f4877a4S曾俊         }
1001f4877a4S曾俊 
1011f4877a4S曾俊         return r;
1021f4877a4S曾俊     }
1031f4877a4S曾俊 
driver(&self) -> Option<Arc<dyn Driver>>1041f4877a4S曾俊     fn driver(&self) -> Option<Arc<dyn Driver>> {
105*28fe4ad2S黄铭涛         self.inner.read().device_common.driver.clone()?.upgrade()
1061f4877a4S曾俊     }
1071f4877a4S曾俊 
dev_type(&self) -> DeviceType1081f4877a4S曾俊     fn dev_type(&self) -> DeviceType {
1091f4877a4S曾俊         DeviceType::Pci
1101f4877a4S曾俊     }
1111f4877a4S曾俊 
id_table(&self) -> IdTable1121f4877a4S曾俊     fn id_table(&self) -> IdTable {
1131f4877a4S曾俊         IdTable::new("testPci".to_string(), None)
1141f4877a4S曾俊     }
1151f4877a4S曾俊 
can_match(&self) -> bool1161f4877a4S曾俊     fn can_match(&self) -> bool {
1171f4877a4S曾俊         true
1181f4877a4S曾俊     }
1191f4877a4S曾俊 
is_dead(&self) -> bool1201f4877a4S曾俊     fn is_dead(&self) -> bool {
1211f4877a4S曾俊         false
1221f4877a4S曾俊     }
1231f4877a4S曾俊 
set_bus(&self, bus: Option<Weak<dyn Bus>>)1241f4877a4S曾俊     fn set_bus(&self, bus: Option<Weak<dyn Bus>>) {
125*28fe4ad2S黄铭涛         self.inner.write().device_common.bus = bus;
1261f4877a4S曾俊     }
1271f4877a4S曾俊 
set_can_match(&self, _can_match: bool)1281f4877a4S曾俊     fn set_can_match(&self, _can_match: bool) {}
1291f4877a4S曾俊 
set_class(&self, class: Option<Weak<dyn Class>>)1301f4877a4S曾俊     fn set_class(&self, class: Option<Weak<dyn Class>>) {
131*28fe4ad2S黄铭涛         self.inner.write().device_common.class = class;
1321f4877a4S曾俊     }
1331f4877a4S曾俊 
set_driver(&self, driver: Option<Weak<dyn Driver>>)1341f4877a4S曾俊     fn set_driver(&self, driver: Option<Weak<dyn Driver>>) {
135*28fe4ad2S黄铭涛         self.inner.write().device_common.driver = driver
1361f4877a4S曾俊     }
1371f4877a4S曾俊 
state_synced(&self) -> bool1381f4877a4S曾俊     fn state_synced(&self) -> bool {
1391f4877a4S曾俊         true
1401f4877a4S曾俊     }
141*28fe4ad2S黄铭涛 
dev_parent(&self) -> Option<Weak<dyn Device>>142*28fe4ad2S黄铭涛     fn dev_parent(&self) -> Option<Weak<dyn Device>> {
143*28fe4ad2S黄铭涛         self.inner.write().device_common.parent.clone()
144*28fe4ad2S黄铭涛     }
145*28fe4ad2S黄铭涛 
set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>)146*28fe4ad2S黄铭涛     fn set_dev_parent(&self, dev_parent: Option<Weak<dyn Device>>) {
147*28fe4ad2S黄铭涛         self.inner.write().device_common.parent = dev_parent;
148*28fe4ad2S黄铭涛     }
1491f4877a4S曾俊 }
1501f4877a4S曾俊 
1511f4877a4S曾俊 impl KObject for PciGeneralDevice {
as_any_ref(&self) -> &dyn Any1521f4877a4S曾俊     fn as_any_ref(&self) -> &dyn Any {
1531f4877a4S曾俊         self
1541f4877a4S曾俊     }
1551f4877a4S曾俊 
set_inode(&self, inode: Option<Arc<KernFSInode>>)1561f4877a4S曾俊     fn set_inode(&self, inode: Option<Arc<KernFSInode>>) {
157*28fe4ad2S黄铭涛         self.inner.write().kobject_common.kern_inode = inode;
1581f4877a4S曾俊     }
1591f4877a4S曾俊 
inode(&self) -> Option<Arc<KernFSInode>>1601f4877a4S曾俊     fn inode(&self) -> Option<Arc<KernFSInode>> {
161*28fe4ad2S黄铭涛         self.inner.read().kobject_common.kern_inode.clone()
1621f4877a4S曾俊     }
1631f4877a4S曾俊 
parent(&self) -> Option<Weak<dyn KObject>>1641f4877a4S曾俊     fn parent(&self) -> Option<Weak<dyn KObject>> {
165*28fe4ad2S黄铭涛         self.inner.read().kobject_common.parent.clone()
1661f4877a4S曾俊     }
1671f4877a4S曾俊 
set_parent(&self, parent: Option<Weak<dyn KObject>>)1681f4877a4S曾俊     fn set_parent(&self, parent: Option<Weak<dyn KObject>>) {
169*28fe4ad2S黄铭涛         self.inner.write().kobject_common.parent = parent;
1701f4877a4S曾俊     }
1711f4877a4S曾俊 
kset(&self) -> Option<Arc<KSet>>1721f4877a4S曾俊     fn kset(&self) -> Option<Arc<KSet>> {
173*28fe4ad2S黄铭涛         self.inner.read().kobject_common.kset.clone()
1741f4877a4S曾俊     }
1751f4877a4S曾俊 
set_kset(&self, kset: Option<Arc<KSet>>)1761f4877a4S曾俊     fn set_kset(&self, kset: Option<Arc<KSet>>) {
177*28fe4ad2S黄铭涛         self.inner.write().kobject_common.kset = kset;
1781f4877a4S曾俊     }
1791f4877a4S曾俊 
kobj_type(&self) -> Option<&'static dyn KObjType>1801f4877a4S曾俊     fn kobj_type(&self) -> Option<&'static dyn KObjType> {
181*28fe4ad2S黄铭涛         self.inner.read().kobject_common.kobj_type
1821f4877a4S曾俊     }
1831f4877a4S曾俊 
set_kobj_type(&self, ktype: Option<&'static dyn KObjType>)1841f4877a4S曾俊     fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) {
185*28fe4ad2S黄铭涛         self.inner.write().kobject_common.kobj_type = ktype;
1861f4877a4S曾俊     }
1871f4877a4S曾俊 
name(&self) -> String1881f4877a4S曾俊     fn name(&self) -> String {
189*28fe4ad2S黄铭涛         self.inner.read().name.clone().unwrap()
1901f4877a4S曾俊     }
1911f4877a4S曾俊 
set_name(&self, name: String)1921f4877a4S曾俊     fn set_name(&self, name: String) {
193*28fe4ad2S黄铭涛         self.inner.write().name = Some(name);
1941f4877a4S曾俊     }
1951f4877a4S曾俊 
kobj_state(&self) -> RwLockReadGuard<KObjectState>1961f4877a4S曾俊     fn kobj_state(&self) -> RwLockReadGuard<KObjectState> {
1971f4877a4S曾俊         self.kobj_state.read()
1981f4877a4S曾俊     }
1991f4877a4S曾俊 
kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState>2001f4877a4S曾俊     fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> {
2011f4877a4S曾俊         self.kobj_state.write()
2021f4877a4S曾俊     }
2031f4877a4S曾俊 
set_kobj_state(&self, state: KObjectState)2041f4877a4S曾俊     fn set_kobj_state(&self, state: KObjectState) {
2051f4877a4S曾俊         *self.kobj_state.write() = state;
2061f4877a4S曾俊     }
2071f4877a4S曾俊 }
208