4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved. <BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #ifndef _PEI_USB_PEIM_H_
11 #define _PEI_USB_PEIM_H_
15 #include <Ppi/UsbHostController.h>
16 #include <Ppi/Usb2HostController.h>
17 #include <Ppi/UsbIo.h>
19 #include <Library/DebugLib.h>
20 #include <Library/PeimEntryPoint.h>
21 #include <Library/PeiServicesLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/TimerLib.h>
24 #include <Library/PcdLib.h>
26 #include <IndustryStandard/Usb.h>
29 // A common header for usb standard descriptor.
30 // Each stand descriptor has a length and type.
39 #define MAX_INTERFACE 8
40 #define MAX_ENDPOINT 16
42 #define PEI_USB_DEVICE_SIGNATURE SIGNATURE_32 ('U', 's', 'b', 'D')
45 PEI_USB_IO_PPI UsbIoPpi
;
46 EFI_PEI_PPI_DESCRIPTOR UsbIoPpiList
;
47 UINT16 MaxPacketSize0
;
52 UINT8 DownStreamPortNo
;
53 UINTN AllocateAddress
;
54 PEI_USB_HOST_CONTROLLER_PPI
*UsbHcPpi
;
55 PEI_USB2_HOST_CONTROLLER_PPI
*Usb2HcPpi
;
56 UINT8 ConfigurationData
[1024];
57 EFI_USB_CONFIG_DESCRIPTOR
*ConfigDesc
;
58 EFI_USB_INTERFACE_DESCRIPTOR
*InterfaceDesc
;
59 EFI_USB_INTERFACE_DESCRIPTOR
*InterfaceDescList
[MAX_INTERFACE
];
60 EFI_USB_ENDPOINT_DESCRIPTOR
*EndpointDesc
[MAX_ENDPOINT
];
61 EFI_USB_ENDPOINT_DESCRIPTOR
*EndpointDescList
[MAX_INTERFACE
][MAX_ENDPOINT
];
62 EFI_USB2_HC_TRANSACTION_TRANSLATOR Translator
;
66 #define PEI_USB_DEVICE_FROM_THIS(a) CR (a, PEI_USB_DEVICE, UsbIoPpi, PEI_USB_DEVICE_SIGNATURE)
68 #define USB_BIT_IS_SET(Data, Bit) ((BOOLEAN)(((Data) & (Bit)) == (Bit)))
70 #define USB_BUS_1_MILLISECOND 1000
73 // Wait for port reset, refers to specification
74 // [USB20-7.1.7.5, it says 10ms for hub and 50ms for
77 // According to USB2.0, Chapter 11.5.1.5 Resetting,
78 // the worst case for TDRST is 20ms
80 #define USB_SET_PORT_RESET_STALL (20 * USB_BUS_1_MILLISECOND)
81 #define USB_SET_ROOT_PORT_RESET_STALL (50 * USB_BUS_1_MILLISECOND)
84 // Wait for clear roothub port reset, set by experience
86 #define USB_CLR_ROOT_PORT_RESET_STALL (20 * USB_BUS_1_MILLISECOND)
89 // Wait for port statue reg change, set by experience
91 #define USB_WAIT_PORT_STS_CHANGE_STALL (100)
94 // Host software return timeout if port status doesn't change
95 // after 500ms(LOOP * STALL = 5000 * 0.1ms), set by experience
97 #define USB_WAIT_PORT_STS_CHANGE_LOOP 5000
100 // Wait for hub port power-on, refers to specification
103 #define USB_SET_PORT_POWER_STALL (2 * USB_BUS_1_MILLISECOND)
106 // Wait for set device address, refers to specification
107 // [USB20-9.2.6.3, it says 2ms]
109 #define USB_SET_DEVICE_ADDRESS_STALL (2 * USB_BUS_1_MILLISECOND)
112 // Wait for get configuration descriptor, set by experience
114 #define USB_GET_CONFIG_DESCRIPTOR_STALL (1 * USB_BUS_1_MILLISECOND)
117 Submits control transfer to a target USB device.
119 @param PeiServices The pointer of EFI_PEI_SERVICES.
120 @param This The pointer of PEI_USB_IO_PPI.
121 @param Request USB device request to send.
122 @param Direction Specifies the data direction for the data stage.
123 @param Timeout Indicates the maximum timeout, in millisecond. If Timeout
124 is 0, then the caller must wait for the function to be
125 completed until EFI_SUCCESS or EFI_DEVICE_ERROR is returned.
126 @param Data Data buffer to be transmitted or received from USB device.
127 @param DataLength The size (in bytes) of the data buffer.
129 @retval EFI_SUCCESS Transfer was completed successfully.
130 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
131 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
132 @retval EFI_TIMEOUT Transfer failed due to timeout.
133 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
138 PeiUsbControlTransfer (
139 IN EFI_PEI_SERVICES
**PeiServices
,
140 IN PEI_USB_IO_PPI
*This
,
141 IN EFI_USB_DEVICE_REQUEST
*Request
,
142 IN EFI_USB_DATA_DIRECTION Direction
,
144 IN OUT VOID
*Data OPTIONAL
,
145 IN UINTN DataLength OPTIONAL
149 Submits bulk transfer to a bulk endpoint of a USB device.
151 @param PeiServices The pointer of EFI_PEI_SERVICES.
152 @param This The pointer of PEI_USB_IO_PPI.
153 @param DeviceEndpoint Endpoint number and its direction in bit 7.
154 @param Data A pointer to the buffer of data to transmit
155 from or receive into.
156 @param DataLength The length of the data buffer.
157 @param Timeout Indicates the maximum time, in millisecond, which the
158 transfer is allowed to complete. If Timeout is 0, then
159 the caller must wait for the function to be completed
160 until EFI_SUCCESS or EFI_DEVICE_ERROR is returned.
162 @retval EFI_SUCCESS The transfer was completed successfully.
163 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
164 @retval EFI_INVALID_PARAMETER Parameters are invalid.
165 @retval EFI_TIMEOUT The transfer failed due to timeout.
166 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
172 IN EFI_PEI_SERVICES
**PeiServices
,
173 IN PEI_USB_IO_PPI
*This
,
174 IN UINT8 DeviceEndpoint
,
176 IN OUT UINTN
*DataLength
,
181 Get the usb interface descriptor.
183 @param PeiServices General-purpose services that are available to every PEIM.
184 @param This Indicates the PEI_USB_IO_PPI instance.
185 @param InterfaceDescriptor Request interface descriptor.
188 @retval EFI_SUCCESS Usb interface descriptor is obtained successfully.
193 PeiUsbGetInterfaceDescriptor (
194 IN EFI_PEI_SERVICES
**PeiServices
,
195 IN PEI_USB_IO_PPI
*This
,
196 OUT EFI_USB_INTERFACE_DESCRIPTOR
**InterfaceDescriptor
200 Get the usb endpoint 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 EndpointIndex The valid index of the specified endpoint.
205 @param EndpointDescriptor Request endpoint descriptor.
207 @retval EFI_SUCCESS Usb endpoint descriptor is obtained successfully.
208 @retval EFI_NOT_FOUND Usb endpoint descriptor is NOT found.
213 PeiUsbGetEndpointDescriptor (
214 IN EFI_PEI_SERVICES
**PeiServices
,
215 IN PEI_USB_IO_PPI
*This
,
216 IN UINT8 EndpointIndex
,
217 OUT EFI_USB_ENDPOINT_DESCRIPTOR
**EndpointDescriptor
221 Reset the port and re-configure the usb device.
223 @param PeiServices General-purpose services that are available to every PEIM.
224 @param This Indicates the PEI_USB_IO_PPI instance.
226 @retval EFI_SUCCESS Usb device is reset and configured successfully.
227 @retval Others Other failure occurs.
233 IN EFI_PEI_SERVICES
**PeiServices
,
234 IN PEI_USB_IO_PPI
*This
238 Send reset signal over the given root hub port.
240 @param PeiServices Describes the list of possible PEI Services.
241 @param UsbHcPpi The pointer of PEI_USB_HOST_CONTROLLER_PPI instance.
242 @param Usb2HcPpi The pointer of PEI_USB2_HOST_CONTROLLER_PPI instance.
243 @param PortNum The port to be reset.
244 @param RetryIndex The retry times.
249 IN EFI_PEI_SERVICES
**PeiServices
,
250 IN PEI_USB_HOST_CONTROLLER_PPI
*UsbHcPpi
,
251 IN PEI_USB2_HOST_CONTROLLER_PPI
*Usb2HcPpi
,