ehci.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 2,436 行 · 第 1/5 页

C
2,436
字号
  IN  OUT UINTN                           *DataLength,
  IN  OUT UINT8                           *DataToggle,
  IN  UINTN                               TimeOut,
  IN  EFI_USB2_HC_TRANSACTION_TRANSLATOR  *Translator,
  OUT UINT32                              *TransferResult
  )
/*++

  Routine Description:

    Submits bulk transfer to a bulk endpoint of a USB device.

  Arguments:

    This              - A pointer to the EFI_USB2_HC_PROTOCOL instance.
    DeviceAddress     - Represents the address of the target device on the USB,
                        which is assigned during USB enumeration.
    EndPointAddress   - The combination of an endpoint number and an
                        endpoint direction of the target USB device.
                        Each endpoint address supports data transfer in
                        one direction except the control endpoint
                        (whose default endpoint address is 0).
                        It is the caller's responsibility to make sure that
                        the EndPointAddress represents a bulk endpoint.
    DeviceSpeed       - Indicates device speed. The supported values are EFI_USB_SPEED_FULL
                        and EFI_USB_SPEED_HIGH.
    MaximumPacketLength - Indicates the maximum packet size the target endpoint
                          is capable of sending or receiving.
    DataBuffersNumber - Number of data buffers prepared for the transfer.
    Data              - Array of pointers to the buffers of data that will be transmitted
                        to USB device or received from USB device.
    DataLength        - When input, indicates the size, in bytes, of the data buffer
                        specified by Data. When output, indicates the actually
                        transferred data size.
    DataToggle        - A pointer to the data toggle value. On input, it indicates
                        the initial data toggle value the bulk transfer should adopt;
                        on output, it is updated to indicate the data toggle value
                        of the subsequent bulk transfer.
    Translator        - A pointr to the transaction translator data.
    TimeOut           - Indicates the maximum time, in microseconds, which the
                        transfer is allowed to complete.
    TransferResult    - A pointer to the detailed result information of the
                        bulk transfer.

  Returns:

    EFI_SUCCESS
        The bulk transfer was completed successfully.
    EFI_OUT_OF_RESOURCES
        The bulk transfer could not be submitted due to lack of resource.
    EFI_INVALID_PARAMETER
        Some parameters are invalid.
    EFI_TIMEOUT
        The bulk transfer failed due to timeout.
    EFI_DEVICE_ERROR
        The bulk transfer failed due to host controller or device error.
        Caller should check TranferResult for detailed error information.

--*/
{
  EFI_STATUS              Status;
  USB2_HC_DEV             *HcDev;
  UINT8                   PktId;
  EHCI_QH_ENTITY          *QhPtr;
  EHCI_QTD_ENTITY         *BulkQtdsPtr;
  UINT8                   *DataCursor;
  VOID                    *DataMap;
  EFI_USB_DATA_DIRECTION  TransferDirection;

  QhPtr       = NULL;
  BulkQtdsPtr = NULL;
  DataCursor  = NULL;
  DataMap     = NULL;
  HcDev       = USB2_HC_DEV_FROM_THIS (This);



  //
  // Parameters Checking
  //
  if (NULL == DataLength ||
    NULL == Data ||
    NULL == Data[0] ||
    NULL == TransferResult
    ) {
    Status = EFI_INVALID_PARAMETER;
    goto exit;
  }

  if (*DataLength == 0) {
    Status = EFI_INVALID_PARAMETER;
    goto exit;
  }

  if (1 != *DataToggle && 0 != *DataToggle) {
    Status = EFI_INVALID_PARAMETER;
    goto exit;
  }

  if (EFI_USB_SPEED_LOW == DeviceSpeed) {
    Status = EFI_INVALID_PARAMETER;
    goto exit;
  }

  if (EFI_USB_SPEED_FULL == DeviceSpeed) {
    if (MaximumPacketLength > 64) {
      Status = EFI_INVALID_PARAMETER;
      goto exit;
    }
  }

  if (EFI_USB_SPEED_HIGH == DeviceSpeed) {
    if (MaximumPacketLength > 512) {
      Status = EFI_INVALID_PARAMETER;
      goto exit;
    }
  }

  //
  // if has errors that cause host controller halt,
  // then return EFI_DEVICE_ERROR directly.
  //
  if (IsEhcHalted (HcDev) || IsEhcSysError (HcDev)) {
    ClearEhcAllStatus (HcDev);
    *TransferResult = EFI_USB_ERR_SYSTEM;
    Status          = EFI_DEVICE_ERROR;
    goto exit;
  }

  Status = ClearEhcAllStatus (HcDev);
  if (EFI_ERROR (Status)) {
    Status = EFI_DEVICE_ERROR;
    goto exit;
  }

  //
  // construct QH and TD data structures,
  // and link them together
  //
  if (EndPointAddress & 0x80) {
    TransferDirection = EfiUsbDataIn;
  } else {
    TransferDirection = EfiUsbDataOut;
  }

  Status = MapDataBuffer (
             HcDev,
             TransferDirection,
             Data[0],
             DataLength,
             &PktId,
             &DataCursor,
             &DataMap
             );
  if (EFI_ERROR (Status)) {
    *TransferResult = EFI_USB_ERR_SYSTEM;
    Status          = EFI_DEVICE_ERROR;
    goto exit;
  }

  //
  // Create and init Bulk Qh
  //
  Status = CreateBulkQh (
             HcDev,
             DeviceAddress,
             EndPointAddress,
             DeviceSpeed,
             *DataToggle,
             MaximumPacketLength,
             Translator,
             &QhPtr
             );
  if (EFI_ERROR (Status)) {
    *TransferResult = EFI_USB_ERR_SYSTEM;
    Status          = EFI_OUT_OF_RESOURCES;
    goto unmap_data;
  }

  //
  // Create and init Bulk Qtds
  //
  Status = CreateBulkOrInterruptQtds (
             HcDev,
             PktId,
             DataCursor,
             *DataLength,
             Translator,
             &BulkQtdsPtr
             );
  if (EFI_ERROR (Status)) {
    *TransferResult = EFI_USB_ERR_SYSTEM;
    Status          = EFI_OUT_OF_RESOURCES;
    goto destory_qh;
  }

  //
  // Link Qtds to Qh
  //
  LinkQtdToQh (QhPtr, BulkQtdsPtr);

  ClearEhcAllStatus (HcDev);

  //
  // Link Qh and qtds to Async Schedule List
  //
  Status = LinkQhToAsyncList (HcDev, QhPtr);
  if (EFI_ERROR (Status)) {
    *TransferResult = EFI_USB_ERR_SYSTEM;
    Status          = EFI_DEVICE_ERROR;
    goto destory_qtds;
  }

  //
  // Poll QH-TDs execution and get result.
  // detail status is returned
  //
  Status = ExecuteTransfer (
             HcDev,
             FALSE,
             QhPtr,
             DataLength,
             DataToggle,
             TimeOut,
             TransferResult
             );
  if (EFI_ERROR (Status)) {
    goto destory_qtds;
  }


  //
  // if has errors that cause host controller halt,
  // then return EFI_DEVICE_ERROR directly.
  //
  if (IsEhcHalted (HcDev) || IsEhcSysError (HcDev)) {
    *TransferResult |= EFI_USB_ERR_SYSTEM;
  }

  ClearEhcAllStatus (HcDev);

destory_qtds:
  UnlinkQhFromAsyncList (HcDev, QhPtr);
  DestoryQtds (HcDev, BulkQtdsPtr);
destory_qh:
  DestoryQh (HcDev, QhPtr);
unmap_data:
  HcDev->PciIo->Unmap (HcDev->PciIo, DataMap);
exit:
  HcDev->PciIo->Flush (HcDev->PciIo);
  return Status;
}

