[PATCH v9 2/6] rust: pwm: Add complete abstraction layer

Uwe Kleine-König ukleinek at kernel.org
Sun Jul 6 23:57:24 PDT 2025


Hello Danilo,

On Sun, Jul 06, 2025 at 02:23:04PM +0200, Danilo Krummrich wrote:
> On Sun, Jul 06, 2025 at 01:45:13PM +0200, Michal Wilczynski wrote:
> > +    /// # Safety
> > +    ///
> > +    /// `dev` must be a valid pointer to a `bindings::device` embedded within a
> > +    /// `bindings::pwm_chip`. This function is called by the device core when the
> > +    /// last reference to the device is dropped.
> > +    unsafe extern "C" fn release_callback(dev: *mut bindings::device) {
> > +        // SAFETY: The function's contract guarantees that `dev` points to a `device`
> > +        // field embedded within a valid `pwm_chip`. `container_of!` can therefore
> > +        // safely calculate the address of the containing struct.
> > +        let c_chip_ptr = unsafe { container_of!(dev, bindings::pwm_chip, dev) };
> > +
> > +        // SAFETY: `c_chip_ptr` is a valid pointer to a `pwm_chip` as established
> > +        // above. Calling this FFI function is safe.
> > +        let drvdata_ptr = unsafe { bindings::pwmchip_get_drvdata(c_chip_ptr) };
> > +
> > +        if !drvdata_ptr.is_null() {
> 
> Is this check needed? I think one can't create a pwm::Chip instance without
> providing a T, so this pointer can't be NULL I think.

There are currently a few C drivers, that don't use a private data
struct that is managed by the pwmchip. One of them doesn't make use of
the pwmchip's drvdata at all. The latter is drivers/pwm/pwm-twl-led.c.

Best regards
Uwe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-riscv/attachments/20250707/8bfb4391/attachment.sig>


More information about the linux-riscv mailing list