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 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Host Sierra Wireless AR module class                                */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_host_class_swar.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_swar_read                            PORTABLE C      */
37 /*                                                           6.1.11       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function reads from the swar interface. The call is            */
45 /*    blocking and only returns when there is either an error or when     */
46 /*    the transfer is complete.                                           */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    swar                                  Pointer to swar class         */
51 /*    data_pointer                          Pointer to buffer             */
52 /*    requested_length                      Requested data read           */
53 /*    actual_length                         Actual data read              */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Completion Status                                                   */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_host_stack_class_instance_verify  Verify the class instance     */
62 /*    _ux_host_stack_transfer_request       Process transfer request      */
63 /*    _ux_host_stack_transfer_request_abort Abort transfer request        */
64 /*    _ux_host_semaphore_get                Get protection semaphore      */
65 /*    _ux_host_semaphore_put                Release protection semaphore  */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    Application                                                         */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
76 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            prefixed UX to MS_TO_TICK,  */
78 /*                                            resulting in version 6.1    */
79 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            refined macros names,       */
81 /*                                            resulting in version 6.1.10 */
82 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
83 /*                                            fixed standalone compile,   */
84 /*                                            resulting in version 6.1.11 */
85 /*                                                                        */
86 /**************************************************************************/
_ux_host_class_swar_read(UX_HOST_CLASS_SWAR * swar,UCHAR * data_pointer,ULONG requested_length,ULONG * actual_length)87 UINT  _ux_host_class_swar_read(UX_HOST_CLASS_SWAR *swar, UCHAR *data_pointer,
88                                     ULONG requested_length, ULONG *actual_length)
89 {
90 
91 UX_TRANSFER     *transfer_request;
92 UINT            status;
93 ULONG           transfer_request_length;
94 
95     /* If trace is enabled, insert this event into the trace buffer.  */
96     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_SWAR_READ, swar, data_pointer, requested_length, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
97 
98     /* Ensure the instance is valid.  */
99     if (_ux_host_stack_class_instance_verify(_ux_system_host_class_swar_name, (VOID *) swar) != UX_SUCCESS)
100     {
101 
102         /* Error trap. */
103         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
104 
105         /* If trace is enabled, insert this event into the trace buffer.  */
106         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, swar, 0, 0, UX_TRACE_ERRORS, 0, 0)
107 
108         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
109     }
110 
111     /* Protect thread reentry to this instance.  */
112     status =  _ux_host_semaphore_get(&swar -> ux_host_class_swar_semaphore, UX_WAIT_FOREVER);
113     if (status != UX_SUCCESS)
114         return(status);
115 
116     /* Start by resetting the actual length of the transfer to zero.  */
117     *actual_length =  0;
118 
119     /* Get the pointer to the bulk in endpoint in the transfer_request.  */
120     transfer_request =  &swar -> ux_host_class_swar_bulk_in_endpoint -> ux_endpoint_transfer_request;
121 
122     /* Perform a transfer on the bulk in endpoint until either the transfer is
123        completed or until there is an error.  */
124     while (requested_length)
125     {
126 
127         /* Program the maximum authorized length for this transfer request.  */
128         if (requested_length > transfer_request -> ux_transfer_request_maximum_length)
129             transfer_request_length =  transfer_request -> ux_transfer_request_maximum_length;
130         else
131             transfer_request_length =  requested_length;
132 
133         /* Initialize the transfer request.  */
134         transfer_request -> ux_transfer_request_data_pointer =      data_pointer;
135         transfer_request -> ux_transfer_request_requested_length =  transfer_request_length;
136 
137         /* Perform the transfer.  */
138         status =  _ux_host_stack_transfer_request(transfer_request);
139 
140         /* If the transfer is successful, we need to wait for the transfer request to be completed.  */
141         if (status == UX_SUCCESS)
142         {
143 
144             /* Wait for the completion of the transfer_request.  */
145             status =  _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_SWAR_CLASS_TRANSFER_TIMEOUT));
146 
147             /* If the semaphore did not succeed we probably have a time out.  */
148             if (status != UX_SUCCESS)
149             {
150 
151                 /* All transfers pending need to abort. There may have been a partial transfer.  */
152                 _ux_host_stack_transfer_request_abort(transfer_request);
153 
154                 /* Update the length of the actual data transferred. We do this after the
155                    abort of the transfer request in case some data was actually received.  */
156                 *actual_length +=  transfer_request -> ux_transfer_request_actual_length;
157 
158                 /* Unprotect thread reentry to this instance.  */
159                 _ux_host_semaphore_put(&swar -> ux_host_class_swar_semaphore);
160 
161                 /* Set the completion code.  */
162                 transfer_request -> ux_transfer_request_completion_code =  UX_TRANSFER_TIMEOUT;
163 
164                 /* Error trap. */
165                 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_TIMEOUT);
166 
167                 /* If trace is enabled, insert this event into the trace buffer.  */
168                 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_TRANSFER_TIMEOUT, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0)
169 
170                 /* There was an error, return to the caller */
171                 return(UX_TRANSFER_TIMEOUT);
172             }
173         }
174         else
175         {
176 
177             /* Unprotect thread reentry to this instance.  */
178             _ux_host_semaphore_put(&swar -> ux_host_class_swar_semaphore);
179 
180             /* There was a non transfer error, no partial transfer to be checked.  */
181             return(status);
182         }
183 
184         /* Update the length of the transfer. Normally all the data has to be received.  */
185         *actual_length +=  transfer_request -> ux_transfer_request_actual_length;
186 
187         /* Check for completion of transfer. If the transfer is partial, return to caller.
188            The transfer is marked as successful but the caller will need to check the length
189            actually received and determine if a partial transfer is OK.  */
190         if (transfer_request_length != transfer_request -> ux_transfer_request_actual_length)
191         {
192 
193             /* Unprotect thread reentry to this instance.  */
194             _ux_host_semaphore_put(&swar -> ux_host_class_swar_semaphore);
195 
196             /* Return success to caller.  */
197             return(UX_SUCCESS);
198         }
199 
200         /* Update the data pointer for next transfer. */
201         data_pointer +=  transfer_request_length;
202 
203         /* Update what is left to receive.  */
204         requested_length -=  transfer_request_length;
205     }
206 
207     /* Unprotect thread reentry to this instance.  */
208     _ux_host_semaphore_put(&swar -> ux_host_class_swar_semaphore);
209 
210     /* We get here when all the transfers went through without errors.  */
211     return(UX_SUCCESS);
212 }
213 
214 
215 /**************************************************************************/
216 /*                                                                        */
217 /*  FUNCTION                                               RELEASE        */
218 /*                                                                        */
219 /*    _uxe_host_class_swar_read                           PORTABLE C      */
220 /*                                                           6.3.0        */
221 /*  AUTHOR                                                                */
222 /*                                                                        */
223 /*    Chaoqiong Xiao, Microsoft Corporation                               */
224 /*                                                                        */
225 /*  DESCRIPTION                                                           */
226 /*                                                                        */
227 /*    This function checks errors in SWAR read function call.             */
228 /*                                                                        */
229 /*  INPUT                                                                 */
230 /*                                                                        */
231 /*    swar                                  Pointer to SWAR class         */
232 /*    data_pointer                          Pointer to buffer             */
233 /*    requested_length                      Requested data read           */
234 /*    actual_length                         Actual data read              */
235 /*                                                                        */
236 /*  OUTPUT                                                                */
237 /*                                                                        */
238 /*    Status                                                              */
239 /*                                                                        */
240 /*  CALLS                                                                 */
241 /*                                                                        */
242 /*    _ux_host_class_swar_read              SWAR read                     */
243 /*                                                                        */
244 /*  CALLED BY                                                             */
245 /*                                                                        */
246 /*    Application                                                         */
247 /*                                                                        */
248 /*  RELEASE HISTORY                                                       */
249 /*                                                                        */
250 /*    DATE              NAME                      DESCRIPTION             */
251 /*                                                                        */
252 /*  10-31-2023     Chaoqiong Xiao           Initial Version 6.3.0         */
253 /*                                                                        */
254 /**************************************************************************/
_uxe_host_class_swar_read(UX_HOST_CLASS_SWAR * swar,UCHAR * data_pointer,ULONG requested_length,ULONG * actual_length)255 UINT  _uxe_host_class_swar_read (UX_HOST_CLASS_SWAR *swar, UCHAR *data_pointer,
256                                     ULONG requested_length, ULONG *actual_length)
257 {
258 
259     /* Sanity checks.  */
260     if ((swar == UX_NULL) || (data_pointer == UX_NULL) || (actual_length == UX_NULL))
261         return(UX_INVALID_PARAMETER);
262 
263     /* Invoke SWAR read function.  */
264     return(_ux_host_class_swar_read(swar, data_pointer, requested_length, actual_length));
265 }
266