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_device_configuration_reset           PORTABLE C      */
37 /*                                                           6.1.12       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function resets the configuration of the device to zero.       */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    configuration                          Pointer to configuration     */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    Completion Status                                                   */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _ux_host_stack_configuration_instance_delete                        */
57 /*                                           Delete configuration instance*/
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    USBX Components                                                     */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
68 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
69 /*                                            resulting in version 6.1    */
70 /*  02-02-2021     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            used pointer for current    */
72 /*                                            selected configuration,     */
73 /*                                            resulting in version 6.1.4  */
74 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            fixed device state support, */
76 /*                                            reset device power source,  */
77 /*                                            resulting in version 6.1.10 */
78 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            reset shared device config  */
80 /*                                            descriptor for enum scan,   */
81 /*                                            resulting in version 6.1.12 */
82 /*                                                                        */
83 /**************************************************************************/
_ux_host_stack_device_configuration_reset(UX_DEVICE * device)84 UINT  _ux_host_stack_device_configuration_reset(UX_DEVICE *device)
85 {
86 
87 UX_TRANSFER             *transfer_request;
88 UX_ENDPOINT             *control_endpoint;
89 UX_CONFIGURATION        *current_configuration;
90 UINT                    status;
91 
92     /* If trace is enabled, insert this event into the trace buffer.  */
93     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_DEVICE_CONFIGURATION_SELECT, device, 0, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
94 
95     /* A configuration is selected. Retrieve the pointer to the control endpoint
96        and its transfer request.  */
97     control_endpoint =  &device -> ux_device_control_endpoint;
98     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
99 
100     /* Check for the state of the device . If the device is already configured,
101        we need to cancel the existing configuration before resetting it.   */
102     if (device -> ux_device_state == UX_DEVICE_CONFIGURED)
103     {
104 
105         /* The device is configured. Get the first configuration pointer.  */
106         current_configuration =  device -> ux_device_current_configuration;
107 
108         /* Deselect this instance */
109         _ux_host_stack_configuration_instance_delete(current_configuration);
110     }
111 
112     /* No configuration is selected now.  */
113     device -> ux_device_current_configuration = UX_NULL;
114 
115     /* Packed descriptor are not valid now.  */
116     if (device -> ux_device_packed_configuration)
117     {
118         _ux_utility_memory_free(device -> ux_device_packed_configuration);
119         device -> ux_device_packed_configuration = UX_NULL;
120         device -> ux_device_packed_configuration_keep_count = 0;
121     }
122 
123     /* Set state of device to ADDRESSED.  */
124     device -> ux_device_state = UX_DEVICE_ADDRESSED;
125 
126     /* Reset power source.  */
127     device -> ux_device_power_source = UX_DEVICE_BUS_POWERED;
128 
129     /* Create a transfer_request for the SET_CONFIGURATION request. No data for this request.  */
130     transfer_request -> ux_transfer_request_requested_length =  0;
131     transfer_request -> ux_transfer_request_function =          UX_SET_CONFIGURATION;
132     transfer_request -> ux_transfer_request_type =              UX_REQUEST_OUT | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
133     transfer_request -> ux_transfer_request_value =             0;
134     transfer_request -> ux_transfer_request_index =             0;
135 
136     /* Send request to HCD layer.  */
137     status =  _ux_host_stack_transfer_request(transfer_request);
138 
139     /* Return status.  */
140     return(status);
141 }
142 
143