From nobody Wed Dec 17 05:26:48 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4C9510E0; Wed, 19 Mar 2025 20:31:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742416284; cv=none; b=o/x53y355Xgq/lkZOVPRQ/oPiywBdGwBxt+Xu6HCh+VqKnMPXH/lnhILxxsqWD2wL5Se7zZxm+AGHE89RZhMOfl1orh1Yd7vcc3ZaTcPx+jZG5IoW+Z0MyLX4CTbO/DjedjlS4gwCuu1vwDlYdyPUnXS8DLeDp2aNY5Cm92MOgQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742416284; c=relaxed/simple; bh=7/fXqZFF2VFB4dh0IoJT5tB8cl6kDShPSNOQnvkmn0A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jtFLzq4bXV2wOwlaVwbBzfpSb4Z71sJ9fRCtN8zCrLH9g50PR8fIOcUx9GuZshiXU/uZINXAzntYWr33sDgebroc7XiWHObCH0S/UyUJZPNcBuafwvQ8Biq6bAtuvxfuhMj4JYhB6YpgwMxEhLYG0Oc/AO2OLOTm0ef2v/ke4gQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=l7k6vKpp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="l7k6vKpp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 244BBC4CEE4; Wed, 19 Mar 2025 20:31:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742416284; bh=7/fXqZFF2VFB4dh0IoJT5tB8cl6kDShPSNOQnvkmn0A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l7k6vKppXEtsOHWzR8c7uFTnOGUeVfa+ssx5drIrT8f5K/G2TEHQxjKD3TXi27e07 jh/Ae3fkA5T1zkDly4foZ+CgS5lyVHUKb9nLlEUHtbbUhonqqvUsjHejXStlo6zCfI ANa7BYf8Gh3cJqBLqYZXduLsSXdts5VgxNX/66mZSKWlzTYRZbMRhpQNGe6y+TFYY8 n7ji0+70dmq2FcUq3LPLeTsAvH3KGgJxy/8GjnV6qMTm+PwI0uzaKiqz6sNbVu5doS Udb4F2Iosiw7S8dGMPxNDkoRVOkmkAlPfl1ABdokNR59YEWhbTOjdHemKAyFU15Q+Y dZN26zfOx0VEA== From: Danilo Krummrich To: bhelgaas@google.com, gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH 1/4] rust: device: implement Device::parent() Date: Wed, 19 Mar 2025 21:30:25 +0100 Message-ID: <20250319203112.131959-2-dakr@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250319203112.131959-1-dakr@kernel.org> References: <20250319203112.131959-1-dakr@kernel.org> 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" Device::parent() returns a reference to the device' parent device, if any. Signed-off-by: Danilo Krummrich Reviewed-by: Alice Ryhl --- rust/kernel/device.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 21b343a1dc4d..76b341441f3f 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -65,6 +65,19 @@ pub(crate) fn as_raw(&self) -> *mut bindings::device { self.0.get() } =20 + /// Returns a reference to the parent device, if any. + pub fn parent<'a>(&self) -> Option<&'a Self> { + // SAFETY: By the type invariant `self.as_raw()` is always valid. + let parent =3D unsafe { *self.as_raw() }.parent; + + if parent.is_null() { + None + } else { + // SAFETY: Since `parent` is not NULL, it must be a valid poin= ter to a `struct device`. + Some(unsafe { Self::as_ref(parent) }) + } + } + /// Convert a raw C `struct device` pointer to a `&'a Device`. /// /// # Safety --=20 2.48.1 From nobody Wed Dec 17 05:26:48 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 68F2D10E0; Wed, 19 Mar 2025 20:31:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742416288; cv=none; b=er/xyNTx9Z8TLTjmW/qzAOPT+baGEZGbKiB30MKnH/pCafXvEa+uoAeLVOg0vkxGU1BmbzBoUuKBKySPYMg8bX03ZpB75uq8RCBOhC4bgDmElWy+ohGuw/pN5nCoAq52BoSqXDH/gkjL+WMcVnXJrzBtgofcY49c8kcTJqzFC7A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742416288; c=relaxed/simple; bh=6Xl/Pcaq5N7AF/vlFg1rfoIOXVG0Cl0VCCKDZcJgVOA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SUTCsuBmxiv3sOr06Y8S77XdugmBJdhUHdG/3LgDUnQWkYfT0yESnBMjnRRnTH5IDNaecm1Blp4N0+SnBvQvTaYTq6U1XXS/2A+WRXY88m/j7hzu7vkVtOjXhPPMUgZ06x61KWZg38P3hgdN2sZs9WkPOlWppz5/QZe9NtSpicc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Y0nefezm; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Y0nefezm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BDEF5C4CEE7; Wed, 19 Mar 2025 20:31:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742416287; bh=6Xl/Pcaq5N7AF/vlFg1rfoIOXVG0Cl0VCCKDZcJgVOA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y0nefezm0QfeifDJoype7OPoLq8chL+cVhw+t9KdXPiyw4HsfFV7PWe+5FnrCyvR8 ccqqtxpx3ylIzSNKIlcxZLcMugFZuWm8jXEByVot2Wyytl1dm7JpjF01KOddL/hWUy BYkTiKwXu54LOW6BYCBdg0x3UqHh+QfB6QLGhVr6gG4EFDautU9dFIlPzVZs+tAa49 iEmSWlQLwGylFmlH7mpnXdB0tCaI2IgND53h1cIbcrz9tJan9Bpz+3QHH5kHKjHlow jgL0pmhkz/4I8rS/nbOxwgGU2iIpaScW25hTuQ0Ovy4XaWC8N/QdyjlYwMkaEJh2KM trZG7Q69rxXzw== From: Danilo Krummrich To: bhelgaas@google.com, gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH 2/4] rust: device: implement bus_type_raw() Date: Wed, 19 Mar 2025 21:30:26 +0100 Message-ID: <20250319203112.131959-3-dakr@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250319203112.131959-1-dakr@kernel.org> References: <20250319203112.131959-1-dakr@kernel.org> 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" Implement bus_type_raw(), which returns a raw pointer to the device' struct bus_type. This is useful for bus devices, to implement the following trait. impl TryFrom<&Device> for &pci::Device With this a caller can try to get the bus specific device from a generic device in a safe way. try_from() will only succeed if the generic device' bus type pointer matches the pointer of the bus' type. Signed-off-by: Danilo Krummrich Reviewed-by: Alice Ryhl --- rust/kernel/device.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 76b341441f3f..e2de0efd4a27 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -78,6 +78,13 @@ pub fn parent<'a>(&self) -> Option<&'a Self> { } } =20 + /// Returns a raw pointer to the device' bus type. + #[expect(unused)] + pub(crate) fn bus_type_raw(&self) -> *const bindings::bus_type { + // SAFETY: By the type invariants, `self.as_raw()` is a valid poin= ter to a `struct device`. + unsafe { (*self.as_raw()).bus } + } + /// Convert a raw C `struct device` pointer to a `&'a Device`. /// /// # Safety --=20 2.48.1 From nobody Wed Dec 17 05:26:48 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B0B5810E0; Wed, 19 Mar 2025 20:31:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742416291; cv=none; b=hyl+C8AgzF3aCOKo9paE0f4F8v55z3MQDtmXUJWwp8+n42Te1n41AwpYH353/0Dswaw1PDFfe17ttC50fVKafFbKYi1qgS+if6FVtrMQ3HX2NgjeTLxUJQ2jRGgRc8L16YKBHAy7a/FXveiG6gGXZK89aeyq6SdnTzbmzQZ4RUU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742416291; c=relaxed/simple; bh=KwVDyqu7ToQAgJMSbumNmrl4oPW891U9MUKymaHYbyw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Zijfw3DZarZrN9B/ykKL51fQr+Of2QmnfIJdHO3hKIcRhAjGP4S1USzF0xIWrSoyoBXPjygpV2zgiEk/na+i+LLCpNliY56EBYNx08cVwS/DvR3XGPYVaj+gvp/baAleQtYVU3IRG5j3YqSofCIYq0Td59P+WYc6fHKhUm3pfx0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kRa7KHcK; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kRa7KHcK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60182C4CEE4; Wed, 19 Mar 2025 20:31:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742416291; bh=KwVDyqu7ToQAgJMSbumNmrl4oPW891U9MUKymaHYbyw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kRa7KHcKMGJofJCG9g4R/I8lxiJdyxmQywzQ0865p1/w+HO3UuItMgv8hyY+SRJuQ 1GcffmCAfwAPd3OG5vpznRg/yyM2HiYOQ+nrseUdKo8mHyoiJ9gHByIjE0VSywEcHD DHCezEP749urwQcmq4JMcgqZNc8Lur1Ong8CK5sB+0gRsrFR0vul7vGvGkprDc+QbO 2XqcXv6xwe6AiNr5vjsygmCx4efLEXqWFisBZd3J5jnGNclGwwInuW91dqHEyt4arf lHt8J3vlxE67z/yA1cBY9AXX/IEZagW/ixwfXhJNGzoiQyI1jooTJE+wEkYEyYxooN X4R6AfQleEIKg== From: Danilo Krummrich To: bhelgaas@google.com, gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH 3/4] rust: pci: impl TryFrom<&Device> for &pci::Device Date: Wed, 19 Mar 2025 21:30:27 +0100 Message-ID: <20250319203112.131959-4-dakr@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250319203112.131959-1-dakr@kernel.org> References: <20250319203112.131959-1-dakr@kernel.org> 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" Implement TryFrom<&device::Device> for &Device. This allows us to get a &pci::Device from a generic &Device in a safe way; the conversion fails if the device' bus type does not match with the PCI bus type. Signed-off-by: Danilo Krummrich Reviewed-by: Alice Ryhl --- rust/kernel/device.rs | 1 - rust/kernel/pci.rs | 21 +++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index e2de0efd4a27..34244c139afc 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -79,7 +79,6 @@ pub fn parent<'a>(&self) -> Option<&'a Self> { } =20 /// Returns a raw pointer to the device' bus type. - #[expect(unused)] pub(crate) fn bus_type_raw(&self) -> *const bindings::bus_type { // SAFETY: By the type invariants, `self.as_raw()` is a valid poin= ter to a `struct device`. unsafe { (*self.as_raw()).bus } diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 22a32172b108..b717bcdb9abf 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -6,7 +6,7 @@ =20 use crate::{ alloc::flags::*, - bindings, device, + bindings, container_of, device, device_id::RawDeviceId, devres::Devres, driver, @@ -20,7 +20,7 @@ use core::{ marker::PhantomData, ops::Deref, - ptr::{addr_of_mut, NonNull}, + ptr::{addr_of, addr_of_mut, NonNull}, }; use kernel::prelude::*; =20 @@ -466,6 +466,23 @@ fn as_ref(&self) -> &device::Device { } } =20 +impl TryFrom<&device::Device> for &Device { + type Error =3D kernel::error::Error; + + fn try_from(dev: &device::Device) -> Result { + if dev.bus_type_raw() !=3D addr_of!(bindings::pci_bus_type) { + return Err(EINVAL); + } + + // SAFETY: We've just verified that the bus type of `dev` equals `= bindings::pci_bus_type`, + // hence `dev` must be embedded in a valid `struct pci_dev`. + let pdev =3D unsafe { container_of!(dev.as_raw(), bindings::pci_de= v, dev) }; + + // SAFETY: `pdev` is a valid pointer to a `struct pci_dev`. + Ok(unsafe { &*pdev.cast() }) + } +} + // SAFETY: A `Device` is always reference-counted and can be released from= any thread. unsafe impl Send for Device {} =20 --=20 2.48.1 From nobody Wed Dec 17 05:26:48 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B258210E0; Wed, 19 Mar 2025 20:31:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742416295; cv=none; b=ctLoBGdYGQiry/peqXLvbe/ZHFnPOIl20TNsgs+UzlsI/Py/VLTDxSDJbA9i8kQYBZobuHNVnjEkKWlyaggg7l3rchywlMqKcejyN70oibA/4QBBsySEe4iqe136BKk8z/5eLy0ZeG0yj/OUpCFapfsDIMd7m6cnqqKLmqxhyos= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742416295; c=relaxed/simple; bh=8dSbmv6RUXno2wJjkEPEkGjO99eu0MpD6e+agTv04vE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BJHr/8IBPRv3cRwawDtP03Y+fJxgDwoAVbcUghtvoIpJ90D8e1WuCg3dZDtVDyi2OaBhrAZez8ana0ktbrCohTt6+VvGLxrA0f7kB22RByxoy6AHokB6P6BUsajKlTxYAXJCpJ+F0yZYExTBkYdWG7mnlrbRJ0ZSqm9zlGllyZM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GdMiZXcU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GdMiZXcU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 012C9C4CEE4; Wed, 19 Mar 2025 20:31:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742416295; bh=8dSbmv6RUXno2wJjkEPEkGjO99eu0MpD6e+agTv04vE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GdMiZXcUBwYVxbdCg/A4RqfgFZb7QwFmp96v/B5fdXrFMCap6wzgWr+ASfnY71f0R fkEo3oZtjuJFGqnS8TwpPrV/5Q0t3Ccgm4dWUrdWitH6gCIV2+Aua/wyB+sEpdZCXT KWuxYZPGHC+BMll1ksUN70PAuEFsOpXLzRDvhsXeWxXJsL6UD3YMlNIMBd4Yl/rfqy vVdJ1inQ2E3XHGscx79qSVMNx2i/KT5F++PW1pOd4jXDf57xmRFcNknLni7qPsXQKM 0bh2eyNsELxhE2mpU+7AR/RiFvDeGF5iVhRAuc3x3vfPbVSr7jv/hfOh1NZnS1Zjnq P40uxgUVCTKOw== From: Danilo Krummrich To: bhelgaas@google.com, gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH 4/4] rust: platform: impl TryFrom<&Device> for &platform::Device Date: Wed, 19 Mar 2025 21:30:28 +0100 Message-ID: <20250319203112.131959-5-dakr@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250319203112.131959-1-dakr@kernel.org> References: <20250319203112.131959-1-dakr@kernel.org> 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" Implement TryFrom<&device::Device> for &Device. This allows us to get a &platform::Device from a generic &Device in a safe way; the conversion fails if the device' bus type does not match with the platform bus type. Signed-off-by: Danilo Krummrich Reviewed-by: Alice Ryhl --- rust/kernel/platform.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index e37531bae8e9..c17fc6e7c596 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -5,7 +5,7 @@ //! C header: [`include/linux/platform_device.h`](srctree/include/linux/pl= atform_device.h) =20 use crate::{ - bindings, device, driver, + bindings, container_of, device, driver, error::{to_result, Result}, of, prelude::*, @@ -17,7 +17,7 @@ use core::{ marker::PhantomData, ops::Deref, - ptr::{addr_of_mut, NonNull}, + ptr::{addr_of, addr_of_mut, NonNull}, }; =20 /// An adapter for the registration of platform drivers. @@ -234,6 +234,24 @@ fn as_ref(&self) -> &device::Device { } } =20 +impl TryFrom<&device::Device> for &Device { + type Error =3D kernel::error::Error; + + fn try_from(dev: &device::Device) -> Result { + if dev.bus_type_raw() !=3D addr_of!(bindings::platform_bus_type) { + return Err(EINVAL); + } + + // SAFETY: We've just verified that the bus type of `dev` equals + // `bindings::platform_bus_type`, hence `dev` must be embedded in = a valid + // `struct platform_device`. + let pdev =3D unsafe { container_of!(dev.as_raw(), bindings::platfo= rm_device, dev) }; + + // SAFETY: `pdev` is a valid pointer to a `struct platform_device`. + Ok(unsafe { &*pdev.cast() }) + } +} + // SAFETY: A `Device` is always reference-counted and can be released from= any thread. unsafe impl Send for Device {} =20 --=20 2.48.1