11/*
2- * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+ * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 */
@@ -227,8 +227,8 @@ static void usb_event_cb(const usb_host_client_event_msg_t *event_msg, void *arg
227227 const usb_device_desc_t * device_desc ;
228228 ESP_ERROR_CHECK (usb_host_get_device_descriptor (current_device , & device_desc ));
229229
230- bool opened = false;
231230 usbh_cdc_t * cdc ;
231+ bool if_opened = false;
232232 SLIST_FOREACH (cdc , & p_usbh_cdc_obj -> cdc_devices_list , list_entry ) {
233233 if ((cdc -> vid != CDC_HOST_ANY_VID && cdc -> vid != device_desc -> idVendor ) ||
234234 (cdc -> pid != CDC_HOST_ANY_PID && cdc -> pid != device_desc -> idProduct )) {
@@ -243,14 +243,14 @@ static void usb_event_cb(const usb_host_client_event_msg_t *event_msg, void *arg
243243 if (_cdc_open (cdc ) != ESP_OK ) {
244244 ESP_LOGE (TAG , "Failed to open cdc device: %d" , event_msg -> new_dev .address );
245245 } else {
246- opened = true;
246+ if_opened = true;
247247 ESP_LOGI (TAG , "Opened cdc device: %d" , event_msg -> new_dev .address );
248248 }
249249 }
250- if (!opened ) {
250+
251+ if (!if_opened ) {
251252 usb_host_device_close (p_usbh_cdc_obj -> cdc_client_hdl , current_device ); // Gracefully continue on error
252253 }
253-
254254 ESP_LOGI (TAG , "New device connected, address: %d" , event_msg -> new_dev .address );
255255 break ;
256256 }
@@ -394,7 +394,9 @@ static void in_xfer_cb(usb_transfer_t *in_xfer)
394394 // if ringbuffer overflow, drop the data
395395 ESP_LOGD (TAG , "in ringbuf full" );
396396 } else {
397- _ringbuf_push (cdc -> in_ringbuf_handle , in_xfer -> data_buffer , in_xfer -> actual_num_bytes , pdMS_TO_TICKS (TIMEOUT_USB_RINGBUF_MS ));
397+ if (_ringbuf_push (cdc -> in_ringbuf_handle , in_xfer -> data_buffer , in_xfer -> actual_num_bytes , pdMS_TO_TICKS (TIMEOUT_USB_RINGBUF_MS )) != ESP_OK ) {
398+ ESP_LOGE (TAG , "in ringbuf push failed" );
399+ }
398400 }
399401
400402 usb_host_transfer_submit (in_xfer );
@@ -607,6 +609,10 @@ static esp_err_t _cdc_open(usbh_cdc_t *cdc)
607609 assert (cdc );
608610 ESP_RETURN_ON_FALSE (cdc -> state == USBH_CDC_CLOSE , ESP_OK , TAG , );
609611
612+ if (cdc -> cbs .connect ) {
613+ cdc -> cbs .connect ((usbh_cdc_handle_t )cdc , cdc -> cbs .user_data );
614+ }
615+
610616 // Get Device and Configuration descriptors
611617 const usb_config_desc_t * config_desc ;
612618 const usb_device_desc_t * device_desc ;
@@ -616,7 +622,7 @@ static esp_err_t _cdc_open(usbh_cdc_t *cdc)
616622 cdc_parsed_info_t cdc_info ;
617623 ret = cdc_parse_interface_descriptor (device_desc , config_desc , cdc -> intf_idx , & cdc -> data .intf_desc , & cdc_info );
618624 if (ret != ESP_OK ) {
619- usb_host_device_close ( p_usbh_cdc_obj -> cdc_client_hdl , cdc -> dev_hdl ); // Gracefully continue on error
625+ goto err ;
620626 }
621627
622628 _ring_buffer_flush (cdc -> in_ringbuf_handle );
@@ -632,14 +638,9 @@ static esp_err_t _cdc_open(usbh_cdc_t *cdc)
632638 cdc -> vid = device_desc -> idVendor ;
633639 cdc -> pid = device_desc -> idProduct ;
634640
635- if (cdc -> cbs .connect ) {
636- cdc -> cbs .connect ((usbh_cdc_handle_t )cdc , cdc -> cbs .user_data );
637- }
638-
639641 return ESP_OK ;
640642
641643err :
642- usb_host_device_close (p_usbh_cdc_obj -> cdc_client_hdl , cdc -> dev_hdl ); // Gracefully continue on error
643644 return ret ;
644645}
645646
@@ -719,6 +720,9 @@ esp_err_t usbh_cdc_create(const usbh_cdc_device_config_t *config, usbh_cdc_handl
719720 if (cdc -> data .out_xfer_free_sem ) {
720721 vSemaphoreDelete (cdc -> data .out_xfer_free_sem );
721722 }
723+ if (cdc -> dev_hdl ) {
724+ usb_host_device_close (p_usbh_cdc_obj -> cdc_client_hdl , cdc -> dev_hdl );
725+ }
722726 if (cdc ) {
723727 free (cdc );
724728 }
@@ -842,7 +846,6 @@ esp_err_t usbh_cdc_desc_print(usbh_cdc_handle_t cdc_handle)
842846{
843847 ESP_RETURN_ON_FALSE (cdc_handle != NULL , ESP_ERR_INVALID_ARG , TAG , "cdc_handle is NULL" );
844848 usbh_cdc_t * cdc = (usbh_cdc_t * ) cdc_handle ;
845- ESP_RETURN_ON_FALSE (cdc -> state == USBH_CDC_OPEN , ESP_ERR_INVALID_STATE , TAG , "Device is not connected" );
846849 ESP_RETURN_ON_FALSE (cdc -> dev_hdl != NULL , ESP_ERR_INVALID_STATE , TAG , "Device is not open yet" );
847850
848851 const usb_device_desc_t * device_desc ;
0 commit comments