/** @file\r
USB Mouse Driver that manages USB mouse and produces Absolute Pointer Protocol.\r
\r
-Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution. The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
\r
**/\r
\r
if (EFI_ERROR (Status)) {\r
return Status;\r
}\r
- \r
+\r
//\r
// Use the USB I/O Protocol interface to check whether Controller is\r
// a mouse device that can be managed by this driver.\r
if (!IsUsbMouse (UsbIo)) {\r
Status = EFI_UNSUPPORTED;\r
}\r
- \r
+\r
gBS->CloseProtocol (\r
Controller,\r
&gEfiUsbIoProtocolGuid,\r
This->DriverBindingHandle,\r
Controller\r
);\r
- \r
+\r
return Status;\r
}\r
\r
/**\r
Starts the mouse device with this driver.\r
\r
- This function consumes USB I/O Portocol, intializes USB mouse device,\r
+ This function consumes USB I/O Protocol, initializes USB mouse device,\r
installs Absolute Pointer Protocol, and submits Asynchronous Interrupt\r
Transfer to manage the USB mouse device.\r
\r
(VOID **) &UsbIo,\r
This->DriverBindingHandle,\r
Controller,\r
- EFI_OPEN_PROTOCOL_BY_DRIVER \r
+ EFI_OPEN_PROTOCOL_BY_DRIVER\r
);\r
if (EFI_ERROR (Status)) {\r
goto ErrorExit1;\r
}\r
- \r
+\r
UsbMouseAbsolutePointerDevice = AllocateZeroPool (sizeof (USB_MOUSE_ABSOLUTE_POINTER_DEV));\r
ASSERT (UsbMouseAbsolutePointerDevice != NULL);\r
\r
if (EFI_ERROR (Status)) {\r
goto ErrorExit;\r
}\r
+\r
+ //\r
+ // Report Status Code here since USB mouse will be detected next.\r
+ //\r
+ REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
+ EFI_PROGRESS_CODE,\r
+ (EFI_PERIPHERAL_MOUSE | EFI_P_PC_PRESENCE_DETECT),\r
+ UsbMouseAbsolutePointerDevice->DevicePath\r
+ );\r
+\r
//\r
// Get interface & endpoint descriptor\r
//\r
EndpointNumber = UsbMouseAbsolutePointerDevice->InterfaceDescriptor.NumEndpoints;\r
\r
//\r
- // Traverse endpoints to find interrupt endpoint\r
+ // Traverse endpoints to find interrupt endpoint IN\r
//\r
Found = FALSE;\r
for (Index = 0; Index < EndpointNumber; Index++) {\r
&EndpointDescriptor\r
);\r
\r
- if ((EndpointDescriptor.Attributes & (BIT0 | BIT1)) == USB_ENDPOINT_INTERRUPT) {\r
+ if (((EndpointDescriptor.Attributes & (BIT0 | BIT1)) == USB_ENDPOINT_INTERRUPT) &&\r
+ ((EndpointDescriptor.EndpointAddress & USB_ENDPOINT_DIR_IN) != 0)) {\r
//\r
// We only care interrupt endpoint here\r
//\r
\r
if (!Found) {\r
//\r
+ // Report Status Code to indicate that there is no USB mouse\r
+ //\r
+ REPORT_STATUS_CODE (\r
+ EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
+ (EFI_PERIPHERAL_MOUSE | EFI_P_EC_NOT_DETECTED)\r
+ );\r
+ //\r
// No interrupt endpoint found, then return unsupported.\r
//\r
Status = EFI_UNSUPPORTED;\r
goto ErrorExit;\r
}\r
\r
+ //\r
+ // Report Status Code here since USB mouse has be detected.\r
+ //\r
+ REPORT_STATUS_CODE_WITH_DEVICE_PATH (\r
+ EFI_PROGRESS_CODE,\r
+ (EFI_PERIPHERAL_MOUSE | EFI_P_PC_DETECTED),\r
+ UsbMouseAbsolutePointerDevice->DevicePath\r
+ );\r
+\r
Status = InitializeUsbMouseDevice (UsbMouseAbsolutePointerDevice);\r
if (EFI_ERROR (Status)) {\r
//\r
// Initialize and install EFI Absolute Pointer Protocol.\r
//\r
UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol.GetState = GetMouseAbsolutePointerState;\r
- UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol.Reset = UsbMouseAbsolutePointerReset;\r
- UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol.Mode = &UsbMouseAbsolutePointerDevice->Mode;\r
+ UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol.Reset = UsbMouseAbsolutePointerReset;\r
+ UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol.Mode = &UsbMouseAbsolutePointerDevice->Mode;\r
\r
Status = gBS->CreateEvent (\r
EVT_NOTIFY_WAIT,\r
// Free all resources.\r
//\r
gBS->CloseEvent (UsbMouseAbsolutePointerDevice->AbsolutePointerProtocol.WaitForInput);\r
- \r
+\r
if (UsbMouseAbsolutePointerDevice->DelayedRecoveryEvent != NULL) {\r
gBS->CloseEvent (UsbMouseAbsolutePointerDevice->DelayedRecoveryEvent);\r
UsbMouseAbsolutePointerDevice->DelayedRecoveryEvent = NULL;\r
IN USB_MOUSE_ABSOLUTE_POINTER_DEV *UsbMouseAbsolutePointerDev\r
)\r
{\r
- EFI_USB_IO_PROTOCOL *UsbIo;\r
- UINT8 Protocol;\r
- EFI_STATUS Status;\r
- EFI_USB_HID_DESCRIPTOR MouseHidDesc;\r
- UINT8 *ReportDesc;\r
- UINT8 ReportId;\r
- UINT8 Duration;\r
+ EFI_USB_IO_PROTOCOL *UsbIo;\r
+ UINT8 Protocol;\r
+ EFI_STATUS Status;\r
+ EFI_USB_HID_DESCRIPTOR *MouseHidDesc;\r
+ UINT8 *ReportDesc;\r
+ EFI_USB_CONFIG_DESCRIPTOR ConfigDesc;\r
+ VOID *Buf;\r
+ UINT32 TransferResult;\r
+ UINT16 Total;\r
+ USB_DESC_HEAD *Head;\r
+ BOOLEAN Start;\r
\r
UsbIo = UsbMouseAbsolutePointerDev->UsbIo;\r
\r
//\r
- // Get HID descriptor\r
+ // Get the current configuration descriptor. Note that it doesn't include other descriptors.\r
//\r
- Status = UsbGetHidDescriptor (\r
+ Status = UsbIo->UsbGetConfigDescriptor (\r
+ UsbIo,\r
+ &ConfigDesc\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // By issuing Get_Descriptor(Configuration) request with total length, we get the Configuration descriptor,\r
+ // all Interface descriptors, all Endpoint descriptors, and the HID descriptor for each interface.\r
+ //\r
+ Buf = AllocateZeroPool (ConfigDesc.TotalLength);\r
+ if (Buf == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ Status = UsbGetDescriptor (\r
UsbIo,\r
- UsbMouseAbsolutePointerDev->InterfaceDescriptor.InterfaceNumber,\r
- &MouseHidDesc\r
+ (UINT16)((USB_DESC_TYPE_CONFIG << 8) | (ConfigDesc.ConfigurationValue - 1)),\r
+ 0,\r
+ ConfigDesc.TotalLength,\r
+ Buf,\r
+ &TransferResult\r
);\r
if (EFI_ERROR (Status)) {\r
+ FreePool (Buf);\r
return Status;\r
}\r
\r
+ Total = 0;\r
+ Start = FALSE;\r
+ Head = (USB_DESC_HEAD *)Buf;\r
+ MouseHidDesc = NULL;\r
+\r
+ //\r
+ // Get HID descriptor from the receipt of Get_Descriptor(Configuration) request.\r
+ // This algorithm is based on the fact that the HID descriptor shall be interleaved\r
+ // between the interface and endpoint descriptors for HID interfaces.\r
+ //\r
+ while (Total < ConfigDesc.TotalLength) {\r
+ if (Head->Type == USB_DESC_TYPE_INTERFACE) {\r
+ if ((((USB_INTERFACE_DESCRIPTOR *)Head)->InterfaceNumber == UsbMouseAbsolutePointerDev->InterfaceDescriptor.InterfaceNumber) &&\r
+ (((USB_INTERFACE_DESCRIPTOR *)Head)->AlternateSetting == UsbMouseAbsolutePointerDev->InterfaceDescriptor.AlternateSetting)) {\r
+ Start = TRUE;\r
+ }\r
+ }\r
+ if (Start && (Head->Type == USB_DESC_TYPE_ENDPOINT)) {\r
+ break;\r
+ }\r
+ if (Start && (Head->Type == USB_DESC_TYPE_HID)) {\r
+ MouseHidDesc = (EFI_USB_HID_DESCRIPTOR *)Head;\r
+ break;\r
+ }\r
+ Total = Total + (UINT16)Head->Len;\r
+ Head = (USB_DESC_HEAD*)((UINT8 *)Buf + Total);\r
+ }\r
+\r
+ if (MouseHidDesc == NULL) {\r
+ FreePool (Buf);\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
//\r
// Get report descriptor\r
//\r
- if (MouseHidDesc.HidClassDesc[0].DescriptorType != USB_DESC_TYPE_REPORT) {\r
+ if (MouseHidDesc->HidClassDesc[0].DescriptorType != USB_DESC_TYPE_REPORT) {\r
+ FreePool (Buf);\r
return EFI_UNSUPPORTED;\r
}\r
\r
- ReportDesc = AllocateZeroPool (MouseHidDesc.HidClassDesc[0].DescriptorLength);\r
+ ReportDesc = AllocateZeroPool (MouseHidDesc->HidClassDesc[0].DescriptorLength);\r
ASSERT (ReportDesc != NULL);\r
\r
Status = UsbGetReportDescriptor (\r
UsbIo,\r
UsbMouseAbsolutePointerDev->InterfaceDescriptor.InterfaceNumber,\r
- MouseHidDesc.HidClassDesc[0].DescriptorLength,\r
+ MouseHidDesc->HidClassDesc[0].DescriptorLength,\r
ReportDesc\r
);\r
\r
if (EFI_ERROR (Status)) {\r
+ FreePool (Buf);\r
FreePool (ReportDesc);\r
return Status;\r
}\r
Status = ParseMouseReportDescriptor (\r
UsbMouseAbsolutePointerDev,\r
ReportDesc,\r
- MouseHidDesc.HidClassDesc[0].DescriptorLength\r
+ MouseHidDesc->HidClassDesc[0].DescriptorLength\r
);\r
\r
if (EFI_ERROR (Status)) {\r
+ FreePool (Buf);\r
FreePool (ReportDesc);\r
return Status;\r
}\r
UsbMouseAbsolutePointerDev->Mode.AbsoluteMinY = 0;\r
UsbMouseAbsolutePointerDev->Mode.AbsoluteMinZ = 0;\r
UsbMouseAbsolutePointerDev->Mode.Attributes = 0x3;\r
- \r
+\r
+ //\r
+ // Let the cursor's starting position is in the center of the screen.\r
+ //\r
+ UsbMouseAbsolutePointerDev->State.CurrentX =\r
+ DivU64x32 (UsbMouseAbsolutePointerDev->Mode.AbsoluteMaxX + UsbMouseAbsolutePointerDev->Mode.AbsoluteMinX, 2);\r
+ UsbMouseAbsolutePointerDev->State.CurrentY =\r
+ DivU64x32 (UsbMouseAbsolutePointerDev->Mode.AbsoluteMaxY + UsbMouseAbsolutePointerDev->Mode.AbsoluteMinY, 2);\r
+\r
//\r
// Set boot protocol for the USB mouse.\r
// This driver only supports boot protocol.\r
);\r
\r
if (EFI_ERROR (Status)) {\r
+ FreePool (Buf);\r
FreePool (ReportDesc);\r
return Status;\r
}\r
}\r
\r
- //\r
- // ReportId is zero, which means the idle rate applies to all input reports.\r
- //\r
- ReportId = 0;\r
- //\r
- // Duration is zero, which means the duration is infinite.\r
- // so the endpoint will inhibit reporting forever,\r
- // and only reporting when a change is detected in the report data.\r
- //\r
- Duration = 0;\r
- UsbSetIdleRequest (\r
- UsbIo,\r
- UsbMouseAbsolutePointerDev->InterfaceDescriptor.InterfaceNumber,\r
- ReportId,\r
- Duration\r
- );\r
-\r
+ FreePool (Buf);\r
FreePool (ReportDesc);\r
\r
//\r
\r
//\r
// Delete & Submit this interrupt again\r
- // Handler of DelayedRecoveryEvent triggered by timer will re-submit the interrupt. \r
+ // Handler of DelayedRecoveryEvent triggered by timer will re-submit the interrupt.\r
//\r
UsbIo->UsbAsyncInterruptTransfer (\r
UsbIo,\r
return EFI_SUCCESS;\r
}\r
\r
- UsbMouseAbsolutePointerDevice->StateChanged = TRUE;\r
-\r
//\r
// Check mouse Data\r
// USB HID Specification specifies following data format:\r
// 2 0 to 7 Y displacement\r
// 3 to n 0 to 7 Device specific (optional)\r
//\r
- UsbMouseAbsolutePointerDevice->State.CurrentX += *((INT8 *) Data + 1);\r
- UsbMouseAbsolutePointerDevice->State.CurrentY += *((INT8 *) Data + 2);\r
- \r
+ if (DataLength < 3) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ UsbMouseAbsolutePointerDevice->StateChanged = TRUE;\r
+\r
+ UsbMouseAbsolutePointerDevice->State.ActiveButtons = *(UINT8 *) Data & (BIT0 | BIT1 | BIT2);\r
+\r
+ UsbMouseAbsolutePointerDevice->State.CurrentX =\r
+ MIN (\r
+ MAX ((INT64) UsbMouseAbsolutePointerDevice->State.CurrentX + *((INT8 *) Data + 1),\r
+ (INT64) UsbMouseAbsolutePointerDevice->Mode.AbsoluteMinX),\r
+ (INT64) UsbMouseAbsolutePointerDevice->Mode.AbsoluteMaxX\r
+ );\r
+ UsbMouseAbsolutePointerDevice->State.CurrentY =\r
+ MIN (\r
+ MAX ((INT64) UsbMouseAbsolutePointerDevice->State.CurrentY + *((INT8 *) Data + 2),\r
+ (INT64) UsbMouseAbsolutePointerDevice->Mode.AbsoluteMinY),\r
+ (INT64) UsbMouseAbsolutePointerDevice->Mode.AbsoluteMaxY\r
+ );\r
if (DataLength > 3) {\r
- UsbMouseAbsolutePointerDevice->State.CurrentZ += *((INT8 *) Data + 3);\r
+ UsbMouseAbsolutePointerDevice->State.CurrentZ =\r
+ MIN (\r
+ MAX ((INT64) UsbMouseAbsolutePointerDevice->State.CurrentZ + *((INT8 *) Data + 1),\r
+ (INT64) UsbMouseAbsolutePointerDevice->Mode.AbsoluteMinZ),\r
+ (INT64) UsbMouseAbsolutePointerDevice->Mode.AbsoluteMaxZ\r
+ );\r
}\r
- UsbMouseAbsolutePointerDevice->State.ActiveButtons = *(UINT8 *) Data & (BIT0 | BIT1);\r
\r
return EFI_SUCCESS;\r
}\r
/**\r
Retrieves the current state of a pointer device.\r
\r
- @param This A pointer to the EFI_ABSOLUTE_POINTER_PROTOCOL instance. \r
+ @param This A pointer to the EFI_ABSOLUTE_POINTER_PROTOCOL instance.\r
@param MouseState A pointer to the state information on the pointer device.\r
\r
@retval EFI_SUCCESS The state of the pointer device was returned in State.\r
@retval EFI_NOT_READY The state of the pointer device has not changed since the last call to\r
- GetState(). \r
+ GetState().\r
@retval EFI_DEVICE_ERROR A device error occurred while attempting to retrieve the pointer device's\r
- current state. \r
- @retval EFI_INVALID_PARAMETER State is NULL. \r
+ current state.\r
+ @retval EFI_INVALID_PARAMETER State is NULL.\r
\r
**/\r
EFI_STATUS\r
sizeof (EFI_ABSOLUTE_POINTER_STATE)\r
);\r
\r
- //\r
- // Clear previous move state\r
- //\r
- MouseAbsolutePointerDev->State.CurrentX = 0;\r
- MouseAbsolutePointerDev->State.CurrentY = 0;\r
- MouseAbsolutePointerDev->State.CurrentZ = 0;\r
- MouseAbsolutePointerDev->State.ActiveButtons = 0;\r
-\r
MouseAbsolutePointerDev->StateChanged = FALSE;\r
\r
return EFI_SUCCESS;\r
&UsbMouseAbsolutePointerDevice->State,\r
sizeof (EFI_ABSOLUTE_POINTER_STATE)\r
);\r
+\r
+ //\r
+ // Let the cursor's starting position is in the center of the screen.\r
+ //\r
+ UsbMouseAbsolutePointerDevice->State.CurrentX =\r
+ DivU64x32 (UsbMouseAbsolutePointerDevice->Mode.AbsoluteMaxX + UsbMouseAbsolutePointerDevice->Mode.AbsoluteMinX, 2);\r
+ UsbMouseAbsolutePointerDevice->State.CurrentY =\r
+ DivU64x32 (UsbMouseAbsolutePointerDevice->Mode.AbsoluteMaxY + UsbMouseAbsolutePointerDevice->Mode.AbsoluteMinY, 2);\r
+\r
UsbMouseAbsolutePointerDevice->StateChanged = FALSE;\r
\r
return EFI_SUCCESS;\r