Re: [PATCH v4 3/3] rust: platform: allow ioremap of platform resources

From: Alice Ryhl
Date: Thu Jan 09 2025 - 09:01:20 EST


On Thu, Jan 9, 2025 at 2:32 PM Daniel Almeida
<daniel.almeida@xxxxxxxxxxxxx> wrote:
>
> The preceding patches added support for resources, and for a general
> IoMem abstraction, but thus far there is no way to access said IoMem
> from drivers, as its creation is unsafe and depends on a resource that
> must be acquired from a some device first.

"a some device" typo?
> + /// ```no_run
> + /// # use kernel::{bindings, c_str, platform};
> + ///
> + /// fn probe(pdev: &mut platform::Device, /* ... */) -> Result<()> {

When you use `#` to hide part of the example, you end up with a weird
empty line at the top of the example. Please un-hide the line or
remove the empty line.

> + /// Returns the resource at `index`, if any.
> + pub fn resource(&self, index: u32) -> Option<&Resource> {
> + // SAFETY: `self.as_raw()` returns a valid pointer to a `struct platform_device`.
> + let resource = unsafe {
> + bindings::platform_get_resource(self.as_raw(), bindings::IORESOURCE_MEM, index)
> + };
> +
> + // SAFETY: `resource` is a valid pointer to a `struct resource` as
> + // returned by `platform_get_resource`.
> + (!resource.is_null()).then(|| unsafe { Resource::from_ptr(resource) })

This is a bit confusing. Perhaps just

if resource.is_null() {
return None;
}
Some(Resource::from_ptr(resource))

Alice