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 /**   Asix 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_asix.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_asix_read                            PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function reads from the asix 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 /*    asix                                  Pointer to asix 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 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application                                                         */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
75 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            prefixed UX to MS_TO_TICK,  */
77 /*                                            resulting in version 6.1    */
78 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            refined macros names,       */
80 /*                                            resulting in version 6.1.10 */
81 /*                                                                        */
82 /**************************************************************************/
_ux_host_class_asix_read(UX_HOST_CLASS_ASIX * asix,UCHAR * data_pointer,ULONG requested_length,ULONG * actual_length)83 UINT  _ux_host_class_asix_read (UX_HOST_CLASS_ASIX *asix, UCHAR *data_pointer,
84                                     ULONG requested_length, ULONG *actual_length)
85 {
86 
87 UX_TRANSFER     *transfer_request;
88 UINT            status;
89 ULONG           transfer_request_length;
90 
91     /* If trace is enabled, insert this event into the trace buffer.  */
92     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_ASIX_READ, asix, data_pointer, requested_length, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
93 
94     /* Ensure the instance is valid.  */
95     if (asix -> ux_host_class_asix_state !=  UX_HOST_CLASS_INSTANCE_LIVE)
96     {
97 
98         /* Error trap. */
99         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
100 
101         /* If trace is enabled, insert this event into the trace buffer.  */
102         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, asix, 0, 0, UX_TRACE_ERRORS, 0, 0)
103 
104         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
105     }
106 
107     /* Start by resetting the actual length of the transfer to zero.  */
108     *actual_length =  0;
109 
110     /* Get the pointer to the bulk in endpoint in the transfer_request.  */
111     transfer_request =  &asix -> ux_host_class_asix_bulk_in_endpoint -> ux_endpoint_transfer_request;
112 
113     /* Perform a transfer on the bulk in endpoint until either the transfer is
114        completed or until there is an error.  */
115     while (requested_length)
116     {
117 
118         /* Program the maximum authorized length for this transfer request.  */
119         if (requested_length > transfer_request -> ux_transfer_request_maximum_length)
120             transfer_request_length =  transfer_request -> ux_transfer_request_maximum_length;
121         else
122             transfer_request_length =  requested_length;
123 
124         /* Initialize the transfer request.  */
125         transfer_request -> ux_transfer_request_data_pointer =      data_pointer;
126         transfer_request -> ux_transfer_request_requested_length =  transfer_request_length;
127 
128         /* Perform the transfer.  */
129         status =  _ux_host_stack_transfer_request(transfer_request);
130 
131         /* If the transfer is successful, we need to wait for the transfer request to be completed.  */
132         if (status == UX_SUCCESS)
133         {
134 
135             /* Wait for the completion of the transfer_request.  */
136             status =  _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_ASIX_CLASS_TRANSFER_TIMEOUT));
137 
138             /* If the semaphore did not succeed we probably have a time out.  */
139             if (status != UX_SUCCESS || transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
140             {
141 
142                 /* All transfers pending need to abort. There may have been a partial transfer.  */
143                 _ux_host_stack_transfer_request_abort(transfer_request);
144 
145                 /* Update the length of the actual data transferred. We do this after the
146                    abort of the transfer request in case some data was actually received.  */
147                 *actual_length +=  transfer_request -> ux_transfer_request_actual_length;
148 
149                 /* There was an error, return to the caller */
150                 return(UX_TRANSFER_ERROR);
151             }
152         }
153         else
154         {
155 
156             /* There was a non transfer error, no partial transfer to be checked.  */
157             return(status);
158         }
159 
160         /* Update the length of the transfer. Normally all the data has to be received.  */
161         *actual_length +=  transfer_request -> ux_transfer_request_actual_length;
162 
163         /* Check for completion status.  */
164         if (transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
165             return(transfer_request -> ux_transfer_request_completion_code);
166 
167         /* Check for completion of transfer. If the transfer is partial, return to caller.
168            The transfer is marked as successful but the caller will need to check the length
169            actually received and determine if a partial transfer is OK.  */
170         if (transfer_request_length != transfer_request -> ux_transfer_request_actual_length)
171         {
172 
173             /* Return success to caller.  */
174             return(UX_SUCCESS);
175         }
176 
177         /* Update the data pointer for next transfer. */
178         data_pointer +=  transfer_request_length;
179 
180         /* Update what is left to receive.  */
181         requested_length -=  transfer_request_length;
182     }
183 
184     /* We get here when all the transfers went through without errors.  */
185     return(UX_SUCCESS);
186 }
187 
188