GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_device_stack_disconnect.c Lines: 25 25 100.0 %
Date: 2026-03-06 18:57:10 Branches: 10 10 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
10
 **************************************************************************/
11
12
13
/**************************************************************************/
14
/**************************************************************************/
15
/**                                                                       */
16
/** USBX Component                                                        */
17
/**                                                                       */
18
/**   Device Stack                                                        */
19
/**                                                                       */
20
/**************************************************************************/
21
/**************************************************************************/
22
23
#define UX_SOURCE_CODE
24
25
26
/* Include necessary system files.  */
27
28
#include "ux_api.h"
29
#include "ux_device_stack.h"
30
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_device_stack_disconnect                         PORTABLE C      */
37
/*                                                           6.1.12       */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function is called when the device gets disconnected from the  */
45
/*    host. All the device resources are freed.                           */
46
/*                                                                        */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    None                                                                */
51
/*                                                                        */
52
/*  OUTPUT                                                                */
53
/*                                                                        */
54
/*    Completion Status                                                   */
55
/*                                                                        */
56
/*  CALLS                                                                 */
57
/*                                                                        */
58
/*    (ux_slave_class_entry_function)       Device class entry function   */
59
/*    (ux_slave_dcd_function)               DCD dispatch function         */
60
/*    _ux_device_stack_interface_delete     Delete interface              */
61
/*                                                                        */
62
/*  CALLED BY                                                             */
63
/*                                                                        */
64
/*    Application                                                         */
65
/*    Device Stack                                                        */
66
/*                                                                        */
67
/**************************************************************************/
68
1464
UINT  _ux_device_stack_disconnect(VOID)
69
{
70
71
UX_SLAVE_DCD                *dcd;
72
UX_SLAVE_DEVICE             *device;
73
UX_SLAVE_INTERFACE          *interface_ptr;
74
#if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1
75
UX_SLAVE_INTERFACE          *next_interface;
76
#endif
77
UX_SLAVE_CLASS              *class_ptr;
78
UX_SLAVE_CLASS_COMMAND      class_command;
79
1464
UINT                        status = UX_ERROR;
80
81
    /* Get the pointer to the DCD.  */
82
1464
    dcd =  &_ux_system_slave -> ux_system_slave_dcd;
83
84
    /* Get the pointer to the device.  */
85
1464
    device =  &_ux_system_slave -> ux_system_slave_device;
86
87
    /* If trace is enabled, insert this event into the trace buffer.  */
88
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_STACK_DISCONNECT, device, 0, 0, 0, UX_TRACE_DEVICE_STACK_EVENTS, 0, 0)
89
90
    /* If trace is enabled, register this object.  */
91
    UX_TRACE_OBJECT_UNREGISTER(device);
92
93
    /* If the device was in the configured state, there may be interfaces
94
       attached to the configuration.  */
95
1464
    if (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
96
    {
97
        /* Get the pointer to the first interface.  */
98
829
        interface_ptr =  device -> ux_slave_device_first_interface;
99
100
#if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1
101
        /* Parse all the interfaces if any.  */
102
2242
        while (interface_ptr != UX_NULL)
103
        {
104
#endif
105
106
            /* Build all the fields of the Class Command.  */
107
1413
            class_command.ux_slave_class_command_request =   UX_SLAVE_CLASS_COMMAND_DEACTIVATE;
108
1413
            class_command.ux_slave_class_command_interface =  (VOID *) interface_ptr;
109
110
            /* Get the pointer to the class container of this interface.  */
111
1413
            class_ptr =  interface_ptr -> ux_slave_interface_class;
112
113
            /* Store the class container. */
114
1413
            class_command.ux_slave_class_command_class_ptr =  class_ptr;
115
116
            /* If there is a class container for this instance, deactivate it.  */
117
1413
            if (class_ptr != UX_NULL)
118
119
                /* Call the class with the DEACTIVATE signal.  */
120
1262
                class_ptr -> ux_slave_class_entry_function(&class_command);
121
122
#if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1
123
            /* Get the next interface.  */
124
1413
            next_interface =  interface_ptr -> ux_slave_interface_next_interface;
125
#endif
126
127
            /* Remove the interface and all endpoints associated with it.  */
128
1413
            _ux_device_stack_interface_delete(interface_ptr);
129
130
#if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1
131
            /* Now we refresh the interface pointer.  */
132
1413
            interface_ptr =  next_interface;
133
        }
134
#endif
135
136
        /* Mark the device as attached now.  */
137
829
        device -> ux_slave_device_state =  UX_DEVICE_ATTACHED;
138
    }
139
140
    /* If the device was attached, we need to destroy the control endpoint.  */
141
1464
    if (device -> ux_slave_device_state == UX_DEVICE_ATTACHED)
142
143
        /* Now we can destroy the default control endpoint.  */
144
1454
        status =  dcd -> ux_slave_dcd_function(dcd, UX_DCD_DESTROY_ENDPOINT,
145
1454
                                (VOID *) &device -> ux_slave_device_control_endpoint);
146
147
    /* We are reverting to configuration 0.  */
148
1464
    device -> ux_slave_device_configuration_selected =  0;
149
150
    /* Set the device to be non attached.  */
151
1464
    device -> ux_slave_device_state =  UX_DEVICE_RESET;
152
153
    /* Check the status change callback.  */
154
1464
    if(_ux_system_slave -> ux_system_slave_change_function != UX_NULL)
155
    {
156
157
        /* Inform the application if a callback function was programmed.  */
158
59
        _ux_system_slave -> ux_system_slave_change_function(UX_DEVICE_REMOVED);
159
    }
160
161
    /* Return the status to the caller.  */
162
1464
    return(status);
163
}
164