1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Slave Simulator Controller Driver                                   */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define UX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "ux_api.h"
28 #include "ux_dcd_sim_slave.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_dcd_sim_slave_transfer_request                  PORTABLE C      */
36 /*                                                           6.1.10       */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function will initiate a transfer to a specific endpoint.      */
44 /*    If the endpoint is IN, the endpoint register will be set to accept  */
45 /*    the request.                                                        */
46 /*                                                                        */
47 /*    If the endpoint is IN, the endpoint FIFO will be filled with the    */
48 /*    buffer and the endpoint register set.                               */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    dcd_sim_slave                         Pointer to device controller  */
53 /*    transfer_request                      Pointer to transfer request   */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Completion Status                                                   */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_utility_semaphore_get             Get semaphore                 */
62 /*    _ux_dcd_sim_slave_transfer_abort      Abort transfer                */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Slave Simulator Controller Driver                                   */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            added standalone support,   */
77 /*                                            cleared transfer status     */
78 /*                                            before semaphore wakeup to  */
79 /*                                            avoid a race condition,     */
80 /*                                            resulting in version 6.1.10 */
81 /*                                                                        */
82 /**************************************************************************/
_ux_dcd_sim_slave_transfer_request(UX_DCD_SIM_SLAVE * dcd_sim_slave,UX_SLAVE_TRANSFER * transfer_request)83 UINT  _ux_dcd_sim_slave_transfer_request(UX_DCD_SIM_SLAVE *dcd_sim_slave, UX_SLAVE_TRANSFER *transfer_request)
84 {
85 
86 UX_SLAVE_ENDPOINT       *endpoint;
87 UX_DCD_SIM_SLAVE_ED     *ed;
88 UINT                    status;
89 
90 
91     /* Get the pointer to the logical endpoint from the transfer request.  */
92     endpoint =  transfer_request -> ux_slave_transfer_request_endpoint;
93 
94     /* Get the slave endpoint.  */
95     ed = (UX_DCD_SIM_SLAVE_ED *) endpoint -> ux_slave_endpoint_ed;
96 
97     /* We have a request for a OUT or IN transaction from the host.
98        If the endpoint is a Control endpoint, all this is happening under Interrupt and there is no
99        thread to suspend.  */
100     if (ed -> ux_sim_slave_ed_index != 0)
101     {
102 
103         /* Set the ED to TRANSFER status.  */
104         ed -> ux_sim_slave_ed_status |= UX_DCD_SIM_SLAVE_ED_STATUS_TRANSFER;
105 
106         /* We should wait for the semaphore to wake us up.  */
107         status =  _ux_device_semaphore_get(&transfer_request -> ux_slave_transfer_request_semaphore,
108                                             transfer_request -> ux_slave_transfer_request_timeout);
109 
110         /* Check the completion code. */
111         if (status != UX_SUCCESS)
112         {
113             _ux_dcd_sim_slave_transfer_abort(dcd_sim_slave, transfer_request);
114             transfer_request -> ux_slave_transfer_request_status = UX_TRANSFER_STATUS_COMPLETED;
115             return(status);
116         }
117 
118         /* ED TRANSFER has been cleared before semaphore wakeup.  */
119 
120         /* Check the transfer request completion code. We may have had a BUS reset or
121            a device disconnection.  */
122         if (transfer_request -> ux_slave_transfer_request_completion_code != UX_SUCCESS)
123             return(transfer_request -> ux_slave_transfer_request_completion_code);
124     }
125 
126     /* Return to caller with success.  */
127     return(UX_SUCCESS);
128 }
129 
130