📄 efikey.c
字号:
/*++
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:
EfiKey.c
Abstract:
USB Keyboard Driver
Revision History
--*/
#include "efikey.h"
#include "keyboard.h"
//
// Prototypes
// Driver model protocol interface
//
EFI_STATUS
USBKeyboardDriverBindingEntryPoint (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
);
EFI_STATUS
USBKeyboardDriverBindingSupported (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Controller,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
);
EFI_STATUS
USBKeyboardDriverBindingStart (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Controller,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
);
EFI_STATUS
USBKeyboardDriverBindingStop (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Controller,
IN UINTN NumberOfChildren,
IN EFI_HANDLE *ChildHandleBuffer
);
//
// Simple Text In Protocol Interface
//
STATIC
EFI_STATUS
EFIAPI
USBKeyboardReset (
IN EFI_SIMPLE_TEXT_IN_PROTOCOL *This,
IN BOOLEAN ExtendedVerification
);
STATIC
EFI_STATUS
EFIAPI
USBKeyboardReadKeyStroke (
IN EFI_SIMPLE_TEXT_IN_PROTOCOL *This,
OUT EFI_INPUT_KEY *Key
);
STATIC
VOID
EFIAPI
USBKeyboardWaitForKey (
IN EFI_EVENT Event,
IN VOID *Context
);
// Helper functions
STATIC
EFI_STATUS
USBKeyboardCheckForKey (
IN USB_KB_DEV *UsbKeyboardDevice
);
//
// USB Keyboard Driver Global Variables
//
EFI_DRIVER_BINDING_PROTOCOL gUsbKeyboardDriverBinding = {
USBKeyboardDriverBindingSupported,
USBKeyboardDriverBindingStart,
USBKeyboardDriverBindingStop,
0x10,
NULL,
NULL
};
EFI_DRIVER_ENTRY_POINT(USBKeyboardDriverBindingEntryPoint)
EFI_STATUS
USBKeyboardDriverBindingEntryPoint (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
/*++
Routine Description:
Driver Entry Point.
Arguments:
(Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
Returns:
EFI_STATUS
--*/
{
return EfiLibInstallAllDriverProtocols (
ImageHandle,
SystemTable,
&gUsbKeyboardDriverBinding,
ImageHandle,
&gUsbKeyboardComponentName,
NULL,
NULL
);
}
EFI_STATUS
USBKeyboardDriverBindingSupported (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Controller,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
)
/*++
Routine Description:
Supported.
Arguments:
(Standard DriverBinding Protocol Supported() function)
Returns:
EFI_STATUS
--*/
{
EFI_STATUS OpenStatus;
EFI_USB_IO_PROTOCOL *UsbIo;
EFI_STATUS Status;
//
// Check if USB_IO protocol is attached on the controller handle.
//
OpenStatus = gBS->OpenProtocol (
Controller,
&gEfiUsbIoProtocolGuid,
&UsbIo,
This->DriverBindingHandle,
Controller,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (OpenStatus)) {
return OpenStatus;
}
//
// Use the USB I/O protocol interface to check whether the Controller is
// the Keyboard controller that can be managed by this driver.
//
Status = EFI_SUCCESS;
if(!IsUSBKeyboard(UsbIo)) {
Status = EFI_UNSUPPORTED;
}
gBS->CloseProtocol (
Controller,
&gEfiUsbIoProtocolGuid,
This->DriverBindingHandle,
Controller
);
return Status;
}
EFI_STATUS
USBKeyboardDriverBindingStart (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Controller,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
)
/*++
Routine Description:
Start.
Arguments:
(Standard DriverBinding Protocol Start() function)
Returns:
EFI_STATUS
--*/
{
EFI_STATUS Status;
EFI_USB_IO_PROTOCOL *UsbIo;
USB_KB_DEV *UsbKeyboardDevice;
UINT8 EndpointNumber;
EFI_USB_ENDPOINT_DESCRIPTOR EndpointDescriptor;
UINT8 Index;
UINT8 EndpointAddr;
UINT8 PollingInterval;
UINT8 PacketSize;
BOOLEAN Found;
UsbKeyboardDevice = NULL;
Found = FALSE;
//
// Open USB_IO Protocol
//
Status = gBS->OpenProtocol (
Controller,
&gEfiUsbIoProtocolGuid,
&UsbIo,
This->DriverBindingHandle,
Controller,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status)) {
return Status;
}
Status = gBS->AllocatePool(
EfiBootServicesData,
sizeof(USB_KB_DEV),
&UsbKeyboardDevice
);
if(EFI_ERROR(Status)) {
gBS->CloseProtocol (
Controller,
&gEfiUsbIoProtocolGuid,
This->DriverBindingHandle,
Controller
);
return Status;
}
EfiZeroMem(UsbKeyboardDevice,sizeof(USB_KB_DEV));
//
// Initialize UsbKeyboardDevice
//
UsbKeyboardDevice->UsbIo = UsbIo;
//
// Get interface & endpoint descriptor
//
UsbIo->UsbGetInterfaceDescriptor(
UsbIo,
&UsbKeyboardDevice->InterfaceDescriptor
);
EndpointNumber = UsbKeyboardDevice->InterfaceDescriptor.NumEndpoints;
for(Index = 0; Index < EndpointNumber; Index ++) {
UsbIo->UsbGetEndpointDescriptor(
UsbIo,
Index,
&EndpointDescriptor
);
if((EndpointDescriptor.Attributes & 0x03) == 0x03) {
//
// We only care interrupt endpoint here
//
UsbKeyboardDevice->IntEndpointDescriptor = EndpointDescriptor;
Found = TRUE;
}
}
if(!Found) {
//
// No interrupt endpoint found, then return unsupported.
//
gBS->FreePool(UsbKeyboardDevice);
gBS->CloseProtocol (
Controller,
&gEfiUsbIoProtocolGuid,
This->DriverBindingHandle,
Controller
);
return EFI_UNSUPPORTED;
}
UsbKeyboardDevice->Signature = USB_KB_DEV_SIGNATURE;
UsbKeyboardDevice->SimpleInput.Reset = USBKeyboardReset;
UsbKeyboardDevice->SimpleInput.ReadKeyStroke = USBKeyboardReadKeyStroke;
Status = gBS->CreateEvent (
EFI_EVENT_NOTIFY_WAIT,
EFI_TPL_NOTIFY,
USBKeyboardWaitForKey,
UsbKeyboardDevice,
&(UsbKeyboardDevice->SimpleInput.WaitForKey)
);
if(EFI_ERROR(Status)) {
gBS->FreePool(UsbKeyboardDevice);
gBS->CloseProtocol (
Controller,
&gEfiUsbIoProtocolGuid,
This->DriverBindingHandle,
Controller
);
return Status;
}
//
// Install simple txt in protocol interface
// for the usb keyboard device.
// Usb keyboard is a hot plug device, and expected to work immediately
// when plugging into system, so a HotPlugDeviceGuid is installed onto
// the usb keyboard device handle, to distinguish it from other conventional
// console devices.
//
Status = gBS->InstallMultipleProtocolInterfaces (
&Controller,
&gEfiSimpleTextInProtocolGuid,
&UsbKeyboardDevice->SimpleInput,
&gEfiHotPlugDeviceGuid,
NULL,
NULL
);
if (EFI_ERROR(Status)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -