Add link state control support for ECM mode

Extends the link state control feature to CDC-ECM mode.
RNDIS mode prints state changes but doesn't send notifications
to the host yet (would require RNDIS_INDICATE_STATUS_MSG).

For ECM:
- Tracks link state and sends proper CDC notifications
- Handles notification endpoint busy conditions
- Only sends connection notification when link is actually up

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Andrew Leech
2025-05-27 15:06:27 +10:00
parent 5de4a23abe
commit 9021efcacb

View File

@@ -81,6 +81,7 @@ typedef struct {
static netd_interface_t _netd_itf;
CFG_TUD_MEM_SECTION static netd_epbuf_t _netd_epbuf;
static bool can_xmit;
static bool ecm_link_is_up = true; // Store link state for ECM mode
void tud_network_recv_renew(void) {
usbd_edpt_xfer(0, _netd_itf.ep_out, _netd_epbuf.rx, NETD_PACKET_SIZE);
@@ -95,7 +96,11 @@ void netd_report(uint8_t *buf, uint16_t len) {
const uint8_t rhport = 0;
len = tu_min16(len, sizeof(ecm_notify_t));
TU_VERIFY(usbd_edpt_claim(rhport, _netd_itf.ep_notif), );
if (!usbd_edpt_claim(rhport, _netd_itf.ep_notif)) {
TU_LOG1("ECM: Failed to claim notification endpoint\n");
return;
}
memcpy(_netd_epbuf.notify, buf, len);
usbd_edpt_xfer(rhport, _netd_itf.ep_notif, _netd_epbuf.notify, len);
}
@@ -196,11 +201,11 @@ uint16_t netd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
}
static void ecm_report(bool nc) {
const ecm_notify_t ecm_notify_nc = {
ecm_notify_t ecm_notify_nc = {
.header = {
.bmRequestType = 0xA1,
.bRequest = 0, /* NETWORK_CONNECTION aka NetworkConnection */
.wValue = 1, /* Connected */
.wValue = ecm_link_is_up ? 1 : 0, /* Use current link state */
.wLength = 0,
},
};
@@ -286,8 +291,11 @@ bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t
/* the only required CDC-ECM Management Element Request is SetEthernetPacketFilter */
if (0x43 /* SET_ETHERNET_PACKET_FILTER */ == request->bRequest) {
tud_control_xfer(rhport, request, NULL, 0);
// Only send connection notification if link is up
if (ecm_link_is_up) {
ecm_report(true);
}
}
} else {
if (request->bmRequestType_bit.direction == TUSB_DIR_IN) {
rndis_generic_msg_t* rndis_msg = (rndis_generic_msg_t*)((void*)_netd_epbuf.ctrl);
@@ -363,9 +371,8 @@ bool netd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
}
if (_netd_itf.ecm_mode && (ep_addr == _netd_itf.ep_notif)) {
if (sizeof(tusb_control_request_t) == xferred_bytes) {
ecm_report(false);
}
// Notification transfer complete - endpoint is now free
// Don't automatically send speed change notification after link state changes
}
return true;
@@ -398,4 +405,31 @@ void tud_network_xmit(void *ref, uint16_t arg) {
do_in_xfer(_netd_epbuf.tx, len);
}
// Set the network link state (up/down) and notify the host
void tud_network_link_state(uint8_t rhport, bool is_up) {
(void)rhport;
if (_netd_itf.ecm_mode) {
ecm_link_is_up = is_up;
// For ECM mode, send network connection notification only
// Don't trigger speed change notification for link state changes
ecm_notify_t notify = {
.header = {
.bmRequestType = 0xA1,
.bRequest = 0, /* NETWORK_CONNECTION */
.wValue = is_up ? 1 : 0, /* 0 = disconnected, 1 = connected */
.wLength = 0,
},
};
notify.header.wIndex = _netd_itf.itf_num;
netd_report((uint8_t *)&notify, sizeof(notify.header));
} else {
// For RNDIS mode, we would need to implement RNDIS status indication
// This is more complex and requires RNDIS_INDICATE_STATUS_MSG
// For now, RNDIS doesn't support dynamic link state changes
(void)is_up;
}
}
#endif