]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Usb/UsbBusPei/PeiUsbLib.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusPei / PeiUsbLib.c
1 /** @file
2 Common Libarary 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
102 /**
103 Configure a usb device to Configuration 1.
104
105 @param PeiServices General-purpose services that are available to every PEIM.
106 @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance.
107
108 @retval EFI_SUCCESS Usb device is set to use Configuration 1 successfully.
109 @retval EFI_DEVICE_ERROR Cannot set the usb device due to a hardware error.
110 @retval Others Other failure occurs.
111
112 **/
113 EFI_STATUS
114 PeiUsbSetConfiguration (
115 IN EFI_PEI_SERVICES **PeiServices,
116 IN PEI_USB_IO_PPI *UsbIoPpi
117 )
118 {
119 EFI_USB_DEVICE_REQUEST DevReq;
120 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));
121
122 DevReq.RequestType = USB_DEV_SET_CONFIGURATION_REQ_TYPE;
123 DevReq.Request = USB_DEV_SET_CONFIGURATION;
124 DevReq.Value = 1;
125
126 return UsbIoPpi->UsbControlTransfer (
127 PeiServices,
128 UsbIoPpi,
129 &DevReq,
130 EfiUsbNoData,
131 PcdGet32 (PcdUsbTransferTimeoutValue),
132 NULL,
133 0
134 );
135 }
136
137 /**
138 Judge if the port is connected with a usb device or not.
139
140 @param PortStatus The usb port status gotten.
141
142 @retval TRUE A usb device is connected with the port.
143 @retval FALSE No usb device is connected with the port.
144
145 **/
146 BOOLEAN
147 IsPortConnect (
148 IN UINT16 PortStatus
149 )
150 {
151 //
152 // return the bit 0 value of PortStatus
153 //
154 if ((PortStatus & USB_PORT_STAT_CONNECTION) != 0) {
155 return TRUE;
156 } else {
157 return FALSE;
158 }
159 }
160
161 /**
162 Get device speed according to port status.
163
164 @param PortStatus The usb port status gotten.
165
166 @return Device speed value.
167
168 **/
169 UINTN
170 PeiUsbGetDeviceSpeed (
171 IN UINT16 PortStatus
172 )
173 {
174 if ((PortStatus & USB_PORT_STAT_LOW_SPEED) != 0) {
175 return EFI_USB_SPEED_LOW;
176 } else if ((PortStatus & USB_PORT_STAT_HIGH_SPEED) != 0){
177 return EFI_USB_SPEED_HIGH;
178 } else if ((PortStatus & USB_PORT_STAT_SUPER_SPEED) != 0) {
179 return EFI_USB_SPEED_SUPER;
180 } else {
181 return EFI_USB_SPEED_FULL;
182 }
183 }
184
185