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_thread_entry PORTABLE C */
37 /* 6.3.0 */
38 /* AUTHOR */
39 /* */
40 /* Chaoqiong Xiao, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function is thread of ISO IN for the Video class. */
45 /* */
46 /* INPUT */
47 /* */
48 /* video_stream Address of video stream */
49 /* instance */
50 /* */
51 /* OUTPUT */
52 /* */
53 /* None */
54 /* */
55 /* CALLS */
56 /* */
57 /* _ux_system_error_handler System error trap */
58 /* _ux_utility_thread_suspend Suspend thread used */
59 /* _ux_device_stack_transfer_request Issue transfer request */
60 /* _ux_utility_memory_copy Copy data */
61 /* */
62 /* CALLED BY */
63 /* */
64 /* ThreadX */
65 /* */
66 /* RELEASE HISTORY */
67 /* */
68 /* DATE NAME DESCRIPTION */
69 /* */
70 /* 04-25-2022 Chaoqiong Xiao Initial Version 6.1.11 */
71 /* 10-31-2022 Chaoqiong Xiao Modified comment(s), */
72 /* added error statistics, */
73 /* resulting in 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_thread_entry(ULONG video_stream)80 VOID _ux_device_class_video_write_thread_entry(ULONG video_stream)
81 {
82
83 UINT status;
84 UX_DEVICE_CLASS_VIDEO_STREAM *stream;
85 UX_SLAVE_DEVICE *device;
86 UX_SLAVE_ENDPOINT *endpoint;
87 UX_SLAVE_TRANSFER *transfer;
88 UCHAR *next_pos;
89 UX_DEVICE_CLASS_VIDEO_PAYLOAD *next_payload;
90 ULONG transfer_length;
91 ULONG actual_length;
92
93
94 /* Get Video class stream instance. */
95 UX_THREAD_EXTENSION_PTR_GET(stream, UX_DEVICE_CLASS_VIDEO_STREAM, video_stream)
96
97 /* Get stack device instance. */
98 device = stream -> ux_device_class_video_stream_video -> ux_device_class_video_device;
99
100 /* This thread runs forever but can be suspended or resumed. */
101 while(1)
102 {
103 while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
104 {
105
106 /* Get endpoint instance. */
107 endpoint = stream -> ux_device_class_video_stream_endpoint;
108
109 /* Endpoint not available, maybe it's alternate setting 0. */
110 if (endpoint == UX_NULL)
111 break;
112
113 /* Get transfer instance. */
114 transfer = &endpoint -> ux_slave_endpoint_transfer_request;
115
116 /* Start payload transfer anyway (even ZLP). */
117 transfer_length = stream -> ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_length;
118
119 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
120
121 /* Zero copy: directly use frame buffer for transfer. */
122 transfer -> ux_slave_transfer_request_data_pointer = stream ->
123 ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_data;
124 #else
125
126 /* Copy frame buffer to transfer buffer. */
127 if (transfer_length)
128 _ux_utility_memory_copy(transfer -> ux_slave_transfer_request_data_pointer,
129 stream -> ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_data, transfer_length); /* Use case of memcpy is verified. */
130 #endif
131
132 /* Issue transfer request, thread blocked until transfer done. */
133 status = _ux_device_stack_transfer_request(transfer, transfer_length, transfer_length);
134
135 /* Check error. */
136 if (status != UX_SUCCESS)
137 {
138
139 /* Error notification! */
140 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_ERROR);
141 break;
142 }
143
144 /* Frame sent, free it. */
145 stream -> ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_length = 0;
146
147 /* Get actual transfer length. */
148 actual_length = transfer -> ux_slave_transfer_request_actual_length;
149
150 /* Calculate next position. */
151 next_pos = (UCHAR *)stream -> ux_device_class_video_stream_transfer_pos;
152 next_pos += stream -> ux_device_class_video_stream_payload_buffer_size;
153 if (next_pos >= stream -> ux_device_class_video_stream_buffer + stream -> ux_device_class_video_stream_buffer_size)
154 next_pos = stream -> ux_device_class_video_stream_buffer;
155 next_payload = (UX_DEVICE_CLASS_VIDEO_PAYLOAD *)next_pos;
156
157 /* Underflow check! */
158 if (transfer_length)
159 {
160
161 /* Advance position. */
162 stream -> ux_device_class_video_stream_transfer_pos = next_payload;
163
164 /* Error trap! */
165 if (next_payload -> ux_device_class_video_payload_length == 0)
166 {
167 stream -> ux_device_class_video_stream_buffer_error_count ++;
168 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
169 }
170 }
171 else
172 {
173
174 /* Advance position if next payload available. */
175 if (next_payload -> ux_device_class_video_payload_length)
176 stream -> ux_device_class_video_stream_transfer_pos = next_payload;
177 else
178 {
179
180 /* Error trap! */
181 stream -> ux_device_class_video_stream_buffer_error_count ++;
182 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
183 }
184 }
185
186 /* Invoke notification callback. */
187 if (stream -> ux_device_class_video_stream_callbacks.ux_device_class_video_stream_payload_done != UX_NULL)
188 stream -> ux_device_class_video_stream_callbacks.ux_device_class_video_stream_payload_done(stream, actual_length);
189 }
190
191 /* We need to suspend ourselves. We will be resumed by the device enumeration module or when a change of alternate setting happens. */
192 _ux_utility_thread_suspend(&stream -> ux_device_class_video_stream_thread);
193 }
194 }
195 #endif
196