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_write                           PORTABLE C      */
38 /*                                                           6.1.11       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function writes to the swar interface. The call is blocking    */
46 /*    and only returns when there is either an error or when the transfer */
47 /*    is complete.                                                        */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    swar                                  Pointer to swar class         */
52 /*    data_pointer                          Pointer to data to write      */
53 /*    requested_length                      Length of data to write       */
54 /*    actual_length                         Actual length of data written */
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 /*                                            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_write(UX_HOST_CLASS_SWAR * swar,UCHAR * data_pointer,ULONG requested_length,ULONG * actual_length)87 UINT  _ux_host_class_swar_write(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_WRITE, 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((UCHAR *) _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.  */
117     *actual_length =  0;
118 
119     /* Get the pointer to the bulk out endpoint transfer request.  */
120     transfer_request =  &swar -> ux_host_class_swar_bulk_out_endpoint -> ux_endpoint_transfer_request;
121 
122     /* Perform a transfer on the bulk out endpoint until either the transfer is
123        completed or when there is an error.  */
124     do
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_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 actually went out.  */
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 sent.  */
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 sent 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.  */
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 send out.  */
204         requested_length -=  transfer_request_length;
205 
206     } while (requested_length != 0);
207 
208 
209     /* Unprotect thread reentry to this instance.  */
210     _ux_host_semaphore_put(&swar -> ux_host_class_swar_semaphore);
211 
212     /* We get here when all the transfers went through without errors.  */
213     return(UX_SUCCESS);
214 }
215 
216