DebugCommunicationLibUsb.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. /** @file
  2. Debug Port Library implementation based on usb debug port.
  3. Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Base.h>
  7. #include <IndustryStandard/Pci.h>
  8. #include <IndustryStandard/Usb.h>
  9. #include <Library/IoLib.h>
  10. #include <Library/PciLib.h>
  11. #include <Library/PcdLib.h>
  12. #include <Library/TimerLib.h>
  13. #include <Library/DebugCommunicationLib.h>
  14. #include <Library/BaseMemoryLib.h>
  15. #include <Library/BaseLib.h>
  16. #include <Library/DebugLib.h>
  17. #define SETUP_PID 0x2D
  18. #define INPUT_PID 0x69
  19. #define OUTPUT_PID 0xE1
  20. #define ERROR_PID 0x55
  21. #define DATA0_PID 0xC3
  22. #define DATA1_PID 0x4B
  23. #define DATA2_PID 0x87
  24. #define MDATA_PID 0x0F
  25. #define ACK_PID 0xD2
  26. #define NAK_PID 0x5A
  27. #define STALL_PID 0x1E
  28. #define NYET_PID 0x96
  29. #define PCI_CAPABILITY_ID_DEBUG_PORT 0x0A
  30. #define USB_DEBUG_PORT_MAX_PACKET_SIZE 0x08
  31. #define USB_DEBUG_PORT_IN_USE BIT10
  32. #define USB_DEBUG_PORT_ENABLE BIT28
  33. #define USB_DEBUG_PORT_OWNER BIT30
  34. #define USB_PORT_LINE_STATUS_LS 0x400
  35. #define USB_PORT_LINE_STATUS_MASK 0xC00
  36. //
  37. // Usb debug device descriptor, which is defined at
  38. // USB2 Debug Device Specification.
  39. //
  40. typedef struct _USB_DEBUG_PORT_DESCRIPTOR {
  41. UINT8 Length;
  42. UINT8 DescriptorType;
  43. UINT8 DebugInEndpoint;
  44. UINT8 DebugOutEndpoint;
  45. } USB_DEBUG_PORT_DESCRIPTOR;
  46. USB_DEVICE_REQUEST mDebugCommunicationLibUsbGetDebugDescriptor = {
  47. 0x80,
  48. USB_REQ_GET_DESCRIPTOR,
  49. (UINT16)(0x0A << 8),
  50. 0x0000,
  51. sizeof (USB_DEBUG_PORT_DESCRIPTOR)
  52. };
  53. USB_DEVICE_REQUEST mDebugCommunicationLibUsbSetDebugFeature = {
  54. 0x0,
  55. USB_REQ_SET_FEATURE,
  56. (UINT16)(0x06),
  57. 0x0000,
  58. 0x0
  59. };
  60. USB_DEVICE_REQUEST mDebugCommunicationLibUsbSetDebugAddress = {
  61. 0x0,
  62. USB_REQ_SET_ADDRESS,
  63. (UINT16)(0x7F),
  64. 0x0000,
  65. 0x0
  66. };
  67. //
  68. // Usb debug port register file, which is defined at
  69. // EHCI Specification.
  70. //
  71. typedef struct _USB_DEBUG_PORT_REGISTER {
  72. UINT32 ControlStatus;
  73. UINT8 TokenPid;
  74. UINT8 SendPid;
  75. UINT8 ReceivedPid;
  76. UINT8 Reserved1;
  77. UINT8 DataBuffer[8];
  78. UINT8 UsbEndPoint;
  79. UINT8 UsbAddress;
  80. UINT8 Reserved2;
  81. UINT8 Reserved3;
  82. } USB_DEBUG_PORT_REGISTER;
  83. //
  84. // The state machine of usb debug port
  85. //
  86. #define USBDBG_NO_DEV 0 // No device present at debug port
  87. #define USBDBG_NO_DBG_CAB 1 // The device attached is not usb debug cable
  88. #define USBDBG_DBG_CAB 2 // The device attached is usb debug cable
  89. #define USBDBG_INIT_DONE 4 // The usb debug cable device is initialized
  90. #define USBDBG_RESET 8 // The system is reset
  91. #pragma pack(1)
  92. //
  93. // The internal data structure of DEBUG_PORT_HANDLE, which stores some
  94. // important datum which are used across various phases.
  95. //
  96. typedef struct _USB_DEBUG_PORT_HANDLE {
  97. //
  98. // The usb debug port memory BAR number in EHCI configuration space.
  99. //
  100. UINT8 DebugPortBarNumber;
  101. UINT8 Initialized;
  102. //
  103. // The offset of usb debug port registers in EHCI memory range.
  104. //
  105. UINT16 DebugPortOffset;
  106. //
  107. // The usb debug port memory BAR address.
  108. //
  109. UINT32 UsbDebugPortMemoryBase;
  110. //
  111. // The EHCI memory BAR address.
  112. //
  113. UINT32 EhciMemoryBase;
  114. //
  115. // The usb debug device In endpoint.
  116. //
  117. UINT8 InEndpoint;
  118. //
  119. // The usb debug device Out endpoint.
  120. //
  121. UINT8 OutEndpoint;
  122. //
  123. // The Bulk In endpoint toggle bit.
  124. //
  125. UINT8 BulkInToggle;
  126. //
  127. // The Bulk Out endpoint toggle bit.
  128. //
  129. UINT8 BulkOutToggle;
  130. //
  131. // The available data length in the following data buffer.
  132. //
  133. UINT8 DataCount;
  134. //
  135. // The data buffer. Maximum length is 8 bytes.
  136. //
  137. UINT8 Data[8];
  138. } USB_DEBUG_PORT_HANDLE;
  139. #pragma pack()
  140. //
  141. // The global variable which can be used after memory is ready.
  142. //
  143. USB_DEBUG_PORT_HANDLE mDebugCommunicationLibUsbDebugPortHandle;
  144. /**
  145. Calculate the usb debug port bar address.
  146. @param DebugPortOffset Get usb debug port offset in the usb debug port memory space.
  147. @param DebugPortBarNumbar Get the bar number at which usb debug port is located.
  148. @retval RETURN_UNSUPPORTED The usb host controller does not supported usb debug port capability.
  149. @retval RETURN_SUCCESS Get bar and offset successfully.
  150. **/
  151. RETURN_STATUS
  152. EFIAPI
  153. CalculateUsbDebugPortBar (
  154. OUT UINT16 *DebugPortOffset,
  155. OUT UINT8 *DebugPortBarNumbar
  156. )
  157. {
  158. UINT16 PciStatus;
  159. UINT16 VendorId;
  160. UINT16 DeviceId;
  161. UINT8 ProgInterface;
  162. UINT8 SubClassCode;
  163. UINT8 BaseCode;
  164. UINT8 CapabilityPtr;
  165. UINT8 CapabilityId;
  166. VendorId = PciRead16 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_VENDOR_ID_OFFSET);
  167. DeviceId = PciRead16 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_DEVICE_ID_OFFSET);
  168. if ((VendorId == 0xFFFF) || (DeviceId == 0xFFFF)) {
  169. return RETURN_UNSUPPORTED;
  170. }
  171. ProgInterface = PciRead8 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET);
  172. SubClassCode = PciRead8 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET + 1);
  173. BaseCode = PciRead8 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET + 2);
  174. if ((ProgInterface != PCI_IF_EHCI) || (SubClassCode != PCI_CLASS_SERIAL_USB) || (BaseCode != PCI_CLASS_SERIAL)) {
  175. return RETURN_UNSUPPORTED;
  176. }
  177. //
  178. // Enable Ehci Host Controller MMIO Space.
  179. //
  180. PciStatus = PciRead16 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_PRIMARY_STATUS_OFFSET);
  181. if ((PciStatus & EFI_PCI_STATUS_CAPABILITY) == 0) {
  182. //
  183. // The Pci Device Doesn't Support Capability Pointer.
  184. //
  185. return RETURN_UNSUPPORTED;
  186. }
  187. //
  188. // Get Pointer To Capability List
  189. //
  190. CapabilityPtr = PciRead8 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_CAPBILITY_POINTER_OFFSET);
  191. //
  192. // Find Capability ID 0xA, Which Is For Debug Port
  193. //
  194. while (CapabilityPtr != 0) {
  195. CapabilityId = PciRead8 (PcdGet32 (PcdUsbEhciPciAddress) + CapabilityPtr);
  196. if (CapabilityId == PCI_CAPABILITY_ID_DEBUG_PORT) {
  197. break;
  198. }
  199. CapabilityPtr = PciRead8 (PcdGet32 (PcdUsbEhciPciAddress) + CapabilityPtr + 1);
  200. }
  201. //
  202. // No Debug Port Capability Found
  203. //
  204. if (CapabilityPtr == 0) {
  205. return RETURN_UNSUPPORTED;
  206. }
  207. //
  208. // Get The Base Address Of Debug Port Register In Debug Port Capability Register
  209. //
  210. *DebugPortOffset = (UINT16)(PciRead16 (PcdGet32 (PcdUsbEhciPciAddress) + CapabilityPtr + 2) & 0x1FFF);
  211. *DebugPortBarNumbar = (UINT8)((PciRead16 (PcdGet32 (PcdUsbEhciPciAddress) + CapabilityPtr + 2) >> 13) - 1);
  212. return RETURN_SUCCESS;
  213. }
  214. /**
  215. Do a usb IN transaction by usb debug port.
  216. @param DebugPortRegister Pointer to the base address of usb debug port register interface.
  217. @param Buffer Pointer to the buffer receiving data.
  218. @param Length Number of bytes of the received data.
  219. @param Token The token PID for each USB transaction.
  220. @param Addr The usb device address for usb transaction.
  221. @param Ep The endpoint for usb transaction.
  222. @param DataToggle The toggle bit used at usb transaction.
  223. @retval RETURN_SUCCESS The IN transaction is executed successfully.
  224. @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
  225. @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
  226. **/
  227. RETURN_STATUS
  228. EFIAPI
  229. UsbDebugPortIn (
  230. IN USB_DEBUG_PORT_REGISTER *DebugPortRegister,
  231. IN OUT UINT8 *Buffer,
  232. OUT UINT8 *Length,
  233. IN UINT8 Token,
  234. IN UINT8 Addr,
  235. IN UINT8 Ep,
  236. IN UINT8 DataToggle
  237. )
  238. {
  239. UINTN Index;
  240. if (Length == NULL) {
  241. return RETURN_INVALID_PARAMETER;
  242. }
  243. *Length = 0;
  244. DebugPortRegister->TokenPid = Token;
  245. if (DataToggle != 0) {
  246. DebugPortRegister->SendPid = DATA1_PID;
  247. } else {
  248. DebugPortRegister->SendPid = DATA0_PID;
  249. }
  250. DebugPortRegister->UsbAddress = (UINT8)(Addr & 0x7F);
  251. DebugPortRegister->UsbEndPoint = (UINT8)(Ep & 0xF);
  252. //
  253. // Clearing W/R bit to indicate it's a READ operation
  254. //
  255. MmioAnd32 ((UINTN)&DebugPortRegister->ControlStatus, (UINT32) ~BIT4);
  256. //
  257. // Setting GO bit as well as clearing DONE bit
  258. //
  259. MmioOr32 ((UINTN)&DebugPortRegister->ControlStatus, (UINT32)BIT5);
  260. //
  261. // Wait for completing the request
  262. //
  263. while ((MmioRead32 ((UINTN)&DebugPortRegister->ControlStatus) & (UINT32)BIT16) == 0) {
  264. if ((MmioRead32 ((UINTN)&DebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE))
  265. != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE))
  266. {
  267. return RETURN_DEVICE_ERROR;
  268. }
  269. }
  270. //
  271. // Clearing DONE bit by writing 1
  272. //
  273. MmioOr32 ((UINTN)&DebugPortRegister->ControlStatus, BIT16);
  274. //
  275. // Check if the request is executed successfully or not.
  276. //
  277. if ((MmioRead32 ((UINTN)&DebugPortRegister->ControlStatus)) & BIT6) {
  278. return RETURN_DEVICE_ERROR;
  279. }
  280. //
  281. // Make sure the received data are not beyond the allowable maximum length - 8 byte
  282. //
  283. if (((MmioRead32 ((UINTN)&DebugPortRegister->ControlStatus)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE) {
  284. return RETURN_DEVICE_ERROR;
  285. }
  286. *Length = (UINT8)(MmioRead32 ((UINTN)&DebugPortRegister->ControlStatus) & 0xF);
  287. if (*Length > 8) {
  288. return RETURN_DEVICE_ERROR;
  289. }
  290. for (Index = 0; Index < *Length; Index++) {
  291. Buffer[Index] = DebugPortRegister->DataBuffer[Index];
  292. }
  293. return RETURN_SUCCESS;
  294. }
  295. /**
  296. Do a usb SETUP/OUT transaction by usb debug port.
  297. @param DebugPortRegister Pointer to the base address of usb debug port register interface.
  298. @param Buffer Pointer to the buffer receiving data.
  299. @param Length Number of bytes of the received data.
  300. @param Token The token PID for each USB transaction.
  301. @param Addr The usb device address for usb transaction.
  302. @param Ep The endpoint for usb transaction.
  303. @param DataToggle The toggle bit used at usb transaction.
  304. @retval RETURN_SUCCESS The IN transaction is executed successfully.
  305. @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
  306. @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
  307. **/
  308. RETURN_STATUS
  309. EFIAPI
  310. UsbDebugPortOut (
  311. IN USB_DEBUG_PORT_REGISTER *DebugPortRegister,
  312. IN UINT8 *Buffer,
  313. IN UINT8 Length,
  314. IN UINT8 Token,
  315. IN UINT8 Addr,
  316. IN UINT8 Ep,
  317. IN UINT8 DataToggle
  318. )
  319. {
  320. UINT8 Index;
  321. if (Length > 8) {
  322. return RETURN_INVALID_PARAMETER;
  323. }
  324. DebugPortRegister->TokenPid = Token;
  325. if (DataToggle != 0) {
  326. DebugPortRegister->SendPid = DATA1_PID;
  327. } else {
  328. DebugPortRegister->SendPid = DATA0_PID;
  329. }
  330. DebugPortRegister->UsbAddress = (UINT8)(Addr & 0x7F);
  331. DebugPortRegister->UsbEndPoint = (UINT8)(Ep & 0xF);
  332. //
  333. // Fill in the data length and corresponding data.
  334. //
  335. MmioAnd32 ((UINTN)&DebugPortRegister->ControlStatus, (UINT32) ~0xF);
  336. MmioOr32 ((UINTN)&DebugPortRegister->ControlStatus, Length & 0xF);
  337. for (Index = 0; Index < Length; Index++) {
  338. DebugPortRegister->DataBuffer[Index] = Buffer[Index];
  339. }
  340. //
  341. // Setting W/R bit to indicate it's a WRITE operation
  342. //
  343. MmioOr32 ((UINTN)&DebugPortRegister->ControlStatus, BIT4);
  344. //
  345. // Setting GO bit as well as clearing DONE bit
  346. //
  347. MmioOr32 ((UINTN)&DebugPortRegister->ControlStatus, BIT5);
  348. //
  349. // Wait for completing the request
  350. //
  351. while ((MmioRead32 ((UINTN)&DebugPortRegister->ControlStatus) & BIT16) == 0) {
  352. if ((MmioRead32 ((UINTN)&DebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE))
  353. != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE))
  354. {
  355. return RETURN_DEVICE_ERROR;
  356. }
  357. }
  358. //
  359. // Clearing DONE bit by writing 1
  360. //
  361. MmioOr32 ((UINTN)&DebugPortRegister->ControlStatus, BIT16);
  362. //
  363. // Check if the request is executed successfully or not.
  364. //
  365. if ((MmioRead32 ((UINTN)&DebugPortRegister->ControlStatus)) & BIT6) {
  366. return RETURN_DEVICE_ERROR;
  367. }
  368. //
  369. // Make sure the sent data are not beyond the allowable maximum length - 8 byte
  370. //
  371. if (((MmioRead32 ((UINTN)&DebugPortRegister->ControlStatus)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE) {
  372. return RETURN_DEVICE_ERROR;
  373. }
  374. return RETURN_SUCCESS;
  375. }
  376. /**
  377. Do a usb control transfer by usb debug port.
  378. @param DebugPortRegister Pointer to the base address of usb debug port register interface.
  379. @param SetupPacket The token PID for each USB transaction.
  380. @param Addr The usb device address for usb transaction.
  381. @param Ep The endpoint for usb transaction.
  382. @param Data Pointer to the buffer receiving data.
  383. @param DataLength Number of bytes of the received data.
  384. @retval RETURN_SUCCESS The IN transaction is executed successfully.
  385. @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
  386. @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
  387. **/
  388. RETURN_STATUS
  389. EFIAPI
  390. UsbDebugPortControlTransfer (
  391. IN USB_DEBUG_PORT_REGISTER *DebugPortRegister,
  392. IN USB_DEVICE_REQUEST *SetupPacket,
  393. IN UINT8 Addr,
  394. IN UINT8 Ep,
  395. OUT UINT8 *Data,
  396. IN OUT UINT8 *DataLength
  397. )
  398. {
  399. RETURN_STATUS Status;
  400. UINT8 Temp;
  401. UINT8 ReturnStatus[8];
  402. //
  403. // Setup Phase
  404. //
  405. Status = UsbDebugPortOut (DebugPortRegister, (UINT8 *)SetupPacket, (UINT8)sizeof (USB_DEVICE_REQUEST), SETUP_PID, Addr, Ep, 0);
  406. if (RETURN_ERROR (Status)) {
  407. return Status;
  408. }
  409. //
  410. // Data Phase
  411. //
  412. if (DataLength != 0) {
  413. if ((SetupPacket->RequestType & BIT7) != 0) {
  414. //
  415. // Get Data From Device
  416. //
  417. Status = UsbDebugPortIn (DebugPortRegister, Data, DataLength, INPUT_PID, Addr, Ep, 1);
  418. if (RETURN_ERROR (Status)) {
  419. return Status;
  420. }
  421. } else {
  422. //
  423. // Send Data To Device
  424. //
  425. Status = UsbDebugPortOut (DebugPortRegister, Data, *DataLength, OUTPUT_PID, Addr, Ep, 1);
  426. if (RETURN_ERROR (Status)) {
  427. return Status;
  428. }
  429. }
  430. }
  431. //
  432. // Status Phase
  433. //
  434. if ((SetupPacket->RequestType & BIT7) != 0) {
  435. //
  436. // For READ operation, Data Toggle in Status Phase Should be 1.
  437. //
  438. Status = UsbDebugPortOut (DebugPortRegister, NULL, 0, OUTPUT_PID, Addr, Ep, 1);
  439. } else {
  440. //
  441. // For WRITE operation, Data Toggle in Status Phase Should be 1.
  442. //
  443. Status = UsbDebugPortIn (DebugPortRegister, ReturnStatus, &Temp, INPUT_PID, Addr, Ep, 1);
  444. }
  445. return Status;
  446. }
  447. /**
  448. Check if it needs to re-initialize usb debug port hardware.
  449. During different phases switch, such as SEC to PEI or PEI to DXE or DXE to SMM, we should check
  450. whether the usb debug port hardware configuration is changed. Such case can be triggered by
  451. Pci bus resource allocation and so on.
  452. @param Handle Debug port handle.
  453. @retval TRUE The usb debug port hardware configuration is changed.
  454. @retval FALSE The usb debug port hardware configuration is not changed.
  455. **/
  456. BOOLEAN
  457. EFIAPI
  458. NeedReinitializeHardware (
  459. IN USB_DEBUG_PORT_HANDLE *Handle
  460. )
  461. {
  462. UINT16 PciCmd;
  463. UINT32 UsbDebugPortMemoryBase;
  464. UINT32 EhciMemoryBase;
  465. BOOLEAN Status;
  466. USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister;
  467. Status = FALSE;
  468. EhciMemoryBase = 0xFFFFFC00 & PciRead32 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET);
  469. if (EhciMemoryBase != Handle->EhciMemoryBase) {
  470. Handle->EhciMemoryBase = EhciMemoryBase;
  471. Status = TRUE;
  472. }
  473. UsbDebugPortMemoryBase = 0xFFFFFC00 & PciRead32 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET + Handle->DebugPortBarNumber * 4);
  474. if (UsbDebugPortMemoryBase != Handle->UsbDebugPortMemoryBase) {
  475. Handle->UsbDebugPortMemoryBase = UsbDebugPortMemoryBase;
  476. Status = TRUE;
  477. }
  478. //
  479. // Enable Ehci Memory Space Access
  480. //
  481. PciCmd = PciRead16 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_COMMAND_OFFSET);
  482. if (((PciCmd & EFI_PCI_COMMAND_MEMORY_SPACE) == 0) || ((PciCmd & EFI_PCI_COMMAND_BUS_MASTER) == 0)) {
  483. PciCmd |= EFI_PCI_COMMAND_MEMORY_SPACE | EFI_PCI_COMMAND_BUS_MASTER;
  484. PciWrite16 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_COMMAND_OFFSET, PciCmd);
  485. Status = TRUE;
  486. }
  487. //
  488. // If the owner and in_use bit is not set, it means system is doing cold/warm boot or EHCI host controller is reset by system software.
  489. //
  490. UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)((UINTN)Handle->UsbDebugPortMemoryBase + Handle->DebugPortOffset);
  491. if ((MmioRead32 ((UINTN)&UsbDebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_ENABLE | USB_DEBUG_PORT_IN_USE))
  492. != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_ENABLE | USB_DEBUG_PORT_IN_USE))
  493. {
  494. Status = TRUE;
  495. }
  496. if (Handle->Initialized == USBDBG_RESET) {
  497. Status = TRUE;
  498. } else if (Handle->Initialized != USBDBG_INIT_DONE) {
  499. Status = TRUE;
  500. }
  501. return Status;
  502. }
  503. /**
  504. Initialize usb debug port hardware.
  505. 1. reset ehci host controller.
  506. 2. set right port to debug port.
  507. 3. find a usb debug device is attached by getting debug device descriptor.
  508. 4. set address for the usb debug device.
  509. 5. configure the usb debug device to debug mode.
  510. @param Handle Debug port handle.
  511. @retval TRUE The usb debug port hardware configuration is changed.
  512. @retval FALSE The usb debug port hardware configuration is not changed.
  513. **/
  514. RETURN_STATUS
  515. EFIAPI
  516. InitializeUsbDebugHardware (
  517. IN USB_DEBUG_PORT_HANDLE *Handle
  518. )
  519. {
  520. RETURN_STATUS Status;
  521. USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister;
  522. USB_DEBUG_PORT_DESCRIPTOR UsbDebugPortDescriptor;
  523. UINT32 *PortStatus;
  524. UINT32 *UsbCmd;
  525. UINT32 *UsbStatus;
  526. UINT32 *UsbHCSParam;
  527. UINT8 DebugPortNumber;
  528. UINT8 Length;
  529. UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)((UINTN)Handle->UsbDebugPortMemoryBase + Handle->DebugPortOffset);
  530. UsbHCSParam = (UINT32 *)((UINTN)Handle->EhciMemoryBase + 0x04);
  531. UsbCmd = (UINT32 *)((UINTN)Handle->EhciMemoryBase + 0x20);
  532. UsbStatus = (UINT32 *)((UINTN)Handle->EhciMemoryBase + 0x24);
  533. //
  534. // Check if the debug port is enabled and owned by myself.
  535. //
  536. if (((MmioRead32 ((UINTN)&UsbDebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE))
  537. != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE)) || (Handle->Initialized == USBDBG_RESET))
  538. {
  539. DEBUG ((
  540. DEBUG_INFO,
  541. "UsbDbg: Need to reset the host controller. ControlStatus = %08x\n",
  542. MmioRead32 ((UINTN)&UsbDebugPortRegister->ControlStatus)
  543. ));
  544. //
  545. // If the host controller is halted, then reset and restart it.
  546. //
  547. if ((MmioRead32 ((UINTN)UsbStatus) & BIT12) != 0) {
  548. DEBUG ((DEBUG_INFO, "UsbDbg: Reset the host controller.\n"));
  549. //
  550. // reset the host controller.
  551. //
  552. MmioOr32 ((UINTN)UsbCmd, BIT1);
  553. //
  554. // ensure that the host controller is reset.
  555. //
  556. while ((MmioRead32 ((UINTN)UsbCmd) & BIT1) != 0) {
  557. }
  558. MmioOr32 ((UINTN)UsbCmd, BIT0);
  559. // ensure that the host controller is started (HALTED bit must be cleared)
  560. while ((MmioRead32 ((UINTN)UsbStatus) & BIT12) != 0) {
  561. }
  562. }
  563. //
  564. // First get the ownership of port 0.
  565. //
  566. MmioOr32 ((UINTN)&UsbDebugPortRegister->ControlStatus, USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE);
  567. MicroSecondDelay (200000);
  568. }
  569. //
  570. // Find out which port is used as debug port.
  571. //
  572. DebugPortNumber = (UINT8)((MmioRead32 ((UINTN)UsbHCSParam) & 0x00F00000) >> 20);
  573. //
  574. // Should find a device is connected at debug port
  575. //
  576. PortStatus = (UINT32 *)((UINTN)Handle->EhciMemoryBase + 0x64 + (DebugPortNumber - 1) * 4);
  577. if (!(MmioRead32 ((UINTN)PortStatus) & BIT0)) {
  578. Handle->Initialized = USBDBG_NO_DEV;
  579. return RETURN_NOT_FOUND;
  580. }
  581. if ((Handle->Initialized != USBDBG_INIT_DONE) ||
  582. ((MmioRead32 ((UINTN)&UsbDebugPortRegister->ControlStatus) & USB_DEBUG_PORT_ENABLE) == 0))
  583. {
  584. DEBUG ((DEBUG_INFO, "UsbDbg: Reset the debug port.\n"));
  585. //
  586. // Reset the debug port
  587. //
  588. MmioOr32 ((UINTN)PortStatus, BIT8);
  589. MicroSecondDelay (500000);
  590. MmioAnd32 ((UINTN)PortStatus, (UINT32) ~BIT8);
  591. while (MmioRead32 ((UINTN)PortStatus) & BIT8) {
  592. }
  593. //
  594. // The port enabled bit should be set by HW.
  595. //
  596. if ((MmioRead32 ((UINTN)PortStatus) & BIT2) == 0) {
  597. Handle->Initialized = USBDBG_NO_DBG_CAB;
  598. return RETURN_DEVICE_ERROR;
  599. }
  600. //
  601. // Enable Usb Debug Port Capability
  602. //
  603. MmioOr32 ((UINTN)&UsbDebugPortRegister->ControlStatus, USB_DEBUG_PORT_ENABLE);
  604. //
  605. // initialize the data toggle used by bulk in/out endpoint.
  606. //
  607. Handle->BulkInToggle = 0;
  608. Handle->BulkOutToggle = 0;
  609. //
  610. // set usb debug device address as 0x7F.
  611. //
  612. Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mDebugCommunicationLibUsbSetDebugAddress, 0x0, 0x0, NULL, NULL);
  613. if (RETURN_ERROR (Status)) {
  614. //
  615. // The device can not work well.
  616. //
  617. Handle->Initialized = USBDBG_NO_DBG_CAB;
  618. return Status;
  619. }
  620. //
  621. // Start to communicate with Usb Debug Device to see if the attached device is usb debug device or not.
  622. //
  623. Length = (UINT8)sizeof (USB_DEBUG_PORT_DESCRIPTOR);
  624. //
  625. // Get debug descriptor.
  626. //
  627. Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mDebugCommunicationLibUsbGetDebugDescriptor, 0x7F, 0x0, (UINT8 *)&UsbDebugPortDescriptor, &Length);
  628. if (RETURN_ERROR (Status)) {
  629. //
  630. // The device is not a usb debug device.
  631. //
  632. Handle->Initialized = USBDBG_NO_DBG_CAB;
  633. return Status;
  634. }
  635. if (Length != sizeof (USB_DEBUG_PORT_DESCRIPTOR)) {
  636. Handle->Initialized = USBDBG_NO_DBG_CAB;
  637. return RETURN_DEVICE_ERROR;
  638. }
  639. //
  640. // Determine the usb debug device endpoints.
  641. //
  642. Handle->InEndpoint = UsbDebugPortDescriptor.DebugInEndpoint;
  643. Handle->OutEndpoint = UsbDebugPortDescriptor.DebugOutEndpoint;
  644. //
  645. // enable the usb debug feature.
  646. //
  647. Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mDebugCommunicationLibUsbSetDebugFeature, 0x7F, 0x0, NULL, NULL);
  648. if (RETURN_ERROR (Status)) {
  649. //
  650. // The device can not work well.
  651. //
  652. Handle->Initialized = USBDBG_NO_DBG_CAB;
  653. return Status;
  654. }
  655. Handle->Initialized = USBDBG_DBG_CAB;
  656. }
  657. //
  658. // Set initialized flag
  659. //
  660. Handle->Initialized = USBDBG_INIT_DONE;
  661. return RETURN_SUCCESS;
  662. }
  663. /**
  664. Read data from debug device and save the data in a buffer.
  665. Reads NumberOfBytes data bytes from a debug device into the buffer
  666. specified by Buffer. The number of bytes actually read is returned.
  667. If the return value is less than NumberOfBytes, then the rest operation failed.
  668. If NumberOfBytes is zero, then return 0.
  669. @param Handle Debug port handle.
  670. @param Buffer Pointer to the data buffer to store the data read from the debug device.
  671. @param NumberOfBytes Number of bytes which will be read.
  672. @param Timeout Timeout value for reading from debug device. It unit is Microsecond.
  673. @retval 0 Read data failed, no data is to be read.
  674. @retval >0 Actual number of bytes read from debug device.
  675. **/
  676. UINTN
  677. EFIAPI
  678. DebugPortReadBuffer (
  679. IN DEBUG_PORT_HANDLE Handle,
  680. IN UINT8 *Buffer,
  681. IN UINTN NumberOfBytes,
  682. IN UINTN Timeout
  683. )
  684. {
  685. USB_DEBUG_PORT_HANDLE *UsbDebugPortHandle;
  686. RETURN_STATUS Status;
  687. UINT8 Index;
  688. if ((NumberOfBytes != 1) || (Buffer == NULL) || (Timeout != 0)) {
  689. return 0;
  690. }
  691. //
  692. // If Handle is NULL, it means memory is ready for use.
  693. // Use global variable to store handle value.
  694. //
  695. if (Handle == NULL) {
  696. UsbDebugPortHandle = &mDebugCommunicationLibUsbDebugPortHandle;
  697. } else {
  698. UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;
  699. }
  700. if (NeedReinitializeHardware (UsbDebugPortHandle)) {
  701. Status = InitializeUsbDebugHardware (UsbDebugPortHandle);
  702. if (RETURN_ERROR (Status)) {
  703. return 0;
  704. }
  705. }
  706. //
  707. // Read data from buffer
  708. //
  709. if (UsbDebugPortHandle->DataCount < 1) {
  710. return 0;
  711. } else {
  712. *Buffer = UsbDebugPortHandle->Data[0];
  713. for (Index = 0; Index < UsbDebugPortHandle->DataCount - 1; Index++) {
  714. if ((Index + 1) >= USB_DEBUG_PORT_MAX_PACKET_SIZE) {
  715. return 0;
  716. }
  717. UsbDebugPortHandle->Data[Index] = UsbDebugPortHandle->Data[Index + 1];
  718. }
  719. UsbDebugPortHandle->DataCount = (UINT8)(UsbDebugPortHandle->DataCount - 1);
  720. return 1;
  721. }
  722. }
  723. /**
  724. Write data from buffer to debug device.
  725. Writes NumberOfBytes data bytes from Buffer to the debug device.
  726. The number of bytes actually written to the debug device is returned.
  727. If the return value is less than NumberOfBytes, then the write operation failed.
  728. If NumberOfBytes is zero, then return 0.
  729. @param Handle Debug port handle.
  730. @param Buffer Pointer to the data buffer to be written.
  731. @param NumberOfBytes Number of bytes to written to the debug device.
  732. @retval 0 NumberOfBytes is 0.
  733. @retval >0 The number of bytes written to the debug device.
  734. If this value is less than NumberOfBytes, then the read operation failed.
  735. **/
  736. UINTN
  737. EFIAPI
  738. DebugPortWriteBuffer (
  739. IN DEBUG_PORT_HANDLE Handle,
  740. IN UINT8 *Buffer,
  741. IN UINTN NumberOfBytes
  742. )
  743. {
  744. USB_DEBUG_PORT_HANDLE *UsbDebugPortHandle;
  745. USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister;
  746. RETURN_STATUS Status;
  747. UINT8 Sent;
  748. UINTN Total;
  749. UINT8 ReceivedPid;
  750. if ((NumberOfBytes == 0) || (Buffer == NULL)) {
  751. return 0;
  752. }
  753. Sent = 0;
  754. Total = 0;
  755. //
  756. // If Handle is NULL, it means memory is ready for use.
  757. // Use global variable to store handle value.
  758. //
  759. if (Handle == NULL) {
  760. UsbDebugPortHandle = &mDebugCommunicationLibUsbDebugPortHandle;
  761. } else {
  762. UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;
  763. }
  764. if (NeedReinitializeHardware (UsbDebugPortHandle)) {
  765. Status = InitializeUsbDebugHardware (UsbDebugPortHandle);
  766. if (RETURN_ERROR (Status)) {
  767. return 0;
  768. }
  769. }
  770. UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)((UINTN)UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);
  771. while ((Total < NumberOfBytes)) {
  772. if (NumberOfBytes - Total > USB_DEBUG_PORT_MAX_PACKET_SIZE) {
  773. Sent = USB_DEBUG_PORT_MAX_PACKET_SIZE;
  774. } else {
  775. Sent = (UINT8)(NumberOfBytes - Total);
  776. }
  777. Status = UsbDebugPortOut (UsbDebugPortRegister, Buffer + Total, Sent, OUTPUT_PID, 0x7F, UsbDebugPortHandle->OutEndpoint, UsbDebugPortHandle->BulkOutToggle);
  778. if (RETURN_ERROR (Status)) {
  779. return Total;
  780. }
  781. ReceivedPid = (MmioRead8 ((UINTN)&UsbDebugPortRegister->ReceivedPid));
  782. //
  783. // If received a NAK_PID on write transaction, it means the usb debug device is busy and can not handle this transaction.
  784. // should send the packet again.
  785. //
  786. if (ReceivedPid == NAK_PID) {
  787. Sent = 0;
  788. } else {
  789. UsbDebugPortHandle->BulkOutToggle ^= 1;
  790. }
  791. Total += Sent;
  792. }
  793. return Total;
  794. }
  795. /**
  796. Polls a debug device to see if there is any data waiting to be read.
  797. Polls a debug device to see if there is any data waiting to be read.
  798. If there is data waiting to be read from the debug device, then TRUE is returned.
  799. If there is no data waiting to be read from the debug device, then FALSE is returned.
  800. @param Handle Debug port handle.
  801. @retval TRUE Data is waiting to be read from the debug device.
  802. @retval FALSE There is no data waiting to be read from the serial device.
  803. **/
  804. BOOLEAN
  805. EFIAPI
  806. DebugPortPollBuffer (
  807. IN DEBUG_PORT_HANDLE Handle
  808. )
  809. {
  810. USB_DEBUG_PORT_HANDLE *UsbDebugPortHandle;
  811. USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister;
  812. UINT8 Length;
  813. UINT8 Index;
  814. RETURN_STATUS Status;
  815. //
  816. // If Handle is NULL, it means memory is ready for use.
  817. // Use global variable to store handle value.
  818. //
  819. if (Handle == NULL) {
  820. UsbDebugPortHandle = &mDebugCommunicationLibUsbDebugPortHandle;
  821. } else {
  822. UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;
  823. }
  824. if (NeedReinitializeHardware (UsbDebugPortHandle)) {
  825. Status = InitializeUsbDebugHardware (UsbDebugPortHandle);
  826. if (RETURN_ERROR (Status)) {
  827. return FALSE;
  828. }
  829. }
  830. //
  831. // If the data buffer is not empty, then return TRUE directly.
  832. // else initialize a usb read transaction and read data to the data buffer.
  833. //
  834. if (UsbDebugPortHandle->DataCount != 0) {
  835. return TRUE;
  836. }
  837. UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)((UINTN)UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);
  838. UsbDebugPortRegister->TokenPid = INPUT_PID;
  839. if (UsbDebugPortHandle->BulkInToggle == 0) {
  840. UsbDebugPortRegister->SendPid = DATA0_PID;
  841. } else {
  842. UsbDebugPortRegister->SendPid = DATA1_PID;
  843. }
  844. UsbDebugPortRegister->UsbAddress = 0x7F;
  845. UsbDebugPortRegister->UsbEndPoint = UsbDebugPortHandle->InEndpoint & 0x0F;
  846. //
  847. // Clearing W/R bit to indicate it's a READ operation
  848. //
  849. MmioAnd32 ((UINTN)&UsbDebugPortRegister->ControlStatus, (UINT32) ~BIT4);
  850. //
  851. // Setting GO bit as well as clearing DONE bit
  852. //
  853. MmioOr32 ((UINTN)&UsbDebugPortRegister->ControlStatus, (UINT32)BIT5);
  854. //
  855. // Wait for completing the request
  856. //
  857. while ((MmioRead32 ((UINTN)&UsbDebugPortRegister->ControlStatus) & (UINT32)BIT16) == 0) {
  858. if ((MmioRead32 ((UINTN)&UsbDebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE))
  859. != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE))
  860. {
  861. return FALSE;
  862. }
  863. }
  864. if ((MmioRead32 ((UINTN)&UsbDebugPortRegister->ControlStatus)) & BIT6) {
  865. return FALSE;
  866. }
  867. Length = (UINT8)(MmioRead32 ((UINTN)&UsbDebugPortRegister->ControlStatus) & 0xF);
  868. if (Length > 8) {
  869. return FALSE;
  870. }
  871. UsbDebugPortHandle->BulkInToggle ^= 1;
  872. if (Length == 0) {
  873. return FALSE;
  874. }
  875. for (Index = 0; Index < Length; Index++) {
  876. UsbDebugPortHandle->Data[Index] = UsbDebugPortRegister->DataBuffer[Index];
  877. }
  878. UsbDebugPortHandle->DataCount = Length;
  879. return TRUE;
  880. }
  881. /**
  882. Initialize the debug port.
  883. If Function is not NULL, Debug Communication Library will call this function
  884. by passing in the Context to be the first parameter. If needed, Debug Communication
  885. Library will create one debug port handle to be the second argument passing in
  886. calling the Function, otherwise it will pass NULL to be the second argument of
  887. Function.
  888. If Function is NULL, and Context is not NULL, the Debug Communication Library could
  889. a) Return the same handle as passed in (as Context parameter).
  890. b) Ignore the input Context parameter and create new handle to be returned.
  891. If parameter Function is NULL and Context is NULL, Debug Communication Library could
  892. created a new handle if needed and return it, otherwise it will return NULL.
  893. @param[in] Context Context needed by callback function; it was optional.
  894. @param[in] Function Continue function called by Debug Communication library;
  895. it was optional.
  896. @return The debug port handle created by Debug Communication Library if Function
  897. is not NULL.
  898. **/
  899. DEBUG_PORT_HANDLE
  900. EFIAPI
  901. DebugPortInitialize (
  902. IN VOID *Context,
  903. IN DEBUG_PORT_CONTINUE Function
  904. )
  905. {
  906. RETURN_STATUS Status;
  907. USB_DEBUG_PORT_HANDLE Handle;
  908. //
  909. // Validate the PCD PcdDebugPortHandleBufferSize value
  910. //
  911. ASSERT (PcdGet16 (PcdDebugPortHandleBufferSize) == sizeof (USB_DEBUG_PORT_HANDLE));
  912. if ((Function == NULL) && (Context != NULL)) {
  913. return (DEBUG_PORT_HANDLE *)Context;
  914. }
  915. ZeroMem (&Handle, sizeof (USB_DEBUG_PORT_HANDLE));
  916. Status = CalculateUsbDebugPortBar (&Handle.DebugPortOffset, &Handle.DebugPortBarNumber);
  917. if (RETURN_ERROR (Status)) {
  918. DEBUG ((DEBUG_ERROR, "UsbDbg: the pci device pointed by PcdUsbEhciPciAddress is not EHCI host controller or does not support debug port capability!\n"));
  919. goto Exit;
  920. }
  921. Handle.EhciMemoryBase = 0xFFFFFC00 & PciRead32 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET);
  922. if (Handle.EhciMemoryBase == 0) {
  923. //
  924. // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
  925. //
  926. PciWrite32 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET, PcdGet32 (PcdUsbEhciMemorySpaceBase));
  927. Handle.EhciMemoryBase = 0xFFFFFC00 & PciRead32 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET);
  928. }
  929. Handle.UsbDebugPortMemoryBase = 0xFFFFFC00 & PciRead32 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET + Handle.DebugPortBarNumber * 4);
  930. if (Handle.UsbDebugPortMemoryBase == 0) {
  931. //
  932. // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
  933. //
  934. PciWrite32 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET + Handle.DebugPortBarNumber * 4, PcdGet32 (PcdUsbDebugPortMemorySpaceBase));
  935. Handle.UsbDebugPortMemoryBase = 0xFFFFFC00 & PciRead32 (PcdGet32 (PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET + Handle.DebugPortBarNumber * 4);
  936. }
  937. Handle.Initialized = USBDBG_RESET;
  938. if (NeedReinitializeHardware (&Handle)) {
  939. DEBUG ((DEBUG_ERROR, "UsbDbg: Start EHCI debug port initialization!\n"));
  940. Status = InitializeUsbDebugHardware (&Handle);
  941. if (RETURN_ERROR (Status)) {
  942. DEBUG ((DEBUG_ERROR, "UsbDbg: Failed, please check if USB debug cable is plugged into EHCI debug port correctly!\n"));
  943. goto Exit;
  944. }
  945. }
  946. Exit:
  947. if (Function != NULL) {
  948. Function (Context, &Handle);
  949. } else {
  950. CopyMem (&mDebugCommunicationLibUsbDebugPortHandle, &Handle, sizeof (USB_DEBUG_PORT_HANDLE));
  951. }
  952. return (DEBUG_PORT_HANDLE)(UINTN)&mDebugCommunicationLibUsbDebugPortHandle;
  953. }