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 RNDIS 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_rndis.h"
29 #include "ux_device_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_device_class_rndis_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 rndis 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 /*    RNDIS 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 /*  08-02-2021     Wen Wang                 Modified comment(s),          */
76 /*                                            fixed spelling error,       */
77 /*                                            resulting in version 6.1.8  */
78 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            fixed standalone compile,   */
80 /*                                            resulting in version 6.1.11 */
81 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
82 /*                                            fixed parameter/variable    */
83 /*                                            names conflict C++ keyword, */
84 /*                                            resulting in version 6.1.12 */
85 /*                                                                        */
86 /**************************************************************************/
_ux_device_class_rndis_deactivate(UX_SLAVE_CLASS_COMMAND * command)87 UINT  _ux_device_class_rndis_deactivate(UX_SLAVE_CLASS_COMMAND *command)
88 {
89 
90 UX_SLAVE_CLASS_RNDIS        *rndis;
91 UX_SLAVE_INTERFACE          *interface_ptr;
92 UX_SLAVE_CLASS              *class_ptr;
93 
94     /* Get the class container.  */
95     class_ptr =  command -> ux_slave_class_command_class_ptr;
96 
97     /* Get the class instance in the container.  */
98     rndis = (UX_SLAVE_CLASS_RNDIS *) class_ptr -> ux_slave_class_instance;
99 
100     /* Get the interface that owns this instance.  Normally the interface can be derived
101        from the class instance but since RNDIS has 2 interfaces and we only store the Control
102        interface in the class container, we used the class_command pointer to retrieve the
103        correct interface which issued the deactivation. */
104     interface_ptr =  (UX_SLAVE_INTERFACE  *) command -> ux_slave_class_command_interface;
105 
106     /* Check if this is the Control or Data interface.  We only need to dismount the link and abort the
107        transfer once for the 2 classes.  */
108     if (interface_ptr -> ux_slave_interface_descriptor.bInterfaceClass == UX_DEVICE_CLASS_RNDIS_CLASS_COMMUNICATION_CONTROL)
109     {
110 
111         /* Declare the link to be down. That may need to change later to make it dependant on the
112            WAN/Wireless modem.  */
113         rndis -> ux_slave_class_rndis_link_state = UX_DEVICE_CLASS_RNDIS_LINK_STATE_DOWN;
114 
115         /* Terminate the transactions pending on the endpoints (interrupt, bulk in, bulk out).  */
116         _ux_device_stack_transfer_all_request_abort(rndis -> ux_slave_class_rndis_interrupt_endpoint, UX_TRANSFER_BUS_RESET);
117         _ux_device_stack_transfer_all_request_abort(rndis -> ux_slave_class_rndis_bulkin_endpoint, UX_TRANSFER_BUS_RESET);
118         _ux_device_stack_transfer_all_request_abort(rndis -> ux_slave_class_rndis_bulkout_endpoint, UX_TRANSFER_BUS_RESET);
119 
120         /* We have 2 threads waiting for an event, interrupt and bulk in. We wake them up with
121            a DEVICE_STATE_CHANGE event. In turn they will release the NetX resources used and suspend.  */
122         _ux_device_event_flags_set(&rndis -> ux_slave_class_rndis_event_flags_group, UX_DEVICE_CLASS_RNDIS_NEW_DEVICE_STATE_CHANGE_EVENT, UX_OR);
123 
124         /* If there is a deactivate function call it.  */
125         if (rndis -> ux_slave_class_rndis_parameter.ux_slave_class_rndis_instance_deactivate != UX_NULL)
126 
127             /* Invoke the application.  */
128             rndis -> ux_slave_class_rndis_parameter.ux_slave_class_rndis_instance_deactivate(rndis);
129 
130         /* Deregister this interface to the NetX USB interface broker.  */
131         _ux_network_driver_deactivate((VOID *) rndis, rndis -> ux_slave_class_rndis_network_handle);
132 
133     }
134 
135     /* If trace is enabled, insert this event into the trace buffer.  */
136     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_RNDIS_DEACTIVATE, rndis, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
137 
138     /* If trace is enabled, register this object.  */
139     UX_TRACE_OBJECT_UNREGISTER(rndis);
140 
141     /* Return completion status.  */
142     return(UX_SUCCESS);
143 }
144 
145