@@ -48,9 +48,10 @@ typedef struct {
4848 uint8_t itf_num ; // Index number of Management Interface, +1 for Data Interface
4949 uint8_t itf_data_alt ; // Alternate setting of Data Interface. 0 : inactive, 1 : active
5050
51- uint8_t ep_notif ;
5251 uint8_t ep_in ;
5352 uint8_t ep_out ;
53+ uint16_t ep_size ; // bulk endpoint max packet size (IN and OUT assumed equal)
54+ uint8_t ep_notif ;
5455
5556 bool ecm_mode ;
5657
@@ -81,6 +82,13 @@ CFG_TUD_MEM_SECTION static netd_epbuf_t _netd_epbuf;
8182static bool can_xmit ;
8283static bool ecm_link_is_up = true; // Store link state for ECM mode
8384
85+ //--------------------------------------------------------------------+
86+ // Weak stubs: invoked if no strong implementation is available
87+ //--------------------------------------------------------------------+
88+ TU_ATTR_WEAK void tud_network_set_packet_filter_cb (uint16_t packet_filter ) {
89+ (void ) packet_filter ;
90+ }
91+
8492void tud_network_recv_renew (void ) {
8593 usbd_edpt_xfer (0 , _netd_itf .ep_out , _netd_epbuf .rx , NETD_PACKET_SIZE , false);
8694}
@@ -176,6 +184,9 @@ uint16_t netd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
176184 // Pair of endpoints
177185 TU_ASSERT (TUSB_DESC_ENDPOINT == tu_desc_type (p_desc ), 0 );
178186
187+ // Save the actual bulk endpoint size (IN and OUT assumed equal)
188+ _netd_itf .ep_size = tu_edpt_packet_size ((tusb_desc_endpoint_t const * ) p_desc );
189+
179190 if (_netd_itf .ecm_mode ) {
180191 // ECM by default is in-active, save the endpoint attribute
181192 // to open later when received setInterface
@@ -206,14 +217,15 @@ static void ecm_report(bool nc) {
206217 },
207218 };
208219
220+ const uint32_t link_bps = (tud_speed_get () == TUSB_SPEED_HIGH ) ? 480000000U : 12000000U ;
209221 const ecm_notify_t ecm_notify_csc = {
210222 .header = {
211223 .bmRequestType = 0xA1 ,
212224 .bRequest = 0x2A , /* CONNECTION_SPEED_CHANGE aka ConnectionSpeedChange */
213225 .wLength = 8 ,
214226 },
215- .downlink = 9728000 ,
216- .uplink = 9728000 ,
227+ .downlink = link_bps ,
228+ .uplink = link_bps ,
217229 };
218230
219231 ecm_notify_t notify = (nc ) ? ecm_notify_nc : ecm_notify_csc ;
@@ -285,6 +297,7 @@ bool netd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t
285297 if (_netd_itf .ecm_mode ) {
286298 /* the only required CDC-ECM Management Element Request is SetEthernetPacketFilter */
287299 if (0x43 /* SET_ETHERNET_PACKET_FILTER */ == request -> bRequest ) {
300+ tud_network_set_packet_filter_cb (request -> wValue );
288301 tud_control_xfer (rhport , request , NULL , 0 );
289302 // Only send connection notification if link is up
290303 if (ecm_link_is_up ) {
@@ -356,8 +369,7 @@ bool netd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
356369 /* data transmission finished */
357370 if (ep_addr == _netd_itf .ep_in ) {
358371 /* TinyUSB requires the class driver to implement ZLP (since ZLP usage is class-specific) */
359-
360- if (xferred_bytes && (0 == (xferred_bytes % CFG_TUD_NET_ENDPOINT_SIZE ))) {
372+ if (xferred_bytes > 0 && 0 == (xferred_bytes & (_netd_itf .ep_size - 1 ))) {
361373 do_in_xfer (NULL , 0 ); /* a ZLP is needed */
362374 } else {
363375 /* we're finally finished */
0 commit comments