GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_host_stack_device_descriptor_read.c Lines: 40 40 100.0 %
Date: 2026-03-06 18:57:10 Branches: 18 18 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_device_descriptor_read               PORTABLE C      */
37
/*                                                           6.1.10       */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function reads the device descriptor.                          */
45
/*                                                                        */
46
/*  INPUT                                                                 */
47
/*                                                                        */
48
/*    device                                Pointer to device             */
49
/*                                                                        */
50
/*  OUTPUT                                                                */
51
/*                                                                        */
52
/*    Completion Status                                                   */
53
/*                                                                        */
54
/*  CALLS                                                                 */
55
/*                                                                        */
56
/*    _ux_host_stack_transfer_request       Process transfer request      */
57
/*    _ux_utility_descriptor_parse          Parse descriptor              */
58
/*    _ux_utility_memory_allocate           Allocate memory block         */
59
/*    _ux_utility_memory_free               Free memory block             */
60
/*                                                                        */
61
/*  CALLED BY                                                             */
62
/*                                                                        */
63
/*    USBX Components                                                     */
64
/*                                                                        */
65
/**************************************************************************/
66
1085
UINT  _ux_host_stack_device_descriptor_read(UX_DEVICE *device)
67
{
68
69
UX_TRANSFER     *transfer_request;
70
UINT            status;
71
UCHAR *         descriptor;
72
UX_ENDPOINT     *control_endpoint;
73
74
    /* If trace is enabled, insert this event into the trace buffer.  */
75
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_DEVICE_DESCRIPTOR_READ, device, 0, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
76
77
    /* Retrieve the pointer to the control endpoint.  */
78
1085
    control_endpoint =  &device -> ux_device_control_endpoint;
79
1085
    transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
80
81
    /* Need to allocate memory for the descriptor.  */
82
1085
    descriptor =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_DEVICE_DESCRIPTOR_LENGTH);
83
1085
    if (descriptor == UX_NULL)
84
2
        return(UX_MEMORY_INSUFFICIENT);
85
86
    /* Create a transfer_request for the GET_DESCRIPTOR request. The first transfer_request asks
87
       for the first 8 bytes only. This way we will know the real MaxPacketSize
88
       value for the control endpoint.  */
89
1083
    transfer_request -> ux_transfer_request_data_pointer =      descriptor;
90
1083
    transfer_request -> ux_transfer_request_requested_length =  8;
91
1083
    transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
92
1083
    transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
93
1083
    transfer_request -> ux_transfer_request_value =             UX_DEVICE_DESCRIPTOR_ITEM << 8;
94
1083
    transfer_request -> ux_transfer_request_index =             0;
95
96
#if defined(UX_HOST_STANDALONE)
97
    device -> ux_device_enum_trans = transfer_request;
98
    status = UX_SUCCESS;
99
    return(status);
100
#else
101
102
    /* Send request to HCD layer.  */
103
1083
    status =  _ux_host_stack_transfer_request(transfer_request);
104
105
    /* Check for correct transfer and entire descriptor returned.  */
106

1082
    if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == 8))
107
    {
108
109
        /* Parse the device descriptor and create the local descriptor.  */
110
1072
        _ux_utility_descriptor_parse(descriptor, _ux_system_device_descriptor_structure, UX_DEVICE_DESCRIPTOR_ENTRIES,
111
1072
                                                                                (UCHAR *) &device -> ux_device_descriptor);
112
    }
113
    else
114
    {
115
116
        /* Free all used resources.  */
117
10
        _ux_utility_memory_free(descriptor);
118
119
        /* Return completion status.  */
120
10
        return(status);
121
    }
122
123
    /* Validate the bMaxPacketSize0.  */
124
1072
    if (device -> ux_device_descriptor.bMaxPacketSize0 != 8 &&
125
374
        device -> ux_device_descriptor.bMaxPacketSize0 != 16 &&
126
373
        device -> ux_device_descriptor.bMaxPacketSize0 != 32 &&
127
372
        device -> ux_device_descriptor.bMaxPacketSize0 != 64)
128
    {
129
9
        _ux_utility_memory_free(descriptor);
130
9
        return(UX_DESCRIPTOR_CORRUPTED);
131
    }
132
133
#if defined(UX_HOST_DEVICE_CLASS_CODE_VALIDATION_ENABLE)
134
135
    /* Validate the USB-IF bDeviceClass class code.  */
136
    switch(device -> ux_device_descriptor.bDeviceClass)
137
    {
138
    case 0x00: /* Fall through.  */
139
    case 0x02: /* Fall through.  */
140
    case 0x09: /* Fall through.  */
141
    case 0x11: /* Fall through.  */
142
    case 0xDC: /* Fall through.  */
143
    case 0xEF: /* Fall through.  */
144
    case 0xFF:
145
        break;
146
    default:
147
148
        /* Invalid device class code.  */
149
        _ux_utility_memory_free(descriptor);
150
        return(UX_DESCRIPTOR_CORRUPTED);
151
    }
152
#endif
153
154
    /* Update the max packet size value for the endpoint.  */
155
1063
    control_endpoint -> ux_endpoint_descriptor.wMaxPacketSize =  device -> ux_device_descriptor.bMaxPacketSize0;
156
157
    /* Create a transfer_request for the GET_DESCRIPTOR request. This time, we have the complete length */
158
1063
    transfer_request -> ux_transfer_request_data_pointer =      descriptor;
159
1063
    transfer_request -> ux_transfer_request_requested_length =  UX_DEVICE_DESCRIPTOR_LENGTH;
160
1063
    transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
161
1063
    transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
162
1063
    transfer_request -> ux_transfer_request_value =             UX_DEVICE_DESCRIPTOR_ITEM << 8;
163
1063
    transfer_request -> ux_transfer_request_index =             0;
164
1063
    transfer_request -> ux_transfer_request_packet_length =     device -> ux_device_descriptor.bMaxPacketSize0;
165
166
    /* Send request to HCD layer.  */
167
1063
    status =  _ux_host_stack_transfer_request(transfer_request);
168
169
    /* Check for correct transfer and entire descriptor returned.  */
170

1063
    if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == UX_DEVICE_DESCRIPTOR_LENGTH))
171
    {
172
173
        /* Parse the device descriptor and create the local descriptor.  */
174
1060
        _ux_utility_descriptor_parse(descriptor, _ux_system_device_descriptor_structure, UX_DEVICE_DESCRIPTOR_ENTRIES,
175
1060
                                                                    (UCHAR *) &device -> ux_device_descriptor);
176
    }
177
    else
178
    {
179
180
        /* Error trap. */
181
3
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_DESCRIPTOR_CORRUPTED);
182
183
        /* If trace is enabled, insert this event into the trace buffer.  */
184
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
185
186
        /* The device descriptor does not contain the right amount of data. Maybe corruption.  */
187
3
        status =  UX_DESCRIPTOR_CORRUPTED;
188
    }
189
190
    /* Free all used resources.  */
191
1063
    _ux_utility_memory_free(descriptor);
192
193
    /* Return completion status.  */
194
1063
    return(status);
195
#endif
196
}
197