summaryrefslogtreecommitdiff
path: root/src/class
diff options
context:
space:
mode:
authorhathach <[email protected]>2025-06-11 16:23:54 +0700
committerhathach <[email protected]>2025-06-11 16:23:54 +0700
commitd3d505e8f7872871185029b91588ae41ac037288 (patch)
tree92a2ea3f40efd05f71abd6819dbe5a71a32b27fd /src/class
parent6346fbec16739d91fc9ece57216d40ca1c091936 (diff)
parent8e252203697bc74f622d31a0de113e0ad01c9704 (diff)
Merge branch 'master' into fork/HiFiPhile/h7rs
Diffstat (limited to 'src/class')
-rw-r--r--src/class/net/ecm_rndis_device.c51
-rw-r--r--src/class/net/ncm_device.c36
-rw-r--r--src/class/net/net_device.h5
-rw-r--r--src/class/vendor/vendor_device.c40
4 files changed, 101 insertions, 31 deletions
diff --git a/src/class/net/ecm_rndis_device.c b/src/class/net/ecm_rndis_device.c
index a54e6d662..299eb97c8 100644
--- a/src/class/net/ecm_rndis_device.c
+++ b/src/class/net/ecm_rndis_device.c
@@ -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);
}
@@ -181,8 +186,6 @@ uint16_t netd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
// Open endpoint pair for RNDIS
TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, 2, TUSB_XFER_BULK, &_netd_itf.ep_out, &_netd_itf.ep_in), 0);
- tud_network_init_cb();
-
// we are ready to transmit a packet
can_xmit = true;
@@ -196,11 +199,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,
},
};
@@ -259,7 +262,6 @@ bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t
// TODO should be merge with RNDIS's after endpoint opened
// Also should have opposite callback for application to disable network !!
- tud_network_init_cb();
can_xmit = true; // we are ready to transmit a packet
tud_network_recv_renew(); // prepare for incoming packets
}
@@ -286,7 +288,10 @@ 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);
- ecm_report(true);
+ // 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) {
@@ -363,9 +368,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 +402,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
diff --git a/src/class/net/ncm_device.c b/src/class/net/ncm_device.c
index f9fda0698..02833c5f1 100644
--- a/src/class/net/ncm_device.c
+++ b/src/class/net/ncm_device.c
@@ -110,6 +110,7 @@ typedef struct {
NOTIFICATION_DONE
} notification_xmit_state; // state of notification transmission
bool notification_xmit_is_running; // notification is currently transmitted
+ bool link_is_up; // current link state
// misc
bool tud_network_recv_renew_active; // tud_network_recv_renew() is active (avoid recursive invocations)
@@ -218,7 +219,7 @@ static void notification_xmit(uint8_t rhport, bool force_next) {
.direction = TUSB_DIR_IN
},
.bRequest = CDC_NOTIF_NETWORK_CONNECTION,
- .wValue = 1 /* Connected */,
+ .wValue = ncm_interface.link_is_up ? 1 : 0, /* Dynamic link state */
.wIndex = ncm_interface.itf_num,
.wLength = 0,
},
@@ -232,6 +233,7 @@ static void notification_xmit(uint8_t rhport, bool force_next) {
ncm_interface.notification_xmit_is_running = true;
} else {
TU_LOG_DRV(" NOTIFICATION_FINISHED\n");
+ ncm_interface.notification_xmit_is_running = false;
}
} // notification_xmit
@@ -755,6 +757,32 @@ static void tud_network_recv_renew_r(uint8_t rhport) {
tud_network_recv_renew();
} // tud_network_recv_renew
+/**
+ * Set the link state and send notification to host
+ */
+void tud_network_link_state(uint8_t rhport, bool is_up) {
+ TU_LOG_DRV("tud_network_link_state(%d, %d)\n", rhport, is_up);
+
+ if (ncm_interface.link_is_up == is_up) {
+ // No change in link state
+ return;
+ }
+
+ ncm_interface.link_is_up = is_up;
+
+ // Only send notification if we have an active data interface
+ if (ncm_interface.itf_data_alt != 1) {
+ TU_LOG_DRV(" link state notification skipped (interface not active)\n");
+ return;
+ }
+
+ // Reset notification state to send link state update
+ ncm_interface.notification_xmit_state = NOTIFICATION_CONNECTED;
+
+ // Trigger notification transmission
+ notification_xmit(rhport, false);
+}
+
//-----------------------------------------------------------------------------
//
// all the netd_*() stuff (interface TinyUSB -> driver)
@@ -774,6 +802,12 @@ 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
} // netd_init
/**
diff --git a/src/class/net/net_device.h b/src/class/net/net_device.h
index 4c9a92f2d..fff2623b7 100644
--- a/src/class/net/net_device.h
+++ b/src/class/net/net_device.h
@@ -87,6 +87,11 @@ void tud_network_init_cb(void);
// TODO removed later since it is not part of tinyusb stack
extern uint8_t tud_network_mac_address[6];
+//------------- NCM -------------//
+
+// Set the network link state (up/down) and notify the host
+void tud_network_link_state(uint8_t rhport, bool is_up);
+
//--------------------------------------------------------------------+
// INTERNAL USBD-CLASS DRIVER API
//--------------------------------------------------------------------+
diff --git a/src/class/vendor/vendor_device.c b/src/class/vendor/vendor_device.c
index 2fc0ac944..7f1fd8c41 100644
--- a/src/class/vendor/vendor_device.c
+++ b/src/class/vendor/vendor_device.c
@@ -196,8 +196,8 @@ void vendord_reset(uint8_t rhport) {
uint16_t vendord_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uint16_t max_len) {
TU_VERIFY(TUSB_CLASS_VENDOR_SPECIFIC == desc_itf->bInterfaceClass, 0);
+ const uint8_t* desc_end = (const uint8_t*)desc_itf + max_len;
const uint8_t* p_desc = tu_desc_next(desc_itf);
- const uint8_t* desc_end = (uint8_t const*)desc_itf + max_len;
// Find available interface
vendord_interface_t* p_vendor = NULL;
@@ -210,26 +210,26 @@ uint16_t vendord_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uin
TU_VERIFY(p_vendor, 0);
p_vendor->itf_num = desc_itf->bInterfaceNumber;
- uint8_t found_ep = 0;
- while (found_ep < desc_itf->bNumEndpoints) {
- // skip non-endpoint descriptors
- while ( (TUSB_DESC_ENDPOINT != tu_desc_type(p_desc)) && (p_desc < desc_end) ) {
- p_desc = tu_desc_next(p_desc);
- }
- if (p_desc >= desc_end) {
- break;
- }
-
- const tusb_desc_endpoint_t* desc_ep = (const tusb_desc_endpoint_t*) p_desc;
- TU_ASSERT(usbd_edpt_open(rhport, desc_ep));
- found_ep++;
+ while (tu_desc_is_valid(p_desc, desc_end)) {
+ const uint8_t desc_type = tu_desc_type(p_desc);
+ if (desc_type == TUSB_DESC_INTERFACE || desc_type == TUSB_DESC_INTERFACE_ASSOCIATION) {
+ break; // end of this interface
+ } else if (desc_type == TUSB_DESC_ENDPOINT) {
+ const tusb_desc_endpoint_t* desc_ep = (const tusb_desc_endpoint_t*) p_desc;
+ TU_ASSERT(usbd_edpt_open(rhport, desc_ep));
- if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) {
- tu_edpt_stream_open(&p_vendor->tx.stream, desc_ep);
- tud_vendor_n_write_flush((uint8_t)(p_vendor - _vendord_itf));
- } else {
- tu_edpt_stream_open(&p_vendor->rx.stream, desc_ep);
- TU_ASSERT(tu_edpt_stream_read_xfer(rhport, &p_vendor->rx.stream) > 0, 0); // prepare for incoming data
+ // open endpoint stream, skip if already opened
+ if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) {
+ if (p_vendor->tx.stream.ep_addr == 0) {
+ tu_edpt_stream_open(&p_vendor->tx.stream, desc_ep);
+ tud_vendor_n_write_flush((uint8_t)(p_vendor - _vendord_itf));
+ }
+ } else {
+ if (p_vendor->rx.stream.ep_addr == 0) {
+ tu_edpt_stream_open(&p_vendor->rx.stream, desc_ep);
+ TU_ASSERT(tu_edpt_stream_read_xfer(rhport, &p_vendor->rx.stream) > 0, 0); // prepare for incoming data
+ }
+ }
}
p_desc = tu_desc_next(p_desc);