HdaCodecInfo.c 12.9 KB
Newer Older
V
vit9696 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * File: HdaCodecInfo.c
 *
 * Copyright (c) 2018 John Davis
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "HdaCodec.h"

27 28 29 30 31 32 33 34 35
EFI_STATUS
EFIAPI
HdaCodecInfoGetAddress (
  IN  EFI_HDA_CODEC_INFO_PROTOCOL  *This,
  OUT UINT8                        *Address
  )
{
  HDA_CODEC_INFO_PRIVATE_DATA *HdaPrivateData;

G
Goldfish64 已提交
36 37 38
  if (This == NULL || Address == NULL) {
    return EFI_INVALID_PARAMETER;
  }
39 40 41 42 43 44

  HdaPrivateData = HDA_CODEC_INFO_PRIVATE_DATA_FROM_THIS (This);

  return HdaPrivateData->HdaCodecDev->HdaIo->GetAddress (HdaPrivateData->HdaCodecDev->HdaIo, Address);
}

V
vit9696 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
/**
  Gets the codec's name.

  @param[in]  This              A pointer to the EFI_HDA_CODEC_INFO_PROTOCOL instance.
  @param[out] CodecName         A pointer to the buffer to return the codec name.

  @retval EFI_SUCCESS           The codec name was retrieved.
  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
**/
EFI_STATUS
EFIAPI
HdaCodecInfoGetCodecName(
  IN  EFI_HDA_CODEC_INFO_PROTOCOL *This,
  OUT CONST CHAR16 **CodecName) {
  //DEBUG((DEBUG_INFO, "HdaCodecInfoGetCodecName(): start\n"));

  // Create variables.
  HDA_CODEC_INFO_PRIVATE_DATA *HdaPrivateData;

  // If parameters are null, fail.
  if ((This == NULL) || (CodecName == NULL))
    return EFI_INVALID_PARAMETER;

  // Get private data and fill codec name parameter.
  HdaPrivateData = HDA_CODEC_INFO_PRIVATE_DATA_FROM_THIS(This);
  *CodecName = HdaPrivateData->HdaCodecDev->Name;
  return EFI_SUCCESS;
}

/**
  Gets the codec's vendor and device ID.

  @param[in]  This              A pointer to the EFI_HDA_CODEC_INFO_PROTOCOL instance.
  @param[out] VendorId          The vendor and device ID of the codec.

  @retval EFI_SUCCESS           The vendor and device ID was retrieved.
  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
**/
EFI_STATUS
EFIAPI
HdaCodecInfoGetVendorId(
  IN  EFI_HDA_CODEC_INFO_PROTOCOL *This,
  OUT UINT32 *VendorId) {
  //DEBUG((DEBUG_INFO, "HdaCodecInfoGetVendorId(): start\n"));

  // Create variables.
  HDA_CODEC_INFO_PRIVATE_DATA *HdaPrivateData;

  // If parameters are null, fail.
  if ((This == NULL) || (VendorId == NULL))
    return EFI_INVALID_PARAMETER;

  // Get private data and fill vendor ID parameter.
  HdaPrivateData = HDA_CODEC_INFO_PRIVATE_DATA_FROM_THIS(This);
  *VendorId = HdaPrivateData->HdaCodecDev->VendorId;
  return EFI_SUCCESS;
}

/**
  Gets the codec's revision ID.

  @param[in]  This              A pointer to the EFI_HDA_CODEC_INFO_PROTOCOL instance.
  @param[out] RevisionId        The revision ID of the codec.

  @retval EFI_SUCCESS           The revision ID was retrieved.
  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
**/
EFI_STATUS
EFIAPI
HdaCodecInfoGetRevisionId(
  IN  EFI_HDA_CODEC_INFO_PROTOCOL *This,
  OUT UINT32 *RevisionId) {
  //DEBUG((DEBUG_INFO, "HdaCodecInfoGetRevisionId(): start\n"));

  // Create variables.
  HDA_CODEC_INFO_PRIVATE_DATA *HdaPrivateData;

  // If parameters are null, fail.
  if ((This == NULL) || (RevisionId == NULL))
    return EFI_INVALID_PARAMETER;

  // Get private data and fill revision ID parameter.
  HdaPrivateData = HDA_CODEC_INFO_PRIVATE_DATA_FROM_THIS(This);
  *RevisionId = HdaPrivateData->HdaCodecDev->RevisionId;
  return EFI_SUCCESS;
}

