From nobody Mon Oct 6 21:00:41 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 C3C7B2FE320; Thu, 17 Jul 2025 15:55:58 +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=1752767760; cv=none; b=POt1Jmel8TQN2T4I0yucikBnUNz+kIbUQgKx8etoVIVx57W4dz/vAXmr4ZvRdiFdYNsVJ7FrvUpYt4C/0ndtR+atNlmjgYl6ASEWk1rStZYnDV82nUsbWfJNEpDJtGDynp8Idfq3n6RaI+Sj8xMqMbYqM3K1M/hAO+cjl+tR4ZI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752767760; c=relaxed/simple; bh=31AqHo3GIvu3IzySTWWesuZK0J2sD7GkvCbF0fO1rZY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FFrfDBUeFMM1Ny1aTfuHMthfz9NwGvEoowC74FYmKwwWFYZzlLT6/GahvFlHpX4F2umOcC5g4a82JjWiIsS4d3kebur/mPrNIvDjc7+6u/DRafQAp+vMvtltmCxr5waMVyXVTY0/+KT/E5Nj59gZdEmADWpPlV02tXYFWLBeYVs= 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=MjlDAhTk; 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="MjlDAhTk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1752767757; bh=31AqHo3GIvu3IzySTWWesuZK0J2sD7GkvCbF0fO1rZY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=MjlDAhTkQaYS7v9/hj5wXQWjOj7tBYWdWGw9lwWqc3ActlAfxw2JoRt2BdZf4GaHP 7OX3BN+HvbOfubAcBhT1JMLg0x39rtpBda1O8MVqkV6ZoADLErHOEQgACDo7My3gR+ HBH/GX3qf3Yc+XIBdBaKMYq83NpX/0zvHHfkhJK2CAnJ/c5jdgBwQN556PoUObOCge T2ut7xSmtwRzgbOr4VYQfrech7aU762XStbMeI6vudKqpyrjfDOwiNAzYyISp+nw+T S9b5YkBaJ/RhBd4PtU+Y+cKV6Hy+5+1I2CSBfx7GNc5rpjssR16Bs+ew0gY8qdZf8L iOkaVr1cWoB9Q== 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 105A417E1541; Thu, 17 Jul 2025 17:55:54 +0200 (CEST) From: Daniel Almeida Date: Thu, 17 Jul 2025 12:55:24 -0300 Subject: [PATCH v15 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: <20250717-topics-tyr-platform_iomem-v15-3-beca780b77e3@collabora.com> References: <20250717-topics-tyr-platform_iomem-v15-0-beca780b77e3@collabora.com> In-Reply-To: <20250717-topics-tyr-platform_iomem-v15-0-beca780b77e3@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. Acked-by: Miguel Ojeda 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