+ USB_INTERFACE_DESC *IfDesc;\r
+ EFI_USB_INTERFACE_DESCRIPTOR *ActIfDesc;\r
+ EFI_USB_DEVICE_DESCRIPTOR *DevDesc;\r
+ EFI_USB_STRING_DESCRIPTOR *StrDesc;\r
+ UINT16 *SnString;\r
+\r
+ if ((UsbWWIDDevicePathPtr->Header.Type != MESSAGING_DEVICE_PATH) ||\r
+ (UsbWWIDDevicePathPtr->Header.SubType != MSG_USB_WWID_DP )){\r
+ ASSERT (0);\r
+ return FALSE;\r
+ }\r
+\r
+ IfDesc = UsbIf->IfDesc;\r
+ ActIfDesc = &(IfDesc->Settings[IfDesc->ActiveIndex]->Desc);\r
+ DevDesc = &(UsbIf->Device->DevDesc->Desc);\r
+ StrDesc = UsbGetOneString (UsbIf->Device, DevDesc->StrSerialNumber, USB_US_LAND_ID);\r
+ SnString = (UINT16 *) ((UINT8 *)UsbWWIDDevicePathPtr + 10);\r
+\r
+ //\r
+ //In addtion, hub interface is always matched for this policy.\r
+ //\r
+ if ((ActIfDesc->InterfaceClass == USB_HUB_CLASS_CODE) &&\r
+ (ActIfDesc->InterfaceSubClass == USB_HUB_SUBCLASS_CODE)) {\r
+ return TRUE;\r
+ }\r
+ //\r
+ // If connect wwid policy, determine the objective device by the serial number of\r
+ // device descriptor.\r
+ // Get serial number index from device descriptor, then get serial number by index\r
+ // and land id, compare the serial number with wwid device path node at last\r
+ //\r
+ // BugBug: only check serial number here, should check Interface Number, Device Vendor Id, Device Product Id in later version\r
+ //\r
+ if (StrDesc != NULL && !StrnCmp (StrDesc->String, SnString, StrDesc->Length)) {\r
+\r
+ return TRUE;\r
+ }\r
+\r
+ return FALSE;\r
+}\r
+\r
+/**\r
+ Free a DEVICE_PATH_LIST_ITEM list\r
+\r
+ @param UsbIoDPList a DEVICE_PATH_LIST_ITEM list pointer\r
+\r
+ @retval EFI_INVALID_PARAMETER\r
+ @retval EFI_SUCCESS\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UsbBusFreeUsbDPList (\r
+ IN LIST_ENTRY *UsbIoDPList\r
+ )\r
+{\r
+ LIST_ENTRY *ListIndex;\r
+ DEVICE_PATH_LIST_ITEM *ListItem;\r
+\r
+ //\r
+ // Check that ControllerHandle is a valid handle\r
+ //\r
+ if (UsbIoDPList == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ ListIndex = UsbIoDPList->ForwardLink;\r
+ while (ListIndex != UsbIoDPList){\r
+ ListItem = CR(ListIndex, DEVICE_PATH_LIST_ITEM, Link, DEVICE_PATH_LIST_ITEM_SIGNATURE);\r
+ //\r
+ // Free DEVICE_PATH_LIST_ITEM.DevicePath[]\r
+ //\r
+ if (ListItem->DevicePath != NULL){\r
+ FreePool(ListItem->DevicePath);\r
+ }\r
+ //\r
+ // Free DEVICE_PATH_LIST_ITEM itself\r
+ //\r
+ ListIndex = ListIndex->ForwardLink;\r
+ RemoveEntryList (&ListItem->Link);\r
+ FreePool (ListItem);\r
+ }\r
+\r
+ InitializeListHead (UsbIoDPList);\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Store a wanted usb child device info (its Usb part of device path) which is indicated by\r
+ RemainingDevicePath in a Usb bus which is indicated by UsbBusId\r
+\r
+ @param UsbBusId Point to EFI_USB_BUS_PROTOCOL interface\r
+ @param RemainingDevicePath The remaining device patch\r
+\r
+ @retval EFI_SUCCESS\r
+ @retval EFI_INVALID_PARAMETER\r
+ @retval EFI_OUT_OF_RESOURCES\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UsbBusAddWantedUsbIoDP (\r
+ IN EFI_USB_BUS_PROTOCOL *UsbBusId,\r
+ IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
+ )\r
+{\r
+ USB_BUS *Bus;\r
+ EFI_STATUS Status;\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePathPtr;\r
+\r
+ //\r
+ // Check whether remaining device path is valid\r
+ //\r
+ if (RemainingDevicePath != NULL) {\r
+ if ((RemainingDevicePath->Type != MESSAGING_DEVICE_PATH) ||\r
+ (RemainingDevicePath->SubType != MSG_USB_DP &&\r
+ RemainingDevicePath->SubType != MSG_USB_CLASS_DP\r
+ && RemainingDevicePath->SubType != MSG_USB_WWID_DP\r
+ )) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ }\r
+\r
+ if (UsbBusId == NULL){\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ Bus = USB_BUS_FROM_THIS (UsbBusId);\r
+\r
+ if (RemainingDevicePath == NULL) {\r
+ //\r
+ // RemainingDevicePath== NULL means all Usb devices in this bus are wanted.\r
+ // Here use a Usb class Device Path in WantedUsbIoDPList to indicate all Usb devices\r
+ // are wanted Usb devices\r
+ //\r
+ Status = UsbBusFreeUsbDPList (&Bus->WantedUsbIoDPList);\r
+ ASSERT (!EFI_ERROR (Status));\r
+ DevicePathPtr = DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL *) &mAllUsbClassDevicePath);\r
+ } else {\r
+ //\r
+ // Create new Usb device path according to the usb part in remaining device path\r
+ //\r
+ DevicePathPtr = GetUsbDPFromFullDP (RemainingDevicePath);\r
+ }\r
+\r
+ ASSERT (DevicePathPtr != NULL);\r
+ Status = AddUsbDPToList (DevicePathPtr, &Bus->WantedUsbIoDPList);\r
+ ASSERT (!EFI_ERROR (Status));\r
+ gBS->FreePool (DevicePathPtr);\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Check whether a usb child device is the wanted device in a bus\r
+\r
+ @param Bus The Usb bus's private data pointer\r
+ @param UsbIf The usb child device inferface\r
+\r
+ @retval EFI_SUCCESS\r
+ @retval EFI_INVALID_PARAMETER\r
+ @retval EFI_OUT_OF_RESOURCES\r
+\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+UsbBusIsWantedUsbIO (\r
+ IN USB_BUS *Bus,\r
+ IN USB_INTERFACE *UsbIf\r
+ )\r
+{\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePathPtr;\r
+ LIST_ENTRY *WantedUsbIoDPListPtr;\r
+ LIST_ENTRY *WantedListIndex;\r
+ DEVICE_PATH_LIST_ITEM *WantedListItem;\r
+ BOOLEAN DoConvert;\r
+ UINTN FirstDevicePathSize;\r
+\r
+ //\r
+ // Check whether passed in parameters are valid\r
+ //\r
+ if ((UsbIf == NULL) || (Bus == NULL)) {\r
+ return FALSE;\r
+ }\r
+ //\r
+ // Check whether UsbIf is Hub\r
+ //\r
+ if (UsbIf->IsHub) {\r
+ return TRUE;\r
+ }\r
+\r
+ //\r
+ // Check whether all Usb devices in this bus are wanted\r
+ //\r
+ if (SearchUsbDPInList ((EFI_DEVICE_PATH_PROTOCOL *)&mAllUsbClassDevicePath, &Bus->WantedUsbIoDPList)){\r
+ return TRUE;\r
+ }\r
+\r
+ //\r
+ // Check whether the Usb device match any item in WantedUsbIoDPList\r
+ //\r
+ WantedUsbIoDPListPtr = &Bus->WantedUsbIoDPList;\r
+ //\r
+ // Create new Usb device path according to the usb part in UsbIo full device path\r
+ //\r
+ DevicePathPtr = GetUsbDPFromFullDP (UsbIf->DevicePath);\r
+ ASSERT (DevicePathPtr != NULL);\r
+\r
+ DoConvert = FALSE;\r
+ WantedListIndex = WantedUsbIoDPListPtr->ForwardLink;\r
+ while (WantedListIndex != WantedUsbIoDPListPtr){\r
+ WantedListItem = CR(WantedListIndex, DEVICE_PATH_LIST_ITEM, Link, DEVICE_PATH_LIST_ITEM_SIGNATURE);\r
+ ASSERT (WantedListItem->DevicePath->Type == MESSAGING_DEVICE_PATH);\r
+ switch (WantedListItem->DevicePath->SubType) {\r
+ case MSG_USB_DP:\r
+ FirstDevicePathSize = GetDevicePathSize (WantedListItem->DevicePath);\r
+ if (FirstDevicePathSize == GetDevicePathSize (DevicePathPtr)) {\r
+ if (CompareMem (\r
+ WantedListItem->DevicePath,\r
+ DevicePathPtr,\r
+ GetDevicePathSize (DevicePathPtr)) == 0\r
+ ) {\r
+ DoConvert = TRUE;\r
+ }\r
+ }\r
+ break;\r
+ case MSG_USB_CLASS_DP:\r
+ if (MatchUsbClass((USB_CLASS_DEVICE_PATH *)WantedListItem->DevicePath, UsbIf)) {\r
+ DoConvert = TRUE;\r
+ }\r
+ break;\r
+ case MSG_USB_WWID_DP:\r
+ if (MatchUsbWwid((USB_WWID_DEVICE_PATH *)WantedListItem->DevicePath, UsbIf)) {\r
+ DoConvert = TRUE;\r
+ }\r
+ break;\r
+ default:\r
+ ASSERT (0);\r
+ break;\r
+ }\r
+\r
+ if (DoConvert) {\r
+ break;\r
+ }\r
+\r
+ WantedListIndex = WantedListIndex->ForwardLink;\r
+ }\r
+ gBS->FreePool (DevicePathPtr);\r
+\r
+ //\r
+ // Check whether the new Usb device path is wanted\r
+ //\r
+ if (DoConvert){\r
+ return TRUE;\r
+ } else {\r
+ return FALSE;\r
+ }\r
+}\r
+\r
+/**\r
+ Recursively connnect every wanted usb child device to ensure they all fully connected.\r
+ Check all the child Usb IO handles in this bus, recursively connecte if it is wanted usb child device\r
+\r
+ @param UsbBusId point to EFI_USB_BUS_PROTOCOL interface\r
+\r
+ @retval EFI_SUCCESS\r
+ @retval EFI_INVALID_PARAMETER\r
+ @retval EFI_OUT_OF_RESOURCES\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UsbBusRecursivelyConnectWantedUsbIo (\r
+ IN EFI_USB_BUS_PROTOCOL *UsbBusId\r
+ )\r
+{\r
+ USB_BUS *Bus;\r
+ EFI_STATUS Status;\r
+ UINTN Index;\r
+ EFI_USB_IO_PROTOCOL *UsbIo;\r
+ USB_INTERFACE *UsbIf;\r
+ UINTN UsbIoHandleCount;\r
+ EFI_HANDLE *UsbIoBuffer;\r
+ EFI_DEVICE_PATH_PROTOCOL *UsbIoDevicePath;\r
+\r
+ if (UsbBusId == NULL){\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ Bus = USB_BUS_FROM_THIS (UsbBusId);\r
+\r
+ //\r
+ // Get all Usb IO handles in system\r
+ //\r
+ UsbIoHandleCount = 0;\r
+ Status = gBS->LocateHandleBuffer (ByProtocol, &gEfiUsbIoProtocolGuid, NULL, &UsbIoHandleCount, &UsbIoBuffer);\r
+ if (Status == EFI_NOT_FOUND || UsbIoHandleCount == 0) {\r
+ return EFI_SUCCESS;\r
+ }\r
+ ASSERT (!EFI_ERROR (Status));\r
+\r
+ for (Index = 0; Index < UsbIoHandleCount; Index++) {\r
+ //\r
+ // Check whether the USB IO handle is a child of this bus\r
+ // Note: The usb child handle maybe invalid because of hot plugged out during the loop\r
+ //\r
+ UsbIoDevicePath = NULL;\r
+ Status = gBS->HandleProtocol (UsbIoBuffer[Index], &gEfiDevicePathProtocolGuid, (VOID *) &UsbIoDevicePath);\r
+ if (EFI_ERROR (Status) || UsbIoDevicePath == NULL) {\r
+ continue;\r
+ }\r
+ if (CompareMem (\r
+ UsbIoDevicePath,\r
+ Bus->DevicePath,\r
+ (GetDevicePathSize (Bus->DevicePath) - sizeof (EFI_DEVICE_PATH_PROTOCOL))\r
+ ) != 0) {\r
+ continue;\r
+ }\r
+\r
+ //\r
+ // Get the child Usb IO interface\r
+ //\r
+ Status = gBS->HandleProtocol(\r
+ UsbIoBuffer[Index],\r
+ &gEfiUsbIoProtocolGuid,\r
+ (VOID **) &UsbIo\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ continue;\r
+ }\r
+ UsbIf = USB_INTERFACE_FROM_USBIO (UsbIo);\r
+\r
+ if (UsbBusIsWantedUsbIO (Bus, UsbIf)) {\r
+ if (!UsbIf->IsManaged) {\r
+ //\r
+ // Recursively connect the wanted Usb Io handle\r
+ //\r
+ DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL before connect is %d\n", UsbGetCurrentTpl ()));\r
+ Status = gBS->ConnectController (UsbIf->Handle, NULL, NULL, TRUE);\r
+ UsbIf->IsManaged = (BOOLEAN)!EFI_ERROR (Status);\r
+ DEBUG ((EFI_D_INFO, "UsbConnectDriver: TPL after connect is %d\n", UsbGetCurrentTpl()));\r
+ }\r
+ }\r
+ }\r