EFI_STATUS
EFIAPI
EhciAsyncInterruptTransfer (
  IN  EFI_USB2_HC_PROTOCOL                  * This,
  IN  UINT8                                 DeviceAddress,
  IN  UINT8                                 EndPointAddress,
  IN  UINT8                                 DeviceSpeed,
  IN  UINTN                                 MaximumPacketLength,
  IN  BOOLEAN                               IsNewTransfer,
  IN  OUT UINT8                             *DataToggle,
  IN  UINTN                                 PollingInterval,
  IN  UINTN                                 DataLength,
  IN  EFI_USB2_HC_TRANSACTION_TRANSLATOR    * Translator,
  IN  EFI_ASYNC_USB_TRANSFER_CALLBACK       CallBackFunction,
  IN  VOID                                  *Context OPTIONAL
  )
/*++

  Routine Description:

    Submits an asynchronous interrupt transfer to an
    interrupt endpoint of a USB device.

  Arguments:

    This            - A pointer to the EFI_USB2_HC_PROTOCOL instance.
    DeviceAddress   - Represents the address of the target device on the USB,
                      which is assigned during USB enumeration.
    EndPointAddress - The combination of an endpoint number and an endpoint
                      direction of the target USB device. Each endpoint address
                      supports data transfer in one direction except the
                      control endpoint (whose default endpoint address is 0).
                      It is the caller's responsibility to make sure that
                      the EndPointAddress represents an interrupt endpoint.
    DeviceSpeed     - Indicates device speed.
    MaximumPacketLength  - Indicates the maximum packet size the target endpoint
                           is capable of sending or receiving.
    IsNewTransfer   - If TRUE, an asynchronous interrupt pipe is built between
                      the host and the target interrupt endpoint.
                      If FALSE, the specified asynchronous interrupt pipe
                      is canceled.
    DataToggle      - A pointer to the data toggle value.  On input, it is valid
                      when IsNewTransfer is TRUE, and it indicates the initial
                      data toggle value the asynchronous interrupt transfer
                      should adopt.
                      On output, it is valid when IsNewTransfer is FALSE,
                      and it is updated to indicate the data toggle value of
                      the subsequent asynchronous interrupt transfer.
    PollingInterval - Indicates the interval, in milliseconds, that the
                      asynchronous interrupt transfer is polled.
                      This parameter is required when IsNewTransfer is TRUE.
    DataLength      - Indicates the length of data to be received at the
                      rate specified by PollingInterval from the target
                      asynchronous interrupt endpoint.  This parameter
                      is only required when IsNewTransfer is TRUE.
    Translator      - A pointr to the transaction translator data.
    CallBackFunction  - The Callback function.This function is called at the
                        rate specified by PollingInterval.This parameter is
                        only required when IsNewTransfer is TRUE.
    Context         - The context that is passed to the CallBackFunction.
                    - This is an optional parameter and may be NULL.

  Returns:

    EFI_SUCCESS
        The asynchronous interrupt transfer request has been successfully
        submitted or canceled.
    EFI_INVALID_PARAMETER
        Some parameters are invalid.
    EFI_OUT_OF_RESOURCES
        The request could not be completed due to a lack of resources.
    EFI_DEVICE_ERROR
        Can't read register

--*/
{
  EFI_STATUS          Status;
  USB2_HC_DEV         *HcDev;
  UINT8               PktId;
  EHCI_QH_ENTITY      *QhPtr;
  EHCI_QTD_ENTITY     *InterruptQtdsPtr;
  UINT8               *DataPtr;
  UINT8               *DataCursor;
  VOID                *DataMap;
  UINTN               MappedLength;
  EHCI_ASYNC_REQUEST  *AsyncRequestPtr;
  EFI_TPL             OldTpl;

  QhPtr             = NULL;
  InterruptQtdsPtr  = NULL;
  DataPtr           = NULL;
  DataCursor        = NULL;
  DataMap           = NULL;
  AsyncRequestPtr   = NULL;
  HcDev             = USB2_HC_DEV_FROM_THIS (This);

  //
  // Parameters Checking
  //
  if (!IsDataInTransfer (EndPointAddress)) {
    Status = EFI_INVALID_PARAMETER;
    goto exit;
  }

  if (IsNewTransfer) {
    if (0 == DataLength) {
      Status = EFI_INVALID_PARAMETER;
      goto exit;
    }

    if (*DataToggle != 1 && *DataToggle != 0) {
      Status = EFI_INVALID_PARAMETER;
      goto exit;
    }

    if (PollingInterval > 255 || PollingInterval < 1) {
      Status = EFI_INVALID_PARAMETER;
      goto exit;
    }
  }

  //
  // if has errors that cause host controller halt,
  // then return EFI_DEVICE_ERROR directly.
  //
  if (IsEhcHalted (HcDev) || IsEhcSysError (HcDev)) {
    ClearEhcAllStatus (HcDev);
    Status = EFI_DEVICE_ERROR;
    goto exit;
  }

  Status = ClearEhcAllStatus (HcDev);
  if (EFI_ERROR (Status)) {
    Status = EFI_DEVICE_ERROR;
    goto exit;
  }

  //
  // Delete Async interrupt transfer request
  //
  if (!IsNewTransfer) {

    OldTpl = gBS->RaiseTPL (EFI_TPL_NOTIFY);

    Status = DeleteAsyncRequestTransfer (
               HcDev,
               DeviceAddress,
               EndPointAddress,
               DataToggle
               );

    gBS->RestoreTPL (OldTpl);

    goto exit;
  }

  Status = EhciAllocatePool (
             HcDev,
             (UINT8 **) &AsyncRequestPtr,
             sizeof (EHCI_ASYNC_REQUEST)
             );
  if (EFI_ERROR (Status)) {
    Status = EFI_OUT_OF_RESOURCES;
    goto exit;
  }

  Status = EhciAllocatePool (HcDev, &DataPtr, DataLength);
  if (EFI_ERROR (Status)) {
    Status = EFI_OUT_OF_RESOURCES;
    goto free_request;
  }

  MappedLength = DataLength;
  Status = MapDataBuffer (
             HcDev,
             EfiUsbDataIn,
             DataPtr,
             &MappedLength,
             &PktId,
             &DataCursor,
             &DataMap
             );
  if (EFI_ERROR (Status)) {
    Status = EFI_DEVICE_ERROR;
    goto free_data;
  }

  //
  // Create and init Interrupt Qh
  //
  Status = CreateInterruptQh (
             HcDev,
             DeviceAddress,
             EndPointAddress,
             DeviceSpeed,
             *DataToggle,
             MaximumPacketLength,
             PollingInterval,
             Translator,
             &QhPtr
             );
  if (EFI_ERROR (Status)) {
    Status = EFI_OUT_OF_RESOURCES;
    goto unmap_data;
  }

  //
  // Create and init Interrupt Qtds
  //
  Status = CreateBulkOrInterruptQtds (
             HcDev,
             PktId,
             DataCursor,
             MappedLength,
             Translator,
             &InterruptQtdsPtr
             );
  if (EFI_ERROR (Status)) {
    Status = EFI_OUT_OF_RESOURCES;
    goto destory_qh;
  }

  //
  // Link Qtds to Qh
  //
  LinkQtdToQh (QhPtr, InterruptQtdsPtr);

  //
  // Init AsyncRequest Entry
  //
  AsyncRequestPtr->Context      = Conte

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?