summaryrefslogtreecommitdiff
path: root/common/usbx_host_classes/src/ux_host_class_hub_port_reset.c
blob: 027fc773b664caf6a2f1218c6ebe9e25be5c7500 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/***************************************************************************
 * 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_reset                       PORTABLE C      */
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function will reset a downstream port of the HUB. When the     */
/*    port is reset, the hardware logic of the HUB also enables it.       */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    hub                                   Pointer to HUB class          */
/*    port                                  Port number                   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_class_hub_port_change_reset_process Reset HUB              */
/*    _ux_host_class_hub_feature            Set HUB class feature         */
/*    _ux_host_class_hub_status_get         Get HUB status                */
/*    _ux_utility_delay_ms                  Thread sleep                  */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    HUB Class                                                           */
/*                                                                        */
/**************************************************************************/
UINT  _ux_host_class_hub_port_reset(UX_HOST_CLASS_HUB *hub, UINT port)
{

UINT        status;
UINT        port_enable_retry;
USHORT      port_status;
USHORT      port_change;


    /* Send the PORT_RESET command.  */
    status =  _ux_host_class_hub_feature(hub, port, UX_SET_FEATURE, UX_HOST_CLASS_HUB_PORT_RESET);

    /* Check the function result and update HUB status if there was a problem.  */
    if (status != UX_SUCCESS)
        return(status);

    /* We allow retrying of this as we may not get the port enable status bit
       set on the first try.  */
    port_enable_retry =  UX_HOST_CLASS_HUB_ENABLE_RETRY_COUNT;
    while (port_enable_retry--)
    {

        /* Now get the status until we have a port enabled.  */
        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_RESET)
        {

            /* The port has been successfully reset.  */

            /* Clear the RESET change bit.  */
            _ux_host_class_hub_port_change_reset_process(hub, port, port_status);

            /* Return success.  */
            return(UX_SUCCESS);
        }

        /* We should wait a bit before retrying this operation.  */
        _ux_utility_delay_ms(UX_HOST_CLASS_HUB_ENABLE_RETRY_DELAY);
    }

    /* We get here when the port never reported RESET completion.  */

    /* Invoke error callback.  */
    _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_HUB, UX_PORT_RESET_FAILED);

    /* Return error.  */
    return(UX_PORT_RESET_FAILED);
}