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 /**                                                                       */
16 /** USBX Component                                                        */
17 /**                                                                       */
18 /**   CDC ACM Class                                                       */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /* Include necessary system files.  */
25 
26 #define UX_SOURCE_CODE
27 
28 #include "ux_api.h"
29 #include "ux_host_class_cdc_acm.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_cdc_acm_endpoints_get                PORTABLE C      */
38 /*                                                           6.1.11       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function distinguishes for either the Data or Control Class.   */
46 /*    For the data class, we mount the bulk in and bulk out endpoints.    */
47 /*    For the control class, we mount the optional interrupt endpoint.    */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    cdc_acm                               Pointer to cdc_acm class      */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_interface_endpoint_get Get interface endpoint        */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    _ux_host_class_cdc_acm_activate       Activate cdc_acm 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 /*                                            resulting in version 6.1    */
72 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            use pre-calculated value    */
74 /*                                            instead of wMaxPacketSize,  */
75 /*                                            resulting in version 6.1.9  */
76 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            initialized timeout value,  */
78 /*                                            resulting in version 6.1.10 */
79 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            internal clean up,          */
81 /*                                            resulting in version 6.1.11 */
82 /*                                                                        */
83 /**************************************************************************/
_ux_host_class_cdc_acm_endpoints_get(UX_HOST_CLASS_CDC_ACM * cdc_acm)84 UINT  _ux_host_class_cdc_acm_endpoints_get(UX_HOST_CLASS_CDC_ACM *cdc_acm)
85 {
86 
87 UINT            status;
88 UINT            endpoint_index;
89 UX_ENDPOINT     *endpoint;
90 UX_TRANSFER     *transfer_request;
91 
92 
93     /* Check what interface we are mounting.  */
94     if (cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass == UX_HOST_CLASS_CDC_DATA_CLASS)
95     {
96 
97         /* Search the bulk OUT endpoint. It is attached to the interface container.  */
98         for (endpoint_index = 0; endpoint_index < cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bNumEndpoints;
99                             endpoint_index++)
100         {
101 
102             /* Get interface endpoint.  */
103             _ux_host_stack_interface_endpoint_get(cdc_acm -> ux_host_class_cdc_acm_interface, endpoint_index, &endpoint);
104 
105             /* Check if endpoint is bulk and OUT.  */
106             if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT) &&
107                 ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
108             {
109 
110                 /* This transfer_request always have the OUT direction.  */
111                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_OUT;
112 
113                 /* Set default timeout for transfer.  */
114                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_timeout_value = UX_HOST_CLASS_CDC_ACM_CLASS_TRANSFER_TIMEOUT;
115 
116                 /* We have found the bulk endpoint, save it.  */
117                 cdc_acm -> ux_host_class_cdc_acm_bulk_out_endpoint =  endpoint;
118 
119                 /* If found all, we break.  */
120                 if (cdc_acm -> ux_host_class_cdc_acm_bulk_in_endpoint)
121                     break;
122             }
123 
124             /* Check if endpoint is bulk and IN.  */
125             if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
126                 ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
127             {
128 
129                 /* This transfer_request always have the IN direction.  */
130                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;
131 
132                 /* Set default timeout for transfer.  */
133                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_timeout_value = UX_HOST_CLASS_CDC_ACM_CLASS_TRANSFER_TIMEOUT;
134 
135                 /* We have found the bulk endpoint, save it.  */
136                 cdc_acm -> ux_host_class_cdc_acm_bulk_in_endpoint =  endpoint;
137 
138                 /* If found all, we break.  */
139                 if (cdc_acm -> ux_host_class_cdc_acm_bulk_out_endpoint)
140                     break;
141             }
142         }
143 
144         /* The both bulk endpoints are mandatory.  */
145         if (cdc_acm -> ux_host_class_cdc_acm_bulk_out_endpoint == UX_NULL ||
146             cdc_acm -> ux_host_class_cdc_acm_bulk_in_endpoint == UX_NULL)
147         {
148 
149             /* Error trap. */
150             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
151 
152             /* If trace is enabled, insert this event into the trace buffer.  */
153             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, cdc_acm, 0, 0, UX_TRACE_ERRORS, 0, 0)
154 
155             return(UX_ENDPOINT_HANDLE_UNKNOWN);
156         }
157     }
158     else
159     {
160         /* Search the Interrupt endpoint. It is attached to the interface container of the control interface. It is not mandatory.  */
161         for (endpoint_index = 0; endpoint_index < cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bNumEndpoints;
162                             endpoint_index++)
163         {
164 
165             /* Get the endpoint handle.  */
166             _ux_host_stack_interface_endpoint_get(cdc_acm -> ux_host_class_cdc_acm_interface, endpoint_index, &endpoint);
167 
168             /* Check if endpoint is Interrupt and IN.  */
169             if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
170                 ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_INTERRUPT_ENDPOINT))
171             {
172 
173                 /* This transfer_request always have the IN direction.  */
174                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;
175 
176                 /* We have found the interrupt endpoint, save it.  */
177                 cdc_acm -> ux_host_class_cdc_acm_interrupt_endpoint =  endpoint;
178 
179                 /* The endpoint is correct, Fill in the transfer request with the length requested for this endpoint.  */
180                 transfer_request =  &cdc_acm -> ux_host_class_cdc_acm_interrupt_endpoint -> ux_endpoint_transfer_request;
181                 transfer_request -> ux_transfer_request_requested_length =  transfer_request -> ux_transfer_request_packet_length;
182                 transfer_request -> ux_transfer_request_actual_length =     0;
183 
184                 /* The direction is always IN for the CDC interrupt endpoint.  */
185                 transfer_request -> ux_transfer_request_type =  UX_REQUEST_IN;
186 
187                 /* There is a callback function associated with the transfer request, so we need the class instance.  */
188                 transfer_request -> ux_transfer_request_class_instance =  (VOID *) cdc_acm;
189 
190                 /* Interrupt transactions have a completion routine. */
191                 transfer_request -> ux_transfer_request_completion_function =  _ux_host_class_cdc_acm_transfer_request_completed;
192 
193                 /* Obtain a buffer for this transaction. The buffer will always be reused.  */
194                 transfer_request -> ux_transfer_request_data_pointer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY,
195                                                                 transfer_request -> ux_transfer_request_requested_length);
196 
197                 /* If the endpoint is available and we have memory, we start the interrupt endpoint.  */
198                 if (transfer_request -> ux_transfer_request_data_pointer != UX_NULL)
199                 {
200 
201                     /* The transfer on the interrupt endpoint can be started.  */
202                     status =  _ux_host_stack_transfer_request(transfer_request);
203 
204                     /* Check error, if endpoint interrupt IN transfer not successful, do not proceed. */
205                     if (status != UX_SUCCESS)
206 
207                         /* Error, do not proceed.  */
208                         return(status);
209                 }
210                 else
211                 {
212 
213                     /* Error trap. */
214                     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_MEMORY_INSUFFICIENT);
215 
216                     /* If trace is enabled, insert this event into the trace buffer.  */
217                     UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_MEMORY_INSUFFICIENT, cdc_acm, 0, 0, UX_TRACE_ERRORS, 0, 0)
218 
219                     /* We must return an error.  */
220                     return(UX_ENDPOINT_HANDLE_UNKNOWN);
221                 }
222                 break;
223             }
224         }
225     }
226 
227     /* All endpoints have been mounted.  */
228     return(UX_SUCCESS);
229 }
230 
231