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_endpoint_stall                     PORTABLE C      */
36 /*                                                           6.1.10       */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function stalls an endpoint. The host will need to reissue     */
44 /*    a reset to clear the endpoint.                                      */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    endpoint                              Pointer to endpoint           */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    Completion Status                                                   */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    (ux_slave_dcd_function)               DCD dispatch function         */
57 /*                                                                        */
58 /*  CALLED BY                                                             */
59 /*                                                                        */
60 /*    Application                                                         */
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 /*                                            used UX prefix to refer to  */
69 /*                                            TX symbols instead of using */
70 /*                                            them directly,              */
71 /*                                            resulting in version 6.1    */
72 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            added standalone support,   */
74 /*                                            resulting in version 6.1.10 */
75 /*                                                                        */
76 /**************************************************************************/
_ux_device_stack_endpoint_stall(UX_SLAVE_ENDPOINT * endpoint)77 UINT  _ux_device_stack_endpoint_stall(UX_SLAVE_ENDPOINT *endpoint)
78 {
79 
80 UX_INTERRUPT_SAVE_AREA
81 
82 UX_SLAVE_DCD        *dcd;
83 UINT                status;
84 
85     /* If trace is enabled, insert this event into the trace buffer.  */
86     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_STACK_ENDPOINT_STALL, endpoint, 0, 0, 0, UX_TRACE_DEVICE_STACK_EVENTS, 0, 0)
87 
88     /* Get the pointer to the DCD.  */
89     dcd =  &_ux_system_slave -> ux_system_slave_dcd;
90 
91     /* Assume device is in an invalid state here in order to reduce code in following
92        section where interrupts are disabled.  */
93     status =  UX_ERROR;
94 
95     /* Ensure we don't change the endpoint's state after disconnection routine
96        resets it.  */
97     UX_DISABLE
98 
99     /* Check if the device is in a valid state; as soon as the device is out
100        of the RESET state, transfers occur and thus endpoints may be stalled. */
101     if (_ux_system_slave -> ux_system_slave_device.ux_slave_device_state != UX_DEVICE_RESET &&
102         endpoint -> ux_slave_endpoint_state != UX_ENDPOINT_HALTED)
103     {
104 
105         /* Stall the endpoint.  */
106         status =  dcd -> ux_slave_dcd_function(dcd, UX_DCD_STALL_ENDPOINT, endpoint);
107 
108         /* Mark the endpoint state.  */
109         if ((endpoint -> ux_slave_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) !=
110             UX_CONTROL_ENDPOINT)
111             endpoint -> ux_slave_endpoint_state =  UX_ENDPOINT_HALTED;
112     }
113 
114     /* Restore interrupts.  */
115     UX_RESTORE
116 
117     /* Return completion status.  */
118     return(status);
119 }
120 
121