Re: [PATCH net-next v3 1/3] net: phy: mxl-86110: add basic support for led_brightness_set op
From: Jakub Kicinski
Date: Thu Aug 21 2025 - 20:36:33 EST
On Wed, 20 Aug 2025 14:34:07 +0100 Daniel Golle wrote:
> > > + if (index >= MXL86110_MAX_LEDS)
> > > + return -EINVAL;
> > > +
> > > + /* force manual control */
> > > + set = MXL86110_COM_EXT_LED_GEN_CFG_LFE(index);
> > > + /* clear previous force mode */
> > > + mask = MXL86110_COM_EXT_LED_GEN_CFG_LFM(index);
> > > +
> > > + /* force LED to be permanently on */
> > > + if (value != LED_OFF)
> > > + set |= MXL86110_COM_EXT_LED_GEN_CFG_LFME(index);
> >
> > That is particularly complex. We know index is a u8, so why not
> > GENMASK_U8(1 + 3 * index, 3 * index)? But set is a u16, so
> > GENMASK_U16() would also be valid.
>
> I chose this construct to avoid reusing the macro parameter as gcc would
> rightously complain about that potentially having unexpected side-effects.
>
> Eg.
>
> #define FOO(a) ((a)+(a))
>
> Now with var=10, when calling FOO(var++) the result will be 21 and
> var will be equal to 12, which isn't intuitive without seeing the
> macro definition.
>
> Also using GENMASK_TYPE would not avoid the problem of macro
> parameter reuse.
>
> However, I agree that the macro itself is also weirdly complex and
> confusing (but at the same time also very common, a quick grep reveals
> hundreds of occurances of that pattern in Linux sources), so maybe we
> should introduce some generic helpers for this (quite common) use-case?
> I can do that, but I certainly can't take care of migrating all the
> existing uses of this pattern to switch to the new helper.
IMHO this is one of the cases where the code would be far clearer
without he macros..
#define MXL86110_COM_EXT_LED_GEN_CFG 0xA00B
# define MXL86110_COM_EXT_LED_GEN_CFG_LFME(x) (1 << (3 * (x)))
# define MXL86110_COM_EXT_LED_GEN_CFG_LFE(x) (2 << (3 * (x)))
# define MXL86110_COM_EXT_LED_GEN_CFG_MASK(x) (3 << (3 * (x)))
Right?