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 /** USBX Component                                                        */
15 /**                                                                       */
16 /**   Device CDC_ECM Class                                                */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 #define UX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "ux_api.h"
27 #include "ux_device_class_cdc_ecm.h"
28 #include "ux_device_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_class_cdc_ecm_deactivate                 PORTABLE C      */
36 /*                                                           6.1.12       */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function deactivate an instance of the cdc_ecm class.          */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    command                               Pointer to a class command    */
48 /*                                                                        */
49 /*  OUTPUT                                                                */
50 /*                                                                        */
51 /*    Completion Status                                                   */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*    _ux_device_stack_transfer_all_request_abort                         */
56 /*                                          Abort all transfers           */
57 /*    _ux_device_event_flags_set            Set event flags               */
58 /*    _ux_network_driver_deactivate         Deactivate NetX USB interface */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    CDC_ECM Class                                                       */
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 /*                                            used UX prefix to refer to  */
71 /*                                            TX symbols instead of using */
72 /*                                            them directly,              */
73 /*                                            resulting in version 6.1    */
74 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            fixed standalone compile,   */
76 /*                                            resulting in version 6.1.11 */
77 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            fixed parameter/variable    */
79 /*                                            names conflict C++ keyword, */
80 /*                                            resulting in version 6.1.12 */
81 /*                                                                        */
82 /**************************************************************************/
_ux_device_class_cdc_ecm_deactivate(UX_SLAVE_CLASS_COMMAND * command)83 UINT  _ux_device_class_cdc_ecm_deactivate(UX_SLAVE_CLASS_COMMAND *command)
84 {
85 
86 UX_SLAVE_CLASS_CDC_ECM      *cdc_ecm;
87 UX_SLAVE_INTERFACE          *interface_ptr;
88 UX_SLAVE_CLASS              *class_ptr;
89 
90     /* Get the class container.  */
91     class_ptr =  command -> ux_slave_class_command_class_ptr;
92 
93     /* Get the class instance in the container.  */
94     cdc_ecm = (UX_SLAVE_CLASS_CDC_ECM *) class_ptr -> ux_slave_class_instance;
95 
96     /* Get the interface that owns this instance.  Normally the interface can be derived
97        from the class instance but since CDC_ECM has 2 interfaces and we only store the Control
98        interface in the class container, we used the class_command pointer to retrieve the
99        correct interface which issued the deactivation. */
100     interface_ptr =  (UX_SLAVE_INTERFACE  *) command -> ux_slave_class_command_interface;
101 
102     /* Check if this is the Control or Data interface.  We only need to dismount the link and abort the
103        transfer once for the 2 classes.  */
104     if (interface_ptr -> ux_slave_interface_descriptor.bInterfaceClass == UX_DEVICE_CLASS_CDC_ECM_CLASS_COMMUNICATION_CONTROL)
105     {
106 
107         /* Is the link state up?  */
108         if (cdc_ecm -> ux_slave_class_cdc_ecm_link_state == UX_DEVICE_CLASS_CDC_ECM_LINK_STATE_UP)
109         {
110 
111             /* Then we've found the bulk endpoints and started the threads.  */
112 
113             /* Abort transfers. Note that since the bulk out thread is most likely waiting for
114                a transfer from the host, this will allow it to resume and suspend itself.  */
115             _ux_device_stack_transfer_all_request_abort(cdc_ecm -> ux_slave_class_cdc_ecm_bulkin_endpoint, UX_TRANSFER_BUS_RESET);
116             _ux_device_stack_transfer_all_request_abort(cdc_ecm -> ux_slave_class_cdc_ecm_bulkout_endpoint, UX_TRANSFER_BUS_RESET);
117 
118             /* Declare the link to be down. That may need to change later to make it dependent on the
119                WAN/Wireless modem.  */
120             cdc_ecm -> ux_slave_class_cdc_ecm_link_state = UX_DEVICE_CLASS_CDC_ECM_LINK_STATE_DOWN;
121 
122             /* Is there an interrupt endpoint?  */
123             if (cdc_ecm -> ux_slave_class_cdc_ecm_interrupt_endpoint != UX_NULL)
124 
125                 /* Abort the transfers on the interrupt endpoint as well.  */
126                 _ux_device_stack_transfer_all_request_abort(cdc_ecm -> ux_slave_class_cdc_ecm_interrupt_endpoint, UX_TRANSFER_BUS_RESET);
127 
128             /* Wake up the bulk in thread so it will release the NetX resources used and suspend.  */
129             _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);
130 
131             /* If there is a deactivate function call it.  */
132             if (cdc_ecm -> ux_slave_class_cdc_ecm_parameter.ux_slave_class_cdc_ecm_instance_deactivate != UX_NULL)
133 
134                 /* Invoke the application.  */
135                 cdc_ecm -> ux_slave_class_cdc_ecm_parameter.ux_slave_class_cdc_ecm_instance_deactivate(cdc_ecm);
136 
137             /* Deregister this interface to the NetX USB interface broker.  */
138             _ux_network_driver_deactivate((VOID *) cdc_ecm, cdc_ecm -> ux_slave_class_cdc_ecm_network_handle);
139         }
140         else
141         {
142 
143             /* The link state is down.  */
144 
145             /* Did activation succeed?  */
146             if (cdc_ecm -> ux_slave_class_cdc_ecm_bulkin_endpoint != UX_NULL && cdc_ecm -> ux_slave_class_cdc_ecm_bulkout_endpoint != UX_NULL)
147             {
148 
149                 /* The only thing we need to do is deregister this interface to the NetX USB interface broker.  */
150                 _ux_network_driver_deactivate((VOID *) cdc_ecm, cdc_ecm -> ux_slave_class_cdc_ecm_network_handle);
151             }
152             else
153             {
154 
155                 /* Activation did not succeed. Nothing to do.  */
156             }
157         }
158     }
159 
160     /* If trace is enabled, insert this event into the trace buffer.  */
161     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_CDC_ECM_DEACTIVATE, cdc_ecm, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
162 
163     /* If trace is enabled, register this object.  */
164     UX_TRACE_OBJECT_UNREGISTER(cdc_ecm);
165 
166     /* Return completion status.  */
167     return(UX_SUCCESS);
168 }
169 
170