attr.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. use alloc::sync::Arc;
  2. use intertrait::cast::CastArc;
  3. use log::warn;
  4. use system_error::SystemError;
  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. use super::device::PciDevice;
  15. #[derive(Debug)]
  16. pub struct BasicPciReadOnlyAttrs;
  17. impl AttributeGroup for BasicPciReadOnlyAttrs {
  18. fn name(&self) -> Option<&str> {
  19. None
  20. }
  21. fn attrs(&self) -> &[&'static dyn Attribute] {
  22. &[&Vendor, &DeviceID, &SubsystemVendor, &SubsystemDevice]
  23. }
  24. fn is_visible(
  25. &self,
  26. _kobj: Arc<dyn KObject>,
  27. attr: &'static dyn Attribute,
  28. ) -> Option<ModeType> {
  29. return Some(attr.mode());
  30. }
  31. }
  32. #[derive(Debug)]
  33. pub struct Vendor;
  34. impl Attribute for Vendor {
  35. fn mode(&self) -> ModeType {
  36. SYSFS_ATTR_MODE_RO
  37. }
  38. fn name(&self) -> &str {
  39. "vendor"
  40. }
  41. fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
  42. let dev = _kobj
  43. .cast::<dyn PciDevice>()
  44. .map_err(|e: Arc<dyn KObject>| {
  45. warn!("device:{:?} is not a pci device!", e);
  46. SystemError::EINVAL
  47. })?;
  48. return sysfs_emit_str(_buf, &format!("0x{:04x}", dev.vendor()));
  49. }
  50. fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
  51. todo!()
  52. }
  53. fn support(&self) -> SysFSOpsSupport {
  54. SysFSOpsSupport::ATTR_SHOW
  55. }
  56. }
  57. #[derive(Debug)]
  58. pub struct DeviceID;
  59. impl Attribute for DeviceID {
  60. fn mode(&self) -> ModeType {
  61. SYSFS_ATTR_MODE_RO
  62. }
  63. fn name(&self) -> &str {
  64. "device"
  65. }
  66. fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
  67. let dev = _kobj
  68. .cast::<dyn PciDevice>()
  69. .map_err(|e: Arc<dyn KObject>| {
  70. warn!("device:{:?} is not a pci device!", e);
  71. SystemError::EINVAL
  72. })?;
  73. return sysfs_emit_str(_buf, &format!("0x{:04x}", dev.device_id()));
  74. }
  75. fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
  76. todo!()
  77. }
  78. fn support(&self) -> SysFSOpsSupport {
  79. SysFSOpsSupport::ATTR_SHOW
  80. }
  81. }
  82. #[derive(Debug)]
  83. pub struct SubsystemVendor;
  84. impl Attribute for SubsystemVendor {
  85. fn mode(&self) -> ModeType {
  86. SYSFS_ATTR_MODE_RO
  87. }
  88. fn name(&self) -> &str {
  89. "subsystem_vendor"
  90. }
  91. fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
  92. let dev = _kobj
  93. .cast::<dyn PciDevice>()
  94. .map_err(|e: Arc<dyn KObject>| {
  95. warn!("device:{:?} is not a pci device!", e);
  96. SystemError::EINVAL
  97. })?;
  98. return sysfs_emit_str(_buf, &format!("0x{:04x}", dev.subsystem_vendor()));
  99. }
  100. fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
  101. todo!()
  102. }
  103. fn support(&self) -> SysFSOpsSupport {
  104. SysFSOpsSupport::ATTR_SHOW
  105. }
  106. }
  107. #[derive(Debug)]
  108. pub struct SubsystemDevice;
  109. impl Attribute for SubsystemDevice {
  110. fn mode(&self) -> ModeType {
  111. SYSFS_ATTR_MODE_RO
  112. }
  113. fn name(&self) -> &str {
  114. "subsystem_device"
  115. }
  116. fn show(&self, _kobj: Arc<dyn KObject>, _buf: &mut [u8]) -> Result<usize, SystemError> {
  117. let dev = _kobj
  118. .cast::<dyn PciDevice>()
  119. .map_err(|e: Arc<dyn KObject>| {
  120. warn!("device:{:?} is not a pci device!", e);
  121. SystemError::EINVAL
  122. })?;
  123. return sysfs_emit_str(_buf, &format!("0x{:04x}", dev.subsystem_device()));
  124. }
  125. fn store(&self, _kobj: Arc<dyn KObject>, _buf: &[u8]) -> Result<usize, SystemError> {
  126. todo!()
  127. }
  128. fn support(&self) -> SysFSOpsSupport {
  129. SysFSOpsSupport::ATTR_SHOW
  130. }
  131. }