Home | History | Annotate | Download | only in DebugPortDxe
      1 /** @file
      2   Definitions and prototypes for DebugPort driver.
      3 
      4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
      5 This program and the accompanying materials
      6 are licensed and made available under the terms and conditions of the BSD License
      7 which accompanies this distribution.  The full text of the license may be found at
      8 http://opensource.org/licenses/bsd-license.php
      9 
     10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     12 
     13 **/
     14 
     15 #ifndef __DEBUGPORT_H__
     16 #define __DEBUGPORT_H__
     17 
     18 
     19 #include <Uefi.h>
     20 
     21 #include <Protocol/DevicePath.h>
     22 #include <Protocol/ComponentName.h>
     23 #include <Protocol/DriverBinding.h>
     24 #include <Protocol/SerialIo.h>
     25 #include <Protocol/DebugPort.h>
     26 
     27 #include <Library/DebugLib.h>
     28 #include <Library/UefiDriverEntryPoint.h>
     29 #include <Library/UefiLib.h>
     30 #include <Library/BaseMemoryLib.h>
     31 #include <Library/MemoryAllocationLib.h>
     32 #include <Library/UefiBootServicesTableLib.h>
     33 #include <Library/UefiRuntimeServicesTableLib.h>
     34 #include <Library/DevicePathLib.h>
     35 
     36 //
     37 // Driver Binding Externs
     38 //
     39 extern EFI_DRIVER_BINDING_PROTOCOL  gDebugPortDriverBinding;
     40 extern EFI_COMPONENT_NAME_PROTOCOL  gDebugPortComponentName;
     41 extern EFI_COMPONENT_NAME2_PROTOCOL gDebugPortComponentName2;
     42 
     43 //
     44 // local type definitions
     45 //
     46 #define DEBUGPORT_DEVICE_SIGNATURE  SIGNATURE_32 ('D', 'B', 'G', 'P')
     47 
     48 //
     49 // Device structure used by driver
     50 //
     51 typedef struct {
     52   UINT32                      Signature;
     53   EFI_HANDLE                  DriverBindingHandle;
     54   EFI_HANDLE                  DebugPortDeviceHandle;
     55 
     56   EFI_DEVICE_PATH_PROTOCOL    *DebugPortDevicePath;
     57   EFI_DEBUGPORT_PROTOCOL      DebugPortInterface;
     58 
     59   EFI_HANDLE                  SerialIoDeviceHandle;
     60   EFI_SERIAL_IO_PROTOCOL      *SerialIoBinding;
     61   UINT64                      BaudRate;
     62   UINT32                      ReceiveFifoDepth;
     63   UINT32                      Timeout;
     64   EFI_PARITY_TYPE             Parity;
     65   UINT8                       DataBits;
     66   EFI_STOP_BITS_TYPE          StopBits;
     67 } DEBUGPORT_DEVICE;
     68 
     69 #define DEBUGPORT_DEVICE_FROM_THIS(a)     CR (a, DEBUGPORT_DEVICE, DebugPortInterface, DEBUGPORT_DEVICE_SIGNATURE)
     70 
     71 #define EFI_ACPI_PC_COMPORT_HID           EISA_PNP_ID (0x0500)
     72 #define EFI_ACPI_16550UART_HID            EISA_PNP_ID (0x0501)
     73 
     74 #define DEBUGPORT_UART_DEFAULT_BAUDRATE   115200
     75 #define DEBUGPORT_UART_DEFAULT_PARITY     0
     76 #define DEBUGPORT_UART_DEFAULT_FIFO_DEPTH 16
     77 #define DEBUGPORT_UART_DEFAULT_TIMEOUT    50000 ///< 5 ms
     78 #define DEBUGPORT_UART_DEFAULT_DATA_BITS  8
     79 #define DEBUGPORT_UART_DEFAULT_STOP_BITS  1
     80 
     81 #define DEBUGPORT_DRIVER_VERSION          1
     82 
     83 #define IS_UART_DEVICEPATH(dp)           (DevicePathType (dp) == MESSAGING_DEVICE_PATH && DevicePathSubType (dp) == MSG_UART_DP)
     84 
     85 /**
     86   Debug Port Driver entry point.
     87 
     88   Reads DebugPort variable to determine what device and settings to use as the
     89   debug port.  Binds exclusively to SerialIo. Reverts to defaults if no variable
     90   is found.
     91 
     92   @param[in] ImageHandle       The firmware allocated handle for the EFI image.
     93   @param[in] SystemTable       A pointer to the EFI System Table.
     94 
     95   @retval EFI_SUCCESS          The entry point is executed successfully.
     96   @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.
     97   @retval other                Some error occurs when executing this entry point.
     98 
     99 **/
    100 EFI_STATUS
    101 EFIAPI
    102 InitializeDebugPortDriver (
    103   IN EFI_HANDLE                     ImageHandle,
    104   IN EFI_SYSTEM_TABLE               *SystemTable
    105   );
    106 
    107 /**
    108   Checks to see if there's not already a DebugPort interface somewhere.
    109 
    110   If there's a DEBUGPORT variable, the device path must match exactly.  If there's
    111   no DEBUGPORT variable, then device path is not checked and does not matter.
    112   Checks to see that there's a serial io interface on the controller handle
    113   that can be bound BY_DRIVER | EXCLUSIVE.
    114   If all these tests succeed, then we return EFI_SUCCESS, else, EFI_UNSUPPORTED
    115   or other error returned by OpenProtocol.
    116 
    117   @param  This                 Protocol instance pointer.
    118   @param  ControllerHandle     Handle of device to test.
    119   @param  RemainingDevicePath  Optional parameter use to pick a specific child
    120                                device to start.
    121 
    122   @retval EFI_SUCCESS          This driver supports this device.
    123   @retval EFI_UNSUPPORTED      Debug Port device is not supported.
    124   @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.
    125   @retval others               Some error occurs.
    126 
    127 **/
    128 EFI_STATUS
    129 EFIAPI
    130 DebugPortSupported (
    131   IN EFI_DRIVER_BINDING_PROTOCOL    *This,
    132   IN EFI_HANDLE                     Controller,
    133   IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
    134   );
    135 
    136 /**
    137   Binds exclusively to serial io on the controller handle, Produces DebugPort
    138   protocol and DevicePath on new handle.
    139 
    140   @param  This                 Protocol instance pointer.
    141   @param  ControllerHandle     Handle of device to bind driver to.
    142   @param  RemainingDevicePath  Optional parameter use to pick a specific child
    143                                device to start.
    144 
    145   @retval EFI_SUCCESS          This driver is added to ControllerHandle.
    146   @retval EFI_OUT_OF_RESOURCES Fails to allocate memory for device.
    147   @retval others               Some error occurs.
    148 
    149 **/
    150 EFI_STATUS
    151 EFIAPI
    152 DebugPortStart (
    153   IN EFI_DRIVER_BINDING_PROTOCOL    *This,
    154   IN EFI_HANDLE                     Controller,
    155   IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
    156   );
    157 
    158 /**
    159   Stop this driver on ControllerHandle by removing Serial IO protocol on
    160   the ControllerHandle.
    161 
    162   @param  This              Protocol instance pointer.
    163   @param  ControllerHandle  Handle of device to stop driver on
    164   @param  NumberOfChildren  Number of Handles in ChildHandleBuffer. If number of
    165                             children is zero stop the entire bus driver.
    166   @param  ChildHandleBuffer List of Child Handles to Stop.
    167 
    168   @retval EFI_SUCCESS       This driver is removed ControllerHandle.
    169   @retval other             This driver was not removed from this device.
    170 
    171 **/
    172 EFI_STATUS
    173 EFIAPI
    174 DebugPortStop (
    175   IN  EFI_DRIVER_BINDING_PROTOCOL    *This,
    176   IN  EFI_HANDLE                     Controller,
    177   IN  UINTN                          NumberOfChildren,
    178   IN  EFI_HANDLE                     *ChildHandleBuffer
    179   );
    180 
    181 //
    182 // EFI Component Name Functions
    183 //
    184 /**
    185   Retrieves a Unicode string that is the user readable name of the driver.
    186 
    187   This function retrieves the user readable name of a driver in the form of a
    188   Unicode string. If the driver specified by This has a user readable name in
    189   the language specified by Language, then a pointer to the driver name is
    190   returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
    191   by This does not support the language specified by Language,
    192   then EFI_UNSUPPORTED is returned.
    193 
    194   @param  This[in]              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
    195                                 EFI_COMPONENT_NAME_PROTOCOL instance.
    196 
    197   @param  Language[in]          A pointer to a Null-terminated ASCII string
    198                                 array indicating the language. This is the
    199                                 language of the driver name that the caller is
    200                                 requesting, and it must match one of the
    201                                 languages specified in SupportedLanguages. The
    202                                 number of languages supported by a driver is up
    203                                 to the driver writer. Language is specified
    204                                 in RFC 4646 or ISO 639-2 language code format.
    205 
    206   @param  DriverName[out]       A pointer to the Unicode string to return.
    207                                 This Unicode string is the name of the
    208                                 driver specified by This in the language
    209                                 specified by Language.
    210 
    211   @retval EFI_SUCCESS           The Unicode string for the Driver specified by
    212                                 This and the language specified by Language was
    213                                 returned in DriverName.
    214 
    215   @retval EFI_INVALID_PARAMETER Language is NULL.
    216 
    217   @retval EFI_INVALID_PARAMETER DriverName is NULL.
    218 
    219   @retval EFI_UNSUPPORTED       The driver specified by This does not support
    220                                 the language specified by Language.
    221 
    222 **/
    223 EFI_STATUS
    224 EFIAPI
    225 DebugPortComponentNameGetDriverName (
    226   IN  EFI_COMPONENT_NAME_PROTOCOL  *This,
    227   IN  CHAR8                        *Language,
    228   OUT CHAR16                       **DriverName
    229   );
    230 
    231 
    232 /**
    233   Retrieves a Unicode string that is the user readable name of the controller
    234   that is being managed by a driver.
    235 
    236   This function retrieves the user readable name of the controller specified by
    237   ControllerHandle and ChildHandle in the form of a Unicode string. If the
    238   driver specified by This has a user readable name in the language specified by
    239   Language, then a pointer to the controller name is returned in ControllerName,
    240   and EFI_SUCCESS is returned.  If the driver specified by This is not currently
    241   managing the controller specified by ControllerHandle and ChildHandle,
    242   then EFI_UNSUPPORTED is returned.  If the driver specified by This does not
    243   support the language specified by Language, then EFI_UNSUPPORTED is returned.
    244 
    245   @param  This[in]              A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
    246                                 EFI_COMPONENT_NAME_PROTOCOL instance.
    247 
    248   @param  ControllerHandle[in]  The handle of a controller that the driver
    249                                 specified by This is managing.  This handle
    250                                 specifies the controller whose name is to be
    251                                 returned.
    252 
    253   @param  ChildHandle[in]       The handle of the child controller to retrieve
    254                                 the name of.  This is an optional parameter that
    255                                 may be NULL.  It will be NULL for device
    256                                 drivers.  It will also be NULL for a bus drivers
    257                                 that wish to retrieve the name of the bus
    258                                 controller.  It will not be NULL for a bus
    259                                 driver that wishes to retrieve the name of a
    260                                 child controller.
    261 
    262   @param  Language[in]          A pointer to a Null-terminated ASCII string
    263                                 array indicating the language.  This is the
    264                                 language of the driver name that the caller is
    265                                 requesting, and it must match one of the
    266                                 languages specified in SupportedLanguages. The
    267                                 number of languages supported by a driver is up
    268                                 to the driver writer. Language is specified in
    269                                 RFC 4646 or ISO 639-2 language code format.
    270 
    271   @param  ControllerName[out]   A pointer to the Unicode string to return.
    272                                 This Unicode string is the name of the
    273                                 controller specified by ControllerHandle and
    274                                 ChildHandle in the language specified by
    275                                 Language from the point of view of the driver
    276                                 specified by This.
    277 
    278   @retval EFI_SUCCESS           The Unicode string for the user readable name in
    279                                 the language specified by Language for the
    280                                 driver specified by This was returned in
    281                                 DriverName.
    282 
    283   @retval EFI_INVALID_PARAMETER ControllerHandle is NULL.
    284 
    285   @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
    286                                 EFI_HANDLE.
    287 
    288   @retval EFI_INVALID_PARAMETER Language is NULL.
    289 
    290   @retval EFI_INVALID_PARAMETER ControllerName is NULL.
    291 
    292   @retval EFI_UNSUPPORTED       The driver specified by This is not currently
    293                                 managing the controller specified by
    294                                 ControllerHandle and ChildHandle.
    295 
    296   @retval EFI_UNSUPPORTED       The driver specified by This does not support
    297                                 the language specified by Language.
    298 
    299 **/
    300 EFI_STATUS
    301 EFIAPI
    302 DebugPortComponentNameGetControllerName (
    303   IN  EFI_COMPONENT_NAME_PROTOCOL                     *This,
    304   IN  EFI_HANDLE                                      ControllerHandle,
    305   IN  EFI_HANDLE                                      ChildHandle        OPTIONAL,
    306   IN  CHAR8                                           *Language,
    307   OUT CHAR16                                          **ControllerName
    308   );
    309 
    310 
    311 /**
    312   DebugPort protocol member function.  Calls SerialIo:GetControl to flush buffer.
    313   We cannot call SerialIo:SetAttributes because it uses pool services, which use
    314   locks, which affect TPL, so it's not interrupt context safe or re-entrant.
    315   SerialIo:Reset() calls SetAttributes, so it can't be used either.
    316 
    317   The port itself should be fine since it was set up during initialization.
    318 
    319   @param  This              Protocol instance pointer.
    320 
    321   @return EFI_SUCCESS       Always.
    322 
    323 **/
    324 EFI_STATUS
    325 EFIAPI
    326 DebugPortReset (
    327   IN EFI_DEBUGPORT_PROTOCOL         *This
    328   );
    329 
    330 /**
    331   DebugPort protocol member function.  Calls SerialIo:Read() after setting
    332   if it's different than the last SerialIo access.
    333 
    334   @param  This                Pointer to DebugPort protocol.
    335   @param  Timeout             Timeout value.
    336   @param  BufferSize          On input, the size of Buffer.
    337                               On output, the amount of data actually written.
    338   @param  Buffer              Pointer to buffer to read.
    339 
    340   @retval EFI_SUCCESS
    341   @retval others
    342 
    343 **/
    344 EFI_STATUS
    345 EFIAPI
    346 DebugPortRead (
    347   IN EFI_DEBUGPORT_PROTOCOL         *This,
    348   IN UINT32                         Timeout,
    349   IN OUT UINTN                      *BufferSize,
    350   IN VOID                           *Buffer
    351   );
    352 
    353 /**
    354   DebugPort protocol member function.  Calls SerialIo:Write() Writes 8 bytes at
    355   a time and does a GetControl between 8 byte writes to help insure reads are
    356   interspersed This is poor-man's flow control.
    357 
    358   @param  This                Pointer to DebugPort protocol.
    359   @param  Timeout             Timeout value.
    360   @param  BufferSize          On input, the size of Buffer.
    361                               On output, the amount of data actually written.
    362   @param  Buffer              Pointer to buffer to read.
    363 
    364   @retval EFI_SUCCESS         The data was written.
    365   @retval others              Fails when writting datas to debug port device.
    366 
    367 **/
    368 EFI_STATUS
    369 EFIAPI
    370 DebugPortWrite (
    371   IN EFI_DEBUGPORT_PROTOCOL         *This,
    372   IN UINT32                         Timeout,
    373   IN OUT UINTN                      *BufferSize,
    374   OUT VOID                          *Buffer
    375   );
    376 
    377 /**
    378   DebugPort protocol member function.  Calls SerialIo:Write() after setting
    379   if it's different than the last SerialIo access.
    380 
    381   @param  This                Pointer to DebugPort protocol.
    382 
    383   @retval EFI_SUCCESS         At least 1 character is ready to be read from
    384                               the DebugPort interface.
    385   @retval EFI_NOT_READY       There are no characters ready to read from the
    386                               DebugPort interface
    387   @retval EFI_DEVICE_ERROR    A hardware failure occured... (from SerialIo)
    388 
    389 **/
    390 EFI_STATUS
    391 EFIAPI
    392 DebugPortPoll (
    393   IN EFI_DEBUGPORT_PROTOCOL         *This
    394   );
    395 
    396 #endif
    397