[PATCH v5 3/9] rust: pwm: Add driver operations trait and registration support
Danilo Krummrich
dakr at kernel.org
Fri Jun 27 11:51:51 PDT 2025
On 6/23/25 8:08 PM, Michal Wilczynski wrote:
> diff --git a/rust/kernel/pwm.rs b/rust/kernel/pwm.rs
> index 3865b43ec47df6cb0c09bc74a228535512b6b1a8..25bc07a3df1d43467a3a6ec8f2362ae8f770360a 100644
> --- a/rust/kernel/pwm.rs
> +++ b/rust/kernel/pwm.rs
> @@ -8,12 +8,13 @@
>
> use crate::{
> bindings,
> - device,
> - error,
> + device::{self, Bound},
> + devres::Devres,
> + error::{self, to_result},
> prelude::*,
> types::{ARef, AlwaysRefCounted, ForeignOwnable, Opaque},
> };
> -use core::{convert::TryFrom, ptr::NonNull};
> +use core::{convert::TryFrom, marker::PhantomData, ptr::NonNull};
>
> /// Maximum size for the hardware-specific waveform representation buffer.
> ///
> @@ -408,3 +409,482 @@ unsafe impl Send for Chip {}
> // kernel locks, which the C core is responsible for. Any interior mutability is
> // handled and synchronized by the C kernel code.
> unsafe impl Sync for Chip {}
> +
> +/// A resource guard that ensures `pwmchip_remove` is called on drop.
> +///
> +/// This struct is intended to be managed by the `devres` framework by transferring its ownership
> +/// via [`Devres::new_foreign_owned`]. This ties the lifetime of the PWM chip registration
> +/// to the lifetime of the underlying device.
> +pub struct Registration {
> + chip: ARef<Chip>,
> +}
> +
> +impl Registration {
> + /// Registers a PWM chip with the PWM subsystem.
> + ///
> + /// Transfers its ownership to the `devres` framework, which ties its lifetime
> + /// to the parent device.
> + /// On unbind of the parent device, the `devres` entry will be dropped, automatically
> + /// calling `pwmchip_remove`. This function should be called from the driver's `probe`.
> + pub fn new_foreign_owned(
> + dev: &device::Device<Bound>,
> + chip: ARef<Chip>,
> + ops_vtable: &'static PwmOpsVTable,
> + ) -> Result {
> + let c_chip_ptr = chip.as_raw();
> +
> + // SAFETY: `c_chip_ptr` is valid because the `ARef<Chip>` that owns it exists.
> + // The vtable pointer is also valid. This sets the `.ops` field on the C struct.
> + unsafe {
> + (*c_chip_ptr).ops = ops_vtable.as_raw();
> + }
> +
> + // SAFETY: `c_chip_ptr` points to a valid chip with its ops initialized.
> + // `__pwmchip_add` is the C function to register the chip with the PWM core.
> + unsafe {
> + to_result(bindings::__pwmchip_add(c_chip_ptr, core::ptr::null_mut()))?;
> + }
> +
> + let registration = Registration { chip };
> +
> + Devres::new_foreign_owned(dev, registration, GFP_KERNEL)?;
> +
> + Ok(())
This can just be:
Devres::new_foreign_owned(dev, registration, GFP_KERNEL)
I.e. no need for the `Ok(())` below.
With that,
Reviewed-by: Danilo Krummrich <dakr at kernel.org>
for the Registration bits.
@Uwe: Just a head-up if you plan to pick this up for the upcoming merge window,
Devres::new_foreign_owned() will be replaced with devres::register() --
semantics and arguments do not change.
> + }
> +}
> +
> +impl Drop for Registration {
> + fn drop(&mut self) {
> + let chip_raw = self.chip.as_raw();
> +
> + // SAFETY: `chip_raw` points to a chip that was successfully registered.
> + // `bindings::pwmchip_remove` is the correct C function to unregister it.
> + // This `drop` implementation is called automatically by `devres` on driver unbind.
> + unsafe {
> + bindings::pwmchip_remove(chip_raw);
> + }
NIT: You can write this in one line as:
unsafe { bindings::pwmchip_remove(chip_raw) };
More information about the linux-riscv
mailing list