GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_host_stack_configuration_enumerate.c Lines: 32 32 100.0 %
Date: 2026-03-06 18:57:10 Branches: 12 12 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_enumerate              PORTABLE C      */
37
/*                                                           6.1          */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function reads the configuration descriptor, creates the       */
45
/*    configuration container(s) for the device, and enumerates all found */
46
/*    configurations.                                                     */
47
/*                                                                        */
48
/*    At this stage, only the containers for each subcomponents are       */
49
/*    linked. No configuration, interface or endpoints are active unless  */
50
/*    a class issues a SET_CONFIGURATION.                                 */
51
/*                                                                        */
52
/*  INPUT                                                                 */
53
/*                                                                        */
54
/*    device                                Pointer to device             */
55
/*                                                                        */
56
/*  OUTPUT                                                                */
57
/*                                                                        */
58
/*    Completion Status                                                   */
59
/*                                                                        */
60
/*  CALLS                                                                 */
61
/*                                                                        */
62
/*    _ux_host_stack_configuration_descriptor_parse                       */
63
/*                                          Parse configuration descriptor*/
64
/*    _ux_host_stack_configuration_instance_delete                        */
65
/*                                          Delete configuration instance */
66
/*    _ux_host_stack_new_configuration_create                             */
67
/*                                          Create new configuration      */
68
/*    _ux_host_stack_transfer_request       Process transfer request      */
69
/*    _ux_utility_descriptor_parse          Parse descriptor              */
70
/*    _ux_utility_memory_allocate           Allocate block of memory      */
71
/*    _ux_utility_memory_free               Free block of memory          */
72
/*                                                                        */
73
/*  CALLED BY                                                             */
74
/*                                                                        */
75
/*    USBX Components                                                     */
76
/*                                                                        */
77
/**************************************************************************/
78
1067
UINT  _ux_host_stack_configuration_enumerate(UX_DEVICE *device)
79
{
80
81
UX_TRANSFER         *transfer_request;
82
1067
UINT                status =  UX_ERROR;
83
UCHAR *             descriptor;
84
UX_ENDPOINT         *control_endpoint;
85
UX_CONFIGURATION    *configuration;
86
ULONG               nb_configurations;
87
ULONG               configuration_index;
88
89
    /* If trace is enabled, insert this event into the trace buffer.  */
90
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_CONFIGURATION_ENUMERATE, device, 0, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
91
92
    /* Retrieve the pointer to the control endpoint and its transfer_request.  */
93
1067
    control_endpoint =  &device -> ux_device_control_endpoint;
94
1067
    transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
95
96
    /* Need to allocate memory for the configuration descriptor the first time we read
97
       only the configuration descriptor when we have the configuration descriptor, we have
98
       the length of the entire configuration\interface\endpoint descriptors.  */
99
1067
    descriptor =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_CONFIGURATION_DESCRIPTOR_LENGTH);
100
1067
    if (descriptor == UX_NULL)
101
2
        return(UX_MEMORY_INSUFFICIENT);
102
103
    /* There maybe multiple configurations for this device.  */
104
1065
    nb_configurations =  device -> ux_device_descriptor.bNumConfigurations;
105
106
    /* Parse all the configurations attached to the device. We start with the first index.
107
       The index and the actual configuration value may be different according to the USB specification!  */
108
2124
    for (configuration_index = 0; configuration_index < nb_configurations; configuration_index++)
109
    {
110
111
        /* Create a transfer_request for the GET_DESCRIPTOR request.  */
112
1065
        transfer_request -> ux_transfer_request_data_pointer =      descriptor;
113
1065
        transfer_request -> ux_transfer_request_requested_length =  UX_CONFIGURATION_DESCRIPTOR_LENGTH;
114
1065
        transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
115
1065
        transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
116
1065
        transfer_request -> ux_transfer_request_value =             configuration_index | (UINT)(UX_CONFIGURATION_DESCRIPTOR_ITEM << 8);
117
1065
        transfer_request -> ux_transfer_request_index =             0;
118
119
        /* Send request to HCD layer.  */
120
1065
        status =  _ux_host_stack_transfer_request(transfer_request);
121
122
        /* Check for correct transfer and entire descriptor returned.  */
123

1065
        if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == UX_CONFIGURATION_DESCRIPTOR_LENGTH))
124
        {
125
126
            /* Allocate some memory for the container of this descriptor.  */
127
1061
            configuration =  _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_CONFIGURATION));
128
129
            /* Check to see if the block was allocated.  */
130
1061
            if (configuration != UX_NULL)
131
            {
132
133
                /* This configuration must be linked to the device.  */
134
1059
                _ux_host_stack_new_configuration_create(device, configuration);
135
136
                /* The descriptor is in a packed format, parse it locally.  */
137
1059
                _ux_utility_descriptor_parse(descriptor, _ux_system_configuration_descriptor_structure,
138
1059
                                    UX_CONFIGURATION_DESCRIPTOR_ENTRIES, (UCHAR *) &configuration -> ux_configuration_descriptor);
139
140
                /* Parse the device descriptor so that we can retrieve the length
141
                    of the entire configuration.  */
142
1059
                status =  _ux_host_stack_configuration_descriptor_parse(device, configuration, configuration_index);
143
144
                /* Check the completion status.  */
145
1059
                if (status != UX_SUCCESS)
146
                {
147
                    /* Error, delete the configuration instance.  */
148
128
                    _ux_host_stack_configuration_instance_delete(configuration);
149
                }
150
            }
151
            else
152
            {
153
154
                /* Cannot allocate configuration memory. Abort enumeration */
155
2
                status =  UX_MEMORY_INSUFFICIENT;
156
157
2
                break;
158
            }
159
        }
160
        else
161
        {
162
163
            /* Error trap. */
164
4
            _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_DESCRIPTOR_CORRUPTED);
165
166
            /* If trace is enabled, insert this event into the trace buffer.  */
167
            UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
168
169
            /* The device descriptor does not contain the right amount of data. Maybe corruption.  */
170
4
            status =  UX_DESCRIPTOR_CORRUPTED;
171
172
4
            break;
173
        }
174
175
    }
176
177
    /* Free all used resources.  */
178
1065
    _ux_utility_memory_free(descriptor);
179
180
    /* Return completion status.  */
181
1065
    return(status);
182
}
183