2 The module is used to implement Usb Io PPI interfaces.
4 Copyright (c) 2006 - 2014, 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
;
55 EFI_USB_ENDPOINT_DESCRIPTOR
*EndpointDescriptor
;
58 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
60 EndpointDescriptor
= NULL
;
63 if ((Request
->Request
== USB_REQ_CLEAR_FEATURE
) &&
64 (Request
->RequestType
== USB_DEV_CLEAR_FEATURE_REQ_TYPE_E
) &&
65 (Request
->Value
== USB_FEATURE_ENDPOINT_HALT
)) {
67 // Request->Index is the Endpoint Address, use it to get the Endpoint Index.
69 while (EndpointIndex
< MAX_ENDPOINT
) {
70 Status
= PeiUsbGetEndpointDescriptor (PeiServices
, This
, EndpointIndex
, &EndpointDescriptor
);
71 if (EFI_ERROR (Status
)) {
72 return EFI_INVALID_PARAMETER
;
75 if (EndpointDescriptor
->EndpointAddress
== Request
->Index
) {
82 if (EndpointIndex
== MAX_ENDPOINT
) {
83 return EFI_INVALID_PARAMETER
;
87 if (PeiUsbDev
->Usb2HcPpi
!= NULL
) {
88 Status
= PeiUsbDev
->Usb2HcPpi
->ControlTransfer (
91 PeiUsbDev
->DeviceAddress
,
92 PeiUsbDev
->DeviceSpeed
,
93 PeiUsbDev
->MaxPacketSize0
,
99 &(PeiUsbDev
->Translator
),
103 Status
= PeiUsbDev
->UsbHcPpi
->ControlTransfer (
106 PeiUsbDev
->DeviceAddress
,
107 PeiUsbDev
->DeviceSpeed
,
108 PeiUsbDev
->MaxPacketSize0
,
119 // Reset the endpoint toggle when endpoint stall is cleared
121 if ((Request
->Request
== USB_REQ_CLEAR_FEATURE
) &&
122 (Request
->RequestType
== USB_DEV_CLEAR_FEATURE_REQ_TYPE_E
) &&
123 (Request
->Value
== USB_FEATURE_ENDPOINT_HALT
)) {
124 if ((PeiUsbDev
->DataToggle
& (1 << EndpointIndex
)) != 0) {
125 PeiUsbDev
->DataToggle
= (UINT16
) (PeiUsbDev
->DataToggle
^ (1 << EndpointIndex
));
133 Submits bulk transfer to a bulk endpoint of a USB device.
135 @param PeiServices The pointer of EFI_PEI_SERVICES.
136 @param This The pointer of PEI_USB_IO_PPI.
137 @param DeviceEndpoint Endpoint number and its direction in bit 7.
138 @param Data A pointer to the buffer of data to transmit
139 from or receive into.
140 @param DataLength The lenght of the data buffer.
141 @param Timeout Indicates the maximum time, in millisecond, which the
142 transfer is allowed to complete. If Timeout is 0, then
143 the caller must wait for the function to be completed
144 until EFI_SUCCESS or EFI_DEVICE_ERROR is returned.
146 @retval EFI_SUCCESS The transfer was completed successfully.
147 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
148 @retval EFI_INVALID_PARAMETER Parameters are invalid.
149 @retval EFI_TIMEOUT The transfer failed due to timeout.
150 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
156 IN EFI_PEI_SERVICES
**PeiServices
,
157 IN PEI_USB_IO_PPI
*This
,
158 IN UINT8 DeviceEndpoint
,
160 IN OUT UINTN
*DataLength
,
165 PEI_USB_DEVICE
*PeiUsbDev
;
166 UINT32 TransferResult
;
167 UINTN MaxPacketLength
;
170 EFI_USB_ENDPOINT_DESCRIPTOR
*EndpointDescriptor
;
172 VOID
*Data2
[EFI_USB_MAX_BULK_BUFFER_NUM
];
174 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
176 EndpointDescriptor
= NULL
;
181 while (EndpointIndex
< MAX_ENDPOINT
) {
182 Status
= PeiUsbGetEndpointDescriptor (PeiServices
, This
, EndpointIndex
, &EndpointDescriptor
);
183 if (EFI_ERROR (Status
)) {
184 return EFI_INVALID_PARAMETER
;
187 if (EndpointDescriptor
->EndpointAddress
== DeviceEndpoint
) {
194 if (EndpointIndex
== MAX_ENDPOINT
) {
195 return EFI_INVALID_PARAMETER
;
198 MaxPacketLength
= PeiUsbDev
->EndpointDesc
[EndpointIndex
]->MaxPacketSize
;
199 if ((PeiUsbDev
->DataToggle
& (1 << EndpointIndex
)) != 0) {
205 OldToggle
= DataToggle
;
207 if (PeiUsbDev
->Usb2HcPpi
!= NULL
) {
208 Status
= PeiUsbDev
->Usb2HcPpi
->BulkTransfer (
210 PeiUsbDev
->Usb2HcPpi
,
211 PeiUsbDev
->DeviceAddress
,
213 PeiUsbDev
->DeviceSpeed
,
219 &(PeiUsbDev
->Translator
),
223 Status
= PeiUsbDev
->UsbHcPpi
->BulkTransfer (
226 PeiUsbDev
->DeviceAddress
,
228 (UINT8
) MaxPacketLength
,
237 if (OldToggle
!= DataToggle
) {
238 PeiUsbDev
->DataToggle
= (UINT16
) (PeiUsbDev
->DataToggle
^ (1 << EndpointIndex
));
245 Get the usb interface descriptor.
247 @param PeiServices General-purpose services that are available to every PEIM.
248 @param This Indicates the PEI_USB_IO_PPI instance.
249 @param InterfaceDescriptor Request interface descriptor.
252 @retval EFI_SUCCESS Usb interface descriptor is obtained successfully.
257 PeiUsbGetInterfaceDescriptor (
258 IN EFI_PEI_SERVICES
**PeiServices
,
259 IN PEI_USB_IO_PPI
*This
,
260 OUT EFI_USB_INTERFACE_DESCRIPTOR
**InterfaceDescriptor
263 PEI_USB_DEVICE
*PeiUsbDev
;
264 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
265 *InterfaceDescriptor
= PeiUsbDev
->InterfaceDesc
;
270 Get the usb endpoint descriptor.
272 @param PeiServices General-purpose services that are available to every PEIM.
273 @param This Indicates the PEI_USB_IO_PPI instance.
274 @param EndpointIndex The valid index of the specified endpoint.
275 @param EndpointDescriptor Request endpoint descriptor.
277 @retval EFI_SUCCESS Usb endpoint descriptor is obtained successfully.
278 @retval EFI_NOT_FOUND Usb endpoint descriptor is NOT found.
283 PeiUsbGetEndpointDescriptor (
284 IN EFI_PEI_SERVICES
**PeiServices
,
285 IN PEI_USB_IO_PPI
*This
,
286 IN UINT8 EndpointIndex
,
287 OUT EFI_USB_ENDPOINT_DESCRIPTOR
**EndpointDescriptor
290 PEI_USB_DEVICE
*PeiUsbDev
;
292 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
294 ASSERT (EndpointDescriptor
!= NULL
);
297 // The valid range of EndpointIndex is 0..15
298 // If EndpointIndex is lesser than 15 but larger than the number of interfaces,
299 // a EFI_NOT_FOUND should be returned
301 ASSERT (EndpointIndex
<= 15);
303 if (EndpointIndex
>= PeiUsbDev
->InterfaceDesc
->NumEndpoints
) {
304 return EFI_NOT_FOUND
;
307 *EndpointDescriptor
= PeiUsbDev
->EndpointDesc
[EndpointIndex
];
313 Reset the port and re-configure the usb device.
315 @param PeiServices General-purpose services that are available to every PEIM.
316 @param This Indicates the PEI_USB_IO_PPI instance.
318 @retval EFI_SUCCESS Usb device is reset and configured successfully.
319 @retval Others Other failure occurs.
325 IN EFI_PEI_SERVICES
**PeiServices
,
326 IN PEI_USB_IO_PPI
*This
329 PEI_USB_DEVICE
*PeiUsbDev
;
333 PeiUsbDev
= PEI_USB_DEVICE_FROM_THIS (This
);
338 PeiUsbDev
->Usb2HcPpi
,
339 PeiUsbDev
->DeviceAddress
,
346 Address
= PeiUsbDev
->DeviceAddress
;
347 PeiUsbDev
->DeviceAddress
= 0;
349 Status
= PeiUsbSetDeviceAddress (
355 if (EFI_ERROR (Status
)) {
359 PeiUsbDev
->DeviceAddress
= Address
;
362 // Set default configuration
364 Status
= PeiUsbSetConfiguration (