From 6d3d33d997cd73a565e8355c84b67f0366d752ce Mon Sep 17 00:00:00 2001 From: Cedric Van den Bergh Date: Tue, 16 Jun 2026 23:28:07 +0100 Subject: [PATCH] ncm: add weak callback for initial link state netd_init resets link_is_up to a compile-time default, which is incorrect when the host reboots without power-cycling the device. Add tud_network_default_link_state_cb() so applications can return the actual physical link state. The weak default preserves existing CFG_TUD_NCM_DEFAULT_LINK_UP behaviour. --- src/class/net/ncm_device.c | 15 +++++++++------ src/class/net/net_device.h | 4 ++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/class/net/ncm_device.c b/src/class/net/ncm_device.c index a78e472c2..e5f441300 100644 --- a/src/class/net/ncm_device.c +++ b/src/class/net/ncm_device.c @@ -152,6 +152,14 @@ TU_ATTR_WEAK void tud_network_set_packet_filter_cb(uint16_t packet_filter) { (void) packet_filter; } +TU_ATTR_WEAK bool tud_network_default_link_state_cb(void) { + #ifdef CFG_TUD_NCM_DEFAULT_LINK_UP + return CFG_TUD_NCM_DEFAULT_LINK_UP; + #else + return true; + #endif +} + /** * This is the NTB parameter structure * @@ -852,12 +860,7 @@ void netd_init(void) { for (int i = 0; i < RECV_NTB_N; ++i) { ncm_interface.recv_free_ntb[i] = &ncm_epbuf.recv[i].ntb; } - // Default link state - can be configured via CFG_TUD_NCM_DEFAULT_LINK_UP - #ifdef CFG_TUD_NCM_DEFAULT_LINK_UP - ncm_interface.link_is_up = CFG_TUD_NCM_DEFAULT_LINK_UP; - #else - ncm_interface.link_is_up = true; // Default to link up if not set. - #endif + ncm_interface.link_is_up = tud_network_default_link_state_cb(); } // netd_init /** diff --git a/src/class/net/net_device.h b/src/class/net/net_device.h index 332df09b3..1ad069d92 100644 --- a/src/class/net/net_device.h +++ b/src/class/net/net_device.h @@ -106,6 +106,10 @@ extern uint8_t tud_network_mac_address[6]; // Optional callback: informs the application about host requested packet filter bits void tud_network_set_packet_filter_cb(uint16_t packet_filter); +// Optional callback: called during netd_init() to get the initial link state. +// Override to return the actual physical link state instead of the compile-time default. +bool tud_network_default_link_state_cb(void); + // Set the network link state (up/down) and notify the host void tud_network_link_state(uint8_t rhport, bool is_up);