GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_device_classes/src/ux_device_class_cdc_ecm_deactivate.c Lines: 18 18 100.0 %
Date: 2026-03-06 18:57:10 Branches: 12 12 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
/** USBX Component                                                        */
16
/**                                                                       */
17
/**   Device CDC_ECM Class                                                */
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_class_cdc_ecm.h"
29
#include "ux_device_stack.h"
30
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_device_class_cdc_ecm_deactivate                 PORTABLE C      */
37
/*                                                           6.1.12       */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function deactivate an instance of the cdc_ecm class.          */
45
/*                                                                        */
46
/*  INPUT                                                                 */
47
/*                                                                        */
48
/*    command                               Pointer to a class command    */
49
/*                                                                        */
50
/*  OUTPUT                                                                */
51
/*                                                                        */
52
/*    Completion Status                                                   */
53
/*                                                                        */
54
/*  CALLS                                                                 */
55
/*                                                                        */
56
/*    _ux_device_stack_transfer_all_request_abort                         */
57
/*                                          Abort all transfers           */
58
/*    _ux_device_event_flags_set            Set event flags               */
59
/*    _ux_network_driver_deactivate         Deactivate NetX USB interface */
60
/*                                                                        */
61
/*  CALLED BY                                                             */
62
/*                                                                        */
63
/*    CDC_ECM Class                                                       */
64
/*                                                                        */
65
/**************************************************************************/
66
236
UINT  _ux_device_class_cdc_ecm_deactivate(UX_SLAVE_CLASS_COMMAND *command)
67
{
68
69
UX_SLAVE_CLASS_CDC_ECM      *cdc_ecm;
70
UX_SLAVE_INTERFACE          *interface_ptr;
71
UX_SLAVE_CLASS              *class_ptr;
72
73
    /* Get the class container.  */
74
236
    class_ptr =  command -> ux_slave_class_command_class_ptr;
75
76
    /* Get the class instance in the container.  */
77
236
    cdc_ecm = (UX_SLAVE_CLASS_CDC_ECM *) class_ptr -> ux_slave_class_instance;
78
79
    /* Get the interface that owns this instance.  Normally the interface can be derived
80
       from the class instance but since CDC_ECM has 2 interfaces and we only store the Control
81
       interface in the class container, we used the class_command pointer to retrieve the
82
       correct interface which issued the deactivation. */
83
236
    interface_ptr =  (UX_SLAVE_INTERFACE  *) command -> ux_slave_class_command_interface;
84
85
    /* Check if this is the Control or Data interface.  We only need to dismount the link and abort the
86
       transfer once for the 2 classes.  */
87
236
    if (interface_ptr -> ux_slave_interface_descriptor.bInterfaceClass == UX_DEVICE_CLASS_CDC_ECM_CLASS_COMMUNICATION_CONTROL)
88
    {
89
90
        /* Is the link state up?  */
91
121
        if (cdc_ecm -> ux_slave_class_cdc_ecm_link_state == UX_DEVICE_CLASS_CDC_ECM_LINK_STATE_UP)
92
        {
93
94
            /* Then we've found the bulk endpoints and started the threads.  */
95
96
            /* Abort transfers. Note that since the bulk out thread is most likely waiting for
97
               a transfer from the host, this will allow it to resume and suspend itself.  */
98
88
            _ux_device_stack_transfer_all_request_abort(cdc_ecm -> ux_slave_class_cdc_ecm_bulkin_endpoint, UX_TRANSFER_BUS_RESET);
99
88
            _ux_device_stack_transfer_all_request_abort(cdc_ecm -> ux_slave_class_cdc_ecm_bulkout_endpoint, UX_TRANSFER_BUS_RESET);
100
101
            /* Declare the link to be down. That may need to change later to make it dependent on the
102
               WAN/Wireless modem.  */
103
88
            cdc_ecm -> ux_slave_class_cdc_ecm_link_state = UX_DEVICE_CLASS_CDC_ECM_LINK_STATE_DOWN;
104
105
            /* Is there an interrupt endpoint?  */
106
88
            if (cdc_ecm -> ux_slave_class_cdc_ecm_interrupt_endpoint != UX_NULL)
107
108
                /* Abort the transfers on the interrupt endpoint as well.  */
109
87
                _ux_device_stack_transfer_all_request_abort(cdc_ecm -> ux_slave_class_cdc_ecm_interrupt_endpoint, UX_TRANSFER_BUS_RESET);
110
111
            /* Wake up the bulk in thread so it will release the NetX resources used and suspend.  */
112
88
            _ux_device_event_flags_set(&cdc_ecm -> ux_slave_class_cdc_ecm_event_flags_group, UX_DEVICE_CLASS_CDC_ECM_NEW_DEVICE_STATE_CHANGE_EVENT, UX_OR);
113
114
            /* If there is a deactivate function call it.  */
115
88
            if (cdc_ecm -> ux_slave_class_cdc_ecm_parameter.ux_slave_class_cdc_ecm_instance_deactivate != UX_NULL)
116
117
                /* Invoke the application.  */
118
79
                cdc_ecm -> ux_slave_class_cdc_ecm_parameter.ux_slave_class_cdc_ecm_instance_deactivate(cdc_ecm);
119
120
            /* Deregister this interface to the NetX USB interface broker.  */
121
88
            _ux_network_driver_deactivate((VOID *) cdc_ecm, cdc_ecm -> ux_slave_class_cdc_ecm_network_handle);
122
        }
123
        else
124
        {
125
126
            /* The link state is down.  */
127
128
            /* Did activation succeed?  */
129

33
            if (cdc_ecm -> ux_slave_class_cdc_ecm_bulkin_endpoint != UX_NULL && cdc_ecm -> ux_slave_class_cdc_ecm_bulkout_endpoint != UX_NULL)
130
            {
131
132
                /* The only thing we need to do is deregister this interface to the NetX USB interface broker.  */
133
7
                _ux_network_driver_deactivate((VOID *) cdc_ecm, cdc_ecm -> ux_slave_class_cdc_ecm_network_handle);
134
            }
135
            else
136
            {
137
138
                /* Activation did not succeed. Nothing to do.  */
139
            }
140
        }
141
    }
142
143
    /* If trace is enabled, insert this event into the trace buffer.  */
144
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_CDC_ECM_DEACTIVATE, cdc_ecm, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
145
146
    /* If trace is enabled, register this object.  */
147
    UX_TRACE_OBJECT_UNREGISTER(cdc_ecm);
148
149
    /* Return completion status.  */
150
236
    return(UX_SUCCESS);
151
}
152