[PATCH 1/4] rust: phy: add read-only device field accessors
From: Artem Lytkin <hidden>
Date: 2026-03-23 20:19:49
Also in:
rust-for-linux
Subsystem:
ethernet phy library [rust], rust, the rest · Maintainers:
FUJITA Tomonori, Miguel Ojeda, Linus Torvalds
Add getter methods for commonly needed PHY device fields:
- speed(): Returns the current link speed as i32, matching the
C struct field type (int speed).
- duplex(): Returns the current duplex mode as DuplexMode enum.
Only the setter (set_duplex) existed previously.
- interface(): Returns the PHY interface mode (RGMII, SGMII, etc.)
as a raw phy_interface_t value. A typed Rust enum is future work
since phy_interface_t has 40+ variants tied to DT bindings.
- irq(): Returns the PHY's IRQ number.
These accessors are needed by Rust PHY drivers that must inspect
hardware state, particularly during config_init and read_status
callbacks.
Signed-off-by: Artem Lytkin <redacted>
---
rust/kernel/net/phy.rs | 41 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
index 3ca99db5cccf2..646b2a78a2710 100644
--- a/rust/kernel/net/phy.rs
+++ b/rust/kernel/net/phy.rs@@ -179,6 +179,47 @@ pub fn set_duplex(&mut self, mode: DuplexMode) { unsafe { (*phydev).duplex = v as c_int }; } + /// Gets the current speed setting. + pub fn speed(&self) -> i32 { + let phydev = self.0.get(); + // SAFETY: The struct invariant ensures that we may access + // this field without additional synchronization. + unsafe { (*phydev).speed } + } + + /// Gets the current duplex mode. + pub fn duplex(&self) -> DuplexMode { + let phydev = self.0.get(); + // SAFETY: The struct invariant ensures that we may access + // this field without additional synchronization. + let v = unsafe { (*phydev).duplex }; + match v as u32 { + bindings::DUPLEX_FULL => DuplexMode::Full, + bindings::DUPLEX_HALF => DuplexMode::Half, + _ => DuplexMode::Unknown, + } + } + + /// Gets the PHY interface mode as a raw `phy_interface_t` value. + /// + /// Common values include `bindings::phy_interface_t_PHY_INTERFACE_MODE_RGMII`, + /// `bindings::phy_interface_t_PHY_INTERFACE_MODE_SGMII`, etc. + /// A typed Rust enum is planned for future work. + pub fn interface(&self) -> u32 { + let phydev = self.0.get(); + // SAFETY: The struct invariant ensures that we may access + // this field without additional synchronization. + unsafe { (*phydev).interface } + } + + /// Gets the PHY's IRQ number. + pub fn irq(&self) -> i32 { + let phydev = self.0.get(); + // SAFETY: The struct invariant ensures that we may access + // this field without additional synchronization. + unsafe { (*phydev).irq } + } + /// Reads a PHY register. // This function reads a hardware register and updates the stats so takes `&mut self`. pub fn read<R: reg::Register>(&mut self, reg: R) -> Result<u16> {
--
2.43.0