]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbBusPei/PeiUsbLib.c
ShellPkg/ShellProtocol.c: Don't put consective "\"s in file paths
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusPei / PeiUsbLib.c
CommitLineData
4b1bf81c 1/** @file\r
2Common Libarary for PEI USB\r
3\r
4Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved. <BR>\r
5 \r
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
199/**\r
200 Clear Endpoint Halt.\r
201\r
202 @param PeiServices General-purpose services that are available to every PEIM.\r
203 @param UsbIoPpi Indicates the PEI_USB_IO_PPI instance.\r
204 @param EndpointAddress The endpoint address.\r
205\r
206 @retval EFI_SUCCESS Endpoint halt is cleared successfully.\r
207 @retval EFI_DEVICE_ERROR Cannot clear the endpoint halt status due to a hardware error.\r
208 @retval Others Other failure occurs.\r
209\r
210**/\r
211EFI_STATUS\r
212PeiUsbClearEndpointHalt (\r
213 IN EFI_PEI_SERVICES **PeiServices,\r
214 IN PEI_USB_IO_PPI *UsbIoPpi,\r
215 IN UINT8 EndpointAddress\r
216 )\r
217{\r
218 EFI_STATUS Status;\r
219 PEI_USB_DEVICE *PeiUsbDev;\r
220 EFI_USB_ENDPOINT_DESCRIPTOR *EndpointDescriptor;\r
221 UINT8 EndpointIndex;\r
222\r
223 EndpointIndex = 0;\r
224 PeiUsbDev = PEI_USB_DEVICE_FROM_THIS (UsbIoPpi);\r
225\r
226 while (EndpointIndex < MAX_ENDPOINT) {\r
227 Status = UsbIoPpi->UsbGetEndpointDescriptor (PeiServices, UsbIoPpi, EndpointIndex, &EndpointDescriptor);\r
228 if (EFI_ERROR (Status)) {\r
229 return EFI_INVALID_PARAMETER;\r
230 }\r
231\r
232 if (EndpointDescriptor->EndpointAddress == EndpointAddress) {\r
233 break;\r
234 }\r
235\r
236 EndpointIndex++;\r
237 }\r
238\r
239 if (EndpointIndex == MAX_ENDPOINT) {\r
240 return EFI_INVALID_PARAMETER;\r
241 }\r
242\r
243 Status = PeiUsbClearDeviceFeature (\r
244 PeiServices,\r
245 UsbIoPpi,\r
246 EfiUsbEndpoint,\r
247 EfiUsbEndpointHalt,\r
248 EndpointAddress\r
249 );\r
250\r
251 //\r
252 // set data toggle to zero.\r
253 //\r
254 if ((PeiUsbDev->DataToggle & (1 << EndpointIndex)) != 0) {\r
255 PeiUsbDev->DataToggle = (UINT8) (PeiUsbDev->DataToggle ^ (1 << EndpointIndex));\r
256 }\r
257\r
258 return Status;\r
259}\r
260\r
261/**\r
262 Judge if the port is connected with a usb device or not.\r
263\r
264 @param PortStatus The usb port status gotten.\r
265\r
266 @retval TRUE A usb device is connected with the port.\r
267 @retval FALSE No usb device is connected with the port.\r
268\r
269**/\r
270BOOLEAN\r
271IsPortConnect (\r
272 IN UINT16 PortStatus\r
273 )\r
274{\r
275 //\r
276 // return the bit 0 value of PortStatus\r
277 //\r
278 if ((PortStatus & USB_PORT_STAT_CONNECTION) != 0) {\r
279 return TRUE;\r
280 } else {\r
281 return FALSE;\r
282 }\r
283}\r
284\r
285/**\r
286 Judge if the port is connected with a low-speed usb device or not.\r
287\r
288 @param PortStatus The usb port status gotten.\r
289\r
290 @retval TRUE A low-speed usb device is connected with the port.\r
291 @retval FALSE No low-speed usb device is connected with the port.\r
292\r
293**/\r
294UINTN\r
295IsPortLowSpeedDeviceAttached (\r
296 IN UINT16 PortStatus\r
297 )\r
298{\r
299 //\r
300 // return the bit 9 value of PortStatus\r
301 //\r
302 if ((PortStatus & USB_PORT_STAT_LOW_SPEED) != 0) {\r
303 return EFI_USB_SPEED_LOW;\r
304 } else if ((PortStatus & USB_PORT_STAT_HIGH_SPEED) != 0){\r
305 return EFI_USB_SPEED_HIGH;\r
306 } else {\r
307 return EFI_USB_SPEED_FULL;\r
308 }\r
309}\r
310\r
311/**\r
312 Judge if the port is in "connection change" status or not.\r
313\r
314 @param PortChangeStatus The usb port change status gotten.\r
315\r
316 @retval TRUE The port is in "connection change" status.\r
317 @retval FALSE The port is NOT in "connection change" status.\r
318\r
319**/\r
320BOOLEAN\r
321IsPortConnectChange (\r
322 IN UINT16 PortChangeStatus\r
323 )\r
324{\r
325 //\r
326 // return the bit 0 value of PortChangeStatus\r
327 //\r
328 if ((PortChangeStatus & USB_PORT_STAT_C_CONNECTION) != 0) {\r
329 return TRUE;\r
330 } else {\r
331 return FALSE;\r
332 }\r
333}\r