]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbBusPei/UsbPeim.h
MdeModulePkg/UsbBusPei: Fix out-of-bound read access to descriptors
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusPei / UsbPeim.h
CommitLineData
4b1bf81c 1/** @file\r
2Usb Peim definition.\r
3\r
d1102dba
LG
4Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved. <BR>\r
5\r
4b1bf81c 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#ifndef _PEI_USB_PEIM_H_\r
18#define _PEI_USB_PEIM_H_\r
19\r
20\r
21#include <PiPei.h>\r
22\r
23#include <Ppi/UsbHostController.h>\r
24#include <Ppi/Usb2HostController.h>\r
25#include <Ppi/UsbIo.h>\r
26\r
27#include <Library/DebugLib.h>\r
28#include <Library/PeimEntryPoint.h>\r
29#include <Library/PeiServicesLib.h>\r
30#include <Library/BaseMemoryLib.h>\r
31#include <Library/TimerLib.h>\r
32#include <Library/PcdLib.h>\r
33\r
34#include <IndustryStandard/Usb.h>\r
35\r
c96de1db
RN
36//\r
37// A common header for usb standard descriptor.\r
38// Each stand descriptor has a length and type.\r
39//\r
40#pragma pack(1)\r
41typedef struct {\r
42 UINT8 Len;\r
43 UINT8 Type;\r
44} USB_DESC_HEAD;\r
45#pragma pack()\r
46\r
8a718ba9 47#define MAX_INTERFACE 8\r
4b1bf81c 48#define MAX_ENDPOINT 16\r
49\r
4b1bf81c 50#define PEI_USB_DEVICE_SIGNATURE SIGNATURE_32 ('U', 's', 'b', 'D')\r
51typedef struct {\r
52 UINTN Signature;\r
53 PEI_USB_IO_PPI UsbIoPpi;\r
54 EFI_PEI_PPI_DESCRIPTOR UsbIoPpiList;\r
d987459f
SZ
55 UINT16 MaxPacketSize0;\r
56 UINT16 DataToggle;\r
4b1bf81c 57 UINT8 DeviceAddress;\r
4b1bf81c 58 UINT8 DeviceSpeed;\r
4b1bf81c 59 UINT8 IsHub;\r
60 UINT8 DownStreamPortNo;\r
4b1bf81c 61 UINTN AllocateAddress;\r
62 PEI_USB_HOST_CONTROLLER_PPI *UsbHcPpi;\r
63 PEI_USB2_HOST_CONTROLLER_PPI *Usb2HcPpi;\r
64 UINT8 ConfigurationData[1024];\r
65 EFI_USB_CONFIG_DESCRIPTOR *ConfigDesc;\r
66 EFI_USB_INTERFACE_DESCRIPTOR *InterfaceDesc;\r
8a718ba9 67 EFI_USB_INTERFACE_DESCRIPTOR *InterfaceDescList[MAX_INTERFACE];\r
4b1bf81c 68 EFI_USB_ENDPOINT_DESCRIPTOR *EndpointDesc[MAX_ENDPOINT];\r
8a718ba9 69 EFI_USB_ENDPOINT_DESCRIPTOR *EndpointDescList[MAX_INTERFACE][MAX_ENDPOINT];\r
d987459f
SZ
70 EFI_USB2_HC_TRANSACTION_TRANSLATOR Translator;\r
71 UINT8 Tier;\r
4b1bf81c 72} PEI_USB_DEVICE;\r
73\r
74#define PEI_USB_DEVICE_FROM_THIS(a) CR (a, PEI_USB_DEVICE, UsbIoPpi, PEI_USB_DEVICE_SIGNATURE)\r
75\r
d987459f
SZ
76#define USB_BIT_IS_SET(Data, Bit) ((BOOLEAN)(((Data) & (Bit)) == (Bit)))\r
77\r
78#define USB_BUS_1_MILLISECOND 1000\r
79\r
80//\r
81// Wait for port reset, refers to specification\r
82// [USB20-7.1.7.5, it says 10ms for hub and 50ms for\r
83// root hub]\r
84//\r
85// According to USB2.0, Chapter 11.5.1.5 Resetting,\r
86// the worst case for TDRST is 20ms\r
87//\r
88#define USB_SET_PORT_RESET_STALL (20 * USB_BUS_1_MILLISECOND)\r
89#define USB_SET_ROOT_PORT_RESET_STALL (50 * USB_BUS_1_MILLISECOND)\r
90\r
91//\r
92// Wait for clear roothub port reset, set by experience\r
93//\r
94#define USB_CLR_ROOT_PORT_RESET_STALL (20 * USB_BUS_1_MILLISECOND)\r
95\r
96//\r
97// Wait for port statue reg change, set by experience\r
98//\r
99#define USB_WAIT_PORT_STS_CHANGE_STALL (100)\r
100\r
101//\r
d1102dba 102// Host software return timeout if port status doesn't change\r
d987459f
SZ
103// after 500ms(LOOP * STALL = 5000 * 0.1ms), set by experience\r
104//\r
105#define USB_WAIT_PORT_STS_CHANGE_LOOP 5000\r
106\r
107//\r
108// Wait for hub port power-on, refers to specification\r
109// [USB20-11.23.2]\r
110//\r
111#define USB_SET_PORT_POWER_STALL (2 * USB_BUS_1_MILLISECOND)\r
4b1bf81c 112\r
04910ceb
SZ
113//\r
114// Wait for set device address, refers to specification\r
115// [USB20-9.2.6.3, it says 2ms]\r
116//\r
117#define USB_SET_DEVICE_ADDRESS_STALL (2 * USB_BUS_1_MILLISECOND)\r
118\r
119//\r
120// Wait for get configuration descriptor, set by experience\r
121//\r
122#define USB_GET_CONFIG_DESCRIPTOR_STALL (1 * USB_BUS_1_MILLISECOND)\r
123\r
4b1bf81c 124/**\r
125 Submits control transfer to a target USB device.\r
d1102dba 126\r
4b1bf81c 127 @param PeiServices The pointer of EFI_PEI_SERVICES.\r
128 @param This The pointer of PEI_USB_IO_PPI.\r
129 @param Request USB device request to send.\r
130 @param Direction Specifies the data direction for the data stage.\r
ca243131
FT
131 @param Timeout Indicates the maximum timeout, in millisecond. If Timeout\r
132 is 0, then the caller must wait for the function to be\r
133 completed until EFI_SUCCESS or EFI_DEVICE_ERROR is returned.\r
4b1bf81c 134 @param Data Data buffer to be transmitted or received from USB device.\r
135 @param DataLength The size (in bytes) of the data buffer.\r
136\r
137 @retval EFI_SUCCESS Transfer was completed successfully.\r
138 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.\r
139 @retval EFI_INVALID_PARAMETER Some parameters are invalid.\r
140 @retval EFI_TIMEOUT Transfer failed due to timeout.\r
141 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.\r
142\r
143**/\r
144EFI_STATUS\r
145EFIAPI\r
146PeiUsbControlTransfer (\r
147 IN EFI_PEI_SERVICES **PeiServices,\r
148 IN PEI_USB_IO_PPI *This,\r
149 IN EFI_USB_DEVICE_REQUEST *Request,\r
150 IN EFI_USB_DATA_DIRECTION Direction,\r
151 IN UINT32 Timeout,\r
152 IN OUT VOID *Data, OPTIONAL\r
153 IN UINTN DataLength OPTIONAL\r
154 );\r
155\r
156/**\r
157 Submits bulk transfer to a bulk endpoint of a USB device.\r
d1102dba 158\r
4b1bf81c 159 @param PeiServices The pointer of EFI_PEI_SERVICES.\r
160 @param This The pointer of PEI_USB_IO_PPI.\r
161 @param DeviceEndpoint Endpoint number and its direction in bit 7.\r
d1102dba 162 @param Data A pointer to the buffer of data to transmit\r
4b1bf81c 163 from or receive into.\r
164 @param DataLength The lenght of the data buffer.\r
165 @param Timeout Indicates the maximum time, in millisecond, which the\r
ca243131
FT
166 transfer is allowed to complete. If Timeout is 0, then\r
167 the caller must wait for the function to be completed\r
168 until EFI_SUCCESS or EFI_DEVICE_ERROR is returned.\r
4b1bf81c 169\r
170 @retval EFI_SUCCESS The transfer was completed successfully.\r
171 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.\r
172 @retval EFI_INVALID_PARAMETER Parameters are invalid.\r
173 @retval EFI_TIMEOUT The transfer failed due to timeout.\r
174 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.\r
175\r
176**/\r
177EFI_STATUS\r
178EFIAPI\r
179PeiUsbBulkTransfer (\r
180 IN EFI_PEI_SERVICES **PeiServices,\r
181 IN PEI_USB_IO_PPI *This,\r
182 IN UINT8 DeviceEndpoint,\r
183 IN OUT VOID *Data,\r
184 IN OUT UINTN *DataLength,\r
185 IN UINTN Timeout\r
186 );\r
187\r
188/**\r
189 Get the usb interface descriptor.\r
190\r
191 @param PeiServices General-purpose services that are available to every PEIM.\r
192 @param This Indicates the PEI_USB_IO_PPI instance.\r
193 @param InterfaceDescriptor Request interface descriptor.\r
194\r
195\r
196 @retval EFI_SUCCESS Usb interface descriptor is obtained successfully.\r
197\r
198**/\r
199EFI_STATUS\r
200EFIAPI\r
201PeiUsbGetInterfaceDescriptor (\r
202 IN EFI_PEI_SERVICES **PeiServices,\r
203 IN PEI_USB_IO_PPI *This,\r
204 OUT EFI_USB_INTERFACE_DESCRIPTOR **InterfaceDescriptor\r
205 );\r
206\r
207/**\r
208 Get the usb endpoint descriptor.\r
209\r
210 @param PeiServices General-purpose services that are available to every PEIM.\r
211 @param This Indicates the PEI_USB_IO_PPI instance.\r
212 @param EndpointIndex The valid index of the specified endpoint.\r
213 @param EndpointDescriptor Request endpoint descriptor.\r
214\r
215 @retval EFI_SUCCESS Usb endpoint descriptor is obtained successfully.\r
216 @retval EFI_NOT_FOUND Usb endpoint descriptor is NOT found.\r
217\r
218**/\r
219EFI_STATUS\r
220EFIAPI\r
221PeiUsbGetEndpointDescriptor (\r
222 IN EFI_PEI_SERVICES **PeiServices,\r
223 IN PEI_USB_IO_PPI *This,\r
224 IN UINT8 EndpointIndex,\r
225 OUT EFI_USB_ENDPOINT_DESCRIPTOR **EndpointDescriptor\r
226 );\r
227\r
228/**\r
229 Reset the port and re-configure the usb device.\r
230\r
231 @param PeiServices General-purpose services that are available to every PEIM.\r
232 @param This Indicates the PEI_USB_IO_PPI instance.\r
233\r
234 @retval EFI_SUCCESS Usb device is reset and configured successfully.\r
235 @retval Others Other failure occurs.\r
236\r
237**/\r
238EFI_STATUS\r
239EFIAPI\r
240PeiUsbPortReset (\r
241 IN EFI_PEI_SERVICES **PeiServices,\r
242 IN PEI_USB_IO_PPI *This\r
243 );\r
244\r
245/**\r
246 Send reset signal over the given root hub port.\r
d1102dba 247\r
4b1bf81c 248 @param PeiServices Describes the list of possible PEI Services.\r
249 @param UsbHcPpi The pointer of PEI_USB_HOST_CONTROLLER_PPI instance.\r
250 @param Usb2HcPpi The pointer of PEI_USB2_HOST_CONTROLLER_PPI instance.\r
251 @param PortNum The port to be reset.\r
252 @param RetryIndex The retry times.\r
253\r
254**/\r
255VOID\r
256ResetRootPort (\r
257 IN EFI_PEI_SERVICES **PeiServices,\r
258 IN PEI_USB_HOST_CONTROLLER_PPI *UsbHcPpi,\r
259 IN PEI_USB2_HOST_CONTROLLER_PPI *Usb2HcPpi,\r
260 IN UINT8 PortNum,\r
261 IN UINT8 RetryIndex\r
262 );\r
263\r
264#endif\r