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 /**   Device Stack                                                        */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define UX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "ux_api.h"
28 #include "ux_device_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_stack_clear_feature                      PORTABLE C      */
36 /*                                                           6.1.12       */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function clears a specific feature (Device, Interface,         */
44 /*    Endpoint ....).                                                     */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    request_type                          Request type                  */
49 /*    request_value                         Request value                 */
50 /*    request_index                         Request index                 */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    (ux_slave_dcd_function)               DCD dispatch function         */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    Device Stack                                                        */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
69 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            optimized based on compile  */
71 /*                                            definitions,                */
72 /*                                            resulting in version 6.1    */
73 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            fixed parameter/variable    */
75 /*                                            names conflict C++ keyword, */
76 /*                                            resulting in version 6.1.12 */
77 /*                                                                        */
78 /**************************************************************************/
_ux_device_stack_clear_feature(ULONG request_type,ULONG request_value,ULONG request_index)79 UINT  _ux_device_stack_clear_feature(ULONG request_type, ULONG request_value, ULONG request_index)
80 {
81 
82 UX_SLAVE_DCD            *dcd;
83 UX_SLAVE_DEVICE         *device;
84 UX_SLAVE_INTERFACE      *interface_ptr;
85 UX_SLAVE_ENDPOINT       *endpoint;
86 UX_SLAVE_ENDPOINT       *endpoint_target;
87 
88     UX_PARAMETER_NOT_USED(request_value);
89 
90     /* If trace is enabled, insert this event into the trace buffer.  */
91     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_STACK_CLEAR_FEATURE, request_type, request_value, request_index, 0, UX_TRACE_DEVICE_STACK_EVENTS, 0, 0)
92 
93     /* Get the pointer to the DCD.  */
94     dcd =  &_ux_system_slave -> ux_system_slave_dcd;
95 
96     /* Get the pointer to the device.  */
97     device =  &_ux_system_slave -> ux_system_slave_device;
98 
99     /* Get the control endpoint for the device.  */
100     endpoint =  &device -> ux_slave_device_control_endpoint;
101 
102     /* The request can be for either the device or the endpoint.  */
103     switch (request_type & UX_REQUEST_TARGET)
104     {
105 
106     case UX_REQUEST_TARGET_DEVICE:
107 
108         /* Check if we have a DEVICE_REMOTE_WAKEUP Feature.  */
109         if (request_value == UX_REQUEST_FEATURE_DEVICE_REMOTE_WAKEUP)
110         {
111 
112             /* Check if we have the capability. */
113             if (_ux_system_slave -> ux_system_slave_remote_wakeup_capability)
114             {
115 
116                 /* Disable the feature. */
117                 _ux_system_slave -> ux_system_slave_remote_wakeup_enabled = UX_FALSE;
118             }
119 
120             else
121 
122                 /* Protocol error. */
123                 return (UX_FUNCTION_NOT_SUPPORTED);
124         }
125 
126         break;
127 
128     case UX_REQUEST_TARGET_ENDPOINT:
129 
130         /* The only clear feature for endpoint is ENDPOINT_STALL. This clears
131            the endpoint of the stall situation and resets its data toggle.
132            We need to find the endpoint through the interface(s). */
133         interface_ptr =  device -> ux_slave_device_first_interface;
134 
135 #if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1
136         while (interface_ptr != UX_NULL)
137         {
138 #endif
139 
140             /* Get the first endpoint for this interface.  */
141             endpoint_target =  interface_ptr -> ux_slave_interface_first_endpoint;
142 
143             /* Parse all the endpoints.  */
144             while (endpoint_target != UX_NULL)
145             {
146 
147                 /* Check the endpoint index.  */
148                 if (endpoint_target -> ux_slave_endpoint_descriptor.bEndpointAddress == request_index)
149                 {
150 
151                     /* Reset the endpoint.  */
152                     dcd -> ux_slave_dcd_function(dcd, UX_DCD_RESET_ENDPOINT, endpoint_target);
153 
154                     /* Mark its state now.  */
155                     endpoint_target -> ux_slave_endpoint_state = UX_ENDPOINT_RESET;
156 
157                     /* Return the function status.  */
158                     return(UX_SUCCESS);
159                 }
160 
161                 /* Next endpoint.  */
162                 endpoint_target =  endpoint_target -> ux_slave_endpoint_next_endpoint;
163             }
164 
165 #if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1
166             /* Next interface.  */
167             interface_ptr =  interface_ptr -> ux_slave_interface_next_interface;
168         }
169 #endif
170 
171         /* Intentional fallthrough and go into the default case. */
172         /* fall through */
173 
174     /* We get here when the endpoint is wrong. Should not happen though.  */
175     default:
176 
177         /* We stall the command.  */
178         dcd -> ux_slave_dcd_function(dcd, UX_DCD_STALL_ENDPOINT, endpoint);
179 
180         /* No more work to do here.  The command failed but the upper layer does not depend on it.  */
181         return(UX_SUCCESS);
182     }
183 
184     /* Return the function status.  */
185     return(UX_SUCCESS);
186 }
187 
188