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 3/3] rust: net::phy Change module_phy_driver macro to use module_device_table macro
Date: Mon, 23 Jun 2025 15:09:51 +0900 [thread overview]
Message-ID: <20250623060951.118564-4-fujita.tomonori@gmail.com> (raw)
In-Reply-To: <20250623060951.118564-1-fujita.tomonori@gmail.com>
Change module_phy_driver macro to build device tables which are
exported to userspace by using module_device_table macro.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
rust/kernel/net/phy.rs | 56 ++++++++++++++++++++++--------------------
1 file changed, 29 insertions(+), 27 deletions(-)
diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
index 940972ffadae..4d797f7b79d2 100644
--- a/rust/kernel/net/phy.rs
+++ b/rust/kernel/net/phy.rs
@@ -6,7 +6,7 @@
//!
//! C headers: [`include/linux/phy.h`](srctree/include/linux/phy.h).
-use crate::{error::*, prelude::*, types::Opaque};
+use crate::{device_id::RawDeviceId, error::*, prelude::*, types::Opaque};
use core::{marker::PhantomData, ptr::addr_of_mut};
pub mod reg;
@@ -750,6 +750,17 @@ pub const fn mdio_device_id(&self) -> bindings::mdio_device_id {
}
}
+// SAFETY: [`DeviceId`] is a `#[repr(transparent)` wrapper of `struct mdio_device_id`
+// and does not add additional invariants, so it's safe to transmute to `RawType`.
+unsafe impl RawDeviceId for DeviceId {
+ type RawType = bindings::mdio_device_id;
+
+ // This should never be called.
+ fn index(&self) -> usize {
+ 0
+ }
+}
+
enum DeviceMask {
Exact,
Model,
@@ -850,19 +861,18 @@ const fn as_int(&self) -> u32 {
/// }
/// };
///
-/// const _DEVICE_TABLE: [::kernel::bindings::mdio_device_id; 2] = [
-/// ::kernel::bindings::mdio_device_id {
-/// phy_id: 0x00000001,
-/// phy_id_mask: 0xffffffff,
-/// },
-/// ::kernel::bindings::mdio_device_id {
-/// phy_id: 0,
-/// phy_id_mask: 0,
-/// },
-/// ];
-/// #[cfg(MODULE)]
-/// #[no_mangle]
-/// static __mod_device_table__mdio__phydev: [::kernel::bindings::mdio_device_id; 2] = _DEVICE_TABLE;
+/// const N: usize = 1;
+///
+/// const TABLE: ::kernel::device_id::IdArray<::kernel::net::phy::DeviceId, (), N> =
+/// ::kernel::device_id::IdArray::new([
+/// ::kernel::net::phy::DeviceId(
+/// ::kernel::bindings::mdio_device_id {
+/// phy_id: 0x00000001,
+/// phy_id_mask: 0xffffffff,
+/// }),
+/// ]);
+///
+/// ::kernel::module_device_table!("mdio", phydev, TABLE);
/// ```
#[macro_export]
macro_rules! module_phy_driver {
@@ -873,20 +883,12 @@ macro_rules! module_phy_driver {
};
(@device_table [$($dev:expr),+]) => {
- // SAFETY: C will not read off the end of this constant since the last element is zero.
- const _DEVICE_TABLE: [$crate::bindings::mdio_device_id;
- $crate::module_phy_driver!(@count_devices $($dev),+) + 1] = [
- $($dev.mdio_device_id()),+,
- $crate::bindings::mdio_device_id {
- phy_id: 0,
- phy_id_mask: 0
- }
- ];
+ const N: usize = $crate::module_phy_driver!(@count_devices $($dev),+);
+
+ const TABLE: $crate::device_id::IdArray<$crate::net::phy::DeviceId, (), N> =
+ $crate::device_id::IdArray::new([ $(($dev,())),+, ]);
- #[cfg(MODULE)]
- #[no_mangle]
- static __mod_device_table__mdio__phydev: [$crate::bindings::mdio_device_id;
- $crate::module_phy_driver!(@count_devices $($dev),+) + 1] = _DEVICE_TABLE;
+ $crate::module_device_table!("mdio", phydev, TABLE);
};
(drivers: [$($driver:ident),+ $(,)?], device_table: [$($dev:expr),+ $(,)?], $($f:tt)*) => {
--
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 ` [PATCH v1 2/3] rust: net::phy Represent DeviceId as transparent wrapper over mdio_device_id FUJITA Tomonori
2025-06-23 6:09 ` FUJITA Tomonori [this message]
2025-06-23 9:21 ` [PATCH v1 3/3] rust: net::phy Change module_phy_driver macro to use module_device_table macro 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-4-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).