⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usblib.c

📁 Next BIOS Source code : Extensible Firmware Interface
💻 C
📖 第 1 页 / 共 2 页
字号:
/*++

Copyright (c)  1999 - 2002 Intel Corporation. All rights reserved
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.


 Module Name:

    usblib.c

 Abstract:

   Common Libarary  for USB

 Revision History

--*/

#include "Efi.h"
#include "EfiDriverLib.h"

#include EFI_PROTOCOL_DEFINITION(UsbIo)
#include "usbbus.h"
#include "usblib.h"

//
// Get Device Descriptor
//
EFI_STATUS
UsbGetDescriptor (
  IN  EFI_USB_IO_PROTOCOL     *UsbIo,
  IN  UINT16                  Value,
  IN  UINT16                  Index,
  IN  UINT16                  DescriptorLength,
  IN  VOID                    *Descriptor,
  OUT UINT32                  *Status
  )
{
  EFI_USB_DEVICE_REQUEST  DevReq;
  UINT32                  Timeout;

  if(UsbIo == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  EfiZeroMem ( &DevReq, sizeof(EFI_USB_DEVICE_REQUEST)) ;

  DevReq.RequestType = USB_DEV_GET_DESCRIPTOR_REQ_TYPE ;
  DevReq.Request = USB_DEV_GET_DESCRIPTOR ;
  DevReq.Value = Value ;
  DevReq.Index = Index ;
  DevReq.Length = DescriptorLength ;

  Timeout = 3000;
  return UsbIo->UsbControlTransfer(
                      UsbIo,
                      &DevReq,
                      EfiUsbDataIn,
                      Timeout,
                      Descriptor,
                      DescriptorLength,
                      Status
                 );
}

//
// Set Device Descriptor
//
EFI_STATUS
UsbSetDescriptor (
  IN  EFI_USB_IO_PROTOCOL    *UsbIo,
  IN  UINT16                  Value,
  IN  UINT16                  Index,
  IN  UINT16                  DescriptorLength,
  IN  VOID                    *Descriptor,
  OUT UINT32                  *Status
  )
{
  EFI_USB_DEVICE_REQUEST  DevReq;
  UINT32                  Timeout;

  if(UsbIo == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  EfiZeroMem ( &DevReq, sizeof(EFI_USB_DEVICE_REQUEST)) ;

  DevReq.RequestType = USB_DEV_SET_DESCRIPTOR_REQ_TYPE ;
  DevReq.Request = USB_DEV_SET_DESCRIPTOR ;
  DevReq.Value = Value ;
  DevReq.Index = Index ;
  DevReq.Length = DescriptorLength ;

  Timeout = 3000;
  return UsbIo->UsbControlTransfer(
                  UsbIo,
                  &DevReq,
                  EfiUsbDataOut,
                  Timeout,
                  Descriptor,
                  DescriptorLength,
                  Status
                );
}

//
// Set Address
//
EFI_STATUS
UsbSetDeviceAddress (
  IN  EFI_USB_IO_PROTOCOL    *UsbIo,
  IN  UINT16                  AddressValue,
  OUT UINT32                 *Status
  )
{
  EFI_USB_DEVICE_REQUEST  DevReq;
  UINT32                  Timeout;

  if(UsbIo == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  EfiZeroMem ( &DevReq, sizeof(EFI_USB_DEVICE_REQUEST)) ;

  DevReq.RequestType = USB_DEV_SET_ADDRESS_REQ_TYPE ;
  DevReq.Request = USB_DEV_SET_ADDRESS ;
  DevReq.Value = AddressValue ;
  DevReq.Index = 0 ;
  DevReq.Length = 0 ;

  Timeout = 3000;
  return UsbIo->UsbControlTransfer(
                  UsbIo,
                  &DevReq,
                  EfiUsbNoData,
                  Timeout,
                  NULL,
                  0,
                  Status
               );
}

//
// Get device Interface
//
EFI_STATUS
UsbGetDeviceInterface (
  IN  EFI_USB_IO_PROTOCOL    *UsbIo,
  IN  UINT16                  Index,
  OUT UINT8                   *AltSetting,
  OUT UINT32                  *Status
  )
{
  EFI_USB_DEVICE_REQUEST  DevReq;
  UINT32                  Timeout;

  if(UsbIo == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  EfiZeroMem ( &DevReq, sizeof(EFI_USB_DEVICE_REQUEST)) ;

  DevReq.RequestType = USB_DEV_GET_INTERFACE_REQ_TYPE ;
  DevReq.Request = USB_DEV_GET_INTERFACE ;
  DevReq.Value = 0 ;
  DevReq.Index = Index ;
  DevReq.Length = 1 ;

  Timeout = 3000;
  return UsbIo->UsbControlTransfer(
                  UsbIo,
                  &DevReq,
                  EfiUsbDataIn,
                  Timeout,
                  AltSetting,
                  1,
                  Status
                );
}

//
// Set device interface
//
EFI_STATUS
UsbSetDeviceInterface (
  IN  EFI_USB_IO_PROTOCOL    *UsbIo,
  IN  UINT16                  InterfaceNo,
  IN  UINT16                  AltSetting,
  OUT UINT32                  *Status
  )
{
  EFI_USB_DEVICE_REQUEST  DevReq;
  UINT32                  Timeout;

  if(UsbIo == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  EfiZeroMem ( &DevReq, sizeof(EFI_USB_DEVICE_REQUEST)) ;

  DevReq.RequestType = USB_DEV_SET_INTERFACE_REQ_TYPE ;
  DevReq.Request = USB_DEV_SET_INTERFACE ;
  DevReq.Value = AltSetting ;
  DevReq.Index = InterfaceNo ;
  DevReq.Length = 0 ;

  Timeout = 3000;
  return UsbIo->UsbControlTransfer(
                  UsbIo,
                  &DevReq,
                  EfiUsbNoData,
                  Timeout,
                  NULL,
                  0,
                  Status
                );
}

//
// Get device configuration
//
EFI_STATUS
UsbGetDeviceConfiguration (
  IN  EFI_USB_IO_PROTOCOL    *UsbIo,
  OUT UINT8                  *ConfigValue,
  OUT UINT32                 *Status
  )
{
  EFI_USB_DEVICE_REQUEST  DevReq;
  UINT32                  Timeout;

  if(UsbIo == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  EfiZeroMem ( &DevReq, sizeof(EFI_USB_DEVICE_REQUEST)) ;

  DevReq.RequestType = USB_DEV_GET_CONFIGURATION_REQ_TYPE ;
  DevReq.Request = USB_DEV_GET_CONFIGURATION ;
  DevReq.Value = 0 ;
  DevReq.Index = 0 ;
  DevReq.Length = 1 ;

  Timeout = 3000;
  return UsbIo->UsbControlTransfer(
                  UsbIo,
                  &DevReq,
                  EfiUsbDataIn,
                  Timeout,
                  ConfigValue,
                  1,
                  Status
                );
}

//
// Set device configuration
//
EFI_STATUS
UsbSetDeviceConfiguration (
  IN  EFI_USB_IO_PROTOCOL    *UsbIo,
  IN  UINT16                 Value,
  OUT UINT32                 *Status
  )
{
  EFI_USB_DEVICE_REQUEST  DevReq;
  UINT32                  Timeout;

  if(UsbIo == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  EfiZeroMem ( &DevReq, sizeof(EFI_USB_DEVICE_REQUEST)) ;

  DevReq.RequestType = USB_DEV_SET_CONFIGURATION_REQ_TYPE ;
  DevReq.Request = USB_DEV_SET_CONFIGURATION ;
  DevReq.Value = Value ;
  DevReq.Index = 0 ;
  DevReq.Length = 0 ;

  Timeout = 3000;
  return UsbIo->UsbControlTransfer(
                  UsbIo,
                  &DevReq,
                  EfiUsbNoData,
                  Timeout,
                  NULL,
                  0,
                  Status
               );
}

//
//  Set Device Feature
//
EFI_STATUS
UsbSetDeviceFeature (
  IN  EFI_USB_IO_PROTOCOL     *UsbIo,
  IN  EFI_USB_RECIPIENT       Recipient,
  IN  UINT16                  Value,
  IN  UINT16                  Target,
  OUT UINT32                  *Status
  )
{
  EFI_USB_DEVICE_REQUEST  DevReq;
  UINT32                  Timeout;

  if(UsbIo == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  EfiZeroMem ( &DevReq, sizeof(EFI_USB_DEVICE_REQUEST)) ;

  switch (Recipient) {
  case EfiUsbDevice:
    DevReq.RequestType = 0x00 ;
    break ;

  case EfiUsbInterface:
    DevReq.RequestType = 0x01 ;
    break ;

  case EfiUsbEndpoint:
    DevReq.RequestType = 0x02 ;
    break ;
  }

  //
  // Fill device request, see USB1.1 spec
  //
  DevReq.Request = USB_DEV_SET_FEATURE ;
  DevReq.Value = Value ;
  DevReq.Index = Target ;
  DevReq.Length = 0 ;

  Timeout = 3000;
  return UsbIo->UsbControlTransfer(
                  UsbIo,
                  &DevReq,
                  EfiUsbNoData,
                  Timeout,
                  NULL,
                  0,
                  Status
                );
}


//
// Clear Device Feature
//
EFI_STATUS
UsbClearDeviceFeature (
  IN  EFI_USB_IO_PROTOCOL     *UsbIo,
  IN  EFI_USB_RECIPIENT       Recipient,
  IN  UINT16                  Value,
  IN  UINT16                  Target,
  OUT UINT32                  *Status
  )
{
  EFI_USB_DEVICE_REQUEST  DevReq;
  UINT32                  Timeout;

  if(UsbIo == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  EfiZeroMem ( &DevReq, sizeof(EFI_USB_DEVICE_REQUEST)) ;

  switch (Recipient) {
  case EfiUsbDevice:
    DevReq.RequestType = 0x00 ;
    break ;

  case EfiUsbInterface:
    DevReq.RequestType = 0x01 ;
    break ;

  case EfiUsbEndpoint:
    DevReq.RequestType = 0x02 ;
    break ;
  }

  //
  // Fill device request, see USB1.1 spec
  //
  DevReq.Request = USB_DEV_CLEAR_FEATURE ;
  DevReq.Value = Value ;
  DevReq.Index = Target ;
  DevReq.Length = 0 ;

  Timeout = 3000;
  return UsbIo->UsbControlTransfer(
                  UsbIo,
                  &DevReq,
                  EfiUsbNoData,
                  Timeout,
                  NULL,
                  0,
                  Status
               );
}

//
//  Get Device Status
//
EFI_STATUS
UsbGetDeviceStatus (
  IN  EFI_USB_IO_PROTOCOL     *UsbIo,
  IN  EFI_USB_RECIPIENT       Recipient,
  IN  UINT16                  Target,
  OUT UINT16                  *DevStatus,
  OUT UINT32                  *Status
  )
{
  EFI_USB_DEVICE_REQUEST  DevReq;
  UINT32                  Timeout;

  if(UsbIo == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  EfiZeroMem ( &DevReq, sizeof(EFI_USB_DEVICE_REQUEST)) ;

  switch (Recipient) {
  case EfiUsbDevice:
    DevReq.RequestType = 0x80 ;
    break ;

  case EfiUsbInterface:
    DevReq.RequestType = 0x81 ;
    break ;

  case EfiUsbEndpoint:
    DevReq.RequestType = 0x82 ;
    break ;
  }

  //
  // Fill device request, see USB1.1 spec
  //
  DevReq.Request = USB_DEV_GET_STATUS ;
  DevReq.Value = 0 ;
  DevReq.Index = Target ;
  DevReq.Length = 2 ;

  Timeout = 3000;
  return UsbIo->UsbControlTransfer(
                  UsbIo,
                  &DevReq,
                  EfiUsbDataIn,
                  Timeout,
                  DevStatus,
                  2,
                  Status
               );
}

//
// Usb Get String
//
EFI_STATUS
UsbGetString(
  IN  EFI_USB_IO_PROTOCOL     *UsbIo,
  IN  UINT16                  LangID,
  IN  UINT8                   Index,
  IN  VOID                    *Buf,
  IN  UINTN                   BufSize,
  OUT UINT32                  *Status
  )
{
  UINT16 Value;

  if(UsbIo == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  //
  // Fill value, see USB1.1 spec
  //
  Value = (UINT16)((USB_DT_STRING << 8) | Index);

  return UsbGetDescriptor(
              UsbIo,
              Value,

⌨️ 快捷键说明

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