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 #if defined(UX_DEVICE_STANDALONE)
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                                RELEASE       */
35 /*                                                                        */
36 /*    _ux_dcd_sim_slave_transfer_run                       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 /*    It's for standalone mode.                                           */
52 /*                                                                        */
53 /*  INPUT                                                                 */
54 /*                                                                        */
55 /*    dcd_sim_slave                         Pointer to device controller  */
56 /*    transfer_request                      Pointer to transfer request   */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    State machine Status to check                                       */
61 /*    UX_STATE_NEXT                         Transfer done, to next state  */
62 /*    UX_STATE_EXIT                         Abnormal, to reset state      */
63 /*    (others)                              Keep running, waiting         */
64 /*                                                                        */
65 /*  CALLS                                                                 */
66 /*                                                                        */
67 /*    _ux_utility_semaphore_get             Get semaphore                 */
68 /*    _ux_dcd_sim_slave_transfer_abort      Abort transfer                */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    Slave Simulator Controller Driver                                   */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  01-31-2022     Chaoqiong Xiao           Initial Version 6.1.10        */
79 /*                                                                        */
80 /**************************************************************************/
_ux_dcd_sim_slave_transfer_run(UX_DCD_SIM_SLAVE * dcd_sim_slave,UX_SLAVE_TRANSFER * transfer_request)81 UINT  _ux_dcd_sim_slave_transfer_run(UX_DCD_SIM_SLAVE *dcd_sim_slave, UX_SLAVE_TRANSFER *transfer_request)
82 {
83 
84 UX_INTERRUPT_SAVE_AREA
85 
86 UX_SLAVE_ENDPOINT       *endpoint;
87 UX_DCD_SIM_SLAVE_ED     *ed;
88 ULONG                   ed_status;
89 
90 
91     UX_PARAMETER_NOT_USED(dcd_sim_slave);
92 
93     /* Get the pointer to the logical endpoint from the transfer request.  */
94     endpoint =  transfer_request -> ux_slave_transfer_request_endpoint;
95 
96     /* Get the slave endpoint.  */
97     ed = (UX_DCD_SIM_SLAVE_ED *) endpoint -> ux_slave_endpoint_ed;
98 
99     UX_DISABLE
100 
101     /* Get current status.  */
102     ed_status = ed -> ux_sim_slave_ed_status;
103 
104     /* ED freed, must disconnected.  */
105     if (ed_status == UX_DCD_SIM_SLAVE_ED_STATUS_UNUSED)
106     {
107         transfer_request -> ux_slave_transfer_request_status = UX_TRANSFER_BUS_RESET;
108         UX_RESTORE
109         return(UX_STATE_EXIT);
110     }
111 
112     /* For control endpoint, always go to next state.  */
113     if(ed -> ux_sim_slave_ed_index == 0)
114     {
115         UX_RESTORE
116         return(UX_STATE_NEXT);
117     }
118 
119     /* ED stalled.  */
120     if (ed_status & UX_DCD_SIM_SLAVE_ED_STATUS_STALLED)
121     {
122         ed -> ux_sim_slave_ed_status = UX_DCD_SIM_SLAVE_ED_STATUS_USED;
123         transfer_request -> ux_slave_transfer_request_status = UX_TRANSFER_STALLED;
124         UX_RESTORE
125         return(UX_STATE_NEXT);
126     }
127 
128     /* Transfer started.  */
129     if (ed_status & UX_DCD_SIM_SLAVE_ED_STATUS_TRANSFER)
130     {
131         if (ed_status & UX_DCD_SIM_SLAVE_ED_STATUS_DONE)
132         {
133             ed -> ux_sim_slave_ed_status = UX_DCD_SIM_SLAVE_ED_STATUS_USED;
134             UX_RESTORE
135             return(UX_STATE_NEXT);
136         }
137         UX_RESTORE
138         return(UX_STATE_WAIT);
139     }
140 
141     /* Start transfer.  */
142     ed->ux_sim_slave_ed_status |= UX_DCD_SIM_SLAVE_ED_STATUS_TRANSFER;
143     UX_RESTORE
144     return(UX_STATE_WAIT);
145 
146 }
147 #endif
148