/**
  Gets the node ID of the codec's audio function.

  @param[in]  This              A pointer to the EFI_HDA_CODEC_INFO_PROTOCOL instance.
  @param[out] AudioFuncId       The node ID of the codec's audio function.

  @retval EFI_SUCCESS           The node ID was retrieved.
  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
**/
EFI_STATUS
EFIAPI
HdaCodecInfoGetAudioFuncId(
  IN  EFI_HDA_CODEC_INFO_PROTOCOL *This,
  OUT UINT8 *AudioFuncId,
  OUT BOOLEAN *UnsolCapable) {
  //DEBUG((DEBUG_INFO, "HdaCodecInfoGetAudioFuncId(): start\n"));

  // Create variables.
  HDA_CODEC_INFO_PRIVATE_DATA *HdaPrivateData;

  // If parameters are null, fail.
  if ((This == NULL) || (AudioFuncId == NULL) || (UnsolCapable == NULL))
    return EFI_INVALID_PARAMETER;

  // Get private data and fill node ID parameter.
  HdaPrivateData = HDA_CODEC_INFO_PRIVATE_DATA_FROM_THIS(This);
  *AudioFuncId = HdaPrivateData->HdaCodecDev->AudioFuncGroup->NodeId;
  *UnsolCapable = HdaPrivateData->HdaCodecDev->AudioFuncGroup->UnsolCapable;
  return EFI_SUCCESS;
}

/**
  Gets the codec's default supported stream rates and formats.

  @param[in]  This              A pointer to the EFI_HDA_CODEC_INFO_PROTOCOL instance.
  @param[out] Rates             The default supported rates.
  @param[out] Formats           The default supported formats.

  @retval EFI_SUCCESS           The stream rates and formats were retrieved.
  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
**/
EFI_STATUS
EFIAPI
HdaCodecInfoGetDefaultRatesFormats(
  IN  EFI_HDA_CODEC_INFO_PROTOCOL *This,
  OUT UINT32 *Rates,
  OUT UINT32 *Formats) {
  //DEBUG((DEBUG_INFO, "HdaCodecInfoGetDefaultRatesFormats(): start\n"));

  // Create variables.
  HDA_CODEC_INFO_PRIVATE_DATA *HdaPrivateData;

  // If parameters are null, fail.
  if ((This == NULL) || (Rates == NULL) || (Formats == NULL))
    return EFI_INVALID_PARAMETER;

  // Get private data and fill rates and formats parameters.
  HdaPrivateData = HDA_CODEC_INFO_PRIVATE_DATA_FROM_THIS(This);
  *Rates = HdaPrivateData->HdaCodecDev->AudioFuncGroup->SupportedPcmRates;
  *Formats = HdaPrivateData->HdaCodecDev->AudioFuncGroup->SupportedFormats;
  return EFI_SUCCESS;
}

/**
  Gets the codec's default amp capabilities.

  @param[in]  This              A pointer to the EFI_HDA_CODEC_INFO_PROTOCOL instance.
  @param[out] AmpInCaps         The default input amp capabilities.
  @param[out] AmpOutCaps        The default output amp capabilities.

  @retval EFI_SUCCESS           The default amp capabilities were retrieved.
  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
**/
EFI_STATUS
EFIAPI
HdaCodecInfoGetDefaultAmpCaps(
  IN  EFI_HDA_CODEC_INFO_PROTOCOL *This,
  OUT UINT32 *AmpInCaps,
  OUT UINT32 *AmpOutCaps) {
  //DEBUG((DEBUG_INFO, "HdaCodecInfoGetDefaultAmpCaps(): start\n"));

  // Create variables.
  HDA_CODEC_INFO_PRIVATE_DATA *HdaPrivateData;

  // If parameters are null, fail.
  if ((This == NULL) || (AmpInCaps == NULL) || (AmpOutCaps == NULL))
    return EFI_INVALID_PARAMETER;

  // Get private data and fill amp caps parameters.
  HdaPrivateData = HDA_CODEC_INFO_PRIVATE_DATA_FROM_THIS(This);
  *AmpInCaps = HdaPrivateData->HdaCodecDev->AudioFuncGroup->AmpInCapabilities;
  *AmpOutCaps = HdaPrivateData->HdaCodecDev->AudioFuncGroup->AmpOutCapabilities;
  return EFI_SUCCESS;
}

