diff --git a/Components/USB/Include/rl_usb.h b/Components/USB/Include/rl_usb.h index fe8d8e15..ccffb67f 100644 --- a/Components/USB/Include/rl_usb.h +++ b/Components/USB/Include/rl_usb.h @@ -89,12 +89,12 @@ extern "C" { /// Status code values returned by USB library functions. typedef enum { usbOK = 0U, ///< Function completed with no error - usbTimeout, ///< Function completed; time-out occurred + usbTimeout, ///< Function timed out usbInvalidParameter, ///< Invalid Parameter error: a mandatory parameter was missing or specified an incorrect object usbThreadError = 0x10U, ///< CMSIS-RTOS Thread creation/termination failed usbTimerError, ///< CMSIS-RTOS Timer creation/deletion failed - usbSemaphoreError, ///< CMSIS-RTOS Semaphore creation failed - usbMutexError, ///< CMSIS-RTOS Mutex creation failed + usbSemaphoreError, ///< CMSIS-RTOS Semaphore creation/acquisition/release/deletion failed + usbMutexError, ///< CMSIS-RTOS Mutex creation/acquisition/release/deletion failed usbControllerError = 0x20U, ///< Controller does not exist usbDeviceError, ///< Device does not exist usbDriverError, ///< Driver function produced error @@ -2314,7 +2314,7 @@ extern usbStatus USBD_HID_Uninitialize (uint8_t instance); extern void USBD_HID_Reset (uint8_t instance); extern void USBD_HID_SetConfiguration (uint8_t instance); extern void USBD_HID_EndpointStart (uint8_t instance, uint8_t ep_addr); -extern void USBD_HID_Timer (void const *argument); +extern void USBD_HID_Timer (void *argument); extern usbStatus USBD_MSC_Initialize (uint8_t instance); extern usbStatus USBD_MSC_Uninitialize (uint8_t instance); diff --git a/Components/USB/Script/Headers/usbd_config_def_x.h b/Components/USB/Script/Headers/usbd_config_def_x.h index 0e790ab3..0de233a5 100644 --- a/Components/USB/Script/Headers/usbd_config_def_x.h +++ b/Components/USB/Script/Headers/usbd_config_def_x.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_config_def_$x.h * Purpose: USB Device (USBD) - Device $x Definitions @@ -5641,8 +5641,8 @@ #define USBD_CDC4_RNDIS (USBD$x_CDC4_RNDIS) #define USBD_CDC4_IF0_NUM (USBD$x_CUSTOM_CLASS_IF_CNT+USBD$x_ADC_IF_CNT+USBD$x_CDC0_IF_CNT+USBD$x_CDC1_IF_CNT+USBD$x_CDC2_IF_CNT+USBD$x_CDC3_IF_CNT) #define USBD_CDC4_IF1_NUM (USBD_CDC4_IF0_NUM+1) -#define USBD_CDC4_IF_CNT (USBD$x_CDC3_IF_CNT) -#define USBD_CDC4_EP_CNT (USBD$x_CDC3_EP_CNT) +#define USBD_CDC4_IF_CNT (USBD$x_CDC4_IF_CNT) +#define USBD_CDC4_EP_CNT (USBD$x_CDC4_EP_CNT) #define USBD_CDC4_IF0_STR_IDX (USBD$x_DEV_STR_CNT+USBD$x_CUSTOM_CLASS_STR_CNT+USBD$x_ADC_STR_CNT+USBD$x_CDC0_STR_CNT+USBD$x_CDC1_STR_CNT+USBD$x_CDC2_STR_CNT+USBD$x_CDC3_STR_CNT) #define USBD_CDC4_IF1_STR_IDX (USBD_CDC4_IF0_STR_IDX+1) #define USBD_CDC4_IMAC_STR_IDX (USBD_CDC4_IF0_STR_IDX+2) @@ -5871,8 +5871,8 @@ #define USBD_CDC5_RNDIS (USBD$x_CDC5_RNDIS) #define USBD_CDC5_IF0_NUM (USBD$x_CUSTOM_CLASS_IF_CNT+USBD$x_ADC_IF_CNT+USBD$x_CDC0_IF_CNT+USBD$x_CDC1_IF_CNT+USBD$x_CDC2_IF_CNT+USBD$x_CDC3_IF_CNT+USBD$x_CDC4_IF_CNT) #define USBD_CDC5_IF1_NUM (USBD_CDC5_IF0_NUM+1) -#define USBD_CDC5_IF_CNT (USBD$x_CDC3_IF_CNT) -#define USBD_CDC5_EP_CNT (USBD$x_CDC3_EP_CNT) +#define USBD_CDC5_IF_CNT (USBD$x_CDC5_IF_CNT) +#define USBD_CDC5_EP_CNT (USBD$x_CDC5_EP_CNT) #define USBD_CDC5_IF0_STR_IDX (USBD$x_DEV_STR_CNT+USBD$x_CUSTOM_CLASS_STR_CNT+USBD$x_ADC_STR_CNT+USBD$x_CDC0_STR_CNT+USBD$x_CDC1_STR_CNT+USBD$x_CDC2_STR_CNT+USBD$x_CDC3_STR_CNT+USBD$x_CDC4_STR_CNT) #define USBD_CDC5_IF1_STR_IDX (USBD_CDC5_IF0_STR_IDX+1) #define USBD_CDC5_IMAC_STR_IDX (USBD_CDC5_IF0_STR_IDX+2) @@ -6101,8 +6101,8 @@ #define USBD_CDC6_RNDIS (USBD$x_CDC6_RNDIS) #define USBD_CDC6_IF0_NUM (USBD$x_CUSTOM_CLASS_IF_CNT+USBD$x_ADC_IF_CNT+USBD$x_CDC0_IF_CNT+USBD$x_CDC1_IF_CNT+USBD$x_CDC2_IF_CNT+USBD$x_CDC3_IF_CNT+USBD$x_CDC4_IF_CNT+USBD$x_CDC5_IF_CNT) #define USBD_CDC6_IF1_NUM (USBD_CDC6_IF0_NUM+1) -#define USBD_CDC6_IF_CNT (USBD$x_CDC3_IF_CNT) -#define USBD_CDC6_EP_CNT (USBD$x_CDC3_EP_CNT) +#define USBD_CDC6_IF_CNT (USBD$x_CDC6_IF_CNT) +#define USBD_CDC6_EP_CNT (USBD$x_CDC6_EP_CNT) #define USBD_CDC6_IF0_STR_IDX (USBD$x_DEV_STR_CNT+USBD$x_CUSTOM_CLASS_STR_CNT+USBD$x_ADC_STR_CNT+USBD$x_CDC0_STR_CNT+USBD$x_CDC1_STR_CNT+USBD$x_CDC2_STR_CNT+USBD$x_CDC3_STR_CNT+USBD$x_CDC4_STR_CNT+USBD$x_CDC5_STR_CNT) #define USBD_CDC6_IF1_STR_IDX (USBD_CDC6_IF0_STR_IDX+1) #define USBD_CDC6_IMAC_STR_IDX (USBD_CDC6_IF0_STR_IDX+2) @@ -6331,8 +6331,8 @@ #define USBD_CDC7_RNDIS (USBD$x_CDC7_RNDIS) #define USBD_CDC7_IF0_NUM (USBD$x_CUSTOM_CLASS_IF_CNT+USBD$x_ADC_IF_CNT+USBD$x_CDC0_IF_CNT+USBD$x_CDC1_IF_CNT+USBD$x_CDC2_IF_CNT+USBD$x_CDC3_IF_CNT+USBD$x_CDC4_IF_CNT+USBD$x_CDC5_IF_CNT+USBD$x_CDC6_IF_CNT) #define USBD_CDC7_IF1_NUM (USBD_CDC7_IF0_NUM+1) -#define USBD_CDC7_IF_CNT (USBD$x_CDC3_IF_CNT) -#define USBD_CDC7_EP_CNT (USBD$x_CDC3_EP_CNT) +#define USBD_CDC7_IF_CNT (USBD$x_CDC7_IF_CNT) +#define USBD_CDC7_EP_CNT (USBD$x_CDC7_EP_CNT) #define USBD_CDC7_IF0_STR_IDX (USBD$x_DEV_STR_CNT+USBD$x_CUSTOM_CLASS_STR_CNT+USBD$x_ADC_STR_CNT+USBD$x_CDC0_STR_CNT+USBD$x_CDC1_STR_CNT+USBD$x_CDC2_STR_CNT+USBD$x_CDC3_STR_CNT+USBD$x_CDC4_STR_CNT+USBD$x_CDC5_STR_CNT+USBD$x_CDC6_STR_CNT) #define USBD_CDC7_IF1_STR_IDX (USBD_CDC7_IF0_STR_IDX+1) #define USBD_CDC7_IMAC_STR_IDX (USBD_CDC7_IF0_STR_IDX+2) diff --git a/Components/USB/Source/usb_lib_ver.c b/Components/USB/Source/usb_lib_ver.c index ae2b8058..e0267cf4 100644 --- a/Components/USB/Source/usb_lib_ver.c +++ b/Components/USB/Source/usb_lib_ver.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Common - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usb_lib_ver.c * Purpose: USB Common Device/Host - Version module @@ -9,5 +9,6 @@ #include #include "rl_usb.h" +#include "usb_lib_ver.h" const uint32_t usb_lib_version = MW_USB_VERSION; diff --git a/Components/USB/Source/usbd_cmsis_rtos2.c b/Components/USB/Source/usbd_cmsis_rtos2.c index ccb95775..d490792e 100644 --- a/Components/USB/Source/usbd_cmsis_rtos2.c +++ b/Components/USB/Source/usbd_cmsis_rtos2.c @@ -12,6 +12,13 @@ #include "usbd_compatibility.h" +#include "usbd_lib_core.h" +#include "usbd_lib_adc.h" +#include "usbd_lib_cdc.h" +#include "usbd_lib_custom_class.h" +#include "usbd_lib_hid.h" +#include "usbd_lib_msc.h" + // Local structure definitions typedef struct { uint32_t stack_size; // size of thread stack @@ -20,15 +27,6 @@ typedef struct { // Resources definition -// Import thread function prototypes -extern void USBD_Core_Thread (void *arg); -extern void USBD_ADC_Thread (void *arg); -extern void USBD_CDC_Int_Thread (void *arg); -extern void USBD_CDC_Bulk_Thread (void *arg); -extern void USBD_HID_Thread (void *arg); -extern void USBD_MSC_Thread (void *arg); -extern void USBD_CustomClass_EP_Thread (void *arg); - // Import thread definitions generated by each Device instance // in usbd_config_dev_n.c file (n = 0 .. 3) #ifdef RTE_USB_Device_0 @@ -1031,7 +1029,7 @@ void *USBD_TimerCreate (uint8_t instance) { timer_attr.cb_mem = &usbd_hid_timer_cb_mem[instance]; timer_attr.cb_size = sizeof(osRtxTimer_t); #endif - return ((void *)osTimerNew ((osTimerFunc_t)USBD_HID_Timer, osTimerPeriodic, (void *)((uint32_t)instance), &timer_attr)); + return ((void *)osTimerNew (USBD_HID_Timer, osTimerPeriodic, (void *)((uint32_t)instance), &timer_attr)); #else (void)instance; return NULL; diff --git a/Components/USB/Source/usbd_config.c b/Components/USB/Source/usbd_config.c index 249c4008..6602f54c 100644 --- a/Components/USB/Source/usbd_config.c +++ b/Components/USB/Source/usbd_config.c @@ -5962,55 +5962,72 @@ usbStatus USBD_ClassInitialize (uint8_t device) { status_ret = usbOK; #if (USBD_CUSTOM_CLASS_NUM > 0) for (instance = 0; instance < usbd_custom_class_num; instance++) { - if (usbd_custom_class_ptr[instance] != NULL) { - if (usbd_custom_class_ptr[instance]->dev_num == device) { - status = USBD_CustomClass_Initialize (instance); - if (status) status_ret = status; - } + if (usbd_custom_class_ptr[instance] == NULL) { + continue; + } + if (usbd_custom_class_ptr[instance]->dev_num == device) { + status = USBD_CustomClass_Initialize (instance); + if (status) status_ret = status; } } #endif #if (USBD_ADC_NUM > 0) for (instance = 0; instance < usbd_adc_num; instance++) { - if ((usbd_adc_ptr[instance]->spkr_data_ptr != NULL) || (usbd_adc_ptr[instance]->mic_data_ptr != NULL)) { - if (usbd_adc_ptr[instance]->dev_num == device) { - status = USBD_ADC_Initialize (instance); - if (status) status_ret = status; - } + if (usbd_adc_ptr[instance] == NULL) { + continue; + } + if ((usbd_adc_ptr[instance]->spkr_data_ptr == NULL) && (usbd_adc_ptr[instance]->mic_data_ptr == NULL)) { + continue; + } + if (usbd_adc_ptr[instance]->dev_num == device) { + status = USBD_ADC_Initialize (instance); + if (status) status_ret = status; } } #endif #if (USBD_CDC_NUM > 0) for (instance = 0; instance < usbd_cdc_num; instance++) { - if (usbd_cdc_ptr[instance]->data_ptr != NULL) { - if (usbd_cdc_ptr[instance]->dev_num == device) { - status = USBD_CDC_Initialize (instance); - if (status) status_ret = status; - } + if (usbd_cdc_ptr[instance] == NULL) { + continue; + } + if (usbd_cdc_ptr[instance]->data_ptr == NULL) { + continue; + } + if (usbd_cdc_ptr[instance]->dev_num == device) { + status = USBD_CDC_Initialize (instance); + if (status) status_ret = status; } } #endif #if (USBD_HID_NUM > 0) for (instance = 0; instance < usbd_hid_num; instance++) { - if (usbd_hid_ptr[instance]->data_ptr != NULL) { - if (usbd_hid_ptr[instance]->dev_num == device) { - status = USBD_HID_Initialize (instance); - if (status) status_ret = status; - } + if (usbd_hid_ptr[instance] == NULL) { + continue; + } + if (usbd_hid_ptr[instance]->data_ptr == NULL) { + continue; + } + if (usbd_hid_ptr[instance]->dev_num == device) { + status = USBD_HID_Initialize (instance); + if (status) status_ret = status; } } #endif #if (USBD_MSC_NUM > 0) for (instance = 0; instance < usbd_msc_num; instance++) { - if (usbd_msc_ptr[instance]->data_ptr != NULL) { - if (usbd_msc_ptr[instance]->dev_num == device) { - status = USBD_MSC_Initialize (instance); - if (status) status_ret = status; - } + if (usbd_msc_ptr[instance] == NULL) { + continue; + } + if (usbd_msc_ptr[instance]->data_ptr == NULL) { + continue; + } + if (usbd_msc_ptr[instance]->dev_num == device) { + status = USBD_MSC_Initialize (instance); + if (status) status_ret = status; } } #endif @@ -6025,55 +6042,72 @@ usbStatus USBD_ClassUninitialize (uint8_t device) { status_ret = usbOK; #if (USBD_CUSTOM_CLASS_NUM > 0) for (instance = 0; instance < usbd_custom_class_num; instance++) { - if (usbd_custom_class_ptr[instance] != NULL) { - if (usbd_custom_class_ptr[instance]->dev_num == device) { - status = USBD_CustomClass_Uninitialize (instance); - if (status) status_ret = status; - } + if (usbd_custom_class_ptr[instance] == NULL) { + continue; + } + if (usbd_custom_class_ptr[instance]->dev_num == device) { + status = USBD_CustomClass_Uninitialize (instance); + if (status) status_ret = status; } } #endif #if (USBD_ADC_NUM > 0) for (instance = 0; instance < usbd_adc_num; instance++) { - if ((usbd_adc_ptr[instance]->spkr_data_ptr != NULL) || (usbd_adc_ptr[instance]->mic_data_ptr != NULL)) { - if (usbd_adc_ptr[instance]->dev_num == device) { - status = USBD_ADC_Uninitialize (instance); - if (status) status_ret = status; - } + if (usbd_adc_ptr[instance] == NULL) { + continue; + } + if ((usbd_adc_ptr[instance]->spkr_data_ptr == NULL) && (usbd_adc_ptr[instance]->mic_data_ptr == NULL)) { + continue; + } + if (usbd_adc_ptr[instance]->dev_num == device) { + status = USBD_ADC_Uninitialize (instance); + if (status) status_ret = status; } } #endif #if (USBD_CDC_NUM > 0) for (instance = 0; instance < usbd_cdc_num; instance++) { - if (usbd_cdc_ptr[instance]->data_ptr != NULL) { - if (usbd_cdc_ptr[instance]->dev_num == device) { - status = USBD_CDC_Uninitialize (instance); - if (status) status_ret = status; - } + if (usbd_cdc_ptr[instance] == NULL) { + continue; + } + if (usbd_cdc_ptr[instance]->data_ptr == NULL) { + continue; + } + if (usbd_cdc_ptr[instance]->dev_num == device) { + status = USBD_CDC_Uninitialize (instance); + if (status) status_ret = status; } } #endif #if (USBD_HID_NUM > 0) for (instance = 0; instance < usbd_hid_num; instance++) { - if (usbd_hid_ptr[instance]->data_ptr != NULL) { - if (usbd_hid_ptr[instance]->dev_num == device) { - status = USBD_HID_Uninitialize (instance); - if (status) status_ret = status; - } + if (usbd_hid_ptr[instance] == NULL) { + continue; + } + if (usbd_hid_ptr[instance]->data_ptr == NULL) { + continue; + } + if (usbd_hid_ptr[instance]->dev_num == device) { + status = USBD_HID_Uninitialize (instance); + if (status) status_ret = status; } } #endif #if (USBD_MSC_NUM > 0) for (instance = 0; instance < usbd_msc_num; instance++) { - if (usbd_msc_ptr[instance]->data_ptr != NULL) { - if (usbd_msc_ptr[instance]->dev_num == device) { - status = USBD_MSC_Uninitialize (instance); - if (status) status_ret = status; - } + if (usbd_msc_ptr[instance] == NULL) { + continue; + } + if (usbd_msc_ptr[instance]->data_ptr == NULL) { + continue; + } + if (usbd_msc_ptr[instance]->dev_num == device) { + status = USBD_MSC_Uninitialize (instance); + if (status) status_ret = status; } } #endif @@ -6086,50 +6120,67 @@ void USBD_Reset (uint8_t device) { #if (USBD_CUSTOM_CLASS_NUM > 0) for (instance = 0; instance < usbd_custom_class_num; instance++) { - if (usbd_custom_class_ptr[instance] != NULL) { - if (usbd_custom_class_ptr[instance]->dev_num == device) { - USBD_CustomClass_Reset (instance); - } + if (usbd_custom_class_ptr[instance] == NULL) { + continue; + } + if (usbd_custom_class_ptr[instance]->dev_num == device) { + USBD_CustomClass_Reset (instance); } } #endif #if (USBD_ADC_NUM > 0) for (instance = 0; instance < usbd_adc_num; instance++) { - if ((usbd_adc_ptr[instance]->spkr_data_ptr != NULL) || (usbd_adc_ptr[instance]->mic_data_ptr != NULL)){ - if (usbd_adc_ptr[instance]->dev_num == device) { - USBD_ADC_Reset (instance); - } + if (usbd_adc_ptr[instance] == NULL) { + continue; + } + if ((usbd_adc_ptr[instance]->spkr_data_ptr == NULL) && (usbd_adc_ptr[instance]->mic_data_ptr == NULL)){ + continue; + } + if (usbd_adc_ptr[instance]->dev_num == device) { + USBD_ADC_Reset (instance); } } #endif #if (USBD_CDC_NUM > 0) for (instance = 0; instance < usbd_cdc_num; instance++) { - if (usbd_cdc_ptr[instance]->data_ptr != NULL) { - if (usbd_cdc_ptr[instance]->dev_num == device) { - USBD_CDC_Reset (instance); - } + if (usbd_cdc_ptr[instance] == NULL) { + continue; + } + if (usbd_cdc_ptr[instance]->data_ptr == NULL) { + continue; + } + if (usbd_cdc_ptr[instance]->dev_num == device) { + USBD_CDC_Reset (instance); } } #endif #if (USBD_HID_NUM > 0) for (instance = 0; instance < usbd_hid_num; instance++) { - if (usbd_hid_ptr[instance]->data_ptr != NULL) { - if (usbd_hid_ptr[instance]->dev_num == device) { - USBD_HID_Reset (instance); - } + if (usbd_hid_ptr[instance] == NULL) { + continue; + } + if (usbd_hid_ptr[instance]->data_ptr == NULL) { + continue; + } + if (usbd_hid_ptr[instance]->dev_num == device) { + USBD_HID_Reset (instance); } } #endif #if (USBD_MSC_NUM > 0) for (instance = 0; instance < usbd_msc_num; instance++) { - if (usbd_msc_ptr[instance]->data_ptr != NULL) { - if (usbd_msc_ptr[instance]->dev_num == device) { - USBD_MSC_Reset (instance); - } + if (usbd_msc_ptr[instance] == NULL) { + continue; + } + if (usbd_msc_ptr[instance]->data_ptr == NULL) { + continue; + } + if (usbd_msc_ptr[instance]->dev_num == device) { + USBD_MSC_Reset (instance); } } #endif @@ -6142,10 +6193,14 @@ void USBD_SetConfiguration (uint8_t device, uint8_t configuration) { if (configuration != 0U) { for (instance = 0; instance < usbd_hid_num; instance++) { - if (usbd_hid_ptr[instance]->data_ptr != NULL) { - if (usbd_hid_ptr[instance]->dev_num == device) { - USBD_HID_SetConfiguration (instance); - } + if (usbd_hid_ptr[instance] == NULL) { + continue; + } + if (usbd_hid_ptr[instance]->data_ptr == NULL) { + continue; + } + if (usbd_hid_ptr[instance]->dev_num == device) { + USBD_HID_SetConfiguration (instance); } } } @@ -6162,28 +6217,29 @@ void USBD_EndpointStart (uint8_t device, uint8_t ep_addr) { uint8_t i; for (instance = 0; instance < usbd_custom_class_num; instance++) { - if (usbd_custom_class_ptr[instance] != NULL) { - if (usbd_custom_class_ptr[instance]->dev_num == device) { - for (i = 0U; i < 4U; i++) { - if (usbd_custom_class_ptr[instance]->cc[i].if_en) { - if (((usbd_custom_class_ptr[instance]->cc[i].if_ep0_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep0_addr == ep_addr)) || - ((usbd_custom_class_ptr[instance]->cc[i].if_ep1_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep1_addr == ep_addr)) || - ((usbd_custom_class_ptr[instance]->cc[i].if_ep2_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep2_addr == ep_addr)) || - ((usbd_custom_class_ptr[instance]->cc[i].if_ep3_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep3_addr == ep_addr)) || - ((usbd_custom_class_ptr[instance]->cc[i].if_ep4_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep4_addr == ep_addr)) || - ((usbd_custom_class_ptr[instance]->cc[i].if_ep5_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep5_addr == ep_addr)) || - ((usbd_custom_class_ptr[instance]->cc[i].if_ep6_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep6_addr == ep_addr)) || - ((usbd_custom_class_ptr[instance]->cc[i].if_ep7_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep7_addr == ep_addr))) { - USBD_CustomClass_EndpointStart (instance, ep_addr); - } + if (usbd_custom_class_ptr[instance] == NULL) { + continue; + } + if (usbd_custom_class_ptr[instance]->dev_num == device) { + for (i = 0U; i < 4U; i++) { + if (usbd_custom_class_ptr[instance]->cc[i].if_en) { + if (((usbd_custom_class_ptr[instance]->cc[i].if_ep0_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep0_addr == ep_addr)) || + ((usbd_custom_class_ptr[instance]->cc[i].if_ep1_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep1_addr == ep_addr)) || + ((usbd_custom_class_ptr[instance]->cc[i].if_ep2_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep2_addr == ep_addr)) || + ((usbd_custom_class_ptr[instance]->cc[i].if_ep3_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep3_addr == ep_addr)) || + ((usbd_custom_class_ptr[instance]->cc[i].if_ep4_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep4_addr == ep_addr)) || + ((usbd_custom_class_ptr[instance]->cc[i].if_ep5_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep5_addr == ep_addr)) || + ((usbd_custom_class_ptr[instance]->cc[i].if_ep6_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep6_addr == ep_addr)) || + ((usbd_custom_class_ptr[instance]->cc[i].if_ep7_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep7_addr == ep_addr))) { + USBD_CustomClass_EndpointStart (instance, ep_addr); } } } @@ -6193,6 +6249,9 @@ void USBD_EndpointStart (uint8_t device, uint8_t ep_addr) { #if (USBD_ADC_NUM > 0) for (instance = 0; instance < usbd_adc_num; instance++) { + if (usbd_adc_ptr[instance] == NULL) { + continue; + } if (usbd_adc_ptr[instance]->dev_num == device) { if ((usbd_adc_ptr[instance]->spkr_data_ptr != NULL) && (usbd_adc_ptr[instance]->out_cfg != NULL)) { if ((usbd_adc_ptr[instance]->out_cfg->ep_iso) == ep_addr) { @@ -6210,13 +6269,17 @@ void USBD_EndpointStart (uint8_t device, uint8_t ep_addr) { #if (USBD_CDC_NUM > 0) for (instance = 0; instance < usbd_cdc_num; instance++) { - if (usbd_cdc_ptr[instance]->data_ptr != NULL) { - if (usbd_cdc_ptr[instance]->dev_num == device) { - if (((usbd_cdc_ptr[instance]->ep_int_in | 0x80) == ep_addr) || - ((usbd_cdc_ptr[instance]->ep_bulk_in | 0x80) == ep_addr) || - ((usbd_cdc_ptr[instance]->ep_bulk_out ) == ep_addr)) { - USBD_CDC_EndpointStart (instance, ep_addr); - } + if (usbd_cdc_ptr[instance] == NULL) { + continue; + } + if (usbd_cdc_ptr[instance]->data_ptr == NULL) { + continue; + } + if (usbd_cdc_ptr[instance]->dev_num == device) { + if (((usbd_cdc_ptr[instance]->ep_int_in | 0x80) == ep_addr) || + ((usbd_cdc_ptr[instance]->ep_bulk_in | 0x80) == ep_addr) || + ((usbd_cdc_ptr[instance]->ep_bulk_out ) == ep_addr)) { + USBD_CDC_EndpointStart (instance, ep_addr); } } } @@ -6224,12 +6287,16 @@ void USBD_EndpointStart (uint8_t device, uint8_t ep_addr) { #if (USBD_HID_NUM > 0) for (instance = 0; instance < usbd_hid_num; instance++) { - if (usbd_hid_ptr[instance]->data_ptr != NULL) { - if (usbd_hid_ptr[instance]->dev_num == device) { - if (((usbd_hid_ptr[instance]->ep_int_in | 0x80) == ep_addr) || - ((usbd_hid_ptr[instance]->ep_int_out ) == ep_addr)) { - USBD_HID_EndpointStart (instance, ep_addr); - } + if (usbd_hid_ptr[instance] == NULL) { + continue; + } + if (usbd_hid_ptr[instance]->data_ptr == NULL) { + continue; + } + if (usbd_hid_ptr[instance]->dev_num == device) { + if (((usbd_hid_ptr[instance]->ep_int_in | 0x80) == ep_addr) || + ((usbd_hid_ptr[instance]->ep_int_out ) == ep_addr)) { + USBD_HID_EndpointStart (instance, ep_addr); } } } @@ -6237,12 +6304,16 @@ void USBD_EndpointStart (uint8_t device, uint8_t ep_addr) { #if (USBD_MSC_NUM > 0) for (instance = 0; instance < usbd_msc_num; instance++) { - if (usbd_msc_ptr[instance]->data_ptr != NULL) { - if (usbd_msc_ptr[instance]->dev_num == device) { - if (((usbd_msc_ptr[instance]->ep_bulk_in | 0x80) == ep_addr) || - ((usbd_msc_ptr[instance]->ep_bulk_out ) == ep_addr)) { - USBD_MSC_EndpointStart (instance, ep_addr); - } + if (usbd_msc_ptr[instance] == NULL) { + continue; + } + if (usbd_msc_ptr[instance]->data_ptr == NULL) { + continue; + } + if (usbd_msc_ptr[instance]->dev_num == device) { + if (((usbd_msc_ptr[instance]->ep_bulk_in | 0x80) == ep_addr) || + ((usbd_msc_ptr[instance]->ep_bulk_out ) == ep_addr)) { + USBD_MSC_EndpointStart (instance, ep_addr); } } } @@ -6261,28 +6332,29 @@ void USBD_EndpointStop (uint8_t device, uint8_t ep_addr) { uint8_t i; for (instance = 0; instance < usbd_custom_class_num; instance++) { - if (usbd_custom_class_ptr[instance] != NULL) { - if (usbd_custom_class_ptr[instance]->dev_num == device) { - for (i = 0U; i < 4U; i++) { - if (usbd_custom_class_ptr[instance]->cc[i].if_en) { - if (((usbd_custom_class_ptr[instance]->cc[i].if_ep0_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep0_addr == ep_addr)) || - ((usbd_custom_class_ptr[instance]->cc[i].if_ep1_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep1_addr == ep_addr)) || - ((usbd_custom_class_ptr[instance]->cc[i].if_ep2_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep2_addr == ep_addr)) || - ((usbd_custom_class_ptr[instance]->cc[i].if_ep3_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep3_addr == ep_addr)) || - ((usbd_custom_class_ptr[instance]->cc[i].if_ep4_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep4_addr == ep_addr)) || - ((usbd_custom_class_ptr[instance]->cc[i].if_ep5_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep5_addr == ep_addr)) || - ((usbd_custom_class_ptr[instance]->cc[i].if_ep6_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep6_addr == ep_addr)) || - ((usbd_custom_class_ptr[instance]->cc[i].if_ep7_en != 0U) && - (usbd_custom_class_ptr[instance]->cc[i].if_ep7_addr == ep_addr))) { - USBD_CustomClass_EndpointStop (instance, ep_addr); - } + if (usbd_custom_class_ptr[instance] == NULL) { + continue; + } + if (usbd_custom_class_ptr[instance]->dev_num == device) { + for (i = 0U; i < 4U; i++) { + if (usbd_custom_class_ptr[instance]->cc[i].if_en) { + if (((usbd_custom_class_ptr[instance]->cc[i].if_ep0_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep0_addr == ep_addr)) || + ((usbd_custom_class_ptr[instance]->cc[i].if_ep1_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep1_addr == ep_addr)) || + ((usbd_custom_class_ptr[instance]->cc[i].if_ep2_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep2_addr == ep_addr)) || + ((usbd_custom_class_ptr[instance]->cc[i].if_ep3_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep3_addr == ep_addr)) || + ((usbd_custom_class_ptr[instance]->cc[i].if_ep4_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep4_addr == ep_addr)) || + ((usbd_custom_class_ptr[instance]->cc[i].if_ep5_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep5_addr == ep_addr)) || + ((usbd_custom_class_ptr[instance]->cc[i].if_ep6_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep6_addr == ep_addr)) || + ((usbd_custom_class_ptr[instance]->cc[i].if_ep7_en != 0U) && + (usbd_custom_class_ptr[instance]->cc[i].if_ep7_addr == ep_addr))) { + USBD_CustomClass_EndpointStop (instance, ep_addr); } } } @@ -6292,6 +6364,9 @@ void USBD_EndpointStop (uint8_t device, uint8_t ep_addr) { #if (USBD_ADC_NUM > 0) for (instance = 0; instance < usbd_adc_num; instance++) { + if (usbd_adc_ptr[instance] == NULL) { + continue; + } if (usbd_adc_ptr[instance]->dev_num == device) { if ((usbd_adc_ptr[instance]->spkr_data_ptr != NULL) && (usbd_adc_ptr[instance]->out_cfg != NULL)) { if ((usbd_adc_ptr[instance]->out_cfg->ep_iso) == ep_addr) { @@ -6309,13 +6384,17 @@ void USBD_EndpointStop (uint8_t device, uint8_t ep_addr) { #if (USBD_CDC_NUM > 0) for (instance = 0; instance < usbd_cdc_num; instance++) { - if (usbd_cdc_ptr[instance]->data_ptr != NULL) { - if (usbd_cdc_ptr[instance]->dev_num == device) { - if (((usbd_cdc_ptr[instance]->ep_int_in | 0x80) == ep_addr) || - ((usbd_cdc_ptr[instance]->ep_bulk_in | 0x80) == ep_addr) || - ((usbd_cdc_ptr[instance]->ep_bulk_out ) == ep_addr)) { - USBD_CDC_EndpointStop (instance, ep_addr); - } + if (usbd_cdc_ptr[instance] == NULL) { + continue; + } + if (usbd_cdc_ptr[instance]->data_ptr == NULL) { + continue; + } + if (usbd_cdc_ptr[instance]->dev_num == device) { + if (((usbd_cdc_ptr[instance]->ep_int_in | 0x80) == ep_addr) || + ((usbd_cdc_ptr[instance]->ep_bulk_in | 0x80) == ep_addr) || + ((usbd_cdc_ptr[instance]->ep_bulk_out ) == ep_addr)) { + USBD_CDC_EndpointStop (instance, ep_addr); } } } diff --git a/Components/USB/Source/usbd_config_def_0.h b/Components/USB/Source/usbd_config_def_0.h index f7e6a21f..2b42cb19 100644 --- a/Components/USB/Source/usbd_config_def_0.h +++ b/Components/USB/Source/usbd_config_def_0.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_config_def_0.h * Purpose: USB Device (USBD) - Device 0 Definitions @@ -5641,8 +5641,8 @@ #define USBD_CDC4_RNDIS (USBD0_CDC4_RNDIS) #define USBD_CDC4_IF0_NUM (USBD0_CUSTOM_CLASS_IF_CNT+USBD0_ADC_IF_CNT+USBD0_CDC0_IF_CNT+USBD0_CDC1_IF_CNT+USBD0_CDC2_IF_CNT+USBD0_CDC3_IF_CNT) #define USBD_CDC4_IF1_NUM (USBD_CDC4_IF0_NUM+1) -#define USBD_CDC4_IF_CNT (USBD0_CDC3_IF_CNT) -#define USBD_CDC4_EP_CNT (USBD0_CDC3_EP_CNT) +#define USBD_CDC4_IF_CNT (USBD0_CDC4_IF_CNT) +#define USBD_CDC4_EP_CNT (USBD0_CDC4_EP_CNT) #define USBD_CDC4_IF0_STR_IDX (USBD0_DEV_STR_CNT+USBD0_CUSTOM_CLASS_STR_CNT+USBD0_ADC_STR_CNT+USBD0_CDC0_STR_CNT+USBD0_CDC1_STR_CNT+USBD0_CDC2_STR_CNT+USBD0_CDC3_STR_CNT) #define USBD_CDC4_IF1_STR_IDX (USBD_CDC4_IF0_STR_IDX+1) #define USBD_CDC4_IMAC_STR_IDX (USBD_CDC4_IF0_STR_IDX+2) @@ -5871,8 +5871,8 @@ #define USBD_CDC5_RNDIS (USBD0_CDC5_RNDIS) #define USBD_CDC5_IF0_NUM (USBD0_CUSTOM_CLASS_IF_CNT+USBD0_ADC_IF_CNT+USBD0_CDC0_IF_CNT+USBD0_CDC1_IF_CNT+USBD0_CDC2_IF_CNT+USBD0_CDC3_IF_CNT+USBD0_CDC4_IF_CNT) #define USBD_CDC5_IF1_NUM (USBD_CDC5_IF0_NUM+1) -#define USBD_CDC5_IF_CNT (USBD0_CDC3_IF_CNT) -#define USBD_CDC5_EP_CNT (USBD0_CDC3_EP_CNT) +#define USBD_CDC5_IF_CNT (USBD0_CDC5_IF_CNT) +#define USBD_CDC5_EP_CNT (USBD0_CDC5_EP_CNT) #define USBD_CDC5_IF0_STR_IDX (USBD0_DEV_STR_CNT+USBD0_CUSTOM_CLASS_STR_CNT+USBD0_ADC_STR_CNT+USBD0_CDC0_STR_CNT+USBD0_CDC1_STR_CNT+USBD0_CDC2_STR_CNT+USBD0_CDC3_STR_CNT+USBD0_CDC4_STR_CNT) #define USBD_CDC5_IF1_STR_IDX (USBD_CDC5_IF0_STR_IDX+1) #define USBD_CDC5_IMAC_STR_IDX (USBD_CDC5_IF0_STR_IDX+2) @@ -6101,8 +6101,8 @@ #define USBD_CDC6_RNDIS (USBD0_CDC6_RNDIS) #define USBD_CDC6_IF0_NUM (USBD0_CUSTOM_CLASS_IF_CNT+USBD0_ADC_IF_CNT+USBD0_CDC0_IF_CNT+USBD0_CDC1_IF_CNT+USBD0_CDC2_IF_CNT+USBD0_CDC3_IF_CNT+USBD0_CDC4_IF_CNT+USBD0_CDC5_IF_CNT) #define USBD_CDC6_IF1_NUM (USBD_CDC6_IF0_NUM+1) -#define USBD_CDC6_IF_CNT (USBD0_CDC3_IF_CNT) -#define USBD_CDC6_EP_CNT (USBD0_CDC3_EP_CNT) +#define USBD_CDC6_IF_CNT (USBD0_CDC6_IF_CNT) +#define USBD_CDC6_EP_CNT (USBD0_CDC6_EP_CNT) #define USBD_CDC6_IF0_STR_IDX (USBD0_DEV_STR_CNT+USBD0_CUSTOM_CLASS_STR_CNT+USBD0_ADC_STR_CNT+USBD0_CDC0_STR_CNT+USBD0_CDC1_STR_CNT+USBD0_CDC2_STR_CNT+USBD0_CDC3_STR_CNT+USBD0_CDC4_STR_CNT+USBD0_CDC5_STR_CNT) #define USBD_CDC6_IF1_STR_IDX (USBD_CDC6_IF0_STR_IDX+1) #define USBD_CDC6_IMAC_STR_IDX (USBD_CDC6_IF0_STR_IDX+2) @@ -6331,8 +6331,8 @@ #define USBD_CDC7_RNDIS (USBD0_CDC7_RNDIS) #define USBD_CDC7_IF0_NUM (USBD0_CUSTOM_CLASS_IF_CNT+USBD0_ADC_IF_CNT+USBD0_CDC0_IF_CNT+USBD0_CDC1_IF_CNT+USBD0_CDC2_IF_CNT+USBD0_CDC3_IF_CNT+USBD0_CDC4_IF_CNT+USBD0_CDC5_IF_CNT+USBD0_CDC6_IF_CNT) #define USBD_CDC7_IF1_NUM (USBD_CDC7_IF0_NUM+1) -#define USBD_CDC7_IF_CNT (USBD0_CDC3_IF_CNT) -#define USBD_CDC7_EP_CNT (USBD0_CDC3_EP_CNT) +#define USBD_CDC7_IF_CNT (USBD0_CDC7_IF_CNT) +#define USBD_CDC7_EP_CNT (USBD0_CDC7_EP_CNT) #define USBD_CDC7_IF0_STR_IDX (USBD0_DEV_STR_CNT+USBD0_CUSTOM_CLASS_STR_CNT+USBD0_ADC_STR_CNT+USBD0_CDC0_STR_CNT+USBD0_CDC1_STR_CNT+USBD0_CDC2_STR_CNT+USBD0_CDC3_STR_CNT+USBD0_CDC4_STR_CNT+USBD0_CDC5_STR_CNT+USBD0_CDC6_STR_CNT) #define USBD_CDC7_IF1_STR_IDX (USBD_CDC7_IF0_STR_IDX+1) #define USBD_CDC7_IMAC_STR_IDX (USBD_CDC7_IF0_STR_IDX+2) diff --git a/Components/USB/Source/usbd_config_def_1.h b/Components/USB/Source/usbd_config_def_1.h index 4b5f761d..c782f1f8 100644 --- a/Components/USB/Source/usbd_config_def_1.h +++ b/Components/USB/Source/usbd_config_def_1.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_config_def_1.h * Purpose: USB Device (USBD) - Device 1 Definitions @@ -5641,8 +5641,8 @@ #define USBD_CDC4_RNDIS (USBD1_CDC4_RNDIS) #define USBD_CDC4_IF0_NUM (USBD1_CUSTOM_CLASS_IF_CNT+USBD1_ADC_IF_CNT+USBD1_CDC0_IF_CNT+USBD1_CDC1_IF_CNT+USBD1_CDC2_IF_CNT+USBD1_CDC3_IF_CNT) #define USBD_CDC4_IF1_NUM (USBD_CDC4_IF0_NUM+1) -#define USBD_CDC4_IF_CNT (USBD1_CDC3_IF_CNT) -#define USBD_CDC4_EP_CNT (USBD1_CDC3_EP_CNT) +#define USBD_CDC4_IF_CNT (USBD1_CDC4_IF_CNT) +#define USBD_CDC4_EP_CNT (USBD1_CDC4_EP_CNT) #define USBD_CDC4_IF0_STR_IDX (USBD1_DEV_STR_CNT+USBD1_CUSTOM_CLASS_STR_CNT+USBD1_ADC_STR_CNT+USBD1_CDC0_STR_CNT+USBD1_CDC1_STR_CNT+USBD1_CDC2_STR_CNT+USBD1_CDC3_STR_CNT) #define USBD_CDC4_IF1_STR_IDX (USBD_CDC4_IF0_STR_IDX+1) #define USBD_CDC4_IMAC_STR_IDX (USBD_CDC4_IF0_STR_IDX+2) @@ -5871,8 +5871,8 @@ #define USBD_CDC5_RNDIS (USBD1_CDC5_RNDIS) #define USBD_CDC5_IF0_NUM (USBD1_CUSTOM_CLASS_IF_CNT+USBD1_ADC_IF_CNT+USBD1_CDC0_IF_CNT+USBD1_CDC1_IF_CNT+USBD1_CDC2_IF_CNT+USBD1_CDC3_IF_CNT+USBD1_CDC4_IF_CNT) #define USBD_CDC5_IF1_NUM (USBD_CDC5_IF0_NUM+1) -#define USBD_CDC5_IF_CNT (USBD1_CDC3_IF_CNT) -#define USBD_CDC5_EP_CNT (USBD1_CDC3_EP_CNT) +#define USBD_CDC5_IF_CNT (USBD1_CDC5_IF_CNT) +#define USBD_CDC5_EP_CNT (USBD1_CDC5_EP_CNT) #define USBD_CDC5_IF0_STR_IDX (USBD1_DEV_STR_CNT+USBD1_CUSTOM_CLASS_STR_CNT+USBD1_ADC_STR_CNT+USBD1_CDC0_STR_CNT+USBD1_CDC1_STR_CNT+USBD1_CDC2_STR_CNT+USBD1_CDC3_STR_CNT+USBD1_CDC4_STR_CNT) #define USBD_CDC5_IF1_STR_IDX (USBD_CDC5_IF0_STR_IDX+1) #define USBD_CDC5_IMAC_STR_IDX (USBD_CDC5_IF0_STR_IDX+2) @@ -6101,8 +6101,8 @@ #define USBD_CDC6_RNDIS (USBD1_CDC6_RNDIS) #define USBD_CDC6_IF0_NUM (USBD1_CUSTOM_CLASS_IF_CNT+USBD1_ADC_IF_CNT+USBD1_CDC0_IF_CNT+USBD1_CDC1_IF_CNT+USBD1_CDC2_IF_CNT+USBD1_CDC3_IF_CNT+USBD1_CDC4_IF_CNT+USBD1_CDC5_IF_CNT) #define USBD_CDC6_IF1_NUM (USBD_CDC6_IF0_NUM+1) -#define USBD_CDC6_IF_CNT (USBD1_CDC3_IF_CNT) -#define USBD_CDC6_EP_CNT (USBD1_CDC3_EP_CNT) +#define USBD_CDC6_IF_CNT (USBD1_CDC6_IF_CNT) +#define USBD_CDC6_EP_CNT (USBD1_CDC6_EP_CNT) #define USBD_CDC6_IF0_STR_IDX (USBD1_DEV_STR_CNT+USBD1_CUSTOM_CLASS_STR_CNT+USBD1_ADC_STR_CNT+USBD1_CDC0_STR_CNT+USBD1_CDC1_STR_CNT+USBD1_CDC2_STR_CNT+USBD1_CDC3_STR_CNT+USBD1_CDC4_STR_CNT+USBD1_CDC5_STR_CNT) #define USBD_CDC6_IF1_STR_IDX (USBD_CDC6_IF0_STR_IDX+1) #define USBD_CDC6_IMAC_STR_IDX (USBD_CDC6_IF0_STR_IDX+2) @@ -6331,8 +6331,8 @@ #define USBD_CDC7_RNDIS (USBD1_CDC7_RNDIS) #define USBD_CDC7_IF0_NUM (USBD1_CUSTOM_CLASS_IF_CNT+USBD1_ADC_IF_CNT+USBD1_CDC0_IF_CNT+USBD1_CDC1_IF_CNT+USBD1_CDC2_IF_CNT+USBD1_CDC3_IF_CNT+USBD1_CDC4_IF_CNT+USBD1_CDC5_IF_CNT+USBD1_CDC6_IF_CNT) #define USBD_CDC7_IF1_NUM (USBD_CDC7_IF0_NUM+1) -#define USBD_CDC7_IF_CNT (USBD1_CDC3_IF_CNT) -#define USBD_CDC7_EP_CNT (USBD1_CDC3_EP_CNT) +#define USBD_CDC7_IF_CNT (USBD1_CDC7_IF_CNT) +#define USBD_CDC7_EP_CNT (USBD1_CDC7_EP_CNT) #define USBD_CDC7_IF0_STR_IDX (USBD1_DEV_STR_CNT+USBD1_CUSTOM_CLASS_STR_CNT+USBD1_ADC_STR_CNT+USBD1_CDC0_STR_CNT+USBD1_CDC1_STR_CNT+USBD1_CDC2_STR_CNT+USBD1_CDC3_STR_CNT+USBD1_CDC4_STR_CNT+USBD1_CDC5_STR_CNT+USBD1_CDC6_STR_CNT) #define USBD_CDC7_IF1_STR_IDX (USBD_CDC7_IF0_STR_IDX+1) #define USBD_CDC7_IMAC_STR_IDX (USBD_CDC7_IF0_STR_IDX+2) diff --git a/Components/USB/Source/usbd_config_def_2.h b/Components/USB/Source/usbd_config_def_2.h index 9ccd911c..5f41da54 100644 --- a/Components/USB/Source/usbd_config_def_2.h +++ b/Components/USB/Source/usbd_config_def_2.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_config_def_2.h * Purpose: USB Device (USBD) - Device 2 Definitions @@ -5641,8 +5641,8 @@ #define USBD_CDC4_RNDIS (USBD2_CDC4_RNDIS) #define USBD_CDC4_IF0_NUM (USBD2_CUSTOM_CLASS_IF_CNT+USBD2_ADC_IF_CNT+USBD2_CDC0_IF_CNT+USBD2_CDC1_IF_CNT+USBD2_CDC2_IF_CNT+USBD2_CDC3_IF_CNT) #define USBD_CDC4_IF1_NUM (USBD_CDC4_IF0_NUM+1) -#define USBD_CDC4_IF_CNT (USBD2_CDC3_IF_CNT) -#define USBD_CDC4_EP_CNT (USBD2_CDC3_EP_CNT) +#define USBD_CDC4_IF_CNT (USBD2_CDC4_IF_CNT) +#define USBD_CDC4_EP_CNT (USBD2_CDC4_EP_CNT) #define USBD_CDC4_IF0_STR_IDX (USBD2_DEV_STR_CNT+USBD2_CUSTOM_CLASS_STR_CNT+USBD2_ADC_STR_CNT+USBD2_CDC0_STR_CNT+USBD2_CDC1_STR_CNT+USBD2_CDC2_STR_CNT+USBD2_CDC3_STR_CNT) #define USBD_CDC4_IF1_STR_IDX (USBD_CDC4_IF0_STR_IDX+1) #define USBD_CDC4_IMAC_STR_IDX (USBD_CDC4_IF0_STR_IDX+2) @@ -5871,8 +5871,8 @@ #define USBD_CDC5_RNDIS (USBD2_CDC5_RNDIS) #define USBD_CDC5_IF0_NUM (USBD2_CUSTOM_CLASS_IF_CNT+USBD2_ADC_IF_CNT+USBD2_CDC0_IF_CNT+USBD2_CDC1_IF_CNT+USBD2_CDC2_IF_CNT+USBD2_CDC3_IF_CNT+USBD2_CDC4_IF_CNT) #define USBD_CDC5_IF1_NUM (USBD_CDC5_IF0_NUM+1) -#define USBD_CDC5_IF_CNT (USBD2_CDC3_IF_CNT) -#define USBD_CDC5_EP_CNT (USBD2_CDC3_EP_CNT) +#define USBD_CDC5_IF_CNT (USBD2_CDC5_IF_CNT) +#define USBD_CDC5_EP_CNT (USBD2_CDC5_EP_CNT) #define USBD_CDC5_IF0_STR_IDX (USBD2_DEV_STR_CNT+USBD2_CUSTOM_CLASS_STR_CNT+USBD2_ADC_STR_CNT+USBD2_CDC0_STR_CNT+USBD2_CDC1_STR_CNT+USBD2_CDC2_STR_CNT+USBD2_CDC3_STR_CNT+USBD2_CDC4_STR_CNT) #define USBD_CDC5_IF1_STR_IDX (USBD_CDC5_IF0_STR_IDX+1) #define USBD_CDC5_IMAC_STR_IDX (USBD_CDC5_IF0_STR_IDX+2) @@ -6101,8 +6101,8 @@ #define USBD_CDC6_RNDIS (USBD2_CDC6_RNDIS) #define USBD_CDC6_IF0_NUM (USBD2_CUSTOM_CLASS_IF_CNT+USBD2_ADC_IF_CNT+USBD2_CDC0_IF_CNT+USBD2_CDC1_IF_CNT+USBD2_CDC2_IF_CNT+USBD2_CDC3_IF_CNT+USBD2_CDC4_IF_CNT+USBD2_CDC5_IF_CNT) #define USBD_CDC6_IF1_NUM (USBD_CDC6_IF0_NUM+1) -#define USBD_CDC6_IF_CNT (USBD2_CDC3_IF_CNT) -#define USBD_CDC6_EP_CNT (USBD2_CDC3_EP_CNT) +#define USBD_CDC6_IF_CNT (USBD2_CDC6_IF_CNT) +#define USBD_CDC6_EP_CNT (USBD2_CDC6_EP_CNT) #define USBD_CDC6_IF0_STR_IDX (USBD2_DEV_STR_CNT+USBD2_CUSTOM_CLASS_STR_CNT+USBD2_ADC_STR_CNT+USBD2_CDC0_STR_CNT+USBD2_CDC1_STR_CNT+USBD2_CDC2_STR_CNT+USBD2_CDC3_STR_CNT+USBD2_CDC4_STR_CNT+USBD2_CDC5_STR_CNT) #define USBD_CDC6_IF1_STR_IDX (USBD_CDC6_IF0_STR_IDX+1) #define USBD_CDC6_IMAC_STR_IDX (USBD_CDC6_IF0_STR_IDX+2) @@ -6331,8 +6331,8 @@ #define USBD_CDC7_RNDIS (USBD2_CDC7_RNDIS) #define USBD_CDC7_IF0_NUM (USBD2_CUSTOM_CLASS_IF_CNT+USBD2_ADC_IF_CNT+USBD2_CDC0_IF_CNT+USBD2_CDC1_IF_CNT+USBD2_CDC2_IF_CNT+USBD2_CDC3_IF_CNT+USBD2_CDC4_IF_CNT+USBD2_CDC5_IF_CNT+USBD2_CDC6_IF_CNT) #define USBD_CDC7_IF1_NUM (USBD_CDC7_IF0_NUM+1) -#define USBD_CDC7_IF_CNT (USBD2_CDC3_IF_CNT) -#define USBD_CDC7_EP_CNT (USBD2_CDC3_EP_CNT) +#define USBD_CDC7_IF_CNT (USBD2_CDC7_IF_CNT) +#define USBD_CDC7_EP_CNT (USBD2_CDC7_EP_CNT) #define USBD_CDC7_IF0_STR_IDX (USBD2_DEV_STR_CNT+USBD2_CUSTOM_CLASS_STR_CNT+USBD2_ADC_STR_CNT+USBD2_CDC0_STR_CNT+USBD2_CDC1_STR_CNT+USBD2_CDC2_STR_CNT+USBD2_CDC3_STR_CNT+USBD2_CDC4_STR_CNT+USBD2_CDC5_STR_CNT+USBD2_CDC6_STR_CNT) #define USBD_CDC7_IF1_STR_IDX (USBD_CDC7_IF0_STR_IDX+1) #define USBD_CDC7_IMAC_STR_IDX (USBD_CDC7_IF0_STR_IDX+2) diff --git a/Components/USB/Source/usbd_config_def_3.h b/Components/USB/Source/usbd_config_def_3.h index ac79ad03..273679b7 100644 --- a/Components/USB/Source/usbd_config_def_3.h +++ b/Components/USB/Source/usbd_config_def_3.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_config_def_3.h * Purpose: USB Device (USBD) - Device 3 Definitions @@ -5641,8 +5641,8 @@ #define USBD_CDC4_RNDIS (USBD3_CDC4_RNDIS) #define USBD_CDC4_IF0_NUM (USBD3_CUSTOM_CLASS_IF_CNT+USBD3_ADC_IF_CNT+USBD3_CDC0_IF_CNT+USBD3_CDC1_IF_CNT+USBD3_CDC2_IF_CNT+USBD3_CDC3_IF_CNT) #define USBD_CDC4_IF1_NUM (USBD_CDC4_IF0_NUM+1) -#define USBD_CDC4_IF_CNT (USBD3_CDC3_IF_CNT) -#define USBD_CDC4_EP_CNT (USBD3_CDC3_EP_CNT) +#define USBD_CDC4_IF_CNT (USBD3_CDC4_IF_CNT) +#define USBD_CDC4_EP_CNT (USBD3_CDC4_EP_CNT) #define USBD_CDC4_IF0_STR_IDX (USBD3_DEV_STR_CNT+USBD3_CUSTOM_CLASS_STR_CNT+USBD3_ADC_STR_CNT+USBD3_CDC0_STR_CNT+USBD3_CDC1_STR_CNT+USBD3_CDC2_STR_CNT+USBD3_CDC3_STR_CNT) #define USBD_CDC4_IF1_STR_IDX (USBD_CDC4_IF0_STR_IDX+1) #define USBD_CDC4_IMAC_STR_IDX (USBD_CDC4_IF0_STR_IDX+2) @@ -5871,8 +5871,8 @@ #define USBD_CDC5_RNDIS (USBD3_CDC5_RNDIS) #define USBD_CDC5_IF0_NUM (USBD3_CUSTOM_CLASS_IF_CNT+USBD3_ADC_IF_CNT+USBD3_CDC0_IF_CNT+USBD3_CDC1_IF_CNT+USBD3_CDC2_IF_CNT+USBD3_CDC3_IF_CNT+USBD3_CDC4_IF_CNT) #define USBD_CDC5_IF1_NUM (USBD_CDC5_IF0_NUM+1) -#define USBD_CDC5_IF_CNT (USBD3_CDC3_IF_CNT) -#define USBD_CDC5_EP_CNT (USBD3_CDC3_EP_CNT) +#define USBD_CDC5_IF_CNT (USBD3_CDC5_IF_CNT) +#define USBD_CDC5_EP_CNT (USBD3_CDC5_EP_CNT) #define USBD_CDC5_IF0_STR_IDX (USBD3_DEV_STR_CNT+USBD3_CUSTOM_CLASS_STR_CNT+USBD3_ADC_STR_CNT+USBD3_CDC0_STR_CNT+USBD3_CDC1_STR_CNT+USBD3_CDC2_STR_CNT+USBD3_CDC3_STR_CNT+USBD3_CDC4_STR_CNT) #define USBD_CDC5_IF1_STR_IDX (USBD_CDC5_IF0_STR_IDX+1) #define USBD_CDC5_IMAC_STR_IDX (USBD_CDC5_IF0_STR_IDX+2) @@ -6101,8 +6101,8 @@ #define USBD_CDC6_RNDIS (USBD3_CDC6_RNDIS) #define USBD_CDC6_IF0_NUM (USBD3_CUSTOM_CLASS_IF_CNT+USBD3_ADC_IF_CNT+USBD3_CDC0_IF_CNT+USBD3_CDC1_IF_CNT+USBD3_CDC2_IF_CNT+USBD3_CDC3_IF_CNT+USBD3_CDC4_IF_CNT+USBD3_CDC5_IF_CNT) #define USBD_CDC6_IF1_NUM (USBD_CDC6_IF0_NUM+1) -#define USBD_CDC6_IF_CNT (USBD3_CDC3_IF_CNT) -#define USBD_CDC6_EP_CNT (USBD3_CDC3_EP_CNT) +#define USBD_CDC6_IF_CNT (USBD3_CDC6_IF_CNT) +#define USBD_CDC6_EP_CNT (USBD3_CDC6_EP_CNT) #define USBD_CDC6_IF0_STR_IDX (USBD3_DEV_STR_CNT+USBD3_CUSTOM_CLASS_STR_CNT+USBD3_ADC_STR_CNT+USBD3_CDC0_STR_CNT+USBD3_CDC1_STR_CNT+USBD3_CDC2_STR_CNT+USBD3_CDC3_STR_CNT+USBD3_CDC4_STR_CNT+USBD3_CDC5_STR_CNT) #define USBD_CDC6_IF1_STR_IDX (USBD_CDC6_IF0_STR_IDX+1) #define USBD_CDC6_IMAC_STR_IDX (USBD_CDC6_IF0_STR_IDX+2) @@ -6331,8 +6331,8 @@ #define USBD_CDC7_RNDIS (USBD3_CDC7_RNDIS) #define USBD_CDC7_IF0_NUM (USBD3_CUSTOM_CLASS_IF_CNT+USBD3_ADC_IF_CNT+USBD3_CDC0_IF_CNT+USBD3_CDC1_IF_CNT+USBD3_CDC2_IF_CNT+USBD3_CDC3_IF_CNT+USBD3_CDC4_IF_CNT+USBD3_CDC5_IF_CNT+USBD3_CDC6_IF_CNT) #define USBD_CDC7_IF1_NUM (USBD_CDC7_IF0_NUM+1) -#define USBD_CDC7_IF_CNT (USBD3_CDC3_IF_CNT) -#define USBD_CDC7_EP_CNT (USBD3_CDC3_EP_CNT) +#define USBD_CDC7_IF_CNT (USBD3_CDC7_IF_CNT) +#define USBD_CDC7_EP_CNT (USBD3_CDC7_EP_CNT) #define USBD_CDC7_IF0_STR_IDX (USBD3_DEV_STR_CNT+USBD3_CUSTOM_CLASS_STR_CNT+USBD3_ADC_STR_CNT+USBD3_CDC0_STR_CNT+USBD3_CDC1_STR_CNT+USBD3_CDC2_STR_CNT+USBD3_CDC3_STR_CNT+USBD3_CDC4_STR_CNT+USBD3_CDC5_STR_CNT+USBD3_CDC6_STR_CNT) #define USBD_CDC7_IF1_STR_IDX (USBD_CDC7_IF0_STR_IDX+1) #define USBD_CDC7_IMAC_STR_IDX (USBD_CDC7_IF0_STR_IDX+2) diff --git a/Components/USB/Source/usbd_evr.h b/Components/USB/Source/usbd_evr.h index 4ec1feb5..ae8949ab 100644 --- a/Components/USB/Source/usbd_evr.h +++ b/Components/USB/Source/usbd_evr.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_evr.h * Purpose: USB Device (USBD) - Event Recorder definitions @@ -973,7 +973,9 @@ */ #ifdef EvtUSBD_Core_ReadSetupPacket __STATIC_INLINE void EvrUSBD_Core_ReadSetupPacket( uint8_t device, const void * setup_packet) { - EventRecord4( EvtUSBD_Core_ReadSetupPacket, TO_BYTE0(device), *((const uint32_t *)setup_packet), *((const uint32_t *)setup_packet+1U), 0U); + if (setup_packet != NULL) { + EventRecord4( EvtUSBD_Core_ReadSetupPacket, TO_BYTE0(device), *((const uint32_t *)setup_packet), *((const uint32_t *)setup_packet+1U), 0U); + } } #else #define EvrUSBD_Core_ReadSetupPacket(...) @@ -1273,7 +1275,9 @@ */ #ifdef EvtUSBD_Core_OnSetupPacketReceivedDetail __STATIC_INLINE void EvrUSBD_Core_OnSetupPacketReceivedDetail( uint8_t n, const void * setup_packet, uint32_t len) { - EventRecord4( EvtUSBD_Core_OnSetupPacketReceivedDetail, TO_BYTE0(n), *((const uint32_t *)setup_packet), *((const uint32_t *)setup_packet+1U), len); + if (setup_packet != NULL) { + EventRecord4( EvtUSBD_Core_OnSetupPacketReceivedDetail, TO_BYTE0(n), *((const uint32_t *)setup_packet), *((const uint32_t *)setup_packet+1U), len); + } } #else #define EvrUSBD_Core_OnSetupPacketReceivedDetail(...) @@ -1300,7 +1304,9 @@ */ #ifdef EvtUSBD_Core_OnSetupPacketProcessedDetail __STATIC_INLINE void EvrUSBD_Core_OnSetupPacketProcessedDetail( uint8_t n, const void * setup_packet) { - EventRecord4( EvtUSBD_Core_OnSetupPacketProcessedDetail, TO_BYTE0(n), *((const uint32_t *)setup_packet), *((const uint32_t *)setup_packet+1U), 0U); + if (setup_packet != NULL) { + EventRecord4( EvtUSBD_Core_OnSetupPacketProcessedDetail, TO_BYTE0(n), *((const uint32_t *)setup_packet), *((const uint32_t *)setup_packet+1U), 0U); + } } #else #define EvrUSBD_Core_OnSetupPacketProcessedDetail(...) @@ -1561,7 +1567,9 @@ */ #ifdef EvtUSBD_Driver_ReadSetupPacket __STATIC_INLINE void EvrUSBD_Driver_ReadSetupPacket( uint8_t device, const void * setup_packet) { - EventRecord4( EvtUSBD_Driver_ReadSetupPacket, TO_BYTE0(device), *((const uint32_t *)setup_packet), *((const uint32_t *)setup_packet+1U), 0U); + if (setup_packet != NULL) { + EventRecord4( EvtUSBD_Driver_ReadSetupPacket, TO_BYTE0(device), *((const uint32_t *)setup_packet), *((const uint32_t *)setup_packet+1U), 0U); + } } #else #define EvrUSBD_Driver_ReadSetupPacket(...) @@ -1920,7 +1928,9 @@ */ #ifdef EvtUSBD_CC_OnEndpoint0SetupPacketReceivedDetail __STATIC_INLINE void EvrUSBD_CC_OnEndpoint0SetupPacketReceivedDetail( uint8_t n, const void * setup_packet, uint32_t len) { - EventRecord4( EvtUSBD_CC_OnEndpoint0SetupPacketReceivedDetail, TO_BYTE0(n), *((const uint32_t *)setup_packet), *((const uint32_t *)setup_packet+1U), len); + if (setup_packet != NULL) { + EventRecord4( EvtUSBD_CC_OnEndpoint0SetupPacketReceivedDetail, TO_BYTE0(n), *((const uint32_t *)setup_packet), *((const uint32_t *)setup_packet+1U), len); + } } #else #define EvrUSBD_CC_OnEndpoint0SetupPacketReceivedDetail(...) @@ -1947,7 +1957,9 @@ */ #ifdef EvtUSBD_CC_OnEndpoint0SetupPacketProcessedDetail __STATIC_INLINE void EvrUSBD_CC_OnEndpoint0SetupPacketProcessedDetail( uint8_t n, const void * setup_packet) { - EventRecord4( EvtUSBD_CC_OnEndpoint0SetupPacketProcessedDetail, TO_BYTE0(n), *((const uint32_t *)setup_packet), *((const uint32_t *)setup_packet+1U), 0U); + if (setup_packet != NULL) { + EventRecord4( EvtUSBD_CC_OnEndpoint0SetupPacketProcessedDetail, TO_BYTE0(n), *((const uint32_t *)setup_packet), *((const uint32_t *)setup_packet+1U), 0U); + } } #else #define EvrUSBD_CC_OnEndpoint0SetupPacketProcessedDetail(...) diff --git a/Components/USB/Source/usbd_lib_adc.c b/Components/USB/Source/usbd_lib_adc.c index ff195ee7..74e31504 100644 --- a/Components/USB/Source/usbd_lib_adc.c +++ b/Components/USB/Source/usbd_lib_adc.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_adc.c * Purpose: USB Device - Audio Device Class (ADC) module @@ -238,6 +238,10 @@ int32_t USBD_ADC_ReadSamples (uint8_t instance, void *buf, int32_t num) { EvrUSBD_ADC_ReadSamplesFailed(instance, status); return -(int32_t)status; } + if ((buf == NULL) || (num < 0)) { + EvrUSBD_ADC_ReadSamplesFailed(instance, usbInvalidParameter); + return -(int32_t)usbInvalidParameter; + } result = 0U; ptr_adc_cfg = usbd_adc_ptr[instance]; @@ -435,6 +439,10 @@ int32_t USBD_ADC_WriteSamples (uint8_t instance, const void *buf, int32_t num) { EvrUSBD_ADC_WriteSamplesFailed(instance, status); return -(int32_t)status; } + if ((buf == NULL) || (num < 0)) { + EvrUSBD_ADC_WriteSamplesFailed(instance, usbInvalidParameter); + return -(int32_t)usbInvalidParameter; + } result = 0U; ptr_adc_cfg = usbd_adc_ptr[instance]; @@ -621,6 +629,9 @@ bool USBD_ADC_SpeakerMuteGetCur (uint8_t instance, uint8_t ch) { } ptr_adc_cfg = usbd_adc_ptr[instance]; + if (ptr_adc_cfg->spkr_data_ptr == NULL) { + return false; + } usbd_dev_ptr[ptr_adc_cfg->dev_num]->ep0_buf[0] = ptr_adc_cfg->spkr_data_ptr->mute; @@ -639,6 +650,9 @@ bool USBD_ADC_SpeakerMuteSetCur (uint8_t instance, uint8_t ch, bool on) { if (USBD_ADC_CheckInstance(instance) != usbOK) { return false; } + if (usbd_adc_ptr[instance]->spkr_data_ptr == NULL) { + return false; + } if (on) { usbd_adc_ptr[instance]->spkr_data_ptr->mute = 1U; @@ -671,6 +685,9 @@ bool USBD_ADC_SpeakerVolumeGetCur (uint8_t instance, uint8_t ch) { } ptr_adc_cfg = usbd_adc_ptr[instance]; + if (ptr_adc_cfg->spkr_data_ptr == NULL) { + return false; + } *((uint16_t *)((uint32_t)usbd_dev_ptr[ptr_adc_cfg->dev_num]->ep0_buf)) = ptr_adc_cfg->spkr_data_ptr->vol_cur[ch-1U]; @@ -689,6 +706,9 @@ bool USBD_ADC_SpeakerVolumeSetCur (uint8_t instance, uint8_t ch, uint16_t vol) { if (USBD_ADC_CheckInstance(instance) != usbOK) { return false; } + if (usbd_adc_ptr[instance]->spkr_data_ptr == NULL) { + return false; + } if ((ch == 0U) || (ch > 2U)) { // Master channel volume or non-existent channel return false; @@ -721,6 +741,9 @@ bool USBD_ADC_SpeakerVolumeGetMin (uint8_t instance, uint8_t ch) { } ptr_adc_cfg = usbd_adc_ptr[instance]; + if (ptr_adc_cfg->spkr_data_ptr == NULL) { + return false; + } *((uint16_t *)((uint32_t)usbd_dev_ptr[ptr_adc_cfg->dev_num]->ep0_buf)) = ptr_adc_cfg->spkr_data_ptr->vol_min[ch-1U]; @@ -744,6 +767,9 @@ bool USBD_ADC_SpeakerVolumeGetMax (uint8_t instance, uint8_t ch) { } ptr_adc_cfg = usbd_adc_ptr[instance]; + if (ptr_adc_cfg->spkr_data_ptr == NULL) { + return false; + } *((uint16_t *)((uint32_t)usbd_dev_ptr[ptr_adc_cfg->dev_num]->ep0_buf)) = ptr_adc_cfg->spkr_data_ptr->vol_max[ch-1U]; @@ -767,6 +793,9 @@ bool USBD_ADC_SpeakerVolumeGetRes (uint8_t instance, uint8_t ch) { } ptr_adc_cfg = usbd_adc_ptr[instance]; + if (ptr_adc_cfg->spkr_data_ptr == NULL) { + return false; + } *((uint16_t *)((uint32_t)usbd_dev_ptr[ptr_adc_cfg->dev_num]->ep0_buf)) = ptr_adc_cfg->spkr_data_ptr->vol_res[ch-1U]; @@ -788,6 +817,9 @@ bool USBD_ADC_MicrophoneMuteGetCur (uint8_t instance, uint8_t ch) { } ptr_adc_cfg = usbd_adc_ptr[instance]; + if (ptr_adc_cfg->mic_data_ptr == NULL) { + return false; + } usbd_dev_ptr[ptr_adc_cfg->dev_num]->ep0_buf[0] = ptr_adc_cfg->mic_data_ptr->mute; @@ -806,6 +838,9 @@ bool USBD_ADC_MicrophoneMuteSetCur (uint8_t instance, uint8_t ch, bool on) { if (USBD_ADC_CheckInstance(instance) != usbOK) { return false; } + if (usbd_adc_ptr[instance]->mic_data_ptr == NULL) { + return false; + } if (on) { usbd_adc_ptr[instance]->mic_data_ptr->mute = 1U; @@ -838,6 +873,9 @@ bool USBD_ADC_MicrophoneVolumeGetCur (uint8_t instance, uint8_t ch) { } ptr_adc_cfg = usbd_adc_ptr[instance]; + if (ptr_adc_cfg->mic_data_ptr == NULL) { + return false; + } *((uint16_t *)((uint32_t)usbd_dev_ptr[ptr_adc_cfg->dev_num]->ep0_buf)) = ptr_adc_cfg->mic_data_ptr->vol_cur[ch-1U]; @@ -856,6 +894,9 @@ bool USBD_ADC_MicrophoneVolumeSetCur (uint8_t instance, uint8_t ch, uint16_t vol if (USBD_ADC_CheckInstance(instance) != usbOK) { return false; } + if (usbd_adc_ptr[instance]->mic_data_ptr == NULL) { + return false; + } if ((ch == 0U) || (ch > 2U)) { // Master channel volume or non-existent channel return false; @@ -888,6 +929,9 @@ bool USBD_ADC_MicrophoneVolumeGetMin (uint8_t instance, uint8_t ch) { } ptr_adc_cfg = usbd_adc_ptr[instance]; + if (ptr_adc_cfg->mic_data_ptr == NULL) { + return false; + } *((uint16_t *)((uint32_t)usbd_dev_ptr[ptr_adc_cfg->dev_num]->ep0_buf)) = ptr_adc_cfg->mic_data_ptr->vol_min[ch-1U]; @@ -911,6 +955,9 @@ bool USBD_ADC_MicrophoneVolumeGetMax (uint8_t instance, uint8_t ch) { } ptr_adc_cfg = usbd_adc_ptr[instance]; + if (ptr_adc_cfg->mic_data_ptr == NULL) { + return false; + } *((uint16_t *)((uint32_t)usbd_dev_ptr[ptr_adc_cfg->dev_num]->ep0_buf)) = ptr_adc_cfg->mic_data_ptr->vol_max[ch-1U]; @@ -934,6 +981,9 @@ bool USBD_ADC_MicrophoneVolumeGetRes (uint8_t instance, uint8_t ch) { } ptr_adc_cfg = usbd_adc_ptr[instance]; + if (ptr_adc_cfg->mic_data_ptr == NULL) { + return false; + } *((uint16_t *)((uint32_t)usbd_dev_ptr[ptr_adc_cfg->dev_num]->ep0_buf)) = ptr_adc_cfg->mic_data_ptr->vol_res[ch-1U]; @@ -1055,7 +1105,10 @@ void USBD_ADC_EndpointStart (uint8_t instance, uint8_t ep_addr) { return; } - ptr_adc_cfg = usbd_adc_ptr[instance]; + ptr_adc_cfg = usbd_adc_ptr[instance]; + if (ptr_adc_cfg == NULL) { + return; + } ptr_adc_spkr_data = ptr_adc_cfg->spkr_data_ptr; ptr_adc_mic_data = ptr_adc_cfg->mic_data_ptr; ptr_out_cfg = ptr_adc_cfg->out_cfg; @@ -1074,7 +1127,7 @@ void USBD_ADC_EndpointStart (uint8_t instance, uint8_t ep_addr) { fpUSBD_ADC_PlayStart [instance] (); } } - if ((ptr_in_cfg != NULL) && (USB_ENDPOINT_IN(ptr_in_cfg->ep_iso) == ep_addr)) { + if ((ptr_adc_mic_data != NULL) && (ptr_in_cfg != NULL) && (USB_ENDPOINT_IN(ptr_in_cfg->ep_iso) == ep_addr)) { ptr_adc_mic_data->active = 1U; if (fpUSBD_ADC_RecordStart [instance] != NULL) { EvrUSBD_ADC_OnRecordStart(instance); @@ -1099,7 +1152,10 @@ void USBD_ADC_EndpointStop (uint8_t instance, uint8_t ep_addr) { return; } - ptr_adc_cfg = usbd_adc_ptr[instance]; + ptr_adc_cfg = usbd_adc_ptr[instance]; + if (ptr_adc_cfg == NULL) { + return; + } ptr_adc_spkr_data = ptr_adc_cfg->spkr_data_ptr; ptr_adc_mic_data = ptr_adc_cfg->mic_data_ptr; ptr_out_cfg = ptr_adc_cfg->out_cfg; @@ -1162,8 +1218,17 @@ static void USBD_ADC_EpIsoOut (uint8_t instance) { } ptr_adc_cfg = usbd_adc_ptr[instance]; + if (ptr_adc_cfg == NULL) { + return; + } ptr_adc_spkr_data = ptr_adc_cfg->spkr_data_ptr; + if (ptr_adc_spkr_data == NULL) { + return; + } ptr_out_cfg = ptr_adc_cfg->out_cfg; + if (ptr_out_cfg == NULL) { + return; + } device = ptr_adc_cfg->dev_num; ep_iso = ptr_out_cfg->ep_iso; @@ -1218,8 +1283,17 @@ static void USBD_ADC_EpIsoIn (uint8_t instance) { } ptr_adc_cfg = usbd_adc_ptr[instance]; + if (ptr_adc_cfg == NULL) { + return; + } ptr_adc_mic_data = ptr_adc_cfg->mic_data_ptr; + if (ptr_adc_mic_data == NULL) { + return; + } ptr_in_cfg = ptr_adc_cfg->in_cfg; + if (ptr_in_cfg == NULL) { + return; + } device = ptr_adc_cfg->dev_num; ep_iso = ptr_in_cfg->ep_iso; @@ -1273,7 +1347,7 @@ void USBD_ADC_Thread (void *arg) { for (;;) { event = USBD_ThreadFlagsWait (0xFFFFFFFFU); - if ((event & 0x8000000U) == 0U) { + if ((event & 0x80000000U) == 0U) { if (((event >> 8) & ARM_USBD_EVENT_OUT) != 0U) { USBD_ADC_EpIsoOut (instance); } diff --git a/Components/USB/Source/usbd_lib_adc.h b/Components/USB/Source/usbd_lib_adc.h index 85010bfa..cf60827b 100644 --- a/Components/USB/Source/usbd_lib_adc.h +++ b/Components/USB/Source/usbd_lib_adc.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_adc.h * Purpose: USB Device - Audio Device Class (ADC) module header file @@ -118,4 +118,6 @@ extern bool USBD_ADC_MicrophoneVolumeGetMax (uint8_t instance, uint8_t ch); /// \return false fail extern bool USBD_ADC_MicrophoneVolumeGetRes (uint8_t instance, uint8_t ch); +extern void USBD_ADC_Thread (void *arg); + #endif // USBD_LIB_ADC_H_ diff --git a/Components/USB/Source/usbd_lib_cdc.c b/Components/USB/Source/usbd_lib_cdc.c index 9fa121d2..45129b95 100644 --- a/Components/USB/Source/usbd_lib_cdc.c +++ b/Components/USB/Source/usbd_lib_cdc.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_cdc.c * Purpose: USB Device - Communication Device Class (CDC) module @@ -46,10 +46,15 @@ int32_t USBD_CDC_ACM_ReadData (uint8_t instance, uint8_t *buf, int32_t len) { uint32_t max_packet_size, len_data, len_till_wrap, len_req = 0U, len_loc; usbStatus status; + // Check parameters status = USBD_CDC_CheckInstance(instance); if (status != usbOK) { goto exit; } + if ((buf == NULL) || (len < 0)) { + status = usbInvalidParameter; + goto exit; + } ptr_cdc_cfg = usbd_cdc_ptr[instance]; ptr_cdc_data = ptr_cdc_cfg->data_ptr; @@ -153,10 +158,15 @@ int32_t USBD_CDC_ACM_WriteData (uint8_t instance, const uint8_t *buf, int32_t le uint32_t len_free, len_till_wrap, len_req = 0U, len_loc; usbStatus status; + // Check parameters status = USBD_CDC_CheckInstance(instance); if (status != usbOK) { goto exit; } + if ((buf == NULL) || (len < 0)) { + status = usbInvalidParameter; + goto exit; + } ptr_cdc_cfg = usbd_cdc_ptr[instance]; ptr_cdc_data = ptr_cdc_cfg->data_ptr; @@ -606,10 +616,15 @@ int32_t USBD_CDC_NCM_NTB_IN_WriteDatagram (uint8_t instance, const uint8_t *buf, uint32_t tot_size; usbStatus status; + // Check parameters status = USBD_CDC_CheckInstance(instance); if (status != usbOK) { goto exit; } + if ((buf == NULL) || (len == 0U)) { + status = usbInvalidParameter; + goto exit; + } ptr_cdc_cfg = usbd_cdc_ptr[instance]; ptr_cdc_data = ptr_cdc_cfg->data_ptr; @@ -923,11 +938,17 @@ int32_t USBD_CDC_NCM_NTB_OUT_ReadDatagram (uint8_t instance, uint8_t *buf, uint3 uint32_t len; usbStatus status; + // Check parameters status = USBD_CDC_CheckInstance(instance); if (status != usbOK) { EvrUSBD_CDC_NCM_NTB_OUT_ReadDatagramFailed(instance, max_len, -(int32_t)status); return -(int32_t)status; } + if ((buf == NULL) || (max_len == 0U)) { + status = usbInvalidParameter; + EvrUSBD_CDC_NCM_NTB_OUT_ReadDatagramFailed(instance, max_len, -(int32_t)status); + return -(int32_t)status; + } ptr_cdc_data = usbd_cdc_ptr[instance]->data_ptr; @@ -979,10 +1000,15 @@ int32_t USBD_CDC_NCM_NTB_IN_RawSend (uint8_t instance, const uint8_t *buf, uint3 uint32_t max_packet_size; usbStatus status; + // Check parameters status = USBD_CDC_CheckInstance(instance); if (status != usbOK) { goto exit; } + if ((buf == NULL) || (len == 0U)) { + status = usbInvalidParameter; + goto exit; + } ptr_cdc_cfg = usbd_cdc_ptr[instance]; ptr_cdc_data = ptr_cdc_cfg->data_ptr; @@ -1055,10 +1081,15 @@ int32_t USBD_CDC_NCM_NTB_OUT_RawReceive (uint8_t instance, uint8_t *buf, uint32_ usbd_cdc_data_t *ptr_cdc_data; usbStatus status; + // Check parameters status = USBD_CDC_CheckInstance(instance); if (status != usbOK) { goto exit; } + if ((buf == NULL) || (max_len == 0U)) { + status = usbInvalidParameter; + goto exit; + } ptr_cdc_cfg = usbd_cdc_ptr[instance]; ptr_cdc_data = ptr_cdc_cfg->data_ptr; @@ -1302,8 +1333,10 @@ bool USBD_CDC_ACM_CoreGetEncapsulatedResponse (uint8_t instance, uint16_t max_le result = false; if (USBD_CDC_CheckInstance(instance) == usbOK) { if (fpUSBD_CDC_ACM_GetEncapsulatedResponse[instance] != NULL) { - result = fpUSBD_CDC_ACM_GetEncapsulatedResponse[instance] (max_len, buf, len); - EvrUSBD_CDC_ACM_OnGetEncapsulatedResponse(instance, max_len, (uint32_t)(*buf), *len, result); + if ((buf != NULL) && (len != NULL)) { + result = fpUSBD_CDC_ACM_GetEncapsulatedResponse[instance] (max_len, buf, len); + EvrUSBD_CDC_ACM_OnGetEncapsulatedResponse(instance, max_len, (uint32_t)(*buf), *len, result); + } } } @@ -2258,7 +2291,7 @@ void USBD_CDC_Int_Thread (void *arg) { for (;;) { event = USBD_ThreadFlagsWait (0xFFFFFFFFU); - if ((event & 0x8000000U) == 0U) { + if ((event & 0x80000000U) == 0U) { if (((event >> 8) & ARM_USBD_EVENT_IN) != 0U) { USBD_CDC_EpIntIn (instance); } @@ -2279,7 +2312,7 @@ void USBD_CDC_Bulk_Thread (void *arg) { for (;;) { event = USBD_ThreadFlagsWait (0xFFFFFFFFU); - if ((event & 0x8000000U) == 0U) { + if ((event & 0x80000000U) == 0U) { if (((event >> 8) & ARM_USBD_EVENT_OUT) != 0U) { USBD_CDC_EpBulkOut (instance); } diff --git a/Components/USB/Source/usbd_lib_cdc.h b/Components/USB/Source/usbd_lib_cdc.h index ea138795..ae2e41b0 100644 --- a/Components/USB/Source/usbd_lib_cdc.h +++ b/Components/USB/Source/usbd_lib_cdc.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_cdc.h * Purpose: USB Device - Communication Device Class (CDC) module header file @@ -73,4 +73,7 @@ extern bool USBD_CDC_NCM_CoreSetMaxDatagramSize (uint8_t in extern bool USBD_CDC_NCM_CoreGetCrcMode (uint8_t instance, uint16_t *crc_mode); extern bool USBD_CDC_NCM_CoreSetCrcMode (uint8_t instance, uint16_t crc_mode); +extern void USBD_CDC_Int_Thread (void *arg); +extern void USBD_CDC_Bulk_Thread (void *arg); + #endif // USBD_LIB_CDC_H_ diff --git a/Components/USB/Source/usbd_lib_core.c b/Components/USB/Source/usbd_lib_core.c index a4ca6131..5d987a4b 100644 --- a/Components/USB/Source/usbd_lib_core.c +++ b/Components/USB/Source/usbd_lib_core.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2025 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_core.c * Purpose: USB Device - Core module @@ -93,6 +93,11 @@ usbStatus USBD_Initialize (uint8_t device) { return usbOK; } + if ((((uint32_t)device + 1U) * usbd_ep_max_num * 2U) >= UINT8_MAX) { + status = usbUnknownError; + goto exit; + } + usbd_drv_version[device] = USBD_DriverGetVersion (device); usbd_capabilities[device] = USBD_DriverGetCapabilities (device); @@ -107,7 +112,7 @@ usbStatus USBD_Initialize (uint8_t device) { } for (i = 0U; i < (usbd_ep_max_num * 2U); i++) { - sem_idx = (uint8_t)(device * usbd_ep_max_num * 2U) + i; + sem_idx = (device * usbd_ep_max_num * 2U) + i; if (usbd_driver_ep_semaphore_id[sem_idx] == NULL) { usbd_driver_ep_semaphore_id[sem_idx] = USBD_SemaphoreCreate (usbdSemaphoreEndpoint, device, i); if (usbd_driver_ep_semaphore_id[sem_idx] == NULL) { @@ -161,7 +166,7 @@ usbStatus USBD_Initialize (uint8_t device) { fpUSBD_Device_Uninitialize [device] (); } for (i = 0U; i < (usbd_ep_max_num * 2U); i++) { - sem_idx = (uint8_t)(device * usbd_ep_max_num * 2U) + i; + sem_idx = (device * usbd_ep_max_num * 2U) + i; if (usbd_driver_ep_semaphore_id[sem_idx] != NULL) { USBD_SemaphoreDelete (usbd_driver_ep_semaphore_id[sem_idx]); usbd_driver_ep_semaphore_id[sem_idx] = NULL; @@ -402,6 +407,12 @@ usbStatus USBD_SetSerialNumber (uint8_t device, const char *string) { EvrUSBD_Core_SetSerialNumber(device); + // Check parameters + if (string == NULL) { + status = usbInvalidParameter; + goto exit; + } + status = USBD_CheckDevice (device); if (status != usbOK) { goto exit; @@ -412,11 +423,24 @@ usbStatus USBD_SetSerialNumber (uint8_t device, const char *string) { ptr_str_desc = (USB_STRING_DESCRIPTOR *)usbd_desc_ptr[device]->ser_num_string_descriptor; if (ptr_str_desc == NULL) { status = usbUnknownError; goto exit; } - str_len = strlen(string); - if (str_len > ptr_dev_cfg->ser_num_str_len) { - str_len = ptr_dev_cfg->ser_num_str_len; + // ptr_dev_cfg->ser_num_str_len is the maximum length of the serial number string in characters as configured in the USBD_Config_n.h file + + if ((ptr_dev_cfg->ser_num_str_len == 0U) || (ptr_dev_cfg->ser_num_str_len > 126U)) { + status = usbInvalidParameter; + goto exit; } + // Determine the actual length of the provided serial number string (string parameter). + + // The string length must not exceed the maximum length configured in the USBD_Config_n.h file. + // If it does, the string will be truncated to the configured maximum length. + for (i = 0U; i < ptr_dev_cfg->ser_num_str_len; i++) { + if (string[i] == '\0') { + break; + } + } + str_len = i; + ptr_str_desc_str = (uint8_t *)ptr_str_desc + offsetof(USB_STRING_DESCRIPTOR, bString); ptr_str_desc->bLength = (uint8_t)((str_len * 2U) + 2U); @@ -449,11 +473,18 @@ usbStatus USBD_EndpointRead (uint8_t device, uint8_t ep_addr, uint8_t *buf, uint EvrUSBD_Core_EndpointRead(device, ep_addr, len); + // Check parameters + if (buf == NULL) { + status = usbInvalidParameter; + goto exit; + } + status = USBD_CheckDevice (device); if (status == usbOK) { status = USBD_DriverEndpointTransfer (device, USB_ENDPOINT_OUT(ep_addr), buf, len); } +exit: #if (defined(USBD_DEBUG) && (USBD_DEBUG == 1)) if (status != usbOK) { EvrUSBD_Core_EndpointReadFailed(device, ep_addr, status); @@ -494,13 +525,20 @@ uint32_t USBD_EndpointReadGetResult (uint8_t device, uint8_t ep_addr) { usbStatus USBD_EndpointWrite (uint8_t device, uint8_t ep_addr, const uint8_t *buf, uint32_t len) { usbStatus status; + // Check parameters + if ((buf == NULL) && (len != 0U)) { + status = usbInvalidParameter; + goto exit; + } + EvrUSBD_Core_EndpointWrite(device, ep_addr, len); status = USBD_CheckDevice (device); if (status == usbOK) { - status = USBD_DriverEndpointTransfer (device, USB_ENDPOINT_IN(ep_addr), (uint8_t *)((uint32_t)buf), len); + status = USBD_DriverEndpointTransfer (device, USB_ENDPOINT_IN(ep_addr), (uint8_t *)(uintptr_t)buf, len); } +exit: #if (defined(USBD_DEBUG) && (USBD_DEBUG == 1)) if (status != usbOK) { EvrUSBD_Core_EndpointWriteFailed(device, ep_addr, status); @@ -989,11 +1027,11 @@ static bool USBD_ReqSetAddress (uint8_t device) { /// \return true success /// \return false fail static bool USBD_ReqGetDescriptor (uint8_t device) { - const usbd_dev_t *ptr_dev_cfg; - usbd_data_t *ptr_dev_data; - static const uint8_t *ptr_desc; - static uint32_t len; - uint32_t i; + const usbd_dev_t *ptr_dev_cfg; + usbd_data_t *ptr_dev_data; + const uint8_t *ptr_desc; + uint32_t len; + uint32_t i; if (device >= usbd_dev_num) { return false; } // Check if device exists @@ -1117,10 +1155,10 @@ static bool USBD_ReqGetDescriptor (uint8_t device) { /// \return true success /// \return false fail static bool USBD_ReqGetMSDescriptor (uint8_t device) { - const usbd_dev_t *ptr_dev_cfg; - usbd_data_t *ptr_dev_data; - static const uint8_t *ptr_desc; - static uint32_t len; + const usbd_dev_t *ptr_dev_cfg; + usbd_data_t *ptr_dev_data; + const uint8_t *ptr_desc; + uint32_t len = 0U; if (device >= usbd_dev_num) { return false; } // Check if device exists @@ -1146,11 +1184,12 @@ static bool USBD_ReqGetMSDescriptor (uint8_t device) { case USB_REQUEST_TO_DEVICE: switch (ptr_dev_data->setup_packet.wIndex) { case USB_MS_OS_FEAT_EXT_COMPAT_ID_DESCRIPTOR_IDX: // Is Extended Compat ID Descriptor request - if (usbd_desc_ptr[device]->ms_os_ext_compat_id_descriptor != NULL) { // Extended Compat ID Descriptor pointer is valid - ptr_desc = usbd_desc_ptr[device]->ms_os_ext_compat_id_descriptor; - ptr_dev_data->ep0_data.data = (uint8_t *)((uint32_t)ptr_desc); - len = ((const USB_MS_OS_FEAT_EXT_COMPAT_ID_HEADER *)((uint32_t)ptr_desc))->dwLength; + if (usbd_desc_ptr[device]->ms_os_ext_compat_id_descriptor == NULL) { // Extended Compat ID Descriptor pointer is valid + return false; } + ptr_desc = usbd_desc_ptr[device]->ms_os_ext_compat_id_descriptor; + ptr_dev_data->ep0_data.data = (uint8_t *)((uint32_t)ptr_desc); + len = ((const USB_MS_OS_FEAT_EXT_COMPAT_ID_HEADER *)((uint32_t)ptr_desc))->dwLength; break; case USB_MS_OS_FEAT_EXT_PROP_DESCRIPTOR_IDX: // Is Extended Properties OS Descriptor request if (USBD_Endpoint0_ReqGetExtProp_CC (device, &ptr_desc) == false) { @@ -1170,6 +1209,8 @@ static bool USBD_ReqGetMSDescriptor (uint8_t device) { } ptr_dev_data->ep0_data.data = (uint8_t *)((uint32_t)ptr_desc); len = ((const USB_MS_OS_FEAT_EXT_PROP_HEADER *)((uint32_t)ptr_desc))->dwLength; + } else { + return false; } break; default: @@ -1924,7 +1965,10 @@ void USBD_Core_Thread (void *arg) { for (;;) { event = USBD_ThreadFlagsWait (millisec); - if ((event & 0x8000000U) == 0U) { + if ((event & 0x80000000U) != 0U) { + continue; // Ignore wait errors + } + if (event != 0U) { USBD_Core (device, event); } else { // If timeout vbus = ptr_dev_driver->DeviceGetState().vbus; diff --git a/Components/USB/Source/usbd_lib_core.h b/Components/USB/Source/usbd_lib_core.h index 54d0e22a..77b4edf8 100644 --- a/Components/USB/Source/usbd_lib_core.h +++ b/Components/USB/Source/usbd_lib_core.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_core.h * Purpose: USB Device - Core module header file @@ -43,4 +43,6 @@ extern usbStatus USBD_StatusOutStage (uint8_t device); /// \return status code that indicates the execution status of the function as defined with \ref usbStatus. extern usbStatus USBD_StatusOutStageDone (uint8_t device); +extern void USBD_Core_Thread (void *arg); + #endif // USBD_LIB_CORE_H_ diff --git a/Components/USB/Source/usbd_lib_core_adc.c b/Components/USB/Source/usbd_lib_core_adc.c index 2a065d04..12af3e80 100644 --- a/Components/USB/Source/usbd_lib_core_adc.c +++ b/Components/USB/Source/usbd_lib_core_adc.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2021 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_core_adc.c * Purpose: USB Device - Audio Device Class (ADC) core module @@ -322,7 +322,7 @@ __WEAK bool USBD_Endpoint0_Out_ADC_ReqToIF (uint8_t device) { case AUDIO_VOLUME_CONTROL: // Volume Control switch (ptr_dev_data->setup_packet.bRequest) { case AUDIO_REQUEST_SET_CUR: - if (USBD_ADC_SpeakerVolumeSetCur(index, (uint8_t)(ptr_dev_data->setup_packet.wValue), *(uint16_t *)((uint32_t)ptr_dev_cfg->ep0_buf))) { + if (USBD_ADC_SpeakerVolumeSetCur(index, (uint8_t)(ptr_dev_data->setup_packet.wValue), *((uint16_t *)ptr_dev_cfg->ep0_buf))) { if (USBD_StatusInStage(device) == usbOK) { // send ZLP IN return true; } @@ -355,7 +355,7 @@ __WEAK bool USBD_Endpoint0_Out_ADC_ReqToIF (uint8_t device) { case AUDIO_VOLUME_CONTROL: // Volume Control switch (ptr_dev_data->setup_packet.bRequest) { case AUDIO_REQUEST_SET_CUR: - if (USBD_ADC_MicrophoneVolumeSetCur(index, (uint8_t)(ptr_dev_data->setup_packet.wValue), *(uint16_t *)((uint32_t)ptr_dev_cfg->ep0_buf))) { + if (USBD_ADC_MicrophoneVolumeSetCur(index, (uint8_t)(ptr_dev_data->setup_packet.wValue), *(uint16_t *)ptr_dev_cfg->ep0_buf)) { if (USBD_StatusInStage(device) == usbOK) { // send ZLP IN return true; } diff --git a/Components/USB/Source/usbd_lib_core_cdc.c b/Components/USB/Source/usbd_lib_core_cdc.c index 0facd065..b1e6a7fe 100644 --- a/Components/USB/Source/usbd_lib_core_cdc.c +++ b/Components/USB/Source/usbd_lib_core_cdc.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2021 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_core_cdc.c * Purpose: USB Device - Communication Device Class (CDC) core module @@ -133,12 +133,12 @@ __WEAK bool USBD_Endpoint0_Setup_CDC_ReqToIF (uint8_t device) { } break; case CDC_SET_ETHERNET_MULTICAST_FILTERS: - if (ptr_dev_data->ep0_data.cnt < (ptr_dev_data->setup_packet.wValue * 6UL)) { + if (ptr_dev_data->ep0_data.cnt < ((uint32_t)ptr_dev_data->setup_packet.wValue * 6UL)) { return false; } ptr_dev_data->ep0_data.data = ptr_dev_cfg->ep0_buf; // data to be received, see USBD_EVT_OUT - if (ptr_dev_data->ep0_data.cnt > (ptr_dev_data->setup_packet.wValue * 6UL)) { - ptr_dev_data->ep0_data.cnt = (ptr_dev_data->setup_packet.wValue * 6UL); + if (ptr_dev_data->ep0_data.cnt > ((uint32_t)ptr_dev_data->setup_packet.wValue * 6UL)) { + ptr_dev_data->ep0_data.cnt = ((uint32_t)ptr_dev_data->setup_packet.wValue * 6UL); } return true; case CDC_SET_ETHERNET_PMP_FILTER: @@ -172,7 +172,7 @@ __WEAK bool USBD_Endpoint0_Setup_CDC_ReqToIF (uint8_t device) { } return true; case CDC_GET_ETHERNET_PMP_FILTER: - if (USBD_CDC_NCM_CoreGetEthernetPowerManagementPatternFilter (index, ptr_dev_data->setup_packet.wValue, (uint16_t *)((uint32_t)ptr_dev_cfg->ep0_buf))) { + if (USBD_CDC_NCM_CoreGetEthernetPowerManagementPatternFilter (index, ptr_dev_data->setup_packet.wValue, (uint16_t *)ptr_dev_cfg->ep0_buf)) { ptr_dev_data->ep0_data.data = ptr_dev_cfg->ep0_buf; // point to data to be sent if (ptr_dev_data->ep0_data.cnt > 2U) { ptr_dev_data->ep0_data.cnt = 2U; @@ -195,7 +195,7 @@ __WEAK bool USBD_Endpoint0_Setup_CDC_ReqToIF (uint8_t device) { } break; case CDC_GET_ETHERNET_STATISTIC: - if (USBD_CDC_NCM_CoreGetEthernetStatistic (index, ptr_dev_data->setup_packet.wValue, (uint32_t *)((uint32_t)ptr_dev_cfg->ep0_buf))) { + if (USBD_CDC_NCM_CoreGetEthernetStatistic (index, ptr_dev_data->setup_packet.wValue, (uint32_t *)((uintptr_t)ptr_dev_cfg->ep0_buf))) { ptr_dev_data->ep0_data.data = ptr_dev_cfg->ep0_buf; // point to data to be sent if (ptr_dev_data->ep0_data.cnt > 4U) { ptr_dev_data->ep0_data.cnt = 4U; @@ -234,7 +234,7 @@ __WEAK bool USBD_Endpoint0_Setup_CDC_ReqToIF (uint8_t device) { } break; case CDC_GET_NTB_FORMAT: - if (USBD_CDC_NCM_CoreGetNtbFormat (index, (uint16_t *)((uint32_t)ptr_dev_cfg->ep0_buf))) { + if (USBD_CDC_NCM_CoreGetNtbFormat (index, (uint16_t *)(ptr_dev_cfg->ep0_buf))) { ptr_dev_data->ep0_data.data = ptr_dev_cfg->ep0_buf; // point to data to be sent if (ptr_dev_data->ep0_data.cnt > 2U) { ptr_dev_data->ep0_data.cnt = 2U; @@ -257,7 +257,7 @@ __WEAK bool USBD_Endpoint0_Setup_CDC_ReqToIF (uint8_t device) { } break; case CDC_GET_NTB_INPUT_SIZE: - if (USBD_CDC_NCM_CoreGetNtbInputSize (index, (uint32_t *)((uint32_t)ptr_dev_cfg->ep0_buf))) { + if (USBD_CDC_NCM_CoreGetNtbInputSize (index, (uint32_t *)(ptr_dev_cfg->ep0_buf))) { ptr_dev_data->ep0_data.data = ptr_dev_cfg->ep0_buf; // point to data to be sent if (ptr_dev_data->ep0_data.cnt > 4U) { ptr_dev_data->ep0_data.cnt = 4U; @@ -270,7 +270,7 @@ __WEAK bool USBD_Endpoint0_Setup_CDC_ReqToIF (uint8_t device) { } break; case CDC_GET_MAX_DATAGRAM_SIZE: - if (USBD_CDC_NCM_CoreGetMaxDatagramSize (index, (uint16_t *)((uint32_t)ptr_dev_cfg->ep0_buf))) { + if (USBD_CDC_NCM_CoreGetMaxDatagramSize (index, (uint16_t *)(ptr_dev_cfg->ep0_buf))) { ptr_dev_data->ep0_data.data = ptr_dev_cfg->ep0_buf; // point to data to be sent if (ptr_dev_data->ep0_data.cnt > 2U) { ptr_dev_data->ep0_data.cnt = 2U; @@ -283,7 +283,7 @@ __WEAK bool USBD_Endpoint0_Setup_CDC_ReqToIF (uint8_t device) { } break; case CDC_GET_CRC_MODE: - if (USBD_CDC_NCM_CoreGetCrcMode (index, (uint16_t *)((uint32_t)ptr_dev_cfg->ep0_buf))) { + if (USBD_CDC_NCM_CoreGetCrcMode (index, (uint16_t *)(ptr_dev_cfg->ep0_buf))) { ptr_dev_data->ep0_data.data = ptr_dev_cfg->ep0_buf; // point to data to be sent if (ptr_dev_data->ep0_data.cnt > 2U) { ptr_dev_data->ep0_data.cnt = 2U; @@ -379,14 +379,14 @@ __WEAK bool USBD_Endpoint0_Out_CDC_ReqToIF (uint8_t device) { } break; case CDC_SET_NTB_INPUT_SIZE: - if (USBD_CDC_NCM_CoreSetNtbInputSize (index, *((uint32_t *)((uint32_t)ptr_dev_cfg->ep0_buf)))) { + if (USBD_CDC_NCM_CoreSetNtbInputSize (index, *((uint32_t *)ptr_dev_cfg->ep0_buf))) { if (USBD_StatusInStage(device) == usbOK) { // send ZLP IN return true; } } break; case CDC_SET_MAX_DATAGRAM_SIZE: - if (USBD_CDC_NCM_CoreSetMaxDatagramSize (index, *((uint16_t *)((uint32_t)ptr_dev_cfg->ep0_buf)))) { + if (USBD_CDC_NCM_CoreSetMaxDatagramSize (index, *((uint16_t *)ptr_dev_cfg->ep0_buf))) { if (USBD_StatusInStage(device) == usbOK) { // send ZLP IN return true; } diff --git a/Components/USB/Source/usbd_lib_core_custom_class.c b/Components/USB/Source/usbd_lib_core_custom_class.c index 9e5abc79..83400c28 100644 --- a/Components/USB/Source/usbd_lib_core_custom_class.c +++ b/Components/USB/Source/usbd_lib_core_custom_class.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_core_custom_class.c * Purpose: USB Device - Custom Class core module @@ -23,16 +23,23 @@ __WEAK bool USBD_Endpoint0_ReqGetExtProp_CC (uint8_t device, const uint8_t **pD) const usbd_custom_class_t *ptr_cc_cfg; uint8_t index, i; - if (device >= usbd_dev_num) { return false; } // Check if device exists + if (device >= usbd_dev_num) { return false; } // Check if device exists + if (usbd_dev_ptr[device] == NULL) { return false; } ptr_dev_data = usbd_dev_ptr[device]->data_ptr; if (ptr_dev_data == NULL) { return false; } for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if ((ptr_cc_cfg == NULL) || (usbd_cc_desc_ptr[index] == NULL)) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class for (i = 0U; i < 4U; i++) { if ((ptr_cc_cfg->cc[i].if_en != 0U) && ((ptr_dev_data->setup_packet.wValue & 0x00FFU) == (uint16_t)(ptr_cc_cfg->cc[i].if_num))) { - *pD = (uint8_t *)((uint32_t)usbd_cc_desc_ptr[index]->ms_ext_prop_descriptor[i]); + if (usbd_cc_desc_ptr[index]->ms_ext_prop_descriptor[i] == NULL) { + return false; + } + *pD = (uint8_t *)usbd_cc_desc_ptr[index]->ms_ext_prop_descriptor[i]; return true; } } @@ -57,8 +64,8 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_SetupPacketReceived (uint8_t devic status = usbdRequestNotProcessed; - if (device >= usbd_dev_num) { return status; } - if (fpUSBD_CustomClass_Endpoint0_SetupPacketReceived[device] == NULL) { return status; } + if (device >= usbd_dev_num) { return status; } + if (usbd_dev_ptr[device] == NULL) { return status; } ptr_dev_data = usbd_dev_ptr[device]->data_ptr; if (ptr_dev_data == NULL) { return status; } @@ -68,10 +75,16 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_SetupPacketReceived (uint8_t devic case USB_REQUEST_TO_DEVICE: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class - EvrUSBD_CC_OnEndpoint0SetupPacketReceivedDetail(device, (const void *)&ptr_dev_data->setup_packet, ptr_dev_data->len); + EvrUSBD_CC_OnEndpoint0SetupPacketReceivedDetail(index, (const void *)&ptr_dev_data->setup_packet, ptr_dev_data->len); + if (fpUSBD_CustomClass_Endpoint0_SetupPacketReceived[index] == NULL) { + continue; + } status = fpUSBD_CustomClass_Endpoint0_SetupPacketReceived[index] ((const USB_SETUP_PACKET *) &ptr_dev_data->setup_packet, &ptr_dev_data->buf, &ptr_dev_data->len); - EvrUSBD_CC_OnEndpoint0SetupPacketReceived(device, status); + EvrUSBD_CC_OnEndpoint0SetupPacketReceived(index, status); if (status != usbdRequestNotProcessed) { break; } @@ -82,24 +95,36 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_SetupPacketReceived (uint8_t devic case USB_REQUEST_TO_INTERFACE: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class for (i = 0U; i < 4U; i++) { if ((ptr_cc_cfg->cc[i].if_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_num))) { - EvrUSBD_CC_OnEndpoint0SetupPacketReceivedDetail(device, (const void *)&ptr_dev_data->setup_packet, ptr_dev_data->len); + EvrUSBD_CC_OnEndpoint0SetupPacketReceivedDetail(index, (const void *)&ptr_dev_data->setup_packet, ptr_dev_data->len); + if (fpUSBD_CustomClass_Endpoint0_SetupPacketReceived[index] == NULL) { + continue; + } status = fpUSBD_CustomClass_Endpoint0_SetupPacketReceived[index] ((const USB_SETUP_PACKET *) &ptr_dev_data->setup_packet, &ptr_dev_data->buf, &ptr_dev_data->len); - EvrUSBD_CC_OnEndpoint0SetupPacketReceived(device, status); + EvrUSBD_CC_OnEndpoint0SetupPacketReceived(index, status); if (status != usbdRequestNotProcessed) { break; } } } } + if (status != usbdRequestNotProcessed) { + break; + } } break; case USB_REQUEST_TO_ENDPOINT: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class for (i = 0U; i < 4U; i++) { if (ptr_cc_cfg->cc[i].if_en != 0U) { @@ -111,9 +136,12 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_SetupPacketReceived (uint8_t devic ((ptr_cc_cfg->cc[i].if_ep5_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_ep5_addr))) || ((ptr_cc_cfg->cc[i].if_ep6_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_ep6_addr))) || ((ptr_cc_cfg->cc[i].if_ep7_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_ep7_addr)))) { - EvrUSBD_CC_OnEndpoint0SetupPacketReceivedDetail(device, (const void *)&ptr_dev_data->setup_packet, ptr_dev_data->len); + EvrUSBD_CC_OnEndpoint0SetupPacketReceivedDetail(index, (const void *)&ptr_dev_data->setup_packet, ptr_dev_data->len); + if (fpUSBD_CustomClass_Endpoint0_SetupPacketReceived[index] == NULL) { + continue; + } status = fpUSBD_CustomClass_Endpoint0_SetupPacketReceived[index] ((const USB_SETUP_PACKET *) &ptr_dev_data->setup_packet, &ptr_dev_data->buf, &ptr_dev_data->len); - EvrUSBD_CC_OnEndpoint0SetupPacketReceived(device, status); + EvrUSBD_CC_OnEndpoint0SetupPacketReceived(index, status); if (status != usbdRequestNotProcessed) { break; } @@ -127,10 +155,16 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_SetupPacketReceived (uint8_t devic case USB_REQUEST_TO_OTHER: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class - EvrUSBD_CC_OnEndpoint0SetupPacketReceivedDetail(device, (const void *)&ptr_dev_data->setup_packet, ptr_dev_data->len); - status = fpUSBD_CustomClass_Endpoint0_SetupPacketReceived[device] ((const USB_SETUP_PACKET *) &ptr_dev_data->setup_packet, &ptr_dev_data->buf, &ptr_dev_data->len); - EvrUSBD_CC_OnEndpoint0SetupPacketReceived(device, status); + EvrUSBD_CC_OnEndpoint0SetupPacketReceivedDetail(index, (const void *)&ptr_dev_data->setup_packet, ptr_dev_data->len); + if (fpUSBD_CustomClass_Endpoint0_SetupPacketReceived[index] == NULL) { + continue; + } + status = fpUSBD_CustomClass_Endpoint0_SetupPacketReceived[index] ((const USB_SETUP_PACKET *) &ptr_dev_data->setup_packet, &ptr_dev_data->buf, &ptr_dev_data->len); + EvrUSBD_CC_OnEndpoint0SetupPacketReceived(index, status); if (status != usbdRequestNotProcessed) { break; } @@ -153,8 +187,8 @@ __WEAK void USBD_Class_Endpoint0_SetupPacketProcessed (uint8_t device) { const usbd_custom_class_t *ptr_cc_cfg; uint8_t index, i; - if (device >= usbd_dev_num) { return; } - if (fpUSBD_CustomClass_Endpoint0_SetupPacketProcessed[device] == NULL) { return; } + if (device >= usbd_dev_num) { return; } + if (usbd_dev_ptr[device] == NULL) { return; } ptr_dev_data = usbd_dev_ptr[device]->data_ptr; if (ptr_dev_data == NULL) { return; } @@ -164,10 +198,16 @@ __WEAK void USBD_Class_Endpoint0_SetupPacketProcessed (uint8_t device) { case USB_REQUEST_TO_DEVICE: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class - EvrUSBD_CC_OnEndpoint0SetupPacketProcessedDetail(device, (const void *)&ptr_dev_data->setup_packet); + EvrUSBD_CC_OnEndpoint0SetupPacketProcessedDetail(index, (const void *)&ptr_dev_data->setup_packet); + if (fpUSBD_CustomClass_Endpoint0_SetupPacketProcessed[index] == NULL) { + continue; + } fpUSBD_CustomClass_Endpoint0_SetupPacketProcessed[index] ((const USB_SETUP_PACKET *) &ptr_dev_data->setup_packet); - EvrUSBD_CC_OnEndpoint0SetupPacketProcessed(device); + EvrUSBD_CC_OnEndpoint0SetupPacketProcessed(index); } } break; @@ -176,12 +216,18 @@ __WEAK void USBD_Class_Endpoint0_SetupPacketProcessed (uint8_t device) { ptr_dev_data = (usbd_dev_ptr[device])->data_ptr; for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class for (i = 0U; i < 4U; i++) { if ((ptr_cc_cfg->cc[i].if_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_num))) { - EvrUSBD_CC_OnEndpoint0SetupPacketProcessedDetail(device, (const void *)&ptr_dev_data->setup_packet); + EvrUSBD_CC_OnEndpoint0SetupPacketProcessedDetail(index, (const void *)&ptr_dev_data->setup_packet); + if (fpUSBD_CustomClass_Endpoint0_SetupPacketProcessed[index] == NULL) { + continue; + } fpUSBD_CustomClass_Endpoint0_SetupPacketProcessed[index] ((const USB_SETUP_PACKET *) &ptr_dev_data->setup_packet); - EvrUSBD_CC_OnEndpoint0SetupPacketProcessed(device); + EvrUSBD_CC_OnEndpoint0SetupPacketProcessed(index); } } } @@ -191,6 +237,9 @@ __WEAK void USBD_Class_Endpoint0_SetupPacketProcessed (uint8_t device) { case USB_REQUEST_TO_ENDPOINT: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class for (i = 0U; i < 4U; i++) { if (ptr_cc_cfg->cc[i].if_en != 0U) { @@ -202,9 +251,12 @@ __WEAK void USBD_Class_Endpoint0_SetupPacketProcessed (uint8_t device) { ((ptr_cc_cfg->cc[i].if_ep5_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_ep5_addr))) || ((ptr_cc_cfg->cc[i].if_ep6_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_ep6_addr))) || ((ptr_cc_cfg->cc[i].if_ep7_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_ep7_addr)))) { - EvrUSBD_CC_OnEndpoint0SetupPacketProcessedDetail(device, (const void *)&ptr_dev_data->setup_packet); + EvrUSBD_CC_OnEndpoint0SetupPacketProcessedDetail(index, (const void *)&ptr_dev_data->setup_packet); + if (fpUSBD_CustomClass_Endpoint0_SetupPacketProcessed[index] == NULL) { + continue; + } fpUSBD_CustomClass_Endpoint0_SetupPacketProcessed[index] ((const USB_SETUP_PACKET *) &ptr_dev_data->setup_packet); - EvrUSBD_CC_OnEndpoint0SetupPacketProcessed(device); + EvrUSBD_CC_OnEndpoint0SetupPacketProcessed(index); } } } @@ -215,10 +267,16 @@ __WEAK void USBD_Class_Endpoint0_SetupPacketProcessed (uint8_t device) { case USB_REQUEST_TO_OTHER: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class - EvrUSBD_CC_OnEndpoint0SetupPacketProcessedDetail(device, (const void *)&ptr_dev_data->setup_packet); + EvrUSBD_CC_OnEndpoint0SetupPacketProcessedDetail(index, (const void *)&ptr_dev_data->setup_packet); + if (fpUSBD_CustomClass_Endpoint0_SetupPacketProcessed[index] == NULL) { + continue; + } fpUSBD_CustomClass_Endpoint0_SetupPacketProcessed[index] ((const USB_SETUP_PACKET *) &ptr_dev_data->setup_packet); - EvrUSBD_CC_OnEndpoint0SetupPacketProcessed(device); + EvrUSBD_CC_OnEndpoint0SetupPacketProcessed(index); } } break; @@ -244,8 +302,8 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_OutDataReceived (uint8_t device) { status = usbdRequestNotProcessed; - if (device >= usbd_dev_num) { return status; } - if (fpUSBD_CustomClass_Endpoint0_OutDataReceived[device] == NULL) { return status; } + if (device >= usbd_dev_num) { return status; } + if (usbd_dev_ptr[device] == NULL) { return status; } ptr_dev_data = usbd_dev_ptr[device]->data_ptr; if (ptr_dev_data == NULL) { return status; } @@ -255,9 +313,15 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_OutDataReceived (uint8_t device) { case USB_REQUEST_TO_DEVICE: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class + if (fpUSBD_CustomClass_Endpoint0_OutDataReceived [index] == NULL) { + continue; + } status = fpUSBD_CustomClass_Endpoint0_OutDataReceived [index] (ptr_dev_data->len); - EvrUSBD_CC_OnEndpoint0OutDataReceived(device, ptr_dev_data->len, status); + EvrUSBD_CC_OnEndpoint0OutDataReceived(index, ptr_dev_data->len, status); if (status != usbdRequestNotProcessed) { break; } @@ -268,11 +332,17 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_OutDataReceived (uint8_t device) { case USB_REQUEST_TO_INTERFACE: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class for (i = 0U; i < 4U; i++) { if ((ptr_cc_cfg->cc[i].if_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_num))) { + if (fpUSBD_CustomClass_Endpoint0_OutDataReceived [index] == NULL) { + continue; + } status = fpUSBD_CustomClass_Endpoint0_OutDataReceived [index] (ptr_dev_data->len); - EvrUSBD_CC_OnEndpoint0OutDataReceived(device, ptr_dev_data->len, status); + EvrUSBD_CC_OnEndpoint0OutDataReceived(index, ptr_dev_data->len, status); if (status != usbdRequestNotProcessed) { break; } @@ -285,6 +355,9 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_OutDataReceived (uint8_t device) { case USB_REQUEST_TO_ENDPOINT: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class for (i = 0U; i < 4U; i++) { if (ptr_cc_cfg->cc[i].if_en != 0U) { @@ -296,8 +369,11 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_OutDataReceived (uint8_t device) { ((ptr_cc_cfg->cc[i].if_ep5_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_ep5_addr))) || ((ptr_cc_cfg->cc[i].if_ep6_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_ep6_addr))) || ((ptr_cc_cfg->cc[i].if_ep7_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_ep7_addr)))) { + if (fpUSBD_CustomClass_Endpoint0_OutDataReceived[index] == NULL) { + continue; + } status = fpUSBD_CustomClass_Endpoint0_OutDataReceived [index] (ptr_dev_data->len); - EvrUSBD_CC_OnEndpoint0OutDataReceived(device, ptr_dev_data->len, status); + EvrUSBD_CC_OnEndpoint0OutDataReceived(index, ptr_dev_data->len, status); if (status != usbdRequestNotProcessed) { break; } @@ -311,9 +387,15 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_OutDataReceived (uint8_t device) { case USB_REQUEST_TO_OTHER: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class + if (fpUSBD_CustomClass_Endpoint0_OutDataReceived[index] == NULL) { + continue; + } status = fpUSBD_CustomClass_Endpoint0_OutDataReceived [index] (ptr_dev_data->len); - EvrUSBD_CC_OnEndpoint0OutDataReceived(device, ptr_dev_data->len, status); + EvrUSBD_CC_OnEndpoint0OutDataReceived(index, ptr_dev_data->len, status); if (status != usbdRequestNotProcessed) { break; } @@ -344,8 +426,8 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_InDataSent (uint8_t device) { status = usbdRequestNotProcessed; - if (device >= usbd_dev_num) { return status; } - if (fpUSBD_CustomClass_Endpoint0_InDataSent[device] == NULL) { return status; } + if (device >= usbd_dev_num) { return status; } + if (usbd_dev_ptr[device] == NULL) { return status; } ptr_dev_data = usbd_dev_ptr[device]->data_ptr; if (ptr_dev_data == NULL) { return status; } @@ -355,9 +437,15 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_InDataSent (uint8_t device) { case USB_REQUEST_TO_DEVICE: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class + if (fpUSBD_CustomClass_Endpoint0_InDataSent [index] == NULL) { + continue; + } status = fpUSBD_CustomClass_Endpoint0_InDataSent [index] (ptr_dev_data->len); - EvrUSBD_CC_OnEndpoint0InDataSent(device, ptr_dev_data->len, status); + EvrUSBD_CC_OnEndpoint0InDataSent(index, ptr_dev_data->len, status); if (status != usbdRequestNotProcessed) { break; } @@ -368,11 +456,17 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_InDataSent (uint8_t device) { case USB_REQUEST_TO_INTERFACE: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class for (i = 0U; i < 4U; i++) { if ((ptr_cc_cfg->cc[i].if_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_num))) { + if (fpUSBD_CustomClass_Endpoint0_InDataSent [index] == NULL) { + continue; + } status = fpUSBD_CustomClass_Endpoint0_InDataSent [index] (ptr_dev_data->len); - EvrUSBD_CC_OnEndpoint0InDataSent(device, ptr_dev_data->len, status); + EvrUSBD_CC_OnEndpoint0InDataSent(index, ptr_dev_data->len, status); if (status != usbdRequestNotProcessed) { break; } @@ -385,6 +479,9 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_InDataSent (uint8_t device) { case USB_REQUEST_TO_ENDPOINT: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class for (i = 0U; i < 4U; i++) { if (ptr_cc_cfg->cc[i].if_en != 0U) { @@ -396,8 +493,11 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_InDataSent (uint8_t device) { ((ptr_cc_cfg->cc[i].if_ep5_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_ep5_addr))) || ((ptr_cc_cfg->cc[i].if_ep6_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_ep6_addr))) || ((ptr_cc_cfg->cc[i].if_ep7_en != 0U) && (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_cc_cfg->cc[i].if_ep7_addr)))) { + if (fpUSBD_CustomClass_Endpoint0_InDataSent[index] == NULL) { + continue; + } status = fpUSBD_CustomClass_Endpoint0_InDataSent [index] (ptr_dev_data->len); - EvrUSBD_CC_OnEndpoint0InDataSent(device, ptr_dev_data->len, status); + EvrUSBD_CC_OnEndpoint0InDataSent(index, ptr_dev_data->len, status); if (status != usbdRequestNotProcessed) { break; } @@ -411,9 +511,15 @@ __WEAK usbdRequestStatus USBD_Class_Endpoint0_InDataSent (uint8_t device) { case USB_REQUEST_TO_OTHER: for (index = 0U; index < usbd_custom_class_num; index++) { ptr_cc_cfg = usbd_custom_class_ptr[index]; + if (ptr_cc_cfg == NULL) { + continue; + } if (device == ptr_cc_cfg->dev_num) { // If request is on device containing custom class + if (fpUSBD_CustomClass_Endpoint0_InDataSent [index] == NULL) { + continue; + } status = fpUSBD_CustomClass_Endpoint0_InDataSent [index] (ptr_dev_data->len); - EvrUSBD_CC_OnEndpoint0InDataSent(device, ptr_dev_data->len, status); + EvrUSBD_CC_OnEndpoint0InDataSent(index, ptr_dev_data->len, status); if (status != usbdRequestNotProcessed) { break; } diff --git a/Components/USB/Source/usbd_lib_core_device.c b/Components/USB/Source/usbd_lib_core_device.c index a11ffac5..f062992a 100644 --- a/Components/USB/Source/usbd_lib_core_device.c +++ b/Components/USB/Source/usbd_lib_core_device.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_core_custom_device.c * Purpose: USB Device - Device core module @@ -24,14 +24,18 @@ __WEAK usbdRequestStatus USBD_Core_Endpoint0_SetupPacketReceived (uint8_t device usbd_data_t *ptr_dev_data; usbdRequestStatus status; - if (device >= usbd_dev_num) { return usbdRequestNotProcessed; } + if (device >= usbd_dev_num) { return usbdRequestNotProcessed; } + if (usbd_dev_ptr[device] == NULL) { return usbdRequestNotProcessed; } ptr_dev_data = usbd_dev_ptr[device]->data_ptr; if (ptr_dev_data == NULL) { return usbdRequestNotProcessed; } // Allow SETUP PACKET processing at device level ptr_dev_data->buf = NULL; ptr_dev_data->len = 0U; - status = fpUSBD_Device_Endpoint0_SetupPacketReceived [device] ((const USB_SETUP_PACKET *)&ptr_dev_data->setup_packet, &ptr_dev_data->buf, &ptr_dev_data->len); + status = usbdRequestNotProcessed; + if (fpUSBD_Device_Endpoint0_SetupPacketReceived[device] != NULL) { + status = fpUSBD_Device_Endpoint0_SetupPacketReceived [device] ((const USB_SETUP_PACKET *)&ptr_dev_data->setup_packet, &ptr_dev_data->buf, &ptr_dev_data->len); + } EvrUSBD_Core_OnSetupPacketReceived(device, status); EvrUSBD_Core_OnSetupPacketReceivedDetail(device, (const void *)&ptr_dev_data->setup_packet, ptr_dev_data->len); if (status != usbdRequestNotProcessed) { @@ -49,7 +53,12 @@ __WEAK usbdRequestStatus USBD_Core_Endpoint0_SetupPacketReceived (uint8_t device __WEAK void USBD_Core_Endpoint0_SetupPacketProcessed (uint8_t device) { const usbd_data_t *ptr_dev_data; - if (device >= usbd_dev_num) { return; } + if (device >= usbd_dev_num) { return; } + if (usbd_dev_ptr[device] == NULL) { return; } + + if (fpUSBD_Device_Endpoint0_SetupPacketProcessed[device] == NULL) { + return; + } ptr_dev_data = usbd_dev_ptr[device]->data_ptr; if (ptr_dev_data == NULL) { return; } @@ -73,7 +82,12 @@ __WEAK usbdRequestStatus USBD_Core_Endpoint0_OutDataReceived (uint8_t device) { const usbd_data_t *ptr_dev_data; usbdRequestStatus status; - if (device >= usbd_dev_num) { return usbdRequestNotProcessed; } + if (device >= usbd_dev_num) { return usbdRequestNotProcessed; } + if (usbd_dev_ptr[device] == NULL) { return usbdRequestNotProcessed; } + + if (fpUSBD_Device_Endpoint0_OutDataReceived[device] == NULL) { + return usbdRequestNotProcessed; + } ptr_dev_data = usbd_dev_ptr[device]->data_ptr; if (ptr_dev_data == NULL) { return usbdRequestNotProcessed; } @@ -99,7 +113,12 @@ __WEAK usbdRequestStatus USBD_Core_Endpoint0_InDataSent (uint8_t device) { const usbd_data_t *ptr_dev_data; usbdRequestStatus status; - if (device >= usbd_dev_num) { return usbdRequestNotProcessed; } + if (device >= usbd_dev_num) { return usbdRequestNotProcessed; } + if (usbd_dev_ptr[device] == NULL) { return usbdRequestNotProcessed; } + + if (fpUSBD_Device_Endpoint0_InDataSent[device] == NULL) { + return usbdRequestNotProcessed; + } ptr_dev_data = usbd_dev_ptr[device]->data_ptr; if (ptr_dev_data == NULL) { return usbdRequestNotProcessed; } diff --git a/Components/USB/Source/usbd_lib_core_hid.c b/Components/USB/Source/usbd_lib_core_hid.c index f5fdc45d..f0514d0f 100644 --- a/Components/USB/Source/usbd_lib_core_hid.c +++ b/Components/USB/Source/usbd_lib_core_hid.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2021 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_core_hid.c * Purpose: USB Device - Human Interface Device (HID) core module @@ -23,7 +23,7 @@ __WEAK bool USBD_Endpoint0_ReqGetDescriptor_HID (uint8_t device, const uint8_t * const usbd_dev_t *ptr_dev_cfg; const usbd_data_t *ptr_dev_data; const usbd_hid_t *ptr_hid_cfg; - uint8_t index, cnt; + uint8_t index; if (device >= usbd_dev_num) { return false; } // Check if device exists @@ -33,44 +33,49 @@ __WEAK bool USBD_Endpoint0_ReqGetDescriptor_HID (uint8_t device, const uint8_t * switch (ptr_dev_data->setup_packet.wValue >> 8) { case HID_HID_DESCRIPTOR_TYPE: - cnt = 0U; for (index = 0U; index < usbd_hid_num; index++) { ptr_hid_cfg = usbd_hid_ptr[index]; if (ptr_hid_cfg == NULL) { continue; } if (device == ptr_hid_cfg->dev_num) { if (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_hid_cfg->if_num)) { // Interface number correct? break; } - cnt++; } } - if (cnt == usbd_hid_num) { + if (index == usbd_hid_num) { return false; // If HID Interface does not exist } if ((ptr_dev_cfg->hs == 0U) && (ptr_dev_data->high_speed != 0U)) { return false; // High speed request but high-speed not enabled } + if (usbd_desc_ptr[device] == NULL) { + return false; + } if (ptr_dev_data->high_speed == 0U) { - *pD = (uint8_t *)((uint32_t)usbd_desc_ptr[device]->config_descriptor_fs); + *pD = (uint8_t *)(uintptr_t)usbd_desc_ptr[device]->config_descriptor_fs; } else { - *pD = (uint8_t *)((uint32_t)usbd_desc_ptr[device]->config_descriptor_hs); + *pD = (uint8_t *)(uintptr_t)usbd_desc_ptr[device]->config_descriptor_hs; + } + if (usbd_hid_desc_ptr[index] == NULL) { + return false; } *pD += usbd_hid_desc_ptr[index]->hid_descriptor_offset; *len = sizeof(HID_DESCRIPTOR); break; case HID_REPORT_DESCRIPTOR_TYPE: - cnt = 0U; for (index = 0U; index < usbd_hid_num; index++) { ptr_hid_cfg = usbd_hid_ptr[index]; if (ptr_hid_cfg == NULL) { continue; } if (device == ptr_hid_cfg->dev_num) { if (ptr_dev_data->setup_packet.wIndex == (uint16_t)(ptr_hid_cfg->if_num)) { // Interface number correct? break; } - cnt++; } } - if (cnt == usbd_hid_num) { + if (index == usbd_hid_num) { return false; // If HID Interface does not exist } + if (usbd_hid_desc_ptr[index] == NULL) { + return false; + } *pD = usbd_hid_desc_ptr[index]->report_descriptor; *len = usbd_hid_desc_ptr[index]->report_descriptor_size; break; @@ -208,6 +213,9 @@ __WEAK bool USBD_Endpoint0_Out_HID_ReqToIF (uint8_t device) { if (device >= usbd_dev_num) { return false; } // Check if device exists // Check all necessary pointers + if (usbd_dev_ptr[device] == NULL) { + return false; + } ptr_dev_data = usbd_dev_ptr[device]->data_ptr; if (ptr_dev_data == NULL) { return false; } for (index = 0U; index < usbd_hid_num; index++) { diff --git a/Components/USB/Source/usbd_lib_custom_class.c b/Components/USB/Source/usbd_lib_custom_class.c index 22ed5aa3..312bd320 100644 --- a/Components/USB/Source/usbd_lib_custom_class.c +++ b/Components/USB/Source/usbd_lib_custom_class.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_custom_class.c * Purpose: USB Device - Custom Class module @@ -31,7 +31,7 @@ static usbStatus USBD_CustomClass_CheckInstance (uint8_t instance); __WEAK usbStatus USBD_CustomClass_Initialize (uint8_t instance) { uint32_t ep_msk; usbStatus status; - uint8_t index; + uint8_t device, index; status = USBD_CustomClass_CheckInstance(instance); if (status != usbOK) { @@ -43,20 +43,34 @@ __WEAK usbStatus USBD_CustomClass_Initialize (uint8_t instance) { fpUSBD_CustomClass_Initialize[instance] (); } + device = usbd_custom_class_ptr[instance]->dev_num; ep_msk = usbd_custom_class_ptr[instance]->ep_msk; for (index = 1U; index < usbd_ep_max_num; index ++) { - if ((usbd_ep_thread_id_ptr[(instance * usbd_ep_max_num) + index] != NULL) && ((ep_msk & (1UL << index)) != 0U)) { + if ((usbd_ep_thread_id_ptr[(device * usbd_ep_max_num) + index] != NULL) && ((ep_msk & (1UL << index)) != 0U)) { if (usbd_custom_class_ep_thread_id[instance][index] == NULL) { usbd_custom_class_ep_thread_id[instance][index] = USBD_ThreadCreate (usbdThreadCC, (uint8_t)(instance * usbd_ep_max_num) + index); if (usbd_custom_class_ep_thread_id[instance][index] == NULL) { - return usbThreadError; + status = usbThreadError; } } } } - return usbOK; + // If thread creation failed, terminate any already created thread + if (status != usbOK) { + for (index = 1U; index < usbd_ep_max_num; index ++) { + if ((usbd_ep_thread_id_ptr[(device * usbd_ep_max_num) + index] != NULL) && ((ep_msk & (1UL << index)) != 0U)) { + if (usbd_custom_class_ep_thread_id[instance][index] != NULL) { + if (USBD_ThreadTerminate (usbd_custom_class_ep_thread_id[instance][index]) == osOK) { + usbd_custom_class_ep_thread_id[instance][index] = NULL; + } + } + } + } + } + + return status; } /// \brief Uninitialize Custom Class device instance and terminate Endpoint handling thread(s) @@ -65,17 +79,18 @@ __WEAK usbStatus USBD_CustomClass_Initialize (uint8_t instance) { __WEAK usbStatus USBD_CustomClass_Uninitialize (uint8_t instance) { uint32_t ep_msk; usbStatus status; - uint8_t index; + uint8_t device, index; status = USBD_CustomClass_CheckInstance(instance); if (status != usbOK) { return status; } + device = usbd_custom_class_ptr[instance]->dev_num; ep_msk = usbd_custom_class_ptr[instance]->ep_msk; for (index = 1U; index < usbd_ep_max_num; index ++) { - if ((usbd_ep_thread_id_ptr[(instance * usbd_ep_max_num) + index] != NULL) && ((ep_msk & (1UL << index)) != 0U)) { + if ((usbd_ep_thread_id_ptr[(device * usbd_ep_max_num) + index] != NULL) && ((ep_msk & (1UL << index)) != 0U)) { if (usbd_custom_class_ep_thread_id[instance][index] != NULL) { if (USBD_ThreadTerminate (usbd_custom_class_ep_thread_id[instance][index]) != 0) { return usbThreadError; @@ -150,6 +165,7 @@ __WEAK void USBD_CustomClass_EndpointStop (uint8_t instance, uint8_t ep_addr) { static usbStatus USBD_CustomClass_CheckInstance (uint8_t instance) { if (instance >= usbd_custom_class_num) { return usbClassErrorCustom; } + if (usbd_custom_class_ptr[instance] == NULL) { return usbClassErrorCustom; } if (usbd_custom_class_ptr[instance]->dev_num >= usbd_dev_num) { return usbDeviceError; } if (usbd_dev_ptr[usbd_custom_class_ptr[instance]->dev_num] == NULL) { return usbDeviceError; } if (usbd_dev_ptr[usbd_custom_class_ptr[instance]->dev_num]->data_ptr == NULL) { return usbDeviceError; } @@ -164,7 +180,8 @@ static usbStatus USBD_CustomClass_CheckInstance (uint8_t instance) { /// \param[in] instance instance of Custom Class class. void USBD_CustomClass_EP_Thread (void *arg) { uint32_t event; - uint8_t ep_num, ep_evt, idx; + uint16_t idx; + uint8_t ep_num, ep_evt; uint8_t instance; instance = (uint8_t)((uint32_t)arg); @@ -174,12 +191,15 @@ void USBD_CustomClass_EP_Thread (void *arg) { for (;;) { event = USBD_ThreadFlagsWait (0xFFFFFFFFU); - if ((event & 0x8000000U) == 0U) { + if ((event & 0x80000000U) == 0U) { // (event >> 12) & 0x0F = endpoint number // (event >> 8) & 0x0F = endpoint event ep_num = (uint8_t)((event >> 12) & 0x0FU); + if (ep_num >= usbd_ep_max_num) { + continue; + } ep_evt = (uint8_t)((event >> 8) & 0x0FU); - idx = (uint8_t)(instance * usbd_ep_max_num) + ep_num; + idx = ((uint16_t)instance * usbd_ep_max_num) + ep_num; if (fpUSBD_Endpoint_Event[idx] != NULL) { EvrUSBD_CC_OnEndpointmEvent(instance, ep_num, ep_evt); fpUSBD_Endpoint_Event[idx](ep_evt); diff --git a/Components/USB/Source/usbd_lib_custom_class.h b/Components/USB/Source/usbd_lib_custom_class.h index 3f72f6b1..1e1d1ed0 100644 --- a/Components/USB/Source/usbd_lib_custom_class.h +++ b/Components/USB/Source/usbd_lib_custom_class.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_custom_class.h * Purpose: USB Device - Custom Class module header file @@ -42,4 +42,6 @@ extern void USBD_CustomClass_EndpointStart (uint8_t instance, uint8_t ep_addr); /// - ep_addr.7: direction extern void USBD_CustomClass_EndpointStop (uint8_t instance, uint8_t ep_addr); +extern void USBD_CustomClass_EP_Thread (void *arg); + #endif // USBD_LIB_CUSTOM_CLASS_H_ diff --git a/Components/USB/Source/usbd_lib_drv.c b/Components/USB/Source/usbd_lib_drv.c index 215c1dad..99a04372 100644 --- a/Components/USB/Source/usbd_lib_drv.c +++ b/Components/USB/Source/usbd_lib_drv.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_drv.c * Purpose: USB Device - Driver access module @@ -8,6 +8,8 @@ #include "usbd_lib_drv.h" +#include + #include "usb_lib_debug.h" #include "usbd_lib_config_def.h" #include "usbd_evr.h" @@ -22,6 +24,14 @@ /// \param[in] device Device Index /// \return \ref ARM_DRIVER_VERSION ARM_DRIVER_VERSION USBD_DriverGetVersion (uint8_t device) { + ARM_DRIVER_VERSION ver; + + // Check parameters + if (device >= usbd_dev_num) { + memset(&ver, 0, sizeof(ARM_DRIVER_VERSION)); + return ver; + } + return (usbd_dev_ptr[device]->driver->GetVersion()); } @@ -29,6 +39,14 @@ ARM_DRIVER_VERSION USBD_DriverGetVersion (uint8_t device) { /// \param[in] device Device Index /// \return \ref ARM_USBD_CAPABILITIES ARM_USBD_CAPABILITIES USBD_DriverGetCapabilities (uint8_t device) { + ARM_USBD_CAPABILITIES caps; + + // Check parameters + if (device >= usbd_dev_num) { + memset(&caps, 0, sizeof(ARM_USBD_CAPABILITIES)); + return caps; + } + return (usbd_dev_ptr[device]->driver->GetCapabilities()); } @@ -41,6 +59,11 @@ usbStatus USBD_DriverInitialize (uint8_t device, ARM_USBD_SignalDeviceEvent_t cb int32_t driver_status; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return usbInvalidParameter; + } + retry = USBD_DRIVER_RETRY_COUNT; do { if (USBD_SemaphoreAcquire (usbd_driver_semaphore_id[device], 0U) == 0) { break; } @@ -77,6 +100,11 @@ usbStatus USBD_DriverUninitialize (uint8_t device) { int32_t driver_status; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return usbInvalidParameter; + } + retry = USBD_DRIVER_RETRY_COUNT; do { if (USBD_SemaphoreAcquire (usbd_driver_semaphore_id[device], 0U) == 0) { break; } @@ -114,6 +142,11 @@ usbStatus USBD_DriverPowerControl (uint8_t device, ARM_POWER_STATE state) { int32_t driver_status; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return usbInvalidParameter; + } + retry = USBD_DRIVER_RETRY_COUNT; do { if (USBD_SemaphoreAcquire (usbd_driver_semaphore_id[device], 0U) == 0) { break; } @@ -150,6 +183,11 @@ usbStatus USBD_DriverDeviceConnect (uint8_t device) { int32_t driver_status; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return usbInvalidParameter; + } + retry = USBD_DRIVER_RETRY_COUNT; do { if (USBD_SemaphoreAcquire (usbd_driver_semaphore_id[device], 0U) == 0) { break; } @@ -186,6 +224,11 @@ usbStatus USBD_DriverDeviceDisconnect (uint8_t device) { int32_t driver_status; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return usbInvalidParameter; + } + retry = USBD_DRIVER_RETRY_COUNT; do { if (USBD_SemaphoreAcquire (usbd_driver_semaphore_id[device], 0U) == 0) { break; } @@ -222,6 +265,13 @@ USBD_STATE USBD_DriverDeviceGetState (uint8_t device) { ARM_USBD_STATE hw_val; USBD_STATE val; + memset(&val, 0, sizeof(USBD_STATE)); + + // Check parameters + if (device >= usbd_dev_num) { + goto exit; + } + hw_val = usbd_dev_ptr[device]->driver->DeviceGetState (); val.vbus = hw_val.vbus; switch (hw_val.speed) { @@ -242,6 +292,7 @@ USBD_STATE USBD_DriverDeviceGetState (uint8_t device) { EvrUSBD_Driver_DeviceGetState(device, hw_val); +exit: return val; } @@ -252,6 +303,11 @@ usbStatus USBD_DriverDeviceRemoteWakeup (uint8_t device) { int32_t driver_status; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return usbInvalidParameter; + } + retry = USBD_DRIVER_RETRY_COUNT; do { if (USBD_SemaphoreAcquire (usbd_driver_semaphore_id[device], 0U) == 0) { break; } @@ -289,6 +345,11 @@ usbStatus USBD_DriverDeviceSetAddress (uint8_t device, uint8_t dev_addr) { int32_t driver_status; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return usbInvalidParameter; + } + retry = USBD_DRIVER_RETRY_COUNT; do { if (USBD_SemaphoreAcquire (usbd_driver_semaphore_id[device], 0U) == 0) { break; } @@ -326,6 +387,14 @@ usbStatus USBD_DriverReadSetupPacket (uint8_t device, uint8_t *data) { int32_t driver_status; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return usbInvalidParameter; + } + if (data == NULL) { + return usbInvalidParameter; + } + retry = USBD_DRIVER_RETRY_COUNT; do { if (USBD_SemaphoreAcquire (usbd_driver_semaphore_id[device], 0U) == 0) { break; } @@ -368,9 +437,17 @@ usbStatus USBD_DriverEndpointConfigure (uint8_t device, uint8_t ep_addr, uint8_t uint8_t ep_idx, sem_idx; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return usbInvalidParameter; + } + if ((ep_addr & 0x7FU) >= usbd_ep_max_num) { + return usbInvalidParameter; + } + ep_idx = (ep_addr & 0x0FU) + ((ep_addr & 0x80U) >> 3); - sem_idx = (uint8_t)(device * usbd_ep_max_num * 2U) + (ep_addr & 0x0FU); + sem_idx = (device * usbd_ep_max_num * 2U) + (ep_addr & 0x0FU); if (ep_idx >= 16U) { sem_idx += usbd_ep_max_num; } @@ -439,9 +516,17 @@ usbStatus USBD_DriverEndpointUnconfigure (uint8_t device, uint8_t ep_addr) { uint8_t ep_idx, sem_idx; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return usbInvalidParameter; + } + if ((ep_addr & 0x7FU) >= usbd_ep_max_num) { + return usbInvalidParameter; + } + ep_idx = (ep_addr & 0x0FU) + ((ep_addr & 0x80U) >> 3); - sem_idx = (uint8_t)(device * usbd_ep_max_num * 2U) + (ep_addr & 0x0FU); + sem_idx = (device * usbd_ep_max_num * 2U) + (ep_addr & 0x0FU); if (ep_idx >= 16U) { sem_idx += usbd_ep_max_num; } @@ -499,9 +584,17 @@ usbStatus USBD_DriverEndpointStall (uint8_t device, uint8_t ep_addr, bool stall) uint8_t ep_idx, sem_idx; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return usbInvalidParameter; + } + if ((ep_addr & 0x7FU) >= usbd_ep_max_num) { + return usbInvalidParameter; + } + ep_idx = (ep_addr & 0x0FU) + ((ep_addr & 0x80U) >> 3); - sem_idx = (uint8_t)(device * usbd_ep_max_num * 2U) + (ep_addr & 0x0FU); + sem_idx = (device * usbd_ep_max_num * 2U) + (ep_addr & 0x0FU); if (ep_idx >= 16U) { sem_idx += usbd_ep_max_num; } @@ -559,10 +652,18 @@ usbStatus USBD_DriverEndpointTransfer (uint8_t device, uint8_t ep_addr, uint8_t uint8_t ep_idx, sem_idx; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return usbInvalidParameter; + } + if ((ep_addr & 0x7FU) >= usbd_ep_max_num) { + return usbInvalidParameter; + } + ep_idx = (ep_addr & 0x0FU) + ((ep_addr & 0x80U) >> 3); ep_msk = 1UL << ep_idx; - sem_idx = (uint8_t)(device * usbd_ep_max_num * 2U) + (ep_addr & 0x0FU); + sem_idx = (device * usbd_ep_max_num * 2U) + (ep_addr & 0x0FU); if (ep_idx >= 16U) { sem_idx += usbd_ep_max_num; } @@ -631,9 +732,17 @@ uint32_t USBD_DriverEndpointTransferGetResult (uint8_t device, uint8_t ep_addr) uint8_t ep_idx, sem_idx; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return 0U; + } + if ((ep_addr & 0x7FU) >= usbd_ep_max_num) { + return 0U; + } + ep_idx = (ep_addr & 0x0FU) + ((ep_addr & 0x80U) >> 3); - sem_idx = (uint8_t)(device * usbd_ep_max_num * 2U) + (ep_addr & 0x0FU); + sem_idx = (device * usbd_ep_max_num * 2U) + (ep_addr & 0x0FU); if (ep_idx >= 16U) { sem_idx += usbd_ep_max_num; } @@ -668,9 +777,17 @@ usbStatus USBD_DriverEndpointTransferAbort (uint8_t device, uint8_t ep_addr) { uint8_t ep_idx, sem_idx; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return usbInvalidParameter; + } + if ((ep_addr & 0x7FU) >= usbd_ep_max_num) { + return usbInvalidParameter; + } + ep_idx = (ep_addr & 0x0FU) + ((ep_addr & 0x80U) >> 3); - sem_idx = (uint8_t)(device * usbd_ep_max_num * 2U) + (ep_addr & 0x0FU); + sem_idx = (device * usbd_ep_max_num * 2U) + (ep_addr & 0x0FU); if (ep_idx >= 16U) { sem_idx += usbd_ep_max_num; } @@ -720,6 +837,11 @@ uint16_t USBD_DriverGetFrameNumber (uint8_t device) { uint16_t val; uint8_t retry; + // Check parameters + if (device >= usbd_dev_num) { + return 0U; + } + retry = USBD_DRIVER_RETRY_COUNT; do { if (USBD_SemaphoreAcquire (usbd_driver_semaphore_id[device], 0U) == 0) { break; } @@ -744,6 +866,12 @@ uint16_t USBD_DriverGetFrameNumber (uint8_t device) { /// \param[in] event \ref USBD_dev_events void USBD_SignalDeviceEvent(uint8_t device, uint32_t event) { EvrUSBD_Driver_OnSignalDeviceEvent(device, event); + + // Check parameters + if (device >= usbd_dev_num) { + return; + } + (void)USBD_ThreadFlagsSet (usbd_core_thread_id[device], event); } @@ -757,11 +885,21 @@ void USBD_SignalEndpointEvent(uint8_t device, uint8_t ep_addr, uint32_t event) { uint8_t ep_num; uint8_t ep_idx; + EvrUSBD_Driver_OnSignalEndpointEvent(device, ep_addr, event); + + // Check parameters + if (device >= usbd_dev_num) { + return; + } + if ((ep_addr & 0x7FU) >= usbd_ep_max_num) { + return; + } + ep_num = (ep_addr & 0x0FU); ep_idx = ep_num + ((ep_addr & 0x80U) >> 3); - EvrUSBD_Driver_OnSignalEndpointEvent(device, ep_addr, event); - usbd_dev_ptr[device]->data_ptr->endpoint_active[ep_idx] = 0U; - (void)USBD_ThreadFlagsSet (*usbd_ep_thread_id_ptr[(device*usbd_ep_max_num)+ep_num], ((((uint32_t)(ep_num)) << 12) | (event << 8))); + if (usbd_ep_thread_id_ptr[(device*usbd_ep_max_num)+ep_num] != NULL) { + (void)USBD_ThreadFlagsSet (*usbd_ep_thread_id_ptr[(device*usbd_ep_max_num)+ep_num], ((((uint32_t)(ep_num)) << 12) | (event << 8))); + } } diff --git a/Components/USB/Source/usbd_lib_hid.c b/Components/USB/Source/usbd_lib_hid.c index 93e5b829..ec8b431e 100644 --- a/Components/USB/Source/usbd_lib_hid.c +++ b/Components/USB/Source/usbd_lib_hid.c @@ -46,6 +46,12 @@ usbStatus USBD_HID_GetReportTrigger (uint8_t instance, uint8_t rid, const uint8_ EvrUSBD_HID_GetReportTrigger(instance, rid, buf, len); + // Check parameters + if (buf == NULL) { + status = usbInvalidParameter; + goto exit; + } + status = USBD_HID_CheckInstance (instance); if (status != usbOK) { goto exit; @@ -55,6 +61,11 @@ usbStatus USBD_HID_GetReportTrigger (uint8_t instance, uint8_t rid, const uint8_ ptr_hid_data = ptr_hid_cfg->data_ptr; ptr_dev_data = usbd_dev_ptr[ptr_hid_cfg->dev_num]->data_ptr; + if (((ptr_hid_cfg->in_report_num > 1U) && (rid == 0U)) || + (rid > ptr_hid_cfg->in_report_num)) { + status = usbInvalidParameter; + goto exit; + } if (len > ptr_hid_cfg->in_report_max_sz) { status = usbInvalidParameter; goto exit; @@ -81,7 +92,10 @@ usbStatus USBD_HID_GetReportTrigger (uint8_t instance, uint8_t rid, const uint8_ } else { // If more IN reports, send ReportID ptr_hid_data->data_out_to_send_len++; } - ptr_hid_cfg->idle_count[rid] = 0U; + if (rid != 0U) { + rid = rid - 1U; + } + ptr_hid_cfg->idle_count[rid] = 0U; USBD_HID_EpIntIn (instance); exit: @@ -102,17 +116,23 @@ usbStatus USBD_HID_GetReportTrigger (uint8_t instance, uint8_t rid, const uint8_ /// \return true success /// \return false fail, not supported request bool USBD_HID_CoreGetReport (uint8_t instance) { - const usbd_data_t *ptr_dev_data; + const usbd_data_t *ptr_dev_data = NULL; uint8_t *ptr_buf; int32_t len; EvrUSBD_HID_GetReport(instance); - ptr_dev_data = usbd_dev_ptr[usbd_hid_ptr[instance]->dev_num]->data_ptr; - if (USBD_HID_CheckInstance(instance) != usbOK) { goto failed; } + if (usbd_hid_ptr[instance]->dev_num >= usbd_dev_num) { + goto failed; + } + + ptr_dev_data = usbd_dev_ptr[usbd_hid_ptr[instance]->dev_num]->data_ptr; + if (ptr_dev_data == NULL) { + goto failed; + } // Report Type = setup_packet.wValue high byte // Report ID = setup_packet.wValue low byte @@ -139,7 +159,9 @@ bool USBD_HID_CoreGetReport (uint8_t instance) { } failed: - EvrUSBD_HID_OnGetReportFailed(instance, (uint8_t)(ptr_dev_data->setup_packet.wValue >> 8), (uint8_t)USBD_HID_REQ_EP_CTRL, (uint8_t)(ptr_dev_data->setup_packet.wValue)); + if (ptr_dev_data != NULL) { + EvrUSBD_HID_OnGetReportFailed(instance, (uint8_t)(ptr_dev_data->setup_packet.wValue >> 8), (uint8_t)USBD_HID_REQ_EP_CTRL, (uint8_t)(ptr_dev_data->setup_packet.wValue)); + } EvrUSBD_HID_GetReportFailed(instance); return false; } @@ -149,18 +171,23 @@ bool USBD_HID_CoreGetReport (uint8_t instance) { /// \return true success /// \return false fail, not supported request bool USBD_HID_CoreSetReport (uint8_t instance) { - const usbd_data_t *ptr_dev_data; + const usbd_data_t *ptr_dev_data = NULL; const uint8_t *ptr_buf; - bool result; + bool result = false; EvrUSBD_HID_SetReport(instance); - ptr_dev_data = usbd_dev_ptr[usbd_hid_ptr[instance]->dev_num]->data_ptr; - - result = false; if (USBD_HID_CheckInstance(instance) != usbOK) { goto failed; } + if (usbd_hid_ptr[instance]->dev_num >= usbd_dev_num) { + goto failed; + } + + ptr_dev_data = usbd_dev_ptr[usbd_hid_ptr[instance]->dev_num]->data_ptr; + if (ptr_dev_data == NULL) { + goto failed; + } // Report Type = setup_packet.wValue high byte // Report ID = setup_packet.wValue low byte @@ -187,7 +214,9 @@ bool USBD_HID_CoreSetReport (uint8_t instance) { } failed: - EvrUSBD_HID_OnSetReportFailed(instance, (uint8_t)(ptr_dev_data->setup_packet.wValue >> 8), (uint8_t)USBD_HID_REQ_EP_CTRL, (uint8_t)(ptr_dev_data->setup_packet.wValue), (int32_t)(ptr_dev_data->setup_packet.wLength)); + if (ptr_dev_data != NULL) { + EvrUSBD_HID_OnSetReportFailed(instance, (uint8_t)(ptr_dev_data->setup_packet.wValue >> 8), (uint8_t)USBD_HID_REQ_EP_CTRL, (uint8_t)(ptr_dev_data->setup_packet.wValue), (int32_t)(ptr_dev_data->setup_packet.wLength)); + } EvrUSBD_HID_SetReportFailed(instance); return result; } @@ -210,8 +239,18 @@ bool USBD_HID_CoreGetIdle (uint8_t instance) { ptr_dev_cfg = usbd_dev_ptr[ptr_hid_cfg->dev_num]; ptr_dev_data = ptr_dev_cfg->data_ptr; - EvrUSBD_HID_GetIdle(instance, (uint8_t)ptr_dev_data->setup_packet.wValue & 0xFFU, ptr_hid_cfg->idle_set[ptr_dev_data->setup_packet.wValue & 0xFFU]); - ptr_dev_cfg->ep0_buf[0] = ptr_hid_cfg->idle_set[ptr_dev_data->setup_packet.wValue & 0xFFU]; + if ((ptr_dev_data->setup_packet.wValue & 0xFFU) != 0U) { // If > 0 Report ID specified + if (((ptr_dev_data->setup_packet.wValue & 0xFFU)-1U) < usbd_hid_ptr[instance]->in_report_num) { + EvrUSBD_HID_GetIdle(instance, (uint8_t)ptr_dev_data->setup_packet.wValue & 0xFFU, ptr_hid_cfg->idle_set[(ptr_dev_data->setup_packet.wValue & 0xFFU)-1U]); + ptr_dev_cfg->ep0_buf[0] = ptr_hid_cfg->idle_set[(ptr_dev_data->setup_packet.wValue & 0xFFU)-1U]; + } else { + EvrUSBD_HID_GetIdleFailed(instance); + return false; + } + } else { // If == 0 all reports + EvrUSBD_HID_GetIdle(instance, 0U, ptr_hid_cfg->idle_set[0]); + ptr_dev_cfg->ep0_buf[0] = ptr_hid_cfg->idle_set[0]; + } return true; } @@ -233,7 +272,12 @@ bool USBD_HID_CoreSetIdle (uint8_t instance) { EvrUSBD_HID_SetIdle(instance, (uint8_t)ptr_dev_data->setup_packet.wValue & 0xFFU, (uint8_t)(ptr_dev_data->setup_packet.wValue >> 8)); if ((ptr_dev_data->setup_packet.wValue & 0xFFU) != 0U) { // If > 0 Report ID specified - usbd_hid_ptr[instance]->idle_set[(ptr_dev_data->setup_packet.wValue & 0xFFU)-1U] = (uint8_t)(ptr_dev_data->setup_packet.wValue >> 8); + if (((ptr_dev_data->setup_packet.wValue & 0xFFU)-1U) < usbd_hid_ptr[instance]->in_report_num) { + usbd_hid_ptr[instance]->idle_set[(ptr_dev_data->setup_packet.wValue & 0xFFU)-1U] = (uint8_t)(ptr_dev_data->setup_packet.wValue >> 8); + } else { + EvrUSBD_HID_SetIdleFailed(instance); + return false; + } } else { // If == 0 all reports for (i = 0U; i < usbd_hid_ptr[instance]->in_report_num; i++) { usbd_hid_ptr[instance]->idle_set[i] = (uint8_t)(ptr_dev_data->setup_packet.wValue >> 8); @@ -366,12 +410,16 @@ usbStatus USBD_HID_Uninitialize (uint8_t instance) { goto exit; } - if (usbd_hid_semaphore_id[instance] != NULL) { - if (USBD_SemaphoreDelete (usbd_hid_semaphore_id[instance]) != 0) { - status = usbSemaphoreError; + if (usbd_hid_timer_id[instance] != NULL) { + if (USBD_TimerStop (usbd_hid_timer_id[instance]) != 0) { + status = usbTimerError; goto exit; } - usbd_hid_semaphore_id[instance] = NULL; + if (USBD_TimerDelete (usbd_hid_timer_id[instance]) != 0) { + status = usbTimerError; + goto exit; + } + usbd_hid_timer_id[instance] = NULL; } if (usbd_hid_thread_id[instance] != NULL) { if (USBD_ThreadTerminate (usbd_hid_thread_id[instance]) != 0) { @@ -380,6 +428,13 @@ usbStatus USBD_HID_Uninitialize (uint8_t instance) { } usbd_hid_thread_id[instance] = NULL; } + if (usbd_hid_semaphore_id[instance] != NULL) { + if (USBD_SemaphoreDelete (usbd_hid_semaphore_id[instance]) != 0) { + status = usbSemaphoreError; + goto exit; + } + usbd_hid_semaphore_id[instance] = NULL; + } if (fpUSBD_HID_Uninitialize [instance] != NULL) { EvrUSBD_HID_OnUninitialize(instance); @@ -475,7 +530,7 @@ void USBD_HID_EndpointStart (uint8_t instance, uint8_t ep_addr) { /// \details The timer interval is 4 ms, as this is the resolution of the idle duration defined by the HID specification /// (HID1_11.pdf, 7.2.4 Set_Idle Request) /// \param[in] instance instance of HID class. -void USBD_HID_Timer (void const *argument) { +void USBD_HID_Timer (void *argument) { const usbd_data_t *ptr_dev_data; const usbd_hid_t *ptr_hid_cfg; usbd_hid_data_t *ptr_hid_data; @@ -735,7 +790,7 @@ void USBD_HID_Thread (void *arg) { for (;;) { event = USBD_ThreadFlagsWait (0xFFFFFFFFU); - if ((event & 0x8000000U) == 0U) { + if ((event & 0x80000000U) == 0U) { if (((event >> 8) & ARM_USBD_EVENT_OUT) != 0U) { USBD_HID_EpIntOut (instance); } diff --git a/Components/USB/Source/usbd_lib_hid.h b/Components/USB/Source/usbd_lib_hid.h index 932225c2..763eb928 100644 --- a/Components/USB/Source/usbd_lib_hid.h +++ b/Components/USB/Source/usbd_lib_hid.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_hid.h * Purpose: USB Device - Human Interface Device (HID) module header file @@ -48,4 +48,7 @@ extern bool USBD_HID_CoreGetProtocol (uint8_t instance); /// \return false fail, not supported request extern bool USBD_HID_CoreSetProtocol (uint8_t instance); +extern void USBD_HID_Timer (void *argument); +extern void USBD_HID_Thread (void *arg); + #endif // USBD_LIB_HID_H_ diff --git a/Components/USB/Source/usbd_lib_msc.c b/Components/USB/Source/usbd_lib_msc.c index 212f013c..c53a60d1 100644 --- a/Components/USB/Source/usbd_lib_msc.c +++ b/Components/USB/Source/usbd_lib_msc.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2025 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_msc.c * Purpose: USB Device - Mass Storage device Class (MSC) module @@ -1693,7 +1693,7 @@ void USBD_MSC_Thread (void *arg) { for (;;) { event = USBD_ThreadFlagsWait (0xFFFFFFFFU); - if ((event & 0x8000000U) == 0U) { + if ((event & 0x80000000U) == 0U) { if (((event >> 8) & ARM_USBD_EVENT_OUT) != 0U) { ptr_msc_data->bulk_len = USBD_DriverEndpointTransferGetResult (device, ep_bulk_out); USBD_MSC_EpBulkOut(instance); diff --git a/Components/USB/Source/usbd_lib_msc.h b/Components/USB/Source/usbd_lib_msc.h index 18fbd34c..6b3e3698 100644 --- a/Components/USB/Source/usbd_lib_msc.h +++ b/Components/USB/Source/usbd_lib_msc.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Device - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbd_lib_msc.h * Purpose: USB Device - Mass Storage device Class (MSC) module header file @@ -29,4 +29,6 @@ extern bool USBD_MSC_GetMaxLUN (uint8_t instance); /// \param[in] ep_addr endpoint address. extern void USBD_MSC_EndpointClearFeatureStall (uint8_t instance, uint8_t ep_addr); +extern void USBD_MSC_Thread (void *arg); + #endif // USBD_LIB_MSC_H_ diff --git a/Components/USB/Source/usbh_cmsis_rtos2.c b/Components/USB/Source/usbh_cmsis_rtos2.c index 121083de..0640d879 100644 --- a/Components/USB/Source/usbh_cmsis_rtos2.c +++ b/Components/USB/Source/usbh_cmsis_rtos2.c @@ -10,12 +10,11 @@ #include -// Resources definition +#include "usbh_lib_core.h" +#include "usbh_lib_cdc.h" +#include "usbh_lib_hid.h" -// Import thread function prototypes -extern void USBH_Core_Thread (void *arg); -extern void USBH_CDC_IntIn_Thread (void *arg); -extern void USBH_HID_IntIn_Thread (void *arg); +// Resources definition // Import thread definitions generated by each Host Controller instance // in usbh_config_hc_n.c file (n = 0 .. 3) @@ -102,35 +101,12 @@ static osRtxTimer_t usbh_debounce_timer_cb_mem[USBH_HC_NUM] __attribute__((sec #ifdef RTE_CMSIS_RTOS2_RTX5 static osRtxMutex_t usbh_def_pipe_mutex_cb_mem[USBH_HC_NUM] __attribute__((section(".bss.os.mutex.cb"))); #endif -static osMutexAttr_t usbh_def_pipe_mutex_attr = { - NULL, -#ifdef RTE_CMSIS_RTOS2_RTX5 - osMutexPrioInherit | osMutexRecursive | osMutexRobust, - &usbh_def_pipe_mutex_cb_mem, - sizeof(osRtxMutex_t) -#else - osMutexPrioInherit | osMutexRecursive, - NULL, - 0U -#endif -}; // Create semaphore definitions #ifdef RTE_CMSIS_RTOS2_RTX5 static osRtxSemaphore_t usbh_driver_semaphore_cb_mem[USBH_HC_NUM] __attribute__((section(".bss.os.semaphore.cb"))); #endif -static osSemaphoreAttr_t usbh_driver_semaphore_attr = { - NULL, - 0U, -#ifdef RTE_CMSIS_RTOS2_RTX5 - &usbh_driver_semaphore_cb_mem, - sizeof(osRtxSemaphore_t) -#else - NULL, - 0U -#endif -}; // Functions definition @@ -212,7 +188,7 @@ int32_t USBH_ThreadTerminate (void *thread_hndl) { /// \return ticks static uint32_t USBH_MsToTick (uint32_t ms) { static uint32_t tick_freq_mul = 0U; - static uint32_t max_ms; + static uint32_t max_ms = 0xFFFFFFFFU >> 10; if (tick_freq_mul == 0U) { // Initialize helper variables once @@ -330,14 +306,30 @@ uint32_t USBH_ThreadFlagsWait (uint32_t millisec) { /// value != 0: handle to created mutex /// value = 0: mutex creation failed void *USBH_MutexCreate (usbhMutex_t mutex, uint8_t ctrl) { - if (mutex == usbhMutexCore) { - if (ctrl >= USBH_HC_NUM) { return NULL; } + if (ctrl >= USBH_HC_NUM) { + return NULL; + } + if (mutex != usbhMutexCore) { + return NULL; + } + + osMutexAttr_t mutex_attr = { #ifdef RTE_CMSIS_RTOS2_RTX5 - usbh_def_pipe_mutex_attr.cb_mem = &usbh_def_pipe_mutex_cb_mem[ctrl]; + .attr_bits = osMutexPrioInherit | osMutexRecursive | osMutexRobust, + .cb_mem = NULL, + .cb_size = sizeof(osRtxMutex_t) +#else + .attr_bits = osMutexPrioInherit | osMutexRecursive, + .cb_mem = NULL, + .cb_size = 0U #endif - return ((void *)osMutexNew (&usbh_def_pipe_mutex_attr)); - } - return NULL; + }; + +#ifdef RTE_CMSIS_RTOS2_RTX5 + mutex_attr.cb_mem = &usbh_def_pipe_mutex_cb_mem[ctrl]; +#endif + + return ((void *)osMutexNew(&mutex_attr)); } /// \brief Acquire a mutex or timeout if it is locked @@ -378,14 +370,30 @@ int32_t USBH_MutexDelete (void *mutex_hndl) { /// value != 0: handle to created semaphore /// value = 0: semaphore creation failed void *USBH_SemaphoreCreate (usbhSemaphore_t semaphore, uint8_t ctrl) { - if (semaphore == usbhSemaphoreCore) { - if (ctrl >= USBH_HC_NUM) { return NULL; } + if (ctrl >= USBH_HC_NUM) { + return NULL; + } + if (semaphore != usbhSemaphoreCore) { + return NULL; + } + + osSemaphoreAttr_t semaphore_attr = { + .name = NULL, + .attr_bits = 0U, #ifdef RTE_CMSIS_RTOS2_RTX5 - usbh_driver_semaphore_attr.cb_mem = &usbh_driver_semaphore_cb_mem[ctrl]; + .cb_mem = NULL, + .cb_size = sizeof(osRtxSemaphore_t) +#else + .cb_mem = NULL, + .cb_size = 0U #endif - return ((void *)osSemaphoreNew (1U, 1U, &usbh_driver_semaphore_attr)); - } - return NULL; + }; + +#ifdef RTE_CMSIS_RTOS2_RTX5 + semaphore_attr.cb_mem = &usbh_driver_semaphore_cb_mem[ctrl]; +#endif + + return ((void *)osSemaphoreNew (1U, 1U, &semaphore_attr)); } /// \brief Wait for a semaphore token to become available and acquire it diff --git a/Components/USB/Source/usbh_evr.h b/Components/USB/Source/usbh_evr.h index 977a9276..a149991a 100644 --- a/Components/USB/Source/usbh_evr.h +++ b/Components/USB/Source/usbh_evr.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Host - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbh_evr.h * Purpose: USB Host (USBH) - Event Recorder definitions @@ -9,6 +9,8 @@ #ifndef USBH_EVR_H_ #define USBH_EVR_H_ +#include + #include "RTE_Components.h" #include "rl_usb.h" @@ -785,7 +787,7 @@ /** - \brief Event on \ref USBH_PipeCreate start (API) + \brief Event on \ref USBH_PipeCreate (API) \param device index of USB Device. \param ep_addr endpoint address - ep_addr.0..3: address @@ -1053,8 +1055,14 @@ \param len number of data bytes in data stage */ #ifdef EvtUSBH_Core_ControlTransfer - __STATIC_INLINE void EvrUSBH_Core_ControlTransfer( uint8_t device, const void * setup_packet, uint32_t len) { - EventRecord4( EvtUSBH_Core_ControlTransfer, TO_BYTE0(device), *((const uint32_t *)setup_packet), *((const uint32_t *)setup_packet+1U), len); + __STATIC_INLINE void EvrUSBH_Core_ControlTransfer( uint8_t device, const USB_SETUP_PACKET *setup_packet, uint32_t len) { + uint32_t word0; + uint32_t word1; + if (setup_packet != NULL) { + memcpy(&word0, setup_packet, 4U); + memcpy(&word1, ((const uint8_t *)setup_packet) + 4U, 4U); + EventRecord4( EvtUSBH_Core_ControlTransfer, TO_BYTE0(device), word0, word1, len); + } } #else #define EvrUSBH_Core_ControlTransfer(...) @@ -1068,8 +1076,14 @@ \param error error code \ref usbStatus */ #ifdef EvtUSBH_Core_ControlTransferFailed - __STATIC_INLINE void EvrUSBH_Core_ControlTransferFailed( uint8_t device, const void * setup_packet, usbStatus error) { - EventRecord4( EvtUSBH_Core_ControlTransferFailed, TO_BYTE0(device), *((const uint32_t *)setup_packet), *((const uint32_t *)setup_packet+1U), (uint32_t)error); + __STATIC_INLINE void EvrUSBH_Core_ControlTransferFailed( uint8_t device, const USB_SETUP_PACKET *setup_packet, usbStatus error) { + uint32_t word0; + uint32_t word1; + if (setup_packet != NULL) { + memcpy(&word0, setup_packet, 4U); + memcpy(&word1, ((const uint8_t *)setup_packet) + 4U, 4U); + EventRecord4( EvtUSBH_Core_ControlTransferFailed, TO_BYTE0(device), word0, word1, (uint32_t)error); + } } #else #define EvrUSBH_Core_ControlTransferFailed(...) @@ -1278,7 +1292,7 @@ /** - \brief Event on \ref USBH_DeviceRequest_GetConfiguration start (API) + \brief Event on \ref USBH_DeviceRequest_GetConfiguration (API) \param device device index \param config configuration */ @@ -1335,7 +1349,7 @@ /** - \brief Event on \ref USBH_DeviceRequest_GetInterface start (API) + \brief Event on \ref USBH_DeviceRequest_GetInterface (API) \param device device index \param index interface index \param alt alternate setting @@ -2096,7 +2110,7 @@ \param status device status \ref usbStatus */ #ifdef EvtUSBH_CC_GetStatus - __STATIC_INLINE void EvrUSBH_CC_GetStatus( uint8_t instance, uint8_t status) { + __STATIC_INLINE void EvrUSBH_CC_GetStatus( uint8_t instance, usbStatus status) { EventRecord2( EvtUSBH_CC_GetStatus, TO_BYTE0(instance), (uint32_t)status); } #else @@ -2322,7 +2336,7 @@ \param status device status \ref usbStatus */ #ifdef EvtUSBH_CDC_ACM_GetStatus - __STATIC_INLINE void EvrUSBH_CDC_ACM_GetStatus( uint8_t instance, uint8_t status) { + __STATIC_INLINE void EvrUSBH_CDC_ACM_GetStatus( uint8_t instance, usbStatus status) { EventRecord2( EvtUSBH_CDC_ACM_GetStatus, TO_BYTE0(instance), (uint32_t)status); } #else @@ -2823,7 +2837,7 @@ \param status device status \ref usbStatus */ #ifdef EvtUSBH_HID_GetStatus - __STATIC_INLINE void EvrUSBH_HID_GetStatus( uint8_t instance, uint8_t status) { + __STATIC_INLINE void EvrUSBH_HID_GetStatus( uint8_t instance, usbStatus status) { EventRecord2( EvtUSBH_HID_GetStatus, TO_BYTE0(instance), (uint32_t)status); } #else @@ -3360,7 +3374,7 @@ \param status device status \ref usbStatus */ #ifdef EvtUSBH_MSC_GetStatus - __STATIC_INLINE void EvrUSBH_MSC_GetStatus( uint8_t instance, uint8_t status) { + __STATIC_INLINE void EvrUSBH_MSC_GetStatus( uint8_t instance, usbStatus status) { EventRecord2( EvtUSBH_MSC_GetStatus, TO_BYTE0(instance), (uint32_t)status); } #else diff --git a/Components/USB/Source/usbh_lib.h b/Components/USB/Source/usbh_lib.h index 988f9361..5ce8d03a 100644 --- a/Components/USB/Source/usbh_lib.h +++ b/Components/USB/Source/usbh_lib.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Host - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbh_lib.h * Purpose: USB Host header file @@ -237,6 +237,7 @@ typedef struct { USBH_PIPE_HANDLE bulk_out_pipe_hndl; ///< bulk out pipe handle USBH_PIPE_HANDLE int_in_pipe_hndl; ///< interrupt in pipe handle void *int_in_thread_id; ///< interrupt in pipe thread ID + uint8_t int_in_data_buf[64]; ///< interrupt in data buffer } USBH_CDC; // ==== USB Host Functions for Memory Management ==== diff --git a/Components/USB/Source/usbh_lib_cdc.c b/Components/USB/Source/usbh_lib_cdc.c index 3df04945..90815552 100644 --- a/Components/USB/Source/usbh_lib_cdc.c +++ b/Components/USB/Source/usbh_lib_cdc.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Host - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). * All rights reserved. *------------------------------------------------------------------------------ * Name: usbh_lib_cdc.c @@ -47,7 +47,7 @@ uint8_t USBH_CDC_ACM_GetDevice (uint8_t instance) { if (usbh_cdc[instance].ptr_dev == usbh_dev) { return 0U; } else { - device = (uint8_t)(((uint32_t)usbh_cdc[instance].ptr_dev - (uint32_t)usbh_dev) / sizeof (USBH_DEV)); + device = (uint8_t)(((uintptr_t)usbh_cdc[instance].ptr_dev - (uintptr_t)usbh_dev) / sizeof (USBH_DEV)); } } else { device = 0xFFU; @@ -214,7 +214,9 @@ usbStatus USBH_CDC_ACM_SetLineCoding (uint8_t instance, const CDC_LINE_CODING *l uint8_t device; usbStatus status; - EvrUSBH_CDC_ACM_SetLineCoding(instance, line_coding->dwDTERate); + if (line_coding != NULL) { + EvrUSBH_CDC_ACM_SetLineCoding(instance, line_coding->dwDTERate); + } status = CheckInstance (instance); if (status == usbOK) { @@ -222,7 +224,7 @@ usbStatus USBH_CDC_ACM_SetLineCoding (uint8_t instance, const CDC_LINE_CODING *l device = USBH_CDC_ACM_GetDevice (instance); if (device != 0xFFU) { PREPARE_SETUP_PACKET_DATA(((USB_SETUP_PACKET *)&setup_packet), USB_REQUEST_HOST_TO_DEVICE, USB_REQUEST_CLASS, USB_REQUEST_TO_INTERFACE, CDC_SET_LINE_CODING, 0U, 0U, sizeof(CDC_LINE_CODING)) - status = USBH_ControlTransfer (device, &setup_packet, (uint8_t *)((uint32_t)line_coding), sizeof(CDC_LINE_CODING)); + status = USBH_ControlTransfer (device, &setup_packet, (uint8_t *)(uintptr_t)line_coding, sizeof(CDC_LINE_CODING)); } else { status = usbDeviceError; } @@ -233,7 +235,9 @@ usbStatus USBH_CDC_ACM_SetLineCoding (uint8_t instance, const CDC_LINE_CODING *l #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { - EvrUSBH_CDC_ACM_SetLineCodingFailed(instance, line_coding->dwDTERate, status); + if (line_coding != NULL) { + EvrUSBH_CDC_ACM_SetLineCodingFailed(instance, line_coding->dwDTERate, status); + } } #endif return status; @@ -264,10 +268,12 @@ usbStatus USBH_CDC_ACM_GetLineCoding (uint8_t instance, CDC_LINE_CODING *line_co } #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) - if (status != usbOK) { - EvrUSBH_CDC_ACM_SetLineCodingFailed(instance, line_coding->dwDTERate, status); - } else { - EvrUSBH_CDC_ACM_SetLineCoding(instance, line_coding->dwDTERate); + if (line_coding != NULL) { + if (status != usbOK) { + EvrUSBH_CDC_ACM_SetLineCodingFailed(instance, line_coding->dwDTERate, status); + } else { + EvrUSBH_CDC_ACM_SetLineCoding(instance, line_coding->dwDTERate); + } } #endif return status; @@ -368,22 +374,21 @@ usbStatus USBH_CDC_ACM_SendBreak (uint8_t instance, uint16_t duration) { /// - value 255 : configuration failed __WEAK uint8_t USBH_CDC_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR *ptr_dev_desc, const USB_CONFIGURATION_DESCRIPTOR *ptr_cfg_desc) { USBH_DEV *ptr_dev; - USBH_CDC *ptr_cdc; + USBH_CDC *ptr_cdc = NULL; const USB_COMMON_DESCRIPTOR *ptr_cmn_desc; const USB_INTERFACE_DESCRIPTOR *ptr_if_desc; const USB_INTERFACE_ASSOCIATION_DESCRIPTOR *ptr_if_assoc_desc; const USB_ENDPOINT_DESCRIPTOR *ptr_ep_desc; const uint8_t *ptr_desc; USBH_PIPE_HANDLE pipe_hndl; + uint32_t cgf_desc_len; uint8_t num; uint8_t ret, idx, if_cnt; - usbStatus status; - status = usbOK; ret = 255U; if_cnt = 0U; - if (device == 255U) { + if (device >= usbh_dev_num) { goto exit; } if (ptr_dev_desc == NULL) { @@ -412,9 +417,19 @@ __WEAK uint8_t USBH_CDC_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR * ptr_cdc->ptr_dev = (USBH_DEV *)ptr_dev; - ptr_desc = (const uint8_t *)ptr_cfg_desc; + ptr_desc = (const uint8_t *)ptr_cfg_desc; + if (ptr_cfg_desc->bLength != sizeof(USB_CONFIGURATION_DESCRIPTOR)) { + goto exit; + } + + cgf_desc_len = (uint32_t)ptr_cfg_desc->wTotalLength; ptr_desc += ptr_cfg_desc->bLength; ptr_cmn_desc = (const USB_COMMON_DESCRIPTOR *)ptr_desc; + if ((ptr_cmn_desc->bLength == 0U) || (ptr_cmn_desc->bLength >= cgf_desc_len)) { + goto exit; + } + + // Skip Interface Association Descriptor if it exists if (ptr_cmn_desc->bDescriptorType == USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE) { // If this is Interface Association Descriptor ptr_if_assoc_desc = (const USB_INTERFACE_ASSOCIATION_DESCRIPTOR *)ptr_desc; @@ -423,6 +438,9 @@ __WEAK uint8_t USBH_CDC_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR * goto exit; // Not CDC device } ptr_desc = (const uint8_t *)ptr_if_assoc_desc; + if (ptr_if_assoc_desc->bLength != sizeof(USB_INTERFACE_ASSOCIATION_DESCRIPTOR)) { + goto exit; + } ptr_desc += ptr_if_assoc_desc->bLength; ptr_cmn_desc = (const USB_COMMON_DESCRIPTOR *)ptr_desc; } @@ -430,44 +448,30 @@ __WEAK uint8_t USBH_CDC_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR * goto exit; // Not CDC device } ptr_if_desc = (const USB_INTERFACE_DESCRIPTOR *)ptr_desc; - while ((uint32_t)ptr_if_desc < ((uint32_t)ptr_cfg_desc + ptr_cfg_desc->wTotalLength) && (if_cnt < 2U)) { + while ((uintptr_t)ptr_if_desc < ((uintptr_t)ptr_cfg_desc + cgf_desc_len) && (if_cnt < 2U)) { num = ptr_if_desc->bNumEndpoints; // Number of endpoints switch (ptr_if_desc->bInterfaceClass) { case CDC_COMMUNICATION_INTERFACE_CLASS: case CDC_DATA_INTERFACE_CLASS: if_cnt++; // Skip all descriptors until Endpoint Descriptor - ptr_desc = (const uint8_t *)ptr_if_desc; - ptr_cmn_desc = (const USB_COMMON_DESCRIPTOR *)ptr_desc; + ptr_desc = (const uint8_t *)ptr_if_desc; + ptr_cmn_desc = (const USB_COMMON_DESCRIPTOR *)ptr_desc; while (ptr_cmn_desc->bDescriptorType != USB_ENDPOINT_DESCRIPTOR_TYPE) { - ptr_desc += ptr_cmn_desc->bLength; - ptr_cmn_desc = (const USB_COMMON_DESCRIPTOR *)ptr_desc; + if ((ptr_cmn_desc->bLength == 0U) || + (((uintptr_t)ptr_desc + ptr_cmn_desc->bLength) > ((uintptr_t)ptr_cfg_desc + cgf_desc_len))) { + goto exit; + } + ptr_desc += ptr_cmn_desc->bLength; + ptr_cmn_desc = (const USB_COMMON_DESCRIPTOR *)ptr_desc; } + // Create Pipes ptr_ep_desc = (const USB_ENDPOINT_DESCRIPTOR *)ptr_desc; while (num-- != 0U) { - pipe_hndl = USBH_PipeCreate (device, ptr_ep_desc->bEndpointAddress, (uint8_t)((uint32_t)ptr_ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK), ptr_ep_desc->wMaxPacketSize & 0x7FFU, ptr_ep_desc->bInterval); + pipe_hndl = USBH_PipeCreate (device, ptr_ep_desc->bEndpointAddress, (uint8_t)(ptr_ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK), ptr_ep_desc->wMaxPacketSize & 0x7FFU, ptr_ep_desc->bInterval); if (pipe_hndl == 0U) { // If creation of pipe has failed delete previously created pipes - if (ptr_cdc->bulk_in_pipe_hndl != 0U) { - status = USBH_PipeDelete (ptr_cdc->bulk_in_pipe_hndl); - if (status != usbOK) { - goto exit; - } - } - if (ptr_cdc->bulk_out_pipe_hndl != 0U) { - status = USBH_PipeDelete (ptr_cdc->bulk_out_pipe_hndl); - if (status != usbOK) { - goto exit; - } - } - if (ptr_cdc->int_in_pipe_hndl != 0U) { - status = USBH_PipeDelete (ptr_cdc->int_in_pipe_hndl); - if (status != usbOK) { - goto exit; - } - } - memset ((void *)ptr_cdc, 0, sizeof (USBH_CDC)); goto exit; } switch (ptr_ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK) { @@ -496,10 +500,16 @@ __WEAK uint8_t USBH_CDC_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR * // Skip all descriptors in current interface as it is not a CDC interface ptr_desc = (const uint8_t *)ptr_if_desc; ptr_cmn_desc = (const USB_COMMON_DESCRIPTOR *)ptr_desc; + if ((ptr_cmn_desc->bLength == 0U) || + (((uintptr_t)ptr_desc + ptr_cmn_desc->bLength) > ((uintptr_t)ptr_cfg_desc + cgf_desc_len))) { + goto exit; + } ptr_desc += ptr_cmn_desc->bLength; // Step over current interface descriptor ptr_cmn_desc = (const USB_COMMON_DESCRIPTOR *)ptr_desc; - while ((ptr_cmn_desc->bDescriptorType != USB_INTERFACE_DESCRIPTOR_TYPE) && - ((uint32_t)ptr_cmn_desc < ((uint32_t)ptr_cfg_desc + ptr_cfg_desc->wTotalLength))) { + while (ptr_cmn_desc->bDescriptorType != USB_INTERFACE_DESCRIPTOR_TYPE) { + if ((ptr_cmn_desc->bLength == 0U) || (((uintptr_t)ptr_cmn_desc + ptr_cmn_desc->bLength) >= ((uintptr_t)ptr_cfg_desc + cgf_desc_len))) { + goto exit; + } ptr_desc += ptr_cmn_desc->bLength; ptr_cmn_desc = (const USB_COMMON_DESCRIPTOR *)ptr_desc; } @@ -507,7 +517,7 @@ __WEAK uint8_t USBH_CDC_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR * break; } } - + if ((ptr_cdc->bulk_in_pipe_hndl != 0U) && (ptr_cdc->bulk_out_pipe_hndl != 0U) && (ptr_cdc->int_in_pipe_hndl != 0U)) { @@ -515,11 +525,34 @@ __WEAK uint8_t USBH_CDC_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR * } exit: -#if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) - if (status != usbOK) { - EvrUSBH_CDC_ACM_ConfigureFailed(status); + if (ret == 255U) { // If configuration failed + if (ptr_cdc != NULL) { + // Delete previously created pipes + if (ptr_cdc->bulk_in_pipe_hndl != 0U) { + if (USBH_PipeDelete (ptr_cdc->bulk_in_pipe_hndl) == usbOK) { + ptr_cdc->bulk_in_pipe_hndl = 0U; + } + } + if (ptr_cdc->bulk_out_pipe_hndl != 0U) { + if (USBH_PipeDelete (ptr_cdc->bulk_out_pipe_hndl) == usbOK) { + ptr_cdc->bulk_out_pipe_hndl = 0U; + } + } + if (ptr_cdc->int_in_pipe_hndl != 0U) { + if (USBH_PipeDelete (ptr_cdc->int_in_pipe_hndl) == usbOK) { + ptr_cdc->int_in_pipe_hndl = 0U; + + } + } + if ((ptr_cdc->bulk_in_pipe_hndl == 0U) && + (ptr_cdc->bulk_out_pipe_hndl == 0U) && + (ptr_cdc->int_in_pipe_hndl == 0U)) { + memset ((void *)ptr_cdc, 0, sizeof (USBH_CDC)); + } + } + EvrUSBH_CDC_ACM_ConfigureFailed(usbClassErrorCDC); } -#endif + return ret; } @@ -589,6 +622,7 @@ __WEAK usbStatus USBH_CDC_Unconfigure (uint8_t instance) { __WEAK usbStatus USBH_CDC_Initialize_Lib (uint8_t instance) { USBH_CDC *ptr_cdc; CDC_LINE_CODING line_coding; + uint16_t max_packet_size; usbStatus status; EvrUSBH_CDC_ACM_Initialize(instance); @@ -603,6 +637,12 @@ __WEAK usbStatus USBH_CDC_Initialize_Lib (uint8_t instance) { line_coding.bParityType = 0; line_coding.bDataBits = 8; status = USBH_CDC_ACM_SetLineCoding (instance, (const CDC_LINE_CODING *)(&line_coding)); + if (status == usbOK) { + max_packet_size = ((USBH_PIPE *)ptr_cdc->int_in_pipe_hndl)->wMaxPacketSize; + if (max_packet_size > sizeof(ptr_cdc->int_in_data_buf)) { + status = usbClassErrorCDC; + } + } if (status == usbOK) { // Start Interrupt In Pipe handling thread ptr_cdc->int_in_thread_id = USBH_ThreadCreate (usbhThreadCDC, ptr_cdc->ptr_dev->class_instance); @@ -678,10 +718,11 @@ static usbStatus CheckInstance (uint8_t instance) { /// device (from Interrupt In endpoint) /// \param[in] arg index of CDC instance. void USBH_CDC_IntIn_Thread (void *arg) { - USBH_CDC *ptr_cdc; - uint16_t status; - uint8_t instance; - static uint8_t buf[16]; + USBH_CDC *ptr_cdc; + uint16_t status; + uint16_t max_packet_size; + uint16_t notify_len; + uint8_t instance; instance = (uint8_t)((uint32_t)arg); if (instance >= usbh_cdc_num) { @@ -692,27 +733,46 @@ void USBH_CDC_IntIn_Thread (void *arg) { } ptr_cdc = &usbh_cdc[instance]; + max_packet_size = ((USBH_PIPE *)ptr_cdc->int_in_pipe_hndl)->wMaxPacketSize; + if (max_packet_size == 0U) { + goto exit; + } + + if (max_packet_size < 10U) { + if ((10U % max_packet_size) == 0U) { + notify_len = 10U; + } else { + notify_len = ((10U + (max_packet_size - 1U)) / max_packet_size) * max_packet_size; + } + } else if (max_packet_size <= sizeof(ptr_cdc->int_in_data_buf)) { + notify_len = max_packet_size; + } else { + notify_len = sizeof(ptr_cdc->int_in_data_buf); + } for (;;) { - if (USBH_PipeReceive (ptr_cdc->int_in_pipe_hndl, buf, ((USBH_PIPE *)ptr_cdc->int_in_pipe_hndl)->wMaxPacketSize) == usbOK) { + if (USBH_PipeReceive(ptr_cdc->int_in_pipe_hndl, ptr_cdc->int_in_data_buf, notify_len) == usbOK) { // Data has been received on Interrupt In Pipe - if ((buf[0] == 0xA1U) && - (buf[1] == CDC_NOTIFICATION_SERIAL_STATE) && - (buf[2] == 0x00U) && - (buf[3] == 0x00U) && - (buf[4] == 0x00U) && - (buf[5] == 0x00U) && - (buf[6] == 0x02U) && - (buf[7] == 0x00U)) { + if ((ptr_cdc->int_in_data_buf[0] == 0xA1U) && + (ptr_cdc->int_in_data_buf[1] == CDC_NOTIFICATION_SERIAL_STATE) && + (ptr_cdc->int_in_data_buf[2] == 0x00U) && + (ptr_cdc->int_in_data_buf[3] == 0x00U) && + (ptr_cdc->int_in_data_buf[4] == 0x00U) && + (ptr_cdc->int_in_data_buf[5] == 0x00U) && + (ptr_cdc->int_in_data_buf[6] == 0x02U) && + (ptr_cdc->int_in_data_buf[7] == 0x00U)) { // If this is Notification then store new modem line and error status - status = (uint16_t)(((uint32_t)buf[9] << 8) | (uint32_t)buf[8]); + status = (uint16_t)(((uint32_t)ptr_cdc->int_in_data_buf[9] << 8) | (uint32_t)ptr_cdc->int_in_data_buf[8]); EvrUSBH_CDC_ACM_OnNotify (instance, status); USBH_CDC_ACM_Notify (instance, status); } } else { - ptr_cdc->int_in_thread_id = NULL; - (void)USBH_ThreadTerminate (USBH_ThreadGetHandle()); - return; + break; } } + +exit: + ptr_cdc->int_in_thread_id = NULL; + (void)USBH_ThreadTerminate (USBH_ThreadGetHandle()); + return; } diff --git a/Components/USB/Source/usbh_lib_cdc.h b/Components/USB/Source/usbh_lib_cdc.h index 9e8c49f6..e4adfc69 100644 --- a/Components/USB/Source/usbh_lib_cdc.h +++ b/Components/USB/Source/usbh_lib_cdc.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Host - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbh_lib_cdc.h * Purpose: USB Host - Communication Device Class (CDC) module header file @@ -40,4 +40,6 @@ extern usbStatus USBH_CDC_Initialize_Lib (uint8_t instance); /// \return status code that indicates the execution status of the function as defined with usbStatus. extern usbStatus USBH_CDC_Uninitialize_Lib (uint8_t instance); +extern void USBH_CDC_IntIn_Thread (void *arg); + #endif // USBH_LIB_CDC_H_ diff --git a/Components/USB/Source/usbh_lib_core.c b/Components/USB/Source/usbh_lib_core.c index 7806ca5e..8634b94d 100644 --- a/Components/USB/Source/usbh_lib_core.c +++ b/Components/USB/Source/usbh_lib_core.c @@ -9,7 +9,6 @@ #include "usbh_lib_core.h" -#include #include #include "usb_lib_debug.h" @@ -143,7 +142,7 @@ usbStatus USBH_Initialize (uint8_t ctrl) { } port_num = 0U; - for (port = 0U; port < 15U; port++) { + for (port = 0U; port < 16U; port++) { if ((usbh_capabilities[ctrl].port_mask & (1UL << port)) != 0U) { port_num ++; // Set VBUS high @@ -214,7 +213,7 @@ usbStatus USBH_Uninitialize (uint8_t ctrl) { // Delete Default Pipe first_err_status = USBH_DefaultPipeDelete (ctrl); - for (port = 0U; port < 15U; port++) { + for (port = 0U; port < 16U; port++) { if ((usbh_capabilities[ctrl].port_mask & (1UL << port)) != 0U) { if ((ptr_hc->port_con & (1UL << port)) != 0U) { // Uninitialize devices on active ports @@ -471,6 +470,8 @@ uint16_t USBH_Device_GetPID (uint8_t device) { usbStatus USBH_Device_GetStringDescriptor (uint8_t device, uint8_t index, uint16_t language_id, uint8_t *descriptor_data, uint16_t descriptor_length) { usbStatus status; + EvrUSBH_Core_DeviceGetStringDescriptor(device, index, language_id, descriptor_length); + status = CheckDeviceInstance (device); if (status == usbOK) { status = USBH_DeviceRequest_GetDescriptor (device, USB_REQUEST_TO_DEVICE, USB_STRING_DESCRIPTOR_TYPE, index, language_id, descriptor_data, descriptor_length); @@ -479,8 +480,6 @@ usbStatus USBH_Device_GetStringDescriptor (uint8_t device, uint8_t index, uint16 #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_Core_DeviceGetStringDescriptorFailed(device, index, language_id, descriptor_length, status); - } else { - EvrUSBH_Core_DeviceGetStringDescriptor(device, index, language_id, descriptor_length); } #endif return status; @@ -522,7 +521,7 @@ uint8_t USBH_CustomClass_GetDevice (uint8_t instance) { if (instance < usbh_cls_num) { // Find custom class device for requested custom class instance - for (i = 0U; i <= usbh_dev_num; i++) { + for (i = 0U; i < usbh_dev_num; i++) { if ((usbh_dev[i].state.in_use == 1U) && (usbh_dev[i].class_custom != 0U) && (usbh_dev[i].class_instance == instance)) { device = i; // Found requested custom class instance break; @@ -547,7 +546,7 @@ usbStatus USBH_CustomClass_GetStatus (uint8_t instance) { status = usbDeviceError; // Find custom class device for requested custom class instance - for (i = 0U; i <= usbh_dev_num; i++) { + for (i = 0U; i < usbh_dev_num; i++) { if ((usbh_dev[i].class_custom != 0U) && (usbh_dev[i].class_instance == instance)) { ptr_dev = &usbh_dev[i]; if ((ptr_dev->state.configured == 1U) && (ptr_dev->state.initialized == 1U)) { @@ -628,30 +627,55 @@ __WEAK usbStatus USBH_CustomClass_Uninitialize (uint8_t instance) { /// - value 0 : pipe creation failed USBH_PIPE_HANDLE USBH_PipeCreate (uint8_t device, uint8_t ep_addr, uint8_t ep_type, uint16_t ep_max_packet_size, uint8_t ep_interval) { const USBH_DEV *ptr_dev; - USBH_PIPE *ptr_pipe; + USBH_PIPE *ptr_pipe = NULL; uint32_t hw_handle; - ptr_dev = &usbh_dev[device]; - ptr_pipe = USBH_GetFree_PIPE (ptr_dev->ctrl); - if (ptr_pipe != NULL) { - EvrUSBH_Core_PipeCreate(device, ep_addr, ep_type, ep_max_packet_size, ep_interval, (USBH_PIPE_HANDLE)ptr_pipe); - hw_handle = USBH_DriverPipeCreate (ptr_dev->ctrl, ptr_dev->dev_addr, ptr_dev->dev_speed, ptr_dev->hub_addr, ptr_dev->hub_port, ep_addr, ep_type, ep_max_packet_size, ep_interval); - if (hw_handle != 0U) { - ptr_pipe->hw_handle = hw_handle; - ptr_pipe->thread_id = NULL; - ptr_pipe->bEndpointAddress = ep_addr; - ptr_pipe->bmAttributes = ep_type; - ptr_pipe->wMaxPacketSize = ep_max_packet_size; - ptr_pipe->bInterval = ep_interval; - ptr_pipe->device = device; - ptr_pipe->locked = 0U; - ptr_pipe->transfer_active = 0U; - ptr_pipe->transferred = 0U; - } + // Check parameters + if (device >= usbh_dev_num) { + goto exit; } -#if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) + ptr_dev = &usbh_dev[device]; + if (ptr_dev == NULL) { + goto exit; + } + if (ptr_dev->ctrl >= usbh_hc_num) { + goto exit; + } + + ptr_pipe = USBH_GetFree_PIPE(ptr_dev->ctrl); if (ptr_pipe == NULL) { + goto exit; + } + if ((ptr_dev->dev_speed == USB_SPEED_HIGH) && + ((ep_type == USB_ENDPOINT_TYPE_ISOCHRONOUS) || (ep_type == USB_ENDPOINT_TYPE_INTERRUPT)) && + (ep_interval > 16U)) { + // Limit maximum bInterval for high-speed isochronous and interrupt endpoints to 16, as per USB specification + ep_interval = 16U; + } + + hw_handle = USBH_DriverPipeCreate(ptr_dev->ctrl, ptr_dev->dev_addr, ptr_dev->dev_speed, ptr_dev->hub_addr, ptr_dev->hub_port, ep_addr, ep_type, ep_max_packet_size, ep_interval); + if (hw_handle == 0U) { + ptr_pipe = NULL; + goto exit; + } + + ptr_pipe->thread_id = NULL; + ptr_pipe->bEndpointAddress = ep_addr; + ptr_pipe->bmAttributes = ep_type; + ptr_pipe->wMaxPacketSize = ep_max_packet_size; + ptr_pipe->bInterval = ep_interval; + ptr_pipe->device = device; + ptr_pipe->locked = 0U; + ptr_pipe->transfer_active = 0U; + ptr_pipe->transferred = 0U; + ptr_pipe->hw_handle = hw_handle; + +exit: +#if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) + if (ptr_pipe != NULL) { + EvrUSBH_Core_PipeCreate(device, ep_addr, ep_type, ep_max_packet_size, ep_interval, (USBH_PIPE_HANDLE)ptr_pipe); + } else { EvrUSBH_Core_PipeCreateFailed(device, ep_addr, ep_type, ep_max_packet_size, ep_interval); } #endif @@ -659,7 +683,7 @@ USBH_PIPE_HANDLE USBH_PipeCreate (uint8_t device, uint8_t ep_addr, uint8_t ep_ty } /// \brief Register a callback for Pipe events -/// \detail Supported only for Isochronous pipes. +/// \details Supported only for Isochronous pipes. /// \param[in] pipe_hndl pipe handle. /// \param[in] cb_pipe_event pointer to pipe event callback function : /// - value > 0 : pointer to pipe event callback function @@ -671,20 +695,26 @@ usbStatus USBH_PipeRegisterCallback (USBH_PIPE_HANDLE pipe_hndl, USBH_PipeEvent_ EvrUSBH_Core_PipeRegisterCallback(pipe_hndl); + // Check parameters + if (pipe_hndl == 0U) { + status = usbInvalidParameter; + goto exit; + } + ptr_pipe = (USBH_PIPE *)pipe_hndl; - status = CheckPipe (ptr_pipe); - - if (status == usbOK) { - if ((((USBH_PIPE *)ptr_pipe)->bmAttributes & USB_ENDPOINT_TYPE_MASK) != USB_ENDPOINT_TYPE_ISOCHRONOUS) { - // If not a pipe to isochronous endpoint - status = usbInvalidParameter; - } + status = CheckPipe(ptr_pipe); + if (status != usbOK) { + goto exit; } - - if (status == usbOK) { - ptr_pipe->cb_pipe_event = cb_pipe_event; + if ((((USBH_PIPE *)ptr_pipe)->bmAttributes & USB_ENDPOINT_TYPE_MASK) != USB_ENDPOINT_TYPE_ISOCHRONOUS) { + // If not a pipe to isochronous endpoint + status = usbInvalidParameter; + goto exit; } + ptr_pipe->cb_pipe_event = cb_pipe_event; + +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_Core_PipeRegisterCallbackFailed(pipe_hndl, status); @@ -703,13 +733,32 @@ usbStatus USBH_PipeUpdate (USBH_PIPE_HANDLE pipe_hndl) { EvrUSBH_Core_PipeUpdate(pipe_hndl); + // Check parameters + if (pipe_hndl == 0U) { + status = usbInvalidParameter; + goto exit; + } + ptr_pipe = (USBH_PIPE *)pipe_hndl; - status = CheckPipe (ptr_pipe); - if (status == usbOK) { - ptr_dev = &usbh_dev[ptr_pipe->device]; - status = USBH_DriverPipeModify (ptr_dev->ctrl, ptr_pipe->hw_handle, ptr_dev->dev_addr, ptr_dev->dev_speed, ptr_dev->hub_addr, ptr_dev->hub_port, ptr_pipe->wMaxPacketSize); + status = CheckPipe(ptr_pipe); + if (status != usbOK) { + goto exit; } + if (ptr_pipe->device >= usbh_dev_num) { + status = usbDeviceError; + goto exit; + } + + ptr_dev = &usbh_dev[ptr_pipe->device]; + if (ptr_dev == NULL) { + status = usbDeviceError; + goto exit; + } + + status = USBH_DriverPipeModify(ptr_dev->ctrl, ptr_pipe->hw_handle, ptr_dev->dev_addr, ptr_dev->dev_speed, ptr_dev->hub_addr, ptr_dev->hub_port, ptr_pipe->wMaxPacketSize); + +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_Core_PipeUpdateFailed(pipe_hndl, status); @@ -728,19 +777,44 @@ usbStatus USBH_PipeDelete (USBH_PIPE_HANDLE pipe_hndl) { EvrUSBH_Core_PipeDelete(pipe_hndl); + // Check parameters + if (pipe_hndl == 0U) { + status = usbInvalidParameter; + goto exit; + } + ptr_pipe = (USBH_PIPE *)pipe_hndl; - status = CheckPipe (ptr_pipe); - if (status == usbOK) { - ptr_dev = &usbh_dev[ptr_pipe->device]; - status = USBH_DriverPipeTransferAbort (ptr_dev->ctrl, ptr_pipe->hw_handle); - if (status == usbOK) { - status = USBH_DriverPipeDelete (ptr_dev->ctrl, ptr_pipe->hw_handle); - if (status == usbOK) { - memset ((void *)ptr_pipe, 0, sizeof(USBH_PIPE)); - } + status = CheckPipe(ptr_pipe); + if (status != usbOK) { + goto exit; + } + + if (ptr_pipe->device >= usbh_dev_num) { + status = usbDeviceError; + goto exit; + } + + if ((ptr_pipe->locked != 0U) || (ptr_pipe->transfer_active != 0U)) { + status = USBH_PipeAbort(pipe_hndl); + if (status != usbOK) { + goto exit; } } + ptr_dev = &usbh_dev[ptr_pipe->device]; + if (ptr_dev == NULL) { + status = usbDeviceError; + goto exit; + } + + status = USBH_DriverPipeDelete(ptr_dev->ctrl, ptr_pipe->hw_handle); + if (status != usbOK) { + goto exit; + } + + memset((void *)ptr_pipe, 0, sizeof(USBH_PIPE)); + +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_Core_PipeDeleteFailed(pipe_hndl, status); @@ -759,13 +833,32 @@ usbStatus USBH_PipeReset (USBH_PIPE_HANDLE pipe_hndl) { EvrUSBH_Core_PipeReset(pipe_hndl); + // Check parameters + if (pipe_hndl == 0U) { + status = usbInvalidParameter; + goto exit; + } + ptr_pipe = (USBH_PIPE *)pipe_hndl; - status = CheckPipe (ptr_pipe); - if (status == usbOK) { - ptr_dev = &usbh_dev[ptr_pipe->device]; - status = USBH_DriverPipeReset (ptr_dev->ctrl, ptr_pipe->hw_handle); + status = CheckPipe(ptr_pipe); + if (status != usbOK) { + goto exit; } + if (ptr_pipe->device >= usbh_dev_num) { + status = usbDeviceError; + goto exit; + } + + ptr_dev = &usbh_dev[ptr_pipe->device]; + if (ptr_dev == NULL) { + status = usbDeviceError; + goto exit; + } + + status = USBH_DriverPipeReset (ptr_dev->ctrl, ptr_pipe->hw_handle); + +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_Core_PipeResetFailed(pipe_hndl, status); @@ -785,7 +878,7 @@ usbStatus USBH_PipeReceive (USBH_PIPE_HANDLE pipe_hndl, uint8_t *buf, uint32_t l USBH_PIPE *ptr_pipe; uint32_t event; ARM_USBH_EP_HANDLE hw_handle; - uint8_t *ptr_data; + uint8_t *ptr_data = NULL; uint32_t max_data_len; uint32_t remain_len; uint32_t receive_len; @@ -800,13 +893,14 @@ usbStatus USBH_PipeReceive (USBH_PIPE_HANDLE pipe_hndl, uint8_t *buf, uint32_t l EvrUSBH_Core_PipeReceive(pipe_hndl, len); + // Check parameters if ((buf == NULL) && (len != 0U)) { status = usbInvalidParameter; goto exit; } ptr_pipe = (USBH_PIPE *)pipe_hndl; - status = CheckPipe (ptr_pipe); + status = CheckPipe(ptr_pipe); if (status != usbOK) { goto exit; } @@ -814,21 +908,50 @@ usbStatus USBH_PipeReceive (USBH_PIPE_HANDLE pipe_hndl, uint8_t *buf, uint32_t l status = usbDriverBusy; goto exit; } + if (ptr_pipe->thread_id != NULL) { + status = usbDriverBusy; + goto exit; + } - ptr_dev = &usbh_dev[ptr_pipe->device]; - ctrl = ptr_dev->ctrl; - ptr_hc = &usbh_hc[ctrl]; - hw_handle = ptr_pipe->hw_handle; - max_packet_size = ptr_pipe->wMaxPacketSize; + if (ptr_pipe->device >= usbh_dev_num) { + status = usbDeviceError; + goto exit; + } + + ptr_dev = &usbh_dev[ptr_pipe->device]; + if (ptr_dev == NULL) { + status = usbDeviceError; + goto exit; + } + ctrl = ptr_dev->ctrl; + status = CheckController(ctrl); + if (status != usbOK) { + goto exit; + } + ptr_hc = &usbh_hc[ctrl]; + if (ptr_hc == NULL) { + status = usbControllerError; + goto exit; + } + hw_handle = ptr_pipe->hw_handle; + if (hw_handle == 0U) { + status = usbDriverError; + goto exit; + } + max_packet_size = ptr_pipe->wMaxPacketSize; + if (max_packet_size == 0U) { + status = usbUnknownError; + goto exit; + } ptr_pipe->locked = 1U; if (((ptr_pipe->bmAttributes & 3U)==ARM_USB_ENDPOINT_ISOCHRONOUS) && // If Isochronous pipe (ptr_pipe->cb_pipe_event != NULL)) { // and registered callback // Isochronous pipe handling - status = USBH_DriverPipeTransfer (ctrl, hw_handle, ARM_USBH_PACKET_IN, buf, len); + status = USBH_DriverPipeTransfer(ctrl, hw_handle, ARM_USBH_PACKET_IN, buf, len); ptr_pipe->locked = 0U; - return status; + goto exit; } remain_len = len; @@ -837,7 +960,7 @@ usbStatus USBH_PipeReceive (USBH_PIPE_HANDLE pipe_hndl, uint8_t *buf, uint32_t l if (remain_len == 0U) { // If ZLP is requested alloc_mem = false; } else { - alloc_mem = !USBH_MemoryIsInPool (ctrl, buf); + alloc_mem = !USBH_MemoryIsInPool(ctrl, buf); } if (alloc_mem) { // If memory for USB must be relocated, allocate maximum memory chunk for reception @@ -845,16 +968,21 @@ usbStatus USBH_PipeReceive (USBH_PIPE_HANDLE pipe_hndl, uint8_t *buf, uint32_t l max_data_len = ptr_hc->port_mem_max & (~((uint32_t)max_packet_size-1U)); } do { - mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_data), max_data_len); + mstatus = USBH_MemoryAllocate(ctrl, &ptr_data, max_data_len); if (mstatus == usbOK) { break; } - max_data_len -= max_packet_size; if (max_data_len <= max_packet_size) { status = usbMemoryError; - goto exit; + goto done; + } else { + max_data_len -= max_packet_size; } - } while (max_data_len > max_packet_size); + } while (max_data_len >= max_packet_size); + if (mstatus != usbOK) { + status = usbMemoryError; + goto done; + } } else { // If memory is in pool it is ok ptr_data = buf; } @@ -863,7 +991,7 @@ usbStatus USBH_PipeReceive (USBH_PIPE_HANDLE pipe_hndl, uint8_t *buf, uint32_t l ptr_data = (uint8_t *)buf; } - packet = ARM_USBH_PACKET_IN; + packet = ARM_USBH_PACKET_IN; if ((ptr_pipe->bEndpointAddress & 0x0FU) == 0U) { // If EP0 IN on control endpoint => force DATA1 packet &= ~ARM_USBH_PACKET_DATA_Msk; packet |= ARM_USBH_PACKET_DATA1; @@ -872,10 +1000,6 @@ usbStatus USBH_PipeReceive (USBH_PIPE_HANDLE pipe_hndl, uint8_t *buf, uint32_t l transferred = 0U; received_len = 0U; - if (ptr_pipe->thread_id != NULL) { - status = usbDriverBusy; - goto mem_free_and_exit; - } ptr_pipe->thread_id = USBH_ThreadGetHandle(); for (;;) { @@ -891,7 +1015,7 @@ usbStatus USBH_PipeReceive (USBH_PIPE_HANDLE pipe_hndl, uint8_t *buf, uint32_t l if (((packet & ARM_USBH_PACKET_DATA1) != 0U) && (transferred != 0U)) { packet &= ~ARM_USBH_PACKET_DATA1; } - status = USBH_DriverPipeTransfer (ctrl, hw_handle, packet, ptr_data + received_len, receive_len - received_len); + status = USBH_DriverPipeTransfer(ctrl, hw_handle, packet, ptr_data + received_len, receive_len - received_len); if (status != usbOK) { // If transfer failed clear EP to TID and exit goto done; } @@ -914,9 +1038,15 @@ usbStatus USBH_PipeReceive (USBH_PIPE_HANDLE pipe_hndl, uint8_t *buf, uint32_t l break; } ptr_pipe->transfer_active = 0U; - transferred = USBH_DriverPipeTransferGetResult (ctrl, hw_handle); + transferred = USBH_DriverPipeTransferGetResult(ctrl, hw_handle); + if (transferred > (receive_len - received_len)) { + transferred = receive_len - received_len; + } + if (transferred > remain_len) { + transferred = remain_len; + } if ((alloc_mem) && (buf != NULL) && (ptr_data != NULL) && (transferred != 0U)) { - memcpy ((void *)buf, (void *)(ptr_data + received_len), transferred); + memcpy((void *)buf, (void *)(ptr_data + received_len), transferred); } ptr_pipe->transferred += transferred; received_len += transferred; @@ -935,6 +1065,10 @@ usbStatus USBH_PipeReceive (USBH_PIPE_HANDLE pipe_hndl, uint8_t *buf, uint32_t l goto done; } } else { + if ((ptr_dev->hub_port == 0U) || (ptr_dev->hub_port >= 16U)) { + status = usbTransferError; + goto done; + } if ((ptr_hc->port_event[ptr_dev->hub_port - 1U]) != 0U) { status = usbTransferError; goto done; @@ -1002,14 +1136,14 @@ usbStatus USBH_PipeReceive (USBH_PIPE_HANDLE pipe_hndl, uint8_t *buf, uint32_t l } abort_and_done: - (void)USBH_DriverPipeTransferAbort (ctrl, hw_handle); + (void)USBH_DriverPipeTransferAbort(ctrl, hw_handle); done: - ptr_pipe->thread_id = NULL; - ptr_pipe->locked = 0U; + ptr_pipe->thread_id = NULL; + ptr_pipe->transfer_active = 0U; + ptr_pipe->locked = 0U; -mem_free_and_exit: - if (alloc_mem) { + if ((alloc_mem) && (ptr_data != NULL)) { mstatus = USBH_MemoryFree (ctrl, (uint8_t *) ptr_data); if (mstatus != usbOK) { status = mstatus; @@ -1035,7 +1169,7 @@ uint32_t USBH_PipeReceiveGetResult (USBH_PIPE_HANDLE pipe_hndl) { num = 0U; ptr_pipe = (USBH_PIPE *)pipe_hndl; - if (CheckPipe (ptr_pipe) == usbOK) { + if (CheckPipe(ptr_pipe) == usbOK) { if ((ptr_pipe->bEndpointAddress & 0x80U) != 0U) { // If pipe endpoint IN num = ptr_pipe->transferred; } @@ -1057,7 +1191,7 @@ usbStatus USBH_PipeSend (USBH_PIPE_HANDLE pipe_hndl, const uint8_t *buf, uint32_ USBH_PIPE *ptr_pipe; uint32_t event; ARM_USBH_EP_HANDLE hw_handle; - uint8_t *ptr_data; + uint8_t *ptr_data = NULL; uint32_t max_data_len; uint32_t remain_len; uint32_t send_len; @@ -1079,7 +1213,7 @@ usbStatus USBH_PipeSend (USBH_PIPE_HANDLE pipe_hndl, const uint8_t *buf, uint32_ } ptr_pipe = (USBH_PIPE *)pipe_hndl; - status = CheckPipe (ptr_pipe); + status = CheckPipe(ptr_pipe); if (status != usbOK) { goto exit; } @@ -1087,21 +1221,50 @@ usbStatus USBH_PipeSend (USBH_PIPE_HANDLE pipe_hndl, const uint8_t *buf, uint32_ status = usbDriverBusy; goto exit; } + if (ptr_pipe->thread_id != NULL) { + status = usbDriverBusy; + goto exit; + } - ptr_dev = &usbh_dev[ptr_pipe->device]; - ctrl = ptr_dev->ctrl; - ptr_hc = &usbh_hc[ctrl]; - hw_handle = ptr_pipe->hw_handle; - max_packet_size = ptr_pipe->wMaxPacketSize; + if (ptr_pipe->device >= usbh_dev_num) { + status = usbDeviceError; + goto exit; + } + + ptr_dev = &usbh_dev[ptr_pipe->device]; + if (ptr_dev == NULL) { + status = usbDeviceError; + goto exit; + } + ctrl = ptr_dev->ctrl; + status = CheckController(ctrl); + if (status != usbOK) { + goto exit; + } + ptr_hc = &usbh_hc[ctrl]; + if (ptr_hc == NULL) { + status = usbControllerError; + goto exit; + } + hw_handle = ptr_pipe->hw_handle; + if (hw_handle == 0U) { + status = usbDriverError; + goto exit; + } + max_packet_size = ptr_pipe->wMaxPacketSize; + if (max_packet_size == 0U) { + status = usbUnknownError; + goto exit; + } ptr_pipe->locked = 1U; if (((ptr_pipe->bmAttributes & 3U)==ARM_USB_ENDPOINT_ISOCHRONOUS) && // If Isochronous pipe (ptr_pipe->cb_pipe_event != NULL)) { // and registered callback // Isochronous pipe handling - status = USBH_DriverPipeTransfer (ctrl, hw_handle, ARM_USBH_PACKET_OUT, (uint8_t *)((uint32_t)buf), len); + status = USBH_DriverPipeTransfer(ctrl, hw_handle, ARM_USBH_PACKET_OUT, (uint8_t *)(uintptr_t)buf, len); ptr_pipe->locked = 0U; - return status; + goto exit; } remain_len = len; @@ -1118,22 +1281,24 @@ usbStatus USBH_PipeSend (USBH_PIPE_HANDLE pipe_hndl, const uint8_t *buf, uint32_ max_data_len = ptr_hc->port_mem_max & (~((uint32_t)max_packet_size-1U)); } do { - mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_data), max_data_len); + mstatus = USBH_MemoryAllocate (ctrl, &ptr_data, max_data_len); if (mstatus == usbOK) { break; } - max_data_len -= max_packet_size; - if (max_data_len <= max_packet_size) { - status = usbMemoryError; - goto exit; + if (max_data_len >= max_packet_size) { + max_data_len -= max_packet_size; } - } while (max_data_len > max_packet_size); + } while (max_data_len >= max_packet_size); + if (mstatus != usbOK) { + status = usbMemoryError; + goto done; + } } else { // If memory is in pool it is ok - ptr_data = (uint8_t *)((uint32_t)buf); + ptr_data = (uint8_t *)(uintptr_t)buf; } } else { // If memory relocation is not enabled alloc_mem = false; - ptr_data = (uint8_t *)((uint32_t)buf); + ptr_data = (uint8_t *)(uintptr_t)buf; } packet = ARM_USBH_PACKET_OUT; @@ -1150,10 +1315,6 @@ usbStatus USBH_PipeSend (USBH_PIPE_HANDLE pipe_hndl, const uint8_t *buf, uint32_ hs = 0U; } - if (ptr_pipe->thread_id != NULL) { - status = usbDriverBusy; - goto mem_free_and_exit; - } ptr_pipe->thread_id = USBH_ThreadGetHandle(); for (;;) { @@ -1172,7 +1333,7 @@ usbStatus USBH_PipeSend (USBH_PIPE_HANDLE pipe_hndl, const uint8_t *buf, uint32_ if (((packet & ARM_USBH_PACKET_DATA1) != 0U) && (transferred != 0U)) { packet &= ~ARM_USBH_PACKET_DATA1; } - status = USBH_DriverPipeTransfer (ctrl, hw_handle, packet, ptr_data + sent_len, send_len - sent_len); + status = USBH_DriverPipeTransfer(ctrl, hw_handle, packet, ptr_data + sent_len, send_len - sent_len); if (status != usbOK) { // If transfer failed clear EP to TID and exit goto done; } @@ -1195,7 +1356,13 @@ usbStatus USBH_PipeSend (USBH_PIPE_HANDLE pipe_hndl, const uint8_t *buf, uint32_ break; } ptr_pipe->transfer_active = 0U; - transferred = USBH_DriverPipeTransferGetResult (ctrl, hw_handle); + transferred = USBH_DriverPipeTransferGetResult(ctrl, hw_handle); + if (transferred > send_len) { + transferred = send_len; + } + if (transferred > remain_len) { + transferred = remain_len; + } ptr_pipe->transferred += transferred; sent_len += transferred; remain_len -= transferred; @@ -1213,6 +1380,10 @@ usbStatus USBH_PipeSend (USBH_PIPE_HANDLE pipe_hndl, const uint8_t *buf, uint32_ goto done; } } else { + if ((ptr_dev->hub_port == 0U) || (ptr_dev->hub_port >= 16U)) { + status = usbTransferError; + goto done; + } if ((ptr_hc->port_event[ptr_dev->hub_port - 1U]) != 0U) { status = usbTransferError; goto done; @@ -1239,7 +1410,7 @@ usbStatus USBH_PipeSend (USBH_PIPE_HANDLE pipe_hndl, const uint8_t *buf, uint32_ case ARM_USB_ENDPOINT_CONTROL: case ARM_USB_ENDPOINT_BULK: if (hs == 1U) { // For high-speed pipe endpoint and was not an error - status = USBH_PipeDoPing (pipe_hndl); + status = USBH_PipeDoPing(pipe_hndl); if (status != usbOK) { goto done; } @@ -1271,7 +1442,7 @@ usbStatus USBH_PipeSend (USBH_PIPE_HANDLE pipe_hndl, const uint8_t *buf, uint32_ status = usbOK; goto done; } - status = USBH_PipeDoPing (pipe_hndl); + status = USBH_PipeDoPing(pipe_hndl); if (status == usbOK) { goto done; } @@ -1302,11 +1473,11 @@ usbStatus USBH_PipeSend (USBH_PIPE_HANDLE pipe_hndl, const uint8_t *buf, uint32_ (void)USBH_DriverPipeTransferAbort (ctrl, hw_handle); done: - ptr_pipe->thread_id = NULL; - ptr_pipe->locked = 0U; + ptr_pipe->thread_id = NULL; + ptr_pipe->transfer_active = 0U; + ptr_pipe->locked = 0U; -mem_free_and_exit: - if (alloc_mem) { + if ((alloc_mem) && (ptr_data != NULL)) { mstatus = USBH_MemoryFree (ctrl, (uint8_t *) ptr_data); if (mstatus != usbOK) { status = mstatus; @@ -1350,38 +1521,64 @@ usbStatus USBH_PipeAbort (USBH_PIPE_HANDLE pipe_hndl) { const USBH_DEV *ptr_dev; USBH_PIPE *ptr_pipe; usbStatus status; + uint8_t ctrl; uint8_t retry; EvrUSBH_Core_PipeAbort(pipe_hndl); ptr_pipe = (USBH_PIPE *)pipe_hndl; status = CheckPipe (ptr_pipe); - ptr_dev = &usbh_dev[ptr_pipe->device]; + if (status != usbOK) { + goto exit; + } + + if (ptr_pipe->device >= usbh_dev_num) { + status = usbDeviceError; + goto exit; + } + + ptr_dev = &usbh_dev[ptr_pipe->device]; + if (ptr_dev == NULL) { + status = usbDeviceError; + goto exit; + } + ctrl = ptr_dev->ctrl; + status = CheckController(ctrl); + if (status != usbOK) { + goto exit; + } if (((ptr_pipe->bmAttributes & 3U)==ARM_USB_ENDPOINT_ISOCHRONOUS) && // If Isochronous pipe (ptr_pipe->cb_pipe_event != NULL)) { // and registered callback // Isochronous pipe handling - status = USBH_DriverPipeTransferAbort (ptr_dev->ctrl, ptr_pipe->hw_handle); + status = USBH_DriverPipeTransferAbort(ctrl, ptr_pipe->hw_handle); ptr_pipe->locked = 0U; - return status; + goto exit; } if (status == usbOK) { - if (ptr_pipe->locked == 1U) { - retry = 3U; - while (retry-- != 0U) { - if (ptr_pipe->transfer_active == 1U) { break; } - (void)USBH_Delay (10U); - } - if (ptr_pipe->transfer_active == 1U) { - status = USBH_DriverPipeTransferAbort (ptr_dev->ctrl, ptr_pipe->hw_handle); - if (status == usbOK) { - USBH_SignalPipeEvent (ptr_dev->ctrl, ptr_pipe->hw_handle, ARM_USBH_EVENT_ABORT); + if (ptr_pipe->locked != 0U) { + for (retry = 3U; retry != 0U; retry--) { + if (ptr_pipe->transfer_active != 0U) { + status = USBH_DriverPipeTransferAbort(ctrl, ptr_pipe->hw_handle); + if (status == usbOK) { + USBH_SignalPipeEvent (ctrl, ptr_pipe->hw_handle, ARM_USBH_EVENT_ABORT); + break; + } } + (void)USBH_Delay (10U); } } + for (retry = 3U; retry != 0U; retry--) { + if ((ptr_pipe->transfer_active == 0U) && (ptr_pipe->locked == 0U)) { break; } + (void)USBH_Delay (10U); + } + if ((ptr_pipe->transfer_active != 0U) || (ptr_pipe->locked != 0U)) { + status = usbTimeout; + } } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_Core_PipeAbortFailed(pipe_hndl, status); @@ -1402,14 +1599,21 @@ usbStatus USBH_ControlTransfer (uint8_t device, const USB_SETUP_PACKET *setup_pa uint8_t ctrl, retry; usbStatus status; - EvrUSBH_Core_ControlTransfer(device, (const void *)setup_packet, len); + // Check parameters + if ((device >= usbh_dev_num) || + (setup_packet == NULL)) { + status = usbInvalidParameter; + goto exit; + } + + EvrUSBH_Core_ControlTransfer(device, setup_packet, len); if ((data != NULL) || (len == 0U)) { ptr_dev = &usbh_dev[device]; ctrl = ptr_dev->ctrl; if (ctrl < usbh_hc_num) { retry = 3U; - while (retry-- != 0U) { + for (retry = 3U; retry != 0U; retry--) { if (USBH_MutexAcquire (usbh_def_pipe_mutex_id[ctrl], 500U) == 0) { break; } (void)USBH_Delay (100U); } @@ -1505,8 +1709,7 @@ usbStatus USBH_ControlTransfer (uint8_t device, const USB_SETUP_PACKET *setup_pa } } - retry = 3U; - while (retry-- != 0U) { + for (retry = 3U; retry != 0U; retry--) { if (USBH_MutexRelease (usbh_def_pipe_mutex_id[ctrl]) == 0) { break; } (void)USBH_Delay (100U); } @@ -1521,9 +1724,10 @@ usbStatus USBH_ControlTransfer (uint8_t device, const USB_SETUP_PACKET *setup_pa status = usbInvalidParameter; } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { - EvrUSBH_Core_ControlTransferFailed(device, (const void *)setup_packet, status); + EvrUSBH_Core_ControlTransferFailed(device, setup_packet, status); } #endif return status; @@ -1541,11 +1745,18 @@ usbStatus USBH_DeviceRequest_GetStatus (uint8_t device, uint8_t recipient, uint8 usbStatus status; usbStatus mstatus; + // Check parameters + if ((device >= usbh_dev_num) || + (ptr_stat_dat == NULL)) { + status = usbInvalidParameter; + goto exit; + } + ctrl = usbh_dev[device].ctrl; status = CheckController (ctrl); if (status == usbOK) { if (ptr_stat_dat != NULL) { - status = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_sp), 8U); + status = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_sp, 8U); if (status == usbOK) { PREPARE_SETUP_PACKET_DATA ( ptr_sp, USB_REQUEST_DEVICE_TO_HOST, USB_REQUEST_STANDARD, recipient, USB_REQUEST_GET_STATUS, 0U, index, 2U) status = USBH_ControlTransfer (device, ptr_sp, ptr_stat_dat, 2U); @@ -1559,9 +1770,10 @@ usbStatus USBH_DeviceRequest_GetStatus (uint8_t device, uint8_t recipient, uint8 } } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status == usbOK) { - EvrUSBH_Core_RequestGetStatus (device, recipient, index, ((uint16_t)(*(ptr_stat_dat+1U))) | ((uint16_t)*ptr_stat_dat)); + EvrUSBH_Core_RequestGetStatus(device, recipient, index, ((uint16_t)(*(ptr_stat_dat+1U))) | ((uint16_t)*ptr_stat_dat)); } else { EvrUSBH_Core_RequestGetStatusFailed(device, recipient, index, status); } @@ -1583,10 +1795,16 @@ usbStatus USBH_DeviceRequest_ClearFeature (uint8_t device, uint8_t recipient, ui EvrUSBH_Core_RequestClearFeature(device, recipient, index, feature_selector); + // Check parameters + if (device >= usbh_dev_num) { + status = usbInvalidParameter; + goto exit; + } + ctrl = usbh_dev[device].ctrl; status = CheckController (ctrl); if (status == usbOK) { - status = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_sp), 8U); + status = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_sp, 8U); if (status == usbOK) { PREPARE_SETUP_PACKET_DATA ( ptr_sp, USB_REQUEST_HOST_TO_DEVICE, USB_REQUEST_STANDARD, recipient, USB_REQUEST_CLEAR_FEATURE, feature_selector, index, 0U) status = USBH_ControlTransfer (device, ptr_sp, NULL, 0U); @@ -1597,6 +1815,7 @@ usbStatus USBH_DeviceRequest_ClearFeature (uint8_t device, uint8_t recipient, ui } } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_Core_RequestClearFeatureFailed(device, recipient, index, feature_selector, status); @@ -1619,10 +1838,16 @@ usbStatus USBH_DeviceRequest_SetFeature (uint8_t device, uint8_t recipient, uint EvrUSBH_Core_RequestSetFeature(device, recipient, index, feature_selector); + // Check parameters + if (device >= usbh_dev_num) { + status = usbInvalidParameter; + goto exit; + } + ctrl = usbh_dev[device].ctrl; status = CheckController (ctrl); if (status == usbOK) { - status = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_sp), 8U); + status = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_sp, 8U); if (status == usbOK) { PREPARE_SETUP_PACKET_DATA ( ptr_sp, USB_REQUEST_HOST_TO_DEVICE, USB_REQUEST_STANDARD, recipient, USB_REQUEST_SET_FEATURE, feature_selector, index, 0U) status = USBH_ControlTransfer (device, ptr_sp, NULL, 0U); @@ -1633,6 +1858,7 @@ usbStatus USBH_DeviceRequest_SetFeature (uint8_t device, uint8_t recipient, uint } } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_Core_RequestSetFeatureFailed(device, recipient, index, feature_selector, status); @@ -1653,10 +1879,17 @@ usbStatus USBH_DeviceRequest_SetAddress (uint8_t device, uint8_t device_address) EvrUSBH_Core_RequestSetAddress(device, device_address); + // Check parameters + if ((device >= usbh_dev_num) || + (device_address > 127U)) { + status = usbInvalidParameter; + goto exit; + } + ctrl = usbh_dev[device].ctrl; status = CheckController (ctrl); if (status == usbOK) { - status = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_sp), 8U); + status = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_sp, 8U); if (status == usbOK) { PREPARE_SETUP_PACKET_DATA ( ptr_sp, USB_REQUEST_HOST_TO_DEVICE, USB_REQUEST_STANDARD, USB_REQUEST_TO_DEVICE, USB_REQUEST_SET_ADDRESS, device_address, 0U, 0U) status = USBH_ControlTransfer (device, ptr_sp, NULL, 0U); @@ -1670,6 +1903,7 @@ usbStatus USBH_DeviceRequest_SetAddress (uint8_t device, uint8_t device_address) } } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_Core_RequestSetAddressFailed(device, device_address, status); @@ -1695,12 +1929,20 @@ usbStatus USBH_DeviceRequest_GetDescriptor (uint8_t device, uint8_t recipient, u EvrUSBH_Core_RequestGetDescriptor(device, recipient, descriptor_type, descriptor_index, language_id, descriptor_length); + // Check parameters + if ((device >= usbh_dev_num) || + (descriptor_data == NULL)) { + status = usbInvalidParameter; + goto exit; + } + ctrl = usbh_dev[device].ctrl; status = CheckController (ctrl); if (status == usbOK) { if (descriptor_data != NULL) { - status = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_sp), 8U); + status = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_sp, 8U); if (status == usbOK) { + memset(descriptor_data, 0, descriptor_length); PREPARE_SETUP_PACKET_DATA ( ptr_sp, USB_REQUEST_DEVICE_TO_HOST, USB_REQUEST_STANDARD, recipient, USB_REQUEST_GET_DESCRIPTOR, (((uint32_t)descriptor_type << 8) | (uint32_t)descriptor_index), language_id, descriptor_length) status = USBH_ControlTransfer (device, ptr_sp, descriptor_data, descriptor_length); mstatus = USBH_MemoryFree (ctrl, (uint8_t *) ptr_sp); @@ -1713,6 +1955,7 @@ usbStatus USBH_DeviceRequest_GetDescriptor (uint8_t device, uint8_t recipient, u } } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_Core_RequestGetDescriptorFailed(device, recipient, descriptor_type, descriptor_index, language_id, descriptor_length, status); @@ -1738,14 +1981,21 @@ usbStatus USBH_DeviceRequest_SetDescriptor (uint8_t device, uint8_t recipient, u EvrUSBH_Core_RequestSetDescriptor(device, recipient, descriptor_type, descriptor_index, language_id, descriptor_length); + // Check parameters + if ((device >= usbh_dev_num) || + (descriptor_data == NULL)) { + status = usbInvalidParameter; + goto exit; + } + ctrl = usbh_dev[device].ctrl; status = CheckController (ctrl); if (status == usbOK) { if (descriptor_data != NULL) { - status = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_sp), 8U); + status = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_sp, 8U); if (status == usbOK) { PREPARE_SETUP_PACKET_DATA ( ptr_sp, USB_REQUEST_HOST_TO_DEVICE, USB_REQUEST_STANDARD, recipient, USB_REQUEST_SET_DESCRIPTOR, (((uint32_t)descriptor_type << 8) | (uint32_t)descriptor_index), language_id, descriptor_length) - status = USBH_ControlTransfer (device, ptr_sp, (uint8_t *)((uint32_t)descriptor_data), descriptor_length); + status = USBH_ControlTransfer (device, ptr_sp, (uint8_t *)(uintptr_t)descriptor_data, descriptor_length); mstatus = USBH_MemoryFree (ctrl, (uint8_t *) ptr_sp); if ((mstatus != usbOK) && (status == usbOK)) { status = mstatus; @@ -1756,6 +2006,7 @@ usbStatus USBH_DeviceRequest_SetDescriptor (uint8_t device, uint8_t recipient, u } } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_Core_RequestSetDescriptorFailed(device, recipient, descriptor_type, descriptor_index, language_id, descriptor_length, status); @@ -1774,15 +2025,18 @@ usbStatus USBH_DeviceRequest_GetConfiguration (uint8_t device, uint8_t *ptr_conf usbStatus status; usbStatus mstatus; - if (ptr_configuration != NULL) { - EvrUSBH_Core_RequestGetConfiguration(device, *ptr_configuration); + // Check parameters + if ((device >= usbh_dev_num) || + (ptr_configuration == NULL)) { + status = usbInvalidParameter; + goto exit; } ctrl = usbh_dev[device].ctrl; status = CheckController (ctrl); if (status == usbOK) { if (ptr_configuration != NULL) { - status = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_sp), 8U); + status = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_sp, 8U); if (status == usbOK) { PREPARE_SETUP_PACKET_DATA ( ptr_sp, USB_REQUEST_DEVICE_TO_HOST, USB_REQUEST_STANDARD, USB_REQUEST_TO_DEVICE, USB_REQUEST_GET_CONFIGURATION, 0U, 0U, 1U) status = USBH_ControlTransfer (device, ptr_sp, ptr_configuration, 1U); @@ -1796,8 +2050,11 @@ usbStatus USBH_DeviceRequest_GetConfiguration (uint8_t device, uint8_t *ptr_conf } } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) - if (status != usbOK) { + if (status == usbOK) { + EvrUSBH_Core_RequestGetConfiguration(device, *ptr_configuration); + } else { EvrUSBH_Core_RequestGetConfigurationFailed(device, status); } #endif @@ -1816,10 +2073,16 @@ usbStatus USBH_DeviceRequest_SetConfiguration (uint8_t device, uint8_t configura EvrUSBH_Core_RequestSetConfiguration(device, configuration); + // Check parameters + if (device >= usbh_dev_num) { + status = usbInvalidParameter; + goto exit; + } + ctrl = usbh_dev[device].ctrl; status = CheckController (ctrl); if (status == usbOK) { - status = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_sp), 8U); + status = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_sp, 8U); if (status == usbOK) { PREPARE_SETUP_PACKET_DATA ( ptr_sp, USB_REQUEST_HOST_TO_DEVICE, USB_REQUEST_STANDARD, USB_REQUEST_TO_DEVICE, USB_REQUEST_SET_CONFIGURATION, configuration, 0U, 0U) status = USBH_ControlTransfer (device, ptr_sp, NULL, 0U); @@ -1833,6 +2096,7 @@ usbStatus USBH_DeviceRequest_SetConfiguration (uint8_t device, uint8_t configura } } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_Core_RequestSetConfigurationFailed(device, configuration, status); @@ -1852,15 +2116,18 @@ usbStatus USBH_DeviceRequest_GetInterface (uint8_t device, uint8_t index, uint8_ usbStatus status; usbStatus mstatus; - if (ptr_alternate != NULL) { - EvrUSBH_Core_RequestGetInterface(device, index, *ptr_alternate); + // Check parameters + if ((device >= usbh_dev_num) || + (ptr_alternate == NULL)) { + status = usbInvalidParameter; + goto exit; } ctrl = usbh_dev[device].ctrl; status = CheckController (ctrl); if (status == usbOK) { if (ptr_alternate != NULL) { - status = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_sp), 8U); + status = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_sp, 8U); if (status == usbOK) { PREPARE_SETUP_PACKET_DATA ( ptr_sp, USB_REQUEST_DEVICE_TO_HOST, USB_REQUEST_STANDARD, USB_REQUEST_TO_DEVICE, USB_REQUEST_GET_INTERFACE, 0U, index, 1U) status = USBH_ControlTransfer (device, ptr_sp, ptr_alternate, 1U); @@ -1874,8 +2141,11 @@ usbStatus USBH_DeviceRequest_GetInterface (uint8_t device, uint8_t index, uint8_ } } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) - if (status != usbOK) { + if (status == usbOK) { + EvrUSBH_Core_RequestGetInterface(device, index, *ptr_alternate); + } else { EvrUSBH_Core_RequestGetInterfaceFailed(device, index, status); } #endif @@ -1895,10 +2165,16 @@ usbStatus USBH_DeviceRequest_SetInterface (uint8_t device, uint8_t index, uint8_ EvrUSBH_Core_RequestSetInterface(device, index, alternate); + // Check parameters + if (device >= usbh_dev_num) { + status = usbInvalidParameter; + goto exit; + } + ctrl = usbh_dev[device].ctrl; status = CheckController (ctrl); if (status == usbOK) { - status = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_sp), 8U); + status = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_sp, 8U); if (status == usbOK) { PREPARE_SETUP_PACKET_DATA ( ptr_sp, USB_REQUEST_HOST_TO_DEVICE, USB_REQUEST_STANDARD, USB_REQUEST_TO_INTERFACE, USB_REQUEST_SET_INTERFACE, alternate, index, 0U) status = USBH_ControlTransfer (device, ptr_sp, NULL, 0U); @@ -1909,6 +2185,7 @@ usbStatus USBH_DeviceRequest_SetInterface (uint8_t device, uint8_t index, uint8_ } } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_Core_RequestSetInterfaceFailed(device, index, alternate, status); @@ -1928,11 +2205,18 @@ usbStatus USBH_DeviceRequest_SynchFrame (uint8_t device, uint8_t index, uint8_t usbStatus status; usbStatus mstatus; + // Check parameters + if ((device >= usbh_dev_num) || + (ptr_frame_number == NULL)) { + status = usbInvalidParameter; + goto exit; + } + ctrl = usbh_dev[device].ctrl; status = CheckController (ctrl); if (status == usbOK) { if (ptr_frame_number != NULL) { - status = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_sp), 8U); + status = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_sp, 8U); if (status == usbOK) { PREPARE_SETUP_PACKET_DATA ( ptr_sp, USB_REQUEST_DEVICE_TO_HOST, USB_REQUEST_STANDARD, USB_REQUEST_TO_DEVICE, USB_REQUEST_SYNC_FRAME, 0U, index, 2U) status = USBH_ControlTransfer (device, ptr_sp, ptr_frame_number, 2U); @@ -1946,9 +2230,10 @@ usbStatus USBH_DeviceRequest_SynchFrame (uint8_t device, uint8_t index, uint8_t } } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status == usbOK) { - EvrUSBH_Core_RequestSynchFrame (device, index, ((uint16_t)(*(ptr_frame_number+1U))) | ((uint16_t)*ptr_frame_number)); + EvrUSBH_Core_RequestSynchFrame(device, index, ((uint16_t)(*(ptr_frame_number+1U))) | ((uint16_t)*ptr_frame_number)); } else { EvrUSBH_Core_RequestSynchFrameFailed(device, index, status); } @@ -1968,6 +2253,15 @@ USBH_PIPE *USBH_GetFree_PIPE (uint8_t ctrl) { uint32_t max_pipe; uint32_t i; + if (CheckController (ctrl) != usbOK) { + return NULL; + } + + if ((usbh_pipe_ptr[ctrl] == NULL) || + (usbh_pipe_num_ptr[ctrl] == NULL)) { + return NULL; + } + ptr_pipe = usbh_pipe_ptr[ctrl]; max_pipe = *usbh_pipe_num_ptr[ctrl]; @@ -1999,7 +2293,12 @@ void *USBH_Get_Thread_ID_of_Pipe (uint8_t ctrl, ARM_USBH_PIPE_HANDLE hw_handle) return NULL; } - ptr_pipe = usbh_pipe_ptr[ctrl]; + ptr_pipe = usbh_pipe_ptr[ctrl]; + if ((usbh_pipe_ptr[ctrl] == NULL) || + (usbh_pipe_num_ptr[ctrl] == NULL)) { + return NULL; + } + max_pipe = *usbh_pipe_num_ptr[ctrl]; for (i = 0U; i < max_pipe; i++) { @@ -2031,6 +2330,11 @@ USBH_PipeEvent_t USBH_Get_PipeCallback_of_Pipe (uint8_t ctrl, ARM_USBH_PIPE_HAND } ptr_pipe = usbh_pipe_ptr[ctrl]; + if ((usbh_pipe_ptr[ctrl] == NULL) || + (usbh_pipe_num_ptr[ctrl] == NULL)) { + return NULL; + } + max_pipe = *usbh_pipe_num_ptr[ctrl]; for (i = 0U; i < max_pipe; i++) { @@ -2049,13 +2353,17 @@ USBH_PipeEvent_t USBH_Get_PipeCallback_of_Pipe (uint8_t ctrl, ARM_USBH_PIPE_HAND usbStatus USBH_MemoryInitialize (uint8_t ctrl) { usbStatus status; - status = CheckController (ctrl); - if (status == usbOK) { - if (USBH_MemoryInitializeLib (ctrl) == false) { - status = usbMemoryError; - } + // Check parameters + status = CheckController(ctrl); + if (status != usbOK) { + goto exit; } + if (USBH_MemoryInitializeLib(ctrl) != true) { + status = usbMemoryError; + } + +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status == usbOK) { EvrUSBH_Core_MemInit(ctrl); @@ -2072,13 +2380,17 @@ usbStatus USBH_MemoryInitialize (uint8_t ctrl) { usbStatus USBH_MemoryUninitialize (uint8_t ctrl) { usbStatus status; - status = CheckController (ctrl); - if (status == usbOK) { - if (USBH_MemoryUninitializeLib (ctrl) == false) { - status = usbMemoryError; - } + // Check parameters + status = CheckController(ctrl); + if (status != usbOK) { + goto exit; + } + + if (USBH_MemoryUninitializeLib(ctrl) != true) { + status = usbMemoryError; } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status == usbOK) { EvrUSBH_Core_MemUninit(ctrl); @@ -2098,22 +2410,25 @@ usbStatus USBH_MemoryAllocate (uint8_t ctrl, uint8_t **ptr, uint32_t size) { uint8_t *mem; usbStatus status; - mem = NULL; + // Check parameters + status = CheckController(ctrl); + if (status != usbOK) { + goto exit; + } + if ((ptr == NULL) || (size == 0U)) { + status = usbInvalidParameter; + goto exit; + } - status = CheckController (ctrl); - if (status == usbOK) { - if (ptr != NULL) { - mem = USBH_MemoryAllocateLib (ctrl, size); - if (mem != NULL) { - *ptr = mem; - } else { - status = usbMemoryError; - } - } else { - status = usbInvalidParameter; - } + mem = USBH_MemoryAllocateLib(ctrl, size); + if (mem == NULL) { + status = usbMemoryError; + goto exit; } + *ptr = mem; + +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status == usbOK) { EvrUSBH_Core_MemAlloc(ctrl, (const uint8_t *)mem, size); @@ -2131,17 +2446,21 @@ usbStatus USBH_MemoryAllocate (uint8_t ctrl, uint8_t **ptr, uint32_t size) { usbStatus USBH_MemoryFree (uint8_t ctrl, uint8_t *ptr) { usbStatus status; + // Check parameters status = CheckController (ctrl); - if (status == usbOK) { - if (ptr != NULL) { - if (USBH_MemoryFreeLib (ctrl, ptr) == false) { - status = usbMemoryError; - } - } else { - status = usbInvalidParameter; - } + if (status != usbOK) { + goto exit; + } + if (ptr == NULL) { + status = usbInvalidParameter; + goto exit; + } + + if (USBH_MemoryFreeLib(ctrl, ptr) != true) { + status = usbMemoryError; } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status == usbOK) { EvrUSBH_Core_MemFree(ctrl, (const uint8_t *)ptr); @@ -2154,15 +2473,26 @@ usbStatus USBH_MemoryFree (uint8_t ctrl, uint8_t *ptr) { /// \brief Try to recover USB Device (reset and re-enumerate) /// \param[in] ptr_dev pointer to USB Device instance. -/// \param[in] port port index (0 .. 127). /// \return status code that indicates the execution status of the function as defined with \ref usbStatus. usbStatus USBH_RecoverDevice (USBH_DEV *ptr_dev) { USBH_HC *ptr_hc; uint32_t event; uint8_t ctrl, port; + usbStatus status; + + if (ptr_dev == NULL) { + return usbInvalidParameter; + } - ctrl = ptr_dev->ctrl; - port = ptr_dev->hub_port; + ctrl = ptr_dev->ctrl; + status = CheckController(ctrl); + if (status != usbOK) { + return status; + } + port = ptr_dev->hub_port; + if (port >= 16U) { + return usbControllerError; + } ptr_hc = &usbh_hc[ctrl]; ptr_dev->recovery_thread_id = USBH_ThreadGetHandle (); @@ -2217,7 +2547,7 @@ void USBH_ConnectDebounce (void *arg){ ptr_hc->debounce_max_countdown--; } - for (port = 0U; port < 15U; port++) { + for (port = 0U; port < 16U; port++) { if (((port_mask >> port) == 0U) || ((ptr_hc->port_debounce >> port) == 0U)) { break; } @@ -2235,7 +2565,7 @@ void USBH_ConnectDebounce (void *arg){ return; } if (ptr_hc->debounce_countdown == 0U) { - for (port = 0U; port < 15U; port++) { + for (port = 0U; port < 16U; port++) { if (((port_mask >> port) == 0U) || ((ptr_hc->port_debounce >> port) == 0U)) { break; } @@ -2273,6 +2603,7 @@ static usbStatus CheckController (uint8_t ctrl) { /// \return status code that indicates the execution status of the function as defined with \ref usbStatus. static usbStatus CheckDeviceInstanceConfigured (uint8_t instance) { + if (instance >= usbh_dev_num) { return usbDeviceError; } if (usbh_dev[instance].dev_addr == 0U) { return usbDeviceError; } if (usbh_dev[instance].state.configured == 0U) { return usbDeviceError; } if (usbh_dev[instance].vid == 0U) { return usbDeviceError; } @@ -2286,6 +2617,7 @@ static usbStatus CheckDeviceInstanceConfigured (uint8_t instance) { /// \return status code that indicates the execution status of the function as defined with \ref usbStatus. static usbStatus CheckDeviceInstance (uint8_t instance) { + if (instance >= usbh_dev_num) { return usbDeviceError; } if (usbh_dev[instance].dev_addr == 0U) { return usbDeviceError; } if (usbh_dev[instance].state.configured == 0U) { return usbDeviceError; } if (usbh_dev[instance].state.initialized == 0U) { return usbDeviceError; } @@ -2339,30 +2671,36 @@ static usbStatus USBH_DefaultPipeCreate (uint8_t ctrl) { uint32_t hw_handle; usbStatus status; - status = CheckController (ctrl); - if (status == usbOK) { - ptr_pipe = USBH_GetFree_PIPE (ctrl); - if (ptr_pipe != NULL) { - hw_handle = USBH_DriverPipeCreate (ctrl, 0U, ARM_USB_SPEED_LOW, 0U, 0U, 0U, USB_ENDPOINT_TYPE_CONTROL, 8U, 0U); - if (hw_handle != 0U) { - ptr_pipe->hw_handle = hw_handle; - ptr_pipe->bEndpointAddress = 0U; - ptr_pipe->bmAttributes = USB_ENDPOINT_TYPE_CONTROL; - ptr_pipe->wMaxPacketSize = 8U; - ptr_pipe->bInterval = 0U; - ptr_pipe->device = 255U; - ptr_pipe->locked = 0U; - ptr_pipe->transfer_active = 0U; - ptr_pipe->transferred = 0U; - usbh_hc[ctrl].def_pipe_hndl = (USBH_PIPE_HANDLE)ptr_pipe; - } else { - status = usbDriverError; - } - } else { - status = usbDriverError; - } + // Check parameters + status = CheckController(ctrl); + if (status != usbOK) { + return status; } + ptr_pipe = USBH_GetFree_PIPE(ctrl); + if (ptr_pipe == NULL) { + status = usbDriverError; + goto exit; + } + + hw_handle = USBH_DriverPipeCreate(ctrl, 0U, ARM_USB_SPEED_LOW, 0U, 0U, 0U, USB_ENDPOINT_TYPE_CONTROL, 8U, 0U); + if (hw_handle == 0U) { + status = usbDriverError; + goto exit; + } + + ptr_pipe->bEndpointAddress = 0U; + ptr_pipe->bmAttributes = USB_ENDPOINT_TYPE_CONTROL; + ptr_pipe->wMaxPacketSize = 8U; + ptr_pipe->bInterval = 0U; + ptr_pipe->device = 255U; + ptr_pipe->locked = 0U; + ptr_pipe->transfer_active = 0U; + ptr_pipe->transferred = 0U; + usbh_hc[ctrl].def_pipe_hndl = (USBH_PIPE_HANDLE)ptr_pipe; + ptr_pipe->hw_handle = hw_handle; + +exit: return status; } @@ -2376,22 +2714,32 @@ static usbStatus USBH_DefaultPipeDelete (uint8_t ctrl) { ptr_pipe = NULL; status = CheckController (ctrl); - if (status == usbOK) { - ptr_pipe = (USBH_PIPE *)usbh_hc[ctrl].def_pipe_hndl; - EvrUSBH_Core_PipeDelete((USBH_PIPE_HANDLE)ptr_pipe); - if (ptr_pipe != NULL) { - status = USBH_DriverPipeTransferAbort (ctrl, ptr_pipe->hw_handle); - if (status == usbOK) { - status = USBH_DriverPipeDelete (ctrl, ptr_pipe->hw_handle); - if (status == usbOK) { - memset ((void *)ptr_pipe, 0, sizeof(USBH_PIPE)); - } - } - } else { - status = usbControllerError; + if (status != usbOK) { + goto exit; + } + + ptr_pipe = (USBH_PIPE *)usbh_hc[ctrl].def_pipe_hndl; + EvrUSBH_Core_PipeDelete((USBH_PIPE_HANDLE)ptr_pipe); + if (ptr_pipe == NULL) { + status = usbControllerError; + goto exit; + } + + if ((ptr_pipe->locked != 0U) || (ptr_pipe->transfer_active != 0U)) { + status = USBH_PipeAbort((USBH_PIPE_HANDLE)ptr_pipe); + if (status != usbOK) { + goto exit; } } + status = USBH_DriverPipeDelete(ctrl, ptr_pipe->hw_handle); + if (status != usbOK) { + goto exit; + } + + memset((void *)ptr_pipe, 0, sizeof(USBH_PIPE)); + +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_Core_PipeDeleteFailed((USBH_PIPE_HANDLE)ptr_pipe, status); @@ -2437,29 +2785,49 @@ static usbStatus USBH_PipeDoPing (USBH_PIPE_HANDLE pipe_hndl) { uint32_t event; ARM_USBH_EP_HANDLE hw_handle; uint8_t ctrl; - uint8_t device; usbStatus status; + // Check parameters ptr_pipe = (USBH_PIPE *)pipe_hndl; - status = CheckPipe (ptr_pipe); + status = CheckPipe(ptr_pipe); if (status != usbOK) { - return status; + goto exit; } - device = ptr_pipe->device; - ptr_dev = &usbh_dev[device]; - ctrl = ptr_dev->ctrl; - ptr_hc = &usbh_hc[ctrl]; - hw_handle = ptr_pipe->hw_handle; - + if (ptr_pipe->device >= usbh_dev_num) { + status = usbDeviceError; + goto exit; + } if (ptr_pipe->thread_id != NULL) { status = usbDriverBusy; return status; } + + ptr_dev = &usbh_dev[ptr_pipe->device]; + if (ptr_dev == NULL) { + status = usbDeviceError; + goto exit; + } + ctrl = ptr_dev->ctrl; + status = CheckController(ctrl); + if (status != usbOK) { + goto exit; + } + ptr_hc = &usbh_hc[ctrl]; + if (ptr_hc == NULL) { + status = usbControllerError; + goto exit; + } + hw_handle = ptr_pipe->hw_handle; + if (hw_handle == 0U) { + status = usbDriverError; + goto exit; + } + ptr_pipe->thread_id = USBH_ThreadGetHandle(); for (;;) { - status = USBH_DriverPipeTransfer (ctrl, hw_handle, ARM_USBH_PACKET_PING, NULL, 0U); + status = USBH_DriverPipeTransfer(ctrl, hw_handle, ARM_USBH_PACKET_PING, NULL, 0U); if (status != usbOK) { // If transfer failed clear EP to TID and exit goto done; } @@ -2475,6 +2843,10 @@ static usbStatus USBH_PipeDoPing (USBH_PIPE_HANDLE pipe_hndl) { goto done; } } else { + if ((ptr_dev->hub_port == 0U) || (ptr_dev->hub_port >= 16U)) { + status = usbTransferError; + goto done; + } if ((ptr_hc->port_event[ptr_dev->hub_port - 1U]) != 0U) { status = usbTransferError; goto done; @@ -2510,11 +2882,12 @@ static usbStatus USBH_PipeDoPing (USBH_PIPE_HANDLE pipe_hndl) { } abort_and_done: - (void)USBH_DriverPipeTransferAbort (ctrl, hw_handle); + (void)USBH_DriverPipeTransferAbort(ctrl, hw_handle); done: ptr_pipe->thread_id = NULL; +exit: return status; } @@ -2528,7 +2901,7 @@ static usbStatus USBH_PipeSendSetup (USBH_PIPE_HANDLE pipe_hndl, const USB_SETUP USBH_PIPE *ptr_pipe; uint32_t event; ARM_USBH_EP_HANDLE hw_handle; - uint8_t *ptr_data; + uint8_t *ptr_data = NULL; uint8_t ctrl; bool alloc_mem; usbStatus status; @@ -2540,19 +2913,44 @@ static usbStatus USBH_PipeSendSetup (USBH_PIPE_HANDLE pipe_hndl, const USB_SETUP } ptr_pipe = (USBH_PIPE *)pipe_hndl; - status = CheckPipe (ptr_pipe); + status = CheckPipe(ptr_pipe); if (status != usbOK) { - return status; + goto exit; } if (ptr_pipe->locked != 0U) { status = usbDriverBusy; - return status; + goto exit; + } + if (ptr_pipe->thread_id != NULL) { + status = usbDriverBusy; + goto exit; } - ptr_dev = &usbh_dev[ptr_pipe->device]; - ctrl = ptr_dev->ctrl; - ptr_hc = &usbh_hc[ctrl]; - hw_handle = ptr_pipe->hw_handle; + if (ptr_pipe->device >= usbh_dev_num) { + status = usbDeviceError; + goto exit; + } + + ptr_dev = &usbh_dev[ptr_pipe->device]; + if (ptr_dev == NULL) { + status = usbDeviceError; + goto exit; + } + ctrl = ptr_dev->ctrl; + status = CheckController(ctrl); + if (status != usbOK) { + goto exit; + } + ptr_hc = &usbh_hc[ctrl]; + if (ptr_hc == NULL) { + status = usbControllerError; + goto exit; + } + hw_handle = ptr_pipe->hw_handle; + if (hw_handle == 0U) { + status = usbDriverError; + goto exit; + } ptr_pipe->locked = 1U; @@ -2560,27 +2958,23 @@ static usbStatus USBH_PipeSendSetup (USBH_PIPE_HANDLE pipe_hndl, const USB_SETUP alloc_mem = !USBH_MemoryIsInPool (ctrl, (const uint8_t *)setup_packet); if (alloc_mem) { // If memory for USB must be relocated, allocate 8 bytes for SETUP packet - mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_data), 8U); + mstatus = USBH_MemoryAllocate (ctrl, &ptr_data, 8U); if (mstatus != usbOK) { status = usbMemoryError; - goto exit; + goto done; } memcpy((void *)ptr_data, (const void *)setup_packet, 8U); // Copy SETUP data to allocated buffer } else { // If memory is in pool it is ok - ptr_data = (uint8_t *)((uint32_t)setup_packet); + ptr_data = (uint8_t *)(uintptr_t)setup_packet; } } else { // If memory relocation is not enabled alloc_mem = false; - ptr_data = (uint8_t *)((uint32_t)setup_packet); + ptr_data = (uint8_t *)(uintptr_t)setup_packet; } // Send Setup Packet - if (ptr_pipe->thread_id != NULL) { - status = usbDriverBusy; - goto mem_free_and_exit; - } ptr_pipe->thread_id = USBH_ThreadGetHandle(); - status = USBH_DriverPipeTransfer (ctrl, hw_handle, ARM_USBH_PACKET_SETUP, (uint8_t *)ptr_data, 8U); + status = USBH_DriverPipeTransfer(ctrl, hw_handle, ARM_USBH_PACKET_SETUP, (uint8_t *)ptr_data, 8U); if (status != usbOK) { // If transfer failed clear EP to TID and exit goto done; } @@ -2598,6 +2992,10 @@ static usbStatus USBH_PipeSendSetup (USBH_PIPE_HANDLE pipe_hndl, const USB_SETUP goto done; } } else { + if ((ptr_dev->hub_port == 0U) || (ptr_dev->hub_port >= 16U)) { + status = usbTransferError; + goto done; + } if ((ptr_hc->port_event[ptr_dev->hub_port - 1U]) != 0U) { status = usbTransferError; goto done; @@ -2634,14 +3032,13 @@ static usbStatus USBH_PipeSendSetup (USBH_PIPE_HANDLE pipe_hndl, const USB_SETUP } abort_and_done: - (void)USBH_DriverPipeTransferAbort (ctrl, hw_handle); + (void)USBH_DriverPipeTransferAbort(ctrl, hw_handle); done: ptr_pipe->thread_id = NULL; ptr_pipe->locked = 0U; -mem_free_and_exit: - if (alloc_mem) { + if ((alloc_mem) && (ptr_data != NULL)) { mstatus = USBH_MemoryFree (ctrl, (uint8_t *)ptr_data); if (mstatus != usbOK) { status = mstatus; @@ -2696,9 +3093,10 @@ static usbStatus USBH_PrepareEnumerateDevice (uint8_t ctrl, uint8_t port, uint8_ // Get Device Descriptor (8 or 64 bytes) to determine maximum packet size // and set device max packet size - mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_dev_desc), 64U); + mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_dev_desc, 64U); if (mstatus != usbOK) { - return mstatus; + status = mstatus; + goto exit; } status = USBH_DeviceRequest_GetDescriptor (device, USB_REQUEST_TO_DEVICE, USB_DEVICE_DESCRIPTOR_TYPE, 0U, 0U, (uint8_t *)ptr_dev_desc, 64U); if (status == usbOK) { @@ -2709,6 +3107,11 @@ static usbStatus USBH_PrepareEnumerateDevice (uint8_t ctrl, uint8_t port, uint8_ status = mstatus; } +exit: + if (status != usbOK) { + usbh_dev[device].state.in_use = 0U; + } + return status; } @@ -2769,7 +3172,7 @@ static usbStatus USBH_EnumerateDevice (uint8_t ctrl, uint8_t port, uint8_t speed (void)USBH_Delay (50U); // Delay 50 ms (max allowed) // 2. USB bus: Get Device Descriptor - mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_dev_desc), 18U); + mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_dev_desc, 64U); if (mstatus != usbOK) { return mstatus; } @@ -2781,13 +3184,19 @@ static usbStatus USBH_EnumerateDevice (uint8_t ctrl, uint8_t port, uint8_t speed } // 3. USB bus: Get 9 bytes of Configuration Descriptor to determine size of whole Configuration Descriptor - mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_cfg_desc), 9U); + mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_cfg_desc, 64U); if (mstatus != usbOK) { - return mstatus; + status = mstatus; + goto mem_free_and_exit; } - status = USBH_DeviceRequest_GetDescriptor (device, USB_REQUEST_TO_DEVICE, USB_CONFIGURATION_DESCRIPTOR_TYPE, 0U, 0U, (uint8_t *)ptr_cfg_desc, 9U); + status = USBH_DeviceRequest_GetDescriptor (device, USB_REQUEST_TO_DEVICE, USB_CONFIGURATION_DESCRIPTOR_TYPE, 0U, 0U, (uint8_t *)ptr_cfg_desc, 9U); if (status == usbOK) { - len = ptr_cfg_desc->wTotalLength; + if ((ptr_cfg_desc->wTotalLength >= (sizeof(USB_CONFIGURATION_DESCRIPTOR) + sizeof(USB_INTERFACE_DESCRIPTOR))) && (ptr_cfg_desc->wTotalLength < (UINT16_MAX - 63U))) { + len = ptr_cfg_desc->wTotalLength; + } else { + status = usbDeviceError; + goto mem_free_and_exit; + } } else { goto mem_free_and_exit; } @@ -2800,18 +3209,35 @@ static usbStatus USBH_EnumerateDevice (uint8_t ctrl, uint8_t port, uint8_t speed (void)USBH_Delay (10U); // 4. USB bus: Get Configuration Descriptor - mstatus = USBH_MemoryAllocate (ctrl,(uint8_t **)((uint32_t)&ptr_cfg_desc), len); + mstatus = USBH_MemoryAllocate (ctrl,(uint8_t **)&ptr_cfg_desc, ((len + 63U) / 64U) * 64U); if (mstatus != usbOK) { status = mstatus; goto mem_free_and_exit; } + memset(ptr_cfg_desc, 0, ((len + 63U) / 64U) * 64U); status = USBH_DeviceRequest_GetDescriptor (device, USB_REQUEST_TO_DEVICE, USB_CONFIGURATION_DESCRIPTOR_TYPE, 0U, 0U, (uint8_t *)ptr_cfg_desc, len); if (status == usbOK) { ptr_hc->dev_addr_mask[dev_addr >> 5] |= (1UL << (dev_addr & 0x1FU)); ptr_desc = (uint8_t *)ptr_cfg_desc; + if ((uint32_t)ptr_cfg_desc->bLength != sizeof(USB_CONFIGURATION_DESCRIPTOR)) { + status = usbNotConfigured; + goto mem_free_and_exit; + } ptr_desc += (uint32_t)ptr_cfg_desc->bLength; ptr_if_desc = (const USB_INTERFACE_DESCRIPTOR *)ptr_desc; + // If the current descriptor is not an interface descriptor or an interface association descriptor, the device is not supported + if ((ptr_if_desc->bDescriptorType != USB_INTERFACE_DESCRIPTOR_TYPE) && (ptr_if_desc->bDescriptorType != USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE)) { + status = usbNotConfigured; + goto mem_free_and_exit; + } + // If the current descriptor length does not match the expected size for its type, the device is not supported + if (((ptr_if_desc->bDescriptorType == USB_INTERFACE_DESCRIPTOR_TYPE) && ((uint32_t)ptr_if_desc->bLength != sizeof(USB_INTERFACE_DESCRIPTOR))) || + ((ptr_if_desc->bDescriptorType == USB_INTERFACE_ASSOCIATION_DESCRIPTOR_TYPE) && ((uint32_t)ptr_if_desc->bLength != sizeof(USB_INTERFACE_ASSOCIATION_DESCRIPTOR)))) { + status = usbNotConfigured; + goto mem_free_and_exit; + } + ptr_dev = &usbh_dev[device]; ptr_dev->ctrl = ctrl; @@ -2919,7 +3345,7 @@ static usbStatus USBH_EnumerateDevice (uint8_t ctrl, uint8_t port, uint8_t speed if (status != usbOK) { // If device is unknown or configuration of device has failed - goto exit; + goto mem_free_and_exit; } (void)USBH_Delay (10U); @@ -2927,7 +3353,7 @@ static usbStatus USBH_EnumerateDevice (uint8_t ctrl, uint8_t port, uint8_t speed // 5. USB bus: Set Configuration (1) status = USBH_DeviceRequest_SetConfiguration (device, 1U); if (status != usbOK) { - goto exit; + goto mem_free_and_exit; } (void)USBH_Delay (50U); @@ -2977,6 +3403,9 @@ static usbStatus USBH_EnumerateDevice (uint8_t ctrl, uint8_t port, uint8_t speed } mem_free_and_exit: + if (status != usbOK) { + ptr_hc->dev_addr_mask[dev_addr >> 5] &= ~(1UL << (dev_addr & 0x1FU)); + } if (ptr_cfg_desc != NULL) { mstatus = USBH_MemoryFree (ctrl, (uint8_t *)ptr_cfg_desc); if ((status == usbOK) && (mstatus != usbOK)) { @@ -2990,7 +3419,6 @@ static usbStatus USBH_EnumerateDevice (uint8_t ctrl, uint8_t port, uint8_t speed } } -exit: return status; } @@ -3010,7 +3438,7 @@ static uint8_t USBH_FindRecoveryDevice (uint8_t ctrl, uint8_t port) { return 0xFFU; // Recovery device was not found } if ((ptr_dev->ctrl == ctrl) && (ptr_dev->hub_port == port)) { - break; // Recovery device found */ + break; // Recovery device found } ptr_dev++; } @@ -3025,7 +3453,7 @@ static uint8_t USBH_FindRecoveryDevice (uint8_t ctrl, uint8_t port) { static usbStatus USBH_RecoveryEnumerateDevice (uint8_t ctrl, uint8_t port) { USBH_HC *ptr_hc; USBH_DEV *ptr_dev; - USB_DEVICE_DESCRIPTOR *ptr_dev_desc; + USB_DEVICE_DESCRIPTOR *ptr_dev_desc = NULL; USB_CONFIGURATION_DESCRIPTOR *ptr_cfg_desc; uint8_t device; uint8_t dev_addr; @@ -3043,7 +3471,7 @@ static usbStatus USBH_RecoveryEnumerateDevice (uint8_t ctrl, uint8_t port) { // Clear previously used address if (device != 0xFFU) { - ptr_hc->dev_addr_mask[usbh_dev[device].dev_addr >> 5] |= (1UL << (usbh_dev[device].dev_addr & 0x1FU)); + ptr_hc->dev_addr_mask[usbh_dev[device].dev_addr >> 5] &= ~(1UL << (usbh_dev[device].dev_addr & 0x1FU)); ptr_dev = &usbh_dev[device]; } else { return usbDeviceError; @@ -3069,7 +3497,7 @@ static usbStatus USBH_RecoveryEnumerateDevice (uint8_t ctrl, uint8_t port) { ptr_dev->dev_addr = 0U; // 1. USB bus: Get Device Descriptor - mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_dev_desc), usbh_dev[device].dev_desc_len); + mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_dev_desc, usbh_dev[device].dev_desc_len); if (mstatus != usbOK) { return mstatus; } @@ -3090,11 +3518,11 @@ static usbStatus USBH_RecoveryEnumerateDevice (uint8_t ctrl, uint8_t port) { (void)USBH_Delay (50U); // Delay 50 ms (max time) // 3. USB bus: Get Device Descriptor - mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)((uint32_t)&ptr_dev_desc), 64U); + mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_dev_desc, 64U); if (mstatus != usbOK) { return mstatus; } - status = USBH_DeviceRequest_GetDescriptor (device, USB_REQUEST_TO_DEVICE, USB_DEVICE_DESCRIPTOR_TYPE, 0U, 0U, (uint8_t *)ptr_dev_desc, usbh_dev[device].dev_desc_len); + status = USBH_DeviceRequest_GetDescriptor (device, USB_REQUEST_TO_DEVICE, USB_DEVICE_DESCRIPTOR_TYPE, 0U, 0U, (uint8_t *)ptr_dev_desc, sizeof(USB_DEVICE_DESCRIPTOR)); mstatus = USBH_MemoryFree (ctrl, (uint8_t *)ptr_dev_desc); if (mstatus != usbOK) { return mstatus; @@ -3105,7 +3533,7 @@ static usbStatus USBH_RecoveryEnumerateDevice (uint8_t ctrl, uint8_t port) { // 4. USB bus: Get Configuration Descriptor (void)USBH_Delay (50U); // Wait 50 ms for robustness - mstatus = USBH_MemoryAllocate (ctrl,(uint8_t **)((uint32_t)&ptr_cfg_desc), usbh_dev[device].cfg_desc_len); + mstatus = USBH_MemoryAllocate (ctrl,(uint8_t **)&ptr_cfg_desc, usbh_dev[device].cfg_desc_len); if (mstatus != usbOK) { return mstatus; } @@ -3231,6 +3659,11 @@ static void USBH_Engine (uint8_t ctrl, uint8_t port, uint32_t event) { return; } + // Port must be in range 0 .. 15 + if (port >= 16U) { + return; + } + ptr_hc = &usbh_hc[ctrl]; ptr_port_state = &ptr_hc->port_state[port]; @@ -3248,6 +3681,9 @@ static void USBH_Engine (uint8_t ctrl, uint8_t port, uint32_t event) { (USBH_DriverPortGetState (ctrl, port).connected == 0U))) { // if state is not connected) if ((*ptr_port_state >= 7U) && (*ptr_port_state <= 10U)){ // If state was during reset recovery device = usbh_hc[ctrl].device; + if (device >= usbh_dev_num) { + break; + } (void)USBH_ThreadFlagsSet (usbh_dev[device].recovery_thread_id, ARM_USBH_EVENT_CORE_RECOVERY_FAIL); } ptr_hc->port_con &= (uint16_t)(~(1UL << port)); @@ -3388,17 +3824,17 @@ static void USBH_Engine (uint8_t ctrl, uint8_t port, uint32_t event) { (void)USBH_Delay (100U); status = USBH_RecoveryEnumerateDevice (ctrl, port); device = usbh_hc[ctrl].device; - if (status == usbOK) { // If recovery succeeded - (void)USBH_ThreadFlagsSet (usbh_dev[device].recovery_thread_id, ARM_USBH_EVENT_CORE_RECOVERY_OK); - *ptr_port_state = 15U; // => Port active - } else if (ptr_hc->port_retry != 0U) { - ptr_hc->port_retry--; - *ptr_port_state = 7U; // Repeat 1st recovery reset - } else { // If recovery enumeration failed - if (device != 0xFFU) { + if (device < usbh_dev_num) { + if (status == usbOK) { // If recovery succeeded + (void)USBH_ThreadFlagsSet (usbh_dev[device].recovery_thread_id, ARM_USBH_EVENT_CORE_RECOVERY_OK); + *ptr_port_state = 15U; // => Port active + } else if (ptr_hc->port_retry != 0U) { + ptr_hc->port_retry--; + *ptr_port_state = 7U; // Repeat 1st recovery reset + } else { // If recovery enumeration failed (void)USBH_ThreadFlagsSet (usbh_dev[device].recovery_thread_id, ARM_USBH_EVENT_CORE_RECOVERY_FAIL); + *ptr_port_state = 15U; // => Port active } - *ptr_port_state = 15U; // => Port active } } else { *ptr_port_state = 15U; // No reset event => Port active @@ -3461,8 +3897,8 @@ void USBH_Core_Thread (void *arg) { for (;;) { port_debounce = 0U; event = USBH_ThreadFlagsWait (millisec); - if ((event & 0x8000000U) == 0U) { - for (port = 0U; port < 15U; port++) { + if ((event & 0x80000000U) == 0U) { + for (port = 0U; port < 16U; port++) { if ((port_mask >> port) == 0U) { break; } @@ -3514,7 +3950,7 @@ void USBH_Core_Thread (void *arg) { } } } else { - for (port = 0U; port < 15U; port++) { + for (port = 0U; port < 16U; port++) { if ((port_mask >> port) == 0U) { break; } diff --git a/Components/USB/Source/usbh_lib_core.h b/Components/USB/Source/usbh_lib_core.h index 457cb7bf..5138bb8f 100644 --- a/Components/USB/Source/usbh_lib_core.h +++ b/Components/USB/Source/usbh_lib_core.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Host - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbh_lib_core.h * Purpose: USB Host - Core module header file @@ -83,4 +83,6 @@ extern usbStatus USBH_RecoverDevice (USBH_DEV *ptr_dev); /// \return none. extern void USBH_ConnectDebounce (void * arg); +extern void USBH_Core_Thread (void *arg); + #endif // USBH_LIB_CORE_H_ diff --git a/Components/USB/Source/usbh_lib_drv.c b/Components/USB/Source/usbh_lib_drv.c index 6cd0b01c..c096e412 100644 --- a/Components/USB/Source/usbh_lib_drv.c +++ b/Components/USB/Source/usbh_lib_drv.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Host - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). * All rights reserved. *------------------------------------------------------------------------------ * Name: usbh_lib_drv.c @@ -9,6 +9,8 @@ #include "usbh_lib_drv.h" +#include + #include "usb_lib_debug.h" #include "usbh_config.h" #include "usbh_lib_core.h" @@ -19,6 +21,14 @@ /// \param[in] ctrl Controller Index /// \return \ref ARM_DRIVER_VERSION ARM_DRIVER_VERSION USBH_DriverGetVersion (uint8_t ctrl) { + ARM_DRIVER_VERSION ver; + + // Check parameters + if (ctrl >= usbh_hc_num) { + memset(&ver, 0, sizeof(ARM_DRIVER_VERSION)); + return ver; + } + return ((*usbh_hcd_ptr[ctrl])->GetVersion()); } @@ -26,6 +36,14 @@ ARM_DRIVER_VERSION USBH_DriverGetVersion (uint8_t ctrl) { /// \param[in] ctrl Controller Index /// \return \ref ARM_USBH_CAPABILITIES ARM_USBH_CAPABILITIES USBH_DriverGetCapabilities (uint8_t ctrl) { + ARM_USBH_CAPABILITIES caps; + + // Check parameters + if (ctrl >= usbh_hc_num) { + memset(&caps, 0, sizeof(ARM_USBH_CAPABILITIES)); + return caps; + } + return ((*usbh_hcd_ptr[ctrl])->GetCapabilities()); } @@ -38,13 +56,12 @@ usbStatus USBH_DriverInitialize (uint8_t ctrl, ARM_USBH_SignalPortEvent_t cb_por int32_t driver_status; uint8_t retry; - retry = 3U; - do { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); - retry--; - } while (retry != 0U); - if (retry == 0U) { + // Check parameters + if (ctrl >= usbh_hc_num) { + return usbInvalidParameter; + } + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return usbDriverBusy; } @@ -74,13 +91,12 @@ usbStatus USBH_DriverUninitialize (uint8_t ctrl) { int32_t driver_status; uint8_t retry; - retry = 3U; - do { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); - retry--; - } while (retry != 0U); - if (retry == 0U) { + // Check parameters + if (ctrl >= usbh_hc_num) { + return usbInvalidParameter; + } + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return usbDriverBusy; } @@ -111,13 +127,12 @@ usbStatus USBH_DriverPowerControl (uint8_t ctrl, ARM_POWER_STATE state) { int32_t driver_status; uint8_t retry; - retry = 3U; - do { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); - retry--; - } while (retry != 0U); - if (retry == 0U) { + // Check parameters + if (ctrl >= usbh_hc_num) { + return usbInvalidParameter; + } + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return usbDriverBusy; } @@ -151,13 +166,13 @@ usbStatus USBH_DriverPortVbusOnOff (uint8_t ctrl, uint8_t port, bool vbus) { int32_t driver_status; uint8_t retry; - retry = 3U; - do { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); - retry--; - } while (retry != 0U); - if (retry == 0U) { + // Check parameters + if ((ctrl >= usbh_hc_num) || + (port >= 16U)) { + return usbInvalidParameter; + } + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return usbDriverBusy; } @@ -188,13 +203,13 @@ usbStatus USBH_DriverPortReset (uint8_t ctrl, uint8_t port) { int32_t driver_status; uint8_t retry; - retry = 3U; - do { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); - retry--; - } while (retry != 0U); - if (retry == 0U) { + // Check parameters + if ((ctrl >= usbh_hc_num) || + (port >= 16U)) { + return usbInvalidParameter; + } + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return usbDriverBusy; } @@ -226,13 +241,13 @@ usbStatus USBH_DriverPortSuspend (uint8_t ctrl, uint8_t port) { int32_t driver_status; uint8_t retry; - retry = 3U; - do { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); - retry--; - } while (retry != 0U); - if (retry == 0U) { + // Check parameters + if ((ctrl >= usbh_hc_num) || + (port >= 16U)) { + return usbInvalidParameter; + } + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return usbDriverBusy; } @@ -263,13 +278,13 @@ usbStatus USBH_DriverPortResume (uint8_t ctrl, uint8_t port) { int32_t driver_status; uint8_t retry; - retry = 3U; - do { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); - retry--; - } while (retry != 0U); - if (retry == 0U) { + // Check parameters + if ((ctrl >= usbh_hc_num) || + (port >= 16U)) { + return usbInvalidParameter; + } + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return usbDriverBusy; } @@ -299,6 +314,14 @@ usbStatus USBH_DriverPortResume (uint8_t ctrl, uint8_t port) { ARM_USBH_PORT_STATE USBH_DriverPortGetState (uint8_t ctrl, uint8_t port) { ARM_USBH_PORT_STATE port_state; + memset(&port_state, 0, sizeof(port_state)); + + // Check parameters + if ((ctrl >= usbh_hc_num) || + (port >= 16U)) { + return port_state; + } + port_state = (*usbh_hcd_ptr[ctrl])->PortGetState (port); if ((usbh_hc[ctrl].port_discon & (1UL << port)) != 0U) { port_state.connected = 0U; @@ -323,15 +346,13 @@ ARM_USBH_PORT_STATE USBH_DriverPortGetState (uint8_t ctrl, uint8_t port) { /// \return Pipe Handle \ref ARM_USBH_PIPE_HANDLE ARM_USBH_PIPE_HANDLE USBH_DriverPipeCreate (uint8_t ctrl, uint8_t dev_addr, uint8_t dev_speed, uint8_t hub_addr, uint8_t hub_port, uint8_t ep_addr, uint8_t ep_type, uint16_t ep_max_packet_size, uint8_t ep_interval) { ARM_USBH_PIPE_HANDLE pipe_hndl; - uint8_t retry; - retry = 3U; - do { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); - retry--; - } while (retry != 0U); - if (retry == 0U) { + // Check parameters + if (ctrl >= usbh_hc_num) { + return 0U; + } + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return 0U; } @@ -362,13 +383,12 @@ usbStatus USBH_DriverPipeModify (uint8_t ctrl, ARM_USBH_PIPE_HANDLE pipe_hndl, u int32_t driver_status; uint8_t retry; - retry = 3U; - do { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); - retry--; - } while (retry != 0U); - if (retry == 0U) { + // Check parameters + if (ctrl >= usbh_hc_num) { + return usbInvalidParameter; + } + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return usbDriverBusy; } @@ -399,12 +419,12 @@ usbStatus USBH_DriverPipeDelete (uint8_t ctrl, ARM_USBH_PIPE_HANDLE pipe_hndl) { int32_t driver_status; uint8_t retry; - retry = 3U; - while (retry-- != 0U) { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); + // Check parameters + if (ctrl >= usbh_hc_num) { + return usbInvalidParameter; } - if (retry == 0U) { + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return usbDriverBusy; } @@ -435,13 +455,12 @@ usbStatus USBH_DriverPipeReset (uint8_t ctrl, ARM_USBH_PIPE_HANDLE pipe_hndl) { int32_t driver_status; uint8_t retry; - retry = 3U; - do { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); - retry--; - } while (retry != 0U); - if (retry == 0U) { + // Check parameters + if (ctrl >= usbh_hc_num) { + return usbInvalidParameter; + } + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return usbDriverBusy; } @@ -475,13 +494,12 @@ usbStatus USBH_DriverPipeTransfer (uint8_t ctrl, ARM_USBH_PIPE_HANDLE pipe_hndl, int32_t driver_status; uint8_t retry; - retry = 3U; - do { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); - retry--; - } while (retry != 0U); - if (retry == 0U) { + // Check parameters + if (ctrl >= usbh_hc_num) { + return usbInvalidParameter; + } + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return usbDriverBusy; } @@ -514,15 +532,13 @@ usbStatus USBH_DriverPipeTransfer (uint8_t ctrl, ARM_USBH_PIPE_HANDLE pipe_hndl, /// \return number of successfully transferred data bytes uint32_t USBH_DriverPipeTransferGetResult (uint8_t ctrl, ARM_USBH_PIPE_HANDLE pipe_hndl) { uint32_t val; - uint8_t retry; - retry = 3U; - do { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); - retry--; - } while (retry != 0U); - if (retry == 0U) { + // Check parameters + if (ctrl >= usbh_hc_num) { + return 0U; + } + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return 0U; } @@ -543,13 +559,12 @@ usbStatus USBH_DriverPipeTransferAbort (uint8_t ctrl, ARM_USBH_PIPE_HANDLE pipe_ int32_t driver_status; uint8_t retry; - retry = 3U; - do { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); - retry--; - } while (retry != 0U); - if (retry == 0U) { + // Check parameters + if (ctrl >= usbh_hc_num) { + return usbInvalidParameter; + } + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return usbDriverBusy; } @@ -577,15 +592,13 @@ usbStatus USBH_DriverPipeTransferAbort (uint8_t ctrl, ARM_USBH_PIPE_HANDLE pipe_ /// \return Frame Number uint16_t USBH_DriverGetFrameNumber (uint8_t ctrl) { uint16_t val; - uint8_t retry; - retry = 3U; - do { - if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 100U) == 0) { break; } - (void)USBH_Delay (10U); - retry--; - } while (retry != 0U); - if (retry == 0U) { + // Check parameters + if (ctrl >= usbh_hc_num) { + return 0U; + } + + if (USBH_SemaphoreAcquire (usbh_driver_semaphore_id[ctrl], 300U) != 0) { return 0U; } @@ -606,6 +619,12 @@ uint16_t USBH_DriverGetFrameNumber (uint8_t ctrl) { void USBH_SignalPortEvent (uint8_t ctrl, uint8_t port, uint32_t event) { USBH_HC *ptr_hc; + // Check parameters + if ((ctrl >= usbh_hc_num) || + (port >= 16U)) { + return; + } + ptr_hc = &usbh_hc[ctrl]; if (((event & ARM_USBH_EVENT_DISCONNECT) != 0U) && ((usbh_hc[ctrl].port_con & (1UL << port)) != 0U) && (usbh_hc[ctrl].port_rst != (port + 1U))) { diff --git a/Components/USB/Source/usbh_lib_hid.c b/Components/USB/Source/usbh_lib_hid.c index 12b8b2ec..95a66760 100644 --- a/Components/USB/Source/usbh_lib_hid.c +++ b/Components/USB/Source/usbh_lib_hid.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Host - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). * All rights reserved. *------------------------------------------------------------------------------ * Name: usbh_lib_hid.c @@ -55,7 +55,7 @@ uint8_t USBH_HID_GetDevice (uint8_t instance) { if (usbh_hid[instance].ptr_dev == usbh_dev) { return 0U; } else { - device = (uint8_t)(((uint32_t)usbh_hid[instance].ptr_dev - (uint32_t)usbh_dev) / sizeof (USBH_DEV)); + device = (uint8_t)(((uintptr_t)usbh_hid[instance].ptr_dev - (uintptr_t)usbh_dev) / sizeof (USBH_DEV)); } } else { device = 0xFFU; @@ -94,32 +94,42 @@ usbStatus USBH_HID_GetDeviceStatus (uint8_t instance) { /// - value < 0 : error occurred, -value is execution status as defined with \ref usbStatus int32_t USBH_HID_Read (uint8_t instance, uint8_t *buf, int32_t len) { USBH_HID *ptr_hid; - int32_t num; + int32_t num = 0; usbStatus status; EvrUSBH_HID_Read(instance, len); - num = 0; + if (buf == NULL) { + status = usbInvalidParameter; + goto exit; + } + if (len < 0) { + status = usbInvalidParameter; + goto exit; + } + if (len > UINT16_MAX) { + status = usbInvalidParameter; + goto exit; + } status = CheckInstance (instance); - if (status == usbOK) { - if (buf != NULL) { - ptr_hid = &usbh_hid[instance]; + if (status != usbOK) { + goto exit; + } - num = (int32_t)ptr_hid->hid_in_data_len; - if (num > len) { - num = len; - } + ptr_hid = &usbh_hid[instance]; - if (num != 0) { - memcpy ((void *)buf, (void *)ptr_hid->ptr_int_in_buf, (uint32_t)num); - ptr_hid->hid_in_data_len = 0U; - } - } else { - status = usbInvalidParameter; - } + num = (int32_t)ptr_hid->hid_in_data_len; + if (num > len) { + num = len; } + if (num != 0) { + memcpy ((void *)buf, (void *)ptr_hid->ptr_int_in_buf, (uint32_t)num); + ptr_hid->hid_in_data_len = 0U; + } + +exit: if (status != usbOK) { EvrUSBH_HID_ReadFailed(instance, len, status); return -(int32_t)status; @@ -138,23 +148,33 @@ int32_t USBH_HID_Read (uint8_t instance, uint8_t *buf, int32_t len) { /// - value < 0 : error occurred, -value is execution status as defined with \ref usbStatus int32_t USBH_HID_Write (uint8_t instance, const uint8_t *buf, int32_t len) { usbStatus status; - int32_t num; + int32_t num = 0; EvrUSBH_HID_Write(instance, len); - num = 0; + if (buf == NULL) { + status = usbInvalidParameter; + goto exit; + } + if (len < 0) { + status = usbInvalidParameter; + goto exit; + } + if (len > UINT16_MAX) { + status = usbInvalidParameter; + goto exit; + } status = CheckInstance (instance); - if (status == usbOK) { - if (buf != NULL) { - if (USBH_HID_ReportOut (instance, buf, (uint16_t)len) == usbOK) { - num = len; - } - } else { - status = usbInvalidParameter; - } + if (status != usbOK) { + goto exit; + } + + if (USBH_HID_ReportOut (instance, buf, (uint16_t)len) == usbOK) { + num = len; } +exit: if (status != usbOK) { EvrUSBH_HID_WriteFailed(instance, len, status); return -(int32_t)status; @@ -215,7 +235,7 @@ __WEAK int USBH_HID_GetKeyboardKey (uint8_t instance) { ptr_hid->hid_in_data_len = 0U; } } else { - if ((key >= HID_USAGE_KEYBOARD_F1) || (HID_KEYBOARD_ID_TO_ASCII[key] == 0xFFU)) { + if ((key >= sizeof(HID_KEYBOARD_ID_TO_ASCII)) || (HID_KEYBOARD_ID_TO_ASCII[key] == 0xFFU)) { key = key | 0x10000UL; // bit 16. specifies if it is a non-ASCII translated HID ID } else { // Translate to ASCII @@ -247,23 +267,37 @@ __WEAK usbStatus USBH_HID_GetMouseState (uint8_t instance, usbHID_MouseState *st EvrUSBH_HID_GetMouseState(instance); + if (state == NULL) { + status = usbInvalidParameter; + goto exit; + } + status = CheckInstance (instance); - if (status == usbOK) { - ptr_hid = &usbh_hid[instance]; + if (status != usbOK) { + goto exit; + } - if (ptr_hid->hid_in_data_len != 0U) { - ptr_hid->hid_in_data_len = 0U; - if (ptr_hid->protocol == HID_PROTOCOL_MOUSE) { - ptr_hid->mouse_state.button = ptr_hid->hid_in_data_buf[0]; - x = (int8_t)ptr_hid->hid_in_data_buf[1]; - ptr_hid->mouse_state.x += (int16_t)x; - y = (int8_t)ptr_hid->hid_in_data_buf[2]; - ptr_hid->mouse_state.y += (int16_t)y; - } - } - *state = ptr_hid->mouse_state; + ptr_hid = &usbh_hid[instance]; + if (ptr_hid == NULL) { + status = usbInvalidParameter; + goto exit; + } + if (ptr_hid->protocol != HID_PROTOCOL_MOUSE) { + status = usbInvalidParameter; + goto exit; + } + + if (ptr_hid->hid_in_data_len != 0U) { + ptr_hid->hid_in_data_len = 0U; + ptr_hid->mouse_state.button = ptr_hid->hid_in_data_buf[0]; + x = (int8_t)ptr_hid->hid_in_data_buf[1]; + ptr_hid->mouse_state.x += (int16_t)x; + y = (int8_t)ptr_hid->hid_in_data_buf[2]; + ptr_hid->mouse_state.y += (int16_t)y; } + *state = ptr_hid->mouse_state; +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_HID_GetMouseStateFailed(instance, status); @@ -308,20 +342,20 @@ __WEAK void USBH_HID_DataReceived (uint8_t instance, uint32_t len) { /// - value 255 : configuration failed __WEAK uint8_t USBH_HID_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR *ptr_dev_desc, const USB_CONFIGURATION_DESCRIPTOR *ptr_cfg_desc) { USBH_DEV *ptr_dev; - USBH_HID *ptr_hid; + USBH_HID *ptr_hid = NULL; + const USB_COMMON_DESCRIPTOR *ptr_cmn_desc; const USB_INTERFACE_DESCRIPTOR *ptr_if_desc; const HID_DESCRIPTOR *ptr_hid_desc; const USB_ENDPOINT_DESCRIPTOR *ptr_ep_desc; const uint8_t *ptr_desc; USBH_PIPE_HANDLE pipe_hndl; + uint32_t cgf_desc_len; uint8_t num; uint8_t ret, idx; - usbStatus status; - status = usbOK; - ret = 255U; + ret = 255U; - if (device == 255U) { + if (device >= usbh_dev_num) { goto exit; } if (ptr_dev_desc == NULL) { @@ -350,8 +384,17 @@ __WEAK uint8_t USBH_HID_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR * ptr_hid->ptr_dev = (USBH_DEV *)ptr_dev; - ptr_desc = (const uint8_t *)ptr_cfg_desc; - ptr_desc += ptr_cfg_desc->bLength; + ptr_desc = (const uint8_t *)ptr_cfg_desc; + if (ptr_cfg_desc->bLength != sizeof(USB_CONFIGURATION_DESCRIPTOR)) { + goto exit; + } + + cgf_desc_len = (uint32_t)ptr_cfg_desc->wTotalLength; + ptr_desc += ptr_cfg_desc->bLength; + ptr_cmn_desc = (const USB_COMMON_DESCRIPTOR *)ptr_desc; + if ((ptr_cmn_desc->bLength == 0U) || (ptr_cmn_desc->bLength >= cgf_desc_len)) { + goto exit; + } ptr_if_desc = (const USB_INTERFACE_DESCRIPTOR *)ptr_desc; num = ptr_if_desc->bNumEndpoints; // Number of endpoints @@ -361,33 +404,30 @@ __WEAK uint8_t USBH_HID_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR * // HID Descriptor ptr_desc = (const uint8_t *)ptr_if_desc; - ptr_desc += ptr_if_desc->bLength; + ptr_cmn_desc = (const USB_COMMON_DESCRIPTOR *)ptr_desc; + if ((ptr_cmn_desc->bLength == 0U) || + (((uintptr_t)ptr_desc + ptr_cmn_desc->bLength) > ((uintptr_t)ptr_cfg_desc + cgf_desc_len))) { + goto exit; + } + ptr_desc += ptr_cmn_desc->bLength; ptr_hid_desc = (const HID_DESCRIPTOR *)ptr_desc; ptr_hid->report_desc_type = ptr_hid_desc->DescriptorList[0].bDescriptorType; ptr_hid->report_desc_len = ptr_hid_desc->DescriptorList[0].wDescriptorLength; + ptr_cmn_desc = (const USB_COMMON_DESCRIPTOR *)ptr_desc; + if ((ptr_cmn_desc->bLength == 0U) || + (((uintptr_t)ptr_desc + ptr_cmn_desc->bLength) > ((uintptr_t)ptr_cfg_desc + cgf_desc_len))) { + goto exit; + } + ptr_desc += ptr_cmn_desc->bLength; + // Create Pipes - ptr_desc = (const uint8_t *)ptr_hid_desc; - ptr_desc += ptr_hid_desc->bLength; - ptr_ep_desc = (const USB_ENDPOINT_DESCRIPTOR *)ptr_desc; + ptr_ep_desc = (const USB_ENDPOINT_DESCRIPTOR *)ptr_desc; while (num-- != 0U) { if ((ptr_ep_desc->bmAttributes & 3U) == USB_ENDPOINT_TYPE_INTERRUPT) { // Interrupt Endpoint - pipe_hndl = USBH_PipeCreate (device, ptr_ep_desc->bEndpointAddress, (uint8_t)((uint32_t)ptr_ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK), ptr_ep_desc->wMaxPacketSize & 0x7FFU, ptr_ep_desc->bInterval); + pipe_hndl = USBH_PipeCreate (device, ptr_ep_desc->bEndpointAddress, (uint8_t)(ptr_ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK), ptr_ep_desc->wMaxPacketSize & 0x7FFU, ptr_ep_desc->bInterval); if (pipe_hndl == 0U) { // If creation of pipe has failed delete previously created pipes - if (ptr_hid->int_in_pipe_hndl != 0U) { - status = USBH_PipeDelete (ptr_hid->int_in_pipe_hndl); - if (status != usbOK) { - goto exit; - } - } - if (ptr_hid->int_out_pipe_hndl != 0U) { - status = USBH_PipeDelete (ptr_hid->int_out_pipe_hndl); - if (status != usbOK) { - goto exit; - } - } - memset ((void *)ptr_hid, 0, sizeof (USBH_HID)); goto exit; } if ((ptr_ep_desc->bEndpointAddress & USB_ENDPOINT_DIRECTION_MASK) != 0U) { @@ -408,11 +448,26 @@ __WEAK uint8_t USBH_HID_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR * } exit: -#if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) - if (status != usbOK) { - EvrUSBH_HID_ConfigureFailed(status); + if (ret == 255U) { // If configuration failed + if (ptr_hid != NULL) { + if (ptr_hid->int_in_pipe_hndl != 0U) { + if (USBH_PipeDelete (ptr_hid->int_in_pipe_hndl) == usbOK) { + ptr_hid->int_in_pipe_hndl = 0U; + } + } + if (ptr_hid->int_out_pipe_hndl != 0U) { + if (USBH_PipeDelete (ptr_hid->int_out_pipe_hndl) == usbOK) { + ptr_hid->int_out_pipe_hndl = 0U; + } + } + if ((ptr_hid->int_in_pipe_hndl == 0U) && + (ptr_hid->int_out_pipe_hndl == 0U)) { + memset ((void *)ptr_hid, 0, sizeof (USBH_HID)); + } + } + EvrUSBH_HID_ConfigureFailed(usbClassErrorHID); } -#endif + return ret; } @@ -856,9 +911,9 @@ static usbStatus USBH_HID_ReportOut (uint8_t instance, const uint8_t *ptr_data, ptr_hid = &usbh_hid[instance]; if (ptr_hid->int_out_pipe_hndl != 0U) { - status = USBH_PipeSend (ptr_hid->int_out_pipe_hndl, (uint8_t *)((uint32_t)ptr_data), data_len); + status = USBH_PipeSend (ptr_hid->int_out_pipe_hndl, (uint8_t *)(uintptr_t)ptr_data, data_len); } else { - status = USBH_HID_SetReport (instance, 2, 0, 0, (uint8_t *)((uint32_t)ptr_data), data_len); + status = USBH_HID_SetReport (instance, 2, 0, 0, (uint8_t *)(uintptr_t)ptr_data, data_len); } exit: diff --git a/Components/USB/Source/usbh_lib_hid.h b/Components/USB/Source/usbh_lib_hid.h index fe184ec9..af11d2bb 100644 --- a/Components/USB/Source/usbh_lib_hid.h +++ b/Components/USB/Source/usbh_lib_hid.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Host - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbh_lib_hid.h * Purpose: USB Host - Human Interface Device (HID) module header file @@ -40,4 +40,6 @@ extern usbStatus USBH_HID_Initialize_Lib (uint8_t instance); /// \return status code that indicates the execution status of the function as defined with usbStatus. extern usbStatus USBH_HID_Uninitialize_Lib (uint8_t instance); +extern void USBH_HID_IntIn_Thread (void *arg); + #endif // USBH_LIB_HID_H_ diff --git a/Components/USB/Source/usbh_lib_hid_idtoascii.c b/Components/USB/Source/usbh_lib_hid_idtoascii.c index 375373dd..35885599 100644 --- a/Components/USB/Source/usbh_lib_hid_idtoascii.c +++ b/Components/USB/Source/usbh_lib_hid_idtoascii.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Host - * Copyright (c) 2004-2019 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbh_lib_hid_idtoascii.c * Purpose: USB Host - Human Interface Device Class Key mapping table @@ -11,7 +11,7 @@ // Values of 0xFFU will be translated to HID ID + 0x10000 -const unsigned char HID_KEYBOARD_ID_TO_ASCII[] = { +const unsigned char HID_KEYBOARD_ID_TO_ASCII[58] = { // Usage IDs: 0 .. 9 0xFFU, // Reserved 0xFFU, // ErrorRollOver diff --git a/Components/USB/Source/usbh_lib_hid_idtoascii.h b/Components/USB/Source/usbh_lib_hid_idtoascii.h index 84bc1810..ddee9085 100644 --- a/Components/USB/Source/usbh_lib_hid_idtoascii.h +++ b/Components/USB/Source/usbh_lib_hid_idtoascii.h @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Host - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). All rights reserved. + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). All rights reserved. *------------------------------------------------------------------------------ * Name: usbh_lib_hid_idtoascii.h * Purpose: USB Host - Human Interface Device Class Key mapping table header @@ -10,6 +10,6 @@ #ifndef USBH_LIB_HID_IDTOASCII_H_ #define USBH_LIB_HID_IDTOASCII_H_ -extern const unsigned char HID_KEYBOARD_ID_TO_ASCII[]; +extern const unsigned char HID_KEYBOARD_ID_TO_ASCII[58]; #endif // USBH_LIB_HID_IDTOASCII_H_ diff --git a/Components/USB/Source/usbh_lib_mem.c b/Components/USB/Source/usbh_lib_mem.c index 778fba7e..f0d44535 100644 --- a/Components/USB/Source/usbh_lib_mem.c +++ b/Components/USB/Source/usbh_lib_mem.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Host - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). * All rights reserved. *------------------------------------------------------------------------------ * Name: usbh_lib_mem.c @@ -23,6 +23,8 @@ /************************** Variable and Memory Definitions *******************/ +#define USBH_MIN_MEM_POOL_SIZE 64U + #ifdef __DEBUG static uint32_t mem_load [4]; /* Memory Pool Load for debugging */ static uint8_t mem_counter[4]; /* Number of Allocated Memory Blocks */ @@ -37,14 +39,23 @@ static uint8_t mem_counter[4]; /* Number of Allocated Memory Blocks */ /// \return false initialization failed bool USBH_MemoryInitializeLib (uint8_t idx) { MEMP_t *ptr_init; - uint32_t addr; + uintptr_t addr; - if ((usbh_mem_pool_ptr[idx] == NULL) || (*usbh_mem_pool_size_ptr[idx] == 0U)) { return false; } + // Check parameters + if (idx >= usbh_hc_num) { + return false; + } + if ((usbh_mem_pool_ptr[idx] == NULL) || (usbh_mem_pool_size_ptr[idx] == NULL)) { + return false; + } + if (*usbh_mem_pool_size_ptr[idx] < USBH_MIN_MEM_POOL_SIZE) { + return false; + } memset ((void *)usbh_mem_pool_ptr[idx], 0, *usbh_mem_pool_size_ptr[idx]); - ptr_init = (MEMP_t *)((uint32_t)usbh_mem_pool_ptr[idx]); - addr = (uint32_t)usbh_mem_pool_ptr[idx] + (uint32_t)(*usbh_mem_pool_size_ptr[idx]) - sizeof (struct mem *); + ptr_init = (MEMP_t *)usbh_mem_pool_ptr[idx]; + addr = (uintptr_t)usbh_mem_pool_ptr[idx] + (uintptr_t)(*usbh_mem_pool_size_ptr[idx]) - sizeof (struct mem *); ptr_init->next = (MEMP_t *)addr; ptr_init->next->next = NULL; ptr_init->len = 0U; @@ -63,6 +74,14 @@ bool USBH_MemoryInitializeLib (uint8_t idx) { /// \return false uninitialization failed bool USBH_MemoryUninitializeLib (uint8_t idx) { + // Check parameters + if (idx >= usbh_hc_num) { + return false; + } + if ((usbh_mem_pool_ptr[idx] == NULL) || (usbh_mem_pool_size_ptr[idx] == NULL)) { + return false; + } + memset ((void *)usbh_mem_pool_ptr[idx], 0, *usbh_mem_pool_size_ptr[idx]); #ifdef __DEBUG @@ -79,6 +98,14 @@ bool USBH_MemoryUninitializeLib (uint8_t idx) { /// \return size of memory pool in bytes uint32_t USBH_MemoryGetPoolSize (uint8_t idx) { + // Check parameters + if (idx >= usbh_hc_num) { + return 0U; + } + if (usbh_mem_pool_size_ptr[idx] == NULL) { + return 0U; + } + return (*usbh_mem_pool_size_ptr[idx]); } @@ -90,7 +117,22 @@ uint32_t USBH_MemoryGetPoolSize (uint8_t idx) { /// \return false is not in pool bool USBH_MemoryIsInPool (uint8_t idx, const uint8_t *ptr_mem) { - if ((ptr_mem != NULL) && (ptr_mem >= (uint8_t *)usbh_mem_pool_ptr[idx]) && (ptr_mem < ((uint8_t *)usbh_mem_pool_ptr[idx] + *usbh_mem_pool_size_ptr[idx]))) { + // Check parameters + if (idx >= usbh_hc_num) { + return false; + } + if (ptr_mem == NULL) { + return false; + } + if ((usbh_mem_pool_ptr[idx] == NULL) || (usbh_mem_pool_size_ptr[idx] == NULL)) { + return false; + } + if (*usbh_mem_pool_size_ptr[idx] < USBH_MIN_MEM_POOL_SIZE) { + return false; + } + + if (((uintptr_t)ptr_mem >= ((uintptr_t)usbh_mem_pool_ptr[idx] + sizeof(struct mem *) + sizeof(uintptr_t))) && + ((uintptr_t)ptr_mem < ((uintptr_t)usbh_mem_pool_ptr[idx] + *usbh_mem_pool_size_ptr[idx] - sizeof(uintptr_t)))) { return true; } @@ -109,16 +151,45 @@ uint8_t *USBH_MemoryAllocateLib (uint8_t idx, uint32_t sz) { uint8_t *ptr_data; uint32_t total_sz; uint32_t hole_sz; - uint32_t addr; + uintptr_t addr; + + // Check parameters + if (idx >= usbh_hc_num) { + return NULL; + } + if (sz == 0U) { + return NULL; + } + if (sz >= (UINT32_MAX - (sizeof(struct mem *) + sizeof(uintptr_t) + 3U))) { + return NULL; + } + if ((usbh_mem_pool_ptr[idx] == NULL) || (usbh_mem_pool_size_ptr[idx] == NULL)) { + return NULL; + } + if (*usbh_mem_pool_size_ptr[idx] < USBH_MIN_MEM_POOL_SIZE) { + return NULL; + } // Add the header offset to 'sz' - total_sz = sz + sizeof(struct mem *) + sizeof (uint32_t); + total_sz = sz + sizeof(struct mem *) + sizeof(uintptr_t); // Make sure that block is 4-byte aligned total_sz = (total_sz + 3U) & ~0x00000003U; - ptr_search = (MEMP_t *)((uint32_t)usbh_mem_pool_ptr[idx]); + ptr_search = (MEMP_t *)usbh_mem_pool_ptr[idx]; + if (ptr_search->next == NULL) { + // If memory pool was not initialized + return NULL; + } + for (;;) { - hole_sz = (uint32_t)(ptr_search->next) - (uint32_t)ptr_search; + if (((uintptr_t)(ptr_search->next) - (uintptr_t)ptr_search) >= UINT32_MAX) { + return NULL; + } + hole_sz = (uint32_t)((uintptr_t)(ptr_search->next) - (uintptr_t)ptr_search); + if (ptr_search->len > hole_sz) { + // Corrupted memory list + return NULL; + } hole_sz -= ptr_search->len; // Check if the hole size is big enough if (hole_sz >= total_sz) { @@ -138,7 +209,7 @@ uint8_t *USBH_MemoryAllocateLib (uint8_t idx, uint32_t sz) { ptr_data = (uint8_t *)ptr_search->content; } else { // Insert a new list element into the memory list - addr = (uint32_t)ptr_search + ptr_search->len; + addr = (uintptr_t)ptr_search + ptr_search->len; ptr_new = (MEMP_t *)addr; ptr_new->next = ptr_search->next; ptr_search->next = ptr_new; @@ -161,17 +232,27 @@ uint8_t *USBH_MemoryAllocateLib (uint8_t idx, uint32_t sz) { bool USBH_MemoryFreeLib (uint8_t idx, uint8_t *ptr_mem) { const MEMP_t *ptr_return; MEMP_t *ptr_prev, *ptr_search; - uint32_t addr; + uintptr_t addr; + // Check parameters + if (idx >= usbh_hc_num) { + return false; + } if (ptr_mem == NULL) { - return true; + return false; + } + if (USBH_MemoryIsInPool(idx, ptr_mem) == false) { + return false; } - addr = (uint32_t)ptr_mem - (sizeof(struct mem *) + sizeof(uint32_t)); + addr = (uintptr_t)ptr_mem - (sizeof(struct mem *) + sizeof(uintptr_t)); ptr_return = (MEMP_t *)addr; // Set list header - ptr_search = (MEMP_t *)((uint32_t)usbh_mem_pool_ptr[idx]); + ptr_search = (MEMP_t *)usbh_mem_pool_ptr[idx]; + if (ptr_search == NULL) { + return false; + } ptr_prev = ptr_search; while (ptr_search != ptr_return) { ptr_prev = ptr_search; @@ -180,7 +261,12 @@ bool USBH_MemoryFreeLib (uint8_t idx, uint8_t *ptr_mem) { // This is not a valid Memory Block pointer return false; } - if ((((uint32_t)(ptr_search->next)) & 3U) != 0U) { + if (((uintptr_t)ptr_search < ((uintptr_t)usbh_mem_pool_ptr[idx] + sizeof(struct mem *) + sizeof(uintptr_t))) || + ((uintptr_t)ptr_search >= ((uintptr_t)usbh_mem_pool_ptr[idx] + *usbh_mem_pool_size_ptr[idx] - sizeof(uintptr_t)))) { + // If ptr_search is not within the valid memory pool range + return false; + } + if ((((uintptr_t)(ptr_search->next)) & 3U) != 0U) { // Error Link pointer overwritten, memory corrupt return false; } @@ -189,12 +275,13 @@ bool USBH_MemoryFreeLib (uint8_t idx, uint8_t *ptr_mem) { mem_load[idx] -= ptr_search->len; mem_counter[idx]--; #endif - if (ptr_search == (MEMP_t *)((uint32_t)usbh_mem_pool_ptr[idx])) { + if (ptr_search == (MEMP_t *)usbh_mem_pool_ptr[idx]) { // First element to be set free, only set Length to 0 ptr_search->len = 0U; } else { // Discard list element ptr_prev->next = ptr_search->next; + memset(ptr_search, 0, (sizeof(struct mem *) + sizeof(uintptr_t))); } return true; diff --git a/Components/USB/Source/usbh_lib_msc.c b/Components/USB/Source/usbh_lib_msc.c index a92d873d..0739edbc 100644 --- a/Components/USB/Source/usbh_lib_msc.c +++ b/Components/USB/Source/usbh_lib_msc.c @@ -1,6 +1,6 @@ /*------------------------------------------------------------------------------ * MDK Middleware - Component ::USB:Host - * Copyright (c) 2004-2024 Arm Limited (or its affiliates). + * Copyright (c) 2004-2026 Arm Limited (or its affiliates). * All rights reserved. *------------------------------------------------------------------------------ * Name: usbh_lib_msc.c @@ -58,7 +58,7 @@ uint8_t USBH_MSC_GetDevice (uint8_t instance) { if (usbh_msc[instance].ptr_dev == usbh_dev) { return 0U; } else { - device = (uint8_t)(((uint32_t)usbh_msc[instance].ptr_dev - (uint32_t)usbh_dev) / sizeof (USBH_DEV)); + device = (uint8_t)(((uintptr_t)usbh_msc[instance].ptr_dev - (uintptr_t)usbh_dev) / sizeof (USBH_DEV)); } } else { device = 0xFFU; @@ -205,6 +205,11 @@ usbStatus USBH_MSC_ReadCapacity (uint8_t instance, uint32_t *block_count, uint32 EvrUSBH_MSC_ReadCapacity(instance); + if ((block_count == NULL) || (block_size == NULL)) { + status = usbInvalidParameter; + goto exit; + } + status = CheckInstance (instance); if (status == usbOK) { if ((block_count != NULL) && (block_size != NULL)) { @@ -215,10 +220,11 @@ usbStatus USBH_MSC_ReadCapacity (uint8_t instance, uint32_t *block_count, uint32 } } +exit: #if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) if (status != usbOK) { EvrUSBH_MSC_ReadCapacityFailed(instance, status); - } else { + } else if ((block_count != NULL) && (block_size != NULL)) { EvrUSBH_MSC_ReadCapacityDone(instance, *block_count, *block_size); } #endif @@ -232,24 +238,24 @@ usbStatus USBH_MSC_ReadCapacity (uint8_t instance, uint32_t *block_count, uint32 /// \param[in] device index of USB Device. /// \param[in] ptr_dev_desc pointer to device descriptor. /// \param[in] ptr_cfg_desc pointer to configuration descriptor. -/// \return index of configured MSC instance or configuration failed +/// \return index of configured MSC instance or configuration failed : /// - value <= 127 : index of configured MSC instance /// - value 255 : configuration failed __WEAK uint8_t USBH_MSC_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR *ptr_dev_desc, const USB_CONFIGURATION_DESCRIPTOR *ptr_cfg_desc) { USBH_DEV *ptr_dev; - USBH_MSC *ptr_msc; + USBH_MSC *ptr_msc = NULL; + const USB_COMMON_DESCRIPTOR *ptr_cmn_desc; const USB_INTERFACE_DESCRIPTOR *ptr_if_desc; const USB_ENDPOINT_DESCRIPTOR *ptr_ep_desc; const uint8_t *ptr_desc; USBH_PIPE_HANDLE pipe_hndl; + uint32_t cgf_desc_len; uint8_t num; uint8_t ret, idx; - usbStatus status; - status = usbOK; - ret = 255U; + ret = 255U; - if (device == 255U) { + if (device >= usbh_dev_num) { goto exit; } if (ptr_dev_desc == NULL) { @@ -278,8 +284,17 @@ __WEAK uint8_t USBH_MSC_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR * ptr_msc->ptr_dev = (USBH_DEV *)ptr_dev; - ptr_desc = (const uint8_t *)ptr_cfg_desc; - ptr_desc += ptr_cfg_desc->bLength; + ptr_desc = (const uint8_t *)ptr_cfg_desc; + if (ptr_cfg_desc->bLength != sizeof(USB_CONFIGURATION_DESCRIPTOR)) { + goto exit; + } + + cgf_desc_len = (uint32_t)ptr_cfg_desc->wTotalLength; + ptr_desc += ptr_cfg_desc->bLength; + ptr_cmn_desc = (const USB_COMMON_DESCRIPTOR *)ptr_desc; + if ((ptr_cmn_desc->bLength == 0U) || (ptr_cmn_desc->bLength >= cgf_desc_len)) { + goto exit; + } ptr_if_desc = (const USB_INTERFACE_DESCRIPTOR *)ptr_desc; num = ptr_if_desc->bNumEndpoints; // Number of endpoints @@ -288,30 +303,24 @@ __WEAK uint8_t USBH_MSC_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR * switch (ptr_if_desc->bInterfaceSubClass) { case MSC_SUBCLASS_SCSI: // SCSI transparent command set if (ptr_if_desc->bInterfaceProtocol == MSC_PROTOCOL_BULK_ONLY) { + ptr_desc = (const uint8_t *)ptr_if_desc; + ptr_cmn_desc = (const USB_COMMON_DESCRIPTOR *)ptr_desc; + if ((ptr_cmn_desc->bLength == 0U) || + (((uintptr_t)ptr_desc + ptr_cmn_desc->bLength) > ((uintptr_t)ptr_cfg_desc + cgf_desc_len))) { + goto exit; + } + ptr_desc += ptr_cmn_desc->bLength; + // Create Pipes - ptr_desc = (const uint8_t *)ptr_if_desc; - ptr_desc += ptr_if_desc->bLength; ptr_ep_desc = (const USB_ENDPOINT_DESCRIPTOR *)ptr_desc; while (num-- != 0U) { if ((ptr_ep_desc->bmAttributes & 3U) == USB_ENDPOINT_TYPE_BULK) { // Bulk Endpoint - pipe_hndl = USBH_PipeCreate (device, ptr_ep_desc->bEndpointAddress, ptr_ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK, ptr_ep_desc->wMaxPacketSize & 0x7FFU, ptr_ep_desc->bInterval); + pipe_hndl = USBH_PipeCreate (device, ptr_ep_desc->bEndpointAddress, (uint8_t)(ptr_ep_desc->bmAttributes & USB_ENDPOINT_TYPE_MASK), ptr_ep_desc->wMaxPacketSize & 0x7FFU, ptr_ep_desc->bInterval); if (pipe_hndl == 0U) { // If creation of pipe has failed delete previously created pipes - if (ptr_msc->bulk_in_pipe_hndl != 0U) { - status = USBH_PipeDelete (ptr_msc->bulk_in_pipe_hndl); - if (status != usbOK) { - goto exit; - } - } - if (ptr_msc->bulk_out_pipe_hndl != 0U) { - status = USBH_PipeDelete (ptr_msc->bulk_out_pipe_hndl); - if (status != usbOK) { - goto exit; - } - } - memset ((void *)ptr_msc, 0, sizeof (USBH_MSC)); goto exit; } + if ((ptr_ep_desc->bEndpointAddress & USB_ENDPOINT_DIRECTION_MASK) != 0U) { ptr_msc->bulk_in_pipe_hndl = pipe_hndl; } else { @@ -336,11 +345,27 @@ __WEAK uint8_t USBH_MSC_Configure (uint8_t device, const USB_DEVICE_DESCRIPTOR * } exit: -#if (defined(USBH_DEBUG) && (USBH_DEBUG == 1)) - if (status != usbOK) { - EvrUSBH_MSC_ConfigureFailed(status); + if (ret == 255U) { // If configuration failed + if (ptr_msc != NULL) { + // Delete previously created pipes + if (ptr_msc->bulk_in_pipe_hndl != 0U) { + if (USBH_PipeDelete (ptr_msc->bulk_in_pipe_hndl) == usbOK) { + ptr_msc->bulk_in_pipe_hndl = 0U; + } + } + if (ptr_msc->bulk_out_pipe_hndl != 0U) { + if (USBH_PipeDelete (ptr_msc->bulk_out_pipe_hndl) == usbOK) { + ptr_msc->bulk_out_pipe_hndl = 0U; + } + } + if ((ptr_msc->bulk_in_pipe_hndl == 0U) && + (ptr_msc->bulk_out_pipe_hndl == 0U)) { + memset ((void *)ptr_msc, 0, sizeof (USBH_MSC)); + } + } + EvrUSBH_MSC_ConfigureFailed(usbClassErrorMSC); } -#endif + return ret; } @@ -1204,6 +1229,10 @@ static usbStatus USBH_MSC_SCSI_Read10 (uint8_t instance, uint32_t block_addr, ui EvrUSBH_MSC_ScsiRead10(instance, block_addr, block_num); + if (block_num > UINT16_MAX) { + status = usbInvalidParameter; + goto exit; + } if (ptr_data == NULL) { status = usbInvalidParameter; goto exit; @@ -1222,6 +1251,11 @@ static usbStatus USBH_MSC_SCSI_Read10 (uint8_t instance, uint32_t block_addr, ui bulk_out_pipe_hndl = ptr_msc->bulk_out_pipe_hndl; bulk_in_pipe_hndl = ptr_msc->bulk_in_pipe_hndl; + if (ptr_msc->block_size != 512U) { + status = usbClassErrorMSC; + goto exit; + } + // Send CBW mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_mem, 31U); ptr_cbw = (MSC_CBW *)ptr_mem; if (mstatus != usbOK) { @@ -1303,6 +1337,10 @@ static usbStatus USBH_MSC_SCSI_Write10 (uint8_t instance, uint32_t block_addr, u EvrUSBH_MSC_ScsiWrite10(instance, block_addr, block_num); + if (block_num > UINT16_MAX) { + status = usbInvalidParameter; + goto exit; + } if (ptr_data == NULL) { status = usbInvalidParameter; goto exit; @@ -1321,6 +1359,11 @@ static usbStatus USBH_MSC_SCSI_Write10 (uint8_t instance, uint32_t block_addr, u bulk_out_pipe_hndl = ptr_msc->bulk_out_pipe_hndl; bulk_in_pipe_hndl = ptr_msc->bulk_in_pipe_hndl; + if (ptr_msc->block_size != 512U) { + status = usbClassErrorMSC; + goto exit; + } + // Send CBW mstatus = USBH_MemoryAllocate (ctrl, (uint8_t **)&ptr_mem, 31U); ptr_cbw = (MSC_CBW *)ptr_mem; if (mstatus != usbOK) { diff --git a/Components/USB/USB.scvd b/Components/USB/USB.scvd index e6adc7ca..75f059e3 100644 --- a/Components/USB/USB.scvd +++ b/Components/USB/USB.scvd @@ -980,12 +980,13 @@ - + + diff --git a/Documentation/Doxygen/USB/src/revision_history.md b/Documentation/Doxygen/USB/src/revision_history.md index fd4205fd..2471f7f1 100644 --- a/Documentation/Doxygen/USB/src/revision_history.md +++ b/Documentation/Doxygen/USB/src/revision_history.md @@ -8,6 +8,7 @@ V8.0.3 + - USB Device/Host: improved code robustness - USB Device: updated HID configuration for high-speed interrupt endpoints - USB Device: fixed HID idle handling for endpoint intervals not aligned to 4 ms - USB Device: fixed HID idle handling for multiple input reports @@ -16,7 +17,7 @@ V8.0.2 - - USB Host: improve code robustness + - USB Host: improved code robustness diff --git a/Keil.MDK-Middleware.pdsc b/Keil.MDK-Middleware.pdsc index 16d081f1..e867369e 100644 --- a/Keil.MDK-Middleware.pdsc +++ b/Keil.MDK-Middleware.pdsc @@ -26,6 +26,7 @@ - fixed possible TCP socket reset after timeout recovery - various minor fixes and security improvements USB Component Version 8.0.3 + - USB Device/Host: improved code robustness - USB Device: improved HID idle handling