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_buffer_add            PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function adds a buffer for video transfer requests.            */
46 /*                                                                        */
47 /*    Note check ux_host_class_video_max_payload_get to see minimum       */
48 /*    recommended buffer size.                                            */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    video                                 Pointer to video class        */
53 /*    buffer                                Pointer to data buffer        */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Completion Status                                                   */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_host_stack_class_instance_verify  Verify instance is valid      */
62 /*    _ux_host_stack_transfer_request       Process transfer request      */
63 /*    _ux_host_semaphore_get                Get semaphore                 */
64 /*    _ux_host_semaphore_put                Release semaphore             */
65 /*    _ux_system_error_handler              Log system error              */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    Video Class                                                         */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
76 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            use pre-calculated value    */
80 /*                                            instead of wMaxPacketSize,  */
81 /*                                            resulting in version 6.1.9  */
82 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
83 /*                                            refined macros names,       */
84 /*                                            resulting in version 6.1.10 */
85 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
86 /*                                            fixed standalone compile,   */
87 /*                                            resulting in version 6.1.11 */
88 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
89 /*                                            set pending on endpoint,    */
90 /*                                            resulting in version 6.1.12 */
91 /*                                                                        */
92 /**************************************************************************/
_ux_host_class_video_transfer_buffer_add(UX_HOST_CLASS_VIDEO * video,UCHAR * buffer)93 UINT  _ux_host_class_video_transfer_buffer_add(UX_HOST_CLASS_VIDEO *video, UCHAR* buffer)
94 {
95 
96 UINT            status;
97 UX_TRANSFER     *transfer_request;
98 UX_ENDPOINT     *endpoint;
99 ULONG           transfer_index;
100 ULONG           packet_size;
101 
102     /* Ensure the instance is valid.  */
103     if (_ux_host_stack_class_instance_verify(_ux_system_host_class_video_name, (VOID *) video) != UX_SUCCESS)
104     {
105 
106         /* Error trap. */
107         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
108 
109         /* If trace is enabled, insert this event into the trace buffer.  */
110     //    UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, video, 0, 0, UX_TRACE_ERRORS, 0, 0)
111 
112         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
113     }
114 
115     /* Protect thread reentry to this instance.  */
116     status =  _ux_host_semaphore_get(&video -> ux_host_class_video_semaphore, UX_WAIT_FOREVER);
117     if (status != UX_SUCCESS)
118         return(status);
119 
120     /* Get endpoint.  */
121     endpoint = video -> ux_host_class_video_isochronous_endpoint;
122 
123     /* Ensure we have a selected interface that allows isoch transmission.  */
124     if ((endpoint == UX_NULL) ||
125         (endpoint -> ux_endpoint_descriptor.wMaxPacketSize == 0))
126     {
127 
128         /* Unprotect thread reentry to this instance.  */
129         _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
130 
131         /* Error trap. */
132         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_VIDEO_WRONG_INTERFACE);
133 
134         /* Return error status.  */
135         return(UX_HOST_CLASS_VIDEO_WRONG_INTERFACE);
136     }
137 
138     transfer_index = video->ux_host_class_video_transfer_request_start_index;
139     transfer_index ++;
140     if (transfer_index == UX_HOST_CLASS_VIDEO_TRANSFER_REQUEST_COUNT)
141         transfer_index = 0;
142     if (transfer_index == video->ux_host_class_video_transfer_request_end_index)
143     {
144 
145         /* Unprotect thread reentry to this instance.  */
146         _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
147 
148         /* Return error status.  */
149         return(UX_MEMORY_ARRAY_FULL);
150     }
151 
152     transfer_request = &video->ux_host_class_video_transfer_requests[video->ux_host_class_video_transfer_request_start_index];
153     video->ux_host_class_video_transfer_request_start_index = transfer_index;
154 
155     /* Select the direction. We do this by taking the endpoint direction.  */
156     transfer_request -> ux_transfer_request_type =  endpoint ->
157                 ux_endpoint_descriptor.bEndpointAddress & UX_REQUEST_DIRECTION;
158 
159     /* Calculate packet size.  */
160     packet_size = video -> ux_host_class_video_current_max_payload_size;
161 
162     /* Fill the transfer request with all the required fields.  */
163     transfer_request -> ux_transfer_request_endpoint =             endpoint;
164     transfer_request -> ux_transfer_request_data_pointer =         buffer;
165     transfer_request -> ux_transfer_request_requested_length =     packet_size;
166     transfer_request -> ux_transfer_request_completion_function =  _ux_host_class_video_transfer_request_callback;
167     transfer_request -> ux_transfer_request_class_instance =       video;
168 
169     /* Add single transfer.  */
170     transfer_request -> ux_transfer_request_next_transfer_request = UX_NULL;
171 
172     /* Set endpoint to pending state, for callback and abort to check.  */
173     endpoint -> ux_endpoint_transfer_request.ux_transfer_request_completion_code = UX_TRANSFER_STATUS_PENDING;
174 
175     /* Transfer the transfer request.  */
176     status =  _ux_host_stack_transfer_request(transfer_request);
177 
178     /* Unprotect thread reentry to this instance.  */
179     _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
180 
181     /* Return completion status.  */
182     return(status);
183 }
184