diff options
| author | sakumisu <[email protected]> | 2026-10-11 20:57:59 +0800 |
|---|---|---|
| committer | sakumisu <[email protected]> | 2026-10-11 20:57:59 +0800 |
| commit | fc3a6e9eac848d4c2a783ee0f17ea169e37963ce (patch) | |
| tree | bba7f5541b7b879a608df0a172c02e7aff90e2c9 | |
| parent | c616753ea909e9d0a7bde3c6c6a05ce961b88dc3 (diff) | |
fix(class/cdc/usbd_cdc_ecm): read & write only after host sends set_interface
Signed-off-by: sakumisu <[email protected]>
| -rw-r--r-- | class/cdc/usbd_cdc_ecm.c | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/class/cdc/usbd_cdc_ecm.c b/class/cdc/usbd_cdc_ecm.c index dde40907..bb3071ee 100644 --- a/class/cdc/usbd_cdc_ecm.c +++ b/class/cdc/usbd_cdc_ecm.c @@ -27,6 +27,7 @@ volatile uint32_t g_cdc_ecm_tx_data_length = 0; static volatile uint8_t g_current_net_status = 0; static volatile uint8_t g_cmd_intf = 0; +static volatile bool g_cdc_ecm_opened = false; static uint32_t g_connect_speed_table[2] = { CDC_ECM_CONNECT_SPEED_UPSTREAM, CDC_ECM_CONNECT_SPEED_DOWNSTREAM }; @@ -122,12 +123,23 @@ void cdc_ecm_notify_handler(uint8_t busid, uint8_t event, void *arg) g_current_net_status = 0; g_cdc_ecm_rx_data_length = 0; g_cdc_ecm_tx_data_length = 0; + g_cdc_ecm_opened = false; break; case USBD_EVENT_CONFIGURED: + break; + case USBD_EVENT_SET_INTERFACE: { + struct usb_interface_descriptor *intf = (struct usb_interface_descriptor *)arg; + if (intf->bAlternateSetting) { + g_cdc_ecm_opened = true; #ifdef CONFIG_USBDEV_CDC_ECM_USING_LWIP - usbd_cdc_ecm_start_read(g_cdc_ecm_rx_buffer, CONFIG_CDC_ECM_ETH_MAX_SEGSZE); + usbd_cdc_ecm_start_read(g_cdc_ecm_rx_buffer, CONFIG_CDC_ECM_ETH_MAX_SEGSZE); #endif - break; + } else { + g_cdc_ecm_rx_data_length = 0; + g_cdc_ecm_tx_data_length = 0; + g_cdc_ecm_opened = false; + } + } break; default: break; @@ -171,6 +183,8 @@ void cdc_ecm_int_in(uint8_t busid, uint8_t ep, uint32_t nbytes) int usbd_cdc_ecm_start_write(uint8_t *buf, uint32_t len) { + int ret; + if (!usb_device_is_configured(0)) { return -USB_ERR_NOTCONN; } @@ -179,10 +193,19 @@ int usbd_cdc_ecm_start_write(uint8_t *buf, uint32_t len) return -USB_ERR_BUSY; } + if (!g_cdc_ecm_opened) { + return -USB_ERR_NOTCONN; + } + g_cdc_ecm_tx_data_length = len; USB_LOG_DBG("txlen:%d\r\n", g_cdc_ecm_tx_data_length); - return usbd_ep_start_write(0, cdc_ecm_ep_data[CDC_ECM_IN_EP_IDX].ep_addr, buf, len); + ret = usbd_ep_start_write(0, cdc_ecm_ep_data[CDC_ECM_IN_EP_IDX].ep_addr, buf, len); + if (ret < 0) { + g_cdc_ecm_tx_data_length = 0; + return ret; + } + return 0; } int usbd_cdc_ecm_start_read(uint8_t *buf, uint32_t len) @@ -191,6 +214,10 @@ int usbd_cdc_ecm_start_read(uint8_t *buf, uint32_t len) return -USB_ERR_NOTCONN; } + if (!g_cdc_ecm_opened) { + return -USB_ERR_NOTCONN; + } + g_cdc_ecm_rx_data_length = 0; return usbd_ep_start_read(0, cdc_ecm_ep_data[CDC_ECM_OUT_EP_IDX].ep_addr, buf, len); } |
