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 /**   Host Stack                                                          */
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_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_stack_device_string_get                    PORTABLE C      */
37 /*                                                           6.1.10       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function obtains the device string descriptor.                 */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    device                                Pointer to device instance    */
49 /*    descriptor_buffer                     Pointer to a buffer to fill   */
50 /*                                          LANGID or STRING descriptor   */
51 /*    length                                Length of buffer              */
52 /*    language_id                           0 to obtain LANGID descriptor */
53 /*                                          valid language ID to obtain   */
54 /*                                          string descriptor             */
55 /*    string_index                          Index of the string           */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    Completion Status                                                   */
60 /*                                                                        */
61 /*  CALLS                                                                 */
62 /*                                                                        */
63 /*    _ux_host_stack_transfer_request       Process transfer request      */
64 /*    _ux_utility_semaphore_get             Get Semaphore                 */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application                                                         */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  02-02-2021     Chaoqiong Xiao           Initial Version 6.1.4         */
75 /*  06-02-2021     Chaoqiong Xiao           Modified comment(s), and      */
76 /*                                            removed unnecessary header, */
77 /*                                            resulting in version 6.1.7  */
78 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            added standalone support,   */
80 /*                                            resulting in version 6.1.10 */
81 /*                                                                        */
82 /**************************************************************************/
_ux_host_stack_device_string_get(UX_DEVICE * device,UCHAR * descriptor_buffer,ULONG length,ULONG language_id,ULONG string_index)83 UINT  _ux_host_stack_device_string_get(UX_DEVICE *device, UCHAR *descriptor_buffer, ULONG length, ULONG language_id, ULONG string_index)
84 {
85 #if defined(UX_HOST_STANDALONE)
86 UX_INTERRUPT_SAVE_AREA
87 #endif
88 UX_ENDPOINT     *control_endpoint;
89 UX_TRANSFER     *transfer_request;
90 UINT            status;
91 
92 
93     /* Do a sanity check on the device handle.  */
94     if (device -> ux_device_handle != (ULONG) (ALIGN_TYPE) device)
95     {
96 
97         /* Error trap. */
98         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_DEVICE_HANDLE_UNKNOWN);
99 
100         /* If trace is enabled, insert this event into the trace buffer.  */
101         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DEVICE_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0)
102 
103         return(UX_DEVICE_HANDLE_UNKNOWN);
104     }
105 
106     /* If trace is enabled, insert this event into the trace buffer.  */
107     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_DEVICE_STRING_GET, device, descriptor_buffer, length, (language_id << 16) | string_index, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
108 
109     /* We need to get the default control endpoint transfer request pointer.  */
110     control_endpoint =  &device -> ux_device_control_endpoint;
111     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
112 
113 #if defined(UX_HOST_STANDALONE)
114     UX_DISABLE
115     if ((device -> ux_device_flags & UX_DEVICE_FLAG_LOCK) ||
116         (transfer_request -> ux_transfer_request_flags & UX_TRANSFER_FLAG_LOCK))
117     {
118         UX_RESTORE
119         return(UX_BUSY);
120     }
121     device -> ux_device_flags |= UX_DEVICE_FLAG_LOCK;
122     transfer_request -> ux_transfer_request_flags |=
123                 (UX_TRANSFER_FLAG_LOCK | UX_TRANSFER_FLAG_AUTO_DEVICE_UNLOCK);
124     UX_RESTORE
125 #else
126 
127     /* Protect the control endpoint semaphore here.  It will be unprotected in the
128        transfer request function.  */
129     status =  _ux_host_semaphore_get(&device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
130 
131     /* Check for status.  */
132     if (status != UX_SUCCESS)
133         return(UX_SEMAPHORE_ERROR);
134 #endif
135 
136     /* Create a transfer request for the GET_DEVICE_ID request.  */
137     transfer_request -> ux_transfer_request_data_pointer =      descriptor_buffer;
138     transfer_request -> ux_transfer_request_requested_length =  length;
139     transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
140     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
141     transfer_request -> ux_transfer_request_value =             (UX_STRING_DESCRIPTOR_ITEM << 8) | string_index;
142     transfer_request -> ux_transfer_request_index =             (language_id);
143 
144     /* Send request to HCD layer.  */
145     status =  _ux_host_stack_transfer_request(transfer_request);
146 
147     /* Return completion status.  */
148     return(status);
149 }
150 
151 
152 /**************************************************************************/
153 /*                                                                        */
154 /*  FUNCTION                                               RELEASE        */
155 /*                                                                        */
156 /*    _uxe_host_stack_device_string_get                   PORTABLE C      */
157 /*                                                           6.3.0        */
158 /*  AUTHOR                                                                */
159 /*                                                                        */
160 /*    Chaoqiong Xiao, Microsoft Corporation                               */
161 /*                                                                        */
162 /*  DESCRIPTION                                                           */
163 /*                                                                        */
164 /*    This function checks errors in host stack device get function call. */
165 /*                                                                        */
166 /*  INPUT                                                                 */
167 /*                                                                        */
168 /*    device                                Pointer to device instance    */
169 /*    descriptor_buffer                     Pointer to a buffer to fill   */
170 /*                                          LANGID or STRING descriptor   */
171 /*    length                                Length of buffer              */
172 /*    language_id                           0 to obtain LANGID descriptor */
173 /*                                          valid language ID to obtain   */
174 /*                                          string descriptor             */
175 /*    string_index                          Index of the string           */
176 /*                                                                        */
177 /*  OUTPUT                                                                */
178 /*                                                                        */
179 /*    None                                                                */
180 /*                                                                        */
181 /*  CALLS                                                                 */
182 /*                                                                        */
183 /*    _ux_host_stack_device_string_get      String descriptor get         */
184 /*                                                                        */
185 /*  CALLED BY                                                             */
186 /*                                                                        */
187 /*    Application                                                         */
188 /*                                                                        */
189 /*  RELEASE HISTORY                                                       */
190 /*                                                                        */
191 /*    DATE              NAME                      DESCRIPTION             */
192 /*                                                                        */
193 /*  10-31-2023     Chaoqiong Xiao           Initial Version 6.3.0         */
194 /*                                                                        */
195 /**************************************************************************/
_uxe_host_stack_device_string_get(UX_DEVICE * device,UCHAR * descriptor_buffer,ULONG length,ULONG language_id,ULONG string_index)196 UINT  _uxe_host_stack_device_string_get(UX_DEVICE *device, UCHAR *descriptor_buffer, ULONG length, ULONG language_id, ULONG string_index)
197 {
198 
199     /* Sanity check.  */
200     if ((device == UX_NULL) || (descriptor_buffer == UX_NULL) || (length == 0))
201         return(UX_INVALID_PARAMETER);
202 
203     /* Invoke string descriptor get function.  */
204     return(_ux_host_stack_device_string_get(device, descriptor_buffer, length, language_id, string_index));
205 }
206