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 /** Video Class */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23
24 /* Include necessary system files. */
25
26 #define UX_SOURCE_CODE
27
28 #include "ux_api.h"
29 #include "ux_host_class_video.h"
30 #include "ux_host_stack.h"
31
32
33 /**************************************************************************/
34 /* */
35 /* FUNCTION RELEASE */
36 /* */
37 /* _ux_host_class_video_transfer_buffers_add PORTABLE C */
38 /* 6.1.12 */
39 /* AUTHOR */
40 /* */
41 /* Chaoqiong Xiao, Microsoft Corporation */
42 /* */
43 /* DESCRIPTION */
44 /* */
45 /* This function adds buffers for video transfer requests. */
46 /* */
47 /* Usually it's the very first step to start a video stream on a high */
48 /* bandwidth isochronous endpoint. Since adding new buffer while */
49 /* the prepared buffers in progress helps to improve performance. */
50 /* */
51 /* Note the maximum number of transfers could be buffered is */
52 /* UX_HOST_CLASS_VIDEO_TRANSFER_REQUEST_COUNT - 1. */
53 /* */
54 /* Note check ux_host_class_video_max_payload_get to see minimum */
55 /* recommended buffer size. */
56 /* */
57 /* INPUT */
58 /* */
59 /* video Pointer to video class */
60 /* buffers Pointer to data buffer */
61 /* pointers array */
62 /* num_buffers Number of data buffers */
63 /* */
64 /* OUTPUT */
65 /* */
66 /* Completion Status */
67 /* */
68 /* CALLS */
69 /* */
70 /* _ux_host_stack_class_instance_verify Verify instance is valid */
71 /* _ux_host_stack_transfer_request Process transfer request */
72 /* _ux_host_semaphore_get Get semaphore */
73 /* _ux_host_semaphore_put Release semaphore */
74 /* _ux_system_error_handler Log system error */
75 /* */
76 /* CALLED BY */
77 /* */
78 /* Video Class */
79 /* */
80 /* RELEASE HISTORY */
81 /* */
82 /* DATE NAME DESCRIPTION */
83 /* */
84 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
85 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
86 /* resulting in version 6.1 */
87 /* 10-15-2021 Chaoqiong Xiao Modified comment(s), */
88 /* use pre-calculated value */
89 /* instead of wMaxPacketSize, */
90 /* resulting in version 6.1.9 */
91 /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */
92 /* refined macros names, */
93 /* resulting in version 6.1.10 */
94 /* 04-25-2022 Chaoqiong Xiao Modified comment(s), */
95 /* fixed standalone compile, */
96 /* resulting in version 6.1.11 */
97 /* 07-29-2022 Chaoqiong Xiao Modified comment(s), */
98 /* set pending on endpoint, */
99 /* resulting in version 6.1.12 */
100 /* */
101 /**************************************************************************/
_ux_host_class_video_transfer_buffers_add(UX_HOST_CLASS_VIDEO * video,UCHAR ** buffers,ULONG num_buffers)102 UINT _ux_host_class_video_transfer_buffers_add(UX_HOST_CLASS_VIDEO *video, UCHAR** buffers, ULONG num_buffers)
103 {
104
105 UINT status;
106 UX_TRANSFER *transfer_request;
107 UX_TRANSFER *previous_transfer;
108 UX_ENDPOINT *endpoint;
109 ULONG transfer_index;
110 ULONG packet_size;
111 UINT i;
112
113
114 /* Ensure the instance is valid. */
115 if (_ux_host_stack_class_instance_verify(_ux_system_host_class_video_name, (VOID *) video) != UX_SUCCESS)
116 {
117
118 /* Error trap. */
119 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
120
121 return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
122 }
123
124 /* Protect thread reentry to this instance. */
125 status = _ux_host_semaphore_get(&video -> ux_host_class_video_semaphore, UX_WAIT_FOREVER);
126 if (status != UX_SUCCESS)
127 return(status);
128
129 /* Get endpoint. */
130 endpoint = video -> ux_host_class_video_isochronous_endpoint;
131
132 /* Ensure we have a selected interface that allows isoch transmission. */
133 if ((endpoint == UX_NULL) ||
134 (endpoint -> ux_endpoint_descriptor.wMaxPacketSize == 0))
135 {
136
137 /* Unprotect thread reentry to this instance. */
138 _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
139
140 /* Error trap. */
141 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_VIDEO_WRONG_INTERFACE);
142
143 /* Return error status. */
144 return(UX_HOST_CLASS_VIDEO_WRONG_INTERFACE);
145 }
146
147 /* Check if there is enough requests available. */
148 if (video -> ux_host_class_video_transfer_request_start_index >=
149 video -> ux_host_class_video_transfer_request_end_index)
150 {
151
152 if (video -> ux_host_class_video_transfer_request_start_index + num_buffers >=
153 video -> ux_host_class_video_transfer_request_end_index + UX_HOST_CLASS_VIDEO_TRANSFER_REQUEST_COUNT)
154 status = (UX_MEMORY_ARRAY_FULL);
155 }
156 else
157 {
158 if (video -> ux_host_class_video_transfer_request_start_index + num_buffers >=
159 video -> ux_host_class_video_transfer_request_end_index)
160 status = (UX_MEMORY_ARRAY_FULL);
161 }
162
163 /* Check error. */
164 if (status != UX_SUCCESS)
165 {
166
167 /* Unprotect thread reentry to this instance. */
168 _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
169
170 /* Return error status. */
171 return(status);
172 }
173
174 /* Calculate packet size. */
175 packet_size = video -> ux_host_class_video_current_max_payload_size;
176
177 /* Add buffers one by one. */
178 for (i = 0,
179 transfer_index = video -> ux_host_class_video_transfer_request_start_index,
180 previous_transfer = UX_NULL;
181 i < num_buffers; i ++)
182 {
183
184 transfer_request = &video->ux_host_class_video_transfer_requests[transfer_index];
185
186 /* Select the direction. We do this by taking the endpoint direction. */
187 transfer_request -> ux_transfer_request_type = endpoint ->
188 ux_endpoint_descriptor.bEndpointAddress & UX_REQUEST_DIRECTION;
189
190 /* Fill the transfer request with all the required fields. */
191 transfer_request -> ux_transfer_request_endpoint = endpoint;
192 transfer_request -> ux_transfer_request_data_pointer = buffers[i];
193 transfer_request -> ux_transfer_request_requested_length = packet_size;
194 transfer_request -> ux_transfer_request_completion_function = _ux_host_class_video_transfer_request_callback;
195 transfer_request -> ux_transfer_request_class_instance = video;
196
197 /* Confirm the transfer request is single one. */
198 transfer_request -> ux_transfer_request_next_transfer_request = UX_NULL;
199
200 /* Link to transfer request tail. */
201 if (previous_transfer)
202 previous_transfer -> ux_transfer_request_next_transfer_request = transfer_request;
203
204 /* Save as previous request. */
205 previous_transfer = transfer_request;
206
207 /* Move to next transfer request. */
208 transfer_index ++;
209 if (transfer_index >= UX_HOST_CLASS_VIDEO_TRANSFER_REQUEST_COUNT)
210 transfer_index = 0;
211 }
212
213 /* Get request list head. */
214 transfer_request = &video -> ux_host_class_video_transfer_requests[video->ux_host_class_video_transfer_request_start_index];
215
216 /* Move request index. */
217 video -> ux_host_class_video_transfer_request_start_index = transfer_index;
218
219 /* Set endpoint to pending state, for callback and abort to check. */
220 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_completion_code = UX_TRANSFER_STATUS_PENDING;
221
222 /* Transfer the transfer request (list). */
223 status = _ux_host_stack_transfer_request(transfer_request);
224
225 /* Unprotect thread reentry to this instance. */
226 _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
227
228 /* Return completion status. */
229 return(status);
230 }
231