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