From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mout01.posteo.de (mout01.posteo.de [185.67.36.65]) (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 67C642AC17 for ; Wed, 15 Oct 2025 15:02:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.67.36.65 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760540567; cv=none; b=c/BXvq63zVkfnZUtIfvTEJIDjb38Pix+UikftXrq4bv4EE698mozpI/PC1kwzYa307Sixypgtw9EinGPVbvAkf0LwKP9ucW3fCiiigWxcm4pYAAg5hluN7y553LMERKO9fCOn1NN5RDYFbdgl72A28Wbt4NzVeYzEIc8+CTobJM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760540567; c=relaxed/simple; bh=hHvO7+pAuANETqlkdKcLeY8V60XOi07rOreJSG/Jdp4=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=S1n/Du8q4CZA5NeapW1VWRp9Oj2fLqKBR0A4wMfj2EqRroKaphWVxFFFaEGFFoknHIVNOImge/39T2YHWtiP++WmhhgIMCtnP7iWMLLA6Pj30YqoW2AzlMOei75TrUby/Ubb2lOKoMk7dy3Iyf6TaswTlm0M1GyLioJpDPDow+0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=posteo.de; spf=pass smtp.mailfrom=posteo.de; dkim=pass (2048-bit key) header.d=posteo.de header.i=@posteo.de header.b=ICrcqYps; arc=none smtp.client-ip=185.67.36.65 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=posteo.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=posteo.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=posteo.de header.i=@posteo.de header.b="ICrcqYps" Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 598D4240028 for ; Wed, 15 Oct 2025 17:02:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=posteo.de; s=2017; t=1760540561; bh=7vRn8VJxRxqVRWolykt9lD+vLVOYMFlK1KobOslfuNw=; h=Message-ID:Subject:From:To:Cc:Date:Content-Type: Content-Transfer-Encoding:MIME-Version:Autocrypt:OpenPGP:From; b=ICrcqYpsQ7GNY5y1C0CaOCKAzodxxE/OYFBqv588qpjnO0qnFlE344Qw/2DGgSZ5a zO6q+mUGMiVFRbK+6mowhXU69KOi23wyAjkg4904DqzoRddeGWjFsq9OKA5oadqhZ5 UBPmVPCwJo0Hc7AXohBogV4j37FzzgEShV13YhxzgkhQ2yKgY1pTIZzdx1ukVElRF7 idebzus40Tso/yAtldu/3pO3OqPusZXSU04ff3+1Iac6XI/XG1SargBVPpZlIIZs+4 UC0chboiMYuZ+MRKAAbvEKaBKnA+ekARP57dzAettoWm2fG0pPJUfXE6BsXL3zJHuR hdzqQKv3b/DMA== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4cmvTC3NSqz9rxP; Wed, 15 Oct 2025 17:02:39 +0200 (CEST) Message-ID: <25f3fd337ce5e58915125c4e2eb85ef4d7af3627.camel@posteo.de> Subject: Re: [PATCH v4 2/2] rust: leds: add basic led classdev abstractions From: Markus Probst To: Danilo Krummrich Cc: Alice Ryhl , Miguel Ojeda , Alex Gaynor , Lee Jones , Pavel Machek , Lorenzo Stoakes , Vlastimil Babka , "Liam R. Howlett" , Uladzislau Rezki , Boqun Feng , Gary Guo , bjorn3_gh@protonmail.com, Benno Lossin , Andreas Hindborg , Trevor Gross , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-leds@vger.kernel.org Date: Wed, 15 Oct 2025 15:02:40 +0000 In-Reply-To: References: <20251012145221.172116-1-markus.probst@posteo.de> <20251012145221.172116-3-markus.probst@posteo.de> <7de58fd25b52dd5195c8ac06ed4df5a1e60e5070.camel@posteo.de> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Autocrypt: addr=markus.probst@posteo.de; prefer-encrypt=mutual; keydata=xsFNBGiDvXgBEADAXUceKafpl46S35UmDh2wRvvx+UfZbcTjeQOlSwKP7YVJ4JOZrVs93qReNLkO WguIqPBxR9blQ4nyYrqSCV+MMw/3ifyXIm6Pw2YRUDg+WTEOjTixRCoWDgUj1nOsvJ9tVAm76Ww+ /pAnepVRafMID0rqEfD9oGv1YrfpeFJhyE2zUw3SyyNLIKWD6QeLRhKQRbSnsXhGLFBXCqt9k5JA RhgQof9zvztcCVlT5KVvuyfC4H+HzeGmu9201BVyihJwKdcKPq+n/aY5FUVxNTgtI9f8wIbmfAja oT1pjXSp+dszakA98fhONM98pOq723o/1ZGMZukyXFfsDGtA3BB79HoopHKujLGWAGskzClwTjRQ xBqxh/U/lL1pc+0xPWikTNCmtziCOvv0KA0arDOMQlyFvImzX6oGVgE4ksKQYbMZ3Ikw6L1Rv1J+ FvN0aNwOKgL2ztBRYscUGcQvA0Zo1fGCAn/BLEJvQYShWKeKqjyncVGoXFsz2AcuFKe1pwETSsN6 OZncjy32e4ktgs07cWBfx0v62b8md36jau+B6RVnnodaA8++oXl3FRwiEW8XfXWIjy4umIv93tb8 8ekYsfOfWkTSewZYXGoqe4RtK80ulMHb/dh2FZQIFyRdN4HOmB4FYO5sEYFr9YjHLmDkrUgNodJC XCeMe4BO4iaxUQARAQABzRdtYXJrdXMucHJvYnN0QHBvc3Rlby5kZcLBkQQTAQgAOxYhBIJ0GMT0 rFjncjDEczR2H/jnrUPSBQJog714AhsDBQsJCAcCAiICBhUKCQgLAgQWAgMBAh4HAheAAAoJEDR2 H/jnrUPSgd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o.de/keys/markus.probst@posteo.de.asc; preference=encrypt On Wed, 2025-10-15 at 16:52 +0200, Danilo Krummrich wrote: > On Wed Oct 15, 2025 at 3:44 PM CEST, Markus Probst wrote: > > On Mon, 2025-10-13 at 18:34 +0000, Alice RyhlV wrote: > > > On Sun, Oct 12, 2025 at 02:52:39PM +0000, Markus Probst wrote: > > > > Implement the core abstractions needed for led class devices, > > > > including: > > > >=20 > > > > * `led::LedOps` - the trait for handling leds, including > > > > =C2=A0 `brightness_set`, `brightness_get` and `blink_set` > > > >=20 > > > > * `led::InitData` - data set for the led class device > > > >=20 > > > > * `led::Device` - a safe wrapper around `led_classdev` > > > >=20 > > > > Signed-off-by: Markus Probst > > >=20 > > > > +pub trait LedOps: Send + 'static + Sized { > > > > +=C2=A0=C2=A0=C2=A0 /// If set true, [`LedOps::brightness_set`] and > > > > [`LedOps::blink_set`] must not sleep > > > > +=C2=A0=C2=A0=C2=A0 /// and perform the operation immediately. > > > > +=C2=A0=C2=A0=C2=A0 const BLOCKING: bool; > > > > +=C2=A0=C2=A0=C2=A0 /// The max brightness level > > > > +=C2=A0=C2=A0=C2=A0 const MAX_BRIGHTNESS: u32; > > > > + > > > > +=C2=A0=C2=A0=C2=A0 /// Sets the brightness level. > > > > +=C2=A0=C2=A0=C2=A0 /// > > > > +=C2=A0=C2=A0=C2=A0 /// See also [`LedOps::BLOCKING`] > > > > +=C2=A0=C2=A0=C2=A0 fn brightness_set(&self, brightness: u32) -> Re= sult<()>; > > > > + > > > > +=C2=A0=C2=A0=C2=A0 /// Gets the current brightness level. > > > > +=C2=A0=C2=A0=C2=A0 fn brightness_get(&self) -> u32 { > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 build_error!(VTABLE_DEF= AULT_ERROR) > > > > +=C2=A0=C2=A0=C2=A0 } > > > > + > > > > +=C2=A0=C2=A0=C2=A0 /// Activates hardware accelerated blinking. > > > > +=C2=A0=C2=A0=C2=A0 /// > > > > +=C2=A0=C2=A0=C2=A0 /// delays are in milliseconds. If both are zer= o, a > > > > sensible > > > > default should be chosen. > > > > +=C2=A0=C2=A0=C2=A0 /// The caller should adjust the timings in tha= t case and > > > > if > > > > it can't match the values > > > > +=C2=A0=C2=A0=C2=A0 /// specified exactly. Setting the brightness t= o 0 will > > > > disable the hardware accelerated > > > > +=C2=A0=C2=A0=C2=A0 /// blinking. > > > > +=C2=A0=C2=A0=C2=A0 /// > > > > +=C2=A0=C2=A0=C2=A0 /// See also [`LedOps::BLOCKING`] > > > > +=C2=A0=C2=A0=C2=A0 fn blink_set(&self, _delay_on: &mut usize, _del= ay_off: > > > > &mut > > > > usize) -> Result<()> { > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 build_error!(VTABLE_DEF= AULT_ERROR) > > > > +=C2=A0=C2=A0=C2=A0 } > > >=20 > > > These functions should probably take a &Device argument so > > > that > > > they can use methods that require a bound device (such as IO). > > How about instead something like > >=20 > > mod device { > >=20 > > =C2=A0 unsafe trait Container: AsRef> { > > =C2=A0=C2=A0=C2=A0 const Offset: usize; > >=20 > > =C2=A0=C2=A0=C2=A0 unsafe fn from_device(dev: &Device) -> &Self { > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 > > =C2=A0=C2=A0=C2=A0 } > > =C2=A0 } > >=20 > > =C2=A0 unsafe impl Device for Container { > > =C2=A0=C2=A0=C2=A0 const Offset: usize =3D 0; > > =C2=A0 } > >=20 > > } > >=20 > > And instead of passing &Device to the functions, we should > > add a > > type parameter to LedOps, e.g.: > >=20 > > trait LedOps> { > >=20 > > =C2=A0 ... > >=20 > > =C2=A0 fn brightness_set(&self, dev: &T, brightness: u32) -> Result<()>= ; > >=20 > > =C2=A0 ... > >=20 > > } > >=20 > > impl, E: device::Container> Device { > >=20 > > =C2=A0 pub fn new<'a>( > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 parent: &'a E, > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 init_data: InitData<'a>, > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ops: T, > > =C2=A0=C2=A0=C2=A0 ) -> impl PinInit, Error> + 'a { > > =C2=A0=C2=A0=C2=A0=C2=A0 ... > > =C2=A0 } > >=20 > > =C2=A0 ... > >=20 > > } > >=20 > > In the example of i2c (or any other container for `struct device`), > > we > > implement the device::Container trait: > >=20 > > mod i2c { > >=20 > > =C2=A0 unsafe impl device::Container for I2cClient { > > =C2=A0=C2=A0=C2=A0 const Offset: usize =3D offset_of!(bindings::i2c_cli= ent, dev); > > =C2=A0 } > >=20 > > } > > This allows the LedOps function to use any functions from the > > I2cClient > > or any other device container which may be used (removing the need > > to > > store it inside the LedOps implementations struct). It still allows > > Device to be used, as it also would implement > > device::Container. >=20 > I had a similar idea in the past, but it has some disadvantages: >=20 > =C2=A0 (1) You have to implement the upcast from a generic device to a bu= s > device for > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 every bus device. Not necessarily every, like I said `container::Device` itself also would implement `device::Container` (still allowing &Device). >=20 > =C2=A0 (2) You have to store a Box in the Rust LED class device; > the C struct > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 device can't carry the fat pointer. Why carry a fat pointer (assuming you mean pointers to unsized types)? We already know the address of it with the `struct led_classdev`- >`parent` field, we just need to substract the offset from `::Offset`, and we have the address of the device container (like `struct i2c_client`). No Box needed. Thanks - Markus Probst >=20 > The alternative would be to provide a safe method for bus devices to > upgrade to > a bound device by presenting its corresponding &Device base > device, for > instance: >=20 > impl I2cClient { > =C2=A0=C2=A0=C2=A0 pub fn into_bound<'a>(&'a self, &'a Device) -> > Result<&'a I2cClient> { > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // Fails if the presented `&D= evice base device of `self`. > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ... > =C2=A0=C2=A0=C2=A0 } > } >=20 > The advantage is that this can easily be implemented with a macro for > all bus > devices. >=20 > There is a slight downside in ergonomics due to the into_bound() call > though: >=20 > fn brightness_set(&self, parent: &Device, brightness: > u32) -> Result { > =C2=A0=C2=A0=C2=A0 let i2c: &I2cClient =3D > self.i2c.into_bound(parent)?; >=20 > =C2=A0=C2=A0=C2=A0 ... > }