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_set_feature                        PORTABLE C      */
36 /*                                                           6.1.12       */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function sets 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 controller 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, stalled on not */
72 /*                                            supported device requests,  */
73 /*                                            resulting in version 6.1    */
74 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            fixed parameter/variable    */
76 /*                                            names conflict C++ keyword, */
77 /*                                            resulting in version 6.1.12 */
78 /*                                                                        */
79 /**************************************************************************/
_ux_device_stack_set_feature(ULONG request_type,ULONG request_value,ULONG request_index)80 UINT  _ux_device_stack_set_feature(ULONG request_type, ULONG request_value, ULONG request_index)
81 {
82 
83 UX_SLAVE_DCD            *dcd;
84 UX_SLAVE_DEVICE         *device;
85 UX_SLAVE_INTERFACE      *interface_ptr;
86 UX_SLAVE_ENDPOINT       *endpoint;
87 UX_SLAVE_ENDPOINT       *endpoint_target;
88 
89     UX_PARAMETER_NOT_USED(request_value);
90 
91     /* If trace is enabled, insert this event into the trace buffer.  */
92     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_STACK_SET_FEATURE, request_value, request_index, 0, 0, UX_TRACE_DEVICE_STACK_EVENTS, 0, 0)
93 
94     /* Get the pointer to the DCD.  */
95     dcd =  &_ux_system_slave -> ux_system_slave_dcd;
96 
97     /* Get the pointer to the device.  */
98     device =  &_ux_system_slave -> ux_system_slave_device;
99 
100     /* Get the control endpoint for the device.  */
101     endpoint =  &device -> ux_slave_device_control_endpoint;
102 
103     /* The feature can be for either the device or the endpoint.  */
104     switch (request_type & UX_REQUEST_TARGET)
105     {
106 
107     case UX_REQUEST_TARGET_DEVICE:
108 
109         /* Check if we have a DEVICE_REMOTE_WAKEUP Feature.  */
110         if (request_value == UX_REQUEST_FEATURE_DEVICE_REMOTE_WAKEUP)
111         {
112 
113             /* Check if we have the capability. */
114             if (_ux_system_slave -> ux_system_slave_remote_wakeup_capability)
115             {
116 
117                 /* Enable the feature. */
118                 _ux_system_slave -> ux_system_slave_remote_wakeup_enabled = UX_TRUE;
119 
120                 /* OK. */
121                 return (UX_SUCCESS);
122             }
123             else
124 
125                 /* Protocol error. */
126                 return (UX_FUNCTION_NOT_SUPPORTED);
127         }
128 
129 #ifdef UX_OTG_SUPPORT
130         /* Check if we have a A_HNP_SUPPORT Feature. This is set when the Host is HNP capable. */
131         if (request_value == UX_OTG_FEATURE_A_HNP_SUPPORT)
132         {
133 
134             /* Store the A_HNP_SUPPORT flag.  */
135             _ux_system_otg -> ux_system_otg_slave_set_feature_flag |= UX_OTG_FEATURE_A_HNP_SUPPORT;
136 
137             /* OK.  */
138             return(UX_SUCCESS);
139         }
140 
141         /* Check if the host asks us to perform HNP.  If also we become the host.  */
142         if (request_value == UX_OTG_FEATURE_B_HNP_ENABLE)
143         {
144 
145             /* The ISR will pick up the suspend event and check if we need to become IDLE or HOST.  */
146             _ux_system_otg -> ux_system_otg_slave_set_feature_flag |= UX_OTG_FEATURE_B_HNP_ENABLE;
147 
148             /* OK.  */
149             return(UX_SUCCESS);
150         }
151 #endif
152 
153         /* Request value not supported.  */
154         return(UX_FUNCTION_NOT_SUPPORTED);
155 
156     case UX_REQUEST_TARGET_ENDPOINT:
157 
158         /* The only set feature for endpoint is ENDPOINT_STALL. This forces
159            the endpoint to the stall situation.
160            We need to find the endpoint through the interface(s). */
161         interface_ptr =  device -> ux_slave_device_first_interface;
162 
163 #if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1
164         while (interface_ptr != UX_NULL)
165         {
166 #endif
167             /* Get the first endpoint for this interface.  */
168             endpoint_target =  interface_ptr -> ux_slave_interface_first_endpoint;
169 
170             /* Parse all the endpoints.  */
171             while (endpoint_target != UX_NULL)
172             {
173 
174                 /* Check the endpoint index.  */
175                 if (endpoint_target -> ux_slave_endpoint_descriptor.bEndpointAddress == request_index)
176                 {
177 
178                     /* Stall the endpoint.  */
179                     dcd -> ux_slave_dcd_function(dcd, UX_DCD_STALL_ENDPOINT, endpoint_target);
180 
181                     /* Return the function status.  */
182                     return(UX_SUCCESS);
183                 }
184 
185                 /* Next endpoint.  */
186                 endpoint_target =  endpoint_target -> ux_slave_endpoint_next_endpoint;
187             }
188 
189 #if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1
190             /* Next interface.  */
191             interface_ptr =  interface_ptr -> ux_slave_interface_next_interface;
192         }
193 #endif
194 
195         /* We get here when the endpoint is wrong. Should not happen though.  */
196         /* Intentionally fall through into the default case. */
197         /* fall through */
198     default:
199 
200         /* We stall the command.  */
201         dcd -> ux_slave_dcd_function(dcd, UX_DCD_STALL_ENDPOINT, endpoint);
202 
203         /* No more work to do here.  The command failed but the upper layer does not depend on it.  */
204         return(UX_SUCCESS);
205     }
206 }
207