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