xref: /DragonOS/kernel/src/driver/pci/attr.rs (revision 2b7818e80e00fcfe4d03533f587cc125ea5e4bec)
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 {
21     fn name(&self) -> Option<&str> {
22         None
23     }
24 
25     fn attrs(&self) -> &[&'static dyn Attribute] {
26         &[&Vendor, &DeviceID, &SubsystemVendor, &SubsystemDevice]
27     }
28 
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 {
42     fn mode(&self) -> ModeType {
43         SYSFS_ATTR_MODE_RO
44     }
45 
46     fn name(&self) -> &str {
47         "vendor"
48     }
49 
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 
60     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
61         todo!()
62     }
63 
64     fn support(&self) -> SysFSOpsSupport {
65         SysFSOpsSupport::ATTR_SHOW
66     }
67 }
68 
69 #[derive(Debug)]
70 pub struct DeviceID;
71 
72 impl Attribute for DeviceID {
73     fn mode(&self) -> ModeType {
74         SYSFS_ATTR_MODE_RO
75     }
76 
77     fn name(&self) -> &str {
78         "device"
79     }
80 
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 
91     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
92         todo!()
93     }
94 
95     fn support(&self) -> SysFSOpsSupport {
96         SysFSOpsSupport::ATTR_SHOW
97     }
98 }
99 
100 #[derive(Debug)]
101 pub struct SubsystemVendor;
102 
103 impl Attribute for SubsystemVendor {
104     fn mode(&self) -> ModeType {
105         SYSFS_ATTR_MODE_RO
106     }
107 
108     fn name(&self) -> &str {
109         "subsystem_vendor"
110     }
111 
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 
122     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
123         todo!()
124     }
125 
126     fn support(&self) -> SysFSOpsSupport {
127         SysFSOpsSupport::ATTR_SHOW
128     }
129 }
130 
131 #[derive(Debug)]
132 pub struct SubsystemDevice;
133 
134 impl Attribute for SubsystemDevice {
135     fn mode(&self) -> ModeType {
136         SYSFS_ATTR_MODE_RO
137     }
138 
139     fn name(&self) -> &str {
140         "subsystem_device"
141     }
142 
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 
153     fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
154         todo!()
155     }
156 
157     fn support(&self) -> SysFSOpsSupport {
158         SysFSOpsSupport::ATTR_SHOW
159     }
160 }
161