Re: [PATCH v7 4/8] rust: pwm: Add driver operations trait and registration support

Danilo Krummrich
2025-07-02T15:21:08Z

On Wed, Jul 02, 2025 at 03:45:32PM +0200, Michal Wilczynski wrote:
> +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 register(
> + dev: &device::Device<Bound>,
> + chip: ARef<Chip>,
> + ops_vtable: &'static PwmOpsVTable,
> + ) -> Result {

One thing I did miss here: Given that this should give us the guarantee that the
parent device of the Chip is always bound, you have to add a check for this
here, i.e. fail if `dev.as_raw() != chip.parent().as_raw()`.

> + 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::register(dev, registration, GFP_KERNEL)
> + }
> +}