]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Usb/UsbBusPei/UsbIoPeim.c
MdeModulePkg/UsbBus: Fix various typos
[mirror_edk2.git] / MdeModulePkg / Bus / Usb / UsbBusPei / UsbIoPeim.c
CommitLineData
4b1bf81c 1/** @file\r
2The module is used to implement Usb Io PPI interfaces.\r
3\r
d1102dba
LG
4Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved. <BR>\r
5\r
9d510e61 6SPDX-License-Identifier: BSD-2-Clause-Patent\r
4b1bf81c 7\r
8**/\r
9\r
10#include "UsbPeim.h"\r
11#include "PeiUsbLib.h"\r
12\r
13/**\r
14 Submits control transfer to a target USB device.\r
d1102dba 15\r
4b1bf81c 16 @param PeiServices The pointer of EFI_PEI_SERVICES.\r
17 @param This The pointer of PEI_USB_IO_PPI.\r
18 @param Request USB device request to send.\r
19 @param Direction Specifies the data direction for the data stage.\r
ca243131
FT
20 @param Timeout Indicates the maximum timeout, in millisecond. If Timeout\r
21 is 0, then the caller must wait for the function to be\r
22 completed until EFI_SUCCESS or EFI_DEVICE_ERROR is returned.\r
4b1bf81c 23 @param Data Data buffer to be transmitted or received from USB device.\r
24 @param DataLength The size (in bytes) of the data buffer.\r
25\r
26 @retval EFI_SUCCESS Transfer was completed successfully.\r
27 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.\r
28 @retval EFI_INVALID_PARAMETER Some parameters are invalid.\r
29 @retval EFI_TIMEOUT Transfer failed due to timeout.\r
30 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.\r
31\r
32**/\r
33EFI_STATUS\r
34EFIAPI\r
35PeiUsbControlTransfer (\r
36 IN EFI_PEI_SERVICES **PeiServices,\r
37 IN PEI_USB_IO_PPI *This,\r
38 IN EFI_USB_DEVICE_REQUEST *Request,\r
39 IN EFI_USB_DATA_DIRECTION Direction,\r
40 IN UINT32 Timeout,\r
41 IN OUT VOID *Data, OPTIONAL\r
42 IN UINTN DataLength OPTIONAL\r
43 )\r
44{\r
45 EFI_STATUS Status;\r
46 PEI_USB_DEVICE *PeiUsbDev;\r
47 UINT32 TransferResult;\r
506560e7
SZ
48 EFI_USB_ENDPOINT_DESCRIPTOR *EndpointDescriptor;\r
49 UINT8 EndpointIndex;\r
4b1bf81c 50\r
51 PeiUsbDev = PEI_USB_DEVICE_FROM_THIS (This);\r
52\r
506560e7
SZ
53 EndpointDescriptor = NULL;\r
54 EndpointIndex = 0;\r
55\r
56 if ((Request->Request == USB_REQ_CLEAR_FEATURE) &&\r
57 (Request->RequestType == USB_DEV_CLEAR_FEATURE_REQ_TYPE_E) &&\r
58 (Request->Value == USB_FEATURE_ENDPOINT_HALT)) {\r
59 //\r
60 // Request->Index is the Endpoint Address, use it to get the Endpoint Index.\r
61 //\r
62 while (EndpointIndex < MAX_ENDPOINT) {\r
63 Status = PeiUsbGetEndpointDescriptor (PeiServices, This, EndpointIndex, &EndpointDescriptor);\r
64 if (EFI_ERROR (Status)) {\r
65 return EFI_INVALID_PARAMETER;\r
66 }\r
67\r
68 if (EndpointDescriptor->EndpointAddress == Request->Index) {\r
69 break;\r
70 }\r
71\r
72 EndpointIndex++;\r
73 }\r
74\r
75 if (EndpointIndex == MAX_ENDPOINT) {\r
76 return EFI_INVALID_PARAMETER;\r
77 }\r
78 }\r
79\r
4b1bf81c 80 if (PeiUsbDev->Usb2HcPpi != NULL) {\r
81 Status = PeiUsbDev->Usb2HcPpi->ControlTransfer (\r
82 PeiServices,\r
83 PeiUsbDev->Usb2HcPpi,\r
84 PeiUsbDev->DeviceAddress,\r
85 PeiUsbDev->DeviceSpeed,\r
86 PeiUsbDev->MaxPacketSize0,\r
87 Request,\r
88 Direction,\r
89 Data,\r
90 &DataLength,\r
91 Timeout,\r
92 &(PeiUsbDev->Translator),\r
93 &TransferResult\r
94 );\r
95 } else {\r
96 Status = PeiUsbDev->UsbHcPpi->ControlTransfer (\r
97 PeiServices,\r
98 PeiUsbDev->UsbHcPpi,\r
99 PeiUsbDev->DeviceAddress,\r
100 PeiUsbDev->DeviceSpeed,\r
d987459f 101 (UINT8) PeiUsbDev->MaxPacketSize0,\r
4b1bf81c 102 Request,\r
103 Direction,\r
104 Data,\r
105 &DataLength,\r
106 Timeout,\r
107 &TransferResult\r
108 );\r
109 }\r
506560e7
SZ
110\r
111 //\r
112 // Reset the endpoint toggle when endpoint stall is cleared\r
113 //\r
114 if ((Request->Request == USB_REQ_CLEAR_FEATURE) &&\r
115 (Request->RequestType == USB_DEV_CLEAR_FEATURE_REQ_TYPE_E) &&\r
116 (Request->Value == USB_FEATURE_ENDPOINT_HALT)) {\r
117 if ((PeiUsbDev->DataToggle & (1 << EndpointIndex)) != 0) {\r
118 PeiUsbDev->DataToggle = (UINT16) (PeiUsbDev->DataToggle ^ (1 << EndpointIndex));\r
119 }\r
120 }\r
121\r
d987459f 122 DEBUG ((EFI_D_INFO, "PeiUsbControlTransfer: %r\n", Status));\r
4b1bf81c 123 return Status;\r
124}\r
125\r
126/**\r
127 Submits bulk transfer to a bulk endpoint of a USB device.\r
d1102dba 128\r
4b1bf81c 129 @param PeiServices The pointer of EFI_PEI_SERVICES.\r
130 @param This The pointer of PEI_USB_IO_PPI.\r
131 @param DeviceEndpoint Endpoint number and its direction in bit 7.\r
d1102dba 132 @param Data A pointer to the buffer of data to transmit\r
4b1bf81c 133 from or receive into.\r
13a623cf 134 @param DataLength The length of the data buffer.\r
4b1bf81c 135 @param Timeout Indicates the maximum time, in millisecond, which the\r
ca243131
FT
136 transfer is allowed to complete. If Timeout is 0, then\r
137 the caller must wait for the function to be completed\r
138 until EFI_SUCCESS or EFI_DEVICE_ERROR is returned.\r
4b1bf81c 139\r
140 @retval EFI_SUCCESS The transfer was completed successfully.\r
141 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.\r
142 @retval EFI_INVALID_PARAMETER Parameters are invalid.\r
143 @retval EFI_TIMEOUT The transfer failed due to timeout.\r
144 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.\r
145\r
146**/\r
147EFI_STATUS\r
148EFIAPI\r
149PeiUsbBulkTransfer (\r
150 IN EFI_PEI_SERVICES **PeiServices,\r
151 IN PEI_USB_IO_PPI *This,\r
152 IN UINT8 DeviceEndpoint,\r
153 IN OUT VOID *Data,\r
154 IN OUT UINTN *DataLength,\r
155 IN UINTN Timeout\r
156 )\r
157{\r
158 EFI_STATUS Status;\r
159 PEI_USB_DEVICE *PeiUsbDev;\r
160 UINT32 TransferResult;\r
161 UINTN MaxPacketLength;\r
162 UINT8 DataToggle;\r
163 UINT8 OldToggle;\r
164 EFI_USB_ENDPOINT_DESCRIPTOR *EndpointDescriptor;\r
165 UINT8 EndpointIndex;\r
166 VOID *Data2[EFI_USB_MAX_BULK_BUFFER_NUM];\r
167\r
168 PeiUsbDev = PEI_USB_DEVICE_FROM_THIS (This);\r
169\r
170 EndpointDescriptor = NULL;\r
171 EndpointIndex = 0;\r
172 Data2[0] = Data;\r
173 Data2[1] = NULL;\r
174\r
175 while (EndpointIndex < MAX_ENDPOINT) {\r
176 Status = PeiUsbGetEndpointDescriptor (PeiServices, This, EndpointIndex, &EndpointDescriptor);\r
177 if (EFI_ERROR (Status)) {\r
178 return EFI_INVALID_PARAMETER;\r
179 }\r
180\r
181 if (EndpointDescriptor->EndpointAddress == DeviceEndpoint) {\r
182 break;\r
183 }\r
184\r
185 EndpointIndex++;\r
186 }\r
187\r
188 if (EndpointIndex == MAX_ENDPOINT) {\r
189 return EFI_INVALID_PARAMETER;\r
190 }\r
191\r
192 MaxPacketLength = PeiUsbDev->EndpointDesc[EndpointIndex]->MaxPacketSize;\r
193 if ((PeiUsbDev->DataToggle & (1 << EndpointIndex)) != 0) {\r
194 DataToggle = 1;\r
195 } else {\r
196 DataToggle = 0;\r
197 }\r
198\r
199 OldToggle = DataToggle;\r
200\r
201 if (PeiUsbDev->Usb2HcPpi != NULL) {\r
202 Status = PeiUsbDev->Usb2HcPpi->BulkTransfer (\r
203 PeiServices,\r
204 PeiUsbDev->Usb2HcPpi,\r
205 PeiUsbDev->DeviceAddress,\r
206 DeviceEndpoint,\r
207 PeiUsbDev->DeviceSpeed,\r
208 MaxPacketLength,\r
209 Data2,\r
210 DataLength,\r
211 &DataToggle,\r
212 Timeout,\r
213 &(PeiUsbDev->Translator),\r
214 &TransferResult\r
215 );\r
216 } else {\r
217 Status = PeiUsbDev->UsbHcPpi->BulkTransfer (\r
218 PeiServices,\r
219 PeiUsbDev->UsbHcPpi,\r
220 PeiUsbDev->DeviceAddress,\r
221 DeviceEndpoint,\r
222 (UINT8) MaxPacketLength,\r
223 Data,\r
224 DataLength,\r
225 &DataToggle,\r
226 Timeout,\r
227 &TransferResult\r
228 );\r
229 }\r
230\r
231 if (OldToggle != DataToggle) {\r
506560e7 232 PeiUsbDev->DataToggle = (UINT16) (PeiUsbDev->DataToggle ^ (1 << EndpointIndex));\r
4b1bf81c 233 }\r
234\r
d987459f 235 DEBUG ((EFI_D_INFO, "PeiUsbBulkTransfer: %r\n", Status));\r
4b1bf81c 236 return Status;\r
237}\r
238\r
239/**\r
240 Get the usb interface descriptor.\r
241\r
242 @param PeiServices General-purpose services that are available to every PEIM.\r
243 @param This Indicates the PEI_USB_IO_PPI instance.\r
244 @param InterfaceDescriptor Request interface descriptor.\r
245\r
246\r
247 @retval EFI_SUCCESS Usb interface descriptor is obtained successfully.\r
248\r
249**/\r
250EFI_STATUS\r
251EFIAPI\r
252PeiUsbGetInterfaceDescriptor (\r
253 IN EFI_PEI_SERVICES **PeiServices,\r
254 IN PEI_USB_IO_PPI *This,\r
255 OUT EFI_USB_INTERFACE_DESCRIPTOR **InterfaceDescriptor\r
256 )\r
257{\r
258 PEI_USB_DEVICE *PeiUsbDev;\r
259 PeiUsbDev = PEI_USB_DEVICE_FROM_THIS (This);\r
260 *InterfaceDescriptor = PeiUsbDev->InterfaceDesc;\r
261 return EFI_SUCCESS;\r
262}\r
263\r
264/**\r
265 Get the usb endpoint descriptor.\r
266\r
267 @param PeiServices General-purpose services that are available to every PEIM.\r
268 @param This Indicates the PEI_USB_IO_PPI instance.\r
269 @param EndpointIndex The valid index of the specified endpoint.\r
270 @param EndpointDescriptor Request endpoint descriptor.\r
271\r
272 @retval EFI_SUCCESS Usb endpoint descriptor is obtained successfully.\r
273 @retval EFI_NOT_FOUND Usb endpoint descriptor is NOT found.\r
274\r
275**/\r
276EFI_STATUS\r
277EFIAPI\r
278PeiUsbGetEndpointDescriptor (\r
279 IN EFI_PEI_SERVICES **PeiServices,\r
280 IN PEI_USB_IO_PPI *This,\r
281 IN UINT8 EndpointIndex,\r
282 OUT EFI_USB_ENDPOINT_DESCRIPTOR **EndpointDescriptor\r
283 )\r
284{\r
285 PEI_USB_DEVICE *PeiUsbDev;\r
286\r
287 PeiUsbDev = PEI_USB_DEVICE_FROM_THIS (This);\r
288\r
289 ASSERT (EndpointDescriptor != NULL);\r
290\r
291 //\r
292 // The valid range of EndpointIndex is 0..15\r
293 // If EndpointIndex is lesser than 15 but larger than the number of interfaces,\r
294 // a EFI_NOT_FOUND should be returned\r
295 //\r
296 ASSERT (EndpointIndex <= 15);\r
297\r
298 if (EndpointIndex >= PeiUsbDev->InterfaceDesc->NumEndpoints) {\r
299 return EFI_NOT_FOUND;\r
300 }\r
301\r
302 *EndpointDescriptor = PeiUsbDev->EndpointDesc[EndpointIndex];\r
303\r
304 return EFI_SUCCESS;\r
305}\r
306\r
307/**\r
308 Reset the port and re-configure the usb device.\r
309\r
310 @param PeiServices General-purpose services that are available to every PEIM.\r
311 @param This Indicates the PEI_USB_IO_PPI instance.\r
312\r
313 @retval EFI_SUCCESS Usb device is reset and configured successfully.\r
314 @retval Others Other failure occurs.\r
315\r
316**/\r
317EFI_STATUS\r
318EFIAPI\r
319PeiUsbPortReset (\r
320 IN EFI_PEI_SERVICES **PeiServices,\r
321 IN PEI_USB_IO_PPI *This\r
322 )\r
323{\r
324 PEI_USB_DEVICE *PeiUsbDev;\r
325 EFI_STATUS Status;\r
326 UINT8 Address;\r
327\r
328 PeiUsbDev = PEI_USB_DEVICE_FROM_THIS (This);\r
329\r
330 ResetRootPort (\r
331 PeiServices,\r
332 PeiUsbDev->UsbHcPpi,\r
333 PeiUsbDev->Usb2HcPpi,\r
334 PeiUsbDev->DeviceAddress,\r
335 0\r
336 );\r
337\r
338 //\r
339 // Set address\r
340 //\r
341 Address = PeiUsbDev->DeviceAddress;\r
342 PeiUsbDev->DeviceAddress = 0;\r
343\r
344 Status = PeiUsbSetDeviceAddress (\r
345 PeiServices,\r
346 This,\r
347 Address\r
348 );\r
349\r
350 if (EFI_ERROR (Status)) {\r
351 return Status;\r
352 }\r
353\r
354 PeiUsbDev->DeviceAddress = Address;\r
355\r
356 //\r
357 // Set default configuration\r
358 //\r
359 Status = PeiUsbSetConfiguration (\r
360 PeiServices,\r
361 This\r
362 );\r
363\r
364 return Status;\r
365}\r