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 /** USBX Component */
15 /** */
16 /** Device Video Class */
17 /** */
18 /**************************************************************************/
19 /**************************************************************************/
20
21 #define UX_SOURCE_CODE
22
23
24 /* Include necessary system files. */
25
26 #include "ux_api.h"
27 #include "ux_device_class_video.h"
28 #include "ux_device_stack.h"
29
30
31 #if defined(UX_DEVICE_STANDALONE)
32 /**************************************************************************/
33 /* */
34 /* FUNCTION RELEASE */
35 /* */
36 /* _ux_device_class_video_write_task_function PORTABLE C */
37 /* 6.3.0 */
38 /* AUTHOR */
39 /* */
40 /* Chaoqiong Xiao, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function is the background task of the video stream write. */
45 /* */
46 /* It's for standalone mode. */
47 /* */
48 /* INPUT */
49 /* */
50 /* video_class Address of video class */
51 /* container */
52 /* */
53 /* OUTPUT */
54 /* */
55 /* State machine status */
56 /* UX_STATE_EXIT Device not configured */
57 /* UX_STATE_IDLE No streaming transfer running */
58 /* UX_STATE_WAIT Streaming transfer running */
59 /* */
60 /* CALLS */
61 /* */
62 /* _ux_device_stack_transfer_run Run transfer state machine */
63 /* _ux_utility_memory_copy Copy memory */
64 /* */
65 /* CALLED BY */
66 /* */
67 /* USBX Device Stack */
68 /* */
69 /* RELEASE HISTORY */
70 /* */
71 /* DATE NAME DESCRIPTION */
72 /* */
73 /* 10-31-2022 Chaoqiong Xiao Initial Version 6.2.0 */
74 /* 10-31-2023 Chaoqiong Xiao Modified comment(s), */
75 /* useed zero copy when class */
76 /* owns endpoint buffer, */
77 /* resulting in version 6.3.0 */
78 /* */
79 /**************************************************************************/
_ux_device_class_video_write_task_function(UX_DEVICE_CLASS_VIDEO_STREAM * stream)80 UINT _ux_device_class_video_write_task_function(UX_DEVICE_CLASS_VIDEO_STREAM *stream)
81 {
82 UX_SLAVE_DEVICE *device;
83 UX_SLAVE_ENDPOINT *endpoint;
84 UX_SLAVE_TRANSFER *transfer;
85 UCHAR *next_pos;
86 UX_DEVICE_CLASS_VIDEO_PAYLOAD *next_payload;
87 ULONG transfer_length;
88 ULONG actual_length;
89 UINT status;
90
91
92 /* Get the pointer to the device. */
93 device = stream -> ux_device_class_video_stream_video -> ux_device_class_video_device;
94
95 /* Check if the device is configured. */
96 if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
97 {
98 stream -> ux_device_class_video_stream_task_state = UX_STATE_EXIT;
99 return(UX_STATE_EXIT);
100 }
101
102 /* Get the endpoint. */
103 endpoint = stream -> ux_device_class_video_stream_endpoint;
104
105 /* No endpoint ready, maybe it's alternate setting 0. */
106 if (endpoint == UX_NULL)
107 return(UX_STATE_IDLE);
108
109 /* Check if background transfer task is started. */
110 if (stream -> ux_device_class_video_stream_task_state == UX_DEVICE_CLASS_VIDEO_STREAM_RW_STOP)
111 return(UX_STATE_IDLE);
112
113 /* Get transfer instance. */
114 transfer = &endpoint -> ux_slave_endpoint_transfer_request;
115
116 /* If not started yet, prepare data, reset transfer and start polling. */
117 if (stream -> ux_device_class_video_stream_task_state == UX_DEVICE_CLASS_VIDEO_STREAM_RW_START)
118 {
119
120 /* Next state: transfer wait. */
121 stream -> ux_device_class_video_stream_task_state = UX_DEVICE_CLASS_VIDEO_STREAM_RW_WAIT;
122
123 /* Start payload transfer anyway (even ZLP). */
124 transfer_length = stream -> ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_length;
125
126 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
127
128 /* Zero copy: directly use frame buffer for transfer. */
129 transfer -> ux_slave_transfer_request_data_pointer = stream ->
130 ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_data;
131 #else
132
133 /* Copy frame buffer to transfer buffer. */
134 if (transfer_length)
135 _ux_utility_memory_copy(transfer -> ux_slave_transfer_request_data_pointer,
136 stream -> ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_data, transfer_length); /* Use case of memcpy is verified. */
137 #endif
138
139 /* Reset transfer state. */
140 UX_SLAVE_TRANSFER_STATE_RESET(transfer);
141 }
142
143 /* Get current transfer length. */
144 transfer_length = stream -> ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_length;
145
146 /* Run transfer states. */
147 status = _ux_device_stack_transfer_run(transfer, transfer_length, transfer_length);
148
149 /* Error case. */
150 if (status < UX_STATE_NEXT)
151 {
152
153 /* Error on background transfer task start. */
154 stream -> ux_device_class_video_stream_task_state = UX_STATE_RESET;
155 stream -> ux_device_class_video_stream_task_status =
156 transfer -> ux_slave_transfer_request_completion_code;
157
158 /* Error notification! */
159 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_ERROR);
160 return(UX_STATE_EXIT);
161 }
162
163 /* Success case. */
164 if (status == UX_STATE_NEXT)
165 {
166
167 /* Next state: start. */
168 stream -> ux_device_class_video_stream_task_state = UX_DEVICE_CLASS_VIDEO_STREAM_RW_START;
169 stream -> ux_device_class_video_stream_task_status =
170 transfer -> ux_slave_transfer_request_completion_code;
171
172 /* Frame sent, free it. */
173 stream -> ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_length = 0;
174
175 /* Get actual transfer length. */
176 actual_length = transfer -> ux_slave_transfer_request_actual_length;
177
178 /* Calculate next position. */
179 next_pos = (UCHAR *)stream -> ux_device_class_video_stream_transfer_pos;
180 next_pos += stream -> ux_device_class_video_stream_payload_buffer_size;
181 if (next_pos >= stream -> ux_device_class_video_stream_buffer + stream -> ux_device_class_video_stream_buffer_size)
182 next_pos = stream -> ux_device_class_video_stream_buffer;
183 next_payload = (UX_DEVICE_CLASS_VIDEO_PAYLOAD *)next_pos;
184
185 /* Underflow check! */
186 if (transfer_length)
187 {
188
189 /* Advance position. */
190 stream -> ux_device_class_video_stream_transfer_pos = next_payload;
191
192 /* Error trap! */
193 if (next_payload -> ux_device_class_video_payload_length == 0)
194 {
195 stream -> ux_device_class_video_stream_buffer_error_count ++;
196 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
197 }
198 }
199 else
200 {
201
202 /* Advance position if next payload available. */
203 if (next_payload -> ux_device_class_video_payload_length)
204 stream -> ux_device_class_video_stream_transfer_pos = next_payload;
205 else
206 {
207
208 /* Error trap! */
209 stream -> ux_device_class_video_stream_buffer_error_count ++;
210 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
211 }
212 }
213
214 /* Invoke notification callback. */
215 if (stream -> ux_device_class_video_stream_callbacks.ux_device_class_video_stream_payload_done != UX_NULL)
216 stream -> ux_device_class_video_stream_callbacks.ux_device_class_video_stream_payload_done(stream, actual_length);
217 }
218
219 /* Keep waiting. */
220 return(UX_STATE_WAIT);
221 }
222 #endif
223