]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/DebugPortDxe/DebugPort.c
Update the copyright notice format
[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
e5eed7d3
HT
7Copyright (c) 2006 - 2009, Intel Corporation. All rights reserved.<BR>\r
8This program and the accompanying materials\r
fb0b259e 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
c1f23d63 414 gBS->CloseProtocol (\r
415 ControllerHandle,\r
416 &gEfiSerialIoProtocolGuid,\r
417 This->DriverBindingHandle,\r
418 ControllerHandle\r
419 );\r
420 return Status;\r
421 }\r
422\r
c1f23d63 423 return EFI_SUCCESS;\r
424}\r
425\r
49c8b87c 426/**\r
427 Stop this driver on ControllerHandle by removing Serial IO protocol on\r
428 the ControllerHandle.\r
429\r
430 @param This Protocol instance pointer.\r
431 @param ControllerHandle Handle of device to stop driver on\r
432 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r
433 children is zero stop the entire bus driver.\r
434 @param ChildHandleBuffer List of Child Handles to Stop.\r
435\r
436 @retval EFI_SUCCESS This driver is removed ControllerHandle.\r
437 @retval other This driver was not removed from this device.\r
438\r
439**/\r
c1f23d63 440EFI_STATUS\r
441EFIAPI\r
442DebugPortStop (\r
443 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
444 IN EFI_HANDLE ControllerHandle,\r
445 IN UINTN NumberOfChildren,\r
446 IN EFI_HANDLE *ChildHandleBuffer\r
447 )\r
c1f23d63 448{\r
449 EFI_STATUS Status;\r
450\r
451 if (NumberOfChildren == 0) {\r
452 //\r
453 // Close the bus driver\r
454 //\r
455 gBS->CloseProtocol (\r
456 ControllerHandle,\r
457 &gEfiSerialIoProtocolGuid,\r
458 This->DriverBindingHandle,\r
459 ControllerHandle\r
460 );\r
461\r
2d285faa 462 mDebugPortDevice.SerialIoBinding = NULL;\r
c1f23d63 463\r
464 gBS->CloseProtocol (\r
465 ControllerHandle,\r
466 &gEfiDevicePathProtocolGuid,\r
467 This->DriverBindingHandle,\r
468 ControllerHandle\r
469 );\r
470\r
2d285faa 471 FreePool (mDebugPortDevice.DebugPortDevicePath);\r
c1f23d63 472\r
473 return EFI_SUCCESS;\r
474 } else {\r
475 //\r
476 // Disconnect SerialIo child handle\r
477 //\r
478 Status = gBS->CloseProtocol (\r
2d285faa 479 mDebugPortDevice.SerialIoDeviceHandle,\r
c1f23d63 480 &gEfiSerialIoProtocolGuid,\r
481 This->DriverBindingHandle,\r
2d285faa 482 mDebugPortDevice.DebugPortDeviceHandle\r
c1f23d63 483 );\r
484\r
485 if (EFI_ERROR (Status)) {\r
486 return Status;\r
487 }\r
488 //\r
489 // Unpublish our protocols (DevicePath, DebugPort)\r
490 //\r
491 Status = gBS->UninstallMultipleProtocolInterfaces (\r
2d285faa 492 mDebugPortDevice.DebugPortDeviceHandle,\r
c1f23d63 493 &gEfiDevicePathProtocolGuid,\r
2d285faa 494 mDebugPortDevice.DebugPortDevicePath,\r
c1f23d63 495 &gEfiDebugPortProtocolGuid,\r
2d285faa 496 &mDebugPortDevice.DebugPortInterface,\r
c1f23d63 497 NULL\r
498 );\r
499\r
500 if (EFI_ERROR (Status)) {\r
501 gBS->OpenProtocol (\r
502 ControllerHandle,\r
503 &gEfiSerialIoProtocolGuid,\r
2d285faa 504 (VOID **) &mDebugPortDevice.SerialIoBinding,\r
c1f23d63 505 This->DriverBindingHandle,\r
2d285faa 506 mDebugPortDevice.DebugPortDeviceHandle,\r
c1f23d63 507 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
508 );\r
509 } else {\r
2d285faa 510 mDebugPortDevice.DebugPortDeviceHandle = NULL;\r
c1f23d63 511 }\r
512 }\r
513\r
514 return Status;\r
515}\r
c1f23d63 516\r
49c8b87c 517/**\r
c1f23d63 518 DebugPort protocol member function. Calls SerialIo:GetControl to flush buffer.\r
519 We cannot call SerialIo:SetAttributes because it uses pool services, which use\r
520 locks, which affect TPL, so it's not interrupt context safe or re-entrant.\r
521 SerialIo:Reset() calls SetAttributes, so it can't be used either.\r
fb0b259e 522\r
523 The port itself should be fine since it was set up during initialization.\r
524\r
cfcbb8bc 525 @param This Protocol instance pointer.\r
c1f23d63 526\r
49c8b87c 527 @return EFI_SUCCESS Always.\r
c1f23d63 528\r
cfcbb8bc 529**/\r
49c8b87c 530EFI_STATUS\r
531EFIAPI\r
532DebugPortReset (\r
533 IN EFI_DEBUGPORT_PROTOCOL *This\r
534 )\r
c1f23d63 535{\r
536 UINTN BufferSize;\r
537 UINTN BitBucket;\r
538\r
539 while (This->Poll (This) == EFI_SUCCESS) {\r
540 BufferSize = 1;\r
541 This->Read (This, 0, &BufferSize, &BitBucket);\r
542 }\r
543\r
544 return EFI_SUCCESS;\r
545}\r
546\r
49c8b87c 547/**\r
548 DebugPort protocol member function. Calls SerialIo:Read() after setting\r
549 if it's different than the last SerialIo access.\r
550\r
551 @param This Pointer to DebugPort protocol.\r
552 @param Timeout Timeout value.\r
553 @param BufferSize On input, the size of Buffer.\r
554 On output, the amount of data actually written.\r
555 @param Buffer Pointer to buffer to read.\r
556\r
cfcbb8bc 557 @retval EFI_SUCCESS\r
558 @retval others\r
49c8b87c 559\r
560**/\r
c1f23d63 561EFI_STATUS\r
562EFIAPI\r
563DebugPortRead (\r
564 IN EFI_DEBUGPORT_PROTOCOL *This,\r
565 IN UINT32 Timeout,\r
566 IN OUT UINTN *BufferSize,\r
567 IN VOID *Buffer\r
568 )\r
c1f23d63 569{\r
570 DEBUGPORT_DEVICE *DebugPortDevice;\r
571 UINTN LocalBufferSize;\r
572 EFI_STATUS Status;\r
573 UINT8 *BufferPtr;\r
574\r
575 DebugPortDevice = DEBUGPORT_DEVICE_FROM_THIS (This);\r
576 BufferPtr = Buffer;\r
577 LocalBufferSize = *BufferSize;\r
2d285faa 578\r
c1f23d63 579 do {\r
580 Status = DebugPortDevice->SerialIoBinding->Read (\r
581 DebugPortDevice->SerialIoBinding,\r
582 &LocalBufferSize,\r
583 BufferPtr\r
584 );\r
585 if (Status == EFI_TIMEOUT) {\r
586 if (Timeout > DEBUGPORT_UART_DEFAULT_TIMEOUT) {\r
587 Timeout -= DEBUGPORT_UART_DEFAULT_TIMEOUT;\r
588 } else {\r
589 Timeout = 0;\r
590 }\r
591 } else if (EFI_ERROR (Status)) {\r
592 break;\r
593 }\r
594\r
595 BufferPtr += LocalBufferSize;\r
596 LocalBufferSize = *BufferSize - (BufferPtr - (UINT8 *) Buffer);\r
597 } while (LocalBufferSize != 0 && Timeout > 0);\r
598\r
599 *BufferSize = (UINTN) (BufferPtr - (UINT8 *) Buffer);\r
600\r
601 return Status;\r
602}\r
603\r
49c8b87c 604/**\r
605 DebugPort protocol member function. Calls SerialIo:Write() Writes 8 bytes at\r
606 a time and does a GetControl between 8 byte writes to help insure reads are\r
607 interspersed This is poor-man's flow control.\r
608\r
609 @param This Pointer to DebugPort protocol.\r
610 @param Timeout Timeout value.\r
611 @param BufferSize On input, the size of Buffer.\r
612 On output, the amount of data actually written.\r
613 @param Buffer Pointer to buffer to read.\r
614\r
615 @retval EFI_SUCCESS The data was written.\r
616 @retval others Fails when writting datas to debug port device.\r
617\r
618**/\r
c1f23d63 619EFI_STATUS\r
620EFIAPI\r
621DebugPortWrite (\r
622 IN EFI_DEBUGPORT_PROTOCOL *This,\r
623 IN UINT32 Timeout,\r
624 IN OUT UINTN *BufferSize,\r
625 OUT VOID *Buffer\r
626 )\r
c1f23d63 627{\r
628 DEBUGPORT_DEVICE *DebugPortDevice;\r
629 UINTN Position;\r
630 UINTN WriteSize;\r
631 EFI_STATUS Status;\r
632 UINT32 SerialControl;\r
633\r
634 Status = EFI_SUCCESS;\r
635 DebugPortDevice = DEBUGPORT_DEVICE_FROM_THIS (This);\r
636\r
637 WriteSize = 8;\r
638 for (Position = 0; Position < *BufferSize && !EFI_ERROR (Status); Position += WriteSize) {\r
639 DebugPortDevice->SerialIoBinding->GetControl (\r
640 DebugPortDevice->SerialIoBinding,\r
641 &SerialControl\r
642 );\r
643 if (*BufferSize - Position < 8) {\r
644 WriteSize = *BufferSize - Position;\r
645 }\r
646\r
647 Status = DebugPortDevice->SerialIoBinding->Write (\r
648 DebugPortDevice->SerialIoBinding,\r
649 &WriteSize,\r
650 &((UINT8 *) Buffer)[Position]\r
651 );\r
652 }\r
653\r
654 *BufferSize = Position;\r
655 return Status;\r
656}\r
657\r
49c8b87c 658/**\r
c1f23d63 659 DebugPort protocol member function. Calls SerialIo:Write() after setting\r
660 if it's different than the last SerialIo access.\r
fb0b259e 661\r
49c8b87c 662 @param This Pointer to DebugPort protocol.\r
c1f23d63 663\r
49c8b87c 664 @retval EFI_SUCCESS At least 1 character is ready to be read from\r
665 the DebugPort interface.\r
666 @retval EFI_NOT_READY There are no characters ready to read from the\r
667 DebugPort interface\r
668 @retval EFI_DEVICE_ERROR A hardware failure occured... (from SerialIo)\r
c1f23d63 669\r
cfcbb8bc 670**/\r
49c8b87c 671EFI_STATUS\r
672EFIAPI\r
673DebugPortPoll (\r
674 IN EFI_DEBUGPORT_PROTOCOL *This\r
675 )\r
c1f23d63 676{\r
677 EFI_STATUS Status;\r
678 UINT32 SerialControl;\r
679 DEBUGPORT_DEVICE *DebugPortDevice;\r
680\r
681 DebugPortDevice = DEBUGPORT_DEVICE_FROM_THIS (This);\r
682\r
683 Status = DebugPortDevice->SerialIoBinding->GetControl (\r
684 DebugPortDevice->SerialIoBinding,\r
685 &SerialControl\r
686 );\r
687\r
688 if (!EFI_ERROR (Status)) {\r
49c8b87c 689 if ((SerialControl & EFI_SERIAL_INPUT_BUFFER_EMPTY) != 0) {\r
c1f23d63 690 Status = EFI_NOT_READY;\r
691 } else {\r
692 Status = EFI_SUCCESS;\r
693 }\r
694 }\r
695\r
696 return Status;\r
697}\r
698\r
49c8b87c 699/**\r
c1f23d63 700 Unload function that is registered in the LoadImage protocol. It un-installs\r
701 protocols produced and deallocates pool used by the driver. Called by the core\r
702 when unloading the driver.\r
fb0b259e 703\r
49c8b87c 704 @param ImageHandle\r
c1f23d63 705\r
49c8b87c 706 @retval EFI_SUCCESS Unload Debug Port driver successfully.\r
707 @retval EFI_ABORTED Serial IO is still binding.\r
c1f23d63 708\r
49c8b87c 709**/\r
710EFI_STATUS\r
711EFIAPI\r
712ImageUnloadHandler (\r
713 EFI_HANDLE ImageHandle\r
714 )\r
c1f23d63 715{\r
2d285faa 716 if (mDebugPortDevice.SerialIoBinding != NULL) {\r
c1f23d63 717 return EFI_ABORTED;\r
718 }\r
719\r
c1f23d63 720 //\r
721 // Clean up allocations\r
722 //\r
2d285faa 723 if (mDebugPortDevice.DebugPortVariable != NULL) {\r
724 FreePool (mDebugPortDevice.DebugPortVariable);\r
c1f23d63 725 }\r
726\r
c1f23d63 727 return EFI_SUCCESS;\r
728}\r