From: FUJITA Tomonori <fujita.tomonori@gmail.com>
To: alex.gaynor@gmail.com, dakr@kernel.org,
gregkh@linuxfoundation.org, ojeda@kernel.org, rafael@kernel.org,
robh@kernel.org, saravanak@google.com
Cc: a.hindborg@kernel.org, aliceryhl@google.com, bhelgaas@google.com,
bjorn3_gh@protonmail.com, boqun.feng@gmail.com,
david.m.ertman@intel.com, devicetree@vger.kernel.org,
gary@garyguo.net, ira.weiny@intel.com, kwilczynski@kernel.org,
leon@kernel.org, linux-kernel@vger.kernel.org,
linux-pci@vger.kernel.org, lossin@kernel.org,
netdev@vger.kernel.org, rust-for-linux@vger.kernel.org,
tmgross@umich.edu
Subject: [PATCH v1 2/3] rust: net::phy Represent DeviceId as transparent wrapper over mdio_device_id
Date: Mon, 23 Jun 2025 15:09:50 +0900 [thread overview]
Message-ID: <20250623060951.118564-3-fujita.tomonori@gmail.com> (raw)
In-Reply-To: <20250623060951.118564-1-fujita.tomonori@gmail.com>
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 device_id trait.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
rust/kernel/net/phy.rs | 53 +++++++++++++++++++++---------------------
1 file changed, 27 insertions(+), 26 deletions(-)
diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
index 65ac4d59ad77..940972ffadae 100644
--- a/rust/kernel/net/phy.rs
+++ b/rust/kernel/net/phy.rs
@@ -507,7 +507,7 @@ pub const fn create_phy_driver<T: Driver>() -> DriverVTable {
DriverVTable(Opaque::new(bindings::phy_driver {
name: T::NAME.as_char_ptr().cast_mut(),
flags: T::FLAGS,
- phy_id: T::PHY_DEVICE_ID.id,
+ phy_id: T::PHY_DEVICE_ID.id(),
phy_id_mask: T::PHY_DEVICE_ID.mask_as_int(),
soft_reset: if T::HAS_SOFT_RESET {
Some(Adapter::<T>::soft_reset_callback)
@@ -691,42 +691,41 @@ fn drop(&mut self) {
///
/// Represents the kernel's `struct mdio_device_id`. This is used to find an appropriate
/// PHY driver.
-pub struct DeviceId {
- id: u32,
- mask: DeviceMask,
-}
+#[repr(transparent)]
+#[derive(Clone, Copy)]
+pub struct DeviceId(bindings::mdio_device_id);
impl DeviceId {
/// Creates a new instance with the exact match mask.
pub const fn new_with_exact_mask(id: u32) -> Self {
- DeviceId {
- id,
- mask: DeviceMask::Exact,
- }
+ Self(bindings::mdio_device_id {
+ phy_id: id,
+ phy_id_mask: DeviceMask::Exact.as_int(),
+ })
}
/// Creates a new instance with the model match mask.
pub const fn new_with_model_mask(id: u32) -> Self {
- DeviceId {
- id,
- mask: DeviceMask::Model,
- }
+ Self(bindings::mdio_device_id {
+ phy_id: id,
+ phy_id_mask: DeviceMask::Model.as_int(),
+ })
}
/// Creates a new instance with the vendor match mask.
pub const fn new_with_vendor_mask(id: u32) -> Self {
- DeviceId {
- id,
- mask: DeviceMask::Vendor,
- }
+ Self(bindings::mdio_device_id {
+ phy_id: id,
+ phy_id_mask: DeviceMask::Vendor.as_int(),
+ })
}
/// Creates a new instance with a custom match mask.
pub const fn new_with_custom_mask(id: u32, mask: u32) -> Self {
- DeviceId {
- id,
- mask: DeviceMask::Custom(mask),
- }
+ Self(bindings::mdio_device_id {
+ phy_id: id,
+ phy_id_mask: DeviceMask::Custom(mask).as_int(),
+ })
}
/// Creates a new instance from [`Driver`].
@@ -734,18 +733,20 @@ pub const fn new_with_driver<T: Driver>() -> Self {
T::PHY_DEVICE_ID
}
+ /// Get a `phy_id` as u32.
+ pub const fn id(&self) -> u32 {
+ self.0.phy_id
+ }
+
/// Get a `mask` as u32.
pub const fn mask_as_int(&self) -> u32 {
- self.mask.as_int()
+ self.0.phy_id_mask
}
// macro use only
#[doc(hidden)]
pub const fn mdio_device_id(&self) -> bindings::mdio_device_id {
- bindings::mdio_device_id {
- phy_id: self.id,
- phy_id_mask: self.mask.as_int(),
- }
+ self.0
}
}
--
2.43.0
next prev parent reply other threads:[~2025-06-23 6:10 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-23 6:09 [PATCH v1 0/3] rust: Build PHY device tables by using module_device_table macro FUJITA Tomonori
2025-06-23 6:09 ` [PATCH v1 1/3] rust: device_id: make DRIVER_DATA_OFFSET optional FUJITA Tomonori
2025-07-03 22:15 ` Danilo Krummrich
2025-07-03 23:41 ` FUJITA Tomonori
2025-07-04 0:19 ` Danilo Krummrich
2025-06-23 6:09 ` FUJITA Tomonori [this message]
2025-06-23 6:09 ` [PATCH v1 3/3] rust: net::phy Change module_phy_driver macro to use module_device_table macro FUJITA Tomonori
2025-06-23 9:21 ` Miguel Ojeda
2025-06-23 13:02 ` FUJITA Tomonori
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250623060951.118564-3-fujita.tomonori@gmail.com \
--to=fujita.tomonori@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bhelgaas@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=david.m.ertman@intel.com \
--cc=devicetree@vger.kernel.org \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=ira.weiny@intel.com \
--cc=kwilczynski@kernel.org \
--cc=leon@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=robh@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=saravanak@google.com \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).