]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiUsbLib/Hid.c
MdePkg/Library/Dxe: Fix various typos
[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
48 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
d5954c61 49 IN UINT8 Interface,\r
ce821dff 50 OUT EFI_USB_HID_DESCRIPTOR *HidDescriptor\r
51 )\r
52{\r
53 UINT32 Status;\r
54 EFI_STATUS Result;\r
55 EFI_USB_DEVICE_REQUEST Request;\r
d5954c61 56\r
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
62 Request.Value = (UINT16) (USB_DESC_TYPE_HID << 8);\r
d5954c61 63 Request.Index = Interface;\r
83c7c803 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
77\r
78}\r
ce821dff 79\r
80/**\r
d5954c61 81 Get the report descriptor of the specified USB HID interface.\r
ce821dff 82\r
d5954c61 83 Submit a USB get HID report descriptor request for the USB device specified by\r
84 UsbIo and Interface and return the report descriptor in DescriptorBuffer.\r
85 If UsbIo is NULL, then ASSERT().\r
86 If DescriptorBuffer is NULL, then ASSERT().\r
ce821dff 87\r
d5954c61 88 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
89 @param Interface The index of the report interface on the USB target.\r
90 @param DescriptorLength The size, in bytes, of DescriptorBuffer.\r
91 @param DescriptorBuffer A pointer to the buffer to store the report class descriptor.\r
92\r
93 @retval EFI_SUCCESS The request executed successfully.\r
94 @retval EFI_OUT_OF_RESOURCES The request could not be completed because the\r
070a76b1 95 buffer specified by DescriptorLength and DescriptorBuffer\r
d5954c61 96 is not large enough to hold the result of the request.\r
97 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
98 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 99\r
100**/\r
101EFI_STATUS\r
373b5cf9 102EFIAPI\r
ce821dff 103UsbGetReportDescriptor (\r
104 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
d5954c61 105 IN UINT8 Interface,\r
106 IN UINT16 DescriptorLength,\r
ce821dff 107 OUT UINT8 *DescriptorBuffer\r
108 )\r
109{\r
110 UINT32 Status;\r
111 EFI_STATUS Result;\r
112 EFI_USB_DEVICE_REQUEST Request;\r
113\r
d5954c61 114 ASSERT (UsbIo != NULL);\r
115 ASSERT (DescriptorBuffer != NULL);\r
116\r
ce821dff 117 //\r
118 // Fill Device request packet\r
119 //\r
11ceade4
LG
120 Request.RequestType = USB_HID_GET_DESCRIPTOR_REQ_TYPE;\r
121 Request.Request = USB_REQ_GET_DESCRIPTOR;\r
122 Request.Value = (UINT16) (USB_DESC_TYPE_REPORT << 8);\r
d5954c61 123 Request.Index = Interface;\r
124 Request.Length = DescriptorLength;\r
ce821dff 125\r
126 Result = UsbIo->UsbControlTransfer (\r
127 UsbIo,\r
128 &Request,\r
129 EfiUsbDataIn,\r
65442978 130 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 131 DescriptorBuffer,\r
d5954c61 132 DescriptorLength,\r
ce821dff 133 &Status\r
134 );\r
135\r
136 return Result;\r
137\r
138}\r
ce821dff 139\r
140/**\r
d5954c61 141 Get the HID protocol of the specified USB HID interface.\r
142\r
143 Submit a USB get HID protocol request for the USB device specified by UsbIo\r
144 and Interface and return the protocol retrieved in Protocol.\r
145 If UsbIo is NULL, then ASSERT().\r
146 If Protocol is NULL, then ASSERT().\r
ce821dff 147\r
d5954c61 148 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
149 @param Interface The index of the report interface on the USB target.\r
150 @param Protocol A pointer to the protocol for the specified USB target.\r
ce821dff 151\r
d5954c61 152 @retval EFI_SUCCESS The request executed successfully.\r
153 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
154 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 155\r
156**/\r
157EFI_STATUS\r
373b5cf9 158EFIAPI\r
ce821dff 159UsbGetProtocolRequest (\r
28d3e14f 160 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
161 IN UINT8 Interface,\r
4cb616da 162 OUT UINT8 *Protocol\r
ce821dff 163 )\r
164{\r
165 UINT32 Status;\r
166 EFI_STATUS Result;\r
167 EFI_USB_DEVICE_REQUEST Request;\r
168\r
d5954c61 169 ASSERT (UsbIo != NULL);\r
170 ASSERT (Protocol != NULL);\r
171\r
ce821dff 172 //\r
173 // Fill Device request packet\r
174 //\r
11ceade4 175 Request.RequestType = USB_HID_CLASS_GET_REQ_TYPE;\r
ce821dff 176 Request.Request = EFI_USB_GET_PROTOCOL_REQUEST;\r
177 Request.Value = 0;\r
178 Request.Index = Interface;\r
179 Request.Length = 1;\r
180\r
181 Result = UsbIo->UsbControlTransfer (\r
182 UsbIo,\r
183 &Request,\r
184 EfiUsbDataIn,\r
65442978 185 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 186 Protocol,\r
187 sizeof (UINT8),\r
188 &Status\r
189 );\r
190\r
191 return Result;\r
192}\r
193\r
194\r
373b5cf9 195\r
ce821dff 196/**\r
d5954c61 197 Set the HID protocol of the specified USB HID interface.\r
ce821dff 198\r
d5954c61 199 Submit a USB set HID protocol request for the USB device specified by UsbIo\r
200 and Interface and set the protocol to the value specified by Protocol.\r
201 If UsbIo is NULL, then ASSERT().\r
ce821dff 202\r
d5954c61 203 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
204 @param Interface The index of the report interface on the USB target.\r
205 @param Protocol The protocol value to set for the specified USB target.\r
206\r
207 @retval EFI_SUCCESS The request executed successfully.\r
208 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
209 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 210\r
211**/\r
212EFI_STATUS\r
373b5cf9 213EFIAPI\r
ce821dff 214UsbSetProtocolRequest (\r
215 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
216 IN UINT8 Interface,\r
217 IN UINT8 Protocol\r
218 )\r
219{\r
220 UINT32 Status;\r
221 EFI_STATUS Result;\r
222 EFI_USB_DEVICE_REQUEST Request;\r
223\r
d5954c61 224 ASSERT (UsbIo != NULL);\r
9095d37b 225\r
ce821dff 226 //\r
227 // Fill Device request packet\r
228 //\r
11ceade4 229 Request.RequestType = USB_HID_CLASS_SET_REQ_TYPE;\r
ce821dff 230 Request.Request = EFI_USB_SET_PROTOCOL_REQUEST;\r
231 Request.Value = Protocol;\r
232 Request.Index = Interface;\r
233 Request.Length = 0;\r
234\r
235 Result = UsbIo->UsbControlTransfer (\r
236 UsbIo,\r
237 &Request,\r
238 EfiUsbNoData,\r
65442978 239 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 240 NULL,\r
241 0,\r
242 &Status\r
243 );\r
244 return Result;\r
245}\r
246\r
247\r
ce821dff 248/**\r
d5954c61 249 Set the idle rate of the specified USB HID report.\r
ce821dff 250\r
d5954c61 251 Submit a USB set HID report idle request for the USB device specified by UsbIo,\r
252 Interface, and ReportId, and set the idle rate to the value specified by Duration.\r
253 If UsbIo is NULL, then ASSERT().\r
ce821dff 254\r
d5954c61 255 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
256 @param Interface The index of the report interface on the USB target.\r
257 @param ReportId The identifier of the report to retrieve.\r
258 @param Duration The idle rate to set for the specified USB target.\r
259\r
260 @retval EFI_SUCCESS The request executed successfully.\r
261 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
262 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 263\r
264**/\r
265EFI_STATUS\r
373b5cf9 266EFIAPI\r
ce821dff 267UsbSetIdleRequest (\r
268 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
269 IN UINT8 Interface,\r
270 IN UINT8 ReportId,\r
271 IN UINT8 Duration\r
272 )\r
273{\r
274 UINT32 Status;\r
275 EFI_STATUS Result;\r
276 EFI_USB_DEVICE_REQUEST Request;\r
277\r
d5954c61 278 ASSERT (UsbIo != NULL);\r
ce821dff 279 //\r
280 // Fill Device request packet\r
281 //\r
11ceade4 282 Request.RequestType = USB_HID_CLASS_SET_REQ_TYPE;\r
ce821dff 283 Request.Request = EFI_USB_SET_IDLE_REQUEST;\r
284 Request.Value = (UINT16) ((Duration << 8) | ReportId);\r
285 Request.Index = Interface;\r
286 Request.Length = 0;\r
287\r
288 Result = UsbIo->UsbControlTransfer (\r
289 UsbIo,\r
290 &Request,\r
291 EfiUsbNoData,\r
65442978 292 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 293 NULL,\r
294 0,\r
295 &Status\r
296 );\r
297 return Result;\r
298}\r
299\r
300\r
301/**\r
d5954c61 302 Get the idle rate of the specified USB HID report.\r
303\r
304 Submit a USB get HID report idle request for the USB device specified by UsbIo,\r
305 Interface, and ReportId, and return the ide rate in Duration.\r
306 If UsbIo is NULL, then ASSERT().\r
307 If Duration is NULL, then ASSERT().\r
ce821dff 308\r
d5954c61 309 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
310 @param Interface The index of the report interface on the USB target.\r
311 @param ReportId The identifier of the report to retrieve.\r
312 @param Duration A pointer to the idle rate retrieved from the specified USB target.\r
ce821dff 313\r
d5954c61 314 @retval EFI_SUCCESS The request executed successfully.\r
315 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
316 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 317\r
318**/\r
319EFI_STATUS\r
373b5cf9 320EFIAPI\r
ce821dff 321UsbGetIdleRequest (\r
322 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
323 IN UINT8 Interface,\r
324 IN UINT8 ReportId,\r
325 OUT UINT8 *Duration\r
326 )\r
327{\r
328 UINT32 Status;\r
329 EFI_STATUS Result;\r
330 EFI_USB_DEVICE_REQUEST Request;\r
9095d37b 331\r
d5954c61 332 ASSERT (UsbIo != NULL);\r
333 ASSERT (Duration != NULL);\r
ce821dff 334 //\r
335 // Fill Device request packet\r
336 //\r
11ceade4 337 Request.RequestType = USB_HID_CLASS_GET_REQ_TYPE;\r
ce821dff 338 Request.Request = EFI_USB_GET_IDLE_REQUEST;\r
339 Request.Value = ReportId;\r
340 Request.Index = Interface;\r
341 Request.Length = 1;\r
342\r
343 Result = UsbIo->UsbControlTransfer (\r
344 UsbIo,\r
345 &Request,\r
346 EfiUsbDataIn,\r
65442978 347 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 348 Duration,\r
349 1,\r
350 &Status\r
351 );\r
352\r
353 return Result;\r
354}\r
355\r
356\r
373b5cf9 357\r
ce821dff 358/**\r
d5954c61 359 Set the report descriptor of the specified USB HID interface.\r
360\r
361 Submit a USB set HID report request for the USB device specified by UsbIo,\r
362 Interface, ReportId, and ReportType, and set the report descriptor using the\r
363 buffer specified by ReportLength and Report.\r
364 If UsbIo is NULL, then ASSERT().\r
365 If Report is NULL, then ASSERT().\r
ce821dff 366\r
d5954c61 367 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
368 @param Interface The index of the report interface on the USB target.\r
369 @param ReportId The identifier of the report to retrieve.\r
370 @param ReportType The type of report to retrieve.\r
371 @param ReportLength The size, in bytes, of Report.\r
372 @param Report A pointer to the report descriptor buffer to set.\r
ce821dff 373\r
d5954c61 374 @retval EFI_SUCCESS The request executed successfully.\r
375 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
376 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 377\r
378**/\r
379EFI_STATUS\r
373b5cf9 380EFIAPI\r
ce821dff 381UsbSetReportRequest (\r
382 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
383 IN UINT8 Interface,\r
384 IN UINT8 ReportId,\r
385 IN UINT8 ReportType,\r
070a76b1 386 IN UINT16 ReportLen,\r
ce821dff 387 IN UINT8 *Report\r
388 )\r
389{\r
390 UINT32 Status;\r
391 EFI_STATUS Result;\r
392 EFI_USB_DEVICE_REQUEST Request;\r
393\r
d5954c61 394 ASSERT (UsbIo != NULL);\r
395 ASSERT (Report != NULL);\r
396\r
ce821dff 397 //\r
398 // Fill Device request packet\r
399 //\r
11ceade4 400 Request.RequestType = USB_HID_CLASS_SET_REQ_TYPE;\r
ce821dff 401 Request.Request = EFI_USB_SET_REPORT_REQUEST;\r
402 Request.Value = (UINT16) ((ReportType << 8) | ReportId);\r
403 Request.Index = Interface;\r
2281e7a9 404 Request.Length = ReportLen;\r
ce821dff 405\r
406 Result = UsbIo->UsbControlTransfer (\r
407 UsbIo,\r
408 &Request,\r
409 EfiUsbDataOut,\r
65442978 410 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 411 Report,\r
070a76b1 412 ReportLen,\r
ce821dff 413 &Status\r
414 );\r
415\r
416 return Result;\r
417}\r
418\r
419\r
420/**\r
d5954c61 421 Get the report descriptor of the specified USB HID interface.\r
422\r
423 Submit a USB get HID report request for the USB device specified by UsbIo,\r
424 Interface, ReportId, and ReportType, and return the report in the buffer\r
425 specified by Report.\r
426 If UsbIo is NULL, then ASSERT().\r
427 If Report is NULL, then ASSERT().\r
428\r
429 @param UsbIo A pointer to the USB I/O Protocol instance for the specific USB target.\r
430 @param Interface The index of the report interface on the USB target.\r
431 @param ReportId The identifier of the report to retrieve.\r
432 @param ReportType The type of report to retrieve.\r
433 @param ReportLength The size, in bytes, of Report.\r
434 @param Report A pointer to the buffer to store the report descriptor.\r
435\r
436 @retval EFI_SUCCESS The request executed successfully.\r
437 @retval EFI_OUT_OF_RESOURCES The request could not be completed because the\r
28d3e14f 438 buffer specified by ReportLength and Report is not\r
d5954c61 439 large enough to hold the result of the request.\r
440 @retval EFI_TIMEOUT A timeout occurred executing the request.\r
441 @retval EFI_DEVICE_ERROR The request failed due to a device error.\r
ce821dff 442\r
443**/\r
444EFI_STATUS\r
373b5cf9 445EFIAPI\r
ce821dff 446UsbGetReportRequest (\r
4cb616da 447 IN EFI_USB_IO_PROTOCOL *UsbIo,\r
448 IN UINT8 Interface,\r
449 IN UINT8 ReportId,\r
450 IN UINT8 ReportType,\r
451 IN UINT16 ReportLen,\r
452 OUT UINT8 *Report\r
ce821dff 453 )\r
454{\r
455 UINT32 Status;\r
456 EFI_STATUS Result;\r
457 EFI_USB_DEVICE_REQUEST Request;\r
458\r
d5954c61 459 ASSERT (UsbIo != NULL);\r
460 ASSERT (Report != NULL);\r
461\r
ce821dff 462 //\r
463 // Fill Device request packet\r
464 //\r
11ceade4 465 Request.RequestType = USB_HID_CLASS_GET_REQ_TYPE;\r
ce821dff 466 Request.Request = EFI_USB_GET_REPORT_REQUEST;\r
467 Request.Value = (UINT16) ((ReportType << 8) | ReportId);\r
468 Request.Index = Interface;\r
469 Request.Length = ReportLen;\r
470\r
471 Result = UsbIo->UsbControlTransfer (\r
472 UsbIo,\r
473 &Request,\r
474 EfiUsbDataIn,\r
65442978 475 PcdGet32 (PcdUsbTransferTimeoutValue),\r
ce821dff 476 Report,\r
477 ReportLen,\r
478 &Status\r
479 );\r
480\r
481 return Result;\r
482}\r