/**
  Gets the codec's widgets.

  @param[in]  This              A pointer to the EFI_HDA_CODEC_INFO_PROTOCOL instance.
  @param[out] Widgets           A pointer to the buffer to return the requested array of widgets.
  @param[out] WidgetCount       The number of widgets returned in Widgets.

  @retval EFI_SUCCESS           The widgets were retrieved.
  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  @retval EFI_OUT_OF_RESOURCES  A buffer couldn't be allocated.
**/
EFI_STATUS
EFIAPI
HdaCodecInfoGetWidgets(
  IN  EFI_HDA_CODEC_INFO_PROTOCOL *This,
  OUT HDA_WIDGET **Widgets,
  OUT UINTN *WidgetCount) {
  //DEBUG((DEBUG_INFO, "HdaCodecInfoGetWidgets(): start\n"));

  // Create variables.
  HDA_CODEC_INFO_PRIVATE_DATA *HdaPrivateData;
  HDA_WIDGET_DEV *HdaWidgetDev;
  UINT8 AmpInCount;
  HDA_WIDGET *HdaWidgets;
  UINTN HdaWidgetsCount;

  // If parameters are null, fail.
  if ((This == NULL) || (Widgets == NULL) || (WidgetCount == NULL))
    return EFI_INVALID_PARAMETER;

  // Get private data and allocate widgets array.
  HdaPrivateData = HDA_CODEC_INFO_PRIVATE_DATA_FROM_THIS(This);
  HdaWidgetsCount = HdaPrivateData->HdaCodecDev->AudioFuncGroup->WidgetsCount;
  HdaWidgets = AllocateZeroPool(sizeof(HDA_WIDGET) * HdaWidgetsCount);
  if (HdaWidgets == NULL)
    return EFI_OUT_OF_RESOURCES;

  // Populate widgets array.
  for (UINTN w = 0; w < HdaWidgetsCount; w++) {
    // Get widget.
    HdaWidgetDev = HdaPrivateData->HdaCodecDev->AudioFuncGroup->Widgets + w;

    // Get basic data.
    HdaWidgets[w].NodeId = HdaWidgetDev->NodeId;
    HdaWidgets[w].Capabilities = HdaWidgetDev->Capabilities;
    HdaWidgets[w].DefaultUnSol = HdaWidgetDev->DefaultUnSol;
    HdaWidgets[w].DefaultEapd = HdaWidgetDev->DefaultEapd;

    // Get connections.
    HdaWidgets[w].ConnectionListLength = HdaWidgetDev->ConnectionListLength;
    HdaWidgets[w].Connections = AllocateZeroPool(sizeof(UINT16) * HdaWidgetDev->ConnectionCount);
    if (HdaWidgets[w].Connections == NULL)
      goto FREE_WIDGETS;
    CopyMem(HdaWidgets[w].Connections, HdaWidgetDev->Connections, sizeof(UINT16) * HdaWidgetDev->ConnectionCount);

    // Get power info.
    HdaWidgets[w].SupportedPowerStates = HdaWidgetDev->SupportedPowerStates;
    HdaWidgets[w].DefaultPowerState = HdaWidgetDev->DefaultPowerState;

    // Get input amps.
    HdaWidgets[w].AmpInCapabilities = HdaWidgetDev->AmpInCapabilities;
    if (HdaWidgetDev->Capabilities & HDA_PARAMETER_WIDGET_CAPS_IN_AMP) {
      // Determine number of input amps.
      AmpInCount = HdaWidgetDev->ConnectionCount;
      if (AmpInCount < 1)
        AmpInCount = 1;

      // Allocate arrays.
      HdaWidgets[w].AmpInLeftDefaultGainMute = AllocateZeroPool(sizeof(UINT8) * AmpInCount);
      HdaWidgets[w].AmpInRightDefaultGainMute = AllocateZeroPool(sizeof(UINT8) * AmpInCount);
      if ((HdaWidgets[w].AmpInLeftDefaultGainMute == NULL) ||
        (HdaWidgets[w].AmpInRightDefaultGainMute == NULL))
        goto FREE_WIDGETS;

      // Copy arrays.
      CopyMem(HdaWidgets[w].AmpInLeftDefaultGainMute, HdaWidgetDev->AmpInLeftDefaultGainMute,
        sizeof(UINT8) * AmpInCount);
      CopyMem(HdaWidgets[w].AmpInRightDefaultGainMute, HdaWidgetDev->AmpInRightDefaultGainMute,
        sizeof(UINT8) * AmpInCount);
    }

    // Get output amp.
    HdaWidgets[w].AmpOutCapabilities = HdaWidgetDev->AmpOutCapabilities;
    HdaWidgets[w].AmpOutLeftDefaultGainMute = HdaWidgetDev->AmpOutLeftDefaultGainMute;
    HdaWidgets[w].AmpOutRightDefaultGainMute = HdaWidgetDev->AmpOutRightDefaultGainMute;

    // Get input/output data.
    HdaWidgets[w].SupportedPcmRates = HdaWidgetDev->SupportedPcmRates;
    HdaWidgets[w].SupportedFormats = HdaWidgetDev->SupportedFormats;
    HdaWidgets[w].DefaultConvFormat = HdaWidgetDev->DefaultConvFormat;
    HdaWidgets[w].DefaultConvStreamChannel = HdaWidgetDev->DefaultConvStreamChannel;
    HdaWidgets[w].DefaultConvChannelCount = HdaWidgetDev->DefaultConvChannelCount;

    // Get pin complex data.
    HdaWidgets[w].PinCapabilities = HdaWidgetDev->PinCapabilities;
    HdaWidgets[w].DefaultPinControl = HdaWidgetDev->DefaultPinControl;
    HdaWidgets[w].DefaultConfiguration = HdaWidgetDev->DefaultConfiguration;

    // Get volume knob data.
    HdaWidgets[w].VolumeCapabilities = HdaWidgetDev->VolumeCapabilities;
    HdaWidgets[w].DefaultVolume = HdaWidgetDev->DefaultVolume;
  }

  // Fill parameters.
  *WidgetCount = HdaWidgetsCount;
  *Widgets = HdaWidgets;
  return EFI_SUCCESS;

FREE_WIDGETS:
  HdaCodecInfoFreeWidgetsBuffer(HdaWidgets, HdaWidgetsCount);
  return EFI_OUT_OF_RESOURCES;
}

/**
  Frees an array of HDA_WIDGET.

  @param[in] Widgets            A pointer to the buffer array of widgets that is to be freed.
  @param[in] WidgetCount        The number of widgets in Widgets.

  @retval EFI_SUCCESS           The buffer was freed.
  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
**/
EFI_STATUS
EFIAPI
HdaCodecInfoFreeWidgetsBuffer(
  IN  HDA_WIDGET *Widgets,
  IN  UINTN WidgetCount) {
  //DEBUG((DEBUG_INFO, "HdaCodecInfoFreeWidgetsBuffer(): start\n"));

  // If parameter is null, fail.
  if (Widgets == NULL)
    return EFI_INVALID_PARAMETER;

  // Free pool buffers.
  for (UINTN w = 0; w < WidgetCount; w++) {
    if (Widgets[w].Connections != NULL)
      FreePool(Widgets[w].Connections);
    if (Widgets[w].AmpInLeftDefaultGainMute != NULL)
      FreePool(Widgets[w].AmpInLeftDefaultGainMute);
    if (Widgets[w].AmpInRightDefaultGainMute != NULL)
      FreePool(Widgets[w].AmpInRightDefaultGainMute);
  }
  FreePool(Widgets);
  return EFI_SUCCESS;
}