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_report_descriptor_get PORTABLE C */
38 /* 6.1 */
39 /* AUTHOR */
40 /* */
41 /* Chaoqiong Xiao, Microsoft Corporation */
42 /* */
43 /* DESCRIPTION */
44 /* */
45 /* This function gets the report descriptor and analyzes it. */
46 /* */
47 /* INPUT */
48 /* */
49 /* hid Pointer to HID class */
50 /* length Length of descriptor */
51 /* */
52 /* OUTPUT */
53 /* */
54 /* Completion Status */
55 /* */
56 /* CALLS */
57 /* */
58 /* _ux_host_class_hid_global_item_parse Parse global item */
59 /* _ux_host_class_hid_local_item_parse Parse local item */
60 /* _ux_host_class_hid_report_item_analyse Analyze report */
61 /* _ux_host_class_hid_resources_free Free HID resources */
62 /* _ux_host_stack_transfer_request Process transfer request */
63 /* _ux_utility_memory_allocate Allocate memory block */
64 /* _ux_utility_memory_free Release memory block */
65 /* */
66 /* CALLED BY */
67 /* */
68 /* HID Class */
69 /* */
70 /* RELEASE HISTORY */
71 /* */
72 /* DATE NAME DESCRIPTION */
73 /* */
74 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
75 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
76 /* resulting in version 6.1 */
77 /* */
78 /**************************************************************************/
_ux_host_class_hid_report_descriptor_get(UX_HOST_CLASS_HID * hid,ULONG length)79 UINT _ux_host_class_hid_report_descriptor_get(UX_HOST_CLASS_HID *hid, ULONG length)
80 {
81
82 UX_ENDPOINT *control_endpoint;
83 UX_TRANSFER *transfer_request;
84 UCHAR *descriptor;
85 UCHAR *start_descriptor;
86 UX_HOST_CLASS_HID_ITEM item;
87 UINT status;
88
89
90 /* We need to get the default control endpoint transfer request pointer. */
91 control_endpoint = &hid -> ux_host_class_hid_device -> ux_device_control_endpoint;
92 transfer_request = &control_endpoint -> ux_endpoint_transfer_request;
93
94 /* Need to allocate memory for the report descriptor. */
95 descriptor = _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, length);
96 if (descriptor == UX_NULL)
97 return(UX_MEMORY_INSUFFICIENT);
98
99 /* We need to save the descriptor starting address. */
100 start_descriptor = descriptor;
101
102 /* Create a transfer_request for the GET_DESCRIPTOR request */
103 transfer_request -> ux_transfer_request_data_pointer = descriptor;
104 transfer_request -> ux_transfer_request_requested_length = length;
105 transfer_request -> ux_transfer_request_function = UX_GET_DESCRIPTOR;
106 transfer_request -> ux_transfer_request_type = UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_INTERFACE;
107 transfer_request -> ux_transfer_request_value = UX_HOST_CLASS_HID_REPORT_DESCRIPTOR << 8;
108 transfer_request -> ux_transfer_request_index = hid -> ux_host_class_hid_interface -> ux_interface_descriptor.bInterfaceNumber;
109
110 /* Send request to HCD layer. */
111 status = _ux_host_stack_transfer_request(transfer_request);
112
113 /* Check for correct transfer and entire descriptor returned. */
114 if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == length))
115 {
116
117 /* Parse the report descriptor and build the report items. */
118 while (length)
119 {
120
121 /* Get one item from the report and analyze it. */
122 _ux_host_class_hid_report_item_analyse(descriptor, &item);
123
124 /* Point the descriptor right after the item identifier. */
125 descriptor += item.ux_host_class_hid_item_report_format;
126
127 /* Process relative to the item type. */
128 switch (item.ux_host_class_hid_item_report_type)
129 {
130
131 case UX_HOST_CLASS_HID_TYPE_GLOBAL:
132
133 /* This is a global item. */
134 status = _ux_host_class_hid_global_item_parse(hid, &item, descriptor);
135 break;
136
137
138 case UX_HOST_CLASS_HID_TYPE_MAIN:
139
140 /* This is a main item. */
141 status = _ux_host_class_hid_main_item_parse(hid, &item, descriptor);
142 break;
143
144
145 case UX_HOST_CLASS_HID_TYPE_LOCAL:
146
147 /* This is a local item. */
148 status = _ux_host_class_hid_local_item_parse(hid, &item, descriptor);
149 break;
150
151 default:
152
153 /* This is a reserved item, meaning it shouldn't be used! */
154
155 /* Set status to error. The check after this switch statement
156 will handle the rest. */
157 status = UX_DESCRIPTOR_CORRUPTED;
158 break;
159 }
160
161 /* Recheck the status code. */
162 if (status != UX_SUCCESS)
163 {
164 break;
165 }
166
167 /* Jump to the next item. */
168 descriptor += item.ux_host_class_hid_item_report_length;
169
170 /* Verify that the report descriptor is not corrupted. */
171 if (length < item.ux_host_class_hid_item_report_length)
172 {
173
174 /* Return error status. */
175 status = (UX_DESCRIPTOR_CORRUPTED);
176 break;
177 }
178
179 /* Adjust the length. */
180 length -= (ULONG)(item.ux_host_class_hid_item_report_length + item.ux_host_class_hid_item_report_format);
181 }
182 }
183 else
184
185 /* Descriptor length error! */
186 status = UX_DESCRIPTOR_CORRUPTED;
187
188 if (status != UX_SUCCESS)
189 {
190 /* Error trap. */
191 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
192
193 /* If trace is enabled, insert this event into the trace buffer. */
194 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
195
196 /* The descriptor is corrupted! */
197 _ux_host_class_hid_resources_free(hid);
198 }
199
200 /* Free used resources. */
201 _ux_utility_memory_free(start_descriptor);
202
203 /* Return completion status. */
204 return(status);
205 }
206
207