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 /**   Host Stack                                                          */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_host_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_host_stack_endpoint_instance_create             PORTABLE C      */
36 /*                                                           6.1.10       */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function will create an endpoint instance. The HCD layer is    */
44 /*    invoked to create each endpoint and the bandwidth claimed by each   */
45 /*    endpoint is allocated.                                              */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    endpoint                              Endpoint to delete            */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_host_stack_bandwidth_check        Check bandwidth               */
58 /*    _ux_host_stack_bandwidth_claim        Claim bandwidth               */
59 /*    _ux_utility_semaphore_create          Semaphore create              */
60 /*    (ux_hcd_entry_function)               HCD entry function            */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    USBX Components                                                     */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
71 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            optimized based on compile  */
73 /*                                            definitions,                */
74 /*                                            resulting in version 6.1    */
75 /*  06-02-2021     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            fixed trace enabled error,  */
77 /*                                            filled default endpoint     */
78 /*                                            request endpoint pointer,   */
79 /*                                            resulting in version 6.1.7  */
80 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            added standalone support,   */
82 /*                                            resulting in version 6.1.10 */
83 /*                                                                        */
84 /**************************************************************************/
_ux_host_stack_endpoint_instance_create(UX_ENDPOINT * endpoint)85 UINT  _ux_host_stack_endpoint_instance_create(UX_ENDPOINT *endpoint)
86 {
87 
88 UX_HCD          *hcd;
89 UINT            status;
90 UCHAR           endpoint_type;
91 
92 
93     /* Obtain the HCD for this endpoint.  */
94     hcd = UX_DEVICE_HCD_GET(endpoint -> ux_endpoint_device);
95 
96     /* If trace is enabled, insert this event into the trace buffer.  */
97     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_ENDPOINT_INSTANCE_CREATE, endpoint -> ux_endpoint_device, endpoint, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
98 
99 
100     /* If the endpoint needs guaranteed bandwidth, check if we have enough */
101     endpoint_type = (endpoint -> ux_endpoint_descriptor.bmAttributes) & UX_MASK_ENDPOINT_TYPE;
102     switch (endpoint_type)
103     {
104 
105     case UX_CONTROL_ENDPOINT:
106     case UX_BULK_ENDPOINT:
107 
108         break;
109 
110     default:
111 
112         /* Check the bandwidth for this endpoint */
113         if (_ux_host_stack_bandwidth_check(hcd, endpoint) != UX_SUCCESS)
114         {
115 
116             /* Error trap. */
117             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_NO_BANDWIDTH_AVAILABLE);
118 
119             return(UX_NO_BANDWIDTH_AVAILABLE);
120         }
121 
122 
123         break;
124     }
125 
126     /* Create this endpoint.  */
127     status = hcd -> ux_hcd_entry_function(hcd, UX_HCD_CREATE_ENDPOINT, (VOID *) endpoint);
128 
129     /* Check status.  */
130     if (status != UX_SUCCESS)
131     {
132 
133         /* Return completion status.  */
134         return(status);
135     }
136 
137     /* Claim bandwidth if needed.  */
138     if ((endpoint_type == UX_INTERRUPT_ENDPOINT) || (endpoint_type == UX_ISOCHRONOUS_ENDPOINT))
139     {
140 
141         /* Claim its bandwidth */
142         _ux_host_stack_bandwidth_claim(hcd, endpoint);
143     }
144 
145     /* Create a semaphore for this endpoint to be attached to its transfer request.  */
146     status =  _ux_host_semaphore_create(&endpoint -> ux_endpoint_transfer_request.ux_transfer_request_semaphore,
147                                                                 "ux_transfer_request_semaphore", 0);
148 
149     /* Check status.  */
150     if (status == UX_SUCCESS)
151     {
152 
153         /* If trace is enabled, register this object.  */
154         UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_ENDPOINT, endpoint, 0, 0, 0)
155 
156         /* By default transfer request contained is for endpoint itself.  */
157         endpoint -> ux_endpoint_transfer_request.ux_transfer_request_endpoint = endpoint;
158     }
159 
160     /* Return completion status.  */
161     return(status);
162 }
163 
164