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_read_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 read.      */
45 /*                                                                        */
46 /*    It's for standalone mode.                                           */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    stream                                Pointer to video stream       */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    State machine status                                                */
55 /*    UX_STATE_EXIT                         Device not configured         */
56 /*    UX_STATE_IDLE                         No streaming transfer running */
57 /*    UX_STATE_WAIT                         Streaming transfer running    */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_device_stack_transfer_run         Run transfer state machine    */
62 /*    _ux_utility_memory_copy               Copy memory                   */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    USBX Device Stack                                                   */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  10-31-2022     Chaoqiong Xiao           Initial Version 6.2.0         */
73 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            useed zero copy when class  */
75 /*                                            owns endpoint buffer,       */
76 /*                                            resulting in version 6.3.0  */
77 /*                                                                        */
78 /**************************************************************************/
_ux_device_class_video_read_task_function(UX_DEVICE_CLASS_VIDEO_STREAM * stream)79 UINT _ux_device_class_video_read_task_function(UX_DEVICE_CLASS_VIDEO_STREAM *stream)
80 {
81 UX_SLAVE_DEVICE                 *device;
82 UX_SLAVE_ENDPOINT               *endpoint;
83 UX_SLAVE_TRANSFER               *transfer;
84 UCHAR                           *next_pos;
85 UX_DEVICE_CLASS_VIDEO_PAYLOAD   *next_payload;
86 ULONG                           max_packet_size;
87 ULONG                           actual_length;
88 UINT                            status;
89 
90 
91     /* Get the pointer to the device.  */
92     device = stream -> ux_device_class_video_stream_video -> ux_device_class_video_device;
93 
94     /* Check if the device is configured.  */
95     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
96     {
97         stream -> ux_device_class_video_stream_task_state = UX_STATE_EXIT;
98         return(UX_STATE_EXIT);
99     }
100 
101     /* Get the endpoint.  */
102     endpoint = stream -> ux_device_class_video_stream_endpoint;
103 
104     /* No endpoint ready, maybe it's alternate setting 0.  */
105     if (endpoint == UX_NULL)
106         return(UX_STATE_IDLE);
107 
108     /* Check if background transfer task is started.  */
109     if (stream -> ux_device_class_video_stream_task_state == UX_DEVICE_CLASS_VIDEO_STREAM_RW_STOP)
110         return(UX_STATE_IDLE);
111 
112     /* Get transfer instance.  */
113     transfer = &endpoint -> ux_slave_endpoint_transfer_request;
114 
115     /* If not started yet, reset transfer and start polling.  */
116     if (stream -> ux_device_class_video_stream_task_state == UX_DEVICE_CLASS_VIDEO_STREAM_RW_START)
117     {
118 
119         /* Next state: transfer wait.  */
120         stream -> ux_device_class_video_stream_task_state = UX_DEVICE_CLASS_VIDEO_STREAM_RW_WAIT;
121 
122 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
123 
124         /* Zero copy: directly use frame buffer.  */
125         transfer -> ux_slave_transfer_request_data_pointer = stream ->
126                 ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_data;
127 #endif
128 
129         /* Reset transfer state.  */
130         UX_SLAVE_TRANSFER_STATE_RESET(transfer);
131     }
132 
133     /* Run transfer state machine.  */
134     max_packet_size = endpoint -> ux_slave_endpoint_transfer_request.
135                                 ux_slave_transfer_request_transfer_length;
136     status = _ux_device_stack_transfer_run(transfer, max_packet_size, max_packet_size);
137 
138     /* Error case.  */
139     if (status < UX_STATE_NEXT)
140     {
141 
142         /* Error on background transfer task start.  */
143         stream -> ux_device_class_video_stream_task_state = UX_STATE_RESET;
144         stream -> ux_device_class_video_stream_task_status =
145                         transfer -> ux_slave_transfer_request_completion_code;
146 
147         /* Error notification!  */
148         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_ERROR);
149         return(UX_STATE_EXIT);
150     }
151 
152     /* Success case.  */
153     if (status == UX_STATE_NEXT)
154     {
155 
156         /* Next state: start.  */
157         stream -> ux_device_class_video_stream_task_state = UX_DEVICE_CLASS_VIDEO_STREAM_RW_START;
158         stream -> ux_device_class_video_stream_task_status =
159                         transfer -> ux_slave_transfer_request_completion_code;
160 
161         /* Get actual transfer length.  */
162         actual_length = transfer -> ux_slave_transfer_request_actual_length;
163 
164         /* Frame received, log it.  */
165         stream -> ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_length = actual_length;
166 
167 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
168 
169         /* Zero copy: data already in frame buffer.  */
170 #else
171 
172         /* Copy data from endpoint buffer.  */
173         _ux_utility_memory_copy(stream -> ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_data,
174                         transfer -> ux_slave_transfer_request_data_pointer,
175                         actual_length); /* Use case of memcpy is verified. */
176 #endif
177 
178         /* For simple, do not advance the transfer position if there is overflow.  */
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         /* Check overflow!  */
186         if (next_payload -> ux_device_class_video_payload_length > 0)
187         {
188 
189             /* Error notification!  */
190             stream -> ux_device_class_video_stream_buffer_error_count ++;
191             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
192         }
193         else
194 
195             /* Update transfer position.  */
196             stream -> ux_device_class_video_stream_transfer_pos = next_payload;
197 
198         /* Invoke notification callback. */
199         if (stream -> ux_device_class_video_stream_callbacks.ux_device_class_video_stream_payload_done != UX_NULL)
200             stream -> ux_device_class_video_stream_callbacks.ux_device_class_video_stream_payload_done(stream, actual_length);
201     }
202 
203     /* Keep waiting.  */
204     return(UX_STATE_WAIT);
205 }
206 #endif
207