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