3 Copyright (c) 2004 - 2007, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Usb Bus Driver Binding and Bus IO Protocol
28 // USB_BUS_PROTOCOL is only used to locate USB_BUS
30 EFI_GUID mUsbBusProtocolGuid
= EFI_USB_BUS_PROTOCOL_GUID
;
34 USB_IO function to execute a control transfer. This
35 function will execute the USB transfer. If transfer
36 successes, it will sync the internal state of USB bus
39 @param This The USB_IO instance
40 @param Request The control transfer request
41 @param Direction Direction for data stage
42 @param Timeout The time to wait before timeout
43 @param Data The buffer holding the data
44 @param DataLength Then length of the data
45 @param UsbStatus USB result
47 @retval EFI_INVALID_PARAMETER The parameters are invalid
48 @retval EFI_SUCCESS The control transfer succeded.
49 @retval Others Failed to execute the transfer
55 UsbIoControlTransfer (
56 IN EFI_USB_IO_PROTOCOL
*This
,
57 IN EFI_USB_DEVICE_REQUEST
*Request
,
58 IN EFI_USB_DATA_DIRECTION Direction
,
60 IN OUT VOID
*Data
, OPTIONAL
61 IN UINTN DataLength
, OPTIONAL
67 USB_ENDPOINT_DESC
*EpDesc
;
71 if (UsbStatus
== NULL
) {
72 return EFI_INVALID_PARAMETER
;
75 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
77 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
80 Status
= UsbHcControlTransfer (
94 if (EFI_ERROR (Status
) || (*UsbStatus
!= EFI_USB_NOERROR
)) {
96 // Clear TT buffer when CTRL/BULK split transaction failes
97 // Clear the TRANSLATOR TT buffer, not parent's buffer
99 if (Dev
->Translator
.TranslatorHubAddress
!= 0) {
100 UsbHubCtrlClearTTBuffer (
101 Dev
->Bus
->Devices
[Dev
->Translator
.TranslatorHubAddress
],
102 Dev
->Translator
.TranslatorPortNumber
,
113 // Some control transfer will change the device's internal
114 // status, such as Set_Configuration and Set_Interface.
115 // We must synchronize the bus driver's status with that in
116 // device. We ignore the Set_Descriptor request because it's
117 // hardly used by any device, especially in pre-boot environment
121 // Reset the endpoint toggle when endpoint stall is cleared
123 if ((Request
->Request
== USB_REQ_CLEAR_FEATURE
) &&
124 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
,
125 USB_TARGET_ENDPOINT
)) &&
126 (Request
->Value
== USB_FEATURE_ENDPOINT_HALT
)) {
128 EpDesc
= UsbGetEndpointDesc (UsbIf
, (UINT8
) Request
->Index
);
130 if (EpDesc
!= NULL
) {
136 // Select a new configuration. This is a dangerous action. Upper driver
137 // should stop use its current UsbIo after calling this driver. The old
138 // UsbIo will be uninstalled and new UsbIo be installed. We can't use
139 // ReinstallProtocol since interfaces in different configuration may be
140 // completely irrellvant.
142 if ((Request
->Request
== USB_REQ_SET_CONFIG
) &&
143 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
,
144 USB_TARGET_DEVICE
))) {
146 // Don't re-create the USB interfaces if configuration isn't changed.
148 if ((Dev
->ActiveConfig
!= NULL
) &&
149 (Request
->Value
== Dev
->ActiveConfig
->Desc
.ConfigurationValue
)) {
153 DEBUG ((EFI_D_INFO
, "UsbIoControlTransfer: configure changed!!! Do NOT use old UsbIo!!!\n"));
155 if (Dev
->ActiveConfig
!= NULL
) {
156 UsbRemoveConfig (Dev
);
159 if (Request
->Value
!= 0) {
160 Status
= UsbSelectConfig (Dev
, (UINT8
) Request
->Value
);
164 // Exit now, Old USB_IO is invalid now
170 // A new alternative setting is selected for the interface.
171 // No need to reinstall UsbIo in this case because only
172 // underlying communication endpoints are changed. Functionality
173 // should remains the same.
175 if ((Request
->Request
== USB_REQ_SET_INTERFACE
) &&
176 (Request
->RequestType
== USB_REQUEST_TYPE (EfiUsbNoData
, USB_REQ_TYPE_STANDARD
,
177 USB_TARGET_INTERFACE
)) &&
178 (Request
->Index
== UsbIf
->IfSetting
->Desc
.InterfaceNumber
)) {
180 Status
= UsbSelectSetting (UsbIf
->IfDesc
, (UINT8
) Request
->Value
);
182 if (!EFI_ERROR (Status
)) {
183 UsbIf
->IfSetting
= UsbIf
->IfDesc
->Settings
[UsbIf
->IfDesc
->ActiveIndex
];
188 gBS
->RestoreTPL (OldTpl
);
194 Execute a bulk transfer to the device endpoint
196 @param This The USB IO instance
197 @param Endpoint The device endpoint
198 @param Data The data to transfer
199 @param DataLength The length of the data to transfer
200 @param Timeout Time to wait before timeout
201 @param UsbStatus The result of USB transfer
203 @retval EFI_SUCCESS The bulk transfer is OK
204 @retval EFI_INVALID_PARAMETER Some parameters are invalid
205 @retval Others Failed to execute transfer, reason returned in
213 IN EFI_USB_IO_PROTOCOL
*This
,
216 IN OUT UINTN
*DataLength
,
218 OUT UINT32
*UsbStatus
222 USB_INTERFACE
*UsbIf
;
223 USB_ENDPOINT_DESC
*EpDesc
;
229 if ((USB_ENDPOINT_ADDR (Endpoint
) == 0) || (USB_ENDPOINT_ADDR(Endpoint
) > 15) ||
230 (UsbStatus
== NULL
)) {
232 return EFI_INVALID_PARAMETER
;
235 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
237 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
240 EpDesc
= UsbGetEndpointDesc (UsbIf
, Endpoint
);
242 if ((EpDesc
== NULL
) || (USB_ENDPOINT_TYPE (&EpDesc
->Desc
) != USB_ENDPOINT_BULK
)) {
243 Status
= EFI_INVALID_PARAMETER
;
248 Toggle
= EpDesc
->Toggle
;
249 Status
= UsbHcBulkTransfer (
254 EpDesc
->Desc
.MaxPacketSize
,
264 EpDesc
->Toggle
= Toggle
;
266 if (EFI_ERROR (Status
) || (*UsbStatus
!= EFI_USB_NOERROR
)) {
268 // Clear TT buffer when CTRL/BULK split transaction failes.
269 // Clear the TRANSLATOR TT buffer, not parent's buffer
271 if (Dev
->Translator
.TranslatorHubAddress
!= 0) {
272 UsbHubCtrlClearTTBuffer (
273 Dev
->Bus
->Devices
[Dev
->Translator
.TranslatorHubAddress
],
274 Dev
->Translator
.TranslatorPortNumber
,
283 gBS
->RestoreTPL (OldTpl
);
289 Execute a synchronous interrupt transfer
291 @param This The USB IO instance
292 @param Endpoint The device endpoint
293 @param Data The data to transfer
294 @param DataLength The length of the data to transfer
295 @param Timeout Time to wait before timeout
296 @param UsbStatus The result of USB transfer
298 @retval EFI_SUCCESS The synchronous interrupt transfer is OK
299 @retval EFI_INVALID_PARAMETER Some parameters are invalid
300 @retval Others Failed to execute transfer, reason returned in
307 UsbIoSyncInterruptTransfer (
308 IN EFI_USB_IO_PROTOCOL
*This
,
311 IN OUT UINTN
*DataLength
,
313 OUT UINT32
*UsbStatus
317 USB_INTERFACE
*UsbIf
;
318 USB_ENDPOINT_DESC
*EpDesc
;
323 if ((USB_ENDPOINT_ADDR (Endpoint
) == 0) || (USB_ENDPOINT_ADDR(Endpoint
) > 15) ||
324 (UsbStatus
== NULL
)) {
326 return EFI_INVALID_PARAMETER
;
329 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
331 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
334 EpDesc
= UsbGetEndpointDesc (UsbIf
, Endpoint
);
336 if ((EpDesc
== NULL
) || (USB_ENDPOINT_TYPE (&EpDesc
->Desc
) != USB_ENDPOINT_INTERRUPT
)) {
337 Status
= EFI_INVALID_PARAMETER
;
341 Toggle
= EpDesc
->Toggle
;
342 Status
= UsbHcSyncInterruptTransfer (
347 EpDesc
->Desc
.MaxPacketSize
,
356 EpDesc
->Toggle
= Toggle
;
359 gBS
->RestoreTPL (OldTpl
);
365 Queue a new asynchronous interrupt transfer, or remove the old
366 request if (IsNewTransfer == FALSE)
368 @param This The USB_IO instance
369 @param Endpoint The device endpoint
370 @param IsNewTransfer Whether this is a new request, if it's old, remove
372 @param PollInterval The interval to poll the transfer result, (in ms)
373 @param DataLength The length of perodic data transfer
374 @param Callback The function to call periodicaly when transfer is
376 @param Context The context to the callback
378 @retval EFI_SUCCESS New transfer is queued or old request is removed
379 @retval EFI_INVALID_PARAMETER Some parameters are invalid
380 @retval Others Failed to queue the new request or remove the old
387 UsbIoAsyncInterruptTransfer (
388 IN EFI_USB_IO_PROTOCOL
*This
,
390 IN BOOLEAN IsNewTransfer
,
391 IN UINTN PollInterval
, OPTIONAL
392 IN UINTN DataLength
, OPTIONAL
393 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
, OPTIONAL
394 IN VOID
*Context OPTIONAL
398 USB_INTERFACE
*UsbIf
;
399 USB_ENDPOINT_DESC
*EpDesc
;
404 if ((USB_ENDPOINT_ADDR (Endpoint
) == 0) || (USB_ENDPOINT_ADDR (Endpoint
) > 15)) {
405 return EFI_INVALID_PARAMETER
;
408 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
409 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
412 EpDesc
= UsbGetEndpointDesc (UsbIf
, Endpoint
);
414 if ((EpDesc
== NULL
) || (USB_ENDPOINT_TYPE (&EpDesc
->Desc
) != USB_ENDPOINT_INTERRUPT
)) {
415 Status
= EFI_INVALID_PARAMETER
;
419 Toggle
= EpDesc
->Toggle
;
420 Status
= UsbHcAsyncInterruptTransfer (
425 EpDesc
->Desc
.MaxPacketSize
,
435 EpDesc
->Toggle
= Toggle
;
438 gBS
->RestoreTPL (OldTpl
);
444 Execute a synchronous isochronous transfer
446 @param This The USB IO instance
447 @param DeviceEndpoint The device endpoint
448 @param Data The data to transfer
449 @param DataLength The length of the data to transfer
450 @param UsbStatus The result of USB transfer
452 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported
458 UsbIoIsochronousTransfer (
459 IN EFI_USB_IO_PROTOCOL
*This
,
460 IN UINT8 DeviceEndpoint
,
466 return EFI_UNSUPPORTED
;
471 Queue an asynchronous isochronous transfer
473 @param This The USB_IO instance
474 @param DeviceEndpoint The device endpoint
475 @param DataLength The length of perodic data transfer
476 @param IsochronousCallBack The function to call periodicaly when transfer is
478 @param Context The context to the callback
480 @retval EFI_UNSUPPORTED Currently isochronous transfer isn't supported
486 UsbIoAsyncIsochronousTransfer (
487 IN EFI_USB_IO_PROTOCOL
*This
,
488 IN UINT8 DeviceEndpoint
,
491 IN EFI_ASYNC_USB_TRANSFER_CALLBACK IsochronousCallBack
,
492 IN VOID
*Context OPTIONAL
495 return EFI_UNSUPPORTED
;
500 Retrieve the device descriptor of the device
502 @param This The USB IO instance
503 @param Descriptor The variable to receive the device descriptor
505 @retval EFI_SUCCESS The device descriptor is returned
506 @retval EFI_INVALID_PARAMETER The parameter is invalid
512 UsbIoGetDeviceDescriptor (
513 IN EFI_USB_IO_PROTOCOL
*This
,
514 OUT EFI_USB_DEVICE_DESCRIPTOR
*Descriptor
518 USB_INTERFACE
*UsbIf
;
521 if (Descriptor
== NULL
) {
522 return EFI_INVALID_PARAMETER
;
525 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
527 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
530 CopyMem (Descriptor
, &Dev
->DevDesc
->Desc
, sizeof (EFI_USB_DEVICE_DESCRIPTOR
));
532 gBS
->RestoreTPL (OldTpl
);
538 Return the configuration descriptor of the current active configuration
540 @param This The USB IO instance
541 @param Descriptor The USB configuration descriptor
543 @retval EFI_SUCCESS The active configuration descriptor is returned
544 @retval EFI_INVALID_PARAMETER Some parameter is invalid
545 @retval EFI_NOT_FOUND Currently no active configuration is selected.
551 UsbIoGetActiveConfigDescriptor (
552 IN EFI_USB_IO_PROTOCOL
*This
,
553 OUT EFI_USB_CONFIG_DESCRIPTOR
*Descriptor
557 USB_INTERFACE
*UsbIf
;
561 if (Descriptor
== NULL
) {
562 return EFI_INVALID_PARAMETER
;
565 Status
= EFI_SUCCESS
;
566 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
568 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
571 if (Dev
->ActiveConfig
== NULL
) {
572 Status
= EFI_NOT_FOUND
;
576 CopyMem (Descriptor
, &(Dev
->ActiveConfig
->Desc
), sizeof (EFI_USB_CONFIG_DESCRIPTOR
));
579 gBS
->RestoreTPL (OldTpl
);
585 Retrieve the active interface setting descriptor for this USB IO instance
587 @param This The USB IO instance
588 @param Descriptor The variable to receive active interface setting
590 @retval EFI_SUCCESS The active interface setting is returned
591 @retval EFI_INVALID_PARAMETER Some parameter is invalid
597 UsbIoGetInterfaceDescriptor (
598 IN EFI_USB_IO_PROTOCOL
*This
,
599 OUT EFI_USB_INTERFACE_DESCRIPTOR
*Descriptor
602 USB_INTERFACE
*UsbIf
;
605 if (Descriptor
== NULL
) {
606 return EFI_INVALID_PARAMETER
;
609 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
611 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
612 CopyMem (Descriptor
, &(UsbIf
->IfSetting
->Desc
), sizeof (EFI_USB_INTERFACE_DESCRIPTOR
));
614 gBS
->RestoreTPL (OldTpl
);
620 Retrieve the endpoint descriptor from this interface setting
622 @param This The USB IO instance
623 @param Index The index (start from zero) of the endpoint to
625 @param Descriptor The variable to receive the descriptor
627 @retval EFI_SUCCESS The endpoint descriptor is returned
628 @retval EFI_INVALID_PARAMETER Some parameter is invalid
634 UsbIoGetEndpointDescriptor (
635 IN EFI_USB_IO_PROTOCOL
*This
,
637 OUT EFI_USB_ENDPOINT_DESCRIPTOR
*Descriptor
640 USB_INTERFACE
*UsbIf
;
643 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
645 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
647 if ((Descriptor
== NULL
) || (Index
> 15)) {
648 gBS
->RestoreTPL (OldTpl
);
649 return EFI_INVALID_PARAMETER
;
652 if (Index
>= UsbIf
->IfSetting
->Desc
.NumEndpoints
) {
653 gBS
->RestoreTPL (OldTpl
);
654 return EFI_NOT_FOUND
;
659 &(UsbIf
->IfSetting
->Endpoints
[Index
]->Desc
),
660 sizeof (EFI_USB_ENDPOINT_DESCRIPTOR
)
663 gBS
->RestoreTPL (OldTpl
);
669 Retrieve the supported language ID table from the device
671 @param This The USB IO instance
672 @param LangIDTable The table to return the language IDs
673 @param TableSize The number of supported languanges
675 @retval EFI_SUCCESS The language ID is return
681 UsbIoGetSupportedLanguages (
682 IN EFI_USB_IO_PROTOCOL
*This
,
683 OUT UINT16
**LangIDTable
,
684 OUT UINT16
*TableSize
688 USB_INTERFACE
*UsbIf
;
691 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
693 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
696 *LangIDTable
= Dev
->LangId
;
697 *TableSize
= Dev
->TotalLangId
;
699 gBS
->RestoreTPL (OldTpl
);
705 Retrieve an indexed string in the language of LangID
707 @param This The USB IO instance
708 @param LangID The language ID of the string to retrieve
709 @param StringIndex The index of the string
710 @param String The variable to receive the string
712 @retval EFI_SUCCESS The string is returned
713 @retval EFI_NOT_FOUND No such string existed
719 UsbIoGetStringDescriptor (
720 IN EFI_USB_IO_PROTOCOL
*This
,
722 IN UINT8 StringIndex
,
727 USB_INTERFACE
*UsbIf
;
728 EFI_USB_STRING_DESCRIPTOR
*StrDesc
;
734 if ((StringIndex
== 0) || (LangID
== 0)) {
735 return EFI_NOT_FOUND
;
738 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
740 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
744 // Check whether language ID is supported
746 Status
= EFI_NOT_FOUND
;
748 for (Index
= 0; Index
< Dev
->TotalLangId
; Index
++) {
749 if (Dev
->LangId
[Index
] == LangID
) {
754 if (Index
== Dev
->TotalLangId
) {
759 // Retrieve the string descriptor then allocate a buffer
760 // to hold the string itself.
762 StrDesc
= UsbGetOneString (Dev
, StringIndex
, LangID
);
764 if (StrDesc
== NULL
) {
768 if (StrDesc
->Length
<= 2) {
772 Buf
= AllocateZeroPool (StrDesc
->Length
);
775 Status
= EFI_OUT_OF_RESOURCES
;
779 CopyMem (Buf
, StrDesc
->String
, StrDesc
->Length
- 2);
780 *String
= (CHAR16
*) Buf
;
781 Status
= EFI_SUCCESS
;
784 gBS
->FreePool (StrDesc
);
787 gBS
->RestoreTPL (OldTpl
);
793 Reset the device, then if that succeeds, reconfigure the
794 device with its address and current active configuration.
796 @param This The USB IO instance
798 @retval EFI_SUCCESS The device is reset and configured
799 @retval Others Failed to reset the device
805 IN EFI_USB_IO_PROTOCOL
*This
808 USB_INTERFACE
*UsbIf
;
809 USB_INTERFACE
*HubIf
;
815 OldTpl
= gBS
->RaiseTPL (USB_BUS_TPL
);
817 UsbIf
= USB_INTERFACE_FROM_USBIO (This
);
820 if (UsbIf
->IsHub
== TRUE
) {
821 Status
= EFI_INVALID_PARAMETER
;
825 HubIf
= Dev
->ParentIf
;
826 Status
= HubIf
->HubApi
->ResetPort (HubIf
, Dev
->ParentPort
);
828 if (EFI_ERROR (Status
)) {
829 DEBUG (( EFI_D_ERROR
, "UsbIoPortReset: failed to reset hub port %d@hub %d, %r \n",
830 Dev
->ParentPort
, Dev
->ParentAddr
, Status
));
836 // Reset the device to its current address. The device now has a
837 // address of ZERO, so need to set Dev->Address to zero first for
838 // host to communicate with the device
840 Address
= Dev
->Address
;
842 Status
= UsbSetAddress (Dev
, Address
);
844 if (EFI_ERROR (Status
)) {
845 DEBUG (( EFI_D_ERROR
, "UsbIoPortReset: failed to set address for device %d - %r\n",
851 Dev
->Address
= Address
;
854 // Reset the current active configure, after this device
855 // is in CONFIGURED state.
857 if (Dev
->ActiveConfig
!= NULL
) {
858 Status
= UsbSetConfig (Dev
, Dev
->ActiveConfig
->Desc
.ConfigurationValue
);
860 if (EFI_ERROR (Status
)) {
861 DEBUG (( EFI_D_ERROR
, "UsbIoPortReset: failed to set configure for device %d - %r\n",
867 gBS
->RestoreTPL (OldTpl
);
873 Install Usb Bus Protocol on host controller, and start the Usb bus
875 @param This The USB bus driver binding instance
876 @param Controller The controller to check
877 @param RemainingDevicePath The remaining device patch
879 @retval EFI_SUCCESS The controller is controlled by the usb bus
880 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb bus
881 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources
886 UsbBusBuildProtocol (
887 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
888 IN EFI_HANDLE Controller
,
889 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
894 USB_INTERFACE
*RootIf
;
898 UsbBus
= AllocateZeroPool (sizeof (USB_BUS
));
900 if (UsbBus
== NULL
) {
901 return EFI_OUT_OF_RESOURCES
;
904 UsbBus
->Signature
= USB_BUS_SIGNATURE
;
905 UsbBus
->HostHandle
= Controller
;
907 Status
= gBS
->OpenProtocol (
909 &gEfiDevicePathProtocolGuid
,
910 (VOID
**) &UsbBus
->DevicePath
,
911 This
->DriverBindingHandle
,
913 EFI_OPEN_PROTOCOL_BY_DRIVER
916 if (EFI_ERROR (Status
)) {
917 DEBUG ((EFI_D_ERROR
, "UsbBusStart: Failed to open device path %r\n", Status
));
919 gBS
->FreePool (UsbBus
);
924 // Get USB_HC2/USB_HC host controller protocol (EHCI/UHCI).
925 // This is for backward compatbility with EFI 1.x. In UEFI
926 // 2.x, USB_HC2 replaces USB_HC. We will open both USB_HC2
927 // and USB_HC because EHCI driver will install both protocols
928 // (for the same reason). If we don't consume both of them,
929 // the unconsumed one may be opened by others.
931 Status
= gBS
->OpenProtocol (
933 &gEfiUsb2HcProtocolGuid
,
934 (VOID
**) &(UsbBus
->Usb2Hc
),
935 This
->DriverBindingHandle
,
937 EFI_OPEN_PROTOCOL_BY_DRIVER
940 Status2
= gBS
->OpenProtocol (
942 &gEfiUsbHcProtocolGuid
,
943 (VOID
**) &(UsbBus
->UsbHc
),
944 This
->DriverBindingHandle
,
946 EFI_OPEN_PROTOCOL_BY_DRIVER
949 if (EFI_ERROR (Status
) && EFI_ERROR (Status2
)) {
950 DEBUG ((EFI_D_ERROR
, "UsbBusStart: Failed to open USB_HC/USB2_HC %r\n", Status
));
952 Status
= EFI_DEVICE_ERROR
;
956 UsbHcReset (UsbBus
, EFI_USB_HC_RESET_GLOBAL
);
957 UsbHcSetState (UsbBus
, EfiUsbHcStateOperational
);
960 // Install an EFI_USB_BUS_PROTOCOL to host controler to identify it.
962 Status
= gBS
->InstallProtocolInterface (
964 &mUsbBusProtocolGuid
,
965 EFI_NATIVE_INTERFACE
,
969 if (EFI_ERROR (Status
)) {
970 DEBUG ((EFI_D_ERROR
, "UsbBusStart: Failed to install bus protocol %r\n", Status
));
975 // Initial the wanted child device path list, and add first RemainingDevicePath
977 InitializeListHead (&UsbBus
->WantedUsbIoDPList
);
978 Status
= UsbBusAddWantedUsbIoDP (&UsbBus
->BusId
, RemainingDevicePath
);
979 ASSERT (!EFI_ERROR (Status
));
981 // Create a fake usb device for root hub
983 RootHub
= AllocateZeroPool (sizeof (USB_DEVICE
));
985 if (RootHub
== NULL
) {
986 Status
= EFI_OUT_OF_RESOURCES
;
987 goto UNINSTALL_USBBUS
;
990 RootIf
= AllocateZeroPool (sizeof (USB_INTERFACE
));
992 if (RootIf
== NULL
) {
993 gBS
->FreePool (RootHub
);
994 Status
= EFI_OUT_OF_RESOURCES
;
998 RootHub
->Bus
= UsbBus
;
999 RootHub
->NumOfInterface
= 1;
1000 RootHub
->Interfaces
[0] = RootIf
;
1001 RootIf
->Signature
= USB_INTERFACE_SIGNATURE
;
1002 RootIf
->Device
= RootHub
;
1003 RootIf
->DevicePath
= UsbBus
->DevicePath
;
1005 Status
= mUsbRootHubApi
.Init (RootIf
);
1007 if (EFI_ERROR (Status
)) {
1008 DEBUG ((EFI_D_ERROR
, "UsbBusStart: Failed to init root hub %r\n", Status
));
1012 UsbBus
->Devices
[0] = RootHub
;
1014 DEBUG ((EFI_D_INFO
, "UsbBusStart: usb bus started on %x, root hub %x\n", Controller
, RootIf
));
1018 if (RootIf
!= NULL
) {
1019 gBS
->FreePool (RootIf
);
1021 if (RootHub
!= NULL
) {
1022 gBS
->FreePool (RootHub
);
1026 gBS
->UninstallProtocolInterface (Controller
, &mUsbBusProtocolGuid
, &UsbBus
->BusId
);
1029 if (UsbBus
->Usb2Hc
!= NULL
) {
1030 gBS
->CloseProtocol (
1032 &gEfiUsb2HcProtocolGuid
,
1033 This
->DriverBindingHandle
,
1037 if (UsbBus
->UsbHc
!= NULL
) {
1038 gBS
->CloseProtocol (
1040 &gEfiUsbHcProtocolGuid
,
1041 This
->DriverBindingHandle
,
1045 gBS
->CloseProtocol (
1047 &gEfiDevicePathProtocolGuid
,
1048 This
->DriverBindingHandle
,
1051 gBS
->FreePool (UsbBus
);
1053 DEBUG ((EFI_D_ERROR
, "UsbBusStart: Failed to start bus driver %r\n", Status
));
1057 EFI_USB_IO_PROTOCOL mUsbIoProtocol
= {
1058 UsbIoControlTransfer
,
1060 UsbIoAsyncInterruptTransfer
,
1061 UsbIoSyncInterruptTransfer
,
1062 UsbIoIsochronousTransfer
,
1063 UsbIoAsyncIsochronousTransfer
,
1064 UsbIoGetDeviceDescriptor
,
1065 UsbIoGetActiveConfigDescriptor
,
1066 UsbIoGetInterfaceDescriptor
,
1067 UsbIoGetEndpointDescriptor
,
1068 UsbIoGetStringDescriptor
,
1069 UsbIoGetSupportedLanguages
,
1076 UsbBusDriverEntryPoint (
1077 IN EFI_HANDLE ImageHandle
,
1078 IN EFI_SYSTEM_TABLE
*SystemTable
1082 Routine Description:
1084 The USB bus driver entry pointer
1088 ImageHandle - The driver image handle
1089 SystemTable - The system table
1093 EFI_SUCCESS - The component name protocol is installed
1094 Others - Failed to init the usb driver
1098 return EfiLibInstallDriverBindingComponentName2 (
1101 &mUsbBusDriverBinding
,
1103 &mUsbBusComponentName
,
1104 &mUsbBusComponentName2
1110 Check whether USB bus driver support this device
1112 @param This The USB bus driver binding protocol
1113 @param Controller The controller handle to test againist
1114 @param RemainingDevicePath The remaining device path
1116 @retval EFI_SUCCESS The bus supports this controller.
1117 @retval EFI_UNSUPPORTED This device isn't supported
1122 UsbBusControllerDriverSupported (
1123 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1124 IN EFI_HANDLE Controller
,
1125 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1128 EFI_DEV_PATH_PTR DevicePathNode
;
1129 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
1130 EFI_USB2_HC_PROTOCOL
*Usb2Hc
;
1131 EFI_USB_HC_PROTOCOL
*UsbHc
;
1135 // Check whether device path is valid
1137 if (RemainingDevicePath
!= NULL
) {
1138 DevicePathNode
.DevPath
= RemainingDevicePath
;
1140 if ((DevicePathNode
.DevPath
->Type
!= MESSAGING_DEVICE_PATH
) ||
1141 (DevicePathNode
.DevPath
->SubType
!= MSG_USB_DP
&&
1142 DevicePathNode
.DevPath
->SubType
!= MSG_USB_CLASS_DP
1143 && DevicePathNode
.DevPath
->SubType
!= MSG_USB_WWID_DP
1146 return EFI_UNSUPPORTED
;
1150 Status
= gBS
->OpenProtocol (
1152 &gEfiDevicePathProtocolGuid
,
1153 (VOID
**) &ParentDevicePath
,
1154 This
->DriverBindingHandle
,
1156 EFI_OPEN_PROTOCOL_BY_DRIVER
1159 if (Status
== EFI_ALREADY_STARTED
) {
1163 if (EFI_ERROR (Status
)) {
1167 gBS
->CloseProtocol (
1169 &gEfiDevicePathProtocolGuid
,
1170 This
->DriverBindingHandle
,
1175 // Check whether USB_HC2 protocol is installed
1177 Status
= gBS
->OpenProtocol (
1179 &gEfiUsb2HcProtocolGuid
,
1181 This
->DriverBindingHandle
,
1183 EFI_OPEN_PROTOCOL_BY_DRIVER
1186 if (Status
== EFI_ALREADY_STARTED
) {
1190 if (!EFI_ERROR (Status
)) {
1191 gBS
->CloseProtocol (
1193 &gEfiUsb2HcProtocolGuid
,
1194 This
->DriverBindingHandle
,
1202 // If failed to open USB_HC2, fall back to USB_HC
1204 Status
= gBS
->OpenProtocol (
1206 &gEfiUsbHcProtocolGuid
,
1208 This
->DriverBindingHandle
,
1210 EFI_OPEN_PROTOCOL_BY_DRIVER
1213 if (Status
== EFI_ALREADY_STARTED
) {
1217 if (!EFI_ERROR (Status
)) {
1218 gBS
->CloseProtocol (
1220 &gEfiUsbHcProtocolGuid
,
1221 This
->DriverBindingHandle
,
1231 Start to process the controller
1233 @param This The USB bus driver binding instance
1234 @param Controller The controller to check
1235 @param RemainingDevicePath The remaining device patch
1237 @retval EFI_SUCCESS The controller is controlled by the usb bus
1238 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb
1240 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources
1245 UsbBusControllerDriverStart (
1246 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1247 IN EFI_HANDLE Controller
,
1248 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
1251 EFI_USB_BUS_PROTOCOL
*UsbBusId
;
1255 // Locate the USB bus protocol, if it is found, USB bus
1256 // is already started on this controller.
1258 Status
= gBS
->OpenProtocol (
1260 &mUsbBusProtocolGuid
,
1261 (VOID
**) &UsbBusId
,
1262 This
->DriverBindingHandle
,
1264 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1267 if (EFI_ERROR (Status
)) {
1269 // If first start, build the bus execute enviorment and install bus protocol
1271 Status
= UsbBusBuildProtocol (This
, Controller
, RemainingDevicePath
);
1272 if (EFI_ERROR (Status
)) {
1276 // Try get the Usb Bus protocol interface again
1278 Status
= gBS
->OpenProtocol (
1280 &mUsbBusProtocolGuid
,
1281 (VOID
**) &UsbBusId
,
1282 This
->DriverBindingHandle
,
1284 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1286 ASSERT (!EFI_ERROR (Status
));
1289 // USB Bus driver need to control the recursive connect policy of the bus, only those wanted
1290 // usb child device will be recursively connected.
1291 // The RemainingDevicePath indicate the child usb device which user want to fully recursively connecte this time.
1292 // All wanted usb child devices will be remembered by the usb bus driver itself.
1293 // If RemainingDevicePath == NULL, all the usb child devices in the usb bus are wanted devices.
1295 // Save the passed in RemainingDevicePath this time
1297 Status
= UsbBusAddWantedUsbIoDP (UsbBusId
, RemainingDevicePath
);
1298 ASSERT (!EFI_ERROR (Status
));
1300 // Ensure all wanted child usb devices are fully recursively connected
1302 Status
= UsbBusRecursivelyConnectWantedUsbIo (UsbBusId
);
1303 ASSERT (!EFI_ERROR (Status
));
1312 Stop handle the controller by this USB bus driver
1314 @param This The USB bus driver binding protocol
1315 @param Controller The controller to release
1316 @param NumberOfChildren The child of USB bus that opened controller
1318 @param ChildHandleBuffer The array of child handle
1320 @retval EFI_SUCCESS The controller or children are stopped
1321 @retval EFI_DEVICE_ERROR Failed to stop the driver
1326 UsbBusControllerDriverStop (
1327 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1328 IN EFI_HANDLE Controller
,
1329 IN UINTN NumberOfChildren
,
1330 IN EFI_HANDLE
*ChildHandleBuffer
1334 USB_DEVICE
*RootHub
;
1336 USB_INTERFACE
*RootIf
;
1337 USB_INTERFACE
*UsbIf
;
1338 EFI_USB_BUS_PROTOCOL
*BusId
;
1339 EFI_USB_IO_PROTOCOL
*UsbIo
;
1344 Status
= EFI_SUCCESS
;
1346 if (NumberOfChildren
> 0) {
1348 // BugBug: Raise TPL to callback level instead of USB_BUS_TPL to avoid TPL conflict
1350 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1352 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
1353 Status
= gBS
->OpenProtocol (
1354 ChildHandleBuffer
[Index
],
1355 &gEfiUsbIoProtocolGuid
,
1357 This
->DriverBindingHandle
,
1359 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1362 if (EFI_ERROR (Status
)) {
1364 // It is possible that the child has already been released:
1365 // 1. For combo device, free one device will release others.
1366 // 2. If a hub is released, all devices on its down facing
1367 // ports are released also.
1372 UsbIf
= USB_INTERFACE_FROM_USBIO (UsbIo
);
1373 UsbDev
= UsbIf
->Device
;
1375 UsbRemoveDevice (UsbDev
);
1378 gBS
->RestoreTPL (OldTpl
);
1382 DEBUG (( EFI_D_INFO
, "UsbBusStop: usb bus stopped on %x\n", Controller
));
1385 // Locate USB_BUS for the current host controller
1387 Status
= gBS
->OpenProtocol (
1389 &mUsbBusProtocolGuid
,
1391 This
->DriverBindingHandle
,
1393 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1396 if (EFI_ERROR (Status
)) {
1400 Bus
= USB_BUS_FROM_THIS (BusId
);
1403 // Stop the root hub, then free all the devices
1405 // BugBug: Raise TPL to callback level instead of USB_BUS_TPL to avoid TPL conflict
1407 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
1408 UsbHcSetState (Bus
, EfiUsbHcStateHalt
);
1410 RootHub
= Bus
->Devices
[0];
1411 RootIf
= RootHub
->Interfaces
[0];
1413 mUsbRootHubApi
.Release (RootIf
);
1415 for (Index
= 1; Index
< USB_MAX_DEVICES
; Index
++) {
1416 if (Bus
->Devices
[Index
] != NULL
) {
1417 UsbRemoveDevice (Bus
->Devices
[Index
]);
1421 gBS
->RestoreTPL (OldTpl
);
1423 gBS
->FreePool (RootIf
);
1424 gBS
->FreePool (RootHub
);
1425 Status
= UsbBusFreeUsbDPList (&Bus
->WantedUsbIoDPList
);
1426 ASSERT (!EFI_ERROR (Status
));
1429 // Uninstall the bus identifier and close USB_HC/USB2_HC protocols
1431 gBS
->UninstallProtocolInterface (Controller
, &mUsbBusProtocolGuid
, &Bus
->BusId
);
1433 if (Bus
->Usb2Hc
!= NULL
) {
1434 gBS
->CloseProtocol (
1436 &gEfiUsb2HcProtocolGuid
,
1437 This
->DriverBindingHandle
,
1442 if (Bus
->UsbHc
!= NULL
) {
1443 gBS
->CloseProtocol (
1445 &gEfiUsbHcProtocolGuid
,
1446 This
->DriverBindingHandle
,
1451 gBS
->CloseProtocol (
1453 &gEfiDevicePathProtocolGuid
,
1454 This
->DriverBindingHandle
,
1458 gBS
->FreePool (Bus
);
1463 EFI_DRIVER_BINDING_PROTOCOL mUsbBusDriverBinding
= {
1464 UsbBusControllerDriverSupported
,
1465 UsbBusControllerDriverStart
,
1466 UsbBusControllerDriverStop
,