From nobody Tue Oct 7 09:02:43 2025 Received: from bali.collaboradmins.com (bali.collaboradmins.com [148.251.105.195]) (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 87498223327; Fri, 11 Jul 2025 22:32:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.251.105.195 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752273166; cv=none; b=V64CBd9HNe/iGYhy4FjxYlcPMWtXZ9tdtAJkUeCM3OCSk60+gDwi0t09e+uCyXJidFy89yCx6W0tAL3MarRbDwvp75lKMueJsfWiG/MJ3w6ax1bCurpQtcZW/nzBrD2FDMbETurhxkmJweu5/eYHluVBWHeNKXiooLDx0gcgP1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752273166; c=relaxed/simple; bh=yPSOgNyoGmgWCeMxGnn15uBykHIufP1j0ynDY2EjCpk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Vi9x5zpZmr6izTGBc/ulnMYf1DNedAxdr0ODi0+bEXweV7LJ8RdMfn3fFxXxheFjeg8ZRi5QliRYcPSRFUwlj2oqSgj1KjlK+vs4PLQeu8Of5/KoCplXKpvqARlU67x25NX0XzvtocyMTguhP1wfeV2bYr4k1LrQEgIIsAitIqA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=mG5NJ/yf; arc=none smtp.client-ip=148.251.105.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="mG5NJ/yf" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1752273161; bh=yPSOgNyoGmgWCeMxGnn15uBykHIufP1j0ynDY2EjCpk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=mG5NJ/yfitDbaIaKIvmyfZBIKU/60H4CoQrSqNKTPQEMfPuggJsrsJLWCcHNA3hQy ZN9JGHpNQs/ffe1P2lnm9DUggFzMCPkkFjunCG6YJOYlaaHM9S6DIHHwEtllhUvUBj QXyLH3sUg3RdxSwmmnHXDyhfsarKpxSaqpjtgdZAKYWwWqGVHhOmJ0bw4mCt5Ezq9t Qrzlo/iHbHKXe40ZUvY7pvvWVslEG7V7pLK3wex5heo2E6DsEURUmIJg7CdWOaD0o7 ip0oHlNNsIEBQ1XKTmE3nTgqsofmKjaZazjTbpGDFzk4HJJkZGBTFpAHzDZ1lJoOK6 LmkfWHXYpowjA== Received: from [192.168.0.2] (unknown [IPv6:2804:14d:72b4:82f6:67c:16ff:fe57:b5a3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: dwlsalmeida) by bali.collaboradmins.com (Postfix) with ESMTPSA id A507B17E1110; Sat, 12 Jul 2025 00:32:39 +0200 (CEST) From: Daniel Almeida Date: Fri, 11 Jul 2025 19:32:29 -0300 Subject: [PATCH v13 3/3] rust: platform: add resource accessors Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250711-topics-tyr-platform_iomem-v13-3-06328b514db3@collabora.com> References: <20250711-topics-tyr-platform_iomem-v13-0-06328b514db3@collabora.com> In-Reply-To: <20250711-topics-tyr-platform_iomem-v13-0-06328b514db3@collabora.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Daniel Almeida X-Mailer: b4 0.14.2 The previous patches have added the abstractions for Resources and the ability to map them and therefore read and write the underlying memory . The only thing missing to make this accessible for platform devices is to provide accessors that return instances of IoRequest<'a>. These ensure that the resource are valid only for the lifetime of the platform device, and that the platform device is in the Bound state. Therefore, add these accessors. Also make it possible to retrieve resources from platform devices in Rust using either a name or an index. Signed-off-by: Daniel Almeida --- rust/kernel/platform.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 86f9d73c64b38ffe067be329a77b2fc04564c7fe..57f9964bb736cf5749ec3954def= 03c0d02873642 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -5,8 +5,11 @@ //! C header: [`include/linux/platform_device.h`](srctree/include/linux/pl= atform_device.h) =20 use crate::{ - acpi, bindings, container_of, device, driver, + acpi, bindings, container_of, + device::{self, Bound}, + driver, error::{to_result, Result}, + io::{mem::IoRequest, Resource}, of, prelude::*, str::CStr, @@ -211,6 +214,61 @@ impl Device { fn as_raw(&self) -> *mut bindings::platform_device { self.0.get() } + + /// Returns the resource at `index`, if any. + pub fn resource_by_index(&self, index: u32) -> Option<&Resource> { + // SAFETY: `self.as_raw()` returns a valid pointer to a `struct pl= atform_device`. + let resource =3D unsafe { + bindings::platform_get_resource(self.as_raw(), bindings::IORES= OURCE_MEM, index) + }; + + if resource.is_null() { + return None; + } + + // SAFETY: `resource` is a valid pointer to a `struct resource` as + // returned by `platform_get_resource`. + Some(unsafe { Resource::from_raw(resource) }) + } + + /// Returns the resource with a given `name`, if any. + pub fn resource_by_name(&self, name: &CStr) -> Option<&Resource> { + // SAFETY: `self.as_raw()` returns a valid pointer to a `struct + // platform_device` and `name` points to a valid C string. + let resource =3D unsafe { + bindings::platform_get_resource_byname( + self.as_raw(), + bindings::IORESOURCE_MEM, + name.as_char_ptr(), + ) + }; + + if resource.is_null() { + return None; + } + + // SAFETY: `resource` is a valid pointer to a `struct resource` as + // returned by `platform_get_resource`. + Some(unsafe { Resource::from_raw(resource) }) + } +} + +impl Device { + /// Returns an `IoRequest` for the resource at `index`, if any. + pub fn io_request_by_index(&self, index: u32) -> Option>= { + // SAFETY: `resource` is a valid resource for `&self` during the + // lifetime of the `IoRequest`. + self.resource_by_index(index) + .map(|resource| unsafe { IoRequest::new(self.as_ref(), resourc= e) }) + } + + /// Returns an `IoRequest` for the resource with a given `name`, if an= y. + pub fn io_request_by_name(&self, name: &CStr) -> Option>= { + // SAFETY: `resource` is a valid resource for `&self` during the + // lifetime of the `IoRequest`. + self.resource_by_name(name) + .map(|resource| unsafe { IoRequest::new(self.as_ref(), resourc= e) }) + } } =20 // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend= on `Device`'s generic --=20 2.50.0