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_endpoint_create                   PORTABLE C      */
36 /*                                                           6.1.6        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function will create a physical endpoint.                      */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    dcd_sim_slave                         Pointer to device controller  */
48 /*    endpoint                              Pointer to endpoint container */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    Completion Status                                                   */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLED BY                                                             */
59 /*                                                                        */
60 /*    Slave Simulator Controller Driver                                   */
61 /*                                                                        */
62 /*  RELEASE HISTORY                                                       */
63 /*                                                                        */
64 /*    DATE              NAME                      DESCRIPTION             */
65 /*                                                                        */
66 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
67 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
68 /*                                            resulting in version 6.1    */
69 /*  04-02-2021     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            supported bi-dir-endpoints, */
71 /*                                            resulting in version 6.1.6  */
72 /*                                                                        */
73 /**************************************************************************/
_ux_dcd_sim_slave_endpoint_create(UX_DCD_SIM_SLAVE * dcd_sim_slave,UX_SLAVE_ENDPOINT * endpoint)74 UINT  _ux_dcd_sim_slave_endpoint_create(UX_DCD_SIM_SLAVE *dcd_sim_slave, UX_SLAVE_ENDPOINT *endpoint)
75 {
76 
77 UX_DCD_SIM_SLAVE_ED     *ed;
78 ULONG                   sim_slave_endpoint_index;
79 
80 
81     /* The simulator slave controller has 16 endpoints maximum. Endpoint 0 is always control.
82        The other endpoints are generic. We can use the endpoint number as an index.  */
83     sim_slave_endpoint_index =  endpoint ->ux_slave_endpoint_descriptor.bEndpointAddress & ~(ULONG)UX_ENDPOINT_DIRECTION;
84 
85     /* Fetch the address of the physical endpoint.  */
86 #ifdef UX_DEVICE_BIDIRECTIONAL_ENDPOINT_SUPPORT
87     ed = ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress == 0) ?
88             &dcd_sim_slave -> ux_dcd_sim_slave_ed[0] :
89             ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) ?
90                 &dcd_sim_slave -> ux_dcd_sim_slave_ed_in[sim_slave_endpoint_index] :
91                 &dcd_sim_slave -> ux_dcd_sim_slave_ed[sim_slave_endpoint_index]));
92 #else
93     ed =  &dcd_sim_slave -> ux_dcd_sim_slave_ed[sim_slave_endpoint_index];
94 #endif
95 
96     /* Check the endpoint status, if it is free, reserve it. If not reject this endpoint.  */
97     if ((ed -> ux_sim_slave_ed_status & UX_DCD_SIM_SLAVE_ED_STATUS_USED) == 0)
98     {
99 
100         /* We can use this endpoint.  */
101         ed -> ux_sim_slave_ed_status |=  UX_DCD_SIM_SLAVE_ED_STATUS_USED;
102 
103         /* Keep the physical endpoint address in the endpoint container.  */
104         endpoint -> ux_slave_endpoint_ed =  (VOID *) ed;
105 
106         /* And its mask.  */
107         ed -> ux_sim_slave_ed_index =  sim_slave_endpoint_index;
108 
109         /* Save the endpoint pointer.  */
110         ed -> ux_sim_slave_ed_endpoint =  endpoint;
111 
112         /* If this is endpoint 0, it is always ready for transactions.  */
113         if ( sim_slave_endpoint_index == 0)
114             ed -> ux_sim_slave_ed_status |= UX_DCD_SIM_SLAVE_ED_STATUS_TRANSFER;
115 
116         /* Enable this endpoint.  */
117         return(UX_SUCCESS);
118     }
119 
120     /* Notify application.  */
121     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_DCD, UX_MEMORY_INSUFFICIENT);
122 
123     /* Return error to caller.  */
124     return(UX_NO_ED_AVAILABLE);
125 }
126 
127