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