GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_cdc_acm_capabilities_get.c Lines: 58 58 100.0 %
Date: 2024-12-12 17:16:36 Branches: 27 27 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
/**   CDC ACM Class                                                       */
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_class_cdc_acm.h"
29
#include "ux_host_stack.h"
30
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_host_class_cdc_acm_capabilities_get             PORTABLE C      */
37
/*                                                           6.2.1        */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function obtains the entire cdc_acm configuration descriptors. */
45
/*    This is needed because the cdc_acm class needs to know if commands  */
46
/*    are routed through the comm interface or the data class.            */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    cdc_acm                                 Pointer to cdc_acm class    */
51
/*                                                                        */
52
/*  OUTPUT                                                                */
53
/*                                                                        */
54
/*    Completion Status                                                   */
55
/*                                                                        */
56
/*  CALLS                                                                 */
57
/*                                                                        */
58
/*    _ux_host_stack_transfer_request       Process transfer request      */
59
/*    _ux_utility_descriptor_parse          Parse descriptor              */
60
/*    _ux_utility_memory_allocate           Allocate memory block         */
61
/*    _ux_utility_memory_free               Release memory block          */
62
/*                                                                        */
63
/*  CALLED BY                                                             */
64
/*                                                                        */
65
/*    _ux_host_class_cdc_acm_activate                                     */
66
/*                                                                        */
67
/*  RELEASE HISTORY                                                       */
68
/*                                                                        */
69
/*    DATE              NAME                      DESCRIPTION             */
70
/*                                                                        */
71
/*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
72
/*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
73
/*                                            resulting in version 6.1    */
74
/*  03-08-2023     Chaoqiong Xiao           Modified comment(s),          */
75
/*                                            fixed capabilities get from */
76
/*                                            multiple CDC-ACM functions, */
77
/*                                            resulting in version 6.2.1  */
78
/*                                                                        */
79
/**************************************************************************/
80
80
UINT  _ux_host_class_cdc_acm_capabilities_get(UX_HOST_CLASS_CDC_ACM *cdc_acm)
81
{
82
83
UCHAR                       *descriptor;
84
UCHAR                       *saved_descriptor;
85
UX_ENDPOINT                 *control_endpoint;
86
UX_TRANSFER                 *transfer_request;
87
UX_CONFIGURATION            configuration;
88
UX_INTERFACE_DESCRIPTOR     interface_descriptor;
89
UINT                        status;
90
ULONG                       total_descriptor_length;
91
UCHAR                       descriptor_length;
92
UCHAR                       descriptor_type;
93
UCHAR                       descriptor_subtype;
94
ULONG                       interface_found;
95
96
    /* We need to get the default control endpoint transfer request pointer.  */
97
80
    control_endpoint =  &cdc_acm -> ux_host_class_cdc_acm_device -> ux_device_control_endpoint;
98
80
    transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
99
100
    /* Need to allocate memory for the descriptor.  */
101
80
    descriptor =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_CONFIGURATION_DESCRIPTOR_LENGTH);
102
80
    if (descriptor == UX_NULL)
103
1
        return(UX_MEMORY_INSUFFICIENT);
104
105
    /* Save this descriptor address since we need to free it. */
106
79
    saved_descriptor = descriptor;
107
108
    /* Create a transfer request for the GET_DESCRIPTOR request.  */
109
79
    transfer_request -> ux_transfer_request_data_pointer =      descriptor;
110
79
    transfer_request -> ux_transfer_request_requested_length =  UX_CONFIGURATION_DESCRIPTOR_LENGTH;
111
79
    transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
112
79
    transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
113
79
    transfer_request -> ux_transfer_request_value =             UX_CONFIGURATION_DESCRIPTOR_ITEM << 8;
114
79
    transfer_request -> ux_transfer_request_index =             0;
115
116
    /* Send request to HCD layer.  */
117
79
    status =  _ux_host_stack_transfer_request(transfer_request);
118
119
    /* Check for correct transfer and entire descriptor returned.  */
120

79
    if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == UX_CONFIGURATION_DESCRIPTOR_LENGTH))
121
    {
122
123
        /* The descriptor is in a packed format, parse it locally.  */
124
76
        _ux_utility_descriptor_parse(descriptor, _ux_system_configuration_descriptor_structure,
125
                                    UX_CONFIGURATION_DESCRIPTOR_ENTRIES, (UCHAR *) &configuration.ux_configuration_descriptor);
126
127
        /* Now we have the configuration descriptor which will tell us how many
128
           bytes there are in the entire descriptor.  */
129
76
        total_descriptor_length =  configuration.ux_configuration_descriptor.wTotalLength;
130
131
        /* Free the previous descriptor.  */
132
76
        _ux_utility_memory_free(descriptor);
133
134
        /* Allocate enough memory to read all descriptors attached
135
          to this configuration.  */
136
76
        descriptor =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, total_descriptor_length);
