@@ -64,6 +64,7 @@ USB_NOCACHE_RAM_SECTION struct usbd_core_priv {
6464 /** Currently selected configuration */
6565 uint8_t configuration ;
6666 uint8_t device_address ;
67+ uint8_t ep0_next_state ;
6768 bool self_powered ;
6869 bool remote_wakeup_support ;
6970 bool remote_wakeup_enabled ;
@@ -1118,6 +1119,7 @@ void usbd_event_reset_handler(uint8_t busid)
11181119 usbd_set_address (busid , 0 );
11191120 g_usbd_core [busid ].device_address = 0 ;
11201121 g_usbd_core [busid ].configuration = 0 ;
1122+ g_usbd_core [busid ].ep0_next_state = USBD_EP0_STATE_SETUP ;
11211123#ifdef CONFIG_USBDEV_ADVANCE_DESC
11221124 g_usbd_core [busid ].speed = USB_SPEED_UNKNOWN ;
11231125#endif
@@ -1162,12 +1164,14 @@ static void __usbd_event_ep0_setup_complete_handler(uint8_t busid, struct usb_se
11621164 /* handle class request when all the data is received */
11631165 if (setup -> wLength && ((setup -> bmRequestType & USB_REQUEST_DIR_MASK ) == USB_REQUEST_DIR_OUT )) {
11641166 USB_LOG_DBG ("Start reading %d bytes from ep0\r\n" , setup -> wLength );
1167+ g_usbd_core [busid ].ep0_next_state = USBD_EP0_STATE_OUT_DATA ;
11651168 usbd_ep_start_read (busid , USB_CONTROL_OUT_EP0 , g_usbd_core [busid ].ep0_data_buf , setup -> wLength );
11661169 return ;
11671170 }
11681171
11691172 /* Ask installed handler to process request */
11701173 if (!usbd_setup_request_handler (busid , setup , & buf , & g_usbd_core [busid ].ep0_data_buf_len )) {
1174+ g_usbd_core [busid ].ep0_next_state = USBD_EP0_STATE_SETUP ;
11711175 usbd_ep_set_stall (busid , USB_CONTROL_IN_EP0 );
11721176 return ;
11731177 }
@@ -1176,6 +1180,7 @@ static void __usbd_event_ep0_setup_complete_handler(uint8_t busid, struct usb_se
11761180 g_usbd_core [busid ].ep0_data_buf_residue = MIN (g_usbd_core [busid ].ep0_data_buf_len , setup -> wLength );
11771181 if (g_usbd_core [busid ].ep0_data_buf_residue > CONFIG_USBDEV_REQUEST_BUFFER_LEN ) {
11781182 USB_LOG_ERR ("Request buffer too small\r\n" );
1183+ g_usbd_core [busid ].ep0_next_state = USBD_EP0_STATE_SETUP ;
11791184 usbd_ep_set_stall (busid , USB_CONTROL_IN_EP0 );
11801185 return ;
11811186 }
@@ -1193,6 +1198,12 @@ static void __usbd_event_ep0_setup_complete_handler(uint8_t busid, struct usb_se
11931198 /* use memcpy(*data, xxx, len); has copied into ep0 buffer, we do nothing */
11941199 }
11951200
1201+ if (g_usbd_core [busid ].ep0_data_buf_residue > 0 ) {
1202+ g_usbd_core [busid ].ep0_next_state = USBD_EP0_STATE_IN_DATA ;
1203+ } else {
1204+ g_usbd_core [busid ].ep0_next_state = USBD_EP0_STATE_IN_STATUS ;
1205+ }
1206+
11961207 /* Send data or status to host */
11971208 usbd_ep_start_write (busid , USB_CONTROL_IN_EP0 , g_usbd_core [busid ].ep0_data_buf , g_usbd_core [busid ].ep0_data_buf_residue );
11981209 /*
@@ -1247,7 +1258,13 @@ static void usbd_event_ep0_in_complete_handler(uint8_t busid, uint8_t ep, uint32
12471258 */
12481259 if (setup -> wLength && ((setup -> bmRequestType & USB_REQUEST_DIR_MASK ) == USB_REQUEST_DIR_IN )) {
12491260 /* if all data has sent completely, start reading out status */
1261+ g_usbd_core [busid ].ep0_next_state = USBD_EP0_STATE_OUT_STATUS ;
12501262 usbd_ep_start_read (busid , USB_CONTROL_OUT_EP0 , NULL , 0 );
1263+ return ;
1264+ }
1265+
1266+ if (g_usbd_core [busid ].ep0_next_state == USBD_EP0_STATE_IN_STATUS ) {
1267+ g_usbd_core [busid ].ep0_next_state = USBD_EP0_STATE_SETUP ;
12511268 }
12521269
12531270#ifdef CONFIG_USBDEV_TEST_MODE
@@ -1280,10 +1297,12 @@ static void usbd_event_ep0_out_complete_handler(uint8_t busid, uint8_t ep, uint3
12801297 /* Received all, send data to handler */
12811298 g_usbd_core [busid ].ep0_data_buf = g_usbd_core [busid ].req_data ;
12821299 if (!usbd_setup_request_handler (busid , setup , & g_usbd_core [busid ].ep0_data_buf , & g_usbd_core [busid ].ep0_data_buf_len )) {
1300+ g_usbd_core [busid ].ep0_next_state = USBD_EP0_STATE_SETUP ;
12831301 usbd_ep_set_stall (busid , USB_CONTROL_IN_EP0 );
12841302 return ;
12851303 }
12861304
1305+ g_usbd_core [busid ].ep0_next_state = USBD_EP0_STATE_IN_STATUS ;
12871306 /*Send status to host*/
12881307 usbd_ep_start_write (busid , USB_CONTROL_IN_EP0 , NULL , 0 );
12891308#endif
@@ -1292,6 +1311,7 @@ static void usbd_event_ep0_out_complete_handler(uint8_t busid, uint8_t ep, uint3
12921311 usbd_ep_start_read (busid , USB_CONTROL_OUT_EP0 , g_usbd_core [busid ].ep0_data_buf , g_usbd_core [busid ].ep0_data_buf_residue );
12931312 }
12941313 } else {
1314+ g_usbd_core [busid ].ep0_next_state = USBD_EP0_STATE_SETUP ;
12951315 /* Read out status completely, do nothing */
12961316 USB_LOG_DBG ("EP0 recv out status\r\n" );
12971317 }
@@ -1425,6 +1445,11 @@ int usbd_send_remote_wakeup(uint8_t busid)
14251445 }
14261446}
14271447
1448+ uint8_t usbd_get_ep0_next_state (uint8_t busid )
1449+ {
1450+ return g_usbd_core [busid ].ep0_next_state ;
1451+ }
1452+
14281453#ifdef CONFIG_USBDEV_EP0_THREAD
14291454static void usbdev_ep0_thread (CONFIG_USB_OSAL_THREAD_SET_ARGV )
14301455{
@@ -1451,10 +1476,12 @@ static void usbdev_ep0_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
14511476 /* Received all, send data to handler */
14521477 g_usbd_core [busid ].ep0_data_buf = g_usbd_core [busid ].req_data ;
14531478 if (!usbd_setup_request_handler (busid , setup , & g_usbd_core [busid ].ep0_data_buf , & g_usbd_core [busid ].ep0_data_buf_len )) {
1479+ g_usbd_core [busid ].ep0_next_state = USBD_EP0_STATE_SETUP ;
14541480 usbd_ep_set_stall (busid , USB_CONTROL_IN_EP0 );
14551481 continue ;
14561482 }
14571483
1484+ g_usbd_core [busid ].ep0_next_state = USBD_EP0_STATE_IN_STATUS ;
14581485 /*Send status to host*/
14591486 usbd_ep_start_write (busid , USB_CONTROL_IN_EP0 , NULL , 0 );
14601487 break ;
0 commit comments