#include <Library/DebugCommunicationLib.h>\r
#include <Library/BaseMemoryLib.h>\r
#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
\r
#define SETUP_PID 0x2D\r
#define INPUT_PID 0x69\r
// The usb debug port memory BAR number in EHCI configuration space.\r
//\r
UINT8 DebugPortBarNumber;\r
- UINT8 Reserved;\r
+ BOOLEAN Initialized;\r
//\r
// The offset of usb debug port registers in EHCI memory range.\r
//\r
)\r
{\r
UINT16 PciStatus;\r
+ UINT16 VendorId;\r
+ UINT16 DeviceId;\r
+ UINT8 ProgInterface;\r
+ UINT8 SubClassCode;\r
+ UINT8 BaseCode;\r
UINT8 CapabilityPtr;\r
UINT8 CapabilityId;\r
\r
+ VendorId = PciRead16 (PcdGet32(PcdUsbEhciPciAddress) + PCI_VENDOR_ID_OFFSET);\r
+ DeviceId = PciRead16 (PcdGet32(PcdUsbEhciPciAddress) + PCI_DEVICE_ID_OFFSET);\r
+ \r
+ if ((VendorId == 0xFFFF) || (DeviceId == 0xFFFF)) {\r
+ return RETURN_UNSUPPORTED;\r
+ }\r
+\r
+ ProgInterface = PciRead8 (PcdGet32(PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET);\r
+ SubClassCode = PciRead8 (PcdGet32(PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET + 1);\r
+ BaseCode = PciRead8 (PcdGet32(PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET + 2);\r
+ \r
+ if ((ProgInterface != PCI_IF_EHCI) || (SubClassCode != PCI_CLASS_SERIAL_USB) || (BaseCode != PCI_CLASS_SERIAL)) {\r
+ return RETURN_UNSUPPORTED;\r
+ }\r
+\r
//\r
// Enable Ehci Host Controller MMIO Space.\r
//\r
}\r
\r
*Length = (UINT8)(MmioRead32((UINTN)&DebugPortRegister->ControlStatus) & 0xF);\r
- ASSERT (*Length <= 8);\r
+ if (*Length > 8) {\r
+ return RETURN_DEVICE_ERROR;\r
+ }\r
+\r
for (Index = 0; Index < *Length; Index++) {\r
Buffer[Index] = DebugPortRegister->DataBuffer[Index];\r
}\r
//\r
return Status;\r
}\r
- ASSERT (Length == sizeof(USB_DEBUG_PORT_DESCRIPTOR));\r
+\r
+ if (Length != sizeof(USB_DEBUG_PORT_DESCRIPTOR)) {\r
+ return RETURN_DEVICE_ERROR;\r
+ }\r
+\r
//\r
// set usb debug device address as 0x7F.\r
//\r
UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r
}\r
\r
+ //\r
+ // Check if debug port is ready\r
+ //\r
+ if (!UsbDebugPortHandle->Initialized) {\r
+ return 0;\r
+ }\r
+\r
if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
Status = InitializeUsbDebugHardware (UsbDebugPortHandle);\r
if (RETURN_ERROR(Status)) {\r
UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r
}\r
\r
+ //\r
+ // Check if debug port is ready\r
+ //\r
+ if (!UsbDebugPortHandle->Initialized) {\r
+ return 0;\r
+ }\r
+\r
if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
Status = InitializeUsbDebugHardware (UsbDebugPortHandle);\r
if (RETURN_ERROR(Status)) {\r
UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r
}\r
\r
+ //\r
+ // Check if debug port is ready\r
+ //\r
+ if (!UsbDebugPortHandle->Initialized) {\r
+ return 0;\r
+ }\r
+\r
if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
Status = InitializeUsbDebugHardware(UsbDebugPortHandle);\r
if (RETURN_ERROR(Status)) {\r
\r
Status = CalculateUsbDebugPortBar(&Handle.DebugPortOffset, &Handle.DebugPortBarNumber);\r
if (RETURN_ERROR (Status)) {\r
- return NULL;\r
+ DEBUG ((EFI_D_ERROR, "USB Debug Port: the pci device pointed by PcdUsbEhciPciAddress is not EHCI host controller or does not support debug port capability!\n"));\r
+ goto Exit;\r
}\r
\r
Handle.EhciMemoryBase = 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET);\r
\r
Status = InitializeUsbDebugHardware (&Handle);\r
if (RETURN_ERROR(Status)) {\r
- return NULL;\r
+ DEBUG ((EFI_D_ERROR, "USB Debug Port: Initialization failed, please check if USB debug cable is plugged into EHCI debug port correctly!\n"));\r
+ goto Exit;\r
}\r
\r
+ //\r
+ // Set debug port initialized successfully flag\r
+ //\r
+ Handle.Initialized = TRUE;\r
+\r
+Exit:\r
+\r
if (Function != NULL) {\r
Function (Context, &Handle);\r
} else {\r