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