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