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_new_endpoint_create PORTABLE C */
36 /* 6.1.11 */
37 /* AUTHOR */
38 /* */
39 /* Chaoqiong Xiao, Microsoft Corporation */
40 /* */
41 /* DESCRIPTION */
42 /* */
43 /* This function creates a new endpoint for the current interface */
44 /* scanned. The endpoint is hooked to the interface that owns it. */
45 /* It is not active yet until either the default interface for the */
46 /* configuration is selected by a SET_CONFIGURATION or when an */
47 /* alternate setting for this interface is set. */
48 /* */
49 /* INPUT */
50 /* */
51 /* interface Interface container that owns */
52 /* this endpoint */
53 /* endpoint_pointer Pointer to a unparsed */
54 /* endpoint descriptor */
55 /* */
56 /* OUTPUT */
57 /* */
58 /* Completion Status */
59 /* */
60 /* CALLS */
61 /* */
62 /* _ux_utility_descriptor_parse Parse the descriptor */
63 /* _ux_utility_memory_allocate Allocate memory block */
64 /* */
65 /* CALLED BY */
66 /* */
67 /* USBX Components */
68 /* */
69 /* RELEASE HISTORY */
70 /* */
71 /* DATE NAME DESCRIPTION */
72 /* */
73 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
74 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
75 /* resulting in version 6.1 */
76 /* 10-15-2021 Chaoqiong Xiao Modified comment(s), */
77 /* added descriptor validate, */
78 /* resulting in version 6.1.9 */
79 /* 04-25-2022 Chaoqiong Xiao Modified comment(s), */
80 /* internal clean up, */
81 /* fixed size calculation, */
82 /* resulting in version 6.1.11 */
83 /* */
84 /**************************************************************************/
_ux_host_stack_new_endpoint_create(UX_INTERFACE * interface_ptr,UCHAR * interface_endpoint)85 UINT _ux_host_stack_new_endpoint_create(UX_INTERFACE *interface_ptr,
86 UCHAR * interface_endpoint)
87 {
88
89 UX_ENDPOINT *endpoint;
90 UX_ENDPOINT *list_endpoint;
91 ULONG endpoint_type;
92 ULONG packet_size;
93 ULONG n_tran;
94
95 /* Obtain memory for storing this new endpoint. */
96 endpoint = (UX_ENDPOINT *) _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_ENDPOINT));
97 if (endpoint == UX_NULL)
98 return(UX_MEMORY_INSUFFICIENT);
99
100 /* If trace is enabled, insert this event into the trace buffer. */
101 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_NEW_ENDPOINT_CREATE, interface_ptr, endpoint, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
102
103 /* Save the endpoint handle in the container, this is for ensuring the
104 endpoint container is not corrupted. */
105 endpoint -> ux_endpoint = (ULONG) (ALIGN_TYPE) endpoint;
106
107 /* The endpoint container has a built in transfer_request.
108 The transfer_request needs to point to the endpoint as well. */
109 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_endpoint = endpoint;
110
111 /* Save the pointer to the device. This is useful for the HCD layer. */
112 endpoint -> ux_endpoint_device = interface_ptr -> ux_interface_configuration -> ux_configuration_device;
113
114 /* Parse the interface descriptor and make it machine independent. */
115 _ux_utility_descriptor_parse(interface_endpoint,
116 _ux_system_endpoint_descriptor_structure,
117 UX_ENDPOINT_DESCRIPTOR_ENTRIES,
118 (UCHAR *) &endpoint -> ux_endpoint_descriptor);
119
120 /* Check endpoint size and interval to see if they are valid. */
121 endpoint_type = endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE;
122
123 /* Endpoint size should not be zero. */
124 if (endpoint -> ux_endpoint_descriptor.wMaxPacketSize == 0)
125 {
126 _ux_utility_memory_free(endpoint);
127 return(UX_DESCRIPTOR_CORRUPTED);
128 }
129
130 /* Control/bulk endpoint, 8, 16, 32, 64, ... 512 can be accepted.
131 Note non-standard size in 2^N is accepted since they really works. */
132 if (endpoint_type == UX_CONTROL_ENDPOINT || endpoint_type == UX_BULK_ENDPOINT)
133 {
134 for (packet_size = 8; packet_size <= 512; packet_size <<= 1)
135 {
136 if (packet_size == endpoint -> ux_endpoint_descriptor.wMaxPacketSize)
137 break;
138 }
139
140 /* If endpoint size not valid, return error. */
141 if (packet_size > 512)
142 {
143 _ux_utility_memory_free(endpoint);
144 return(UX_DESCRIPTOR_CORRUPTED);
145 }
146 }
147
148 /* Interrupt/isochronous endpoint, max 1024 and 3 transactions can be accepted. */
149 else
150 {
151
152 /* Max size over 1024 is not allowed. */
153 packet_size = endpoint -> ux_endpoint_descriptor.wMaxPacketSize & UX_MAX_PACKET_SIZE_MASK;
154 if (packet_size > 1024)
155 {
156 _ux_utility_memory_free(endpoint);
157 return(UX_DESCRIPTOR_CORRUPTED);
158 }
159
160 /* Number transaction over 2 additional is not allowed. */
161 n_tran = endpoint -> ux_endpoint_descriptor.wMaxPacketSize & UX_MAX_NUMBER_OF_TRANSACTIONS_MASK;
162 if (n_tran >= UX_MAX_NUMBER_OF_TRANSACTIONS_MASK)
163 {
164 _ux_utility_memory_free(endpoint);
165 return(UX_DESCRIPTOR_CORRUPTED);
166 }
167
168 /* Isochronous/high speed interrupt interval should be 1~16. */
169 if (endpoint -> ux_endpoint_descriptor.bInterval < 1)
170 {
171 _ux_utility_memory_free(endpoint);
172 return(UX_DESCRIPTOR_CORRUPTED);
173 }
174 if ((endpoint_type == UX_ISOCHRONOUS_ENDPOINT) ||
175 (interface_ptr -> ux_interface_configuration -> ux_configuration_device
176 -> ux_device_speed == UX_HIGH_SPEED_DEVICE)
177 )
178 {
179 if (endpoint -> ux_endpoint_descriptor.bInterval > 16)
180 {
181 _ux_utility_memory_free(endpoint);
182 return(UX_DESCRIPTOR_CORRUPTED);
183 }
184 }
185
186 /* Save final packet size. */
187 n_tran >>= UX_MAX_NUMBER_OF_TRANSACTIONS_SHIFT;
188 packet_size *= (n_tran + 1);
189 }
190
191 /* Save transfer packet size. */
192 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_packet_length = packet_size;
193
194 /* The interface that owns this endpoint is memorized in the
195 endpoint container itself, easier for back chaining. */
196 endpoint -> ux_endpoint_interface = interface_ptr;
197
198 /* There is 2 cases for the creation of the endpoint descriptor
199 if this is the first one, the endpoint descriptor is hooked
200 to the interface.
201 If it is not the first one, the endpoint is hooked to the
202 end of the chain of endpoints. */
203 if (interface_ptr -> ux_interface_first_endpoint == UX_NULL)
204 {
205
206 interface_ptr -> ux_interface_first_endpoint = endpoint;
207 }
208 else
209 {
210
211 list_endpoint = interface_ptr -> ux_interface_first_endpoint;
212
213 /* Traverse the list until the end. */
214 while (list_endpoint -> ux_endpoint_next_endpoint != UX_NULL)
215 list_endpoint = list_endpoint -> ux_endpoint_next_endpoint;
216
217 /* Hook the endpoint. */
218 list_endpoint -> ux_endpoint_next_endpoint = endpoint;
219 }
220
221 /* Return successful status. */
222 return(UX_SUCCESS);
223 }
224
225