GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_host_stack_configuration_set.c Lines: 17 17 100.0 %
Date: 2026-03-06 18:57:10 Branches: 4 4 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
10
 **************************************************************************/
11
12
13
/**************************************************************************/
14
/**************************************************************************/
15
/**                                                                       */
16
/** USBX Component                                                        */
17
/**                                                                       */
18
/**   Host Stack                                                          */
19
/**                                                                       */
20
/**************************************************************************/
21
/**************************************************************************/
22
23
24
/* Include necessary system files.  */
25
26
#define UX_SOURCE_CODE
27
28
#include "ux_api.h"
29
#include "ux_host_stack.h"
30
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_host_stack_configuration_set                    PORTABLE C      */
37
/*                                                           6.1.10       */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function performs a setting of a device configuration.         */
45
/*                                                                        */
46
/*    In RTOS mode, this function is blocking.                            */
47
/*    If the host is OTG capable and the device has an OTG descriptor     */
48
/*    that supports HNP we perform a SET_FEATURE with b_hnp_support.      */
49
/*                                                                        */
50
/*    In standalone mode, when device enumeration is in progress, this    */
51
/*    function is non-blocking, it prepares transfer for enum step of     */
52
/*    SET_CONFIGURE request. Otherwise it blocks until transfer request   */
53
/*    done.                                                               */
54
/*                                                                        */
55
/*  INPUT                                                                 */
56
/*                                                                        */
57
/*    configuration                         Pointer to configuration      */
58
/*                                                                        */
59
/*  OUTPUT                                                                */
60
/*                                                                        */
61
/*    Completion Status                                                   */
62
/*                                                                        */
63
/*  CALLS                                                                 */
64
/*                                                                        */
65
/*    _ux_host_stack_transfer_request       Process transfer request      */
66
/*                                                                        */
67
/*  CALLED BY                                                             */
68
/*                                                                        */
69
/*    USBX Components                                                     */
70
/*                                                                        */
71
/**************************************************************************/
72
934
UINT  _ux_host_stack_configuration_set(UX_CONFIGURATION *configuration)
73
{
74
75
UX_DEVICE       *device;
76
UX_TRANSFER     *transfer_request;
77
UINT            status;
78
UX_ENDPOINT     *control_endpoint;
79
#ifdef UX_OTG_SUPPORT
80
UX_HCD          *hcd;
81
#endif
82
83
84
    /* A configuration is selected. Retrieve the pointer to the control endpoint
85
       and its transfer request.  */
86
934
    device =            configuration -> ux_configuration_device;
87
934
    control_endpoint =  &device -> ux_device_control_endpoint;
88
934
    transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
89
90
#ifdef UX_OTG_SUPPORT
91
    /* Check if the configuration has an OTG device with HNP feature.  */
92
    if (configuration -> ux_configuration_otg_capabilities & UX_OTG_HNP_SUPPORT)
93
    {
94
95
        /* For HNP to work the device has to be connected directly to the Root Hub and not
96
           a down stream hub.  If the parent is NULL, the device is on the root hub.  */
97
        if (UX_DEVICE_PARENT_IS_ROOTHUB(device))
98
        {
99
100
            /* With the device we have the pointer to the HCD.  */
101
            hcd = UX_DEVICE_HCD_GET(device);
102
103
            /* Check the HCD to ensure we have an OTG host controller.  */
104
            if (hcd -> ux_hcd_otg_capabilities & UX_HCD_OTG_CAPABLE)
105
            {
106
107
                /* The Host controller is OTG aware.  Perform a SET_FEATURE with b_hnp_support.  */
108
                transfer_request -> ux_transfer_request_data_pointer =      UX_NULL;
109
                transfer_request -> ux_transfer_request_requested_length =  0;
110
                transfer_request -> ux_transfer_request_function =          UX_SET_FEATURE;
111
                transfer_request -> ux_transfer_request_type =              UX_REQUEST_OUT| UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
112
                transfer_request -> ux_transfer_request_value =             UX_OTG_FEATURE_A_HNP_SUPPORT;
113
                transfer_request -> ux_transfer_request_index =             0;
114
115
                /* Send request to HCD layer.  */
116
                status =  _ux_host_stack_transfer_request(transfer_request);
117
118
                /* If the device fails this command we turn off its OTG capabilities.  */
119
                if (status != UX_SUCCESS)
120
121
                    /* Reset the OTG capabilities of the device.  */
122
                    configuration -> ux_configuration_otg_capabilities = 0;
123
124
            }
125
        }
126
    }
127
#endif
128
129
    /* If trace is enabled, insert this event into the trace buffer.  */
130
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_CONFIGURATION_SET, configuration, 0, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
131
132
    /* Create a transfer_request for the SET_CONFIGURATION request. No data for this request.  */
133
934
    transfer_request -> ux_transfer_request_requested_length =  0;
134
934
    transfer_request -> ux_transfer_request_function =          UX_SET_CONFIGURATION;
135
934
    transfer_request -> ux_transfer_request_type =              UX_REQUEST_OUT | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
136
934
    transfer_request -> ux_transfer_request_value =             (USHORT) configuration -> ux_configuration_descriptor.bConfigurationValue;
137
934
    transfer_request -> ux_transfer_request_index =             0;
138
139
#if defined(UX_HOST_STANDALONE)
140
    if (device -> ux_device_flags &= UX_DEVICE_FLAG_ENUM)
141
    {
142
143
        /* Special case for enumeration process, non-blocking.  */
144
        device -> ux_device_enum_trans = transfer_request;
145
        status = UX_SUCCESS;
146
        return(status);
147
    }
148
149
    /* Tend to be blocking after enumeration done.  */
150
#endif
151
152
    /* Send request to HCD layer.  */
153
934
    status =  _ux_host_stack_transfer_request(transfer_request);
154
155
    /* Check completion status.  */
156
934
    if(status == UX_SUCCESS)
157
    {
158
159
        /* Change the device state to configured.  */
160
918
        device -> ux_device_state =  UX_DEVICE_CONFIGURED;
161
162
        /* Store the new configuration value in the device container.  */
163
918
        device -> ux_device_current_configuration =  configuration;
164
165
        /* Save current device power source.  */
166
918
        device -> ux_device_power_source = (configuration ->
167
918
                                            ux_configuration_descriptor.bmAttributes &
168
                                            UX_CONFIGURATION_DEVICE_SELF_POWERED) ?
169
918
                                UX_DEVICE_SELF_POWERED : UX_DEVICE_BUS_POWERED;
170
    }
171
172
    /* Return status to caller.  */
173
934
    return(status);
174
}