1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
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_descriptor_parse       PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function reads the entire configuration descriptor and         */
45 /*    enumerates the interfaces, binds the interface to a class driver... */
46 /*    if the device has multiple configurations, we read all the          */
47 /*    configurations but do not instantiate any configuration. Rather we  */
48 /*    let a class driver do the work.                                     */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    device                                Pointer to device             */
53 /*    configuration                         Pointer to configuration      */
54 /*    configuration_index                   Index of configuration        */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    Completion Status                                                   */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    _ux_host_stack_interfaces_scan        Scan host interfaces          */
63 /*    _ux_host_stack_transfer_request       Process transfer request      */
64 /*    _ux_utility_memory_allocate           Allocate block of memory      */
65 /*    _ux_utility_memory_free               Free block of memory          */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    USBX Components                                                     */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
76 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*                                                                        */
79 /**************************************************************************/
_ux_host_stack_configuration_descriptor_parse(UX_DEVICE * device,UX_CONFIGURATION * configuration,UINT configuration_index)80 UINT  _ux_host_stack_configuration_descriptor_parse(UX_DEVICE *device, UX_CONFIGURATION *configuration,
81                                                                         UINT configuration_index)
82 {
83 
84 UX_TRANSFER     *transfer_request;
85 UINT            status;
86 UCHAR           *descriptor;
87 UX_ENDPOINT     *control_endpoint;
88 ULONG           total_configuration_length;
89 
90 
91     /* Retrieve the pointer to the control endpoint and its transfer_request.  */
92     control_endpoint =  &device -> ux_device_control_endpoint;
93     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
94 
95     /* Retrieve the size of all the configuration descriptor.  */
96     total_configuration_length =  configuration -> ux_configuration_descriptor.wTotalLength;
97 
98     /* Allocate enough memory to read all descriptors attached to this configuration.  */
99     descriptor =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, total_configuration_length);
100 
101     /* Determine if the memory was allocated.  */
102     if (descriptor == UX_NULL)
103     {
104 
105         /* No, return an error.  */
106         return(UX_MEMORY_INSUFFICIENT);
107     }
108     else
109     {
110 
111         /* Create a transfer_request for the GET_DESCRIPTOR request.  */
112         transfer_request -> ux_transfer_request_data_pointer =      descriptor;
113         transfer_request -> ux_transfer_request_requested_length =  total_configuration_length;
114         transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
115         transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
116         transfer_request -> ux_transfer_request_value =             configuration_index | (UINT)(UX_CONFIGURATION_DESCRIPTOR_ITEM << 8);
117         transfer_request -> ux_transfer_request_index =             0;
118 
119         /* Send request to HCD layer.  */
120         status =  _ux_host_stack_transfer_request(transfer_request);
121 
122         /* Check for correct transfer and entire descriptor returned.  */
123         if((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == total_configuration_length))
124         {
125 
126             /* The entire descriptor now contains the configuration descriptor,
127                the interface(s) descriptors, all alternate settings, endpoints
128                and descriptor specific to the class. The descriptor is parsed for all interfaces.  */
129             status =  _ux_host_stack_interfaces_scan(configuration, descriptor);
130         }
131     }
132 
133     /* Free all used resources.  */
134     _ux_utility_memory_free(descriptor);
135 
136     /* Return completion status.  */
137     return(status);
138 }
139 
140