net: dsa: motorcomm: Add LED support
Subsystems (5)
David Yang
maintainerv11 cover net: dsa: motorcomm: Add LED support
LEDs can be described in the device tree using the standard LED node notation. Currently, only parallel mode and strict 1:1 mapping (i.e. the HW default) are supported.
v10: https://lore.kernel.org/r/[email protected]
- new patch to fix lock status check
- LED control block redesigned
v9: https://lore.kernel.org/r/[email protected]
- LED initialization sequence adjusted
- add DT description
v8: https://lore.kernel.org/r/[email protected]
- limit the led port number
v7: https://lore.kernel.org/r/[email protected]
- rebase
v6: https://lore.kernel.org/r/[email protected]
- per-port initialization support
v5: https://lore.kernel.org/r/[email protected]
- improve trigger selection
v4: https://lore.kernel.org/r/[email protected]
- fix some reg op typos
v3: https://lore.kernel.org/r/[email protected]
- fix null pointer dereference
- support polarity auto-configuration
v2: https://lore.kernel.org/r/[email protected]
- allocate LED structures only
- eliminate double locking
v1: https://lore.kernel.org/r/[email protected]
- set up polarity correctly
- do not set up .brightness_get() to prevent dead lo
David Yang (5):
net: dsa: yt921x: Check lock status with lockdep_assert_held_once()
net: dsa: motorcomm: Move to subdirectory
net: dsa: motorcomm: Split SMI module
net: dsa: motorcomm: Add LED support
dt-bindings: net: dsa: yt921x: Add LEDs definition example
.../bindings/net/dsa/motorcomm,yt921x.yaml | 23 +
MAINTAINERS | 2 +-
drivers/net/dsa/Kconfig | 10 +-
drivers/net/dsa/Makefile | 2 +-
drivers/net/dsa/motorcomm/Kconfig | 17 +
drivers/net/dsa/motorcomm/Makefile | 5 +
.../net/dsa/{yt921x.c => motorcomm/chip.c} | 228 +------
.../net/dsa/{yt921x.h => motorcomm/chip.h} | 15 +
drivers/net/dsa/motorcomm/leds.c | 641 ++++++++++++++++++
drivers/net/dsa/motorcomm/leds.h | 118 ++++
drivers/net/dsa/motorcomm/smi.c | 180 +++++
drivers/net/dsa/motorcomm/smi.h | 61 ++
12 files changed, 1083 insertions(+), 219 deletions(-)
create mode 100644 drivers/net/dsa/motorcomm/Kconfig
create mode 100644 drivers/net/dsa/motorcomm/Makefile
rename drivers/net/dsa/{yt921x.c => motorcomm/chip.c} (96%)
rename drivers/net/dsa/{yt921x.h => motorcomm/chip.h} (99%)
create mode 100644 drivers/net/dsa/motorcomm/leds.c
create mode 100644 drivers/net/dsa/motorcomm/leds.h
create mode 100644 drivers/net/dsa/motorcomm/smi.c
create mode 100644 drivers/net/dsa/motorcomm/smi.hSignature · 1 line
2.53.0
David Yang
maintainerPATCH v11 1/5 net: dsa: yt921x: Check lock status with lockdep_assert_held_once()
mutex_is_locked() was used to check the lock status. While it should not be triggered unless a bug is introduced, it does not warn on lock being held by another thread. Fix it with lockdep helpers.
--- drivers/net/dsa/yt921x.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/dsa/yt921x.c +2 -2
@@ -198,14 +198,14 @@ static u32 ethaddr_lo2_to_u32(const unsigned char *addr)static int yt921x_reg_read (struct yt921x_priv *priv,u32 reg,u32 *valp) { -WARN_ON (!mutex_is_locked (&priv->reg_lock)); +lockdep_assert_held_once (&priv->reg_lock);return priv->reg_ops->read (priv->reg_ctx, reg, valp); }static int yt921x_reg_write (struct yt921x_priv *priv,u32 reg,u32 val) { -WARN_ON (!mutex_is_locked (&priv->reg_lock)); +lockdep_assert_held_once (&priv->reg_lock);return priv->reg_ops->write (priv->reg_ctx, reg, val); }
Signature · 1 line
2.53.0
Re: [PATCH net-next v11 1/5] net: dsa: yt921x: Check lock status with lockdep_assert_held_once()
Quoted 6 lines · David Yang
mutex_is_locked()was used to check the lock status. While it should not be triggered unless a bug is introduced, it does not warn on lock being held by another thread. Fix it with lockdep helpers.Fixes 186623f4aa72 ("net: dsa: yt921x: Add support for Motorcomm YT921x")Signed-off-by David Yang <[email protected]>
Andrew
Jakub Kicinski
maintainerRe: [PATCH net-next v11 1/5] net: dsa: yt921x: Check lock status with lockdep_assert_held_once()
In reply to David YangQuoted 5 lines · On Mon, 31 Aug 2026 12:38:54 +0800 David Yang wrote:
mutex_is_locked()was used to check the lock status. While it should not be triggered unless a bug is introduced, it does not warn on lock being held by another thread. Fix it with lockdep helpers.Fixes 186623f4aa72 ("net: dsa: yt921x: Add support for Motorcomm YT921x")
If you're reposting please drop this Fixes tag. Either it's a bug that needs to go to net or it's a net-next improvement which doesn't need a Fixes tag.
David Yang
maintainerPATCH v11 2/5 net: dsa: motorcomm: Move to subdirectory
In reply to David Yangyt921x is already the longest single-file DSA driver, so it's time to split it into parts.
---
MAINTAINERS | 2 +-
drivers/net/dsa/Kconfig | 10 ++--------
drivers/net/dsa/Makefile | 2 +-
drivers/net/dsa/motorcomm/Kconfig | 8 ++++++++
drivers/net/dsa/motorcomm/Makefile | 3 +++
drivers/net/dsa/{yt921x.c => motorcomm/chip.c} | 2 +-
drivers/net/dsa/{yt921x.h => motorcomm/chip.h} | 0
7 files changed, 16 insertions(+), 11 deletions(-)
create mode 100644 drivers/net/dsa/motorcomm/Kconfig
create mode 100644 drivers/net/dsa/motorcomm/Makefile
rename drivers/net/dsa/{yt921x.c => motorcomm/chip.c} (99%)
rename drivers/net/dsa/{yt921x.h => motorcomm/chip.h} (100%)MAINTAINERS +1 -1
@@ -18422,7 +18422,7 @@ M: David Yang <[email protected]> L: [email protected] S: Maintained F: Documentation/devicetree/bindings/net/dsa/motorcomm,yt921x.yaml -F: drivers/net/dsa/yt921x.* +F: drivers/net/dsa/motorcomm/ F: net/dsa/tag_yt921x.c MOXA SMARTIO/INDUSTIO/INTELLIO SERIAL CARD
drivers/net/dsa/Kconfig +2 -8
@@ -82,6 +82,8 @@ config NET_DSA_MV88E6060 source "drivers/net/dsa/microchip/Kconfig" +source "drivers/net/dsa/motorcomm/Kconfig" + source "drivers/net/dsa/mv88e6xxx/Kconfig" source "drivers/net/dsa/mxl862xx/Kconfig"
@@ -168,12 +170,4 @@ config NET_DSA_VITESSE_VSC73XX_PLATFORM This enables support for the Vitesse VSC7385, VSC7388, VSC7395 and VSC7398 SparX integrated ethernet switches, connected over a CPU-attached address bus and work in memory-mapped I/O mode. - -config NET_DSA_YT921X - tristate "Motorcomm YT9215 ethernet switch chip support" - select NET_DSA_TAG_YT921X - select NET_IEEE8021Q_HELPERS if DCB - help - This enables support for the Motorcomm YT9215 ethernet switch - chip. endmenu
drivers/net/dsa/Makefile +1 -1
@@ -15,11 +15,11 @@ obj-$(CONFIG_NET_DSA_SMSC_LAN9303_MDIO) += lan9303_mdio.o obj-$(CONFIG_NET_DSA_VITESSE_VSC73XX) += vitesse-vsc73xx-core.o obj-$(CONFIG_NET_DSA_VITESSE_VSC73XX_PLATFORM) += vitesse-vsc73xx-platform.o obj-$(CONFIG_NET_DSA_VITESSE_VSC73XX_SPI) += vitesse-vsc73xx-spi.o -obj-$(CONFIG_NET_DSA_YT921X) += yt921x.o obj-y += b53/ obj-y += hirschmann/ obj-y += lantiq/ obj-y += microchip/ +obj-y += motorcomm/ obj-y += mv88e6xxx/ obj-y += mxl862xx/ obj-y += netc/
drivers/net/dsa/motorcomm/Kconfig new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0-only +config NET_DSA_YT921X + tristate "Motorcomm YT9215 ethernet switch chip support" + select NET_DSA_TAG_YT921X + select NET_IEEE8021Q_HELPERS if DCB + help + This enables support for the Motorcomm YT9215 ethernet switch + chip.
drivers/net/dsa/motorcomm/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_NET_DSA_YT921X) += yt921x.o +yt921x-objs := chip.o
drivers/net/dsa/motorcomm/chip.c renamed from drivers/net/dsa/yt921x.c +1 -1
@@ -26,7 +26,7 @@#include <net/ieee8021q.h> #include <net/pkt_cls.h> -#include "yt921x.h" +#include "chip.h" struct yt921x_mib_desc {unsigned int size;
drivers/net/dsa/motorcomm/chip.h renamed from drivers/net/dsa/yt921x.h +0 -0
Signature · 1 line
2.53.0
David Yang
maintainerPATCH v11 3/5 net: dsa: motorcomm: Split SMI module
In reply to David YangSMI operations are going to be used across different modules. Minor changes are applied to fix build errors.
--- drivers/net/dsa/motorcomm/Makefile | 1 + drivers/net/dsa/motorcomm/chip.c | 207 +---------------------------- drivers/net/dsa/motorcomm/smi.c | 180 +++++++++++++++++++++++++ drivers/net/dsa/motorcomm/smi.h | 61 +++++++++ 4 files changed, 243 insertions(+), 206 deletions(-) create mode 100644 drivers/net/dsa/motorcomm/smi.c create mode 100644 drivers/net/dsa/motorcomm/smi.h
drivers/net/dsa/motorcomm/Makefile +1 -0
@@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_NET_DSA_YT921X) += yt921x.o yt921x-objs := chip.o +yt921x-objs += smi.o
drivers/net/dsa/motorcomm/chip.c +1 -206
231 lines not on this page · raw message
drivers/net/dsa/motorcomm/smi.c new file +180 -0
180 lines not on this page · raw message
drivers/net/dsa/motorcomm/smi.h new file +61 -0
61 lines not on this page · raw message
Signature · 1 line
2.53.0
David Yang
maintainerPATCH v11 4/5 net: dsa: motorcomm: Add LED support
In reply to David YangEach port has at most 3 LEDs connected and can be declared using the standard LEDs structure.
Currently, only parallel mode and strict 1:1 mapping (i.e. the HW default) are supported.
--- drivers/net/dsa/motorcomm/Kconfig | 9 + drivers/net/dsa/motorcomm/Makefile | 1 + drivers/net/dsa/motorcomm/chip.c | 19 +- drivers/net/dsa/motorcomm/chip.h | 15 + drivers/net/dsa/motorcomm/leds.c | 641 +++++++++++++++++++++++++++++ drivers/net/dsa/motorcomm/leds.h | 118 ++++++ 6 files changed, 801 insertions(+), 2 deletions(-) create mode 100644 drivers/net/dsa/motorcomm/leds.c create mode 100644 drivers/net/dsa/motorcomm/leds.h
drivers/net/dsa/motorcomm/Kconfig +9 -0
@@ -6,3 +6,12 @@ config NET_DSA_YT921X help This enables support for the Motorcomm YT9215 ethernet switch chip. + +config NET_DSA_YT921X_LEDS + bool "LED support for Motorcomm YT9215" + default y + depends on NET_DSA_YT921X + depends on LEDS_CLASS=y || LEDS_CLASS=NET_DSA_YT921X + help + This enables support for controlling the LEDs attached to the + Motorcomm YT9215 switch chips.
drivers/net/dsa/motorcomm/Makefile +1 -0
@@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_NET_DSA_YT921X) += yt921x.o yt921x-objs := chip.o +yt921x-$(CONFIG_NET_DSA_YT921X_LEDS) += leds.o yt921x-objs += smi.o
drivers/net/dsa/motorcomm/chip.c +17 -2
@@ -26,6 +26,7 @@#include <net/pkt_cls.h> #include "chip.h" +#include "leds.h" #include "smi.h" struct yt921x_mib_desc {
@@ -151,8 +152,6 @@ static const struct yt921x_info yt921x_infos[] = { {} }; -#define YT921X_NAME "yt921x" -#define YT921X_VID_UNWARE 4095/* The interval should be small enough to avoid overflow of 32bit MIBs.
@@ -4225,6 +4224,15 @@ static int yt921x_edata_read(struct yt921x_priv *priv, u8 addr, u8 *valp)return yt921x_edata_read_cont (priv, addr, valp); } +static void yt921x_dsa_teardown (struct dsa_switch *ds) +{ +struct yt921x_priv *priv =to_yt921x_priv (ds); + +#if IS_ENABLED (CONFIG_NET_DSA_YT921X_LEDS ) +yt921x_leds_remove (priv); +#endif +} +static int yt921x_chip_detect (struct yt921x_priv *priv) {struct device *dev =to_device (priv);
@@ -4581,6 +4589,12 @@ static int yt921x_dsa_setup(struct dsa_switch *ds)if (res)return res; +#if IS_ENABLED (CONFIG_NET_DSA_YT921X_LEDS ) + res =yt921x_leds_setup (priv); +if (res) +dev_warn (dev,"Failed to setup LEDs: %d\n" , res); +#endif +return 0 ; }
@@ -4661,6 +4675,7 @@ static const struct dsa_switch_ops yt921x_dsa_switch_ops = { .port_add_dscp_prio = yt921x_dsa_port_add_dscp_prio,#endif /* chip */ + .teardown = yt921x_dsa_teardown, .setup = yt921x_dsa_setup, };
drivers/net/dsa/motorcomm/chip.h +15 -0
@@ -850,9 +850,14 @@ enum yt921x_fdb_entry_status {#define YT921X_ACL_NUM (YT921X_ACL_BLK_NUM * YT921X_ACL_ENT_PER_BLK)#define YT921X_UDF_NUM 8 +#define YT921X_LED_GROUP_NUM 3 +#define YT921X_LED_PORT_NUM 10 +/* 8 internal + 2 external + 1 mcu */ #define YT921X_PORT_NUM 11 +#define YT921X_NAME "yt921x" +#define yt921x_port_is_internal (port) ((port) < 8)#define yt921x_port_is_external (port) ((port) == 8 || (port) == 9)
@@ -938,6 +943,16 @@ struct yt921x_port {struct yt921x_mib mib;u64 rx_frames;u64 tx_frames; + +#if IS_ENABLED (CONFIG_NET_DSA_YT921X_LEDS ) +unsigned char led_duty; +unsigned short led_cycle; + +unsigned char led_duty_mask; +unsigned char led_cycle_mask; + +struct yt921x_led *leds[YT921X_LED_GROUP_NUM ]; +#endif };struct yt921x_reg_ops {
drivers/net/dsa/motorcomm/leds.c new file +641 -0
641 lines not on this page · raw message
drivers/net/dsa/motorcomm/leds.h new file +118 -0
118 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Lockless Time-of-Check to Time-of-Use (TOCTOU) race when accessing `dp->user` and `dp->conduit`…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Lockless Time-of-Check to Time-of-Use (TOCTOU) race when accessing dp->user and dp->conduit leads to garbage pointers and Use-After-Free (UAF).
- [Medium] Flawed register state restoration in yt921x_led_setup() error path clobbers concurrent modifications and conflates valid register states with sentinels. --
commit 47253868ab0bfc21ecc3dfb700f5599e47b8967c
Author: David Yang <[email protected]> Date: Mon Aug 31 04:38:57 2026 +0000
net: dsa: motorcomm: Add LED support
This patch adds LED support for the Motorcomm YT9215 ethernet switch chip, allowing each port to declare up to 3 LEDs using the standard LEDs structure. Currently, only parallel mode and strict 1:1 mapping are supported.
Quoted 5 lines
drivers/net/dsa/motorcomm/leds.c new file +0
-0
[ ... ]
Quoted 6 lines
drivers/net/dsa/motorcomm/leds.c new file +6
-0+static struct device *__maybe_unused +yt921x_cled_hw_control_get_device (struct led_classdev *led_cdev) +{ +struct yt921x_led *led =to_yt921x_led (led_cdev); +struct yt921x_port *pp =to_yt921x_port (led); +struct yt921x_priv *priv =to_yt921x_priv (pp);
drivers/net/dsa/motorcomm/leds.c line 364
Quoted 10 lines
drivers/net/dsa/motorcomm/leds.c new file +10
-0+struct dsa_port *dp; + + dp =dsa_to_port (&priv->ds, pp->index); +if (!dp) +return NULL ; + +if (dsa_port_is_user (dp)) +return !dp->user ?NULL : &dp->user->dev; +if (dsa_port_is_cpu (dp)) +return !dp->conduit ?NULL : &dp->conduit->dev;
[Severity: High]
Can accessing dp->user and dp->conduit twice without READ_ONCE() or lock protection lead to a lockless Time-of-Check to Time-of-Use (TOCTOU) race?
If dp->user is concurrently set to NULL by the DSA core during teardown, the compiler can emit two separate loads. The second load could evaluate as NULL, yielding an invalid offset pointer (&NULL->dev) and potentially causing a kernel crash or Use-After-Free.
During the teardown window, dsa_port_teardown() destroys and frees the user netdev before dsa_switch_teardown() invokes yt921x_dsa_teardown() to unregister the LEDs.
Would it be safer to read these pointers into local variables once or use READ_ONCE()?
[ ... ]
Quoted 4 lines
drivers/net/dsa/motorcomm/leds.c new file +4
-0+static int +yt921x_led_setup (struct yt921x_priv *priv,int port, +struct fwnode_handle *fwnode) +{
[ ... ]
drivers/net/dsa/motorcomm/leds.c line 490
Quoted 5 lines
drivers/net/dsa/motorcomm/leds.c new file +5
-0+ led2_val =U32_MAX ; + res =yt921x_reg_read (priv,YT921X_LED2_PORTn (port), &led2_val); +if (res) + goto revoke_led2; + mask =YT921X_LED2_PORT_FORCEn_M (group);
[Severity: Medium]
Does using U32_MAX as a sentinel here prevent restoration if the hardware register was legitimately set to 0xFFFFFFFF (all mask bits set)?
[ ... ]
drivers/net/dsa/motorcomm/leds.c line 544
Quoted 5 lines
drivers/net/dsa/motorcomm/leds.c new file +5
-0+revoke: +mutex_lock (&priv->reg_lock); +revoke_led2: +if (led2_val !=U32_MAX ) { + ret =yt921x_reg_write (priv,YT921X_LED2_PORTn (port), led2_val);
[Severity: Medium]
When restoring the register state here, the lock priv->reg_lock was previously released prior to calling devm_led_classdev_register_ext().
Does the unconditional write-back of led2_val overwrite the entire register, potentially destroying concurrent sysfs changes made to sibling LED groups on the same port during the unlocked window?
Could we use yt921x_reg_update_bits() instead of a full register write to only restore the specific mask bits modified by the failed initialization?
Quoted 5 lines
drivers/net/dsa/motorcomm/leds.c new file +5
-0+if (ret) +dev_err (dev, +"Failed to restore %s for LED %02d:%02u: %d\n" , +"LED2_PORT" , port, group, ret); + }
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Jakub Kicinski
maintainerThank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
Critical: 0 · High: 0 · Medium: 2 · Low: 5
- [Medium] yt921x_led_blink_set() (via the blink_set callback yt921x_cled_blink_set()) special-cases only the both-delays-zero…
- [Medium] LED class devices are removed only from the DSA .teardown callback (yt921x_dsa_teardown() → yt921x_leds_remove()), but…
- [Low] Dead error-handling plumbing in the LED setup chain:
yt921x_leds_setup() (drivers/net/dsa/motorcomm/leds.c) ends with an…
- [Low] Concern (assessed false positive): the driver programs only per- port LED registers (YT921X_LED0/1/2_PORTn) plus YT921X_LED_PAR_INV and…
- [Low] Concern (assessed resolved later in the series): the driver introduces an undocumented DT contract — a per-port leds child node,…
- [Low] yt921x_led_trigger_get() (hw_control_get) is not the inverse of yt921x_led_trigger_set() (hw_control_set): TRIGGER_NETDEV_LINK is…
- [Low] The revoke/revoke_led2 rollback in yt921x_led_setup() writes back a whole-register snapshot of YT921X_LED2_PORTn(port)…
--
net: dsa: motorcomm: Add LED support
This adds LED class device support to the Motorcomm yt921x DSA driver, with up to three LEDs per port described by the standard device tree LEDs structure, plus hardware offload of the netdev trigger. Only the hardware default (parallel mode, 1:1 mapping) is handled, and the LEDs are torn down from a new DSA teardown callback.
A few questions on the new code below.
Quoted 4 lines
drivers/net/dsa/motorcomm/chip.c +0
-0
[ ... ]
drivers/net/dsa/motorcomm/chip.c static int yt921x_edata_read(struct yt921x_priv *priv, u8 addr, u8 *valp) · line 4235
Quoted 13 lines
drivers/net/dsa/motorcomm/chip.c +9
-0@@ -4225,6 +4224,15 @@ static int yt921x_edata_read(struct yt921x_priv *priv, u8 addr, u8 *valp)return yt921x_edata_read_cont (priv, addr, valp); } +static void yt921x_dsa_teardown (struct dsa_switch *ds) +{ +struct yt921x_priv *priv =to_yt921x_priv (ds); + +#if IS_ENABLED (CONFIG_NET_DSA_YT921X_LEDS ) +yt921x_leds_remove (priv); +#endif +} +
[Severity: Medium]
Can removing the LEDs only from the DSA teardown callback leave the LED class devices registered after the port netdevs have been freed?
net/dsa/dsa.c tears the tree down in this order:
dsa_tree_teardown()
dsa_tree_teardown_ports()
dsa_port_teardown()
dsa_user_destroy(dp->user); /* ends in free_netdev() */
dp->user = NULL;
dsa_tree_teardown_switches()
dsa_switch_teardown()
ds->ops->teardown(ds) /* yt921x_leds_remove() */So between free_netdev() and the dp->user = NULL assignment, dp->user is a dangling pointer while the LEDs are still registered and reachable from sysfs.
yt921x_cled_hw_control_get_device() reads dp->user / dp->conduit with no rtnl_lock, no RCU and takes no reference:
if (dsa_port_is_user(dp)) return !dp->user ? NULL : &dp->user->dev;
and ledtrig-netdev dereferences the returned device immediately, with dev_name() in netdev_trig_activate() and to_net_dev() in validate_net_dev().
Would an "echo netdev > /sys/class/leds/<led>/trigger" racing with switch unbind, or with the dsa_tree_setup() error path, touch a freed net_device here? Would removing the LEDs per port before the netdevs go away, or validating dp->user under rtnl_lock, avoid that?
drivers/net/dsa/motorcomm/chip.c static int yt921x_dsa_setup(struct dsa_switch *ds) · line 4597
Quoted 10 lines
drivers/net/dsa/motorcomm/chip.c +6
-0@@ -4581,6 +4589,12 @@ static int yt921x_dsa_setup(struct dsa_switch *ds)if (res)return res; +#if IS_ENABLED (CONFIG_NET_DSA_YT921X_LEDS ) + res =yt921x_leds_setup (priv); +if (res) +dev_warn (dev,"Failed to setup LEDs: %d\n" , res); +#endif +
[Severity: Low]
Can this dev_warn() ever be reached? yt921x_leds_setup() in leds.c ends with an unconditional return 0 and drops the result of yt921x_leds_setup_port():
dsa_switch_for_each_port(dp, ds) {
...
yt921x_leds_setup_port(priv, port);
/* Allow partial configuration: LEDs are optional */
}
return 0;yt921x_leds_setup_port() in turn discards every yt921x_led_setup() return value, so the -EINVAL it produces for port >= YT921X_LED_PORT_NUM is also thrown away.
Should the error be propagated, or should this branch in yt921x_dsa_setup() be dropped?
Quoted 6 lines
drivers/net/dsa/motorcomm/leds.c new file +0
-0@@ -0,0 +1,641 @@
[ ... ]
Quoted 11 lines
drivers/net/dsa/motorcomm/leds.c new file +11
-0+static int +yt921x_led_blink_select (const struct yt921x_priv *priv,unsigned long on, +unsigned long off,unsigned short *cyclep, +unsigned char *dutyp) +{ +static const unsigned char dutys[] = { +YT921X_LED_DUTY (1 ,6 ), +YT921X_LED_DUTY (1 ,4 ), +YT921X_LED_DUTY (1 ,3 ), +YT921X_LED_DUTY (1 ,2 ), + };
[ ... ]
Quoted 7 lines
drivers/net/dsa/motorcomm/leds.c new file +7
-0+ duty_req =DIV_ROUND_CLOSEST (YT921X_LED_DUTY_DENOM * + (on > off ? off : on), cycle_req); +for (unsigned int i =ARRAY_SIZE (dutys) -1 ;; i--) +if (i ==0 || duty_req >= (dutys[i -1 ] + dutys[i]) /2 ) { + duty = dutys[i]; +break ; + }
[ ... ]
drivers/net/dsa/motorcomm/leds.c line 113
Quoted 10 lines
drivers/net/dsa/motorcomm/leds.c new file +10
-0+if (!*onp && !*offp) { + cycle =YT921X_LED_BLINK_DEF ; + duty = (pp->led_duty_mask & ~BIT (group)) ? pp->led_duty : +YT921X_LED_DUTY (1 ,2 ); + + use_cycle = false; + }else { + res =yt921x_led_blink_select (priv, *onp, *offp, &cycle, &duty); +if (res) +return res;
[Severity: Medium]
What happens when exactly one of *delay_on / *delay_off is zero? The LED core defines delay_on == 0 as steady off and delay_off == 0 as steady on, but drivers/leds/led-core.c led_blink_setup() returns as soon as blink_set() succeeds:
if (!test_bit(LED_BLINK_ONESHOT, &led_cdev->work_flags) && led_cdev->blink_set && !led_cdev->blink_set(led_cdev, delay_on, delay_off)) return;
so led_set_software_blink() and its zero-delay handling never run.
Only the both-zero case is special-cased here. For *onp == 0 and *offp == 500, yt921x_led_blink_select() computes duty_req == 0, the search bottoms out at dutys[0] (1/6), and yt921x_led_blink_set() then programs YT921X_LED1_PORT_BLINK_DUTY_5_6 with _COMP plus YT921X_LED2_PORT_FORCEn_BLINK(group) and returns 0.
Does the LED then blink at roughly 1/6 duty where a steady off was requested (and roughly 5/6 for delay_off == 0)?
drivers/net/dsa/motorcomm/leds.c line 188
Quoted 2 lines
drivers/net/dsa/motorcomm/leds.c new file +2
-0+ *onp =DIV_ROUND_CLOSEST (duty * cycle,YT921X_LED_DUTY_DENOM ); + *offp = cycle - *onp;
These two lines also replace the requested zero with a non-zero value, so the timer trigger's delay_on / delay_off files report timings user space never asked for. Would driving the steady states through YT921X_LED2_PORT_FORCEn_ON / _OFF, or returning -EOPNOTSUPP for a single zero delay, be preferable?
[ ... ]
Quoted 3 lines
drivers/net/dsa/motorcomm/leds.c new file +3
-0+static const struct yt921x_led_trigger_map yt921x_led_trigger_maps[] = { + {BIT (TRIGGER_NETDEV_LINK ), + YT921X_LEDx_PORT_ACT_DUPLEX_HALF | YT921X_LEDx_PORT_ACT_DUPLEX_FULL},
[ ... ]
Quoted 2 lines
drivers/net/dsa/motorcomm/leds.c new file +2
-0+ {BIT(TRIGGER_NETDEV_HALF_DUPLEX), YT921X_LEDx_PORT_ACT_DUPLEX_HALF}, + {BIT(TRIGGER_NETDEV_FULL_DUPLEX), YT921X_LEDx_PORT_ACT_DUPLEX_FULL},
[ ... ]
drivers/net/dsa/motorcomm/leds.c line 262
Quoted 7 lines
drivers/net/dsa/motorcomm/leds.c new file +7
-0+ *flagsp =0 ; +for (unsigned int i =0 ; i <ARRAY_SIZE (yt921x_led_trigger_maps); i++) { +const struct yt921x_led_trigger_map *map = &yt921x_led_trigger_maps[i]; + +if ((val & map->mask) == map->mask) + *flagsp |= map->flags; + }
[Severity: Low]
Is yt921x_led_trigger_get() intended to be the inverse of yt921x_led_trigger_set()?
TRIGGER_NETDEV_LINK is encoded as the union of the two duplex masks, and this read-back loop ORs in the flags of every entry whose mask bits are all set, with no early break. yt921x_led_trigger_set() instead matches the LINK entry first and breaks once the flags are consumed.
So a LED programmed with only BIT(TRIGGER_NETDEV_LINK) appears to read back as LINK | HALF_DUPLEX | FULL_DUPLEX. ledtrig-netdev keeps that value as the trigger state at activation:
rc = led_cdev->hw_control_get(led_cdev, &mode); if (!rc) trigger_data->mode = mode;
Would /sys/class/leds/<led>/half_duplex and full_duplex then report 1 even though only link indication was requested?
[ ... ]
drivers/net/dsa/motorcomm/leds.c line 442
Quoted 4 lines
drivers/net/dsa/motorcomm/leds.c new file +4
-0+if (group >=YT921X_LED_GROUP_NUM ) { +dev_err (dev,"Invalid LED reg %u for port %d\n" , group, port); +return -EINVAL ; + }
[Severity: Low]
This isn't a bug, but the device tree contract introduced here (a per-port leds child node, a per-LED reg in 0..2, LEDs only on ports below YT921X_LED_PORT_NUM) is not described in Documentation/devicetree/bindings/net/dsa/motorcomm,yt921x.yaml by this commit.
The follow-on patch in the same series, "dt-bindings: net: dsa: yt921x: Add LEDs definition example", adds the description text and a worked leds example, so this is only a question about ordering within the series.
What the schema still does not express is a maximum for reg, so a device tree with led@3 validates cleanly and is rejected only at probe time by the dev_err() above. Would adding that constraint to the binding be worthwhile?
[ ... ]
Quoted 4 lines
drivers/net/dsa/motorcomm/leds.c new file +4
-0+ led2_val =U32_MAX ; + res =yt921x_reg_read (priv,YT921X_LED2_PORTn (port), &led2_val); +if (res) + goto revoke_led2;
[ ... ]
Quoted 1 line
drivers/net/dsa/motorcomm/leds.c new file +1
-0+mutex_unlock (&priv->reg_lock);
[ ... ]
drivers/net/dsa/motorcomm/leds.c line 544
Quoted 11 lines
drivers/net/dsa/motorcomm/leds.c new file +11
-0+ res =devm_led_classdev_register_ext (dev, led_cdev, &init_data); +if (res) + goto revoke; + +return 0 ; + +revoke: +mutex_lock (&priv->reg_lock); +revoke_led2: +if (led2_val !=U32_MAX ) { + ret =yt921x_reg_write (priv,YT921X_LED2_PORTn (port), led2_val);
[Severity: Low]
Can this rollback clobber the other LED groups of the same port?
led2_val is a snapshot of the whole YT921X_LED2_PORTn(port) word, taken before mutex_unlock(&priv->reg_lock) and before devm_led_classdev_register_ext() runs. That word holds the FORCE and FORCE_BLINK fields of all three groups, and the groups set up in earlier loop iterations are already registered class devices reachable from sysfs.
If devm_led_classdev_register_ext() fails (for example -ENOMEM from devres_alloc()) while a concurrent brightness or delay_on write for a sibling LED has gone through yt921x_cled_brightness_set_blocking() or yt921x_cled_blink_set(), does the revoke path write the stale word back and silently revert that sibling's force/blink state, leaving pp->led_duty, pp->led_cycle and the masks out of sync with the hardware?
Would re-reading the register under reg_lock and using yt921x_reg_update_bits() with YT921X_LED2_PORT_FORCEn_M(group) | YT921X_LED2_PORT_FORCE_BLINKn_M(group) be safer here?
[ ... ]
drivers/net/dsa/motorcomm/leds.c line 628
Quoted 8 lines
drivers/net/dsa/motorcomm/leds.c new file +8
-0+int yt921x_leds_setup (struct yt921x_priv *priv) +{ +struct dsa_switch *ds = &priv->ds; +struct dsa_port *dp; + +/* LEDs are always enabled. There is no way to disable them altogether +* (as far as I know). +*/
[Severity: Low]
This isn't a bug, just a question about scope: the driver programs only the per-port registers (YT921X_LED0/1/2_PORTn) and YT921X_LED_PAR_INV, and never reads YT921X_LED_CTRL, YT921X_LED_PAR_PORTS or the parallel/serial mapping registers declared in leds.h.
As far as I can tell that is fine, since yt921x_dsa_setup() runs yt921x_chip_reset() (which writes YT921X_RST_HW, described in the code as almost the same as a GPIO hard reset) before yt921x_leds_setup(), so the LED block is back at the parallel mode plus 1:1 mapping default that the commit message names.
Is a check of YT921X_LED_CTRL_MODE_M worth adding anyway, or is the reset considered sufficient?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260831044818.4103477-1-mmyangfl%40gmail.com
David Yang
maintainerQuoted 53 lines · Jakub Kicinski
drivers/net/dsa/motorcomm/chip.c static int yt921x_edata_read(struct yt921x_priv *priv, u8 addr, u8 *valp) · line 4235
Quoted 13 lines
drivers/net/dsa/motorcomm/chip.c +9
-0@@ -4225,6 +4224,15 @@ static int yt921x_edata_read(struct yt921x_priv *priv, u8 addr, u8 *valp)return yt921x_edata_read_cont (priv, addr, valp); } +static void yt921x_dsa_teardown (struct dsa_switch *ds) +{ +struct yt921x_priv *priv =to_yt921x_priv (ds); + +#if IS_ENABLED (CONFIG_NET_DSA_YT921X_LEDS ) +yt921x_leds_remove (priv); +#endif +} +[Severity: Medium]
Can removing the LEDs only from the DSA teardown callback leave the LED class devices registered after the port netdevs have been freed?
net/dsa/dsa.ctears the tree down in this order:
dsa_tree_teardown()dsa_tree_teardown_ports() dsa_port_teardown() dsa_user_destroy(dp->user); /* ends in free_netdev() */ dp->user = NULL; dsa_tree_teardown_switches() dsa_switch_teardown() ds->ops->teardown(ds) /* yt921x_leds_remove() */So between
free_netdev()and thedp->user= NULL assignment,dp->useris a dangling pointer while the LEDs are still registered and reachable from sysfs.
yt921x_cled_hw_control_get_device()readsdp->user/dp->conduitwith nortnl_lock, no RCU and takes no reference:if (dsa_port_is_user(dp)) return !dp->user ? NULL : &dp->user->dev;and ledtrig-netdev dereferences the returned device immediately, with
dev_name()innetdev_trig_activate()andto_net_dev()invalidate_net_dev().Would an "echo netdev > /sys/class/leds/<led>/trigger" racing with switch unbind, or with the
dsa_tree_setup()error path, touch a freednet_devicehere? Would removing the LEDs per port before the netdevs go away, or validatingdp->userunderrtnl_lock, avoid that?Quoted 4 lines
drivers/net/dsa/motorcomm/chip.c +0
-0@@ -4581,6 +4589,12 @@ static int yt921x_dsa_setup(struct dsa_switch *ds)if (res)return res;
Quoted 6 lines
drivers/net/dsa/motorcomm/chip.c static int yt921x_dsa_setup(struct dsa_switch *ds) · line 4597
Quoted 6 lines
drivers/net/dsa/motorcomm/chip.c +6
-0+#if IS_ENABLED (CONFIG_NET_DSA_YT921X_LEDS ) + res =yt921x_leds_setup (priv); +if (res) +dev_warn (dev,"Failed to setup LEDs: %d\n" , res); +#endif +
Just a question: I don't see any protection against this in other DSA led drivers, like mv88e6 or qca8k.
Quoted 14 lines
[Severity: Low]
Can this
dev_warn()ever be reached?yt921x_leds_setup()inleds.cends with an unconditional return 0 and drops the result ofyt921x_leds_setup_port():dsa_switch_for_each_port(dp, ds) { ... yt921x_leds_setup_port(priv, port); /* Allow partial configuration: LEDs are optional */ } return 0;
Quoted 6 lines
yt921x_leds_setup_port()in turn discards everyyt921x_led_setup()return value, so the-EINVALit produces for port >=YT921X_LED_PORT_NUMis also thrown away.Should the error be propagated, or should this branch in
yt921x_dsa_setup()be dropped?
Intended, explained in the response for the previous version.
Quoted 14 lines
[Severity: Low]
Is
yt921x_led_trigger_get()intended to be the inverse ofyt921x_led_trigger_set()?
TRIGGER_NETDEV_LINKis encoded as the union of the two duplex masks, and this read-back loop ORs in the flags of every entry whose mask bits are all set, with no early break.yt921x_led_trigger_set()instead matches the LINK entry first and breaks once the flags are consumed.So a LED programmed with only
BIT(TRIGGER_NETDEV_LINK)appears to read back as LINK |HALF_DUPLEX|FULL_DUPLEX. ledtrig-netdev keeps that value as the trigger state at activation:
Quoted 6 lines
rc = led_cdev->hw_control_get(led_cdev, &mode); if (!rc) trigger_data->mode = mode;Would /sys/class/leds/<led>/half_duplex and
full_duplexthen report 1 even though only link indication was requested?
Yes.
Quoted 13 lines
drivers/net/dsa/motorcomm/leds.c line 442
Quoted 4 lines
drivers/net/dsa/motorcomm/leds.c new file +4
-0+if (group >=YT921X_LED_GROUP_NUM ) { +dev_err (dev,"Invalid LED reg %u for port %d\n" , group, port); +return -EINVAL ; + }[Severity: Low]
This isn't a bug, but the device tree contract introduced here (a per-port leds child node, a per-LED reg in 0..2, LEDs only on ports below
YT921X_LED_PORT_NUM) is not described inDocumentation/devicetree/bindings/net/dsa/motorcomm,yt921x.yamlby this commit.
Quoted 7 lines
The follow-on patch in the same series, "dt-bindings: net: dsa: yt921x: Add LEDs definition example", adds the description text and a worked leds example, so this is only a question about ordering within the series.
What the schema still does not express is a maximum for reg, so a device tree with led@3 validates cleanly and is rejected only at probe time by the
dev_err()above. Would adding that constraint to the binding be worthwhile?
I don't see a good notation to impose the constraint (from sibling DSA bindings).
Quoted 16 lines
drivers/net/dsa/motorcomm/leds.c line 628
Quoted 8 lines
drivers/net/dsa/motorcomm/leds.c new file +8
-0+int yt921x_leds_setup (struct yt921x_priv *priv) +{ +struct dsa_switch *ds = &priv->ds; +struct dsa_port *dp; + +/* LEDs are always enabled. There is no way to disable them altogether +* (as far as I know). +*/ [Severity: Low]
This isn't a bug, just a question about scope: the driver programs only the per-port registers (YT921X_LED0/1/2_PORTn) and
YT921X_LED_PAR_INV, and never readsYT921X_LED_CTRL,YT921X_LED_PAR_PORTSor the parallel/serial mapping registers declared inleds.h.
Quoted 8 lines
As far as I can tell that is fine, since
yt921x_dsa_setup()runsyt921x_chip_reset()(which writesYT921X_RST_HW, described in the code as almost the same as a GPIO hard reset) beforeyt921x_leds_setup(), so the LED block is back at the parallel mode plus 1:1 mapping default that the commit message names.Is a check of
YT921X_LED_CTRL_MODE_Mworth adding anyway, or is the reset considered sufficient?
Reset is sufficient.
Andrew Lunn
maintainerQuoted 9 lines
Quoted 9 lines
Would an "echo netdev > /sys/class/leds/<led>/trigger" racing with switch unbind, or with the
dsa_tree_setup()error path, touch a freednet_devicehere? Would removing the LEDs per port before the netdevs go away, or validatingdp->userunderrtnl_lock, avoid that?Quoted 4 lines
drivers/net/dsa/motorcomm/chip.c +0
-0@@ -4581,6 +4589,12 @@ static int yt921x_dsa_setup(struct dsa_switch *ds)if (res)return res;
Quoted 9 lines
Quoted 6 lines
drivers/net/dsa/motorcomm/chip.c static int yt921x_dsa_setup(struct dsa_switch *ds) · line 4597
Quoted 6 lines
drivers/net/dsa/motorcomm/chip.c +6
-0+#if IS_ENABLED (CONFIG_NET_DSA_YT921X_LEDS ) + res =yt921x_leds_setup (priv); +if (res) +dev_warn (dev,"Failed to setup LEDs: %d\n" , res); +#endif +Just a question: I don't see any protection against this in other DSA led drivers, like mv88e6 or qca8k.
Registering the LEDs uses devm_ so the life of the LEDs is the life of the chip. The LED trigger does however take a reference on the network device, so it should not be possible for it to disappear while the trigger is still bound to it.
Andrew
David Yang
maintainerPATCH v11 5/5 dt-bindings: net: dsa: yt921x: Add LEDs definition example
In reply to David YangAdd LEDs definition example for yt921x to describe how they should be defined for a correct usage.
--- .../bindings/net/dsa/motorcomm,yt921x.yaml | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+)
Documentation/devicetree/bindings/net/dsa/motorcomm,yt921x.yaml +23 -0
@@ -21,6 +21,9 @@ description: | Any port can be used as the CPU port. + Each port has at most 3 LEDs connected and can be declared using the standard + LEDs structure. + properties: compatible: const: motorcomm,yt9215
@@ -59,6 +62,7 @@ unevaluatedProperties: false examples: - | #include <dt-bindings/gpio/gpio.h> + #include <dt-bindings/leds/common.h> mdio { #address-cells = <1>;
@@ -113,6 +117,25 @@ examples: label = "lan1"; phy-mode = "internal"; phy-handle = <&sw_phy0>; + + leds { + #address-cells = <1>; + #size-cells = <0>; + + led@0 { + reg = <0>; + color = <LED_COLOR_ID_WHITE>; + function = LED_FUNCTION_LAN; + default-state = "keep"; + }; + + led@1 { + reg = <1>; + color = <LED_COLOR_ID_AMBER>; + function = LED_FUNCTION_LAN; + default-state = "keep"; + }; + }; }; ethernet-port@1 {
Signature · 1 line
2.53.0
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 30 lines · On Mon, 31 Aug 2026 12:38:53 +0800 you wrote:
LEDs can be described in the device tree using the standard LED node notation. Currently, only parallel mode and strict 1:1 mapping (i.e. the HW default) are supported.
v10: https://lore.kernel.org/r/[email protected]
- new patch to fix lock status check
- LED control block redesigned
v9: https://lore.kernel.org/r/[email protected]
- LED initialization sequence adjusted
- add DT description
v8: https://lore.kernel.org/r/[email protected]
- limit the led port number
v7: https://lore.kernel.org/r/[email protected]
- rebase
v6: https://lore.kernel.org/r/[email protected]
- per-port initialization support
v5: https://lore.kernel.org/r/[email protected]
- improve trigger selection
v4: https://lore.kernel.org/r/[email protected]
- fix some reg op typos
v3: https://lore.kernel.org/r/[email protected]
- fix null pointer dereference
- support polarity auto-configuration
v2: https://lore.kernel.org/r/[email protected]
- allocate LED structures only
- eliminate double locking
v1: https://lore.kernel.org/r/[email protected]
- set up polarity correctly
- do not set up .brightness_get() to prevent dead lo
Quoted 1 line
[...]
Here is the summary with links:
- [net-next,v11,1/5] net: dsa: yt921x: Check lock status with lockdep_assert_held_once()
https://git.kernel.org/netdev/net-next/c/b1950d0426aa - [net-next,v11,2/5] net: dsa: motorcomm: Move to subdirectory https://git.kernel.org/netdev/net-next/c/9c472a0d7c38 - [net-next,v11,3/5] net: dsa: motorcomm: Split SMI module https://git.kernel.org/netdev/net-next/c/e9423a64d382 - [net-next,v11,4/5] net: dsa: motorcomm: Add LED support https://git.kernel.org/netdev/net-next/c/9af9a8168213 - [net-next,v11,5/5] dt-bindings: net: dsa: yt921x: Add LEDs definition example https://git.kernel.org/netdev/net-next/c/5f28f63c5d19
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html