/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * 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                                                        */
/**                                                                       */
/**   HUB Class                                                           */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

#include "ux_api.h"
#include "ux_host_class_hub.h"
#include "ux_host_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_class_hub_port_change_process              PORTABLE C      */
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function will process a port change indication.                */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    hub                                   Pointer to HUB class          */
/*    port                                  Port number                   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_class_hub_port_change_connection_process                   */
/*                                          Process connection            */
/*    _ux_host_class_hub_port_change_enable_process                       */
/*                                          Enable process                */
/*    _ux_host_class_hub_port_change_over_current_process                 */
/*                                          Change over current process   */
/*    _ux_host_class_hub_port_change_reset_process                        */
/*                                          Reset process                 */
/*    _ux_host_class_hub_port_change_suspend_process                      */
/*                                          Suspend process               */
/*    _ux_host_class_hub_status_get         Get HUB status                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    HUB Class                                                           */
/*                                                                        */
/**************************************************************************/
UINT  _ux_host_class_hub_port_change_process(UX_HOST_CLASS_HUB *hub, UINT port)
{

USHORT      port_status;
USHORT      port_change;
UINT        status;


    /* First step is to retrieve the status on the port with a GET_STATUS.  */
    status =  _ux_host_class_hub_status_get(hub, port, &port_status, &port_change);
    if (status != UX_SUCCESS)
        return(status);

    /* On return of the GET_STATUS, the port change field has been updated
       check for each of the bits it may contain.  */
    if (port_change & UX_HOST_CLASS_HUB_PORT_CHANGE_CONNECTION)
        _ux_host_class_hub_port_change_connection_process(hub, port, port_status);

    if (port_change & UX_HOST_CLASS_HUB_PORT_CHANGE_ENABLE)
        _ux_host_class_hub_port_change_enable_process(hub, port, port_status);

    if (port_change & UX_HOST_CLASS_HUB_PORT_CHANGE_SUSPEND)
        _ux_host_class_hub_port_change_suspend_process(hub, port, port_status);

    if (port_change & UX_HOST_CLASS_HUB_PORT_CHANGE_OVER_CURRENT)
        _ux_host_class_hub_port_change_over_current_process(hub, port, port_status);

    if (port_change & UX_HOST_CLASS_HUB_PORT_CHANGE_RESET)
        _ux_host_class_hub_port_change_reset_process(hub, port, port_status);

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

