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 /** HID 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_hid.h"
30 #include "ux_host_stack.h"
31
32
33 /**************************************************************************/
34 /* */
35 /* FUNCTION RELEASE */
36 /* */
37 /* _ux_host_class_hid_idle_get PORTABLE C */
38 /* 6.1.10 */
39 /* AUTHOR */
40 /* */
41 /* Chaoqiong Xiao, Microsoft Corporation */
42 /* */
43 /* DESCRIPTION */
44 /* */
45 /* This function performs a GET_IDLE to the HID device. */
46 /* */
47 /* INPUT */
48 /* */
49 /* hid Pointer to HID class */
50 /* idle_time Idle time */
51 /* report_id Report ID */
52 /* */
53 /* OUTPUT */
54 /* */
55 /* Completion Status */
56 /* */
57 /* CALLS */
58 /* */
59 /* _ux_host_stack_class_instance_verify Verify instance is valid */
60 /* _ux_host_stack_transfer_request Process transfer request */
61 /* _ux_host_semaphore_get Get protection semaphore */
62 /* _ux_host_semaphore_put Release protection semaphore */
63 /* */
64 /* CALLED BY */
65 /* */
66 /* HID Class */
67 /* */
68 /* RELEASE HISTORY */
69 /* */
70 /* DATE NAME DESCRIPTION */
71 /* */
72 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
73 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
74 /* protected default control */
75 /* endpoint before using it, */
76 /* resulting in version 6.1 */
77 /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */
78 /* added standalone support, */
79 /* resulting in version 6.1.10 */
80 /* */
81 /**************************************************************************/
_ux_host_class_hid_idle_get(UX_HOST_CLASS_HID * hid,USHORT * idle_time,USHORT report_id)82 UINT _ux_host_class_hid_idle_get(UX_HOST_CLASS_HID *hid, USHORT *idle_time, USHORT report_id)
83 {
84 #if defined(UX_HOST_STANDALONE)
85 UX_INTERRUPT_SAVE_AREA
86 #endif
87 UX_ENDPOINT *control_endpoint;
88 UX_TRANSFER *transfer_request;
89 UCHAR *idle_byte;
90 UINT status;
91
92 /* If trace is enabled, insert this event into the trace buffer. */
93 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HID_IDLE_GET, hid, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
94
95 /* Ensure the instance is valid. */
96 if (_ux_host_stack_class_instance_verify(_ux_system_host_class_hid_name, (VOID *) hid) != UX_SUCCESS)
97 {
98
99 /* If trace is enabled, insert this event into the trace buffer. */
100 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, hid, 0, 0, UX_TRACE_ERRORS, 0, 0)
101
102 return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
103 }
104
105 /* We need to get the default control endpoint transfer request pointer. */
106 control_endpoint = &hid -> ux_host_class_hid_device -> ux_device_control_endpoint;
107 transfer_request = &control_endpoint -> ux_endpoint_transfer_request;
108
109 /* Protect thread reentry to this instance. */
110 #if defined(UX_HOST_STANDALONE)
111 UX_DISABLE
112 if (hid -> ux_host_class_hid_flags & UX_HOST_CLASS_HID_FLAG_LOCK)
113 {
114 UX_RESTORE
115 return(UX_BUSY);
116 }
117 hid -> ux_host_class_hid_flags &= ~UX_HOST_CLASS_HID_FLAG_LOCK;
118 UX_RESTORE
119 #else
120
121 status = _ux_host_semaphore_get(&hid -> ux_host_class_hid_semaphore, UX_WAIT_FOREVER);
122 if (status != UX_SUCCESS)
123
124 /* Return error. */
125 return(status);
126 #endif
127
128 /* Need to allocate memory for the idle byte. */
129 idle_byte = _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, 1);
130 if (idle_byte == UX_NULL)
131 {
132
133 /* Unprotect thread reentry to this instance. */
134 _ux_host_class_hid_unlock(hid);
135
136 /* Return error status. */
137 return(UX_MEMORY_INSUFFICIENT);
138 }
139
140 /* Protect the control endpoint semaphore here. It will be unprotected in the
141 transfer request function. */
142 #if defined(UX_HOST_STANDALONE)
143 UX_DISABLE
144 if (hid -> ux_host_class_hid_device -> ux_device_flags & UX_DEVICE_FLAG_LOCK)
145 {
146
147 /* Free allocated return busy. */
148 _ux_utility_memory_free(idle_byte);
149 hid -> ux_host_class_hid_flags &= ~UX_HOST_CLASS_HID_FLAG_LOCK;
150 UX_RESTORE
151 return(UX_BUSY);
152 }
153 hid -> ux_host_class_hid_device -> ux_device_flags |= UX_DEVICE_FLAG_LOCK;
154 transfer_request -> ux_transfer_request_flags |= UX_TRANSFER_FLAG_AUTO_DEVICE_UNLOCK;
155 UX_TRANSFER_STATE_RESET(transfer_request);
156 UX_RESTORE
157 #else
158 status = _ux_host_semaphore_get(&hid -> ux_host_class_hid_device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
159
160 /* Check for status. */
161 if (status != UX_SUCCESS)
162 {
163
164 /* Something went wrong. */
165
166 /* Free allocated memory. */
167 _ux_utility_memory_free(idle_byte);
168
169 /* Unprotect thread reentry to this instance. */
170 _ux_host_semaphore_put(&hid -> ux_host_class_hid_semaphore);
171
172 return(status);
173 }
174 #endif
175
176 /* Create a transfer request for the GET_IDLE request. */
177 transfer_request -> ux_transfer_request_data_pointer = idle_byte;
178 transfer_request -> ux_transfer_request_requested_length = 1;
179 transfer_request -> ux_transfer_request_function = UX_HOST_CLASS_HID_GET_IDLE;
180 transfer_request -> ux_transfer_request_type = UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
181 transfer_request -> ux_transfer_request_value = report_id;
182 transfer_request -> ux_transfer_request_index = hid -> ux_host_class_hid_interface -> ux_interface_descriptor.bInterfaceNumber;
183
184 /* Send request to HCD layer. */
185 status = _ux_host_stack_transfer_request(transfer_request);
186
187 #if defined(UX_HOST_STANDALONE)
188 if (!(transfer_request -> ux_transfer_request_flags & UX_TRANSFER_FLAG_AUTO_WAIT))
189 {
190 hid -> ux_host_class_hid_allocated = idle_byte;
191 return(status);
192 }
193 #endif
194
195 /* Check for correct transfer and for the entire descriptor returned. */
196 if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == 1))
197 *idle_time = (USHORT) *idle_byte;
198
199 /* Free used resources. */
200 _ux_utility_memory_free(idle_byte);
201
202 /* Unprotect thread reentry to this instance. */
203 #if defined(UX_HOST_STANDALONE)
204 _ux_host_class_hid_unlock(hid);
205 #else
206 _ux_host_semaphore_put(&hid -> ux_host_class_hid_semaphore);
207 #endif
208
209 /* Return the completion status. */
210 return(status);
211 }
212