GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_host_stack_configuration_enumerate.c Lines: 32 32 100.0 %
Date: 2024-12-12 17:16:36 Branches: 12 12 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 *
4
 * This program and the accompanying materials are made available under the
5
 * terms of the MIT License which is available at
6
 * https://opensource.org/licenses/MIT.
7
 *
8
 * SPDX-License-Identifier: MIT
9
 **************************************************************************/
10
11
12
/**************************************************************************/
13
/**************************************************************************/
14
/**                                                                       */
15
/** USBX Component                                                        */
16
/**                                                                       */
17
/**   Host Stack                                                          */
18
/**                                                                       */
19
/**************************************************************************/
20
/**************************************************************************/
21
22
23
/* Include necessary system files.  */
24
25
#define UX_SOURCE_CODE
26
27
#include "ux_api.h"
28
#include "ux_host_stack.h"
29
30
31
/**************************************************************************/
32
/*                                                                        */
33
/*  FUNCTION                                               RELEASE        */
34
/*                                                                        */
35
/*    _ux_host_stack_configuration_enumerate              PORTABLE C      */
36
/*                                                           6.1          */
37
/*  AUTHOR                                                                */
38
/*                                                                        */
39
/*    Chaoqiong Xiao, Microsoft Corporation                               */
40
/*                                                                        */
41
/*  DESCRIPTION                                                           */
42
/*                                                                        */
43
/*    This function reads the configuration descriptor, creates the       */
44
/*    configuration container(s) for the device, and enumerates all found */
45
/*    configurations.                                                     */
46
/*                                                                        */
47
/*    At this stage, only the containers for each subcomponents are       */
48
/*    linked. No configuration, interface or endpoints are active unless  */
49
/*    a class issues a SET_CONFIGURATION.                                 */
50
/*                                                                        */
51
/*  INPUT                                                                 */
52
/*                                                                        */
53
/*    device                                Pointer to device             */
54
/*                                                                        */
55
/*  OUTPUT                                                                */
56
/*                                                                        */
57
/*    Completion Status                                                   */
58
/*                                                                        */
59
/*  CALLS                                                                 */
60
/*                                                                        */
61
/*    _ux_host_stack_configuration_descriptor_parse                       */
62
/*                                          Parse configuration descriptor*/
63
/*    _ux_host_stack_configuration_instance_delete                        */
64
/*                                          Delete configuration instance */
65
/*    _ux_host_stack_new_configuration_create                             */
66
/*                                          Create new configuration      */
67
/*    _ux_host_stack_transfer_request       Process transfer request      */
68
/*    _ux_utility_descriptor_parse          Parse descriptor              */
69
/*    _ux_utility_memory_allocate           Allocate block of memory      */
70
/*    _ux_utility_memory_free               Free block of memory          */
71
/*                                                                        */
72
/*  CALLED BY                                                             */
73
/*                                                                        */
74
/*    USBX Components                                                     */
75
/*                                                                        */
76
/*  RELEASE HISTORY                                                       */
77
/*                                                                        */
78
/*    DATE              NAME                      DESCRIPTION             */
79
/*                                                                        */
80
/*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
81
/*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
82
/*                                            resulting in version 6.1    */
83
/*                                                                        */
84
/**************************************************************************/
85
1066
UINT  _ux_host_stack_configuration_enumerate(UX_DEVICE *device)
86
{
87
88
UX_TRANSFER         *transfer_request;
89
1066
UINT                status =  UX_ERROR;
90
UCHAR *             descriptor;
91
UX_ENDPOINT         *control_endpoint;
92
UX_CONFIGURATION    *configuration;
93
ULONG               nb_configurations;
94
ULONG               configuration_index;
95
96
    /* If trace is enabled, insert this event into the trace buffer.  */
97
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_CONFIGURATION_ENUMERATE, device, 0, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
98
99
    /* Retrieve the pointer to the control endpoint and its transfer_request.  */
100
1066
    control_endpoint =  &device -> ux_device_control_endpoint;
101
1066
    transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
102
103
    /* Need to allocate memory for the configuration descriptor the first time we read
104
       only the configuration descriptor when we have the configuration descriptor, we have
105
       the length of the entire configuration\interface\endpoint descriptors.  */
106
1066
    descriptor =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_CONFIGURATION_DESCRIPTOR_LENGTH);
107
1066
    if (descriptor == UX_NULL)
108
2
        return(UX_MEMORY_INSUFFICIENT);
109
110
    /* There maybe multiple configurations for this device.  */
111
1064
    nb_configurations =  device -> ux_device_descriptor.bNumConfigurations;
112
113
    /* Parse all the configurations attached to the device. We start with the first index.
114
       The index and the actual configuration value may be different according to the USB specification!  */
115
2122
    for (configuration_index = 0; configuration_index < nb_configurations; configuration_index++)
116
    {
117
118
        /* Create a transfer_request for the GET_DESCRIPTOR request.  */
119
1064
        transfer_request -> ux_transfer_request_data_pointer =      descriptor;
120
1064
        transfer_request -> ux_transfer_request_requested_length =  UX_CONFIGURATION_DESCRIPTOR_LENGTH;
121
1064
        transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
122
1064
        transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
123
1064
        transfer_request -> ux_transfer_request_value =             configuration_index | (UINT)(UX_CONFIGURATION_DESCRIPTOR_ITEM << 8);
124
1064
        transfer_request -> ux_transfer_request_index =             0;
125
126
        /* Send request to HCD layer.  */
127
1064
        status =  _ux_host_stack_transfer_request(transfer_request);
128
129
        /* Check for correct transfer and entire descriptor returned.  */
130

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