]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiUsbLib/Hid.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / UefiUsbLib / Hid.c
CommitLineData
ce821dff 1/** @file\r
2\r
11ceade4
LG
3 The library provides USB HID Class standard and specific requests defined\r
4 in USB HID Firmware Specification 7 section : Requests.\r
9095d37b
LG
5\r
6 Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>\r
9344f092 7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
ce821dff 8\r
9**/\r
10\r
6c401377 11#include "UefiUsbLibInternal.h"\r
ce821dff 12\r
9095d37b 13//\r
11ceade4
LG
14// Hid RequestType Bits specifying characteristics of request.\r
15// Valid values are 10100001b (0xa1) or 00100001b (0x21).\r
16// The following description:\r
17// 7 Data transfer direction\r
18// 0 = Host to device\r
19// 1 = Device to host\r
20// 6..5 Type\r
21// 1 = Class\r
22// 4..0 Recipient\r
23// 1 = Interface\r
24//\r
ce821dff 25\r
26/**\r
d5954c61 27 Get the descriptor of the specified USB HID interface.\r
ce821dff 28\r
d5954c61 29 Submit a USB get HID descriptor request for the USB device specified by UsbIo\r
30 and Interface and return the HID descriptor in HidDescriptor.\r
31 If UsbIo is NULL, then ASSERT().\r
32 If HidDescriptor is NULL, then ASSERT().\r
ce821dff 33\r
d5954c61 34 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
35 @param Interface The index of the HID interface on the USB target.\r
2fc59a00 36 @param HidDescriptor The pointer to the USB HID descriptor that was retrieved from\r
d5954c61 37 the specified USB target and interface. Type EFI_USB_HID_DESCRIPTOR\r
38 is defined in the MDE Package Industry Standard include file Usb.h.\r
39\r
40 @retval EFI_SUCCESS The request executed successfully.\r
41 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
42 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 43\r
44**/\r
45EFI_STATUS\r
373b5cf9 46EFIAPI\r
ce821dff 47UsbGetHidDescriptor (\r
2f88bd3a
MK
48 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
49 IN UINT8 Interface,\r
50 OUT EFI_USB_HID_DESCRIPTOR *HidDescriptor\r
ce821dff 51 )\r
52{\r
53 UINT32 Status;\r
54 EFI_STATUS Result;\r
55 EFI_USB_DEVICE_REQUEST Request;\r
d5954c61 56\r
2f88bd3a
MK
57 ASSERT (UsbIo != NULL);\r
58 ASSERT (HidDescriptor != NULL);\r
ce821dff 59\r
11ceade4
LG
60 Request.RequestType = USB_HID_GET_DESCRIPTOR_REQ_TYPE;\r
61 Request.Request = USB_REQ_GET_DESCRIPTOR;\r
2f88bd3a 62 Request.Value = (UINT16)(USB_DESC_TYPE_HID << 8);\r
d5954c61 63 Request.Index = Interface;\r
2f88bd3a 64 Request.Length = (UINT16)sizeof (EFI_USB_HID_DESCRIPTOR);\r
ce821dff 65\r
66 Result = UsbIo->UsbControlTransfer (\r
67 UsbIo,\r
68 &Request,\r
69 EfiUsbDataIn,\r
65442978 70 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 71 HidDescriptor,\r
72 sizeof (EFI_USB_HID_DESCRIPTOR),\r
73 &Status\r
74 );\r
75\r
76 return Result;\r
ce821dff 77}\r
ce821dff 78\r
79/**\r
d5954c61 80 Get the report descriptor of the specified USB HID interface.\r
ce821dff 81\r
d5954c61 82 Submit a USB get HID report descriptor request for the USB device specified by\r
83 UsbIo and Interface and return the report descriptor in DescriptorBuffer.\r
84 If UsbIo is NULL, then ASSERT().\r
85 If DescriptorBuffer is NULL, then ASSERT().\r
ce821dff 86\r
d5954c61 87 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
88 @param Interface The index of the report interface on the USB target.\r
89 @param DescriptorLength The size, in bytes, of DescriptorBuffer.\r
90 @param DescriptorBuffer A pointer to the buffer to store the report class descriptor.\r
91\r
92 @retval EFI_SUCCESS The request executed successfully.\r
93 @retval EFI_OUT_OF_RESOURCES The request could not be completed because the\r
070a76b1 94 buffer specified by DescriptorLength and DescriptorBuffer\r
d5954c61 95 is not large enough to hold the result of the request.\r
96 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
97 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 98\r
99**/\r
100EFI_STATUS\r
373b5cf9 101EFIAPI\r
ce821dff 102UsbGetReportDescriptor (\r
2f88bd3a
MK
103 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
104 IN UINT8 Interface,\r
105 IN UINT16 DescriptorLength,\r
106 OUT UINT8 *DescriptorBuffer\r
ce821dff 107 )\r
108{\r
109 UINT32 Status;\r
110 EFI_STATUS Result;\r
111 EFI_USB_DEVICE_REQUEST Request;\r
112\r
d5954c61 113 ASSERT (UsbIo != NULL);\r
114 ASSERT (DescriptorBuffer != NULL);\r
115\r
ce821dff 116 //\r
117 // Fill Device request packet\r
118 //\r
11ceade4
LG
119 Request.RequestType = USB_HID_GET_DESCRIPTOR_REQ_TYPE;\r
120 Request.Request = USB_REQ_GET_DESCRIPTOR;\r
2f88bd3a 121 Request.Value = (UINT16)(USB_DESC_TYPE_REPORT << 8);\r
d5954c61 122 Request.Index = Interface;\r
123 Request.Length = DescriptorLength;\r
ce821dff 124\r
125 Result = UsbIo->UsbControlTransfer (\r
126 UsbIo,\r
127 &Request,\r
128 EfiUsbDataIn,\r
65442978 129 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 130 DescriptorBuffer,\r
d5954c61 131 DescriptorLength,\r
ce821dff 132 &Status\r
133 );\r
134\r
135 return Result;\r
ce821dff 136}\r
ce821dff 137\r
138/**\r
d5954c61 139 Get the HID protocol of the specified USB HID interface.\r
140\r
141 Submit a USB get HID protocol request for the USB device specified by UsbIo\r
142 and Interface and return the protocol retrieved in Protocol.\r
143 If UsbIo is NULL, then ASSERT().\r
144 If Protocol is NULL, then ASSERT().\r
ce821dff 145\r
d5954c61 146 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
147 @param Interface The index of the report interface on the USB target.\r
148 @param Protocol A pointer to the protocol for the specified USB target.\r
ce821dff 149\r
d5954c61 150 @retval EFI_SUCCESS The request executed successfully.\r
151 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
152 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 153\r
154**/\r
155EFI_STATUS\r
373b5cf9 156EFIAPI\r
ce821dff 157UsbGetProtocolRequest (\r
2f88bd3a
MK
158 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
159 IN UINT8 Interface,\r
160 OUT UINT8 *Protocol\r
ce821dff 161 )\r
162{\r
163 UINT32 Status;\r
164 EFI_STATUS Result;\r
165 EFI_USB_DEVICE_REQUEST Request;\r
166\r
d5954c61 167 ASSERT (UsbIo != NULL);\r
168 ASSERT (Protocol != NULL);\r
169\r
ce821dff 170 //\r
171 // Fill Device request packet\r
172 //\r
11ceade4 173 Request.RequestType = USB_HID_CLASS_GET_REQ_TYPE;\r
2f88bd3a
MK
174 Request.Request = EFI_USB_GET_PROTOCOL_REQUEST;\r
175 Request.Value = 0;\r
176 Request.Index = Interface;\r
177 Request.Length = 1;\r
ce821dff 178\r
179 Result = UsbIo->UsbControlTransfer (\r
180 UsbIo,\r
181 &Request,\r
182 EfiUsbDataIn,\r
65442978 183 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 184 Protocol,\r
185 sizeof (UINT8),\r
186 &Status\r
187 );\r
188\r
189 return Result;\r
190}\r
191\r
ce821dff 192/**\r
d5954c61 193 Set the HID protocol of the specified USB HID interface.\r
ce821dff 194\r
d5954c61 195 Submit a USB set HID protocol request for the USB device specified by UsbIo\r
196 and Interface and set the protocol to the value specified by Protocol.\r
197 If UsbIo is NULL, then ASSERT().\r
ce821dff 198\r
d5954c61 199 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
200 @param Interface The index of the report interface on the USB target.\r
201 @param Protocol The protocol value to set for the specified USB target.\r
202\r
203 @retval EFI_SUCCESS The request executed successfully.\r
204 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
205 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 206\r
207**/\r
208EFI_STATUS\r
373b5cf9 209EFIAPI\r
ce821dff 210UsbSetProtocolRequest (\r
2f88bd3a
MK
211 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
212 IN UINT8 Interface,\r
213 IN UINT8 Protocol\r
ce821dff 214 )\r
215{\r
216 UINT32 Status;\r
217 EFI_STATUS Result;\r
218 EFI_USB_DEVICE_REQUEST Request;\r
219\r
d5954c61 220 ASSERT (UsbIo != NULL);\r
9095d37b 221\r
ce821dff 222 //\r
223 // Fill Device request packet\r
224 //\r
11ceade4 225 Request.RequestType = USB_HID_CLASS_SET_REQ_TYPE;\r
2f88bd3a
MK
226 Request.Request = EFI_USB_SET_PROTOCOL_REQUEST;\r
227 Request.Value = Protocol;\r
228 Request.Index = Interface;\r
229 Request.Length = 0;\r
ce821dff 230\r
231 Result = UsbIo->UsbControlTransfer (\r
232 UsbIo,\r
233 &Request,\r
234 EfiUsbNoData,\r
65442978 235 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 236 NULL,\r
237 0,\r
238 &Status\r
239 );\r
240 return Result;\r
241}\r
242\r
ce821dff 243/**\r
d5954c61 244 Set the idle rate of the specified USB HID report.\r
ce821dff 245\r
d5954c61 246 Submit a USB set HID report idle request for the USB device specified by UsbIo,\r
247 Interface, and ReportId, and set the idle rate to the value specified by Duration.\r
248 If UsbIo is NULL, then ASSERT().\r
ce821dff 249\r
d5954c61 250 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
251 @param Interface The index of the report interface on the USB target.\r
252 @param ReportId The identifier of the report to retrieve.\r
253 @param Duration The idle rate to set for the specified USB target.\r
254\r
255 @retval EFI_SUCCESS The request executed successfully.\r
256 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
257 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 258\r
259**/\r
260EFI_STATUS\r
373b5cf9 261EFIAPI\r
ce821dff 262UsbSetIdleRequest (\r
2f88bd3a
MK
263 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
264 IN UINT8 Interface,\r
265 IN UINT8 ReportId,\r
266 IN UINT8 Duration\r
ce821dff 267 )\r
268{\r
269 UINT32 Status;\r
270 EFI_STATUS Result;\r
271 EFI_USB_DEVICE_REQUEST Request;\r
272\r
d5954c61 273 ASSERT (UsbIo != NULL);\r
ce821dff 274 //\r
275 // Fill Device request packet\r
276 //\r
11ceade4 277 Request.RequestType = USB_HID_CLASS_SET_REQ_TYPE;\r
2f88bd3a
MK
278 Request.Request = EFI_USB_SET_IDLE_REQUEST;\r
279 Request.Value = (UINT16)((Duration << 8) | ReportId);\r
280 Request.Index = Interface;\r
281 Request.Length = 0;\r
ce821dff 282\r
283 Result = UsbIo->UsbControlTransfer (\r
284 UsbIo,\r
285 &Request,\r
286 EfiUsbNoData,\r
65442978 287 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 288 NULL,\r
289 0,\r
290 &Status\r
291 );\r
292 return Result;\r
293}\r
294\r
ce821dff 295/**\r
d5954c61 296 Get the idle rate of the specified USB HID report.\r
297\r
298 Submit a USB get HID report idle request for the USB device specified by UsbIo,\r
299 Interface, and ReportId, and return the ide rate in Duration.\r
300 If UsbIo is NULL, then ASSERT().\r
301 If Duration is NULL, then ASSERT().\r
ce821dff 302\r
d5954c61 303 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
304 @param Interface The index of the report interface on the USB target.\r
305 @param ReportId The identifier of the report to retrieve.\r
306 @param Duration A pointer to the idle rate retrieved from the specified USB target.\r
ce821dff 307\r
d5954c61 308 @retval EFI_SUCCESS The request executed successfully.\r
309 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
310 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 311\r
312**/\r
313EFI_STATUS\r
373b5cf9 314EFIAPI\r
ce821dff 315UsbGetIdleRequest (\r
2f88bd3a
MK
316 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
317 IN UINT8 Interface,\r
318 IN UINT8 ReportId,\r
319 OUT UINT8 *Duration\r
ce821dff 320 )\r
321{\r
322 UINT32 Status;\r
323 EFI_STATUS Result;\r
324 EFI_USB_DEVICE_REQUEST Request;\r
9095d37b 325\r
d5954c61 326 ASSERT (UsbIo != NULL);\r
327 ASSERT (Duration != NULL);\r
ce821dff 328 //\r
329 // Fill Device request packet\r
330 //\r
11ceade4 331 Request.RequestType = USB_HID_CLASS_GET_REQ_TYPE;\r
2f88bd3a
MK
332 Request.Request = EFI_USB_GET_IDLE_REQUEST;\r
333 Request.Value = ReportId;\r
334 Request.Index = Interface;\r
335 Request.Length = 1;\r
ce821dff 336\r
337 Result = UsbIo->UsbControlTransfer (\r
338 UsbIo,\r
339 &Request,\r
340 EfiUsbDataIn,\r
65442978 341 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 342 Duration,\r
343 1,\r
344 &Status\r
345 );\r
346\r
347 return Result;\r
348}\r
349\r
ce821dff 350/**\r
d5954c61 351 Set the report descriptor of the specified USB HID interface.\r
352\r
353 Submit a USB set HID report request for the USB device specified by UsbIo,\r
354 Interface, ReportId, and ReportType, and set the report descriptor using the\r
355 buffer specified by ReportLength and Report.\r
356 If UsbIo is NULL, then ASSERT().\r
357 If Report is NULL, then ASSERT().\r
ce821dff 358\r
d5954c61 359 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
360 @param Interface The index of the report interface on the USB target.\r
361 @param ReportId The identifier of the report to retrieve.\r
362 @param ReportType The type of report to retrieve.\r
363 @param ReportLength The size, in bytes, of Report.\r
364 @param Report A pointer to the report descriptor buffer to set.\r
ce821dff 365\r
d5954c61 366 @retval EFI_SUCCESS The request executed successfully.\r
367 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
368 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 369\r
370**/\r
371EFI_STATUS\r
373b5cf9 372EFIAPI\r
ce821dff 373UsbSetReportRequest (\r
2f88bd3a
MK
374 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
375 IN UINT8 Interface,\r
376 IN UINT8 ReportId,\r
377 IN UINT8 ReportType,\r
378 IN UINT16 ReportLen,\r
379 IN UINT8 *Report\r
ce821dff 380 )\r
381{\r
382 UINT32 Status;\r
383 EFI_STATUS Result;\r
384 EFI_USB_DEVICE_REQUEST Request;\r
385\r
d5954c61 386 ASSERT (UsbIo != NULL);\r
387 ASSERT (Report != NULL);\r
388\r
ce821dff 389 //\r
390 // Fill Device request packet\r
391 //\r
11ceade4 392 Request.RequestType = USB_HID_CLASS_SET_REQ_TYPE;\r
2f88bd3a
MK
393 Request.Request = EFI_USB_SET_REPORT_REQUEST;\r
394 Request.Value = (UINT16)((ReportType << 8) | ReportId);\r
395 Request.Index = Interface;\r
396 Request.Length = ReportLen;\r
ce821dff 397\r
398 Result = UsbIo->UsbControlTransfer (\r
399 UsbIo,\r
400 &Request,\r
401 EfiUsbDataOut,\r
65442978 402 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 403 Report,\r
070a76b1 404 ReportLen,\r
ce821dff 405 &Status\r
406 );\r
407\r
408 return Result;\r
409}\r
410\r
ce821dff 411/**\r
d5954c61 412 Get the report descriptor of the specified USB HID interface.\r
413\r
414 Submit a USB get HID report request for the USB device specified by UsbIo,\r
415 Interface, ReportId, and ReportType, and return the report in the buffer\r
416 specified by Report.\r
417 If UsbIo is NULL, then ASSERT().\r
418 If Report is NULL, then ASSERT().\r
419\r
420 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
421 @param Interface The index of the report interface on the USB target.\r
422 @param ReportId The identifier of the report to retrieve.\r
423 @param ReportType The type of report to retrieve.\r
424 @param ReportLength The size, in bytes, of Report.\r
425 @param Report A pointer to the buffer to store the report descriptor.\r
426\r
427 @retval EFI_SUCCESS The request executed successfully.\r
428 @retval EFI_OUT_OF_RESOURCES The request could not be completed because the\r
28d3e14f 429 buffer specified by ReportLength and Report is not\r
d5954c61 430 large enough to hold the result of the request.\r
431 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
432 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 433\r
434**/\r
435EFI_STATUS\r
373b5cf9 436EFIAPI\r
ce821dff 437UsbGetReportRequest (\r
2f88bd3a
MK
438 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
439 IN UINT8 Interface,\r
440 IN UINT8 ReportId,\r
441 IN UINT8 ReportType,\r
442 IN UINT16 ReportLen,\r
443 OUT UINT8 *Report\r
ce821dff 444 )\r
445{\r
446 UINT32 Status;\r
447 EFI_STATUS Result;\r
448 EFI_USB_DEVICE_REQUEST Request;\r
449\r
d5954c61 450 ASSERT (UsbIo != NULL);\r
451 ASSERT (Report != NULL);\r
452\r
ce821dff 453 //\r
454 // Fill Device request packet\r
455 //\r
11ceade4 456 Request.RequestType = USB_HID_CLASS_GET_REQ_TYPE;\r
2f88bd3a
MK
457 Request.Request = EFI_USB_GET_REPORT_REQUEST;\r
458 Request.Value = (UINT16)((ReportType << 8) | ReportId);\r
459 Request.Index = Interface;\r
460 Request.Length = ReportLen;\r
ce821dff 461\r
462 Result = UsbIo->UsbControlTransfer (\r
463 UsbIo,\r
464 &Request,\r
465 EfiUsbDataIn,\r
65442978 466 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 467 Report,\r
468 ReportLen,\r
469 &Status\r
470 );\r
471\r
472 return Result;\r
473}\r