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 /**   Slave 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_dcd_sim_slave.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_dcd_sim_slave_transfer_request                  PORTABLE C      */
37 /*                                                           6.1.10       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function will initiate a transfer to a specific endpoint.      */
45 /*    If the endpoint is IN, the endpoint register will be set to accept  */
46 /*    the request.                                                        */
47 /*                                                                        */
48 /*    If the endpoint is IN, the endpoint FIFO will be filled with the    */
49 /*    buffer and the endpoint register set.                               */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    dcd_sim_slave                         Pointer to device controller  */
54 /*    transfer_request                      Pointer to transfer request   */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    Completion Status                                                   */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    _ux_utility_semaphore_get             Get semaphore                 */
63 /*    _ux_dcd_sim_slave_transfer_abort      Abort transfer                */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Slave Simulator Controller Driver                                   */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
74 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            added standalone support,   */
78 /*                                            cleared transfer status     */
79 /*                                            before semaphore wakeup to  */
80 /*                                            avoid a race condition,     */
81 /*                                            resulting in version 6.1.10 */
82 /*                                                                        */
83 /**************************************************************************/
_ux_dcd_sim_slave_transfer_request(UX_DCD_SIM_SLAVE * dcd_sim_slave,UX_SLAVE_TRANSFER * transfer_request)84 UINT  _ux_dcd_sim_slave_transfer_request(UX_DCD_SIM_SLAVE *dcd_sim_slave, UX_SLAVE_TRANSFER *transfer_request)
85 {
86 
87 UX_SLAVE_ENDPOINT       *endpoint;
88 UX_DCD_SIM_SLAVE_ED     *ed;
89 UINT                    status;
90 
91 
92     /* Get the pointer to the logical endpoint from the transfer request.  */
93     endpoint =  transfer_request -> ux_slave_transfer_request_endpoint;
94 
95     /* Get the slave endpoint.  */
96     ed = (UX_DCD_SIM_SLAVE_ED *) endpoint -> ux_slave_endpoint_ed;
97 
98     /* We have a request for a OUT or IN transaction from the host.
99        If the endpoint is a Control endpoint, all this is happening under Interrupt and there is no
100        thread to suspend.  */
101     if (ed -> ux_sim_slave_ed_index != 0)
102     {
103 
104         /* Set the ED to TRANSFER status.  */
105         ed -> ux_sim_slave_ed_status |= UX_DCD_SIM_SLAVE_ED_STATUS_TRANSFER;
106 
107         /* We should wait for the semaphore to wake us up.  */
108         status =  _ux_device_semaphore_get(&transfer_request -> ux_slave_transfer_request_semaphore,
109                                             transfer_request -> ux_slave_transfer_request_timeout);
110 
111         /* Check the completion code. */
112         if (status != UX_SUCCESS)
113         {
114             _ux_dcd_sim_slave_transfer_abort(dcd_sim_slave, transfer_request);
115             transfer_request -> ux_slave_transfer_request_status = UX_TRANSFER_STATUS_COMPLETED;
116             return(status);
117         }
118 
119         /* ED TRANSFER has been cleared before semaphore wakeup.  */
120 
121         /* Check the transfer request completion code. We may have had a BUS reset or
122            a device disconnection.  */
123         if (transfer_request -> ux_slave_transfer_request_completion_code != UX_SUCCESS)
124             return(transfer_request -> ux_slave_transfer_request_completion_code);
125     }
126 
127     /* Return to caller with success.  */
128     return(UX_SUCCESS);
129 }
130 
131