From nobody Tue Oct 7 08:53:52 2025 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 39F891B87F2; Fri, 11 Jul 2025 04:10:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752207031; cv=none; b=AbObG2RpSVtEirbh/XR+aL61e6OUB7uSOtbim7DomPLQ1yxZNXMaP0xaCIXWCDs7Vi6Oe0hnQmf48/kF4liqouna0bKNCfv7E+t3GHTbELCQfo41yeATWDweZNnG9SFxcbB58JTmv6eLF7XxpE6xJx6UxunzofmrnwK4QgZG93w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752207031; c=relaxed/simple; bh=LyLDiZQiInijvva1oOsPfQzFCieHR8L+i2c7EPsTlwM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LCC9UEckok01ArTgxBq1AZIPBYExDxvQBMYkGLBhtP2K4CMeaoQ8JPGF4aRa66/RPyKIodF/Vbv+MmMqb8Aj9WPI8V7A1Gm7/EX34FClrE766xtrkxljv30uEIJ7xDm5Sdb6+2YvAFlpGMSfGW9F4wNPYI2iK89yguySyCzwjnE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=VuwhWvSf; arc=none smtp.client-ip=209.85.216.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VuwhWvSf" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-3122a63201bso1453485a91.0; Thu, 10 Jul 2025 21:10:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1752207028; x=1752811828; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ezT4Ms7PLFObqMVGkF8Ub6RgV5PSvQW8O2CmBLmTgTM=; b=VuwhWvSfh/QWeZvNIhFDWfc4ATKO9JRheX6SoztoRCGbe0EbzIosfiC53MALOzB0gd FArkdAm81w+x+/hWHkmlu8AwyY5P40BTVvfnzUeasulf0BSHee7WW+Pa72EbNnYRB9u6 wcBxF3s9jHtTqYx3eIKwBX2+r8auB/6jXWkmQ5/XohqHp7CmmSO+wqZfpu557Hsa73W8 58UuLC6rPhOSR4i0z+a2cAvnLWgViX8/uomTM9SV6GbD6BIqDgweSW9QmdAHUrGc9NxN tj1F45myDtJtvdattno+I+8kB4zhsJqSe6BUKWczG27kKo+Wg9UzCj6urSu1ri8kuxKK RHuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752207028; x=1752811828; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ezT4Ms7PLFObqMVGkF8Ub6RgV5PSvQW8O2CmBLmTgTM=; b=MjvX+gGfCMoTmxFapNepwBt3p9GECZf/OehZKBAhH7BHHp+GrpiqH7LadPBddp6GSx jCqvjnBA3KnT46EZPuYGEsyLC/pdT5KeqeSJytLXOtSJSUb9dmXLGP9yp+lLcU77VitH gOJHqDgMNxF77CK7cmOROM0eWYQraDfSZRRPAzsCL90oT1BUFfe9ps9h465tLqSuEGKa dUTUuTi4JX0aXOAyRphtzFrhnMyRP1t3n8FywqTueoy91eKxfQdsc3CrGxJvTDBlupqr uT2EPdNGdsk/BqXxwpubXfWsUgwzJwbrixyUesR7YNPkZhFdLIuWig+NUBkBBOuxk4Vk +KbA== X-Forwarded-Encrypted: i=1; AJvYcCUngiE8FdjCwP6LJiqiW9lWe/05hKmOP7tWwWmV1DYFAtgkRqPhj2Qn24kLe0GK2ep+asGJJpMt@vger.kernel.org, AJvYcCUtthiPg+qeorV/vGBd714HsG0QCq/ODYM9iOMfdw0nRtullMgwUcoE1wEnOhEx9B1kwmKeWUIHf3lPzQra@vger.kernel.org, AJvYcCUvItQI4F7hG3Fl6unw/+PXaKVQ+rRk9wrgc9F6YMJy/qVXCYdWZ+9v/RvvyZ1W4JMYOuC+s2mYTI8XWA==@vger.kernel.org, AJvYcCVV/nr/BBfIEAuzBhVO6xWKFTCySs05uJsYewhk3akquXKMbBXE9rM6iNUodAg65FSlgGKzq1KhighzCJFYKWU=@vger.kernel.org, AJvYcCWK6A2+TU1g0uOGR7LgumE8+uPtgs9U6Fx/wBJ9pRHYdBM3VrfXI1zDnv1WaH7kfdtGpewa0lcB8oIh@vger.kernel.org, AJvYcCXlYikCHfII0JDJ16fjwL1PjWk4s6NTkyTGXtLvvdapzw8S76P03ZH7dhd2x9UoPDln0d5IMcl3uHTd@vger.kernel.org X-Gm-Message-State: AOJu0YyY7qHCeiuxdBYfm139A9DqGxdLnVUmDeRVgvb7PSZpwzWa4mbt TfapjZQSoaFHkvahyk5XpCTlTo2nD/8hBcN95jpoJmRpjQu5Y6EZo8zvSZYpOb25 X-Gm-Gg: ASbGnct2TcPCa1wYyTjzbUgu4b/n0lnBgmRQBuGjnAJE00KPxi8ii2HdzbFEpqY/2XV OlzGQRhzGCPUrg3gI5L4Sq9aloefCHCkV4LboHIm1MANId6fukC5LDrdDS49RFwQRBjx7XHtKNT +UPvkQuvYGV3b6tKVEbP3hdUjFsQ7PsGI6TbPvKYswwnpCiNXEcM7ZzAm4E0lInpcB6iz9LkjBh Z9nhta9Z1bcZPK0+w18NMP0XtrxgXHFdBVpbJLZUdvEKxPs5ArliqU+Mgwt1dKatn9gQ/chbj91 SOSbudX7yZjfD3eU+LNU0n9vjc6P+pQqJ2ixCCyU31fdCfAzeN7wNa1KMY8Qv5QIJBbPPKUczJq lOCsKh8jYnFIkKIkHDnaQv3+sfbOMOWcjO9z4RPwnhEKe5ibrfkRnJuwsoFikLhLaW1owz5w= X-Google-Smtp-Source: AGHT+IEdbQo15DQTIuzDEdYJAct5+7W0ur1LE/Qkw/aUTLWSQ9nnhjN0SK07zlHORp5a8N2bEMvXFQ== X-Received: by 2002:a17:90b:3c12:b0:314:2bae:97d7 with SMTP id 98e67ed59e1d1-31c4f4cca77mr1809039a91.15.1752207028246; Thu, 10 Jul 2025 21:10:28 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b3bbe6f4fc8sm3902095a12.51.2025.07.10.21.10.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jul 2025 21:10:27 -0700 (PDT) From: FUJITA Tomonori To: alex.gaynor@gmail.com, dakr@kernel.org, gregkh@linuxfoundation.org, ojeda@kernel.org, rafael@kernel.org, robh@kernel.org, saravanak@google.com, tmgross@umich.edu 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, lenb@kernel.org, leon@kernel.org, linux-acpi@vger.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 Subject: [PATCH v4 1/3] rust: device_id: split out index support into a separate trait Date: Fri, 11 Jul 2025 13:09:45 +0900 Message-ID: <20250711040947.1252162-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250711040947.1252162-1-fujita.tomonori@gmail.com> References: <20250711040947.1252162-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a new trait `RawDeviceIdIndex`, which extends `RawDeviceId` to provide support for device ID types that include an index or context field (e.g., `driver_data`). This separates the concerns of layout compatibility and index-based data embedding, and allows `RawDeviceId` to be implemented for types that do not contain a `driver_data` field. Several such structures are defined in include/linux/mod_devicetable.h. Refactor `IdArray::new()` into a generic `build()` function, which takes an optional offset. Based on the presence of `RawDeviceIdIndex`, index writing is conditionally enabled. A new `new_without_index()` constructor is also provided for use cases where no index should be written. This refactoring is a preparation for enabling the PHY abstractions to use the RawDeviceId trait. The changes to acpi.rs and driver.rs were made by Danilo. Acked-by: Danilo Krummrich Reviewed-by: Trevor Gross Signed-off-by: FUJITA Tomonori Acked-by: Greg Kroah-Hartman --- rust/kernel/acpi.rs | 15 ++++--- rust/kernel/auxiliary.rs | 11 ++--- rust/kernel/device_id.rs | 91 +++++++++++++++++++++++++++++----------- rust/kernel/driver.rs | 8 +++- rust/kernel/of.rs | 15 ++++--- rust/kernel/pci.rs | 11 ++--- 6 files changed, 104 insertions(+), 47 deletions(-) diff --git a/rust/kernel/acpi.rs b/rust/kernel/acpi.rs index 2af4d4f92924..7ae317368b00 100644 --- a/rust/kernel/acpi.rs +++ b/rust/kernel/acpi.rs @@ -2,7 +2,11 @@ =20 //! Advanced Configuration and Power Interface abstractions. =20 -use crate::{bindings, device_id::RawDeviceId, prelude::*}; +use crate::{ + bindings, + device_id::{RawDeviceId, RawDeviceIdIndex}, + prelude::*, +}; =20 /// IdTable type for ACPI drivers. pub type IdTable =3D &'static dyn kernel::device_id::IdTable; @@ -12,13 +16,14 @@ #[derive(Clone, Copy)] pub struct DeviceId(bindings::acpi_device_id); =20 -// SAFETY: -// * `DeviceId` is a `#[repr(transparent)` wrapper of `struct acpi_device_= id` and does not add -// additional invariants, so it's safe to transmute to `RawType`. -// * `DRIVER_DATA_OFFSET` is the offset to the `data` field. +// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `acpi_device_= id` and does not add +// additional invariants, so it's safe to transmute to `RawType`. unsafe impl RawDeviceId for DeviceId { type RawType =3D bindings::acpi_device_id; +} =20 +// SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. +unsafe impl RawDeviceIdIndex for DeviceId { const DRIVER_DATA_OFFSET: usize =3D core::mem::offset_of!(bindings::ac= pi_device_id, driver_data); =20 fn index(&self) -> usize { diff --git a/rust/kernel/auxiliary.rs b/rust/kernel/auxiliary.rs index 2985673181b7..12a4d36dc452 100644 --- a/rust/kernel/auxiliary.rs +++ b/rust/kernel/auxiliary.rs @@ -6,7 +6,7 @@ =20 use crate::{ bindings, container_of, device, - device_id::RawDeviceId, + device_id::{RawDeviceId, RawDeviceIdIndex}, driver, error::{from_result, to_result, Result}, prelude::*, @@ -134,13 +134,14 @@ pub const fn new(modname: &'static CStr, name: &'stat= ic CStr) -> Self { } } =20 -// SAFETY: -// * `DeviceId` is a `#[repr(transparent)`] wrapper of `auxiliary_device_i= d` and does not add -// additional invariants, so it's safe to transmute to `RawType`. -// * `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. +// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `auxiliary_de= vice_id` and does not add +// additional invariants, so it's safe to transmute to `RawType`. unsafe impl RawDeviceId for DeviceId { type RawType =3D bindings::auxiliary_device_id; +} =20 +// SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. +unsafe impl RawDeviceIdIndex for DeviceId { const DRIVER_DATA_OFFSET: usize =3D core::mem::offset_of!(bindings::auxiliary_device_id, driver_data); =20 diff --git a/rust/kernel/device_id.rs b/rust/kernel/device_id.rs index 0a4eb56d98f2..8ed2c946144c 100644 --- a/rust/kernel/device_id.rs +++ b/rust/kernel/device_id.rs @@ -14,32 +14,41 @@ /// /// # Safety /// -/// Implementers must ensure that: -/// - `Self` is layout-compatible with [`RawDeviceId::RawType`]; i.e. it= 's safe to transmute to -/// `RawDeviceId`. +/// Implementers must ensure that `Self` is layout-compatible with [`RawDe= viceId::RawType`]; +/// i.e. it's safe to transmute to `RawDeviceId`. /// -/// This requirement is needed so `IdArray::new` can convert `Self` to= `RawType` when building -/// the ID table. +/// This requirement is needed so `IdArray::new` can convert `Self` to `Ra= wType` when building +/// the ID table. /// -/// Ideally, this should be achieved using a const function that does = conversion instead of -/// transmute; however, const trait functions relies on `const_trait_i= mpl` unstable feature, -/// which is broken/gone in Rust 1.73. -/// -/// - `DRIVER_DATA_OFFSET` is the offset of context/data field of the de= vice ID (usually named -/// `driver_data`) of the device ID, the field is suitable sized to wr= ite a `usize` value. -/// -/// Similar to the previous requirement, the data should ideally be ad= ded during `Self` to -/// `RawType` conversion, but there's currently no way to do it when u= sing traits in const. +/// Ideally, this should be achieved using a const function that does conv= ersion instead of +/// transmute; however, const trait functions relies on `const_trait_impl`= unstable feature, +/// which is broken/gone in Rust 1.73. pub unsafe trait RawDeviceId { /// The raw type that holds the device id. /// /// Id tables created from [`Self`] are going to hold this type in its= zero-terminated array. type RawType: Copy; +} =20 - /// The offset to the context/data field. +/// Extension trait for [`RawDeviceId`] for devices that embed an index or= context value. +/// +/// This is typically used when the device ID struct includes a field like= `driver_data` +/// that is used to store a pointer-sized value (e.g., an index or context= pointer). +/// +/// # Safety +/// +/// Implementers must ensure that `DRIVER_DATA_OFFSET` is the correct offs= et (in bytes) to +/// the context/data field (e.g., the `driver_data` field) within the raw = device ID structure. +/// This field must be correctly sized to hold a `usize`. +/// +/// Ideally, the data should be added during `Self` to `RawType` conversio= n, +/// but there's currently no way to do it when using traits in const. +pub unsafe trait RawDeviceIdIndex: RawDeviceId { + /// The offset (in bytes) to the context/data field in the raw device = ID. const DRIVER_DATA_OFFSET: usize; =20 - /// The index stored at `DRIVER_DATA_OFFSET` of the implementor of the= [`RawDeviceId`] trait. + /// The index stored at `DRIVER_DATA_OFFSET` of the implementor of the= [`RawDeviceIdIndex`] + /// trait. fn index(&self) -> usize; } =20 @@ -68,7 +77,15 @@ impl IdArray= { /// Creates a new instance of the array. /// /// The contents are derived from the given identifiers and context in= formation. - pub const fn new(ids: [(T, U); N]) -> Self { + /// + /// # Safety + /// + /// `data_offset` as `None` is always safe. + /// If `data_offset` is `Some(data_offset)`, then: + /// - `data_offset` must be the correct offset (in bytes) to the conte= xt/data field + /// (e.g., the `driver_data` field) within the raw device ID structu= re. + /// - The field at `data_offset` must be correctly sized to hold a `us= ize`. + const unsafe fn build(ids: [(T, U); N], data_offset: Option) ->= Self { let mut raw_ids =3D [const { MaybeUninit::::uninit() }= ; N]; let mut infos =3D [const { MaybeUninit::uninit() }; N]; =20 @@ -77,14 +94,16 @@ impl IdArray { // SAFETY: by the safety requirement of `RawDeviceId`, we're g= uaranteed that `T` is // layout-wise compatible with `RawType`. raw_ids[i] =3D unsafe { core::mem::transmute_copy(&ids[i].0) }; - // SAFETY: by the safety requirement of `RawDeviceId`, this wo= uld be effectively - // `raw_ids[i].driver_data =3D i;`. - unsafe { - raw_ids[i] - .as_mut_ptr() - .byte_offset(T::DRIVER_DATA_OFFSET as _) - .cast::() - .write(i); + if let Some(data_offset) =3D data_offset { + // SAFETY: by the safety requirement of this function, thi= s would be effectively + // `raw_ids[i].driver_data =3D i;`. + unsafe { + raw_ids[i] + .as_mut_ptr() + .byte_offset(data_offset as _) + .cast::() + .write(i); + } } =20 // SAFETY: this is effectively a move: `infos[i] =3D ids[i].1`= . We make a copy here but @@ -109,12 +128,34 @@ impl IdArray { } } =20 + /// Creates a new instance of the array without writing index values. + /// + /// The contents are derived from the given identifiers and context in= formation. + /// If the device implements [`RawDeviceIdIndex`], consider using [`Id= Array::new`] instead. + pub const fn new_without_index(ids: [(T, U); N]) -> Self { + // SAFETY: Calling `Self::build` with `offset =3D None` is always = safe, + // because no raw memory writes are performed in this case. + unsafe { Self::build(ids, None) } + } + /// Reference to the contained [`RawIdArray`]. pub const fn raw_ids(&self) -> &RawIdArray { &self.raw_ids } } =20 +impl IdArray { + /// Creates a new instance of the array. + /// + /// The contents are derived from the given identifiers and context in= formation. + pub const fn new(ids: [(T, U); N]) -> Self { + // SAFETY: by the safety requirement of `RawDeviceIdIndex`, + // `T::DRIVER_DATA_OFFSET` is guaranteed to be the correct offset = (in bytes) to + // a field within `T::RawType`. + unsafe { Self::build(ids, Some(T::DRIVER_DATA_OFFSET)) } + } +} + /// A device id table. /// /// This trait is only implemented by `IdArray`. diff --git a/rust/kernel/driver.rs b/rust/kernel/driver.rs index f8dd7593e8dc..a8f2675ba7a7 100644 --- a/rust/kernel/driver.rs +++ b/rust/kernel/driver.rs @@ -170,7 +170,7 @@ fn acpi_id_info(dev: &device::Device) -> Option<&'stati= c Self::IdInfo> { // and does not add additional invariants, so it's safe to= transmute. let id =3D unsafe { &*raw_id.cast::() }; =20 - Some(table.info(::index(id))) + Some(table.info(::index(id))) } } } @@ -204,7 +204,11 @@ fn of_id_info(dev: &device::Device) -> Option<&'static= Self::IdInfo> { // and does not add additional invariants, so it's safe to= transmute. let id =3D unsafe { &*raw_id.cast::() }; =20 - Some(table.info(::index(id))) + Some( + table.info(::index( + id, + )), + ) } } } diff --git a/rust/kernel/of.rs b/rust/kernel/of.rs index 52fe0c77b9a1..0888469bddb7 100644 --- a/rust/kernel/of.rs +++ b/rust/kernel/of.rs @@ -2,7 +2,11 @@ =20 //! Device Tree / Open Firmware abstractions. =20 -use crate::{bindings, device_id::RawDeviceId, prelude::*}; +use crate::{ + bindings, + device_id::{RawDeviceId, RawDeviceIdIndex}, + prelude::*, +}; =20 /// IdTable type for OF drivers. pub type IdTable =3D &'static dyn kernel::device_id::IdTable; @@ -12,13 +16,14 @@ #[derive(Clone, Copy)] pub struct DeviceId(bindings::of_device_id); =20 -// SAFETY: -// * `DeviceId` is a `#[repr(transparent)]` wrapper of `struct of_device_i= d` and does not add -// additional invariants, so it's safe to transmute to `RawType`. -// * `DRIVER_DATA_OFFSET` is the offset to the `data` field. +// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct of_de= vice_id` and +// does not add additional invariants, so it's safe to transmute to `RawTy= pe`. unsafe impl RawDeviceId for DeviceId { type RawType =3D bindings::of_device_id; +} =20 +// SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `data` field. +unsafe impl RawDeviceIdIndex for DeviceId { const DRIVER_DATA_OFFSET: usize =3D core::mem::offset_of!(bindings::of= _device_id, data); =20 fn index(&self) -> usize { diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 8b884e324dcf..ba9a5c6fc513 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -6,7 +6,7 @@ =20 use crate::{ bindings, container_of, device, - device_id::RawDeviceId, + device_id::{RawDeviceId, RawDeviceIdIndex}, devres::Devres, driver, error::{from_result, to_result, Result}, @@ -159,13 +159,14 @@ pub const fn from_class(class: u32, class_mask: u32) = -> Self { } } =20 -// SAFETY: -// * `DeviceId` is a `#[repr(transparent)]` wrapper of `pci_device_id` and= does not add -// additional invariants, so it's safe to transmute to `RawType`. -// * `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. +// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `pci_device_i= d` and does not add +// additional invariants, so it's safe to transmute to `RawType`. unsafe impl RawDeviceId for DeviceId { type RawType =3D bindings::pci_device_id; +} =20 +// SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. +unsafe impl RawDeviceIdIndex for DeviceId { const DRIVER_DATA_OFFSET: usize =3D core::mem::offset_of!(bindings::pc= i_device_id, driver_data); =20 fn index(&self) -> usize { --=20 2.43.0 From nobody Tue Oct 7 08:53:52 2025 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F86B1A7AF7; Fri, 11 Jul 2025 04:10:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752207035; cv=none; b=YrrP32zT7UNZIYTf98636QJ51D1ncDYMdiPruQjOzSa4F+Jmym3HA3bEkxZqCG8TUi9ShA3b6Hhqa2SFkHmvn4HMN542qN5oxl85FbHijQzZDsnX5suZxRdGPPKH4XUXA01m5NTO1Iru5fP5BNlJWPuZfOzwgdYSyrj1CP2YmqM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752207035; c=relaxed/simple; bh=NYdzn4bzZK/DX+l9u4RVlEZ2M7V5Ui5MJnFn0sT7qlM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rFrtB1K9TxHRl+Y5bjwEZEAM0KXCA6SVc6zYsp7F9d+wM7dwjqPsTg8HUj3K8FLUNZRN27Reeoosiy24sZ5yKaII5KyZxFIc+mlw7U8y1ZlzdMX3yJVjFiG1erj0cyqfJ8N0q0ulfMUHt7xozL8K6T7aHYLFh0tbf2DISEeaj0I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jK4GgdvS; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jK4GgdvS" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-7424ccbef4eso1693513b3a.2; Thu, 10 Jul 2025 21:10:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1752207034; x=1752811834; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HKQNRfLiwMNYz+RbhoybiNi7cvwiTAK0Zc8YMyuPU+Q=; b=jK4GgdvSkf/tNw7zRw6GA86m10OeGrhbHiBEvj1/cVo0GZhDeI22DUteBocrLJOcvh tnsFpS7+R+jx7vv7UwPheAIDfIA88yTZQEcByhmpbDvfWClwn48W+qpKTSbmS8WaQpqY KeQ+6aNYhoxVH4JyWKuPVKfukPfvLjh9b/esws/1JW2mN830rtyD+23jwOLhKVawgvC1 BhtrN+lWsE6rSItNN+KEWLVijZSvXvvCy3bUaK6gp7xAL/+f9cp/x6iFwWYFXlRYiu83 B983DrWTTSmUkdZkBycdzVBI5wKmseuWlMHYxjV0MOSBUxGeJ5POrYCfk72bR7STlP0W H+sQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752207034; x=1752811834; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HKQNRfLiwMNYz+RbhoybiNi7cvwiTAK0Zc8YMyuPU+Q=; b=jreZX6hdTfA9Mj54lePqvnY5C4ahbE0D3CMHCiUnIr4MuM16qf/TdruhcuNL1nqj+7 ErsNaJWq1+mnY2WMY75n0fCrEgkdtUIDZ1Q6HEYjoto4Qvid/8Yfx+sEywRBy4voFjFB L0GJ1InbldbDn8jMyVNiCcehgc6mT3B7qZGsAzV+0catnFcItqwt10X97FR5qzfGkfOi qymhehn/ICIUlmAHZDrwwXJ/h8L986Os/xbZ9pbYDc9gTO+l/b4mDUd5xt+2ZnTn/aNN CxyF9NYFuYel5dm3X1Hk2gQcM9Uc8/sCBPcTKCe7/rFNkvaDrFqVsz/gLlN2+9X1rkjT yBWA== X-Forwarded-Encrypted: i=1; AJvYcCUzOSs026ecCbvc2a8cg2mizhywBlTY7D/yXOIdXQ46nz+k0HueUel9EoOaHVRQp3vECSWX41ivM1e1@vger.kernel.org, AJvYcCVxXDE1O9dMaGkaApU7jTeHK48PnOrmtje4pNCGRtGwX4wnK8M3q34OK2YvX2c6q6aAE8dAduYM6BLYL3QExGg=@vger.kernel.org, AJvYcCWPrTp0tX8cIZZdgtA3eW2QVjG67qozaWZNKjI6477BUtcU6CWuQP0EIgHSg0sLhtVBebAaNO0KQWEu8w==@vger.kernel.org, AJvYcCWcawzP18uu9MaRC4WtOBzwDMaUJA+gt9O1Xl9dZD9NYCKhGMszamIyEXv4gs2QJJBQybl7kftxvjY7n4KC@vger.kernel.org, AJvYcCXTonxr2NiUa59LhzQAk2ohKqIKf8xAic2nG9cFt0ZD3dFERRh/f9wASNfG6X/dM7Y9yvC8HLwzaQ1V@vger.kernel.org, AJvYcCXhuND2pQmpJYQn6iXcRyJs7zL9J1+SNe1ddCRfI4EALNFXEFMbDL5+Et2J0H3g8Kd35BN5+MAY@vger.kernel.org X-Gm-Message-State: AOJu0YwJI6XPbmYAPDucy3m8eGWzZaVopUQ7YlJ65nC4C2uvN58KUHi/ gGX1NMxwhPHO0xF0OL0LwcPqiob/RvhC+x5DsQjdGf9KOEBq8m3ipbhn X-Gm-Gg: ASbGnctqS35on4R0JLaGOK8js6eVFpG2+qtMChLWPCt6vWPzglRn7MSqV92j62rZIgN NnnLwO57xf4TNO1XpZmpiuGgFGttRNMtaUuzps/UrjTuCwxOx52jKU4yje2sXV6I4PdkUSuCweX fG3ijfZsGYaDjxCtwNcCI+VFQvhodQakA7gvaKQuoDOpbg970yEuM9xB8l1wUIr140eFqGRzf7V xOYpGn94a8ASk/6VXlZurROuQGme9y6P2hIEydSIqE8ogn4FF8sYIWreTT+qPJaRY408HFdthiQ JdNov8+uZdlNRV7PBJuQZtbikZZGvgm5TbEudR34DBzi/MMuGXIHWdJedX8nWt/JivTWnI1V7Jq OvyJRjkTjj8T+oNXn+Aa2r1SQ5YXwsskvh9ML+JYs3z/erYTrAXrRSZhQ9O1Xqncq+oirQ10= X-Google-Smtp-Source: AGHT+IEbIdGkpA2W2Gy1wpx5qJPhxRbcqQj4wR5Pfptb5+eOqCXfrSh2swZyuKtThg35F2fRnphmFA== X-Received: by 2002:a05:6a21:3385:b0:220:78b9:f849 with SMTP id adf61e73a8af0-23136861dcfmr2820636637.24.1752207033613; Thu, 10 Jul 2025 21:10:33 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b3bbe6f4fc8sm3902095a12.51.2025.07.10.21.10.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jul 2025 21:10:33 -0700 (PDT) From: FUJITA Tomonori To: alex.gaynor@gmail.com, dakr@kernel.org, gregkh@linuxfoundation.org, ojeda@kernel.org, rafael@kernel.org, robh@kernel.org, saravanak@google.com, tmgross@umich.edu 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, lenb@kernel.org, leon@kernel.org, linux-acpi@vger.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 Subject: [PATCH v4 2/3] rust: net::phy represent DeviceId as transparent wrapper over mdio_device_id Date: Fri, 11 Jul 2025 13:09:46 +0900 Message-ID: <20250711040947.1252162-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250711040947.1252162-1-fujita.tomonori@gmail.com> References: <20250711040947.1252162-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Reviewed-by: Trevor Gross Signed-off-by: FUJITA Tomonori --- rust/kernel/net/phy.rs | 55 +++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs index 32ea43ece646..f44e8107cec4 100644 --- a/rust/kernel/net/phy.rs +++ b/rust/kernel/net/phy.rs @@ -507,7 +507,7 @@ pub const fn create_phy_driver() -> DriverVT= able { 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::::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); =20 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(), + }) } =20 /// 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(), + }) } =20 /// 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(), + }) } =20 /// 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(), + }) } =20 /// Creates a new instance from [`Driver`]. @@ -734,18 +733,20 @@ pub const fn new_with_driver() -> Self { T::PHY_DEVICE_ID } =20 - /// Get a `mask` as u32. + /// Get the MDIO device's PHY ID. + pub const fn id(&self) -> u32 { + self.0.phy_id + } + + /// Get the MDIO device's match mask. pub const fn mask_as_int(&self) -> u32 { - self.mask.as_int() + self.0.phy_id_mask } =20 // 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 } } =20 --=20 2.43.0 From nobody Tue Oct 7 08:53:52 2025 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D42F11A7AF7; Fri, 11 Jul 2025 04:10:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752207041; cv=none; b=KamQJi9NiM+ynDM/uBPyff7j87fVmz8vUbeygXqS9SxJ0C5VCCLNYZRYX746OvOaZq9N56eSyTnkCNLslEXNY4Mb6J/9JkZG7MZ00bNbbABo88F06iRk7fcLuaXiS6iUyrpGFEVZZm2pATCYCdl0Vw3RRxk91mDU3C8s5zstN+0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752207041; c=relaxed/simple; bh=hJB5UNZJFKPG2CgDqbDXrpAZl8wnBtsy+3T2LOvpdKs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LbinDTYNsuEu4Nxs6ztSUwcpTHE7FDAPLD7rtJpwUUDpLBu+O0YcqmbeD09G6Ed1ETb4zSqbwGHPVL+VU3Wm47FRBp6iuf4TZajXDub/T6shVSwXmfkRybZOPxKgSTK+3hMaMWzpLW9vTx1RtXL/C8WcQM7Ii3xt4ni/tdqtDIU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=E3L0xCES; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="E3L0xCES" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-3137c20213cso1656423a91.3; Thu, 10 Jul 2025 21:10:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1752207039; x=1752811839; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=FftY+H42oo78/iJj/bhTIQoeZQcrJ3tJuTl2e4g04Ts=; b=E3L0xCESOOmFpI7BP8S10EU/fXyujfybEG9/C4I0/hFHukm/wndUNjxiTWKx/OVLRF xAOGtS+zZdIAQUm0Ii56m9B4le4zGdpRM3xu6VsxL+tMHfmpzRCGj2a01K0uO+9kzoQ9 IFV5725v2tE4VsS9JW3/jPDHC5rQ/c1Vy7VU0hUnyaD4KY+2QUdH3Fzy6H5q0wr9Phff P7zD1OcGHejgP6SGaOEhH8Fapny+DrvMo7RBg1AfMJ723JnMANbxJCAb+BH03w8RPGr6 Y6uaMi3Sl5X/5CLo6d79j+FOVOVwL/ly/ZwpE7EbBFCPPS2EXrSXC254GO3wOqrKQaix nx9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752207039; x=1752811839; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FftY+H42oo78/iJj/bhTIQoeZQcrJ3tJuTl2e4g04Ts=; b=MhEMnZUJRyWW31LG8wjSm3KA0h3TNkaI/ek/bNtUQlV3KjMCAu3LTszlr+akt8fawN GLQ/Mgh8U4Xog1ZtH6mXhRgXXjYz3iVID8w7xoh9e+wAU2BdZXOTV2YSEA6LwzLLDrxV OvtNRHyh8BZb6DYOeRHkfbS41ZelUpCQmc5Xi4e+Z72V2Cm96dkEUAHAru57MQw+yU5C PUH6CncZc2oK/MFkQNcO8oOAskqIZRwjl0WJwd5Y/DcuCXgYvDYsWIz313nQOp9107br BDlfzz1NjFojWuRdwZYwErovjG4Gu/UJRNIw60EQLbBBZVhuvFZtSvyvZuUj6+vDwWoA y4+w== X-Forwarded-Encrypted: i=1; AJvYcCUMCvHqQy5pU73/hWkKkeJWlF1+1NCmjJeKvlaGg9ydhi+ulLESPdiSQGsUQhkEf+7UhjFfci3ER/uX@vger.kernel.org, AJvYcCUNorcnO+jTy247qD7v+Q0Yhmda4lH/p9rqO5hReFv2yefAGEZTIgAjhaI0iHsYnkdK6xzs/dL2dLvD@vger.kernel.org, AJvYcCUROa0mZ3BVr8e7ILLV6EixPyUIDNH1dAw0FWs9hOpgCS1k4/AJCSZbH3SuM9viEmJjCC5MSGKoBU7nu9A/@vger.kernel.org, AJvYcCUz8SCgOYckDyyP5UOxLWDyY0vOy/Fb30Mm9uEs8zcsOEu4e+GbW444OEoajcGIpOVXCGeB9sZKVRBiVw==@vger.kernel.org, AJvYcCXQUB56XFQ8Nq7MT/USBEHVX1QmJhGR5r23GJs2CzJ/to+erJ+OGYfQrflyU/ZGlSu5aOaA5gDs@vger.kernel.org, AJvYcCXR0lfFUCWV6X6a1mm83ux7CGvp06lHNOU2IWkSiFy83CgWci0G6K2e4jnBxauSXG43UGfmLSTuKC9dNxHqdcs=@vger.kernel.org X-Gm-Message-State: AOJu0Yx9eLflw/pchEqWlIrtdsew6NyAJEFmGzoeV2VPKjwRQKJc1ynE 8pFq0u4dftRMRMJHialrGIRzENENsdCckvXdLg4L1LABt8WwOFO+8cvx X-Gm-Gg: ASbGncv9iUa3haI734IMp0KQDlL+IZyTIyrw4o0tmjEaMZbXLFHtNRYz7hazfxhFtKu 1t8vvgBa2a7roEtxJd/kCk48szX2cshecGHFSi4EW3vKApFNNZw1ozXs8kwgs/yFMQ50+xdLm/M Bls5bwNHKBMITuODE/TTTTiNJAz//izg7WxSwJUp1hxemH/IM6zh8B051fd/5L4hVUNAxGaIuDT hDgILrz86ORZDCgeIInoF1JVUVhXg8MPMLnFHTmP5pdVAdLny9H80iSWhrmj8gbS1B5ZqSnw1Rz j6S45w1aLsWqXeBBHk90Ads2oNlO2L3v+7PENa0kZKmVwaFivVHbXv1+YvxmCITTSb0lm2tdwvI VrHWAeupnFOivuoVFqQQhvCBFUlaXOeY60Dw+ewBwzTsgQw/3EKZdhZfl65pT1YHcpRNvKCKJQ/ UbRp+fEg== X-Google-Smtp-Source: AGHT+IFWTiFyINY3p3GkFh9rLrPAgb2Rm2YLBk8fWEOnNPV1fS36WcePgUDPT5GTTf3HkIlDqW/aCA== X-Received: by 2002:a17:90b:2ecb:b0:311:f99e:7f4a with SMTP id 98e67ed59e1d1-31c4ccedab5mr2713396a91.26.1752207039080; Thu, 10 Jul 2025 21:10:39 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b3bbe6f4fc8sm3902095a12.51.2025.07.10.21.10.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jul 2025 21:10:38 -0700 (PDT) From: FUJITA Tomonori To: alex.gaynor@gmail.com, dakr@kernel.org, gregkh@linuxfoundation.org, ojeda@kernel.org, rafael@kernel.org, robh@kernel.org, saravanak@google.com, tmgross@umich.edu 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, lenb@kernel.org, leon@kernel.org, linux-acpi@vger.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 Subject: [PATCH v4 3/3] rust: net::phy Change module_phy_driver macro to use module_device_table macro Date: Fri, 11 Jul 2025 13:09:47 +0900 Message-ID: <20250711040947.1252162-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250711040947.1252162-1-fujita.tomonori@gmail.com> References: <20250711040947.1252162-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Change module_phy_driver macro to build device tables which are exported to userspace by using module_device_table macro. Reviewed-by: Trevor Gross Signed-off-by: FUJITA Tomonori --- rust/kernel/net/phy.rs | 51 ++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs index f44e8107cec4..8b94f964f936 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). =20 -use crate::{error::*, prelude::*, types::Opaque}; +use crate::{device_id::RawDeviceId, error::*, prelude::*, types::Opaque}; use core::{marker::PhantomData, ptr::addr_of_mut}; =20 pub mod reg; @@ -750,6 +750,12 @@ pub const fn mdio_device_id(&self) -> bindings::mdio_d= evice_id { } } =20 +// 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 `R= awType`. +unsafe impl RawDeviceId for DeviceId { + type RawType =3D bindings::mdio_device_id; +} + enum DeviceMask { Exact, Model, @@ -850,19 +856,18 @@ const fn as_int(&self) -> u32 { /// } /// }; /// -/// const _DEVICE_TABLE: [::kernel::bindings::mdio_device_id; 2] =3D [ -/// ::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_dev= ice_id; 2] =3D _DEVICE_TABLE; +/// const N: usize =3D 1; +/// +/// const TABLE: ::kernel::device_id::IdArray<::kernel::net::phy::DeviceId= , (), N> =3D +/// ::kernel::device_id::IdArray::new_without_index([ +/// ::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 +878,12 @@ macro_rules! module_phy_driver { }; =20 (@device_table [$($dev:expr),+]) =3D> { - // 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] =3D [ - $($dev.mdio_device_id()),+, - $crate::bindings::mdio_device_id { - phy_id: 0, - phy_id_mask: 0 - } - ]; + const N: usize =3D $crate::module_phy_driver!(@count_devices $($de= v),+); + + const TABLE: $crate::device_id::IdArray<$crate::net::phy::DeviceId= , (), N> =3D + $crate::device_id::IdArray::new_without_index([ $(($dev,())),+= , ]); =20 - #[cfg(MODULE)] - #[no_mangle] - static __mod_device_table__mdio__phydev: [$crate::bindings::mdio_d= evice_id; - $crate::module_phy_driver!(@count_devices $($dev),+) + 1] =3D = _DEVICE_TABLE; + $crate::module_device_table!("mdio", phydev, TABLE); }; =20 (drivers: [$($driver:ident),+ $(,)?], device_table: [$($dev:expr),+ $(= ,)?], $($f:tt)*) =3D> { --=20 2.43.0