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 /**   Generic Serial Host 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_gser.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_gser_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 gser 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 /*    gser                                  Pointer to gser 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_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_gser_read(UX_HOST_CLASS_GSER * gser,ULONG interface_index,UCHAR * data_pointer,ULONG requested_length,ULONG * actual_length)87 UINT  _ux_host_class_gser_read(UX_HOST_CLASS_GSER *gser,
88                                     ULONG interface_index,
89                                     UCHAR *data_pointer,
90                                     ULONG requested_length,
91                                     ULONG *actual_length)
92 {
93 
94 UX_TRANSFER     *transfer_request;
95 UINT            status;
96 ULONG           transfer_request_length;
97 
98     /* If trace is enabled, insert this event into the trace buffer.  */
99     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_GSER_READ, gser, data_pointer, requested_length, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
100 
101     /* Ensure the instance is valid.  */
102     if (gser -> ux_host_class_gser_state !=  UX_HOST_CLASS_INSTANCE_LIVE)
103     {
104 
105         /* Error trap. */
106         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
107 
108         /* If trace is enabled, insert this event into the trace buffer.  */
109         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, gser, 0, 0, UX_TRACE_ERRORS, 0, 0)
110 
111         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
112     }
113 
114     /* Protect thread reentry to this instance.  */
115     status =  _ux_host_semaphore_get(&gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_semaphore, UX_WAIT_FOREVER);
116     if (status != UX_SUCCESS)
117         return(status);
118 
119     /* Start by resetting the actual length of the transfer to zero.  */
120     *actual_length =  0;
121 
122     /* Get the pointer to the bulk in endpoint in the transfer_request.  */
123     transfer_request =  &gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_bulk_in_endpoint -> ux_endpoint_transfer_request;
124 
125     /* Save the interface number in the Transfer Request. */
126     transfer_request -> ux_transfer_request_user_specific = (VOID *)(ALIGN_TYPE)interface_index;
127 
128     /* Perform a transfer on the bulk in endpoint until either the transfer is
129        completed or until there is an error.  */
130     while (requested_length)
131     {
132 
133         /* Program the maximum authorized length for this transfer request.  */
134         if (requested_length > transfer_request -> ux_transfer_request_maximum_length)
135             transfer_request_length =  transfer_request -> ux_transfer_request_maximum_length;
136         else
137             transfer_request_length =  requested_length;
138 
139         /* Initialize the transfer request.  */
140         transfer_request -> ux_transfer_request_data_pointer =      data_pointer;
141         transfer_request -> ux_transfer_request_requested_length =  transfer_request_length;
142 
143         /* Perform the transfer.  */
144         status =  _ux_host_stack_transfer_request(transfer_request);
145 
146         /* If the transfer is successful, we need to wait for the transfer request to be completed.  */
147         if (status == UX_SUCCESS)
148         {
149 
150             /* Wait for the completion of the transfer_request.  */
151             status =  _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_GSER_CLASS_TRANSFER_TIMEOUT));
152 
153             /* If the semaphore did not succeed we probably have a time out.  */
154             if (status != UX_SUCCESS)
155             {
156 
157                 /* All transfers pending need to abort. There may have been a partial transfer.  */
158                 _ux_host_stack_transfer_request_abort(transfer_request);
159 
160                 /* Update the length of the actual data transferred. We do this after the
161                    abort of the transfer request in case some data was actually received.  */
162                 *actual_length +=  transfer_request -> ux_transfer_request_actual_length;
163 
164                 /* Unprotect thread reentry to this instance.  */
165                 _ux_host_semaphore_put(&gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_semaphore);
166 
167                 /* Set the completion code.  */
168                 transfer_request -> ux_transfer_request_completion_code =  UX_TRANSFER_TIMEOUT;
169 
170                 /* Error trap. */
171                 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_TIMEOUT);
172 
173                 /* If trace is enabled, insert this event into the trace buffer.  */
174                 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_TRANSFER_TIMEOUT, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0)
175 
176                 /* There was an error, return to the caller */
177                 return(UX_TRANSFER_TIMEOUT);
178             }
179         }
180         else
181         {
182 
183             /* Unprotect thread reentry to this instance.  */
184             _ux_host_semaphore_put(&gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_semaphore);
185 
186             /* There was a non transfer error, no partial transfer to be checked.  */
187             return(status);
188         }
189 
190         /* Update the length of the transfer. Normally all the data has to be received.  */
191         *actual_length +=  transfer_request -> ux_transfer_request_actual_length;
192 
193         /* Check for completion of transfer. If the transfer is partial, return to caller.
194            The transfer is marked as successful but the caller will need to check the length
195            actually received and determine if a partial transfer is OK.  */
196         if (transfer_request_length != transfer_request -> ux_transfer_request_actual_length)
197         {
198 
199             /* Unprotect thread reentry to this instance.  */
200             _ux_host_semaphore_put(&gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_semaphore);
201 
202             /* Return success to caller.  */
203             return(UX_SUCCESS);
204         }
205 
206         /* Update the data pointer for next transfer. */
207         data_pointer +=  transfer_request_length;
208 
209         /* Update what is left to receive.  */
210         requested_length -=  transfer_request_length;
211     }
212 
213     /* Unprotect thread reentry to this instance.  */
214     _ux_host_semaphore_put(&gser -> ux_host_class_gser_interface_array[interface_index].ux_host_class_gser_semaphore);
215 
216     /* We get here when all the transfers went through without errors.  */
217     return(UX_SUCCESS);
218 }
219 
220