]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/DebugPortDxe/DebugPort.c
1. add gEfiDebugPortVariableGuid and gEfiDebugPortDevicePathGuid in MdePkg.dec, even...
[mirror_edk2.git] / MdeModulePkg / Universal / DebugPortDxe / DebugPort.c
CommitLineData
fb0b259e 1/** @file\r
2 Top level C file for debugport driver. Contains initialization function.\r
3 This driver layers on top of SerialIo.\r
4 ALL CODE IN THE SERIALIO STACK MUST BE RE-ENTRANT AND CALLABLE FROM\r
5 INTERRUPT CONTEXT\r
c1f23d63 6\r
fb0b259e 7Copyright (c) 2006 - 2008, Intel Corporation. <BR>\r
8All rights reserved. This program and the accompanying materials\r
9are licensed and made available under the terms and conditions of the BSD License\r
10which accompanies this distribution. The full text of the license may be found at\r
11http://opensource.org/licenses/bsd-license.php\r
c1f23d63 12\r
fb0b259e 13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
c1f23d63 15\r
fb0b259e 16**/\r
c1f23d63 17\r
c1f23d63 18#include "DebugPort.h"\r
19\r
20//\r
49c8b87c 21// Globals\r
c1f23d63 22//\r
49c8b87c 23EFI_DRIVER_BINDING_PROTOCOL gDebugPortDriverBinding = {\r
24 DebugPortSupported,\r
25 DebugPortStart,\r
26 DebugPortStop,\r
27 DEBUGPORT_DRIVER_VERSION,\r
28 NULL,\r
29 NULL\r
30};\r
fb0b259e 31\r
2d285faa 32DEBUGPORT_DEVICE mDebugPortDevice = {\r
33 DEBUGPORT_DEVICE_SIGNATURE,\r
34 (EFI_HANDLE) 0,\r
35 (EFI_HANDLE) 0,\r
36 (VOID *) NULL,\r
37 (EFI_DEVICE_PATH_PROTOCOL *) NULL,\r
38 {\r
39 DebugPortReset,\r
cfcbb8bc 40 DebugPortRead,\r
41 DebugPortWrite,\r
42 DebugPortPoll\r
2d285faa 43 },\r
44 (EFI_HANDLE) 0,\r
45 (EFI_SERIAL_IO_PROTOCOL *) NULL,\r
46 DEBUGPORT_UART_DEFAULT_BAUDRATE,\r
47 DEBUGPORT_UART_DEFAULT_FIFO_DEPTH,\r
48 DEBUGPORT_UART_DEFAULT_TIMEOUT,\r
49 (EFI_PARITY_TYPE) DEBUGPORT_UART_DEFAULT_PARITY,\r
50 DEBUGPORT_UART_DEFAULT_DATA_BITS,\r
51 (EFI_STOP_BITS_TYPE) DEBUGPORT_UART_DEFAULT_STOP_BITS\r
52};\r
c1f23d63 53\r
49c8b87c 54/**\r
55 Local worker function to obtain device path information from DebugPort variable.\r
c1f23d63 56\r
49c8b87c 57 Records requested settings in DebugPort device structure.\r
c1f23d63 58\r
49c8b87c 59**/\r
60VOID\r
61GetDebugPortVariable (\r
2d285faa 62 VOID\r
49c8b87c 63 )\r
c1f23d63 64{\r
65 UINTN DataSize;\r
66 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
67 EFI_STATUS Status;\r
68\r
69 DataSize = 0;\r
70\r
71 Status = gRT->GetVariable (\r
72 (CHAR16 *) EFI_DEBUGPORT_VARIABLE_NAME,\r
73 &gEfiDebugPortVariableGuid,\r
74 NULL,\r
75 &DataSize,\r
2d285faa 76 mDebugPortDevice.DebugPortVariable\r
c1f23d63 77 );\r
78\r
79 if (Status == EFI_BUFFER_TOO_SMALL) {\r
2d285faa 80 if (mDebugPortDevice.DebugPortVariable != NULL) {\r
81 FreePool (mDebugPortDevice.DebugPortVariable);\r
c1f23d63 82 }\r
83\r
2d285faa 84 mDebugPortDevice.DebugPortVariable = AllocatePool (DataSize);\r
85 if (mDebugPortDevice.DebugPortVariable != NULL) {\r
c1f23d63 86 gRT->GetVariable (\r
87 (CHAR16 *) EFI_DEBUGPORT_VARIABLE_NAME,\r
88 &gEfiDebugPortVariableGuid,\r
89 NULL,\r
90 &DataSize,\r
2d285faa 91 mDebugPortDevice.DebugPortVariable\r
c1f23d63 92 );\r
2d285faa 93 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) mDebugPortDevice.DebugPortVariable;\r
1232b214 94 while (!IsDevicePathEnd (DevicePath) && !IS_UART_DEVICEPATH (DevicePath)) {\r
95 DevicePath = NextDevicePathNode (DevicePath);\r
c1f23d63 96 }\r
97\r
1232b214 98 if (IsDevicePathEnd (DevicePath)) {\r
2d285faa 99 FreePool (mDebugPortDevice.DebugPortVariable);\r
100 mDebugPortDevice.DebugPortVariable = NULL;\r
c1f23d63 101 } else {\r
102 CopyMem (\r
2d285faa 103 &mDebugPortDevice.BaudRate,\r
c1f23d63 104 &((UART_DEVICE_PATH *) DevicePath)->BaudRate,\r
105 sizeof (((UART_DEVICE_PATH *) DevicePath)->BaudRate)\r
106 );\r
2d285faa 107 mDebugPortDevice.ReceiveFifoDepth = DEBUGPORT_UART_DEFAULT_FIFO_DEPTH;\r
108 mDebugPortDevice.Timeout = DEBUGPORT_UART_DEFAULT_TIMEOUT;\r
c1f23d63 109 CopyMem (\r
2d285faa 110 &mDebugPortDevice.Parity,\r
c1f23d63 111 &((UART_DEVICE_PATH *) DevicePath)->Parity,\r
112 sizeof (((UART_DEVICE_PATH *) DevicePath)->Parity)\r
113 );\r
114 CopyMem (\r
2d285faa 115 &mDebugPortDevice.DataBits,\r
c1f23d63 116 &((UART_DEVICE_PATH *) DevicePath)->DataBits,\r
117 sizeof (((UART_DEVICE_PATH *) DevicePath)->DataBits)\r
118 );\r
119 CopyMem (\r
2d285faa 120 &mDebugPortDevice.StopBits,\r
c1f23d63 121 &((UART_DEVICE_PATH *) DevicePath)->StopBits,\r
122 sizeof (((UART_DEVICE_PATH *) DevicePath)->StopBits)\r
123 );\r
124 }\r
125 }\r
126 }\r
127}\r
128\r
49c8b87c 129/**\r
cfcbb8bc 130 Debug Port Driver entry point.\r
c1f23d63 131\r
49c8b87c 132 Reads DebugPort variable to determine what device and settings to use as the\r
133 debug port. Binds exclusively to SerialIo. Reverts to defaults if no variable\r
134 is found.\r
c1f23d63 135\r
cfcbb8bc 136 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
49c8b87c 137 @param[in] SystemTable A pointer to the EFI System Table.\r
cfcbb8bc 138\r
49c8b87c 139 @retval EFI_SUCCESS The entry point is executed successfully.\r
140 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.\r
141 @retval other Some error occurs when executing this entry point.\r
c1f23d63 142\r
49c8b87c 143**/\r
c1f23d63 144EFI_STATUS\r
145EFIAPI\r
146InitializeDebugPortDriver (\r
147 IN EFI_HANDLE ImageHandle,\r
148 IN EFI_SYSTEM_TABLE *SystemTable\r
149 )\r
c1f23d63 150{\r
151 EFI_STATUS Status;\r
152\r
153 //\r
154 // Install driver model protocol(s).\r
155 //\r
19f97f7e 156 Status = EfiLibInstallDriverBindingComponentName2 (\r
c1f23d63 157 ImageHandle,\r
158 SystemTable,\r
159 &gDebugPortDriverBinding,\r
160 ImageHandle,\r
161 &gDebugPortComponentName,\r
19f97f7e 162 &gDebugPortComponentName2\r
c1f23d63 163 );\r
164 ASSERT_EFI_ERROR (Status);\r
c1f23d63 165\r
166 return EFI_SUCCESS;\r
167}\r
c1f23d63 168\r
49c8b87c 169/**\r
cfcbb8bc 170 Checks to see if there's not already a DebugPort interface somewhere.\r
fb0b259e 171\r
c1f23d63 172 If there's a DEBUGPORT variable, the device path must match exactly. If there's\r
173 no DEBUGPORT variable, then device path is not checked and does not matter.\r
c1f23d63 174 Checks to see that there's a serial io interface on the controller handle\r
175 that can be bound BY_DRIVER | EXCLUSIVE.\r
c1f23d63 176 If all these tests succeed, then we return EFI_SUCCESS, else, EFI_UNSUPPORTED\r
177 or other error returned by OpenProtocol.\r
178\r
49c8b87c 179 @param This Protocol instance pointer.\r
180 @param ControllerHandle Handle of device to test.\r
181 @param RemainingDevicePath Optional parameter use to pick a specific child\r
182 device to start.\r
fb0b259e 183\r
49c8b87c 184 @retval EFI_SUCCESS This driver supports this device.\r
185 @retval EFI_UNSUPPORTED Debug Port device is not supported.\r
186 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.\r
187 @retval others Some error occurs.\r
fb0b259e 188\r
49c8b87c 189**/\r
190EFI_STATUS\r
191EFIAPI\r
192DebugPortSupported (\r
193 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
194 IN EFI_HANDLE ControllerHandle,\r
195 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
196 )\r
c1f23d63 197{\r
198 EFI_STATUS Status;\r
199 EFI_DEVICE_PATH_PROTOCOL *Dp1;\r
200 EFI_DEVICE_PATH_PROTOCOL *Dp2;\r
201 EFI_SERIAL_IO_PROTOCOL *SerialIo;\r
202 EFI_DEBUGPORT_PROTOCOL *DebugPortInterface;\r
203 EFI_HANDLE TempHandle;\r
204\r
205 //\r
0da0de5e 206 // Check to see that there's not a debugport protocol already published,\r
207 // since only one standard UART serial port could be supported by this driver.\r
c1f23d63 208 //\r
209 if (gBS->LocateProtocol (&gEfiDebugPortProtocolGuid, NULL, (VOID **) &DebugPortInterface) != EFI_NOT_FOUND) {\r
210 return EFI_UNSUPPORTED;\r
211 }\r
212 //\r
213 // Read DebugPort variable to determine debug port selection and parameters\r
214 //\r
2d285faa 215 GetDebugPortVariable ();\r
c1f23d63 216\r
2d285faa 217 if (mDebugPortDevice.DebugPortVariable != NULL) {\r
c1f23d63 218 //\r
219 // There's a DEBUGPORT variable, so do LocateDevicePath and check to see if\r
220 // the closest matching handle matches the controller handle, and if it does,\r
221 // check to see that the remaining device path has the DebugPort GUIDed messaging\r
222 // device path only. Otherwise, it's a mismatch and EFI_UNSUPPORTED is returned.\r
223 //\r
2d285faa 224 Dp1 = DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL *) mDebugPortDevice.DebugPortVariable);\r
c1f23d63 225 if (Dp1 == NULL) {\r
226 return EFI_OUT_OF_RESOURCES;\r
227 }\r
228\r
229 Dp2 = Dp1;\r
230\r
231 Status = gBS->LocateDevicePath (\r
232 &gEfiSerialIoProtocolGuid,\r
233 &Dp2,\r
234 &TempHandle\r
235 );\r
236\r
237 if (Status == EFI_SUCCESS && TempHandle != ControllerHandle) {\r
238 Status = EFI_UNSUPPORTED;\r
239 }\r
240\r
cfcbb8bc 241 if (Status == EFI_SUCCESS &&\r
2d285faa 242 (Dp2->Type != MESSAGING_DEVICE_PATH ||\r
cfcbb8bc 243 Dp2->SubType != MSG_VENDOR_DP ||\r
b643f69c 244 *((UINT16 *) Dp2->Length) != sizeof (DEBUGPORT_DEVICE_PATH))) {\r
2d285faa 245\r
c1f23d63 246 Status = EFI_UNSUPPORTED;\r
247 }\r
248\r
cfcbb8bc 249 if (Status == EFI_SUCCESS && !CompareGuid (&gEfiDebugPortDevicePathGuid, (GUID *) (Dp2 + 1))) {\r
c1f23d63 250 Status = EFI_UNSUPPORTED;\r
251 }\r
252\r
253 FreePool (Dp1);\r
254 if (EFI_ERROR (Status)) {\r
255 return Status;\r
256 }\r
257 }\r
258\r
259 Status = gBS->OpenProtocol (\r
260 ControllerHandle,\r
261 &gEfiSerialIoProtocolGuid,\r
262 (VOID **) &SerialIo,\r
263 This->DriverBindingHandle,\r
264 ControllerHandle,\r
265 EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE\r
266 );\r
267 if (EFI_ERROR (Status)) {\r
268 return Status;\r
269 }\r
270\r
271 gBS->CloseProtocol (\r
272 ControllerHandle,\r
273 &gEfiSerialIoProtocolGuid,\r
274 This->DriverBindingHandle,\r
275 ControllerHandle\r
276 );\r
277\r
278 return EFI_SUCCESS;\r
279}\r
280\r
49c8b87c 281/**\r
282 Binds exclusively to serial io on the controller handle, Produces DebugPort\r
283 protocol and DevicePath on new handle.\r
284\r
285 @param This Protocol instance pointer.\r
286 @param ControllerHandle Handle of device to bind driver to.\r
287 @param RemainingDevicePath Optional parameter use to pick a specific child\r
288 device to start.\r
289\r
290 @retval EFI_SUCCESS This driver is added to ControllerHandle.\r
291 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.\r
cfcbb8bc 292 @retval others Some error occurs.\r
49c8b87c 293\r
294**/\r
c1f23d63 295EFI_STATUS\r
296EFIAPI\r
297DebugPortStart (\r
298 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
299 IN EFI_HANDLE ControllerHandle,\r
300 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
301 )\r
c1f23d63 302{\r
303 EFI_STATUS Status;\r
304 DEBUGPORT_DEVICE_PATH DebugPortDP;\r
305 EFI_DEVICE_PATH_PROTOCOL EndDP;\r
306 EFI_DEVICE_PATH_PROTOCOL *Dp1;\r
307\r
308 Status = gBS->OpenProtocol (\r
309 ControllerHandle,\r
310 &gEfiSerialIoProtocolGuid,\r
2d285faa 311 (VOID **) &mDebugPortDevice.SerialIoBinding,\r
c1f23d63 312 This->DriverBindingHandle,\r
313 ControllerHandle,\r
314 EFI_OPEN_PROTOCOL_BY_DRIVER | EFI_OPEN_PROTOCOL_EXCLUSIVE\r
315 );\r
316 if (EFI_ERROR (Status)) {\r
317 return Status;\r
318 }\r
319\r
2d285faa 320 mDebugPortDevice.SerialIoDeviceHandle = ControllerHandle;\r
c1f23d63 321\r
322 //\r
323 // Initialize the Serial Io interface...\r
324 //\r
2d285faa 325 Status = mDebugPortDevice.SerialIoBinding->SetAttributes (\r
326 mDebugPortDevice.SerialIoBinding,\r
327 mDebugPortDevice.BaudRate,\r
328 mDebugPortDevice.ReceiveFifoDepth,\r
329 mDebugPortDevice.Timeout,\r
330 mDebugPortDevice.Parity,\r
331 mDebugPortDevice.DataBits,\r
332 mDebugPortDevice.StopBits\r
c1f23d63 333 );\r
334 if (EFI_ERROR (Status)) {\r
2d285faa 335 mDebugPortDevice.BaudRate = 0;\r
336 mDebugPortDevice.Parity = DefaultParity;\r
337 mDebugPortDevice.DataBits = 0;\r
338 mDebugPortDevice.StopBits = DefaultStopBits;\r
339 mDebugPortDevice.ReceiveFifoDepth = 0;\r
340 Status = mDebugPortDevice.SerialIoBinding->SetAttributes (\r
341 mDebugPortDevice.SerialIoBinding,\r
342 mDebugPortDevice.BaudRate,\r
343 mDebugPortDevice.ReceiveFifoDepth,\r
344 mDebugPortDevice.Timeout,\r
345 mDebugPortDevice.Parity,\r
346 mDebugPortDevice.DataBits,\r
347 mDebugPortDevice.StopBits\r
c1f23d63 348 );\r
349 if (EFI_ERROR (Status)) {\r
350 gBS->CloseProtocol (\r
351 ControllerHandle,\r
352 &gEfiSerialIoProtocolGuid,\r
353 This->DriverBindingHandle,\r
354 ControllerHandle\r
355 );\r
356 return Status;\r
357 }\r
358 }\r
359\r
2d285faa 360 mDebugPortDevice.SerialIoBinding->Reset (mDebugPortDevice.SerialIoBinding);\r
c1f23d63 361\r
362 //\r
363 // Create device path instance for DebugPort\r
364 //\r
365 DebugPortDP.Header.Type = MESSAGING_DEVICE_PATH;\r
366 DebugPortDP.Header.SubType = MSG_VENDOR_DP;\r
367 SetDevicePathNodeLength (&(DebugPortDP.Header), sizeof (DebugPortDP));\r
cfcbb8bc 368 CopyGuid (&DebugPortDP.Guid, &gEfiDebugPortDevicePathGuid);\r
c1f23d63 369\r
370 Dp1 = DevicePathFromHandle (ControllerHandle);\r
371 if (Dp1 == NULL) {\r
372 Dp1 = &EndDP;\r
373 SetDevicePathEndNode (Dp1);\r
374 }\r
375\r
2d285faa 376 mDebugPortDevice.DebugPortDevicePath = AppendDevicePathNode (Dp1, (EFI_DEVICE_PATH_PROTOCOL *) &DebugPortDP);\r
377 if (mDebugPortDevice.DebugPortDevicePath == NULL) {\r
c1f23d63 378 return EFI_OUT_OF_RESOURCES;\r
379 }\r
380 //\r
381 // Publish DebugPort and Device Path protocols\r
382 //\r
383 Status = gBS->InstallMultipleProtocolInterfaces (\r
2d285faa 384 &mDebugPortDevice.DebugPortDeviceHandle,\r
c1f23d63 385 &gEfiDevicePathProtocolGuid,\r
2d285faa 386 mDebugPortDevice.DebugPortDevicePath,\r
c1f23d63 387 &gEfiDebugPortProtocolGuid,\r
2d285faa 388 &mDebugPortDevice.DebugPortInterface,\r
c1f23d63 389 NULL\r
390 );\r
391\r
392 if (EFI_ERROR (Status)) {\r
393 gBS->CloseProtocol (\r
394 ControllerHandle,\r
395 &gEfiSerialIoProtocolGuid,\r
396 This->DriverBindingHandle,\r
397 ControllerHandle\r
398 );\r
399 return Status;\r
400 }\r
401 //\r
402 // Connect debugport child to serial io\r
403 //\r
404 Status = gBS->OpenProtocol (\r
405 ControllerHandle,\r
406 &gEfiSerialIoProtocolGuid,\r
2d285faa 407 (VOID **) &mDebugPortDevice.SerialIoBinding,\r
c1f23d63 408 This->DriverBindingHandle,\r
2d285faa 409 mDebugPortDevice.DebugPortDeviceHandle,\r
c1f23d63 410 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
411 );\r
412\r
413 if (EFI_ERROR (Status)) {\r
414 DEBUG_CODE_BEGIN ();\r
415 UINTN BufferSize;\r
416\r
417 BufferSize = 48;\r
418 DebugPortWrite (\r
2d285faa 419 &mDebugPortDevice.DebugPortInterface,\r
c1f23d63 420 0,\r
421 &BufferSize,\r
422 "DebugPort driver failed to open child controller\n\n"\r
423 );\r
424 DEBUG_CODE_END ();\r
425\r
426 gBS->CloseProtocol (\r
427 ControllerHandle,\r
428 &gEfiSerialIoProtocolGuid,\r
429 This->DriverBindingHandle,\r
430 ControllerHandle\r
431 );\r
432 return Status;\r
433 }\r
434\r
435 DEBUG_CODE_BEGIN ();\r
2d285faa 436 UINTN BufferSize;\r
c1f23d63 437\r
438 BufferSize = 38;\r
439 DebugPortWrite (\r
2d285faa 440 &mDebugPortDevice.DebugPortInterface,\r
c1f23d63 441 0,\r
442 &BufferSize,\r
443 "Hello World from the DebugPort driver\n\n"\r
444 );\r
2d285faa 445\r
c1f23d63 446 DEBUG_CODE_END ();\r
447\r
448 return EFI_SUCCESS;\r
449}\r
450\r
49c8b87c 451/**\r
452 Stop this driver on ControllerHandle by removing Serial IO protocol on\r
453 the ControllerHandle.\r
454\r
455 @param This Protocol instance pointer.\r
456 @param ControllerHandle Handle of device to stop driver on\r
457 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
458 children is zero stop the entire bus driver.\r
459 @param ChildHandleBuffer List of Child Handles to Stop.\r
460\r
461 @retval EFI_SUCCESS This driver is removed ControllerHandle.\r
462 @retval other This driver was not removed from this device.\r
463\r
464**/\r
c1f23d63 465EFI_STATUS\r
466EFIAPI\r
467DebugPortStop (\r
468 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
469 IN EFI_HANDLE ControllerHandle,\r
470 IN UINTN NumberOfChildren,\r
471 IN EFI_HANDLE *ChildHandleBuffer\r
472 )\r
c1f23d63 473{\r
474 EFI_STATUS Status;\r
475\r
476 if (NumberOfChildren == 0) {\r
477 //\r
478 // Close the bus driver\r
479 //\r
480 gBS->CloseProtocol (\r
481 ControllerHandle,\r
482 &gEfiSerialIoProtocolGuid,\r
483 This->DriverBindingHandle,\r
484 ControllerHandle\r
485 );\r
486\r
2d285faa 487 mDebugPortDevice.SerialIoBinding = NULL;\r
c1f23d63 488\r
489 gBS->CloseProtocol (\r
490 ControllerHandle,\r
491 &gEfiDevicePathProtocolGuid,\r
492 This->DriverBindingHandle,\r
493 ControllerHandle\r
494 );\r
495\r
2d285faa 496 FreePool (mDebugPortDevice.DebugPortDevicePath);\r
c1f23d63 497\r
498 return EFI_SUCCESS;\r
499 } else {\r
500 //\r
501 // Disconnect SerialIo child handle\r
502 //\r
503 Status = gBS->CloseProtocol (\r
2d285faa 504 mDebugPortDevice.SerialIoDeviceHandle,\r
c1f23d63 505 &gEfiSerialIoProtocolGuid,\r
506 This->DriverBindingHandle,\r
2d285faa 507 mDebugPortDevice.DebugPortDeviceHandle\r
c1f23d63 508 );\r
509\r
510 if (EFI_ERROR (Status)) {\r
511 return Status;\r
512 }\r
513 //\r
514 // Unpublish our protocols (DevicePath, DebugPort)\r
515 //\r
516 Status = gBS->UninstallMultipleProtocolInterfaces (\r
2d285faa 517 mDebugPortDevice.DebugPortDeviceHandle,\r
c1f23d63 518 &gEfiDevicePathProtocolGuid,\r
2d285faa 519 mDebugPortDevice.DebugPortDevicePath,\r
c1f23d63 520 &gEfiDebugPortProtocolGuid,\r
2d285faa 521 &mDebugPortDevice.DebugPortInterface,\r
c1f23d63 522 NULL\r
523 );\r
524\r
525 if (EFI_ERROR (Status)) {\r
526 gBS->OpenProtocol (\r
527 ControllerHandle,\r
528 &gEfiSerialIoProtocolGuid,\r
2d285faa 529 (VOID **) &mDebugPortDevice.SerialIoBinding,\r
c1f23d63 530 This->DriverBindingHandle,\r
2d285faa 531 mDebugPortDevice.DebugPortDeviceHandle,\r
c1f23d63 532 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
533 );\r
534 } else {\r
2d285faa 535 mDebugPortDevice.DebugPortDeviceHandle = NULL;\r
c1f23d63 536 }\r
537 }\r
538\r
539 return Status;\r
540}\r
c1f23d63 541\r
49c8b87c 542/**\r
c1f23d63 543 DebugPort protocol member function. Calls SerialIo:GetControl to flush buffer.\r
544 We cannot call SerialIo:SetAttributes because it uses pool services, which use\r
545 locks, which affect TPL, so it's not interrupt context safe or re-entrant.\r
546 SerialIo:Reset() calls SetAttributes, so it can't be used either.\r
fb0b259e 547\r
548 The port itself should be fine since it was set up during initialization.\r
549\r
cfcbb8bc 550 @param This Protocol instance pointer.\r
c1f23d63 551\r
49c8b87c 552 @return EFI_SUCCESS Always.\r
c1f23d63 553\r
cfcbb8bc 554**/\r
49c8b87c 555EFI_STATUS\r
556EFIAPI\r
557DebugPortReset (\r
558 IN EFI_DEBUGPORT_PROTOCOL *This\r
559 )\r
c1f23d63 560{\r
561 UINTN BufferSize;\r
562 UINTN BitBucket;\r
563\r
564 while (This->Poll (This) == EFI_SUCCESS) {\r
565 BufferSize = 1;\r
566 This->Read (This, 0, &BufferSize, &BitBucket);\r
567 }\r
568\r
569 return EFI_SUCCESS;\r
570}\r
571\r
49c8b87c 572/**\r
573 DebugPort protocol member function. Calls SerialIo:Read() after setting\r
574 if it's different than the last SerialIo access.\r
575\r
576 @param This Pointer to DebugPort protocol.\r
577 @param Timeout Timeout value.\r
578 @param BufferSize On input, the size of Buffer.\r
579 On output, the amount of data actually written.\r
580 @param Buffer Pointer to buffer to read.\r
581\r
cfcbb8bc 582 @retval EFI_SUCCESS\r
583 @retval others\r
49c8b87c 584\r
585**/\r
c1f23d63 586EFI_STATUS\r
587EFIAPI\r
588DebugPortRead (\r
589 IN EFI_DEBUGPORT_PROTOCOL *This,\r
590 IN UINT32 Timeout,\r
591 IN OUT UINTN *BufferSize,\r
592 IN VOID *Buffer\r
593 )\r
c1f23d63 594{\r
595 DEBUGPORT_DEVICE *DebugPortDevice;\r
596 UINTN LocalBufferSize;\r
597 EFI_STATUS Status;\r
598 UINT8 *BufferPtr;\r
599\r
600 DebugPortDevice = DEBUGPORT_DEVICE_FROM_THIS (This);\r
601 BufferPtr = Buffer;\r
602 LocalBufferSize = *BufferSize;\r
2d285faa 603\r
c1f23d63 604 do {\r
605 Status = DebugPortDevice->SerialIoBinding->Read (\r
606 DebugPortDevice->SerialIoBinding,\r
607 &LocalBufferSize,\r
608 BufferPtr\r
609 );\r
610 if (Status == EFI_TIMEOUT) {\r
611 if (Timeout > DEBUGPORT_UART_DEFAULT_TIMEOUT) {\r
612 Timeout -= DEBUGPORT_UART_DEFAULT_TIMEOUT;\r
613 } else {\r
614 Timeout = 0;\r
615 }\r
616 } else if (EFI_ERROR (Status)) {\r
617 break;\r
618 }\r
619\r
620 BufferPtr += LocalBufferSize;\r
621 LocalBufferSize = *BufferSize - (BufferPtr - (UINT8 *) Buffer);\r
622 } while (LocalBufferSize != 0 && Timeout > 0);\r
623\r
624 *BufferSize = (UINTN) (BufferPtr - (UINT8 *) Buffer);\r
625\r
626 return Status;\r
627}\r
628\r
49c8b87c 629/**\r
630 DebugPort protocol member function. Calls SerialIo:Write() Writes 8 bytes at\r
631 a time and does a GetControl between 8 byte writes to help insure reads are\r
632 interspersed This is poor-man's flow control.\r
633\r
634 @param This Pointer to DebugPort protocol.\r
635 @param Timeout Timeout value.\r
636 @param BufferSize On input, the size of Buffer.\r
637 On output, the amount of data actually written.\r
638 @param Buffer Pointer to buffer to read.\r
639\r
640 @retval EFI_SUCCESS The data was written.\r
641 @retval others Fails when writting datas to debug port device.\r
642\r
643**/\r
c1f23d63 644EFI_STATUS\r
645EFIAPI\r
646DebugPortWrite (\r
647 IN EFI_DEBUGPORT_PROTOCOL *This,\r
648 IN UINT32 Timeout,\r
649 IN OUT UINTN *BufferSize,\r
650 OUT VOID *Buffer\r
651 )\r
c1f23d63 652{\r
653 DEBUGPORT_DEVICE *DebugPortDevice;\r
654 UINTN Position;\r
655 UINTN WriteSize;\r
656 EFI_STATUS Status;\r
657 UINT32 SerialControl;\r
658\r
659 Status = EFI_SUCCESS;\r
660 DebugPortDevice = DEBUGPORT_DEVICE_FROM_THIS (This);\r
661\r
662 WriteSize = 8;\r
663 for (Position = 0; Position < *BufferSize && !EFI_ERROR (Status); Position += WriteSize) {\r
664 DebugPortDevice->SerialIoBinding->GetControl (\r
665 DebugPortDevice->SerialIoBinding,\r
666 &SerialControl\r
667 );\r
668 if (*BufferSize - Position < 8) {\r
669 WriteSize = *BufferSize - Position;\r
670 }\r
671\r
672 Status = DebugPortDevice->SerialIoBinding->Write (\r
673 DebugPortDevice->SerialIoBinding,\r
674 &WriteSize,\r
675 &((UINT8 *) Buffer)[Position]\r
676 );\r
677 }\r
678\r
679 *BufferSize = Position;\r
680 return Status;\r
681}\r
682\r
49c8b87c 683/**\r
c1f23d63 684 DebugPort protocol member function. Calls SerialIo:Write() after setting\r
685 if it's different than the last SerialIo access.\r
fb0b259e 686\r
49c8b87c 687 @param This Pointer to DebugPort protocol.\r
c1f23d63 688\r
49c8b87c 689 @retval EFI_SUCCESS At least 1 character is ready to be read from\r
690 the DebugPort interface.\r
691 @retval EFI_NOT_READY There are no characters ready to read from the\r
692 DebugPort interface\r
693 @retval EFI_DEVICE_ERROR A hardware failure occured... (from SerialIo)\r
c1f23d63 694\r
cfcbb8bc 695**/\r
49c8b87c 696EFI_STATUS\r
697EFIAPI\r
698DebugPortPoll (\r
699 IN EFI_DEBUGPORT_PROTOCOL *This\r
700 )\r
c1f23d63 701{\r
702 EFI_STATUS Status;\r
703 UINT32 SerialControl;\r
704 DEBUGPORT_DEVICE *DebugPortDevice;\r
705\r
706 DebugPortDevice = DEBUGPORT_DEVICE_FROM_THIS (This);\r
707\r
708 Status = DebugPortDevice->SerialIoBinding->GetControl (\r
709 DebugPortDevice->SerialIoBinding,\r
710 &SerialControl\r
711 );\r
712\r
713 if (!EFI_ERROR (Status)) {\r
49c8b87c 714 if ((SerialControl & EFI_SERIAL_INPUT_BUFFER_EMPTY) != 0) {\r
c1f23d63 715 Status = EFI_NOT_READY;\r
716 } else {\r
717 Status = EFI_SUCCESS;\r
718 }\r
719 }\r
720\r
721 return Status;\r
722}\r
723\r
49c8b87c 724/**\r
c1f23d63 725 Unload function that is registered in the LoadImage protocol. It un-installs\r
726 protocols produced and deallocates pool used by the driver. Called by the core\r
727 when unloading the driver.\r
fb0b259e 728\r
49c8b87c 729 @param ImageHandle\r
c1f23d63 730\r
49c8b87c 731 @retval EFI_SUCCESS Unload Debug Port driver successfully.\r
732 @retval EFI_ABORTED Serial IO is still binding.\r
c1f23d63 733\r
49c8b87c 734**/\r
735EFI_STATUS\r
736EFIAPI\r
737ImageUnloadHandler (\r
738 EFI_HANDLE ImageHandle\r
739 )\r
c1f23d63 740{\r
2d285faa 741 if (mDebugPortDevice.SerialIoBinding != NULL) {\r
c1f23d63 742 return EFI_ABORTED;\r
743 }\r
744\r
c1f23d63 745 //\r
746 // Clean up allocations\r
747 //\r
2d285faa 748 if (mDebugPortDevice.DebugPortVariable != NULL) {\r
749 FreePool (mDebugPortDevice.DebugPortVariable);\r
c1f23d63 750 }\r
751\r
c1f23d63 752 return EFI_SUCCESS;\r
753}\r