2 Top level C file for debugport driver. Contains initialization function.
3 This driver layers on top of SerialIo.
4 ALL CODE IN THE SERIALIO STACK MUST BE RE-ENTRANT AND CALLABLE FROM
7 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "DebugPort.h"
23 EFI_DRIVER_BINDING_PROTOCOL gDebugPortDriverBinding
= {
27 DEBUGPORT_DRIVER_VERSION
,
32 DEBUGPORT_DEVICE mDebugPortDevice
= {
33 DEBUGPORT_DEVICE_SIGNATURE
,
37 (EFI_DEVICE_PATH_PROTOCOL
*) NULL
,
45 (EFI_SERIAL_IO_PROTOCOL
*) NULL
,
46 DEBUGPORT_UART_DEFAULT_BAUDRATE
,
47 DEBUGPORT_UART_DEFAULT_FIFO_DEPTH
,
48 DEBUGPORT_UART_DEFAULT_TIMEOUT
,
49 (EFI_PARITY_TYPE
) DEBUGPORT_UART_DEFAULT_PARITY
,
50 DEBUGPORT_UART_DEFAULT_DATA_BITS
,
51 (EFI_STOP_BITS_TYPE
) DEBUGPORT_UART_DEFAULT_STOP_BITS
55 Local worker function to obtain device path information from DebugPort variable.
57 Records requested settings in DebugPort device structure.
61 GetDebugPortVariable (
66 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
71 Status
= gRT
->GetVariable (
72 (CHAR16
*) EFI_DEBUGPORT_VARIABLE_NAME
,
73 &gEfiDebugPortVariableGuid
,
76 mDebugPortDevice
.DebugPortVariable
79 if (Status
== EFI_BUFFER_TOO_SMALL
) {
80 if (mDebugPortDevice
.DebugPortVariable
!= NULL
) {
81 FreePool (mDebugPortDevice
.DebugPortVariable
);
84 mDebugPortDevice
.DebugPortVariable
= AllocatePool (DataSize
);
85 if (mDebugPortDevice
.DebugPortVariable
!= NULL
) {
87 (CHAR16
*) EFI_DEBUGPORT_VARIABLE_NAME
,
88 &gEfiDebugPortVariableGuid
,
91 mDebugPortDevice
.DebugPortVariable
93 DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) mDebugPortDevice
.DebugPortVariable
;
94 while (!IsDevicePathEnd (DevicePath
) && !IS_UART_DEVICEPATH (DevicePath
)) {
95 DevicePath
= NextDevicePathNode (DevicePath
);
98 if (IsDevicePathEnd (DevicePath
)) {
99 FreePool (mDebugPortDevice
.DebugPortVariable
);
100 mDebugPortDevice
.DebugPortVariable
= NULL
;
103 &mDebugPortDevice
.BaudRate
,
104 &((UART_DEVICE_PATH
*) DevicePath
)->BaudRate
,
105 sizeof (((UART_DEVICE_PATH
*) DevicePath
)->BaudRate
)
107 mDebugPortDevice
.ReceiveFifoDepth
= DEBUGPORT_UART_DEFAULT_FIFO_DEPTH
;
108 mDebugPortDevice
.Timeout
= DEBUGPORT_UART_DEFAULT_TIMEOUT
;
110 &mDebugPortDevice
.Parity
,
111 &((UART_DEVICE_PATH
*) DevicePath
)->Parity
,
112 sizeof (((UART_DEVICE_PATH
*) DevicePath
)->Parity
)
115 &mDebugPortDevice
.DataBits
,
116 &((UART_DEVICE_PATH
*) DevicePath
)->DataBits
,
117 sizeof (((UART_DEVICE_PATH
*) DevicePath
)->DataBits
)
120 &mDebugPortDevice
.StopBits
,
121 &((UART_DEVICE_PATH
*) DevicePath
)->StopBits
,
122 sizeof (((UART_DEVICE_PATH
*) DevicePath
)->StopBits
)
130 Debug Port Driver entry point.
132 Reads DebugPort variable to determine what device and settings to use as the
133 debug port. Binds exclusively to SerialIo. Reverts to defaults if no variable
136 @param[in] ImageHandle The firmware allocated handle for the EFI image.
137 @param[in] SystemTable A pointer to the EFI System Table.
139 @retval EFI_SUCCESS The entry point is executed successfully.
140 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.
141 @retval other Some error occurs when executing this entry point.
146 InitializeDebugPortDriver (
147 IN EFI_HANDLE ImageHandle
,
148 IN EFI_SYSTEM_TABLE
*SystemTable
154 // Install driver model protocol(s).
156 Status
= EfiLibInstallDriverBindingComponentName2 (
159 &gDebugPortDriverBinding
,
161 &gDebugPortComponentName
,
162 &gDebugPortComponentName2
164 ASSERT_EFI_ERROR (Status
);
170 Checks to see if there's not already a DebugPort interface somewhere.
172 If there's a DEBUGPORT variable, the device path must match exactly. If there's
173 no DEBUGPORT variable, then device path is not checked and does not matter.
174 Checks to see that there's a serial io interface on the controller handle
175 that can be bound BY_DRIVER | EXCLUSIVE.
176 If all these tests succeed, then we return EFI_SUCCESS, else, EFI_UNSUPPORTED
177 or other error returned by OpenProtocol.
179 @param This Protocol instance pointer.
180 @param ControllerHandle Handle of device to test.
181 @param RemainingDevicePath Optional parameter use to pick a specific child
184 @retval EFI_SUCCESS This driver supports this device.
185 @retval EFI_UNSUPPORTED Debug Port device is not supported.
186 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.
187 @retval others Some error occurs.
193 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
194 IN EFI_HANDLE ControllerHandle
,
195 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
199 EFI_DEVICE_PATH_PROTOCOL
*Dp1
;
200 EFI_DEVICE_PATH_PROTOCOL
*Dp2
;
201 EFI_SERIAL_IO_PROTOCOL
*SerialIo
;
202 EFI_DEBUGPORT_PROTOCOL
*DebugPortInterface
;
203 EFI_HANDLE TempHandle
;
206 // Check to see that there's not a debugport protocol already published,
207 // since only one standard UART serial port could be supported by this driver.
209 if (gBS
->LocateProtocol (&gEfiDebugPortProtocolGuid
, NULL
, (VOID
**) &DebugPortInterface
) != EFI_NOT_FOUND
) {
210 return EFI_UNSUPPORTED
;
213 // Read DebugPort variable to determine debug port selection and parameters
215 GetDebugPortVariable ();
217 if (mDebugPortDevice
.DebugPortVariable
!= NULL
) {
219 // There's a DEBUGPORT variable, so do LocateDevicePath and check to see if
220 // the closest matching handle matches the controller handle, and if it does,
221 // check to see that the remaining device path has the DebugPort GUIDed messaging
222 // device path only. Otherwise, it's a mismatch and EFI_UNSUPPORTED is returned.
224 Dp1
= DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL
*) mDebugPortDevice
.DebugPortVariable
);
226 return EFI_OUT_OF_RESOURCES
;
231 Status
= gBS
->LocateDevicePath (
232 &gEfiSerialIoProtocolGuid
,
237 if (Status
== EFI_SUCCESS
&& TempHandle
!= ControllerHandle
) {
238 Status
= EFI_UNSUPPORTED
;
241 if (Status
== EFI_SUCCESS
&&
242 (Dp2
->Type
!= MESSAGING_DEVICE_PATH
||
243 Dp2
->SubType
!= MSG_VENDOR_DP
||
244 *((UINT16
*) Dp2
->Length
) != sizeof (DEBUGPORT_DEVICE_PATH
))) {
246 Status
= EFI_UNSUPPORTED
;
249 if (Status
== EFI_SUCCESS
&& !CompareGuid (&gEfiDebugPortDevicePathGuid
, (GUID
*) (Dp2
+ 1))) {
250 Status
= EFI_UNSUPPORTED
;
254 if (EFI_ERROR (Status
)) {
259 Status
= gBS
->OpenProtocol (
261 &gEfiSerialIoProtocolGuid
,
263 This
->DriverBindingHandle
,
265 EFI_OPEN_PROTOCOL_BY_DRIVER
| EFI_OPEN_PROTOCOL_EXCLUSIVE
267 if (EFI_ERROR (Status
)) {
273 &gEfiSerialIoProtocolGuid
,
274 This
->DriverBindingHandle
,
282 Binds exclusively to serial io on the controller handle, Produces DebugPort
283 protocol and DevicePath on new handle.
285 @param This Protocol instance pointer.
286 @param ControllerHandle Handle of device to bind driver to.
287 @param RemainingDevicePath Optional parameter use to pick a specific child
290 @retval EFI_SUCCESS This driver is added to ControllerHandle.
291 @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.
292 @retval others Some error occurs.
298 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
299 IN EFI_HANDLE ControllerHandle
,
300 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
304 DEBUGPORT_DEVICE_PATH DebugPortDP
;
305 EFI_DEVICE_PATH_PROTOCOL EndDP
;
306 EFI_DEVICE_PATH_PROTOCOL
*Dp1
;
308 Status
= gBS
->OpenProtocol (
310 &gEfiSerialIoProtocolGuid
,
311 (VOID
**) &mDebugPortDevice
.SerialIoBinding
,
312 This
->DriverBindingHandle
,
314 EFI_OPEN_PROTOCOL_BY_DRIVER
| EFI_OPEN_PROTOCOL_EXCLUSIVE
316 if (EFI_ERROR (Status
)) {
320 mDebugPortDevice
.SerialIoDeviceHandle
= ControllerHandle
;
323 // Initialize the Serial Io interface...
325 Status
= mDebugPortDevice
.SerialIoBinding
->SetAttributes (
326 mDebugPortDevice
.SerialIoBinding
,
327 mDebugPortDevice
.BaudRate
,
328 mDebugPortDevice
.ReceiveFifoDepth
,
329 mDebugPortDevice
.Timeout
,
330 mDebugPortDevice
.Parity
,
331 mDebugPortDevice
.DataBits
,
332 mDebugPortDevice
.StopBits
334 if (EFI_ERROR (Status
)) {
335 mDebugPortDevice
.BaudRate
= 0;
336 mDebugPortDevice
.Parity
= DefaultParity
;
337 mDebugPortDevice
.DataBits
= 0;
338 mDebugPortDevice
.StopBits
= DefaultStopBits
;
339 mDebugPortDevice
.ReceiveFifoDepth
= 0;
340 Status
= mDebugPortDevice
.SerialIoBinding
->SetAttributes (
341 mDebugPortDevice
.SerialIoBinding
,
342 mDebugPortDevice
.BaudRate
,
343 mDebugPortDevice
.ReceiveFifoDepth
,
344 mDebugPortDevice
.Timeout
,
345 mDebugPortDevice
.Parity
,
346 mDebugPortDevice
.DataBits
,
347 mDebugPortDevice
.StopBits
349 if (EFI_ERROR (Status
)) {
352 &gEfiSerialIoProtocolGuid
,
353 This
->DriverBindingHandle
,
360 mDebugPortDevice
.SerialIoBinding
->Reset (mDebugPortDevice
.SerialIoBinding
);
363 // Create device path instance for DebugPort
365 DebugPortDP
.Header
.Type
= MESSAGING_DEVICE_PATH
;
366 DebugPortDP
.Header
.SubType
= MSG_VENDOR_DP
;
367 SetDevicePathNodeLength (&(DebugPortDP
.Header
), sizeof (DebugPortDP
));
368 CopyGuid (&DebugPortDP
.Guid
, &gEfiDebugPortDevicePathGuid
);
370 Dp1
= DevicePathFromHandle (ControllerHandle
);
373 SetDevicePathEndNode (Dp1
);
376 mDebugPortDevice
.DebugPortDevicePath
= AppendDevicePathNode (Dp1
, (EFI_DEVICE_PATH_PROTOCOL
*) &DebugPortDP
);
377 if (mDebugPortDevice
.DebugPortDevicePath
== NULL
) {
378 return EFI_OUT_OF_RESOURCES
;
381 // Publish DebugPort and Device Path protocols
383 Status
= gBS
->InstallMultipleProtocolInterfaces (
384 &mDebugPortDevice
.DebugPortDeviceHandle
,
385 &gEfiDevicePathProtocolGuid
,
386 mDebugPortDevice
.DebugPortDevicePath
,
387 &gEfiDebugPortProtocolGuid
,
388 &mDebugPortDevice
.DebugPortInterface
,
392 if (EFI_ERROR (Status
)) {
395 &gEfiSerialIoProtocolGuid
,
396 This
->DriverBindingHandle
,
402 // Connect debugport child to serial io
404 Status
= gBS
->OpenProtocol (
406 &gEfiSerialIoProtocolGuid
,
407 (VOID
**) &mDebugPortDevice
.SerialIoBinding
,
408 This
->DriverBindingHandle
,
409 mDebugPortDevice
.DebugPortDeviceHandle
,
410 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
413 if (EFI_ERROR (Status
)) {
419 &mDebugPortDevice
.DebugPortInterface
,
422 "DebugPort driver failed to open child controller\n\n"
428 &gEfiSerialIoProtocolGuid
,
429 This
->DriverBindingHandle
,
440 &mDebugPortDevice
.DebugPortInterface
,
443 "Hello World from the DebugPort driver\n\n"
452 Stop this driver on ControllerHandle by removing Serial IO protocol on
453 the ControllerHandle.
455 @param This Protocol instance pointer.
456 @param ControllerHandle Handle of device to stop driver on
457 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
458 children is zero stop the entire bus driver.
459 @param ChildHandleBuffer List of Child Handles to Stop.
461 @retval EFI_SUCCESS This driver is removed ControllerHandle.
462 @retval other This driver was not removed from this device.
468 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
469 IN EFI_HANDLE ControllerHandle
,
470 IN UINTN NumberOfChildren
,
471 IN EFI_HANDLE
*ChildHandleBuffer
476 if (NumberOfChildren
== 0) {
478 // Close the bus driver
482 &gEfiSerialIoProtocolGuid
,
483 This
->DriverBindingHandle
,
487 mDebugPortDevice
.SerialIoBinding
= NULL
;
491 &gEfiDevicePathProtocolGuid
,
492 This
->DriverBindingHandle
,
496 FreePool (mDebugPortDevice
.DebugPortDevicePath
);
501 // Disconnect SerialIo child handle
503 Status
= gBS
->CloseProtocol (
504 mDebugPortDevice
.SerialIoDeviceHandle
,
505 &gEfiSerialIoProtocolGuid
,
506 This
->DriverBindingHandle
,
507 mDebugPortDevice
.DebugPortDeviceHandle
510 if (EFI_ERROR (Status
)) {
514 // Unpublish our protocols (DevicePath, DebugPort)
516 Status
= gBS
->UninstallMultipleProtocolInterfaces (
517 mDebugPortDevice
.DebugPortDeviceHandle
,
518 &gEfiDevicePathProtocolGuid
,
519 mDebugPortDevice
.DebugPortDevicePath
,
520 &gEfiDebugPortProtocolGuid
,
521 &mDebugPortDevice
.DebugPortInterface
,
525 if (EFI_ERROR (Status
)) {
528 &gEfiSerialIoProtocolGuid
,
529 (VOID
**) &mDebugPortDevice
.SerialIoBinding
,
530 This
->DriverBindingHandle
,
531 mDebugPortDevice
.DebugPortDeviceHandle
,
532 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
535 mDebugPortDevice
.DebugPortDeviceHandle
= NULL
;
543 DebugPort protocol member function. Calls SerialIo:GetControl to flush buffer.
544 We cannot call SerialIo:SetAttributes because it uses pool services, which use
545 locks, which affect TPL, so it's not interrupt context safe or re-entrant.
546 SerialIo:Reset() calls SetAttributes, so it can't be used either.
548 The port itself should be fine since it was set up during initialization.
550 @param This Protocol instance pointer.
552 @return EFI_SUCCESS Always.
558 IN EFI_DEBUGPORT_PROTOCOL
*This
564 while (This
->Poll (This
) == EFI_SUCCESS
) {
566 This
->Read (This
, 0, &BufferSize
, &BitBucket
);
573 DebugPort protocol member function. Calls SerialIo:Read() after setting
574 if it's different than the last SerialIo access.
576 @param This Pointer to DebugPort protocol.
577 @param Timeout Timeout value.
578 @param BufferSize On input, the size of Buffer.
579 On output, the amount of data actually written.
580 @param Buffer Pointer to buffer to read.
589 IN EFI_DEBUGPORT_PROTOCOL
*This
,
591 IN OUT UINTN
*BufferSize
,
595 DEBUGPORT_DEVICE
*DebugPortDevice
;
596 UINTN LocalBufferSize
;
600 DebugPortDevice
= DEBUGPORT_DEVICE_FROM_THIS (This
);
602 LocalBufferSize
= *BufferSize
;
605 Status
= DebugPortDevice
->SerialIoBinding
->Read (
606 DebugPortDevice
->SerialIoBinding
,
610 if (Status
== EFI_TIMEOUT
) {
611 if (Timeout
> DEBUGPORT_UART_DEFAULT_TIMEOUT
) {
612 Timeout
-= DEBUGPORT_UART_DEFAULT_TIMEOUT
;
616 } else if (EFI_ERROR (Status
)) {
620 BufferPtr
+= LocalBufferSize
;
621 LocalBufferSize
= *BufferSize
- (BufferPtr
- (UINT8
*) Buffer
);
622 } while (LocalBufferSize
!= 0 && Timeout
> 0);
624 *BufferSize
= (UINTN
) (BufferPtr
- (UINT8
*) Buffer
);
630 DebugPort protocol member function. Calls SerialIo:Write() Writes 8 bytes at
631 a time and does a GetControl between 8 byte writes to help insure reads are
632 interspersed This is poor-man's flow control.
634 @param This Pointer to DebugPort protocol.
635 @param Timeout Timeout value.
636 @param BufferSize On input, the size of Buffer.
637 On output, the amount of data actually written.
638 @param Buffer Pointer to buffer to read.
640 @retval EFI_SUCCESS The data was written.
641 @retval others Fails when writting datas to debug port device.
647 IN EFI_DEBUGPORT_PROTOCOL
*This
,
649 IN OUT UINTN
*BufferSize
,
653 DEBUGPORT_DEVICE
*DebugPortDevice
;
657 UINT32 SerialControl
;
659 Status
= EFI_SUCCESS
;
660 DebugPortDevice
= DEBUGPORT_DEVICE_FROM_THIS (This
);
663 for (Position
= 0; Position
< *BufferSize
&& !EFI_ERROR (Status
); Position
+= WriteSize
) {
664 DebugPortDevice
->SerialIoBinding
->GetControl (
665 DebugPortDevice
->SerialIoBinding
,
668 if (*BufferSize
- Position
< 8) {
669 WriteSize
= *BufferSize
- Position
;
672 Status
= DebugPortDevice
->SerialIoBinding
->Write (
673 DebugPortDevice
->SerialIoBinding
,
675 &((UINT8
*) Buffer
)[Position
]
679 *BufferSize
= Position
;
684 DebugPort protocol member function. Calls SerialIo:Write() after setting
685 if it's different than the last SerialIo access.
687 @param This Pointer to DebugPort protocol.
689 @retval EFI_SUCCESS At least 1 character is ready to be read from
690 the DebugPort interface.
691 @retval EFI_NOT_READY There are no characters ready to read from the
693 @retval EFI_DEVICE_ERROR A hardware failure occured... (from SerialIo)
699 IN EFI_DEBUGPORT_PROTOCOL
*This
703 UINT32 SerialControl
;
704 DEBUGPORT_DEVICE
*DebugPortDevice
;
706 DebugPortDevice
= DEBUGPORT_DEVICE_FROM_THIS (This
);
708 Status
= DebugPortDevice
->SerialIoBinding
->GetControl (
709 DebugPortDevice
->SerialIoBinding
,
713 if (!EFI_ERROR (Status
)) {
714 if ((SerialControl
& EFI_SERIAL_INPUT_BUFFER_EMPTY
) != 0) {
715 Status
= EFI_NOT_READY
;
717 Status
= EFI_SUCCESS
;
725 Unload function that is registered in the LoadImage protocol. It un-installs
726 protocols produced and deallocates pool used by the driver. Called by the core
727 when unloading the driver.
731 @retval EFI_SUCCESS Unload Debug Port driver successfully.
732 @retval EFI_ABORTED Serial IO is still binding.
738 EFI_HANDLE ImageHandle
741 if (mDebugPortDevice
.SerialIoBinding
!= NULL
) {
746 // Clean up allocations
748 if (mDebugPortDevice
.DebugPortVariable
!= NULL
) {
749 FreePool (mDebugPortDevice
.DebugPortVariable
);