xref: /DragonOS/kernel/src/driver/pci/attr.rs (revision 2eab6dd743e94a86a685f1f3c01e599adf86610a)
11f4877a4S曾俊 use alloc::sync::Arc;
21f4877a4S曾俊 use intertrait::cast::CastArc;
3*2eab6dd7S曾俊 use log::warn;
41f4877a4S曾俊 use system_error::SystemError;
51f4877a4S曾俊 
61f4877a4S曾俊 use crate::{
71f4877a4S曾俊     driver::base::kobject::KObject,
81f4877a4S曾俊     filesystem::{
91f4877a4S曾俊         sysfs::{
101f4877a4S曾俊             file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport, SYSFS_ATTR_MODE_RO,
111f4877a4S曾俊         },
121f4877a4S曾俊         vfs::syscall::ModeType,
131f4877a4S曾俊     },
141f4877a4S曾俊 };
151f4877a4S曾俊 
161f4877a4S曾俊 use super::device::PciDevice;
171f4877a4S曾俊 #[derive(Debug)]
181f4877a4S曾俊 pub struct BasicPciReadOnlyAttrs;
191f4877a4S曾俊 
201f4877a4S曾俊 impl AttributeGroup for BasicPciReadOnlyAttrs {
name(&self) -> Option<&str>211f4877a4S曾俊     fn name(&self) -> Option<&str> {
221f4877a4S曾俊         None
231f4877a4S曾俊     }
241f4877a4S曾俊 
attrs(&self) -> &[&'static dyn Attribute]251f4877a4S曾俊     fn attrs(&self) -> &[&'static dyn Attribute] {
261f4877a4S曾俊         &[&Vendor, &DeviceID, &SubsystemVendor, &SubsystemDevice]
271f4877a4S曾俊     }
281f4877a4S曾俊 
is_visible( &self, _kobj: Arc<dyn KObject>, attr: &'static dyn Attribute, ) -> Option<ModeType>291f4877a4S曾俊     fn is_visible(
301f4877a4S曾俊         &self,
311f4877a4S曾俊         _kobj: Arc<dyn KObject>,
321f4877a4S曾俊         attr: &'static dyn Attribute,
331f4877a4S曾俊     ) -> Option<ModeType> {
341f4877a4S曾俊         return Some(attr.mode());
351f4877a4S曾俊     }
361f4877a4S曾俊 }
371f4877a4S曾俊 
381f4877a4S曾俊 #[derive(Debug)]
391f4877a4S曾俊 pub struct Vendor;
401f4877a4S曾俊 
411f4877a4S曾俊 impl Attribute for Vendor {
mode(&self) -> ModeType421f4877a4S曾俊     fn mode(&self) -> ModeType {
431f4877a4S曾俊         SYSFS_ATTR_MODE_RO
441f4877a4S曾俊     }
451f4877a4S曾俊 
name(&self) -> &str461f4877a4S曾俊     fn name(&self) -> &str {
471f4877a4S曾俊         "vendor"
481f4877a4S曾俊     }
491f4877a4S曾俊 
show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError>501f4877a4S曾俊     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
511f4877a4S曾俊         let dev = _kobj
521f4877a4S曾俊             .cast::<dyn PciDevice>()
531f4877a4S曾俊             .map_err(|e: Arc<dyn KObject>| {
54*2eab6dd7S曾俊                 warn!("device:{:?} is not a pci device!", e);
551f4877a4S曾俊                 SystemError::EINVAL
561f4877a4S曾俊             })?;
571f4877a4S曾俊         return sysfs_emit_str(_buf, &format!("0x{:04x}", dev.vendor()));
581f4877a4S曾俊     }
591f4877a4S曾俊 
store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>601f4877a4S曾俊     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
611f4877a4S曾俊         todo!()
621f4877a4S曾俊     }
631f4877a4S曾俊 
support(&self) -> SysFSOpsSupport641f4877a4S曾俊     fn support(&self) -> SysFSOpsSupport {
651f4877a4S曾俊         SysFSOpsSupport::ATTR_SHOW
661f4877a4S曾俊     }
671f4877a4S曾俊 }
681f4877a4S曾俊 
691f4877a4S曾俊 #[derive(Debug)]
701f4877a4S曾俊 pub struct DeviceID;
711f4877a4S曾俊 
721f4877a4S曾俊 impl Attribute for DeviceID {
mode(&self) -> ModeType731f4877a4S曾俊     fn mode(&self) -> ModeType {
741f4877a4S曾俊         SYSFS_ATTR_MODE_RO
751f4877a4S曾俊     }
761f4877a4S曾俊 
name(&self) -> &str771f4877a4S曾俊     fn name(&self) -> &str {
781f4877a4S曾俊         "device"
791f4877a4S曾俊     }
801f4877a4S曾俊 
show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError>811f4877a4S曾俊     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
821f4877a4S曾俊         let dev = _kobj
831f4877a4S曾俊             .cast::<dyn PciDevice>()
841f4877a4S曾俊             .map_err(|e: Arc<dyn KObject>| {
85*2eab6dd7S曾俊                 warn!("device:{:?} is not a pci device!", e);
861f4877a4S曾俊                 SystemError::EINVAL
871f4877a4S曾俊             })?;
881f4877a4S曾俊         return sysfs_emit_str(_buf, &format!("0x{:04x}", dev.device_id()));
891f4877a4S曾俊     }
901f4877a4S曾俊 
store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>911f4877a4S曾俊     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
921f4877a4S曾俊         todo!()
931f4877a4S曾俊     }
941f4877a4S曾俊 
support(&self) -> SysFSOpsSupport951f4877a4S曾俊     fn support(&self) -> SysFSOpsSupport {
961f4877a4S曾俊         SysFSOpsSupport::ATTR_SHOW
971f4877a4S曾俊     }
981f4877a4S曾俊 }
991f4877a4S曾俊 
1001f4877a4S曾俊 #[derive(Debug)]
1011f4877a4S曾俊 pub struct SubsystemVendor;
1021f4877a4S曾俊 
1031f4877a4S曾俊 impl Attribute for SubsystemVendor {
mode(&self) -> ModeType1041f4877a4S曾俊     fn mode(&self) -> ModeType {
1051f4877a4S曾俊         SYSFS_ATTR_MODE_RO
1061f4877a4S曾俊     }
1071f4877a4S曾俊 
name(&self) -> &str1081f4877a4S曾俊     fn name(&self) -> &str {
1091f4877a4S曾俊         "subsystem_vendor"
1101f4877a4S曾俊     }
1111f4877a4S曾俊 
show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError>1121f4877a4S曾俊     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
1131f4877a4S曾俊         let dev = _kobj
1141f4877a4S曾俊             .cast::<dyn PciDevice>()
1151f4877a4S曾俊             .map_err(|e: Arc<dyn KObject>| {
116*2eab6dd7S曾俊                 warn!("device:{:?} is not a pci device!", e);
1171f4877a4S曾俊                 SystemError::EINVAL
1181f4877a4S曾俊             })?;
1191f4877a4S曾俊         return sysfs_emit_str(_buf, &format!("0x{:04x}", dev.subsystem_vendor()));
1201f4877a4S曾俊     }
1211f4877a4S曾俊 
store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>1221f4877a4S曾俊     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
1231f4877a4S曾俊         todo!()
1241f4877a4S曾俊     }
1251f4877a4S曾俊 
support(&self) -> SysFSOpsSupport1261f4877a4S曾俊     fn support(&self) -> SysFSOpsSupport {
1271f4877a4S曾俊         SysFSOpsSupport::ATTR_SHOW
1281f4877a4S曾俊     }
1291f4877a4S曾俊 }
1301f4877a4S曾俊 
1311f4877a4S曾俊 #[derive(Debug)]
1321f4877a4S曾俊 pub struct SubsystemDevice;
1331f4877a4S曾俊 
1341f4877a4S曾俊 impl Attribute for SubsystemDevice {
mode(&self) -> ModeType1351f4877a4S曾俊     fn mode(&self) -> ModeType {
1361f4877a4S曾俊         SYSFS_ATTR_MODE_RO
1371f4877a4S曾俊     }
1381f4877a4S曾俊 
name(&self) -> &str1391f4877a4S曾俊     fn name(&self) -> &str {
1401f4877a4S曾俊         "subsystem_device"
1411f4877a4S曾俊     }
1421f4877a4S曾俊 
show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError>1431f4877a4S曾俊     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
1441f4877a4S曾俊         let dev = _kobj
1451f4877a4S曾俊             .cast::<dyn PciDevice>()
1461f4877a4S曾俊             .map_err(|e: Arc<dyn KObject>| {
147*2eab6dd7S曾俊                 warn!("device:{:?} is not a pci device!", e);
1481f4877a4S曾俊                 SystemError::EINVAL
1491f4877a4S曾俊             })?;
1501f4877a4S曾俊         return sysfs_emit_str(_buf, &format!("0x{:04x}", dev.subsystem_device()));
1511f4877a4S曾俊     }
1521f4877a4S曾俊 
store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>1531f4877a4S曾俊     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
1541f4877a4S曾俊         todo!()
1551f4877a4S曾俊     }
1561f4877a4S曾俊 
support(&self) -> SysFSOpsSupport1571f4877a4S曾俊     fn support(&self) -> SysFSOpsSupport {
1581f4877a4S曾俊         SysFSOpsSupport::ATTR_SHOW
1591f4877a4S曾俊     }
1601f4877a4S曾俊 }
161