2 The module is used to implement Usb Io PPI interfaces.
4 Copyright (c) 2006 - 2010, 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.
28 @param Data Data buffer to be transmitted or received from USB device.
29 @param DataLength The size (in bytes) of the data buffer.
31 @retval EFI_SUCCESS Transfer was completed successfully.
32 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
33 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
34 @retval EFI_TIMEOUT Transfer failed due to timeout.
35 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
40 PeiUsbControlTransfer (
41 IN EFI_PEI_SERVICES
**PeiServices
,
42 IN PEI_USB_IO_PPI
*This
,
43 IN EFI_USB_DEVICE_REQUEST
*Request
,
44 IN EFI_USB_DATA_DIRECTION Direction
,
46 IN OUT VOID
*Data
, OPTIONAL
47 IN UINTN DataLength OPTIONAL
51 PEI_USB_DEVICE
*PeiUsbDev
;
52 UINT32 TransferResult
;
54 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
56 if (PeiUsbDev
->Usb2HcPpi
!= NULL
) {
57 Status
= PeiUsbDev
->Usb2HcPpi
->ControlTransfer (
60 PeiUsbDev
->DeviceAddress
,
61 PeiUsbDev
->DeviceSpeed
,
62 PeiUsbDev
->MaxPacketSize0
,
68 &(PeiUsbDev
->Translator
),
72 Status
= PeiUsbDev
->UsbHcPpi
->ControlTransfer (
75 PeiUsbDev
->DeviceAddress
,
76 PeiUsbDev
->DeviceSpeed
,
77 PeiUsbDev
->MaxPacketSize0
,
90 Submits bulk transfer to a bulk endpoint of a USB device.
92 @param PeiServices The pointer of EFI_PEI_SERVICES.
93 @param This The pointer of PEI_USB_IO_PPI.
94 @param DeviceEndpoint Endpoint number and its direction in bit 7.
95 @param Data A pointer to the buffer of data to transmit
97 @param DataLength The lenght of the data buffer.
98 @param Timeout Indicates the maximum time, in millisecond, which the
99 transfer is allowed to complete.
101 @retval EFI_SUCCESS The transfer was completed successfully.
102 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
103 @retval EFI_INVALID_PARAMETER Parameters are invalid.
104 @retval EFI_TIMEOUT The transfer failed due to timeout.
105 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
111 IN EFI_PEI_SERVICES
**PeiServices
,
112 IN PEI_USB_IO_PPI
*This
,
113 IN UINT8 DeviceEndpoint
,
115 IN OUT UINTN
*DataLength
,
120 PEI_USB_DEVICE
*PeiUsbDev
;
121 UINT32 TransferResult
;
122 UINTN MaxPacketLength
;
125 EFI_USB_ENDPOINT_DESCRIPTOR
*EndpointDescriptor
;
127 VOID
*Data2
[EFI_USB_MAX_BULK_BUFFER_NUM
];
129 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
131 EndpointDescriptor
= NULL
;
136 while (EndpointIndex
< MAX_ENDPOINT
) {
137 Status
= PeiUsbGetEndpointDescriptor (PeiServices
, This
, EndpointIndex
, &EndpointDescriptor
);
138 if (EFI_ERROR (Status
)) {
139 return EFI_INVALID_PARAMETER
;
142 if (EndpointDescriptor
->EndpointAddress
== DeviceEndpoint
) {
149 if (EndpointIndex
== MAX_ENDPOINT
) {
150 return EFI_INVALID_PARAMETER
;
153 MaxPacketLength
= PeiUsbDev
->EndpointDesc
[EndpointIndex
]->MaxPacketSize
;
154 if ((PeiUsbDev
->DataToggle
& (1 << EndpointIndex
)) != 0) {
160 OldToggle
= DataToggle
;
162 if (PeiUsbDev
->Usb2HcPpi
!= NULL
) {
163 Status
= PeiUsbDev
->Usb2HcPpi
->BulkTransfer (
165 PeiUsbDev
->Usb2HcPpi
,
166 PeiUsbDev
->DeviceAddress
,
168 PeiUsbDev
->DeviceSpeed
,
174 &(PeiUsbDev
->Translator
),
178 Status
= PeiUsbDev
->UsbHcPpi
->BulkTransfer (
181 PeiUsbDev
->DeviceAddress
,
183 (UINT8
) MaxPacketLength
,
192 if (OldToggle
!= DataToggle
) {
193 PeiUsbDev
->DataToggle
= (UINT8
) (PeiUsbDev
->DataToggle
^ (1 << EndpointIndex
));
200 Get the usb interface descriptor.
202 @param PeiServices General-purpose services that are available to every PEIM.
203 @param This Indicates the PEI_USB_IO_PPI instance.
204 @param InterfaceDescriptor Request interface descriptor.
207 @retval EFI_SUCCESS Usb interface descriptor is obtained successfully.
212 PeiUsbGetInterfaceDescriptor (
213 IN EFI_PEI_SERVICES
**PeiServices
,
214 IN PEI_USB_IO_PPI
*This
,
215 OUT EFI_USB_INTERFACE_DESCRIPTOR
**InterfaceDescriptor
218 PEI_USB_DEVICE
*PeiUsbDev
;
219 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
220 *InterfaceDescriptor
= PeiUsbDev
->InterfaceDesc
;
225 Get the usb endpoint descriptor.
227 @param PeiServices General-purpose services that are available to every PEIM.
228 @param This Indicates the PEI_USB_IO_PPI instance.
229 @param EndpointIndex The valid index of the specified endpoint.
230 @param EndpointDescriptor Request endpoint descriptor.
232 @retval EFI_SUCCESS Usb endpoint descriptor is obtained successfully.
233 @retval EFI_NOT_FOUND Usb endpoint descriptor is NOT found.
238 PeiUsbGetEndpointDescriptor (
239 IN EFI_PEI_SERVICES
**PeiServices
,
240 IN PEI_USB_IO_PPI
*This
,
241 IN UINT8 EndpointIndex
,
242 OUT EFI_USB_ENDPOINT_DESCRIPTOR
**EndpointDescriptor
245 PEI_USB_DEVICE
*PeiUsbDev
;
247 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
249 ASSERT (EndpointDescriptor
!= NULL
);
252 // The valid range of EndpointIndex is 0..15
253 // If EndpointIndex is lesser than 15 but larger than the number of interfaces,
254 // a EFI_NOT_FOUND should be returned
256 ASSERT (EndpointIndex
<= 15);
258 if (EndpointIndex
>= PeiUsbDev
->InterfaceDesc
->NumEndpoints
) {
259 return EFI_NOT_FOUND
;
262 *EndpointDescriptor
= PeiUsbDev
->EndpointDesc
[EndpointIndex
];
268 Reset the port and re-configure the usb device.
270 @param PeiServices General-purpose services that are available to every PEIM.
271 @param This Indicates the PEI_USB_IO_PPI instance.
273 @retval EFI_SUCCESS Usb device is reset and configured successfully.
274 @retval Others Other failure occurs.
280 IN EFI_PEI_SERVICES
**PeiServices
,
281 IN PEI_USB_IO_PPI
*This
284 PEI_USB_DEVICE
*PeiUsbDev
;
288 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
293 PeiUsbDev
->Usb2HcPpi
,
294 PeiUsbDev
->DeviceAddress
,
301 Address
= PeiUsbDev
->DeviceAddress
;
302 PeiUsbDev
->DeviceAddress
= 0;
304 Status
= PeiUsbSetDeviceAddress (
310 if (EFI_ERROR (Status
)) {
314 PeiUsbDev
->DeviceAddress
= Address
;
317 // Set default configuration
319 Status
= PeiUsbSetConfiguration (