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