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_control_value_get              PORTABLE C      */
38 /*                                                           6.1.11       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function obtains the current values for a single video control.*/
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    video                                 Pointer to video class        */
50 /*    video_control                         Pointer to video control      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_stack_class_instance_verify  Verify instance is valid      */
59 /*    _ux_host_stack_transfer_request       Process transfer request      */
60 /*    _ux_host_semaphore_get                Get semaphore                 */
61 /*    _ux_host_semaphore_put                Release semaphore             */
62 /*    _ux_utility_memory_allocate           Allocate memory block         */
63 /*    _ux_utility_memory_free               Release memory block          */
64 /*    _ux_utility_short_get                 Read 16-bit value             */
65 /*    _ux_system_error_handler              Log system error              */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    Application                                                         */
70 /*    Video Class                                                         */
71 /*                                                                        */
72 /*  RELEASE HISTORY                                                       */
73 /*                                                                        */
74 /*    DATE              NAME                      DESCRIPTION             */
75 /*                                                                        */
76 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
77 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            resulting in version 6.1    */
79 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            refined macros names,       */
81 /*                                            resulting in version 6.1.10 */
82 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
83 /*                                            fixed standalone compile,   */
84 /*                                            resulting in version 6.1.11 */
85 /*                                                                        */
86 /**************************************************************************/
_ux_host_class_video_control_value_get(UX_HOST_CLASS_VIDEO * video,UX_HOST_CLASS_VIDEO_CONTROL * video_control)87 UINT  _ux_host_class_video_control_value_get(UX_HOST_CLASS_VIDEO *video, UX_HOST_CLASS_VIDEO_CONTROL *video_control)
88 {
89 
90 UX_ENDPOINT     *control_endpoint;
91 UX_TRANSFER     *transfer_request;
92 UINT            status;
93 UCHAR *         control_buffer;
94 
95 
96     /* Ensure the instance is valid.  */
97     if (_ux_host_stack_class_instance_verify(_ux_system_host_class_video_name, (VOID *) video) != UX_SUCCESS)
98     {
99 
100         /* Error trap. */
101         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
102 
103         /* If trace is enabled, insert this event into the trace buffer.  */
104         //UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, video, 0, 0, UX_TRACE_ERRORS, 0, 0)
105 
106         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
107     }
108 
109     /* Protect thread reentry to this instance.  */
110     status =  _ux_host_semaphore_get(&video -> ux_host_class_video_semaphore, UX_WAIT_FOREVER);
111     if (status != UX_SUCCESS)
112         return(status);
113 
114     /* We need to get the default control endpoint transfer request pointer.  */
115     control_endpoint =  &video -> ux_host_class_video_device -> ux_device_control_endpoint;
116     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
117 
118     /* Need to allocate memory for the control buffer.  */
119     control_buffer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, 2);
120     if (control_buffer == UX_NULL)
121     {
122 
123         /* Unprotect thread reentry to this instance.  */
124         _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
125 
126         /* Return an error.  */
127         return(UX_MEMORY_INSUFFICIENT);
128     }
129 
130     /* Protect the control endpoint semaphore here.  It will be unprotected in the
131        transfer request function.  */
132     status =  _ux_host_semaphore_get(&video -> ux_host_class_video_device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
133 
134     /* Check for status.  */
135     if (status != UX_SUCCESS)
136 
137         /* Something went wrong. */
138         return(status);
139 
140     /* Create a transfer request for the GET_MIN request.  */
141     transfer_request -> ux_transfer_request_data_pointer =      control_buffer;
142     transfer_request -> ux_transfer_request_requested_length =  2;
143     transfer_request -> ux_transfer_request_function =          UX_HOST_CLASS_VIDEO_GET_CUR;
144     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
145     transfer_request -> ux_transfer_request_value =             (video_control -> ux_host_class_video_control << 8);
146     transfer_request -> ux_transfer_request_index =             video -> ux_host_class_video_control_interface_number | (video -> ux_host_class_video_feature_unit_id << 8);
147 
148     /* Send request to HCD layer.  */
149     status =  _ux_host_stack_transfer_request(transfer_request);
150 
151     /* Check for correct transfer and entire control buffer returned.  */
152     if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == 2))
153     {
154 
155         /* Update the MIN static value for the caller.  */
156         video_control -> ux_host_class_video_control_cur =  (LONG)(SHORT)_ux_utility_short_get(control_buffer);
157     }
158     else
159     {
160 
161         /* Free the previous control buffer.  */
162         _ux_utility_memory_free(control_buffer);
163 
164         /* Unprotect thread reentry to this instance.  */
165         _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
166 
167         /* Return completion status.  */
168         return(UX_TRANSFER_ERROR);
169     }
170 
171     /* Free all used resources.  */
172     _ux_utility_memory_free(control_buffer);
173 
174     /* Unprotect thread reentry to this instance.  */
175     _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
176 
177     /* Return completion status.  */
178     return(status);
179 }
180 
181