2 The module is used to implement Usb Io PPI interfaces.
4 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved. <BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions
8 of the BSD License which accompanies this distribution. The
9 full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "PeiUsbLib.h"
21 Submits control transfer to a target USB device.
23 @param PeiServices The pointer of EFI_PEI_SERVICES.
24 @param This The pointer of PEI_USB_IO_PPI.
25 @param Request USB device request to send.
26 @param Direction Specifies the data direction for the data stage.
27 @param Timeout Indicates the maximum timeout, in millisecond. If Timeout
28 is 0, then the caller must wait for the function to be
29 completed until EFI_SUCCESS or EFI_DEVICE_ERROR is returned.
30 @param Data Data buffer to be transmitted or received from USB device.
31 @param DataLength The size (in bytes) of the data buffer.
33 @retval EFI_SUCCESS Transfer was completed successfully.
34 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
35 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
36 @retval EFI_TIMEOUT Transfer failed due to timeout.
37 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
42 PeiUsbControlTransfer (
43 IN EFI_PEI_SERVICES
**PeiServices
,
44 IN PEI_USB_IO_PPI
*This
,
45 IN EFI_USB_DEVICE_REQUEST
*Request
,
46 IN EFI_USB_DATA_DIRECTION Direction
,
48 IN OUT VOID
*Data
, OPTIONAL
49 IN UINTN DataLength OPTIONAL
53 PEI_USB_DEVICE
*PeiUsbDev
;
54 UINT32 TransferResult
;
56 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
58 if (PeiUsbDev
->Usb2HcPpi
!= NULL
) {
59 Status
= PeiUsbDev
->Usb2HcPpi
->ControlTransfer (
62 PeiUsbDev
->DeviceAddress
,
63 PeiUsbDev
->DeviceSpeed
,
64 PeiUsbDev
->MaxPacketSize0
,
70 &(PeiUsbDev
->Translator
),
74 Status
= PeiUsbDev
->UsbHcPpi
->ControlTransfer (
77 PeiUsbDev
->DeviceAddress
,
78 PeiUsbDev
->DeviceSpeed
,
79 PeiUsbDev
->MaxPacketSize0
,
92 Submits bulk transfer to a bulk endpoint of a USB device.
94 @param PeiServices The pointer of EFI_PEI_SERVICES.
95 @param This The pointer of PEI_USB_IO_PPI.
96 @param DeviceEndpoint Endpoint number and its direction in bit 7.
97 @param Data A pointer to the buffer of data to transmit
99 @param DataLength The lenght of the data buffer.
100 @param Timeout Indicates the maximum time, in millisecond, which the
101 transfer is allowed to complete. If Timeout is 0, then
102 the caller must wait for the function to be completed
103 until EFI_SUCCESS or EFI_DEVICE_ERROR is returned.
105 @retval EFI_SUCCESS The transfer was completed successfully.
106 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
107 @retval EFI_INVALID_PARAMETER Parameters are invalid.
108 @retval EFI_TIMEOUT The transfer failed due to timeout.
109 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
115 IN EFI_PEI_SERVICES
**PeiServices
,
116 IN PEI_USB_IO_PPI
*This
,
117 IN UINT8 DeviceEndpoint
,
119 IN OUT UINTN
*DataLength
,
124 PEI_USB_DEVICE
*PeiUsbDev
;
125 UINT32 TransferResult
;
126 UINTN MaxPacketLength
;
129 EFI_USB_ENDPOINT_DESCRIPTOR
*EndpointDescriptor
;
131 VOID
*Data2
[EFI_USB_MAX_BULK_BUFFER_NUM
];
133 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
135 EndpointDescriptor
= NULL
;
140 while (EndpointIndex
< MAX_ENDPOINT
) {
141 Status
= PeiUsbGetEndpointDescriptor (PeiServices
, This
, EndpointIndex
, &EndpointDescriptor
);
142 if (EFI_ERROR (Status
)) {
143 return EFI_INVALID_PARAMETER
;
146 if (EndpointDescriptor
->EndpointAddress
== DeviceEndpoint
) {
153 if (EndpointIndex
== MAX_ENDPOINT
) {
154 return EFI_INVALID_PARAMETER
;
157 MaxPacketLength
= PeiUsbDev
->EndpointDesc
[EndpointIndex
]->MaxPacketSize
;
158 if ((PeiUsbDev
->DataToggle
& (1 << EndpointIndex
)) != 0) {
164 OldToggle
= DataToggle
;
166 if (PeiUsbDev
->Usb2HcPpi
!= NULL
) {
167 Status
= PeiUsbDev
->Usb2HcPpi
->BulkTransfer (
169 PeiUsbDev
->Usb2HcPpi
,
170 PeiUsbDev
->DeviceAddress
,
172 PeiUsbDev
->DeviceSpeed
,
178 &(PeiUsbDev
->Translator
),
182 Status
= PeiUsbDev
->UsbHcPpi
->BulkTransfer (
185 PeiUsbDev
->DeviceAddress
,
187 (UINT8
) MaxPacketLength
,
196 if (OldToggle
!= DataToggle
) {
197 PeiUsbDev
->DataToggle
= (UINT8
) (PeiUsbDev
->DataToggle
^ (1 << EndpointIndex
));
204 Get the usb interface descriptor.
206 @param PeiServices General-purpose services that are available to every PEIM.
207 @param This Indicates the PEI_USB_IO_PPI instance.
208 @param InterfaceDescriptor Request interface descriptor.
211 @retval EFI_SUCCESS Usb interface descriptor is obtained successfully.
216 PeiUsbGetInterfaceDescriptor (
217 IN EFI_PEI_SERVICES
**PeiServices
,
218 IN PEI_USB_IO_PPI
*This
,
219 OUT EFI_USB_INTERFACE_DESCRIPTOR
**InterfaceDescriptor
222 PEI_USB_DEVICE
*PeiUsbDev
;
223 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
224 *InterfaceDescriptor
= PeiUsbDev
->InterfaceDesc
;
229 Get the usb endpoint descriptor.
231 @param PeiServices General-purpose services that are available to every PEIM.
232 @param This Indicates the PEI_USB_IO_PPI instance.
233 @param EndpointIndex The valid index of the specified endpoint.
234 @param EndpointDescriptor Request endpoint descriptor.
236 @retval EFI_SUCCESS Usb endpoint descriptor is obtained successfully.
237 @retval EFI_NOT_FOUND Usb endpoint descriptor is NOT found.
242 PeiUsbGetEndpointDescriptor (
243 IN EFI_PEI_SERVICES
**PeiServices
,
244 IN PEI_USB_IO_PPI
*This
,
245 IN UINT8 EndpointIndex
,
246 OUT EFI_USB_ENDPOINT_DESCRIPTOR
**EndpointDescriptor
249 PEI_USB_DEVICE
*PeiUsbDev
;
251 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
253 ASSERT (EndpointDescriptor
!= NULL
);
256 // The valid range of EndpointIndex is 0..15
257 // If EndpointIndex is lesser than 15 but larger than the number of interfaces,
258 // a EFI_NOT_FOUND should be returned
260 ASSERT (EndpointIndex
<= 15);
262 if (EndpointIndex
>= PeiUsbDev
->InterfaceDesc
->NumEndpoints
) {
263 return EFI_NOT_FOUND
;
266 *EndpointDescriptor
= PeiUsbDev
->EndpointDesc
[EndpointIndex
];
272 Reset the port and re-configure the usb device.
274 @param PeiServices General-purpose services that are available to every PEIM.
275 @param This Indicates the PEI_USB_IO_PPI instance.
277 @retval EFI_SUCCESS Usb device is reset and configured successfully.
278 @retval Others Other failure occurs.
284 IN EFI_PEI_SERVICES
**PeiServices
,
285 IN PEI_USB_IO_PPI
*This
288 PEI_USB_DEVICE
*PeiUsbDev
;
292 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
297 PeiUsbDev
->Usb2HcPpi
,
298 PeiUsbDev
->DeviceAddress
,
305 Address
= PeiUsbDev
->DeviceAddress
;
306 PeiUsbDev
->DeviceAddress
= 0;
308 Status
= PeiUsbSetDeviceAddress (
314 if (EFI_ERROR (Status
)) {
318 PeiUsbDev
->DeviceAddress
= Address
;
321 // Set default configuration
323 Status
= PeiUsbSetConfiguration (