]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbBusPei/PeiUsbLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusPei / PeiUsbLib.c
CommitLineData
4b1bf81c 1/** @file\r
13a623cf 2Common Library for PEI USB\r
4b1bf81c 3\r
d1102dba
LG
4Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved. <BR>\r
5\r
9d510e61 6SPDX-License-Identifier: BSD-2-Clause-Patent\r
4b1bf81c 7\r
8**/\r
9\r
10#include "UsbPeim.h"\r
11#include "PeiUsbLib.h"\r
12\r
13/**\r
14 Get a given usb descriptor.\r
15\r
16 @param PeiServices General-purpose services that are available to every PEIM.\r
17 @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance.\r
18 @param Value Request Value.\r
19 @param Index Request Index.\r
20 @param DescriptorLength Request descriptor Length.\r
21 @param Descriptor Request descriptor.\r
22\r
23\r
24 @retval EFI_SUCCESS Usb descriptor is obtained successfully.\r
25 @retval EFI_DEVICE_ERROR Cannot get the usb descriptor due to a hardware error.\r
26 @retval Others Other failure occurs.\r
27\r
28**/\r
29EFI_STATUS\r
30PeiUsbGetDescriptor (\r
1436aea4
MK
31 IN EFI_PEI_SERVICES **PeiServices,\r
32 IN PEI_USB_IO_PPI *UsbIoPpi,\r
33 IN UINT16 Value,\r
34 IN UINT16 Index,\r
35 IN UINT16 DescriptorLength,\r
36 OUT VOID *Descriptor\r
4b1bf81c 37 )\r
38{\r
39 EFI_USB_DEVICE_REQUEST DevReq;\r
40\r
41 ASSERT (UsbIoPpi != NULL);\r
42\r
1436aea4
MK
43 DevReq.RequestType = USB_DEV_GET_DESCRIPTOR_REQ_TYPE;\r
44 DevReq.Request = USB_DEV_GET_DESCRIPTOR;\r
45 DevReq.Value = Value;\r
46 DevReq.Index = Index;\r
47 DevReq.Length = DescriptorLength;\r
4b1bf81c 48\r
49 return UsbIoPpi->UsbControlTransfer (\r
50 PeiServices,\r
51 UsbIoPpi,\r
52 &DevReq,\r
53 EfiUsbDataIn,\r
54 PcdGet32 (PcdUsbTransferTimeoutValue),\r
55 Descriptor,\r
56 DescriptorLength\r
57 );\r
58}\r
59\r
60/**\r
61 Set a usb device with a specified address.\r
62\r
63 @param PeiServices General-purpose services that are available to every PEIM.\r
64 @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance.\r
65 @param AddressValue The address to assign.\r
66\r
67 @retval EFI_SUCCESS Usb device address is set successfully.\r
68 @retval EFI_DEVICE_ERROR Cannot set the usb address due to a hardware error.\r
69 @retval Others Other failure occurs.\r
70\r
71**/\r
72EFI_STATUS\r
73PeiUsbSetDeviceAddress (\r
1436aea4
MK
74 IN EFI_PEI_SERVICES **PeiServices,\r
75 IN PEI_USB_IO_PPI *UsbIoPpi,\r
76 IN UINT16 AddressValue\r
4b1bf81c 77 )\r
78{\r
79 EFI_USB_DEVICE_REQUEST DevReq;\r
80\r
81 ASSERT (UsbIoPpi != NULL);\r
82\r
1436aea4
MK
83 DevReq.RequestType = USB_DEV_SET_ADDRESS_REQ_TYPE;\r
84 DevReq.Request = USB_DEV_SET_ADDRESS;\r
85 DevReq.Value = AddressValue;\r
86 DevReq.Index = 0;\r
87 DevReq.Length = 0;\r
4b1bf81c 88\r
89 return UsbIoPpi->UsbControlTransfer (\r
90 PeiServices,\r
91 UsbIoPpi,\r
92 &DevReq,\r
93 EfiUsbNoData,\r
94 PcdGet32 (PcdUsbTransferTimeoutValue),\r
95 NULL,\r
96 0\r
97 );\r
98}\r
99\r
4b1bf81c 100/**\r
101 Configure a usb device to Configuration 1.\r
102\r
103 @param PeiServices General-purpose services that are available to every PEIM.\r
104 @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance.\r
105\r
106 @retval EFI_SUCCESS Usb device is set to use Configuration 1 successfully.\r
107 @retval EFI_DEVICE_ERROR Cannot set the usb device due to a hardware error.\r
108 @retval Others Other failure occurs.\r
109\r
110**/\r
111EFI_STATUS\r
112PeiUsbSetConfiguration (\r
1436aea4
MK
113 IN EFI_PEI_SERVICES **PeiServices,\r
114 IN PEI_USB_IO_PPI *UsbIoPpi\r
4b1bf81c 115 )\r
116{\r
117 EFI_USB_DEVICE_REQUEST DevReq;\r
1436aea4 118\r
4b1bf81c 119 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));\r
120\r
1436aea4
MK
121 DevReq.RequestType = USB_DEV_SET_CONFIGURATION_REQ_TYPE;\r
122 DevReq.Request = USB_DEV_SET_CONFIGURATION;\r
123 DevReq.Value = 1;\r
4b1bf81c 124\r
125 return UsbIoPpi->UsbControlTransfer (\r
126 PeiServices,\r
127 UsbIoPpi,\r
128 &DevReq,\r
129 EfiUsbNoData,\r
130 PcdGet32 (PcdUsbTransferTimeoutValue),\r
131 NULL,\r
132 0\r
133 );\r
134}\r
135\r
4b1bf81c 136/**\r
137 Judge if the port is connected with a usb device or not.\r
138\r
139 @param PortStatus The usb port status gotten.\r
140\r
141 @retval TRUE A usb device is connected with the port.\r
142 @retval FALSE No usb device is connected with the port.\r
143\r
144**/\r
145BOOLEAN\r
146IsPortConnect (\r
147 IN UINT16 PortStatus\r
148 )\r
149{\r
150 //\r
151 // return the bit 0 value of PortStatus\r
152 //\r
153 if ((PortStatus & USB_PORT_STAT_CONNECTION) != 0) {\r
154 return TRUE;\r
155 } else {\r
156 return FALSE;\r
157 }\r
158}\r
159\r
160/**\r
d987459f 161 Get device speed according to port status.\r
4b1bf81c 162\r
d987459f 163 @param PortStatus The usb port status gotten.\r
4b1bf81c 164\r
d987459f 165 @return Device speed value.\r
4b1bf81c 166\r
167**/\r
168UINTN\r
d987459f 169PeiUsbGetDeviceSpeed (\r
1436aea4 170 IN UINT16 PortStatus\r
4b1bf81c 171 )\r
172{\r
4b1bf81c 173 if ((PortStatus & USB_PORT_STAT_LOW_SPEED) != 0) {\r
174 return EFI_USB_SPEED_LOW;\r
1436aea4 175 } else if ((PortStatus & USB_PORT_STAT_HIGH_SPEED) != 0) {\r
4b1bf81c 176 return EFI_USB_SPEED_HIGH;\r
d987459f
SZ
177 } else if ((PortStatus & USB_PORT_STAT_SUPER_SPEED) != 0) {\r
178 return EFI_USB_SPEED_SUPER;\r
4b1bf81c 179 } else {\r
180 return EFI_USB_SPEED_FULL;\r
181 }\r
182}\r