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 /**************************************************************************/
_ux_host_class_cdc_acm_capabilities_get(UX_HOST_CLASS_CDC_ACM * cdc_acm)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     control_endpoint =  &cdc_acm -> ux_host_class_cdc_acm_device -> ux_device_control_endpoint;
98     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
99 
100     /* Need to allocate memory for the descriptor.  */
101     descriptor =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_CONFIGURATION_DESCRIPTOR_LENGTH);
102     if (descriptor == UX_NULL)
103         return(UX_MEMORY_INSUFFICIENT);
104 
105     /* Save this descriptor address since we need to free it. */
106     saved_descriptor = descriptor;
107 
108     /* Create a transfer request for the GET_DESCRIPTOR request.  */
109     transfer_request -> ux_transfer_request_data_pointer =      descriptor;
110     transfer_request -> ux_transfer_request_requested_length =  UX_CONFIGURATION_DESCRIPTOR_LENGTH;
111     transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
112     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
113     transfer_request -> ux_transfer_request_value =             UX_CONFIGURATION_DESCRIPTOR_ITEM << 8;
114     transfer_request -> ux_transfer_request_index =             0;
115 
116     /* Send request to HCD layer.  */
117     status =  _ux_host_stack_transfer_request(transfer_request);
118 
119     /* Check for correct transfer and entire descriptor returned.  */
120     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         _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         total_descriptor_length =  configuration.ux_configuration_descriptor.wTotalLength;
130 
131         /* Free the previous descriptor.  */
132         _ux_utility_memory_free(descriptor);
133 
134         /* Allocate enough memory to read all descriptors attached
135           to this configuration.  */
136         descriptor =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, total_descriptor_length);
137         if (descriptor == UX_NULL)
138             return(UX_MEMORY_INSUFFICIENT);
139 
140         /* Save this descriptor address since we need to free it. */
141         saved_descriptor = descriptor;
142 
143         /* Set the length we need to retrieve.  */
144         transfer_request -> ux_transfer_request_requested_length =  total_descriptor_length;
145 
146         /* And reprogram the descriptor buffer address.  */
147         transfer_request -> ux_transfer_request_data_pointer =  descriptor;
148 
149         /* Send request to HCD layer.  */
150         status =  _ux_host_stack_transfer_request(transfer_request);
151 
152         /* Check for correct transfer and entire descriptor returned.  */
153         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             interface_found =  UX_FALSE;
158 
159             /* Scan the descriptor for the CDC Comm interface.  */
160             while (total_descriptor_length)
161             {
162 
163                 /* Gather the length, type and subtype of the descriptor.  */
164                 descriptor_length =   *descriptor;
165                 descriptor_type =     *(descriptor + 1);
166                 descriptor_subtype =  *(descriptor + 2);
167 
168                 /* Make sure this descriptor has at least the minimum length.  */
169                 if (descriptor_length < 3)
170                 {
171 
172                     /* Error trap. */
173                     _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                     _ux_utility_memory_free(saved_descriptor);
180 
181                     /* Descriptor is corrupted.  */
182                     return(UX_DESCRIPTOR_CORRUPTED);
183                 }
184 
185                 /* Process relative to descriptor type.  */
186                 switch (descriptor_type)
187                 {
188 
189 
190                 case UX_INTERFACE_DESCRIPTOR_ITEM:
191 
192                     /* Parse the interface descriptor and make it machine independent.  */
193                     _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                     if (interface_descriptor.bInterfaceNumber ==
198                                 cdc_acm -> ux_host_class_cdc_acm_interface ->
199                                     ux_interface_descriptor.bInterfaceNumber)
200                     {
201 
202                         /* Mark we have found it.  */
203                         interface_found =  UX_TRUE;
204 
205                     }
206                     else
207                     {
208 
209                         /* Haven't found it.  */
210                         interface_found =  UX_FALSE;
211                     }
212                     break;
213 
214 
215                 case UX_HOST_CLASS_CDC_ACM_CS_INTERFACE:
216 
217                     /* First make sure we have found the correct generic interface descriptor.  */
218                     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                         cdc_acm -> ux_host_class_cdc_acm_capabilities  = *(descriptor + UX_HOST_CLASS_CDC_ACM_CALL_MANAGEMENT_CAPABILITIES);
223 
224 
225                     }
226                     break;
227                 }
228 
229                 /* Verify if the descriptor is still valid.  */
230                 if (descriptor_length > total_descriptor_length)
231                 {
232 
233                     /* Error trap. */
234                     _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                     _ux_utility_memory_free(saved_descriptor);
241 
242                     return(UX_DESCRIPTOR_CORRUPTED);
243                 }
244 
245                 /* Jump to the next descriptor if we have not reached the end.  */
246                 descriptor +=  descriptor_length;
247 
248                 /* And adjust the length left to parse in the descriptor.  */
249                 total_descriptor_length -=  descriptor_length;
250             }
251 
252             /* We can free the resource now.  */
253             _ux_utility_memory_free(saved_descriptor);
254 
255             return(UX_SUCCESS);
256         }
257     }
258 
259     /* Free all used resources.  */
260     _ux_utility_memory_free(saved_descriptor);
261 
262     /* Return completion status.  */
263     return(status);
264 }
265 
266