]>
Commit | Line | Data |
---|---|---|
e237e7ae | 1 | /** @file\r |
2 | \r | |
3 | Copyright (c) 2004 - 2007, Intel Corporation\r | |
4 | All rights reserved. This program and the accompanying materials\r | |
5 | are licensed and made available under the terms and conditions of the BSD License\r | |
6 | which accompanies this distribution. The full text of the license may be found at\r | |
7 | http://opensource.org/licenses/bsd-license.php\r | |
8 | \r | |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
11 | \r | |
12 | Module Name:\r | |
13 | \r | |
14 | UsbBus.c\r | |
15 | \r | |
16 | Abstract:\r | |
17 | \r | |
18 | Usb Bus Driver Binding and Bus IO Protocol\r | |
19 | \r | |
20 | Revision History\r | |
21 | \r | |
22 | \r | |
23 | **/\r | |
24 | \r | |
25 | #include "UsbBus.h"\r | |
26 | \r | |
27 | //\r | |
28 | // USB_BUS_PROTOCOL is only used to locate USB_BUS\r | |
29 | //\r | |
30 | EFI_GUID mUsbBusProtocolGuid = EFI_USB_BUS_PROTOCOL_GUID;\r | |
31 | \r | |
32 | \r | |
33 | /**\r | |
34 | USB_IO function to execute a control transfer. This\r | |
35 | function will execute the USB transfer. If transfer\r | |
36 | successes, it will sync the internal state of USB bus\r | |
37 | with device state.\r | |
38 | \r | |
39 | @param This The USB_IO instance\r | |
40 | @param Request The control transfer request\r | |
41 | @param Direction Direction for data stage\r | |
42 | @param Timeout The time to wait before timeout\r | |
43 | @param Data The buffer holding the data\r | |
44 | @param DataLength Then length of the data\r | |
45 | @param UsbStatus USB result\r | |
46 | \r | |
47 | @retval EFI_INVALID_PARAMETER The parameters are invalid\r | |
48 | @retval EFI_SUCCESS The control transfer succeded.\r | |
49 | @retval Others Failed to execute the transfer\r | |
50 | \r | |
51 | **/\r | |
52 | STATIC\r | |
53 | EFI_STATUS\r | |
54 | EFIAPI\r | |
55 | UsbIoControlTransfer (\r | |
56 | IN EFI_USB_IO_PROTOCOL *This,\r | |
57 | IN EFI_USB_DEVICE_REQUEST *Request,\r | |
58 | IN EFI_USB_DATA_DIRECTION Direction,\r | |
59 | IN UINT32 Timeout,\r | |
60 | IN OUT VOID *Data, OPTIONAL\r | |
61 | IN UINTN DataLength, OPTIONAL\r | |
62 | OUT UINT32 *UsbStatus\r | |
63 | )\r | |
64 | {\r | |
65 | USB_DEVICE *Dev;\r | |
66 | USB_INTERFACE *UsbIf;\r | |
67 | USB_ENDPOINT_DESC *EpDesc;\r | |
68 | EFI_TPL OldTpl;\r | |
69 | EFI_STATUS Status;\r | |
70 | \r | |
71 | if (UsbStatus == NULL) {\r | |
72 | return EFI_INVALID_PARAMETER;\r | |
73 | }\r | |
74 | \r | |
75 | OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r | |
76 | \r | |
77 | UsbIf = USB_INTERFACE_FROM_USBIO (This);\r | |
78 | Dev = UsbIf->Device;\r | |
79 | \r | |
80 | Status = UsbHcControlTransfer (\r | |
81 | Dev->Bus,\r | |
82 | Dev->Address,\r | |
83 | Dev->Speed,\r | |
84 | Dev->MaxPacket0,\r | |
85 | Request,\r | |
86 | Direction,\r | |
87 | Data,\r | |
88 | &DataLength,\r | |
89 | (UINTN) Timeout,\r | |
90 | &Dev->Translator,\r | |
91 | UsbStatus\r | |
92 | );\r | |
93 | \r | |
94 | if (EFI_ERROR (Status) || (*UsbStatus != EFI_USB_NOERROR)) {\r | |
95 | //\r | |
96 | // Clear TT buffer when CTRL/BULK split transaction failes\r | |
97 | // Clear the TRANSLATOR TT buffer, not parent's buffer\r | |
98 | //\r | |
99 | if (Dev->Translator.TranslatorHubAddress != 0) {\r | |
100 | UsbHubCtrlClearTTBuffer (\r | |
101 | Dev->Bus->Devices[Dev->Translator.TranslatorHubAddress],\r | |
102 | Dev->Translator.TranslatorPortNumber,\r | |
103 | Dev->Address,\r | |
104 | 0,\r | |
105 | USB_ENDPOINT_CONTROL\r | |
106 | );\r | |
107 | }\r | |
108 | \r | |
109 | goto ON_EXIT;\r | |
110 | }\r | |
111 | \r | |
112 | //\r | |
113 | // Some control transfer will change the device's internal\r | |
114 | // status, such as Set_Configuration and Set_Interface.\r | |
115 | // We must synchronize the bus driver's status with that in\r | |
116 | // device. We ignore the Set_Descriptor request because it's\r | |
117 | // hardly used by any device, especially in pre-boot environment\r | |
118 | //\r | |
119 | \r | |
120 | //\r | |
121 | // Reset the endpoint toggle when endpoint stall is cleared\r | |
122 | //\r | |
123 | if ((Request->Request == USB_REQ_CLEAR_FEATURE) &&\r | |
124 | (Request->RequestType == USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD,\r | |
125 | USB_TARGET_ENDPOINT)) &&\r | |
126 | (Request->Value == USB_FEATURE_ENDPOINT_HALT)) {\r | |
127 | \r | |
128 | EpDesc = UsbGetEndpointDesc (UsbIf, (UINT8) Request->Index);\r | |
129 | \r | |
130 | if (EpDesc != NULL) {\r | |
131 | EpDesc->Toggle = 0;\r | |
132 | }\r | |
133 | }\r | |
134 | \r | |
135 | //\r | |
136 | // Select a new configuration. This is a dangerous action. Upper driver\r | |
137 | // should stop use its current UsbIo after calling this driver. The old\r | |
138 | // UsbIo will be uninstalled and new UsbIo be installed. We can't use\r | |
139 | // ReinstallProtocol since interfaces in different configuration may be\r | |
140 | // completely irrellvant.\r | |
141 | //\r | |
142 | if ((Request->Request == USB_REQ_SET_CONFIG) &&\r | |
143 | (Request->RequestType == USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD,\r | |
144 | USB_TARGET_DEVICE))) {\r | |
145 | //\r | |
146 | // Don't re-create the USB interfaces if configuration isn't changed.\r | |
147 | //\r | |
148 | if ((Dev->ActiveConfig != NULL) &&\r | |
149 | (Request->Value == Dev->ActiveConfig->Desc.ConfigurationValue)) {\r | |
150 | \r | |
151 | goto ON_EXIT;\r | |
152 | }\r | |
d2577026 | 153 | DEBUG ((EFI_D_INFO, "UsbIoControlTransfer: configure changed!!! Do NOT use old UsbIo!!!\n"));\r |
e237e7ae | 154 | \r |
155 | if (Dev->ActiveConfig != NULL) {\r | |
156 | UsbRemoveConfig (Dev);\r | |
157 | }\r | |
158 | \r | |
159 | if (Request->Value != 0) {\r | |
160 | Status = UsbSelectConfig (Dev, (UINT8) Request->Value);\r | |
161 | }\r | |
162 | \r | |
163 | //\r | |
164 | // Exit now, Old USB_IO is invalid now\r | |
165 | //\r | |
166 | goto ON_EXIT;\r | |
167 | }\r | |
168 | \r | |
169 | //\r | |
170 | // A new alternative setting is selected for the interface.\r | |
171 | // No need to reinstall UsbIo in this case because only\r | |
172 | // underlying communication endpoints are changed. Functionality\r | |
173 | // should remains the same.\r | |
174 | //\r | |
175 | if ((Request->Request == USB_REQ_SET_INTERFACE) &&\r | |
176 | (Request->RequestType == USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD,\r | |
177 | USB_TARGET_INTERFACE)) &&\r | |
178 | (Request->Index == UsbIf->IfSetting->Desc.InterfaceNumber)) {\r | |
179 | \r | |
180 | Status = UsbSelectSetting (UsbIf->IfDesc, (UINT8) Request->Value);\r | |
181 | \r | |
182 | if (!EFI_ERROR (Status)) {\r | |
183 | UsbIf->IfSetting = UsbIf->IfDesc->Settings[UsbIf->IfDesc->ActiveIndex];\r | |
184 | }\r | |
185 | }\r | |
186 | \r | |
187 | ON_EXIT:\r | |
188 | gBS->RestoreTPL (OldTpl);\r | |
189 | return Status;\r | |
190 | }\r | |
191 | \r | |
192 | \r | |
193 | /**\r | |
194 | Execute a bulk transfer to the device endpoint\r | |
195 | \r | |
196 | @param This The USB IO instance\r | |
197 | @param Endpoint The device endpoint\r | |
198 | @param Data The data to transfer\r | |
199 | @param DataLength The length of the data to transfer\r | |
200 | @param Timeout Time to wait before timeout\r | |
201 | @param UsbStatus The result of USB transfer\r | |
202 | \r | |
203 | @retval EFI_SUCCESS The bulk transfer is OK\r | |
204 | @retval EFI_INVALID_PARAMETER Some parameters are invalid\r | |
205 | @retval Others Failed to execute transfer, reason returned in\r | |
206 | UsbStatus\r | |
207 | \r | |
208 | **/\r | |
209 | STATIC\r | |
210 | EFI_STATUS\r | |
211 | EFIAPI\r | |
212 | UsbIoBulkTransfer (\r | |
213 | IN EFI_USB_IO_PROTOCOL *This,\r | |
214 | IN UINT8 Endpoint,\r | |
215 | IN OUT VOID *Data,\r | |
216 | IN OUT UINTN *DataLength,\r | |
217 | IN UINTN Timeout,\r | |
218 | OUT UINT32 *UsbStatus\r | |
219 | )\r | |
220 | {\r | |
221 | USB_DEVICE *Dev;\r | |
222 | USB_INTERFACE *UsbIf;\r | |
223 | USB_ENDPOINT_DESC *EpDesc;\r | |
224 | UINT8 BufNum;\r | |
225 | UINT8 Toggle;\r | |
226 | EFI_TPL OldTpl;\r | |
227 | EFI_STATUS Status;\r | |
228 | \r | |
229 | if ((USB_ENDPOINT_ADDR (Endpoint) == 0) || (USB_ENDPOINT_ADDR(Endpoint) > 15) ||\r | |
230 | (UsbStatus == NULL)) {\r | |
231 | \r | |
232 | return EFI_INVALID_PARAMETER;\r | |
233 | }\r | |
234 | \r | |
235 | OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r | |
236 | \r | |
237 | UsbIf = USB_INTERFACE_FROM_USBIO (This);\r | |
238 | Dev = UsbIf->Device;\r | |
239 | \r | |
240 | EpDesc = UsbGetEndpointDesc (UsbIf, Endpoint);\r | |
241 | \r | |
242 | if ((EpDesc == NULL) || (USB_ENDPOINT_TYPE (&EpDesc->Desc) != USB_ENDPOINT_BULK)) {\r | |
243 | Status = EFI_INVALID_PARAMETER;\r | |
244 | goto ON_EXIT;\r | |
245 | }\r | |
246 | \r | |
247 | BufNum = 1;\r | |
248 | Toggle = EpDesc->Toggle;\r | |
249 | Status = UsbHcBulkTransfer (\r | |
250 | Dev->Bus,\r | |
251 | Dev->Address,\r | |
252 | Endpoint,\r | |
253 | Dev->Speed,\r | |
254 | EpDesc->Desc.MaxPacketSize,\r | |
255 | BufNum,\r | |
256 | &Data,\r | |
257 | DataLength,\r | |
258 | &Toggle,\r | |
259 | Timeout,\r | |
260 | &Dev->Translator,\r | |
261 | UsbStatus\r | |
262 | );\r | |
263 | \r | |
264 | EpDesc->Toggle = Toggle;\r | |
265 | \r | |
266 | if (EFI_ERROR (Status) || (*UsbStatus != EFI_USB_NOERROR)) {\r | |
267 | //\r | |
268 | // Clear TT buffer when CTRL/BULK split transaction failes.\r | |
269 | // Clear the TRANSLATOR TT buffer, not parent's buffer\r | |
270 | //\r | |
271 | if (Dev->Translator.TranslatorHubAddress != 0) {\r | |
272 | UsbHubCtrlClearTTBuffer (\r | |
273 | Dev->Bus->Devices[Dev->Translator.TranslatorHubAddress],\r | |
274 | Dev->Translator.TranslatorPortNumber,\r | |
275 | Dev->Address,\r | |
276 | 0,\r | |
277 | USB_ENDPOINT_BULK\r | |
278 | );\r | |
279 | }\r | |
280 | }\r | |
281 | \r | |
282 | ON_EXIT:\r | |
283 | gBS->RestoreTPL (OldTpl);\r | |
284 | return Status;\r | |
285 | }\r | |
286 | \r | |
287 | \r | |
288 | /**\r | |
289 | Execute a synchronous interrupt transfer\r | |
290 | \r | |
291 | @param This The USB IO instance\r | |
292 | @param Endpoint The device endpoint\r | |
293 | @param Data The data to transfer\r | |
294 | @param DataLength The length of the data to transfer\r | |
295 | @param Timeout Time to wait before timeout\r | |
296 | @param UsbStatus The result of USB transfer\r | |
297 | \r | |
298 | @retval EFI_SUCCESS The synchronous interrupt transfer is OK\r | |
299 | @retval EFI_INVALID_PARAMETER Some parameters are invalid\r | |
300 | @retval Others Failed to execute transfer, reason returned in\r | |
301 | UsbStatus\r | |
302 | \r | |
303 | **/\r | |
304 | STATIC\r | |
305 | EFI_STATUS\r | |
306 | EFIAPI\r | |
307 | UsbIoSyncInterruptTransfer (\r | |
308 | IN EFI_USB_IO_PROTOCOL *This,\r | |
309 | IN UINT8 Endpoint,\r | |
310 | IN OUT VOID *Data,\r | |
311 | IN OUT UINTN *DataLength,\r | |
312 | IN UINTN Timeout,\r | |
313 | OUT UINT32 *UsbStatus\r | |
314 | )\r | |
315 | {\r | |
316 | USB_DEVICE *Dev;\r | |
317 | USB_INTERFACE *UsbIf;\r | |
318 | USB_ENDPOINT_DESC *EpDesc;\r | |
319 | EFI_TPL OldTpl;\r | |
320 | UINT8 Toggle;\r | |
321 | EFI_STATUS Status;\r | |
322 | \r | |
323 | if ((USB_ENDPOINT_ADDR (Endpoint) == 0) || (USB_ENDPOINT_ADDR(Endpoint) > 15) ||\r | |
324 | (UsbStatus == NULL)) {\r | |
325 | \r | |
326 | return EFI_INVALID_PARAMETER;\r | |
327 | }\r | |
328 | \r | |
329 | OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r | |
330 | \r | |
331 | UsbIf = USB_INTERFACE_FROM_USBIO (This);\r | |
332 | Dev = UsbIf->Device;\r | |
333 | \r | |
334 | EpDesc = UsbGetEndpointDesc (UsbIf, Endpoint);\r | |
335 | \r | |
336 | if ((EpDesc == NULL) || (USB_ENDPOINT_TYPE (&EpDesc->Desc) != USB_ENDPOINT_INTERRUPT)) {\r | |
337 | Status = EFI_INVALID_PARAMETER;\r | |
338 | goto ON_EXIT;\r | |
339 | }\r | |
340 | \r | |
341 | Toggle = EpDesc->Toggle;\r | |
342 | Status = UsbHcSyncInterruptTransfer (\r | |
343 | Dev->Bus,\r | |
344 | Dev->Address,\r | |
345 | Endpoint,\r | |
346 | Dev->Speed,\r | |
347 | EpDesc->Desc.MaxPacketSize,\r | |
348 | Data,\r | |
349 | DataLength,\r | |
350 | &Toggle,\r | |
351 | Timeout,\r | |
352 | &Dev->Translator,\r | |
353 | UsbStatus\r | |
354 | );\r | |
355 | \r | |
356 | EpDesc->Toggle = Toggle;\r | |
357 | \r | |
358 | ON_EXIT:\r | |
359 | gBS->RestoreTPL (OldTpl);\r | |
360 | return Status;\r | |
361 | }\r | |
362 | \r | |
363 | \r | |
364 | /**\r | |
365 | Queue a new asynchronous interrupt transfer, or remove the old\r | |
366 | request if (IsNewTransfer == FALSE)\r | |
367 | \r | |
368 | @param This The USB_IO instance\r | |
369 | @param Endpoint The device endpoint\r | |
370 | @param IsNewTransfer Whether this is a new request, if it's old, remove\r | |
371 | the request\r | |
372 | @param PollInterval The interval to poll the transfer result, (in ms)\r | |
373 | @param DataLength The length of perodic data transfer\r | |
374 | @param Callback The function to call periodicaly when transfer is\r | |
375 | ready\r | |
376 | @param Context The context to the callback\r | |
377 | \r | |
378 | @retval EFI_SUCCESS New transfer is queued or old request is removed\r | |
379 | @retval EFI_INVALID_PARAMETER Some parameters are invalid\r | |
380 | @retval Others Failed to queue the new request or remove the old\r | |
381 | request\r | |
382 | \r | |
383 | **/\r | |
384 | STATIC\r | |
385 | EFI_STATUS\r | |
386 | EFIAPI\r | |
387 | UsbIoAsyncInterruptTransfer (\r | |
388 | IN EFI_USB_IO_PROTOCOL *This,\r | |
389 | IN UINT8 Endpoint,\r | |
390 | IN BOOLEAN IsNewTransfer,\r | |
391 | IN UINTN PollInterval, OPTIONAL\r | |
392 | IN UINTN DataLength, OPTIONAL\r | |
393 | IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback, OPTIONAL\r | |
394 | IN VOID *Context OPTIONAL\r | |
395 | )\r | |
396 | {\r | |
397 | USB_DEVICE *Dev;\r | |
398 | USB_INTERFACE *UsbIf;\r | |
399 | USB_ENDPOINT_DESC *EpDesc;\r | |
400 | EFI_TPL OldTpl;\r | |
401 | UINT8 Toggle;\r | |
402 | EFI_STATUS Status;\r | |
403 | \r | |
404 | if ((USB_ENDPOINT_ADDR (Endpoint) == 0) || (USB_ENDPOINT_ADDR (Endpoint) > 15)) {\r | |
405 | return EFI_INVALID_PARAMETER;\r | |
406 | }\r | |
407 | \r | |
408 | OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r | |
409 | UsbIf = USB_INTERFACE_FROM_USBIO (This);\r | |
410 | Dev = UsbIf->Device;\r | |
411 | \r | |
412 | EpDesc = UsbGetEndpointDesc (UsbIf, Endpoint);\r | |
413 | \r | |
414 | if ((EpDesc == NULL) || (USB_ENDPOINT_TYPE (&EpDesc->Desc) != USB_ENDPOINT_INTERRUPT)) {\r | |
415 | Status = EFI_INVALID_PARAMETER;\r | |
416 | goto ON_EXIT;\r | |
417 | }\r | |
418 | \r | |
419 | Toggle = EpDesc->Toggle;\r | |
420 | Status = UsbHcAsyncInterruptTransfer (\r | |
421 | Dev->Bus,\r | |
422 | Dev->Address,\r | |
423 | Endpoint,\r | |
424 | Dev->Speed,\r | |
425 | EpDesc->Desc.MaxPacketSize,\r | |
426 | IsNewTransfer,\r | |
427 | &Toggle,\r | |
428 | PollInterval,\r | |
429 | DataLength,\r | |
430 | &Dev->Translator,\r | |
431 | Callback,\r | |
432 | Context\r | |
433 | );\r | |
434 | \r | |
435 | EpDesc->Toggle = Toggle;\r | |
436 | \r | |
437 | ON_EXIT:\r | |
438 | gBS->RestoreTPL (OldTpl);\r | |
439 | return Status;\r | |
440 | }\r | |
441 | \r | |
442 | \r | |
443 | /**\r | |
444 | Execute a synchronous isochronous transfer\r | |
445 | \r | |
446 | @param This The USB IO instance\r | |
447 | @param DeviceEndpoint The device endpoint\r | |
448 | @param Data The data to transfer\r | |
449 | @param DataLength The length of the data to transfer\r | |
450 | @param UsbStatus The result of USB transfer\r | |
451 | \r | |
452 | @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported\r | |
453 | \r | |
454 | **/\r | |
455 | STATIC\r | |
456 | EFI_STATUS\r | |
457 | EFIAPI\r | |
458 | UsbIoIsochronousTransfer (\r | |
459 | IN EFI_USB_IO_PROTOCOL *This,\r | |
460 | IN UINT8 DeviceEndpoint,\r | |
461 | IN OUT VOID *Data,\r | |
462 | IN UINTN DataLength,\r | |
463 | OUT UINT32 *Status\r | |
464 | )\r | |
465 | {\r | |
466 | return EFI_UNSUPPORTED;\r | |
467 | }\r | |
468 | \r | |
469 | \r | |
470 | /**\r | |
471 | Queue an asynchronous isochronous transfer\r | |
472 | \r | |
473 | @param This The USB_IO instance\r | |
474 | @param DeviceEndpoint The device endpoint\r | |
475 | @param DataLength The length of perodic data transfer\r | |
476 | @param IsochronousCallBack The function to call periodicaly when transfer is\r | |
477 | ready\r | |
478 | @param Context The context to the callback\r | |
479 | \r | |
480 | @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported\r | |
481 | \r | |
482 | **/\r | |
483 | STATIC\r | |
484 | EFI_STATUS\r | |
485 | EFIAPI\r | |
486 | UsbIoAsyncIsochronousTransfer (\r | |
487 | IN EFI_USB_IO_PROTOCOL *This,\r | |
488 | IN UINT8 DeviceEndpoint,\r | |
489 | IN OUT VOID *Data,\r | |
490 | IN UINTN DataLength,\r | |
491 | IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack,\r | |
492 | IN VOID *Context OPTIONAL\r | |
493 | )\r | |
494 | {\r | |
495 | return EFI_UNSUPPORTED;\r | |
496 | }\r | |
497 | \r | |
498 | \r | |
499 | /**\r | |
500 | Retrieve the device descriptor of the device\r | |
501 | \r | |
502 | @param This The USB IO instance\r | |
503 | @param Descriptor The variable to receive the device descriptor\r | |
504 | \r | |
505 | @retval EFI_SUCCESS The device descriptor is returned\r | |
506 | @retval EFI_INVALID_PARAMETER The parameter is invalid\r | |
507 | \r | |
508 | **/\r | |
509 | STATIC\r | |
510 | EFI_STATUS\r | |
511 | EFIAPI\r | |
512 | UsbIoGetDeviceDescriptor (\r | |
513 | IN EFI_USB_IO_PROTOCOL *This,\r | |
514 | OUT EFI_USB_DEVICE_DESCRIPTOR *Descriptor\r | |
515 | )\r | |
516 | {\r | |
517 | USB_DEVICE *Dev;\r | |
518 | USB_INTERFACE *UsbIf;\r | |
519 | EFI_TPL OldTpl;\r | |
520 | \r | |
521 | if (Descriptor == NULL) {\r | |
522 | return EFI_INVALID_PARAMETER;\r | |
523 | }\r | |
524 | \r | |
525 | OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r | |
526 | \r | |
527 | UsbIf = USB_INTERFACE_FROM_USBIO (This);\r | |
528 | Dev = UsbIf->Device;\r | |
529 | \r | |
530 | CopyMem (Descriptor, &Dev->DevDesc->Desc, sizeof (EFI_USB_DEVICE_DESCRIPTOR));\r | |
531 | \r | |
532 | gBS->RestoreTPL (OldTpl);\r | |
533 | return EFI_SUCCESS;\r | |
534 | }\r | |
535 | \r | |
536 | \r | |
537 | /**\r | |
538 | Return the configuration descriptor of the current active configuration\r | |
539 | \r | |
540 | @param This The USB IO instance\r | |
541 | @param Descriptor The USB configuration descriptor\r | |
542 | \r | |
543 | @retval EFI_SUCCESS The active configuration descriptor is returned\r | |
544 | @retval EFI_INVALID_PARAMETER Some parameter is invalid\r | |
545 | @retval EFI_NOT_FOUND Currently no active configuration is selected.\r | |
546 | \r | |
547 | **/\r | |
548 | STATIC\r | |
549 | EFI_STATUS\r | |
550 | EFIAPI\r | |
551 | UsbIoGetActiveConfigDescriptor (\r | |
552 | IN EFI_USB_IO_PROTOCOL *This,\r | |
553 | OUT EFI_USB_CONFIG_DESCRIPTOR *Descriptor\r | |
554 | )\r | |
555 | {\r | |
556 | USB_DEVICE *Dev;\r | |
557 | USB_INTERFACE *UsbIf;\r | |
558 | EFI_STATUS Status;\r | |
559 | EFI_TPL OldTpl;\r | |
560 | \r | |
561 | if (Descriptor == NULL) {\r | |
562 | return EFI_INVALID_PARAMETER;\r | |
563 | }\r | |
564 | \r | |
565 | Status = EFI_SUCCESS;\r | |
566 | OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r | |
567 | \r | |
568 | UsbIf = USB_INTERFACE_FROM_USBIO (This);\r | |
569 | Dev = UsbIf->Device;\r | |
570 | \r | |
571 | if (Dev->ActiveConfig == NULL) {\r | |
572 | Status = EFI_NOT_FOUND;\r | |
573 | goto ON_EXIT;\r | |
574 | }\r | |
575 | \r | |
576 | CopyMem (Descriptor, &(Dev->ActiveConfig->Desc), sizeof (EFI_USB_CONFIG_DESCRIPTOR));\r | |
577 | \r | |
578 | ON_EXIT:\r | |
579 | gBS->RestoreTPL (OldTpl);\r | |
580 | return Status;\r | |
581 | }\r | |
582 | \r | |
583 | \r | |
584 | /**\r | |
585 | Retrieve the active interface setting descriptor for this USB IO instance\r | |
586 | \r | |
587 | @param This The USB IO instance\r | |
588 | @param Descriptor The variable to receive active interface setting\r | |
589 | \r | |
590 | @retval EFI_SUCCESS The active interface setting is returned\r | |
591 | @retval EFI_INVALID_PARAMETER Some parameter is invalid\r | |
592 | \r | |
593 | **/\r | |
594 | STATIC\r | |
595 | EFI_STATUS\r | |
596 | EFIAPI\r | |
597 | UsbIoGetInterfaceDescriptor (\r | |
598 | IN EFI_USB_IO_PROTOCOL *This,\r | |
599 | OUT EFI_USB_INTERFACE_DESCRIPTOR *Descriptor\r | |
600 | )\r | |
601 | {\r | |
602 | USB_INTERFACE *UsbIf;\r | |
603 | EFI_TPL OldTpl;\r | |
604 | \r | |
605 | if (Descriptor == NULL) {\r | |
606 | return EFI_INVALID_PARAMETER;\r | |
607 | }\r | |
608 | \r | |
609 | OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r | |
610 | \r | |
611 | UsbIf = USB_INTERFACE_FROM_USBIO (This);\r | |
612 | CopyMem (Descriptor, &(UsbIf->IfSetting->Desc), sizeof (EFI_USB_INTERFACE_DESCRIPTOR));\r | |
613 | \r | |
614 | gBS->RestoreTPL (OldTpl);\r | |
615 | return EFI_SUCCESS;\r | |
616 | }\r | |
617 | \r | |
618 | \r | |
619 | /**\r | |
620 | Retrieve the endpoint descriptor from this interface setting\r | |
621 | \r | |
622 | @param This The USB IO instance\r | |
623 | @param Index The index (start from zero) of the endpoint to\r | |
624 | retrieve\r | |
625 | @param Descriptor The variable to receive the descriptor\r | |
626 | \r | |
627 | @retval EFI_SUCCESS The endpoint descriptor is returned\r | |
628 | @retval EFI_INVALID_PARAMETER Some parameter is invalid\r | |
629 | \r | |
630 | **/\r | |
631 | STATIC\r | |
632 | EFI_STATUS\r | |
633 | EFIAPI\r | |
634 | UsbIoGetEndpointDescriptor (\r | |
635 | IN EFI_USB_IO_PROTOCOL *This,\r | |
636 | IN UINT8 Index,\r | |
637 | OUT EFI_USB_ENDPOINT_DESCRIPTOR *Descriptor\r | |
638 | )\r | |
639 | {\r | |
640 | USB_INTERFACE *UsbIf;\r | |
641 | EFI_TPL OldTpl;\r | |
642 | \r | |
643 | OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r | |
644 | \r | |
645 | UsbIf = USB_INTERFACE_FROM_USBIO (This);\r | |
646 | \r | |
50fa1b3a | 647 | if ((Descriptor == NULL) || (Index > 15)) {\r |
e237e7ae | 648 | gBS->RestoreTPL (OldTpl);\r |
649 | return EFI_INVALID_PARAMETER;\r | |
650 | }\r | |
651 | \r | |
50fa1b3a | 652 | if (Index >= UsbIf->IfSetting->Desc.NumEndpoints) {\r |
653 | gBS->RestoreTPL (OldTpl);\r | |
654 | return EFI_NOT_FOUND;\r | |
655 | }\r | |
656 | \r | |
e237e7ae | 657 | CopyMem (\r |
658 | Descriptor,\r | |
659 | &(UsbIf->IfSetting->Endpoints[Index]->Desc),\r | |
660 | sizeof (EFI_USB_ENDPOINT_DESCRIPTOR)\r | |
661 | );\r | |
662 | \r | |
663 | gBS->RestoreTPL (OldTpl);\r | |
664 | return EFI_SUCCESS;\r | |
665 | }\r | |
666 | \r | |
667 | \r | |
668 | /**\r | |
669 | Retrieve the supported language ID table from the device\r | |
670 | \r | |
671 | @param This The USB IO instance\r | |
672 | @param LangIDTable The table to return the language IDs\r | |
673 | @param TableSize The number of supported languanges\r | |
674 | \r | |
675 | @retval EFI_SUCCESS The language ID is return\r | |
676 | \r | |
677 | **/\r | |
678 | STATIC\r | |
679 | EFI_STATUS\r | |
680 | EFIAPI\r | |
681 | UsbIoGetSupportedLanguages (\r | |
682 | IN EFI_USB_IO_PROTOCOL *This,\r | |
683 | OUT UINT16 **LangIDTable,\r | |
684 | OUT UINT16 *TableSize\r | |
685 | )\r | |
686 | {\r | |
687 | USB_DEVICE *Dev;\r | |
688 | USB_INTERFACE *UsbIf;\r | |
689 | EFI_TPL OldTpl;\r | |
690 | \r | |
691 | OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r | |
692 | \r | |
693 | UsbIf = USB_INTERFACE_FROM_USBIO (This);\r | |
694 | Dev = UsbIf->Device;\r | |
695 | \r | |
696 | *LangIDTable = Dev->LangId;\r | |
697 | *TableSize = Dev->TotalLangId;\r | |
698 | \r | |
699 | gBS->RestoreTPL (OldTpl);\r | |
700 | return EFI_SUCCESS;\r | |
701 | }\r | |
702 | \r | |
703 | \r | |
704 | /**\r | |
705 | Retrieve an indexed string in the language of LangID\r | |
706 | \r | |
707 | @param This The USB IO instance\r | |
708 | @param LangID The language ID of the string to retrieve\r | |
709 | @param StringIndex The index of the string\r | |
710 | @param String The variable to receive the string\r | |
711 | \r | |
712 | @retval EFI_SUCCESS The string is returned\r | |
713 | @retval EFI_NOT_FOUND No such string existed\r | |
714 | \r | |
715 | **/\r | |
716 | STATIC\r | |
717 | EFI_STATUS\r | |
718 | EFIAPI\r | |
719 | UsbIoGetStringDescriptor (\r | |
720 | IN EFI_USB_IO_PROTOCOL *This,\r | |
721 | IN UINT16 LangID,\r | |
722 | IN UINT8 StringIndex,\r | |
723 | OUT CHAR16 **String\r | |
724 | )\r | |
725 | {\r | |
726 | USB_DEVICE *Dev;\r | |
727 | USB_INTERFACE *UsbIf;\r | |
728 | EFI_USB_STRING_DESCRIPTOR *StrDesc;\r | |
729 | EFI_TPL OldTpl;\r | |
730 | UINT8 *Buf;\r | |
731 | UINT8 Index;\r | |
732 | EFI_STATUS Status;\r | |
733 | \r | |
734 | if ((StringIndex == 0) || (LangID == 0)) {\r | |
735 | return EFI_NOT_FOUND;\r | |
736 | }\r | |
737 | \r | |
738 | OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r | |
739 | \r | |
740 | UsbIf = USB_INTERFACE_FROM_USBIO (This);\r | |
741 | Dev = UsbIf->Device;\r | |
742 | \r | |
743 | //\r | |
744 | // Check whether language ID is supported\r | |
745 | //\r | |
746 | Status = EFI_NOT_FOUND;\r | |
747 | \r | |
748 | for (Index = 0; Index < Dev->TotalLangId; Index++) {\r | |
749 | if (Dev->LangId[Index] == LangID) {\r | |
750 | break;\r | |
751 | }\r | |
752 | }\r | |
753 | \r | |
754 | if (Index == Dev->TotalLangId) {\r | |
755 | goto ON_EXIT;\r | |
756 | }\r | |
757 | \r | |
758 | //\r | |
759 | // Retrieve the string descriptor then allocate a buffer\r | |
760 | // to hold the string itself.\r | |
761 | //\r | |
762 | StrDesc = UsbGetOneString (Dev, StringIndex, LangID);\r | |
763 | \r | |
764 | if (StrDesc == NULL) {\r | |
765 | goto ON_EXIT;\r | |
766 | }\r | |
767 | \r | |
768 | if (StrDesc->Length <= 2) {\r | |
769 | goto FREE_STR;\r | |
770 | }\r | |
771 | \r | |
772 | Buf = AllocateZeroPool (StrDesc->Length);\r | |
773 | \r | |
774 | if (Buf == NULL) {\r | |
775 | Status = EFI_OUT_OF_RESOURCES;\r | |
776 | goto FREE_STR;\r | |
777 | }\r | |
778 | \r | |
779 | CopyMem (Buf, StrDesc->String, StrDesc->Length - 2);\r | |
780 | *String = (CHAR16 *) Buf;\r | |
781 | Status = EFI_SUCCESS;\r | |
782 | \r | |
783 | FREE_STR:\r | |
784 | gBS->FreePool (StrDesc);\r | |
785 | \r | |
786 | ON_EXIT:\r | |
787 | gBS->RestoreTPL (OldTpl);\r | |
788 | return Status;\r | |
789 | }\r | |
790 | \r | |
791 | \r | |
792 | /**\r | |
793 | Reset the device, then if that succeeds, reconfigure the\r | |
794 | device with its address and current active configuration.\r | |
795 | \r | |
796 | @param This The USB IO instance\r | |
797 | \r | |
798 | @retval EFI_SUCCESS The device is reset and configured\r | |
799 | @retval Others Failed to reset the device\r | |
800 | \r | |
801 | **/\r | |
802 | EFI_STATUS\r | |
803 | EFIAPI\r | |
804 | UsbIoPortReset (\r | |
805 | IN EFI_USB_IO_PROTOCOL *This\r | |
806 | )\r | |
807 | {\r | |
808 | USB_INTERFACE *UsbIf;\r | |
809 | USB_INTERFACE *HubIf;\r | |
810 | USB_DEVICE *Dev;\r | |
811 | UINT8 Address;\r | |
812 | EFI_TPL OldTpl;\r | |
813 | EFI_STATUS Status;\r | |
814 | \r | |
815 | OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r | |
816 | \r | |
817 | UsbIf = USB_INTERFACE_FROM_USBIO (This);\r | |
818 | Dev = UsbIf->Device;\r | |
819 | \r | |
50fa1b3a | 820 | if (UsbIf->IsHub == TRUE) {\r |
821 | Status = EFI_INVALID_PARAMETER;\r | |
822 | goto ON_EXIT;\r | |
823 | }\r | |
824 | \r | |
e237e7ae | 825 | HubIf = Dev->ParentIf;\r |
826 | Status = HubIf->HubApi->ResetPort (HubIf, Dev->ParentPort);\r | |
827 | \r | |
828 | if (EFI_ERROR (Status)) {\r | |
d2577026 | 829 | DEBUG (( EFI_D_ERROR, "UsbIoPortReset: failed to reset hub port %d@hub %d, %r \n",\r |
e237e7ae | 830 | Dev->ParentPort, Dev->ParentAddr, Status));\r |
831 | \r | |
832 | goto ON_EXIT;\r | |
833 | }\r | |
834 | \r | |
835 | //\r | |
836 | // Reset the device to its current address. The device now has a\r | |
837 | // address of ZERO, so need to set Dev->Address to zero first for\r | |
838 | // host to communicate with the device\r | |
839 | //\r | |
840 | Address = Dev->Address;\r | |
841 | Dev->Address = 0;\r | |
842 | Status = UsbSetAddress (Dev, Address);\r | |
843 | \r | |
844 | if (EFI_ERROR (Status)) {\r | |
d2577026 | 845 | DEBUG (( EFI_D_ERROR, "UsbIoPortReset: failed to set address for device %d - %r\n",\r |
e237e7ae | 846 | Address, Status));\r |
847 | \r | |
848 | goto ON_EXIT;\r | |
849 | }\r | |
850 | \r | |
851 | Dev->Address = Address;\r | |
852 | \r | |
853 | //\r | |
854 | // Reset the current active configure, after this device\r | |
855 | // is in CONFIGURED state.\r | |
856 | //\r | |
857 | if (Dev->ActiveConfig != NULL) {\r | |
858 | Status = UsbSetConfig (Dev, Dev->ActiveConfig->Desc.ConfigurationValue);\r | |
859 | \r | |
860 | if (EFI_ERROR (Status)) {\r | |
d2577026 | 861 | DEBUG (( EFI_D_ERROR, "UsbIoPortReset: failed to set configure for device %d - %r\n",\r |
e237e7ae | 862 | Address, Status));\r |
863 | }\r | |
864 | }\r | |
865 | \r | |
866 | ON_EXIT:\r | |
867 | gBS->RestoreTPL (OldTpl);\r | |
868 | return Status;\r | |
869 | }\r | |
870 | \r | |
871 | EFI_USB_IO_PROTOCOL mUsbIoProtocol = {\r | |
872 | UsbIoControlTransfer,\r | |
873 | UsbIoBulkTransfer,\r | |
874 | UsbIoAsyncInterruptTransfer,\r | |
875 | UsbIoSyncInterruptTransfer,\r | |
876 | UsbIoIsochronousTransfer,\r | |
877 | UsbIoAsyncIsochronousTransfer,\r | |
878 | UsbIoGetDeviceDescriptor,\r | |
879 | UsbIoGetActiveConfigDescriptor,\r | |
880 | UsbIoGetInterfaceDescriptor,\r | |
881 | UsbIoGetEndpointDescriptor,\r | |
882 | UsbIoGetStringDescriptor,\r | |
883 | UsbIoGetSupportedLanguages,\r | |
884 | UsbIoPortReset\r | |
885 | };\r | |
886 | \r | |
e237e7ae | 887 | \r |
888 | EFI_STATUS\r | |
889 | EFIAPI\r | |
890 | UsbBusDriverEntryPoint (\r | |
891 | IN EFI_HANDLE ImageHandle,\r | |
892 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
893 | )\r | |
894 | /*++\r | |
895 | \r | |
896 | Routine Description:\r | |
897 | \r | |
898 | The USB bus driver entry pointer\r | |
899 | \r | |
900 | Arguments:\r | |
901 | \r | |
902 | ImageHandle - The driver image handle\r | |
903 | SystemTable - The system table\r | |
904 | \r | |
905 | Returns:\r | |
906 | \r | |
907 | EFI_SUCCESS - The component name protocol is installed\r | |
908 | Others - Failed to init the usb driver\r | |
909 | \r | |
910 | --*/\r | |
911 | {\r | |
912 | return EfiLibInstallAllDriverProtocols (\r | |
913 | ImageHandle,\r | |
914 | SystemTable,\r | |
915 | &mUsbBusDriverBinding,\r | |
916 | ImageHandle,\r | |
917 | &mUsbBusComponentName,\r | |
918 | NULL,\r | |
919 | NULL\r | |
920 | );\r | |
921 | }\r | |
922 | \r | |
923 | \r | |
924 | /**\r | |
925 | Check whether USB bus driver support this device\r | |
926 | \r | |
927 | @param This The USB bus driver binding protocol\r | |
928 | @param Controller The controller handle to test againist\r | |
929 | @param RemainingDevicePath The remaining device path\r | |
930 | \r | |
931 | @retval EFI_SUCCESS The bus supports this controller.\r | |
932 | @retval EFI_UNSUPPORTED This device isn't supported\r | |
933 | \r | |
934 | **/\r | |
935 | EFI_STATUS\r | |
936 | EFIAPI\r | |
937 | UsbBusControllerDriverSupported (\r | |
938 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
939 | IN EFI_HANDLE Controller,\r | |
940 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r | |
941 | )\r | |
942 | {\r | |
943 | EFI_DEV_PATH_PTR DevicePathNode;\r | |
944 | EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r | |
945 | EFI_USB2_HC_PROTOCOL *Usb2Hc;\r | |
946 | EFI_USB_HC_PROTOCOL *UsbHc;\r | |
947 | EFI_STATUS Status;\r | |
948 | \r | |
949 | //\r | |
950 | // Check whether device path is valid\r | |
951 | //\r | |
952 | if (RemainingDevicePath != NULL) {\r | |
953 | DevicePathNode.DevPath = RemainingDevicePath;\r | |
954 | \r | |
955 | if ((DevicePathNode.DevPath->Type != MESSAGING_DEVICE_PATH) ||\r | |
956 | (DevicePathNode.DevPath->SubType != MSG_USB_DP) ||\r | |
957 | (DevicePathNodeLength (DevicePathNode.DevPath) != sizeof (USB_DEVICE_PATH))) {\r | |
958 | \r | |
959 | return EFI_UNSUPPORTED;\r | |
960 | }\r | |
961 | }\r | |
962 | \r | |
963 | Status = gBS->OpenProtocol (\r | |
964 | Controller,\r | |
965 | &gEfiDevicePathProtocolGuid,\r | |
966 | (VOID **) &ParentDevicePath,\r | |
967 | This->DriverBindingHandle,\r | |
968 | Controller,\r | |
969 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
970 | );\r | |
971 | \r | |
972 | if (Status == EFI_ALREADY_STARTED) {\r | |
973 | return EFI_SUCCESS;\r | |
974 | }\r | |
975 | \r | |
976 | if (EFI_ERROR (Status)) {\r | |
977 | return Status;\r | |
978 | }\r | |
979 | \r | |
980 | gBS->CloseProtocol (\r | |
981 | Controller,\r | |
982 | &gEfiDevicePathProtocolGuid,\r | |
983 | This->DriverBindingHandle,\r | |
984 | Controller\r | |
985 | );\r | |
986 | \r | |
987 | //\r | |
988 | // Check whether USB_HC2 protocol is installed\r | |
989 | //\r | |
990 | Status = gBS->OpenProtocol (\r | |
991 | Controller,\r | |
992 | &gEfiUsb2HcProtocolGuid,\r | |
993 | (VOID **) &Usb2Hc,\r | |
994 | This->DriverBindingHandle,\r | |
995 | Controller,\r | |
996 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
997 | );\r | |
998 | \r | |
999 | if (Status == EFI_ALREADY_STARTED) {\r | |
1000 | return EFI_SUCCESS;\r | |
1001 | }\r | |
1002 | \r | |
1003 | if (!EFI_ERROR (Status)) {\r | |
1004 | gBS->CloseProtocol (\r | |
1005 | Controller,\r | |
1006 | &gEfiUsb2HcProtocolGuid,\r | |
1007 | This->DriverBindingHandle,\r | |
1008 | Controller\r | |
1009 | );\r | |
1010 | \r | |
1011 | return EFI_SUCCESS;\r | |
1012 | }\r | |
1013 | \r | |
1014 | //\r | |
1015 | // If failed to open USB_HC2, fall back to USB_HC\r | |
1016 | //\r | |
1017 | Status = gBS->OpenProtocol (\r | |
1018 | Controller,\r | |
1019 | &gEfiUsbHcProtocolGuid,\r | |
1020 | (VOID **) &UsbHc,\r | |
1021 | This->DriverBindingHandle,\r | |
1022 | Controller,\r | |
1023 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
1024 | );\r | |
1025 | \r | |
1026 | if (Status == EFI_ALREADY_STARTED) {\r | |
1027 | return EFI_SUCCESS;\r | |
1028 | }\r | |
1029 | \r | |
1030 | if (!EFI_ERROR (Status)) {\r | |
1031 | gBS->CloseProtocol (\r | |
1032 | Controller,\r | |
1033 | &gEfiUsbHcProtocolGuid,\r | |
1034 | This->DriverBindingHandle,\r | |
1035 | Controller\r | |
1036 | );\r | |
1037 | }\r | |
1038 | \r | |
1039 | return Status;\r | |
1040 | }\r | |
1041 | \r | |
1042 | \r | |
1043 | /**\r | |
1044 | Start to process the controller\r | |
1045 | \r | |
1046 | @param This The USB bus driver binding instance\r | |
1047 | @param Controller The controller to check\r | |
1048 | @param RemainingDevicePath The remaining device patch\r | |
1049 | \r | |
1050 | @retval EFI_SUCCESS The controller is controlled by the usb bus\r | |
1051 | @retval EFI_ALREADY_STARTED The controller is already controlled by the usb\r | |
1052 | bus\r | |
1053 | @retval EFI_OUT_OF_RESOURCES Failed to allocate resources\r | |
1054 | \r | |
1055 | **/\r | |
1056 | EFI_STATUS\r | |
1057 | EFIAPI\r | |
1058 | UsbBusControllerDriverStart (\r | |
1059 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
1060 | IN EFI_HANDLE Controller,\r | |
1061 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r | |
1062 | )\r | |
1063 | {\r | |
1064 | USB_BUS *UsbBus;\r | |
1065 | USB_DEVICE *RootHub;\r | |
1066 | USB_INTERFACE *RootIf;\r | |
1067 | EFI_USB_BUS_PROTOCOL *UsbBusId;\r | |
1068 | EFI_STATUS Status;\r | |
1069 | EFI_STATUS Status2;\r | |
1070 | \r | |
1071 | //\r | |
1072 | // Locate the USB bus protocol, if it is found, USB bus\r | |
1073 | // is already started on this controller.\r | |
1074 | //\r | |
1075 | Status = gBS->OpenProtocol (\r | |
1076 | Controller,\r | |
1077 | &mUsbBusProtocolGuid,\r | |
1078 | (VOID **) &UsbBusId,\r | |
1079 | This->DriverBindingHandle,\r | |
1080 | Controller,\r | |
1081 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
1082 | );\r | |
1083 | \r | |
1084 | if (!EFI_ERROR (Status)) {\r | |
1085 | return EFI_ALREADY_STARTED;\r | |
1086 | }\r | |
1087 | \r | |
1088 | UsbBus = AllocateZeroPool (sizeof (USB_BUS));\r | |
1089 | \r | |
1090 | if (UsbBus == NULL) {\r | |
1091 | return EFI_OUT_OF_RESOURCES;\r | |
1092 | }\r | |
1093 | \r | |
1094 | UsbBus->Signature = USB_BUS_SIGNATURE;\r | |
1095 | UsbBus->HostHandle = Controller;\r | |
1096 | \r | |
1097 | Status = gBS->OpenProtocol (\r | |
1098 | Controller,\r | |
1099 | &gEfiDevicePathProtocolGuid,\r | |
1100 | (VOID **) &UsbBus->DevicePath,\r | |
1101 | This->DriverBindingHandle,\r | |
1102 | Controller,\r | |
1103 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
1104 | );\r | |
1105 | \r | |
1106 | if (EFI_ERROR (Status)) {\r | |
d2577026 | 1107 | DEBUG (( EFI_D_ERROR, "UsbBusStart: Failed to open device path %r\n", Status));\r |
e237e7ae | 1108 | \r |
1109 | gBS->FreePool (UsbBus);\r | |
1110 | return Status;\r | |
1111 | }\r | |
1112 | \r | |
1113 | //\r | |
1114 | // Get USB_HC2/USB_HC host controller protocol (EHCI/UHCI).\r | |
1115 | // This is for backward compatbility with EFI 1.x. In UEFI\r | |
1116 | // 2.x, USB_HC2 replaces USB_HC. We will open both USB_HC2\r | |
1117 | // and USB_HC because EHCI driver will install both protocols\r | |
1118 | // (for the same reason). If we don't consume both of them,\r | |
1119 | // the unconsumed one may be opened by others.\r | |
1120 | //\r | |
1121 | Status = gBS->OpenProtocol (\r | |
1122 | Controller,\r | |
1123 | &gEfiUsb2HcProtocolGuid,\r | |
1124 | (VOID **) &(UsbBus->Usb2Hc),\r | |
1125 | This->DriverBindingHandle,\r | |
1126 | Controller,\r | |
1127 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
1128 | );\r | |
1129 | \r | |
1130 | Status2 = gBS->OpenProtocol (\r | |
1131 | Controller,\r | |
1132 | &gEfiUsbHcProtocolGuid,\r | |
1133 | (VOID **) &(UsbBus->UsbHc),\r | |
1134 | This->DriverBindingHandle,\r | |
1135 | Controller,\r | |
1136 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
1137 | );\r | |
1138 | \r | |
1139 | if (EFI_ERROR (Status) && EFI_ERROR (Status2)) {\r | |
d2577026 | 1140 | DEBUG (( EFI_D_ERROR, "UsbBusStart: Failed to open USB_HC/USB2_HC %r\n", Status));\r |
e237e7ae | 1141 | \r |
1142 | Status = EFI_DEVICE_ERROR;\r | |
1143 | goto CLOSE_HC;\r | |
1144 | }\r | |
1145 | \r | |
1146 | //\r | |
1147 | // Create a fake usb device for root hub\r | |
1148 | //\r | |
1149 | RootHub = AllocateZeroPool (sizeof (USB_DEVICE));\r | |
1150 | \r | |
1151 | if (RootHub == NULL) {\r | |
1152 | Status = EFI_OUT_OF_RESOURCES;\r | |
1153 | goto CLOSE_HC;\r | |
1154 | }\r | |
1155 | \r | |
1156 | RootIf = AllocateZeroPool (sizeof (USB_INTERFACE));\r | |
1157 | \r | |
1158 | if (RootIf == NULL) {\r | |
1159 | gBS->FreePool (RootHub);\r | |
1160 | Status = EFI_OUT_OF_RESOURCES;\r | |
1161 | goto CLOSE_HC;\r | |
1162 | }\r | |
1163 | \r | |
1164 | RootHub->Bus = UsbBus;\r | |
1165 | RootHub->NumOfInterface = 1;\r | |
1166 | RootHub->Interfaces[0] = RootIf;\r | |
1167 | RootIf->Signature = USB_INTERFACE_SIGNATURE;\r | |
1168 | RootIf->Device = RootHub;\r | |
1169 | RootIf->DevicePath = UsbBus->DevicePath;\r | |
1170 | \r | |
d2577026 | 1171 | \r |
1172 | UsbHcReset (UsbBus, EFI_USB_HC_RESET_GLOBAL);\r | |
1173 | UsbHcSetState (UsbBus, EfiUsbHcStateOperational);\r | |
1174 | \r | |
e237e7ae | 1175 | Status = mUsbRootHubApi.Init (RootIf);\r |
1176 | \r | |
1177 | if (EFI_ERROR (Status)) {\r | |
d2577026 | 1178 | DEBUG (( EFI_D_ERROR, "UsbBusStart: Failed to init root hub %r\n", Status));\r |
e237e7ae | 1179 | goto FREE_ROOTHUB;\r |
1180 | }\r | |
1181 | \r | |
1182 | UsbBus->Devices[0] = RootHub;\r | |
1183 | \r | |
1184 | //\r | |
1185 | // Install an EFI_USB_BUS_PROTOCOL to host controler to identify it.\r | |
1186 | //\r | |
1187 | Status = gBS->InstallProtocolInterface (\r | |
1188 | &Controller,\r | |
1189 | &mUsbBusProtocolGuid,\r | |
1190 | EFI_NATIVE_INTERFACE,\r | |
1191 | &UsbBus->BusId\r | |
1192 | );\r | |
1193 | \r | |
1194 | if (EFI_ERROR (Status)) {\r | |
d2577026 | 1195 | DEBUG (( EFI_D_ERROR, "UsbBusStart: Failed to install bus protocol %r\n", Status));\r |
e237e7ae | 1196 | \r |
1197 | mUsbRootHubApi.Release (RootIf);\r | |
1198 | goto FREE_ROOTHUB;\r | |
1199 | }\r | |
1200 | \r | |
e237e7ae | 1201 | \r |
d2577026 | 1202 | DEBUG (( EFI_D_INFO, "UsbBusStart: usb bus started on %x, root hub %x\n", Controller, RootIf));\r |
e237e7ae | 1203 | return EFI_SUCCESS;\r |
1204 | \r | |
1205 | FREE_ROOTHUB:\r | |
1206 | gBS->FreePool (RootIf);\r | |
1207 | gBS->FreePool (RootHub);\r | |
1208 | \r | |
1209 | CLOSE_HC:\r | |
1210 | if (UsbBus->Usb2Hc != NULL) {\r | |
1211 | gBS->CloseProtocol (\r | |
1212 | Controller,\r | |
1213 | &gEfiUsb2HcProtocolGuid,\r | |
1214 | This->DriverBindingHandle,\r | |
1215 | Controller\r | |
1216 | );\r | |
1217 | }\r | |
1218 | \r | |
1219 | if (UsbBus->UsbHc != NULL) {\r | |
1220 | gBS->CloseProtocol (\r | |
1221 | Controller,\r | |
1222 | &gEfiUsbHcProtocolGuid,\r | |
1223 | This->DriverBindingHandle,\r | |
1224 | Controller\r | |
1225 | );\r | |
1226 | }\r | |
1227 | \r | |
1228 | gBS->CloseProtocol (\r | |
1229 | Controller,\r | |
1230 | &gEfiDevicePathProtocolGuid,\r | |
1231 | This->DriverBindingHandle,\r | |
1232 | Controller\r | |
1233 | );\r | |
1234 | \r | |
1235 | gBS->FreePool (UsbBus);\r | |
1236 | \r | |
d2577026 | 1237 | DEBUG (( EFI_D_ERROR, "UsbBusStart: Failed to start bus driver %r\n", Status));\r |
e237e7ae | 1238 | return Status;\r |
1239 | }\r | |
1240 | \r | |
1241 | \r | |
1242 | /**\r | |
1243 | Stop handle the controller by this USB bus driver\r | |
1244 | \r | |
1245 | @param This The USB bus driver binding protocol\r | |
1246 | @param Controller The controller to release\r | |
1247 | @param NumberOfChildren The child of USB bus that opened controller\r | |
1248 | BY_CHILD\r | |
1249 | @param ChildHandleBuffer The array of child handle\r | |
1250 | \r | |
1251 | @retval EFI_SUCCESS The controller or children are stopped\r | |
1252 | @retval EFI_DEVICE_ERROR Failed to stop the driver\r | |
1253 | \r | |
1254 | **/\r | |
1255 | EFI_STATUS\r | |
1256 | EFIAPI\r | |
1257 | UsbBusControllerDriverStop (\r | |
1258 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
1259 | IN EFI_HANDLE Controller,\r | |
1260 | IN UINTN NumberOfChildren,\r | |
1261 | IN EFI_HANDLE *ChildHandleBuffer\r | |
1262 | )\r | |
1263 | {\r | |
1264 | USB_BUS *Bus;\r | |
1265 | USB_DEVICE *RootHub;\r | |
1266 | USB_DEVICE *UsbDev;\r | |
1267 | USB_INTERFACE *RootIf;\r | |
1268 | USB_INTERFACE *UsbIf;\r | |
1269 | EFI_USB_BUS_PROTOCOL *BusId;\r | |
1270 | EFI_USB_IO_PROTOCOL *UsbIo;\r | |
1271 | EFI_TPL OldTpl;\r | |
1272 | UINTN Index;\r | |
1273 | EFI_STATUS Status;\r | |
1274 | \r | |
1275 | Status = EFI_SUCCESS;\r | |
1276 | \r | |
1277 | if (NumberOfChildren > 0) {\r | |
1278 | OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r | |
1279 | \r | |
1280 | for (Index = 0; Index < NumberOfChildren; Index++) {\r | |
1281 | Status = gBS->OpenProtocol (\r | |
1282 | ChildHandleBuffer[Index],\r | |
1283 | &gEfiUsbIoProtocolGuid,\r | |
1284 | (VOID **) &UsbIo,\r | |
1285 | This->DriverBindingHandle,\r | |
1286 | Controller,\r | |
1287 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
1288 | );\r | |
1289 | \r | |
1290 | if (EFI_ERROR (Status)) {\r | |
1291 | //\r | |
1292 | // It is possible that the child has already been released:\r | |
1293 | // 1. For combo device, free one device will release others.\r | |
1294 | // 2. If a hub is released, all devices on its down facing\r | |
1295 | // ports are released also.\r | |
1296 | //\r | |
1297 | continue;\r | |
1298 | }\r | |
1299 | \r | |
1300 | UsbIf = USB_INTERFACE_FROM_USBIO (UsbIo);\r | |
1301 | UsbDev = UsbIf->Device;\r | |
1302 | \r | |
1303 | UsbRemoveDevice (UsbDev);\r | |
1304 | }\r | |
1305 | \r | |
1306 | gBS->RestoreTPL (OldTpl);\r | |
1307 | return EFI_SUCCESS;\r | |
1308 | }\r | |
1309 | \r | |
d2577026 | 1310 | DEBUG (( EFI_D_INFO, "UsbBusStop: usb bus stopped on %x\n", Controller));\r |
e237e7ae | 1311 | \r |
1312 | //\r | |
1313 | // Locate USB_BUS for the current host controller\r | |
1314 | //\r | |
1315 | Status = gBS->OpenProtocol (\r | |
1316 | Controller,\r | |
1317 | &mUsbBusProtocolGuid,\r | |
1318 | (VOID **) &BusId,\r | |
1319 | This->DriverBindingHandle,\r | |
1320 | Controller,\r | |
1321 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
1322 | );\r | |
1323 | \r | |
1324 | if (EFI_ERROR (Status)) {\r | |
1325 | return Status;\r | |
1326 | }\r | |
1327 | \r | |
1328 | Bus = USB_BUS_FROM_THIS (BusId);\r | |
1329 | \r | |
1330 | //\r | |
1331 | // Stop the root hub, then free all the devices\r | |
1332 | //\r | |
1333 | OldTpl = gBS->RaiseTPL (USB_BUS_TPL);\r | |
1334 | UsbHcSetState (Bus, EfiUsbHcStateHalt);\r | |
1335 | \r | |
1336 | RootHub = Bus->Devices[0];\r | |
1337 | RootIf = RootHub->Interfaces[0];\r | |
1338 | \r | |
1339 | mUsbRootHubApi.Release (RootIf);\r | |
1340 | \r | |
1341 | for (Index = 1; Index < USB_MAX_DEVICES; Index++) {\r | |
1342 | if (Bus->Devices[Index] != NULL) {\r | |
1343 | UsbRemoveDevice (Bus->Devices[Index]);\r | |
1344 | }\r | |
1345 | }\r | |
1346 | \r | |
1347 | gBS->RestoreTPL (OldTpl);\r | |
1348 | \r | |
1349 | gBS->FreePool (RootIf);\r | |
1350 | gBS->FreePool (RootHub);\r | |
1351 | \r | |
1352 | //\r | |
1353 | // Uninstall the bus identifier and close USB_HC/USB2_HC protocols\r | |
1354 | //\r | |
1355 | gBS->UninstallProtocolInterface (Controller, &mUsbBusProtocolGuid, &Bus->BusId);\r | |
1356 | \r | |
1357 | if (Bus->Usb2Hc != NULL) {\r | |
1358 | gBS->CloseProtocol (\r | |
1359 | Controller,\r | |
1360 | &gEfiUsb2HcProtocolGuid,\r | |
1361 | This->DriverBindingHandle,\r | |
1362 | Controller\r | |
1363 | );\r | |
1364 | }\r | |
1365 | \r | |
1366 | if (Bus->UsbHc != NULL) {\r | |
1367 | gBS->CloseProtocol (\r | |
1368 | Controller,\r | |
1369 | &gEfiUsbHcProtocolGuid,\r | |
1370 | This->DriverBindingHandle,\r | |
1371 | Controller\r | |
1372 | );\r | |
1373 | }\r | |
1374 | \r | |
1375 | gBS->CloseProtocol (\r | |
1376 | Controller,\r | |
1377 | &gEfiDevicePathProtocolGuid,\r | |
1378 | This->DriverBindingHandle,\r | |
1379 | Controller\r | |
1380 | );\r | |
1381 | \r | |
1382 | gBS->FreePool (Bus);\r | |
1383 | \r | |
1384 | return Status;\r | |
1385 | }\r | |
1386 | \r | |
1387 | EFI_DRIVER_BINDING_PROTOCOL mUsbBusDriverBinding = {\r | |
1388 | UsbBusControllerDriverSupported,\r | |
1389 | UsbBusControllerDriverStart,\r | |
1390 | UsbBusControllerDriverStop,\r | |
1391 | 0xa,\r | |
1392 | NULL,\r | |
1393 | NULL\r | |
1394 | };\r |