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 Simulator Controller Driver                                    */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define UX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "ux_api.h"
29 #include "ux_hcd_sim_host.h"
30 
31 
32 #if defined(UX_HOST_STANDALONE)
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_hcd_sim_host_transfer_run                       PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*     This function is the handler for all the transactions on the USB.  */
46 /*     The transfer request passed as parameter contains the endpoint and */
47 /*     the device descriptors in addition to the type of transaction de   */
48 /*     be executed. This function routes the transfer request to          */
49 /*     according to the type of transfer to be executed.                  */
50 /*                                                                        */
51 /*     It's for standalone mode.                                          */
52 /*                                                                        */
53 /*  INPUT                                                                 */
54 /*                                                                        */
55 /*    hcd_sim_host                          Pointer to host controller    */
56 /*    transfer_request                      Pointer to transfer request   */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    Completion Status                                                   */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    _ux_hcd_sim_host_request_bulk_transfer        Request bulk transfer */
65 /*    _ux_hcd_sim_host_request_control_transfer     Request control       */
66 /*                                                  transfer              */
67 /*    _ux_hcd_sim_host_request_interrupt_transfer   Request interrupt     */
68 /*                                                  transfer              */
69 /*    _ux_hcd_sim_host_request_isochronous_transfer Request isochronous   */
70 /*                                                  transfer              */
71 /*                                                                        */
72 /*  CALLED BY                                                             */
73 /*                                                                        */
74 /*    Host Simulator Controller Driver                                    */
75 /*                                                                        */
76 /*  RELEASE HISTORY                                                       */
77 /*                                                                        */
78 /*    DATE              NAME                      DESCRIPTION             */
79 /*                                                                        */
80 /*  01-31-2022     Chaoqiong Xiao           Initial Version 6.1.10        */
81 /*                                                                        */
82 /**************************************************************************/
_ux_hcd_sim_host_transfer_run(UX_HCD_SIM_HOST * hcd_sim_host,UX_TRANSFER * transfer_request)83 UINT  _ux_hcd_sim_host_transfer_run(UX_HCD_SIM_HOST *hcd_sim_host, UX_TRANSFER *transfer_request)
84 {
85 
86 UX_INTERRUPT_SAVE_AREA
87 UX_ENDPOINT         *endpoint;
88 UX_HCD_SIM_HOST_ED  *ed;
89 UINT                status = 0;
90 
91 
92     /* Get the pointer to the Endpoint.  */
93     endpoint =  (UX_ENDPOINT *) transfer_request -> ux_transfer_request_endpoint;
94 
95     /* Sanity check.  */
96     if (endpoint == UX_NULL)
97         return(UX_STATE_EXIT);
98 
99     /* Get ED.  */
100     ed = (UX_HCD_SIM_HOST_ED *)endpoint -> ux_endpoint_ed;
101 
102     /* Sanity check.  */
103     if (ed == UX_NULL)
104         return(UX_STATE_EXIT);
105 
106     UX_DISABLE
107 
108     /* If transfer started, check status.  */
109     if (ed -> ux_sim_host_ed_status & UX_HCD_SIM_HOST_ED_TRANSFER)
110     {
111         if (ed -> ux_sim_host_ed_head_td != ed -> ux_sim_host_ed_tail_td)
112         {
113             UX_RESTORE
114             return(UX_STATE_WAIT);
115         }
116 
117         /* Check if it's transfer waiting state.  */
118         if (transfer_request -> ux_transfer_request_status != UX_TRANSFER_STATUS_NOT_PENDING)
119         {
120 
121             /* Yes, polling pending status, report and transfer done.  */
122             ed -> ux_sim_host_ed_status &= ~UX_HCD_SIM_HOST_ED_TRANSFER;
123             UX_RESTORE
124             return(UX_STATE_NEXT);
125         }
126 
127         /* Maybe transfer completed but state not reported yet.  */
128     }
129     ed -> ux_sim_host_ed_status |= UX_HCD_SIM_HOST_ED_TRANSFER;
130     transfer_request -> ux_transfer_request_status = UX_TRANSFER_STATUS_PENDING;
131 
132     UX_RESTORE
133 
134     /* We reset the actual length field of the transfer request as a safety measure.  */
135     transfer_request -> ux_transfer_request_actual_length =  0;
136 
137     /* Isolate the endpoint type and route the transfer request.  */
138     switch ((endpoint -> ux_endpoint_descriptor.bmAttributes) & UX_MASK_ENDPOINT_TYPE)
139     {
140 
141     case UX_CONTROL_ENDPOINT:
142 
143         status =  _ux_hcd_sim_host_request_control_transfer(hcd_sim_host, transfer_request);
144         break;
145 
146 
147     case UX_BULK_ENDPOINT:
148 
149         status =  _ux_hcd_sim_host_request_bulk_transfer(hcd_sim_host, transfer_request);
150         break;
151 
152     case UX_INTERRUPT_ENDPOINT:
153 
154         status =  _ux_hcd_sim_host_request_interrupt_transfer(hcd_sim_host, transfer_request);
155         break;
156 
157     case UX_ISOCHRONOUS_ENDPOINT:
158 
159         status =  _ux_hcd_sim_host_request_isochronous_transfer(hcd_sim_host, transfer_request);
160         break;
161 
162     }
163 
164     return (status == UX_SUCCESS) ? (UX_STATE_WAIT) : (UX_STATE_ERROR);
165 }
166 #endif
167