]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbBusPei/PeiUsbLib.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusPei / PeiUsbLib.c
CommitLineData
4b1bf81c 1/** @file\r
2Common Libarary for PEI USB\r
3\r
d1102dba
LG
4Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved. <BR>\r
5\r
4b1bf81c 6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions\r
8of the BSD License which accompanies this distribution. The\r
9full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
17#include "UsbPeim.h"\r
18#include "PeiUsbLib.h"\r
19\r
20/**\r
21 Get a given usb descriptor.\r
22\r
23 @param PeiServices General-purpose services that are available to every PEIM.\r
24 @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance.\r
25 @param Value Request Value.\r
26 @param Index Request Index.\r
27 @param DescriptorLength Request descriptor Length.\r
28 @param Descriptor Request descriptor.\r
29\r
30\r
31 @retval EFI_SUCCESS Usb descriptor is obtained successfully.\r
32 @retval EFI_DEVICE_ERROR Cannot get the usb descriptor due to a hardware error.\r
33 @retval Others Other failure occurs.\r
34\r
35**/\r
36EFI_STATUS\r
37PeiUsbGetDescriptor (\r
38 IN EFI_PEI_SERVICES **PeiServices,\r
39 IN PEI_USB_IO_PPI *UsbIoPpi,\r
40 IN UINT16 Value,\r
41 IN UINT16 Index,\r
42 IN UINT16 DescriptorLength,\r
43 OUT VOID *Descriptor\r
44 )\r
45{\r
46 EFI_USB_DEVICE_REQUEST DevReq;\r
47\r
48 ASSERT (UsbIoPpi != NULL);\r
49\r
50 DevReq.RequestType = USB_DEV_GET_DESCRIPTOR_REQ_TYPE;\r
51 DevReq.Request = USB_DEV_GET_DESCRIPTOR;\r
52 DevReq.Value = Value;\r
53 DevReq.Index = Index;\r
54 DevReq.Length = DescriptorLength;\r
55\r
56 return UsbIoPpi->UsbControlTransfer (\r
57 PeiServices,\r
58 UsbIoPpi,\r
59 &DevReq,\r
60 EfiUsbDataIn,\r
61 PcdGet32 (PcdUsbTransferTimeoutValue),\r
62 Descriptor,\r
63 DescriptorLength\r
64 );\r
65}\r
66\r
67/**\r
68 Set a usb device with a specified address.\r
69\r
70 @param PeiServices General-purpose services that are available to every PEIM.\r
71 @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance.\r
72 @param AddressValue The address to assign.\r
73\r
74 @retval EFI_SUCCESS Usb device address is set successfully.\r
75 @retval EFI_DEVICE_ERROR Cannot set the usb address due to a hardware error.\r
76 @retval Others Other failure occurs.\r
77\r
78**/\r
79EFI_STATUS\r
80PeiUsbSetDeviceAddress (\r
81 IN EFI_PEI_SERVICES **PeiServices,\r
82 IN PEI_USB_IO_PPI *UsbIoPpi,\r
83 IN UINT16 AddressValue\r
84 )\r
85{\r
86 EFI_USB_DEVICE_REQUEST DevReq;\r
87\r
88 ASSERT (UsbIoPpi != NULL);\r
89\r
90 DevReq.RequestType = USB_DEV_SET_ADDRESS_REQ_TYPE;\r
91 DevReq.Request = USB_DEV_SET_ADDRESS;\r
92 DevReq.Value = AddressValue;\r
93 DevReq.Index = 0;\r
94 DevReq.Length = 0;\r
95\r
96 return UsbIoPpi->UsbControlTransfer (\r
97 PeiServices,\r
98 UsbIoPpi,\r
99 &DevReq,\r
100 EfiUsbNoData,\r
101 PcdGet32 (PcdUsbTransferTimeoutValue),\r
102 NULL,\r
103 0\r
104 );\r
105}\r
106\r
107/**\r
108 Clear a given usb feature.\r
109\r
110 @param PeiServices General-purpose services that are available to every PEIM.\r
111 @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance.\r
112 @param Recipient The recipient of ClearFeature Request, should be one of Device/Interface/Endpoint.\r
113 @param Value Request Value.\r
114 @param Target Request Index.\r
115\r
116 @retval EFI_SUCCESS Usb feature is cleared successfully.\r
117 @retval EFI_DEVICE_ERROR Cannot clear the usb feature due to a hardware error.\r
118 @retval Others Other failure occurs.\r
119\r
120**/\r
121EFI_STATUS\r
122PeiUsbClearDeviceFeature (\r
123 IN EFI_PEI_SERVICES **PeiServices,\r
124 IN PEI_USB_IO_PPI *UsbIoPpi,\r
125 IN EFI_USB_RECIPIENT Recipient,\r
126 IN UINT16 Value,\r
127 IN UINT16 Target\r
128 )\r
129{\r
130 EFI_USB_DEVICE_REQUEST DevReq;\r
131\r
132 ASSERT (UsbIoPpi != NULL);\r
133\r
134 switch (Recipient) {\r
135 case EfiUsbDevice:\r
136 DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_D;\r
137 break;\r
138\r
139 case EfiUsbInterface:\r
140 DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_I;\r
141 break;\r
142\r
143 case EfiUsbEndpoint:\r
144 DevReq.RequestType = USB_DEV_CLEAR_FEATURE_REQ_TYPE_E;\r
145 break;\r
146 }\r
147\r
148 DevReq.Request = USB_DEV_CLEAR_FEATURE;\r
149 DevReq.Value = Value;\r
150 DevReq.Index = Target;\r
151 DevReq.Length = 0;\r
152\r
153 return UsbIoPpi->UsbControlTransfer (\r
154 PeiServices,\r
155 UsbIoPpi,\r
156 &DevReq,\r
157 EfiUsbNoData,\r
158 PcdGet32 (PcdUsbTransferTimeoutValue),\r
159 NULL,\r
160 0\r
161 );\r
162}\r
163\r
164/**\r
165 Configure a usb device to Configuration 1.\r
166\r
167 @param PeiServices General-purpose services that are available to every PEIM.\r
168 @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance.\r
169\r
170 @retval EFI_SUCCESS Usb device is set to use Configuration 1 successfully.\r
171 @retval EFI_DEVICE_ERROR Cannot set the usb device due to a hardware error.\r
172 @retval Others Other failure occurs.\r
173\r
174**/\r
175EFI_STATUS\r
176PeiUsbSetConfiguration (\r
177 IN EFI_PEI_SERVICES **PeiServices,\r
178 IN PEI_USB_IO_PPI *UsbIoPpi\r
179 )\r
180{\r
181 EFI_USB_DEVICE_REQUEST DevReq;\r
182 ZeroMem (&DevReq, sizeof (EFI_USB_DEVICE_REQUEST));\r
183\r
184 DevReq.RequestType = USB_DEV_SET_CONFIGURATION_REQ_TYPE;\r
185 DevReq.Request = USB_DEV_SET_CONFIGURATION;\r
186 DevReq.Value = 1;\r
187\r
188 return UsbIoPpi->UsbControlTransfer (\r
189 PeiServices,\r
190 UsbIoPpi,\r
191 &DevReq,\r
192 EfiUsbNoData,\r
193 PcdGet32 (PcdUsbTransferTimeoutValue),\r
194 NULL,\r
195 0\r
196 );\r
197}\r
198\r
4b1bf81c 199/**\r
200 Judge if the port is connected with a usb device or not.\r
201\r
202 @param PortStatus The usb port status gotten.\r
203\r
204 @retval TRUE A usb device is connected with the port.\r
205 @retval FALSE No usb device is connected with the port.\r
206\r
207**/\r
208BOOLEAN\r
209IsPortConnect (\r
210 IN UINT16 PortStatus\r
211 )\r
212{\r
213 //\r
214 // return the bit 0 value of PortStatus\r
215 //\r
216 if ((PortStatus & USB_PORT_STAT_CONNECTION) != 0) {\r
217 return TRUE;\r
218 } else {\r
219 return FALSE;\r
220 }\r
221}\r
222\r
223/**\r
d987459f 224 Get device speed according to port status.\r
4b1bf81c 225\r
d987459f 226 @param PortStatus The usb port status gotten.\r
4b1bf81c 227\r
d987459f 228 @return Device speed value.\r
4b1bf81c 229\r
230**/\r
231UINTN\r
d987459f
SZ
232PeiUsbGetDeviceSpeed (\r
233 IN UINT16 PortStatus\r
4b1bf81c 234 )\r
235{\r
4b1bf81c 236 if ((PortStatus & USB_PORT_STAT_LOW_SPEED) != 0) {\r
237 return EFI_USB_SPEED_LOW;\r
238 } else if ((PortStatus & USB_PORT_STAT_HIGH_SPEED) != 0){\r
239 return EFI_USB_SPEED_HIGH;\r
d987459f
SZ
240 } else if ((PortStatus & USB_PORT_STAT_SUPER_SPEED) != 0) {\r
241 return EFI_USB_SPEED_SUPER;\r
4b1bf81c 242 } else {\r
243 return EFI_USB_SPEED_FULL;\r
244 }\r
245}\r
246\r
247/**\r
248 Judge if the port is in "connection change" status or not.\r
249\r
250 @param PortChangeStatus The usb port change status gotten.\r
251\r
252 @retval TRUE The port is in "connection change" status.\r
253 @retval FALSE The port is NOT in "connection change" status.\r
254\r
255**/\r
256BOOLEAN\r
257IsPortConnectChange (\r
258 IN UINT16 PortChangeStatus\r
259 )\r
260{\r
261 //\r
262 // return the bit 0 value of PortChangeStatus\r
263 //\r
264 if ((PortChangeStatus & USB_PORT_STAT_C_CONNECTION) != 0) {\r
265 return TRUE;\r
266 } else {\r
267 return FALSE;\r
268 }\r
269}\r