1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
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 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            used UX prefix to refer to  */
72 /*                                            TX symbols instead of using */
73 /*                                            them directly,              */
74 /*                                            resulting in version 6.1    */
75 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            fixed standalone compile,   */
77 /*                                            resulting in version 6.1.11 */
78 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            fixed parameter/variable    */
80 /*                                            names conflict C++ keyword, */
81 /*                                            resulting in version 6.1.12 */
82 /*                                                                        */
83 /**************************************************************************/
_ux_device_class_cdc_ecm_deactivate(UX_SLAVE_CLASS_COMMAND * command)84 UINT  _ux_device_class_cdc_ecm_deactivate(UX_SLAVE_CLASS_COMMAND *command)
85 {
86 
87 UX_SLAVE_CLASS_CDC_ECM      *cdc_ecm;
88 UX_SLAVE_INTERFACE          *interface_ptr;
89 UX_SLAVE_CLASS              *class_ptr;
90 
91     /* Get the class container.  */
92     class_ptr =  command -> ux_slave_class_command_class_ptr;
93 
94     /* Get the class instance in the container.  */
95     cdc_ecm = (UX_SLAVE_CLASS_CDC_ECM *) class_ptr -> ux_slave_class_instance;
96 
97     /* Get the interface that owns this instance.  Normally the interface can be derived
98        from the class instance but since CDC_ECM has 2 interfaces and we only store the Control
99        interface in the class container, we used the class_command pointer to retrieve the
100        correct interface which issued the deactivation. */
101     interface_ptr =  (UX_SLAVE_INTERFACE  *) command -> ux_slave_class_command_interface;
102 
103     /* Check if this is the Control or Data interface.  We only need to dismount the link and abort the
104        transfer once for the 2 classes.  */
105     if (interface_ptr -> ux_slave_interface_descriptor.bInterfaceClass == UX_DEVICE_CLASS_CDC_ECM_CLASS_COMMUNICATION_CONTROL)
106     {
107 
108         /* Is the link state up?  */
109         if (cdc_ecm -> ux_slave_class_cdc_ecm_link_state == UX_DEVICE_CLASS_CDC_ECM_LINK_STATE_UP)
110         {
111 
112             /* Then we've found the bulk endpoints and started the threads.  */
113 
114             /* Abort transfers. Note that since the bulk out thread is most likely waiting for
115                a transfer from the host, this will allow it to resume and suspend itself.  */
116             _ux_device_stack_transfer_all_request_abort(cdc_ecm -> ux_slave_class_cdc_ecm_bulkin_endpoint, UX_TRANSFER_BUS_RESET);
117             _ux_device_stack_transfer_all_request_abort(cdc_ecm -> ux_slave_class_cdc_ecm_bulkout_endpoint, UX_TRANSFER_BUS_RESET);
118 
119             /* Declare the link to be down. That may need to change later to make it dependent on the
120                WAN/Wireless modem.  */
121             cdc_ecm -> ux_slave_class_cdc_ecm_link_state = UX_DEVICE_CLASS_CDC_ECM_LINK_STATE_DOWN;
122 
123             /* Is there an interrupt endpoint?  */
124             if (cdc_ecm -> ux_slave_class_cdc_ecm_interrupt_endpoint != UX_NULL)
125 
126                 /* Abort the transfers on the interrupt endpoint as well.  */
127                 _ux_device_stack_transfer_all_request_abort(cdc_ecm -> ux_slave_class_cdc_ecm_interrupt_endpoint, UX_TRANSFER_BUS_RESET);
128 
129             /* Wake up the bulk in thread so it will release the NetX resources used and suspend.  */
130             _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);
131 
132             /* If there is a deactivate function call it.  */
133             if (cdc_ecm -> ux_slave_class_cdc_ecm_parameter.ux_slave_class_cdc_ecm_instance_deactivate != UX_NULL)
134 
135                 /* Invoke the application.  */
136                 cdc_ecm -> ux_slave_class_cdc_ecm_parameter.ux_slave_class_cdc_ecm_instance_deactivate(cdc_ecm);
137 
138             /* Deregister this interface to the NetX USB interface broker.  */
139             _ux_network_driver_deactivate((VOID *) cdc_ecm, cdc_ecm -> ux_slave_class_cdc_ecm_network_handle);
140         }
141         else
142         {
143 
144             /* The link state is down.  */
145 
146             /* Did activation succeed?  */
147             if (cdc_ecm -> ux_slave_class_cdc_ecm_bulkin_endpoint != UX_NULL && cdc_ecm -> ux_slave_class_cdc_ecm_bulkout_endpoint != UX_NULL)
148             {
149 
150                 /* The only thing we need to do is deregister this interface to the NetX USB interface broker.  */
151                 _ux_network_driver_deactivate((VOID *) cdc_ecm, cdc_ecm -> ux_slave_class_cdc_ecm_network_handle);
152             }
153             else
154             {
155 
156                 /* Activation did not succeed. Nothing to do.  */
157             }
158         }
159     }
160 
161     /* If trace is enabled, insert this event into the trace buffer.  */
162     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_CDC_ECM_DEACTIVATE, cdc_ecm, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
163 
164     /* If trace is enabled, register this object.  */
165     UX_TRACE_OBJECT_UNREGISTER(cdc_ecm);
166 
167     /* Return completion status.  */
168     return(UX_SUCCESS);
169 }
170 
171