Skip to content

Commit 9a8682f

Browse files
fujitaDanilo Krummrich
authored andcommitted
rust: net::phy Change module_phy_driver macro to use module_device_table macro
Change module_phy_driver macro to build device tables which are exported to userspace by using module_device_table macro. 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-4-fujita.tomonori@gmail.com Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent f65a321 commit 9a8682f

1 file changed

Lines changed: 24 additions & 27 deletions

File tree

rust/kernel/net/phy.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//!
77
//! C headers: [`include/linux/phy.h`](srctree/include/linux/phy.h).
88
9-
use crate::{error::*, prelude::*, types::Opaque};
9+
use crate::{device_id::RawDeviceId, error::*, prelude::*, types::Opaque};
1010
use core::{marker::PhantomData, ptr::addr_of_mut};
1111

1212
pub mod reg;
@@ -750,6 +750,12 @@ impl DeviceId {
750750
}
751751
}
752752

753+
// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct mdio_device_id`
754+
// and does not add additional invariants, so it's safe to transmute to `RawType`.
755+
unsafe impl RawDeviceId for DeviceId {
756+
type RawType = bindings::mdio_device_id;
757+
}
758+
753759
enum DeviceMask {
754760
Exact,
755761
Model,
@@ -850,19 +856,18 @@ impl DeviceMask {
850856
/// }
851857
/// };
852858
///
853-
/// const _DEVICE_TABLE: [::kernel::bindings::mdio_device_id; 2] = [
854-
/// ::kernel::bindings::mdio_device_id {
855-
/// phy_id: 0x00000001,
856-
/// phy_id_mask: 0xffffffff,
857-
/// },
858-
/// ::kernel::bindings::mdio_device_id {
859-
/// phy_id: 0,
860-
/// phy_id_mask: 0,
861-
/// },
862-
/// ];
863-
/// #[cfg(MODULE)]
864-
/// #[no_mangle]
865-
/// static __mod_device_table__mdio__phydev: [::kernel::bindings::mdio_device_id; 2] = _DEVICE_TABLE;
859+
/// const N: usize = 1;
860+
///
861+
/// const TABLE: ::kernel::device_id::IdArray<::kernel::net::phy::DeviceId, (), N> =
862+
/// ::kernel::device_id::IdArray::new_without_index([
863+
/// ::kernel::net::phy::DeviceId(
864+
/// ::kernel::bindings::mdio_device_id {
865+
/// phy_id: 0x00000001,
866+
/// phy_id_mask: 0xffffffff,
867+
/// }),
868+
/// ]);
869+
///
870+
/// ::kernel::module_device_table!("mdio", phydev, TABLE);
866871
/// ```
867872
#[macro_export]
868873
macro_rules! module_phy_driver {
@@ -873,20 +878,12 @@ macro_rules! module_phy_driver {
873878
};
874879

875880
(@device_table [$($dev:expr),+]) => {
876-
// SAFETY: C will not read off the end of this constant since the last element is zero.
877-
const _DEVICE_TABLE: [$crate::bindings::mdio_device_id;
878-
$crate::module_phy_driver!(@count_devices $($dev),+) + 1] = [
879-
$($dev.mdio_device_id()),+,
880-
$crate::bindings::mdio_device_id {
881-
phy_id: 0,
882-
phy_id_mask: 0
883-
}
884-
];
881+
const N: usize = $crate::module_phy_driver!(@count_devices $($dev),+);
882+
883+
const TABLE: $crate::device_id::IdArray<$crate::net::phy::DeviceId, (), N> =
884+
$crate::device_id::IdArray::new_without_index([ $(($dev,())),+, ]);
885885

886-
#[cfg(MODULE)]
887-
#[no_mangle]
888-
static __mod_device_table__mdio__phydev: [$crate::bindings::mdio_device_id;
889-
$crate::module_phy_driver!(@count_devices $($dev),+) + 1] = _DEVICE_TABLE;
886+
$crate::module_device_table!("mdio", phydev, TABLE);
890887
};
891888

892889
(drivers: [$($driver:ident),+ $(,)?], device_table: [$($dev:expr),+ $(,)?], $($f:tt)*) => {

0 commit comments

Comments
 (0)