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 /** USBX Component */
16 /** */
17 /** Device HID Class */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22 #define UX_SOURCE_CODE
23
24
25 /* Include necessary system files. */
26
27 #include "ux_api.h"
28 #include "ux_device_class_hid.h"
29 #include "ux_device_stack.h"
30
31
32 #if !defined(UX_DEVICE_STANDALONE)
33 /**************************************************************************/
34 /* */
35 /* FUNCTION RELEASE */
36 /* */
37 /* _ux_device_class_hid_read PORTABLE C */
38 /* 6.1.12 */
39 /* AUTHOR */
40 /* */
41 /* Chaoqiong Xiao, Microsoft Corporation */
42 /* */
43 /* DESCRIPTION */
44 /* */
45 /* This function reads from the HID class. */
46 /* This function must not be used with receiver related functions. */
47 /* This function is for RTOS mode. */
48 /* */
49 /* INPUT */
50 /* */
51 /* hid Address of hid class */
52 /* instance */
53 /* buffer Pointer to buffer to save */
54 /* received data */
55 /* requested_length Length of bytes to read */
56 /* actual_length Pointer to save number of */
57 /* bytes read */
58 /* */
59 /* OUTPUT */
60 /* */
61 /* None */
62 /* */
63 /* CALLS */
64 /* */
65 /* _ux_device_stack_transfer_request Transfer request */
66 /* _ux_utility_memory_copy Copy memory */
67 /* _ux_device_mutex_off Release mutex */
68 /* */
69 /* CALLED BY */
70 /* */
71 /* Application */
72 /* */
73 /* RELEASE HISTORY */
74 /* */
75 /* DATE NAME DESCRIPTION */
76 /* */
77 /* 01-31-2022 Chaoqiong Xiao Initial Version 6.1.10 */
78 /* 04-25-2022 Chaoqiong Xiao Modified comment(s), */
79 /* fixed standalone compile, */
80 /* resulting in version 6.1.11 */
81 /* 07-29-2022 Chaoqiong Xiao Modified comment(s), */
82 /* resulting in version 6.1.12 */
83 /* */
84 /**************************************************************************/
_ux_device_class_hid_read(UX_SLAVE_CLASS_HID * hid,UCHAR * buffer,ULONG requested_length,ULONG * actual_length)85 UINT _ux_device_class_hid_read(UX_SLAVE_CLASS_HID *hid, UCHAR *buffer,
86 ULONG requested_length, ULONG *actual_length)
87 {
88 #if !defined(UX_DEVICE_CLASS_HID_INTERRUPT_OUT_SUPPORT)
89 UX_PARAMETER_NOT_USED(hid);
90 UX_PARAMETER_NOT_USED(buffer);
91 UX_PARAMETER_NOT_USED(requested_length);
92 UX_PARAMETER_NOT_USED(actual_length);
93 return(UX_FUNCTION_NOT_SUPPORTED);
94 #else
95
96 UX_SLAVE_ENDPOINT *endpoint;
97 UX_SLAVE_DEVICE *device;
98 UX_SLAVE_TRANSFER *transfer_request;
99 UINT status= UX_SUCCESS;
100 ULONG local_requested_length;
101
102
103 /* If trace is enabled, insert this event into the trace buffer. */
104 UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_HID_READ, hid, buffer, requested_length, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
105
106 /* Get the pointer to the device. */
107 device = &_ux_system_slave -> ux_system_slave_device;
108
109 /* As long as the device is in the CONFIGURED state. */
110 if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
111 {
112
113 /* Error trap. */
114 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
115
116 /* If trace is enabled, insert this event into the trace buffer. */
117 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0)
118
119 /* Cannot proceed with command, the interface is down. */
120 return(UX_CONFIGURATION_HANDLE_UNKNOWN);
121 }
122
123 /* Protect this thread. */
124 _ux_device_mutex_on(&hid -> ux_device_class_hid_read_mutex);
125
126 /* Locate the endpoint. */
127 endpoint = hid -> ux_device_class_hid_read_endpoint;
128
129 /* All HID reading are on the endpoint OUT, from the host. */
130 transfer_request = &endpoint -> ux_slave_endpoint_transfer_request;
131
132 /* Reset the actual length. */
133 *actual_length = 0;
134
135 /* Check if we need more transactions. */
136 while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED && requested_length != 0)
137 {
138
139 /* Check if we have enough in the local buffer. */
140 if (requested_length > transfer_request -> ux_slave_transfer_request_transfer_length)
141
142 /* We have too much to transfer. */
143 local_requested_length = transfer_request -> ux_slave_transfer_request_transfer_length;
144
145 else
146
147 /* We can proceed with the demanded length. */
148 local_requested_length = requested_length;
149
150 /* Send the request to the device controller. */
151 status = _ux_device_stack_transfer_request(transfer_request, local_requested_length, local_requested_length);
152
153 /* Check the status */
154 if (status == UX_SUCCESS)
155 {
156
157 /* We need to copy the buffer locally. */
158 _ux_utility_memory_copy(buffer, transfer_request -> ux_slave_transfer_request_data_pointer,
159 transfer_request -> ux_slave_transfer_request_actual_length); /* Use case of memcpy is verified. */
160
161 /* Next buffer address. */
162 buffer += transfer_request -> ux_slave_transfer_request_actual_length;
163
164 /* Set the length actually received. */
165 *actual_length += transfer_request -> ux_slave_transfer_request_actual_length;
166
167 /* Decrement what left has to be done. */
168 requested_length -= transfer_request -> ux_slave_transfer_request_actual_length;
169
170 /* Is this a short packet or a ZLP indicating we are done with this transfer ? */
171 if (transfer_request -> ux_slave_transfer_request_actual_length < endpoint -> ux_slave_endpoint_descriptor.wMaxPacketSize)
172 {
173
174 /* We are done. */
175 /* Free Mutex resource. */
176 _ux_device_mutex_off(&hid -> ux_device_class_hid_read_mutex);
177
178 /* Return with success. */
179 return(UX_SUCCESS);
180 }
181 }
182 else
183 {
184
185 /* Free Mutex resource. */
186 _ux_device_mutex_off(&hid -> ux_device_class_hid_read_mutex);
187
188 /* We got an error. */
189 return(status);
190 }
191 }
192
193 /* Free Mutex resource. */
194 _ux_device_mutex_off(&hid -> ux_device_class_hid_read_mutex);
195
196 /* Check why we got here, either completion or device was extracted. */
197 if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
198 {
199
200 /* Error trap. */
201 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_NO_ANSWER);
202
203 /* If trace is enabled, insert this event into the trace buffer. */
204 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_TRANSFER_NO_ANSWER, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0)
205
206 /* Device must have been extracted. */
207 return (UX_TRANSFER_NO_ANSWER);
208 }
209 else
210
211 /* Simply return the last transaction result. */
212 return(status);
213 #endif
214 }
215 #endif
216