Skip to content

Commit f65a321

Browse files
fujitaDanilo Krummrich
authored andcommitted
rust: net::phy represent DeviceId as transparent wrapper over mdio_device_id
Refactor the DeviceId struct to be a #[repr(transparent)] wrapper around the C struct bindings::mdio_device_id. This refactoring is a preparation for enabling the PHY abstractions to use the RawDeviceId trait. Acked-by: Jakub Kicinski <kuba@kernel.org> Reviewed-by: Trevor Gross <tmgross@umich.edu> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Link: https://lore.kernel.org/r/20250711040947.1252162-3-fujita.tomonori@gmail.com Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent 8d84b32 commit f65a321

1 file changed

Lines changed: 28 additions & 27 deletions

File tree

rust/kernel/net/phy.rs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ pub const fn create_phy_driver<T: Driver>() -> DriverVTable {
507507
DriverVTable(Opaque::new(bindings::phy_driver {
508508
name: T::NAME.as_char_ptr().cast_mut(),
509509
flags: T::FLAGS,
510-
phy_id: T::PHY_DEVICE_ID.id,
510+
phy_id: T::PHY_DEVICE_ID.id(),
511511
phy_id_mask: T::PHY_DEVICE_ID.mask_as_int(),
512512
soft_reset: if T::HAS_SOFT_RESET {
513513
Some(Adapter::<T>::soft_reset_callback)
@@ -691,61 +691,62 @@ impl Drop for Registration {
691691
///
692692
/// Represents the kernel's `struct mdio_device_id`. This is used to find an appropriate
693693
/// PHY driver.
694-
pub struct DeviceId {
695-
id: u32,
696-
mask: DeviceMask,
697-
}
694+
#[repr(transparent)]
695+
#[derive(Clone, Copy)]
696+
pub struct DeviceId(bindings::mdio_device_id);
698697

699698
impl DeviceId {
700699
/// Creates a new instance with the exact match mask.
701700
pub const fn new_with_exact_mask(id: u32) -> Self {
702-
DeviceId {
703-
id,
704-
mask: DeviceMask::Exact,
705-
}
701+
Self(bindings::mdio_device_id {
702+
phy_id: id,
703+
phy_id_mask: DeviceMask::Exact.as_int(),
704+
})
706705
}
707706

708707
/// Creates a new instance with the model match mask.
709708
pub const fn new_with_model_mask(id: u32) -> Self {
710-
DeviceId {
711-
id,
712-
mask: DeviceMask::Model,
713-
}
709+
Self(bindings::mdio_device_id {
710+
phy_id: id,
711+
phy_id_mask: DeviceMask::Model.as_int(),
712+
})
714713
}
715714

716715
/// Creates a new instance with the vendor match mask.
717716
pub const fn new_with_vendor_mask(id: u32) -> Self {
718-
DeviceId {
719-
id,
720-
mask: DeviceMask::Vendor,
721-
}
717+
Self(bindings::mdio_device_id {
718+
phy_id: id,
719+
phy_id_mask: DeviceMask::Vendor.as_int(),
720+
})
722721
}
723722

724723
/// Creates a new instance with a custom match mask.
725724
pub const fn new_with_custom_mask(id: u32, mask: u32) -> Self {
726-
DeviceId {
727-
id,
728-
mask: DeviceMask::Custom(mask),
729-
}
725+
Self(bindings::mdio_device_id {
726+
phy_id: id,
727+
phy_id_mask: DeviceMask::Custom(mask).as_int(),
728+
})
730729
}
731730

732731
/// Creates a new instance from [`Driver`].
733732
pub const fn new_with_driver<T: Driver>() -> Self {
734733
T::PHY_DEVICE_ID
735734
}
736735

737-
/// Get a `mask` as u32.
736+
/// Get the MDIO device's PHY ID.
737+
pub const fn id(&self) -> u32 {
738+
self.0.phy_id
739+
}
740+
741+
/// Get the MDIO device's match mask.
738742
pub const fn mask_as_int(&self) -> u32 {
739-
self.mask.as_int()
743+
self.0.phy_id_mask
740744
}
741745

742746
// macro use only
743747
#[doc(hidden)]
744748
pub const fn mdio_device_id(&self) -> bindings::mdio_device_id {
745-
bindings::mdio_device_id {
746-
phy_id: self.id,
747-
phy_id_mask: self.mask.as_int(),
748-
}
749+
self.0
749750
}
750751
}
751752

0 commit comments

Comments
 (0)