xref: /DragonOS/kernel/src/driver/pci/attr.rs (revision 2eab6dd743e94a86a685f1f3c01e599adf86610a)
1 use alloc::sync::Arc;
2 use intertrait::cast::CastArc;
3 use log::warn;
4 use system_error::SystemError;
5 
6 use crate::{
7     driver::base::kobject::KObject,
8     filesystem::{
9         sysfs::{
10             file::sysfs_emit_str, Attribute, AttributeGroup, SysFSOpsSupport, SYSFS_ATTR_MODE_RO,
11         },
12         vfs::syscall::ModeType,
13     },
14 };
15 
16 use super::device::PciDevice;
17 #[derive(Debug)]
18 pub struct BasicPciReadOnlyAttrs;
19 
20 impl AttributeGroup for BasicPciReadOnlyAttrs {
name(&self) -> Option<&str>21     fn name(&self) -> Option<&str> {
22         None
23     }
24 
attrs(&self) -> &[&'static dyn Attribute]25     fn attrs(&self) -> &[&'static dyn Attribute] {
26         &[&Vendor, &DeviceID, &SubsystemVendor, &SubsystemDevice]
27     }
28 
is_visible( &self, _kobj: Arc<dyn KObject>, attr: &'static dyn Attribute, ) -> Option<ModeType>29     fn is_visible(
30         &self,
31         _kobj: Arc<dyn KObject>,
32         attr: &'static dyn Attribute,
33     ) -> Option<ModeType> {
34         return Some(attr.mode());
35     }
36 }
37 
38 #[derive(Debug)]
39 pub struct Vendor;
40 
41 impl Attribute for Vendor {
mode(&self) -> ModeType42     fn mode(&self) -> ModeType {
43         SYSFS_ATTR_MODE_RO
44     }
45 
name(&self) -> &str46     fn name(&self) -> &str {
47         "vendor"
48     }
49 
show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError>50     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
51         let dev = _kobj
52             .cast::<dyn PciDevice>()
53             .map_err(|e: Arc<dyn KObject>| {
54                 warn!("device:{:?} is not a pci device!", e);
55                 SystemError::EINVAL
56             })?;
57         return sysfs_emit_str(_buf, &format!("0x{:04x}", dev.vendor()));
58     }
59 
store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>60     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
61         todo!()
62     }
63 
support(&self) -> SysFSOpsSupport64     fn support(&self) -> SysFSOpsSupport {
65         SysFSOpsSupport::ATTR_SHOW
66     }
67 }
68 
69 #[derive(Debug)]
70 pub struct DeviceID;
71 
72 impl Attribute for DeviceID {
mode(&self) -> ModeType73     fn mode(&self) -> ModeType {
74         SYSFS_ATTR_MODE_RO
75     }
76 
name(&self) -> &str77     fn name(&self) -> &str {
78         "device"
79     }
80 
show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError>81     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
82         let dev = _kobj
83             .cast::<dyn PciDevice>()
84             .map_err(|e: Arc<dyn KObject>| {
85                 warn!("device:{:?} is not a pci device!", e);
86                 SystemError::EINVAL
87             })?;
88         return sysfs_emit_str(_buf, &format!("0x{:04x}", dev.device_id()));
89     }
90 
store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>91     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
92         todo!()
93     }
94 
support(&self) -> SysFSOpsSupport95     fn support(&self) -> SysFSOpsSupport {
96         SysFSOpsSupport::ATTR_SHOW
97     }
98 }
99 
100 #[derive(Debug)]
101 pub struct SubsystemVendor;
102 
103 impl Attribute for SubsystemVendor {
mode(&self) -> ModeType104     fn mode(&self) -> ModeType {
105         SYSFS_ATTR_MODE_RO
106     }
107 
name(&self) -> &str108     fn name(&self) -> &str {
109         "subsystem_vendor"
110     }
111 
show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError>112     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
113         let dev = _kobj
114             .cast::<dyn PciDevice>()
115             .map_err(|e: Arc<dyn KObject>| {
116                 warn!("device:{:?} is not a pci device!", e);
117                 SystemError::EINVAL
118             })?;
119         return sysfs_emit_str(_buf, &format!("0x{:04x}", dev.subsystem_vendor()));
120     }
121 
store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>122     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
123         todo!()
124     }
125 
support(&self) -> SysFSOpsSupport126     fn support(&self) -> SysFSOpsSupport {
127         SysFSOpsSupport::ATTR_SHOW
128     }
129 }
130 
131 #[derive(Debug)]
132 pub struct SubsystemDevice;
133 
134 impl Attribute for SubsystemDevice {
mode(&self) -> ModeType135     fn mode(&self) -> ModeType {
136         SYSFS_ATTR_MODE_RO
137     }
138 
name(&self) -> &str139     fn name(&self) -> &str {
140         "subsystem_device"
141     }
142 
show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError>143     fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
144         let dev = _kobj
145             .cast::<dyn PciDevice>()
146             .map_err(|e: Arc<dyn KObject>| {
147                 warn!("device:{:?} is not a pci device!", e);
148                 SystemError::EINVAL
149             })?;
150         return sysfs_emit_str(_buf, &format!("0x{:04x}", dev.subsystem_device()));
151     }
152 
store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError>153     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
154         todo!()
155     }
156 
support(&self) -> SysFSOpsSupport157     fn support(&self) -> SysFSOpsSupport {
158         SysFSOpsSupport::ATTR_SHOW
159     }
160 }
161