3 Usb Bus Driver Binding and Bus IO Protocol.
5 Copyright (c) 2004 - 2013, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 EFI_USB_IO_PROTOCOL mUsbIoProtocol
= {
21 UsbIoAsyncInterruptTransfer
,
22 UsbIoSyncInterruptTransfer
,
23 UsbIoIsochronousTransfer
,
24 UsbIoAsyncIsochronousTransfer
,
25 UsbIoGetDeviceDescriptor
,
26 UsbIoGetActiveConfigDescriptor
,
27 UsbIoGetInterfaceDescriptor
,
28 UsbIoGetEndpointDescriptor
,
29 UsbIoGetStringDescriptor
,
30 UsbIoGetSupportedLanguages
,
34 EFI_DRIVER_BINDING_PROTOCOL mUsbBusDriverBinding
= {
35 UsbBusControllerDriverSupported
,
36 UsbBusControllerDriverStart
,
37 UsbBusControllerDriverStop
,
44 USB_IO function to execute a control transfer. This
45 function will execute the USB transfer. If transfer
46 successes, it will sync the internal state of USB bus
49 @param This The USB_IO instance
50 @param Request The control transfer request
51 @param Direction Direction for data stage
52 @param Timeout The time to wait before timeout
53 @param Data The buffer holding the data
54 @param DataLength Then length of the data
55 @param UsbStatus USB result
57 @retval EFI_INVALID_PARAMETER The parameters are invalid
58 @retval EFI_SUCCESS The control transfer succeeded.
59 @retval Others Failed to execute the transfer
64 UsbIoControlTransfer (
65 IN EFI_USB_IO_PROTOCOL
*This
,
66 IN EFI_USB_DEVICE_REQUEST
*Request
,
67 IN EFI_USB_DATA_DIRECTION Direction
,
69 IN OUT VOID
*Data
, OPTIONAL
70 IN UINTN DataLength
, OPTIONAL
76 USB_ENDPOINT_DESC
*EpDesc
;
80 if (UsbStatus
== NULL
) {
81 return EFI_INVALID_PARAMETER
;
84 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
86 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
89 Status
= UsbHcControlTransfer (
103 if (EFI_ERROR (Status
) || (*UsbStatus
!= EFI_USB_NOERROR
)) {
105 // Clear TT buffer when CTRL/BULK split transaction failes
106 // Clear the TRANSLATOR TT buffer, not parent's buffer
108 ASSERT (Dev
->Translator
.TranslatorHubAddress
< Dev
->Bus
->MaxDevices
);
109 if (Dev
->Translator
.TranslatorHubAddress
!= 0) {
110 UsbHubCtrlClearTTBuffer (
111 Dev
->Bus
->Devices
[Dev
->Translator
.TranslatorHubAddress
],
112 Dev
->Translator
.TranslatorPortNumber
,
123 // Some control transfer will change the device's internal
124 // status, such as Set_Configuration and Set_Interface.
125 // We must synchronize the bus driver's status with that in
126 // device. We ignore the Set_Descriptor request because it's
127 // hardly used by any device, especially in pre-boot environment
131 // Reset the endpoint toggle when endpoint stall is cleared
133 if ((Request
->Request
== USB_REQ_CLEAR_FEATURE
) &&
134 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
,
135 USB_TARGET_ENDPOINT
)) &&
136 (Request
->Value
== USB_FEATURE_ENDPOINT_HALT
)) {
138 EpDesc
= UsbGetEndpointDesc (UsbIf
, (UINT8
) Request
->Index
);
140 if (EpDesc
!= NULL
) {
146 // Select a new configuration. This is a dangerous action. Upper driver
147 // should stop use its current UsbIo after calling this driver. The old
148 // UsbIo will be uninstalled and new UsbIo be installed. We can't use
149 // ReinstallProtocol since interfaces in different configuration may be
150 // completely irrelevant.
152 if ((Request
->Request
== USB_REQ_SET_CONFIG
) &&
153 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
,
154 USB_TARGET_DEVICE
))) {
156 // Don't re-create the USB interfaces if configuration isn't changed.
158 if ((Dev
->ActiveConfig
!= NULL
) &&
159 (Request
->Value
== Dev
->ActiveConfig
->Desc
.ConfigurationValue
)) {
163 DEBUG ((EFI_D_INFO
, "UsbIoControlTransfer: configure changed!!! Do NOT use old UsbIo!!!\n"));
165 if (Dev
->ActiveConfig
!= NULL
) {
166 UsbRemoveConfig (Dev
);
169 if (Request
->Value
!= 0) {
170 Status
= UsbSelectConfig (Dev
, (UINT8
) Request
->Value
);
174 // Exit now, Old USB_IO is invalid now
180 // A new alternative setting is selected for the interface.
181 // No need to reinstall UsbIo in this case because only
182 // underlying communication endpoints are changed. Functionality
183 // should remains the same.
185 if ((Request
->Request
== USB_REQ_SET_INTERFACE
) &&
186 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
,
187 USB_TARGET_INTERFACE
)) &&
188 (Request
->Index
== UsbIf
->IfSetting
->Desc
.InterfaceNumber
)) {
190 Status
= UsbSelectSetting (UsbIf
->IfDesc
, (UINT8
) Request
->Value
);
192 if (!EFI_ERROR (Status
)) {
193 ASSERT (UsbIf
->IfDesc
->ActiveIndex
< USB_MAX_INTERFACE_SETTING
);
194 UsbIf
->IfSetting
= UsbIf
->IfDesc
->Settings
[UsbIf
->IfDesc
->ActiveIndex
];
199 gBS
->RestoreTPL (OldTpl
);
205 Execute a bulk transfer to the device endpoint.
207 @param This The USB IO instance.
208 @param Endpoint The device endpoint.
209 @param Data The data to transfer.
210 @param DataLength The length of the data to transfer.
211 @param Timeout Time to wait before timeout.
212 @param UsbStatus The result of USB transfer.
214 @retval EFI_SUCCESS The bulk transfer is OK.
215 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
216 @retval Others Failed to execute transfer, reason returned in
223 IN EFI_USB_IO_PROTOCOL
*This
,
226 IN OUT UINTN
*DataLength
,
228 OUT UINT32
*UsbStatus
232 USB_INTERFACE
*UsbIf
;
233 USB_ENDPOINT_DESC
*EpDesc
;
239 if ((USB_ENDPOINT_ADDR (Endpoint
) == 0) || (USB_ENDPOINT_ADDR(Endpoint
) > 15) ||
240 (UsbStatus
== NULL
)) {
242 return EFI_INVALID_PARAMETER
;
245 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
247 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
250 EpDesc
= UsbGetEndpointDesc (UsbIf
, Endpoint
);
252 if ((EpDesc
== NULL
) || (USB_ENDPOINT_TYPE (&EpDesc
->Desc
) != USB_ENDPOINT_BULK
)) {
253 Status
= EFI_INVALID_PARAMETER
;
258 Toggle
= EpDesc
->Toggle
;
259 Status
= UsbHcBulkTransfer (
264 EpDesc
->Desc
.MaxPacketSize
,
274 EpDesc
->Toggle
= Toggle
;
276 if (EFI_ERROR (Status
) || (*UsbStatus
!= EFI_USB_NOERROR
)) {
278 // Clear TT buffer when CTRL/BULK split transaction failes.
279 // Clear the TRANSLATOR TT buffer, not parent's buffer
281 ASSERT (Dev
->Translator
.TranslatorHubAddress
< Dev
->Bus
->MaxDevices
);
282 if (Dev
->Translator
.TranslatorHubAddress
!= 0) {
283 UsbHubCtrlClearTTBuffer (
284 Dev
->Bus
->Devices
[Dev
->Translator
.TranslatorHubAddress
],
285 Dev
->Translator
.TranslatorPortNumber
,
294 gBS
->RestoreTPL (OldTpl
);
300 Execute a synchronous interrupt transfer.
302 @param This The USB IO instance.
303 @param Endpoint The device endpoint.
304 @param Data The data to transfer.
305 @param DataLength The length of the data to transfer.
306 @param Timeout Time to wait before timeout.
307 @param UsbStatus The result of USB transfer.
309 @retval EFI_SUCCESS The synchronous interrupt transfer is OK.
310 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
311 @retval Others Failed to execute transfer, reason returned in
317 UsbIoSyncInterruptTransfer (
318 IN EFI_USB_IO_PROTOCOL
*This
,
321 IN OUT UINTN
*DataLength
,
323 OUT UINT32
*UsbStatus
327 USB_INTERFACE
*UsbIf
;
328 USB_ENDPOINT_DESC
*EpDesc
;
333 if ((USB_ENDPOINT_ADDR (Endpoint
) == 0) || (USB_ENDPOINT_ADDR(Endpoint
) > 15) ||
334 (UsbStatus
== NULL
)) {
336 return EFI_INVALID_PARAMETER
;
339 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
341 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
344 EpDesc
= UsbGetEndpointDesc (UsbIf
, Endpoint
);
346 if ((EpDesc
== NULL
) || (USB_ENDPOINT_TYPE (&EpDesc
->Desc
) != USB_ENDPOINT_INTERRUPT
)) {
347 Status
= EFI_INVALID_PARAMETER
;
351 Toggle
= EpDesc
->Toggle
;
352 Status
= UsbHcSyncInterruptTransfer (
357 EpDesc
->Desc
.MaxPacketSize
,
366 EpDesc
->Toggle
= Toggle
;
369 gBS
->RestoreTPL (OldTpl
);
375 Queue a new asynchronous interrupt transfer, or remove the old
376 request if (IsNewTransfer == FALSE).
378 @param This The USB_IO instance.
379 @param Endpoint The device endpoint.
380 @param IsNewTransfer Whether this is a new request, if it's old, remove
382 @param PollInterval The interval to poll the transfer result, (in ms).
383 @param DataLength The length of perodic data transfer.
384 @param Callback The function to call periodicaly when transfer is
386 @param Context The context to the callback.
388 @retval EFI_SUCCESS New transfer is queued or old request is removed.
389 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
390 @retval Others Failed to queue the new request or remove the old
396 UsbIoAsyncInterruptTransfer (
397 IN EFI_USB_IO_PROTOCOL
*This
,
399 IN BOOLEAN IsNewTransfer
,
400 IN UINTN PollInterval
, OPTIONAL
401 IN UINTN DataLength
, OPTIONAL
402 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
, OPTIONAL
403 IN VOID
*Context OPTIONAL
407 USB_INTERFACE
*UsbIf
;
408 USB_ENDPOINT_DESC
*EpDesc
;
413 if ((USB_ENDPOINT_ADDR (Endpoint
) == 0) || (USB_ENDPOINT_ADDR (Endpoint
) > 15)) {
414 return EFI_INVALID_PARAMETER
;
417 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
418 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
421 EpDesc
= UsbGetEndpointDesc (UsbIf
, Endpoint
);
423 if ((EpDesc
== NULL
) || (USB_ENDPOINT_TYPE (&EpDesc
->Desc
) != USB_ENDPOINT_INTERRUPT
)) {
424 Status
= EFI_INVALID_PARAMETER
;
428 Toggle
= EpDesc
->Toggle
;
429 Status
= UsbHcAsyncInterruptTransfer (
434 EpDesc
->Desc
.MaxPacketSize
,
444 EpDesc
->Toggle
= Toggle
;
447 gBS
->RestoreTPL (OldTpl
);
453 Execute a synchronous isochronous transfer.
455 @param This The USB IO instance.
456 @param DeviceEndpoint The device endpoint.
457 @param Data The data to transfer.
458 @param DataLength The length of the data to transfer.
459 @param UsbStatus The result of USB transfer.
461 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported.
466 UsbIoIsochronousTransfer (
467 IN EFI_USB_IO_PROTOCOL
*This
,
468 IN UINT8 DeviceEndpoint
,
474 return EFI_UNSUPPORTED
;
479 Queue an asynchronous isochronous transfer.
481 @param This The USB_IO instance.
482 @param DeviceEndpoint The device endpoint.
483 @param Data The data to transfer.
484 @param DataLength The length of perodic data transfer.
485 @param IsochronousCallBack The function to call periodicaly when transfer is
487 @param Context The context to the callback.
489 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported.
494 UsbIoAsyncIsochronousTransfer (
495 IN EFI_USB_IO_PROTOCOL
*This
,
496 IN UINT8 DeviceEndpoint
,
499 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
500 IN VOID
*Context OPTIONAL
503 return EFI_UNSUPPORTED
;
508 Retrieve the device descriptor of the device.
510 @param This The USB IO instance.
511 @param Descriptor The variable to receive the device descriptor.
513 @retval EFI_SUCCESS The device descriptor is returned.
514 @retval EFI_INVALID_PARAMETER The parameter is invalid.
519 UsbIoGetDeviceDescriptor (
520 IN EFI_USB_IO_PROTOCOL
*This
,
521 OUT EFI_USB_DEVICE_DESCRIPTOR
*Descriptor
525 USB_INTERFACE
*UsbIf
;
528 if (Descriptor
== NULL
) {
529 return EFI_INVALID_PARAMETER
;
532 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
534 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
537 CopyMem (Descriptor
, &Dev
->DevDesc
->Desc
, sizeof (EFI_USB_DEVICE_DESCRIPTOR
));
539 gBS
->RestoreTPL (OldTpl
);
545 Return the configuration descriptor of the current active configuration.
547 @param This The USB IO instance.
548 @param Descriptor The USB configuration descriptor.
550 @retval EFI_SUCCESS The active configuration descriptor is returned.
551 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
552 @retval EFI_NOT_FOUND Currently no active configuration is selected.
557 UsbIoGetActiveConfigDescriptor (
558 IN EFI_USB_IO_PROTOCOL
*This
,
559 OUT EFI_USB_CONFIG_DESCRIPTOR
*Descriptor
563 USB_INTERFACE
*UsbIf
;
567 if (Descriptor
== NULL
) {
568 return EFI_INVALID_PARAMETER
;
571 Status
= EFI_SUCCESS
;
572 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
574 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
577 if (Dev
->ActiveConfig
== NULL
) {
578 Status
= EFI_NOT_FOUND
;
582 CopyMem (Descriptor
, &(Dev
->ActiveConfig
->Desc
), sizeof (EFI_USB_CONFIG_DESCRIPTOR
));
585 gBS
->RestoreTPL (OldTpl
);
591 Retrieve the active interface setting descriptor for this USB IO instance.
593 @param This The USB IO instance.
594 @param Descriptor The variable to receive active interface setting.
596 @retval EFI_SUCCESS The active interface setting is returned.
597 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
602 UsbIoGetInterfaceDescriptor (
603 IN EFI_USB_IO_PROTOCOL
*This
,
604 OUT EFI_USB_INTERFACE_DESCRIPTOR
*Descriptor
607 USB_INTERFACE
*UsbIf
;
610 if (Descriptor
== NULL
) {
611 return EFI_INVALID_PARAMETER
;
614 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
616 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
617 CopyMem (Descriptor
, &(UsbIf
->IfSetting
->Desc
), sizeof (EFI_USB_INTERFACE_DESCRIPTOR
));
619 gBS
->RestoreTPL (OldTpl
);
625 Retrieve the endpoint descriptor from this interface setting.
627 @param This The USB IO instance.
628 @param Index The index (start from zero) of the endpoint to
630 @param Descriptor The variable to receive the descriptor.
632 @retval EFI_SUCCESS The endpoint descriptor is returned.
633 @retval EFI_INVALID_PARAMETER Some parameter is invalid.
638 UsbIoGetEndpointDescriptor (
639 IN EFI_USB_IO_PROTOCOL
*This
,
641 OUT EFI_USB_ENDPOINT_DESCRIPTOR
*Descriptor
644 USB_INTERFACE
*UsbIf
;
647 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
649 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
651 if ((Descriptor
== NULL
) || (Index
> 15)) {
652 gBS
->RestoreTPL (OldTpl
);
653 return EFI_INVALID_PARAMETER
;
656 if (Index
>= UsbIf
->IfSetting
->Desc
.NumEndpoints
) {
657 gBS
->RestoreTPL (OldTpl
);
658 return EFI_NOT_FOUND
;
663 &(UsbIf
->IfSetting
->Endpoints
[Index
]->Desc
),
664 sizeof (EFI_USB_ENDPOINT_DESCRIPTOR
)
667 gBS
->RestoreTPL (OldTpl
);
673 Retrieve the supported language ID table from the device.
675 @param This The USB IO instance.
676 @param LangIDTable The table to return the language IDs.
677 @param TableSize The size, in bytes, of the table LangIDTable.
679 @retval EFI_SUCCESS The language ID is return.
684 UsbIoGetSupportedLanguages (
685 IN EFI_USB_IO_PROTOCOL
*This
,
686 OUT UINT16
**LangIDTable
,
687 OUT UINT16
*TableSize
691 USB_INTERFACE
*UsbIf
;
694 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
696 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
699 *LangIDTable
= Dev
->LangId
;
700 *TableSize
= (UINT16
) (Dev
->TotalLangId
* sizeof (UINT16
));
702 gBS
->RestoreTPL (OldTpl
);
708 Retrieve an indexed string in the language of LangID.
710 @param This The USB IO instance.
711 @param LangID The language ID of the string to retrieve.
712 @param StringIndex The index of the string.
713 @param String The variable to receive the string.
715 @retval EFI_SUCCESS The string is returned.
716 @retval EFI_NOT_FOUND No such string existed.
721 UsbIoGetStringDescriptor (
722 IN EFI_USB_IO_PROTOCOL
*This
,
724 IN UINT8 StringIndex
,
729 USB_INTERFACE
*UsbIf
;
730 EFI_USB_STRING_DESCRIPTOR
*StrDesc
;
736 if ((StringIndex
== 0) || (LangID
== 0)) {
737 return EFI_NOT_FOUND
;
740 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
742 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
746 // Check whether language ID is supported
748 Status
= EFI_NOT_FOUND
;
750 for (Index
= 0; Index
< Dev
->TotalLangId
; Index
++) {
751 ASSERT (Index
< USB_MAX_LANG_ID
);
752 if (Dev
->LangId
[Index
] == LangID
) {
757 if (Index
== Dev
->TotalLangId
) {
762 // Retrieve the string descriptor then allocate a buffer
763 // to hold the string itself.
765 StrDesc
= UsbGetOneString (Dev
, StringIndex
, LangID
);
767 if (StrDesc
== NULL
) {
771 if (StrDesc
->Length
<= 2) {
775 Buf
= AllocateZeroPool (StrDesc
->Length
);
778 Status
= EFI_OUT_OF_RESOURCES
;
782 CopyMem (Buf
, StrDesc
->String
, StrDesc
->Length
- 2);
783 *String
= (CHAR16
*) Buf
;
784 Status
= EFI_SUCCESS
;
787 gBS
->FreePool (StrDesc
);
790 gBS
->RestoreTPL (OldTpl
);
796 Reset the device, then if that succeeds, reconfigure the
797 device with its address and current active configuration.
799 @param This The USB IO instance.
801 @retval EFI_SUCCESS The device is reset and configured.
802 @retval Others Failed to reset the device.
808 IN EFI_USB_IO_PROTOCOL
*This
811 USB_INTERFACE
*UsbIf
;
812 USB_INTERFACE
*HubIf
;
817 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
819 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
823 Status
= EFI_INVALID_PARAMETER
;
827 HubIf
= Dev
->ParentIf
;
828 Status
= HubIf
->HubApi
->ResetPort (HubIf
, Dev
->ParentPort
);
830 if (EFI_ERROR (Status
)) {
831 DEBUG (( EFI_D_ERROR
, "UsbIoPortReset: failed to reset hub port %d@hub %d, %r \n",
832 Dev
->ParentPort
, Dev
->ParentAddr
, Status
));
838 // Reset the device to its current address. The device now has an address
839 // of ZERO after port reset, so need to set Dev->Address to the device again for
840 // host to communicate with it.
842 Status
= UsbSetAddress (Dev
, Dev
->Address
);
844 gBS
->Stall (USB_SET_DEVICE_ADDRESS_STALL
);
846 if (EFI_ERROR (Status
)) {
848 // It may fail due to device disconnection or other reasons.
850 DEBUG (( EFI_D_ERROR
, "UsbIoPortReset: failed to set address for device %d - %r\n",
851 Dev
->Address
, Status
));
856 DEBUG (( EFI_D_INFO
, "UsbIoPortReset: device is now ADDRESSED at %d\n", Dev
->Address
));
859 // Reset the current active configure, after this device
860 // is in CONFIGURED state.
862 if (Dev
->ActiveConfig
!= NULL
) {
863 Status
= UsbSetConfig (Dev
, Dev
->ActiveConfig
->Desc
.ConfigurationValue
);
865 if (EFI_ERROR (Status
)) {
866 DEBUG (( EFI_D_ERROR
, "UsbIoPortReset: failed to set configure for device %d - %r\n",
867 Dev
->Address
, Status
));
872 gBS
->RestoreTPL (OldTpl
);
878 Install Usb Bus Protocol on host controller, and start the Usb bus.
880 @param This The USB bus driver binding instance.
881 @param Controller The controller to check.
882 @param RemainingDevicePath The remaining device patch.
884 @retval EFI_SUCCESS The controller is controlled by the usb bus.
885 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb bus.
886 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
891 UsbBusBuildProtocol (
892 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
893 IN EFI_HANDLE Controller
,
894 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
899 USB_INTERFACE
*RootIf
;
903 UsbBus
= AllocateZeroPool (sizeof (USB_BUS
));
905 if (UsbBus
== NULL
) {
906 return EFI_OUT_OF_RESOURCES
;
909 UsbBus
->Signature
= USB_BUS_SIGNATURE
;
910 UsbBus
->HostHandle
= Controller
;
911 UsbBus
->MaxDevices
= USB_MAX_DEVICES
;
913 Status
= gBS
->OpenProtocol (
915 &gEfiDevicePathProtocolGuid
,
916 (VOID
**) &UsbBus
->DevicePath
,
917 This
->DriverBindingHandle
,
919 EFI_OPEN_PROTOCOL_BY_DRIVER
922 if (EFI_ERROR (Status
)) {
923 DEBUG ((EFI_D_ERROR
, "UsbBusStart: Failed to open device path %r\n", Status
));
930 // Get USB_HC2/USB_HC host controller protocol (EHCI/UHCI).
931 // This is for backward compatibility with EFI 1.x. In UEFI
932 // 2.x, USB_HC2 replaces USB_HC. We will open both USB_HC2
933 // and USB_HC because EHCI driver will install both protocols
934 // (for the same reason). If we don't consume both of them,
935 // the unconsumed one may be opened by others.
937 Status
= gBS
->OpenProtocol (
939 &gEfiUsb2HcProtocolGuid
,
940 (VOID
**) &(UsbBus
->Usb2Hc
),
941 This
->DriverBindingHandle
,
943 EFI_OPEN_PROTOCOL_BY_DRIVER
946 Status2
= gBS
->OpenProtocol (
948 &gEfiUsbHcProtocolGuid
,
949 (VOID
**) &(UsbBus
->UsbHc
),
950 This
->DriverBindingHandle
,
952 EFI_OPEN_PROTOCOL_BY_DRIVER
955 if (EFI_ERROR (Status
) && EFI_ERROR (Status2
)) {
956 DEBUG ((EFI_D_ERROR
, "UsbBusStart: Failed to open USB_HC/USB2_HC %r\n", Status
));
958 Status
= EFI_DEVICE_ERROR
;
962 if (!EFI_ERROR (Status
)) {
964 // The EFI_USB2_HC_PROTOCOL is produced for XHCI support.
965 // Then its max supported devices are 256. Otherwise it's 128.
967 ASSERT (UsbBus
->Usb2Hc
!= NULL
);
968 if (UsbBus
->Usb2Hc
->MajorRevision
== 0x3) {
969 UsbBus
->MaxDevices
= 256;
973 UsbHcReset (UsbBus
, EFI_USB_HC_RESET_GLOBAL
);
974 UsbHcSetState (UsbBus
, EfiUsbHcStateOperational
);
977 // Install an EFI_USB_BUS_PROTOCOL to host controller to identify it.
979 Status
= gBS
->InstallProtocolInterface (
982 EFI_NATIVE_INTERFACE
,
986 if (EFI_ERROR (Status
)) {
987 DEBUG ((EFI_D_ERROR
, "UsbBusStart: Failed to install bus protocol %r\n", Status
));
992 // Initial the wanted child device path list, and add first RemainingDevicePath
994 InitializeListHead (&UsbBus
->WantedUsbIoDPList
);
995 Status
= UsbBusAddWantedUsbIoDP (&UsbBus
->BusId
, RemainingDevicePath
);
996 ASSERT (!EFI_ERROR (Status
));
998 // Create a fake usb device for root hub
1000 RootHub
= AllocateZeroPool (sizeof (USB_DEVICE
));
1002 if (RootHub
== NULL
) {
1003 Status
= EFI_OUT_OF_RESOURCES
;
1004 goto UNINSTALL_USBBUS
;
1007 RootIf
= AllocateZeroPool (sizeof (USB_INTERFACE
));
1009 if (RootIf
== NULL
) {
1011 Status
= EFI_OUT_OF_RESOURCES
;
1015 RootHub
->Bus
= UsbBus
;
1016 RootHub
->NumOfInterface
= 1;
1017 RootHub
->Interfaces
[0] = RootIf
;
1019 RootIf
->Signature
= USB_INTERFACE_SIGNATURE
;
1020 RootIf
->Device
= RootHub
;
1021 RootIf
->DevicePath
= UsbBus
->DevicePath
;
1024 // Report Status Code here since we will enumerate the USB devices
1026 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1028 (EFI_IO_BUS_USB
| EFI_IOB_PC_DETECT
),
1032 Status
= mUsbRootHubApi
.Init (RootIf
);
1034 if (EFI_ERROR (Status
)) {
1035 DEBUG ((EFI_D_ERROR
, "UsbBusStart: Failed to init root hub %r\n", Status
));
1039 UsbBus
->Devices
[0] = RootHub
;
1041 DEBUG ((EFI_D_INFO
, "UsbBusStart: usb bus started on %p, root hub %p\n", Controller
, RootIf
));
1045 if (RootIf
!= NULL
) {
1048 if (RootHub
!= NULL
) {
1053 gBS
->UninstallProtocolInterface (Controller
, &gEfiCallerIdGuid
, &UsbBus
->BusId
);
1056 if (UsbBus
->Usb2Hc
!= NULL
) {
1057 gBS
->CloseProtocol (
1059 &gEfiUsb2HcProtocolGuid
,
1060 This
->DriverBindingHandle
,
1064 if (UsbBus
->UsbHc
!= NULL
) {
1065 gBS
->CloseProtocol (
1067 &gEfiUsbHcProtocolGuid
,
1068 This
->DriverBindingHandle
,
1072 gBS
->CloseProtocol (
1074 &gEfiDevicePathProtocolGuid
,
1075 This
->DriverBindingHandle
,
1080 DEBUG ((EFI_D_ERROR
, "UsbBusStart: Failed to start bus driver %r\n", Status
));
1086 The USB bus driver entry pointer.
1088 @param ImageHandle The driver image handle.
1089 @param SystemTable The system table.
1091 @return EFI_SUCCESS The component name protocol is installed.
1092 @return Others Failed to init the usb driver.
1097 UsbBusDriverEntryPoint (
1098 IN EFI_HANDLE ImageHandle
,
1099 IN EFI_SYSTEM_TABLE
*SystemTable
1102 return EfiLibInstallDriverBindingComponentName2 (
1105 &mUsbBusDriverBinding
,
1107 &mUsbBusComponentName
,
1108 &mUsbBusComponentName2
1114 Check whether USB bus driver support this device.
1116 @param This The USB bus driver binding protocol.
1117 @param Controller The controller handle to check.
1118 @param RemainingDevicePath The remaining device path.
1120 @retval EFI_SUCCESS The bus supports this controller.
1121 @retval EFI_UNSUPPORTED This device isn't supported.
1126 UsbBusControllerDriverSupported (
1127 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1128 IN EFI_HANDLE Controller
,
1129 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1132 EFI_DEV_PATH_PTR DevicePathNode
;
1133 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
1134 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
1135 EFI_USB_HC_PROTOCOL
*UsbHc
;
1139 // Check whether device path is valid
1141 if (RemainingDevicePath
!= NULL
) {
1143 // Check if RemainingDevicePath is the End of Device Path Node,
1144 // if yes, go on checking other conditions
1146 if (!IsDevicePathEnd (RemainingDevicePath
)) {
1148 // If RemainingDevicePath isn't the End of Device Path Node,
1149 // check its validation
1151 DevicePathNode
.DevPath
= RemainingDevicePath
;
1153 if ((DevicePathNode
.DevPath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1154 (DevicePathNode
.DevPath
->SubType
!= MSG_USB_DP
&&
1155 DevicePathNode
.DevPath
->SubType
!= MSG_USB_CLASS_DP
1156 && DevicePathNode
.DevPath
->SubType
!= MSG_USB_WWID_DP
1159 return EFI_UNSUPPORTED
;
1165 // Check whether USB_HC2 protocol is installed
1167 Status
= gBS
->OpenProtocol (
1169 &gEfiUsb2HcProtocolGuid
,
1171 This
->DriverBindingHandle
,
1173 EFI_OPEN_PROTOCOL_BY_DRIVER
1175 if (Status
== EFI_ALREADY_STARTED
) {
1179 if (EFI_ERROR (Status
)) {
1181 // If failed to open USB_HC2, fall back to USB_HC
1183 Status
= gBS
->OpenProtocol (
1185 &gEfiUsbHcProtocolGuid
,
1187 This
->DriverBindingHandle
,
1189 EFI_OPEN_PROTOCOL_BY_DRIVER
1191 if (Status
== EFI_ALREADY_STARTED
) {
1195 if (EFI_ERROR (Status
)) {
1200 // Close the USB_HC used to perform the supported test
1202 gBS
->CloseProtocol (
1204 &gEfiUsbHcProtocolGuid
,
1205 This
->DriverBindingHandle
,
1212 // Close the USB_HC2 used to perform the supported test
1214 gBS
->CloseProtocol (
1216 &gEfiUsb2HcProtocolGuid
,
1217 This
->DriverBindingHandle
,
1223 // Open the EFI Device Path protocol needed to perform the supported test
1225 Status
= gBS
->OpenProtocol (
1227 &gEfiDevicePathProtocolGuid
,
1228 (VOID
**) &ParentDevicePath
,
1229 This
->DriverBindingHandle
,
1231 EFI_OPEN_PROTOCOL_BY_DRIVER
1233 if (Status
== EFI_ALREADY_STARTED
) {
1237 if (!EFI_ERROR (Status
)) {
1239 // Close protocol, don't use device path protocol in the Support() function
1241 gBS
->CloseProtocol (
1243 &gEfiDevicePathProtocolGuid
,
1244 This
->DriverBindingHandle
,
1256 Start to process the controller.
1258 @param This The USB bus driver binding instance.
1259 @param Controller The controller to check.
1260 @param RemainingDevicePath The remaining device patch.
1262 @retval EFI_SUCCESS The controller is controlled by the usb bus.
1263 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb
1265 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
1270 UsbBusControllerDriverStart (
1271 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1272 IN EFI_HANDLE Controller
,
1273 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1276 EFI_USB_BUS_PROTOCOL
*UsbBusId
;
1278 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
1280 Status
= gBS
->OpenProtocol (
1282 &gEfiDevicePathProtocolGuid
,
1283 (VOID
**) &ParentDevicePath
,
1284 This
->DriverBindingHandle
,
1286 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1288 ASSERT_EFI_ERROR (Status
);
1291 // Report Status Code here since we will initialize the host controller
1293 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1295 (EFI_IO_BUS_USB
| EFI_IOB_PC_INIT
),
1300 // Locate the USB bus protocol, if it is found, USB bus
1301 // is already started on this controller.
1303 Status
= gBS
->OpenProtocol (
1306 (VOID
**) &UsbBusId
,
1307 This
->DriverBindingHandle
,
1309 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1312 if (EFI_ERROR (Status
)) {
1314 // If first start, build the bus execute environment and install bus protocol
1316 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_IO_BUS_USB
| EFI_P_PC_ENABLE
));
1317 Status
= UsbBusBuildProtocol (This
, Controller
, RemainingDevicePath
);
1318 if (EFI_ERROR (Status
)) {
1322 // Try get the Usb Bus protocol interface again
1324 Status
= gBS
->OpenProtocol (
1327 (VOID
**) &UsbBusId
,
1328 This
->DriverBindingHandle
,
1330 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1332 ASSERT (!EFI_ERROR (Status
));
1335 // USB Bus driver need to control the recursive connect policy of the bus, only those wanted
1336 // usb child device will be recursively connected.
1337 // The RemainingDevicePath indicate the child usb device which user want to fully recursively connecte this time.
1338 // All wanted usb child devices will be remembered by the usb bus driver itself.
1339 // If RemainingDevicePath == NULL, all the usb child devices in the usb bus are wanted devices.
1341 // Save the passed in RemainingDevicePath this time
1343 if (RemainingDevicePath
!= NULL
) {
1344 if (IsDevicePathEnd (RemainingDevicePath
)) {
1346 // If RemainingDevicePath is the End of Device Path Node,
1347 // skip enumerate any device and return EFI_SUCESSS
1353 Status
= UsbBusAddWantedUsbIoDP (UsbBusId
, RemainingDevicePath
);
1354 ASSERT (!EFI_ERROR (Status
));
1356 // Ensure all wanted child usb devices are fully recursively connected
1358 Status
= UsbBusRecursivelyConnectWantedUsbIo (UsbBusId
);
1359 ASSERT (!EFI_ERROR (Status
));
1368 Stop handle the controller by this USB bus driver.
1370 @param This The USB bus driver binding protocol.
1371 @param Controller The controller to release.
1372 @param NumberOfChildren The child of USB bus that opened controller
1374 @param ChildHandleBuffer The array of child handle.
1376 @retval EFI_SUCCESS The controller or children are stopped.
1377 @retval EFI_DEVICE_ERROR Failed to stop the driver.
1382 UsbBusControllerDriverStop (
1383 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1384 IN EFI_HANDLE Controller
,
1385 IN UINTN NumberOfChildren
,
1386 IN EFI_HANDLE
*ChildHandleBuffer
1390 USB_DEVICE
*RootHub
;
1392 USB_INTERFACE
*RootIf
;
1393 USB_INTERFACE
*UsbIf
;
1394 EFI_USB_BUS_PROTOCOL
*BusId
;
1395 EFI_USB_IO_PROTOCOL
*UsbIo
;
1400 Status
= EFI_SUCCESS
;
1402 if (NumberOfChildren
> 0) {
1404 // BugBug: Raise TPL to callback level instead of USB_BUS_TPL to avoid TPL conflict
1406 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1408 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
1409 Status
= gBS
->OpenProtocol (
1410 ChildHandleBuffer
[Index
],
1411 &gEfiUsbIoProtocolGuid
,
1413 This
->DriverBindingHandle
,
1415 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1418 if (EFI_ERROR (Status
)) {
1420 // It is possible that the child has already been released:
1421 // 1. For combo device, free one device will release others.
1422 // 2. If a hub is released, all devices on its down facing
1423 // ports are released also.
1428 UsbIf
= USB_INTERFACE_FROM_USBIO (UsbIo
);
1429 UsbDev
= UsbIf
->Device
;
1431 UsbRemoveDevice (UsbDev
);
1434 gBS
->RestoreTPL (OldTpl
);
1438 DEBUG (( EFI_D_INFO
, "UsbBusStop: usb bus stopped on %p\n", Controller
));
1441 // Locate USB_BUS for the current host controller
1443 Status
= gBS
->OpenProtocol (
1447 This
->DriverBindingHandle
,
1449 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1452 if (EFI_ERROR (Status
)) {
1456 Bus
= USB_BUS_FROM_THIS (BusId
);
1459 // Stop the root hub, then free all the devices
1461 // BugBug: Raise TPL to callback level instead of USB_BUS_TPL to avoid TPL conflict
1463 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1465 RootHub
= Bus
->Devices
[0];
1466 RootIf
= RootHub
->Interfaces
[0];
1468 mUsbRootHubApi
.Release (RootIf
);
1470 ASSERT (Bus
->MaxDevices
<= 256);
1471 for (Index
= 1; Index
< Bus
->MaxDevices
; Index
++) {
1472 if (Bus
->Devices
[Index
] != NULL
) {
1473 UsbRemoveDevice (Bus
->Devices
[Index
]);
1477 gBS
->RestoreTPL (OldTpl
);
1479 gBS
->FreePool (RootIf
);
1480 gBS
->FreePool (RootHub
);
1481 Status
= UsbBusFreeUsbDPList (&Bus
->WantedUsbIoDPList
);
1482 ASSERT (!EFI_ERROR (Status
));
1485 // Uninstall the bus identifier and close USB_HC/USB2_HC protocols
1487 gBS
->UninstallProtocolInterface (Controller
, &gEfiCallerIdGuid
, &Bus
->BusId
);
1489 if (Bus
->Usb2Hc
!= NULL
) {
1490 gBS
->CloseProtocol (
1492 &gEfiUsb2HcProtocolGuid
,
1493 This
->DriverBindingHandle
,
1498 if (Bus
->UsbHc
!= NULL
) {
1499 gBS
->CloseProtocol (
1501 &gEfiUsbHcProtocolGuid
,
1502 This
->DriverBindingHandle
,
1507 gBS
->CloseProtocol (
1509 &gEfiDevicePathProtocolGuid
,
1510 This
->DriverBindingHandle
,
1514 gBS
->FreePool (Bus
);