Re: [PATCH v2 1/3] rust: core abstractions for network PHY drivers
From: FUJITA Tomonori <fujita.tomonori@gmail.com>
Date: 2023-10-08 07:49:10
Also in:
rust-for-linux
On Sun, 8 Oct 2023 02:19:46 -0400 Trevor Gross [off-list ref] wrote:
On Sat, Oct 7, 2023 at 6:33 PM FUJITA Tomonori [off-list ref] wrote:quoted
To create an internal type based on `name`, we need to unstringify `name`? I can't find a easy way to do it.I think you should just be able to do it with `paste!` macro_rules! module_phy_driver { (name: $name:expr) => { paste::paste! { #[allow(non_camel_case_types)] struct [<$name _ty>]; } } } // creates struct `demo_driver_ty` module_phy_driver! { name: "demo_driver" }
I realized that we don't need `name`. The name of struct doesn't
matter so I use `Module`. I tried to use `name` for the name of
device_table however the variable name of the table isn't embeded into
the module binary so it doesn't matter.
FYI, I use paste! but got the following error:
= help: message: `"__mod_mdio__\"rust_asix_phy\"_device_table"` is not a valid identifier
= note: this error originates in the macro `$crate::module_phy_driver` which comes from the expansion of the
macro `kernel::module_phy_driver` (in Nightly builds, run with -Z macro-backtrace for more info)
#[macro_export]
macro_rules! module_phy_driver {
(@replace_expr $_t:tt $sub:expr) => {$sub};
(@count_devices $($x:expr),*) => {
0usize $(+ $crate::module_phy_driver!(@replace_expr $x 1usize))*
};
(@device_table $name:tt, [$($dev:expr),+]) => {
::kernel::macros::paste! {
#[no_mangle]
static [<__mod_mdio__ $name _device_table>]: [
kernel::bindings::mdio_device_id;
$crate::module_phy_driver!(@count_devices $($dev),+) + 1
] = [
$(kernel::bindings::mdio_device_id {
phy_id: $dev.id,
phy_id_mask: $dev.mask_as_int()
}),+,
kernel::bindings::mdio_device_id {
phy_id: 0,
phy_id_mask: 0
}
];
}
};
(drivers: [$($driver:ident),+], device_table: [$($dev:expr),+], name: $name:tt, $($f:tt)*) => {
struct Module {
_reg: kernel::net::phy::Registration,
}
$crate::prelude::module! {
type: Module,
name: $name,
$($f)*
}
static mut DRIVERS: [
kernel::types::Opaque<kernel::bindings::phy_driver>;
$crate::module_phy_driver!(@count_devices $($driver),+)
] = [
$(kernel::net::phy::create_phy_driver::<$driver>()),+
];
impl kernel::Module for Module {
fn init(module: &'static ThisModule) -> Result<Self> {
// SAFETY: static `DRIVERS` array is used only in the C side.
let mut reg = unsafe { kernel::net::phy::Registration::register(module, &DRIVERS) }?;
Ok(Module {
_reg: reg,
})
}
}
$crate::module_phy_driver!(@device_table $name, [$($dev),+]);
}
}