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_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 OUT from 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_read_thread_entry(ULONG video_stream)80 VOID _ux_device_class_video_read_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                           max_packet_size;
91 ULONG                           actual_length;
92 
93 
94     /* Get Video class 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         max_packet_size = 0;
104         while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
105         {
106 
107             /* Get endpoint instance.  */
108             endpoint = stream -> ux_device_class_video_stream_endpoint;
109 
110             /* Endpoint not available, maybe it's alternate setting 0.  */
111             if (endpoint == UX_NULL)
112                 break;
113 
114             /* Calculate transfer size based on packet size and number transactions once endpoint is available.  */
115             if (max_packet_size == 0)
116                 max_packet_size = endpoint -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_transfer_length;
117 
118             /* Get transfer instance.  */
119             transfer = &endpoint -> ux_slave_endpoint_transfer_request;
120 
121 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
122 
123             /* Zero copy: directly use frame buffer for transfer.  */
124             transfer -> ux_slave_transfer_request_data_pointer = stream ->
125                     ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_data;
126 #endif
127 
128             /* Start payload transfer anyway.  */
129             status = _ux_device_stack_transfer_request(transfer, max_packet_size, max_packet_size);
130 
131             /* Check error.  */
132             if (status != UX_SUCCESS)
133             {
134 
135                 /* Error notification!  */
136                 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_ERROR);
137                 break;
138             }
139 
140             /* Get actual transfer length.  */
141             actual_length = transfer -> ux_slave_transfer_request_actual_length;
142 
143             /* Frame received, log it.  */
144             stream -> ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_length = actual_length;
145 
146 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
147 
148             /* Zero copy: data already in frame buffer.  */
149 #else
150 
151             /* Copy data from endpoint buffer.  */
152             _ux_utility_memory_copy(stream -> ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_data,
153                             transfer -> ux_slave_transfer_request_data_pointer,
154                             actual_length); /* Use case of memcpy is verified. */
155 #endif
156 
157             /* For simple, do not advance the transfer position if there is overflow.  */
158             next_pos = (UCHAR *)stream -> ux_device_class_video_stream_transfer_pos;
159             next_pos += stream -> ux_device_class_video_stream_payload_buffer_size;
160             if (next_pos >= stream -> ux_device_class_video_stream_buffer + stream -> ux_device_class_video_stream_buffer_size)
161                 next_pos = stream -> ux_device_class_video_stream_buffer;
162             next_payload = (UX_DEVICE_CLASS_VIDEO_PAYLOAD *)next_pos;
163 
164             /* Check overflow!  */
165             if (next_payload -> ux_device_class_video_payload_length > 0)
166             {
167 
168                 /* Error notification!  */
169                 stream -> ux_device_class_video_stream_buffer_error_count ++;
170                 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
171             }
172             else
173 
174                 /* Update transfer position.  */
175                 stream -> ux_device_class_video_stream_transfer_pos = next_payload;
176 
177             /* Invoke notification callback. */
178             if (stream -> ux_device_class_video_stream_callbacks.ux_device_class_video_stream_payload_done != UX_NULL)
179                 stream -> ux_device_class_video_stream_callbacks.ux_device_class_video_stream_payload_done(stream, actual_length);
180         }
181 
182         /* We need to suspend ourselves. We will be resumed by the device enumeration module or when a change of alternate setting happens.  */
183         _ux_utility_thread_suspend(&stream -> ux_device_class_video_stream_thread);
184     }
185 }
186 #endif
187