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_disconnect                         PORTABLE C      */
36 /*                                                           6.1.12       */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function is called when the device gets disconnected from the  */
44 /*    host. All the device resources are freed.                           */
45 /*                                                                        */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    None                                                                */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    (ux_slave_class_entry_function)       Device class entry function   */
58 /*    (ux_slave_dcd_function)               DCD dispatch function         */
59 /*    _ux_device_stack_interface_delete     Delete interface              */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application                                                         */
64 /*    Device Stack                                                        */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
71 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            optimized based on compile  */
73 /*                                            definitions,                */
74 /*                                            resulting in version 6.1    */
75 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            fixed parameter/variable    */
77 /*                                            names conflict C++ keyword, */
78 /*                                            resulting in version 6.1.12 */
79 /*                                                                        */
80 /**************************************************************************/
_ux_device_stack_disconnect(VOID)81 UINT  _ux_device_stack_disconnect(VOID)
82 {
83 
84 UX_SLAVE_DCD                *dcd;
85 UX_SLAVE_DEVICE             *device;
86 UX_SLAVE_INTERFACE          *interface_ptr;
87 #if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1
88 UX_SLAVE_INTERFACE          *next_interface;
89 #endif
90 UX_SLAVE_CLASS              *class_ptr;
91 UX_SLAVE_CLASS_COMMAND      class_command;
92 UINT                        status = UX_ERROR;
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     /* If trace is enabled, insert this event into the trace buffer.  */
101     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_STACK_DISCONNECT, device, 0, 0, 0, UX_TRACE_DEVICE_STACK_EVENTS, 0, 0)
102 
103     /* If trace is enabled, register this object.  */
104     UX_TRACE_OBJECT_UNREGISTER(device);
105 
106     /* If the device was in the configured state, there may be interfaces
107        attached to the configuration.  */
108     if (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
109     {
110         /* Get the pointer to the first interface.  */
111         interface_ptr =  device -> ux_slave_device_first_interface;
112 
113 #if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1
114         /* Parse all the interfaces if any.  */
115         while (interface_ptr != UX_NULL)
116         {
117 #endif
118 
119             /* Build all the fields of the Class Command.  */
120             class_command.ux_slave_class_command_request =   UX_SLAVE_CLASS_COMMAND_DEACTIVATE;
121             class_command.ux_slave_class_command_interface =  (VOID *) interface_ptr;
122 
123             /* Get the pointer to the class container of this interface.  */
124             class_ptr =  interface_ptr -> ux_slave_interface_class;
125 
126             /* Store the class container. */
127             class_command.ux_slave_class_command_class_ptr =  class_ptr;
128 
129             /* If there is a class container for this instance, deactivate it.  */
130             if (class_ptr != UX_NULL)
131 
132                 /* Call the class with the DEACTIVATE signal.  */
133                 class_ptr -> ux_slave_class_entry_function(&class_command);
134 
135 #if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1
136             /* Get the next interface.  */
137             next_interface =  interface_ptr -> ux_slave_interface_next_interface;
138 #endif
139 
140             /* Remove the interface and all endpoints associated with it.  */
141             _ux_device_stack_interface_delete(interface_ptr);
142 
143 #if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1
144             /* Now we refresh the interface pointer.  */
145             interface_ptr =  next_interface;
146         }
147 #endif
148 
149         /* Mark the device as attached now.  */
150         device -> ux_slave_device_state =  UX_DEVICE_ATTACHED;
151     }
152 
153     /* If the device was attached, we need to destroy the control endpoint.  */
154     if (device -> ux_slave_device_state == UX_DEVICE_ATTACHED)
155 
156         /* Now we can destroy the default control endpoint.  */
157         status =  dcd -> ux_slave_dcd_function(dcd, UX_DCD_DESTROY_ENDPOINT,
158                                 (VOID *) &device -> ux_slave_device_control_endpoint);
159 
160     /* We are reverting to configuration 0.  */
161     device -> ux_slave_device_configuration_selected =  0;
162 
163     /* Set the device to be non attached.  */
164     device -> ux_slave_device_state =  UX_DEVICE_RESET;
165 
166     /* Check the status change callback.  */
167     if(_ux_system_slave -> ux_system_slave_change_function != UX_NULL)
168     {
169 
170         /* Inform the application if a callback function was programmed.  */
171         _ux_system_slave -> ux_system_slave_change_function(UX_DEVICE_REMOVED);
172     }
173 
174     /* Return the status to the caller.  */
175     return(status);
176 }
177 
178