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 - 2009, Intel Corporation. All rights reserved.<BR>
8 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
)) {
416 &gEfiSerialIoProtocolGuid
,
417 This
->DriverBindingHandle
,
427 Stop this driver on ControllerHandle by removing Serial IO protocol on
428 the ControllerHandle.
430 @param This Protocol instance pointer.
431 @param ControllerHandle Handle of device to stop driver on
432 @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of
433 children is zero stop the entire bus driver.
434 @param ChildHandleBuffer List of Child Handles to Stop.
436 @retval EFI_SUCCESS This driver is removed ControllerHandle.
437 @retval other This driver was not removed from this device.
443 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
444 IN EFI_HANDLE ControllerHandle
,
445 IN UINTN NumberOfChildren
,
446 IN EFI_HANDLE
*ChildHandleBuffer
451 if (NumberOfChildren
== 0) {
453 // Close the bus driver
457 &gEfiSerialIoProtocolGuid
,
458 This
->DriverBindingHandle
,
462 mDebugPortDevice
.SerialIoBinding
= NULL
;
466 &gEfiDevicePathProtocolGuid
,
467 This
->DriverBindingHandle
,
471 FreePool (mDebugPortDevice
.DebugPortDevicePath
);
476 // Disconnect SerialIo child handle
478 Status
= gBS
->CloseProtocol (
479 mDebugPortDevice
.SerialIoDeviceHandle
,
480 &gEfiSerialIoProtocolGuid
,
481 This
->DriverBindingHandle
,
482 mDebugPortDevice
.DebugPortDeviceHandle
485 if (EFI_ERROR (Status
)) {
489 // Unpublish our protocols (DevicePath, DebugPort)
491 Status
= gBS
->UninstallMultipleProtocolInterfaces (
492 mDebugPortDevice
.DebugPortDeviceHandle
,
493 &gEfiDevicePathProtocolGuid
,
494 mDebugPortDevice
.DebugPortDevicePath
,
495 &gEfiDebugPortProtocolGuid
,
496 &mDebugPortDevice
.DebugPortInterface
,
500 if (EFI_ERROR (Status
)) {
503 &gEfiSerialIoProtocolGuid
,
504 (VOID
**) &mDebugPortDevice
.SerialIoBinding
,
505 This
->DriverBindingHandle
,
506 mDebugPortDevice
.DebugPortDeviceHandle
,
507 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
510 mDebugPortDevice
.DebugPortDeviceHandle
= NULL
;
518 DebugPort protocol member function. Calls SerialIo:GetControl to flush buffer.
519 We cannot call SerialIo:SetAttributes because it uses pool services, which use
520 locks, which affect TPL, so it's not interrupt context safe or re-entrant.
521 SerialIo:Reset() calls SetAttributes, so it can't be used either.
523 The port itself should be fine since it was set up during initialization.
525 @param This Protocol instance pointer.
527 @return EFI_SUCCESS Always.
533 IN EFI_DEBUGPORT_PROTOCOL
*This
539 while (This
->Poll (This
) == EFI_SUCCESS
) {
541 This
->Read (This
, 0, &BufferSize
, &BitBucket
);
548 DebugPort protocol member function. Calls SerialIo:Read() after setting
549 if it's different than the last SerialIo access.
551 @param This Pointer to DebugPort protocol.
552 @param Timeout Timeout value.
553 @param BufferSize On input, the size of Buffer.
554 On output, the amount of data actually written.
555 @param Buffer Pointer to buffer to read.
564 IN EFI_DEBUGPORT_PROTOCOL
*This
,
566 IN OUT UINTN
*BufferSize
,
570 DEBUGPORT_DEVICE
*DebugPortDevice
;
571 UINTN LocalBufferSize
;
575 DebugPortDevice
= DEBUGPORT_DEVICE_FROM_THIS (This
);
577 LocalBufferSize
= *BufferSize
;
580 Status
= DebugPortDevice
->SerialIoBinding
->Read (
581 DebugPortDevice
->SerialIoBinding
,
585 if (Status
== EFI_TIMEOUT
) {
586 if (Timeout
> DEBUGPORT_UART_DEFAULT_TIMEOUT
) {
587 Timeout
-= DEBUGPORT_UART_DEFAULT_TIMEOUT
;
591 } else if (EFI_ERROR (Status
)) {
595 BufferPtr
+= LocalBufferSize
;
596 LocalBufferSize
= *BufferSize
- (BufferPtr
- (UINT8
*) Buffer
);
597 } while (LocalBufferSize
!= 0 && Timeout
> 0);
599 *BufferSize
= (UINTN
) (BufferPtr
- (UINT8
*) Buffer
);
605 DebugPort protocol member function. Calls SerialIo:Write() Writes 8 bytes at
606 a time and does a GetControl between 8 byte writes to help insure reads are
607 interspersed This is poor-man's flow control.
609 @param This Pointer to DebugPort protocol.
610 @param Timeout Timeout value.
611 @param BufferSize On input, the size of Buffer.
612 On output, the amount of data actually written.
613 @param Buffer Pointer to buffer to read.
615 @retval EFI_SUCCESS The data was written.
616 @retval others Fails when writting datas to debug port device.
622 IN EFI_DEBUGPORT_PROTOCOL
*This
,
624 IN OUT UINTN
*BufferSize
,
628 DEBUGPORT_DEVICE
*DebugPortDevice
;
632 UINT32 SerialControl
;
634 Status
= EFI_SUCCESS
;
635 DebugPortDevice
= DEBUGPORT_DEVICE_FROM_THIS (This
);
638 for (Position
= 0; Position
< *BufferSize
&& !EFI_ERROR (Status
); Position
+= WriteSize
) {
639 DebugPortDevice
->SerialIoBinding
->GetControl (
640 DebugPortDevice
->SerialIoBinding
,
643 if (*BufferSize
- Position
< 8) {
644 WriteSize
= *BufferSize
- Position
;
647 Status
= DebugPortDevice
->SerialIoBinding
->Write (
648 DebugPortDevice
->SerialIoBinding
,
650 &((UINT8
*) Buffer
)[Position
]
654 *BufferSize
= Position
;
659 DebugPort protocol member function. Calls SerialIo:Write() after setting
660 if it's different than the last SerialIo access.
662 @param This Pointer to DebugPort protocol.
664 @retval EFI_SUCCESS At least 1 character is ready to be read from
665 the DebugPort interface.
666 @retval EFI_NOT_READY There are no characters ready to read from the
668 @retval EFI_DEVICE_ERROR A hardware failure occured... (from SerialIo)
674 IN EFI_DEBUGPORT_PROTOCOL
*This
678 UINT32 SerialControl
;
679 DEBUGPORT_DEVICE
*DebugPortDevice
;
681 DebugPortDevice
= DEBUGPORT_DEVICE_FROM_THIS (This
);
683 Status
= DebugPortDevice
->SerialIoBinding
->GetControl (
684 DebugPortDevice
->SerialIoBinding
,
688 if (!EFI_ERROR (Status
)) {
689 if ((SerialControl
& EFI_SERIAL_INPUT_BUFFER_EMPTY
) != 0) {
690 Status
= EFI_NOT_READY
;
692 Status
= EFI_SUCCESS
;
700 Unload function that is registered in the LoadImage protocol. It un-installs
701 protocols produced and deallocates pool used by the driver. Called by the core
702 when unloading the driver.
706 @retval EFI_SUCCESS Unload Debug Port driver successfully.
707 @retval EFI_ABORTED Serial IO is still binding.
713 EFI_HANDLE ImageHandle
716 if (mDebugPortDevice
.SerialIoBinding
!= NULL
) {
721 // Clean up allocations
723 if (mDebugPortDevice
.DebugPortVariable
!= NULL
) {
724 FreePool (mDebugPortDevice
.DebugPortVariable
);