/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-2026 STMicroelectronics.
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** USBX Component                                                        */
/**                                                                       */
/**   STM32 Controller Driver                                             */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE
#define UX_HCD_STM32_SOURCE_CODE

#include "ux_api.h"
#include "ux_hcd_stm32.h"
#include "ux_host_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_hcd_stm32_endpoint_destroy                      PORTABLE C      */
/*                                                           6.4.1        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function will destroy an endpoint.                             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    hcd_stm32                             Pointer to STM32 controller   */
/*    endpoint                              Pointer to endpoint           */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_utility_virtual_address           Get virtual address           */
/*    _ux_utility_delay_ms                  Delay ms                      */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    STM32 Controller Driver                                             */
/*                                                                        */
/*  RELEASE HISTORY                                                       */
/*                                                                        */
/*    DATE              NAME                      DESCRIPTION             */
/*                                                                        */
/*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
/*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
/*                                            added standalone support,   */
/*                                            resulting in version 6.1.10 */
/*                                                                        */
/**************************************************************************/
UINT _ux_hcd_stm32_endpoint_destroy(UX_HCD_STM32 *hcd_stm32, UX_ENDPOINT *endpoint)
{
#if defined(UX_HOST_STANDALONE)
  UX_INTERRUPT_SAVE_AREA
#endif /* defined(UX_HOST_STANDALONE) */
  UX_HCD_STM32_ED       *ed;
  UX_HCD_STM32_ED       *next_ed;
  UINT                   endpoint_type;

  /* From the endpoint container fetch the STM32 ED descriptor.  */
  ed = (UX_HCD_STM32_ED *) endpoint -> ux_endpoint_ed;

  /* Check if this physical endpoint has been initialized properly!  */
  if (ed == UX_NULL)
  {
    /* Error trap. */
    _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_HCD, UX_ENDPOINT_HANDLE_UNKNOWN);

    /* If trace is enabled, insert this event into the trace buffer.  */
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, endpoint, 0, 0, UX_TRACE_ERRORS, 0, 0)

    return(UX_ENDPOINT_HANDLE_UNKNOWN);
  }

#if defined(UX_HOST_STANDALONE)

  /* There is no background thread, just remove the ED from processing list.  */
  UX_DISABLE
#else

  /* Wait for the controller to finish the current frame processing.  */
  _ux_utility_delay_ms(1);
#endif /* defined(UX_HOST_STANDALONE) */

  /* We need to free the channel.  */
  *(hcd_stm32 -> ux_hcd_stm32_channels_ed + (ed -> ux_stm32_ed_channel)) = UX_NULL;

  /* Get endpoint type.  */
  endpoint_type = (endpoint -> ux_endpoint_descriptor.bmAttributes) & UX_MASK_ENDPOINT_TYPE;

  /* Check for periodic endpoints.  */
  if ((endpoint_type == UX_INTERRUPT_ENDPOINT) || (endpoint_type == UX_ISOCHRONOUS_ENDPOINT))
  {
    /* Remove the ED from periodic ED list.  */
    if (hcd_stm32 -> ux_hcd_stm32_periodic_ed_head == ed)
    {
      /* The head one in the list, just set the pointer to it's next.  */
      hcd_stm32 -> ux_hcd_stm32_periodic_ed_head = ed -> ux_stm32_ed_next_ed;
    }
    else
    {

      /* Get the first ED in the list.  */
      next_ed = hcd_stm32 -> ux_hcd_stm32_periodic_ed_head;

      /* Search for the ED in the list.  */
      while( (next_ed != UX_NULL) && (next_ed -> ux_stm32_ed_next_ed != ed) )
      {
        /* Move to next ED.  */
        next_ed = next_ed -> ux_stm32_ed_next_ed;
      }

      /* Check if we found the ED.  */
      if (next_ed)
      {
        /* Remove the ED from list.  */
        next_ed -> ux_stm32_ed_next_ed = next_ed -> ux_stm32_ed_next_ed -> ux_stm32_ed_next_ed;
      }
    }

    /* Decrease the periodic active count.  */
    hcd_stm32 -> ux_hcd_stm32_periodic_scheduler_active --;
  }

  /* Now we can safely make the ED free.  */
  ed -> ux_stm32_ed_status = UX_HCD_STM32_ED_STATUS_FREE;

#if defined (USE_HAL_HCD_USB_HUB_HS_SPLIT) && (USE_HAL_HCD_USB_HUB_HS_SPLIT == 1U)
  HAL_HCD_ClearChannelHubInfo(hcd_stm32->hcd_handle, ed -> ux_stm32_ed_channel);
#endif /* defined (USE_HAL_HCD_USB_HUB_HS_SPLIT) && (USE_HAL_HCD_USB_HUB_HS_SPLIT == 1U) */

  /* Finish current transfer and reset the endpoint  */
  _ux_hcd_stm32_endpoint_reset(hcd_stm32, endpoint);

#if defined(UX_HOST_STANDALONE)

  /* If setup memory is not freed correct, free it.  */
  if (ed -> ux_stm32_ed_setup)
    _ux_utility_memory_free(ed -> ux_stm32_ed_setup);

  UX_RESTORE
#endif /* defined(UX_HOST_STANDALONE) */

  /* Return successful completion.  */
  return(UX_SUCCESS);
}

