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 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_acm.h"
28 #include "ux_device_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_class_cdc_acm_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_acm 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 Abort all transfers     */
56 /*    _ux_device_class_cdc_acm_ioctl              IO control              */
57 /*                                                                        */
58 /*  CALLED BY                                                             */
59 /*                                                                        */
60 /*    CDC Class                                                           */
61 /*                                                                        */
62 /*  RELEASE HISTORY                                                       */
63 /*                                                                        */
64 /*    DATE              NAME                      DESCRIPTION             */
65 /*                                                                        */
66 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
67 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
68 /*                                            resulting in version 6.1    */
69 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            fixed parameter/variable    */
71 /*                                            names conflict C++ keyword, */
72 /*                                            resulting in version 6.1.12 */
73 /*                                                                        */
74 /**************************************************************************/
_ux_device_class_cdc_acm_deactivate(UX_SLAVE_CLASS_COMMAND * command)75 UINT  _ux_device_class_cdc_acm_deactivate(UX_SLAVE_CLASS_COMMAND *command)
76 {
77 
78 UX_SLAVE_INTERFACE          *interface_ptr;
79 UX_SLAVE_CLASS              *class_ptr;
80 UX_SLAVE_CLASS_CDC_ACM      *cdc_acm;
81 UX_SLAVE_ENDPOINT           *endpoint_in;
82 UX_SLAVE_ENDPOINT           *endpoint_out;
83 
84     /* Get the class container.  */
85     class_ptr =  command -> ux_slave_class_command_class_ptr;
86 
87     /* Get the class instance in the container.  */
88     cdc_acm = (UX_SLAVE_CLASS_CDC_ACM *) class_ptr -> ux_slave_class_instance;
89 
90     /* We need the interface to the class.  */
91     interface_ptr =  cdc_acm -> ux_slave_class_cdc_acm_interface;
92 
93     /* Locate the endpoints.  */
94     endpoint_in =  interface_ptr -> ux_slave_interface_first_endpoint;
95 
96     /* Check the endpoint direction, if IN we have the correct endpoint.  */
97     if ((endpoint_in -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_IN)
98     {
99 
100         /* Wrong direction, we found the OUT endpoint first.  */
101         endpoint_out =  endpoint_in;
102 
103         /* So the next endpoint has to be the IN endpoint.  */
104         endpoint_in =  endpoint_out -> ux_slave_endpoint_next_endpoint;
105     }
106     else
107     {
108 
109         /* We found the endpoint IN first, so next endpoint is OUT.  */
110         endpoint_out =  endpoint_in -> ux_slave_endpoint_next_endpoint;
111     }
112 
113     /* Terminate the transactions pending on the endpoints.  */
114     _ux_device_stack_transfer_all_request_abort(endpoint_in, UX_TRANSFER_BUS_RESET);
115     _ux_device_stack_transfer_all_request_abort(endpoint_out, UX_TRANSFER_BUS_RESET);
116 
117     /* Terminate transmission and free resources.  */
118     _ux_device_class_cdc_acm_ioctl(cdc_acm, UX_SLAVE_CLASS_CDC_ACM_IOCTL_TRANSMISSION_STOP, UX_NULL);
119 
120     /* If there is a deactivate function call it.  */
121     if (cdc_acm -> ux_slave_class_cdc_acm_parameter.ux_slave_class_cdc_acm_instance_deactivate != UX_NULL)
122     {
123 
124         /* Invoke the application.  */
125         cdc_acm -> ux_slave_class_cdc_acm_parameter.ux_slave_class_cdc_acm_instance_deactivate(cdc_acm);
126     }
127 
128     /* We need to reset the DTR and RTS values so they do not carry over to the
129        next connection.  */
130     cdc_acm -> ux_slave_class_cdc_acm_data_dtr_state =  0;
131     cdc_acm -> ux_slave_class_cdc_acm_data_rts_state =  0;
132 
133     /* If trace is enabled, insert this event into the trace buffer.  */
134     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_CDC_ACM_DEACTIVATE, cdc_acm, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
135 
136     /* If trace is enabled, register this object.  */
137     UX_TRACE_OBJECT_UNREGISTER(cdc_acm);
138 
139     /* Return completion status.  */
140     return(UX_SUCCESS);
141 }
142