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_interface_delete                   PORTABLE C      */
36 /*                                                           6.1.12       */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function deletes an interface. Semaphore and memory are        */
44 /*    released and the controller driver is invoked to disable the        */
45 /*    hardware endpoint.  The interface is then removed from the          */
46 /*    configuration.                                                      */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    interface                             Pointer to interface          */
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 /*    Application                                                         */
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 /*                                            resulting in version 6.1    */
71 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            fixed parameter/variable    */
73 /*                                            names conflict C++ keyword, */
74 /*                                            resulting in version 6.1.12 */
75 /*                                                                        */
76 /**************************************************************************/
_ux_device_stack_interface_delete(UX_SLAVE_INTERFACE * interface_ptr)77 UINT  _ux_device_stack_interface_delete(UX_SLAVE_INTERFACE *interface_ptr)
78 {
79 
80 UX_SLAVE_DCD            *dcd;
81 UX_SLAVE_DEVICE         *device;
82 UX_SLAVE_ENDPOINT       *endpoint;
83 UX_SLAVE_ENDPOINT       *next_endpoint;
84 
85     /* If trace is enabled, register this object.  */
86     UX_TRACE_OBJECT_UNREGISTER(interface_ptr);
87 
88     /* If trace is enabled, insert this event into the trace buffer.  */
89     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_STACK_INTERFACE_DELETE, interface_ptr, 0, 0, 0, UX_TRACE_DEVICE_STACK_EVENTS, 0, 0)
90 
91     /* Get the pointer to the device.  */
92     device =  &_ux_system_slave -> ux_system_slave_device;
93 
94     /* Find the first endpoints associated with this interface.  */
95     next_endpoint =  interface_ptr -> ux_slave_interface_first_endpoint;
96 
97     /* Parse all the endpoints.  */
98     while (next_endpoint != UX_NULL)
99     {
100 
101         /* Save this endpoint.  */
102         endpoint =  next_endpoint;
103 
104         /* Find the next endpoint.  */
105         next_endpoint =  endpoint -> ux_slave_endpoint_next_endpoint;
106 
107         /* Get the pointer to the DCD.  */
108         dcd =  &_ux_system_slave->ux_system_slave_dcd;
109 
110         /* The endpoint must be destroyed.  */
111         dcd -> ux_slave_dcd_function(dcd, UX_DCD_DESTROY_ENDPOINT, endpoint);
112 
113         /* Free the endpoint.  */
114         endpoint -> ux_slave_endpoint_status =  UX_UNUSED;
115 
116         /* Make sure the endpoint instance is now cleaned up.  */
117         endpoint -> ux_slave_endpoint_state =  0;
118         endpoint -> ux_slave_endpoint_next_endpoint =  UX_NULL;
119         endpoint -> ux_slave_endpoint_interface =  UX_NULL;
120         endpoint -> ux_slave_endpoint_device =  UX_NULL;
121     }
122 
123     /* It's always from first one (to delete).  */
124     /* Rebuild the first link.  */
125     device -> ux_slave_device_first_interface =  interface_ptr -> ux_slave_interface_next_interface;
126 
127     /* The interface is removed from the link, its memory must be cleaned and returned to the pool.  */
128     interface_ptr -> ux_slave_interface_class          =  UX_NULL;
129     interface_ptr -> ux_slave_interface_class_instance =  UX_NULL;
130     interface_ptr -> ux_slave_interface_next_interface =  UX_NULL;
131     interface_ptr -> ux_slave_interface_first_endpoint =  UX_NULL;
132     interface_ptr -> ux_slave_interface_status         =  UX_UNUSED;
133 
134     /* Return successful completion.  */
135     return(UX_SUCCESS);
136 }
137 
138