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