137
76
        if (descriptor == UX_NULL)
138
1
            return(UX_MEMORY_INSUFFICIENT);
139
140
        /* Save this descriptor address since we need to free it. */
141
75
        saved_descriptor = descriptor;
142
143
        /* Set the length we need to retrieve.  */
144
75
        transfer_request -> ux_transfer_request_requested_length =  total_descriptor_length;
145
146
        /* And reprogram the descriptor buffer address.  */
147
75
        transfer_request -> ux_transfer_request_data_pointer =  descriptor;
148
149
        /* Send request to HCD layer.  */
150
75
        status =  _ux_host_stack_transfer_request(transfer_request);
151
152
        /* Check for correct transfer and entire descriptor returned.  */
153

75
        if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == total_descriptor_length))
154
        {
155
156
            /* Default is Interface descriptor not yet found.  */
157
73
            interface_found =  UX_FALSE;
158
159
            /* Scan the descriptor for the CDC Comm interface.  */
160
946
            while (total_descriptor_length)
161
            {
162
163
                /* Gather the length, type and subtype of the descriptor.  */
164
875
                descriptor_length =   *descriptor;
165
875
                descriptor_type =     *(descriptor + 1);
166
875
                descriptor_subtype =  *(descriptor + 2);
167
168
                /* Make sure this descriptor has at least the minimum length.  */
169
875
                if (descriptor_length < 3)
170
                {
171
172
                    /* Error trap. */
173
1
                    _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
174
175
                    /* If trace is enabled, insert this event into the trace buffer.  */
176
                    UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
177
178
                    /* We can free the resource now.  */
179
1
                    _ux_utility_memory_free(saved_descriptor);
180
181
                    /* Descriptor is corrupted.  */
182
1
                    return(UX_DESCRIPTOR_CORRUPTED);
183
                }
184
185
                /* Process relative to descriptor type.  */
186
874
                switch (descriptor_type)
187
                {
188
189
190
159
                case UX_INTERFACE_DESCRIPTOR_ITEM:
191
192
                    /* Parse the interface descriptor and make it machine independent.  */
193
159
                    _ux_utility_descriptor_parse(descriptor, _ux_system_interface_descriptor_structure,
194
                                                    UX_INTERFACE_DESCRIPTOR_ENTRIES, (UCHAR *) &interface_descriptor);
195
196
                    /* Ensure we have the correct interface for CDC control (current interface).  */
197
159
                    if (interface_descriptor.bInterfaceNumber ==
198
159
                                cdc_acm -> ux_host_class_cdc_acm_interface ->
199
159
                                    ux_interface_descriptor.bInterfaceNumber)
200
                    {
201
202
                        /* Mark we have found it.  */
203
72
                        interface_found =  UX_TRUE;
204
205
                    }
206
                    else
207
                    {
208
209
                        /* Haven't found it.  */
210
87
                        interface_found =  UX_FALSE;
211
                    }
212
159
                    break;
213
214
215
319
                case UX_HOST_CLASS_CDC_ACM_CS_INTERFACE:
216
217
                    /* First make sure we have found the correct generic interface descriptor.  */
218

319
                    if ((interface_found == UX_TRUE) && (descriptor_subtype == UX_HOST_CLASS_CDC_ACM_CALL_MANAGEMENT_DESCRIPTOR))
219
                    {
220
221
                        /* Retrieve the bmCapabilities field which indicates how ACM commands are sent to the device.  */
222
70
                        cdc_acm -> ux_host_class_cdc_acm_capabilities  = *(descriptor + UX_HOST_CLASS_CDC_ACM_CALL_MANAGEMENT_CAPABILITIES);
223
224
225
                    }
226
319
                    break;
227
                }
228
229
                /* Verify if the descriptor is still valid.  */
230
874
                if (descriptor_length > total_descriptor_length)
231
                {
232
233
                    /* Error trap. */
234
1
                    _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
235
236
                    /* If trace is enabled, insert this event into the trace buffer.  */
237
                    UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
238
239
                    /* We can free the resource now.  */
240
1
                    _ux_utility_memory_free(saved_descriptor);
241
242
1
                    return(UX_DESCRIPTOR_CORRUPTED);
243
                }
244
245
                /* Jump to the next descriptor if we have not reached the end.  */
246
873
                descriptor +=  descriptor_length;
247
248
                /* And adjust the length left to parse in the descriptor.  */
249
873
                total_descriptor_length -=  descriptor_length;
250
            }
251
252
            /* We can free the resource now.  */
253
71
            _ux_utility_memory_free(saved_descriptor);
254
255
71
            return(UX_SUCCESS);
256
        }
257
    }
258
259
    /* Free all used resources.  */
260
5
    _ux_utility_memory_free(saved_descriptor);
261
262
    /* Return completion status.  */
263
5
    return(status);
264
}
265